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;
99 // XXX - should call option rom in "big real mode".
102 debug_serial_setup();
104 if (GET_BDA(ebda_seg) != SEG_EBDA)
105 BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
106 , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
109 // Verify that an option rom looks valid
111 is_valid_rom(struct rom_header *rom)
113 if (rom->signature != OPTION_ROM_SIGNATURE)
115 u32 len = rom->size * 512;
116 u8 sum = checksum((void*)rom, len);
118 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
125 // Check if a valid option rom has a pnp struct; return it if so.
126 static struct pnp_data *
127 get_pnp_rom(struct rom_header *rom)
129 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
130 if (pnp->signature != *(u32*)pnp_string)
135 // Check if a valid option rom has a pci struct; return it if so.
136 static struct pci_data *
137 get_pci_rom(struct rom_header *rom)
139 struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
140 if (pci->signature != PCI_ROM_SIGNATURE)
145 // Add a BEV vector for a given pnp compatible option rom.
147 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
149 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
151 // Found a device that thinks it can boot the system. Record
152 // its BEV and product name string.
157 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
160 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
161 ip->type = IPL_TYPE_BEV;
162 ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
164 u16 desc = pnp->productname;
166 ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
171 // Copy a rom to its permanent location below 1MiB
172 static struct rom_header *
173 copy_rom(struct rom_header *rom)
175 u32 romsize = rom->size * 512;
176 if (next_rom + romsize > BUILD_BIOS_ADDR) {
177 // Option rom doesn't fit.
178 dprintf(1, "Option rom %p doesn't fit.\n", rom);
181 dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
182 memcpy((void*)next_rom, rom, romsize);
183 return (struct rom_header *)next_rom;
186 // Check if an option rom is at a hardcoded location for a device.
187 static struct rom_header *
188 lookup_hardcode(u16 bdf)
190 if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
191 return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
192 else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
193 return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
194 // XXX - check LAR when in coreboot?
198 // Map the option rom of a given PCI device.
199 static struct rom_header *
200 map_optionrom(u16 bdf)
202 dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
204 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
205 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
206 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
210 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
211 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
212 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
214 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
215 orig &= ~PCI_ROM_ADDRESS_ENABLE;
216 if (!sz || sz == 0xffffffff)
219 if (orig < 16*1024*1024) {
220 dprintf(6, "Preset rom address doesn't look valid\n");
224 // Looks like a rom - enable it.
225 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
227 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
228 struct rom_header *rom = (struct rom_header *)orig;
230 dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
232 if (rom->signature != OPTION_ROM_SIGNATURE) {
233 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
236 struct pci_data *pci = get_pci_rom(rom);
238 dprintf(6, "No valid pci signature found\n");
242 u32 vd = (pci->device << 16) | pci->vendor;
243 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
246 dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
248 if (pci->indicator & 0x80) {
249 dprintf(6, "No more images left\n");
252 rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
256 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
259 // Not valid - restore original and exit.
260 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
264 // Attempt to map and initialize the option rom on a given PCI device.
265 static struct rom_header *
266 init_optionrom(u16 bdf)
268 dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
269 struct rom_header *rom = lookup_hardcode(bdf);
271 rom = map_optionrom(bdf);
276 if (! is_valid_rom(rom))
279 if (get_pnp_rom(rom))
281 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
283 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
289 /****************************************************************
290 * Non-VGA option rom init
291 ****************************************************************/
296 if (! CONFIG_OPTIONROMS)
299 dprintf(1, "Scan for option roms\n");
301 u32 post_vga = next_rom;
303 if (CONFIG_OPTIONROMS_DEPLOYED) {
304 // Option roms are already deployed on the system.
306 while (pos < BUILD_BIOS_ADDR) {
307 struct rom_header *rom = (struct rom_header *)pos;
308 if (! is_valid_rom(rom)) {
309 pos += OPTION_ROM_ALIGN;
312 if (get_pnp_rom(rom))
313 callrom(rom, OPTION_ROM_INITVECTOR, 0);
314 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
318 // Find and deploy PCI roms.
320 foreachpci(bdf, max) {
321 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
322 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
323 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
329 // All option roms found and deployed - now build BEV/BCV vectors.
332 while (pos < next_rom) {
333 struct rom_header *rom = (struct rom_header *)pos;
334 if (! is_valid_rom(rom)) {
335 pos += OPTION_ROM_ALIGN;
338 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
339 struct pnp_data *pnp = get_pnp_rom(rom);
341 // Legacy rom - run init vector now.
342 callrom(rom, OPTION_ROM_INITVECTOR, 0);
347 // Can boot system - add to IPL list.
350 // Has BCV - run it now.
351 callrom(rom, pnp->bcv, 0);
356 /****************************************************************
358 ****************************************************************/
360 // Call into vga code to turn on console.
364 if (! CONFIG_OPTIONROMS)
367 dprintf(1, "Scan for VGA option rom\n");
368 next_rom = OPTION_ROM_START;
370 if (CONFIG_OPTIONROMS_DEPLOYED) {
371 // Option roms are already deployed on the system.
372 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
373 if (! is_valid_rom(rom))
375 callrom(rom, OPTION_ROM_INITVECTOR, 0);
376 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
378 // Find and deploy PCI VGA rom.
379 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
384 struct rom_header *rom = init_optionrom(bdf);
385 if (rom && !get_pnp_rom(rom))
386 // Call rom even if it isn't a pnp rom.
387 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
390 dprintf(1, "Turning on vga console\n");
392 memset(&br, 0, sizeof(br));
394 call16_int(0x10, &br);
397 printf("Starting SeaBIOS\n\n");