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