boot - Put loader heap in high memory
[dragonfly.git] / sys / boot / pc32 / loader / main.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  *
58  * $FreeBSD: src/sys/boot/i386/loader/main.c,v 1.28 2003/08/25 23:28:32 obrien Exp $
59  */
60
61 /*
62  * MD bootstrap main() and assorted miscellaneous
63  * commands.
64  */
65
66 #include <stand.h>
67 #include <string.h>
68 #include <machine/bootinfo.h>
69 #include <machine/psl.h>
70 #include <sys/reboot.h>
71
72 #include "bootstrap.h"
73 #include "libi386/libi386.h"
74 #include "btxv86.h"
75
76 #define KARGS_FLAGS_CD          0x1
77 #define KARGS_FLAGS_PXE         0x2
78
79 /* Arguments passed in from the boot1/boot2 loader */
80 static struct 
81 {
82     u_int32_t   howto;
83     u_int32_t   bootdev;
84     u_int32_t   bootflags;
85     u_int32_t   pxeinfo;
86     u_int32_t   res2;
87     u_int32_t   bootinfo;
88 } *kargs;
89
90 static u_int32_t        initial_howto;
91 static u_int32_t        initial_bootdev;
92 static struct bootinfo  *initial_bootinfo;
93
94 struct arch_switch      archsw;         /* MI/MD interface boundary */
95
96 static void             extract_currdev(void);
97 static int              isa_inb(int port);
98 static void             isa_outb(int port, int value);
99 void                    exit(int code);
100
101 /* from vers.c */
102 extern  char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
103
104 /* XXX debugging */
105 extern char _end[];
106
107 #define COMCONSOLE_DEBUG
108 #ifdef COMCONSOLE_DEBUG
109
110 static void
111 WDEBUG_INIT(void)
112 {
113     isa_outb(0x3f8+3, 0x83);    /* DLAB + 8N1 */
114     isa_outb(0x3f8+0, (115200 / 9600) & 0xFF);
115     isa_outb(0x3f8+1, (115200 / 9600) >> 8);
116     isa_outb(0x3f8+3, 0x03);    /* 8N1 */
117     isa_outb(0x3f8+4, 0x03);    /* RTS+DTR */
118     isa_outb(0x3f8+2, 0x01);    /* FIFO_ENABLE */
119 }
120
121 static void
122 WDEBUG(char c)
123 {
124     isa_outb(0x3f8, c);
125 }
126
127 #else
128
129 #define WDEBUG(x)
130 #define WDEBUG_INIT()
131
132 #endif
133
134 int
135 main(void)
136 {
137     char *memend;
138     int i;
139
140     WDEBUG_INIT();
141     WDEBUG('X');
142
143     /* Pick up arguments */
144     kargs = (void *)__args;
145     initial_howto = kargs->howto;
146     initial_bootdev = kargs->bootdev;
147     initial_bootinfo = kargs->bootinfo ? (struct bootinfo *)PTOV(kargs->bootinfo) : NULL;
148
149 #ifdef COMCONSOLE_DEBUG
150     printf("args at %p initial_howto = %08x bootdev = %08x bootinfo = %p\n", 
151         kargs, initial_howto, initial_bootdev, initial_bootinfo);
152 #endif
153
154     /* Initialize the v86 register set to a known-good state. */
155     bzero(&v86, sizeof(v86));
156     v86.efl = PSL_RESERVED_DEFAULT | PSL_I;
157
158
159     /* 
160      * Initialize the heap as early as possible.  Once this is done, 
161      * malloc() is usable. 
162      *
163      * Don't include our stack in the heap.  If the stack is in low
164      * user memory use {end,bios_basemem}.  If the stack is in high
165      * user memory but not extended memory then don't let the heap
166      * overlap the stack.  If the stack is in extended memory limit
167      * the heap to bios_basemem.
168      *
169      * Be sure to use the virtual bios_basemem address rather then
170      * the physical bios_basemem address or we may overwrite BIOS 
171      * data.
172      */
173     bios_getmem();
174     memend = (char *)&memend - 0x8000;  /* space for stack (16K) */
175     memend = (char *)((uintptr_t)memend & ~(uintptr_t)(0x1000 - 1));
176
177     /*
178      * Steal the heap from high memory.  bios_basemem no longer has
179      * enough space, last failure was the gunzip code for initrd.img
180      * needing > ~32KB of temporary buffer space and we ran out.  again.
181      *
182      * High memory reserves at least ~1MB for the loader.
183      */
184 #if 0
185     if (memend < (char *)_end) {
186         setheap((void *)_end, PTOV(bios_basemem));
187     } else {
188         if (memend > (char *)PTOV(bios_basemem))
189             memend = (char *)PTOV(bios_basemem);
190         setheap((void *)_end, memend);
191     }
192 #endif
193     setheap((void *)heapbase, (void *)memtop);
194
195     /* 
196      * XXX Chicken-and-egg problem; we want to have console output early, 
197      * but some console attributes may depend on reading from eg. the boot
198      * device, which we can't do yet.
199      *
200      * We can use printf() etc. once this is done.   The previous boot stage
201      * might have requested a video or serial preference, in which case we
202      * set it.  If neither is specified or both are specified we leave the
203      * console environment variable alone, defaulting to dual boot.
204      */
205     if (initial_howto & RB_MUTE) {
206         setenv("console", "nullconsole", 1);
207     } else if ((initial_howto & (RB_VIDEO|RB_SERIAL)) != (RB_VIDEO|RB_SERIAL)) {
208         if (initial_howto & RB_VIDEO)
209             setenv("console", "vidconsole", 1);
210         if (initial_howto & RB_SERIAL)
211             setenv("console", "comconsole", 1);
212     }
213     cons_probe();
214
215     /*
216      * Initialise the block cache
217      */
218     bcache_init(32, 512);       /* 16k cache XXX tune this */
219
220     /*
221      * Special handling for PXE and CD booting.
222      */
223     if (kargs->bootinfo == 0) {
224         /*
225          * We only want the PXE disk to try to init itself in the below
226          * walk through devsw if we actually booted off of PXE.
227          */
228         if (kargs->bootflags & KARGS_FLAGS_PXE)
229             pxe_enable(kargs->pxeinfo ? PTOV(kargs->pxeinfo) : NULL);
230         else if (kargs->bootflags & KARGS_FLAGS_CD)
231             bc_add(initial_bootdev);
232     }
233
234     /*
235      * March through the device switch probing for things.
236      */
237     for (i = 0; devsw[i] != NULL; i++) {
238         WDEBUG('M' + i);
239         if (devsw[i]->dv_init != NULL)
240             (devsw[i]->dv_init)();
241         WDEBUG('M' + i);
242     }
243     printf("BIOS %dkB/%dkB available memory\n", 
244             bios_basemem / 1024, bios_extmem / 1024);
245     if (initial_bootinfo != NULL) {
246         initial_bootinfo->bi_basemem = bios_basemem / 1024;
247         initial_bootinfo->bi_extmem = bios_extmem / 1024;
248     }
249
250     /* detect ACPI for future reference */
251     biosacpi_detect();
252
253     /* detect SMBIOS for future reference */
254     smbios_detect();
255
256     /* enable EHCI */
257     setenv("ehci_load", "YES", 1);
258
259     /* enable XHCI */
260     setenv("xhci_load", "YES", 1);
261
262     printf("\n");
263     printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
264     printf("(%s, %s)\n", bootprog_maker, bootprog_date);
265
266     extract_currdev();                          /* set $currdev and $loaddev */
267     setenv("LINES", "24", 1);                   /* optional */
268     
269     bios_getsmap();
270
271     archsw.arch_autoload = i386_autoload;
272     archsw.arch_getdev = i386_getdev;
273     archsw.arch_copyin = i386_copyin;
274     archsw.arch_copyout = i386_copyout;
275     archsw.arch_readin = i386_readin;
276     archsw.arch_isainb = isa_inb;
277     archsw.arch_isaoutb = isa_outb;
278
279     interact();                 /* doesn't return */
280
281     /* if we ever get here, it is an error */
282     return (1);
283 }
284
285 /*
286  * Set the 'current device' by (if possible) recovering the boot device as 
287  * supplied by the initial bootstrap.
288  *
289  * XXX should be extended for netbooting.
290  */
291 static void
292 extract_currdev(void)
293 {
294     struct i386_devdesc new_currdev;
295     int                 biosdev = -1;
296
297     /* Assume we are booting from a BIOS disk by default */
298     new_currdev.d_dev = &biosdisk;
299
300     /* new-style boot loaders such as pxeldr and cdldr */
301     if (kargs->bootinfo == 0) {
302         if ((kargs->bootflags & KARGS_FLAGS_CD) != 0) {
303             /* we are booting from a CD with cdboot */
304             new_currdev.d_dev = &bioscd;
305             new_currdev.d_kind.bioscd.unit = bc_bios2unit(initial_bootdev);
306         } else if ((kargs->bootflags & KARGS_FLAGS_PXE) != 0) {
307             /* we are booting from pxeldr */
308             new_currdev.d_dev = &pxedisk;
309             new_currdev.d_kind.netif.unit = 0;
310         } else {
311             /* we don't know what our boot device is */
312             new_currdev.d_kind.biosdisk.slice = -1;
313             new_currdev.d_kind.biosdisk.partition = 0;
314             biosdev = -1;
315         }
316     } else if ((initial_bootdev & B_MAGICMASK) != B_DEVMAGIC) {
317         /* The passed-in boot device is bad */
318         new_currdev.d_kind.biosdisk.slice = -1;
319         new_currdev.d_kind.biosdisk.partition = 0;
320         biosdev = -1;
321     } else {
322         new_currdev.d_kind.biosdisk.slice = (B_ADAPTOR(initial_bootdev) << 4) +
323                                              B_CONTROLLER(initial_bootdev) - 1;
324         new_currdev.d_kind.biosdisk.partition = B_PARTITION(initial_bootdev);
325         biosdev = initial_bootinfo->bi_bios_dev;
326
327         /*
328          * If we are booted by an old bootstrap, we have to guess at the BIOS
329          * unit number.  We will loose if there is more than one disk type
330          * and we are not booting from the lowest-numbered disk type 
331          * (ie. SCSI when IDE also exists).
332          */
333         if ((biosdev == 0) && (B_TYPE(initial_bootdev) != 2))   /* biosdev doesn't match major */
334             biosdev = 0x80 + B_UNIT(initial_bootdev);           /* assume harddisk */
335     }
336     new_currdev.d_type = new_currdev.d_dev->dv_type;
337     
338     /*
339      * If we are booting off of a BIOS disk and we didn't succeed in determining
340      * which one we booted off of, just use disk0: as a reasonable default.
341      */
342     if ((new_currdev.d_type == biosdisk.dv_type) &&
343         ((new_currdev.d_kind.biosdisk.unit = bd_bios2unit(biosdev)) == -1)) {
344         printf("Can't work out which disk we are booting from.\n"
345                "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev);
346         new_currdev.d_kind.biosdisk.unit = 0;
347     }
348     env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&new_currdev),
349                i386_setcurrdev, env_nounset);
350     env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&new_currdev), env_noset,
351                env_nounset);
352 }
353
354 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
355
356 static int
357 command_reboot(int argc, char *argv[])
358 {
359     int i;
360
361     for (i = 0; devsw[i] != NULL; ++i)
362         if (devsw[i]->dv_cleanup != NULL)
363             (devsw[i]->dv_cleanup)();
364
365     printf("Rebooting...\n");
366     delay(1000000);
367     __exit(0);
368 }
369
370 /* provide this for panic, as it's not in the startup code */
371 void
372 exit(int code)
373 {
374     __exit(code);
375 }
376
377 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
378
379 static int
380 command_heap(int argc, char *argv[])
381 {
382     char *base;
383     size_t bytes;
384
385     mallocstats();
386     base = getheap(&bytes);
387     printf("heap %p-%p (%d)\n", base, base + bytes, (int)bytes);
388     printf("stack at %p\n", &argc);
389     return(CMD_OK);
390 }
391
392 /* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
393 static int              
394 isa_inb(int port)
395 {
396     u_char      data;
397     
398     if (__builtin_constant_p(port) && 
399         (((port) & 0xffff) < 0x100) && 
400         ((port) < 0x10000)) {
401         __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port)));
402     } else {
403         __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
404     }
405     return(data);
406 }
407
408 static void
409 isa_outb(int port, int value)
410 {
411     u_char      al = value;
412     
413     if (__builtin_constant_p(port) && 
414         (((port) & 0xffff) < 0x100) && 
415         ((port) < 0x10000)) {
416         __asm __volatile("outb %0,%1" : : "a" (al), "id" ((u_short)(port)));
417     } else {
418         __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
419     }
420 }
421