blob: a389dd93be93e66690ff22c7f7e703079dc96a8f [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
11#include "hw/pci.h" // foreachpci
12#include "hw/pci_ids.h" // PCI_CLASS_DISPLAY_VGA
13#include "hw/pci_regs.h" // PCI_ROM_ADDRESS
Kevin O'Connor9dea5902013-09-14 20:23:54 -040014#include "malloc.h" // rom_confirm
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040015#include "output.h" // dprintf
Kevin O'Connor41639f82013-09-14 19:37:36 -040016#include "romfile.h" // romfile_loadint
Kevin O'Connor3df600b2013-09-14 19:28:55 -040017#include "stacks.h" // farcall16big
Kevin O'Connor8fb3a5e2013-09-14 22:27:14 -040018#include "std/optionrom.h" // struct rom_header
Kevin O'Connor4f790aa2013-09-14 23:04:08 -040019#include "std/pnpbios.h" // PNP_SIGNATURE
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040020#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040021#include "util.h" // get_pnp_offset
Stefan Berger2aff1c12015-05-26 15:48:33 -040022#include "tcgbios.h" // tpm_*
Kevin O'Connorceea03c2008-11-08 21:36:35 -050023
tpearson@raptorengineeringinc.comd23eba62015-02-17 15:04:17 -060024static int EnforceChecksum, S3ResumeVga, RunPCIroms;
25
Kevin O'Connorceea03c2008-11-08 21:36:35 -050026
27/****************************************************************
28 * Helper functions
29 ****************************************************************/
30
Kevin O'Connor714325c2008-11-01 20:32:27 -040031// Execute a given option rom.
32static void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040033__callrom(struct rom_header *rom, u16 offset, u16 bdf)
Kevin O'Connor714325c2008-11-01 20:32:27 -040034{
Kevin O'Connor35ae7262009-01-19 15:44:44 -050035 u16 seg = FLATPTR_TO_SEG(rom);
Kevin O'Connor91b53a72009-05-05 22:52:09 -040036 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
Kevin O'Connor714325c2008-11-01 20:32:27 -040037
38 struct bregs br;
39 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040040 br.flags = F_IF;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050041 br.ax = bdf;
Kevin O'Connor714325c2008-11-01 20:32:27 -040042 br.bx = 0xffff;
43 br.dx = 0xffff;
44 br.es = SEG_BIOS;
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050045 br.di = get_pnp_offset();
Kevin O'Connor9f985422009-09-09 11:34:39 -040046 br.code = SEGOFF(seg, offset);
Kevin O'Connorad901592009-12-13 11:25:25 -050047 start_preempt();
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -040048 farcall16big(&br);
Kevin O'Connorad901592009-12-13 11:25:25 -050049 finish_preempt();
Kevin O'Connor714325c2008-11-01 20:32:27 -040050}
51
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040052// Execute a given option rom at the standard entry vector.
David Woodhouse5c8dd962013-01-25 19:31:47 -060053void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040054callrom(struct rom_header *rom, u16 bdf)
55{
56 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
57}
58
Kevin O'Connor0a924122009-02-08 19:43:47 -050059// Execute a BCV option rom registered via add_bcv().
60void
61call_bcv(u16 seg, u16 ip)
62{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040063 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -050064}
65
Kevin O'Connorceea03c2008-11-08 21:36:35 -050066// Verify that an option rom looks valid
67static int
68is_valid_rom(struct rom_header *rom)
69{
Kevin O'Connorc36273f2009-04-16 20:43:07 -040070 dprintf(6, "Checking rom %p (sig %x size %d)\n"
71 , rom, rom->signature, rom->size);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050072 if (rom->signature != OPTION_ROM_SIGNATURE)
73 return 0;
Kevin O'Connor674e4602009-04-13 19:19:22 -040074 if (! rom->size)
75 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050076 u32 len = rom->size * 512;
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050077 u8 sum = checksum(rom, len);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050078 if (sum != 0) {
79 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
80 , rom, len, sum);
Kevin O'Connore010d852011-07-05 20:47:35 -040081 if (EnforceChecksum)
Kevin O'Connorcc975642011-03-06 19:22:46 -050082 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050083 }
84 return 1;
85}
86
87// Check if a valid option rom has a pnp struct; return it if so.
88static struct pnp_data *
89get_pnp_rom(struct rom_header *rom)
90{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040091 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050092 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connorceea03c2008-11-08 21:36:35 -050093 return NULL;
94 return pnp;
95}
96
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -050097// Check for multiple pnp option rom headers.
98static struct pnp_data *
99get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
100{
101 if (! pnp->nextoffset)
102 return NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400103 pnp = (void*)((u8*)rom + pnp->nextoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500104 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500105 return NULL;
106 return pnp;
107}
108
Kevin O'Connor62eea672008-12-06 11:57:45 -0500109// Check if a valid option rom has a pci struct; return it if so.
110static struct pci_data *
111get_pci_rom(struct rom_header *rom)
112{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400113 struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
114 if (pd->signature != PCI_ROM_SIGNATURE)
Kevin O'Connor62eea672008-12-06 11:57:45 -0500115 return NULL;
Kevin O'Connor97dce0f2013-02-15 22:46:09 -0500116 if (rom->pcioffset & 3)
117 dprintf(1, "WARNING! Found unaligned PCI rom (vd=%04x:%04x)\n"
118 , pd->vendor, pd->device);
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400119 return pd;
Kevin O'Connor62eea672008-12-06 11:57:45 -0500120}
121
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400122// Run rom init code and note rom size.
123static int
124init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
125{
126 if (! is_valid_rom(rom))
127 return -1;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400128 struct rom_header *newrom = rom_reserve(rom->size * 512);
129 if (!newrom) {
130 warn_noalloc();
131 return -1;
132 }
133 if (newrom != rom)
134 memmove(newrom, rom, rom->size * 512);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400135
Stefan Berger5deeec12015-06-09 19:56:33 -0400136 tpm_option_rom(newrom, rom->size * 512);
137
Kevin O'Connor5e019082012-05-20 21:11:43 -0400138 if (isvga || get_pnp_rom(newrom))
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400139 // Only init vga and PnP roms here.
Kevin O'Connor5e019082012-05-20 21:11:43 -0400140 callrom(newrom, bdf);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400141
Kevin O'Connor5e019082012-05-20 21:11:43 -0400142 return rom_confirm(newrom->size * 512);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400143}
144
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500145#define RS_PCIROM (1LL<<33)
146
147static void
148setRomSource(u64 *sources, struct rom_header *rom, u64 source)
149{
150 if (sources)
151 sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
152}
153
Kevin O'Connor031ef552010-12-27 19:26:57 -0500154static int
155getRomPriority(u64 *sources, struct rom_header *rom, int instance)
156{
157 u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
158 if (!source)
159 return -1;
160 if (source & RS_PCIROM)
Kevin O'Connorfce91892011-07-09 14:47:47 -0400161 return bootprio_find_pci_rom((void*)(u32)source, instance);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400162 struct romfile_s *file = (void*)(u32)source;
163 return bootprio_find_named_rom(file->name, instance);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500164}
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400165
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400166
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400167/****************************************************************
168 * Roms in CBFS
169 ****************************************************************/
170
Kevin O'Connor5e019082012-05-20 21:11:43 -0400171static struct rom_header *
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400172deploy_romfile(struct romfile_s *file)
Kevin O'Connor5e019082012-05-20 21:11:43 -0400173{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400174 u32 size = file->size;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400175 struct rom_header *rom = rom_reserve(size);
176 if (!rom) {
177 warn_noalloc();
178 return NULL;
179 }
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400180 int ret = file->copy(file, rom, size);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400181 if (ret <= 0)
182 return NULL;
183 return rom;
184}
185
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400186// Run all roms in a given CBFS directory.
187static void
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500188run_file_roms(const char *prefix, int isvga, u64 *sources)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400189{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400190 struct romfile_s *file = NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400191 for (;;) {
Kevin O'Connore2304262010-06-13 16:05:17 -0400192 file = romfile_findprefix(prefix, file);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400193 if (!file)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400194 break;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400195 struct rom_header *rom = deploy_romfile(file);
196 if (rom) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400197 setRomSource(sources, rom, (u32)file);
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500198 init_optionrom(rom, 0, isvga);
199 }
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400200 }
201}
202
203
204/****************************************************************
205 * PCI roms
206 ****************************************************************/
207
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400208// Verify device is a vga device with legacy address decoding enabled.
Alex Williamson7adfd712013-03-20 10:58:47 -0600209int
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400210is_pci_vga(struct pci_device *pci)
211{
212 if (pci->class != PCI_CLASS_DISPLAY_VGA)
213 return 0;
214 u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
215 if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
216 return 0;
217 while (pci->parent) {
218 pci = pci->parent;
219 u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
220 if (!(ctrl & PCI_BRIDGE_CTL_VGA))
221 return 0;
222 }
223 return 1;
224}
225
Kevin O'Connor5e019082012-05-20 21:11:43 -0400226// Copy a rom to its permanent location below 1MiB
227static struct rom_header *
228copy_rom(struct rom_header *rom)
229{
230 u32 romsize = rom->size * 512;
231 struct rom_header *newrom = rom_reserve(romsize);
232 if (!newrom) {
233 warn_noalloc();
234 return NULL;
235 }
236 dprintf(4, "Copying option rom (size %d) from %p to %p\n"
237 , romsize, rom, newrom);
238 iomemcpy(newrom, rom, romsize);
239 return newrom;
240}
241
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500242// Map the option rom of a given PCI device.
243static struct rom_header *
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400244map_pcirom(struct pci_device *pci)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500245{
Kevin O'Connor7b673002016-02-03 03:03:15 -0500246 dprintf(6, "Attempting to map option rom on dev %pP\n", pci);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500247
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400248 if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
249 dprintf(6, "Skipping non-normal pci device (type=%x)\n"
250 , pci->header_type);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500251 return NULL;
252 }
253
Kevin O'Connor7b673002016-02-03 03:03:15 -0500254 u16 bdf = pci->bdf;
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500255 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
256 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
257 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500258
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500259 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500260 orig &= ~PCI_ROM_ADDRESS_ENABLE;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500261 if (!sz || sz == 0xffffffff)
262 goto fail;
263
Kevin O'Connor64cf2fb2009-04-18 17:01:02 -0400264 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
265 // Don't try to map to a pci addresses at its max, in the last
266 // 4MiB of ram, or the first 16MiB of ram.
Kevin O'Connor62eea672008-12-06 11:57:45 -0500267 dprintf(6, "Preset rom address doesn't look valid\n");
268 goto fail;
269 }
270
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500271 // Looks like a rom - enable it.
272 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500273
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400274 struct rom_header *rom = (void*)orig;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500275 for (;;) {
Kevin O'Connor7b673002016-02-03 03:03:15 -0500276 dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x bdf=%pP)\n"
277 , rom, pci->vendor, pci->device, pci);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500278 if (rom->signature != OPTION_ROM_SIGNATURE) {
279 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
280 goto fail;
281 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400282 struct pci_data *pd = get_pci_rom(rom);
283 if (! pd) {
Kevin O'Connor62eea672008-12-06 11:57:45 -0500284 dprintf(6, "No valid pci signature found\n");
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500285 goto fail;
286 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500287
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400288 if (pd->vendor == pci->vendor && pd->device == pci->device
289 && pd->type == PCIROM_CODETYPE_X86)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500290 // A match
291 break;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400292 dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
293 , pd->vendor, pd->device, pd->type);
294 if (pd->indicator & 0x80) {
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500295 dprintf(6, "No more images left\n");
296 goto fail;
297 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400298 rom = (void*)((u32)rom + pd->ilen * 512);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500299 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500300
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500301 rom = copy_rom(rom);
302 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500303 return rom;
304fail:
305 // Not valid - restore original and exit.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500306 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500307 return NULL;
308}
309
310// Attempt to map and initialize the option rom on a given PCI device.
Kevin O'Connoreeca36c2015-08-10 15:37:13 -0400311static void
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400312init_pcirom(struct pci_device *pci, int isvga, u64 *sources)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500313{
Kevin O'Connor7b673002016-02-03 03:03:15 -0500314 dprintf(4, "Attempting to init PCI bdf %pP (vd %04x:%04x)\n"
315 , pci, pci->vendor, pci->device);
Kevin O'Connoreeca36c2015-08-10 15:37:13 -0400316
317 char fname[17];
318 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
319 , pci->vendor, pci->device);
320 struct romfile_s *file = romfile_find(fname);
321 struct rom_header *rom = NULL;
322 if (file)
323 rom = deploy_romfile(file);
324 else if (RunPCIroms > 1 || (RunPCIroms == 1 && isvga))
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400325 rom = map_pcirom(pci);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500326 if (! rom)
327 // No ROM present.
Kevin O'Connoreeca36c2015-08-10 15:37:13 -0400328 return;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400329 setRomSource(sources, rom, RS_PCIROM | (u32)pci);
Kevin O'Connor7b673002016-02-03 03:03:15 -0500330 init_optionrom(rom, pci->bdf, isvga);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500331}
332
333
334/****************************************************************
335 * Non-VGA option rom init
336 ****************************************************************/
337
338void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500339optionrom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400340{
341 if (! CONFIG_OPTIONROMS)
342 return;
343
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500344 dprintf(1, "Scan for option roms\n");
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500345 u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
346 memset(sources, 0, sizeof(sources));
Kevin O'Connor5e019082012-05-20 21:11:43 -0400347 u32 post_vga = rom_get_last();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500348
349 if (CONFIG_OPTIONROMS_DEPLOYED) {
350 // Option roms are already deployed on the system.
Kevin O'Connor5e019082012-05-20 21:11:43 -0400351 u32 pos = post_vga;
Kevin O'Connor3733f6f2013-02-17 12:44:23 -0500352 while (pos < rom_get_max()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400353 int ret = init_optionrom((void*)pos, 0, 0);
354 if (ret)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500355 pos += OPTION_ROM_ALIGN;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400356 else
Kevin O'Connor5e019082012-05-20 21:11:43 -0400357 pos = rom_get_last();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500358 }
359 } else {
360 // Find and deploy PCI roms.
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400361 struct pci_device *pci;
362 foreachpci(pci) {
Kevin O'Connor76b5e712011-06-21 22:52:51 -0400363 if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver)
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500364 continue;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400365 init_pcirom(pci, 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500366 }
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400367
368 // Find and deploy CBFS roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500369 run_file_roms("genroms/", 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500370 }
Kevin O'Connor5e019082012-05-20 21:11:43 -0400371 rom_reserve(0);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500372
373 // All option roms found and deployed - now build BEV/BCV vectors.
374
375 u32 pos = post_vga;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400376 while (pos < rom_get_last()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400377 struct rom_header *rom = (void*)pos;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500378 if (! is_valid_rom(rom)) {
379 pos += OPTION_ROM_ALIGN;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400380 continue;
381 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500382 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
383 struct pnp_data *pnp = get_pnp_rom(rom);
384 if (! pnp) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500385 // Legacy rom.
Kevin O'Connor031ef552010-12-27 19:26:57 -0500386 boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
387 , getRomPriority(sources, rom, 0));
Kevin O'Connor714325c2008-11-01 20:32:27 -0400388 continue;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500389 }
Kevin O'Connor1e157252012-01-14 11:55:35 -0500390 // PnP rom - check for BEV and BCV boot capabilities.
391 int instance = 0;
392 while (pnp) {
393 if (pnp->bev)
394 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
395 , getRomPriority(sources, rom, instance++));
396 else if (pnp->bcv)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500397 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
398 , getRomPriority(sources, rom, instance++));
Kevin O'Connor1e157252012-01-14 11:55:35 -0500399 else
400 break;
401 pnp = get_pnp_next(rom, pnp);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500402 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400403 }
404}
405
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500406
407/****************************************************************
408 * VGA init
409 ****************************************************************/
410
Kevin O'Connor422263d2011-07-05 20:56:07 -0400411int ScreenAndDebug;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400412struct rom_header *VgaROM;
Kevin O'Connor8b0c5092011-07-05 20:50:32 -0400413
Kevin O'Connor714325c2008-11-01 20:32:27 -0400414// Call into vga code to turn on console.
415void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500416vgarom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400417{
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500418 if (! CONFIG_OPTIONROMS)
419 return;
420
Kevin O'Connor714325c2008-11-01 20:32:27 -0400421 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500422
Kevin O'Connor422263d2011-07-05 20:56:07 -0400423 // Load some config settings that impact VGA.
Kevin O'Connore010d852011-07-05 20:47:35 -0400424 EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
Kevin O'Connor897fb112013-02-07 23:32:48 -0500425 S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", CONFIG_QEMU);
tpearson@raptorengineeringinc.comd23eba62015-02-17 15:04:17 -0600426 RunPCIroms = romfile_loadint("etc/pci-optionrom-exec", 2);
Kevin O'Connor422263d2011-07-05 20:56:07 -0400427 ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
Kevin O'Connore010d852011-07-05 20:47:35 -0400428
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500429 if (CONFIG_OPTIONROMS_DEPLOYED) {
430 // Option roms are already deployed on the system.
Kevin O'Connore7739302009-07-26 19:16:09 -0400431 init_optionrom((void*)BUILD_ROM_START, 0, 1);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500432 } else {
Kevin O'Connore8264652010-09-15 22:06:19 -0400433 // Clear option rom memory
Kevin O'Connor3733f6f2013-02-17 12:44:23 -0500434 memset((void*)BUILD_ROM_START, 0, rom_get_max() - BUILD_ROM_START);
Kevin O'Connore8264652010-09-15 22:06:19 -0400435
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500436 // Find and deploy PCI VGA rom.
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400437 struct pci_device *pci;
438 foreachpci(pci) {
439 if (!is_pci_vga(pci))
440 continue;
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400441 vgahook_setup(pci);
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400442 init_pcirom(pci, 1, NULL);
443 break;
444 }
Kevin O'Connor09880da2009-06-17 20:35:41 -0400445
446 // Find and deploy CBFS vga-style roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500447 run_file_roms("vgaroms/", 1, NULL);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400448 }
Kevin O'Connor5e019082012-05-20 21:11:43 -0400449 rom_reserve(0);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400450
Kevin O'Connor5e019082012-05-20 21:11:43 -0400451 if (rom_get_last() == BUILD_ROM_START)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400452 // No VGA rom found
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400453 return;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400454
Kevin O'Connor5e019082012-05-20 21:11:43 -0400455 VgaROM = (void*)BUILD_ROM_START;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400456 enable_vga_console();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400457}
Kevin O'Connord282af72009-07-04 04:10:32 -0400458
459void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500460s3_resume_vga(void)
Kevin O'Connord282af72009-07-04 04:10:32 -0400461{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500462 if (!S3ResumeVga)
Kevin O'Connord282af72009-07-04 04:10:32 -0400463 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400464 if (!VgaROM || ! is_valid_rom(VgaROM))
Kevin O'Connord282af72009-07-04 04:10:32 -0400465 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400466 callrom(VgaROM, 0);
Kevin O'Connord282af72009-07-04 04:10:32 -0400467}