main.c revision 1.5 1 1.5 christos /* $NetBSD: main.c,v 1.5 1997/07/29 16:01:45 christos Exp $ */
2 1.1 perry
3 1.1 perry /*
4 1.1 perry * Copyright (c) 1996, 1997
5 1.1 perry * Matthias Drochner. All rights reserved.
6 1.1 perry * Copyright (c) 1996, 1997
7 1.1 perry * Perry E. Metzger. All rights reserved.
8 1.1 perry *
9 1.1 perry * Redistribution and use in source and binary forms, with or without
10 1.1 perry * modification, are permitted provided that the following conditions
11 1.1 perry * are met:
12 1.1 perry * 1. Redistributions of source code must retain the above copyright
13 1.1 perry * notice, this list of conditions and the following disclaimer.
14 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 perry * notice, this list of conditions and the following disclaimer in the
16 1.1 perry * documentation and/or other materials provided with the distribution.
17 1.1 perry * 3. All advertising materials mentioning features or use of this software
18 1.1 perry * must display the following acknowledgements:
19 1.1 perry * This product includes software developed for the NetBSD Project
20 1.1 perry * by Matthias Drochner.
21 1.1 perry * This product includes software developed for the NetBSD Project
22 1.1 perry * by Perry E. Metzger.
23 1.1 perry * 4. The names of the authors may not be used to endorse or promote products
24 1.1 perry * derived from this software without specific prior written permission.
25 1.1 perry *
26 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 1.1 perry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 1.1 perry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 1.1 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 1.1 perry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 1.1 perry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 1.1 perry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 1.1 perry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 1.1 perry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 1.1 perry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 1.1 perry */
37 1.1 perry
38 1.1 perry
39 1.1 perry #include <sys/reboot.h>
40 1.1 perry
41 1.1 perry #include <lib/libkern/libkern.h>
42 1.1 perry #include <lib/libsa/stand.h>
43 1.1 perry
44 1.1 perry #include <libi386.h>
45 1.1 perry
46 1.2 thorpej extern void ls __P((char *));
47 1.2 thorpej extern int getopt __P((int, char **, const char *));
48 1.1 perry
49 1.2 thorpej int errno;
50 1.2 thorpej static char *consdev;
51 1.1 perry
52 1.4 thorpej extern char bootprog_name[], bootprog_rev[], bootprog_date[],
53 1.4 thorpej bootprog_maker[];
54 1.1 perry
55 1.1 perry #define MAXDEVNAME 16
56 1.1 perry
57 1.2 thorpej static char *current_fsmode;
58 1.2 thorpej static char *default_devname;
59 1.2 thorpej static int default_unit, default_partition;
60 1.2 thorpej static char *default_filename;
61 1.1 perry
62 1.1 perry int
63 1.1 perry parsebootfile(fname, fsmode, devname, unit, partition, file)
64 1.2 thorpej const char *fname;
65 1.2 thorpej char **fsmode;
66 1.2 thorpej char **devname;/* out */
67 1.2 thorpej unsigned int *unit, *partition; /* out */
68 1.2 thorpej const char **file; /* out */
69 1.2 thorpej {
70 1.2 thorpej const char *col, *help;
71 1.2 thorpej
72 1.2 thorpej *fsmode = current_fsmode;
73 1.2 thorpej *devname = default_devname;
74 1.2 thorpej *unit = default_unit;
75 1.2 thorpej *partition = default_partition;
76 1.2 thorpej *file = default_filename;
77 1.2 thorpej
78 1.2 thorpej if (!fname)
79 1.2 thorpej return (0);
80 1.2 thorpej
81 1.2 thorpej if ((col = strchr(fname, ':'))) { /* device given */
82 1.2 thorpej static char savedevname[MAXDEVNAME + 1];
83 1.2 thorpej int devlen;
84 1.2 thorpej unsigned int u = 0, p = 0;
85 1.2 thorpej int i = 0;
86 1.2 thorpej
87 1.2 thorpej devlen = col - fname;
88 1.2 thorpej if (devlen > MAXDEVNAME)
89 1.2 thorpej return (EINVAL);
90 1.1 perry
91 1.1 perry #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
92 1.2 thorpej if (!isvalidname(fname[i]))
93 1.2 thorpej return (EINVAL);
94 1.2 thorpej do {
95 1.2 thorpej savedevname[i] = fname[i];
96 1.2 thorpej i++;
97 1.2 thorpej } while (isvalidname(fname[i]));
98 1.2 thorpej savedevname[i] = '\0';
99 1.1 perry
100 1.1 perry #define isnum(c) ((c) >= '0' && (c) <= '9')
101 1.2 thorpej if (i < devlen) {
102 1.2 thorpej if (!isnum(fname[i]))
103 1.2 thorpej return (EUNIT);
104 1.2 thorpej do {
105 1.2 thorpej u *= 10;
106 1.2 thorpej u += fname[i++] - '0';
107 1.2 thorpej } while (isnum(fname[i]));
108 1.2 thorpej }
109 1.1 perry #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
110 1.2 thorpej if (i < devlen) {
111 1.2 thorpej if (!isvalidpart(fname[i]))
112 1.2 thorpej return (EPART);
113 1.2 thorpej p = fname[i++] - 'a';
114 1.2 thorpej }
115 1.2 thorpej if (i != devlen)
116 1.2 thorpej return (ENXIO);
117 1.2 thorpej
118 1.2 thorpej *devname = savedevname;
119 1.2 thorpej *unit = u;
120 1.2 thorpej *partition = p;
121 1.2 thorpej help = col + 1;
122 1.2 thorpej } else
123 1.2 thorpej help = fname;
124 1.1 perry
125 1.2 thorpej if (*help)
126 1.2 thorpej *file = help;
127 1.1 perry
128 1.2 thorpej return (0);
129 1.1 perry }
130 1.1 perry
131 1.1 perry static void
132 1.1 perry print_bootsel(filename)
133 1.2 thorpej char *filename;
134 1.1 perry {
135 1.2 thorpej char *fsname;
136 1.2 thorpej char *devname;
137 1.2 thorpej int unit, partition;
138 1.2 thorpej const char *file;
139 1.2 thorpej
140 1.2 thorpej if (!parsebootfile(filename, &fsname, &devname, &unit,
141 1.2 thorpej &partition, &file)) {
142 1.2 thorpej if (!strcmp(fsname, "dos"))
143 1.2 thorpej printf("booting %s\n", file);
144 1.2 thorpej else if (!strcmp(fsname, "ufs"))
145 1.2 thorpej printf("booting %s%d%c:%s\n", devname, unit,
146 1.2 thorpej 'a' + partition, file);
147 1.2 thorpej }
148 1.1 perry }
149 1.1 perry
150 1.1 perry static void
151 1.1 perry bootit(filename, howto, tell)
152 1.2 thorpej const char *filename;
153 1.2 thorpej int howto, tell;
154 1.1 perry {
155 1.2 thorpej if (tell)
156 1.2 thorpej print_bootsel(filename);
157 1.1 perry
158 1.2 thorpej if (exec_netbsd(filename, 0, howto, 0, consdev) < 0)
159 1.2 thorpej printf("boot: %s\n", strerror(errno));
160 1.2 thorpej else
161 1.2 thorpej printf("boot returned\n");
162 1.2 thorpej }
163 1.2 thorpej
164 1.2 thorpej static void
165 1.2 thorpej helpme()
166 1.2 thorpej {
167 1.2 thorpej printf("commands are:\n"
168 1.2 thorpej "boot [xdNx:][filename] [-adrs]\n"
169 1.2 thorpej " (ex. \"sd0a:netbsd.old -s\"\n"
170 1.2 thorpej "ls [path]\n"
171 1.2 thorpej "mode ufs|dos\n"
172 1.2 thorpej "help|?\n"
173 1.2 thorpej "quit\n");
174 1.1 perry }
175 1.1 perry
176 1.1 perry /*
177 1.1 perry * chops the head from the arguments and returns the arguments if any,
178 1.1 perry * or possibly an empty string.
179 1.1 perry */
180 1.2 thorpej static char *
181 1.1 perry gettrailer(arg)
182 1.2 thorpej char *arg;
183 1.1 perry {
184 1.2 thorpej char *options;
185 1.1 perry
186 1.2 thorpej if ((options = strchr(arg, ' ')) == NULL)
187 1.2 thorpej options = "";
188 1.2 thorpej else
189 1.2 thorpej *options++ = '\0';
190 1.2 thorpej /* trim leading blanks */
191 1.2 thorpej while (*options && *options == ' ')
192 1.2 thorpej options++;
193 1.1 perry
194 1.2 thorpej return (options);
195 1.1 perry }
196 1.1 perry
197 1.1 perry static int
198 1.1 perry parseopts(opts, howto)
199 1.2 thorpej char *opts;
200 1.2 thorpej int *howto;
201 1.1 perry {
202 1.2 thorpej int tmpopt = 0;
203 1.1 perry
204 1.2 thorpej opts++; /* skip - */
205 1.2 thorpej while (*opts && *opts != ' ') {
206 1.2 thorpej tmpopt |= netbsd_opt(*opts);
207 1.2 thorpej if (tmpopt == -1) {
208 1.2 thorpej printf("-%c: unknown flag\n", *opts);
209 1.2 thorpej helpme();
210 1.2 thorpej return (0);
211 1.2 thorpej }
212 1.2 thorpej opts++;
213 1.2 thorpej }
214 1.2 thorpej *howto = tmpopt;
215 1.2 thorpej return (1);
216 1.1 perry }
217 1.1 perry
218 1.1 perry static int
219 1.1 perry parseboot(arg, filename, howto)
220 1.2 thorpej char *arg;
221 1.2 thorpej char **filename;
222 1.2 thorpej int *howto;
223 1.2 thorpej {
224 1.2 thorpej char *opts = NULL;
225 1.2 thorpej
226 1.2 thorpej *filename = 0;
227 1.2 thorpej *howto = 0;
228 1.2 thorpej
229 1.2 thorpej /* if there were no arguments */
230 1.2 thorpej if (!*arg)
231 1.2 thorpej return (1);
232 1.2 thorpej
233 1.2 thorpej /* format is... */
234 1.2 thorpej /* [[xxNx:]filename] [-adrs] */
235 1.2 thorpej
236 1.2 thorpej /* check for just args */
237 1.2 thorpej if (arg[0] == '-') {
238 1.2 thorpej opts = arg;
239 1.2 thorpej } else { /* at least a file name */
240 1.2 thorpej *filename = arg;
241 1.2 thorpej
242 1.2 thorpej opts = gettrailer(arg);
243 1.2 thorpej if (!*opts)
244 1.2 thorpej opts = NULL;
245 1.2 thorpej else if (*opts != '-') {
246 1.2 thorpej printf("invalid arguments\n");
247 1.2 thorpej helpme();
248 1.2 thorpej return (0);
249 1.2 thorpej }
250 1.2 thorpej }
251 1.2 thorpej /* at this point, we have dealt with filenames. */
252 1.1 perry
253 1.2 thorpej /* now, deal with options */
254 1.2 thorpej if (opts) {
255 1.2 thorpej if (!parseopts(opts, howto))
256 1.2 thorpej return (0);
257 1.2 thorpej }
258 1.2 thorpej return (1);
259 1.1 perry }
260 1.1 perry
261 1.1 perry static void
262 1.1 perry parsemode(arg, mode)
263 1.2 thorpej char *arg;
264 1.2 thorpej char **mode;
265 1.1 perry {
266 1.2 thorpej if (!strcmp("dos", arg))
267 1.2 thorpej *mode = "dos";
268 1.2 thorpej else if (!strcmp("ufs", arg))
269 1.2 thorpej *mode = "ufs";
270 1.2 thorpej else
271 1.2 thorpej printf("invalid mode\n");
272 1.1 perry }
273 1.1 perry
274 1.1 perry static void
275 1.1 perry docommand(arg)
276 1.2 thorpej char *arg;
277 1.1 perry {
278 1.2 thorpej char *options;
279 1.1 perry
280 1.2 thorpej options = gettrailer(arg);
281 1.1 perry
282 1.2 thorpej if ((strcmp("help", arg) == 0) ||
283 1.2 thorpej (strcmp("?", arg) == 0)) {
284 1.2 thorpej helpme();
285 1.2 thorpej return;
286 1.2 thorpej }
287 1.2 thorpej if (strcmp("ls", arg) == 0) {
288 1.2 thorpej char *help = default_filename;
289 1.2 thorpej if (strcmp(current_fsmode, "ufs")) {
290 1.2 thorpej printf("UFS only\n");
291 1.2 thorpej return;
292 1.2 thorpej }
293 1.2 thorpej default_filename = "/";
294 1.2 thorpej ls(options);
295 1.2 thorpej default_filename = help;
296 1.2 thorpej return;
297 1.2 thorpej }
298 1.2 thorpej if (strcmp("quit", arg) == 0) {
299 1.2 thorpej printf("Exiting... goodbye...\n");
300 1.2 thorpej exit(0);
301 1.2 thorpej }
302 1.2 thorpej if (strcmp("boot", arg) == 0) {
303 1.2 thorpej char *filename;
304 1.2 thorpej int howto;
305 1.2 thorpej if (parseboot(options, &filename, &howto))
306 1.2 thorpej bootit(filename, howto, 1);
307 1.2 thorpej return;
308 1.2 thorpej }
309 1.2 thorpej if (strcmp("mode", arg) == 0) {
310 1.2 thorpej parsemode(options, ¤t_fsmode);
311 1.2 thorpej return;
312 1.2 thorpej }
313 1.2 thorpej printf("unknown command\n");
314 1.2 thorpej helpme();
315 1.2 thorpej }
316 1.2 thorpej
317 1.2 thorpej void
318 1.2 thorpej bootmenu()
319 1.2 thorpej {
320 1.2 thorpej printf("\ntype \"?\" or \"help\" for help.\n");
321 1.2 thorpej for (;;) {
322 1.2 thorpej char input[80];
323 1.2 thorpej
324 1.2 thorpej input[0] = '\0';
325 1.2 thorpej printf("> ");
326 1.2 thorpej gets(input);
327 1.1 perry
328 1.2 thorpej docommand(input);
329 1.2 thorpej }
330 1.1 perry }
331 1.1 perry
332 1.1 perry static void
333 1.1 perry print_banner(void)
334 1.1 perry {
335 1.5 christos int extmem = getextmem();
336 1.5 christos char *s = "";
337 1.5 christos
338 1.5 christos #ifdef XMS
339 1.5 christos if (extmem == 0) {
340 1.5 christos if ((extmem = checkxms()) != 0)
341 1.5 christos s = "(xms) ";
342 1.5 christos }
343 1.5 christos #endif
344 1.4 thorpej
345 1.4 thorpej printf("\n");
346 1.4 thorpej printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
347 1.4 thorpej printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
348 1.5 christos printf(">> Memory: %d/%d %sk\n", getbasemem(), extmem, s);
349 1.2 thorpej }
350 1.2 thorpej
351 1.2 thorpej void
352 1.2 thorpej usage()
353 1.2 thorpej {
354 1.2 thorpej printf("dosboot [-u] [-c <commands>] [-i] [filename [-bootopts]]\n");
355 1.2 thorpej }
356 1.2 thorpej
357 1.2 thorpej int
358 1.2 thorpej main(argc, argv)
359 1.2 thorpej int argc;
360 1.2 thorpej char **argv;
361 1.2 thorpej {
362 1.2 thorpej int ch;
363 1.2 thorpej int interactive = 0;
364 1.2 thorpej int howto;
365 1.2 thorpej extern char *optarg;
366 1.2 thorpej extern int optind;
367 1.2 thorpej
368 1.2 thorpej consdev = initio(CONSDEV_PC);
369 1.2 thorpej gateA20();
370 1.2 thorpej
371 1.2 thorpej print_banner();
372 1.2 thorpej
373 1.2 thorpej current_fsmode = "dos";
374 1.2 thorpej default_devname = "hd";
375 1.2 thorpej default_unit = 0;
376 1.2 thorpej default_partition = 0;
377 1.2 thorpej default_filename = "netbsd";
378 1.2 thorpej
379 1.2 thorpej while ((ch = getopt(argc, argv, "c:iu")) != -1) {
380 1.2 thorpej switch (ch) {
381 1.2 thorpej case 'c':
382 1.2 thorpej docommand(optarg);
383 1.2 thorpej return (1);
384 1.2 thorpej break;
385 1.2 thorpej case 'i':
386 1.2 thorpej interactive = 1;
387 1.2 thorpej break;
388 1.2 thorpej case 'u':
389 1.2 thorpej current_fsmode = "ufs";
390 1.2 thorpej break;
391 1.2 thorpej default:
392 1.2 thorpej usage();
393 1.2 thorpej return (1);
394 1.2 thorpej }
395 1.2 thorpej }
396 1.2 thorpej
397 1.2 thorpej if (interactive)
398 1.2 thorpej bootmenu();
399 1.2 thorpej
400 1.2 thorpej argc -= optind;
401 1.2 thorpej argv += optind;
402 1.2 thorpej
403 1.2 thorpej if (argc > 2) {
404 1.2 thorpej usage();
405 1.2 thorpej return (1);
406 1.2 thorpej }
407 1.2 thorpej howto = 0;
408 1.2 thorpej if (argc > 1 && !parseopts(argv[1], &howto))
409 1.2 thorpej return (1);
410 1.1 perry
411 1.2 thorpej bootit((argc > 0 ? argv[0] : "netbsd"), howto, 1);
412 1.2 thorpej return (1);
413 1.1 perry }
414