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 ****************************************************************/
65 #define OPTION_ROM_START 0xc0000
66 #define OPTION_ROM_SIGNATURE 0xaa55
67 #define OPTION_ROM_ALIGN 2048
68 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
69 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
70 #define PCIROM_CODETYPE_X86 0
72 // Next available position for an option rom.
76 /****************************************************************
78 ****************************************************************/
80 // Execute a given option rom.
82 callrom(struct rom_header *rom, u16 offset, u16 bdf)
84 u16 seg = FARPTR_TO_SEG(rom);
85 dprintf(1, "Running option rom at %x:%x\n", seg, offset);
88 memset(&br, 0, sizeof(br));
93 br.di = get_pnp_offset();
100 if (GET_BDA(ebda_seg) != SEG_EBDA)
101 BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
102 , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
105 // Verify that an option rom looks valid
107 is_valid_rom(struct rom_header *rom)
109 if (rom->signature != OPTION_ROM_SIGNATURE)
111 u32 len = rom->size * 512;
112 u8 sum = checksum((void*)rom, len);
114 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
121 // Check if a valid option rom has a pnp struct; return it if so.
122 static struct pnp_data *
123 get_pnp_rom(struct rom_header *rom)
125 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
126 if (pnp->signature != PNP_SIGNATURE)
131 // Check for multiple pnp option rom headers.
132 static struct pnp_data *
133 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
135 if (! pnp->nextoffset)
137 pnp = (struct pnp_data *)((u8*)rom + pnp->nextoffset);
138 if (pnp->signature != PNP_SIGNATURE)
143 // Check if a valid option rom has a pci struct; return it if so.
144 static struct pci_data *
145 get_pci_rom(struct rom_header *rom)
147 struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
148 if (pci->signature != PCI_ROM_SIGNATURE)
153 // Add a BEV vector for a given pnp compatible option rom.
155 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
157 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
159 // Found a device that thinks it can boot the system. Record
160 // its BEV and product name string.
165 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
168 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
169 ip->type = IPL_TYPE_BEV;
170 ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
172 u16 desc = pnp->productname;
174 ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
179 // Copy a rom to its permanent location below 1MiB
180 static struct rom_header *
181 copy_rom(struct rom_header *rom)
183 u32 romsize = rom->size * 512;
184 if (next_rom + romsize > BUILD_BIOS_ADDR) {
185 // Option rom doesn't fit.
186 dprintf(1, "Option rom %p doesn't fit.\n", rom);
189 dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
190 memcpy((void*)next_rom, rom, romsize);
191 return (struct rom_header *)next_rom;
194 // Check if an option rom is at a hardcoded location for a device.
195 static struct rom_header *
196 lookup_hardcode(u16 bdf)
198 if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
199 return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
200 else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
201 return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
202 // XXX - check LAR when in coreboot?
206 // Map the option rom of a given PCI device.
207 static struct rom_header *
208 map_optionrom(u16 bdf)
210 dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
212 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
213 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
214 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
218 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
219 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
220 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
222 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
223 orig &= ~PCI_ROM_ADDRESS_ENABLE;
224 if (!sz || sz == 0xffffffff)
227 if (orig < 16*1024*1024) {
228 dprintf(6, "Preset rom address doesn't look valid\n");
232 // Looks like a rom - enable it.
233 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
235 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
236 struct rom_header *rom = (struct rom_header *)orig;
238 dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
240 if (rom->signature != OPTION_ROM_SIGNATURE) {
241 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
244 struct pci_data *pci = get_pci_rom(rom);
246 dprintf(6, "No valid pci signature found\n");
250 u32 vd = (pci->device << 16) | pci->vendor;
251 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
254 dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
256 if (pci->indicator & 0x80) {
257 dprintf(6, "No more images left\n");
260 rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
264 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
267 // Not valid - restore original and exit.
268 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
272 // Attempt to map and initialize the option rom on a given PCI device.
273 static struct rom_header *
274 init_optionrom(u16 bdf)
276 dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
277 struct rom_header *rom = lookup_hardcode(bdf);
279 rom = map_optionrom(bdf);
284 if (! is_valid_rom(rom))
287 if (get_pnp_rom(rom))
289 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
291 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
297 /****************************************************************
298 * Non-VGA option rom init
299 ****************************************************************/
304 if (! CONFIG_OPTIONROMS)
307 dprintf(1, "Scan for option roms\n");
309 u32 post_vga = next_rom;
311 if (CONFIG_OPTIONROMS_DEPLOYED) {
312 // Option roms are already deployed on the system.
314 while (pos < BUILD_BIOS_ADDR) {
315 struct rom_header *rom = (struct rom_header *)pos;
316 if (! is_valid_rom(rom)) {
317 pos += OPTION_ROM_ALIGN;
320 if (get_pnp_rom(rom))
321 callrom(rom, OPTION_ROM_INITVECTOR, 0);
322 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
326 // Find and deploy PCI roms.
328 foreachpci(bdf, max) {
329 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
330 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
331 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
337 // All option roms found and deployed - now build BEV/BCV vectors.
340 while (pos < next_rom) {
341 struct rom_header *rom = (struct rom_header *)pos;
342 if (! is_valid_rom(rom)) {
343 pos += OPTION_ROM_ALIGN;
346 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
347 struct pnp_data *pnp = get_pnp_rom(rom);
349 // Legacy rom - run init vector now.
350 callrom(rom, OPTION_ROM_INITVECTOR, 0);
355 // Can boot system - add to IPL list.
358 // Check for BCV (there may be multiple).
359 while (pnp && pnp->bcv) {
360 // Has BCV - run it now.
361 callrom(rom, pnp->bcv, 0);
362 pnp = get_pnp_next(rom, pnp);
368 /****************************************************************
370 ****************************************************************/
372 // Call into vga code to turn on console.
376 if (! CONFIG_OPTIONROMS)
379 dprintf(1, "Scan for VGA option rom\n");
380 next_rom = OPTION_ROM_START;
382 if (CONFIG_OPTIONROMS_DEPLOYED) {
383 // Option roms are already deployed on the system.
384 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
385 if (! is_valid_rom(rom))
387 callrom(rom, OPTION_ROM_INITVECTOR, 0);
388 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
390 // Find and deploy PCI VGA rom.
391 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
396 struct rom_header *rom = init_optionrom(bdf);
397 if (rom && !get_pnp_rom(rom))
398 // Call rom even if it isn't a pnp rom.
399 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
402 dprintf(1, "Turning on vga console\n");
404 memset(&br, 0, sizeof(br));
406 call16_int(0x10, &br);
409 printf("Starting SeaBIOS\n\n");