Add additional PCI option rom checks.
[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 GPLv3 license.
7
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
14
15
16 /****************************************************************
17  * Definitions
18  ****************************************************************/
19
20 // $PnP string with special alignment in romlayout.S
21 extern char pnp_string[];
22
23 struct 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
32 struct 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
48 struct 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
68 #define OPTION_ROM_START 0xc0000
69 #define OPTION_ROM_SIGNATURE 0xaa55
70 #define OPTION_ROM_ALIGN 2048
71 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
72 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
73 #define PCIROM_CODETYPE_X86 0
74
75 // Next available position for an option rom.
76 static u32 next_rom;
77
78
79 /****************************************************************
80  * Helper functions
81  ****************************************************************/
82
83 // Execute a given option rom.
84 static void
85 callrom(struct rom_header *rom, u16 offset, u16 bdf)
86 {
87     u16 seg = FARPTR_TO_SEG(rom);
88     dprintf(1, "Running option rom at %x:%x\n", seg, offset);
89
90     struct bregs br;
91     memset(&br, 0, sizeof(br));
92     br.ax = bdf;
93     br.bx = 0xffff;
94     br.dx = 0xffff;
95     br.es = SEG_BIOS;
96     br.di = (u32)pnp_string - BUILD_BIOS_ADDR;
97     br.cs = seg;
98     br.ip = offset;
99     // XXX - should call option rom in "big real mode".
100     call16(&br);
101
102     debug_serial_setup();
103
104     if (GET_BDA(ebda_seg) != SEG_EBDA)
105         BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
106                  , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
107 }
108
109 // Verify that an option rom looks valid
110 static int
111 is_valid_rom(struct rom_header *rom)
112 {
113     if (rom->signature != OPTION_ROM_SIGNATURE)
114         return 0;
115     u32 len = rom->size * 512;
116     u8 sum = checksum((void*)rom, len);
117     if (sum != 0) {
118         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
119                 , rom, len, sum);
120         return 0;
121     }
122     return 1;
123 }
124
125 // Check if a valid option rom has a pnp struct; return it if so.
126 static struct pnp_data *
127 get_pnp_rom(struct rom_header *rom)
128 {
129     struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
130     if (pnp->signature != *(u32*)pnp_string)
131         return NULL;
132     return pnp;
133 }
134
135 // Check if a valid option rom has a pci struct; return it if so.
136 static struct pci_data *
137 get_pci_rom(struct rom_header *rom)
138 {
139     struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
140     if (pci->signature != PCI_ROM_SIGNATURE)
141         return NULL;
142     return pci;
143 }
144
145 // Add a BEV vector for a given pnp compatible option rom.
146 static void
147 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
148 {
149 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
150
151     // Found a device that thinks it can boot the system.  Record
152     // its BEV and product name string.
153
154     if (! CONFIG_BOOT)
155         return;
156
157     if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
158         return;
159
160     struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
161     ip->type = IPL_TYPE_BEV;
162     ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
163
164     u16 desc = pnp->productname;
165     if (desc)
166         ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
167
168     ebda->ipl.count++;
169 }
170
171 // Copy a rom to its permanent location below 1MiB
172 static struct rom_header *
173 copy_rom(struct rom_header *rom)
174 {
175     u32 romsize = rom->size * 512;
176     if (next_rom + romsize > BUILD_BIOS_ADDR) {
177         // Option rom doesn't fit.
178         dprintf(1, "Option rom %p doesn't fit.\n", rom);
179         return NULL;
180     }
181     dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
182     memcpy((void*)next_rom, rom, romsize);
183     return (struct rom_header *)next_rom;
184 }
185
186 // Check if an option rom is at a hardcoded location for a device.
187 static struct rom_header *
188 lookup_hardcode(u16 bdf)
189 {
190     if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
191         return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
192     else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
193         return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
194     // XXX - check LAR when in coreboot?
195     return NULL;
196 }
197
198 // Map the option rom of a given PCI device.
199 static struct rom_header *
200 map_optionrom(u16 bdf)
201 {
202     dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
203
204     u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
205     if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
206         dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
207         return NULL;
208     }
209
210     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
211     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
212     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
213
214     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
215     orig &= ~PCI_ROM_ADDRESS_ENABLE;
216     if (!sz || sz == 0xffffffff)
217         goto fail;
218
219     if (orig < 16*1024*1024) {
220         dprintf(6, "Preset rom address doesn't look valid\n");
221         goto fail;
222     }
223
224     // Looks like a rom - enable it.
225     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
226
227     u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
228     struct rom_header *rom = (struct rom_header *)orig;
229     for (;;) {
230         dprintf(5, "Inspecting possible rom at %p (vd=%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 vendev (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 // Attempt to map and initialize the option rom on a given PCI device.
265 static struct rom_header *
266 init_optionrom(u16 bdf)
267 {
268     dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
269     struct rom_header *rom = lookup_hardcode(bdf);
270     if (! rom)
271         rom = map_optionrom(bdf);
272     if (! rom)
273         // No ROM present.
274         return NULL;
275
276     if (! is_valid_rom(rom))
277         return NULL;
278
279     if (get_pnp_rom(rom))
280         // Init the PnP rom.
281         callrom(rom, OPTION_ROM_INITVECTOR, bdf);
282
283     next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
284
285     return rom;
286 }
287
288
289 /****************************************************************
290  * Non-VGA option rom init
291  ****************************************************************/
292
293 void
294 optionrom_setup()
295 {
296     if (! CONFIG_OPTIONROMS)
297         return;
298
299     dprintf(1, "Scan for option roms\n");
300
301     u32 post_vga = next_rom;
302
303     if (CONFIG_OPTIONROMS_DEPLOYED) {
304         // Option roms are already deployed on the system.
305         u32 pos = next_rom;
306         while (pos < BUILD_BIOS_ADDR) {
307             struct rom_header *rom = (struct rom_header *)pos;
308             if (! is_valid_rom(rom)) {
309                 pos += OPTION_ROM_ALIGN;
310                 continue;
311             }
312             if (get_pnp_rom(rom))
313                 callrom(rom, OPTION_ROM_INITVECTOR, 0);
314             pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
315             next_rom = pos;
316         }
317     } else {
318         // Find and deploy PCI roms.
319         int bdf, max;
320         foreachpci(bdf, max) {
321             u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
322             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
323                 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
324                 continue;
325             init_optionrom(bdf);
326         }
327     }
328
329     // All option roms found and deployed - now build BEV/BCV vectors.
330
331     u32 pos = post_vga;
332     while (pos < next_rom) {
333         struct rom_header *rom = (struct rom_header *)pos;
334         if (! is_valid_rom(rom)) {
335             pos += OPTION_ROM_ALIGN;
336             continue;
337         }
338         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
339         struct pnp_data *pnp = get_pnp_rom(rom);
340         if (! pnp) {
341             // Legacy rom - run init vector now.
342             callrom(rom, OPTION_ROM_INITVECTOR, 0);
343             continue;
344         }
345         // PnP rom.
346         if (pnp->bev)
347             // Can boot system - add to IPL list.
348             add_ipl(rom, pnp);
349         else if (pnp->bcv)
350             // Has BCV - run it now.
351             callrom(rom, pnp->bcv, 0);
352     }
353 }
354
355
356 /****************************************************************
357  * VGA init
358  ****************************************************************/
359
360 // Call into vga code to turn on console.
361 void
362 vga_setup()
363 {
364     if (! CONFIG_OPTIONROMS)
365         return;
366
367     dprintf(1, "Scan for VGA option rom\n");
368     next_rom = OPTION_ROM_START;
369
370     if (CONFIG_OPTIONROMS_DEPLOYED) {
371         // Option roms are already deployed on the system.
372         struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
373         if (! is_valid_rom(rom))
374             return;
375         callrom(rom, OPTION_ROM_INITVECTOR, 0);
376         next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
377     } else {
378         // Find and deploy PCI VGA rom.
379         int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
380         if (bdf < 0)
381             // Device not found
382             return;
383
384         struct rom_header *rom = init_optionrom(bdf);
385         if (rom && !get_pnp_rom(rom))
386             // Call rom even if it isn't a pnp rom.
387             callrom(rom, OPTION_ROM_INITVECTOR, bdf);
388     }
389
390     dprintf(1, "Turning on vga console\n");
391     struct bregs br;
392     memset(&br, 0, sizeof(br));
393     br.ax = 0x0003;
394     call16_int(0x10, &br);
395
396     // Write to screen.
397     printf("Starting SeaBIOS\n\n");
398 }