2 * Copyright (c) KATO Takenori, 2000.
4 * All rights reserved. Unpublished rights reserved under the copyright
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Copyright (c) 1999 Robert Nordier
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
51 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
52 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
53 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
54 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
55 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
56 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
57 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 #include <sys/param.h>
72 #define IPLSIZE 512 /* IPL size */
73 #define BOOTMENUSIZE 7168 /* Max HDD boot menu size */
74 #define BOOTMENUOFF 0x400
76 static char *mkrdev(char *);
77 static void usage(void);
79 u_char boot0buf[0x2000];
81 char menu[BOOTMENUSIZE];
84 * Produce a device path for a "canonical" name, where appropriate.
94 if (!strchr(fname, '/')) {
95 snprintf(buf, sizeof(buf), "%sr%s", _PATH_DEV, fname);
97 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
98 if (!(s = strdup(buf)))
108 "boot98cfg [-B][-i boot0][-m boot0.5][-s secsize][-v version]\n"
109 " [-f ipl.bak][-F menu.bak] disk\n");
114 main(int argc, char *argv[])
117 const char *iplpath = "/boot/boot0", *menupath = "/boot/boot0.5";
118 char *iplbakpath = NULL, *menubakpath = NULL;
125 int v_flag = 0, version;
127 while ((c = getopt(argc, argv, "BF:f:i:m:s:v:")) != -1) {
133 menubakpath = optarg;
145 secsize = strtol(optarg, &endptr, 0);
146 if (errno || *optarg == NULL || *endptr)
147 errx(1, "%s: Bad argument to -s option",
156 errx(1, "%s: unsupported sector size", optarg);
162 version = strtol(optarg, &endptr, 0);
163 if (errno || *optarg == NULL || *endptr ||
164 version < 0 || version > 255)
165 errx(1, "%s: Bad argument to -s option",
178 disk = mkrdev(*argv);
180 /* Read IPL, partition table and HDD boot menu. */
181 fd = open(disk, O_RDWR);
184 n = read(fd, boot0buf, 0x2000);
186 errx(1, "%s: short read", disk);
187 if (!B_flag && !v_flag)
190 if (iplbakpath != NULL) {
191 fd1 = open(iplbakpath, O_WRONLY | O_CREAT | O_TRUNC, 0666);
193 err(1, "%s", iplbakpath);
194 n = write(fd1, boot0buf, IPLSIZE);
196 err(1, "%s", iplbakpath);
198 errx(1, "%s: short write", iplbakpath);
202 if (menubakpath != NULL) {
203 fd1 = open(menubakpath, O_WRONLY | O_CREAT | O_TRUNC, 0666);
205 err(1, "%s", menubakpath);
206 n = write(fd1, boot0buf + BOOTMENUOFF, BOOTMENUSIZE);
208 err(1, "%s", menubakpath);
209 if (n != BOOTMENUSIZE)
210 errx(1, "%s: short write", menubakpath);
215 /* Read IPL (boot0). */
216 fd1 = open(iplpath, O_RDONLY);
219 n = read(fd1, ipl, IPLSIZE);
221 err(1, "%s", iplpath);
223 errx(1, "%s: invalid file", iplpath);
226 /* Read HDD boot menu (boot0.5). */
227 fd1 = open(menupath, O_RDONLY);
230 n = read(fd1, menu, BOOTMENUSIZE);
232 err(1, "%s", menupath);
233 if (n != BOOTMENUSIZE)
234 errx(1, "%s: invalid file", menupath);
237 memcpy(boot0buf, ipl, IPLSIZE);
238 memcpy(boot0buf + BOOTMENUOFF, menu, BOOTMENUSIZE);
241 /* Set version number field. */
243 *(boot0buf + secsize - 4) = (u_char)version;
245 if (B_flag || v_flag) {
246 if (lseek(fd, 0, SEEK_SET) == -1 ||
247 (n = write(fd, boot0buf, 0x2000)) < 0 ||
251 errx(1, "%s: short write", disk);