aboutsummaryrefslogtreecommitdiff
path: root/src/boot.c
blob: 26427ba51f8e46e6988eb3a486449d9ee861d219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Code to load disk image and start system boot.
//
// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2002  MandrakeSoft S.A.
//
// This file may be distributed under the terms of the GNU LGPLv3 license.

#include "util.h" // irq_enable
#include "biosvar.h" // GET_EBDA
#include "config.h" // CONFIG_*
#include "disk.h" // cdrom_boot
#include "bregs.h" // struct bregs
#include "boot.h" // struct ipl_s
#include "cmos.h" // inb_cmos

struct ipl_s IPL;


/****************************************************************
 * IPL and BCV handlers
 ****************************************************************/

void
boot_setup()
{
    if (! CONFIG_BOOT)
        return;
    dprintf(3, "init boot device ordering\n");

    memset(&IPL, 0, sizeof(IPL));

    // Floppy drive
    struct ipl_entry_s *ie = &IPL.bev[0];
    ie->type = IPL_TYPE_FLOPPY;
    ie->description = "Floppy";
    ie++;

    // First HDD
    ie->type = IPL_TYPE_HARDDISK;
    ie->description = "Hard Disk";
    ie++;

    // CDROM
    if (CONFIG_CDROM_BOOT) {
        ie->type = IPL_TYPE_CDROM;
        ie->description = "CD-Rom";
        ie++;
    }

    if (CONFIG_COREBOOT_FLASH) {
        ie->type = IPL_TYPE_CBFS;
        ie->description = "CBFS";
        ie++;
    }

    IPL.bevcount = ie - IPL.bev;
    SET_EBDA(boot_sequence, 0xffff);
    if (CONFIG_COREBOOT) {
        // XXX - hardcode defaults for coreboot.
        IPL.bootorder = 0x87654231;
        IPL.checkfloppysig = 1;
    } else {
        // On emulators, get boot order from nvram.
        IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
                         | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
        if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
            IPL.checkfloppysig = 1;
    }
}

// Add a BEV vector for a given pnp compatible option rom.
void
add_bev(u16 seg, u16 bev, u16 desc)
{
    if (! CONFIG_BOOT)
        return;
    if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
        return;

    struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
    ie->type = IPL_TYPE_BEV;
    ie->vector = (seg << 16) | bev;
    const char *d = "Unknown";
    if (desc)
        d = MAKE_FLATPTR(seg, desc);
    ie->description = d;
}

// Add a bcv entry for an expansion card harddrive or legacy option rom
void
add_bcv(u16 seg, u16 ip, u16 desc)
{
    if (! CONFIG_BOOT)
        return;
    if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
        return;

    struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
    ie->type = IPL_TYPE_BEV;
    ie->vector = (seg << 16) | ip;
    const char *d = "Legacy option rom";
    if (desc)
        d = MAKE_FLATPTR(seg, desc);
    ie->description = d;
}

// Add a bcv entry for an ata harddrive
void
add_bcv_hd(int driveid, const char *desc)
{
    if (! CONFIG_BOOT)
        return;
    if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
        return;

    struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
    ie->type = IPL_TYPE_HARDDISK;
    ie->vector = driveid;
    ie->description = desc;
}


/****************************************************************
 * Boot menu and BCV execution
 ****************************************************************/

