blob: 5bcb4a412ae3878874c66d0bd79410cb3f932dc2 [file] [log] [blame]
Kevin O'Connorf13b0082008-08-17 11:26:42 -04001// Code to load disk image and start system boot.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05002//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
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'Connor180a9592008-03-04 22:50:53 -050011#include "disk.h" // cdrom_boot
Kevin O'Connor9521e262008-07-04 13:04:29 -040012#include "bregs.h" // struct bregs
Kevin O'Connorc659fde2008-12-28 23:43:20 -050013#include "boot.h" // struct ipl_s
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050014#include "cmos.h" // inb_cmos
Kevin O'Connorc659fde2008-12-28 23:43:20 -050015
16struct ipl_s IPL;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050017
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050018
19/****************************************************************
20 * IPL handlers
21 ****************************************************************/
22
23void
24boot_setup()
25{
26 if (! CONFIG_BOOT)
27 return;
28 dprintf(3, "init boot device ordering\n");
29
30 memset(&IPL, 0, sizeof(IPL));
31
32 // Floppy drive
33 struct ipl_entry_s *ip = &IPL.table[0];
34 ip->type = IPL_TYPE_FLOPPY;
35 ip++;
36
37 // First HDD
38 ip->type = IPL_TYPE_HARDDISK;
39 ip++;
40
41 // CDROM
42 if (CONFIG_CDROM_BOOT) {
43 ip->type = IPL_TYPE_CDROM;
44 ip++;
45 }
46
47 IPL.count = ip - IPL.table;
48 SET_EBDA(boot_sequence, 0xffff);
49 if (CONFIG_COREBOOT) {
50 // XXX - hardcode defaults for coreboot.
51 IPL.bootorder = 0x00000231;
52 IPL.checkfloppysig = 1;
53 } else {
54 // On emulators, get boot order from nvram.
55 IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
56 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
57 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
58 IPL.checkfloppysig = 1;
59 }
60}
61
62// Add a BEV vector for a given pnp compatible option rom.
63void
64add_bev(u16 seg, u16 bev, u16 desc)
65{
66 // Found a device that thinks it can boot the system. Record
67 // its BEV and product name string.
68
69 if (! CONFIG_BOOT)
70 return;
71
72 if (IPL.count >= ARRAY_SIZE(IPL.table))
73 return;
74
75 struct ipl_entry_s *ip = &IPL.table[IPL.count];
76 ip->type = IPL_TYPE_BEV;
77 ip->vector = (seg << 16) | bev;
78 if (desc)
79 ip->description = MAKE_FLATPTR(seg, desc);
80
81 IPL.count++;
82}
83
84
85/****************************************************************
86 * Printing helpers
87 ****************************************************************/
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050088
89static const char drivetypes[][10]={
Kevin O'Connorabc75972008-05-18 00:20:53 -040090 "", "Floppy", "Hard Disk", "CD-Rom", "Network"
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050091};
92
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050093// display a device name
94static void
Kevin O'Connore0113c92008-04-05 15:51:12 -040095printf_bootdev(u16 bootdev)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050096{
Kevin O'Connorc659fde2008-12-28 23:43:20 -050097 u16 type = IPL.table[bootdev].type;
Kevin O'Connore0113c92008-04-05 15:51:12 -040098
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050099 /* NIC appears as type 0x80 */
100 if (type == IPL_TYPE_BEV)
101 type = 0x4;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500102 if (type == 0 || type > 0x4) {
103 printf("Unknown");
104 return;
105 }
Kevin O'Connore0113c92008-04-05 15:51:12 -0400106 printf("%s", drivetypes[type]);
Kevin O'Connora2e73802008-02-27 10:27:00 -0500107
Kevin O'Connore0113c92008-04-05 15:51:12 -0400108 /* print product string if BEV */
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500109 char *description_fl = IPL.table[bootdev].description;
110 if (type == 4 && description_fl != 0) {
Kevin O'Connore0113c92008-04-05 15:51:12 -0400111 char description[33];
112 /* first 32 bytes are significant */
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500113 memcpy(description, description_fl, 32);
Kevin O'Connore0113c92008-04-05 15:51:12 -0400114 /* terminate string */
115 description[32] = 0;
116 printf(" [%.s]", description);
117 }
118}
119
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500120// display the boot device
Kevin O'Connore0113c92008-04-05 15:51:12 -0400121static void
122print_boot_device(u16 bootdev)
123{
124 printf("Booting from ");
125 printf_bootdev(bootdev);
126 printf("...\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500127}
128
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500129// display the reason why a boot failed
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500130static void
131print_boot_failure(u16 type, u8 reason)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500132{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500133 printf("Boot failed");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500134 if (type < 4) {
135 /* Report the reason too */
136 if (reason==0)
137 printf(": not a bootable disk");
138 else
139 printf(": could not read the boot disk");
140 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500141 printf("\n\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500142}
143
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500144
145/****************************************************************
146 * Boot menu
147 ****************************************************************/
148
149void
150interactive_bootmenu()
151{
152 if (! CONFIG_BOOTMENU)
153 return;
154
155 while (get_keystroke(0) >= 0)
156 ;
157
158 printf("Press F12 for boot menu.\n\n");
159
160 int scan_code = get_keystroke(2500);
161 if (scan_code != 0x86)
162 /* not F12 */
163 return;
164
165 while (get_keystroke(0) >= 0)
166 ;
167
168 printf("Select boot device:\n\n");
169
170 int count = IPL.count;
171 int i;
172 for (i = 0; i < count; i++) {
173 printf("%d. ", i+1);
174 printf_bootdev(i);
175 printf("\n");
176 }
177
178 for (;;) {
179 scan_code = get_keystroke(1000);
180 if (scan_code == 0x01)
181 // ESC
182 break;
183 if (scan_code >= 0 && scan_code <= count + 1) {
184 // Add user choice to the boot order.
185 u16 choice = scan_code - 1;
186 u32 bootorder = IPL.bootorder;
187 IPL.bootorder = (bootorder << 4) | choice;
188 break;
189 }
190 }
191 printf("\n");
192}
193
194
195/****************************************************************
196 * Boot code (int 18/19)
197 ****************************************************************/
198
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500199static void
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500200try_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500201{
Kevin O'Connor40967022008-07-21 22:23:05 -0400202 if (! CONFIG_BOOT)
203 BX_PANIC("Boot support not compiled in.\n");
204
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500205 u32 bootdev = IPL.bootorder;
Kevin O'Connor840c5342008-03-29 14:04:34 -0400206 bootdev >>= 4 * seq_nr;
207 bootdev &= 0xf;
Kevin O'Connore0113c92008-04-05 15:51:12 -0400208
Kevin O'Connor840c5342008-03-29 14:04:34 -0400209 if (bootdev == 0)
210 BX_PANIC("No bootable device.\n");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500211
Kevin O'Connorabc75972008-05-18 00:20:53 -0400212 /* Translate bootdev to an IPL table offset by subtracting 1 */
Kevin O'Connor840c5342008-03-29 14:04:34 -0400213 bootdev -= 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500214
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500215 if (bootdev >= IPL.count) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400216 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500217 return;
218 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500219
220 /* Do the loading, and set up vector as a far pointer to the boot
221 * address, and bootdrv as the boot drive */
Kevin O'Connore0113c92008-04-05 15:51:12 -0400222 print_boot_device(bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500223
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500224 u16 type = IPL.table[bootdev].type;
Kevin O'Connore0113c92008-04-05 15:51:12 -0400225
226 u16 bootseg, bootip;
227 u8 bootdrv = 0;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500228 struct bregs cr;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500229 switch(type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400230 case IPL_TYPE_FLOPPY:
231 case IPL_TYPE_HARDDISK:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500232
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500233 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
234 bootseg = 0x07c0;
235
236 // Read sector
237 memset(&cr, 0, sizeof(cr));
238 cr.dl = bootdrv;
239 cr.es = bootseg;
240 cr.ah = 2;
241 cr.al = 1;
242 cr.cl = 1;
243 call16_int(0x13, &cr);
244
245 if (cr.flags & F_CF) {
246 print_boot_failure(type, 1);
247 return;
248 }
249
250 /* Always check the signature on a HDD boot sector; on FDD,
Kevin O'Connorabc75972008-05-18 00:20:53 -0400251 * only do the check if configured for it */
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500252 if (type != IPL_TYPE_FLOPPY || IPL.checkfloppysig) {
Kevin O'Connor95827c42009-02-07 00:04:57 -0500253 struct mbr_s *mbr = (void*)0;
254 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500255 print_boot_failure(type, 0);
256 return;
257 }
258 }
259
260 /* Canonicalize bootseg:bootip */
261 bootip = (bootseg & 0x0fff) << 4;
262 bootseg &= 0xf000;
263 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500264 case IPL_TYPE_CDROM: {
265 /* CD-ROM */
266 if (! CONFIG_CDROM_BOOT)
Kevin O'Connord19501e2008-12-10 20:52:09 -0500267 return;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400268 int status = cdrom_boot();
Kevin O'Connor180a9592008-03-04 22:50:53 -0500269 if (status) {
270 printf("CDROM boot failure code : %04x\n", status);
271 print_boot_failure(type, 1);
272 return;
273 }
274
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500275 u16 ebda_seg = get_ebda_seg();
276 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
277 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500278 /* Canonicalize bootseg:bootip */
279 bootip = (bootseg & 0x0fff) << 4;
280 bootseg &= 0xf000;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500281 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500282 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500283 case IPL_TYPE_BEV: {
284 /* Expansion ROM with a Bootstrap Entry Vector (a far
285 * pointer) */
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500286 u32 vector = IPL.table[bootdev].vector;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500287 bootseg = vector >> 16;
288 bootip = vector & 0xffff;
289 break;
290 }
291 default:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500292 return;
293 }
294
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500295 /* Debugging info */
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400296 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500297
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500298 memset(&cr, 0, sizeof(cr));
299 cr.ip = bootip;
300 cr.cs = bootseg;
301 // Set the magic number in ax and the boot drive in dl.
302 cr.dl = bootdrv;
303 cr.ax = 0xaa55;
304 call16(&cr);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400305}
306
307static void
308do_boot(u16 seq_nr)
309{
310 try_boot(seq_nr);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500311
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500312 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400313 struct bregs br;
314 memset(&br, 0, sizeof(br));
315 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500316}
317
318// Boot Failure recovery: try the next device.
Kevin O'Connor44eeaf12008-07-06 10:14:49 -0400319void VISIBLE32
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500320handle_18()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500321{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400322 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400323 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500324 u16 ebda_seg = get_ebda_seg();
325 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
326 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400327 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500328}
329
330// INT 19h Boot Load Service Entry Point
Kevin O'Connor44eeaf12008-07-06 10:14:49 -0400331void VISIBLE32
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500332handle_19()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500333{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400334 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400335 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500336 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400337 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500338}
Kevin O'Connorcdd3d652008-07-12 14:22:14 -0400339
340// Ughh - some older gcc compilers have a bug which causes VISIBLE32
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400341// functions to not be exported as global variables.
Kevin O'Connorcdd3d652008-07-12 14:22:14 -0400342asm(".global handle_18, handle_19");