blob: c75bc1b24804d3de7410f292f408760f500d89b7 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 16bit code to load disk image and start system boot.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008#include "util.h" // irq_enable
9#include "biosvar.h" // struct bregs
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050010#include "config.h" // CONFIG_*
11#include "cmos.h" // inb_cmos
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050012#include "ata.h" // ata_detect
Kevin O'Connor180a9592008-03-04 22:50:53 -050013#include "disk.h" // cdrom_boot
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050014
Kevin O'Connor44c631d2008-03-02 11:24:36 -050015// 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
17// scan.
Kevin O'Connor19786762008-03-05 21:09:59 -050018char pnp_string[] VISIBLE16 __attribute__((aligned (2))) = " $PnP";
Kevin O'Connor44c631d2008-03-02 11:24:36 -050019
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050020//--------------------------------------------------------------------------
21// print_boot_device
22// displays the boot device
23//--------------------------------------------------------------------------
24
25static const char drivetypes[][10]={
26 "", "Floppy","Hard Disk","CD-Rom", "Network"
27};
28
29static void
30print_boot_device(u16 type)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050031{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050032 /* NIC appears as type 0x80 */
33 if (type == IPL_TYPE_BEV)
34 type = 0x4;
35 if (type == 0 || type > 0x4)
36 BX_PANIC("Bad drive type\n");
37 printf("Booting from %s...\n", drivetypes[type]);
Kevin O'Connora2e73802008-02-27 10:27:00 -050038
39 // XXX - latest cvs has BEV description
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050040}
41
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050042//--------------------------------------------------------------------------
43// print_boot_failure
44// displays the reason why boot failed
45//--------------------------------------------------------------------------
46static void
47print_boot_failure(u16 type, u8 reason)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050048{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050049 if (type == 0 || type > 0x3)
50 BX_PANIC("Bad drive type\n");
51
Kevin O'Connora2e73802008-02-27 10:27:00 -050052 printf("Boot failed");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050053 if (type < 4) {
54 /* Report the reason too */
55 if (reason==0)
56 printf(": not a bootable disk");
57 else
58 printf(": could not read the boot disk");
59 }
Kevin O'Connora2e73802008-02-27 10:27:00 -050060 printf("\n\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050061}
62
63static void
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050064try_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050065{
Kevin O'Connor7a558e42008-03-11 20:38:33 -040066 irq_enable();
67
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050068 SET_IPL(sequence, seq_nr);
Kevin O'Connor7a558e42008-03-11 20:38:33 -040069
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050070 u16 bootseg;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050071 u8 bootdrv = 0;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050072 u16 bootdev, bootip;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050073
Kevin O'Connor180a9592008-03-04 22:50:53 -050074 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050075 bootdev = inb_cmos(CMOS_BIOS_BOOTFLAG2);
76 bootdev |= ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4);
77 bootdev >>= 4 * seq_nr;
78 bootdev &= 0xf;
79 if (bootdev == 0)
80 BX_PANIC("No bootable device.\n");
81
82 /* Translate from CMOS runes to an IPL table offset by subtracting 1 */
83 bootdev -= 1;
84 } else {
85 if (seq_nr ==2)
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 */
89 bootdev = 0x00;
90 else
91 bootdev = 0x01;
92 }
93
94 if (bootdev >= GET_IPL(count)) {
95 BX_INFO("Invalid boot device (0x%x)\n", bootdev);
96 return;
97 }
98 u16 type = GET_IPL(table[bootdev].type);
99
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);
103
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500104 struct bregs cr;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500105 switch(type) {
106 case IPL_TYPE_FLOPPY: /* FDD */
107 case IPL_TYPE_HARDDISK: /* HDD */
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500108
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500109 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
110 bootseg = 0x07c0;
111
112 // Read sector
113 memset(&cr, 0, sizeof(cr));
114 cr.dl = bootdrv;
115 cr.es = bootseg;
116 cr.ah = 2;
117 cr.al = 1;
118 cr.cl = 1;
119 call16_int(0x13, &cr);
120
121 if (cr.flags & F_CF) {
122 print_boot_failure(type, 1);
123 return;
124 }
125
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);
132 return;
133 }
134 }
135
136 /* Canonicalize bootseg:bootip */
137 bootip = (bootseg & 0x0fff) << 4;
138 bootseg &= 0xf000;
139 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500140 case IPL_TYPE_CDROM: {
141 /* CD-ROM */
142 if (! CONFIG_CDROM_BOOT)
143 break;
144 u16 status = cdrom_boot();
145 if (status) {
146 printf("CDROM boot failure code : %04x\n", status);
147 print_boot_failure(type, 1);
148 return;
149 }
150
151 bootdrv = GET_EBDA(cdemu.emulated_drive);
152 bootseg = GET_EBDA(cdemu.load_segment);
153 /* Canonicalize bootseg:bootip */
154 bootip = (bootseg & 0x0fff) << 4;
155 bootseg &= 0xf000;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500156 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500157 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500158 case IPL_TYPE_BEV: {
159 /* Expansion ROM with a Bootstrap Entry Vector (a far
160 * pointer) */
161 u32 vector = GET_IPL(table[bootdev].vector);
162 bootseg = vector >> 16;
163 bootip = vector & 0xffff;
164 break;
165 }
166 default:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500167 return;
168 }
169
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500170 /* Debugging info */
171 BX_INFO("Booting from %x:%x\n", bootseg, bootip);
172
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500173 memset(&cr, 0, sizeof(cr));
174 cr.ip = bootip;
175 cr.cs = bootseg;
176 // Set the magic number in ax and the boot drive in dl.
177 cr.dl = bootdrv;
178 cr.ax = 0xaa55;
179 call16(&cr);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400180}
181
182static void
183do_boot(u16 seq_nr)
184{
185 try_boot(seq_nr);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500186
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500187 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400188 struct bregs br;
189 memset(&br, 0, sizeof(br));
190 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500191}
192
193// Boot Failure recovery: try the next device.
Kevin O'Connor19786762008-03-05 21:09:59 -0500194void VISIBLE16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500195handle_18()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500196{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500197 debug_enter(NULL);
198 u16 seq = GET_IPL(sequence) + 1;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400199 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500200}
201
202// INT 19h Boot Load Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500203void VISIBLE16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500204handle_19()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500205{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500206 debug_enter(NULL);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400207 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500208}
209
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500210// Called from 32bit code - start boot process
Kevin O'Connor19786762008-03-05 21:09:59 -0500211void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500212begin_boot()
213{
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500214 if (CONFIG_ATA)
215 ata_detect();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500216 struct bregs br;
217 memset(&br, 0, sizeof(br));
218 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500219}