blob: c3aa9b0812b46fd519f2c7c0185150618ea6b2ab [file] [log] [blame]
Kevin O'Connor714325c2008-11-01 20:32:27 -04001// Option rom scanning code.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor714325c2008-11-01 20:32:27 -04007
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04008#include "bregs.h" // struct bregs
9#include "config.h" // CONFIG_*
10#include "farptr.h" // FLATPTR_TO_SEG
Gerd Hoffmann0932c202018-11-20 08:06:55 +010011#include "biosvar.h" // GET_IVT
Kevin O'Connor4d8510c2016-02-03 01:28:20 -050012#include "hw/pci.h" // pci_config_readl
13#include "hw/pcidevice.h" // foreachpci
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "hw/pci_ids.h" // PCI_CLASS_DISPLAY_VGA
15#include "hw/pci_regs.h" // PCI_ROM_ADDRESS
Kevin O'Connor9dea5902013-09-14 20:23:54 -040016#include "malloc.h" // rom_confirm
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040017#include "output.h" // dprintf
Kevin O'Connor41639f82013-09-14 19:37:36 -040018#include "romfile.h" // romfile_loadint
Kevin O'Connor3df600b2013-09-14 19:28:55 -040019#include "stacks.h" // farcall16big
Kevin O'Connor8fb3a5e2013-09-14 22:27:14 -040020#include "std/optionrom.h" // struct rom_header
Kevin O'Connor4f790aa2013-09-14 23:04:08 -040021#include "std/pnpbios.h" // PNP_SIGNATURE
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040022#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040023#include "util.h" // get_pnp_offset
Stefan Berger2aff1c12015-05-26 15:48:33 -040024#include "tcgbios.h" // tpm_*
Kevin O'Connorceea03c2008-11-08 21:36:35 -050025
tpearson@raptorengineeringinc.comd23eba62015-02-17 15:04:17 -060026static int EnforceChecksum, S3ResumeVga, RunPCIroms;
27
Kevin O'Connorceea03c2008-11-08 21:36:35 -050028
29/****************************************************************
30 * Helper functions
31 ****************************************************************/
32
Kevin O'Connor714325c2008-11-01 20:32:27 -040033// Execute a given option rom.
34static void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040035__callrom(struct rom_header *rom, u16 offset, u16 bdf)
Kevin O'Connor714325c2008-11-01 20:32:27 -040036{
Kevin O'Connor35ae7262009-01-19 15:44:44 -050037 u16 seg = FLATPTR_TO_SEG(rom);
Kevin O'Connor91b53a72009-05-05 22:52:09 -040038 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
Kevin O'Connor714325c2008-11-01 20:32:27 -040039
40 struct bregs br;
41 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040042 br.flags = F_IF;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050043 br.ax = bdf;
Kevin O'Connor714325c2008-11-01 20:32:27 -040044 br.bx = 0xffff;
45 br.dx = 0xffff;
46 br.es = SEG_BIOS;
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050047 br.di = get_pnp_offset();
Kevin O'Connor9f985422009-09-09 11:34:39 -040048 br.code = SEGOFF(seg, offset);
Kevin O'Connorad901592009-12-13 11:25:25 -050049 start_preempt();
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -040050 farcall16big(&br);
Kevin O'Connorad901592009-12-13 11:25:25 -050051 finish_preempt();
Kevin O'Connor714325c2008-11-01 20:32:27 -040052}
53
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040054// Execute a given option rom at the standard entry vector.
David Woodhouse5c8dd962013-01-25 19:31:47 -060055void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040056callrom(struct rom_header *rom, u16 bdf)
57{
58 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
59}
60
Kevin O'Connor0a924122009-02-08 19:43:47 -050061// Execute a BCV option rom registered via add_bcv().
62void
63call_bcv(u16 seg, u16 ip)
64{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040065 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -050066}
67
Kevin O'Connorceea03c2008-11-08 21:36:35 -050068// Verify that an option rom looks valid
69static int
70is_valid_rom(struct rom_header *rom)
71{
Kevin O'Connorc36273f2009-04-16 20:43:07 -040072 dprintf(6, "Checking rom %p (sig %x size %d)\n"
73 , rom, rom->signature, rom->size);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050074 if (rom->signature != OPTION_ROM_SIGNATURE)
75 return 0;
Kevin O'Connor674e4602009-04-13 19:19:22 -040076 if (! rom->size)
77 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050078 u32 len = rom->size * 512;
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050079 u8 sum = checksum(rom, len);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050080 if (sum != 0) {
81 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
82 , rom, len, sum);
Kevin O'Connore010d852011-07-05 20:47:35 -040083 if (EnforceChecksum)
Kevin O'Connorcc975642011-03-06 19:22:46 -050084 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050085 }
86 return 1;
87}
88
89// Check if a valid option rom has a pnp struct; return it if so.
90static struct pnp_data *
91get_pnp_rom(struct rom_header *rom)
92{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040093 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050094 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connorceea03c2008-11-08 21:36:35 -050095 return NULL;
96 return pnp;
97}
98
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -050099// Check for multiple pnp option rom headers.
100static struct pnp_data *
101get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
102{
103 if (! pnp->nextoffset)
104 return NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400105 pnp = (void*)((u8*)rom + pnp->nextoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500106 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500107 return NULL;
108 return pnp;
109}
110
Kevin O'Connor62eea672008-12-06 11:57:45 -0500111// Check if a valid option rom has a pci struct; return it if so.
112static struct pci_data *
113get_pci_rom(struct rom_header *rom)
114{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400115 struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
116 if (pd->signature != PCI_ROM_SIGNATURE)
Kevin O'Connor62eea672008-12-06 11:57:45 -0500117 return NULL;
Kevin O'Connor97dce0f2013-02-15 22:46:09 -0500118 if (rom->pcioffset & 3)
119 dprintf(1, "WARNING! Found unaligned PCI rom (vd=%04x:%04x)\n"
120 , pd->vendor, pd->device);
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400121 return pd;
Kevin O'Connor62eea672008-12-06 11:57:45 -0500122}
123
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400124// Run rom init code and note rom size.
125static int
126init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
127{
128 if (! is_valid_rom(rom))
129 return -1;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400130 struct rom_header *newrom = rom_reserve(rom->size * 512);
131 if (!newrom) {
132 warn_noalloc();
133 return -1;
134 }
135 if (newrom != rom)
136 memmove(newrom, rom, rom->size * 512);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400137
Stefan Berger5deeec12015-06-09 19:56:33 -0400138 tpm_option_rom(newrom, rom->size * 512);
139
Kevin O'Connor5e019082012-05-20 21:11:43 -0400140 if (isvga || get_pnp_rom(newrom))
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400141 // Only init vga and PnP roms here.
Kevin O'Connor5e019082012-05-20 21:11:43 -0400142 callrom(newrom, bdf);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400143
Kevin O'Connor5e019082012-05-20 21:11:43 -0400144 return rom_confirm(newrom->size * 512);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400145}
146
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500147#define RS_PCIROM (1LL<<33)
148
149static void
150setRomSource(u64 *sources, struct rom_header *rom, u64 source)
151{
152 if (sources)
153 sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
154}
155
Kevin O'Connor031ef552010-12-27 19:26:57 -0500156static int
157getRomPriority(u64 *sources, struct rom_header *rom, int instance)
158{
159 u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
160 if (!source)
161 return -1;
162 if (source & RS_PCIROM)
Kevin O'Connorfce91892011-07-09 14:47:47 -0400163 return bootprio_find_pci_rom((void*)(u32)source, instance);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400164 struct romfile_s *file = (void*)(u32)source;
165 return bootprio_find_named_rom(file->name, instance);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500166}
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400167
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400168
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400169/****************************************************************
170 * Roms in CBFS
171 ****************************************************************/
172
Kevin O'Connor5e019082012-05-20 21:11:43 -0400173static struct rom_header *
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400174deploy_romfile(struct romfile_s *file)
Kevin O'Connor5e019082012-05-20 21:11:43 -0400175{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400176 u32 size = file->size;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400177 struct rom_header *rom = rom_reserve(size);
178 if (!rom) {
179 warn_noalloc();
180 return NULL;
181 }
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400182 int ret = file->copy(file, rom, size);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400183 if (ret <= 0)
184 return NULL;
185 return rom;
186}
187
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400188// Run all roms in a given CBFS directory.
189static void
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500190run_file_roms(const char *prefix, int isvga, u64 *sources)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400191{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400192 struct romfile_s *file = NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400193 for (;;) {
Kevin O'Connore2304262010-06-13 16:05:17 -0400194 file = romfile_findprefix(prefix, file);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400195 if (!file)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400196 break;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400197 struct rom_header *rom = deploy_romfile(file);
198 if (rom) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400199 setRomSource(sources, rom, (u32)file);
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500200 init_optionrom(rom, 0, isvga);
201 }
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400202 }
203}
204
205
206/****************************************************************
207 * PCI roms
208 ****************************************************************/
209
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400210// Verify device is a vga device with legacy address decoding enabled.
Alex Williamson7adfd712013-03-20 10:58:47 -0600211int
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400212is_pci_vga(struct pci_device *pci)
213{
214 if (pci->class != PCI_CLASS_DISPLAY_VGA)
215 return 0;
216 u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
217 if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
218 return 0;
219 while (pci->parent) {
220 pci = pci->parent;
221 u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
222 if (!(ctrl & PCI_BRIDGE_CTL_VGA))
223 return 0;
224 }
225 return 1;
226}
227
Kevin O'Connor5e019082012-05-20 21:11:43 -0400228// Copy a rom to its permanent location below 1MiB
229static struct rom_header *
230copy_rom(struct rom_header *rom)
231{
232 u32 romsize = rom->size * 512;
233 struct rom_header *newrom = rom_reserve(romsize);
234 if (!newrom) {
235 warn_noalloc();
236 return NULL;
237 }
238 dprintf(4, "Copying option rom (size %d) from %p to %p\n"
239 , romsize, rom, newrom);
240 iomemcpy(newrom, rom, romsize);
241 return newrom;
242}
243
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500244// Map the option rom of a given PCI device.
245static struct rom_header *
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400246map_pcirom(struct pci_device *pci)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500247{
Kevin O'Connor7b673002016-02-03 03:03:15 -0500248 dprintf(6, "Attempting to map option rom on dev %pP\n", pci);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500249
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400250 if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
251 dprintf(6, "Skipping non-normal pci device (type=%x)\n"
252 , pci->header_type);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500253 return NULL;
254 }
255
Kevin O'Connor7b673002016-02-03 03:03:15 -0500256 u16 bdf = pci->bdf;
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500257 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
258 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
259 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500260
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500261 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500262 orig &= ~PCI_ROM_ADDRESS_ENABLE;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500263 if (!sz || sz == 0xffffffff)
264 goto fail;
265
Kevin O'Connor64cf2fb2009-04-18 17:01:02 -0400266 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
267 // Don't try to map to a pci addresses at its max, in the last
268 // 4MiB of ram, or the first 16MiB of ram.
Kevin O'Connor62eea672008-12-06 11:57:45 -0500269 dprintf(6, "Preset rom address doesn't look valid\n");
270 goto fail;
271 }
272
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500273 // Looks like a rom - enable it.
274 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500275
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400276 struct rom_header *rom = (void*)orig;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500277 for (;;) {
Kevin O'Connor7b673002016-02-03 03:03:15 -0500278 dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x bdf=%pP)\n"
279 , rom, pci->vendor, pci->device, pci);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500280 if (rom->signature != OPTION_ROM_SIGNATURE) {
281 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
282 goto fail;
283 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400284 struct pci_data *pd = get_pci_rom(rom);
285 if (! pd) {
Kevin O'Connor62eea672008-12-06 11:57:45 -0500286 dprintf(6, "No valid pci signature found\n");
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500287 goto fail;
288 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500289
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400290 if (pd->vendor == pci->vendor && pd->device == pci->device
291 && pd->type == PCIROM_CODETYPE_X86)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500292 // A match
293 break;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400294 dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
295 , pd->vendor, pd->device, pd->type);
296 if (pd->indicator & 0x80) {
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500297 dprintf(6, "No more images left\n");
298 goto fail;
299 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400300 rom = (void*)((u32)rom + pd->ilen * 512);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500301 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500302
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500303 rom = copy_rom(rom);
304 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500305 return rom;
306fail:
307 // Not valid - restore original and exit.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500308 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500309 return NULL;
310}
311
Gerd Hoffmann0932c202018-11-20 08:06:55 +0100312static int boot_irq_captured(void)
313{
314 return GET_IVT(0x19).segoff != FUNC16(entry_19_official).segoff;
315}
316
317static void boot_irq_restore(void)
318{
319 struct segoff_s seabios;
320
321 seabios = FUNC16(entry_19_official);
322 SET_IVT(0x19, seabios);
323}
324
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500325// Attempt to map and initialize the option rom on a given PCI device.
Kevin O'Connoreeca36c2015-08-10 15:37:13 -0400326static void
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400327init_pcirom(struct pci_device *pci, int isvga, u64 *sources)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500328{
Kevin O'Connor7b673002016-02-03 03:03:15 -0500329 dprintf(4, "Attempting to init PCI bdf %pP (vd %04x:%04x)\n"
330 , pci, pci->vendor, pci->device);
Kevin O'Connoreeca36c2015-08-10 15:37:13 -0400331
Rob Barnes27b56c62020-01-22 14:09:21 -0700332 char fname[20];
333 // Try to find rom file with revision included
334 snprintf(fname, sizeof(fname), "pci%04x,%04x,%02x.rom"
335 , pci->vendor, pci->device, pci->revision);
Kevin O'Connoreeca36c2015-08-10 15:37:13 -0400336 struct romfile_s *file = romfile_find(fname);
Rob Barnes27b56c62020-01-22 14:09:21 -0700337 if (!file) {
338 // Fallback to finding rom file without revision
339 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
340 , pci->vendor, pci->device);
341 file = romfile_find(fname);
342 }
Kevin O'Connoreeca36c2015-08-10 15:37:13 -0400343 struct rom_header *rom = NULL;
344 if (file)
345 rom = deploy_romfile(file);
346 else if (RunPCIroms > 1 || (RunPCIroms == 1 && isvga))
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400347 rom = map_pcirom(pci);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500348 if (! rom)
349 // No ROM present.
Kevin O'Connoreeca36c2015-08-10 15:37:13 -0400350 return;
Gerd Hoffmann0932c202018-11-20 08:06:55 +0100351 int irq_was_captured = boot_irq_captured();
352 struct pnp_data *pnp = get_pnp_rom(rom);
Kevin O'Connorfce91892011-07-09 14:47:47 -0400353 setRomSource(sources, rom, RS_PCIROM | (u32)pci);
Kevin O'Connor7b673002016-02-03 03:03:15 -0500354 init_optionrom(rom, pci->bdf, isvga);
Gerd Hoffmann0932c202018-11-20 08:06:55 +0100355 if (boot_irq_captured() && !irq_was_captured &&
356 !file && !isvga && pnp) {
357 // This PCI rom is misbehaving - recapture the boot irqs
358 char *desc = MAKE_FLATPTR(FLATPTR_TO_SEG(rom), pnp->productname);
359 dprintf(1, "PnP optionrom \"%s\" (bdf %pP) captured int19, restoring\n",
360 desc, pci);
361 boot_irq_restore();
362 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500363}
364
365
366/****************************************************************
367 * Non-VGA option rom init
368 ****************************************************************/
369
370void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500371optionrom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400372{
373 if (! CONFIG_OPTIONROMS)
374 return;
375
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500376 dprintf(1, "Scan for option roms\n");
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500377 u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
378 memset(sources, 0, sizeof(sources));
Kevin O'Connor5e019082012-05-20 21:11:43 -0400379 u32 post_vga = rom_get_last();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500380
Kevin O'Connor9d691ac2016-04-01 14:23:36 -0400381 // Find and deploy PCI roms.
382 struct pci_device *pci;
383 foreachpci(pci) {
Gerd Hoffmanne28e0bb2018-05-30 13:49:06 +0200384 if (pci->class == PCI_CLASS_DISPLAY_VGA ||
385 pci->class == PCI_CLASS_DISPLAY_OTHER ||
386 pci->have_driver)
Kevin O'Connor9d691ac2016-04-01 14:23:36 -0400387 continue;
388 init_pcirom(pci, 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500389 }
Kevin O'Connor9d691ac2016-04-01 14:23:36 -0400390
391 // Find and deploy CBFS roms not associated with a device.
392 run_file_roms("genroms/", 0, sources);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400393 rom_reserve(0);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500394
395 // All option roms found and deployed - now build BEV/BCV vectors.
396
397 u32 pos = post_vga;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400398 while (pos < rom_get_last()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400399 struct rom_header *rom = (void*)pos;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500400 if (! is_valid_rom(rom)) {
401 pos += OPTION_ROM_ALIGN;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400402 continue;
403 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500404 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
405 struct pnp_data *pnp = get_pnp_rom(rom);
406 if (! pnp) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500407 // Legacy rom.
Kevin O'Connor031ef552010-12-27 19:26:57 -0500408 boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
409 , getRomPriority(sources, rom, 0));
Kevin O'Connor714325c2008-11-01 20:32:27 -0400410 continue;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500411 }
Kevin O'Connor1e157252012-01-14 11:55:35 -0500412 // PnP rom - check for BEV and BCV boot capabilities.
413 int instance = 0;
414 while (pnp) {
415 if (pnp->bev)
416 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
417 , getRomPriority(sources, rom, instance++));
418 else if (pnp->bcv)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500419 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
420 , getRomPriority(sources, rom, instance++));
Kevin O'Connor1e157252012-01-14 11:55:35 -0500421 else
422 break;
423 pnp = get_pnp_next(rom, pnp);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500424 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400425 }
426}
427
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500428
429/****************************************************************
430 * VGA init
431 ****************************************************************/
432
Kevin O'Connor422263d2011-07-05 20:56:07 -0400433int ScreenAndDebug;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400434struct rom_header *VgaROM;
Kevin O'Connor8b0c5092011-07-05 20:50:32 -0400435
Gerd Hoffmanne28e0bb2018-05-30 13:49:06 +0200436static void try_setup_display_other(void)
437{
438 struct pci_device *pci;
439
440 dprintf(1, "No VGA found, scan for other display\n");
441
442 foreachpci(pci) {
443 if (pci->class != PCI_CLASS_DISPLAY_OTHER)
444 continue;
445 struct rom_header *rom = map_pcirom(pci);
446 if (!rom)
447 continue;
448 dprintf(1, "Other display found at %pP\n", pci);
449 pci_config_maskw(pci->bdf, PCI_COMMAND, 0,
450 PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
451 init_optionrom(rom, pci->bdf, 1);
452 return;
453 }
454}
455
Kevin O'Connor714325c2008-11-01 20:32:27 -0400456// Call into vga code to turn on console.
457void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500458vgarom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400459{
Gerd Hoffmanne28e0bb2018-05-30 13:49:06 +0200460 int have_vga = 0;
461
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500462 if (! CONFIG_OPTIONROMS)
463 return;
464
Kevin O'Connor714325c2008-11-01 20:32:27 -0400465 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500466
Kevin O'Connor422263d2011-07-05 20:56:07 -0400467 // Load some config settings that impact VGA.
Kevin O'Connore010d852011-07-05 20:47:35 -0400468 EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
Kevin O'Connor897fb112013-02-07 23:32:48 -0500469 S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", CONFIG_QEMU);
tpearson@raptorengineeringinc.comd23eba62015-02-17 15:04:17 -0600470 RunPCIroms = romfile_loadint("etc/pci-optionrom-exec", 2);
Kevin O'Connor422263d2011-07-05 20:56:07 -0400471 ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
Kevin O'Connore010d852011-07-05 20:47:35 -0400472
Kevin O'Connor9d691ac2016-04-01 14:23:36 -0400473 // Clear option rom memory
474 memset((void*)BUILD_ROM_START, 0, rom_get_max() - BUILD_ROM_START);
Kevin O'Connore8264652010-09-15 22:06:19 -0400475
Kevin O'Connor9d691ac2016-04-01 14:23:36 -0400476 // Find and deploy PCI VGA rom.
477 struct pci_device *pci;
478 foreachpci(pci) {
479 if (!is_pci_vga(pci))
480 continue;
481 vgahook_setup(pci);
482 init_pcirom(pci, 1, NULL);
Gerd Hoffmanne28e0bb2018-05-30 13:49:06 +0200483 have_vga = 1;
Kevin O'Connor9d691ac2016-04-01 14:23:36 -0400484 break;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400485 }
Gerd Hoffmanne28e0bb2018-05-30 13:49:06 +0200486 if (!have_vga)
487 try_setup_display_other();
Kevin O'Connor9d691ac2016-04-01 14:23:36 -0400488
489 // Find and deploy CBFS vga-style roms not associated with a device.
490 run_file_roms("vgaroms/", 1, NULL);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400491 rom_reserve(0);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400492
Gerd Hoffmannd6728f32017-09-18 10:47:23 +0200493 if (rom_get_last() != BUILD_ROM_START)
494 // VGA rom found
495 VgaROM = (void*)BUILD_ROM_START;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400496}
Kevin O'Connord282af72009-07-04 04:10:32 -0400497
498void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500499s3_resume_vga(void)
Kevin O'Connord282af72009-07-04 04:10:32 -0400500{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500501 if (!S3ResumeVga)
Kevin O'Connord282af72009-07-04 04:10:32 -0400502 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400503 if (!VgaROM || ! is_valid_rom(VgaROM))
Kevin O'Connord282af72009-07-04 04:10:32 -0400504 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400505 callrom(VgaROM, 0);
Kevin O'Connord282af72009-07-04 04:10:32 -0400506}