aboutsummaryrefslogtreecommitdiff
path: root/src/optionroms.c
blob: 69ba8b158bce06df2832fe74cc4658d29ad8f6f7 (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
// Option rom scanning code.
//
// 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 "bregs.h" // struct bregs
#include "farptr.h" // FLATPTR_TO_SEG
#include "config.h" // CONFIG_*
#include "util.h" // dprintf
#include "pci.h" // pci_find_class
#include "pci_regs.h" // PCI_ROM_ADDRESS
#include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
#include "boot.h" // IPL


/****************************************************************
 * Definitions
 ****************************************************************/

struct rom_header {
    u16 signature;
    u8 size;
    u8 initVector[4];
    u8 reserved[17];
    u16 pcioffset;
    u16 pnpoffset;
} PACKED;

struct pci_data {
    u32 signature;
    u16 vendor;
    u16 device;
    u16 vitaldata;
    u16 dlen;
    u8 drevision;
    u8 class_lo;
    u16 class_hi;
    u16 ilen;
    u16 irevision;
    u8 type;
    u8 indicator;
    u16 reserved;
} PACKED;

struct pnp_data {
    u32 signature;
    u8 revision;
    u8 len;
    u16 nextoffset;
    u8 reserved_08;
    u8 checksum;
    u32 devid;
    u16 manufacturer;
    u16 productname;
    u8 type_lo;
    u16 type_hi;
    u8 dev_flags;
    u16 bcv;
    u16 dv;
    u16 bev;
    u16 reserved_1c;
    u16 staticresource;
} PACKED;

#define OPTION_ROM_START 0xc0000
#define OPTION_ROM_SIGNATURE 0xaa55
#define OPTION_ROM_ALIGN 2048
#define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
#define PCI_ROM_SIGNATURE 0x52494350 // PCIR
#define PCIROM_CODETYPE_X86 0

// Next available position for an option rom.
static u32 next_rom;


/****************************************************************
 * Helper functions
 ****************************************************************/

// Execute a given option rom.
static void
callrom(struct rom_header *rom, u16 offset, u16 bdf)
{
    u16 seg = FLATPTR_TO_SEG(rom);
    dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);

    struct bregs br;
    memset(&br, 0, sizeof(br));
    br.ax = bdf;
    br.bx = 0xffff;
    br.dx = 0xffff;
    br.es = SEG_BIOS;
    br.di = get_pnp_offset();
    br.cs = seg;
    br.ip = offset;
    call16big(&br);

    debug_serial_setup();
}

// Execute a BCV option rom registered via add_bcv().
void
call_bcv(u16 seg, u16 ip)
{
    callrom(MAKE_FLATPTR(seg, 0), ip, 0);
}

// Verify that an option rom looks valid
static int
is_valid_rom(struct rom_header *rom)
{
    dprintf(6, "Checking rom %p (sig %x size %d)\n"
            , rom, rom->signature, rom->size);
    if (rom->signature != OPTION_ROM_SIGNATURE)
        return 0;
    if (! rom->size)
        return 0;
    u32 len = rom->size * 512;
    u8 sum = checksum(rom, len);
    if (sum != 0) {
        dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
                , rom, len, sum);
        return 0;
    }
    return 1;
}

// Check if a valid option rom has a pnp struct; return it if so.
static struct pnp_data *
get_pnp_rom(struct rom_header *rom)
{
    struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
    if (pnp->signature != PNP_SIGNATURE)
        return NULL;
    return pnp;
}

// Check for multiple pnp option rom headers.
static struct pnp_data *
get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
{
    if (! pnp->nextoffset)
        return NULL;
    pnp = (struct pnp_data *)((u8*)rom + pnp->nextoffset);
    if (pnp->signature != PNP_SIGNATURE)
        return NULL;
    return pnp;
}

