blob: d7c8aefdcfa0af1502c4f3772612a9d753c0274e [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
Kevin O'Connorceea03c2008-11-08 21:36:35 -050022
Kevin O'Connorceea03c2008-11-08 21:36:35 -050023
24/****************************************************************
25 * Helper functions
26 ****************************************************************/
27
Kevin O'Connor714325c2008-11-01 20:32:27 -040028// Execute a given option rom.
29static void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040030__callrom(struct rom_header *rom, u16 offset, u16 bdf)
Kevin O'Connor714325c2008-11-01 20:32:27 -040031{
Kevin O'Connor35ae7262009-01-19 15:44:44 -050032 u16 seg = FLATPTR_TO_SEG(rom);
Kevin O'Connor91b53a72009-05-05 22:52:09 -040033 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
Kevin O'Connor714325c2008-11-01 20:32:27 -040034
35 struct bregs br;
36 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040037 br.flags = F_IF;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050038 br.ax = bdf;
Kevin O'Connor714325c2008-11-01 20:32:27 -040039 br.bx = 0xffff;
40 br.dx = 0xffff;
41 br.es = SEG_BIOS;
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050042 br.di = get_pnp_offset();
Kevin O'Connor9f985422009-09-09 11:34:39 -040043 br.code = SEGOFF(seg, offset);
Kevin O'Connorad901592009-12-13 11:25:25 -050044 start_preempt();
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -040045 farcall16big(&br);
Kevin O'Connorad901592009-12-13 11:25:25 -050046 finish_preempt();
Kevin O'Connor714325c2008-11-01 20:32:27 -040047
Kevin O'Connord83c87b2013-01-21 01:14:12 -050048 debug_serial_preinit();
Kevin O'Connor714325c2008-11-01 20:32:27 -040049}
50
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040051// Execute a given option rom at the standard entry vector.
David Woodhouse5c8dd962013-01-25 19:31:47 -060052void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040053callrom(struct rom_header *rom, u16 bdf)
54{
55 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
56}
57
Kevin O'Connor0a924122009-02-08 19:43:47 -050058// Execute a BCV option rom registered via add_bcv().
59void
60call_bcv(u16 seg, u16 ip)
61{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040062 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -050063}
64
Kevin O'Connore010d852011-07-05 20:47:35 -040065static int EnforceChecksum;
66
Kevin O'Connorceea03c2008-11-08 21:36:35 -050067// Verify that an option rom looks valid
68static int
69is_valid_rom(struct rom_header *rom)
70{
Kevin O'Connorc36273f2009-04-16 20:43:07 -040071 dprintf(6, "Checking rom %p (sig %x size %d)\n"
72 , rom, rom->signature, rom->size);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050073 if (rom->signature != OPTION_ROM_SIGNATURE)
74 return 0;
Kevin O'Connor674e4602009-04-13 19:19:22 -040075 if (! rom->size)
76 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050077 u32 len = rom->size * 512;
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050078 u8 sum = checksum(rom, len);
Kevin O'Connorceea03c2008-11-08 21:36:35 -050079 if (sum != 0) {
80 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
81 , rom, len, sum);
Kevin O'Connore010d852011-07-05 20:47:35 -040082 if (EnforceChecksum)
Kevin O'Connorcc975642011-03-06 19:22:46 -050083 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050084 }
85 return 1;
86}
87
88// Check if a valid option rom has a pnp struct; return it if so.
89static struct pnp_data *
90get_pnp_rom(struct rom_header *rom)
91{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040092 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050093 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connorceea03c2008-11-08 21:36:35 -050094 return NULL;
95 return pnp;
96}
97
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -050098// Check for multiple pnp option rom headers.
99static struct pnp_data *
100get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
101{
102 if (! pnp->nextoffset)
103 return NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400104 pnp = (void*)((u8*)rom + pnp->nextoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500105 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500106 return NULL;
107 return pnp;
108}
109
Kevin O'Connor62eea672008-12-06 11:57:45 -0500110// Check if a valid option rom has a pci struct; return it if so.
111static struct pci_data *
112get_pci_rom(struct rom_header *rom)
113{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400114 struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
115 if (pd->signature != PCI_ROM_SIGNATURE)
Kevin O'Connor62eea672008-12-06 11:57:45 -0500116 return NULL;
Kevin O'Connor97dce0f2013-02-15 22:46:09 -0500117 if (rom->pcioffset & 3)
118 dprintf(1, "WARNING! Found unaligned PCI rom (vd=%04x:%04x)\n"
119 , pd->vendor, pd->device);
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400120 return pd;
Kevin O'Connor62eea672008-12-06 11:57:45 -0500121}
122
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400123// Run rom init code and note rom size.
124static int
125init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
126{
127 if (! is_valid_rom(rom))
128 return -1;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400129 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);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400136
Kevin O'Connor5e019082012-05-20 21:11:43 -0400137 if (isvga || get_pnp_rom(newrom))
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400138 // Only init vga and PnP roms here.
Kevin O'Connor5e019082012-05-20 21:11:43 -0400139 callrom(newrom, bdf);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400140
Kevin O'Connor5e019082012-05-20 21:11:43 -0400141 return rom_confirm(newrom->size * 512);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400142}
143
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500144#define RS_PCIROM (1LL<<33)
145
146static void
147setRomSource(u64 *sources, struct rom_header *rom, u64 source)
148{
149 if (sources)
150 sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
151}
152
Kevin O'Connor031ef552010-12-27 19:26:57 -0500153static int
154getRomPriority(u64 *sources, struct rom_header *rom, int instance)
155{
156 u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
157 if (!source)
158 return -1;
159 if (source & RS_PCIROM)
Kevin O'Connorfce91892011-07-09 14:47:47 -0400160 return bootprio_find_pci_rom((void*)(u32)source, instance);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400161 struct romfile_s *file = (void*)(u32)source;
162 return bootprio_find_named_rom(file->name, instance);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500163}
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400164
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400165
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400166/****************************************************************
167 * Roms in CBFS
168 ****************************************************************/
169
Kevin O'Connor5e019082012-05-20 21:11:43 -0400170static struct rom_header *
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400171deploy_romfile(struct romfile_s *file)
Kevin O'Connor5e019082012-05-20 21:11:43 -0400172{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400173 u32 size = file->size;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400174 struct rom_header *rom = rom_reserve(size);
175 if (!rom) {
176 warn_noalloc();
177 return NULL;
178 }
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400179 int ret = file->copy(file, rom, size);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400180 if (ret <= 0)
181 return NULL;
182 return rom;
183}
184
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400185// Check if an option rom is at a hardcoded location or in CBFS.
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500186static struct rom_header *
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400187lookup_hardcode(struct pci_device *pci)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500188{
Kevin O'Connore2304262010-06-13 16:05:17 -0400189 char fname[17];
190 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400191 , pci->vendor, pci->device);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400192 struct romfile_s *file = romfile_find(fname);
Kevin O'Connor5e019082012-05-20 21:11:43 -0400193 if (file)
194 return deploy_romfile(file);
195 return NULL;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500196}
197
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400198// Run all roms in a given CBFS directory.
199static void
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500200run_file_roms(const char *prefix, int isvga, u64 *sources)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400201{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400202 struct romfile_s *file = NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400203 for (;;) {
Kevin O'Connore2304262010-06-13 16:05:17 -0400204 file = romfile_findprefix(prefix, file);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400205 if (!file)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400206 break;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400207 struct rom_header *rom = deploy_romfile(file);
208 if (rom) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400209 setRomSource(sources, rom, (u32)file);
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500210 init_optionrom(rom, 0, isvga);
211 }
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400212 }
213}
214
215
216/****************************************************************
217 * PCI roms
218 ****************************************************************/
219
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400220// Verify device is a vga device with legacy address decoding enabled.
Alex Williamson7adfd712013-03-20 10:58:47 -0600221int
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400222is_pci_vga(struct pci_device *pci)
223{
224 if (pci->class != PCI_CLASS_DISPLAY_VGA)
225 return 0;
226 u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
227 if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
228 return 0;
229 while (pci->parent) {
230 pci = pci->parent;
231 u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
232 if (!(ctrl & PCI_BRIDGE_CTL_VGA))
233 return 0;
234 }
235 return 1;
236}
237
Kevin O'Connor5e019082012-05-20 21:11:43 -0400238// Copy a rom to its permanent location below 1MiB
239static struct rom_header *
240copy_rom(struct rom_header *rom)
241{
242 u32 romsize = rom->size * 512;
243 struct rom_header *newrom = rom_reserve(romsize);
244 if (!newrom) {
245 warn_noalloc();
246 return NULL;
247 }
248 dprintf(4, "Copying option rom (size %d) from %p to %p\n"
249 , romsize, rom, newrom);
250 iomemcpy(newrom, rom, romsize);
251 return newrom;
252}
253
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500254// Map the option rom of a given PCI device.
255static struct rom_header *
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400256map_pcirom(struct pci_device *pci)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500257{
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400258 u16 bdf = pci->bdf;
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400259 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
260 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500261
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400262 if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
263 dprintf(6, "Skipping non-normal pci device (type=%x)\n"
264 , pci->header_type);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500265 return NULL;
266 }
267
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500268 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
269 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
270 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500271
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500272 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500273 orig &= ~PCI_ROM_ADDRESS_ENABLE;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500274 if (!sz || sz == 0xffffffff)
275 goto fail;
276
Kevin O'Connor64cf2fb2009-04-18 17:01:02 -0400277 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
278 // Don't try to map to a pci addresses at its max, in the last
279 // 4MiB of ram, or the first 16MiB of ram.
Kevin O'Connor62eea672008-12-06 11:57:45 -0500280 dprintf(6, "Preset rom address doesn't look valid\n");
281 goto fail;
282 }
283
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500284 // Looks like a rom - enable it.
285 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500286
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400287 struct rom_header *rom = (void*)orig;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500288 for (;;) {
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400289 dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
290 " bdf=%02x:%02x.%x)\n"
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400291 , rom, pci->vendor, pci->device
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400292 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500293 if (rom->signature != OPTION_ROM_SIGNATURE) {
294 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
295 goto fail;
296 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400297 struct pci_data *pd = get_pci_rom(rom);
298 if (! pd) {
Kevin O'Connor62eea672008-12-06 11:57:45 -0500299 dprintf(6, "No valid pci signature found\n");
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500300 goto fail;
301 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500302
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400303 if (pd->vendor == pci->vendor && pd->device == pci->device
304 && pd->type == PCIROM_CODETYPE_X86)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500305 // A match
306 break;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400307 dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
308 , pd->vendor, pd->device, pd->type);
309 if (pd->indicator & 0x80) {
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500310 dprintf(6, "No more images left\n");
311 goto fail;
312 }
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400313 rom = (void*)((u32)rom + pd->ilen * 512);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500314 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500315
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500316 rom = copy_rom(rom);
317 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500318 return rom;
319fail:
320 // Not valid - restore original and exit.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500321 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500322 return NULL;
323}
324
325// Attempt to map and initialize the option rom on a given PCI device.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400326static int
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'Connor862d5fb2011-06-20 22:19:17 -0400329 u16 bdf = pci->bdf;
Kevin O'Connor49cc72b2010-05-23 10:22:23 -0400330 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400331 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400332 , pci->vendor, pci->device);
333 struct rom_header *rom = lookup_hardcode(pci);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500334 if (! rom)
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400335 rom = map_pcirom(pci);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500336 if (! rom)
337 // No ROM present.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400338 return -1;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400339 setRomSource(sources, rom, RS_PCIROM | (u32)pci);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400340 return init_optionrom(rom, bdf, isvga);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500341}
342
343
344/****************************************************************
345 * Non-VGA option rom init
346 ****************************************************************/
347
348void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500349optionrom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400350{
351 if (! CONFIG_OPTIONROMS)
352 return;
353
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500354 dprintf(1, "Scan for option roms\n");
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500355 u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
356 memset(sources, 0, sizeof(sources));
Kevin O'Connor5e019082012-05-20 21:11:43 -0400357 u32 post_vga = rom_get_last();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500358
359 if (CONFIG_OPTIONROMS_DEPLOYED) {
360 // Option roms are already deployed on the system.
Kevin O'Connor5e019082012-05-20 21:11:43 -0400361 u32 pos = post_vga;
Kevin O'Connor3733f6f2013-02-17 12:44:23 -0500362 while (pos < rom_get_max()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400363 int ret = init_optionrom((void*)pos, 0, 0);
364 if (ret)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500365 pos += OPTION_ROM_ALIGN;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400366 else
Kevin O'Connor5e019082012-05-20 21:11:43 -0400367 pos = rom_get_last();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500368 }
369 } else {
370 // Find and deploy PCI roms.
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400371 struct pci_device *pci;
372 foreachpci(pci) {
Kevin O'Connor76b5e712011-06-21 22:52:51 -0400373 if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver)
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500374 continue;
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400375 init_pcirom(pci, 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500376 }
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400377
378 // Find and deploy CBFS roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500379 run_file_roms("genroms/", 0, sources);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500380 }
Kevin O'Connor5e019082012-05-20 21:11:43 -0400381 rom_reserve(0);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500382
383 // All option roms found and deployed - now build BEV/BCV vectors.
384
385 u32 pos = post_vga;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400386 while (pos < rom_get_last()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400387 struct rom_header *rom = (void*)pos;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500388 if (! is_valid_rom(rom)) {
389 pos += OPTION_ROM_ALIGN;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400390 continue;
391 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500392 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
393 struct pnp_data *pnp = get_pnp_rom(rom);
394 if (! pnp) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500395 // Legacy rom.
Kevin O'Connor031ef552010-12-27 19:26:57 -0500396 boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
397 , getRomPriority(sources, rom, 0));
Kevin O'Connor714325c2008-11-01 20:32:27 -0400398 continue;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500399 }
Kevin O'Connor1e157252012-01-14 11:55:35 -0500400 // PnP rom - check for BEV and BCV boot capabilities.
401 int instance = 0;
402 while (pnp) {
403 if (pnp->bev)
404 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
405 , getRomPriority(sources, rom, instance++));
406 else if (pnp->bcv)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500407 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
408 , getRomPriority(sources, rom, instance++));
Kevin O'Connor1e157252012-01-14 11:55:35 -0500409 else
410 break;
411 pnp = get_pnp_next(rom, pnp);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500412 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400413 }
414}
415
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500416
417/****************************************************************
418 * VGA init
419 ****************************************************************/
420
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500421static int S3ResumeVga;
Kevin O'Connor422263d2011-07-05 20:56:07 -0400422int ScreenAndDebug;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400423struct rom_header *VgaROM;
Kevin O'Connor8b0c5092011-07-05 20:50:32 -0400424
Kevin O'Connor714325c2008-11-01 20:32:27 -0400425// Call into vga code to turn on console.
426void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500427vgarom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400428{
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500429 if (! CONFIG_OPTIONROMS)
430 return;
431
Kevin O'Connor714325c2008-11-01 20:32:27 -0400432 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500433
Kevin O'Connor422263d2011-07-05 20:56:07 -0400434 // Load some config settings that impact VGA.
Kevin O'Connore010d852011-07-05 20:47:35 -0400435 EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
Kevin O'Connor897fb112013-02-07 23:32:48 -0500436 S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", CONFIG_QEMU);
Kevin O'Connor422263d2011-07-05 20:56:07 -0400437 ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
Kevin O'Connore010d852011-07-05 20:47:35 -0400438
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500439 if (CONFIG_OPTIONROMS_DEPLOYED) {
440 // Option roms are already deployed on the system.
Kevin O'Connore7739302009-07-26 19:16:09 -0400441 init_optionrom((void*)BUILD_ROM_START, 0, 1);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500442 } else {
Kevin O'Connore8264652010-09-15 22:06:19 -0400443 // Clear option rom memory
Kevin O'Connor3733f6f2013-02-17 12:44:23 -0500444 memset((void*)BUILD_ROM_START, 0, rom_get_max() - BUILD_ROM_START);
Kevin O'Connore8264652010-09-15 22:06:19 -0400445
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500446 // Find and deploy PCI VGA rom.
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400447 struct pci_device *pci;
448 foreachpci(pci) {
449 if (!is_pci_vga(pci))
450 continue;
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400451 vgahook_setup(pci);
Kevin O'Connor862d5fb2011-06-20 22:19:17 -0400452 init_pcirom(pci, 1, NULL);
453 break;
454 }
Kevin O'Connor09880da2009-06-17 20:35:41 -0400455
456 // Find and deploy CBFS vga-style roms not associated with a device.
Kevin O'Connorbca3a872010-12-24 14:42:42 -0500457 run_file_roms("vgaroms/", 1, NULL);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400458 }
Kevin O'Connor5e019082012-05-20 21:11:43 -0400459 rom_reserve(0);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400460
Kevin O'Connor5e019082012-05-20 21:11:43 -0400461 if (rom_get_last() == BUILD_ROM_START)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400462 // No VGA rom found
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400463 return;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400464
Kevin O'Connor5e019082012-05-20 21:11:43 -0400465 VgaROM = (void*)BUILD_ROM_START;
Kevin O'Connorafbed1b2010-06-28 07:34:53 -0400466 enable_vga_console();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400467}
Kevin O'Connord282af72009-07-04 04:10:32 -0400468
469void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500470s3_resume_vga(void)
Kevin O'Connord282af72009-07-04 04:10:32 -0400471{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500472 if (!S3ResumeVga)
Kevin O'Connord282af72009-07-04 04:10:32 -0400473 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400474 if (!VgaROM || ! is_valid_rom(VgaROM))
Kevin O'Connord282af72009-07-04 04:10:32 -0400475 return;
Kevin O'Connor5e019082012-05-20 21:11:43 -0400476 callrom(VgaROM, 0);
Kevin O'Connord282af72009-07-04 04:10:32 -0400477}