Change license from GPLv3 to LGPLv3.
[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" // FARPTR_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 = FARPTR_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 // Verify that an option rom looks valid
104 static int
105 is_valid_rom(struct rom_header *rom)
106 {
107     if (rom->signature != OPTION_ROM_SIGNATURE)
108         return 0;
109     u32 len = rom->size * 512;
110     u8 sum = checksum((void*)rom, len);
111     if (sum != 0) {
112         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
113                 , rom, len, sum);
114         return 0;
115     }
116     return 1;
117 }
118
119 // Check if a valid option rom has a pnp struct; return it if so.
120 static struct pnp_data *
121 get_pnp_rom(struct rom_header *rom)
122 {
123     struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
124     if (pnp->signature != PNP_SIGNATURE)
125         return NULL;
126     return pnp;
127 }
128
129 // Check for multiple pnp option rom headers.
130 static struct pnp_data *
131 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
132 {
133     if (! pnp->nextoffset)
134         return NULL;
135     pnp = (struct pnp_data *)((u8*)rom + pnp->nextoffset);
136     if (pnp->signature != PNP_SIGNATURE)
137         return NULL;
138     return pnp;
139 }
140
141 // Check if a valid option rom has a pci struct; return it if so.
142 static struct pci_data *
143 get_pci_rom(struct rom_header *rom)
144 {
145     struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
146     if (pci->signature != PCI_ROM_SIGNATURE)
147         return NULL;
148     return pci;
149 }
150
151 // Add a BEV vector for a given pnp compatible option rom.
152 static void
153 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
154 {
155     // Found a device that thinks it can boot the system.  Record
156     // its BEV and product name string.
157
158     if (! CONFIG_BOOT)
159         return;
160
161     if (IPL.count >= ARRAY_SIZE(IPL.table))
162         return;
163
164     struct ipl_entry_s *ip = &IPL.table[IPL.count];
165     ip->type = IPL_TYPE_BEV;
166     ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
167
168     u16 desc = pnp->productname;
169     if (desc)
170         ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
171
172     IPL.count++;
173 }
174
175 // Copy a rom to its permanent location below 1MiB
176 static struct rom_header *
177 copy_rom(struct rom_header *rom)
178 {
179     u32 romsize = rom->size * 512;
180     if (next_rom + romsize > BUILD_BIOS_ADDR) {
181         // Option rom doesn't fit.
182         dprintf(1, "Option rom %p doesn't fit.\n", rom);
183         return NULL;
184     }
185     dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
186     memcpy((void*)next_rom, rom, romsize);
187     return (struct rom_header *)next_rom;
188 }
189
190 // Check if an option rom is at a hardcoded location for a device.
191 static struct rom_header *
192 lookup_hardcode(u16 bdf)
193 {
194     if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
195         return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
196     else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
197         return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
198     // XXX - check LAR when in coreboot?
199     return NULL;
200 }
201
202 // Map the option rom of a given PCI device.
203 static struct rom_header *
204 map_optionrom(u16 bdf)
205 {
206     dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
207
208     u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
209     if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
210         dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
211         return NULL;
212     }
213
214     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
215     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
216     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
217
218     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
219     orig &= ~PCI_ROM_ADDRESS_ENABLE;
220     if (!sz || sz == 0xffffffff)
221         goto fail;
222
223     if (orig < 16*1024*1024) {
224         dprintf(6, "Preset rom address doesn't look valid\n");
225         goto fail;
226     }
227
228     // Looks like a rom - enable it.
229     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
230
231     u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
232     struct rom_header *rom = (struct rom_header *)orig;
233     for (;;) {
234         dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
235                 , rom, vendev, bdf);
236         if (rom->signature != OPTION_ROM_SIGNATURE) {
237             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
238             goto fail;
239         }
240         struct pci_data *pci = get_pci_rom(rom);
241         if (! pci) {
242             dprintf(6, "No valid pci signature found\n");
243             goto fail;
244         }
245
246         u32 vd = (pci->device << 16) | pci->vendor;
247         if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
248             // A match
249             break;
250         dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
251                 , vd, pci->type);
252         if (pci->indicator & 0x80) {
253             dprintf(6, "No more images left\n");
254             goto fail;
255         }
256         rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
257     }
258
259     rom = copy_rom(rom);
260     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
261     return rom;
262 fail:
263     // Not valid - restore original and exit.
264     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
265     return NULL;
266 }
267
268 // Attempt to map and initialize the option rom on a given PCI device.
269 static struct rom_header *
270 init_optionrom(u16 bdf)
271 {
272     dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
273     struct rom_header *rom = lookup_hardcode(bdf);
274     if (! rom)
275         rom = map_optionrom(bdf);
276     if (! rom)
277         // No ROM present.
278         return NULL;
279
280     if (! is_valid_rom(rom))
281         return NULL;
282
283     if (get_pnp_rom(rom))
284         // Init the PnP rom.
285         callrom(rom, OPTION_ROM_INITVECTOR, bdf);
286
287     next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
288
289     return rom;
290 }
291
292
293 /****************************************************************
294  * Non-VGA option rom init
295  ****************************************************************/
296
297 void
298 optionrom_setup()
299 {
300     if (! CONFIG_OPTIONROMS)
301         return;
302
303     dprintf(1, "Scan for option roms\n");
304
305     u32 post_vga = next_rom;
306
307     if (CONFIG_OPTIONROMS_DEPLOYED) {
308         // Option roms are already deployed on the system.
309         u32 pos = next_rom;
310         while (pos < BUILD_BIOS_ADDR) {
311             struct rom_header *rom = (struct rom_header *)pos;
312             if (! is_valid_rom(rom)) {
313                 pos += OPTION_ROM_ALIGN;
314                 continue;
315             }
316             if (get_pnp_rom(rom))
317                 callrom(rom, OPTION_ROM_INITVECTOR, 0);
318             pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
319             next_rom = pos;
320         }
321     } else {
322         // Find and deploy PCI roms.
323         int bdf, max;
324         foreachpci(bdf, max) {
325             u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
326             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
327                 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
328                 continue;
329             init_optionrom(bdf);
330         }
331     }
332
333     // All option roms found and deployed - now build BEV/BCV vectors.
334
335     u32 pos = post_vga;
336     while (pos < next_rom) {
337         struct rom_header *rom = (struct rom_header *)pos;
338         if (! is_valid_rom(rom)) {
339             pos += OPTION_ROM_ALIGN;
340             continue;
341         }
342         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
343         struct pnp_data *pnp = get_pnp_rom(rom);
344         if (! pnp) {
345             // Legacy rom - run init vector now.
346             callrom(rom, OPTION_ROM_INITVECTOR, 0);
347             continue;
348         }
349         // PnP rom.
350         if (pnp->bev)
351             // Can boot system - add to IPL list.
352             add_ipl(rom, pnp);
353         else
354             // Check for BCV (there may be multiple).
355             while (pnp && pnp->bcv) {
356                 // Has BCV - run it now.
357                 callrom(rom, pnp->bcv, 0);
358                 pnp = get_pnp_next(rom, pnp);
359             }
360     }
361 }
362
363
364 /****************************************************************
365  * VGA init
366  ****************************************************************/
367
368 // Call into vga code to turn on console.
369 void
370 vga_setup()
371 {
372     if (! CONFIG_OPTIONROMS)
373         return;
374
375     dprintf(1, "Scan for VGA option rom\n");
376     next_rom = OPTION_ROM_START;
377
378     if (CONFIG_OPTIONROMS_DEPLOYED) {
379         // Option roms are already deployed on the system.
380         struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
381         if (! is_valid_rom(rom))
382             return;
383         callrom(rom, OPTION_ROM_INITVECTOR, 0);
384         next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
385     } else {
386         // Find and deploy PCI VGA rom.
387         int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
388         if (bdf < 0)
389             // Device not found
390             return;
391
392         struct rom_header *rom = init_optionrom(bdf);
393         if (rom && !get_pnp_rom(rom))
394             // Call rom even if it isn't a pnp rom.
395             callrom(rom, OPTION_ROM_INITVECTOR, bdf);
396     }
397
398     dprintf(1, "Turning on vga console\n");
399     struct bregs br;
400     memset(&br, 0, sizeof(br));
401     br.ax = 0x0003;
402     call16_int(0x10, &br);
403
404     // Write to screen.
405     printf("Starting SeaBIOS\n\n");
406 }