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