1 // 16bit code to load disk image and start system boot.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "util.h" // irq_enable
9 #include "biosvar.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "cmos.h" // inb_cmos
12 #include "ata.h" // ata_detect
14 // We need a copy of this string, but we are not actually a PnP BIOS,
15 // so make sure it is *not* aligned, so OSes will not see it if they
17 char pnp_string[] VISIBLE __attribute__((aligned (2))) = " $PnP";
19 //--------------------------------------------------------------------------
21 // displays the boot device
22 //--------------------------------------------------------------------------
24 static const char drivetypes[][10]={
25 "", "Floppy","Hard Disk","CD-Rom", "Network"
29 print_boot_device(u16 type)
31 /* NIC appears as type 0x80 */
32 if (type == IPL_TYPE_BEV)
34 if (type == 0 || type > 0x4)
35 BX_PANIC("Bad drive type\n");
36 printf("Booting from %s...\n", drivetypes[type]);
38 // XXX - latest cvs has BEV description
41 //--------------------------------------------------------------------------
43 // displays the reason why boot failed
44 //--------------------------------------------------------------------------
46 print_boot_failure(u16 type, u8 reason)
48 if (type == 0 || type > 0x3)
49 BX_PANIC("Bad drive type\n");
51 printf("Boot failed");
53 /* Report the reason too */
55 printf(": not a bootable disk");
57 printf(": could not read the boot disk");
65 SET_IPL(sequence, seq_nr);
70 if (CONFIG_ELTORITO_BOOT) {
71 bootdev = inb_cmos(CMOS_BIOS_BOOTFLAG2);
72 bootdev |= ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4);
73 bootdev >>= 4 * seq_nr;
76 BX_PANIC("No bootable device.\n");
78 /* Translate from CMOS runes to an IPL table offset by subtracting 1 */
82 BX_PANIC("No more boot devices.");
83 if (!!(inb_cmos(CMOS_BIOS_CONFIG) & 0x20) ^ (seq_nr == 1))
84 /* Boot from floppy if the bit is set or it's the second boot */
90 if (bootdev >= GET_IPL(count)) {
91 BX_INFO("Invalid boot device (0x%x)\n", bootdev);
94 u16 type = GET_IPL(table[bootdev].type);
96 /* Do the loading, and set up vector as a far pointer to the boot
97 * address, and bootdrv as the boot drive */
98 print_boot_device(type);
102 case IPL_TYPE_FLOPPY: /* FDD */
103 case IPL_TYPE_HARDDISK: /* HDD */
105 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
109 memset(&cr, 0, sizeof(cr));
115 call16_int(0x13, &cr);
117 if (cr.flags & F_CF) {
118 print_boot_failure(type, 1);
122 /* Always check the signature on a HDD boot sector; on FDD,
123 * only do the check if the CMOS doesn't tell us to skip it */
124 if ((type != IPL_TYPE_FLOPPY)
125 || !((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0x01))) {
126 if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
127 print_boot_failure(type, 0);
132 /* Canonicalize bootseg:bootip */
133 bootip = (bootseg & 0x0fff) << 4;
136 case IPL_TYPE_CDROM: /* CD-ROM */
141 /* Expansion ROM with a Bootstrap Entry Vector (a far
143 u32 vector = GET_IPL(table[bootdev].vector);
144 bootseg = vector >> 16;
145 bootip = vector & 0xffff;
152 memset(&cr, 0, sizeof(cr));
155 // Set the magic number in ax and the boot drive in dl.
160 // Boot failed: invoke the boot recovery function
161 memset(&cr, 0, sizeof(cr));
162 call16_int(0x18, &cr);
165 // Boot Failure recovery: try the next device.
170 u16 seq = GET_IPL(sequence) + 1;
174 // INT 19h Boot Load Service Entry Point
182 // Called from 32bit code - start boot process
189 memset(&br, 0, sizeof(br));
190 call16_int(0x19, &br);