// Show a generic menu item
static int
menu_show_default(struct ipl_entry_s *ie, int menupos)
{
    char desc[33];
    printf("%d. %s\n", menupos
           , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
    return 1;
}

// Show floppy menu item - but only if there exists a floppy drive.
static int
menu_show_floppy(struct ipl_entry_s *ie, int menupos)
{
    if (!FloppyCount)
        return 0;
    return menu_show_default(ie, menupos);
}

// Show menu items from BCV list.
static int
menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
{
    int i;
    for (i = 0; i < IPL.bcvcount; i++) {
        struct ipl_entry_s *ie = &IPL.bcv[i];
        switch (ie->type) {
        case IPL_TYPE_HARDDISK:
            printf("%d. ata%d-%d %s\n", menupos + i
                   , ie->vector / 2, ie->vector % 2, ie->description);
            break;
        default:
            menu_show_default(ie, menupos+i);
            break;
        }
    }
    return IPL.bcvcount;
}

// Show cdrom menu item - but only if there exists a cdrom drive.
static int
menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
{
    int i;
    for (i = 0; i < ATA.cdcount; i++) {
        int driveid = ATA.idmap[1][i];
        printf("%d. CD-Rom [ata%d-%d %s]\n", menupos + i
               , driveid / 2, driveid % 2, ATA.devices[driveid].model);
    }
    return ATA.cdcount;
}

// Show coreboot-fs menu item.
static int
menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
{
    int count = 0;
    for (;;) {
        const char *filename = cbfs_findNprefix("img/", count);
        if (!filename)
            break;
        printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
        count++;
        if (count > 8)
            break;
    }
    return count;
}

// Show IPL option menu.
static void
interactive_bootmenu()
{
    if (! CONFIG_BOOTMENU)
        return;

    while (get_keystroke(0) >= 0)
        ;

    printf("Press F12 for boot menu.\n\n");

    int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
    if (scan_code != 0x86)
        /* not F12 */
        return;

    while (get_keystroke(0) >= 0)
        ;

    printf("Select boot device:\n\n");

    int subcount[ARRAY_SIZE(IPL.bev)];
    int menupos = 1;
    int i;
    for (i = 0; i < IPL.bevcount; i++) {
        struct ipl_entry_s *ie = &IPL.bev[i];
        int sc = 1;
        switch (ie->type) {
        case IPL_TYPE_FLOPPY:
            sc = menu_show_floppy(ie, menupos);
            break;
        case IPL_TYPE_HARDDISK:
            sc = menu_show_harddisk(ie, menupos);
            break;
        case IPL_TYPE_CDROM:
            sc = menu_show_cdrom(ie, menupos);
            break;
        case IPL_TYPE_CBFS:
            sc = menu_show_cbfs(ie, menupos);
            break;
        default:
            sc = menu_show_default(ie, menupos);
            break;
        }
        subcount[i] = sc;
        menupos += sc;
    }

    for (;;) {
        scan_code = get_keystroke(1000);
        if (scan_code == 0x01)
            // ESC
            break;
        if (scan_code < 1 || scan_code > menupos)
            continue;
        int choice = scan_code - 1;

        // Find out which IPL this was for.
        int bev = 0;
        while (choice > subcount[bev]) {
            choice -= subcount[bev];
            bev++;
        }
        IPL.bev[bev].subchoice = choice-1;

        // Add user choice to the boot order.
        IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
        break;
    }
    printf("\n");
}

// Run the specified bcv.
static void
run_bcv(struct ipl_entry_s *ie)
{
    switch (ie->type) {
    case IPL_TYPE_HARDDISK:
        map_drive(ie->vector);
        break;
    case IPL_TYPE_BEV:
        call_bcv(ie->vector >> 16, ie->vector & 0xffff);
        break;
    }
}

// Prepare for boot - show menu and run bcvs.
void
boot_prep()
{
    if (! CONFIG_BOOT)
        return;

    // Allow user to modify BCV/IPL order.
    interactive_bootmenu();

    // Run BCVs
    int override = IPL.bev[1].subchoice;
    if (override < IPL.bcvcount)
        run_bcv(&IPL.bcv[override]);
    int i;
    for (i=0; i<IPL.bcvcount; i++)
        if (i != override)
            run_bcv(&IPL.bcv[i]);
}


/****************************************************************
 * Boot code (int 18/19)
 ****************************************************************/

// Jump to a bootup entry point.
static void
call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
{
    dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);

    struct bregs br;
    memset(&br, 0, sizeof(br));
    br.ip = bootip;
    br.cs = bootseg;
    // Set the magic number in ax and the boot drive in dl.
    br.dl = bootdrv;
    br.ax = 0xaa55;
    call16(&br);
}

