installboot.c revision 1.25 1 1.25 dogcow /* $NetBSD: installboot.c,v 1.25 2006/02/22 07:10:26 dogcow Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.1 lukem * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.1 lukem * by Luke Mewburn of Wasabi Systems.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted provided that the following conditions
12 1.1 lukem * are met:
13 1.1 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer.
15 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1 lukem * documentation and/or other materials provided with the distribution.
18 1.1 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1 lukem * must display the following acknowledgement:
20 1.1 lukem * This product includes software developed by the NetBSD
21 1.1 lukem * Foundation, Inc. and its contributors.
22 1.1 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 lukem * contributors may be used to endorse or promote products derived
24 1.1 lukem * from this software without specific prior written permission.
25 1.1 lukem *
26 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
37 1.1 lukem */
38 1.1 lukem
39 1.16 jmc #if HAVE_NBTOOL_CONFIG_H
40 1.16 jmc #include "nbtool_config.h"
41 1.16 jmc #endif
42 1.16 jmc
43 1.1 lukem #include <sys/cdefs.h>
44 1.1 lukem #if defined(__RCSID) && !defined(__lint)
45 1.25 dogcow __RCSID("$NetBSD: installboot.c,v 1.25 2006/02/22 07:10:26 dogcow Exp $");
46 1.1 lukem #endif /* !__lint */
47 1.1 lukem
48 1.24 dsl #include <sys/ioctl.h>
49 1.1 lukem #include <sys/utsname.h>
50 1.1 lukem
51 1.1 lukem #include <assert.h>
52 1.1 lukem #include <err.h>
53 1.1 lukem #include <fcntl.h>
54 1.2 simonb #include <limits.h>
55 1.1 lukem #include <stdio.h>
56 1.1 lukem #include <stdlib.h>
57 1.11 dsl #include <stddef.h>
58 1.1 lukem #include <string.h>
59 1.1 lukem #include <unistd.h>
60 1.1 lukem
61 1.1 lukem #include "installboot.h"
62 1.1 lukem
63 1.1 lukem int main(int, char *[]);
64 1.11 dsl static void getmachine(ib_params *, const char *, const char *);
65 1.11 dsl static void getfstype(ib_params *, const char *, const char *);
66 1.11 dsl static void parseoptions(ib_params *, const char *);
67 1.1 lukem static void usage(void);
68 1.11 dsl static void options_usage(void);
69 1.11 dsl static void machine_usage(void);
70 1.11 dsl static void fstype_usage(void);
71 1.1 lukem
72 1.1 lukem static ib_params installboot_params;
73 1.1 lukem
74 1.11 dsl #define OFFSET(field) offsetof(ib_params, field)
75 1.11 dsl const struct option {
76 1.11 dsl const char *name; /* Name of option */
77 1.11 dsl ib_flags flag; /* Corresponding IB_xxx flag */
78 1.11 dsl enum { /* Type of option value... */
79 1.11 dsl OPT_BOOL, /* no value */
80 1.11 dsl OPT_INT, /* numeric value */
81 1.11 dsl OPT_WORD, /* space/tab/, terminated */
82 1.11 dsl OPT_STRING /* null terminated */
83 1.11 dsl } type;
84 1.11 dsl int offset; /* of field in ib_params */
85 1.11 dsl } options[] = {
86 1.11 dsl { "alphasum", IB_ALPHASUM, OPT_BOOL },
87 1.11 dsl { "append", IB_APPEND, OPT_BOOL },
88 1.11 dsl { "command", IB_COMMAND, OPT_STRING, OFFSET(command) },
89 1.11 dsl { "console", IB_CONSOLE, OPT_WORD, OFFSET(console) },
90 1.17 dsl { "ioaddr", IB_CONSADDR, OPT_INT, OFFSET(consaddr) },
91 1.15 dsl { "keymap", IB_KEYMAP, OPT_WORD, OFFSET(keymap) },
92 1.11 dsl { "password", IB_PASSWORD, OPT_WORD, OFFSET(password) },
93 1.11 dsl { "resetvideo", IB_RESETVIDEO, OPT_BOOL },
94 1.11 dsl { "speed", IB_CONSPEED, OPT_INT, OFFSET(conspeed) },
95 1.11 dsl { "sunsum", IB_SUNSUM, OPT_BOOL },
96 1.11 dsl { "timeout", IB_TIMEOUT, OPT_INT, OFFSET(timeout) },
97 1.11 dsl { NULL },
98 1.11 dsl };
99 1.11 dsl #undef OFFSET
100 1.11 dsl #define OPTION(params, type, opt) (*(type *)((char *)(params) + (opt)->offset))
101 1.11 dsl
102 1.1 lukem int
103 1.1 lukem main(int argc, char *argv[])
104 1.1 lukem {
105 1.1 lukem struct utsname utsname;
106 1.1 lukem ib_params *params;
107 1.6 lukem unsigned long lval;
108 1.1 lukem int ch, rv, mode;
109 1.1 lukem char *p;
110 1.1 lukem const char *op;
111 1.11 dsl ib_flags unsupported_flags;
112 1.1 lukem
113 1.1 lukem setprogname(argv[0]);
114 1.1 lukem params = &installboot_params;
115 1.1 lukem memset(params, 0, sizeof(*params));
116 1.8 lukem params->fsfd = -1;
117 1.8 lukem params->s1fd = -1;
118 1.1 lukem if ((p = getenv("MACHINE")) != NULL)
119 1.11 dsl getmachine(params, p, "$MACHINE");
120 1.1 lukem
121 1.19 dsl while ((ch = getopt(argc, argv, "b:B:cem:no:t:v")) != -1) {
122 1.1 lukem switch (ch) {
123 1.1 lukem
124 1.1 lukem case 'b':
125 1.8 lukem case 'B':
126 1.1 lukem if (*optarg == '\0')
127 1.1 lukem goto badblock;
128 1.6 lukem lval = strtoul(optarg, &p, 0);
129 1.6 lukem if (lval > UINT32_MAX || *p != '\0') {
130 1.1 lukem badblock:
131 1.1 lukem errx(1, "Invalid block number `%s'", optarg);
132 1.1 lukem }
133 1.8 lukem if (ch == 'b') {
134 1.8 lukem params->s1start = (uint32_t)lval;
135 1.8 lukem params->flags |= IB_STAGE1START;
136 1.8 lukem } else {
137 1.8 lukem params->s2start = (uint32_t)lval;
138 1.8 lukem params->flags |= IB_STAGE2START;
139 1.8 lukem }
140 1.1 lukem break;
141 1.1 lukem
142 1.1 lukem case 'c':
143 1.1 lukem params->flags |= IB_CLEAR;
144 1.1 lukem break;
145 1.1 lukem
146 1.19 dsl case 'e':
147 1.19 dsl params->flags |= IB_EDIT;
148 1.19 dsl break;
149 1.19 dsl
150 1.1 lukem case 'm':
151 1.11 dsl getmachine(params, optarg, "-m");
152 1.1 lukem break;
153 1.1 lukem
154 1.1 lukem case 'n':
155 1.1 lukem params->flags |= IB_NOWRITE;
156 1.1 lukem break;
157 1.1 lukem
158 1.1 lukem case 'o':
159 1.11 dsl parseoptions(params, optarg);
160 1.1 lukem break;
161 1.1 lukem
162 1.1 lukem case 't':
163 1.11 dsl getfstype(params, optarg, "-t");
164 1.1 lukem break;
165 1.1 lukem
166 1.1 lukem case 'v':
167 1.1 lukem params->flags |= IB_VERBOSE;
168 1.1 lukem break;
169 1.1 lukem
170 1.1 lukem case '?':
171 1.1 lukem default:
172 1.1 lukem usage();
173 1.1 lukem /* NOTREACHED */
174 1.1 lukem
175 1.1 lukem }
176 1.1 lukem }
177 1.1 lukem argc -= optind;
178 1.1 lukem argv += optind;
179 1.1 lukem
180 1.19 dsl if (params->flags & IB_CLEAR && params->flags & IB_EDIT)
181 1.19 dsl usage();
182 1.19 dsl if (argc < 1 || argc + 2 * !!(params->flags & (IB_CLEAR | IB_EDIT)) > 3)
183 1.1 lukem usage();
184 1.1 lukem
185 1.19 dsl /* set missing defaults */
186 1.1 lukem if (params->machine == NULL) {
187 1.1 lukem if (uname(&utsname) == -1)
188 1.1 lukem err(1, "Determine uname");
189 1.11 dsl getmachine(params, utsname.machine, "uname()");
190 1.11 dsl }
191 1.11 dsl
192 1.11 dsl /* Check that options are supported by this system */
193 1.11 dsl unsupported_flags = params->flags & ~params->machine->valid_flags;
194 1.19 dsl unsupported_flags &= ~(IB_VERBOSE | IB_NOWRITE | IB_CLEAR | IB_EDIT);
195 1.11 dsl if (unsupported_flags != 0) {
196 1.11 dsl int ndx;
197 1.11 dsl for (ndx = 0; options[ndx].name != NULL; ndx++) {
198 1.11 dsl if (unsupported_flags & options[ndx].flag)
199 1.11 dsl warnx("`-o %s' is not supported for %s",
200 1.11 dsl options[ndx].name, params->machine->name);
201 1.11 dsl }
202 1.11 dsl if (unsupported_flags & IB_STAGE1START)
203 1.11 dsl warnx("`-b bno' is not supported for %s",
204 1.11 dsl params->machine->name);
205 1.11 dsl if (unsupported_flags & IB_STAGE2START)
206 1.11 dsl warnx("`-B bno' is not supported for %s",
207 1.11 dsl params->machine->name);
208 1.11 dsl exit(1);
209 1.11 dsl }
210 1.11 dsl /* and some illegal combinations */
211 1.11 dsl if (params->flags & IB_STAGE1START && params->flags & IB_APPEND) {
212 1.11 dsl warnx("Can't use `-b bno' with `-o append'");
213 1.11 dsl exit(1);
214 1.11 dsl }
215 1.11 dsl if (params->flags & IB_CLEAR &&
216 1.11 dsl params->flags & (IB_STAGE1START | IB_STAGE2START | IB_APPEND)) {
217 1.11 dsl warnx("Can't use `-b bno', `-B bno' or `-o append' with `-c'");
218 1.11 dsl exit(1);
219 1.1 lukem }
220 1.1 lukem
221 1.21 dsl if (argc >= 3) {
222 1.21 dsl params->stage2 = argv[2];
223 1.21 dsl }
224 1.21 dsl
225 1.1 lukem params->filesystem = argv[0];
226 1.1 lukem if (params->flags & IB_NOWRITE) {
227 1.1 lukem op = "only";
228 1.1 lukem mode = O_RDONLY;
229 1.1 lukem } else {
230 1.1 lukem op = "write";
231 1.1 lukem mode = O_RDWR;
232 1.1 lukem }
233 1.1 lukem if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
234 1.1 lukem err(1, "Opening file system `%s' read-%s",
235 1.1 lukem params->filesystem, op);
236 1.9 lukem if (fstat(params->fsfd, ¶ms->fsstat) == -1)
237 1.9 lukem err(1, "Examining file system `%s'", params->filesystem);
238 1.6 lukem if (params->fstype != NULL) {
239 1.6 lukem if (! params->fstype->match(params))
240 1.18 isaki errx(1, "File system `%s' is not of type %s",
241 1.6 lukem params->filesystem, params->fstype->name);
242 1.6 lukem } else {
243 1.21 dsl if (params->stage2 != NULL) {
244 1.21 dsl params->fstype = &fstypes[0];
245 1.21 dsl while (params->fstype->name != NULL &&
246 1.21 dsl !params->fstype->match(params))
247 1.21 dsl params->fstype++;
248 1.21 dsl if (params->fstype->name == NULL)
249 1.21 dsl errx(1, "File system `%s' is of an unknown type",
250 1.21 dsl params->filesystem);
251 1.21 dsl }
252 1.6 lukem }
253 1.1 lukem
254 1.5 lukem if (argc >= 2) {
255 1.5 lukem params->stage1 = argv[1];
256 1.21 dsl if ((params->s1fd = open(params->stage1, O_RDONLY, 0600)) == -1)
257 1.5 lukem err(1, "Opening primary bootstrap `%s'",
258 1.5 lukem params->stage1);
259 1.9 lukem if (fstat(params->s1fd, ¶ms->s1stat) == -1)
260 1.9 lukem err(1, "Examining primary bootstrap `%s'",
261 1.9 lukem params->stage1);
262 1.9 lukem if (!S_ISREG(params->s1stat.st_mode))
263 1.18 isaki errx(1, "`%s' must be a regular file", params->stage1);
264 1.5 lukem }
265 1.1 lukem assert(params->machine != NULL);
266 1.1 lukem
267 1.1 lukem if (params->flags & IB_VERBOSE) {
268 1.5 lukem printf("File system: %s\n", params->filesystem);
269 1.21 dsl if (params->fstype)
270 1.21 dsl printf("File system type: %s (blocksize %u, "
271 1.21 dsl "needswap %d)\n",
272 1.21 dsl params->fstype->name, params->fstype->blocksize,
273 1.21 dsl params->fstype->needswap);
274 1.19 dsl if (!(params->flags & IB_EDIT))
275 1.19 dsl printf("Primary bootstrap: %s\n",
276 1.19 dsl (params->flags & IB_CLEAR) ? "(to be cleared)"
277 1.19 dsl : params->stage1 ? params->stage1 : "(none)" );
278 1.5 lukem if (params->stage2 != NULL)
279 1.5 lukem printf("Secondary bootstrap: %s\n", params->stage2);
280 1.1 lukem }
281 1.1 lukem
282 1.19 dsl if (params->flags & IB_EDIT) {
283 1.19 dsl op = "Edit";
284 1.19 dsl rv = params->machine->editboot(params);
285 1.19 dsl } else if (params->flags & IB_CLEAR) {
286 1.1 lukem op = "Clear";
287 1.1 lukem rv = params->machine->clearboot(params);
288 1.1 lukem } else {
289 1.22 jmmv if (argc < 2)
290 1.22 jmmv errx(EXIT_FAILURE, "Please specify the primary "
291 1.22 jmmv "bootstrap file");
292 1.1 lukem op = "Set";
293 1.1 lukem rv = params->machine->setboot(params);
294 1.1 lukem }
295 1.1 lukem if (rv == 0)
296 1.5 lukem errx(1, "%s bootstrap operation failed", op);
297 1.1 lukem
298 1.9 lukem if (S_ISREG(params->fsstat.st_mode)) {
299 1.9 lukem if (fsync(params->fsfd) == -1)
300 1.9 lukem err(1, "Synchronising file system `%s'",
301 1.9 lukem params->filesystem);
302 1.9 lukem } else {
303 1.9 lukem /* Sync filesystems (to clean in-memory superblock?) */
304 1.9 lukem sync();
305 1.9 lukem }
306 1.1 lukem if (close(params->fsfd) == -1)
307 1.1 lukem err(1, "Closing file system `%s'", params->filesystem);
308 1.1 lukem if (argc == 2)
309 1.5 lukem if (close(params->s1fd) == -1)
310 1.5 lukem err(1, "Closing primary bootstrap `%s'",
311 1.5 lukem params->stage1);
312 1.1 lukem
313 1.1 lukem exit(0);
314 1.1 lukem /* NOTREACHED */
315 1.1 lukem }
316 1.1 lukem
317 1.11 dsl static void
318 1.11 dsl parseoptions(ib_params *params, const char *option)
319 1.1 lukem {
320 1.11 dsl char *cp;
321 1.11 dsl const struct option *opt;
322 1.11 dsl int len;
323 1.12 dsl unsigned long val;
324 1.1 lukem
325 1.1 lukem assert(params != NULL);
326 1.1 lukem assert(option != NULL);
327 1.1 lukem
328 1.11 dsl for (;; option += len) {
329 1.11 dsl option += strspn(option, ", \t");
330 1.11 dsl if (*option == 0)
331 1.11 dsl return;
332 1.11 dsl len = strcspn(option, "=,");
333 1.11 dsl for (opt = options; opt->name != NULL; opt++) {
334 1.11 dsl if (memcmp(option, opt->name, len) == 0
335 1.11 dsl && opt->name[len] == 0)
336 1.11 dsl break;
337 1.11 dsl }
338 1.11 dsl if (opt->name == NULL) {
339 1.11 dsl len = strcspn(option, ",");
340 1.11 dsl warnx("Unknown option `-o %.*s'", len, option);
341 1.11 dsl break;
342 1.11 dsl }
343 1.11 dsl params->flags |= opt->flag;
344 1.11 dsl if (opt->type == OPT_BOOL) {
345 1.11 dsl if (option[len] != '=')
346 1.11 dsl continue;
347 1.11 dsl warnx("Option `%s' must not have a value", opt->name);
348 1.11 dsl break;
349 1.11 dsl }
350 1.11 dsl if (option[len] != '=') {
351 1.11 dsl warnx("Option `%s' must have a value", opt->name);
352 1.11 dsl break;
353 1.1 lukem }
354 1.11 dsl option += len + 1;
355 1.11 dsl len = strcspn(option, ",");
356 1.11 dsl switch (opt->type) {
357 1.11 dsl case OPT_STRING:
358 1.11 dsl len = strlen(option);
359 1.11 dsl /* FALLTHROUGH */
360 1.11 dsl case OPT_WORD:
361 1.11 dsl cp = strdup(option);
362 1.11 dsl if (cp == NULL)
363 1.11 dsl err(1, "strdup");
364 1.11 dsl cp[len] = 0;
365 1.11 dsl OPTION(params, char *, opt) = cp;
366 1.11 dsl continue;
367 1.11 dsl case OPT_INT:
368 1.11 dsl val = strtoul(option, &cp, 0);
369 1.11 dsl if (cp > option + len || (*cp != 0 && *cp != ','))
370 1.11 dsl break;
371 1.11 dsl OPTION(params, int, opt) = val;
372 1.11 dsl if (OPTION(params, int, opt) != val)
373 1.11 dsl /* value got truncated on int convertion */
374 1.11 dsl break;
375 1.11 dsl continue;
376 1.11 dsl default:
377 1.13 petrov errx(1, "Internal error: option `%s' has invalid type %d",
378 1.11 dsl opt->name, opt->type);
379 1.11 dsl }
380 1.11 dsl warnx("Invalid option value `%s=%.*s'", opt->name, len, option);
381 1.11 dsl break;
382 1.1 lukem }
383 1.11 dsl options_usage();
384 1.11 dsl exit(1);
385 1.3 lukem }
386 1.3 lukem
387 1.11 dsl static void
388 1.11 dsl options_usage(void)
389 1.3 lukem {
390 1.11 dsl int ndx;
391 1.11 dsl const char *pfx;
392 1.3 lukem
393 1.11 dsl warnx("Valid options are:");
394 1.11 dsl pfx = "\t";
395 1.11 dsl for (ndx = 0; options[ndx].name != 0; ndx++) {
396 1.11 dsl fprintf(stderr, "%s%s", pfx, options[ndx].name);
397 1.11 dsl switch (options[ndx].type) {
398 1.11 dsl case OPT_INT:
399 1.11 dsl fprintf(stderr, "=number");
400 1.11 dsl break;
401 1.11 dsl case OPT_WORD:
402 1.11 dsl fprintf(stderr, "=word");
403 1.11 dsl break;
404 1.11 dsl case OPT_STRING:
405 1.11 dsl fprintf(stderr, "=string");
406 1.11 dsl break;
407 1.11 dsl default:
408 1.11 dsl break;
409 1.11 dsl }
410 1.11 dsl if ((ndx % 5) == 4)
411 1.11 dsl pfx = ",\n\t";
412 1.11 dsl else
413 1.11 dsl pfx = ", ";
414 1.11 dsl }
415 1.11 dsl fprintf(stderr, "\n");
416 1.3 lukem }
417 1.3 lukem
418 1.3 lukem int
419 1.3 lukem no_setboot(ib_params *params)
420 1.3 lukem {
421 1.3 lukem
422 1.7 lukem assert(params != NULL);
423 1.7 lukem
424 1.5 lukem warnx("%s: bootstrap installation is not supported",
425 1.3 lukem params->machine->name);
426 1.3 lukem return (0);
427 1.3 lukem }
428 1.3 lukem
429 1.3 lukem int
430 1.3 lukem no_clearboot(ib_params *params)
431 1.3 lukem {
432 1.3 lukem
433 1.7 lukem assert(params != NULL);
434 1.7 lukem
435 1.5 lukem warnx("%s: bootstrap removal is not supported",
436 1.3 lukem params->machine->name);
437 1.1 lukem return (0);
438 1.1 lukem }
439 1.1 lukem
440 1.19 dsl int
441 1.19 dsl no_editboot(ib_params *params)
442 1.19 dsl {
443 1.19 dsl
444 1.19 dsl assert(params != NULL);
445 1.19 dsl
446 1.19 dsl warnx("%s: bootstrap editing is not supported",
447 1.19 dsl params->machine->name);
448 1.19 dsl return (0);
449 1.19 dsl }
450 1.19 dsl
451 1.1 lukem
452 1.11 dsl static void
453 1.1 lukem getmachine(ib_params *param, const char *mach, const char *provider)
454 1.1 lukem {
455 1.1 lukem int i;
456 1.1 lukem
457 1.11 dsl assert(param != NULL);
458 1.11 dsl assert(mach != NULL);
459 1.11 dsl assert(provider != NULL);
460 1.11 dsl
461 1.23 dsl for (i = 0; machines[i] != NULL; i++) {
462 1.24 dsl if (machines[i]->name == NULL)
463 1.24 dsl continue;
464 1.23 dsl if (strcmp(machines[i]->name, mach) == 0) {
465 1.23 dsl param->machine = machines[i];
466 1.11 dsl return;
467 1.1 lukem }
468 1.1 lukem }
469 1.11 dsl warnx("Invalid machine `%s' from %s", mach, provider);
470 1.11 dsl machine_usage();
471 1.11 dsl exit(1);
472 1.11 dsl }
473 1.11 dsl
474 1.11 dsl static void
475 1.11 dsl machine_usage(void)
476 1.11 dsl {
477 1.11 dsl const char *prefix;
478 1.11 dsl int i;
479 1.24 dsl int col, len;
480 1.24 dsl const char *name;
481 1.25 dogcow int wincol=80;
482 1.25 dogcow #ifdef TIOCGWINSZ
483 1.24 dsl struct winsize win;
484 1.24 dsl
485 1.25 dogcow if (ioctl(fileno(stderr), TIOCGWINSZ, &win) == 0)
486 1.25 dogcow wincol = win.ws_col;
487 1.25 dogcow #endif
488 1.11 dsl
489 1.1 lukem warnx("Supported machines are:");
490 1.24 dsl prefix="\t";
491 1.24 dsl col = 8 + 3;
492 1.23 dsl for (i = 0; machines[i] != NULL; i++) {
493 1.24 dsl name = machines[i]->name;
494 1.24 dsl if (name == NULL)
495 1.24 dsl continue;
496 1.24 dsl len = strlen(name);
497 1.25 dogcow if (col + len > wincol) {
498 1.10 lukem prefix=",\n\t";
499 1.24 dsl col = -2 + 8 + 3;
500 1.24 dsl }
501 1.24 dsl col += fprintf(stderr, "%s%s", prefix, name);
502 1.24 dsl prefix=", ";
503 1.1 lukem }
504 1.10 lukem fputs("\n", stderr);
505 1.6 lukem }
506 1.6 lukem
507 1.11 dsl static void
508 1.6 lukem getfstype(ib_params *param, const char *fstype, const char *provider)
509 1.6 lukem {
510 1.11 dsl int i;
511 1.11 dsl
512 1.11 dsl assert(param != NULL);
513 1.11 dsl assert(fstype != NULL);
514 1.11 dsl assert(provider != NULL);
515 1.11 dsl
516 1.11 dsl for (i = 0; fstypes[i].name != NULL; i++) {
517 1.11 dsl if (strcmp(fstypes[i].name, fstype) == 0) {
518 1.11 dsl param->fstype = &fstypes[i];
519 1.11 dsl return;
520 1.11 dsl }
521 1.11 dsl }
522 1.11 dsl warnx("Invalid file system type `%s' from %s", fstype, provider);
523 1.11 dsl fstype_usage();
524 1.11 dsl exit(1);
525 1.11 dsl }
526 1.11 dsl
527 1.11 dsl static void
528 1.11 dsl fstype_usage(void)
529 1.11 dsl {
530 1.10 lukem const char *prefix;
531 1.6 lukem int i;
532 1.6 lukem
533 1.6 lukem warnx("Supported file system types are:");
534 1.10 lukem #define FSTYPES_PER_LINE 9
535 1.24 dsl prefix="\t";
536 1.6 lukem for (i = 0; fstypes[i].name != NULL; i++) {
537 1.25 dogcow if (i && (i % FSTYPES_PER_LINE) == 0)
538 1.10 lukem prefix=",\n\t";
539 1.10 lukem fprintf(stderr, "%s%s", prefix, fstypes[i].name);
540 1.24 dsl prefix=", ";
541 1.6 lukem }
542 1.10 lukem fputs("\n", stderr);
543 1.1 lukem }
544 1.1 lukem
545 1.1 lukem static void
546 1.1 lukem usage(void)
547 1.1 lukem {
548 1.1 lukem const char *prog;
549 1.1 lukem
550 1.1 lukem prog = getprogname();
551 1.1 lukem fprintf(stderr,
552 1.20 wiz "usage: %s [-nv] [-B s2bno] [-b s1bno] [-m machine] [-o options]\n"
553 1.20 wiz "\t\t [-t fstype] filesystem primary [secondary]\n"
554 1.19 dsl "usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n"
555 1.19 dsl "usage: %s -e [-nv] [-m machine] [-o options] bootstrap\n",
556 1.19 dsl prog, prog, prog);
557 1.11 dsl machine_usage();
558 1.11 dsl fstype_usage();
559 1.11 dsl options_usage();
560 1.1 lukem exit(1);
561 1.1 lukem }
562