// Check if a valid option rom has a pci struct; return it if so.
static struct pci_data *
get_pci_rom(struct rom_header *rom)
{
    struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
    if (pci->signature != PCI_ROM_SIGNATURE)
        return NULL;
    return pci;
}

// Copy a rom to its permanent location below 1MiB
static struct rom_header *
copy_rom(struct rom_header *rom)
{
    u32 romsize = rom->size * 512;
    if (next_rom + romsize > BUILD_BIOS_ADDR) {
        // Option rom doesn't fit.
        dprintf(1, "Option rom %p doesn't fit.\n", rom);
        return NULL;
    }
    dprintf(4, "Copying option rom (size %d) from %p to %x\n"
            , romsize, rom, next_rom);
    memcpy((void*)next_rom, rom, romsize);
    return (struct rom_header *)next_rom;
}

// Check if an option rom is at a hardcoded location for a device.
static struct rom_header *
lookup_hardcode(u32 vendev)
{
    if (OPTIONROM_VENDEV_1
        && ((OPTIONROM_VENDEV_1 >> 16)
            | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
        return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
    if (OPTIONROM_VENDEV_2
        && ((OPTIONROM_VENDEV_2 >> 16)
            | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
        return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
    int ret = cbfs_copy_optionrom((void*)next_rom, BUILD_BIOS_ADDR - next_rom
                                  , vendev);
    if (ret < 0)
        return NULL;
    return (struct rom_header *)next_rom;
}

// Map the option rom of a given PCI device.
static struct rom_header *
map_optionrom(u16 bdf, u32 vendev)
{
    dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
            , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));

    u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
    if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
        dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
        return NULL;
    }

    u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
    pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
    u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);

    dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
    orig &= ~PCI_ROM_ADDRESS_ENABLE;
    if (!sz || sz == 0xffffffff)
        goto fail;

    if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
        // Don't try to map to a pci addresses at its max, in the last
        // 4MiB of ram, or the first 16MiB of ram.
        dprintf(6, "Preset rom address doesn't look valid\n");
        goto fail;
    }

    // Looks like a rom - enable it.
    pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);

    struct rom_header *rom = (struct rom_header *)orig;
    for (;;) {
        dprintf(5, "Inspecting possible rom at %p (dv=%x bdf=%x)\n"
                , rom, vendev, bdf);
        if (rom->signature != OPTION_ROM_SIGNATURE) {
            dprintf(6, "No option rom signature (got %x)\n", rom->signature);
            goto fail;
        }
        struct pci_data *pci = get_pci_rom(rom);
        if (! pci) {
            dprintf(6, "No valid pci signature found\n");
            goto fail;
        }

        u32 vd = (pci->device << 16) | pci->vendor;
        if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
            // A match
            break;
        dprintf(6, "Didn't match dev/ven (got %x) or type (got %d)\n"
                , vd, pci->type);
        if (pci->indicator & 0x80) {
            dprintf(6, "No more images left\n");
            goto fail;
        }
        rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
    }

    rom = copy_rom(rom);
    pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
    return rom;
fail:
    // Not valid - restore original and exit.
    pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
    return NULL;
}

// Adjust for the size of an option rom; allow it to resize.
static struct rom_header *
verifysize_optionrom(struct rom_header *rom, u16 bdf)
{
    if (! is_valid_rom(rom))
        return NULL;

    if (get_pnp_rom(rom))
        // Init the PnP rom.
        callrom(rom, OPTION_ROM_INITVECTOR, bdf);

    next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);

    return rom;
}

// Attempt to map and initialize the option rom on a given PCI device.
static struct rom_header *
init_optionrom(u16 bdf)
{
    u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
    dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %x)\n"
            , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
            , vendev);
    struct rom_header *rom = lookup_hardcode(vendev);
    if (! rom)
        rom = map_optionrom(bdf, vendev);
    if (! rom)
        // No ROM present.
        return NULL;
    return verifysize_optionrom(rom, bdf);
}


