Interrupts should be enabled when calling 16bit code.
[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 LGPLv3 license.
7
8 #include "util.h" // irq_enable
9 #include "biosvar.h" // GET_EBDA
10 #include "config.h" // CONFIG_*
11 #include "disk.h" // cdrom_boot
12 #include "bregs.h" // struct bregs
13 #include "boot.h" // struct ipl_s
14 #include "cmos.h" // inb_cmos
15
16 struct ipl_s IPL;
17
18
19 /****************************************************************
20  * IPL and BCV handlers
21  ****************************************************************/
22
23 void
24 boot_setup()
25 {
26     if (! CONFIG_BOOT)
27         return;
28     dprintf(3, "init boot device ordering\n");
29
30     memset(&IPL, 0, sizeof(IPL));
31
32     // Floppy drive
33     struct ipl_entry_s *ie = &IPL.bev[0];
34     ie->type = IPL_TYPE_FLOPPY;
35     ie->description = "Floppy";
36     ie++;
37
38     // First HDD
39     ie->type = IPL_TYPE_HARDDISK;
40     ie->description = "Hard Disk";
41     ie++;
42
43     // CDROM
44     if (CONFIG_CDROM_BOOT) {
45         ie->type = IPL_TYPE_CDROM;
46         ie->description = "CD-Rom";
47         ie++;
48     }
49
50     if (CONFIG_COREBOOT_FLASH) {
51         ie->type = IPL_TYPE_CBFS;
52         ie->description = "CBFS";
53         ie++;
54     }
55
56     IPL.bevcount = ie - IPL.bev;
57     SET_EBDA(boot_sequence, 0xffff);
58     if (CONFIG_COREBOOT) {
59         // XXX - hardcode defaults for coreboot.
60         IPL.bootorder = 0x87654231;
61         IPL.checkfloppysig = 1;
62     } else {
63         // On emulators, get boot order from nvram.
64         IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
65                          | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
66         if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
67             IPL.checkfloppysig = 1;
68     }
69 }
70
71 // Add a BEV vector for a given pnp compatible option rom.
72 void
73 add_bev(u16 seg, u16 bev, u16 desc)
74 {
75     if (! CONFIG_BOOT)
76         return;
77     if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
78         return;
79
80     struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
81     ie->type = IPL_TYPE_BEV;
82     ie->vector = (seg << 16) | bev;
83     const char *d = "Unknown";
84     if (desc)
85         d = MAKE_FLATPTR(seg, desc);
86     ie->description = d;
87 }
88
89 // Add a bcv entry for an expansion card harddrive or legacy option rom
90 void
91 add_bcv(u16 seg, u16 ip, u16 desc)
92 {
93     if (! CONFIG_BOOT)
94         return;
95     if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
96         return;
97
98     struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
99     ie->type = BCV_TYPE_EXTERNAL;
100     ie->vector = (seg << 16) | ip;
101     const char *d = "Legacy option rom";
102     if (desc)
103         d = MAKE_FLATPTR(seg, desc);
104     ie->description = d;
105 }
106
107 // Add a bcv entry for an internal harddrive
108 void
109 add_bcv_internal(int driveid)
110 {
111     if (! CONFIG_BOOT)
112         return;
113     if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
114         return;
115
116     struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
117     ie->type = BCV_TYPE_INTERNAL;
118     ie->vector = driveid;
119     ie->description = "";
120 }
121
122
123 /****************************************************************
124  * Boot menu and BCV execution
125  ****************************************************************/
126
127 // Show a generic menu item
128 static int
129 menu_show_default(struct ipl_entry_s *ie, int menupos)
130 {
131     char desc[33];
132     printf("%d. %s\n", menupos
133            , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
134     return 1;
135 }
136
137 // Show floppy menu item - but only if there exists a floppy drive.
138 static int
139 menu_show_floppy(struct ipl_entry_s *ie, int menupos)
140 {
141     int i;
142     for (i = 0; i < Drives.floppycount; i++) {
143         int driveid = Drives.idmap[EXTTYPE_FLOPPY][i];
144         printf("%d. Floppy [", menupos + i);
145         describe_drive(driveid);
146         printf("]\n");
147     }
148     return Drives.floppycount;
149 }
150
151 // Show menu items from BCV list.
152 static int
153 menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
154 {
155     int i;
156     for (i = 0; i < IPL.bcvcount; i++) {
157         struct ipl_entry_s *ie = &IPL.bcv[i];
158         switch (ie->type) {
159         case BCV_TYPE_INTERNAL:
160             printf("%d. ", menupos + i);
161             describe_drive(ie->vector);
162             printf("\n");
163             break;
164         default:
165             menu_show_default(ie, menupos+i);
166             break;
167         }
168     }
169     return IPL.bcvcount;
170 }
171
172 // Show cdrom menu item - but only if there exists a cdrom drive.
173 static int
174 menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
175 {
176     int i;
177     for (i = 0; i < Drives.cdcount; i++) {
178         int driveid = Drives.idmap[EXTTYPE_CD][i];
179         printf("%d. CD-Rom [", menupos + i);
180         describe_drive(driveid);
181         printf("]\n");
182     }
183     return Drives.cdcount;
184 }
185
186 // Show coreboot-fs menu item.
187 static int
188 menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
189 {
190     int count = 0;
191     struct cbfs_file *file = NULL;
192     for (;;) {
193         file = cbfs_findprefix("img/", file);
194         if (!file)
195             break;
196         const char *filename = cbfs_filename(file);
197         printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
198         count++;
199         if (count > 8)
200             break;
201     }
202     return count;
203 }
204
205 // Show IPL option menu.
206 static void
207 interactive_bootmenu()
208 {
209     if (! CONFIG_BOOTMENU)
210         return;
211
212     while (get_keystroke(0) >= 0)
213         ;
214
215     printf("Press F12 for boot menu.\n\n");
216
217     int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
218     if (scan_code != 0x86)
219         /* not F12 */
220         return;
221
222     while (get_keystroke(0) >= 0)
223         ;
224
225     printf("Select boot device:\n\n");
226
227     int subcount[ARRAY_SIZE(IPL.bev)];
228     int menupos = 1;
229     int i;
230     for (i = 0; i < IPL.bevcount; i++) {
231         struct ipl_entry_s *ie = &IPL.bev[i];
232         int sc;
233         switch (ie->type) {
234         case IPL_TYPE_FLOPPY:
235             sc = menu_show_floppy(ie, menupos);
236             break;
237         case IPL_TYPE_HARDDISK:
238             sc = menu_show_harddisk(ie, menupos);
239             break;
240         case IPL_TYPE_CDROM:
241             sc = menu_show_cdrom(ie, menupos);
242             break;
243         case IPL_TYPE_CBFS:
244             sc = menu_show_cbfs(ie, menupos);
245             break;
246         default:
247             sc = menu_show_default(ie, menupos);
248             break;
249         }
250         subcount[i] = sc;
251         menupos += sc;
252     }
253
254     for (;;) {
255         scan_code = get_keystroke(1000);
256         if (scan_code == 0x01)
257             // ESC
258             break;
259         if (scan_code < 1 || scan_code > menupos)
260             continue;
261         int choice = scan_code - 1;
262
263         // Find out which IPL this was for.
264         int bev = 0;
265         while (choice > subcount[bev]) {
266             choice -= subcount[bev];
267             bev++;
268         }
269         IPL.bev[bev].subchoice = choice-1;
270
271         // Add user choice to the boot order.
272         IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
273         break;
274     }
275     printf("\n");
276 }
277
278 // Run the specified bcv.
279 static void
280 run_bcv(struct ipl_entry_s *ie)
281 {
282     switch (ie->type) {
283     case BCV_TYPE_INTERNAL:
284         map_hd_drive(ie->vector);
285         break;
286     case BCV_TYPE_EXTERNAL:
287         call_bcv(ie->vector >> 16, ie->vector & 0xffff);
288         break;
289     }
290 }
291
292 // Prepare for boot - show menu and run bcvs.
293 void
294 boot_prep()
295 {
296     if (! CONFIG_BOOT)
297         return;
298
299     // Allow user to modify BCV/IPL order.
300     interactive_bootmenu();
301
302     // Setup floppy boot order
303     int override = IPL.bev[0].subchoice;
304     int tmp = Drives.idmap[EXTTYPE_FLOPPY][0];
305     Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override];
306     Drives.idmap[EXTTYPE_FLOPPY][override] = tmp;
307
308     // Run BCVs
309     override = IPL.bev[1].subchoice;
310     if (override < IPL.bcvcount)
311         run_bcv(&IPL.bcv[override]);
312     int i;
313     for (i=0; i<IPL.bcvcount; i++)
314         if (i != override)
315             run_bcv(&IPL.bcv[i]);
316 }
317
318
319 /****************************************************************
320  * Boot code (int 18/19)
321  ****************************************************************/
322
323 // Jump to a bootup entry point.
324 static void
325 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
326 {
327     dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
328
329     struct bregs br;
330     memset(&br, 0, sizeof(br));
331     br.flags = F_IF;
332     br.code = SEGOFF(bootseg, bootip);
333     // Set the magic number in ax and the boot drive in dl.
334     br.dl = bootdrv;
335     br.ax = 0xaa55;
336     call16(&br);
337 }
338
339 // Boot from a disk (either floppy or harddrive)
340 static void
341 boot_disk(u8 bootdrv, int checksig)
342 {
343     u16 bootseg = 0x07c0;
344
345     // Read sector
346     struct bregs br;
347     memset(&br, 0, sizeof(br));
348     br.flags = F_IF;
349     br.dl = bootdrv;
350     br.es = bootseg;
351     br.ah = 2;
352     br.al = 1;
353     br.cl = 1;
354     call16_int(0x13, &br);
355
356     if (br.flags & F_CF) {
357         printf("Boot failed: could not read the boot disk\n\n");
358         return;
359     }
360
361     if (checksig) {
362         struct mbr_s *mbr = (void*)0;
363         if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
364             printf("Boot failed: not a bootable disk\n\n");
365             return;
366         }
367     }
368
369     /* Canonicalize bootseg:bootip */
370     u16 bootip = (bootseg & 0x0fff) << 4;
371     bootseg &= 0xf000;
372
373     call_boot_entry(bootseg, bootip, bootdrv);
374 }
375
376 // Boot from a CD-ROM
377 static void
378 boot_cdrom(struct ipl_entry_s *ie)
379 {
380     if (! CONFIG_CDROM_BOOT)
381         return;
382     int status = cdrom_boot(ie->subchoice);
383     if (status) {
384         printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
385         return;
386     }
387
388     u16 ebda_seg = get_ebda_seg();
389     u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
390     u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
391     /* Canonicalize bootseg:bootip */
392     u16 bootip = (bootseg & 0x0fff) << 4;
393     bootseg &= 0xf000;
394
395     call_boot_entry(bootseg, bootip, bootdrv);
396 }
397
398 // Boot from a CBFS payload
399 static void
400 boot_cbfs(struct ipl_entry_s *ie)
401 {
402     if (! CONFIG_COREBOOT_FLASH)
403         return;
404     int count = ie->subchoice;
405     struct cbfs_file *file;
406     for (;;) {
407         file = cbfs_findprefix("img/", file);
408         if (!file)
409             return;
410         if (count--)
411             continue;
412         cbfs_run_payload(file);
413     }
414 }
415
416 static void
417 do_boot(u16 seq_nr)
418 {
419     if (! CONFIG_BOOT)
420         panic("Boot support not compiled in.\n");
421
422     u32 bootdev = IPL.bootorder;
423     bootdev >>= 4 * seq_nr;
424     bootdev &= 0xf;
425
426     /* Translate bootdev to an IPL table offset by subtracting 1 */
427     bootdev -= 1;
428
429     if (bootdev >= IPL.bevcount) {
430         printf("No bootable device.\n");
431         // Loop with irqs enabled - this allows ctrl+alt+delete to work.
432         for (;;)
433             usleep(1000000);
434     }
435
436     /* Do the loading, and set up vector as a far pointer to the boot
437      * address, and bootdrv as the boot drive */
438     struct ipl_entry_s *ie = &IPL.bev[bootdev];
439     char desc[33];
440     printf("Booting from %s...\n"
441            , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
442
443     switch(ie->type) {
444     case IPL_TYPE_FLOPPY:
445         boot_disk(0x00, IPL.checkfloppysig);
446         break;
447     case IPL_TYPE_HARDDISK:
448         boot_disk(0x80, 1);
449         break;
450     case IPL_TYPE_CDROM:
451         boot_cdrom(ie);
452         break;
453     case IPL_TYPE_CBFS:
454         boot_cbfs(ie);
455         break;
456     case IPL_TYPE_BEV:
457         call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
458         break;
459     }
460
461     // Boot failed: invoke the boot recovery function
462     struct bregs br;
463     memset(&br, 0, sizeof(br));
464     br.flags = F_IF;
465     call16_int(0x18, &br);
466 }
467
468 // Boot Failure recovery: try the next device.
469 void VISIBLE32
470 handle_18()
471 {
472     debug_serial_setup();
473     debug_enter(NULL, DEBUG_HDL_18);
474     u16 ebda_seg = get_ebda_seg();
475     u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
476     SET_EBDA2(ebda_seg, boot_sequence, seq);
477     do_boot(seq);
478 }
479
480 // INT 19h Boot Load Service Entry Point
481 void VISIBLE32
482 handle_19()
483 {
484     debug_serial_setup();
485     debug_enter(NULL, DEBUG_HDL_19);
486     SET_EBDA(boot_sequence, 0);
487     do_boot(0);
488 }