1 /* $NetBSD: installboot.c,v 1.31 2009/04/05 11:55:39 lukem 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>
37 #if defined(__RCSID) && !defined(__lint)
38 __RCSID("$NetBSD: installboot.c,v 1.31 2009/04/05 11:55:39 lukem Exp $");
41 #include <sys/ioctl.h>
42 #include <sys/utsname.h>
54 #include "installboot.h"
56 int main(int, char *[]);
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 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
) },
93 #define OPTION(params, type, opt) (*(type *)((char *)(params) + (opt)->offset))
95 #define DFL_SECSIZE 512 /* Don't use DEV_BSIZE. It's host's value. */
98 main(int argc
, char *argv
[])
100 struct utsname utsname
;
106 ib_flags unsupported_flags
;
108 setprogname(argv
[0]);
109 params
= &installboot_params
;
110 memset(params
, 0, sizeof(*params
));
113 if ((p
= getenv("MACHINE")) != NULL
)
114 getmachine(params
, p
, "$MACHINE");
116 while ((ch
= getopt(argc
, argv
, "b:B:cefm:no:t:v")) != -1) {
123 lval
= strtoul(optarg
, &p
, 0);
124 if (lval
> UINT32_MAX
|| *p
!= '\0') {
126 errx(1, "Invalid block number `%s'", optarg
);
129 params
->s1start
= (uint32_t)lval
;
130 params
->flags
|= IB_STAGE1START
;
132 params
->s2start
= (uint32_t)lval
;
133 params
->flags
|= IB_STAGE2START
;
138 params
->flags
|= IB_CLEAR
;
142 params
->flags
|= IB_EDIT
;
146 params
->flags
|= IB_FORCE
;
150 getmachine(params
, optarg
, "-m");
154 params
->flags
|= IB_NOWRITE
;
158 parseoptions(params
, optarg
);
162 getfstype(params
, optarg
, "-t");
166 params
->flags
|= IB_VERBOSE
;
179 if (params
->flags
& IB_CLEAR
&& params
->flags
& IB_EDIT
)
181 if (argc
< 1 || argc
+ 2 * !!(params
->flags
& (IB_CLEAR
| IB_EDIT
)) > 3)
184 /* set missing defaults */
185 if (params
->machine
== NULL
) {
186 if (uname(&utsname
) == -1)
187 err(1, "Determine uname");
188 getmachine(params
, utsname
.machine
, "uname()");
191 /* Check that options are supported by this system */
192 unsupported_flags
= params
->flags
& ~params
->machine
->valid_flags
;
193 unsupported_flags
&= ~(IB_VERBOSE
| IB_NOWRITE
| IB_CLEAR
| IB_EDIT
195 if (unsupported_flags
!= 0) {
197 for (ndx
= 0; options
[ndx
].name
!= NULL
; ndx
++) {
198 if (unsupported_flags
& options
[ndx
].flag
) {
199 unsupported_flags
&= ~options
[ndx
].flag
;
200 warnx("`-o %s' is not supported for %s",
201 options
[ndx
].name
, params
->machine
->name
);
204 if (unsupported_flags
& IB_STAGE1START
)
205 warnx("`-b bno' is not supported for %s",
206 params
->machine
->name
);
207 if (unsupported_flags
& IB_STAGE2START
)
208 warnx("`-B bno' is not supported for %s",
209 params
->machine
->name
);
210 unsupported_flags
&= ~(IB_STAGE1START
| IB_STAGE2START
);
211 if (unsupported_flags
!= 0)
212 warnx("Unknown unsupported flag %#x (coding error!)",
216 /* and some illegal combinations */
217 if (params
->flags
& IB_STAGE1START
&& params
->flags
& IB_APPEND
) {
218 warnx("Can't use `-b bno' with `-o append'");
221 if (params
->flags
& IB_CLEAR
&&
222 params
->flags
& (IB_STAGE1START
| IB_STAGE2START
| IB_APPEND
)) {
223 warnx("Can't use `-b bno', `-B bno' or `-o append' with `-c'");
228 params
->stage2
= argv
[2];
231 params
->filesystem
= argv
[0];
232 if (params
->flags
& IB_NOWRITE
) {
239 /* XXX should be specified via option */
240 params
->sectorsize
= DFL_SECSIZE
;
241 if ((params
->fsfd
= open(params
->filesystem
, mode
, 0600)) == -1)
242 err(1, "Opening file system `%s' read-%s",
243 params
->filesystem
, op
);
244 if (fstat(params
->fsfd
, ¶ms
->fsstat
) == -1)
245 err(1, "Examining file system `%s'", params
->filesystem
);
246 if (params
->fstype
!= NULL
) {
247 if (! params
->fstype
->match(params
))
248 errx(1, "File system `%s' is not of type %s",
249 params
->filesystem
, params
->fstype
->name
);
251 if (params
->stage2
!= NULL
) {
252 params
->fstype
= &fstypes
[0];
253 while (params
->fstype
->name
!= NULL
&&
254 !params
->fstype
->match(params
))
256 if (params
->fstype
->name
== NULL
)
257 errx(1, "File system `%s' is of an unknown type",
263 if ((params
->s1fd
= open(argv
[1], O_RDONLY
, 0600)) == -1)
264 err(1, "Opening primary bootstrap `%s'", argv
[1]);
265 if (fstat(params
->s1fd
, ¶ms
->s1stat
) == -1)
266 err(1, "Examining primary bootstrap `%s'", argv
[1]);
267 if (!S_ISREG(params
->s1stat
.st_mode
))
268 errx(1, "`%s' must be a regular file", argv
[1]);
269 params
->stage1
= argv
[1];
271 assert(params
->machine
!= NULL
);
273 if (params
->flags
& IB_VERBOSE
) {
274 printf("File system: %s\n", params
->filesystem
);
276 printf("File system type: %s (blocksize %u, "
278 params
->fstype
->name
, params
->fstype
->blocksize
,
279 params
->fstype
->needswap
);
280 if (!(params
->flags
& IB_EDIT
))
281 printf("Primary bootstrap: %s\n",
282 (params
->flags
& IB_CLEAR
) ? "(to be cleared)"
283 : params
->stage1
? params
->stage1
: "(none)" );
284 if (params
->stage2
!= NULL
)
285 printf("Secondary bootstrap: %s\n", params
->stage2
);
288 if (params
->flags
& IB_EDIT
) {
290 rv
= params
->machine
->editboot(params
);
291 } else if (params
->flags
& IB_CLEAR
) {
293 rv
= params
->machine
->clearboot(params
);
296 errx(EXIT_FAILURE
, "Please specify the primary "
299 rv
= params
->machine
->setboot(params
);
302 errx(1, "%s bootstrap operation failed", op
);
304 if (S_ISREG(params
->fsstat
.st_mode
)) {
305 if (fsync(params
->fsfd
) == -1)
306 err(1, "Synchronising file system `%s'",
309 /* Sync filesystems (to clean in-memory superblock?) */
312 if (close(params
->fsfd
) == -1)
313 err(1, "Closing file system `%s'", params
->filesystem
);
315 if (close(params
->s1fd
) == -1)
316 err(1, "Closing primary bootstrap `%s'",
324 parseoptions(ib_params
*params
, const char *option
)
327 const struct option
*opt
;
331 assert(params
!= NULL
);
332 assert(option
!= NULL
);
334 for (;; option
+= len
) {
335 option
+= strspn(option
, ", \t");
338 len
= strcspn(option
, "=,");
339 for (opt
= options
; opt
->name
!= NULL
; opt
++) {
340 if (memcmp(option
, opt
->name
, len
) == 0
341 && opt
->name
[len
] == 0)
344 if (opt
->name
== NULL
) {
345 len
= strcspn(option
, ",");
346 warnx("Unknown option `-o %.*s'", len
, option
);
349 params
->flags
|= opt
->flag
;
350 if (opt
->type
== OPT_BOOL
) {
351 if (option
[len
] != '=')
353 warnx("Option `%s' must not have a value", opt
->name
);
356 if (option
[len
] != '=') {
357 warnx("Option `%s' must have a value", opt
->name
);
361 len
= strcspn(option
, ",");
364 len
= strlen(option
);
371 OPTION(params
, char *, opt
) = cp
;
374 val
= strtoul(option
, &cp
, 0);
375 if (cp
> option
+ len
|| (*cp
!= 0 && *cp
!= ','))
379 OPTION(params
, int, opt
) = (int)val
;
382 errx(1, "Internal error: option `%s' has invalid type %d",
383 opt
->name
, opt
->type
);
385 warnx("Invalid option value `%s=%.*s'", opt
->name
, len
, option
);
398 warnx("Valid options are:");
400 for (ndx
= 0; options
[ndx
].name
!= 0; ndx
++) {
401 fprintf(stderr
, "%s%s", pfx
, options
[ndx
].name
);
402 switch (options
[ndx
].type
) {
404 fprintf(stderr
, "=number");
407 fprintf(stderr
, "=word");
410 fprintf(stderr
, "=string");
420 fprintf(stderr
, "\n");
424 no_setboot(ib_params
*params
)
427 assert(params
!= NULL
);
429 warnx("%s: bootstrap installation is not supported",
430 params
->machine
->name
);
435 no_clearboot(ib_params
*params
)
438 assert(params
!= NULL
);
440 warnx("%s: bootstrap removal is not supported",
441 params
->machine
->name
);
446 no_editboot(ib_params
*params
)
449 assert(params
!= NULL
);
451 warnx("%s: bootstrap editing is not supported",
452 params
->machine
->name
);
458 getmachine(ib_params
*param
, const char *mach
, const char *provider
)
462 assert(param
!= NULL
);
463 assert(mach
!= NULL
);
464 assert(provider
!= NULL
);
466 for (i
= 0; machines
[i
] != NULL
; i
++) {
467 if (machines
[i
]->name
== NULL
)
469 if (strcmp(machines
[i
]->name
, mach
) == 0) {
470 param
->machine
= machines
[i
];
474 warnx("Invalid machine `%s' from %s", mach
, provider
);
490 if (ioctl(fileno(stderr
), TIOCGWINSZ
, &win
) == 0)
494 warnx("Supported machines are:");
497 for (i
= 0; machines
[i
] != NULL
; i
++) {
498 name
= machines
[i
]->name
;
502 if (col
+ len
> wincol
) {
506 col
+= fprintf(stderr
, "%s%s", prefix
, name
);
513 getfstype(ib_params
*param
, const char *fstype
, const char *provider
)
517 assert(param
!= NULL
);
518 assert(fstype
!= NULL
);
519 assert(provider
!= NULL
);
521 for (i
= 0; fstypes
[i
].name
!= NULL
; i
++) {
522 if (strcmp(fstypes
[i
].name
, fstype
) == 0) {
523 param
->fstype
= &fstypes
[i
];
527 warnx("Invalid file system type `%s' from %s", fstype
, provider
);
538 warnx("Supported file system types are:");
539 #define FSTYPES_PER_LINE 9
541 for (i
= 0; fstypes
[i
].name
!= NULL
; i
++) {
542 if (i
&& (i
% FSTYPES_PER_LINE
) == 0)
544 fprintf(stderr
, "%s%s", prefix
, fstypes
[i
].name
);
555 prog
= getprogname();
557 "usage: %s [-fnv] [-B s2bno] [-b s1bno] [-m machine] [-o options]\n"
558 "\t\t [-t fstype] filesystem primary [secondary]\n"
559 "usage: %s -c [-fnv] [-m machine] [-o options] [-t fstype] filesystem\n"
560 "usage: %s -e [-fnv] [-m machine] [-o options] bootstrap\n",