Cleanup boot order determination.
[seabios.git] / src / boot.c
1 // 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
8 #include "util.h" // irq_enable
9 #include "biosvar.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "cmos.h" // inb_cmos
12 #include "ata.h" // ata_detect
13 #include "disk.h" // cdrom_boot
14
15 // 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.
18 char pnp_string[] VISIBLE16 __attribute__((aligned (2))) = " $PnP";
19
20 //--------------------------------------------------------------------------
21 // print_boot_device
22 //   displays the boot device
23 //--------------------------------------------------------------------------
24
25 static const char drivetypes[][10]={
26     "", "Floppy","Hard Disk","CD-Rom", "Network"
27 };
28
29 static void
30 print_boot_device(u16 type)
31 {
32     /* 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]);
38
39     // XXX - latest cvs has BEV description
40 }
41
42 //--------------------------------------------------------------------------
43 // print_boot_failure
44 //   displays the reason why boot failed
45 //--------------------------------------------------------------------------
46 static void
47 print_boot_failure(u16 type, u8 reason)
48 {
49     if (type == 0 || type > 0x3)
50         BX_PANIC("Bad drive type\n");
51
52     printf("Boot failed");
53     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     }
60     printf("\n\n");
61 }
62
63 static void
64 try_boot(u16 seq_nr)
65 {
66     irq_enable();
67
68     SET_EBDA(ipl.sequence, seq_nr);
69
70     u16 bootseg;
71     u8 bootdrv = 0;
72     u16 bootdev, bootip;
73
74     bootdev = inb_cmos(CMOS_BIOS_BOOTFLAG2);
75     bootdev |= ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4);
76     bootdev >>= 4 * seq_nr;
77     bootdev &= 0xf;
78     if (bootdev == 0)
79         BX_PANIC("No bootable device.\n");
80
81     /* Translate from CMOS runes to an IPL table offset by subtracting 1 */
82     bootdev -= 1;
83
84     if (bootdev >= GET_EBDA(ipl.count)) {
85         BX_INFO("Invalid boot device (0x%x)\n", bootdev);
86         return;
87     }
88     u16 type = GET_EBDA(ipl.table[bootdev].type);
89
90     /* Do the loading, and set up vector as a far pointer to the boot
91      * address, and bootdrv as the boot drive */
92     print_boot_device(type);
93
94     struct bregs cr;
95     switch(type) {
96     case IPL_TYPE_FLOPPY: /* FDD */
97     case IPL_TYPE_HARDDISK: /* HDD */
98
99         bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
100         bootseg = 0x07c0;
101
102         // Read sector
103         memset(&cr, 0, sizeof(cr));
104         cr.dl = bootdrv;
105         cr.es = bootseg;
106         cr.ah = 2;
107         cr.al = 1;
108         cr.cl = 1;
109         call16_int(0x13, &cr);
110
111         if (cr.flags & F_CF) {
112             print_boot_failure(type, 1);
113             return;
114         }
115
116         /* Always check the signature on a HDD boot sector; on FDD,
117          * only do the check if the CMOS doesn't tell us to skip it */
118         if ((type != IPL_TYPE_FLOPPY)
119             || !((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0x01))) {
120             if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
121                 print_boot_failure(type, 0);
122                 return;
123             }
124         }
125
126         /* Canonicalize bootseg:bootip */
127         bootip = (bootseg & 0x0fff) << 4;
128         bootseg &= 0xf000;
129         break;
130     case IPL_TYPE_CDROM: {
131         /* CD-ROM */
132         if (! CONFIG_CDROM_BOOT)
133             break;
134         u16 status = cdrom_boot();
135         if (status) {
136             printf("CDROM boot failure code : %04x\n", status);
137             print_boot_failure(type, 1);
138             return;
139         }
140
141         bootdrv = GET_EBDA(cdemu.emulated_drive);
142         bootseg = GET_EBDA(cdemu.load_segment);
143         /* Canonicalize bootseg:bootip */
144         bootip = (bootseg & 0x0fff) << 4;
145         bootseg &= 0xf000;
146         break;
147     }
148     case IPL_TYPE_BEV: {
149         /* Expansion ROM with a Bootstrap Entry Vector (a far
150          * pointer) */
151         u32 vector = GET_EBDA(ipl.table[bootdev].vector);
152         bootseg = vector >> 16;
153         bootip = vector & 0xffff;
154         break;
155     }
156     default:
157         return;
158     }
159
160     /* Debugging info */
161     BX_INFO("Booting from %x:%x\n", bootseg, bootip);
162
163     memset(&cr, 0, sizeof(cr));
164     cr.ip = bootip;
165     cr.cs = bootseg;
166     // Set the magic number in ax and the boot drive in dl.
167     cr.dl = bootdrv;
168     cr.ax = 0xaa55;
169     call16(&cr);
170 }
171
172 static void
173 do_boot(u16 seq_nr)
174 {
175     try_boot(seq_nr);
176
177     // Boot failed: invoke the boot recovery function
178     struct bregs br;
179     memset(&br, 0, sizeof(br));
180     call16_int(0x18, &br);
181 }
182
183 // Boot Failure recovery: try the next device.
184 void VISIBLE16
185 handle_18()
186 {
187     debug_enter(NULL);
188     u16 seq = GET_EBDA(ipl.sequence) + 1;
189     do_boot(seq);
190 }
191
192 // INT 19h Boot Load Service Entry Point
193 void VISIBLE16
194 handle_19()
195 {
196     debug_enter(NULL);
197     do_boot(0);
198 }