2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
58 * $FreeBSD: src/sys/boot/i386/loader/main.c,v 1.28 2003/08/25 23:28:32 obrien Exp $
59 * $DragonFly: src/sys/boot/pc32/loader/main.c,v 1.9 2008/06/05 18:06:31 swildner Exp $
63 * MD bootstrap main() and assorted miscellaneous
69 #include <machine/bootinfo.h>
70 #include <sys/reboot.h>
72 #include "bootstrap.h"
73 #include "libi386/libi386.h"
76 #define KARGS_FLAGS_CD 0x1
77 #define KARGS_FLAGS_PXE 0x2
79 /* Arguments passed in from the boot1/boot2 loader */
90 static u_int32_t initial_howto
;
91 static u_int32_t initial_bootdev
;
92 static struct bootinfo
*initial_bootinfo
;
94 struct arch_switch archsw
; /* MI/MD interface boundary */
96 static void extract_currdev(void);
97 static int isa_inb(int port
);
98 static void isa_outb(int port
, int value
);
102 extern char bootprog_name
[], bootprog_rev
[], bootprog_date
[], bootprog_maker
[];
107 #define COMCONSOLE_DEBUG
108 #ifdef COMCONSOLE_DEBUG
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 */
130 #define WDEBUG_INIT()
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
;
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
);
155 * Initialize the heap as early as possible. Once this is done,
156 * malloc() is usable.
158 * Don't include our stack in the heap. If the stack is in low
159 * user memory use {end,bios_basemem}. If the stack is in high
160 * user memory but not extended memory then don't let the heap
161 * overlap the stack. If the stack is in extended memory limit
162 * the heap to bios_basemem.
164 * Be sure to use the virtual bios_basemem address rather then
165 * the physical bios_basemem address or we may overwrite BIOS
169 memend
= (char *)&memend
- 0x8000; /* space for stack */
170 memend
= (char *)((uintptr_t)memend
& ~(uintptr_t)(0x1000 - 1));
171 if (memend
< (char *)end
) {
172 setheap((void *)end
, PTOV(bios_basemem
));
174 if (memend
> (char *)PTOV(bios_basemem
))
175 memend
= (char *)PTOV(bios_basemem
);
176 setheap((void *)end
, memend
);
180 * XXX Chicken-and-egg problem; we want to have console output early,
181 * but some console attributes may depend on reading from eg. the boot
182 * device, which we can't do yet.
184 * We can use printf() etc. once this is done. The previous boot stage
185 * might have requested a video or serial preference, in which case we
186 * set it. If neither is specified or both are specified we leave the
187 * console environment variable alone, defaulting to dual boot.
189 if (initial_howto
& RB_MUTE
) {
190 setenv("console", "nullconsole", 1);
191 } else if ((initial_howto
& (RB_VIDEO
|RB_SERIAL
)) != (RB_VIDEO
|RB_SERIAL
)) {
192 if (initial_howto
& RB_VIDEO
)
193 setenv("console", "vidconsole", 1);
194 if (initial_howto
& RB_SERIAL
)
195 setenv("console", "comconsole", 1);
200 * Initialise the block cache
202 bcache_init(32, 512); /* 16k cache XXX tune this */
205 * Special handling for PXE and CD booting.
207 if (kargs
->bootinfo
== 0) {
209 * We only want the PXE disk to try to init itself in the below
210 * walk through devsw if we actually booted off of PXE.
212 if (kargs
->bootflags
& KARGS_FLAGS_PXE
)
213 pxe_enable(kargs
->pxeinfo
? PTOV(kargs
->pxeinfo
) : NULL
);
214 else if (kargs
->bootflags
& KARGS_FLAGS_CD
)
215 bc_add(initial_bootdev
);
219 * March through the device switch probing for things.
221 for (i
= 0; devsw
[i
] != NULL
; i
++) {
223 if (devsw
[i
]->dv_init
!= NULL
)
224 (devsw
[i
]->dv_init
)();
227 printf("BIOS %dkB/%dkB available memory\n",
228 bios_basemem
/ 1024, bios_extmem
/ 1024);
229 if (initial_bootinfo
!= NULL
) {
230 initial_bootinfo
->bi_basemem
= bios_basemem
/ 1024;
231 initial_bootinfo
->bi_extmem
= bios_extmem
/ 1024;
234 /* detect ACPI for future reference */
238 printf("%s, Revision %s\n", bootprog_name
, bootprog_rev
);
239 printf("(%s, %s)\n", bootprog_maker
, bootprog_date
);
241 extract_currdev(); /* set $currdev and $loaddev */
242 setenv("LINES", "24", 1); /* optional */
246 archsw
.arch_autoload
= i386_autoload
;
247 archsw
.arch_getdev
= i386_getdev
;
248 archsw
.arch_copyin
= i386_copyin
;
249 archsw
.arch_copyout
= i386_copyout
;
250 archsw
.arch_readin
= i386_readin
;
251 archsw
.arch_isainb
= isa_inb
;
252 archsw
.arch_isaoutb
= isa_outb
;
254 interact(); /* doesn't return */
256 /* if we ever get here, it is an error */
261 * Set the 'current device' by (if possible) recovering the boot device as
262 * supplied by the initial bootstrap.
264 * XXX should be extended for netbooting.
267 extract_currdev(void)
269 struct i386_devdesc new_currdev
;
270 int major
, biosdev
= -1;
272 /* Assume we are booting from a BIOS disk by default */
273 new_currdev
.d_dev
= &biosdisk
;
275 /* new-style boot loaders such as pxeldr and cdldr */
276 if (kargs
->bootinfo
== 0) {
277 if ((kargs
->bootflags
& KARGS_FLAGS_CD
) != 0) {
278 /* we are booting from a CD with cdboot */
279 new_currdev
.d_dev
= &bioscd
;
280 new_currdev
.d_kind
.bioscd
.unit
= bc_bios2unit(initial_bootdev
);
281 } else if ((kargs
->bootflags
& KARGS_FLAGS_PXE
) != 0) {
282 /* we are booting from pxeldr */
283 new_currdev
.d_dev
= &pxedisk
;
284 new_currdev
.d_kind
.netif
.unit
= 0;
286 /* we don't know what our boot device is */
287 new_currdev
.d_kind
.biosdisk
.slice
= -1;
288 new_currdev
.d_kind
.biosdisk
.partition
= 0;
291 } else if ((initial_bootdev
& B_MAGICMASK
) != B_DEVMAGIC
) {
292 /* The passed-in boot device is bad */
293 new_currdev
.d_kind
.biosdisk
.slice
= -1;
294 new_currdev
.d_kind
.biosdisk
.partition
= 0;
297 new_currdev
.d_kind
.biosdisk
.slice
= (B_ADAPTOR(initial_bootdev
) << 4) +
298 B_CONTROLLER(initial_bootdev
) - 1;
299 new_currdev
.d_kind
.biosdisk
.partition
= B_PARTITION(initial_bootdev
);
300 biosdev
= initial_bootinfo
->bi_bios_dev
;
301 major
= B_TYPE(initial_bootdev
);
304 * If we are booted by an old bootstrap, we have to guess at the BIOS
305 * unit number. We will loose if there is more than one disk type
306 * and we are not booting from the lowest-numbered disk type
307 * (ie. SCSI when IDE also exists).
309 if ((biosdev
== 0) && (B_TYPE(initial_bootdev
) != 2)) /* biosdev doesn't match major */
310 biosdev
= 0x80 + B_UNIT(initial_bootdev
); /* assume harddisk */
312 new_currdev
.d_type
= new_currdev
.d_dev
->dv_type
;
315 * If we are booting off of a BIOS disk and we didn't succeed in determining
316 * which one we booted off of, just use disk0: as a reasonable default.
318 if ((new_currdev
.d_type
== biosdisk
.dv_type
) &&
319 ((new_currdev
.d_kind
.biosdisk
.unit
= bd_bios2unit(biosdev
)) == -1)) {
320 printf("Can't work out which disk we are booting from.\n"
321 "Guessed BIOS device 0x%x not found by probes, defaulting to disk0:\n", biosdev
);
322 new_currdev
.d_kind
.biosdisk
.unit
= 0;
324 env_setenv("currdev", EV_VOLATILE
, i386_fmtdev(&new_currdev
),
325 i386_setcurrdev
, env_nounset
);
326 env_setenv("loaddev", EV_VOLATILE
, i386_fmtdev(&new_currdev
), env_noset
,
330 COMMAND_SET(reboot
, "reboot", "reboot the system", command_reboot
);
333 command_reboot(int argc
, char *argv
[])
337 for (i
= 0; devsw
[i
] != NULL
; ++i
)
338 if (devsw
[i
]->dv_cleanup
!= NULL
)
339 (devsw
[i
]->dv_cleanup
)();
341 printf("Rebooting...\n");
346 /* provide this for panic, as it's not in the startup code */
353 COMMAND_SET(heap
, "heap", "show heap usage", command_heap
);
356 command_heap(int argc
, char *argv
[])
359 printf("heap base at %p, top at %p\n", end
, sbrk(0));
363 /* ISA bus access functions for PnP, derived from <machine/cpufunc.h> */
369 if (__builtin_constant_p(port
) &&
370 (((port
) & 0xffff) < 0x100) &&
371 ((port
) < 0x10000)) {
372 __asm
__volatile("inb %1,%0" : "=a" (data
) : "id" ((u_short
)(port
)));
374 __asm
__volatile("inb %%dx,%0" : "=a" (data
) : "d" (port
));
380 isa_outb(int port
, int value
)
384 if (__builtin_constant_p(port
) &&
385 (((port
) & 0xffff) < 0x100) &&
386 ((port
) < 0x10000)) {
387 __asm
__volatile("outb %0,%1" : : "a" (al
), "id" ((u_short
)(port
)));
389 __asm
__volatile("outb %0,%%dx" : : "a" (al
), "d" (port
));