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 GPLv3 license.
8 #include "bregs.h" // struct bregs
9 #include "biosvar.h" // struct ipl_entry_s
10 #include "util.h" // dprintf
11 #include "pci.h" // pci_find_class
12 #include "pci_regs.h" // PCI_ROM_ADDRESS
13 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
16 /****************************************************************
18 ****************************************************************/
20 // $PnP string with special alignment in romlayout.S
21 extern char pnp_string[];
68 #define OPTION_ROM_START 0xc0000
69 #define OPTION_ROM_SIGNATURE 0xaa55
70 #define OPTION_ROM_ALIGN 2048
71 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
72 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
73 #define PCIROM_CODETYPE_X86 0
75 // Next available position for an option rom.
79 /****************************************************************
81 ****************************************************************/
83 // Execute a given option rom.
85 callrom(struct rom_header *rom, u16 offset, u16 bdf)
87 u16 seg = FARPTR_TO_SEG(rom);
88 dprintf(1, "Running option rom at %x:%x\n", seg, offset);
91 memset(&br, 0, sizeof(br));
96 br.di = (u32)pnp_string - BUILD_BIOS_ADDR;
101 debug_serial_setup();
103 if (GET_BDA(ebda_seg) != SEG_EBDA)
104 BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
105 , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
108 // Verify that an option rom looks valid
110 is_valid_rom(struct rom_header *rom)
112 if (rom->signature != OPTION_ROM_SIGNATURE)
114 u32 len = rom->size * 512;
115 u8 sum = checksum((void*)rom, len);
117 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
124 // Check if a valid option rom has a pnp struct; return it if so.
125 static struct pnp_data *
126 get_pnp_rom(struct rom_header *rom)
128 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
129 if (pnp->signature != *(u32*)pnp_string)
134 // Check if a valid option rom has a pci struct; return it if so.
135 static struct pci_data *
136 get_pci_rom(struct rom_header *rom)
138 struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
139 if (pci->signature != PCI_ROM_SIGNATURE)
144 // Add a BEV vector for a given pnp compatible option rom.
146 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
148 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
150 // Found a device that thinks it can boot the system. Record
151 // its BEV and product name string.
156 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
159 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
160 ip->type = IPL_TYPE_BEV;
161 ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
163 u16 desc = pnp->productname;
165 ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
170 // Copy a rom to its permanent location below 1MiB
171 static struct rom_header *
172 copy_rom(struct rom_header *rom)
174 u32 romsize = rom->size * 512;
175 if (next_rom + romsize > BUILD_BIOS_ADDR) {
176 // Option rom doesn't fit.
177 dprintf(1, "Option rom %p doesn't fit.\n", rom);
180 dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
181 memcpy((void*)next_rom, rom, romsize);
182 return (struct rom_header *)next_rom;
185 // Check if an option rom is at a hardcoded location for a device.
186 static struct rom_header *
187 lookup_hardcode(u16 bdf)
189 if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
190 return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
191 else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
192 return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
193 // XXX - check LAR when in coreboot?
197 // Map the option rom of a given PCI device.
198 static struct rom_header *
199 map_optionrom(u16 bdf)
201 dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
203 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
204 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
205 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
209 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
210 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
211 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
213 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
214 orig &= ~PCI_ROM_ADDRESS_ENABLE;
215 if (!sz || sz == 0xffffffff)
218 if (orig < 16*1024*1024) {
219 dprintf(6, "Preset rom address doesn't look valid\n");
223 // Looks like a rom - enable it.
224 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
226 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
227 struct rom_header *rom = (struct rom_header *)orig;
229 dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
231 if (rom->signature != OPTION_ROM_SIGNATURE) {
232 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
235 struct pci_data *pci = get_pci_rom(rom);
237 dprintf(6, "No valid pci signature found\n");
241 u32 vd = (pci->device << 16) | pci->vendor;
242 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
245 dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
247 if (pci->indicator & 0x80) {
248 dprintf(6, "No more images left\n");
251 rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
255 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
258 // Not valid - restore original and exit.
259 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
263 // Attempt to map and initialize the option rom on a given PCI device.
264 static struct rom_header *
265 init_optionrom(u16 bdf)
267 dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
268 struct rom_header *rom = lookup_hardcode(bdf);
270 rom = map_optionrom(bdf);
275 if (! is_valid_rom(rom))
278 if (get_pnp_rom(rom))
280 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
282 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
288 /****************************************************************
289 * Non-VGA option rom init
290 ****************************************************************/
295 if (! CONFIG_OPTIONROMS)
298 dprintf(1, "Scan for option roms\n");
300 u32 post_vga = next_rom;
302 if (CONFIG_OPTIONROMS_DEPLOYED) {
303 // Option roms are already deployed on the system.
305 while (pos < BUILD_BIOS_ADDR) {
306 struct rom_header *rom = (struct rom_header *)pos;
307 if (! is_valid_rom(rom)) {
308 pos += OPTION_ROM_ALIGN;
311 if (get_pnp_rom(rom))
312 callrom(rom, OPTION_ROM_INITVECTOR, 0);
313 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
317 // Find and deploy PCI roms.
319 foreachpci(bdf, max) {
320 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
321 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
322 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
328 // All option roms found and deployed - now build BEV/BCV vectors.
331 while (pos < next_rom) {
332 struct rom_header *rom = (struct rom_header *)pos;
333 if (! is_valid_rom(rom)) {
334 pos += OPTION_ROM_ALIGN;
337 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
338 struct pnp_data *pnp = get_pnp_rom(rom);
340 // Legacy rom - run init vector now.
341 callrom(rom, OPTION_ROM_INITVECTOR, 0);
346 // Can boot system - add to IPL list.
349 // Has BCV - run it now.
350 callrom(rom, pnp->bcv, 0);
355 /****************************************************************
357 ****************************************************************/
359 // Call into vga code to turn on console.
363 if (! CONFIG_OPTIONROMS)
366 dprintf(1, "Scan for VGA option rom\n");
367 next_rom = OPTION_ROM_START;
369 if (CONFIG_OPTIONROMS_DEPLOYED) {
370 // Option roms are already deployed on the system.
371 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
372 if (! is_valid_rom(rom))
374 callrom(rom, OPTION_ROM_INITVECTOR, 0);
375 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
377 // Find and deploy PCI VGA rom.
378 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
383 struct rom_header *rom = init_optionrom(bdf);
384 if (rom && !get_pnp_rom(rom))
385 // Call rom even if it isn't a pnp rom.
386 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
389 dprintf(1, "Turning on vga console\n");
391 memset(&br, 0, sizeof(br));
393 call16_int(0x10, &br);
396 printf("Starting SeaBIOS\n\n");