]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - sys/boot/i386/loader/main.c
Copy stable/9 to releng/9.1 as part of the 9.1-RELEASE release process.
[FreeBSD/releng/9.1.git] / sys / boot / i386 / loader / main.c
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 __FBSDID("$FreeBSD$");
29
30 /*
31  * MD bootstrap main() and assorted miscellaneous
32  * commands.
33  */
34
35 #include <stand.h>
36 #include <stddef.h>
37 #include <string.h>
38 #include <machine/bootinfo.h>
39 #include <machine/psl.h>
40 #include <sys/reboot.h>
41
42 #include "bootstrap.h"
43 #include "common/bootargs.h"
44 #include "libi386/libi386.h"
45 #include "btxv86.h"
46
47 #ifdef LOADER_ZFS_SUPPORT
48 #include "../zfs/libzfs.h"
49 #endif
50
51 CTASSERT(sizeof(struct bootargs) == BOOTARGS_SIZE);
52 CTASSERT(offsetof(struct bootargs, bootinfo) == BA_BOOTINFO);
53 CTASSERT(offsetof(struct bootargs, bootflags) == BA_BOOTFLAGS);
54 CTASSERT(offsetof(struct bootinfo, bi_size) == BI_SIZE);
55
56 /* Arguments passed in from the boot1/boot2 loader */
57 static struct bootargs *kargs;
58
59 static u_int32_t        initial_howto;
60 static u_int32_t        initial_bootdev;
61 static struct bootinfo  *initial_bootinfo;
62
63 struct arch_switch      archsw;         /* MI/MD interface boundary */
64
65 static void             extract_currdev(void);
66 static int              isa_inb(int port);
67 static void             isa_outb(int port, int value);
68 void                    exit(int code);
69 #ifdef LOADER_ZFS_SUPPORT
70 static void             i386_zfs_probe(void);
71 #endif
72
73 /* from vers.c */
74 extern  char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
75
76 /* XXX debugging */
77 extern char end[];
78
79 static void *heap_top;
80 static void *heap_bottom;
81
82 int
83 main(void)
84 {
85     int                 i;
86
87     /* Pick up arguments */
88     kargs = (void *)__args;
89     initial_howto = kargs->howto;
90     initial_bootdev = kargs->bootdev;
91     initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
92
93     /* Initialize the v86 register set to a known-good state. */
94     bzero(&v86, sizeof(v86));
95     v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
96
97     /* 
98      * Initialise the heap as early as possible.  Once this is done, malloc() is usable.
99      */
100     bios_getmem();
101
102 #if defined(LOADER_BZIP2_SUPPORT) || defined(LOADER_FIREWIRE_SUPPORT) || \
103     defined(LOADER_GPT_SUPPORT) || defined(LOADER_ZFS_SUPPORT)
104     if (high_heap_size > 0) {
105         heap_top = PTOV(high_heap_base + high_heap_size);
106         heap_bottom = PTOV(high_heap_base);
107         if (high_heap_base < memtop_copyin)
108             memtop_copyin = high_heap_base;
109     } else
110 #endif
111     {
112         heap_top = (void *)PTOV(bios_basemem);
113         heap_bottom = (void *)end;
114     }
115     setheap(heap_bottom, heap_top);
116
117     /* 
118      * XXX Chicken-and-egg problem; we want to have console output early, but some
119      * console attributes may depend on reading from eg. the boot device, which we
120      * can't do yet.
121      *
122      * We can use printf() etc. once this is done.
123      * If the previous boot stage has requested a serial console, prefer that.
124      */
125     bi_setboothowto(initial_howto);
126     if (initial_howto & RB_MULTIPLE) {
127         if (initial_howto & RB_SERIAL)
128             setenv("console", "comconsole vidconsole", 1);
129         else
130             setenv("console", "vidconsole comconsole", 1);
131     } else if (initial_howto & RB_SERIAL)
132         setenv("console", "comconsole", 1);
133     else if (initial_howto & RB_MUTE)
134         setenv("console", "nullconsole", 1);
135     cons_probe();
136
137     /*
138      * Initialise the block cache
139      */
140     bcache_init(32, 512);       /* 16k cache XXX tune this */
141
142     /*
143      * Special handling for PXE and CD booting.
144      */
145     if (kargs->bootinfo == 0) {
146         /*
147          * We only want the PXE disk to try to init itself in the below
148          * walk through devsw if we actually booted off of PXE.
149          */
150         if (kargs->bootflags & KARGS_FLAGS_PXE)
151             pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
152         else if (kargs->bootflags & KARGS_FLAGS_CD)
153             bc_add(initial_bootdev);
154     }
155
156     archsw.arch_autoload = i386_autoload;
157     archsw.arch_getdev = i386_getdev;
158     archsw.arch_copyin = i386_copyin;
159     archsw.arch_copyout = i386_copyout;
160     archsw.arch_readin = i386_readin;
161     archsw.arch_isainb = isa_inb;
162     archsw.arch_isaoutb = isa_outb;
163 #ifdef LOADER_ZFS_SUPPORT
164     archsw.arch_zfs_probe = i386_zfs_probe;
165 #endif
166
167     /*
168      * March through the device switch probing for things.
169      */
170     for (i = 0; devsw[i] != NULL; i++)
171         if (devsw[i]->dv_init != NULL)
172             (devsw[i]->dv_init)();
173     printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024, bios_extmem / 1024);
174     if (initial_bootinfo != NULL) {
175         initial_bootinfo->bi_basemem = bios_basemem / 1024;
176         initial_bootinfo->bi_extmem = bios_extmem / 1024;
177     }
178
179     /* detect ACPI for future reference */
180     biosacpi_detect();
181
182     /* detect SMBIOS for future reference */
183     smbios_detect();
184
185     printf("\n");
186     printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
187     printf("(%s, %s)\n", bootprog_maker, bootprog_date);
188
189     extract_currdev();                          /* set $currdev and $loaddev */
190     setenv("LINES", "24", 1);                   /* optional */
191     
192     bios_getsmap();
193
194     interact();                 /* doesn't return */
195
196     /* if we ever get here, it is an error */
197     return (1);
198 }
199
200 /*
201  * Set the 'current device' by (if possible) recovering the boot device as 
202  * supplied by the initial bootstrap.
203  *
204  * XXX should be extended for netbooting.
205  */
206 static void
207 extract_currdev(void)
208 {
209     struct i386_devdesc         new_currdev;
210 #ifdef LOADER_ZFS_SUPPORT
211     struct zfs_boot_args        *zargs;
212 #endif
213     int                         biosdev = -1;
214
215     /* Assume we are booting from a BIOS disk by default */
216     new_currdev.d_dev = &biosdisk;
217
218     /* new-style boot loaders such as pxeldr and cdldr */
219     if (kargs->bootinfo == 0) {
220         if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
221             /* we are booting from a CD with cdboot */
222             new_currdev.d_dev = &bioscd;
223             new_currdev.d_unit = bc_bios2unit(initial_bootdev);
224         } else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
225             /* we are booting from pxeldr */
226             new_currdev.d_dev = &pxedisk;
227             new_currdev.d_unit = 0;
228         } else {
229             /* we don't know what our boot device is */
230             new_currdev.d_kind.biosdisk.slice = -1;
231             new_currdev.d_kind.biosdisk.partition = 0;
232             biosdev = -1;
233         }
234 #ifdef LOADER_ZFS_SUPPORT
235     } else if ((kargs->bootflags & KARGS_FLAGS_ZFS) != 0) {
236         zargs = NULL;
237         /* check for new style extended argument */
238         if ((kargs->bootflags & KARGS_FLAGS_EXTARG) != 0)
239             zargs = (struct zfs_boot_args *)(kargs + 1);
240
241         if (zargs != NULL && zargs->size >= sizeof(*zargs)) {
242             /* sufficient data is provided */
243             new_currdev.d_kind.zfs.pool_guid = zargs->pool;
244             new_currdev.d_kind.zfs.root_guid = zargs->root;
245         } else {
246             /* old style zfsboot block */
247             new_currdev.d_kind.zfs.pool_guid = kargs->zfspool;
248             new_currdev.d_kind.zfs.root_guid = 0;
249         }
250         new_currdev.d_dev = &zfs_dev;
251 #endif
252     } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
253         /* The passed-in boot device is bad */
254         new_currdev.d_kind.biosdisk.slice = -1;
255         new_currdev.d_kind.biosdisk.partition = 0;
256         biosdev = -1;
257     } else {
258         new_currdev.d_kind.biosdisk.slice = B_SLICE(initial_bootdev) - 1;
259         new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
260         biosdev = initial_bootinfo->bi_bios_dev;
261
262         /*
263          * If we are booted by an old bootstrap, we have to guess at the BIOS
264          * unit number.  We will lose if there is more than one disk type
265          * and we are not booting from the lowest-numbered disk type 
266          * (ie. SCSI when IDE also exists).
267          */
268         if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))   /* biosdev doesn't match major */
269             biosdev = 0x80 + B_UNIT(initial_bootdev);           /* assume harddisk */
270     }
271     new_currdev.d_type = new_currdev.d_dev->dv_type;
272
273     /*
274      * If we are booting off of a BIOS disk and we didn't succeed in determining
275      * which one we booted off of, just use disk0: as a reasonable default.
276      */
277     if ((new_currdev.d_type == biosdisk.dv_type) &&
278         ((new_currdev.d_unit = bd_bios2unit(biosdev)) == -1)) {
279         printf("Can't work out which disk we are booting from.\n"
280                "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
281         new_currdev.d_unit = 0;
282     }
283
284     env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
285                i386_setcurrdev, env_nounset);
286     env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
287                env_nounset);
288 }
289
290 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
291
292 static int
293 command_reboot(int argc, char *argv[])
294 {
295     int i;
296
297     for (i = 0; devsw[i] != NULL; ++i)
298         if (devsw[i]->dv_cleanup != NULL)
299             (devsw[i]->dv_cleanup)();
300
301     printf("Rebooting...\n");
302     delay(1000000);
303     __exit(0);
304 }
305
306 /* provide this for panic, as it's not in the startup code */
307 void
308 exit(int code)
309 {
310     __exit(code);
311 }
312
313 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
314
315 static int
316 command_heap(int argc, char *argv[])
317 {
318     mallocstats();
319     printf("heap base at %p, top at %p, upper limit at %p\n", heap_bottom,
320       sbrk(0), heap_top);
321     return(CMD_OK);
322 }
323
324 /* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
325 static int              
326 isa_inb(int port)
327 {
328     u_char      data;
329     
330     if (__builtin_constant_p(port) && 
331         (((port) & 0xffff) < 0x100) && 
332         ((port) < 0x10000)) {
333         __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
334     } else {
335         __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
336     }
337     return(data);
338 }
339
340 static void
341 isa_outb(int port, int value)
342 {
343     u_char      al = value;
344     
345     if (__builtin_constant_p(port) && 
346         (((port) & 0xffff) < 0x100) && 
347         ((port) < 0x10000)) {
348         __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
349     } else {
350         __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
351     }
352 }
353
354 #ifdef LOADER_ZFS_SUPPORT
355 static void
356 i386_zfs_probe(void)
357 {
358     char devname[32];
359     int unit, slice;
360
361     /*
362      * Open all the disks we can find and see if we can reconstruct
363      * ZFS pools from them. Bogusly assumes that the disks are named
364      * diskN, diskNpM or diskNsM.
365      */
366     for (unit = 0; unit < MAXBDDEV; unit++) {
367         sprintf(devname, "disk%d:", unit);
368         if (zfs_probe_dev(devname, NULL) == ENXIO)
369             continue;
370         for (slice = 1; slice <= 128; slice++) {
371             sprintf(devname, "disk%dp%d:", unit, slice);
372             zfs_probe_dev(devname, NULL);
373         }
374         for (slice = 1; slice <= 4; slice++) {
375             sprintf(devname, "disk%ds%d:", unit, slice);
376             zfs_probe_dev(devname, NULL);
377         }
378     }
379 }
380 #endif