~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/seabios/src/optionroms.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Option rom scanning code.
 
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 "bregs.h" // struct bregs
 
9
#include "config.h" // CONFIG_*
 
10
#include "farptr.h" // FLATPTR_TO_SEG
 
11
#include "hw/pci.h" // pci_config_readl
 
12
#include "hw/pcidevice.h" // foreachpci
 
13
#include "hw/pci_ids.h" // PCI_CLASS_DISPLAY_VGA
 
14
#include "hw/pci_regs.h" // PCI_ROM_ADDRESS
 
15
#include "malloc.h" // rom_confirm
 
16
#include "output.h" // dprintf
 
17
#include "romfile.h" // romfile_loadint
 
18
#include "stacks.h" // farcall16big
 
19
#include "std/optionrom.h" // struct rom_header
 
20
#include "std/pnpbios.h" // PNP_SIGNATURE
 
21
#include "string.h" // memset
 
22
#include "util.h" // get_pnp_offset
 
23
#include "tcgbios.h" // tpm_*
 
24
 
 
25
static int EnforceChecksum, S3ResumeVga, RunPCIroms;
 
26
 
 
27
 
 
28
/****************************************************************
 
29
 * Helper functions
 
30
 ****************************************************************/
 
31
 
 
32
// Execute a given option rom.
 
33
static void
 
34
__callrom(struct rom_header *rom, u16 offset, u16 bdf)
 
35
{
 
36
    u16 seg = FLATPTR_TO_SEG(rom);
 
37
    dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
 
38
 
 
39
    struct bregs br;
 
40
    memset(&br, 0, sizeof(br));
 
41
    br.flags = F_IF;
 
42
    br.ax = bdf;
 
43
    br.bx = 0xffff;
 
44
    br.dx = 0xffff;
 
45
    br.es = SEG_BIOS;
 
46
    br.di = get_pnp_offset();
 
47
    br.code = SEGOFF(seg, offset);
 
48
    start_preempt();
 
49
    farcall16big(&br);
 
50
    finish_preempt();
 
51
}
 
52
 
 
53
// Execute a given option rom at the standard entry vector.
 
54
void
 
55
callrom(struct rom_header *rom, u16 bdf)
 
56
{
 
57
    __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
 
58
}
 
59
 
 
60
// Execute a BCV option rom registered via add_bcv().
 
61
void
 
62
call_bcv(u16 seg, u16 ip)
 
63
{
 
64
    __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
 
65
}
 
66
 
 
67
// Verify that an option rom looks valid
 
68
static int
 
69
is_valid_rom(struct rom_header *rom)
 
70
{
 
71
    dprintf(6, "Checking rom %p (sig %x size %d)\n"
 
72
            , rom, rom->signature, rom->size);
 
73
    if (rom->signature != OPTION_ROM_SIGNATURE)
 
74
        return 0;
 
75
    if (! rom->size)
 
76
        return 0;
 
77
    u32 len = rom->size * 512;
 
78
    u8 sum = checksum(rom, len);
 
79
    if (sum != 0) {
 
80
        dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
 
81
                , rom, len, sum);
 
82
        if (EnforceChecksum)
 
83
            return 0;
 
84
    }
 
85
    return 1;
 
86
}
 
87
 
 
88
// Check if a valid option rom has a pnp struct; return it if so.
 
89
static struct pnp_data *
 
90
get_pnp_rom(struct rom_header *rom)
 
91
{
 
92
    struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
 
93
    if (pnp->signature != PNP_SIGNATURE)
 
94
        return NULL;
 
95
    return pnp;
 
96
}
 
97
 
 
98
// Check for multiple pnp option rom headers.
 
99
static struct pnp_data *
 
100
get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
 
101
{
 
102
    if (! pnp->nextoffset)
 
103
        return NULL;
 
104
    pnp = (void*)((u8*)rom + pnp->nextoffset);
 
105
    if (pnp->signature != PNP_SIGNATURE)
 
106
        return NULL;
 
107
    return pnp;
 
108
}
 
109
 
 
110
// Check if a valid option rom has a pci struct; return it if so.
 
111
static struct pci_data *
 
112
get_pci_rom(struct rom_header *rom)
 
