4 * Copyright (c) 2009 NONAKA Kimihiro <nonaka@netbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/bootblock.h>
30 #include <sys/boot_flag.h>
37 #include "pathnames.h"
39 #include <lib/libsa/loadfile.h>
40 #include <lib/libsa/ufs.h>
42 #include "compat_linux.h"
44 static const char * const names
[][2] = {
45 { "netbsd", "netbsd.gz" },
46 { "netbsd.old", "netbsd.old.gz", },
47 { "onetbsd", "onetbsd.gz" },
50 char *default_devname
;
51 uint default_unit
, default_partition
;
52 const char *default_filename
;
53 int default_timeout
= 5;
55 static char probed_disks
[256];
57 static void bootcmd_help(char *);
58 static void bootcmd_ls(char *);
59 static void bootcmd_quit(char *);
60 static void bootcmd_boot(char *);
61 static void bootcmd_disk(char *);
62 #ifdef SUPPORT_CONSDEV
63 static void bootcmd_consdev(char *);
66 static const struct bootblk_command
{
68 void (*c_fn
)(char *arg
);
70 { "help", bootcmd_help
},
71 { "?", bootcmd_help
},
72 { "quit", bootcmd_quit
},
74 { "boot", bootcmd_boot
},
75 { "disk", bootcmd_disk
},
76 #ifdef SUPPORT_CONSDEV
77 { "consdev", bootcmd_consdev
},
82 static struct btinfo_howto bi_howto
;
84 static void print_banner(void);
85 static int exec_netbsd(const char *file
, int howto
);
88 parsebootfile(const char *fname
, char **fsname
, char **devname
,
89 uint
*unit
, uint
*partition
, const char **file
)
94 *devname
= default_devname
;
96 *partition
= default_partition
;
97 *file
= default_filename
;
102 if ((col
= strchr(fname
, ':'))) { /* device given */
103 static char savedevname
[MAXDEVNAME
+1];
105 unsigned int u
= 0, p
= 0;
108 devlen
= col
- fname
;
109 if (devlen
> MAXDEVNAME
)
112 #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
113 if (!isvalidname(fname
[i
]))
116 savedevname
[i
] = fname
[i
];
118 } while (isvalidname(fname
[i
]));
119 savedevname
[i
] = '\0';
121 #define isnum(c) ((c) >= '0' && (c) <= '9')
123 if (!isnum(fname
[i
]))
127 u
+= fname
[i
++] - '0';
128 } while (isnum(fname
[i
]));
131 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'a' + MAXPARTITIONS)
133 if (!isvalidpart(fname
[i
]))
135 p
= fname
[i
++] - 'a';
141 *devname
= savedevname
;
154 sprint_bootsel(const char *filename
)
157 char *fsname
, *devname
;
158 uint unit
, partition
;
161 if (parsebootfile(filename
, &fsname
, &devname
, &unit
, &partition
,
163 snprintf(buf
, sizeof(buf
), "%s%d%c:%s", devname
, unit
,
164 'a' + partition
, file
);
173 extern const char bootprog_name
[];
174 extern const char bootprog_rev
[];
175 extern const char bootprog_date
[];
176 extern const char bootprog_maker
[];
179 printf(">> %s, Revision %s\n", bootprog_name
, bootprog_rev
);
180 printf(">> (%s, %s)\n", bootprog_maker
, bootprog_date
);
186 extern char twiddle_toggle
;
190 consinit(CONSDEV_GLASS
, -1);
192 twiddle_toggle
= 1; /* no twiddling until we're ready */
194 /* set default value: hd0a:netbsd */
195 default_devname
= "hd";
197 default_partition
= 0;
198 default_filename
= names
[0][0];
200 diskprobe(probed_disks
, sizeof(probed_disks
));
202 parsebootconf(_PATH_BOOTCONF
);
204 #ifdef SUPPORT_CONSDEV
206 * If console set in boot.cfg, switch to it.
207 * This will print the banner, so we don't need to explicitly do it
209 if (bootconf
.consdev
)
210 bootcmd_consdev(bootconf
.consdev
);
215 printf("\ndisks: %s\n", probed_disks
);
217 /* Display the menu, if applicable */
219 if (bootconf
.nummenu
> 0) {
220 /* Does not return */
224 printf("Press return to boot now, any other key for boot menu\n");
226 for (currname
= 0; currname
< __arraycount(names
); currname
++) {
227 printf("booting %s - starting in ",
228 sprint_bootsel(names
[currname
][0]));
230 c
= awaitkey((bootconf
.timeout
< 0) ? 0 : bootconf
.timeout
, 1);
231 if ((c
!= '\r') && (c
!= '\n') && (c
!= '\0')) {
232 printf("type \"?\" or \"help\" for help.\n");
233 bootmenu(); /* does not return */
237 * try pairs of names[] entries, foo and foo.gz
239 /* don't print "booting..." again */
240 bootit(names
[currname
][0], 0, 0);
241 /* since it failed, try compressed bootfile. */
242 bootit(names
[currname
][1], 0, 1);
245 bootmenu(); /* does not return */
249 bootit(const char *filename
, int howto
, int tell
)
253 printf("booting %s", sprint_bootsel(filename
));
255 printf(" (howto 0x%x)", howto
);
259 if (exec_netbsd(filename
, howto
) < 0) {
260 printf("boot: %s: %s\n", sprint_bootsel(filename
),
263 printf("boot returned\n");
268 exec_netbsd(const char *file
, int howto
)
270 u_long marks
[MARK_MAX
];
272 BI_ALLOC(BTINFO_MAX
);
274 bi_howto
.howto
= howto
;
275 BI_ADD(&bi_howto
, BTINFO_HOWTO
, sizeof(bi_howto
));
277 if (loadfile_zboot(file
, marks
, LOAD_KERNEL
) == -1)
292 static char *gettrailer(char *arg
);
293 static int parseopts(const char *opts
, int *howto
);
294 static int parseboot(char *arg
, char **filename
, int *howto
);
298 bootcmd_help(char *arg
)
301 printf("commands are:\n"
302 "boot [xdNx:][filename] [-acdqsv]\n"
303 " (ex. \"hd0a:netbsd.old -s\"\n"
305 #ifdef SUPPORT_CONSDEV
306 "consdev {glass|com [speed]}\n"
315 bootcmd_quit(char *arg
)
318 printf("Exiting...\n");
324 bootcmd_ls(char *arg
)
326 const char *save
= default_filename
;
328 default_filename
= "/";
330 default_filename
= save
;
334 bootcmd_boot(char *arg
)
339 if (parseboot(arg
, &filename
, &howto
)) {
340 bootit(filename
, howto
, 1);
346 bootcmd_disk(char *arg
)
349 printf("disks: %s\n", probed_disks
);
352 #ifdef SUPPORT_CONSDEV
353 static const struct cons_devs
{
357 { "glass", CONSDEV_GLASS
},
358 { "com", CONSDEV_COM0
},
359 { "com0", CONSDEV_COM0
},
364 bootcmd_consdev(char *arg
)
366 const struct cons_devs
*cdp
;
370 p
= strchr(arg
, ' ');
375 for (cdp
= cons_devs
; cdp
->name
!= NULL
; cdp
++) {
376 if (strcmp(arg
, cdp
->name
) == 0) {
377 consinit(cdp
->tag
, speed
);
382 printf("invalid console device.\n");
392 options
= gettrailer(arg
);
394 for (i
= 0; bootcmds
[i
].c_name
!= NULL
; i
++) {
395 if (strcmp(arg
, bootcmds
[i
].c_name
) == 0) {
396 (*bootcmds
[i
].c_fn
)(options
);
401 printf("unknown command\n");
419 * Skip leading whitespace.
431 * chops the head from the arguments and returns the arguments if any,
432 * or possibly an empty string.
435 gettrailer(char *arg
)
439 if ((options
= strchr(arg
, ' ')) == NULL
)
444 /* trim leading blanks */
445 while (*options
&& *options
== ' ')
452 parseopts(const char *opts
, int *howto
)
457 while (*opts
&& *opts
!= ' ') {
461 printf("-%c: unknown flag\n", *opts
);
474 parseboot(char *arg
, char **filename
, int *howto
)
481 /* if there were no arguments */
482 if (arg
== NULL
|| *arg
== '\0')
486 /* [[xxNx:]filename] [-adqsv] */
488 /* check for just args */
492 /* there's a file name */
495 opts
= gettrailer(arg
);
496 if (opts
== NULL
|| *opts
== '\0') {
498 } else if (*opts
!= '-') {
499 printf("invalid arguments\n");
505 /* at this point, we have dealt with filenames. */
507 /* now, deal with options */
509 if (parseopts(opts
, howto
) == 0) {