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