main.c revision 1.17 1 /* $NetBSD: main.c,v 1.17 2008/12/13 23:30:54 christos Exp $ */
2
3 /*
4 * Copyright (c) 1996
5 * Matthias Drochner. All rights reserved.
6 * Copyright (c) 1996
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/types.h>
40 #include <sys/reboot.h>
41 #include <sys/bootblock.h>
42
43 #include <lib/libkern/libkern.h>
44
45 #include <lib/libsa/stand.h>
46
47 #include <libi386.h>
48 #include <bootmenu.h>
49 #include <bootmod.h>
50 #include "pxeboot.h"
51
52 extern struct x86_boot_params boot_params;
53
54 int errno;
55 int debug;
56
57 extern char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
58
59 int main(void);
60
61 void command_help __P((char *));
62 void command_quit __P((char *));
63 void command_boot __P((char *));
64 void command_consdev(char *);
65 void command_modules(char *);
66
67 const struct bootblk_command commands[] = {
68 { "help", command_help },
69 { "?", command_help },
70 { "quit", command_quit },
71 { "boot", command_boot },
72 { "consdev", command_consdev },
73 { "modules", command_modules },
74 { "load", module_add },
75 { NULL, NULL },
76 };
77
78 static int
79 bootit(const char *filename, int howto)
80 {
81 if (exec_netbsd(filename, 0, howto, 0) < 0)
82 printf("boot: %s\n", strerror(errno));
83 else
84 printf("boot returned\n");
85 return (-1);
86 }
87
88 static void
89 print_banner(void)
90 {
91 int base = getbasemem();
92 int ext = getextmem();
93
94 if (bootconf.clear)
95 clear_pc_screen();
96
97 printf("\n"
98 ">> NetBSD/x86 PXE boot, Revision %s (from NetBSD %s)\n"
99 ">> Memory: %d/%d k\n",
100 bootprog_rev, bootprog_kernrev,
101 base, ext);
102 }
103
104 int
105 main(void)
106 {
107 extern char twiddle_toggle;
108 char c;
109
110 twiddle_toggle = 1; /* no twiddling until we're ready */
111 printf("\f"); /* clear screen (hopefully) */
112
113 #ifdef SUPPORT_SERIAL
114 initio(SUPPORT_SERIAL);
115 #else
116 initio(CONSDEV_PC);
117 #endif
118 gateA20();
119
120 #ifndef SMALL
121 parsebootconf(BOOTCONF);
122
123 /*
124 * If console set in boot.cfg, switch to it.
125 * This will print the banner, so we don't need to explicitly do it
126 */
127 if (bootconf.consdev)
128 command_consdev(bootconf.consdev);
129 else
130 print_banner();
131
132 /* Display the menu, if applicable */
133 twiddle_toggle = 0;
134 if (bootconf.nummenu > 0) {
135 /* Does not return */
136 doboottypemenu();
137 }
138 #else
139 twiddle_toggle = 0;
140 print_banner();
141 #endif
142
143 printf("Press return to boot now, any other key for boot menu\n");
144 printf("booting netbsd - starting in ");
145
146 #ifdef SMALL
147 c = awaitkey(boot_params.bp_timeout, 1);
148 #else
149 c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1);
150 #endif
151 if ((c != '\r') && (c != '\n') && (c != '\0') &&
152 ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0
153 || check_password((char *)boot_params.bp_password))) {
154 printf("type \"?\" or \"help\" for help.\n");
155 bootmenu(); /* does not return */
156 }
157
158 /*
159 * The file name provided here is just a default. If the
160 * DHCP server provides a file name, we'll use that instead.
161 */
162 bootit("netbsd", 0);
163
164 /*
165 * If that fails, let the BIOS try the next boot device.
166 */
167 return (1);
168 }
169
170 /* ARGSUSED */
171 void
172 command_help(char *arg)
173 {
174 printf("commands are:\n"
175 "boot [filename] [-adsqv]\n"
176 " (ex. \"netbsd.old -s\"\n"
177 "consdev {pc|com[0123]|com[0123]kbd|auto}\n"
178 "modules {enabled|disabled}\n"
179 "load {path_to_module}\n"
180 "help|?\n"
181 "quit\n");
182 }
183
184 /* ARGSUSED */
185 void
186 command_quit(char *arg)
187 {
188
189 printf("Exiting...\n");
190 delay(1000000);
191 reboot();
192 /* Note: we shouldn't get to this point! */
193 panic("Could not reboot!");
194 exit(0);
195 }
196
197 void
198 command_boot(char *arg)
199 {
200 char *filename;
201 int howto;
202
203 if (parseboot(arg, &filename, &howto))
204 bootit(filename, howto);
205 }
206
207 static const struct cons_devs {
208 const char *name;
209 u_int tag;
210 } cons_devs[] = {
211 { "pc", CONSDEV_PC },
212 { "com0", CONSDEV_COM0 },
213 { "com1", CONSDEV_COM1 },
214 { "com2", CONSDEV_COM2 },
215 { "com3", CONSDEV_COM3 },
216 { "com0kbd", CONSDEV_COM0KBD },
217 { "com1kbd", CONSDEV_COM1KBD },
218 { "com2kbd", CONSDEV_COM2KBD },
219 { "com3kbd", CONSDEV_COM3KBD },
220 { "auto", CONSDEV_AUTO },
221 { 0, 0 } };
222
223 void
224 command_consdev(char *arg)
225 {
226 const struct cons_devs *cdp;
227
228 for (cdp = cons_devs; cdp->name; cdp++) {
229 if (!strcmp(arg, cdp->name)) {
230 initio(cdp->tag);
231 print_banner();
232 return;
233 }
234 }
235 printf("invalid console device.\n");
236 }
237 void
238 command_modules(char *arg)
239 {
240 if (strcmp(arg, "enabled") == 0 ||
241 strcmp(arg, "on") == 0)
242 boot_modules_enabled = true;
243 else if (strcmp(arg, "disabled") == 0 ||
244 strcmp(arg, "off") == 0)
245 boot_modules_enabled = false;
246 else
247 printf("invalid flag, must be 'enabled' or 'disabled'.\n");
248 }
249