boot.c revision 1.3 1 1.3 cherry /* $NetBSD: boot.c,v 1.3 2006/07/02 17:28:11 cherry Exp $ */
2 1.1 cherry
3 1.1 cherry /*-
4 1.1 cherry * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.1 cherry * All rights reserved.
6 1.1 cherry *
7 1.1 cherry * Redistribution and use in source and binary forms, with or without
8 1.1 cherry * modification, are permitted provided that the following conditions
9 1.1 cherry * are met:
10 1.1 cherry * 1. Redistributions of source code must retain the above copyright
11 1.1 cherry * notice, this list of conditions and the following disclaimer.
12 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cherry * notice, this list of conditions and the following disclaimer in the
14 1.1 cherry * documentation and/or other materials provided with the distribution.
15 1.1 cherry *
16 1.1 cherry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 cherry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 cherry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 cherry * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 cherry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 cherry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 cherry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 cherry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 cherry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 cherry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 cherry * SUCH DAMAGE.
27 1.1 cherry */
28 1.1 cherry
29 1.1 cherry #include <sys/cdefs.h>
30 1.2 cherry /* __FBSDID("$FreeBSD: src/sys/boot/common/boot.c,v 1.29 2003/08/25 23:30:41 obrien Exp $"); */
31 1.1 cherry
32 1.1 cherry
33 1.1 cherry /*
34 1.1 cherry * Loading modules, booting the system
35 1.1 cherry */
36 1.1 cherry
37 1.1 cherry #include <lib/libsa/stand.h>
38 1.1 cherry #include <lib/libkern/libkern.h>
39 1.1 cherry
40 1.1 cherry #include "bootstrap.h"
41 1.1 cherry
42 1.1 cherry static char *getbootfile(int try);
43 1.1 cherry static int loadakernel(int try, int argc, char* argv[]);
44 1.1 cherry
45 1.3 cherry /* List of kernel names to try */
46 1.1 cherry static const char *default_bootfiles = "netbsd";
47 1.1 cherry
48 1.1 cherry static int autoboot_tried;
49 1.1 cherry
50 1.1 cherry /*
51 1.1 cherry * The user wants us to boot.
52 1.1 cherry */
53 1.1 cherry
54 1.1 cherry int
55 1.1 cherry command_boot(int argc, char *argv[])
56 1.1 cherry {
57 1.1 cherry struct preloaded_file *fp;
58 1.1 cherry
59 1.1 cherry /*
60 1.1 cherry * See if the user has specified an explicit kernel to boot.
61 1.1 cherry */
62 1.1 cherry if ((argc > 1) && (argv[1][0] != '-')) {
63 1.1 cherry
64 1.1 cherry /* XXX maybe we should discard everything and start again? */
65 1.1 cherry if (file_findfile(NULL, NULL) != NULL) {
66 1.1 cherry sprintf(command_errbuf, "can't boot '%s', kernel module already loaded", argv[1]);
67 1.1 cherry return(CMD_ERROR);
68 1.1 cherry }
69 1.1 cherry
70 1.1 cherry /* find/load the kernel module */
71 1.1 cherry if (file_loadkernel(argv[1], argc - 2, argv + 2) != 0)
72 1.1 cherry return(CMD_ERROR);
73 1.1 cherry
74 1.1 cherry /* we have consumed all arguments */
75 1.1 cherry argc = 1;
76 1.1 cherry }
77 1.1 cherry
78 1.1 cherry /*
79 1.1 cherry * See if there is a kernel module already loaded
80 1.1 cherry */
81 1.1 cherry if (file_findfile(NULL, NULL) == NULL)
82 1.1 cherry if (loadakernel(0, argc - 1, argv + 1))
83 1.1 cherry /* we have consumed all arguments */
84 1.1 cherry argc = 1;
85 1.1 cherry
86 1.1 cherry /*
87 1.1 cherry * Loaded anything yet?
88 1.1 cherry */
89 1.1 cherry if ((fp = file_findfile(NULL, NULL)) == NULL) {
90 1.1 cherry command_errmsg = "no bootable kernel";
91 1.1 cherry return(CMD_ERROR);
92 1.1 cherry }
93 1.1 cherry
94 1.1 cherry /*
95 1.1 cherry * If we were given arguments, discard any previous.
96 1.1 cherry * XXX should we merge arguments? Hard to DWIM.
97 1.1 cherry */
98 1.1 cherry if (argc > 1) {
99 1.1 cherry if (fp->f_args != NULL)
100 1.1 cherry free(fp->f_args);
101 1.1 cherry fp->f_args = unargv(argc - 1, argv + 1);
102 1.1 cherry }
103 1.1 cherry
104 1.1 cherry /* Hook for platform-specific autoloading of modules */
105 1.1 cherry if (archsw.arch_autoload() != 0)
106 1.1 cherry return(CMD_ERROR);
107 1.1 cherry
108 1.1 cherry /* Call the exec handler from the loader matching the kernel */
109 1.1 cherry file_formats[fp->f_loader]->l_exec(fp);
110 1.1 cherry command_errmsg = strerror(file_formats[fp->f_loader]->l_exec(fp));
111 1.1 cherry return(CMD_ERROR);
112 1.1 cherry }
113 1.1 cherry
114 1.1 cherry
115 1.1 cherry /*
116 1.1 cherry * Autoboot after a delay
117 1.1 cherry */
118 1.1 cherry
119 1.1 cherry int
120 1.1 cherry command_autoboot(int argc, char *argv[])
121 1.1 cherry {
122 1.1 cherry int howlong;
123 1.1 cherry char *cp, *prompt;
124 1.1 cherry
125 1.1 cherry prompt = NULL;
126 1.1 cherry howlong = -1;
127 1.1 cherry switch(argc) {
128 1.1 cherry case 3:
129 1.1 cherry prompt = argv[2];
130 1.1 cherry /* FALLTHROUGH */
131 1.1 cherry case 2:
132 1.1 cherry howlong = strtol(argv[1], &cp, 0);
133 1.1 cherry if (*cp != 0) {
134 1.1 cherry sprintf(command_errbuf, "bad delay '%s'", argv[1]);
135 1.1 cherry return(CMD_ERROR);
136 1.1 cherry }
137 1.1 cherry /* FALLTHROUGH */
138 1.1 cherry case 1:
139 1.1 cherry return(autoboot(howlong, prompt));
140 1.1 cherry }
141 1.1 cherry
142 1.1 cherry command_errmsg = "too many arguments";
143 1.1 cherry return(CMD_ERROR);
144 1.1 cherry }
145 1.1 cherry
146 1.1 cherry /*
147 1.1 cherry * Called before we go interactive. If we think we can autoboot, and
148 1.1 cherry * we haven't tried already, try now.
149 1.1 cherry */
150 1.1 cherry void
151 1.1 cherry autoboot_maybe()
152 1.1 cherry {
153 1.1 cherry char *cp;
154 1.1 cherry
155 1.1 cherry cp = getenv("autoboot_delay");
156 1.1 cherry if ((autoboot_tried == 0) && ((cp == NULL) || strcasecmp(cp, "NO")))
157 1.1 cherry autoboot(-1, NULL); /* try to boot automatically */
158 1.1 cherry }
159 1.1 cherry
160 1.1 cherry int
161 1.1 cherry autoboot(int timeout, char *prompt)
162 1.1 cherry {
163 1.1 cherry time_t when, otime, ntime;
164 1.1 cherry int c, yes;
165 1.1 cherry char *argv[2], *cp, *ep;
166 1.1 cherry char *kernelname;
167 1.1 cherry
168 1.1 cherry autoboot_tried = 1;
169 1.1 cherry
170 1.1 cherry if (timeout == -1) {
171 1.1 cherry /* try to get a delay from the environment */
172 1.1 cherry if ((cp = getenv("autoboot_delay"))) {
173 1.1 cherry timeout = strtol(cp, &ep, 0);
174 1.1 cherry if (cp == ep)
175 1.1 cherry timeout = -1;
176 1.1 cherry }
177 1.1 cherry }
178 1.1 cherry if (timeout == -1) /* all else fails */
179 1.1 cherry timeout = 10;
180 1.1 cherry
181 1.1 cherry kernelname = getenv("kernelname");
182 1.1 cherry if (kernelname == NULL) {
183 1.1 cherry argv[0] = NULL;
184 1.1 cherry loadakernel(0, 0, argv);
185 1.1 cherry kernelname = getenv("kernelname");
186 1.1 cherry if (kernelname == NULL) {
187 1.1 cherry command_errmsg = "no valid kernel found";
188 1.1 cherry return(CMD_ERROR);
189 1.1 cherry }
190 1.1 cherry }
191 1.1 cherry
192 1.1 cherry otime = time(NULL);
193 1.1 cherry when = otime + timeout; /* when to boot */
194 1.1 cherry yes = 0;
195 1.1 cherry
196 1.1 cherry printf("%s\n", (prompt == NULL) ? "Hit [Enter] to boot immediately, or any other key for command prompt." : prompt);
197 1.1 cherry
198 1.1 cherry for (;;) {
199 1.1 cherry if (ischar()) {
200 1.1 cherry c = getchar();
201 1.1 cherry if ((c == '\r') || (c == '\n'))
202 1.1 cherry yes = 1;
203 1.1 cherry break;
204 1.1 cherry }
205 1.1 cherry ntime = time(NULL);
206 1.1 cherry if (ntime >= when) {
207 1.1 cherry yes = 1;
208 1.1 cherry break;
209 1.1 cherry }
210 1.1 cherry
211 1.1 cherry if (ntime != otime) {
212 1.1 cherry printf("\rBooting [%s] in %d second%s... ",
213 1.1 cherry kernelname, (int)(when - ntime),
214 1.1 cherry (when-ntime)==1?"":"s");
215 1.1 cherry otime = ntime;
216 1.1 cherry }
217 1.1 cherry }
218 1.1 cherry if (yes)
219 1.1 cherry printf("\rBooting [%s]... ", kernelname);
220 1.1 cherry putchar('\n');
221 1.1 cherry if (yes) {
222 1.1 cherry argv[0] = "boot";
223 1.1 cherry argv[1] = NULL;
224 1.1 cherry return(command_boot(1, argv));
225 1.1 cherry }
226 1.1 cherry return(CMD_OK);
227 1.1 cherry }
228 1.1 cherry
229 1.1 cherry /*
230 1.1 cherry * Scrounge for the name of the (try)'th file we will try to boot.
231 1.1 cherry */
232 1.1 cherry static char *
233 1.1 cherry getbootfile(int try)
234 1.1 cherry {
235 1.1 cherry static char *name = NULL;
236 1.1 cherry const char *spec, *ep;
237 1.1 cherry size_t len;
238 1.1 cherry
239 1.1 cherry /* we use dynamic storage */
240 1.1 cherry if (name != NULL) {
241 1.1 cherry free(name);
242 1.1 cherry name = NULL;
243 1.1 cherry }
244 1.1 cherry
245 1.1 cherry /*
246 1.1 cherry * Try $bootfile, then try our builtin default
247 1.1 cherry */
248 1.1 cherry if ((spec = getenv("bootfile")) == NULL)
249 1.1 cherry spec = default_bootfiles;
250 1.1 cherry
251 1.1 cherry while ((try > 0) && (spec != NULL)) {
252 1.1 cherry spec = strchr(spec, ';');
253 1.1 cherry if (spec)
254 1.1 cherry spec++; /* skip over the leading ';' */
255 1.1 cherry try--;
256 1.1 cherry }
257 1.1 cherry if (spec != NULL) {
258 1.1 cherry if ((ep = strchr(spec, ';')) != NULL) {
259 1.1 cherry len = ep - spec;
260 1.1 cherry } else {
261 1.1 cherry len = strlen(spec);
262 1.1 cherry }
263 1.1 cherry name = alloc(len + 1);
264 1.1 cherry strncpy(name, spec, len);
265 1.1 cherry name[len] = 0;
266 1.1 cherry }
267 1.1 cherry if (name && name[0] == 0) {
268 1.1 cherry free(name);
269 1.1 cherry name = NULL;
270 1.1 cherry }
271 1.1 cherry else {
272 1.1 cherry setenv("kernelname", name, 1);
273 1.1 cherry }
274 1.1 cherry
275 1.1 cherry return(name);
276 1.1 cherry }
277 1.1 cherry
278 1.1 cherry /*
279 1.1 cherry * Try to find the /etc/fstab file on the filesystem (rootdev),
280 1.1 cherry * which should be be the root filesystem, and parse it to find
281 1.1 cherry * out what the kernel ought to think the root filesystem is.
282 1.1 cherry *
283 1.1 cherry * If we're successful, set vfs.root.mountfrom to <vfstype>:<path>
284 1.1 cherry * so that the kernel can tell both which VFS and which node to use
285 1.1 cherry * to mount the device. If this variable's already set, don't
286 1.1 cherry * overwrite it.
287 1.1 cherry */
288 1.1 cherry int
289 1.1 cherry getrootmount(char *rootdev)
290 1.1 cherry {
291 1.1 cherry char lbuf[128], *cp, *ep, *dev, *fstyp;
292 1.1 cherry int fd, error;
293 1.1 cherry
294 1.1 cherry if (getenv("vfs.root.mountfrom") != NULL)
295 1.1 cherry return(0);
296 1.1 cherry
297 1.1 cherry sprintf(lbuf, "%s/etc/fstab", rootdev);
298 1.1 cherry if ((fd = open(lbuf, O_RDONLY)) < 0)
299 1.1 cherry return(1);
300 1.1 cherry
301 1.1 cherry /* loop reading lines from /etc/fstab What was that about sscanf again? */
302 1.1 cherry error = 1;
303 1.1 cherry while (fgetstr(lbuf, sizeof(lbuf), fd) >= 0) {
304 1.1 cherry if ((lbuf[0] == 0) || (lbuf[0] == '#'))
305 1.1 cherry continue;
306 1.1 cherry
307 1.1 cherry /* skip device name */
308 1.1 cherry for (cp = lbuf; (*cp != 0) && !isspace(*cp); cp++)
309 1.1 cherry ;
310 1.1 cherry if (*cp == 0) /* misformatted */
311 1.1 cherry continue;
312 1.1 cherry /* delimit and save */
313 1.1 cherry *cp++ = 0;
314 1.1 cherry dev = strdup(lbuf);
315 1.1 cherry
316 1.1 cherry /* skip whitespace up to mountpoint */
317 1.1 cherry while ((*cp != 0) && isspace(*cp))
318 1.1 cherry cp++;
319 1.1 cherry /* must have /<space> to be root */
320 1.1 cherry if ((*cp == 0) || (*cp != '/') || !isspace(*(cp + 1)))
321 1.1 cherry continue;
322 1.1 cherry /* skip whitespace up to fstype */
323 1.1 cherry cp += 2;
324 1.1 cherry while ((*cp != 0) && isspace(*cp))
325 1.1 cherry cp++;
326 1.1 cherry if (*cp == 0) /* misformatted */
327 1.1 cherry continue;
328 1.1 cherry /* skip text to end of fstype and delimit */
329 1.1 cherry ep = cp;
330 1.1 cherry while ((*cp != 0) && !isspace(*cp))
331 1.1 cherry cp++;
332 1.1 cherry *cp = 0;
333 1.1 cherry fstyp = strdup(ep);
334 1.1 cherry
335 1.1 cherry /* build the final result and save it */
336 1.1 cherry sprintf(lbuf, "%s:%s", fstyp, dev);
337 1.1 cherry free(dev);
338 1.1 cherry free(fstyp);
339 1.1 cherry setenv("vfs.root.mountfrom", lbuf, 0);
340 1.1 cherry error = 0;
341 1.1 cherry break;
342 1.1 cherry }
343 1.1 cherry close(fd);
344 1.1 cherry return(error);
345 1.1 cherry }
346 1.1 cherry
347 1.1 cherry static int
348 1.1 cherry loadakernel(int try, int argc, char* argv[])
349 1.1 cherry {
350 1.1 cherry char *cp;
351 1.1 cherry
352 1.1 cherry for (try = 0; (cp = getbootfile(try)) != NULL; try++)
353 1.1 cherry if (file_loadkernel(cp, argc - 1, argv + 1) != 0)
354 1.1 cherry printf("can't load '%s'\n", cp);
355 1.1 cherry else
356 1.1 cherry return 1;
357 1.1 cherry return 0;
358 1.1 cherry }
359 1.1 cherry
360