1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2010 Intel Corporation; author: H. Peter Anvin
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
19 * Return 0 on success, -1 on error, and set errno.
33 #include <sys/types.h>
38 * Read the ADV from an existing instance, or initialize if invalid.
39 * Returns -1 on fatal errors, 0 if ADV is okay, and 1 if no valid
42 int read_adv(const char *path, const char *cfg)
49 asprintf(&file, "%s%s%s",
50 path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg);
57 fd = open(file, O_RDONLY);
59 if (errno != ENOENT) {
62 syslinux_reset_adv(syslinux_adv);
64 } else if (fstat(fd, &st)) {
66 } else if (st.st_size < 2 * ADV_SIZE) {
67 /* Too small to be useful */
68 syslinux_reset_adv(syslinux_adv);
69 err = 0; /* Nothing to read... */
70 } else if (xpread(fd, syslinux_adv, 2 * ADV_SIZE,
71 st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) {
74 /* We got it... maybe? */
75 err = syslinux_validate_adv(syslinux_adv) ? 1 : 0;
90 * Update the ADV in an existing installation.
92 int write_adv(const char *path, const char *cfg)
94 unsigned char advtmp[2 * ADV_SIZE];
100 err = asprintf(&file, "%s%s%s",
101 path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg);
108 fd = open(file, O_RDONLY);
111 } else if (fstat(fd, &st)) {
113 } else if (st.st_size < 2 * ADV_SIZE) {
114 /* Too small to be useful */
116 } else if (xpread(fd, advtmp, 2 * ADV_SIZE,
117 st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) {
120 /* We got it... maybe? */
121 err = syslinux_validate_adv(advtmp) ? -2 : 0;
123 /* Got a good one, write our own ADV here */
124 clear_attributes(fd);
126 /* Need to re-open read-write */
128 fd = open(file, O_RDWR | O_SYNC);
131 } else if (fstat(fd, &xst) || xst.st_ino != st.st_ino ||
132 xst.st_dev != st.st_dev || xst.st_size != st.st_size) {
133 fprintf(stderr, "%s: race condition on write\n", file);
136 /* Write our own version ... */
137 if (xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
138 st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) {
148 fprintf(stderr, "%s: cannot write auxilliary data (need --update)?\n",