xref: /openbsd-src/sys/stand/boot/boot.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: boot.c,v 1.37 2011/04/17 09:49:48 kettenis 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 #ifndef KERNEL
41 #define KERNEL "/bsd"
42 #endif
43 
44 char prog_ident[40];
45 char *progname = "BOOT";
46 
47 extern	const char version[];
48 struct cmd_state cmd;
49 
50 /* bootprompt can be set by MD code to avoid prompt first time round */
51 int bootprompt = 1;
52 char *kernelfile = KERNEL;		/* can be changed by MD code */
53 int boottimeout = 5;			/* can be changed by MD code */
54 
55 void
56 boot(dev_t bootdev)
57 {
58 	int fd;
59 	int try = 0, st;
60 	u_long marks[MARK_MAX];
61 
62 	machdep();
63 
64 	snprintf(prog_ident, sizeof(prog_ident),
65 	    ">> OpenBSD/" MACHINE " %s %s", progname, version);
66 	printf("%s\n", prog_ident);
67 
68 	devboot(bootdev, cmd.bootdev);
69 	strlcpy(cmd.image, kernelfile, sizeof(cmd.image));
70 	cmd.boothowto = 0;
71 	cmd.conf = "/etc/boot.conf";
72 	cmd.addr = (void *)DEFAULT_KERNEL_ADDRESS;
73 	cmd.timeout = boottimeout;
74 
75 	st = read_conf();
76 	if (!bootprompt)
77 		snprintf(cmd.path, sizeof cmd.path, "%s:%s",
78 		    cmd.bootdev, cmd.image);
79 
80 	while (1) {
81 		/* no boot.conf, or no boot cmd in there */
82 		if (bootprompt && st <= 0)
83 			do {
84 				printf("boot> ");
85 			} while(!getcmd());
86 		st = 0;
87 		bootprompt = 1;	/* allow reselect should we fail */
88 
89 		printf("booting %s: ", cmd.path);
90 		marks[MARK_START] = (u_long)cmd.addr;
91 		if ((fd = loadfile(cmd.path, marks, LOAD_ALL)) != -1) {
92 			close(fd);
93 			break;
94 		}
95 
96 		kernelfile = KERNEL;
97 		try++;
98 		strlcpy(cmd.image, kernelfile, sizeof(cmd.image));
99 		printf(" failed(%d). will try %s\n", errno, kernelfile);
100 
101 		if (try < 2) {
102 			if (cmd.timeout > 0)
103 				cmd.timeout++;
104 		} else {
105 			if (cmd.timeout)
106 				printf("Turning timeout off.\n");
107 			cmd.timeout = 0;
108 		}
109 	}
110 
111 	/* exec */
112 	run_loadfile(marks, cmd.boothowto);
113 }
114 
115 #ifdef _TEST
116 int
117 main()
118 {
119 	boot(0);
120 	return 0;
121 }
122 #endif
123