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 OPTIONROM_BDF_1 0x0000
69 #define OPTIONROM_MEM_1 0x00000000
70 #define OPTIONROM_BDF_2 0x0000
71 #define OPTIONROM_MEM_2 0x00000000
73 #define OPTION_ROM_START 0xc0000
74 #define OPTION_ROM_SIGNATURE 0xaa55
75 #define OPTION_ROM_ALIGN 2048
76 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
77 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
79 // Next available position for an option rom.
83 /****************************************************************
85 ****************************************************************/
87 // Execute a given option rom.
89 callrom(struct rom_header *rom, u16 offset, u16 bdf)
91 u16 seg = FARPTR_TO_SEG(rom);
92 dprintf(1, "Running option rom at %x:%x\n", seg, offset);
95 memset(&br, 0, sizeof(br));
100 br.di = (u32)pnp_string - BUILD_BIOS_ADDR;
103 // XXX - should call option rom in "big real mode".
106 debug_serial_setup();
108 if (GET_BDA(ebda_seg) != SEG_EBDA)
109 BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
110 , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
113 // Verify that an option rom looks valid
115 is_valid_rom(struct rom_header *rom)
117 if (rom->signature != OPTION_ROM_SIGNATURE)
119 u32 len = rom->size * 512;
120 u8 sum = checksum((void*)rom, len);
122 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
129 // Check if a valid option rom has a pnp struct; return it if so.
130 static struct pnp_data *
131 get_pnp_rom(struct rom_header *rom)
133 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
134 if (pnp->signature != *(u32*)pnp_string)
139 // Add a BEV vector for a given pnp compatible option rom.
141 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
143 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
145 // Found a device that thinks it can boot the system. Record
146 // its BEV and product name string.
151 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
154 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
155 ip->type = IPL_TYPE_BEV;
156 ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
158 u16 desc = pnp->productname;
160 ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
165 // Copy a rom to its permanent location below 1MiB
166 static struct rom_header *
167 copy_rom(struct rom_header *rom)
169 u32 romsize = rom->size * 512;
170 if (next_rom + romsize > BUILD_BIOS_ADDR) {
171 // Option rom doesn't fit.
172 dprintf(1, "Option rom %p doesn't fit.\n", rom);
175 dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
176 memcpy((void*)next_rom, rom, romsize);
177 return (struct rom_header *)next_rom;
180 // Check if an option rom is at a hardcoded location for a device.
181 static struct rom_header *
182 lookup_hardcode(u16 bdf)
184 if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
185 return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
186 else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
187 return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
188 // XXX - check LAR when in coreboot?
192 // Map the option rom of a given PCI device.
193 static struct rom_header *
194 map_optionrom(u16 bdf)
196 dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
198 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
199 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
200 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
202 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
203 orig &= ~PCI_ROM_ADDRESS_ENABLE;
204 if (!sz || sz == 0xffffffff)
207 // Looks like a rom - enable it.
208 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
210 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
211 struct rom_header *rom = (struct rom_header *)orig;
213 dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
215 if (rom->signature != OPTION_ROM_SIGNATURE) {
216 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
219 if (!rom->pcioffset) {
220 dprintf(6, "No PCI offset\n");
223 struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
224 if (pci->signature != PCI_ROM_SIGNATURE) {
225 dprintf(6, "Invalid pci signature (got %x)\n", pci->signature);
228 u32 vd = (pci->device << 16) | pci->vendor;
229 if (vd == vendev && pci->type == 0)
232 dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
234 if (pci->indicator & 0x80) {
235 dprintf(6, "No more images left\n");
238 rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
242 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
245 // Not valid - restore original and exit.
246 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
250 // Attempt to map and initialize the option rom on a given PCI device.
251 static struct rom_header *
252 init_optionrom(u16 bdf)
254 dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
255 struct rom_header *rom = lookup_hardcode(bdf);
257 rom = map_optionrom(bdf);
262 if (! is_valid_rom(rom))
265 if (get_pnp_rom(rom))
267 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
269 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
275 /****************************************************************
276 * Non-VGA option rom init
277 ****************************************************************/
282 if (! CONFIG_OPTIONROMS)
285 dprintf(1, "Scan for option roms\n");
287 u32 post_vga = next_rom;
289 if (CONFIG_OPTIONROMS_DEPLOYED) {
290 // Option roms are already deployed on the system.
292 while (pos < BUILD_BIOS_ADDR) {
293 struct rom_header *rom = (struct rom_header *)pos;
294 if (! is_valid_rom(rom)) {
295 pos += OPTION_ROM_ALIGN;
298 if (get_pnp_rom(rom))
299 callrom(rom, OPTION_ROM_INITVECTOR, 0);
300 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
304 // Find and deploy PCI roms.
306 foreachpci(bdf, max) {
307 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
308 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA)
314 // All option roms found and deployed - now build BEV/BCV vectors.
317 while (pos < next_rom) {
318 struct rom_header *rom = (struct rom_header *)pos;
319 if (! is_valid_rom(rom)) {
320 pos += OPTION_ROM_ALIGN;
323 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
324 struct pnp_data *pnp = get_pnp_rom(rom);
326 // Legacy rom - run init vector now.
327 callrom(rom, OPTION_ROM_INITVECTOR, 0);
332 // Can boot system - add to IPL list.
335 // Has BCV - run it now.
336 callrom(rom, pnp->bcv, 0);
341 /****************************************************************
343 ****************************************************************/
345 // Call into vga code to turn on console.
349 if (! CONFIG_OPTIONROMS)
352 dprintf(1, "Scan for VGA option rom\n");
353 next_rom = OPTION_ROM_START;
355 if (CONFIG_OPTIONROMS_DEPLOYED) {
356 // Option roms are already deployed on the system.
357 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
358 if (! is_valid_rom(rom))
360 callrom(rom, OPTION_ROM_INITVECTOR, 0);
361 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
363 // Find and deploy PCI VGA rom.
364 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
369 struct rom_header *rom = init_optionrom(bdf);
370 if (rom && !get_pnp_rom(rom))
371 // Call rom even if it isn't a pnp rom.
372 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
375 dprintf(1, "Turning on vga console\n");
377 memset(&br, 0, sizeof(br));
379 call16_int(0x10, &br);
382 printf("Starting SeaBIOS\n\n");