/****************************************************************
 * Non-VGA option rom init
 ****************************************************************/

void
optionrom_setup()
{
    if (! CONFIG_OPTIONROMS)
        return;

    dprintf(1, "Scan for option roms\n");

    u32 post_vga = next_rom;

    if (CONFIG_OPTIONROMS_DEPLOYED) {
        // Option roms are already deployed on the system.
        u32 pos = next_rom;
        while (pos < BUILD_BIOS_ADDR) {
            struct rom_header *rom = (struct rom_header *)pos;
            if (! is_valid_rom(rom)) {
                pos += OPTION_ROM_ALIGN;
                continue;
            }
            if (get_pnp_rom(rom))
                callrom(rom, OPTION_ROM_INITVECTOR, 0);
            pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
            next_rom = pos;
        }
    } else {
        // Find and deploy PCI roms.
        int bdf, max;
        foreachpci(bdf, max) {
            u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
            if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
                || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
                continue;
            init_optionrom(bdf);
        }

        // Find and deploy CBFS roms not associated with a device.
        struct cbfs_file *tmp = NULL;
        for (;;) {
            tmp = cbfs_copyfile_prefix(
                (void*)next_rom, BUILD_BIOS_ADDR - next_rom, "genroms/", tmp);
            if (!tmp)
                break;
            verifysize_optionrom((void*)next_rom, 0);
        }
    }

    // All option roms found and deployed - now build BEV/BCV vectors.

    u32 pos = post_vga;
    while (pos < next_rom) {
        struct rom_header *rom = (struct rom_header *)pos;
        if (! is_valid_rom(rom)) {
            pos += OPTION_ROM_ALIGN;
            continue;
        }
        pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
        struct pnp_data *pnp = get_pnp_rom(rom);
        if (! pnp) {
            // Legacy rom.
            add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
            continue;
        }
        // PnP rom.
        if (pnp->bev)
            // Can boot system - add to IPL list.
            add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
        else
            // Check for BCV (there may be multiple).
            while (pnp && pnp->bcv) {
                add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
                pnp = get_pnp_next(rom, pnp);
            }
    }
}


/****************************************************************
 * VGA init
 ****************************************************************/

// Call into vga code to turn on console.
void
vga_setup()
{
    if (! CONFIG_OPTIONROMS)
        return;

    dprintf(1, "Scan for VGA option rom\n");
    next_rom = OPTION_ROM_START;

    if (CONFIG_OPTIONROMS_DEPLOYED) {
        // Option roms are already deployed on the system.
        struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
        if (! is_valid_rom(rom))
            return;
        callrom(rom, OPTION_ROM_INITVECTOR, 0);
        next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
    } else {
        // Find and deploy PCI VGA rom.
        int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
        if (bdf < 0)
            // Device not found
            return;

        struct rom_header *rom = init_optionrom(bdf);
        if (rom && !get_pnp_rom(rom))
            // Call rom even if it isn't a pnp rom.
            callrom(rom, OPTION_ROM_INITVECTOR, bdf);

        // Find and deploy CBFS vga-style roms not associated with a device.
        struct cbfs_file *tmp = NULL;
        for (;;) {
            tmp = cbfs_copyfile_prefix(
                (void*)next_rom, BUILD_BIOS_ADDR - next_rom, "vgaroms/", tmp);
            if (!tmp)
                break;
            rom = verifysize_optionrom((void*)next_rom, 0);
            if (rom && !get_pnp_rom(rom))
                // Call rom even if it isn't a pnp rom.
                callrom(rom, OPTION_ROM_INITVECTOR, bdf);
        }
    }

    dprintf(1, "Turning on vga console\n");
    struct bregs br;
    memset(&br, 0, sizeof(br));
    br.ax = 0x0003;
    call16_int(0x10, &br);

    // Write to screen.
    printf("Starting SeaBIOS\n\n");
}