xref: /illumos-gate/usr/src/boot/i386/loader/main.c (revision 4adf43b0b51d2123d4add8287f0e31facb0cbab1)
1 /*
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 
29 /*
30  * MD bootstrap main() and assorted miscellaneous
31  * commands.
32  */
33 
34 #include <stand.h>
35 #include <stddef.h>
36 #include <string.h>
37 #include <machine/bootinfo.h>
38 #include <machine/cpufunc.h>
39 #include <machine/psl.h>
40 #include <sys/disk.h>
41 #include <sys/param.h>
42 #include <sys/reboot.h>
43 #include <sys/zfs_bootenv.h>
44 #include <rbx.h>
45 
46 #include "bootstrap.h"
47 #include "common/bootargs.h"
48 #include "libi386/libi386.h"
49 #include "smbios.h"
50 #include "btxv86.h"
51 #include "libzfs.h"
52 
53 CTASSERT(sizeof (struct bootargs) == BOOTARGS_SIZE);
54 CTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO);
55 CTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS);
56 CTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE);
57 
58 /* Arguments passed in from the boot1/boot2 loader */
59 struct bootargs *kargs;
60 
61 uint32_t	opts;
62 static uint32_t	initial_bootdev;
63 static struct bootinfo	*initial_bootinfo;
64 
65 struct arch_switch	archsw;		/* MI/MD interface boundary */
66 
67 static void		extract_currdev(void);
68 static int		isa_inb(int port);
69 static void		isa_outb(int port, int value);
70 void			exit(int code);
71 static void		i386_zfs_probe(void);
72 
73 /* XXX debugging */
74 extern char _end[];
75 
76 static void *heap_top;
77 static void *heap_bottom;
78 
79 caddr_t
80 ptov(uintptr_t x)
81 {
82 	return (PTOV(x));
83 }
84 
85 int
86 main(void)
87 {
88 	int	i;
89 
90 	/* Pick up arguments */
91 	kargs = (void *)__args;
92 	opts = kargs->howto;
93 	initial_bootdev = kargs->bootdev;
94 	initial_bootinfo = kargs->bootinfo ?
95 	    (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
96 
97 	/* Initialize the v86 register set to a known-good state. */
98 	bzero(&v86, sizeof (v86));
99 	v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
100 
101 	/*
102 	 * Initialise the heap as early as possible.
103 	 * Once this is done, malloc() is usable.
104 	 */
105 	bios_getmem();
106 
107 	if (high_heap_size > 0) {
108 		heap_top = PTOV(high_heap_base + high_heap_size);
109 		heap_bottom = PTOV(high_heap_base);
110 		if (high_heap_base < memtop_copyin)
111 			memtop_copyin = high_heap_base;
112 	} else {
113 		heap_top = (void *)PTOV(bios_basemem);
114 		heap_bottom = (void *)_end;
115 	}
116 	setheap(heap_bottom, heap_top);
117 
118 	/* detect ACPI for future reference */
119 	biosacpi_detect();
120 
121 	/*
122 	 * XXX Chicken-and-egg problem; we want to have console output early,
123 	 * but some console attributes may depend on reading from eg. the boot
124 	 * device, which we can't do yet.
125 	 *
126 	 * We can use printf() etc. once this is done.
127 	 * If the previous boot stage has requested a serial console,
128 	 * prefer that.
129 	 */
130 	bi_setboothowto(opts);
131 	if (OPT_CHECK(RBX_DUAL)) {
132 		if (OPT_CHECK(RBX_SERIAL))
133 			setenv("console", "ttya text", 1);
134 		else
135 			setenv("console", "text ttya", 1);
136 	} else if (OPT_CHECK(RBX_SERIAL)) {
137 		setenv("console", "ttya", 1);
138 	} else if (OPT_CHECK(RBX_MUTE)) {
139 		setenv("console", "null", 1);
140 	}
141 	cons_probe();
142 
143 	/*
144 	 * Initialise the block cache. Set the upper limit.
145 	 */
146 	bcache_init(32768, 512);
147 
148 	/*
149 	 * Special handling for PXE and CD booting.
150 	 */
151 	if (kargs->bootinfo == 0) {
152 		/*
153 		 * We only want the PXE disk to try to init itself in the below
154 		 * walk through devsw if we actually booted off of PXE.
155 		 */
156 		if (kargs->bootflags & KARGS_FLAGS_PXE)
157 			pxe_enable(kargs->pxeinfo ?
158 			    PTOV(kargs->pxeinfo) : NULL);
159 		else if (kargs->bootflags & KARGS_FLAGS_CD)
160 			bc_add(initial_bootdev);
161 	}
162 
163 	archsw.arch_autoload = i386_autoload;
164 	archsw.arch_getdev = i386_getdev;
165 	archsw.arch_copyin = i386_copyin;
166 	archsw.arch_copyout = i386_copyout;
167 	archsw.arch_readin = i386_readin;
168 	archsw.arch_isainb = isa_inb;
169 	archsw.arch_isaoutb = isa_outb;
170 	archsw.arch_loadaddr = i386_loadaddr;
171 	archsw.arch_hypervisor = x86_hypervisor;
172 	archsw.arch_zfs_probe = i386_zfs_probe;
173 
174 	/*
175 	 * March through the device switch probing for things.
176 	 */
177 	for (i = 0; devsw[i] != NULL; i++)
178 		if (devsw[i]->dv_init != NULL)
179 			(devsw[i]->dv_init)();
180 
181 	printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024,
182 	    bios_extmem / 1024);
183 	if (initial_bootinfo != NULL) {
184 		initial_bootinfo->bi_basemem = bios_basemem / 1024;
185 		initial_bootinfo->bi_extmem = bios_extmem / 1024;
186 	}
187 
188 	/* detect SMBIOS for future reference */
189 	smbios_detect(NULL);
190 
191 	/* detect PCI BIOS for future reference */
192 	biospci_detect();
193 
194 	printf("\n%s", bootprog_info);
195 
196 	extract_currdev();		/* set $currdev and $loaddev */
197 	autoload_font(OPT_CHECK(RBX_TEXT_MODE) != 0);
198 
199 	bi_isadir();
200 	bios_getsmap();
201 
202 	interact(NULL);
203 
204 	/* if we ever get here, it is an error */
205 	return (1);
206 }
207 
208 /*
209  * Set the 'current device' by (if possible) recovering the boot device as
210  * supplied by the initial bootstrap.
211  *
212  * XXX should be extended for netbooting.
213  */
214 static void
215 extract_currdev(void)
216 {
217 	struct i386_devdesc	new_currdev;
218 	struct zfs_boot_args	*zargs;
219 	char			*bootonce;
220 	int			biosdev = -1;
221 
222 	/* Assume we are booting from a BIOS disk by default */
223 	new_currdev.dd.d_dev = &bioshd;
224 
225 	/* new-style boot loaders such as pxeldr and cdldr */
226 	if (kargs->bootinfo == 0) {
227 		if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
228 			/* we are booting from a CD with cdboot */
229 			new_currdev.dd.d_dev = &bioscd;
230 			new_currdev.dd.d_unit = bd_bios2unit(initial_bootdev);
231 		} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
232 			/* we are booting from pxeldr */
233 			new_currdev.dd.d_dev = &pxedisk;
234 			new_currdev.dd.d_unit = 0;
235 		} else {
236 			/* we don't know what our boot device is */
237 			new_currdev.d_kind.biosdisk.slice = -1;
238 			new_currdev.d_kind.biosdisk.partition = 0;
239 			biosdev = -1;
240 		}
241 	} else if ((kargs->bootflags & KARGS_FLAGS_ZFS) != 0) {
242 		zargs = NULL;
243 		/* check for new style extended argument */
244 		if ((kargs->bootflags & KARGS_FLAGS_EXTARG) != 0)
245 			zargs = (struct zfs_boot_args *)(kargs + 1);
246 
247 		if (zargs != NULL &&
248 		    zargs->size >=
249 		    offsetof(struct zfs_boot_args, primary_pool)) {
250 			/* sufficient data is provided */
251 			new_currdev.d_kind.zfs.pool_guid = zargs->pool;
252 			new_currdev.d_kind.zfs.root_guid = zargs->root;
253 		} else {
254 			/* old style zfsboot block */
255 			new_currdev.d_kind.zfs.pool_guid = kargs->zfspool;
256 			new_currdev.d_kind.zfs.root_guid = 0;
257 		}
258 		new_currdev.dd.d_dev = &zfs_dev;
259 		if ((bootonce = malloc(VDEV_PAD_SIZE)) != NULL) {
260 			if (zfs_get_bootonce(&new_currdev, OS_BOOTONCE_USED,
261 			    bootonce, VDEV_PAD_SIZE) == 0) {
262 				setenv("zfs-bootonce", bootonce, 1);
263 			}
264 			free(bootonce);
265 			(void) zfs_attach_nvstore(&new_currdev);
266 		} else {
267 			printf("Failed to process bootonce data: %s\n",
268 			    strerror(errno));
269 		}
270 	} else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
271 		/* The passed-in boot device is bad */
272 		new_currdev.d_kind.biosdisk.slice = -1;
273 		new_currdev.d_kind.biosdisk.partition = 0;
274 		biosdev = -1;
275 	} else {
276 		new_currdev.d_kind.biosdisk.slice =
277 		    B_SLICE(initial_bootdev) - 1;
278 		new_currdev.d_kind.biosdisk.partition =
279 		    B_PARTITION(initial_bootdev);
280 		biosdev = initial_bootinfo->bi_bios_dev;
281 
282 		/*
283 		 * If we are booted by an old bootstrap, we have to guess at
284 		 * the BIOS unit number. We will lose if there is more than
285 		 * one disk type and we are not booting from the
286 		 * lowest-numbered disk type (ie. SCSI when IDE also exists).
287 		 */
288 		if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2)) {
289 			/*
290 			 * biosdev doesn't match major, assume harddisk
291 			 */
292 			biosdev = 0x80 + B_UNIT(initial_bootdev);
293 		}
294 	}
295 
296 	/*
297 	 * If we are booting off of a BIOS disk and we didn't succeed
298 	 * in determining which one we booted off of, just use disk0:
299 	 * as a reasonable default.
300 	 */
301 	if ((new_currdev.dd.d_dev->dv_type == bioshd.dv_type) &&
302 	    ((new_currdev.dd.d_unit = bd_bios2unit(biosdev)) == -1)) {
303 		printf("Can't work out which disk we are booting "
304 		    "from.\nGuessed BIOS device 0x%x not found by "
305 		    "probes, defaulting to disk0:\n", biosdev);
306 		new_currdev.dd.d_unit = 0;
307 	}
308 
309 	env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
310 	    i386_setcurrdev, env_nounset);
311 	env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev),
312 	    env_noset, env_nounset);
313 }
314 
315 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
316 
317 static int
318 command_reboot(int argc __unused, char *argv[] __unused)
319 {
320 	int i;
321 
322 	for (i = 0; devsw[i] != NULL; ++i)
323 		if (devsw[i]->dv_cleanup != NULL)
324 			(devsw[i]->dv_cleanup)();
325 
326 	printf("Rebooting...\n");
327 	delay(1000000);
328 	__exit(0);
329 }
330 
331 /* provide this for panic, as it's not in the startup code */
332 void
333 exit(int code)
334 {
335 	__exit(code);
336 }
337 
338 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
339 
340 static int
341 command_heap(int argc __unused, char *argv[] __unused)
342 {
343 
344 	mallocstats();
345 	printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
346 	    sbrk(0), heap_top);
347 	return (CMD_OK);
348 }
349 
350 /* ISA bus access functions for PnP. */
351 static int
352 isa_inb(int port)
353 {
354 
355 	return (inb(port));
356 }
357 
358 static void
359 isa_outb(int port, int value)
360 {
361 
362 	outb(port, value);
363 }
364 
365 static void
366 i386_zfs_probe(void)
367 {
368 	char devname[32];
369 	struct i386_devdesc dev;
370 
371 	/*
372 	 * Open all the disks we can find and see if we can reconstruct
373 	 * ZFS pools from them.
374 	 */
375 	dev.dd.d_dev = &bioshd;
376 	for (dev.dd.d_unit = 0; bd_unit2bios(&dev) >= 0; dev.dd.d_unit++) {
377 		snprintf(devname, sizeof (devname), "%s%d:", bioshd.dv_name,
378 		    dev.dd.d_unit);
379 		zfs_probe_dev(devname, NULL);
380 	}
381 }
382