113
{
 
114
    struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
 
115
    if (pd->signature != PCI_ROM_SIGNATURE)
 
116
        return NULL;
 
117
    if (rom->pcioffset & 3)
 
118
        dprintf(1, "WARNING! Found unaligned PCI rom (vd=%04x:%04x)\n"
 
119
                , pd->vendor, pd->device);
 
120
    return pd;
 
121
}
 
122
 
 
123
// Run rom init code and note rom size.
 
124
static int
 
125
init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
 
126
{
 
127
    if (! is_valid_rom(rom))
 
128
        return -1;
 
129
    struct rom_header *newrom = rom_reserve(rom->size * 512);
 
130
    if (!newrom) {
 
131
        warn_noalloc();
 
132
        return -1;
 
133
    }
 
134
    if (newrom != rom)
 
135
        memmove(newrom, rom, rom->size * 512);
 
136
 
 
137
    tpm_option_rom(newrom, rom->size * 512);
 
138
 
 
139
    if (isvga || get_pnp_rom(newrom))
 
140
        // Only init vga and PnP roms here.
 
141
        callrom(newrom, bdf);
 
142
 
 
143
    return rom_confirm(newrom->size * 512);
 
144
}
 
145
 
 
146
#define RS_PCIROM (1LL<<33)
 
147
 
 
148
static void
 
149
setRomSource(u64 *sources, struct rom_header *rom, u64 source)
 
150
{
 
151
    if (sources)
 
152
        sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
 
153
}
 
154
 
 
155
static int
 
156
getRomPriority(u64 *sources, struct rom_header *rom, int instance)
 
157
{
 
158
    u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
 
159
    if (!source)
 
160
        return -1;
 
161
    if (source & RS_PCIROM)
 
162
        return bootprio_find_pci_rom((void*)(u32)source, instance);
 
163
    struct romfile_s *file = (void*)(u32)source;
 
164
    return bootprio_find_named_rom(file->name, instance);
 
165
}
 
166
 
 
167
 
 
168
/****************************************************************
 
169
 * Roms in CBFS
 
170
 ****************************************************************/
 
171
 
 
172
static struct rom_header *
 
173
deploy_romfile(struct romfile_s *file)
 
174
{
 
175
    u32 size = file->size;
 
176
    struct rom_header *rom = rom_reserve(size);
 
177
    if (!rom) {
 
178
        warn_noalloc();
 
179
        return NULL;
 
180
    }
 
181
    int ret = file->copy(file, rom, size);
 
182
    if (ret <= 0)
 
183
        return NULL;
 
184
    return rom;
 
185
}
 
186
 
 
187
// Run all roms in a given CBFS directory.
 
188
static void
 
189
run_file_roms(const char *prefix, int isvga, u64 *sources)
 
190
{
 
191
    struct romfile_s *file = NULL;
 
192
    for (;;) {
 
193
        file = romfile_findprefix(prefix, file);
 
194
        if (!file)
 
195
            break;
 
196
        struct rom_header *rom = deploy_romfile(file);
 
197
        if (rom) {
 
198
            setRomSource(sources, rom, (u32)file);
 
199
            init_optionrom(rom, 0, isvga);
 
200
        }
 
201
    }
 
202
}
 
203
 
 
204
 
 
205
/****************************************************************
 
206
 * PCI roms
 
207
 ****************************************************************/
 
208
 
 
209
// Verify device is a vga device with legacy address decoding enabled.
 
210
int
 
211
is_pci_vga(struct pci_device *pci)
 
212
{
 
213
    if (pci->class != PCI_CLASS_DISPLAY_VGA)
 
214
        return 0;
 
215
    u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
 
216
    if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
 
217
        return 0;
 
218
    while (pci->parent) {
 
219
        pci = pci->parent;
 
220
        u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
 
221
        if (!(ctrl & PCI_BRIDGE_CTL_VGA))
 
222
            return 0;
 
223
    }
 
224
    return 1;
 
225
}
 
226
 
 
227
// Copy a rom to its permanent location below 1MiB
 
228
static struct rom_header *
 
229
copy_rom(struct rom_header *rom)
 
