ldlinux: Fix serial output and delete eprintf()
[syslinux.git] / com32 / elflink / ldlinux / setadv.c
blob2e386213696073db7564b772aa8bfd65a55ba209
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
15 * The above copyright notice and this permission notice shall
16 * be included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
27 * ----------------------------------------------------------------------- */
30 * syslinux/setadv.c
32 * (Over)write a data item in the auxilliary data vector. To
33 * delete an item, set its length to zero.
35 * Return 0 on success, -1 on error, and set errno.
37 * NOTE: Data is not written to disk unless
38 * syslinux_adv_write() is called.
41 #include <syslinux/adv.h>
42 #include <klibc/compiler.h>
43 #include <inttypes.h>
44 #include <string.h>
45 #include <errno.h>
46 #include <alloca.h>
48 __export int syslinux_setadv(int tag, size_t size, const void *data)
50 uint8_t *p, *advtmp;
51 size_t rleft, left;
53 if ((unsigned)tag - 1 > 254) {
54 errno = EINVAL;
55 return -1; /* Impossible tag value */
58 if (size > 255) {
59 errno = ENOSPC; /* Max 255 bytes for a data item */
60 return -1;
63 rleft = left = syslinux_adv_size();
64 p = advtmp = alloca(left);
65 memcpy(p, syslinux_adv_ptr(), left); /* Make working copy */
67 while (rleft >= 2) {
68 uint8_t ptag = p[0];
69 size_t plen = p[1] + 2;
71 if (ptag == ADV_END)
72 break;
74 if (ptag == tag) {
75 /* Found our tag. Delete it. */
77 if (plen >= rleft) {
78 /* Entire remainder is our tag */
79 break;
81 memmove(p, p + plen, rleft - plen);
82 rleft -= plen; /* Fewer bytes to read, but not to write */
83 } else {
84 /* Not our tag */
85 if (plen > rleft)
86 break; /* Corrupt tag (overrun) - overwrite it */
88 left -= plen;
89 rleft -= plen;
90 p += plen;
94 /* Now (p, left) reflects the position to write in and how much space
95 we have for our data. */
97 if (size) {
98 if (left < size + 2) {
99 errno = ENOSPC; /* Not enough space for data */
100 return -1;
103 *p++ = tag;
104 *p++ = size;
105 memcpy(p, data, size);
106 p += size;
107 left -= size + 2;
110 memset(p, 0, left);
112 /* If we got here, everything went OK, commit the write to low memory */
113 memcpy(syslinux_adv_ptr(), advtmp, syslinux_adv_size());
115 return 0;