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