main.c revision 1.12.12.3 1 /* $NetBSD: main.c,v 1.12.12.3 2008/10/10 22:29:05 skrll 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 "pxeboot.h"
49 #include "bootmod.h"
50
51 extern struct x86_boot_params boot_params;
52
53 int errno;
54 int debug;
55
56 extern char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
57
58 int main(void);
59
60 void command_help __P((char *));
61 void command_quit __P((char *));
62 void command_boot __P((char *));
63 void command_consdev(char *);
64 void command_load(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", command_load },
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 printf("\n"
95 ">> %s, Revision %s (from NetBSD %s)\n"
96 ">> Memory: %d/%d k\n",
97 bootprog_name, bootprog_rev, bootprog_kernrev,
98 base, ext);
99 }
100
101 int
102 main(void)
103 {
104 char c;
105
106 #ifdef SUPPORT_SERIAL
107 initio(SUPPORT_SERIAL);
108 #else
109 initio(CONSDEV_PC);
110 #endif
111 gateA20();
112
113 print_banner();
114
115 printf("Press return to boot now, any other key for boot menu\n");
116 printf("Starting in ");
117
118 c = awaitkey(boot_params.bp_timeout, 1);
119 if ((c != '\r') && (c != '\n') && (c != '\0')) {
120 printf("type \"?\" or \"help\" for help.\n");
121 bootmenu(); /* does not return */
122 }
123
124 /*
125 * The file name provided here is just a default. If the
126 * DHCP server provides a file name, we'll use that instead.
127 */
128 bootit("netbsd", 0);
129
130 /*
131 * If that fails, let the BIOS try the next boot device.
132 */
133 return (1);
134 }
135
136 /* ARGSUSED */
137 void
138 command_help(char *arg)
139 {
140 printf("commands are:\n"
141 "boot [filename] [-adsqv]\n"
142 " (ex. \"netbsd.old -s\"\n"
143 "consdev {pc|com[0123]|com[0123]kbd|auto}\n"
144 "modules {enabled|disabled}\n"
145 "load {path_to_module}\n"
146 "help|?\n"
147 "quit\n");
148 }
149
150 /* ARGSUSED */
151 void
152 command_quit(char *arg)
153 {
154
155 printf("Exiting...\n");
156 delay(1000000);
157 reboot();
158 /* Note: we shouldn't get to this point! */
159 panic("Could not reboot!");
160 exit(0);
161 }
162
163 void
164 command_boot(char *arg)
165 {
166 char *filename;
167 int howto;
168
169 if (parseboot(arg, &filename, &howto))
170 bootit(filename, howto);
171 }
172
173 static const struct cons_devs {
174 const char *name;
175 u_int tag;
176 } cons_devs[] = {
177 { "pc", CONSDEV_PC },
178 { "com0", CONSDEV_COM0 },
179 { "com1", CONSDEV_COM1 },
180 { "com2", CONSDEV_COM2 },
181 { "com3", CONSDEV_COM3 },
182 { "com0kbd", CONSDEV_COM0KBD },
183 { "com1kbd", CONSDEV_COM1KBD },
184 { "com2kbd", CONSDEV_COM2KBD },
185 { "com3kbd", CONSDEV_COM3KBD },
186 { "auto", CONSDEV_AUTO },
187 { 0, 0 } };
188
189 void
190 command_consdev(char *arg)
191 {
192 const struct cons_devs *cdp;
193
194 for (cdp = cons_devs; cdp->name; cdp++) {
195 if (!strcmp(arg, cdp->name)) {
196 initio(cdp->tag);
197 print_banner();
198 return;
199 }
200 }
201 printf("invalid console device.\n");
202 }
203 void
204 command_modules(char *arg)
205 {
206 if (strcmp(arg, "enabled") == 0 ||
207 strcmp(arg, "on") == 0)
208 boot_modules_enabled = true;
209 else if (strcmp(arg, "disabled") == 0 ||
210 strcmp(arg, "off") == 0)
211 boot_modules_enabled = false;
212 else
213 printf("invalid flag, must be 'enabled' or 'disabled'.\n");
214 }
215
216 void
217 command_load(char *arg)
218 {
219 boot_module_t *bm, *bmp;
220 size_t len;
221 char *str;
222
223 bm = alloc(sizeof(boot_module_t));
224 len = strlen(arg) + 1;
225 str = alloc(len);
226 if (bm == NULL || str == NULL) {
227 printf("couldn't allocate module\n");
228 return;
229 }
230 memcpy(str, arg, len);
231 bm->bm_path = str;
232 bm->bm_next = NULL;
233 if (boot_modules == NULL)
234 boot_modules = bm;
235 else {
236 for (bmp = boot_modules; bmp->bm_next;
237 bmp = bmp->bm_next)
238 ;
239 bmp->bm_next = bm;
240 }
241 }
242