blob: 850fb2554c42ebcfbe36ee9348d026eed657135e [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
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "biosvar.h" // GET_EBDA
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050010#include "config.h" // CONFIG_*
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050011#include "ata.h" // ata_detect
Kevin O'Connor180a9592008-03-04 22:50:53 -050012#include "disk.h" // cdrom_boot
Kevin O'Connor9521e262008-07-04 13:04:29 -040013#include "bregs.h" // struct bregs
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050014
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050015//--------------------------------------------------------------------------
16// print_boot_device
17// displays the boot device
18//--------------------------------------------------------------------------
19
20static const char drivetypes[][10]={
Kevin O'Connorabc75972008-05-18 00:20:53 -040021 "", "Floppy", "Hard Disk", "CD-Rom", "Network"
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050022};
23
Kevin O'Connore0113c92008-04-05 15:51:12 -040024void
25printf_bootdev(u16 bootdev)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050026{
Kevin O'Connore0113c92008-04-05 15:51:12 -040027 u16 type = GET_EBDA(ipl.table[bootdev].type);
28
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050029 /* NIC appears as type 0x80 */
30 if (type == IPL_TYPE_BEV)
31 type = 0x4;
32 if (type == 0 || type > 0x4)
33 BX_PANIC("Bad drive type\n");
Kevin O'Connore0113c92008-04-05 15:51:12 -040034 printf("%s", drivetypes[type]);
Kevin O'Connora2e73802008-02-27 10:27:00 -050035
Kevin O'Connore0113c92008-04-05 15:51:12 -040036 /* print product string if BEV */
37 void *far_description = (void*)GET_EBDA(ipl.table[bootdev].description);
38 if (type == 4 && far_description != 0) {
39 char description[33];
40 /* first 32 bytes are significant */
41 memcpy(MAKE_FARPTR(GET_SEG(SS), &description), far_description, 32);
42 /* terminate string */
43 description[32] = 0;
44 printf(" [%.s]", description);
45 }
46}
47
48static void
49print_boot_device(u16 bootdev)
50{
51 printf("Booting from ");
52 printf_bootdev(bootdev);
53 printf("...\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050054}
55
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050056//--------------------------------------------------------------------------
57// print_boot_failure
58// displays the reason why boot failed
59//--------------------------------------------------------------------------
60static void
61print_boot_failure(u16 type, u8 reason)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050062{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050063 if (type == 0 || type > 0x3)
64 BX_PANIC("Bad drive type\n");
65
Kevin O'Connora2e73802008-02-27 10:27:00 -050066 printf("Boot failed");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050067 if (type < 4) {
68 /* Report the reason too */
69 if (reason==0)
70 printf(": not a bootable disk");
71 else
72 printf(": could not read the boot disk");
73 }
Kevin O'Connora2e73802008-02-27 10:27:00 -050074 printf("\n\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050075}
76
77static void
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050078try_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050079{
Kevin O'Connor7a558e42008-03-11 20:38:33 -040080 irq_enable();
81
Kevin O'Connor56a506d2008-03-29 13:15:36 -040082 SET_EBDA(ipl.sequence, seq_nr);
Kevin O'Connor7a558e42008-03-11 20:38:33 -040083
Kevin O'Connorabc75972008-05-18 00:20:53 -040084 u32 bootdev = GET_EBDA(ipl.bootorder);
Kevin O'Connor840c5342008-03-29 14:04:34 -040085 bootdev >>= 4 * seq_nr;
86 bootdev &= 0xf;
Kevin O'Connore0113c92008-04-05 15:51:12 -040087
Kevin O'Connor840c5342008-03-29 14:04:34 -040088 if (bootdev == 0)
89 BX_PANIC("No bootable device.\n");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050090
Kevin O'Connorabc75972008-05-18 00:20:53 -040091 /* Translate bootdev to an IPL table offset by subtracting 1 */
Kevin O'Connor840c5342008-03-29 14:04:34 -040092 bootdev -= 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050093
Kevin O'Connor56a506d2008-03-29 13:15:36 -040094 if (bootdev >= GET_EBDA(ipl.count)) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040095 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050096 return;
97 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050098
99 /* Do the loading, and set up vector as a far pointer to the boot
100 * address, and bootdrv as the boot drive */
Kevin O'Connore0113c92008-04-05 15:51:12 -0400101 print_boot_device(bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500102
Kevin O'Connore0113c92008-04-05 15:51:12 -0400103 u16 type = GET_EBDA(ipl.table[bootdev].type);
104
105 u16 bootseg, bootip;
106 u8 bootdrv = 0;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500107 struct bregs cr;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500108 switch(type) {
109 case IPL_TYPE_FLOPPY: /* FDD */
110 case IPL_TYPE_HARDDISK: /* HDD */
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500111
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500112 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
113 bootseg = 0x07c0;
114
115 // Read sector
116 memset(&cr, 0, sizeof(cr));
117 cr.dl = bootdrv;
118 cr.es = bootseg;
119 cr.ah = 2;
120 cr.al = 1;
121 cr.cl = 1;
122 call16_int(0x13, &cr);
123
124 if (cr.flags & F_CF) {
125 print_boot_failure(type, 1);
126 return;
127 }
128
129 /* Always check the signature on a HDD boot sector; on FDD,
Kevin O'Connorabc75972008-05-18 00:20:53 -0400130 * only do the check if configured for it */
131 if (type != IPL_TYPE_FLOPPY || GET_EBDA(ipl.checkfloppysig)) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500132 if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
133 print_boot_failure(type, 0);
134 return;
135 }
136 }
137
138 /* Canonicalize bootseg:bootip */
139 bootip = (bootseg & 0x0fff) << 4;
140 bootseg &= 0xf000;
141 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500142 case IPL_TYPE_CDROM: {
143 /* CD-ROM */
144 if (! CONFIG_CDROM_BOOT)
145 break;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400146 int status = cdrom_boot();
Kevin O'Connor180a9592008-03-04 22:50:53 -0500147 if (status) {
148 printf("CDROM boot failure code : %04x\n", status);
149 print_boot_failure(type, 1);
150 return;
151 }
152
153 bootdrv = GET_EBDA(cdemu.emulated_drive);
154 bootseg = GET_EBDA(cdemu.load_segment);
155 /* Canonicalize bootseg:bootip */
156 bootip = (bootseg & 0x0fff) << 4;
157 bootseg &= 0xf000;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500158 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500159 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500160 case IPL_TYPE_BEV: {
161 /* Expansion ROM with a Bootstrap Entry Vector (a far
162 * pointer) */
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400163 u32 vector = GET_EBDA(ipl.table[bootdev].vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500164 bootseg = vector >> 16;
165 bootip = vector & 0xffff;
166 break;
167 }
168 default:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500169 return;
170 }
171
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500172 /* Debugging info */
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400173 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500174
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500175 memset(&cr, 0, sizeof(cr));
176 cr.ip = bootip;
177 cr.cs = bootseg;
178 // Set the magic number in ax and the boot drive in dl.
179 cr.dl = bootdrv;
180 cr.ax = 0xaa55;
181 call16(&cr);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400182}
183
184static void
185do_boot(u16 seq_nr)
186{
187 try_boot(seq_nr);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500188
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500189 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400190 struct bregs br;
191 memset(&br, 0, sizeof(br));
192 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500193}
194
195// Boot Failure recovery: try the next device.
Kevin O'Connor19786762008-03-05 21:09:59 -0500196void VISIBLE16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500197handle_18()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500198{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400199 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400200 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400201 u16 seq = GET_EBDA(ipl.sequence) + 1;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400202 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500203}
204
205// INT 19h Boot Load Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500206void VISIBLE16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500207handle_19()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500208{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400209 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400210 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400211 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500212}