xref: /freebsd/stand/userboot/userboot/main.c (revision e4c66ddabdb470bab319705c1834a4867c508a43)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 1998,2000 Doug Rabson <dfr@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <stand.h>
32 #include <string.h>
33 #include <setjmp.h>
34 #include <sys/disk.h>
35 
36 #include "bootstrap.h"
37 #include "disk.h"
38 #include "libuserboot.h"
39 
40 #if defined(USERBOOT_ZFS_SUPPORT)
41 #include "libzfs.h"
42 
43 static void userboot_zfs_probe(void);
44 static int userboot_zfs_found;
45 #endif
46 
47 /* Minimum version required */
48 #define	USERBOOT_VERSION	USERBOOT_VERSION_3
49 
50 #define	MALLOCSZ		(64*1024*1024)
51 
52 struct loader_callbacks *callbacks;
53 void *callbacks_arg;
54 
55 static jmp_buf jb;
56 
57 struct arch_switch archsw;	/* MI/MD interface boundary */
58 
59 static void	extract_currdev(void);
60 
61 void
62 delay(int usec)
63 {
64 
65 	CALLBACK(delay, usec);
66 }
67 
68 void
69 exit(int v)
70 {
71 
72 	CALLBACK(exit, v);
73 	longjmp(jb, 1);
74 }
75 
76 void
77 loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
78 {
79 	static char mallocbuf[MALLOCSZ];
80 	char *var;
81 	int i;
82 
83 	if (version < USERBOOT_VERSION)
84 		abort();
85 
86 	callbacks = cb;
87 	callbacks_arg = arg;
88 	userboot_disk_maxunit = ndisks;
89 
90 	/*
91 	 * initialise the heap as early as possible.  Once this is done,
92 	 * alloc() is usable.
93 	 */
94 	setheap((void *)mallocbuf, (void *)(mallocbuf + sizeof(mallocbuf)));
95 
96 	/*
97 	 * Hook up the console
98 	 */
99 	cons_probe();
100 
101 	printf("\n%s", bootprog_info);
102 #if 0
103 	printf("Memory: %ld k\n", memsize() / 1024);
104 #endif
105 
106 	setenv("LINES", "24", 1);	/* optional */
107 
108 	/*
109 	 * Set custom environment variables
110 	 */
111 	i = 0;
112 	while (1) {
113 		var = CALLBACK(getenv, i++);
114 		if (var == NULL)
115 			break;
116 		putenv(var);
117 	}
118 
119 	archsw.arch_autoload = userboot_autoload;
120 	archsw.arch_getdev = userboot_getdev;
121 	archsw.arch_copyin = userboot_copyin;
122 	archsw.arch_copyout = userboot_copyout;
123 	archsw.arch_readin = userboot_readin;
124 #if defined(USERBOOT_ZFS_SUPPORT)
125 	archsw.arch_zfs_probe = userboot_zfs_probe;
126 #endif
127 
128 	/*
129 	 * Initialise the block cache. Set the upper limit.
130 	 */
131 	bcache_init(32768, 512);
132 	/*
133 	 * March through the device switch probing for things.
134 	 */
135 	for (i = 0; devsw[i] != NULL; i++)
136 		if (devsw[i]->dv_init != NULL)
137 			(devsw[i]->dv_init)();
138 
139 	extract_currdev();
140 
141 	if (setjmp(jb))
142 		return;
143 
144 	interact();			/* doesn't return */
145 
146 	exit(0);
147 }
148 
149 /*
150  * Set the 'current device' by (if possible) recovering the boot device as
151  * supplied by the initial bootstrap.
152  */
153 static void
154 extract_currdev(void)
155 {
156 	struct disk_devdesc dev;
157 	struct devdesc *dd;
158 #if defined(USERBOOT_ZFS_SUPPORT)
159 	struct zfs_devdesc zdev;
160 
161 	if (userboot_zfs_found) {
162 
163 		/* Leave the pool/root guid's unassigned */
164 		bzero(&zdev, sizeof(zdev));
165 		zdev.dd.d_dev = &zfs_dev;
166 
167 		init_zfs_bootenv(zfs_fmtdev(&zdev));
168 		dd = &zdev.dd;
169 	} else
170 #endif
171 
172 	if (userboot_disk_maxunit > 0) {
173 		dev.dd.d_dev = &userboot_disk;
174 		dev.dd.d_unit = 0;
175 		dev.d_slice = 0;
176 		dev.d_partition = 0;
177 		/*
178 		 * If we cannot auto-detect the partition type then
179 		 * access the disk as a raw device.
180 		 */
181 		if (dev.dd.d_dev->dv_open(NULL, &dev)) {
182 			dev.d_slice = -1;
183 			dev.d_partition = -1;
184 		}
185 		dd = &dev.dd;
186 	} else {
187 		dev.dd.d_dev = &host_dev;
188 		dev.dd.d_unit = 0;
189 		dd = &dev.dd;
190 	}
191 
192 	env_setenv("currdev", EV_VOLATILE, userboot_fmtdev(dd),
193 	    userboot_setcurrdev, env_nounset);
194 	env_setenv("loaddev", EV_VOLATILE, userboot_fmtdev(dd),
195 	    env_noset, env_nounset);
196 }
197 
198 #if defined(USERBOOT_ZFS_SUPPORT)
199 static void
200 userboot_zfs_probe(void)
201 {
202 	char devname[32];
203 	uint64_t pool_guid;
204 	int unit;
205 
206 	/*
207 	 * Open all the disks we can find and see if we can reconstruct
208 	 * ZFS pools from them. Record if any were found.
209 	 */
210 	for (unit = 0; unit < userboot_disk_maxunit; unit++) {
211 		sprintf(devname, "disk%d:", unit);
212 		pool_guid = 0;
213 		zfs_probe_dev(devname, &pool_guid);
214 		if (pool_guid != 0)
215 			userboot_zfs_found = 1;
216 	}
217 }
218 #endif
219 
220 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
221 
222 static int
223 command_quit(int argc, char *argv[])
224 {
225 
226 	exit(USERBOOT_EXIT_QUIT);
227 	return (CMD_OK);
228 }
229 
230 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
231 
232 static int
233 command_reboot(int argc, char *argv[])
234 {
235 
236 	exit(USERBOOT_EXIT_REBOOT);
237 	return (CMD_OK);
238 }
239