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
13 #include "disk.h" // cdrom_boot
15 // We need a copy of this string, but we are not actually a PnP BIOS,
16 // so make sure it is *not* aligned, so OSes will not see it if they
18 char pnp_string[] VISIBLE16 __attribute__((aligned (2))) = " $PnP";
20 //--------------------------------------------------------------------------
22 // displays the boot device
23 //--------------------------------------------------------------------------
25 static const char drivetypes[][10]={
26 "", "Floppy","Hard Disk","CD-Rom", "Network"
30 print_boot_device(u16 type)
32 /* NIC appears as type 0x80 */
33 if (type == IPL_TYPE_BEV)
35 if (type == 0 || type > 0x4)
36 BX_PANIC("Bad drive type\n");
37 printf("Booting from %s...\n", drivetypes[type]);
39 // XXX - latest cvs has BEV description
42 //--------------------------------------------------------------------------
44 // displays the reason why boot failed
45 //--------------------------------------------------------------------------
47 print_boot_failure(u16 type, u8 reason)
49 if (type == 0 || type > 0x3)
50 BX_PANIC("Bad drive type\n");
52 printf("Boot failed");
54 /* Report the reason too */
56 printf(": not a bootable disk");
58 printf(": could not read the boot disk");
68 SET_IPL(sequence, seq_nr);
74 if (CONFIG_CDROM_BOOT) {
75 bootdev = inb_cmos(CMOS_BIOS_BOOTFLAG2);
76 bootdev |= ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4);
77 bootdev >>= 4 * seq_nr;
80 BX_PANIC("No bootable device.\n");
82 /* Translate from CMOS runes to an IPL table offset by subtracting 1 */
86 BX_PANIC("No more boot devices.");
87 if (!!(inb_cmos(CMOS_BIOS_CONFIG) & 0x20) ^ (seq_nr == 1))
88 /* Boot from floppy if the bit is set or it's the second boot */
94 if (bootdev >= GET_IPL(count)) {
95 BX_INFO("Invalid boot device (0x%x)\n", bootdev);
98 u16 type = GET_IPL(table[bootdev].type);
100 /* Do the loading, and set up vector as a far pointer to the boot
101 * address, and bootdrv as the boot drive */
102 print_boot_device(type);
106 case IPL_TYPE_FLOPPY: /* FDD */
107 case IPL_TYPE_HARDDISK: /* HDD */
109 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
113 memset(&cr, 0, sizeof(cr));
119 call16_int(0x13, &cr);
121 if (cr.flags & F_CF) {
122 print_boot_failure(type, 1);
126 /* Always check the signature on a HDD boot sector; on FDD,
127 * only do the check if the CMOS doesn't tell us to skip it */
128 if ((type != IPL_TYPE_FLOPPY)
129 || !((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0x01))) {
130 if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
131 print_boot_failure(type, 0);
136 /* Canonicalize bootseg:bootip */
137 bootip = (bootseg & 0x0fff) << 4;
140 case IPL_TYPE_CDROM: {
142 if (! CONFIG_CDROM_BOOT)
144 u16 status = cdrom_boot();
146 printf("CDROM boot failure code : %04x\n", status);
147 print_boot_failure(type, 1);
151 bootdrv = GET_EBDA(cdemu.emulated_drive);
152 bootseg = GET_EBDA(cdemu.load_segment);
153 /* Canonicalize bootseg:bootip */
154 bootip = (bootseg & 0x0fff) << 4;
159 /* Expansion ROM with a Bootstrap Entry Vector (a far
161 u32 vector = GET_IPL(table[bootdev].vector);
162 bootseg = vector >> 16;
163 bootip = vector & 0xffff;
171 BX_INFO("Booting from %x:%x\n", bootseg, bootip);
173 memset(&cr, 0, sizeof(cr));
176 // Set the magic number in ax and the boot drive in dl.
187 // Boot failed: invoke the boot recovery function
189 memset(&br, 0, sizeof(br));
190 call16_int(0x18, &br);
193 // Boot Failure recovery: try the next device.
198 u16 seq = GET_IPL(sequence) + 1;
202 // INT 19h Boot Load Service Entry Point