// Boot from a disk (either floppy or harddrive)
static void
boot_disk(u8 bootdrv, int checksig)
{
    u16 bootseg = 0x07c0;

    // Read sector
    struct bregs br;
    memset(&br, 0, sizeof(br));
    br.dl = bootdrv;
    br.es = bootseg;
    br.ah = 2;
    br.al = 1;
    br.cl = 1;
    call16_int(0x13, &br);

    if (br.flags & F_CF) {
        printf("Boot failed: could not read the boot disk\n\n");
        return;
    }

    if (checksig) {
        struct mbr_s *mbr = (void*)0;
        if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
            printf("Boot failed: not a bootable disk\n\n");
            return;
        }
    }

    /* Canonicalize bootseg:bootip */
    u16 bootip = (bootseg & 0x0fff) << 4;
    bootseg &= 0xf000;

    call_boot_entry(bootseg, bootip, bootdrv);
}

// Boot from a CD-ROM
static void
boot_cdrom(struct ipl_entry_s *ie)
{
    if (! CONFIG_CDROM_BOOT)
        return;
    int status = cdrom_boot(ie->subchoice);
    if (status) {
        printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
        return;
    }

    u16 ebda_seg = get_ebda_seg();
    u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
    u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
    /* Canonicalize bootseg:bootip */
    u16 bootip = (bootseg & 0x0fff) << 4;
    bootseg &= 0xf000;

    call_boot_entry(bootseg, bootip, bootdrv);
}

// Boot from a CD-ROM
static void
boot_cbfs(struct ipl_entry_s *ie)
{
    if (! CONFIG_COREBOOT_FLASH)
        return;
    const char *filename = cbfs_findNprefix("img/", ie->subchoice);
    if (! filename)
        return;
    cbfs_run_payload(filename);
}

static void
do_boot(u16 seq_nr)
{
    if (! CONFIG_BOOT)
        panic("Boot support not compiled in.\n");

    u32 bootdev = IPL.bootorder;
    bootdev >>= 4 * seq_nr;
    bootdev &= 0xf;

    /* Translate bootdev to an IPL table offset by subtracting 1 */
    bootdev -= 1;

    if (bootdev >= IPL.bevcount) {
        printf("No bootable device.\n");
        // Loop with irqs enabled - this allows ctrl+alt+delete to work.
        for (;;)
            usleep(1000000);
    }

    /* Do the loading, and set up vector as a far pointer to the boot
     * address, and bootdrv as the boot drive */
    struct ipl_entry_s *ie = &IPL.bev[bootdev];
    char desc[33];
    printf("Booting from %s...\n"
           , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));

    switch(ie->type) {
    case IPL_TYPE_FLOPPY:
        boot_disk(0x00, IPL.checkfloppysig);
        break;
    case IPL_TYPE_HARDDISK:
        boot_disk(0x80, 1);
        break;
    case IPL_TYPE_CDROM:
        boot_cdrom(ie);
        break;
    case IPL_TYPE_CBFS:
        boot_cbfs(ie);
        break;
    case IPL_TYPE_BEV:
        call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
        break;
    }

    // Boot failed: invoke the boot recovery function
    struct bregs br;
    memset(&br, 0, sizeof(br));
    call16_int(0x18, &br);
}

// Boot Failure recovery: try the next device.
void VISIBLE32
handle_18()
{
    debug_serial_setup();
    debug_enter(NULL, DEBUG_HDL_18);
    u16 ebda_seg = get_ebda_seg();
    u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
    SET_EBDA2(ebda_seg, boot_sequence, seq);
    do_boot(seq);
}

// INT 19h Boot Load Service Entry Point
void VISIBLE32
handle_19()
{
    debug_serial_setup();
    debug_enter(NULL, DEBUG_HDL_19);
    SET_EBDA(boot_sequence, 0);
    do_boot(0);
}