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.
33 * Copyright (c) 1999 Robert Nordier
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
49 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
50 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
51 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
52 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
54 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
55 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/diskpc98.h>
75 #define BOOTSIZE 0x2000
76 #define IPLSIZE 512 /* IPL size */
77 #define BOOTMENUSIZE 7168 /* Max HDD boot menu size */
78 #define BOOTMENUOFF 0x400
80 u_char boot0buf[BOOTSIZE];
82 u_char menu[BOOTMENUSIZE];
84 static int read_boot(const char *, u_char *);
85 static int write_boot(const char *, u_char *);
86 static char *mkrdev(const char *);
87 static void usage(void);
90 * Boot manager installation/configuration utility.
93 main(int argc, char *argv[])
96 const char *iplpath = "/boot/boot0", *menupath = "/boot/boot0.5";
97 char *iplbakpath = NULL, *menubakpath = NULL;
104 int v_flag = 0, version;
106 while ((c = getopt(argc, argv, "BF:f:i:m:s:v:")) != -1) {
112 menubakpath = optarg;
124 secsize = strtol(optarg, &endptr, 0);
125 if (errno || *optarg == '\0' || *endptr)
126 errx(1, "%s: Bad argument to -s option",
135 errx(1, "%s: unsupported sector size", optarg);
141 version = strtol(optarg, &endptr, 0);
142 if (errno || *optarg == '\0' || *endptr ||
143 version < 0 || version > 255)
144 errx(1, "%s: Bad argument to -v option",
157 disk = mkrdev(*argv);
159 read_boot(disk, boot0buf);
161 if (iplbakpath != NULL) {
162 fd1 = open(iplbakpath, O_WRONLY | O_CREAT | O_TRUNC, 0666);
164 err(1, "%s", iplbakpath);
165 n = write(fd1, boot0buf, IPLSIZE);
167 err(1, "%s", iplbakpath);
169 errx(1, "%s: short write", iplbakpath);
173 if (menubakpath != NULL) {
174 fd1 = open(menubakpath, O_WRONLY | O_CREAT | O_TRUNC, 0666);
176 err(1, "%s", menubakpath);
177 n = write(fd1, boot0buf + BOOTMENUOFF, BOOTMENUSIZE);
179 err(1, "%s", menubakpath);
180 if (n != BOOTMENUSIZE)
181 errx(1, "%s: short write", menubakpath);
186 /* Read IPL (boot0). */
187 fd1 = open(iplpath, O_RDONLY);
190 n = read(fd1, ipl, IPLSIZE);
192 err(1, "%s", iplpath);
194 errx(1, "%s: invalid file", iplpath);
197 /* Read HDD boot menu (boot0.5). */
198 fd1 = open(menupath, O_RDONLY);
201 n = read(fd1, menu, BOOTMENUSIZE);
203 err(1, "%s", menupath);
204 if (n != BOOTMENUSIZE)
205 errx(1, "%s: invalid file", menupath);
208 memcpy(boot0buf, ipl, IPLSIZE);
209 memcpy(boot0buf + BOOTMENUOFF, menu, BOOTMENUSIZE);
212 /* Set version number field. */
214 *(boot0buf + secsize - 4) = (u_char)version;
216 if (B_flag || v_flag)
217 write_boot(disk, boot0buf);
223 read_boot(const char *disk, u_char *boot)
227 /* Read IPL, partition table and HDD boot menu. */
228 fd = open(disk, O_RDONLY);
231 n = read(fd, boot, BOOTSIZE);
233 errx(1, "%s: short read", disk);
240 write_boot(const char *disk, u_char *boot)
243 char buf[MAXPATHLEN];
245 struct gctl_req *grq;
247 fd = open(disk, O_WRONLY, 0666);
249 if ((n = write(fd, boot, BOOTSIZE)) < 0)
252 errx(1, "%s: short write", disk);
257 grq = gctl_get_handle();
258 gctl_ro_param(grq, "verb", -1, "write PC98");
259 gctl_ro_param(grq, "class", -1, "PC98");
260 q = strrchr(disk, '/');
265 gctl_ro_param(grq, "geom", -1, q);
266 gctl_ro_param(grq, "data", BOOTSIZE, boot);
271 warnx("%s: %s", disk, q);
274 for (i = 0; i < NDOSPART; i++) {
275 snprintf(buf, sizeof(buf), "%ss%d", disk, i + 1);
276 fd = open(buf, O_RDONLY);
279 n = ioctl(fd, DIOCSPC98, boot);
281 err(1, "%s: ioctl DIOCSPC98", disk);
290 * Produce a device path for a "canonical" name, where appropriate.
293 mkrdev(const char *fname)
295 char buf[MAXPATHLEN];
298 if (!strchr(fname, '/')) {
299 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
305 errx(1, "No more memory");
310 * Display usage information.
316 "boot98cfg [-B][-i boot0][-m boot0.5][-s secsize][-v version]\n"
317 " [-f ipl.bak][-F menu.bak] disk\n");