Avoid casting EBDA variables ipl.description and pir_loc.
[seabios.git] / src / boot.c
1 // 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" // GET_EBDA
10 #include "config.h" // CONFIG_*
11 #include "ata.h" // ata_detect
12 #include "disk.h" // cdrom_boot
13 #include "bregs.h" // struct bregs
14
15 //--------------------------------------------------------------------------
16 // print_boot_device
17 //   displays the boot device
18 //--------------------------------------------------------------------------
19
20 static const char drivetypes[][10]={
21     "", "Floppy", "Hard Disk", "CD-Rom", "Network"
22 };
23
24 void
25 printf_bootdev(u16 bootdev)
26 {
27     u16 type = GET_EBDA(ipl.table[bootdev].type);
28
29     /* 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");
34     printf("%s", drivetypes[type]);
35
36     /* print product string if BEV */
37     char *far_description = 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_far(MAKE_FARPTR(GET_SEG(SS), description), far_description, 32);
42         /* terminate string */
43         description[32] = 0;
44         printf(" [%.s]", description);
45     }
46 }
47
48 static void
49 print_boot_device(u16 bootdev)
50 {
51     printf("Booting from ");
52     printf_bootdev(bootdev);
53     printf("...\n");
54 }
55
56 //--------------------------------------------------------------------------
57 // print_boot_failure
58 //   displays the reason why boot failed
59 //--------------------------------------------------------------------------
60 static void
61 print_boot_failure(u16 type, u8 reason)
62 {
63     if (type == 0 || type > 0x3)
64         BX_PANIC("Bad drive type\n");
65
66     printf("Boot failed");
67     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     }
74     printf("\n\n");
75 }
76
77 static void
78 try_boot(u16 seq_nr)
79 {
80     if (! CONFIG_BOOT)
81         BX_PANIC("Boot support not compiled in.\n");
82
83     SET_EBDA(ipl.sequence, seq_nr);
84
85     u32 bootdev = GET_EBDA(ipl.bootorder);
86     bootdev >>= 4 * seq_nr;
87     bootdev &= 0xf;
88
89     if (bootdev == 0)
90         BX_PANIC("No bootable device.\n");
91
92     /* Translate bootdev to an IPL table offset by subtracting 1 */
93     bootdev -= 1;
94
95     if (bootdev >= GET_EBDA(ipl.count)) {
96         dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
97         return;
98     }
99
100     /* Do the loading, and set up vector as a far pointer to the boot
101      * address, and bootdrv as the boot drive */
102     print_boot_device(bootdev);
103
104     u16 type = GET_EBDA(ipl.table[bootdev].type);
105
106     u16 bootseg, bootip;
107     u8 bootdrv = 0;
108     struct bregs cr;
109     switch(type) {
110     case IPL_TYPE_FLOPPY:
111     case IPL_TYPE_HARDDISK:
112
113         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,
131          * only do the check if configured for it */
132         if (type != IPL_TYPE_FLOPPY || GET_EBDA(ipl.checkfloppysig)) {
133             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;
143     case IPL_TYPE_CDROM: {
144         /* CD-ROM */
145         if (! CONFIG_CDROM_BOOT)
146             break;
147         int status = cdrom_boot();
148         if (status) {
149             printf("CDROM boot failure code : %04x\n", status);
150             print_boot_failure(type, 1);
151             return;
152         }
153
154         bootdrv = GET_EBDA(cdemu.emulated_drive);
155         bootseg = GET_EBDA(cdemu.load_segment);
156         /* Canonicalize bootseg:bootip */
157         bootip = (bootseg & 0x0fff) << 4;
158         bootseg &= 0xf000;
159         break;
160     }
161     case IPL_TYPE_BEV: {
162         /* Expansion ROM with a Bootstrap Entry Vector (a far
163          * pointer) */
164         u32 vector = GET_EBDA(ipl.table[bootdev].vector);
165         bootseg = vector >> 16;
166         bootip = vector & 0xffff;
167         break;
168     }
169     default:
170         return;
171     }
172
173     /* Debugging info */
174     dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
175
176     memset(&cr, 0, sizeof(cr));
177     cr.ip = bootip;
178     cr.cs = bootseg;
179     // Set the magic number in ax and the boot drive in dl.
180     cr.dl = bootdrv;
181     cr.ax = 0xaa55;
182     call16(&cr);
183 }
184
185 static void
186 do_boot(u16 seq_nr)
187 {
188     try_boot(seq_nr);
189
190     // Boot failed: invoke the boot recovery function
191     struct bregs br;
192     memset(&br, 0, sizeof(br));
193     call16_int(0x18, &br);
194 }
195
196 // Boot Failure recovery: try the next device.
197 void VISIBLE32
198 handle_18()
199 {
200     debug_serial_setup();
201     debug_enter(NULL, DEBUG_HDL_18);
202     u16 seq = GET_EBDA(ipl.sequence) + 1;
203     do_boot(seq);
204 }
205
206 // INT 19h Boot Load Service Entry Point
207 void VISIBLE32
208 handle_19()
209 {
210     debug_serial_setup();
211     debug_enter(NULL, DEBUG_HDL_19);
212     do_boot(0);
213 }
214
215 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
216 // functions to not be exported as global variables.
217 asm(".global handle_18, handle_19");