xref: /openbsd/sys/stand/boot/boot.c (revision db3296cf)
1 /*	$OpenBSD: boot.c,v 1.28 2003/06/02 20:22:44 mickey Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Dale Rahn
5  * Copyright (c) 1997,1998 Michael Shalayeff
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/stat.h>
34 #include <libsa.h>
35 #include <lib/libsa/loadfile.h>
36 #include <lib/libkern/funcs.h>
37 
38 #include "cmd.h"
39 
40 static const char *const kernels[] = {
41 	"/bsd",
42 	"/obsd",
43 	"/bsd.old",
44 	NULL
45 };
46 
47 extern	const char version[];
48 struct cmd_state cmd;
49 int bootprompt = 1;
50 
51 void
52 boot(bootdev)
53 	dev_t	bootdev;
54 {
55 	register const char *bootfile = kernels[0];
56 	register int i = 0, try = 0, st;
57 	u_long marks[MARK_MAX];
58 
59 	machdep();
60 
61 	printf(">> OpenBSD/" MACHINE " BOOT %s\n", version);
62 
63 	devboot(bootdev, cmd.bootdev);
64 	strlcpy(cmd.image, bootfile, sizeof(cmd.image));
65 	cmd.boothowto = 0;
66 	cmd.conf = "/etc/boot.conf";
67 	cmd.addr = (void *)DEFAULT_KERNEL_ADDRESS;
68 	cmd.timeout = 5;
69 
70 	st = read_conf();
71 	if (!bootprompt)
72 		snprintf(cmd.path, sizeof cmd.path, "%s:%s",
73 		    cmd.bootdev, cmd.image);
74 
75 	while (1) {
76 		/* no boot.conf, or no boot cmd in there */
77 		if (bootprompt && st <= 0)
78 			do {
79 				printf("boot> ");
80 			} while(!getcmd());
81 		st = 0;
82 		bootprompt = 1;	/* allow reselect should we fail */
83 
84 		printf("booting %s: ", cmd.path);
85 		marks[MARK_START] = (u_long)cmd.addr;
86 		if (loadfile(cmd.path, marks, LOAD_ALL) >= 0)
87 			break;
88 
89 		if (kernels[++i] == NULL) {
90 			try += 1;
91 			bootfile = kernels[i=0];
92 		} else
93 			bootfile = kernels[i];
94 		strlcpy(cmd.image, bootfile, sizeof(cmd.image));
95 		printf(" failed(%d). will try %s\n", errno, bootfile);
96 
97 		if (try < 2)
98 			cmd.timeout++;
99 		else {
100 			if (cmd.timeout)
101 				printf("Turning timeout off.\n");
102 			cmd.timeout = 0;
103 		}
104 	}
105 
106 	/* exec */
107 	run_loadfile(marks, cmd.boothowto);
108 }
109 
110 #ifdef _TEST
111 int
112 main()
113 {
114 	boot(0);
115 	return 0;
116 }
117 #endif
118