Extract 'struct bregs' out of biosvar.h; clean up header includes.
[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" // 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 // 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 void
30 printf_bootdev(u16 bootdev)
31 {
32     u16 type = GET_EBDA(ipl.table[bootdev].type);
33
34     /* NIC appears as type 0x80 */
35     if (type == IPL_TYPE_BEV)
36         type = 0x4;
37     if (type == 0 || type > 0x4)
38         BX_PANIC("Bad drive type\n");
39     printf("%s", drivetypes[type]);
40
41     /* print product string if BEV */
42     void *far_description = (void*)GET_EBDA(ipl.table[bootdev].description);
43     if (type == 4 && far_description != 0) {
44         char description[33];
45         /* first 32 bytes are significant */
46         memcpy(MAKE_FARPTR(GET_SEG(SS), &description), far_description, 32);
47         /* terminate string */
48         description[32] = 0;
49         printf(" [%.s]", description);
50     }
51 }
52
53 static void
54 print_boot_device(u16 bootdev)
55 {
56     printf("Booting from ");
57     printf_bootdev(bootdev);
58     printf("...\n");
59 }
60
61 //--------------------------------------------------------------------------
62 // print_boot_failure
63 //   displays the reason why boot failed
64 //--------------------------------------------------------------------------
65 static void
66 print_boot_failure(u16 type, u8 reason)
67 {
68     if (type == 0 || type > 0x3)
69         BX_PANIC("Bad drive type\n");
70
71     printf("Boot failed");
72     if (type < 4) {
73         /* Report the reason too */
74         if (reason==0)
75             printf(": not a bootable disk");
76         else
77             printf(": could not read the boot disk");
78     }
79     printf("\n\n");
80 }
81
82 static void
83 try_boot(u16 seq_nr)
84 {
85     irq_enable();
86
87     SET_EBDA(ipl.sequence, seq_nr);
88
89     u32 bootdev = GET_EBDA(ipl.bootorder);
90     bootdev >>= 4 * seq_nr;
91     bootdev &= 0xf;
92
93     if (bootdev == 0)
94         BX_PANIC("No bootable device.\n");
95
96     /* Translate bootdev to an IPL table offset by subtracting 1 */
97     bootdev -= 1;
98
99     if (bootdev >= GET_EBDA(ipl.count)) {
100         dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
101         return;
102     }
103
104     /* Do the loading, and set up vector as a far pointer to the boot
105      * address, and bootdrv as the boot drive */
106     print_boot_device(bootdev);
107
108     u16 type = GET_EBDA(ipl.table[bootdev].type);
109
110     u16 bootseg, bootip;
111     u8 bootdrv = 0;
112     struct bregs cr;
113     switch(type) {
114     case IPL_TYPE_FLOPPY: /* FDD */
115     case IPL_TYPE_HARDDISK: /* HDD */
116
117         bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
118         bootseg = 0x07c0;
119
120         // Read sector
121         memset(&cr, 0, sizeof(cr));
122         cr.dl = bootdrv;
123         cr.es = bootseg;
124         cr.ah = 2;
125         cr.al = 1;
126         cr.cl = 1;
127         call16_int(0x13, &cr);
128
129         if (cr.flags & F_CF) {
130             print_boot_failure(type, 1);
131             return;
132         }
133
134         /* Always check the signature on a HDD boot sector; on FDD,
135          * only do the check if configured for it */
136         if (type != IPL_TYPE_FLOPPY || GET_EBDA(ipl.checkfloppysig)) {
137             if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
138                 print_boot_failure(type, 0);
139                 return;
140             }
141         }
142
143         /* Canonicalize bootseg:bootip */
144         bootip = (bootseg & 0x0fff) << 4;
145         bootseg &= 0xf000;
146         break;
147     case IPL_TYPE_CDROM: {
148         /* CD-ROM */
149         if (! CONFIG_CDROM_BOOT)
150             break;
151         int status = cdrom_boot();
152         if (status) {
153             printf("CDROM boot failure code : %04x\n", status);
154             print_boot_failure(type, 1);
155             return;
156         }
157
158         bootdrv = GET_EBDA(cdemu.emulated_drive);
159         bootseg = GET_EBDA(cdemu.load_segment);
160         /* Canonicalize bootseg:bootip */
161         bootip = (bootseg & 0x0fff) << 4;
162         bootseg &= 0xf000;
163         break;
164     }
165     case IPL_TYPE_BEV: {
166         /* Expansion ROM with a Bootstrap Entry Vector (a far
167          * pointer) */
168         u32 vector = GET_EBDA(ipl.table[bootdev].vector);
169         bootseg = vector >> 16;
170         bootip = vector & 0xffff;
171         break;
172     }
173     default:
174         return;
175     }
176
177     /* Debugging info */
178     dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
179
180     memset(&cr, 0, sizeof(cr));
181     cr.ip = bootip;
182     cr.cs = bootseg;
183     // Set the magic number in ax and the boot drive in dl.
184     cr.dl = bootdrv;
185     cr.ax = 0xaa55;
186     call16(&cr);
187 }
188
189 static void
190 do_boot(u16 seq_nr)
191 {
192     try_boot(seq_nr);
193
194     // Boot failed: invoke the boot recovery function
195     struct bregs br;
196     memset(&br, 0, sizeof(br));
197     call16_int(0x18, &br);
198 }
199
200 // Boot Failure recovery: try the next device.
201 void VISIBLE16
202 handle_18()
203 {
204     debug_serial_setup();
205     debug_enter(NULL, DEBUG_HDL_18);
206     u16 seq = GET_EBDA(ipl.sequence) + 1;
207     do_boot(seq);
208 }
209
210 // INT 19h Boot Load Service Entry Point
211 void VISIBLE16
212 handle_19()
213 {
214     debug_serial_setup();
215     debug_enter(NULL, DEBUG_HDL_19);
216     do_boot(0);
217 }