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" // FARPTR_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 = FARPTR_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 // Verify that an option rom looks valid
105 is_valid_rom(struct rom_header *rom)
107 if (rom->signature != OPTION_ROM_SIGNATURE)
109 u32 len = rom->size * 512;
110 u8 sum = checksum((void*)rom, len);
112 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
119 // Check if a valid option rom has a pnp struct; return it if so.
120 static struct pnp_data *
121 get_pnp_rom(struct rom_header *rom)
123 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
124 if (pnp->signature != PNP_SIGNATURE)
129 // Check for multiple pnp option rom headers.
130 static struct pnp_data *
131 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
133 if (! pnp->nextoffset)
135 pnp = (struct pnp_data *)((u8*)rom + pnp->nextoffset);
136 if (pnp->signature != PNP_SIGNATURE)
141 // Check if a valid option rom has a pci struct; return it if so.
142 static struct pci_data *
143 get_pci_rom(struct rom_header *rom)
145 struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
146 if (pci->signature != PCI_ROM_SIGNATURE)
151 // Add a BEV vector for a given pnp compatible option rom.
153 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
155 // Found a device that thinks it can boot the system. Record
156 // its BEV and product name string.
161 if (IPL.count >= ARRAY_SIZE(IPL.table))
164 struct ipl_entry_s *ip = &IPL.table[IPL.count];
165 ip->type = IPL_TYPE_BEV;
166 ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
168 u16 desc = pnp->productname;
170 ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
175 // Copy a rom to its permanent location below 1MiB
176 static struct rom_header *
177 copy_rom(struct rom_header *rom)
179 u32 romsize = rom->size * 512;
180 if (next_rom + romsize > BUILD_BIOS_ADDR) {
181 // Option rom doesn't fit.
182 dprintf(1, "Option rom %p doesn't fit.\n", rom);
185 dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
186 memcpy((void*)next_rom, rom, romsize);
187 return (struct rom_header *)next_rom;
190 // Check if an option rom is at a hardcoded location for a device.
191 static struct rom_header *
192 lookup_hardcode(u16 bdf)
194 if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
195 return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
196 else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
197 return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
198 // XXX - check LAR when in coreboot?
202 // Map the option rom of a given PCI device.
203 static struct rom_header *
204 map_optionrom(u16 bdf)
206 dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
208 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
209 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
210 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
214 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
215 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
216 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
218 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
219 orig &= ~PCI_ROM_ADDRESS_ENABLE;
220 if (!sz || sz == 0xffffffff)
223 if (orig < 16*1024*1024) {
224 dprintf(6, "Preset rom address doesn't look valid\n");
228 // Looks like a rom - enable it.
229 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
231 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
232 struct rom_header *rom = (struct rom_header *)orig;
234 dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
236 if (rom->signature != OPTION_ROM_SIGNATURE) {
237 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
240 struct pci_data *pci = get_pci_rom(rom);
242 dprintf(6, "No valid pci signature found\n");
246 u32 vd = (pci->device << 16) | pci->vendor;
247 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
250 dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
252 if (pci->indicator & 0x80) {
253 dprintf(6, "No more images left\n");
256 rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
260 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
263 // Not valid - restore original and exit.
264 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
268 // Attempt to map and initialize the option rom on a given PCI device.
269 static struct rom_header *
270 init_optionrom(u16 bdf)
272 dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
273 struct rom_header *rom = lookup_hardcode(bdf);
275 rom = map_optionrom(bdf);
280 if (! is_valid_rom(rom))
283 if (get_pnp_rom(rom))
285 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
287 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
293 /****************************************************************
294 * Non-VGA option rom init
295 ****************************************************************/
300 if (! CONFIG_OPTIONROMS)
303 dprintf(1, "Scan for option roms\n");
305 u32 post_vga = next_rom;
307 if (CONFIG_OPTIONROMS_DEPLOYED) {
308 // Option roms are already deployed on the system.
310 while (pos < BUILD_BIOS_ADDR) {
311 struct rom_header *rom = (struct rom_header *)pos;
312 if (! is_valid_rom(rom)) {
313 pos += OPTION_ROM_ALIGN;
316 if (get_pnp_rom(rom))
317 callrom(rom, OPTION_ROM_INITVECTOR, 0);
318 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
322 // Find and deploy PCI roms.
324 foreachpci(bdf, max) {
325 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
326 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
327 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
333 // All option roms found and deployed - now build BEV/BCV vectors.
336 while (pos < next_rom) {
337 struct rom_header *rom = (struct rom_header *)pos;
338 if (! is_valid_rom(rom)) {
339 pos += OPTION_ROM_ALIGN;
342 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
343 struct pnp_data *pnp = get_pnp_rom(rom);
345 // Legacy rom - run init vector now.
346 callrom(rom, OPTION_ROM_INITVECTOR, 0);
351 // Can boot system - add to IPL list.
354 // Check for BCV (there may be multiple).
355 while (pnp && pnp->bcv) {
356 // Has BCV - run it now.
357 callrom(rom, pnp->bcv, 0);
358 pnp = get_pnp_next(rom, pnp);
364 /****************************************************************
366 ****************************************************************/
368 // Call into vga code to turn on console.
372 if (! CONFIG_OPTIONROMS)
375 dprintf(1, "Scan for VGA option rom\n");
376 next_rom = OPTION_ROM_START;
378 if (CONFIG_OPTIONROMS_DEPLOYED) {
379 // Option roms are already deployed on the system.
380 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
381 if (! is_valid_rom(rom))
383 callrom(rom, OPTION_ROM_INITVECTOR, 0);
384 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
386 // Find and deploy PCI VGA rom.
387 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
392 struct rom_header *rom = init_optionrom(bdf);
393 if (rom && !get_pnp_rom(rom))
394 // Call rom even if it isn't a pnp rom.
395 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
398 dprintf(1, "Turning on vga console\n");
400 memset(&br, 0, sizeof(br));
402 call16_int(0x10, &br);
405 printf("Starting SeaBIOS\n\n");