Enhance boot menu to allow selection of floppy to boot from.
[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 = IPL_TYPE_BEV;
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 ata harddrive
108 void
109 add_bcv_hd(int driveid, const char *desc)
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 = IPL_TYPE_HARDDISK;
118     ie->vector = driveid;
119     ie->description = desc;
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         printf("%d. floppy %d\n", menupos + i, i+1);
144     }
145     return Drives.floppycount;
146 }
147
148 // Show menu items from BCV list.
149 static int
150 menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
151 {
152     int i;
153     for (i = 0; i < IPL.bcvcount; i++) {
154         struct ipl_entry_s *ie = &IPL.bcv[i];
155         switch (ie->type) {
156         case IPL_TYPE_HARDDISK:
157             printf("%d. ata%d-%d %s\n", menupos + i
158                    , ie->vector / 2, ie->vector % 2, ie->description);
159             break;
160         default:
161             menu_show_default(ie, menupos+i);
162             break;
163         }
164     }
165     return IPL.bcvcount;
166 }
167
168 // Show cdrom menu item - but only if there exists a cdrom drive.
169 static int
170 menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
171 {
172     int i;
173     for (i = 0; i < Drives.cdcount; i++) {
174         int driveid = Drives.idmap[EXTTYPE_CD][i];
175         printf("%d. CD-Rom [ata%d-%d %s]\n", menupos + i
176                , driveid / 2, driveid % 2, Drives.drives[driveid].model);
177     }
178     return Drives.cdcount;
179 }
180
181 // Show coreboot-fs menu item.
182 static int
183 menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
184 {
185     int count = 0;
186     for (;;) {
187         const char *filename = cbfs_findNprefix("img/", count);
188         if (!filename)
189             break;
190         printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
191         count++;
192         if (count > 8)
193             break;
194     }
195     return count;
196 }
197
198 // Show IPL option menu.
199 static void
200 interactive_bootmenu()
201 {
202     if (! CONFIG_BOOTMENU)
203         return;
204
205     while (get_keystroke(0) >= 0)
206         ;
207
208     printf("Press F12 for boot menu.\n\n");
209
210     int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
211     if (scan_code != 0x86)
212         /* not F12 */
213         return;
214
215     while (get_keystroke(0) >= 0)
216         ;
217
218     printf("Select boot device:\n\n");
219
220     int subcount[ARRAY_SIZE(IPL.bev)];
221     int menupos = 1;
222     int i;
223     for (i = 0; i < IPL.bevcount; i++) {
224         struct ipl_entry_s *ie = &IPL.bev[i];
225         int sc = 1;
226         switch (ie->type) {
227         case IPL_TYPE_FLOPPY:
228             sc = menu_show_floppy(ie, menupos);
229             break;
230         case IPL_TYPE_HARDDISK:
231             sc = menu_show_harddisk(ie, menupos);
232             break;
233         case IPL_TYPE_CDROM:
234             sc = menu_show_cdrom(ie, menupos);
235             break;
236         case IPL_TYPE_CBFS:
237             sc = menu_show_cbfs(ie, menupos);
238             break;
239         default:
240             sc = menu_show_default(ie, menupos);
241             break;
242         }
243         subcount[i] = sc;
244         menupos += sc;
245     }
246
247     for (;;) {
248         scan_code = get_keystroke(1000);
249         if (scan_code == 0x01)
250             // ESC
251             break;
252         if (scan_code < 1 || scan_code > menupos)
253             continue;
254         int choice = scan_code - 1;
255
256         // Find out which IPL this was for.
257         int bev = 0;
258         while (choice > subcount[bev]) {
259             choice -= subcount[bev];
260             bev++;
261         }
262         IPL.bev[bev].subchoice = choice-1;
263
264         // Add user choice to the boot order.
265         IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
266         break;
267     }
268     printf("\n");
269 }
270
271 // Run the specified bcv.
272 static void
273 run_bcv(struct ipl_entry_s *ie)
274 {
275     switch (ie->type) {
276     case IPL_TYPE_HARDDISK:
277         map_hd_drive(ie->vector);
278         break;
279     case IPL_TYPE_BEV:
280         call_bcv(ie->vector >> 16, ie->vector & 0xffff);
281         break;
282     }
283 }
284
285 // Prepare for boot - show menu and run bcvs.
286 void
287 boot_prep()
288 {
289     if (! CONFIG_BOOT)
290         return;
291
292     // Allow user to modify BCV/IPL order.
293     interactive_bootmenu();
294
295     // Setup floppy boot order
296     int override = IPL.bev[0].subchoice;
297     int tmp = Drives.idmap[EXTTYPE_FLOPPY][0];
298     Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override];
299     Drives.idmap[EXTTYPE_FLOPPY][override] = tmp;
300
301     // Run BCVs
302     override = IPL.bev[1].subchoice;
303     if (override < IPL.bcvcount)
304         run_bcv(&IPL.bcv[override]);
305     int i;
306     for (i=0; i<IPL.bcvcount; i++)
307         if (i != override)
308             run_bcv(&IPL.bcv[i]);
309 }
310
311
312 /****************************************************************
313  * Boot code (int 18/19)
314  ****************************************************************/
315
316 // Jump to a bootup entry point.
317 static void
318 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
319 {
320     dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
321
322     struct bregs br;
323     memset(&br, 0, sizeof(br));
324     br.ip = bootip;
325     br.cs = bootseg;
326     // Set the magic number in ax and the boot drive in dl.
327     br.dl = bootdrv;
328     br.ax = 0xaa55;
329     call16(&br);
330 }
331
332 // Boot from a disk (either floppy or harddrive)
333 static void
334 boot_disk(u8 bootdrv, int checksig)
335 {
336     u16 bootseg = 0x07c0;
337
338     // Read sector
339     struct bregs br;
340     memset(&br, 0, sizeof(br));
341     br.dl = bootdrv;
342     br.es = bootseg;
343     br.ah = 2;
344     br.al = 1;
345     br.cl = 1;
346     call16_int(0x13, &br);
347
348     if (br.flags & F_CF) {
349         printf("Boot failed: could not read the boot disk\n\n");
350         return;
351     }
352
353     if (checksig) {
354         struct mbr_s *mbr = (void*)0;
355         if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
356             printf("Boot failed: not a bootable disk\n\n");
357             return;
358         }
359     }
360
361     /* Canonicalize bootseg:bootip */
362     u16 bootip = (bootseg & 0x0fff) << 4;
363     bootseg &= 0xf000;
364
365     call_boot_entry(bootseg, bootip, bootdrv);
366 }
367
368 // Boot from a CD-ROM
369 static void
370 boot_cdrom(struct ipl_entry_s *ie)
371 {
372     if (! CONFIG_CDROM_BOOT)
373         return;
374     int status = cdrom_boot(ie->subchoice);
375     if (status) {
376         printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
377         return;
378     }
379
380     u16 ebda_seg = get_ebda_seg();
381     u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
382     u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
383     /* Canonicalize bootseg:bootip */
384     u16 bootip = (bootseg & 0x0fff) << 4;
385     bootseg &= 0xf000;
386
387     call_boot_entry(bootseg, bootip, bootdrv);
388 }
389
390 // Boot from a CBFS payload
391 static void
392 boot_cbfs(struct ipl_entry_s *ie)
393 {
394     if (! CONFIG_COREBOOT_FLASH)
395         return;
396     const char *filename = cbfs_findNprefix("img/", ie->subchoice);
397     if (! filename)
398         return;
399     cbfs_run_payload(filename);
400 }
401
402 static void
403 do_boot(u16 seq_nr)
404 {
405     if (! CONFIG_BOOT)
406         panic("Boot support not compiled in.\n");
407
408     u32 bootdev = IPL.bootorder;
409     bootdev >>= 4 * seq_nr;
410     bootdev &= 0xf;
411
412     /* Translate bootdev to an IPL table offset by subtracting 1 */
413     bootdev -= 1;
414
415     if (bootdev >= IPL.bevcount) {
416         printf("No bootable device.\n");
417         // Loop with irqs enabled - this allows ctrl+alt+delete to work.
418         for (;;)
419             usleep(1000000);
420     }
421
422     /* Do the loading, and set up vector as a far pointer to the boot
423      * address, and bootdrv as the boot drive */
424     struct ipl_entry_s *ie = &IPL.bev[bootdev];
425     char desc[33];
426     printf("Booting from %s...\n"
427            , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
428
429     switch(ie->type) {
430     case IPL_TYPE_FLOPPY:
431         boot_disk(0x00, IPL.checkfloppysig);
432         break;
433     case IPL_TYPE_HARDDISK:
434         boot_disk(0x80, 1);
435         break;
436     case IPL_TYPE_CDROM:
437         boot_cdrom(ie);
438         break;
439     case IPL_TYPE_CBFS:
440         boot_cbfs(ie);
441         break;
442     case IPL_TYPE_BEV:
443         call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
444         break;
445     }
446
447     // Boot failed: invoke the boot recovery function
448     struct bregs br;
449     memset(&br, 0, sizeof(br));
450     call16_int(0x18, &br);
451 }
452
453 // Boot Failure recovery: try the next device.
454 void VISIBLE32
455 handle_18()
456 {
457     debug_serial_setup();
458     debug_enter(NULL, DEBUG_HDL_18);
459     u16 ebda_seg = get_ebda_seg();
460     u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
461     SET_EBDA2(ebda_seg, boot_sequence, seq);
462     do_boot(seq);
463 }
464
465 // INT 19h Boot Load Service Entry Point
466 void VISIBLE32
467 handle_19()
468 {
469     debug_serial_setup();
470     debug_enter(NULL, DEBUG_HDL_19);
471     SET_EBDA(boot_sequence, 0);
472     do_boot(0);
473 }