0a5d85c1fc2609b4602bc6920a6431f65b4c1168
[seabios.git] / src / optionroms.c
1 // Option rom scanning code.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "bregs.h" // struct bregs
9 #include "farptr.h" // FLATPTR_TO_SEG
10 #include "config.h" // CONFIG_*
11 #include "util.h" // dprintf
12 #include "pci.h" // pci_find_class
13 #include "pci_regs.h" // PCI_ROM_ADDRESS
14 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
15 #include "boot.h" // IPL
16
17
18 /****************************************************************
19  * Definitions
20  ****************************************************************/
21
22 struct rom_header {
23     u16 signature;
24     u8 size;
25     u8 initVector[4];
26     u8 reserved[17];
27     u16 pcioffset;
28     u16 pnpoffset;
29 } PACKED;
30
31 struct pci_data {
32     u32 signature;
33     u16 vendor;
34     u16 device;
35     u16 vitaldata;
36     u16 dlen;
37     u8 drevision;
38     u8 class_lo;
39     u16 class_hi;
40     u16 ilen;
41     u16 irevision;
42     u8 type;
43     u8 indicator;
44     u16 reserved;
45 } PACKED;
46
47 struct pnp_data {
48     u32 signature;
49     u8 revision;
50     u8 len;
51     u16 nextoffset;
52     u8 reserved_08;
53     u8 checksum;
54     u32 devid;
55     u16 manufacturer;
56     u16 productname;
57     u8 type_lo;
58     u16 type_hi;
59     u8 dev_flags;
60     u16 bcv;
61     u16 dv;
62     u16 bev;
63     u16 reserved_1c;
64     u16 staticresource;
65 } PACKED;
66
67 #define OPTION_ROM_SIGNATURE 0xaa55
68 #define OPTION_ROM_ALIGN 2048
69 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
70 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
71 #define PCIROM_CODETYPE_X86 0
72
73 // The end of the last deployed rom.
74 u32 RomEnd;
75
76
77 /****************************************************************
78  * Helper functions
79  ****************************************************************/
80
81 // Execute a given option rom.
82 static void
83 __callrom(struct rom_header *rom, u16 offset, u16 bdf)
84 {
85     u16 seg = FLATPTR_TO_SEG(rom);
86     dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
87
88     struct bregs br;
89     memset(&br, 0, sizeof(br));
90     br.ax = bdf;
91     br.bx = 0xffff;
92     br.dx = 0xffff;
93     br.es = SEG_BIOS;
94     br.di = get_pnp_offset();
95     br.cs = seg;
96     br.ip = offset;
97     call16big(&br);
98
99     debug_serial_setup();
100 }
101
102 // Execute a given option rom at the standard entry vector.
103 static void
104 callrom(struct rom_header *rom, u16 bdf)
105 {
106     __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
107 }
108
109 // Execute a BCV option rom registered via add_bcv().
110 void
111 call_bcv(u16 seg, u16 ip)
112 {
113     __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
114 }
115
116 // Verify that an option rom looks valid
117 static int
118 is_valid_rom(struct rom_header *rom)
119 {
120     dprintf(6, "Checking rom %p (sig %x size %d)\n"
121             , rom, rom->signature, rom->size);
122     if (rom->signature != OPTION_ROM_SIGNATURE)
123         return 0;
124     if (! rom->size)
125         return 0;
126     u32 len = rom->size * 512;
127     u8 sum = checksum(rom, len);
128     if (sum != 0) {
129         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
130                 , rom, len, sum);
131         return 0;
132     }
133     return 1;
134 }
135
136 // Check if a valid option rom has a pnp struct; return it if so.
137 static struct pnp_data *
138 get_pnp_rom(struct rom_header *rom)
139 {
140     struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
141     if (pnp->signature != PNP_SIGNATURE)
142         return NULL;
143     return pnp;
144 }
145
146 // Check for multiple pnp option rom headers.
147 static struct pnp_data *
148 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
149 {
150     if (! pnp->nextoffset)
151         return NULL;
152     pnp = (void*)((u8*)rom + pnp->nextoffset);
153     if (pnp->signature != PNP_SIGNATURE)
154         return NULL;
155     return pnp;
156 }
157
158 // Check if a valid option rom has a pci struct; return it if so.
159 static struct pci_data *
160 get_pci_rom(struct rom_header *rom)
161 {
162     struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
163     if (pci->signature != PCI_ROM_SIGNATURE)
164         return NULL;
165     return pci;
166 }
167
168 // Copy a rom to its permanent location below 1MiB
169 static struct rom_header *
170 copy_rom(struct rom_header *rom)
171 {
172     u32 romsize = rom->size * 512;
173     if (RomEnd + romsize > BUILD_BIOS_ADDR) {
174         // Option rom doesn't fit.
175         dprintf(1, "Option rom %p doesn't fit.\n", rom);
176         return NULL;
177     }
178     dprintf(4, "Copying option rom (size %d) from %p to %x\n"
179             , romsize, rom, RomEnd);
180     memcpy((void*)RomEnd, rom, romsize);
181     return (void*)RomEnd;
182 }
183
184 // Run rom init code and note rom size.
185 static int
186 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
187 {
188     if (! is_valid_rom(rom))
189         return -1;
190
191     if (isvga || get_pnp_rom(rom))
192         // Only init vga and PnP roms here.
193         callrom(rom, bdf);
194
195     RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
196
197     return 0;
198 }
199
200
201 /****************************************************************
202  * Roms in CBFS
203  ****************************************************************/
204
205 // Check if an option rom is at a hardcoded location or in CBFS.
206 static struct rom_header *
207 lookup_hardcode(u32 vendev)
208 {
209     if (OPTIONROM_VENDEV_1
210         && ((OPTIONROM_VENDEV_1 >> 16)
211             | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
212         return copy_rom((void*)OPTIONROM_MEM_1);
213     if (OPTIONROM_VENDEV_2
214         && ((OPTIONROM_VENDEV_2 >> 16)
215             | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
216         return copy_rom((void*)OPTIONROM_MEM_2);
217     int ret = cbfs_copy_optionrom((void*)RomEnd, BUILD_BIOS_ADDR - RomEnd
218                                   , vendev);
219     if (ret < 0)
220         return NULL;
221     return (void*)RomEnd;
222 }
223
224 // Run all roms in a given CBFS directory.
225 static void
226 run_cbfs_roms(const char *prefix, int isvga)
227 {
228     struct cbfs_file *file = NULL;
229     for (;;) {
230         file = cbfs_findprefix(prefix, file);
231         if (!file)
232             break;
233         int ret = cbfs_copyfile(file, (void*)RomEnd, BUILD_BIOS_ADDR - RomEnd);
234         if (ret > 0)
235             init_optionrom((void*)RomEnd, 0, isvga);
236     }
237 }
238
239
240 /****************************************************************
241  * PCI roms
242  ****************************************************************/
243
244 // Map the option rom of a given PCI device.
245 static struct rom_header *
246 map_pcirom(u16 bdf, u32 vendev)
247 {
248     dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
249             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
250
251     u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
252     if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
253         dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
254         return NULL;
255     }
256
257     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
258     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
259     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
260
261     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
262     orig &= ~PCI_ROM_ADDRESS_ENABLE;
263     if (!sz || sz == 0xffffffff)
264         goto fail;
265
266     if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
267         // Don't try to map to a pci addresses at its max, in the last
268         // 4MiB of ram, or the first 16MiB of ram.
269         dprintf(6, "Preset rom address doesn't look valid\n");
270         goto fail;
271     }
272
273     // Looks like a rom - enable it.
274     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
275
276     struct rom_header *rom = (void*)orig;
277     for (;;) {
278         dprintf(5, "Inspecting possible rom at %p (dv=%08x bdf=%x)\n"
279                 , rom, vendev, bdf);
280         if (rom->signature != OPTION_ROM_SIGNATURE) {
281             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
282             goto fail;
283         }
284         struct pci_data *pci = get_pci_rom(rom);
285         if (! pci) {
286             dprintf(6, "No valid pci signature found\n");
287             goto fail;
288         }
289
290         u32 vd = (pci->device << 16) | pci->vendor;
291         if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
292             // A match
293             break;
294         dprintf(6, "Didn't match dev/ven (got %08x) or type (got %d)\n"
295                 , vd, pci->type);
296         if (pci->indicator & 0x80) {
297             dprintf(6, "No more images left\n");
298             goto fail;
299         }
300         rom = (void*)((u32)rom + pci->ilen * 512);
301     }
302
303     rom = copy_rom(rom);
304     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
305     return rom;
306 fail:
307     // Not valid - restore original and exit.
308     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
309     return NULL;
310 }
311
312 // Attempt to map and initialize the option rom on a given PCI device.
313 static int
314 init_pcirom(u16 bdf, int isvga)
315 {
316     u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
317     dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %08x)\n"
318             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
319             , vendev);
320     struct rom_header *rom = lookup_hardcode(vendev);
321     if (! rom)
322         rom = map_pcirom(bdf, vendev);
323     if (! rom)
324         // No ROM present.
325         return -1;
326     return init_optionrom(rom, bdf, isvga);
327 }
328
329
330 /****************************************************************
331  * Non-VGA option rom init
332  ****************************************************************/
333
334 void
335 optionrom_setup()
336 {
337     if (! CONFIG_OPTIONROMS)
338         return;
339
340     dprintf(1, "Scan for option roms\n");
341
342     u32 post_vga = RomEnd;
343
344     if (CONFIG_OPTIONROMS_DEPLOYED) {
345         // Option roms are already deployed on the system.
346         u32 pos = RomEnd;
347         while (pos < BUILD_BIOS_ADDR) {
348             int ret = init_optionrom((void*)pos, 0, 0);
349             if (ret)
350                 pos += OPTION_ROM_ALIGN;
351             else
352                 pos = RomEnd;
353         }
354     } else {
355         // Find and deploy PCI roms.
356         int bdf, max;
357         foreachpci(bdf, max) {
358             u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
359             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
360                 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
361                 continue;
362             init_pcirom(bdf, 0);
363         }
364
365         // Find and deploy CBFS roms not associated with a device.
366         run_cbfs_roms("genroms/", 0);
367     }
368
369     // All option roms found and deployed - now build BEV/BCV vectors.
370
371     u32 pos = post_vga;
372     while (pos < RomEnd) {
373         struct rom_header *rom = (void*)pos;
374         if (! is_valid_rom(rom)) {
375             pos += OPTION_ROM_ALIGN;
376             continue;
377         }
378         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
379         struct pnp_data *pnp = get_pnp_rom(rom);
380         if (! pnp) {
381             // Legacy rom.
382             add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
383             continue;
384         }
385         // PnP rom.
386         if (pnp->bev)
387             // Can boot system - add to IPL list.
388             add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
389         else
390             // Check for BCV (there may be multiple).
391             while (pnp && pnp->bcv) {
392                 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
393                 pnp = get_pnp_next(rom, pnp);
394             }
395     }
396 }
397
398
399 /****************************************************************
400  * VGA init
401  ****************************************************************/
402
403 // Call into vga code to turn on console.
404 void
405 vga_setup()
406 {
407     VGAbdf = -1;
408     RomEnd = BUILD_ROM_START;
409
410     if (! CONFIG_OPTIONROMS)
411         return;
412
413     dprintf(1, "Scan for VGA option rom\n");
414
415     if (CONFIG_OPTIONROMS_DEPLOYED) {
416         // Option roms are already deployed on the system.
417         init_optionrom((void*)BUILD_ROM_START, 0, 1);
418     } else {
419         // Find and deploy PCI VGA rom.
420         int bdf = VGAbdf = pci_find_vga();
421         if (bdf >= 0)
422             init_pcirom(bdf, 1);
423
424         // Find and deploy CBFS vga-style roms not associated with a device.
425         run_cbfs_roms("vgaroms/", 1);
426     }
427
428     if (RomEnd == BUILD_ROM_START) {
429         // No VGA rom found
430         RomEnd += OPTION_ROM_ALIGN;
431         return;
432     }
433
434     dprintf(1, "Turning on vga console\n");
435     struct bregs br;
436     memset(&br, 0, sizeof(br));
437     br.ax = 0x0003;
438     call16_int(0x10, &br);
439
440     // Write to screen.
441     printf("Starting SeaBIOS (version %s)\n\n", VERSION);
442 }
443
444 void
445 s3_resume_vga_init()
446 {
447     if (!CONFIG_S3_RESUME_VGA_INIT)
448         return;
449     struct rom_header *rom = (void*)BUILD_ROM_START;
450     if (! is_valid_rom(rom))
451         return;
452     callrom(rom, 0);
453 }