1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
16 * (Over)write a data item in the auxilliary data vector. To
17 * delete an item, set its length to zero.
19 * Return 0 on success, -1 on error, and set errno.
33 #include <sys/ioctl.h>
35 #include <sys/types.h>
39 unsigned char syslinux_adv[2 * ADV_SIZE];
41 #define ADV_MAGIC1 0x5a2d2fa5 /* Head signature */
42 #define ADV_MAGIC2 0xa3041767 /* Total checksum */
43 #define ADV_MAGIC3 0xdd28bf64 /* Tail signature */
45 static void cleanup_adv(unsigned char *advbuf)
50 /* Make sure both copies agree, and update the checksum */
51 set_32((uint32_t *) advbuf, ADV_MAGIC1);
54 for (i = 8; i < ADV_SIZE - 4; i += 4)
55 csum -= get_32((uint32_t *) (advbuf + i));
57 set_32((uint32_t *) (advbuf + 4), csum);
58 set_32((uint32_t *) (advbuf + ADV_SIZE - 4), ADV_MAGIC3);
60 memcpy(advbuf + ADV_SIZE, advbuf, ADV_SIZE);
63 int syslinux_setadv(int tag, size_t size, const void *data)
67 uint8_t advtmp[ADV_SIZE];
69 if ((unsigned)tag - 1 > 254) {
71 return -1; /* Impossible tag value */
75 errno = ENOSPC; /* Max 255 bytes for a data item */
81 memcpy(p, syslinux_adv + 2 * 4, left); /* Make working copy */
85 size_t plen = p[1] + 2;
91 /* Found our tag. Delete it. */
94 /* Entire remainder is our tag */
97 memmove(p, p + plen, left - plen);
101 break; /* Corrupt tag (overrun) - overwrite it */
108 /* Now (p, left) reflects the position to write in and how much space
109 we have for our data. */
112 if (left < size + 2) {
113 errno = ENOSPC; /* Not enough space for data */
119 memcpy(p, data, size);
126 /* If we got here, everything went OK, commit the write */
127 memcpy(syslinux_adv + 2 * 4, advtmp, ADV_LEN);
128 cleanup_adv(syslinux_adv);
133 void syslinux_reset_adv(unsigned char *advbuf)
135 /* Create an all-zero ADV */
136 memset(advbuf + 2 * 4, 0, ADV_LEN);
140 static int adv_consistent(const unsigned char *p)
145 if (get_32((uint32_t *) p) != ADV_MAGIC1 ||
146 get_32((uint32_t *) (p + ADV_SIZE - 4)) != ADV_MAGIC3)
150 for (i = 4; i < ADV_SIZE - 4; i += 4)
151 csum += get_32((uint32_t *) (p + i));
153 return csum == ADV_MAGIC2;
157 * Verify that an in-memory ADV is consistent, making the copies consistent.
158 * If neither copy is OK, return -1 and call syslinux_reset_adv().
160 int syslinux_validate_adv(unsigned char *advbuf)
162 if (adv_consistent(advbuf + 0 * ADV_SIZE)) {
163 memcpy(advbuf + ADV_SIZE, advbuf, ADV_SIZE);
165 } else if (adv_consistent(advbuf + 1 * ADV_SIZE)) {
166 memcpy(advbuf, advbuf + ADV_SIZE, ADV_SIZE);
169 syslinux_reset_adv(advbuf);
175 * Read the ADV from an existing instance, or initialize if invalid.
176 * Returns -1 on fatal errors, 0 if ADV is okay, and 1 if no valid
179 int read_adv(const char *path, const char *cfg)
186 asprintf(&file, "%s%s%s",
187 path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg);
194 fd = open(file, O_RDONLY);
196 if (errno != ENOENT) {
199 syslinux_reset_adv(syslinux_adv);
201 } else if (fstat(fd, &st)) {
203 } else if (st.st_size < 2 * ADV_SIZE) {
204 /* Too small to be useful */
205 syslinux_reset_adv(syslinux_adv);
206 err = 0; /* Nothing to read... */
207 } else if (xpread(fd, syslinux_adv, 2 * ADV_SIZE,
208 st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) {
211 /* We got it... maybe? */
212 err = syslinux_validate_adv(syslinux_adv) ? 1 : 0;
227 * Update the ADV in an existing installation.
229 int write_adv(const char *path, const char *cfg)
231 unsigned char advtmp[2 * ADV_SIZE];
237 err = asprintf(&file, "%s%s%s",
238 path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg);
245 fd = open(file, O_RDONLY);
248 } else if (fstat(fd, &st)) {
250 } else if (st.st_size < 2 * ADV_SIZE) {
251 /* Too small to be useful */
253 } else if (xpread(fd, advtmp, 2 * ADV_SIZE,
254 st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) {
257 /* We got it... maybe? */
258 err = syslinux_validate_adv(advtmp) ? -2 : 0;
260 /* Got a good one, write our own ADV here */
261 clear_attributes(fd);
263 /* Need to re-open read-write */
265 fd = open(file, O_RDWR | O_SYNC);
268 } else if (fstat(fd, &xst) || xst.st_ino != st.st_ino ||
269 xst.st_dev != st.st_dev || xst.st_size != st.st_size) {
270 fprintf(stderr, "%s: race condition on write\n", file);
273 /* Write our own version ... */
274 if (xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
275 st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) {
285 fprintf(stderr, "%s: cannot write auxilliary data (need --update)?\n",