xref: /titanic_54/usr/src/boot/sys/boot/i386/loader/main.c (revision cf7dd858456bdf14e1356d8b4338c2e9b5fadd58)
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/multiboot2.h>
44 
45 #include "bootstrap.h"
46 #include "common/bootargs.h"
47 #include "libi386/libi386.h"
48 #include "libi386/smbios.h"
49 #include "btxv86.h"
50 
51 #ifdef LOADER_ZFS_SUPPORT
52 #include "../zfs/libzfs.h"
53 #endif
54 
55 CTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE);
56 CTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO);
57 CTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS);
58 CTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE);
59 
60 /* Arguments passed in from the boot1/boot2 loader */
61 static struct bootargs *kargs;
62 
63 static u_int32_t	initial_howto;
64 static u_int32_t	initial_bootdev;
65 static struct bootinfo	*initial_bootinfo;
66 
67 struct arch_switch	archsw;		/* MI/MD interface boundary */
68 
69 static void		extract_currdev(void);
70 static int		isa_inb(int port);
71 static void		isa_outb(int port, int value);
72 void			exit(int code);
73 #ifdef LOADER_ZFS_SUPPORT
74 static void		i386_zfs_probe(void);
75 #endif
76 
77 /* from vers.c */
78 extern	char bootprog_info[];
79 
80 /* XXX debugging */
81 extern char end[];
82 
83 static void *heap_top;
84 static void *heap_bottom;
85 
86 static uint64_t
87 i386_loadaddr(u_int type, void *data, uint64_t addr)
88 {
89 	/*
90 	 * Our modules are page aligned.
91 	 */
92 	if (type == LOAD_RAW || type == LOAD_MEM)
93                 return (roundup2(addr, MULTIBOOT_MOD_ALIGN));
94 
95         return (addr);
96 }
97 
98 int
99 main(void)
100 {
101     int			i;
102 
103     /* Pick up arguments */
104     kargs = (void *)__args;
105     initial_howto = kargs->howto;
106     initial_bootdev = kargs->bootdev;
107     initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
108 
109     /* Initialize the v86 register set to a known-good state. */
110     bzero(&v86, sizeof(v86));
111     v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
112 
113     /*
114      * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
115      */
116     bios_getmem();
117 
118 #if defined(LOADER_BZIP2_SUPPORT) || defined(LOADER_FIREWIRE_SUPPORT) || \
119     defined(LOADER_GPT_SUPPORT) || defined(LOADER_ZFS_SUPPORT)
120     if (high_heap_size > 0) {
121 	heap_top = PTOV(high_heap_base + high_heap_size);
122 	heap_bottom = PTOV(high_heap_base);
123 	if (high_heap_base < memtop_copyin)
124 	    memtop_copyin = high_heap_base;
125     } else
126 #endif
127     {
128 	heap_top = (void *)PTOV(bios_basemem);
129 	heap_bottom = (void *)end;
130     }
131     setheap(heap_bottom, heap_top);
132 
133     /*
134      * XXX Chicken-and-egg problem; we want to have console output early, but some
135      * console attributes may depend on reading from eg. the boot device, which we
136      * can't do yet.
137      *
138      * We can use printf() etc. once this is done.
139      * If the previous boot stage has requested a serial console, prefer that.
140      */
141     bi_setboothowto(initial_howto);
142     if (initial_howto & RB_MULTIPLE) {
143 	if (initial_howto & RB_SERIAL)
144 	    setenv("console", "ttya text", 1);
145 	else
146 	    setenv("console", "text ttya", 1);
147     } else if (initial_howto & RB_SERIAL)
148 	setenv("console", "ttya", 1);
149     else if (initial_howto & RB_MUTE)
150 	setenv("console", "null", 1);
151     cons_probe();
152 
153     /*
154      * Initialise the block cache. Set the upper limit.
155      */
156     bcache_init(32768, 512);
157 
158     /*
159      * Special handling for PXE and CD booting.
160      */
161     if (kargs->bootinfo == 0) {
162 	/*
163 	 * We only want the PXE disk to try to init itself in the below
164 	 * walk through devsw if we actually booted off of PXE.
165 	 */
166 	if (kargs->bootflags & KARGS_FLAGS_PXE)
167 	    pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
168 	else if (kargs->bootflags & KARGS_FLAGS_CD)
169 	    bc_add(initial_bootdev);
170     }
171 
172     archsw.arch_autoload = i386_autoload;
173     archsw.arch_getdev = i386_getdev;
174     archsw.arch_copyin = i386_copyin;
175     archsw.arch_copyout = i386_copyout;
176     archsw.arch_readin = i386_readin;
177     archsw.arch_isainb = isa_inb;
178     archsw.arch_isaoutb = isa_outb;
179     archsw.arch_loadaddr = i386_loadaddr;
180 #ifdef LOADER_ZFS_SUPPORT
181     archsw.arch_zfs_probe = i386_zfs_probe;
182 #endif
183 
184     /*
185      * March through the device switch probing for things.
186      */
187     for (i = 0; devsw[i] != NULL; i++)
188 	if (devsw[i]->dv_init != NULL)
189 	    (devsw[i]->dv_init)();
190     printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
191     if (initial_bootinfo != NULL) {
192 	initial_bootinfo->bi_basemem = bios_basemem / 1024;
193 	initial_bootinfo->bi_extmem = bios_extmem / 1024;
194     }
195 
196     /* detect ACPI for future reference */
197     biosacpi_detect();
198 
199     /* detect SMBIOS for future reference */
200     smbios_detect(NULL);
201 
202     /* detect PCI BIOS for future reference */
203     biospci_detect();
204 
205     printf("\n%s", bootprog_info);
206 
207     extract_currdev();				/* set $currdev and $loaddev */
208     setenv("LINES", "24", 1);			/* optional */
209     setenv("COLUMNS", "80", 1);			/* optional */
210 
211     if (bi_checkcpu())
212 	setenv("ISADIR", "amd64", 1);
213     else
214 	setenv("ISADIR", "", 1);
215 
216     bios_getsmap();
217 
218     interact(NULL);
219 
220     /* if we ever get here, it is an error */
221     return (1);
222 }
223 
224 /*
225  * Set the 'current device' by (if possible) recovering the boot device as
226  * supplied by the initial bootstrap.
227  *
228  * XXX should be extended for netbooting.
229  */
230 static void
231 extract_currdev(void)
232 {
233     struct i386_devdesc		new_currdev;
234 #ifdef LOADER_ZFS_SUPPORT
235     char			buf[20];
236     struct zfs_boot_args	*zargs;
237 #endif
238     int				biosdev = -1;
239 
240     /* Assume we are booting from a BIOS disk by default */
241     new_currdev.d_dev = &biosdisk;
242 
243     /* new-style boot loaders such as pxeldr and cdldr */
244     if (kargs->bootinfo == 0) {
245         if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
246 	    /* we are booting from a CD with cdboot */
247 	    new_currdev.d_dev = &bioscd;
248 	    new_currdev.d_unit = bc_bios2unit(initial_bootdev);
249 	} else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
250 	    /* we are booting from pxeldr */
251 	    new_currdev.d_dev = &pxedisk;
252 	    new_currdev.d_unit = 0;
253 	} else {
254 	    /* we don't know what our boot device is */
255 	    new_currdev.d_kind.biosdisk.slice = -1;
256 	    new_currdev.d_kind.biosdisk.partition = 0;
257 	    biosdev = -1;
258 	}
259 #ifdef LOADER_ZFS_SUPPORT
260     } else if ((kargs->bootflags & KARGS_FLAGS_ZFS) != 0) {
261 	zargs = NULL;
262 	/* check for new style extended argument */
263 	if ((kargs->bootflags & KARGS_FLAGS_EXTARG) != 0)
264 	    zargs = (struct zfs_boot_args *)(kargs + 1);
265 
266 	if (zargs != NULL &&
267 	    zargs->size >= offsetof(struct zfs_boot_args, primary_pool)) {
268 	    /* sufficient data is provided */
269 	    new_currdev.d_kind.zfs.pool_guid = zargs->pool;
270 	    new_currdev.d_kind.zfs.root_guid = zargs->root;
271 	    if (zargs->size >= sizeof(*zargs) && zargs->primary_vdev != 0) {
272 		sprintf(buf, "%llu", zargs->primary_pool);
273 		setenv("vfs.zfs.boot.primary_pool", buf, 1);
274 		sprintf(buf, "%llu", zargs->primary_vdev);
275 		setenv("vfs.zfs.boot.primary_vdev", buf, 1);
276 	    }
277 	} else {
278 	    /* old style zfsboot block */
279 	    new_currdev.d_kind.zfs.pool_guid = kargs->zfspool;
280 	    new_currdev.d_kind.zfs.root_guid = 0;
281 	}
282 	new_currdev.d_dev = &zfs_dev;
283 #endif
284     } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
285 	/* The passed-in boot device is bad */
286 	new_currdev.d_kind.biosdisk.slice = -1;
287 	new_currdev.d_kind.biosdisk.partition = 0;
288 	biosdev = -1;
289     } else {
290 	new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1;
291 	new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
292 	biosdev = initial_bootinfo->bi_bios_dev;
293 
294 	/*
295 	 * If we are booted by an old bootstrap, we have to guess at the BIOS
296 	 * unit number.  We will lose if there is more than one disk type
297 	 * and we are not booting from the lowest-numbered disk type
298 	 * (ie. SCSI when IDE also exists).
299 	 */
300 	if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))	/* biosdev doesn't match major */
301 	    biosdev = 0x80 + B_UNIT(initial_bootdev);		/* assume harddisk */
302     }
303     new_currdev.d_type = new_currdev.d_dev->dv_type;
304 
305     /*
306      * If we are booting off of a BIOS disk and we didn't succeed in determining
307      * which one we booted off of, just use disk0: as a reasonable default.
308      */
309     if ((new_currdev.d_type == biosdisk.dv_type) &&
310 	((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) {
311 	printf("Can't work out which disk we are booting from.\n"
312 	       "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
313 	new_currdev.d_unit = 0;
314     }
315 
316 #ifdef LOADER_ZFS_SUPPORT
317 #ifdef __FreeBSD__
318     if (new_currdev.d_type == DEVT_ZFS)
319 	init_zfs_bootenv(zfs_fmtdev(&new_currdev));
320 #endif
321 #endif
322 
323     env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
324 	       i386_setcurrdev, env_nounset);
325     env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
326 	       env_nounset);
327 }
328 
329 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
330 
331 static int
332 command_reboot(int argc, char *argv[])
333 {
334     int i;
335 
336     for (i = 0; devsw[i] != NULL; ++i)
337 	if (devsw[i]->dv_cleanup != NULL)
338 	    (devsw[i]->dv_cleanup)();
339 
340     printf("Rebooting...\n");
341     delay(1000000);
342     __exit(0);
343 }
344 
345 /* provide this for panic, as it's not in the startup code */
346 void
347 exit(int code)
348 {
349     __exit(code);
350 }
351 
352 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
353 
354 static int
355 command_heap(int argc, char *argv[])
356 {
357     mallocstats();
358     printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
359       sbrk(0), heap_top);
360     return(CMD_OK);
361 }
362 
363 #ifdef LOADER_ZFS_SUPPORT
364 COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
365     command_lszfs);
366 
367 static int
368 command_lszfs(int argc, char *argv[])
369 {
370     int err;
371 
372     if (argc != 2) {
373 	command_errmsg = "wrong number of arguments";
374 	return (CMD_ERROR);
375     }
376 
377     err = zfs_list(argv[1]);
378     if (err != 0) {
379 	command_errmsg = strerror(err);
380 	return (CMD_ERROR);
381     }
382 
383     return (CMD_OK);
384 }
385 
386 #ifdef __FreeBSD__
387 COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
388     command_reloadbe);
389 
390 static int
391 command_reloadbe(int argc, char *argv[])
392 {
393     int err;
394     char *root;
395 
396     if (argc > 2) {
397 	command_errmsg = "wrong number of arguments";
398 	return (CMD_ERROR);
399     }
400 
401     if (argc == 2) {
402 	err = zfs_bootenv(argv[1]);
403     } else {
404 	root = getenv("zfs_be_root");
405 	if (root == NULL) {
406 	    /* There does not appear to be a ZFS pool here, exit without error */
407 	    return (CMD_OK);
408 	}
409 	err = zfs_bootenv(getenv("zfs_be_root"));
410     }
411 
412     if (err != 0) {
413 	command_errmsg = strerror(err);
414 	return (CMD_ERROR);
415     }
416 
417     return (CMD_OK);
418 }
419 #endif /* __FreeBSD__ */
420 #endif
421 
422 /* ISA bus access functions for PnP. */
423 static int
424 isa_inb(int port)
425 {
426 
427     return (inb(port));
428 }
429 
430 static void
431 isa_outb(int port, int value)
432 {
433 
434     outb(port, value);
435 }
436 
437 #ifdef LOADER_ZFS_SUPPORT
438 static void
439 i386_zfs_probe(void)
440 {
441     char devname[32];
442     int unit;
443 
444     /*
445      * Open all the disks we can find and see if we can reconstruct
446      * ZFS pools from them.
447      */
448     for (unit = 0; unit < MAXBDDEV; unit++) {
449 	if (bd_unit2bios(unit) == -1)
450 	    break;
451 	sprintf(devname, "disk%d:", unit);
452 	zfs_probe_dev(devname, NULL);
453     }
454 }
455 
456 uint64_t
457 ldi_get_size(void *priv)
458 {
459 	int fd = (uintptr_t) priv;
460 	uint64_t size;
461 
462 	ioctl(fd, DIOCGMEDIASIZE, &size);
463 	return (size);
464 }
465 #endif
466