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