main.c revision 1.29 1 1.29 ad /* $NetBSD: main.c,v 1.29 2009/03/21 15:01:56 ad 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.12 drochner #include <lib/libsa/ufs.h>
44 1.1 perry
45 1.1 perry #include <libi386.h>
46 1.1 perry
47 1.7 drochner #ifdef SUPPORT_LYNX
48 1.21 dyoung extern int exec_lynx(const char*, int);
49 1.7 drochner #endif
50 1.7 drochner
51 1.12 drochner int errno;
52 1.1 perry
53 1.23 perry extern char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
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.21 dyoung char *sprint_bootsel(const char *);
63 1.21 dyoung static void bootit(const char *, int, int);
64 1.21 dyoung void usage(void);
65 1.21 dyoung int main(int, char **);
66 1.21 dyoung
67 1.21 dyoung void command_help(char *);
68 1.21 dyoung void command_ls(char *);
69 1.21 dyoung void command_quit(char *);
70 1.21 dyoung void command_boot(char *);
71 1.21 dyoung void command_mode(char *);
72 1.21 dyoung void command_dev(char *);
73 1.7 drochner
74 1.17 jdolecek const struct bootblk_command commands[] = {
75 1.7 drochner { "help", command_help },
76 1.7 drochner { "?", command_help },
77 1.7 drochner { "ls", command_ls },
78 1.7 drochner { "quit", command_quit },
79 1.7 drochner { "boot", command_boot },
80 1.7 drochner { "mode", command_mode },
81 1.9 drochner { "dev", command_dev },
82 1.7 drochner { NULL, NULL },
83 1.7 drochner };
84 1.7 drochner
85 1.1 perry int
86 1.27 dsl parsebootfile(const char *fname, char **fsmode, char **devname, int *unit, int *partition, const char **file)
87 1.27 dsl /* fsmode: out */
88 1.27 dsl /* devname: out */
89 1.27 dsl /* unit, *partition: out */
90 1.27 dsl /* file: out */
91 1.2 thorpej {
92 1.2 thorpej const char *col, *help;
93 1.2 thorpej
94 1.2 thorpej *fsmode = current_fsmode;
95 1.2 thorpej *devname = default_devname;
96 1.2 thorpej *unit = default_unit;
97 1.2 thorpej *partition = default_partition;
98 1.2 thorpej *file = default_filename;
99 1.2 thorpej
100 1.7 drochner if (fname == NULL)
101 1.2 thorpej return (0);
102 1.2 thorpej
103 1.9 drochner if (strcmp(current_fsmode, "dos") && (col = strchr(fname, ':'))) {
104 1.9 drochner /* no DOS, device given */
105 1.2 thorpej static char savedevname[MAXDEVNAME + 1];
106 1.2 thorpej int devlen;
107 1.2 thorpej unsigned int u = 0, p = 0;
108 1.2 thorpej int i = 0;
109 1.2 thorpej
110 1.2 thorpej devlen = col - fname;
111 1.2 thorpej if (devlen > MAXDEVNAME)
112 1.2 thorpej return (EINVAL);
113 1.1 perry
114 1.1 perry #define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
115 1.2 thorpej if (!isvalidname(fname[i]))
116 1.2 thorpej return (EINVAL);
117 1.2 thorpej do {
118 1.2 thorpej savedevname[i] = fname[i];
119 1.2 thorpej i++;
120 1.2 thorpej } while (isvalidname(fname[i]));
121 1.2 thorpej savedevname[i] = '\0';
122 1.1 perry
123 1.1 perry #define isnum(c) ((c) >= '0' && (c) <= '9')
124 1.2 thorpej if (i < devlen) {
125 1.2 thorpej if (!isnum(fname[i]))
126 1.2 thorpej return (EUNIT);
127 1.2 thorpej do {
128 1.2 thorpej u *= 10;
129 1.2 thorpej u += fname[i++] - '0';
130 1.2 thorpej } while (isnum(fname[i]));
131 1.2 thorpej }
132 1.7 drochner
133 1.1 perry #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
134 1.2 thorpej if (i < devlen) {
135 1.2 thorpej if (!isvalidpart(fname[i]))
136 1.2 thorpej return (EPART);
137 1.2 thorpej p = fname[i++] - 'a';
138 1.2 thorpej }
139 1.2 thorpej if (i != devlen)
140 1.2 thorpej return (ENXIO);
141 1.2 thorpej
142 1.2 thorpej *devname = savedevname;
143 1.2 thorpej *unit = u;
144 1.2 thorpej *partition = p;
145 1.2 thorpej help = col + 1;
146 1.2 thorpej } else
147 1.2 thorpej help = fname;
148 1.1 perry
149 1.2 thorpej if (*help)
150 1.2 thorpej *file = help;
151 1.1 perry
152 1.2 thorpej return (0);
153 1.1 perry }
154 1.1 perry
155 1.12 drochner char *
156 1.26 dsl sprint_bootsel(const char *filename)
157 1.1 perry {
158 1.7 drochner char *fsname, *devname;
159 1.7 drochner int unit, partition;
160 1.7 drochner const char *file;
161 1.7 drochner static char buf[80];
162 1.7 drochner
163 1.7 drochner if (parsebootfile(filename, &fsname, &devname, &unit,
164 1.7 drochner &partition, &file) == 0) {
165 1.2 thorpej if (!strcmp(fsname, "dos"))
166 1.7 drochner sprintf(buf, "dos:%s", file);
167 1.2 thorpej else if (!strcmp(fsname, "ufs"))
168 1.12 drochner sprintf(buf, "%s%d%c:%s", devname, unit,
169 1.12 drochner 'a' + partition, file);
170 1.7 drochner else goto bad;
171 1.12 drochner return (buf);
172 1.2 thorpej }
173 1.7 drochner bad:
174 1.12 drochner return ("(invalid)");
175 1.1 perry }
176 1.1 perry
177 1.1 perry static void
178 1.27 dsl bootit(const char *filename, int howto, int tell)
179 1.1 perry {
180 1.24 christos int floppy = strncmp(default_devname, "fd", 2) == 0;
181 1.7 drochner if (tell) {
182 1.7 drochner printf("booting %s", sprint_bootsel(filename));
183 1.7 drochner if (howto)
184 1.7 drochner printf(" (howto 0x%x)", howto);
185 1.7 drochner printf("\n");
186 1.7 drochner }
187 1.7 drochner #ifdef SUPPORT_LYNX
188 1.29 ad if(exec_netbsd(filename, 0, howto, floppy, NULL) < 0)
189 1.7 drochner printf("boot netbsd: %s: %s\n", sprint_bootsel(filename),
190 1.7 drochner strerror(errno));
191 1.7 drochner else {
192 1.7 drochner printf("boot netbsd returned\n");
193 1.7 drochner return;
194 1.7 drochner }
195 1.7 drochner if (exec_lynx(filename, 0) < 0)
196 1.7 drochner printf("boot lynx: %s: %s\n", sprint_bootsel(filename),
197 1.7 drochner strerror(errno));
198 1.7 drochner else
199 1.7 drochner printf("boot lynx returned\n");
200 1.7 drochner #else
201 1.29 ad if (exec_netbsd(filename, 0, howto, floppy, NULL) < 0)
202 1.7 drochner printf("boot: %s: %s\n", sprint_bootsel(filename),
203 1.7 drochner strerror(errno));
204 1.2 thorpej else
205 1.2 thorpej printf("boot returned\n");
206 1.7 drochner #endif
207 1.1 perry }
208 1.1 perry
209 1.1 perry static void
210 1.1 perry print_banner(void)
211 1.1 perry {
212 1.5 christos int extmem = getextmem();
213 1.5 christos char *s = "";
214 1.5 christos
215 1.25 christos clear_pc_screen();
216 1.25 christos
217 1.5 christos #ifdef XMS
218 1.10 drochner u_long xmsmem;
219 1.10 drochner if (getextmem1() == 0 && (xmsmem = checkxms()) != 0) {
220 1.10 drochner /*
221 1.10 drochner * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
222 1.10 drochner * getextmem() is getextmem1(). Without, the "smart"
223 1.10 drochner * methods could fail to report all memory as well.
224 1.10 drochner * xmsmem is a few kB less than the actual size, but
225 1.10 drochner * better than nothing.
226 1.10 drochner */
227 1.19 fvdl if ((int)xmsmem > extmem)
228 1.10 drochner extmem = xmsmem;
229 1.10 drochner s = "(xms) ";
230 1.5 christos }
231 1.5 christos #endif
232 1.4 thorpej
233 1.23 perry printf("\n"
234 1.23 perry ">> %s, Revision %s (from NetBSD %s)\n"
235 1.23 perry ">> Memory: %d/%d %sk\n",
236 1.23 perry bootprog_name, bootprog_rev, bootprog_kernrev,
237 1.23 perry getbasemem(), extmem, s);
238 1.2 thorpej }
239 1.2 thorpej
240 1.2 thorpej void
241 1.28 cegger usage(void)
242 1.2 thorpej {
243 1.2 thorpej printf("dosboot [-u] [-c <commands>] [-i] [filename [-bootopts]]\n");
244 1.2 thorpej }
245 1.2 thorpej
246 1.2 thorpej int
247 1.26 dsl main(int argc, char **argv)
248 1.2 thorpej {
249 1.2 thorpej int ch;
250 1.2 thorpej int interactive = 0;
251 1.2 thorpej int howto;
252 1.2 thorpej extern char *optarg;
253 1.2 thorpej extern int optind;
254 1.2 thorpej
255 1.14 martin #ifdef SUPPORT_SERIAL
256 1.14 martin initio(SUPPORT_SERIAL);
257 1.14 martin #else
258 1.7 drochner initio(CONSDEV_PC);
259 1.14 martin #endif
260 1.2 thorpej gateA20();
261 1.2 thorpej
262 1.2 thorpej print_banner();
263 1.2 thorpej
264 1.2 thorpej current_fsmode = "dos";
265 1.2 thorpej default_devname = "hd";
266 1.2 thorpej default_unit = 0;
267 1.2 thorpej default_partition = 0;
268 1.2 thorpej default_filename = "netbsd";
269 1.2 thorpej
270 1.2 thorpej while ((ch = getopt(argc, argv, "c:iu")) != -1) {
271 1.2 thorpej switch (ch) {
272 1.2 thorpej case 'c':
273 1.2 thorpej docommand(optarg);
274 1.2 thorpej return (1);
275 1.2 thorpej break;
276 1.2 thorpej case 'i':
277 1.2 thorpej interactive = 1;
278 1.2 thorpej break;
279 1.2 thorpej case 'u':
280 1.2 thorpej current_fsmode = "ufs";
281 1.2 thorpej break;
282 1.2 thorpej default:
283 1.2 thorpej usage();
284 1.2 thorpej return (1);
285 1.2 thorpej }
286 1.2 thorpej }
287 1.2 thorpej
288 1.7 drochner if (interactive) {
289 1.8 drochner printf("type \"?\" or \"help\" for help.\n");
290 1.2 thorpej bootmenu();
291 1.7 drochner }
292 1.2 thorpej
293 1.2 thorpej argc -= optind;
294 1.2 thorpej argv += optind;
295 1.2 thorpej
296 1.2 thorpej if (argc > 2) {
297 1.2 thorpej usage();
298 1.2 thorpej return (1);
299 1.2 thorpej }
300 1.2 thorpej howto = 0;
301 1.2 thorpej if (argc > 1 && !parseopts(argv[1], &howto))
302 1.2 thorpej return (1);
303 1.1 perry
304 1.2 thorpej bootit((argc > 0 ? argv[0] : "netbsd"), howto, 1);
305 1.2 thorpej return (1);
306 1.7 drochner }
307 1.7 drochner
308 1.7 drochner /* ARGSUSED */
309 1.7 drochner void
310 1.26 dsl command_help(char *arg)
311 1.7 drochner {
312 1.7 drochner printf("commands are:\n"
313 1.18 itojun "boot [xdNx:][filename] [-acdqsv]\n"
314 1.7 drochner " (ex. \"sd0a:netbsd.old -s\"\n"
315 1.7 drochner "ls [path]\n"
316 1.7 drochner "mode ufs|dos\n"
317 1.9 drochner "dev xd[N[x]]:\n"
318 1.7 drochner "help|?\n"
319 1.7 drochner "quit\n");
320 1.7 drochner }
321 1.7 drochner
322 1.7 drochner void
323 1.26 dsl command_ls(char *arg)
324 1.7 drochner {
325 1.7 drochner char *help = default_filename;
326 1.7 drochner if (strcmp(current_fsmode, "ufs")) {
327 1.7 drochner printf("UFS only\n");
328 1.7 drochner return;
329 1.7 drochner }
330 1.7 drochner default_filename = "/";
331 1.12 drochner ufs_ls(arg);
332 1.7 drochner default_filename = help;
333 1.7 drochner }
334 1.7 drochner
335 1.7 drochner /* ARGSUSED */
336 1.7 drochner void
337 1.26 dsl command_quit(char *arg)
338 1.7 drochner {
339 1.7 drochner printf("Exiting... goodbye...\n");
340 1.12 drochner exit(0);
341 1.7 drochner }
342 1.7 drochner
343 1.7 drochner void
344 1.26 dsl command_boot(char *arg)
345 1.7 drochner {
346 1.7 drochner char *filename;
347 1.7 drochner int howto;
348 1.7 drochner
349 1.7 drochner if (parseboot(arg, &filename, &howto))
350 1.7 drochner bootit(filename, howto, 1);
351 1.7 drochner }
352 1.7 drochner
353 1.7 drochner void
354 1.26 dsl command_mode(char *arg)
355 1.7 drochner {
356 1.7 drochner if (!strcmp("dos", arg))
357 1.7 drochner current_fsmode = "dos";
358 1.7 drochner else if (!strcmp("ufs", arg))
359 1.7 drochner current_fsmode = "ufs";
360 1.7 drochner else
361 1.7 drochner printf("invalid mode\n");
362 1.9 drochner }
363 1.9 drochner
364 1.9 drochner void
365 1.26 dsl command_dev(char *arg)
366 1.9 drochner {
367 1.9 drochner static char savedevname[MAXDEVNAME + 1];
368 1.9 drochner char *fsname, *devname;
369 1.9 drochner const char *file; /* dummy */
370 1.9 drochner
371 1.9 drochner if (!strcmp(current_fsmode, "dos")) {
372 1.9 drochner printf("not available in DOS mode\n");
373 1.9 drochner return;
374 1.9 drochner }
375 1.9 drochner
376 1.9 drochner if (*arg == '\0') {
377 1.9 drochner printf("%s%d%c:\n", default_devname, default_unit,
378 1.9 drochner 'a' + default_partition);
379 1.9 drochner return;
380 1.9 drochner }
381 1.9 drochner
382 1.9 drochner if (!strchr(arg, ':') ||
383 1.9 drochner parsebootfile(arg, &fsname, &devname, &default_unit,
384 1.9 drochner &default_partition, &file)) {
385 1.9 drochner command_help(NULL);
386 1.9 drochner return;
387 1.9 drochner }
388 1.9 drochner
389 1.9 drochner /* put to own static storage */
390 1.9 drochner strncpy(savedevname, devname, MAXDEVNAME + 1);
391 1.9 drochner default_devname = savedevname;
392 1.1 perry }
393