Move $PnP string from boot.c to romlayout.S.
[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 //--------------------------------------------------------------------------
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     void *far_description = (void*)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(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     irq_enable();
81
82     SET_EBDA(ipl.sequence, seq_nr);
83
84     u32 bootdev = GET_EBDA(ipl.bootorder);
85     bootdev >>= 4 * seq_nr;
86     bootdev &= 0xf;
87
88     if (bootdev == 0)
89         BX_PANIC("No bootable device.\n");
90
91     /* Translate bootdev to an IPL table offset by subtracting 1 */
92     bootdev -= 1;
93
94     if (bootdev >= GET_EBDA(ipl.count)) {
95         dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
96         return;
97     }
98
99     /* Do the loading, and set up vector as a far pointer to the boot
100      * address, and bootdrv as the boot drive */
101     print_boot_device(bootdev);
102
103     u16 type = GET_EBDA(ipl.table[bootdev].type);
104
105     u16 bootseg, bootip;
106     u8 bootdrv = 0;
107     struct bregs cr;
108     switch(type) {
109     case IPL_TYPE_FLOPPY: /* FDD */
110     case IPL_TYPE_HARDDISK: /* HDD */
111
112         bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
113         bootseg = 0x07c0;
114
115         // Read sector
116         memset(&cr, 0, sizeof(cr));
117         cr.dl = bootdrv;
118         cr.es = bootseg;
119         cr.ah = 2;
120         cr.al = 1;
121         cr.cl = 1;
122         call16_int(0x13, &cr);
123
124         if (cr.flags & F_CF) {
125             print_boot_failure(type, 1);
126             return;
127         }
128
129         /* Always check the signature on a HDD boot sector; on FDD,
130          * only do the check if configured for it */
131         if (type != IPL_TYPE_FLOPPY || GET_EBDA(ipl.checkfloppysig)) {
132             if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
133                 print_boot_failure(type, 0);
134                 return;
135             }
136         }
137
138         /* Canonicalize bootseg:bootip */
139         bootip = (bootseg & 0x0fff) << 4;
140         bootseg &= 0xf000;
141         break;
142     case IPL_TYPE_CDROM: {
143         /* CD-ROM */
144         if (! CONFIG_CDROM_BOOT)
145             break;
146         int status = cdrom_boot();
147         if (status) {
148             printf("CDROM boot failure code : %04x\n", status);
149             print_boot_failure(type, 1);
150             return;
151         }
152
153         bootdrv = GET_EBDA(cdemu.emulated_drive);
154         bootseg = GET_EBDA(cdemu.load_segment);
155         /* Canonicalize bootseg:bootip */
156         bootip = (bootseg & 0x0fff) << 4;
157         bootseg &= 0xf000;
158         break;
159     }
160     case IPL_TYPE_BEV: {
161         /* Expansion ROM with a Bootstrap Entry Vector (a far
162          * pointer) */
163         u32 vector = GET_EBDA(ipl.table[bootdev].vector);
164         bootseg = vector >> 16;
165         bootip = vector & 0xffff;
166         break;
167     }
168     default:
169         return;
170     }
171
172     /* Debugging info */
173     dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
174
175     memset(&cr, 0, sizeof(cr));
176     cr.ip = bootip;
177     cr.cs = bootseg;
178     // Set the magic number in ax and the boot drive in dl.
179     cr.dl = bootdrv;
180     cr.ax = 0xaa55;
181     call16(&cr);
182 }
183
184 static void
185 do_boot(u16 seq_nr)
186 {
187     try_boot(seq_nr);
188
189     // Boot failed: invoke the boot recovery function
190     struct bregs br;
191     memset(&br, 0, sizeof(br));
192     call16_int(0x18, &br);
193 }
194
195 // Boot Failure recovery: try the next device.
196 void VISIBLE16
197 handle_18()
198 {
199     debug_serial_setup();
200     debug_enter(NULL, DEBUG_HDL_18);
201     u16 seq = GET_EBDA(ipl.sequence) + 1;
202     do_boot(seq);
203 }
204
205 // INT 19h Boot Load Service Entry Point
206 void VISIBLE16
207 handle_19()
208 {
209     debug_serial_setup();
210     debug_enter(NULL, DEBUG_HDL_19);
211     do_boot(0);
212 }