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_*
19 /****************************************************************
21 ****************************************************************/
68 #define OPTION_ROM_SIGNATURE 0xaa55
69 #define OPTION_ROM_ALIGN 2048
70 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
71 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
72 #define PCIROM_CODETYPE_X86 0
74 // The end of the last deployed rom.
75 u32 RomEnd = BUILD_ROM_START;
78 /****************************************************************
80 ****************************************************************/
82 // Execute a given option rom.
84 __callrom(struct rom_header *rom, u16 offset, u16 bdf)
86 u16 seg = FLATPTR_TO_SEG(rom);
87 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
90 memset(&br, 0, sizeof(br));
96 br.di = get_pnp_offset();
97 br.code = SEGOFF(seg, offset);
102 debug_serial_setup();
105 // Execute a given option rom at the standard entry vector.
107 callrom(struct rom_header *rom, u16 bdf)
109 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
112 // Execute a BCV option rom registered via add_bcv().
114 call_bcv(u16 seg, u16 ip)
116 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
119 // Verify that an option rom looks valid
121 is_valid_rom(struct rom_header *rom)
123 dprintf(6, "Checking rom %p (sig %x size %d)\n"
124 , rom, rom->signature, rom->size);
125 if (rom->signature != OPTION_ROM_SIGNATURE)
129 u32 len = rom->size * 512;
130 u8 sum = checksum(rom, len);
132 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
134 if (CONFIG_OPTIONROMS_CHECKSUM)
140 // Check if a valid option rom has a pnp struct; return it if so.
141 static struct pnp_data *
142 get_pnp_rom(struct rom_header *rom)
144 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
145 if (pnp->signature != PNP_SIGNATURE)
150 // Check for multiple pnp option rom headers.
151 static struct pnp_data *
152 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
154 if (! pnp->nextoffset)
156 pnp = (void*)((u8*)rom + pnp->nextoffset);
157 if (pnp->signature != PNP_SIGNATURE)
162 // Check if a valid option rom has a pci struct; return it if so.
163 static struct pci_data *
164 get_pci_rom(struct rom_header *rom)
166 struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
167 if (pd->signature != PCI_ROM_SIGNATURE)
172 // Return start of code in 0xc0000-0xf0000 space.
173 static inline u32 _max_rom(void) {
174 extern u8 code32flat_start[], code32init_end[];
175 return CONFIG_RELOCATE_INIT ? (u32)code32init_end : (u32)code32flat_start;
177 // Return the memory position up to which roms may be located.
178 static inline u32 max_rom(void) {
179 u32 end = _max_rom();
180 return end > BUILD_BIOS_ADDR ? BUILD_BIOS_ADDR : end;
183 // Copy a rom to its permanent location below 1MiB
184 static struct rom_header *
185 copy_rom(struct rom_header *rom)
187 u32 romsize = rom->size * 512;
188 if (RomEnd + romsize > max_rom()) {
189 // Option rom doesn't fit.
193 dprintf(4, "Copying option rom (size %d) from %p to %x\n"
194 , romsize, rom, RomEnd);
195 iomemcpy((void*)RomEnd, rom, romsize);
196 return (void*)RomEnd;
199 // Run rom init code and note rom size.
201 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
203 if (! is_valid_rom(rom))
206 if (isvga || get_pnp_rom(rom))
207 // Only init vga and PnP roms here.
210 RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
215 #define RS_PCIROM (1LL<<33)
218 setRomSource(u64 *sources, struct rom_header *rom, u64 source)
221 sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
225 getRomPriority(u64 *sources, struct rom_header *rom, int instance)
227 u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
230 if (source & RS_PCIROM)
231 return bootprio_find_pci_rom(source, instance);
232 return bootprio_find_named_rom(romfile_name(source), instance);
235 /****************************************************************
237 ****************************************************************/
239 // Check if an option rom is at a hardcoded location or in CBFS.
240 static struct rom_header *
241 lookup_hardcode(struct pci_device *pci)
244 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
245 , pci->vendor, pci->device);
246 int ret = romfile_copy(romfile_find(fname), (void*)RomEnd
247 , max_rom() - RomEnd);
250 return (void*)RomEnd;
253 // Run all roms in a given CBFS directory.
255 run_file_roms(const char *prefix, int isvga, u64 *sources)
259 file = romfile_findprefix(prefix, file);
262 struct rom_header *rom = (void*)RomEnd;
263 int ret = romfile_copy(file, rom, max_rom() - RomEnd);
265 setRomSource(sources, rom, file);
266 init_optionrom(rom, 0, isvga);
272 /****************************************************************
274 ****************************************************************/
276 // Verify device is a vga device with legacy address decoding enabled.
278 is_pci_vga(struct pci_device *pci)
280 if (pci->class != PCI_CLASS_DISPLAY_VGA)
282 u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
283 if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
285 while (pci->parent) {
287 u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
288 if (!(ctrl & PCI_BRIDGE_CTL_VGA))
294 // Map the option rom of a given PCI device.
295 static struct rom_header *
296 map_pcirom(struct pci_device *pci)
299 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
300 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
302 if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
303 dprintf(6, "Skipping non-normal pci device (type=%x)\n"
308 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
309 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
310 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
312 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
313 orig &= ~PCI_ROM_ADDRESS_ENABLE;
314 if (!sz || sz == 0xffffffff)
317 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
318 // Don't try to map to a pci addresses at its max, in the last
319 // 4MiB of ram, or the first 16MiB of ram.
320 dprintf(6, "Preset rom address doesn't look valid\n");
324 // Looks like a rom - enable it.
325 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
327 struct rom_header *rom = (void*)orig;
329 dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
330 " bdf=%02x:%02x.%x)\n"
331 , rom, pci->vendor, pci->device
332 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
333 if (rom->signature != OPTION_ROM_SIGNATURE) {
334 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
337 struct pci_data *pd = get_pci_rom(rom);
339 dprintf(6, "No valid pci signature found\n");
343 if (pd->vendor == pci->vendor && pd->device == pci->device
344 && pd->type == PCIROM_CODETYPE_X86)
347 dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
348 , pd->vendor, pd->device, pd->type);
349 if (pd->indicator & 0x80) {
350 dprintf(6, "No more images left\n");
353 rom = (void*)((u32)rom + pd->ilen * 512);
357 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
360 // Not valid - restore original and exit.
361 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
365 // Attempt to map and initialize the option rom on a given PCI device.
367 init_pcirom(struct pci_device *pci, int isvga, u64 *sources)
370 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
371 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
372 , pci->vendor, pci->device);
373 struct rom_header *rom = lookup_hardcode(pci);
375 rom = map_pcirom(pci);
379 setRomSource(sources, rom, RS_PCIROM | bdf);
380 return init_optionrom(rom, bdf, isvga);
384 /****************************************************************
385 * Non-VGA option rom init
386 ****************************************************************/
389 optionrom_setup(void)
391 if (! CONFIG_OPTIONROMS)
394 dprintf(1, "Scan for option roms\n");
395 u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
396 memset(sources, 0, sizeof(sources));
397 u32 post_vga = RomEnd;
399 if (CONFIG_OPTIONROMS_DEPLOYED) {
400 // Option roms are already deployed on the system.
402 while (pos < max_rom()) {
403 int ret = init_optionrom((void*)pos, 0, 0);
405 pos += OPTION_ROM_ALIGN;
410 // Find and deploy PCI roms.
411 struct pci_device *pci;
413 if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver)
415 init_pcirom(pci, 0, sources);
418 // Find and deploy CBFS roms not associated with a device.
419 run_file_roms("genroms/", 0, sources);
422 // All option roms found and deployed - now build BEV/BCV vectors.
425 while (pos < RomEnd) {
426 struct rom_header *rom = (void*)pos;
427 if (! is_valid_rom(rom)) {
428 pos += OPTION_ROM_ALIGN;
431 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
432 struct pnp_data *pnp = get_pnp_rom(rom);
435 boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
436 , getRomPriority(sources, rom, 0));
441 // Can boot system - add to IPL list.
442 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
443 , getRomPriority(sources, rom, 0));
445 // Check for BCV (there may be multiple).
447 while (pnp && pnp->bcv) {
448 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
449 , getRomPriority(sources, rom, instance++));
450 pnp = get_pnp_next(rom, pnp);
457 /****************************************************************
459 ****************************************************************/
461 // Call into vga code to turn on console.
465 if (! CONFIG_OPTIONROMS)
468 dprintf(1, "Scan for VGA option rom\n");
470 if (CONFIG_OPTIONROMS_DEPLOYED) {
471 // Option roms are already deployed on the system.
472 init_optionrom((void*)BUILD_ROM_START, 0, 1);
474 // Clear option rom memory
475 memset((void*)RomEnd, 0, _max_rom() - RomEnd);
477 // Find and deploy PCI VGA rom.
478 struct pci_device *pci;
480 if (!is_pci_vga(pci))
483 init_pcirom(pci, 1, NULL);
487 // Find and deploy CBFS vga-style roms not associated with a device.
488 run_file_roms("vgaroms/", 1, NULL);
491 if (RomEnd == BUILD_ROM_START) {
493 RomEnd += OPTION_ROM_ALIGN;
497 enable_vga_console();
501 s3_resume_vga_init(void)
503 if (!CONFIG_S3_RESUME_VGA_INIT)
505 struct rom_header *rom = (void*)BUILD_ROM_START;
506 if (! is_valid_rom(rom))