blob: 142544bea311049735d6cc54c4b0ee4c0f7a2281 [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
14
15struct ipl_s IPL;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050016
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050017//--------------------------------------------------------------------------
18// print_boot_device
19// displays the boot device
20//--------------------------------------------------------------------------
21
22static const char drivetypes[][10]={
Kevin O'Connorabc75972008-05-18 00:20:53 -040023 "", "Floppy", "Hard Disk", "CD-Rom", "Network"
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050024};
25
Kevin O'Connore0113c92008-04-05 15:51:12 -040026void
27printf_bootdev(u16 bootdev)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050028{
Kevin O'Connorc659fde2008-12-28 23:43:20 -050029 u16 type = IPL.table[bootdev].type;
Kevin O'Connore0113c92008-04-05 15:51:12 -040030
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050031 /* NIC appears as type 0x80 */
32 if (type == IPL_TYPE_BEV)
33 type = 0x4;
34 if (type == 0 || type > 0x4)
35 BX_PANIC("Bad drive type\n");
Kevin O'Connore0113c92008-04-05 15:51:12 -040036 printf("%s", drivetypes[type]);
Kevin O'Connora2e73802008-02-27 10:27:00 -050037
Kevin O'Connore0113c92008-04-05 15:51:12 -040038 /* print product string if BEV */
Kevin O'Connor35ae7262009-01-19 15:44:44 -050039 char *description_fl = IPL.table[bootdev].description;
40 if (type == 4 && description_fl != 0) {
Kevin O'Connore0113c92008-04-05 15:51:12 -040041 char description[33];
42 /* first 32 bytes are significant */
Kevin O'Connor35ae7262009-01-19 15:44:44 -050043 memcpy_fl(MAKE_FLATPTR(GET_SEG(SS), description), description_fl, 32);
Kevin O'Connore0113c92008-04-05 15:51:12 -040044 /* terminate string */
45 description[32] = 0;
46 printf(" [%.s]", description);
47 }
48}
49
50static void
51print_boot_device(u16 bootdev)
52{
53 printf("Booting from ");
54 printf_bootdev(bootdev);
55 printf("...\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050056}
57
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050058//--------------------------------------------------------------------------
59// print_boot_failure
60// displays the reason why boot failed
61//--------------------------------------------------------------------------
62static void
63print_boot_failure(u16 type, u8 reason)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050064{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050065 if (type == 0 || type > 0x3)
66 BX_PANIC("Bad drive type\n");
67
Kevin O'Connora2e73802008-02-27 10:27:00 -050068 printf("Boot failed");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050069 if (type < 4) {
70 /* Report the reason too */
71 if (reason==0)
72 printf(": not a bootable disk");
73 else
74 printf(": could not read the boot disk");
75 }
Kevin O'Connora2e73802008-02-27 10:27:00 -050076 printf("\n\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050077}
78
79static void
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050080try_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081{
Kevin O'Connor40967022008-07-21 22:23:05 -040082 if (! CONFIG_BOOT)
83 BX_PANIC("Boot support not compiled in.\n");
84
Kevin O'Connorc659fde2008-12-28 23:43:20 -050085 u32 bootdev = IPL.bootorder;
Kevin O'Connor840c5342008-03-29 14:04:34 -040086 bootdev >>= 4 * seq_nr;
87 bootdev &= 0xf;
Kevin O'Connore0113c92008-04-05 15:51:12 -040088
Kevin O'Connor840c5342008-03-29 14:04:34 -040089 if (bootdev == 0)
90 BX_PANIC("No bootable device.\n");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050091
Kevin O'Connorabc75972008-05-18 00:20:53 -040092 /* Translate bootdev to an IPL table offset by subtracting 1 */
Kevin O'Connor840c5342008-03-29 14:04:34 -040093 bootdev -= 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050094
Kevin O'Connorc659fde2008-12-28 23:43:20 -050095 if (bootdev >= IPL.count) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040096 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050097 return;
98 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050099
100 /* Do the loading, and set up vector as a far pointer to the boot
101 * address, and bootdrv as the boot drive */
Kevin O'Connore0113c92008-04-05 15:51:12 -0400102 print_boot_device(bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500103
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500104 u16 type = IPL.table[bootdev].type;
Kevin O'Connore0113c92008-04-05 15:51:12 -0400105
106 u16 bootseg, bootip;
107 u8 bootdrv = 0;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500108 struct bregs cr;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500109 switch(type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400110 case IPL_TYPE_FLOPPY:
111 case IPL_TYPE_HARDDISK:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500112
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500113 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
114 bootseg = 0x07c0;
115
116 // Read sector
117 memset(&cr, 0, sizeof(cr));
118 cr.dl = bootdrv;
119 cr.es = bootseg;
120 cr.ah = 2;
121 cr.al = 1;
122 cr.cl = 1;
123 call16_int(0x13, &cr);
124
125 if (cr.flags & F_CF) {
126 print_boot_failure(type, 1);
127 return;
128 }
129
130 /* Always check the signature on a HDD boot sector; on FDD,
Kevin O'Connorabc75972008-05-18 00:20:53 -0400131 * only do the check if configured for it */
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500132 if (type != IPL_TYPE_FLOPPY || IPL.checkfloppysig) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500133 if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
134 print_boot_failure(type, 0);
135 return;
136 }
137 }
138
139 /* Canonicalize bootseg:bootip */
140 bootip = (bootseg & 0x0fff) << 4;
141 bootseg &= 0xf000;
142 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500143 case IPL_TYPE_CDROM: {
144 /* CD-ROM */
145 if (! CONFIG_CDROM_BOOT)
Kevin O'Connord19501e2008-12-10 20:52:09 -0500146 return;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400147 int status = cdrom_boot();
Kevin O'Connor180a9592008-03-04 22:50:53 -0500148 if (status) {
149 printf("CDROM boot failure code : %04x\n", status);
150 print_boot_failure(type, 1);
151 return;
152 }
153
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500154 u16 ebda_seg = get_ebda_seg();
155 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
156 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500157 /* Canonicalize bootseg:bootip */
158 bootip = (bootseg & 0x0fff) << 4;
159 bootseg &= 0xf000;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500160 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500161 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500162 case IPL_TYPE_BEV: {
163 /* Expansion ROM with a Bootstrap Entry Vector (a far
164 * pointer) */
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500165 u32 vector = IPL.table[bootdev].vector;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500166 bootseg = vector >> 16;
167 bootip = vector & 0xffff;
168 break;
169 }
170 default:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500171 return;
172 }
173
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500174 /* Debugging info */
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400175 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500176
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500177 memset(&cr, 0, sizeof(cr));
178 cr.ip = bootip;
179 cr.cs = bootseg;
180 // Set the magic number in ax and the boot drive in dl.
181 cr.dl = bootdrv;
182 cr.ax = 0xaa55;
183 call16(&cr);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400184}
185
186static void
187do_boot(u16 seq_nr)
188{
189 try_boot(seq_nr);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500190
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500191 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400192 struct bregs br;
193 memset(&br, 0, sizeof(br));
194 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500195}
196
197// Boot Failure recovery: try the next device.
Kevin O'Connor44eeaf12008-07-06 10:14:49 -0400198void VISIBLE32
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500199handle_18()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500200{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400201 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400202 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500203 u16 ebda_seg = get_ebda_seg();
204 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
205 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400206 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500207}
208
209// INT 19h Boot Load Service Entry Point
Kevin O'Connor44eeaf12008-07-06 10:14:49 -0400210void VISIBLE32
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500211handle_19()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500212{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400213 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400214 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500215 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400216 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500217}
Kevin O'Connorcdd3d652008-07-12 14:22:14 -0400218
219// Ughh - some older gcc compilers have a bug which causes VISIBLE32
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400220// functions to not be exported as global variables.
Kevin O'Connorcdd3d652008-07-12 14:22:14 -0400221asm(".global handle_18, handle_19");