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" // pci_find_class
13 #include "pci_regs.h" // PCI_ROM_ADDRESS
14 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
15 #include "boot.h" // IPL
18 /****************************************************************
20 ****************************************************************/
67 #define OPTION_ROM_SIGNATURE 0xaa55
68 #define OPTION_ROM_ALIGN 2048
69 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
70 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
71 #define PCIROM_CODETYPE_X86 0
73 // The end of the last deployed rom.
77 /****************************************************************
79 ****************************************************************/
81 // Execute a given option rom.
83 __callrom(struct rom_header *rom, u16 offset, u16 bdf)
85 u16 seg = FLATPTR_TO_SEG(rom);
86 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
89 memset(&br, 0, sizeof(br));
94 br.di = get_pnp_offset();
102 // Execute a given option rom at the standard entry vector.
104 callrom(struct rom_header *rom, u16 bdf)
106 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
109 // Execute a BCV option rom registered via add_bcv().
111 call_bcv(u16 seg, u16 ip)
113 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
116 // Verify that an option rom looks valid
118 is_valid_rom(struct rom_header *rom)
120 dprintf(6, "Checking rom %p (sig %x size %d)\n"
121 , rom, rom->signature, rom->size);
122 if (rom->signature != OPTION_ROM_SIGNATURE)
126 u32 len = rom->size * 512;
127 u8 sum = checksum(rom, len);
129 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
136 // Check if a valid option rom has a pnp struct; return it if so.
137 static struct pnp_data *
138 get_pnp_rom(struct rom_header *rom)
140 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
141 if (pnp->signature != PNP_SIGNATURE)
146 // Check for multiple pnp option rom headers.
147 static struct pnp_data *
148 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
150 if (! pnp->nextoffset)
152 pnp = (void*)((u8*)rom + pnp->nextoffset);
153 if (pnp->signature != PNP_SIGNATURE)
158 // Check if a valid option rom has a pci struct; return it if so.
159 static struct pci_data *
160 get_pci_rom(struct rom_header *rom)
162 struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
163 if (pci->signature != PCI_ROM_SIGNATURE)
168 // Copy a rom to its permanent location below 1MiB
169 static struct rom_header *
170 copy_rom(struct rom_header *rom)
172 u32 romsize = rom->size * 512;
173 if (RomEnd + romsize > BUILD_BIOS_ADDR) {
174 // Option rom doesn't fit.
175 dprintf(1, "Option rom %p doesn't fit.\n", rom);
178 dprintf(4, "Copying option rom (size %d) from %p to %x\n"
179 , romsize, rom, RomEnd);
180 memcpy((void*)RomEnd, rom, romsize);
181 return (void*)RomEnd;
184 // Run rom init code and note rom size.
186 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
188 if (! is_valid_rom(rom))
191 if (isvga || get_pnp_rom(rom))
192 // Only init vga and PnP roms here.
195 RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
201 /****************************************************************
203 ****************************************************************/
205 // Check if an option rom is at a hardcoded location or in CBFS.
206 static struct rom_header *
207 lookup_hardcode(u32 vendev)
209 if (OPTIONROM_VENDEV_1
210 && ((OPTIONROM_VENDEV_1 >> 16)
211 | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
212 return copy_rom((void*)OPTIONROM_MEM_1);
213 if (OPTIONROM_VENDEV_2
214 && ((OPTIONROM_VENDEV_2 >> 16)
215 | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
216 return copy_rom((void*)OPTIONROM_MEM_2);
217 int ret = cbfs_copy_optionrom((void*)RomEnd, BUILD_BIOS_ADDR - RomEnd
221 return (void*)RomEnd;
224 // Run all roms in a given CBFS directory.
226 run_cbfs_roms(const char *prefix, int isvga)
228 struct cbfs_file *file = NULL;
231 file = cbfs_finddataprefix(prefix, file, &iscomp);
234 int ret = cbfs_copyfile(file, (void*)RomEnd, BUILD_BIOS_ADDR - RomEnd
237 init_optionrom((void*)RomEnd, 0, isvga);
242 /****************************************************************
244 ****************************************************************/
246 // Map the option rom of a given PCI device.
247 static struct rom_header *
248 map_pcirom(u16 bdf, u32 vendev)
250 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
251 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
253 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
254 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
255 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
259 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
260 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
261 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
263 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
264 orig &= ~PCI_ROM_ADDRESS_ENABLE;
265 if (!sz || sz == 0xffffffff)
268 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
269 // Don't try to map to a pci addresses at its max, in the last
270 // 4MiB of ram, or the first 16MiB of ram.
271 dprintf(6, "Preset rom address doesn't look valid\n");
275 // Looks like a rom - enable it.
276 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
278 struct rom_header *rom = (void*)orig;
280 dprintf(5, "Inspecting possible rom at %p (dv=%08x bdf=%x)\n"
282 if (rom->signature != OPTION_ROM_SIGNATURE) {
283 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
286 struct pci_data *pci = get_pci_rom(rom);
288 dprintf(6, "No valid pci signature found\n");
292 u32 vd = (pci->device << 16) | pci->vendor;
293 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
296 dprintf(6, "Didn't match dev/ven (got %08x) or type (got %d)\n"
298 if (pci->indicator & 0x80) {
299 dprintf(6, "No more images left\n");
302 rom = (void*)((u32)rom + pci->ilen * 512);
306 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
309 // Not valid - restore original and exit.
310 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
314 // Attempt to map and initialize the option rom on a given PCI device.
316 init_pcirom(u16 bdf, int isvga)
318 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
319 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %08x)\n"
320 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
322 struct rom_header *rom = lookup_hardcode(vendev);
324 rom = map_pcirom(bdf, vendev);
328 return init_optionrom(rom, bdf, isvga);
332 /****************************************************************
333 * Non-VGA option rom init
334 ****************************************************************/
339 if (! CONFIG_OPTIONROMS)
342 dprintf(1, "Scan for option roms\n");
344 u32 post_vga = RomEnd;
346 if (CONFIG_OPTIONROMS_DEPLOYED) {
347 // Option roms are already deployed on the system.
349 while (pos < BUILD_BIOS_ADDR) {
350 int ret = init_optionrom((void*)pos, 0, 0);
352 pos += OPTION_ROM_ALIGN;
357 // Find and deploy PCI roms.
359 foreachpci(bdf, max) {
360 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
361 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
362 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
367 // Find and deploy CBFS roms not associated with a device.
368 run_cbfs_roms("genroms/", 0);
371 // All option roms found and deployed - now build BEV/BCV vectors.
374 while (pos < RomEnd) {
375 struct rom_header *rom = (void*)pos;
376 if (! is_valid_rom(rom)) {
377 pos += OPTION_ROM_ALIGN;
380 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
381 struct pnp_data *pnp = get_pnp_rom(rom);
384 add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
389 // Can boot system - add to IPL list.
390 add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
392 // Check for BCV (there may be multiple).
393 while (pnp && pnp->bcv) {
394 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
395 pnp = get_pnp_next(rom, pnp);
401 /****************************************************************
403 ****************************************************************/
405 // Call into vga code to turn on console.
410 RomEnd = BUILD_ROM_START;
412 if (! CONFIG_OPTIONROMS)
415 dprintf(1, "Scan for VGA option rom\n");
417 if (CONFIG_OPTIONROMS_DEPLOYED) {
418 // Option roms are already deployed on the system.
419 init_optionrom((void*)BUILD_ROM_START, 0, 1);
421 // Find and deploy PCI VGA rom.
422 int bdf = VGAbdf = pci_find_vga();
426 // Find and deploy CBFS vga-style roms not associated with a device.
427 run_cbfs_roms("vgaroms/", 1);
430 if (RomEnd == BUILD_ROM_START) {
432 RomEnd += OPTION_ROM_ALIGN;
436 dprintf(1, "Turning on vga console\n");
438 memset(&br, 0, sizeof(br));
440 call16_int(0x10, &br);
443 printf("Starting SeaBIOS (version %s)\n\n", VERSION);
449 if (!CONFIG_S3_RESUME_VGA_INIT)
451 struct rom_header *rom = (void*)BUILD_ROM_START;
452 if (! is_valid_rom(rom))