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 %x:%x\n", seg, offset);
90 memset(&br, 0, sizeof(br));
95 br.di = get_pnp_offset();
100 debug_serial_setup();
103 // Execute a BCV option rom registered via add_bcv().
105 call_bcv(u16 seg, u16 ip)
107 callrom(MAKE_FLATPTR(seg, 0), ip, 0);
110 // Verify that an option rom looks valid
112 is_valid_rom(struct rom_header *rom)
114 if (rom->signature != OPTION_ROM_SIGNATURE)
116 u32 len = rom->size * 512;
117 u8 sum = checksum((void*)rom, len);
119 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
126 // Check if a valid option rom has a pnp struct; return it if so.
127 static struct pnp_data *
128 get_pnp_rom(struct rom_header *rom)
130 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
131 if (pnp->signature != PNP_SIGNATURE)
136 // Check for multiple pnp option rom headers.
137 static struct pnp_data *
138 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
140 if (! pnp->nextoffset)
142 pnp = (struct pnp_data *)((u8*)rom + pnp->nextoffset);
143 if (pnp->signature != PNP_SIGNATURE)
148 // Check if a valid option rom has a pci struct; return it if so.
149 static struct pci_data *
150 get_pci_rom(struct rom_header *rom)
152 struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
153 if (pci->signature != PCI_ROM_SIGNATURE)
158 // Copy a rom to its permanent location below 1MiB
159 static struct rom_header *
160 copy_rom(struct rom_header *rom)
162 u32 romsize = rom->size * 512;
163 if (next_rom + romsize > BUILD_BIOS_ADDR) {
164 // Option rom doesn't fit.
165 dprintf(1, "Option rom %p doesn't fit.\n", rom);
168 dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
169 memcpy((void*)next_rom, rom, romsize);
170 return (struct rom_header *)next_rom;
173 // Check if an option rom is at a hardcoded location for a device.
174 static struct rom_header *
175 lookup_hardcode(u16 bdf)
177 if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
178 return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
179 else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
180 return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
181 // XXX - check LAR when in coreboot?
185 // Map the option rom of a given PCI device.
186 static struct rom_header *
187 map_optionrom(u16 bdf)
189 dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
191 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
192 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
193 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
197 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
198 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
199 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
201 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
202 orig &= ~PCI_ROM_ADDRESS_ENABLE;
203 if (!sz || sz == 0xffffffff)
206 if (orig < 16*1024*1024) {
207 dprintf(6, "Preset rom address doesn't look valid\n");
211 // Looks like a rom - enable it.
212 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
214 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
215 struct rom_header *rom = (struct rom_header *)orig;
217 dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
219 if (rom->signature != OPTION_ROM_SIGNATURE) {
220 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
223 struct pci_data *pci = get_pci_rom(rom);
225 dprintf(6, "No valid pci signature found\n");
229 u32 vd = (pci->device << 16) | pci->vendor;
230 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
233 dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
235 if (pci->indicator & 0x80) {
236 dprintf(6, "No more images left\n");
239 rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
243 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
246 // Not valid - restore original and exit.
247 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
251 // Attempt to map and initialize the option rom on a given PCI device.
252 static struct rom_header *
253 init_optionrom(u16 bdf)
255 dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
256 struct rom_header *rom = lookup_hardcode(bdf);
258 rom = map_optionrom(bdf);
263 if (! is_valid_rom(rom))
266 if (get_pnp_rom(rom))
268 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
270 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
276 /****************************************************************
277 * Non-VGA option rom init
278 ****************************************************************/
283 if (! CONFIG_OPTIONROMS)
286 dprintf(1, "Scan for option roms\n");
288 u32 post_vga = next_rom;
290 if (CONFIG_OPTIONROMS_DEPLOYED) {
291 // Option roms are already deployed on the system.
293 while (pos < BUILD_BIOS_ADDR) {
294 struct rom_header *rom = (struct rom_header *)pos;
295 if (! is_valid_rom(rom)) {
296 pos += OPTION_ROM_ALIGN;
299 if (get_pnp_rom(rom))
300 callrom(rom, OPTION_ROM_INITVECTOR, 0);
301 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
305 // Find and deploy PCI roms.
307 foreachpci(bdf, max) {
308 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
309 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
310 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
316 // All option roms found and deployed - now build BEV/BCV vectors.
319 while (pos < next_rom) {
320 struct rom_header *rom = (struct rom_header *)pos;
321 if (! is_valid_rom(rom)) {
322 pos += OPTION_ROM_ALIGN;
325 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
326 struct pnp_data *pnp = get_pnp_rom(rom);
329 add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
334 // Can boot system - add to IPL list.
335 add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
337 // Check for BCV (there may be multiple).
338 while (pnp && pnp->bcv) {
339 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
340 pnp = get_pnp_next(rom, pnp);
346 /****************************************************************
348 ****************************************************************/
350 // Call into vga code to turn on console.
354 if (! CONFIG_OPTIONROMS)
357 dprintf(1, "Scan for VGA option rom\n");
358 next_rom = OPTION_ROM_START;
360 if (CONFIG_OPTIONROMS_DEPLOYED) {
361 // Option roms are already deployed on the system.
362 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
363 if (! is_valid_rom(rom))
365 callrom(rom, OPTION_ROM_INITVECTOR, 0);
366 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
368 // Find and deploy PCI VGA rom.
369 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
374 struct rom_header *rom = init_optionrom(bdf);
375 if (rom && !get_pnp_rom(rom))
376 // Call rom even if it isn't a pnp rom.
377 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
380 dprintf(1, "Turning on vga console\n");
382 memset(&br, 0, sizeof(br));
384 call16_int(0x10, &br);
387 printf("Starting SeaBIOS\n\n");