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
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 * ----------------------------------------------------------------------- */
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>
48 int syslinux_setadv(int tag
, size_t size
, const void *data
)
53 if ((unsigned)tag
- 1 > 254) {
55 return -1; /* Impossible tag value */
59 errno
= ENOSPC
; /* Max 255 bytes for a data item */
63 rleft
= left
= syslinux_adv_size();
64 p
= advtmp
= alloca(left
);
65 memcpy(p
, syslinux_adv_ptr(), left
); /* Make working copy */
69 size_t plen
= p
[1] + 2;
75 /* Found our tag. Delete it. */
78 /* Entire remainder is our tag */
81 memmove(p
, p
+ plen
, rleft
- plen
);
82 rleft
-= plen
; /* Fewer bytes to read, but not to write */
86 break; /* Corrupt tag (overrun) - overwrite it */
94 /* Now (p, left) reflects the position to write in and how much space
95 we have for our data. */
98 if (left
< size
+ 2) {
99 errno
= ENOSPC
; /* Not enough space for data */
105 memcpy(p
, data
, size
);
112 /* If we got here, everything went OK, commit the write to low memory */
113 memcpy(syslinux_adv_ptr(), advtmp
, syslinux_adv_size());