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"
139 // Check if a valid option rom has a pnp struct; return it if so.
140 static struct pnp_data *
141 get_pnp_rom(struct rom_header *rom)
143 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
144 if (pnp->signature != PNP_SIGNATURE)
149 // Check for multiple pnp option rom headers.
150 static struct pnp_data *
151 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
153 if (! pnp->nextoffset)
155 pnp = (void*)((u8*)rom + pnp->nextoffset);
156 if (pnp->signature != PNP_SIGNATURE)
161 // Check if a valid option rom has a pci struct; return it if so.
162 static struct pci_data *
163 get_pci_rom(struct rom_header *rom)
165 struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
166 if (pci->signature != PCI_ROM_SIGNATURE)
171 // Return start of code in 0xc0000-0xf0000 space.
172 static inline u32 _max_rom(void) {
173 extern u8 code32flat_start[], code32init_end[];
174 return CONFIG_RELOCATE_INIT ? (u32)code32init_end : (u32)code32flat_start;
176 // Return the memory position up to which roms may be located.
177 static inline u32 max_rom(void) {
178 u32 end = _max_rom();
179 return end > BUILD_BIOS_ADDR ? BUILD_BIOS_ADDR : end;
182 // Copy a rom to its permanent location below 1MiB
183 static struct rom_header *
184 copy_rom(struct rom_header *rom)
186 u32 romsize = rom->size * 512;
187 if (RomEnd + romsize > max_rom()) {
188 // Option rom doesn't fit.
192 dprintf(4, "Copying option rom (size %d) from %p to %x\n"
193 , romsize, rom, RomEnd);
194 iomemcpy((void*)RomEnd, rom, romsize);
195 return (void*)RomEnd;
198 // Run rom init code and note rom size.
200 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
202 if (! is_valid_rom(rom))
205 if (isvga || get_pnp_rom(rom))
206 // Only init vga and PnP roms here.
209 RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
214 #define RS_PCIROM (1LL<<33)
217 setRomSource(u64 *sources, struct rom_header *rom, u64 source)
220 sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
224 /****************************************************************
226 ****************************************************************/
228 // Check if an option rom is at a hardcoded location or in CBFS.
229 static struct rom_header *
230 lookup_hardcode(u32 vendev)
232 if (OPTIONROM_VENDEV_1
233 && ((OPTIONROM_VENDEV_1 >> 16)
234 | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
235 return copy_rom((void*)OPTIONROM_MEM_1);
236 if (OPTIONROM_VENDEV_2
237 && ((OPTIONROM_VENDEV_2 >> 16)
238 | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
239 return copy_rom((void*)OPTIONROM_MEM_2);
241 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
242 , pci_vd_to_ven(vendev), pci_vd_to_dev(vendev));
243 int ret = romfile_copy(romfile_find(fname), (void*)RomEnd
244 , max_rom() - RomEnd);
247 return (void*)RomEnd;
250 // Run all roms in a given CBFS directory.
252 run_file_roms(const char *prefix, int isvga, u64 *sources)
256 file = romfile_findprefix(prefix, file);
259 struct rom_header *rom = (void*)RomEnd;
260 int ret = romfile_copy(file, rom, max_rom() - RomEnd);
262 setRomSource(sources, rom, file);
263 init_optionrom(rom, 0, isvga);
269 /****************************************************************
271 ****************************************************************/
273 // Map the option rom of a given PCI device.
274 static struct rom_header *
275 map_pcirom(u16 bdf, u32 vendev)
277 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
278 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
280 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
281 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
282 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
286 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
287 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
288 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
290 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
291 orig &= ~PCI_ROM_ADDRESS_ENABLE;
292 if (!sz || sz == 0xffffffff)
295 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
296 // Don't try to map to a pci addresses at its max, in the last
297 // 4MiB of ram, or the first 16MiB of ram.
298 dprintf(6, "Preset rom address doesn't look valid\n");
302 // Looks like a rom - enable it.
303 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
305 struct rom_header *rom = (void*)orig;
307 dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
308 " bdf=%02x:%02x.%x)\n"
309 , rom, pci_vd_to_ven(vendev), pci_vd_to_dev(vendev)
310 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
311 if (rom->signature != OPTION_ROM_SIGNATURE) {
312 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
315 struct pci_data *pci = get_pci_rom(rom);
317 dprintf(6, "No valid pci signature found\n");
321 u32 vd = pci_vd(pci->vendor, pci->device);
322 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
325 dprintf(6, "Didn't match dev/ven (got %08x) or type (got %d)\n"
327 if (pci->indicator & 0x80) {
328 dprintf(6, "No more images left\n");
331 rom = (void*)((u32)rom + pci->ilen * 512);
335 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
338 // Not valid - restore original and exit.
339 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
343 // Attempt to map and initialize the option rom on a given PCI device.
345 init_pcirom(u16 bdf, int isvga, u64 *sources)
347 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
348 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
349 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
350 , pci_vd_to_ven(vendev), pci_vd_to_dev(vendev));
351 struct rom_header *rom = lookup_hardcode(vendev);
353 rom = map_pcirom(bdf, vendev);
357 setRomSource(sources, rom, RS_PCIROM | bdf);
358 return init_optionrom(rom, bdf, isvga);
362 /****************************************************************
363 * Non-VGA option rom init
364 ****************************************************************/
367 optionrom_setup(void)
369 if (! CONFIG_OPTIONROMS)
372 dprintf(1, "Scan for option roms\n");
373 u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
374 memset(sources, 0, sizeof(sources));
375 u32 post_vga = RomEnd;
377 if (CONFIG_OPTIONROMS_DEPLOYED) {
378 // Option roms are already deployed on the system.
380 while (pos < max_rom()) {
381 int ret = init_optionrom((void*)pos, 0, 0);
383 pos += OPTION_ROM_ALIGN;
388 // Find and deploy PCI roms.
390 foreachpci(bdf, max) {
391 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
392 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
393 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
395 init_pcirom(bdf, 0, sources);
398 // Find and deploy CBFS roms not associated with a device.
399 run_file_roms("genroms/", 0, sources);
402 // All option roms found and deployed - now build BEV/BCV vectors.
405 while (pos < RomEnd) {
406 struct rom_header *rom = (void*)pos;
407 if (! is_valid_rom(rom)) {
408 pos += OPTION_ROM_ALIGN;
411 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
412 struct pnp_data *pnp = get_pnp_rom(rom);
415 add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
420 // Can boot system - add to IPL list.
421 add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
423 // Check for BCV (there may be multiple).
424 while (pnp && pnp->bcv) {
425 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
426 pnp = get_pnp_next(rom, pnp);
432 /****************************************************************
434 ****************************************************************/
436 // Call into vga code to turn on console.
440 if (! CONFIG_OPTIONROMS)
443 dprintf(1, "Scan for VGA option rom\n");
445 if (CONFIG_OPTIONROMS_DEPLOYED) {
446 // Option roms are already deployed on the system.
447 init_optionrom((void*)BUILD_ROM_START, 0, 1);
449 // Clear option rom memory
450 memset((void*)RomEnd, 0, _max_rom() - RomEnd);
452 // Find and deploy PCI VGA rom.
453 int bdf = VGAbdf = pci_find_vga();
455 init_pcirom(bdf, 1, NULL);
457 // Find and deploy CBFS vga-style roms not associated with a device.
458 run_file_roms("vgaroms/", 1, NULL);
461 if (RomEnd == BUILD_ROM_START) {
463 RomEnd += OPTION_ROM_ALIGN;
467 enable_vga_console();
471 s3_resume_vga_init(void)
473 if (!CONFIG_S3_RESUME_VGA_INIT)
475 struct rom_header *rom = (void*)BUILD_ROM_START;
476 if (! is_valid_rom(rom))