Add ability to run all option roms in CBFS directory "genroms/".
[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_START 0xc0000
68 #define OPTION_ROM_SIGNATURE 0xaa55
69 #define OPTION_ROM_ALIGN 2048
70 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
71 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
72 #define PCIROM_CODETYPE_X86 0
73
74 // Next available position for an option rom.
75 static u32 next_rom;
76
77
78 /****************************************************************
79  * Helper functions
80  ****************************************************************/
81
82 // Execute a given option rom.
83 static void
84 callrom(struct rom_header *rom, u16 offset, u16 bdf)
85 {
86     u16 seg = FLATPTR_TO_SEG(rom);
87     dprintf(1, "Running option rom at %x:%x\n", seg, offset);
88
89     struct bregs br;
90     memset(&br, 0, sizeof(br));
91     br.ax = bdf;
92     br.bx = 0xffff;
93     br.dx = 0xffff;
94     br.es = SEG_BIOS;
95     br.di = get_pnp_offset();
96     br.cs = seg;
97     br.ip = offset;
98     call16big(&br);
99
100     debug_serial_setup();
101 }
102
103 // Execute a BCV option rom registered via add_bcv().
104 void
105 call_bcv(u16 seg, u16 ip)
106 {
107     callrom(MAKE_FLATPTR(seg, 0), ip, 0);
108 }
109
110 // Verify that an option rom looks valid
111 static int
112 is_valid_rom(struct rom_header *rom)
113 {
114     dprintf(6, "Checking rom %p (sig %x size %d)\n"
115             , rom, rom->signature, rom->size);
116     if (rom->signature != OPTION_ROM_SIGNATURE)
117         return 0;
118     if (! rom->size)
119         return 0;
120     u32 len = rom->size * 512;
121     u8 sum = checksum(rom, len);
122     if (sum != 0) {
123         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
124                 , rom, len, sum);
125         return 0;
126     }
127     return 1;
128 }
129
130 // Check if a valid option rom has a pnp struct; return it if so.
131 static struct pnp_data *
132 get_pnp_rom(struct rom_header *rom)
133 {
134     struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
135     if (pnp->signature != PNP_SIGNATURE)
136         return NULL;
137     return pnp;
138 }
139
140 // Check for multiple pnp option rom headers.
141 static struct pnp_data *
142 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
143 {
144     if (! pnp->nextoffset)
145         return NULL;
146     pnp = (struct pnp_data *)((u8*)rom + pnp->nextoffset);
147     if (pnp->signature != PNP_SIGNATURE)
148         return NULL;
149     return pnp;
150 }
151
152 // Check if a valid option rom has a pci struct; return it if so.
153 static struct pci_data *
154 get_pci_rom(struct rom_header *rom)
155 {
156     struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
157     if (pci->signature != PCI_ROM_SIGNATURE)
158         return NULL;
159     return pci;
160 }
161
162 // Copy a rom to its permanent location below 1MiB
163 static struct rom_header *
164 copy_rom(struct rom_header *rom)
165 {
166     u32 romsize = rom->size * 512;
167     if (next_rom + romsize > BUILD_BIOS_ADDR) {
168         // Option rom doesn't fit.
169         dprintf(1, "Option rom %p doesn't fit.\n", rom);
170         return NULL;
171     }
172     dprintf(4, "Copying option rom (size %d) from %p to %x\n"
173             , romsize, rom, next_rom);
174     memcpy4((void*)next_rom, rom, romsize);
175     return (struct rom_header *)next_rom;
176 }
177
178 // Check if an option rom is at a hardcoded location for a device.
179 static struct rom_header *
180 lookup_hardcode(u32 vendev)
181 {
182     if (OPTIONROM_VENDEV_1
183         && ((OPTIONROM_VENDEV_1 >> 16)
184             | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
185         return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
186     if (OPTIONROM_VENDEV_2
187         && ((OPTIONROM_VENDEV_2 >> 16)
188             | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
189         return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
190     int ret = cbfs_copy_optionrom((void*)next_rom, BUILD_BIOS_ADDR - next_rom
191                                   , vendev);
192     if (ret < 0)
193         return NULL;
194     return (struct rom_header *)next_rom;
195 }
196
197 // Map the option rom of a given PCI device.
198 static struct rom_header *
199 map_optionrom(u16 bdf, u32 vendev)
200 {
201     dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
202
203     u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
204     if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
205         dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
206         return NULL;
207     }
208
209     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
210     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
211     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
212
213     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
214     orig &= ~PCI_ROM_ADDRESS_ENABLE;
215     if (!sz || sz == 0xffffffff)
216         goto fail;
217
218     if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
219         // Don't try to map to a pci addresses at its max, in the last
220         // 4MiB of ram, or the first 16MiB of ram.
221         dprintf(6, "Preset rom address doesn't look valid\n");
222         goto fail;
223     }
224
225     // Looks like a rom - enable it.
226     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
227
228     struct rom_header *rom = (struct rom_header *)orig;
229     for (;;) {
230         dprintf(5, "Inspecting possible rom at %p (dv=%x bdf=%x)\n"
231                 , rom, vendev, bdf);
232         if (rom->signature != OPTION_ROM_SIGNATURE) {
233             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
234             goto fail;
235         }
236         struct pci_data *pci = get_pci_rom(rom);
237         if (! pci) {
238             dprintf(6, "No valid pci signature found\n");
239             goto fail;
240         }
241
242         u32 vd = (pci->device << 16) | pci->vendor;
243         if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
244             // A match
245             break;
246         dprintf(6, "Didn't match dev/ven (got %x) or type (got %d)\n"
247                 , vd, pci->type);
248         if (pci->indicator & 0x80) {
249             dprintf(6, "No more images left\n");
250             goto fail;
251         }
252         rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
253     }
254
255     rom = copy_rom(rom);
256     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
257     return rom;
258 fail:
259     // Not valid - restore original and exit.
260     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
261     return NULL;
262 }
263
264 // Adjust for the size of an option rom; allow it to resize.
265 static struct rom_header *
266 verifysize_optionrom(struct rom_header *rom, u16 bdf)
267 {
268     if (! is_valid_rom(rom))
269         return NULL;
270
271     if (get_pnp_rom(rom))
272         // Init the PnP rom.
273         callrom(rom, OPTION_ROM_INITVECTOR, bdf);
274
275     next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
276
277     return rom;
278 }
279
280 // Attempt to map and initialize the option rom on a given PCI device.
281 static struct rom_header *
282 init_optionrom(u16 bdf)
283 {
284     u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
285     dprintf(4, "Attempting to init PCI bdf %x (dev/ven %x)\n", bdf, vendev);
286     struct rom_header *rom = lookup_hardcode(vendev);
287     if (! rom)
288         rom = map_optionrom(bdf, vendev);
289     if (! rom)
290         // No ROM present.
291         return NULL;
292     return verifysize_optionrom(rom, bdf);
293 }
294
295
296 /****************************************************************
297  * Non-VGA option rom init
298  ****************************************************************/
299
300 void
301 optionrom_setup()
302 {
303     if (! CONFIG_OPTIONROMS)
304         return;
305
306     dprintf(1, "Scan for option roms\n");
307
308     u32 post_vga = next_rom;
309
310     if (CONFIG_OPTIONROMS_DEPLOYED) {
311         // Option roms are already deployed on the system.
312         u32 pos = next_rom;
313         while (pos < BUILD_BIOS_ADDR) {
314             struct rom_header *rom = (struct rom_header *)pos;
315             if (! is_valid_rom(rom)) {
316                 pos += OPTION_ROM_ALIGN;
317                 continue;
318             }
319             if (get_pnp_rom(rom))
320                 callrom(rom, OPTION_ROM_INITVECTOR, 0);
321             pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
322             next_rom = pos;
323         }
324     } else {
325         // Find and deploy PCI roms.
326         int bdf, max;
327         foreachpci(bdf, max) {
328             u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
329             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
330                 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
331                 continue;
332             init_optionrom(bdf);
333         }
334
335         // Find and deploy CBFS roms not associated with a device.
336         struct cbfs_file *tmp = NULL;
337         for (;;) {
338             tmp = cbfs_copy_gen_optionrom(
339                 (void*)next_rom, BUILD_BIOS_ADDR - next_rom, tmp);
340             if (!tmp)
341                 break;
342             verifysize_optionrom((void*)next_rom, 0);
343         }
344     }
345
346     // All option roms found and deployed - now build BEV/BCV vectors.
347
348     u32 pos = post_vga;
349     while (pos < next_rom) {
350         struct rom_header *rom = (struct rom_header *)pos;
351         if (! is_valid_rom(rom)) {
352             pos += OPTION_ROM_ALIGN;
353             continue;
354         }
355         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
356         struct pnp_data *pnp = get_pnp_rom(rom);
357         if (! pnp) {
358             // Legacy rom.
359             add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
360             continue;
361         }
362         // PnP rom.
363         if (pnp->bev)
364             // Can boot system - add to IPL list.
365             add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
366         else
367             // Check for BCV (there may be multiple).
368             while (pnp && pnp->bcv) {
369                 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
370                 pnp = get_pnp_next(rom, pnp);
371             }
372     }
373 }
374
375
376 /****************************************************************
377  * VGA init
378  ****************************************************************/
379
380 // Call into vga code to turn on console.
381 void
382 vga_setup()
383 {
384     if (! CONFIG_OPTIONROMS)
385         return;
386
387     dprintf(1, "Scan for VGA option rom\n");
388     next_rom = OPTION_ROM_START;
389
390     if (CONFIG_OPTIONROMS_DEPLOYED) {
391         // Option roms are already deployed on the system.
392         struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
393         if (! is_valid_rom(rom))
394             return;
395         callrom(rom, OPTION_ROM_INITVECTOR, 0);
396         next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
397     } else {
398         // Find and deploy PCI VGA rom.
399         int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
400         if (bdf < 0)
401             // Device not found
402             return;
403
404         struct rom_header *rom = init_optionrom(bdf);
405         if (rom && !get_pnp_rom(rom))
406             // Call rom even if it isn't a pnp rom.
407             callrom(rom, OPTION_ROM_INITVECTOR, bdf);
408     }
409
410     dprintf(1, "Turning on vga console\n");
411     struct bregs br;
412     memset(&br, 0, sizeof(br));
413     br.ax = 0x0003;
414     call16_int(0x10, &br);
415
416     // Write to screen.
417     printf("Starting SeaBIOS\n\n");
418 }