1 // Option rom scanning code.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "bregs.h" // struct bregs
9 #include "farptr.h" // FLATPTR_TO_SEG
10 #include "config.h" // CONFIG_*
11 #include "util.h" // dprintf
12 #include "pci.h" // foreachpci
13 #include "pci_regs.h" // PCI_ROM_ADDRESS
14 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
15 #include "boot.h" // IPL
16 #include "paravirt.h" // qemu_cfg_*
17 #include "optionroms.h" // struct rom_header
19 /****************************************************************
21 ****************************************************************/
23 // The end of the last deployed rom.
24 u32 RomEnd = BUILD_ROM_START;
27 /****************************************************************
29 ****************************************************************/
31 // Execute a given option rom.
33 __callrom(struct rom_header *rom, u16 offset, u16 bdf)
35 u16 seg = FLATPTR_TO_SEG(rom);
36 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
39 memset(&br, 0, sizeof(br));
45 br.di = get_pnp_offset();
46 br.code = SEGOFF(seg, offset);
54 // Execute a given option rom at the standard entry vector.
56 callrom(struct rom_header *rom, u16 bdf)
58 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
61 // Execute a BCV option rom registered via add_bcv().
63 call_bcv(u16 seg, u16 ip)
65 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
68 static int EnforceChecksum;
70 // Verify that an option rom looks valid
72 is_valid_rom(struct rom_header *rom)
74 dprintf(6, "Checking rom %p (sig %x size %d)\n"
75 , rom, rom->signature, rom->size);
76 if (rom->signature != OPTION_ROM_SIGNATURE)
80 u32 len = rom->size * 512;
81 u8 sum = checksum(rom, len);
83 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
91 // Check if a valid option rom has a pnp struct; return it if so.
92 static struct pnp_data *
93 get_pnp_rom(struct rom_header *rom)
95 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
96 if (pnp->signature != PNP_SIGNATURE)
101 // Check for multiple pnp option rom headers.
102 static struct pnp_data *
103 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
105 if (! pnp->nextoffset)
107 pnp = (void*)((u8*)rom + pnp->nextoffset);
108 if (pnp->signature != PNP_SIGNATURE)
113 // Check if a valid option rom has a pci struct; return it if so.
114 static struct pci_data *
115 get_pci_rom(struct rom_header *rom)
117 struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
118 if (pd->signature != PCI_ROM_SIGNATURE)
123 // Return start of code in 0xc0000-0xf0000 space.
124 static inline u32 _max_rom(void) {
125 extern u8 code32flat_start[], code32init_end[];
126 return CONFIG_RELOCATE_INIT ? (u32)code32init_end : (u32)code32flat_start;
128 // Return the memory position up to which roms may be located.
129 static inline u32 max_rom(void) {
130 u32 end = _max_rom();
131 return end > BUILD_BIOS_ADDR ? BUILD_BIOS_ADDR : end;
134 // Copy a rom to its permanent location below 1MiB
135 static struct rom_header *
136 copy_rom(struct rom_header *rom)
138 u32 romsize = rom->size * 512;
139 if (RomEnd + romsize > max_rom()) {
140 // Option rom doesn't fit.
144 dprintf(4, "Copying option rom (size %d) from %p to %x\n"
145 , romsize, rom, RomEnd);
146 iomemcpy((void*)RomEnd, rom, romsize);
147 return (void*)RomEnd;
150 // Run rom init code and note rom size.
152 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
154 if (! is_valid_rom(rom))
157 if (isvga || get_pnp_rom(rom))
158 // Only init vga and PnP roms here.
161 RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
166 #define RS_PCIROM (1LL<<33)
169 setRomSource(u64 *sources, struct rom_header *rom, u64 source)
172 sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
176 getRomPriority(u64 *sources, struct rom_header *rom, int instance)
178 u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
181 if (source & RS_PCIROM)
182 return bootprio_find_pci_rom((void*)(u32)source, instance);
183 return bootprio_find_named_rom(romfile_name(source), instance);
187 /****************************************************************
189 ****************************************************************/
191 // Check if an option rom is at a hardcoded location or in CBFS.
192 static struct rom_header *
193 lookup_hardcode(struct pci_device *pci)
196 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
197 , pci->vendor, pci->device);
198 int ret = romfile_copy(romfile_find(fname), (void*)RomEnd
199 , max_rom() - RomEnd);
202 return (void*)RomEnd;
205 // Run all roms in a given CBFS directory.
207 run_file_roms(const char *prefix, int isvga, u64 *sources)
211 file = romfile_findprefix(prefix, file);
214 struct rom_header *rom = (void*)RomEnd;
215 int ret = romfile_copy(file, rom, max_rom() - RomEnd);
217 setRomSource(sources, rom, file);
218 init_optionrom(rom, 0, isvga);
224 /****************************************************************
226 ****************************************************************/
228 // Verify device is a vga device with legacy address decoding enabled.
230 is_pci_vga(struct pci_device *pci)
232 if (pci->class != PCI_CLASS_DISPLAY_VGA)
234 u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
235 if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
237 while (pci->parent) {
239 u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
240 if (!(ctrl & PCI_BRIDGE_CTL_VGA))
246 // Map the option rom of a given PCI device.
247 static struct rom_header *
248 map_pcirom(struct pci_device *pci)
251 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
252 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
254 if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
255 dprintf(6, "Skipping non-normal pci device (type=%x)\n"
260 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
261 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
262 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
264 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
265 orig &= ~PCI_ROM_ADDRESS_ENABLE;
266 if (!sz || sz == 0xffffffff)
269 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
270 // Don't try to map to a pci addresses at its max, in the last
271 // 4MiB of ram, or the first 16MiB of ram.
272 dprintf(6, "Preset rom address doesn't look valid\n");
276 // Looks like a rom - enable it.
277 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
279 struct rom_header *rom = (void*)orig;
281 dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
282 " bdf=%02x:%02x.%x)\n"
283 , rom, pci->vendor, pci->device
284 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
285 if (rom->signature != OPTION_ROM_SIGNATURE) {
286 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
289 struct pci_data *pd = get_pci_rom(rom);
291 dprintf(6, "No valid pci signature found\n");
295 if (pd->vendor == pci->vendor && pd->device == pci->device
296 && pd->type == PCIROM_CODETYPE_X86)
299 dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
300 , pd->vendor, pd->device, pd->type);
301 if (pd->indicator & 0x80) {
302 dprintf(6, "No more images left\n");
305 rom = (void*)((u32)rom + pd->ilen * 512);
309 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
312 // Not valid - restore original and exit.
313 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
317 // Attempt to map and initialize the option rom on a given PCI device.
319 init_pcirom(struct pci_device *pci, int isvga, u64 *sources)
322 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
323 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
324 , pci->vendor, pci->device);
325 struct rom_header *rom = lookup_hardcode(pci);
327 rom = map_pcirom(pci);
331 setRomSource(sources, rom, RS_PCIROM | (u32)pci);
332 return init_optionrom(rom, bdf, isvga);
336 /****************************************************************
337 * Non-VGA option rom init
338 ****************************************************************/
341 optionrom_setup(void)
343 if (! CONFIG_OPTIONROMS)
346 dprintf(1, "Scan for option roms\n");
347 u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
348 memset(sources, 0, sizeof(sources));
349 u32 post_vga = RomEnd;
351 if (CONFIG_OPTIONROMS_DEPLOYED) {
352 // Option roms are already deployed on the system.
354 while (pos < max_rom()) {
355 int ret = init_optionrom((void*)pos, 0, 0);
357 pos += OPTION_ROM_ALIGN;
362 // Find and deploy PCI roms.
363 struct pci_device *pci;
365 if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver)
367 init_pcirom(pci, 0, sources);
370 // Find and deploy CBFS roms not associated with a device.
371 run_file_roms("genroms/", 0, sources);
374 // All option roms found and deployed - now build BEV/BCV vectors.
377 while (pos < RomEnd) {
378 struct rom_header *rom = (void*)pos;
379 if (! is_valid_rom(rom)) {
380 pos += OPTION_ROM_ALIGN;
383 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
384 struct pnp_data *pnp = get_pnp_rom(rom);
387 boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
388 , getRomPriority(sources, rom, 0));
391 // PnP rom - check for BEV and BCV boot capabilities.
395 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
396 , getRomPriority(sources, rom, instance++));
398 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
399 , getRomPriority(sources, rom, instance++));
402 pnp = get_pnp_next(rom, pnp);
408 /****************************************************************
410 ****************************************************************/
412 static int S3ResumeVgaInit;
415 // Call into vga code to turn on console.
419 if (! CONFIG_OPTIONROMS)
422 dprintf(1, "Scan for VGA option rom\n");
424 // Load some config settings that impact VGA.
425 EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
426 S3ResumeVgaInit = romfile_loadint("etc/s3-resume-vga-init", 0);
427 ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
429 if (CONFIG_OPTIONROMS_DEPLOYED) {
430 // Option roms are already deployed on the system.
431 init_optionrom((void*)BUILD_ROM_START, 0, 1);
433 // Clear option rom memory
434 memset((void*)RomEnd, 0, max_rom() - RomEnd);
436 // Find and deploy PCI VGA rom.
437 struct pci_device *pci;
439 if (!is_pci_vga(pci))
442 init_pcirom(pci, 1, NULL);
446 // Find and deploy CBFS vga-style roms not associated with a device.
447 run_file_roms("vgaroms/", 1, NULL);
450 if (RomEnd == BUILD_ROM_START) {
452 RomEnd += OPTION_ROM_ALIGN;
456 enable_vga_console();
460 s3_resume_vga_init(void)
462 if (!S3ResumeVgaInit)
464 struct rom_header *rom = (void*)BUILD_ROM_START;
465 if (! is_valid_rom(rom))