boot.c revision 1.1.2.2 1 1.1.2.2 skrll /* $NetBSD: boot.c,v 1.1.2.2 2009/03/03 18:29:50 skrll Exp $ */
2 1.1.2.2 skrll
3 1.1.2.2 skrll /*
4 1.1.2.2 skrll * Copyright (c) 2009 NONAKA Kimihiro <nonaka (at) netbsd.org>
5 1.1.2.2 skrll * All rights reserved.
6 1.1.2.2 skrll *
7 1.1.2.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 skrll * modification, are permitted provided that the following conditions
9 1.1.2.2 skrll * are met:
10 1.1.2.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 skrll * documentation and/or other materials provided with the distribution.
15 1.1.2.2 skrll *
16 1.1.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 skrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1.2.2 skrll * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1.2.2 skrll * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1.2.2 skrll * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1.2.2 skrll * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1.2.2 skrll * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1.2.2 skrll */
27 1.1.2.2 skrll
28 1.1.2.2 skrll #include <sys/types.h>
29 1.1.2.2 skrll #include <sys/bootblock.h>
30 1.1.2.2 skrll #include <sys/boot_flag.h>
31 1.1.2.2 skrll
32 1.1.2.2 skrll #include "boot.h"
33 1.1.2.2 skrll #include "bootinfo.h"
34 1.1.2.2 skrll #include "bootmenu.h"
35 1.1.2.2 skrll #include "disk.h"
36 1.1.2.2 skrll #include "unixdev.h"
37 1.1.2.2 skrll #include "pathnames.h"
38 1.1.2.2 skrll
39 1.1.2.2 skrll #include <lib/libsa/loadfile.h>
40 1.1.2.2 skrll #include <lib/libsa/ufs.h>
41 1.1.2.2 skrll
42 1.1.2.2 skrll #include "compat_linux.h"
43 1.1.2.2 skrll
44 1.1.2.2 skrll static const char * const names[][2] = {
45 1.1.2.2 skrll { "netbsd", "netbsd.gz" },
46 1.1.2.2 skrll { "netbsd.old", "netbsd.old.gz", },
47 1.1.2.2 skrll { "onetbsd", "onetbsd.gz" },
48 1.1.2.2 skrll };
49 1.1.2.2 skrll
50 1.1.2.2 skrll char *default_devname;
51 1.1.2.2 skrll uint default_unit, default_partition;
52 1.1.2.2 skrll const char *default_filename;
53 1.1.2.2 skrll int default_timeout = 5;
54 1.1.2.2 skrll
55 1.1.2.2 skrll static char probed_disks[256];
56 1.1.2.2 skrll
57 1.1.2.2 skrll static void bootcmd_help(char *);
58 1.1.2.2 skrll static void bootcmd_ls(char *);
59 1.1.2.2 skrll static void bootcmd_quit(char *);
60 1.1.2.2 skrll static void bootcmd_boot(char *);
61 1.1.2.2 skrll static void bootcmd_disk(char *);
62 1.1.2.2 skrll #ifdef SUPPORT_CONSDEV
63 1.1.2.2 skrll static void bootcmd_consdev(char *);
64 1.1.2.2 skrll #endif
65 1.1.2.2 skrll
66 1.1.2.2 skrll static const struct bootblk_command {
67 1.1.2.2 skrll const char *c_name;
68 1.1.2.2 skrll void (*c_fn)(char *arg);
69 1.1.2.2 skrll } bootcmds[] = {
70 1.1.2.2 skrll { "help", bootcmd_help },
71 1.1.2.2 skrll { "?", bootcmd_help },
72 1.1.2.2 skrll { "quit", bootcmd_quit },
73 1.1.2.2 skrll { "ls", bootcmd_ls },
74 1.1.2.2 skrll { "boot", bootcmd_boot },
75 1.1.2.2 skrll { "disk", bootcmd_disk },
76 1.1.2.2 skrll #ifdef SUPPORT_CONSDEV
77 1.1.2.2 skrll { "consdev", bootcmd_consdev },
78 1.1.2.2 skrll #endif
79 1.1.2.2 skrll { NULL, NULL },
80 1.1.2.2 skrll };
81 1.1.2.2 skrll
82 1.1.2.2 skrll static struct btinfo_howto bi_howto;
83 1.1.2.2 skrll
84 1.1.2.2 skrll static void print_banner(void);
85 1.1.2.2 skrll static int exec_netbsd(const char *file, int howto);
86 1.1.2.2 skrll
87 1.1.2.2 skrll int
88 1.1.2.2 skrll parsebootfile(const char *fname, char **fsname, char **devname,
89 1.1.2.2 skrll uint *unit, uint *partition, const char **file)
90 1.1.2.2 skrll {
91 1.1.2.2 skrll const char *col;
92 1.1.2.2 skrll
93 1.1.2.2 skrll *fsname = "ufs";
94 1.1.2.2 skrll *devname = default_devname;
95 1.1.2.2 skrll *unit = default_unit;
96 1.1.2.2 skrll *partition = default_partition;
97 1.1.2.2 skrll *file = default_filename;
98 1.1.2.2 skrll
99 1.1.2.2 skrll if (fname == NULL)
100 1.1.2.2 skrll return 0;
101 1.1.2.2 skrll
102 1.1.2.2 skrll if ((col = strchr(fname, ':'))) { /* device given */
103 1.1.2.2 skrll static char savedevname[MAXDEVNAME+1];
104 1.1.2.2 skrll int devlen;
105 1.1.2.2 skrll unsigned int u = 0, p = 0;
106 1.1.2.2 skrll int i = 0;
107 1.1.2.2 skrll
108 1.1.2.2 skrll devlen = col - fname;
109 1.1.2.2 skrll if (devlen > MAXDEVNAME)
110 1.1.2.2 skrll return EINVAL;
111 1.1.2.2 skrll
112 1.1.2.2 skrll #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
113 1.1.2.2 skrll if (!isvalidname(fname[i]))
114 1.1.2.2 skrll return EINVAL;
115 1.1.2.2 skrll do {
116 1.1.2.2 skrll savedevname[i] = fname[i];
117 1.1.2.2 skrll i++;
118 1.1.2.2 skrll } while (isvalidname(fname[i]));
119 1.1.2.2 skrll savedevname[i] = '\0';
120 1.1.2.2 skrll
121 1.1.2.2 skrll #define isnum(c) ((c) >= '0' && (c) <= '9')
122 1.1.2.2 skrll if (i < devlen) {
123 1.1.2.2 skrll if (!isnum(fname[i]))
124 1.1.2.2 skrll return (EUNIT);
125 1.1.2.2 skrll do {
126 1.1.2.2 skrll u *= 10;
127 1.1.2.2 skrll u += fname[i++] - '0';
128 1.1.2.2 skrll } while (isnum(fname[i]));
129 1.1.2.2 skrll }
130 1.1.2.2 skrll
131 1.1.2.2 skrll #define isvalidpart(c) ((c) >= 'a' && (c) <= 'a' + MAXPARTITIONS)
132 1.1.2.2 skrll if (i < devlen) {
133 1.1.2.2 skrll if (!isvalidpart(fname[i]))
134 1.1.2.2 skrll return (EPART);
135 1.1.2.2 skrll p = fname[i++] - 'a';
136 1.1.2.2 skrll }
137 1.1.2.2 skrll
138 1.1.2.2 skrll if (i != devlen)
139 1.1.2.2 skrll return ENXIO;
140 1.1.2.2 skrll
141 1.1.2.2 skrll *devname = savedevname;
142 1.1.2.2 skrll *unit = u;
143 1.1.2.2 skrll *partition = p;
144 1.1.2.2 skrll fname = col + 1;
145 1.1.2.2 skrll }
146 1.1.2.2 skrll
147 1.1.2.2 skrll if (*fname)
148 1.1.2.2 skrll *file = fname;
149 1.1.2.2 skrll
150 1.1.2.2 skrll return 0;
151 1.1.2.2 skrll }
152 1.1.2.2 skrll
153 1.1.2.2 skrll char *
154 1.1.2.2 skrll sprint_bootsel(const char *filename)
155 1.1.2.2 skrll {
156 1.1.2.2 skrll static char buf[80];
157 1.1.2.2 skrll char *fsname, *devname;
158 1.1.2.2 skrll uint unit, partition;
159 1.1.2.2 skrll const char *file;
160 1.1.2.2 skrll
161 1.1.2.2 skrll if (parsebootfile(filename, &fsname, &devname, &unit, &partition,
162 1.1.2.2 skrll &file) == 0) {
163 1.1.2.2 skrll snprintf(buf, sizeof(buf), "%s%d%c:%s", devname, unit,
164 1.1.2.2 skrll 'a' + partition, file);
165 1.1.2.2 skrll return buf;
166 1.1.2.2 skrll }
167 1.1.2.2 skrll return "(invalid)";
168 1.1.2.2 skrll }
169 1.1.2.2 skrll
170 1.1.2.2 skrll static void
171 1.1.2.2 skrll print_banner(void)
172 1.1.2.2 skrll {
173 1.1.2.2 skrll extern const char bootprog_name[];
174 1.1.2.2 skrll extern const char bootprog_rev[];
175 1.1.2.2 skrll extern const char bootprog_date[];
176 1.1.2.2 skrll extern const char bootprog_maker[];
177 1.1.2.2 skrll
178 1.1.2.2 skrll printf("\n");
179 1.1.2.2 skrll printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
180 1.1.2.2 skrll printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
181 1.1.2.2 skrll }
182 1.1.2.2 skrll
183 1.1.2.2 skrll void
184 1.1.2.2 skrll boot(dev_t bootdev)
185 1.1.2.2 skrll {
186 1.1.2.2 skrll extern char twiddle_toggle;
187 1.1.2.2 skrll int currname;
188 1.1.2.2 skrll int c;
189 1.1.2.2 skrll
190 1.1.2.2 skrll consinit(CONSDEV_GLASS, -1);
191 1.1.2.2 skrll
192 1.1.2.2 skrll twiddle_toggle = 1; /* no twiddling until we're ready */
193 1.1.2.2 skrll
194 1.1.2.2 skrll /* set default value: hd0a:netbsd */
195 1.1.2.2 skrll default_devname = "hd";
196 1.1.2.2 skrll default_unit = 0;
197 1.1.2.2 skrll default_partition = 0;
198 1.1.2.2 skrll default_filename = names[0][0];
199 1.1.2.2 skrll
200 1.1.2.2 skrll diskprobe(probed_disks, sizeof(probed_disks));
201 1.1.2.2 skrll
202 1.1.2.2 skrll parsebootconf(_PATH_BOOTCONF);
203 1.1.2.2 skrll
204 1.1.2.2 skrll #ifdef SUPPORT_CONSDEV
205 1.1.2.2 skrll /*
206 1.1.2.2 skrll * If console set in boot.cfg, switch to it.
207 1.1.2.2 skrll * This will print the banner, so we don't need to explicitly do it
208 1.1.2.2 skrll */
209 1.1.2.2 skrll if (bootconf.consdev)
210 1.1.2.2 skrll bootcmd_consdev(bootconf.consdev);
211 1.1.2.2 skrll else
212 1.1.2.2 skrll #endif
213 1.1.2.2 skrll print_banner();
214 1.1.2.2 skrll
215 1.1.2.2 skrll printf("\ndisks: %s\n", probed_disks);
216 1.1.2.2 skrll
217 1.1.2.2 skrll /* Display the menu, if applicable */
218 1.1.2.2 skrll twiddle_toggle = 0;
219 1.1.2.2 skrll if (bootconf.nummenu > 0) {
220 1.1.2.2 skrll /* Does not return */
221 1.1.2.2 skrll doboottypemenu();
222 1.1.2.2 skrll }
223 1.1.2.2 skrll
224 1.1.2.2 skrll printf("Press return to boot now, any other key for boot menu\n");
225 1.1.2.2 skrll currname = 0;
226 1.1.2.2 skrll for (currname = 0; currname < __arraycount(names); currname++) {
227 1.1.2.2 skrll printf("booting %s - starting in ",
228 1.1.2.2 skrll sprint_bootsel(names[currname][0]));
229 1.1.2.2 skrll
230 1.1.2.2 skrll c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1);
231 1.1.2.2 skrll if ((c != '\r') && (c != '\n') && (c != '\0')) {
232 1.1.2.2 skrll printf("type \"?\" or \"help\" for help.\n");
233 1.1.2.2 skrll bootmenu(); /* does not return */
234 1.1.2.2 skrll }
235 1.1.2.2 skrll
236 1.1.2.2 skrll /*
237 1.1.2.2 skrll * try pairs of names[] entries, foo and foo.gz
238 1.1.2.2 skrll */
239 1.1.2.2 skrll /* don't print "booting..." again */
240 1.1.2.2 skrll bootit(names[currname][0], 0, 0);
241 1.1.2.2 skrll /* since it failed, try compressed bootfile. */
242 1.1.2.2 skrll bootit(names[currname][1], 0, 1);
243 1.1.2.2 skrll }
244 1.1.2.2 skrll
245 1.1.2.2 skrll bootmenu(); /* does not return */
246 1.1.2.2 skrll }
247 1.1.2.2 skrll
248 1.1.2.2 skrll void
249 1.1.2.2 skrll bootit(const char *filename, int howto, int tell)
250 1.1.2.2 skrll {
251 1.1.2.2 skrll
252 1.1.2.2 skrll if (tell) {
253 1.1.2.2 skrll printf("booting %s", sprint_bootsel(filename));
254 1.1.2.2 skrll if (howto)
255 1.1.2.2 skrll printf(" (howto 0x%x)", howto);
256 1.1.2.2 skrll printf("\n");
257 1.1.2.2 skrll }
258 1.1.2.2 skrll
259 1.1.2.2 skrll if (exec_netbsd(filename, howto) < 0) {
260 1.1.2.2 skrll printf("boot: %s: %s\n", sprint_bootsel(filename),
261 1.1.2.2 skrll strerror(errno));
262 1.1.2.2 skrll } else {
263 1.1.2.2 skrll printf("boot returned\n");
264 1.1.2.2 skrll }
265 1.1.2.2 skrll }
266 1.1.2.2 skrll
267 1.1.2.2 skrll static int
268 1.1.2.2 skrll exec_netbsd(const char *file, int howto)
269 1.1.2.2 skrll {
270 1.1.2.2 skrll u_long marks[MARK_MAX];
271 1.1.2.2 skrll
272 1.1.2.2 skrll BI_ALLOC(BTINFO_MAX);
273 1.1.2.2 skrll
274 1.1.2.2 skrll bi_howto.howto = howto;
275 1.1.2.2 skrll BI_ADD(&bi_howto, BTINFO_HOWTO, sizeof(bi_howto));
276 1.1.2.2 skrll
277 1.1.2.2 skrll if (loadfile_zboot(file, marks, LOAD_KERNEL) == -1)
278 1.1.2.2 skrll goto out;
279 1.1.2.2 skrll
280 1.1.2.2 skrll /*NOTREACHED*/
281 1.1.2.2 skrll return 0;
282 1.1.2.2 skrll
283 1.1.2.2 skrll out:
284 1.1.2.2 skrll BI_FREE();
285 1.1.2.2 skrll bootinfo = 0;
286 1.1.2.2 skrll return -1;
287 1.1.2.2 skrll }
288 1.1.2.2 skrll
289 1.1.2.2 skrll /*
290 1.1.2.2 skrll * bootmenu
291 1.1.2.2 skrll */
292 1.1.2.2 skrll static char *gettrailer(char *arg);
293 1.1.2.2 skrll static int parseopts(const char *opts, int *howto);
294 1.1.2.2 skrll static int parseboot(char *arg, char **filename, int *howto);
295 1.1.2.2 skrll
296 1.1.2.2 skrll /* ARGSUSED */
297 1.1.2.2 skrll static void
298 1.1.2.2 skrll bootcmd_help(char *arg)
299 1.1.2.2 skrll {
300 1.1.2.2 skrll
301 1.1.2.2 skrll printf("commands are:\n"
302 1.1.2.2 skrll "boot [xdNx:][filename] [-acdqsv]\n"
303 1.1.2.2 skrll " (ex. \"hd0a:netbsd.old -s\"\n"
304 1.1.2.2 skrll "ls [path]\n"
305 1.1.2.2 skrll #ifdef SUPPORT_CONSDEV
306 1.1.2.2 skrll "consdev {glass|com [speed]}\n"
307 1.1.2.2 skrll #endif
308 1.1.2.2 skrll "disk\n"
309 1.1.2.2 skrll "help|?\n"
310 1.1.2.2 skrll "quit\n");
311 1.1.2.2 skrll }
312 1.1.2.2 skrll
313 1.1.2.2 skrll /* ARGSUSED */
314 1.1.2.2 skrll static void
315 1.1.2.2 skrll bootcmd_quit(char *arg)
316 1.1.2.2 skrll {
317 1.1.2.2 skrll
318 1.1.2.2 skrll printf("Exiting...\n");
319 1.1.2.2 skrll exit(0);
320 1.1.2.2 skrll /*NOTREACHED*/
321 1.1.2.2 skrll }
322 1.1.2.2 skrll
323 1.1.2.2 skrll static void
324 1.1.2.2 skrll bootcmd_ls(char *arg)
325 1.1.2.2 skrll {
326 1.1.2.2 skrll const char *save = default_filename;
327 1.1.2.2 skrll
328 1.1.2.2 skrll default_filename = "/";
329 1.1.2.2 skrll ufs_ls(arg);
330 1.1.2.2 skrll default_filename = save;
331 1.1.2.2 skrll }
332 1.1.2.2 skrll
333 1.1.2.2 skrll static void
334 1.1.2.2 skrll bootcmd_boot(char *arg)
335 1.1.2.2 skrll {
336 1.1.2.2 skrll char *filename;
337 1.1.2.2 skrll int howto;
338 1.1.2.2 skrll
339 1.1.2.2 skrll if (parseboot(arg, &filename, &howto)) {
340 1.1.2.2 skrll bootit(filename, howto, 1);
341 1.1.2.2 skrll }
342 1.1.2.2 skrll }
343 1.1.2.2 skrll
344 1.1.2.2 skrll /* ARGSUSED */
345 1.1.2.2 skrll static void
346 1.1.2.2 skrll bootcmd_disk(char *arg)
347 1.1.2.2 skrll {
348 1.1.2.2 skrll
349 1.1.2.2 skrll printf("disks: %s\n", probed_disks);
350 1.1.2.2 skrll }
351 1.1.2.2 skrll
352 1.1.2.2 skrll #ifdef SUPPORT_CONSDEV
353 1.1.2.2 skrll static const struct cons_devs {
354 1.1.2.2 skrll const char *name;
355 1.1.2.2 skrll int tag;
356 1.1.2.2 skrll } cons_devs[] = {
357 1.1.2.2 skrll { "glass", CONSDEV_GLASS },
358 1.1.2.2 skrll { "com", CONSDEV_COM0 },
359 1.1.2.2 skrll { "com0", CONSDEV_COM0 },
360 1.1.2.2 skrll { NULL, 0 }
361 1.1.2.2 skrll };
362 1.1.2.2 skrll
363 1.1.2.2 skrll static void
364 1.1.2.2 skrll bootcmd_consdev(char *arg)
365 1.1.2.2 skrll {
366 1.1.2.2 skrll const struct cons_devs *cdp;
367 1.1.2.2 skrll char *p;
368 1.1.2.2 skrll int speed = 9600;
369 1.1.2.2 skrll
370 1.1.2.2 skrll p = strchr(arg, ' ');
371 1.1.2.2 skrll if (p != NULL) {
372 1.1.2.2 skrll *p++ = '\0';
373 1.1.2.2 skrll speed = atoi(p);
374 1.1.2.2 skrll }
375 1.1.2.2 skrll for (cdp = cons_devs; cdp->name != NULL; cdp++) {
376 1.1.2.2 skrll if (strcmp(arg, cdp->name) == 0) {
377 1.1.2.2 skrll consinit(cdp->tag, speed);
378 1.1.2.2 skrll print_banner();
379 1.1.2.2 skrll return;
380 1.1.2.2 skrll }
381 1.1.2.2 skrll }
382 1.1.2.2 skrll printf("invalid console device.\n");
383 1.1.2.2 skrll }
384 1.1.2.2 skrll #endif
385 1.1.2.2 skrll
386 1.1.2.2 skrll void
387 1.1.2.2 skrll docommand(char *arg)
388 1.1.2.2 skrll {
389 1.1.2.2 skrll char *options;
390 1.1.2.2 skrll int i;
391 1.1.2.2 skrll
392 1.1.2.2 skrll options = gettrailer(arg);
393 1.1.2.2 skrll
394 1.1.2.2 skrll for (i = 0; bootcmds[i].c_name != NULL; i++) {
395 1.1.2.2 skrll if (strcmp(arg, bootcmds[i].c_name) == 0) {
396 1.1.2.2 skrll (*bootcmds[i].c_fn)(options);
397 1.1.2.2 skrll return;
398 1.1.2.2 skrll }
399 1.1.2.2 skrll }
400 1.1.2.2 skrll
401 1.1.2.2 skrll printf("unknown command\n");
402 1.1.2.2 skrll bootcmd_help(NULL);
403 1.1.2.2 skrll }
404 1.1.2.2 skrll
405 1.1.2.2 skrll void
406 1.1.2.2 skrll bootmenu(void)
407 1.1.2.2 skrll {
408 1.1.2.2 skrll char input[256];
409 1.1.2.2 skrll char *c;
410 1.1.2.2 skrll
411 1.1.2.2 skrll for (;;) {
412 1.1.2.2 skrll c = input;
413 1.1.2.2 skrll
414 1.1.2.2 skrll input[0] = '\0';
415 1.1.2.2 skrll printf("> ");
416 1.1.2.2 skrll gets(input);
417 1.1.2.2 skrll
418 1.1.2.2 skrll /*
419 1.1.2.2 skrll * Skip leading whitespace.
420 1.1.2.2 skrll */
421 1.1.2.2 skrll while (*c == ' ') {
422 1.1.2.2 skrll c++;
423 1.1.2.2 skrll }
424 1.1.2.2 skrll if (*c != '\0') {
425 1.1.2.2 skrll docommand(c);
426 1.1.2.2 skrll }
427 1.1.2.2 skrll }
428 1.1.2.2 skrll }
429 1.1.2.2 skrll
430 1.1.2.2 skrll /*
431 1.1.2.2 skrll * chops the head from the arguments and returns the arguments if any,
432 1.1.2.2 skrll * or possibly an empty string.
433 1.1.2.2 skrll */
434 1.1.2.2 skrll static char *
435 1.1.2.2 skrll gettrailer(char *arg)
436 1.1.2.2 skrll {
437 1.1.2.2 skrll char *options;
438 1.1.2.2 skrll
439 1.1.2.2 skrll if ((options = strchr(arg, ' ')) == NULL)
440 1.1.2.2 skrll return ("");
441 1.1.2.2 skrll else
442 1.1.2.2 skrll *options++ = '\0';
443 1.1.2.2 skrll
444 1.1.2.2 skrll /* trim leading blanks */
445 1.1.2.2 skrll while (*options && *options == ' ')
446 1.1.2.2 skrll options++;
447 1.1.2.2 skrll
448 1.1.2.2 skrll return options;
449 1.1.2.2 skrll }
450 1.1.2.2 skrll
451 1.1.2.2 skrll static int
452 1.1.2.2 skrll parseopts(const char *opts, int *howto)
453 1.1.2.2 skrll {
454 1.1.2.2 skrll int r, tmpopt = 0;
455 1.1.2.2 skrll
456 1.1.2.2 skrll opts++; /* skip - */
457 1.1.2.2 skrll while (*opts && *opts != ' ') {
458 1.1.2.2 skrll r = 0;
459 1.1.2.2 skrll BOOT_FLAG(*opts, r);
460 1.1.2.2 skrll if (r == 0) {
461 1.1.2.2 skrll printf("-%c: unknown flag\n", *opts);
462 1.1.2.2 skrll bootcmd_help(NULL);
463 1.1.2.2 skrll return 0;
464 1.1.2.2 skrll }
465 1.1.2.2 skrll tmpopt |= r;
466 1.1.2.2 skrll opts++;
467 1.1.2.2 skrll }
468 1.1.2.2 skrll
469 1.1.2.2 skrll *howto = tmpopt;
470 1.1.2.2 skrll return 1;
471 1.1.2.2 skrll }
472 1.1.2.2 skrll
473 1.1.2.2 skrll static int
474 1.1.2.2 skrll parseboot(char *arg, char **filename, int *howto)
475 1.1.2.2 skrll {
476 1.1.2.2 skrll char *opts = NULL;
477 1.1.2.2 skrll
478 1.1.2.2 skrll *filename = 0;
479 1.1.2.2 skrll *howto = 0;
480 1.1.2.2 skrll
481 1.1.2.2 skrll /* if there were no arguments */
482 1.1.2.2 skrll if (arg == NULL || *arg == '\0')
483 1.1.2.2 skrll return 1;
484 1.1.2.2 skrll
485 1.1.2.2 skrll /* format is... */
486 1.1.2.2 skrll /* [[xxNx:]filename] [-adqsv] */
487 1.1.2.2 skrll
488 1.1.2.2 skrll /* check for just args */
489 1.1.2.2 skrll if (arg[0] == '-') {
490 1.1.2.2 skrll opts = arg;
491 1.1.2.2 skrll } else {
492 1.1.2.2 skrll /* there's a file name */
493 1.1.2.2 skrll *filename = arg;
494 1.1.2.2 skrll
495 1.1.2.2 skrll opts = gettrailer(arg);
496 1.1.2.2 skrll if (opts == NULL || *opts == '\0') {
497 1.1.2.2 skrll opts = NULL;
498 1.1.2.2 skrll } else if (*opts != '-') {
499 1.1.2.2 skrll printf("invalid arguments\n");
500 1.1.2.2 skrll bootcmd_help(NULL);
501 1.1.2.2 skrll return 0;
502 1.1.2.2 skrll }
503 1.1.2.2 skrll }
504 1.1.2.2 skrll
505 1.1.2.2 skrll /* at this point, we have dealt with filenames. */
506 1.1.2.2 skrll
507 1.1.2.2 skrll /* now, deal with options */
508 1.1.2.2 skrll if (opts) {
509 1.1.2.2 skrll if (parseopts(opts, howto) == 0) {
510 1.1.2.2 skrll return 0;
511 1.1.2.2 skrll }
512 1.1.2.2 skrll }
513 1.1.2.2 skrll return 1;
514 1.1.2.2 skrll }
515