1 /* $NetBSD: installboot.c,v 1.36 2011/11/03 20:46:41 martin Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn of Wasabi Systems.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: installboot.c,v 1.36 2011/11/03 20:46:41 martin Exp $");
41 #include <sys/ioctl.h>
42 #include <sys/utsname.h>
43 #include <sys/ttycom.h>
55 #include "installboot.h"
57 static void getmachine(ib_params
*, const char *, const char *);
58 static void getfstype(ib_params
*, const char *, const char *);
59 static void parseoptions(ib_params
*, const char *);
60 __dead
static void usage(void);
61 static void options_usage(void);
62 static void machine_usage(void);
63 static void fstype_usage(void);
65 static ib_params installboot_params
;
67 #define OFFSET(field) offsetof(ib_params, field)
69 const char *name
; /* Name of option */
70 ib_flags flag
; /* Corresponding IB_xxx flag */
71 enum { /* Type of option value... */
72 OPT_BOOL
, /* no value */
73 OPT_INT
, /* numeric value */
74 OPT_WORD
, /* space/tab/, terminated */
75 OPT_STRING
/* null terminated */
77 int offset
; /* of field in ib_params */
79 { "alphasum", IB_ALPHASUM
, OPT_BOOL
, 0 },
80 { "append", IB_APPEND
, OPT_BOOL
, 0 },
81 { "command", IB_COMMAND
, OPT_STRING
, OFFSET(command
) },
82 { "console", IB_CONSOLE
, OPT_WORD
, OFFSET(console
) },
83 { "ioaddr", IB_CONSADDR
, OPT_INT
, OFFSET(consaddr
) },
84 { "keymap", IB_KEYMAP
, OPT_WORD
, OFFSET(keymap
) },
85 { "password", IB_PASSWORD
, OPT_WORD
, OFFSET(password
) },
86 { "resetvideo", IB_RESETVIDEO
, OPT_BOOL
, 0 },
87 { "speed", IB_CONSPEED
, OPT_INT
, OFFSET(conspeed
) },
88 { "sunsum", IB_SUNSUM
, OPT_BOOL
, 0 },
89 { "timeout", IB_TIMEOUT
, OPT_INT
, OFFSET(timeout
) },
90 { "modules", IB_MODULES
, OPT_BOOL
, 0 },
91 { "bootconf", IB_BOOTCONF
, OPT_BOOL
, 0 },
95 #define OPTION(params, type, opt) (*(type *)((char *)(params) + (opt)->offset))
97 #define DFL_SECSIZE 512 /* Don't use DEV_BSIZE. It's host's value. */
100 main(int argc
, char *argv
[])
102 struct utsname utsname
;
108 ib_flags unsupported_flags
;
110 /* XXX Temp stuff for MINIX until fdisk is ported */
111 if ((4 <= argc
&& argc
<= 6) && isoption(argv
[1], "-master")) {
112 install_master(argv
[2], argv
[3], argv
+ 4);
116 setprogname(argv
[0]);
117 params
= &installboot_params
;
118 memset(params
, 0, sizeof(*params
));
121 if ((p
= getenv("MACHINE")) != NULL
)
122 getmachine(params
, p
, "$MACHINE");
124 while ((ch
= getopt(argc
, argv
, "b:B:cefm:no:t:v")) != -1) {
131 lval
= strtoul(optarg
, &p
, 0);
132 if (lval
> UINT32_MAX
|| *p
!= '\0') {
134 errx(1, "Invalid block number `%s'", optarg
);
137 params
->s1start
= (uint32_t)lval
;
138 params
->flags
|= IB_STAGE1START
;
140 params
->s2start
= (uint32_t)lval
;
141 params
->flags
|= IB_STAGE2START
;
146 params
->flags
|= IB_CLEAR
;
150 params
->flags
|= IB_EDIT
;
154 params
->flags
|= IB_FORCE
;
158 getmachine(params
, optarg
, "-m");
162 params
->flags
|= IB_NOWRITE
;
166 parseoptions(params
, optarg
);
170 getfstype(params
, optarg
, "-t");
174 params
->flags
|= IB_VERBOSE
;
187 if (params
->flags
& IB_CLEAR
&& params
->flags
& IB_EDIT
)
189 if (argc
< 1 || argc
+ 2 * !!(params
->flags
& (IB_CLEAR
| IB_EDIT
)) > 3)
192 /* set missing defaults */
193 if (params
->machine
== NULL
) {
194 if (uname(&utsname
) == -1)
195 err(1, "Determine uname");
196 getmachine(params
, utsname
.machine
, "uname()");
199 /* Check that options are supported by this system */
200 unsupported_flags
= params
->flags
& ~params
->machine
->valid_flags
;
201 unsupported_flags
&= ~(IB_VERBOSE
| IB_NOWRITE
| IB_CLEAR
| IB_EDIT
203 if (unsupported_flags
!= 0) {
205 for (ndx
= 0; options
[ndx
].name
!= NULL
; ndx
++) {
206 if (unsupported_flags
& options
[ndx
].flag
) {
207 unsupported_flags
&= ~options
[ndx
].flag
;
208 warnx("`-o %s' is not supported for %s",
209 options
[ndx
].name
, params
->machine
->name
);
212 if (unsupported_flags
& IB_STAGE1START
)
213 warnx("`-b bno' is not supported for %s",
214 params
->machine
->name
);
215 if (unsupported_flags
& IB_STAGE2START
)
216 warnx("`-B bno' is not supported for %s",
217 params
->machine
->name
);
218 unsupported_flags
&= ~(IB_STAGE1START
| IB_STAGE2START
);
219 if (unsupported_flags
!= 0)
220 warnx("Unknown unsupported flag %#x (coding error!)",
224 /* and some illegal combinations */
225 if (params
->flags
& IB_STAGE1START
&& params
->flags
& IB_APPEND
) {
226 warnx("Can't use `-b bno' with `-o append'");
229 if (params
->flags
& IB_CLEAR
&&
230 params
->flags
& (IB_STAGE1START
| IB_STAGE2START
| IB_APPEND
)) {
231 warnx("Can't use `-b bno', `-B bno' or `-o append' with `-c'");
236 params
->stage2
= argv
[2];
239 params
->filesystem
= argv
[0];
240 if (params
->flags
& IB_NOWRITE
) {
248 if (minixfs3_is_minix_partition(params
->filesystem
)) {
249 /* Old setups has just 1 sector for bootblock,
250 * but bootxx_minixfs is ~8Kb, so we require new setups
251 * to have 32 sectors before the first subpartition.
252 * This prevents from overwriting FS on old setups.
254 if (!minixfs3_has_bootblock_space(params
->filesystem
)) {
255 err(1, "No space for bootxx, you should have 32 sectors"
256 " before the first subpartition on %s",
261 /* XXX should be specified via option */
262 params
->sectorsize
= DFL_SECSIZE
;
263 if ((params
->fsfd
= open(params
->filesystem
, mode
, 0600)) == -1)
264 err(1, "Opening file system `%s' read-%s",
265 params
->filesystem
, op
);
266 if (fstat(params
->fsfd
, ¶ms
->fsstat
) == -1)
267 err(1, "Examining file system `%s'", params
->filesystem
);
268 if (params
->fstype
!= NULL
) {
269 if (! params
->fstype
->match(params
))
270 errx(1, "File system `%s' is not of type %s",
271 params
->filesystem
, params
->fstype
->name
);
273 if (params
->stage2
!= NULL
) {
274 params
->fstype
= &fstypes
[0];
275 while (params
->fstype
->name
!= NULL
&&
276 !params
->fstype
->match(params
))
278 if (params
->fstype
->name
== NULL
)
279 errx(1, "File system `%s' is of an unknown type",
285 if ((params
->s1fd
= open(argv
[1], O_RDONLY
, 0600)) == -1)
286 err(1, "Opening primary bootstrap `%s'", argv
[1]);
287 if (fstat(params
->s1fd
, ¶ms
->s1stat
) == -1)
288 err(1, "Examining primary bootstrap `%s'", argv
[1]);
289 if (!S_ISREG(params
->s1stat
.st_mode
))
290 errx(1, "`%s' must be a regular file", argv
[1]);
291 params
->stage1
= argv
[1];
293 assert(params
->machine
!= NULL
);
295 if (params
->flags
& IB_VERBOSE
) {
296 printf("File system: %s\n", params
->filesystem
);
298 printf("File system type: %s (blocksize %u, "
300 params
->fstype
->name
, params
->fstype
->blocksize
,
301 params
->fstype
->needswap
);
302 if (!(params
->flags
& IB_EDIT
))
303 printf("Primary bootstrap: %s\n",
304 (params
->flags
& IB_CLEAR
) ? "(to be cleared)"
305 : params
->stage1
? params
->stage1
: "(none)" );
306 if (params
->stage2
!= NULL
)
307 printf("Secondary bootstrap: %s\n", params
->stage2
);
310 if (params
->flags
& IB_EDIT
) {
312 rv
= params
->machine
->editboot(params
);
313 } else if (params
->flags
& IB_CLEAR
) {
315 rv
= params
->machine
->clearboot(params
);
318 errx(EXIT_FAILURE
, "Please specify the primary "
321 rv
= params
->machine
->setboot(params
);
324 errx(1, "%s bootstrap operation failed", op
);
326 if (S_ISREG(params
->fsstat
.st_mode
)) {
327 if (fsync(params
->fsfd
) == -1)
328 err(1, "Synchronising file system `%s'",
331 /* Sync filesystems (to clean in-memory superblock?) */
334 if (close(params
->fsfd
) == -1)
335 err(1, "Closing file system `%s'", params
->filesystem
);
337 if (close(params
->s1fd
) == -1)
338 err(1, "Closing primary bootstrap `%s'",
346 parseoptions(ib_params
*params
, const char *option
)
349 const struct option
*opt
;
353 assert(params
!= NULL
);
354 assert(option
!= NULL
);
356 for (;; option
+= len
) {
357 option
+= strspn(option
, ", \t");
360 len
= strcspn(option
, "=,");
361 for (opt
= options
; opt
->name
!= NULL
; opt
++) {
362 if (memcmp(option
, opt
->name
, len
) == 0
363 && opt
->name
[len
] == 0)
366 if (opt
->name
== NULL
) {
367 len
= strcspn(option
, ",");
368 warnx("Unknown option `-o %.*s'", len
, option
);
371 params
->flags
|= opt
->flag
;
372 if (opt
->type
== OPT_BOOL
) {
373 if (option
[len
] != '=')
375 warnx("Option `%s' must not have a value", opt
->name
);
378 if (option
[len
] != '=') {
379 warnx("Option `%s' must have a value", opt
->name
);
383 len
= strcspn(option
, ",");
386 len
= strlen(option
);
393 OPTION(params
, char *, opt
) = cp
;
396 val
= strtoul(option
, &cp
, 0);
397 if (cp
> option
+ len
|| (*cp
!= 0 && *cp
!= ','))
401 OPTION(params
, int, opt
) = (int)val
;
404 errx(1, "Internal error: option `%s' has invalid type %d",
405 opt
->name
, opt
->type
);
407 warnx("Invalid option value `%s=%.*s'", opt
->name
, len
, option
);
420 warnx("Valid options are:");
422 for (ndx
= 0; options
[ndx
].name
!= 0; ndx
++) {
423 fprintf(stderr
, "%s%s", pfx
, options
[ndx
].name
);
424 switch (options
[ndx
].type
) {
426 fprintf(stderr
, "=number");
429 fprintf(stderr
, "=word");
432 fprintf(stderr
, "=string");
442 fprintf(stderr
, "\n");
446 no_setboot(ib_params
*params
)
449 assert(params
!= NULL
);
451 warnx("%s: bootstrap installation is not supported",
452 params
->machine
->name
);
457 no_clearboot(ib_params
*params
)
460 assert(params
!= NULL
);
462 warnx("%s: bootstrap removal is not supported",
463 params
->machine
->name
);
468 no_editboot(ib_params
*params
)
471 assert(params
!= NULL
);
473 warnx("%s: bootstrap editing is not supported",
474 params
->machine
->name
);
480 getmachine(ib_params
*param
, const char *mach
, const char *provider
)
484 assert(param
!= NULL
);
485 assert(mach
!= NULL
);
486 assert(provider
!= NULL
);
488 for (i
= 0; machines
[i
] != NULL
; i
++) {
489 if (machines
[i
]->name
== NULL
)
491 if (strcmp(machines
[i
]->name
, mach
) == 0) {
492 param
->machine
= machines
[i
];
496 warnx("Invalid machine `%s' from %s", mach
, provider
);
512 if (ioctl(fileno(stderr
), TIOCGWINSZ
, &win
) == 0 && win
.ws_col
> 0)
516 warnx("Supported machines are:");
519 for (i
= 0; machines
[i
] != NULL
; i
++) {
520 name
= machines
[i
]->name
;
524 if (col
+ len
> wincol
) {
528 col
+= fprintf(stderr
, "%s%s", prefix
, name
);
535 getfstype(ib_params
*param
, const char *fstype
, const char *provider
)
539 assert(param
!= NULL
);
540 assert(fstype
!= NULL
);
541 assert(provider
!= NULL
);
543 for (i
= 0; fstypes
[i
].name
!= NULL
; i
++) {
544 if (strcmp(fstypes
[i
].name
, fstype
) == 0) {
545 param
->fstype
= &fstypes
[i
];
549 warnx("Invalid file system type `%s' from %s", fstype
, provider
);
561 warnx("Supported file system types are:");
562 #define FSTYPES_PER_LINE 9
564 for (i
= 0; fstypes
[i
].name
!= NULL
; i
++) {
565 if (i
&& (i
% FSTYPES_PER_LINE
) == 0)
567 fprintf(stderr
, "%s%s", prefix
, fstypes
[i
].name
);
579 prog
= getprogname();
581 "usage: %s [-fnv] [-B s2bno] [-b s1bno] [-m machine] [-o options]\n"
582 "\t\t [-t fstype] filesystem primary [secondary]\n"
583 "usage: %s -c [-fnv] [-m machine] [-o options] [-t fstype] filesystem\n"
584 "usage: %s -e [-fnv] [-m machine] [-o options] bootstrap\n",