blob: b8104a5745575bc7ab2660d4425a3d3b19eb26cf [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//
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'Connorc659fde2008-12-28 23:43:20 -050014#include "boot.h" // struct ipl_s
15
16struct ipl_s IPL;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050017
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050018//--------------------------------------------------------------------------
19// print_boot_device
20// displays the boot device
21//--------------------------------------------------------------------------
22
23static const char drivetypes[][10]={
Kevin O'Connorabc75972008-05-18 00:20:53 -040024 "", "Floppy", "Hard Disk", "CD-Rom", "Network"
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050025};
26
Kevin O'Connore0113c92008-04-05 15:51:12 -040027void
28printf_bootdev(u16 bootdev)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050029{
Kevin O'Connorc659fde2008-12-28 23:43:20 -050030 u16 type = IPL.table[bootdev].type;
Kevin O'Connore0113c92008-04-05 15:51:12 -040031
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");
Kevin O'Connore0113c92008-04-05 15:51:12 -040037 printf("%s", drivetypes[type]);
Kevin O'Connora2e73802008-02-27 10:27:00 -050038
Kevin O'Connore0113c92008-04-05 15:51:12 -040039 /* print product string if BEV */
Kevin O'Connorc659fde2008-12-28 23:43:20 -050040 char *far_description = IPL.table[bootdev].description;
Kevin O'Connore0113c92008-04-05 15:51:12 -040041 if (type == 4 && far_description != 0) {
42 char description[33];
43 /* first 32 bytes are significant */
Kevin O'Connorf782d602008-11-08 13:35:32 -050044 memcpy_far(MAKE_FARPTR(GET_SEG(SS), description), far_description, 32);
Kevin O'Connore0113c92008-04-05 15:51:12 -040045 /* terminate string */
46 description[32] = 0;
47 printf(" [%.s]", description);
48 }
49}
50
51static void
52print_boot_device(u16 bootdev)
53{
54 printf("Booting from ");
55 printf_bootdev(bootdev);
56 printf("...\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050057}
58
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050059//--------------------------------------------------------------------------
60// print_boot_failure
61// displays the reason why boot failed
62//--------------------------------------------------------------------------
63static void
64print_boot_failure(u16 type, u8 reason)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050065{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050066 if (type == 0 || type > 0x3)
67 BX_PANIC("Bad drive type\n");
68
Kevin O'Connora2e73802008-02-27 10:27:00 -050069 printf("Boot failed");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050070 if (type < 4) {
71 /* Report the reason too */
72 if (reason==0)
73 printf(": not a bootable disk");
74 else
75 printf(": could not read the boot disk");
76 }
Kevin O'Connora2e73802008-02-27 10:27:00 -050077 printf("\n\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050078}
79
80static void
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050081try_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050082{
Kevin O'Connor40967022008-07-21 22:23:05 -040083 if (! CONFIG_BOOT)
84 BX_PANIC("Boot support not compiled in.\n");
85
Kevin O'Connorc659fde2008-12-28 23:43:20 -050086 SET_EBDA(boot_sequence, seq_nr);
Kevin O'Connor7a558e42008-03-11 20:38:33 -040087
Kevin O'Connorc659fde2008-12-28 23:43:20 -050088 u32 bootdev = IPL.bootorder;
Kevin O'Connor840c5342008-03-29 14:04:34 -040089 bootdev >>= 4 * seq_nr;
90 bootdev &= 0xf;
Kevin O'Connore0113c92008-04-05 15:51:12 -040091
Kevin O'Connor840c5342008-03-29 14:04:34 -040092 if (bootdev == 0)
93 BX_PANIC("No bootable device.\n");
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050094
Kevin O'Connorabc75972008-05-18 00:20:53 -040095 /* Translate bootdev to an IPL table offset by subtracting 1 */
Kevin O'Connor840c5342008-03-29 14:04:34 -040096 bootdev -= 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050097
Kevin O'Connorc659fde2008-12-28 23:43:20 -050098 if (bootdev >= IPL.count) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040099 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500100 return;
101 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500102
103 /* Do the loading, and set up vector as a far pointer to the boot
104 * address, and bootdrv as the boot drive */
Kevin O'Connore0113c92008-04-05 15:51:12 -0400105 print_boot_device(bootdev);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500106
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500107 u16 type = IPL.table[bootdev].type;
Kevin O'Connore0113c92008-04-05 15:51:12 -0400108
109 u16 bootseg, bootip;
110 u8 bootdrv = 0;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500111 struct bregs cr;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500112 switch(type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400113 case IPL_TYPE_FLOPPY:
114 case IPL_TYPE_HARDDISK:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500115
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500116 bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
117 bootseg = 0x07c0;
118
119 // Read sector
120 memset(&cr, 0, sizeof(cr));
121 cr.dl = bootdrv;
122 cr.es = bootseg;
123 cr.ah = 2;
124 cr.al = 1;
125 cr.cl = 1;
126 call16_int(0x13, &cr);
127
128 if (cr.flags & F_CF) {
129 print_boot_failure(type, 1);
130 return;
131 }
132
133 /* Always check the signature on a HDD boot sector; on FDD,
Kevin O'Connorabc75972008-05-18 00:20:53 -0400134 * only do the check if configured for it */
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500135 if (type != IPL_TYPE_FLOPPY || IPL.checkfloppysig) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500136 if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
137 print_boot_failure(type, 0);
138 return;
139 }
140 }
141
142 /* Canonicalize bootseg:bootip */
143 bootip = (bootseg & 0x0fff) << 4;
144 bootseg &= 0xf000;
145 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500146 case IPL_TYPE_CDROM: {
147 /* CD-ROM */
148 if (! CONFIG_CDROM_BOOT)
Kevin O'Connord19501e2008-12-10 20:52:09 -0500149 return;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400150 int status = cdrom_boot();
Kevin O'Connor180a9592008-03-04 22:50:53 -0500151 if (status) {
152 printf("CDROM boot failure code : %04x\n", status);
153 print_boot_failure(type, 1);
154 return;
155 }
156
Kevin O'Connor609da232008-12-28 23:18:57 -0500157 bootdrv = GET_GLOBAL(CDEMU.emulated_drive);
158 bootseg = GET_GLOBAL(CDEMU.load_segment);
Kevin O'Connor180a9592008-03-04 22:50:53 -0500159 /* Canonicalize bootseg:bootip */
160 bootip = (bootseg & 0x0fff) << 4;
161 bootseg &= 0xf000;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500162 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500163 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500164 case IPL_TYPE_BEV: {
165 /* Expansion ROM with a Bootstrap Entry Vector (a far
166 * pointer) */
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500167 u32 vector = IPL.table[bootdev].vector;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500168 bootseg = vector >> 16;
169 bootip = vector & 0xffff;
170 break;
171 }
172 default:
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500173 return;
174 }
175
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500176 /* Debugging info */
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400177 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500178
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500179 memset(&cr, 0, sizeof(cr));
180 cr.ip = bootip;
181 cr.cs = bootseg;
182 // Set the magic number in ax and the boot drive in dl.
183 cr.dl = bootdrv;
184 cr.ax = 0xaa55;
185 call16(&cr);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400186}
187
188static void
189do_boot(u16 seq_nr)
190{
191 try_boot(seq_nr);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500192
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500193 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400194 struct bregs br;
195 memset(&br, 0, sizeof(br));
196 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500197}
198
199// Boot Failure recovery: try the next device.
Kevin O'Connor44eeaf12008-07-06 10:14:49 -0400200void VISIBLE32
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500201handle_18()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500202{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400203 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400204 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500205 u16 seq = GET_EBDA(boot_sequence) + 1;
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'Connor7af49bf2008-03-09 13:46:13 -0400215 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500216}
Kevin O'Connorcdd3d652008-07-12 14:22:14 -0400217
218// Ughh - some older gcc compilers have a bug which causes VISIBLE32
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400219// functions to not be exported as global variables.
Kevin O'Connorcdd3d652008-07-12 14:22:14 -0400220asm(".global handle_18, handle_19");