230
{
 
231
    u32 romsize = rom->size * 512;
 
232
    struct rom_header *newrom = rom_reserve(romsize);
 
233
    if (!newrom) {
 
234
        warn_noalloc();
 
235
        return NULL;
 
236
    }
 
237
    dprintf(4, "Copying option rom (size %d) from %p to %p\n"
 
238
            , romsize, rom, newrom);
 
239
    iomemcpy(newrom, rom, romsize);
 
240
    return newrom;
 
241
}
 
242
 
 
243
// Map the option rom of a given PCI device.
 
244
static struct rom_header *
 
245
map_pcirom(struct pci_device *pci)
 
246
{
 
247
    dprintf(6, "Attempting to map option rom on dev %pP\n", pci);
 
248
 
 
249
    if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
 
250
        dprintf(6, "Skipping non-normal pci device (type=%x)\n"
 
251
                , pci->header_type);
 
252
        return NULL;
 
253
    }
 
254
 
 
255
    u16 bdf = pci->bdf;
 
256
    u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
 
257
    pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
 
258
    u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
 
259
 
 
260
    dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
 
261
    orig &= ~PCI_ROM_ADDRESS_ENABLE;
 
262
    if (!sz || sz == 0xffffffff)
 
263
        goto fail;
 
264
 
 
265
    if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
 
266
        // Don't try to map to a pci addresses at its max, in the last
 
267
        // 4MiB of ram, or the first 16MiB of ram.
 
268
        dprintf(6, "Preset rom address doesn't look valid\n");
 
269
        goto fail;
 
270
    }
 
271
 
 
272
    // Looks like a rom - enable it.
 
273
    pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
 
274
 
 
275
    struct rom_header *rom = (void*)orig;
 
276
    for (;;) {
 
277
        dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x bdf=%pP)\n"
 
278
                , rom, pci->vendor, pci->device, pci);
 
279
        if (rom->signature != OPTION_ROM_SIGNATURE) {
 
280
            dprintf(6, "No option rom signature (got %x)\n", rom->signature);
 
281
            goto fail;
 
282
        }
 
283
        struct pci_data *pd = get_pci_rom(rom);
 
284
        if (! pd) {
 
285
            dprintf(6, "No valid pci signature found\n");
 
286
            goto fail;
 
287
        }
 
288
 
 
289
        if (pd->vendor == pci->vendor && pd->device == pci->device
 
290
            && pd->type == PCIROM_CODETYPE_X86)
 
291
            // A match
 
292
            break;
 
293
        dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
 
294
                , pd->vendor, pd->device, pd->type);
 
295
        if (pd->indicator & 0x80) {
 
296
            dprintf(6, "No more images left\n");
 
297
            goto fail;
 
298
        }
 
299
        rom = (void*)((u32)rom + pd->ilen * 512);
 
300
    }
 
301
 
 
302
    rom = copy_rom(rom);
 
303
    pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
 
304
    return rom;
 
305
fail:
 
306
    // Not valid - restore original and exit.
 
307
    pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
 
308
    return NULL;
 
309
}
 
310
 
 
311
// Attempt to map and initialize the option rom on a given PCI device.
 
312
static void
 
313
init_pcirom(struct pci_device *pci, int isvga, u64 *sources)
 
314
{
 
315
    dprintf(4, "Attempting to init PCI bdf %pP (vd %04x:%04x)\n"
 
316
            , pci, pci->vendor, pci->device);
 
317
 
 
318
    char fname[17];
 
319
    snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
 
320
             , pci->vendor, pci->device);
 
321
    struct romfile_s *file = romfile_find(fname);
 
322
    struct rom_header *rom = NULL;
 
323
    if (file)
 
324
        rom = deploy_romfile(file);
 
325
    else if (RunPCIroms > 1 || (RunPCIroms == 1 && isvga))
 
326
        rom = map_pcirom(pci);
 
327
    if (! rom)
 
328
        // No ROM present.
 
329
        return;
 
330
    setRomSource(sources, rom, RS_PCIROM | (u32)pci);
 
331
    init_optionrom(rom, pci->bdf, isvga);
 
332
}
 
333
 
 
334
 
 
335
/****************************************************************
 
336
 * Non-VGA option rom init
 
337
 ****************************************************************/
 
