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