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