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