338
 
 
339
void
 
340
optionrom_setup(void)
 
341
{
 
342
    if (! CONFIG_OPTIONROMS)
 
343
        return;
 
344
 
 
345
    dprintf(1, "Scan for option roms\n");
 
346
    u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
 
347
    memset(sources, 0, sizeof(sources));
 
348
    u32 post_vga = rom_get_last();
 
349
 
 
350
    // Find and deploy PCI roms.
 
351
    struct pci_device *pci;
 
352
    foreachpci(pci) {
 
353
        if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver)
 
354
            continue;
 
355
        init_pcirom(pci, 0, sources);
 
356
    }
 
357
 
 
358
    // Find and deploy CBFS roms not associated with a device.
 
359
    run_file_roms("genroms/", 0, sources);
 
360
    rom_reserve(0);
 
361
 
 
362
    // All option roms found and deployed - now build BEV/BCV vectors.
 
363
 
 
364
    u32 pos = post_vga;
 
365
    while (pos < rom_get_last()) {
 
366
        struct rom_header *rom = (void*)pos;
 
367
        if (! is_valid_rom(rom)) {
 
368
            pos += OPTION_ROM_ALIGN;
 
369
            continue;
 
370
        }
 
371
        pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
 
372
        struct pnp_data *pnp = get_pnp_rom(rom);
 
373
        if (! pnp) {
 
374
            // Legacy rom.
 
375
            boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
 
376
                         , getRomPriority(sources, rom, 0));
 
377
            continue;
 
378
        }
 
379
        // PnP rom - check for BEV and BCV boot capabilities.
 
380
        int instance = 0;
 
381
        while (pnp) {
 
382
            if (pnp->bev)
 
383
                boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
 
384
                             , getRomPriority(sources, rom, instance++));
 
385
            else if (pnp->bcv)
 
386
                boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
 
387
                             , getRomPriority(sources, rom, instance++));
 
388
            else
 
389
                break;
 
390
            pnp = get_pnp_next(rom, pnp);
 
391
        }
 
392
    }
 
393
}
 
394
 
 
395
 
 
396
/****************************************************************
 
397
 * VGA init
 
398
 ****************************************************************/
 
399
 
 
400
int ScreenAndDebug;
 
401
struct rom_header *VgaROM;
 
402
 
 
403
// Call into vga code to turn on console.
 
404
void
 
405
vgarom_setup(void)
 
406
{
 
407
    if (! CONFIG_OPTIONROMS)
 
408
        return;
 
409
 
 
410
    dprintf(1, "Scan for VGA option rom\n");
 
411
 
 
412
    // Load some config settings that impact VGA.
 
413
    EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
 
414
    S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", CONFIG_QEMU);
 
415
    RunPCIroms = romfile_loadint("etc/pci-optionrom-exec", 2);
 
416
    ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
 
417
 
 
418
    // Clear option rom memory
 
419
    memset((void*)BUILD_ROM_START, 0, rom_get_max() - BUILD_ROM_START);
 
420
 
 
421
    // Find and deploy PCI VGA rom.
 
422
    struct pci_device *pci;
 
423
    foreachpci(pci) {
 
424
        if (!is_pci_vga(pci))
 
425
            continue;
 
426
        vgahook_setup(pci);
 
427
        init_pcirom(pci, 1, NULL);
 
428
        break;
 
429
    }
 
430
 
 
431
    // Find and deploy CBFS vga-style roms not associated with a device.
 
432
    run_file_roms("vgaroms/", 1, NULL);
 
433
    rom_reserve(0);
 
434
 
 
435
    if (rom_get_last() == BUILD_ROM_START)
 
436
        // No VGA rom found
 
437
        return;
 
438
 
 
439
    VgaROM = (void*)BUILD_ROM_START;
 
440
    enable_vga_console();
 
441
}
 
442
 
 
443
void
 
444
s3_resume_vga(void)
 
445
{
 
446
    if (!S3ResumeVga)
 
447
        return;
 
448
    if (!VgaROM || ! is_valid_rom(VgaROM))
 
449
        return;
 
450
    callrom(VgaROM, 0);
 
451
}