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_START 0xc0000
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 // Next available position for an option rom.
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));
95 br.di = get_pnp_offset();
100 debug_serial_setup();
103 // Execute a given option rom at the standard entry vector.
105 callrom(struct rom_header *rom, u16 bdf)
107 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
110 // Execute a BCV option rom registered via add_bcv().
112 call_bcv(u16 seg, u16 ip)
114 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
117 // Verify that an option rom looks valid
119 is_valid_rom(struct rom_header *rom)
121 dprintf(6, "Checking rom %p (sig %x size %d)\n"
122 , rom, rom->signature, rom->size);
123 if (rom->signature != OPTION_ROM_SIGNATURE)
127 u32 len = rom->size * 512;
128 u8 sum = checksum(rom, len);
130 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
137 // Check if a valid option rom has a pnp struct; return it if so.
138 static struct pnp_data *
139 get_pnp_rom(struct rom_header *rom)
141 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
142 if (pnp->signature != PNP_SIGNATURE)
147 // Check for multiple pnp option rom headers.
148 static struct pnp_data *
149 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
151 if (! pnp->nextoffset)
153 pnp = (void*)((u8*)rom + pnp->nextoffset);
154 if (pnp->signature != PNP_SIGNATURE)
159 // Check if a valid option rom has a pci struct; return it if so.
160 static struct pci_data *
161 get_pci_rom(struct rom_header *rom)
163 struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
164 if (pci->signature != PCI_ROM_SIGNATURE)
169 // Copy a rom to its permanent location below 1MiB
170 static struct rom_header *
171 copy_rom(struct rom_header *rom)
173 u32 romsize = rom->size * 512;
174 if (next_rom + romsize > BUILD_BIOS_ADDR) {
175 // Option rom doesn't fit.
176 dprintf(1, "Option rom %p doesn't fit.\n", rom);
179 dprintf(4, "Copying option rom (size %d) from %p to %x\n"
180 , romsize, rom, next_rom);
181 memcpy((void*)next_rom, rom, romsize);
182 return (void*)next_rom;
185 // Run rom init code and note rom size.
187 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
189 if (! is_valid_rom(rom))
192 if (isvga || get_pnp_rom(rom))
193 // Only init vga and PnP roms here.
196 next_rom = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
202 /****************************************************************
204 ****************************************************************/
206 // Check if an option rom is at a hardcoded location or in CBFS.
207 static struct rom_header *
208 lookup_hardcode(u32 vendev)
210 if (OPTIONROM_VENDEV_1
211 && ((OPTIONROM_VENDEV_1 >> 16)
212 | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
213 return copy_rom((void*)OPTIONROM_MEM_1);
214 if (OPTIONROM_VENDEV_2
215 && ((OPTIONROM_VENDEV_2 >> 16)
216 | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
217 return copy_rom((void*)OPTIONROM_MEM_2);
218 int ret = cbfs_copy_optionrom((void*)next_rom, BUILD_BIOS_ADDR - next_rom
222 return (void*)next_rom;
225 // Run all roms in a given CBFS directory.
227 run_cbfs_roms(const char *prefix, int isvga)
229 struct cbfs_file *tmp = NULL;
231 tmp = cbfs_copyfile_prefix(
232 (void*)next_rom, BUILD_BIOS_ADDR - next_rom, prefix, tmp);
235 init_optionrom((void*)next_rom, 0, isvga);
240 /****************************************************************
242 ****************************************************************/
244 // Map the option rom of a given PCI device.
245 static struct rom_header *
246 map_pcirom(u16 bdf, u32 vendev)
248 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
249 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
251 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
252 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
253 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
257 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
258 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
259 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
261 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
262 orig &= ~PCI_ROM_ADDRESS_ENABLE;
263 if (!sz || sz == 0xffffffff)
266 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
267 // Don't try to map to a pci addresses at its max, in the last
268 // 4MiB of ram, or the first 16MiB of ram.
269 dprintf(6, "Preset rom address doesn't look valid\n");
273 // Looks like a rom - enable it.
274 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
276 struct rom_header *rom = (void*)orig;
278 dprintf(5, "Inspecting possible rom at %p (dv=%x bdf=%x)\n"
280 if (rom->signature != OPTION_ROM_SIGNATURE) {
281 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
284 struct pci_data *pci = get_pci_rom(rom);
286 dprintf(6, "No valid pci signature found\n");
290 u32 vd = (pci->device << 16) | pci->vendor;
291 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
294 dprintf(6, "Didn't match dev/ven (got %x) or type (got %d)\n"
296 if (pci->indicator & 0x80) {
297 dprintf(6, "No more images left\n");
300 rom = (void*)((u32)rom + pci->ilen * 512);
304 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
307 // Not valid - restore original and exit.
308 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
312 // Attempt to map and initialize the option rom on a given PCI device.
314 init_pcirom(u16 bdf, int isvga)
316 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
317 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %x)\n"
318 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
320 struct rom_header *rom = lookup_hardcode(vendev);
322 rom = map_pcirom(bdf, vendev);
326 return init_optionrom(rom, bdf, isvga);
330 /****************************************************************
331 * Non-VGA option rom init
332 ****************************************************************/
337 if (! CONFIG_OPTIONROMS)
340 dprintf(1, "Scan for option roms\n");
342 u32 post_vga = next_rom;
344 if (CONFIG_OPTIONROMS_DEPLOYED) {
345 // Option roms are already deployed on the system.
347 while (pos < BUILD_BIOS_ADDR) {
348 int ret = init_optionrom((void*)pos, 0, 0);
350 pos += OPTION_ROM_ALIGN;
355 // Find and deploy PCI roms.
357 foreachpci(bdf, max) {
358 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
359 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
360 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
365 // Find and deploy CBFS roms not associated with a device.
366 run_cbfs_roms("genroms/", 0);
369 // All option roms found and deployed - now build BEV/BCV vectors.
372 while (pos < next_rom) {
373 struct rom_header *rom = (void*)pos;
374 if (! is_valid_rom(rom)) {
375 pos += OPTION_ROM_ALIGN;
378 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
379 struct pnp_data *pnp = get_pnp_rom(rom);
382 add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
387 // Can boot system - add to IPL list.
388 add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
390 // Check for BCV (there may be multiple).
391 while (pnp && pnp->bcv) {
392 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
393 pnp = get_pnp_next(rom, pnp);
399 /****************************************************************
401 ****************************************************************/
403 // Call into vga code to turn on console.
407 if (! CONFIG_OPTIONROMS)
410 dprintf(1, "Scan for VGA option rom\n");
411 next_rom = OPTION_ROM_START;
413 if (CONFIG_OPTIONROMS_DEPLOYED) {
414 // Option roms are already deployed on the system.
415 init_optionrom((void*)OPTION_ROM_START, 0, 1);
417 // Find and deploy PCI VGA rom.
418 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
422 // Find and deploy CBFS vga-style roms not associated with a device.
423 run_cbfs_roms("vgaroms/", 1);
426 if (next_rom == OPTION_ROM_START) {
428 next_rom += OPTION_ROM_ALIGN;
432 dprintf(1, "Turning on vga console\n");
434 memset(&br, 0, sizeof(br));
436 call16_int(0x10, &br);
439 printf("Starting SeaBIOS\n\n");
445 if (!CONFIG_S3_RESUME_VGA_INIT)
447 struct rom_header *rom = (void*)OPTION_ROM_START;
448 if (! is_valid_rom(rom))