blob: 47f5808acd8b7b96b896cbb945141605f284975c [file] [log] [blame]
Kevin O'Connor714325c2008-11-01 20:32:27 -04001// Option rom scanning code.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor714325c2008-11-01 20:32:27 -04007
8#include "bregs.h" // struct bregs
Kevin O'Connor35ae7262009-01-19 15:44:44 -05009#include "farptr.h" // FLATPTR_TO_SEG
Kevin O'Connorc659fde2008-12-28 23:43:20 -050010#include "config.h" // CONFIG_*
Kevin O'Connor714325c2008-11-01 20:32:27 -040011#include "util.h" // dprintf
Kevin O'Connor51fd0a12009-09-12 13:20:14 -040012#include "pci.h" // foreachpci
Kevin O'Connorceea03c2008-11-08 21:36:35 -050013#include "pci_regs.h" // PCI_ROM_ADDRESS
14#include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
Kevin O'Connorc659fde2008-12-28 23:43:20 -050015#include "boot.h" // IPL
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +010016#include "paravirt.h" // qemu_cfg_*
Kevin O'Connorceea03c2008-11-08 21:36:35 -050017
18
19/****************************************************************
20 * Definitions
21 ****************************************************************/
Kevin O'Connor714325c2008-11-01 20:32:27 -040022
Kevin O'Connor714325c2008-11-01 20:32:27 -040023struct rom_header {
24 u16 signature;
25 u8 size;
26 u8 initVector[4];
27 u8 reserved[17];
28 u16 pcioffset;
29 u16 pnpoffset;
30} PACKED;
31
32struct pci_data {
33 u32 signature;
34 u16 vendor;
35 u16 device;
36 u16 vitaldata;
37 u16 dlen;
38 u8 drevision;
39 u8 class_lo;
40 u16 class_hi;
41 u16 ilen;
42 u16 irevision;
43 u8 type;
44 u8 indicator;
45 u16 reserved;
46} PACKED;
47
48struct pnp_data {
49 u32 signature;
50 u8 revision;
51 u8 len;
52 u16 nextoffset;
53 u8 reserved_08;
54 u8 checksum;
55 u32 devid;
56 u16 manufacturer;
57 u16 productname;
58 u8 type_lo;
59 u16 type_hi;
60 u8 dev_flags;
61 u16 bcv;
62 u16 dv;
63 u16 bev;
64 u16 reserved_1c;
65 u16 staticresource;
66} PACKED;
67
Kevin O'Connorceea03c2008-11-08 21:36:35 -050068#define OPTION_ROM_SIGNATURE 0xaa55
69#define OPTION_ROM_ALIGN 2048
70#define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
Kevin O'Connor5a1b8282008-11-29 20:39:06 -050071#define PCI_ROM_SIGNATURE 0x52494350 // PCIR
Kevin O'Connor62eea672008-12-06 11:57:45 -050072#define PCIROM_CODETYPE_X86 0
Kevin O'Connorceea03c2008-11-08 21:36:35 -050073
Kevin O'Connore7739302009-07-26 19:16:09 -040074// The end of the last deployed rom.
75u32 RomEnd;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050076
77
78/****************************************************************
79 * Helper functions
80 ****************************************************************/
81
Kevin O'Connor714325c2008-11-01 20:32:27 -040082// Execute a given option rom.
83static void
Kevin O'Connore8f00ee2009-07-04 04:04:36 -040084__callrom(struct rom_header *rom, u16 offset, u16 bdf)
Kevin O'Connor714325c2008-11-01 20:32:27 -040085{
Kevin O'Connor35ae7262009-01-19 15:44:44 -050086 u16 seg = FLATPTR_TO_SEG(rom);
Kevin O'Connor91b53a72009-05-05 22:52:09 -040087 dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
Kevin O'Connor714325c2008-11-01 20:32:27 -040088
89 struct bregs br;
90 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040091 br.flags = F_IF;
Kevin O'Connorceea03c2008-11-08 21:36:35 -050092 br.ax = bdf;
Kevin O'Connor714325c2008-11-01 20:32:27 -040093 br.bx = 0xffff;
94 br.dx = 0xffff;
95 br.es = SEG_BIOS;
Kevin O'Connor0c3068d2008-12-21 17:51:36 -050096 br.di = get_pnp_offset();
Kevin O'Connor9f985422009-09-09 11:34:39 -040097 br.code = SEGOFF(seg, offset);
Kevin O'Connorad901592009-12-13 11:25:25 -050098 start_preempt();
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050099 call16big(&br);
Kevin O'Connorad901592009-12-13 11:25:25 -0500100 finish_preempt();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400101
102 debug_serial_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400103}
104
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400105// Execute a given option rom at the standard entry vector.
106static void
107callrom(struct rom_header *rom, u16 bdf)
108{
109 __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
110}
111
Kevin O'Connor0a924122009-02-08 19:43:47 -0500112// Execute a BCV option rom registered via add_bcv().
113void
114call_bcv(u16 seg, u16 ip)
115{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400116 __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500117}
118
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500119// Verify that an option rom looks valid
120static int
121is_valid_rom(struct rom_header *rom)
122{
Kevin O'Connorc36273f2009-04-16 20:43:07 -0400123 dprintf(6, "Checking rom %p (sig %x size %d)\n"
124 , rom, rom->signature, rom->size);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500125 if (rom->signature != OPTION_ROM_SIGNATURE)
126 return 0;
Kevin O'Connor674e4602009-04-13 19:19:22 -0400127 if (! rom->size)
128 return 0;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500129 u32 len = rom->size * 512;
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500130 u8 sum = checksum(rom, len);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500131 if (sum != 0) {
132 dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
133 , rom, len, sum);
134 return 0;
135 }
136 return 1;
137}
138
139// Check if a valid option rom has a pnp struct; return it if so.
140static struct pnp_data *
141get_pnp_rom(struct rom_header *rom)
142{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400143 struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500144 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500145 return NULL;
146 return pnp;
147}
148
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500149// Check for multiple pnp option rom headers.
150static struct pnp_data *
151get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
152{
153 if (! pnp->nextoffset)
154 return NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400155 pnp = (void*)((u8*)rom + pnp->nextoffset);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500156 if (pnp->signature != PNP_SIGNATURE)
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500157 return NULL;
158 return pnp;
159}
160
Kevin O'Connor62eea672008-12-06 11:57:45 -0500161// Check if a valid option rom has a pci struct; return it if so.
162static struct pci_data *
163get_pci_rom(struct rom_header *rom)
164{
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400165 struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500166 if (pci->signature != PCI_ROM_SIGNATURE)
167 return NULL;
168 return pci;
169}
170
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400171// Return the memory position up to which roms may be located.
172static inline u32
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500173max_rom(void)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400174{
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500175 extern u8 code32flat_start[];
176 if ((u32)code32flat_start > BUILD_BIOS_ADDR)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400177 return BUILD_BIOS_ADDR;
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500178 return (u32)code32flat_start;
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400179}
180
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500181// Copy a rom to its permanent location below 1MiB
182static struct rom_header *
183copy_rom(struct rom_header *rom)
184{
185 u32 romsize = rom->size * 512;
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400186 if (RomEnd + romsize > max_rom()) {
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500187 // Option rom doesn't fit.
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500188 warn_noalloc();
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500189 return NULL;
190 }
Kevin O'Connorc36273f2009-04-16 20:43:07 -0400191 dprintf(4, "Copying option rom (size %d) from %p to %x\n"
Kevin O'Connore7739302009-07-26 19:16:09 -0400192 , romsize, rom, RomEnd);
Kevin O'Connor34036962009-12-05 18:51:53 -0500193 iomemcpy((void*)RomEnd, rom, romsize);
Kevin O'Connore7739302009-07-26 19:16:09 -0400194 return (void*)RomEnd;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500195}
196
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400197// Run rom init code and note rom size.
198static int
199init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
200{
201 if (! is_valid_rom(rom))
202 return -1;
203
204 if (isvga || get_pnp_rom(rom))
205 // Only init vga and PnP roms here.
206 callrom(rom, bdf);
207
Kevin O'Connore7739302009-07-26 19:16:09 -0400208 RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400209
210 return 0;
211}
212
213
214/****************************************************************
215 * Roms in CBFS
216 ****************************************************************/
217
218// Check if an option rom is at a hardcoded location or in CBFS.
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500219static struct rom_header *
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400220lookup_hardcode(u32 vendev)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500221{
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400222 if (OPTIONROM_VENDEV_1
223 && ((OPTIONROM_VENDEV_1 >> 16)
224 | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400225 return copy_rom((void*)OPTIONROM_MEM_1);
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400226 if (OPTIONROM_VENDEV_2
227 && ((OPTIONROM_VENDEV_2 >> 16)
228 | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400229 return copy_rom((void*)OPTIONROM_MEM_2);
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400230 int ret = cbfs_copy_optionrom((void*)RomEnd, max_rom() - RomEnd, vendev);
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400231 if (ret < 0)
232 return NULL;
Kevin O'Connore7739302009-07-26 19:16:09 -0400233 return (void*)RomEnd;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500234}
235
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400236// Run all roms in a given CBFS directory.
237static void
238run_cbfs_roms(const char *prefix, int isvga)
239{
Kevin O'Connor1f836252009-08-16 20:17:35 -0400240 struct cbfs_file *file = NULL;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400241 for (;;) {
Kevin O'Connor00882592009-08-18 22:21:10 -0400242 file = cbfs_findprefix(prefix, file);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400243 if (!file)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400244 break;
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400245 int ret = cbfs_copyfile(file, (void*)RomEnd, max_rom() - RomEnd);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400246 if (ret > 0)
247 init_optionrom((void*)RomEnd, 0, isvga);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400248 }
249}
250
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100251static void
252run_qemu_roms(const char *prefix, int isvga)
253{
254 struct QemuCfgFile entry;
255 int plen = strlen(prefix);
256 int rc, dlen;
257
258 rc = qemu_cfg_first_file(&entry);
259 while (rc > 0) {
260 if (memcmp(prefix, entry.name, plen) == 0) {
261 dlen = qemu_cfg_read_file(&entry, (void*)RomEnd, max_rom() - RomEnd);
262 if (dlen > 0) {
263 dprintf(1, "init qemu rom: %s\n", entry.name);
264 init_optionrom((void*)RomEnd, 0, isvga);
265 }
266 }
267 rc = qemu_cfg_next_file(&entry);
268 }
269}
270
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400271
272/****************************************************************
273 * PCI roms
274 ****************************************************************/
275
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500276// Map the option rom of a given PCI device.
277static struct rom_header *
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400278map_pcirom(u16 bdf, u32 vendev)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500279{
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400280 dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
281 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500282
Kevin O'Connor62eea672008-12-06 11:57:45 -0500283 u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
284 if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
285 dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
286 return NULL;
287 }
288
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500289 u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
290 pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
291 u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500292
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500293 dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500294 orig &= ~PCI_ROM_ADDRESS_ENABLE;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500295 if (!sz || sz == 0xffffffff)
296 goto fail;
297
Kevin O'Connor64cf2fb2009-04-18 17:01:02 -0400298 if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
299 // Don't try to map to a pci addresses at its max, in the last
300 // 4MiB of ram, or the first 16MiB of ram.
Kevin O'Connor62eea672008-12-06 11:57:45 -0500301 dprintf(6, "Preset rom address doesn't look valid\n");
302 goto fail;
303 }
304
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500305 // Looks like a rom - enable it.
306 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500307
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400308 struct rom_header *rom = (void*)orig;
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500309 for (;;) {
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400310 dprintf(5, "Inspecting possible rom at %p (dv=%08x bdf=%x)\n"
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500311 , rom, vendev, bdf);
312 if (rom->signature != OPTION_ROM_SIGNATURE) {
313 dprintf(6, "No option rom signature (got %x)\n", rom->signature);
314 goto fail;
315 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500316 struct pci_data *pci = get_pci_rom(rom);
317 if (! pci) {
318 dprintf(6, "No valid pci signature found\n");
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500319 goto fail;
320 }
Kevin O'Connor62eea672008-12-06 11:57:45 -0500321
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500322 u32 vd = (pci->device << 16) | pci->vendor;
Kevin O'Connor62eea672008-12-06 11:57:45 -0500323 if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500324 // A match
325 break;
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400326 dprintf(6, "Didn't match dev/ven (got %08x) or type (got %d)\n"
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500327 , vd, pci->type);
328 if (pci->indicator & 0x80) {
329 dprintf(6, "No more images left\n");
330 goto fail;
331 }
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400332 rom = (void*)((u32)rom + pci->ilen * 512);
Kevin O'Connore5a36d52008-11-12 20:10:13 -0500333 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500334
Kevin O'Connor5a1b8282008-11-29 20:39:06 -0500335 rom = copy_rom(rom);
336 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500337 return rom;
338fail:
339 // Not valid - restore original and exit.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500340 pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500341 return NULL;
342}
343
344// Attempt to map and initialize the option rom on a given PCI device.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400345static int
346init_pcirom(u16 bdf, int isvga)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500347{
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400348 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400349 dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %08x)\n"
Kevin O'Connor91b53a72009-05-05 22:52:09 -0400350 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
351 , vendev);
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400352 struct rom_header *rom = lookup_hardcode(vendev);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500353 if (! rom)
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400354 rom = map_pcirom(bdf, vendev);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500355 if (! rom)
356 // No ROM present.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400357 return -1;
358 return init_optionrom(rom, bdf, isvga);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500359}
360
361
362/****************************************************************
363 * Non-VGA option rom init
364 ****************************************************************/
365
366void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500367optionrom_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400368{
369 if (! CONFIG_OPTIONROMS)
370 return;
371
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500372 dprintf(1, "Scan for option roms\n");
373
Kevin O'Connore7739302009-07-26 19:16:09 -0400374 u32 post_vga = RomEnd;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500375
376 if (CONFIG_OPTIONROMS_DEPLOYED) {
377 // Option roms are already deployed on the system.
Kevin O'Connore7739302009-07-26 19:16:09 -0400378 u32 pos = RomEnd;
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400379 while (pos < max_rom()) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400380 int ret = init_optionrom((void*)pos, 0, 0);
381 if (ret)
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500382 pos += OPTION_ROM_ALIGN;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400383 else
Kevin O'Connore7739302009-07-26 19:16:09 -0400384 pos = RomEnd;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500385 }
386 } else {
387 // Find and deploy PCI roms.
Kevin O'Connore6338322008-11-29 20:31:49 -0500388 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500389 foreachpci(bdf, max) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500390 u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
Kevin O'Connor62eea672008-12-06 11:57:45 -0500391 if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
392 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500393 continue;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400394 init_pcirom(bdf, 0);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500395 }
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400396
397 // Find and deploy CBFS roms not associated with a device.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400398 run_cbfs_roms("genroms/", 0);
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100399 run_qemu_roms("genroms/", 0);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500400 }
401
402 // All option roms found and deployed - now build BEV/BCV vectors.
403
404 u32 pos = post_vga;
Kevin O'Connore7739302009-07-26 19:16:09 -0400405 while (pos < RomEnd) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400406 struct rom_header *rom = (void*)pos;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500407 if (! is_valid_rom(rom)) {
408 pos += OPTION_ROM_ALIGN;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400409 continue;
410 }
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500411 pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
412 struct pnp_data *pnp = get_pnp_rom(rom);
413 if (! pnp) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500414 // Legacy rom.
415 add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
Kevin O'Connor714325c2008-11-01 20:32:27 -0400416 continue;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500417 }
418 // PnP rom.
419 if (pnp->bev)
420 // Can boot system - add to IPL list.
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500421 add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500422 else
423 // Check for BCV (there may be multiple).
424 while (pnp && pnp->bcv) {
Kevin O'Connor0a924122009-02-08 19:43:47 -0500425 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
Kevin O'Connor3f57b5c2008-12-20 23:32:16 -0500426 pnp = get_pnp_next(rom, pnp);
427 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400428 }
429}
430
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500431
432/****************************************************************
433 * VGA init
434 ****************************************************************/
435
Kevin O'Connor714325c2008-11-01 20:32:27 -0400436// Call into vga code to turn on console.
437void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500438vga_setup(void)
Kevin O'Connor714325c2008-11-01 20:32:27 -0400439{
Kevin O'Connore7739302009-07-26 19:16:09 -0400440 VGAbdf = -1;
441 RomEnd = BUILD_ROM_START;
442
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500443 if (! CONFIG_OPTIONROMS)
444 return;
445
Kevin O'Connor714325c2008-11-01 20:32:27 -0400446 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500447
448 if (CONFIG_OPTIONROMS_DEPLOYED) {
449 // Option roms are already deployed on the system.
Kevin O'Connore7739302009-07-26 19:16:09 -0400450 init_optionrom((void*)BUILD_ROM_START, 0, 1);
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500451 } else {
452 // Find and deploy PCI VGA rom.
Kevin O'Connor04eece22009-07-19 19:05:30 -0400453 int bdf = VGAbdf = pci_find_vga();
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400454 if (bdf >= 0)
455 init_pcirom(bdf, 1);
Kevin O'Connor09880da2009-06-17 20:35:41 -0400456
457 // Find and deploy CBFS vga-style roms not associated with a device.
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400458 run_cbfs_roms("vgaroms/", 1);
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100459 run_qemu_roms("vgaroms/", 1);
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400460 }
461
Kevin O'Connore7739302009-07-26 19:16:09 -0400462 if (RomEnd == BUILD_ROM_START) {
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400463 // No VGA rom found
Kevin O'Connore7739302009-07-26 19:16:09 -0400464 RomEnd += OPTION_ROM_ALIGN;
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400465 return;
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500466 }
Kevin O'Connor714325c2008-11-01 20:32:27 -0400467
468 dprintf(1, "Turning on vga console\n");
469 struct bregs br;
470 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400471 br.flags = F_IF;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400472 br.ax = 0x0003;
473 call16_int(0x10, &br);
474
475 // Write to screen.
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400476 printf("Starting SeaBIOS (version %s)\n\n", VERSION);
Kevin O'Connor714325c2008-11-01 20:32:27 -0400477}
Kevin O'Connord282af72009-07-04 04:10:32 -0400478
479void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500480s3_resume_vga_init(void)
Kevin O'Connord282af72009-07-04 04:10:32 -0400481{
482 if (!CONFIG_S3_RESUME_VGA_INIT)
483 return;
Kevin O'Connore7739302009-07-26 19:16:09 -0400484 struct rom_header *rom = (void*)BUILD_ROM_START;
Kevin O'Connord282af72009-07-04 04:10:32 -0400485 if (! is_valid_rom(rom))
486 return;
487 callrom(rom, 0);
488}