Allow rom to grow beyond 64K.
[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" // foreachpci
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.code = SEGOFF(seg, offset);
96     call16big(&br);
97
98     debug_serial_setup();
99 }
100
101 // Execute a given option rom at the standard entry vector.
102 static void
103 callrom(struct rom_header *rom, u16 bdf)
104 {
105     __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
106 }
107
108 // Execute a BCV option rom registered via add_bcv().
109 void
110 call_bcv(u16 seg, u16 ip)
111 {
112     __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
113 }
114
115 // Verify that an option rom looks valid
116 static int
117 is_valid_rom(struct rom_header *rom)
118 {
119     dprintf(6, "Checking rom %p (sig %x size %d)\n"
120             , rom, rom->signature, rom->size);
121     if (rom->signature != OPTION_ROM_SIGNATURE)
122         return 0;
123     if (! rom->size)
124         return 0;
125     u32 len = rom->size * 512;
126     u8 sum = checksum(rom, len);
127     if (sum != 0) {
128         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
129                 , rom, len, sum);
130         return 0;
131     }
132     return 1;
133 }
134
135 // Check if a valid option rom has a pnp struct; return it if so.
136 static struct pnp_data *
137 get_pnp_rom(struct rom_header *rom)
138 {
139     struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
140     if (pnp->signature != PNP_SIGNATURE)
141         return NULL;
142     return pnp;
143 }
144
145 // Check for multiple pnp option rom headers.
146 static struct pnp_data *
147 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
148 {
149     if (! pnp->nextoffset)
150         return NULL;
151     pnp = (void*)((u8*)rom + pnp->nextoffset);
152     if (pnp->signature != PNP_SIGNATURE)
153         return NULL;
154     return pnp;
155 }
156
157 // Check if a valid option rom has a pci struct; return it if so.
158 static struct pci_data *
159 get_pci_rom(struct rom_header *rom)
160 {
161     struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
162     if (pci->signature != PCI_ROM_SIGNATURE)
163         return NULL;
164     return pci;
165 }
166
167 // Return the memory position up to which roms may be located.
168 static inline u32
169 max_rom()
170 {
171     extern u8 code32_start[];
172     if ((u32)code32_start > BUILD_BIOS_ADDR)
173         return BUILD_BIOS_ADDR;
174     return (u32)code32_start;
175 }
176
177 // Copy a rom to its permanent location below 1MiB
178 static struct rom_header *
179 copy_rom(struct rom_header *rom)
180 {
181     u32 romsize = rom->size * 512;
182     if (RomEnd + romsize > max_rom()) {
183         // Option rom doesn't fit.
184         dprintf(1, "Option rom %p doesn't fit.\n", rom);
185         return NULL;
186     }
187     dprintf(4, "Copying option rom (size %d) from %p to %x\n"
188             , romsize, rom, RomEnd);
189     memcpy((void*)RomEnd, rom, romsize);
190     return (void*)RomEnd;
191 }
192
193 // Run rom init code and note rom size.
194 static int
195 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
196 {
197     if (! is_valid_rom(rom))
198         return -1;
199
200     if (isvga || get_pnp_rom(rom))
201         // Only init vga and PnP roms here.
202         callrom(rom, bdf);
203
204     RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
205
206     return 0;
207 }
208
209
210 /****************************************************************
211  * Roms in CBFS
212  ****************************************************************/
213
214 // Check if an option rom is at a hardcoded location or in CBFS.
215 static struct rom_header *
216 lookup_hardcode(u32 vendev)
217 {
218     if (OPTIONROM_VENDEV_1
219         && ((OPTIONROM_VENDEV_1 >> 16)
220             | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
221         return copy_rom((void*)OPTIONROM_MEM_1);
222     if (OPTIONROM_VENDEV_2
223         && ((OPTIONROM_VENDEV_2 >> 16)
224             | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
225         return copy_rom((void*)OPTIONROM_MEM_2);
226     int ret = cbfs_copy_optionrom((void*)RomEnd, max_rom() - RomEnd, vendev);
227     if (ret < 0)
228         return NULL;
229     return (void*)RomEnd;
230 }
231
232 // Run all roms in a given CBFS directory.
233 static void
234 run_cbfs_roms(const char *prefix, int isvga)
235 {
236     struct cbfs_file *file = NULL;
237     for (;;) {
238         file = cbfs_findprefix(prefix, file);
239         if (!file)
240             break;
241         int ret = cbfs_copyfile(file, (void*)RomEnd, max_rom() - RomEnd);
242         if (ret > 0)
243             init_optionrom((void*)RomEnd, 0, isvga);
244     }
245 }
246
247
248 /****************************************************************
249  * PCI roms
250  ****************************************************************/
251
252 // Map the option rom of a given PCI device.
253 static struct rom_header *
254 map_pcirom(u16 bdf, u32 vendev)
255 {
256     dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
257             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
258
259     u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
260     if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
261         dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
262         return NULL;
263     }
264
265     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
266     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
267     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
268
269     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
270     orig &= ~PCI_ROM_ADDRESS_ENABLE;
271     if (!sz || sz == 0xffffffff)
272         goto fail;
273
274     if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
275         // Don't try to map to a pci addresses at its max, in the last
276         // 4MiB of ram, or the first 16MiB of ram.
277         dprintf(6, "Preset rom address doesn't look valid\n");
278         goto fail;
279     }
280
281     // Looks like a rom - enable it.
282     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
283
284     struct rom_header *rom = (void*)orig;
285     for (;;) {
286         dprintf(5, "Inspecting possible rom at %p (dv=%08x bdf=%x)\n"
287                 , rom, vendev, bdf);
288         if (rom->signature != OPTION_ROM_SIGNATURE) {
289             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
290             goto fail;
291         }
292         struct pci_data *pci = get_pci_rom(rom);
293         if (! pci) {
294             dprintf(6, "No valid pci signature found\n");
295             goto fail;
296         }
297
298         u32 vd = (pci->device << 16) | pci->vendor;
299         if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
300             // A match
301             break;
302         dprintf(6, "Didn't match dev/ven (got %08x) or type (got %d)\n"
303                 , vd, pci->type);
304         if (pci->indicator & 0x80) {
305             dprintf(6, "No more images left\n");
306             goto fail;
307         }
308         rom = (void*)((u32)rom + pci->ilen * 512);
309     }
310
311     rom = copy_rom(rom);
312     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
313     return rom;
314 fail:
315     // Not valid - restore original and exit.
316     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
317     return NULL;
318 }
319
320 // Attempt to map and initialize the option rom on a given PCI device.
321 static int
322 init_pcirom(u16 bdf, int isvga)
323 {
324     u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
325     dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %08x)\n"
326             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
327             , vendev);
328     struct rom_header *rom = lookup_hardcode(vendev);
329     if (! rom)
330         rom = map_pcirom(bdf, vendev);
331     if (! rom)
332         // No ROM present.
333         return -1;
334     return init_optionrom(rom, bdf, isvga);
335 }
336
337
338 /****************************************************************
339  * Non-VGA option rom init
340  ****************************************************************/
341
342 void
343 optionrom_setup()
344 {
345     if (! CONFIG_OPTIONROMS)
346         return;
347
348     dprintf(1, "Scan for option roms\n");
349
350     u32 post_vga = RomEnd;
351
352     if (CONFIG_OPTIONROMS_DEPLOYED) {
353         // Option roms are already deployed on the system.
354         u32 pos = RomEnd;
355         while (pos < max_rom()) {
356             int ret = init_optionrom((void*)pos, 0, 0);
357             if (ret)
358                 pos += OPTION_ROM_ALIGN;
359             else
360                 pos = RomEnd;
361         }
362     } else {
363         // Find and deploy PCI roms.
364         int bdf, max;
365         foreachpci(bdf, max) {
366             u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
367             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
368                 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
369                 continue;
370             init_pcirom(bdf, 0);
371         }
372
373         // Find and deploy CBFS roms not associated with a device.
374         run_cbfs_roms("genroms/", 0);
375     }
376
377     // All option roms found and deployed - now build BEV/BCV vectors.
378
379     u32 pos = post_vga;
380     while (pos < RomEnd) {
381         struct rom_header *rom = (void*)pos;
382         if (! is_valid_rom(rom)) {
383             pos += OPTION_ROM_ALIGN;
384             continue;
385         }
386         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
387         struct pnp_data *pnp = get_pnp_rom(rom);
388         if (! pnp) {
389             // Legacy rom.
390             add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
391             continue;
392         }
393         // PnP rom.
394         if (pnp->bev)
395             // Can boot system - add to IPL list.
396             add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
397         else
398             // Check for BCV (there may be multiple).
399             while (pnp && pnp->bcv) {
400                 add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
401                 pnp = get_pnp_next(rom, pnp);
402             }
403     }
404 }
405
406
407 /****************************************************************
408  * VGA init
409  ****************************************************************/
410
411 // Call into vga code to turn on console.
412 void
413 vga_setup()
414 {
415     VGAbdf = -1;
416     RomEnd = BUILD_ROM_START;
417
418     if (! CONFIG_OPTIONROMS)
419         return;
420
421     dprintf(1, "Scan for VGA option rom\n");
422
423     if (CONFIG_OPTIONROMS_DEPLOYED) {
424         // Option roms are already deployed on the system.
425         init_optionrom((void*)BUILD_ROM_START, 0, 1);
426     } else {
427         // Find and deploy PCI VGA rom.
428         int bdf = VGAbdf = pci_find_vga();
429         if (bdf >= 0)
430             init_pcirom(bdf, 1);
431
432         // Find and deploy CBFS vga-style roms not associated with a device.
433         run_cbfs_roms("vgaroms/", 1);
434     }
435
436     if (RomEnd == BUILD_ROM_START) {
437         // No VGA rom found
438         RomEnd += OPTION_ROM_ALIGN;
439         return;
440     }
441
442     dprintf(1, "Turning on vga console\n");
443     struct bregs br;
444     memset(&br, 0, sizeof(br));
445     br.ax = 0x0003;
446     call16_int(0x10, &br);
447
448     // Write to screen.
449     printf("Starting SeaBIOS (version %s)\n\n", VERSION);
450 }
451
452 void
453 s3_resume_vga_init()
454 {
455     if (!CONFIG_S3_RESUME_VGA_INIT)
456         return;
457     struct rom_header *rom = (void*)BUILD_ROM_START;
458     if (! is_valid_rom(rom))
459         return;
460     callrom(rom, 0);
461 }