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])
78 // Next available position for an option rom.
82 /****************************************************************
84 ****************************************************************/
86 // Execute a given option rom.
88 callrom(struct rom_header *rom, u16 offset, u16 bdf)
90 u16 seg = FARPTR_TO_SEG(rom);
91 dprintf(1, "Running option rom at %x:%x\n", seg, offset);
94 memset(&br, 0, sizeof(br));
99 br.di = (u32)pnp_string - BUILD_BIOS_ADDR;
104 debug_serial_setup();
106 if (GET_BDA(ebda_seg) != SEG_EBDA)
107 BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
108 , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
111 // Verify that an option rom looks valid
113 is_valid_rom(struct rom_header *rom)
115 if (rom->signature != OPTION_ROM_SIGNATURE)
117 u32 len = rom->size * 512;
118 u8 sum = checksum((void*)rom, len);
120 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
127 // Check if a valid option rom has a pnp struct; return it if so.
128 static struct pnp_data *
129 get_pnp_rom(struct rom_header *rom)
131 struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
132 if (pnp->signature != *(u32*)pnp_string)
137 // Add a BEV vector for a given pnp compatible option rom.
139 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
141 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
143 // Found a device that thinks it can boot the system. Record
144 // its BEV and product name string.
149 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
152 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
153 ip->type = IPL_TYPE_BEV;
154 ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
156 u16 desc = pnp->productname;
158 ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
163 // Check if an option rom is at a hardcoded location for a device.
164 static struct rom_header *
165 lookup_hardcode(u16 bdf)
167 if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
168 return (struct rom_header *)OPTIONROM_MEM_1;
169 else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
170 return (struct rom_header *)OPTIONROM_MEM_2;
171 // XXX - check LAR when in coreboot?
175 // Map the option rom of a given PCI device.
176 static struct rom_header *
177 map_optionrom(u16 bdf)
179 dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
181 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
182 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
183 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
185 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
186 if (!sz || sz == 0xffffffff)
189 // Looks like a rom - map it to just above end of memory.
190 u32 mappos = ALIGN(GET_EBDA(ram_size), OPTION_ROM_ALIGN);
191 pci_config_writel(bdf, PCI_ROM_ADDRESS, mappos | PCI_ROM_ADDRESS_ENABLE);
193 struct rom_header *rom = (struct rom_header *)mappos;
194 if (rom->signature != OPTION_ROM_SIGNATURE) {
195 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
199 dprintf(6, "Card %x option rom mapped at %p\n", bdf, rom);
202 // Not valid - restore original and exit.
203 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
207 // Attempt to map and initialize the option rom on a given PCI device.
208 static struct rom_header *
209 init_optionrom(u16 bdf)
211 dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
212 struct rom_header *rom = lookup_hardcode(bdf);
214 rom = map_optionrom(bdf);
219 u32 romsize = rom->size * 512;
220 if (next_rom + romsize > BUILD_BIOS_ADDR) {
221 // Option rom doesn't fit.
222 dprintf(1, "Option rom %x doesn't fit.\n", bdf);
223 pci_config_writel(bdf, PCI_ROM_ADDRESS, next_rom);
226 dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
227 memcpy((void*)next_rom, rom, romsize);
228 pci_config_writel(bdf, PCI_ROM_ADDRESS, next_rom);
229 rom = (struct rom_header *)next_rom;
231 if (! is_valid_rom(rom))
234 if (get_pnp_rom(rom))
236 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
238 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
244 /****************************************************************
245 * Non-VGA option rom init
246 ****************************************************************/
251 if (! CONFIG_OPTIONROMS)
254 dprintf(1, "Scan for option roms\n");
256 u32 post_vga = next_rom;
258 if (CONFIG_OPTIONROMS_DEPLOYED) {
259 // Option roms are already deployed on the system.
261 while (pos < BUILD_BIOS_ADDR) {
262 struct rom_header *rom = (struct rom_header *)pos;
263 if (! is_valid_rom(rom)) {
264 pos += OPTION_ROM_ALIGN;
267 if (get_pnp_rom(rom))
268 callrom(rom, OPTION_ROM_INITVECTOR, 0);
269 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
273 // Find and deploy PCI roms.
274 int max = GET_VAR(CS, MaxBDF);
276 for (bdf=0; bdf < max; bdf++) {
277 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
278 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA)
284 // All option roms found and deployed - now build BEV/BCV vectors.
287 while (pos < next_rom) {
288 struct rom_header *rom = (struct rom_header *)pos;
289 if (! is_valid_rom(rom)) {
290 pos += OPTION_ROM_ALIGN;
293 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
294 struct pnp_data *pnp = get_pnp_rom(rom);
296 // Legacy rom - run init vector now.
297 callrom(rom, OPTION_ROM_INITVECTOR, 0);
302 // Can boot system - add to IPL list.
305 // Has BCV - run it now.
306 callrom(rom, pnp->bcv, 0);
311 /****************************************************************
313 ****************************************************************/
315 // Call into vga code to turn on console.
319 if (! CONFIG_OPTIONROMS)
322 dprintf(1, "Scan for VGA option rom\n");
323 next_rom = OPTION_ROM_START;
325 if (CONFIG_OPTIONROMS_DEPLOYED) {
326 // Option roms are already deployed on the system.
327 struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
328 if (! is_valid_rom(rom))
330 callrom(rom, OPTION_ROM_INITVECTOR, 0);
331 next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
333 // Find and deploy PCI VGA rom.
334 int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA, 0);
339 struct rom_header *rom = init_optionrom(bdf);
340 if (rom && !get_pnp_rom(rom))
341 // Call rom even if it isn't a pnp rom.
342 callrom(rom, OPTION_ROM_INITVECTOR, bdf);
345 dprintf(1, "Turning on vga console\n");
347 memset(&br, 0, sizeof(br));
349 call16_int(0x10, &br);
352 printf("Starting SeaBIOS\n\n");