grml...
[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 #include "paravirt.h" // qemu_cfg_*
17 #include "optionroms.h" // struct rom_header
18
19 /****************************************************************
20  * Definitions
21  ****************************************************************/
22
23 // The end of the last deployed rom.
24 u32 RomEnd = BUILD_ROM_START;
25
26
27 /****************************************************************
28  * Helper functions
29  ****************************************************************/
30
31 // Execute a given option rom.
32 static void
33 __callrom(struct rom_header *rom, u16 offset, u16 bdf)
34 {
35     u16 seg = FLATPTR_TO_SEG(rom);
36     dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
37
38     struct bregs br;
39     memset(&br, 0, sizeof(br));
40     br.flags = F_IF;
41     br.ax = bdf;
42     br.bx = 0xffff;
43     br.dx = 0xffff;
44     br.es = SEG_BIOS;
45     br.di = get_pnp_offset();
46     br.code = SEGOFF(seg, offset);
47     start_preempt();
48     call16big(&br);
49     finish_preempt();
50
51     debug_serial_setup();
52 }
53
54 // Execute a given option rom at the standard entry vector.
55 static void
56 callrom(struct rom_header *rom, u16 bdf)
57 {
58     __callrom(rom, OPTION_ROM_INITVECTOR, bdf);
59 }
60
61 // Execute a BCV option rom registered via add_bcv().
62 void
63 call_bcv(u16 seg, u16 ip)
64 {
65     __callrom(MAKE_FLATPTR(seg, 0), ip, 0);
66 }
67
68 static int EnforceChecksum;
69
70 // Verify that an option rom looks valid
71 static int
72 is_valid_rom(struct rom_header *rom)
73 {
74     dprintf(6, "Checking rom %p (sig %x size %d)\n"
75             , rom, rom->signature, rom->size);
76     if (rom->signature != OPTION_ROM_SIGNATURE)
77         return 0;
78     if (! rom->size)
79         return 0;
80     u32 len = rom->size * 512;
81     u8 sum = checksum(rom, len);
82     if (sum != 0) {
83         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
84                 , rom, len, sum);
85         if (EnforceChecksum)
86             return 0;
87     }
88     return 1;
89 }
90
91 // Check if a valid option rom has a pnp struct; return it if so.
92 static struct pnp_data *
93 get_pnp_rom(struct rom_header *rom)
94 {
95     struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset);
96     if (pnp->signature != PNP_SIGNATURE)
97         return NULL;
98     return pnp;
99 }
100
101 // Check for multiple pnp option rom headers.
102 static struct pnp_data *
103 get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
104 {
105     if (! pnp->nextoffset)
106         return NULL;
107     pnp = (void*)((u8*)rom + pnp->nextoffset);
108     if (pnp->signature != PNP_SIGNATURE)
109         return NULL;
110     return pnp;
111 }
112
113 // Check if a valid option rom has a pci struct; return it if so.
114 static struct pci_data *
115 get_pci_rom(struct rom_header *rom)
116 {
117     struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
118     if (pd->signature != PCI_ROM_SIGNATURE)
119         return NULL;
120     return pd;
121 }
122
123 // Return start of code in 0xc0000-0xf0000 space.
124 static inline u32 _max_rom(void) {
125     extern u8 code32flat_start[], code32init_end[];
126     return CONFIG_RELOCATE_INIT ? (u32)code32init_end : (u32)code32flat_start;
127 }
128 // Return the memory position up to which roms may be located.
129 static inline u32 max_rom(void) {
130     u32 end = _max_rom();
131     return end > BUILD_BIOS_ADDR ? BUILD_BIOS_ADDR : end;
132 }
133
134 // Copy a rom to its permanent location below 1MiB
135 static struct rom_header *
136 copy_rom(struct rom_header *rom)
137 {
138     u32 romsize = rom->size * 512;
139     if (RomEnd + romsize > max_rom()) {
140         // Option rom doesn't fit.
141         warn_noalloc();
142         return NULL;
143     }
144     dprintf(4, "Copying option rom (size %d) from %p to %x\n"
145             , romsize, rom, RomEnd);
146     iomemcpy((void*)RomEnd, rom, romsize);
147     return (void*)RomEnd;
148 }
149
150 // Run rom init code and note rom size.
151 static int
152 init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
153 {
154     if (! is_valid_rom(rom))
155         return -1;
156
157     if (isvga || get_pnp_rom(rom))
158         // Only init vga and PnP roms here.
159         callrom(rom, bdf);
160
161     RomEnd = (u32)rom + ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
162
163     return 0;
164 }
165
166 #define RS_PCIROM (1LL<<33)
167
168 static void
169 setRomSource(u64 *sources, struct rom_header *rom, u64 source)
170 {
171     if (sources)
172         sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
173 }
174
175 static int
176 getRomPriority(u64 *sources, struct rom_header *rom, int instance)
177 {
178     u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
179     if (!source)
180         return -1;
181     if (source & RS_PCIROM)
182         return bootprio_find_pci_rom((void*)(u32)source, instance);
183     return bootprio_find_named_rom(romfile_name(source), instance);
184 }
185
186
187 /****************************************************************
188  * Roms in CBFS
189  ****************************************************************/
190
191 // Check if an option rom is at a hardcoded location or in CBFS.
192 static struct rom_header *
193 lookup_hardcode(struct pci_device *pci)
194 {
195     char fname[17];
196     snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
197              , pci->vendor, pci->device);
198     int ret = romfile_copy(romfile_find(fname), (void*)RomEnd
199                            , max_rom() - RomEnd);
200     if (ret <= 0)
201         return NULL;
202     return (void*)RomEnd;
203 }
204
205 // Run all roms in a given CBFS directory.
206 static void
207 run_file_roms(const char *prefix, int isvga, u64 *sources)
208 {
209     u32 file = 0;
210     for (;;) {
211         file = romfile_findprefix(prefix, file);
212         if (!file)
213             break;
214         struct rom_header *rom = (void*)RomEnd;
215         int ret = romfile_copy(file, rom, max_rom() - RomEnd);
216         if (ret > 0) {
217             setRomSource(sources, rom, file);
218             init_optionrom(rom, 0, isvga);
219         }
220     }
221 }
222
223
224 /****************************************************************
225  * PCI roms
226  ****************************************************************/
227
228 // Verify device is a vga device with legacy address decoding enabled.
229 static int
230 is_pci_vga(struct pci_device *pci)
231 {
232     if (pci->class != PCI_CLASS_DISPLAY_VGA)
233         return 0;
234     u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
235     if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
236         return 0;
237     while (pci->parent) {
238         pci = pci->parent;
239         u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
240         if (!(ctrl & PCI_BRIDGE_CTL_VGA))
241             return 0;
242     }
243     return 1;
244 }
245
246 // Map the option rom of a given PCI device.
247 static struct rom_header *
248 map_pcirom(struct pci_device *pci)
249 {
250     u16 bdf = pci->bdf;
251     dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
252             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
253
254     if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
255         dprintf(6, "Skipping non-normal pci device (type=%x)\n"
256                 , pci->header_type);
257         return NULL;
258     }
259
260     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
261     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
262     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
263
264     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
265     orig &= ~PCI_ROM_ADDRESS_ENABLE;
266     if (!sz || sz == 0xffffffff)
267         goto fail;
268
269     if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
270         // Don't try to map to a pci addresses at its max, in the last
271         // 4MiB of ram, or the first 16MiB of ram.
272         dprintf(6, "Preset rom address doesn't look valid\n");
273         goto fail;
274     }
275
276     // Looks like a rom - enable it.
277     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
278
279     struct rom_header *rom = (void*)orig;
280     for (;;) {
281         dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
282                 " bdf=%02x:%02x.%x)\n"
283                 , rom, pci->vendor, pci->device
284                 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
285         if (rom->signature != OPTION_ROM_SIGNATURE) {
286             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
287             goto fail;
288         }
289         struct pci_data *pd = get_pci_rom(rom);
290         if (! pd) {
291             dprintf(6, "No valid pci signature found\n");
292             goto fail;
293         }
294
295         if (pd->vendor == pci->vendor && pd->device == pci->device
296             && pd->type == PCIROM_CODETYPE_X86)
297             // A match
298             break;
299         dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
300                 , pd->vendor, pd->device, pd->type);
301         if (pd->indicator & 0x80) {
302             dprintf(6, "No more images left\n");
303             goto fail;
304         }
305         rom = (void*)((u32)rom + pd->ilen * 512);
306     }
307
308     rom = copy_rom(rom);
309     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
310     return rom;
311 fail:
312     // Not valid - restore original and exit.
313     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
314     return NULL;
315 }
316
317 // Attempt to map and initialize the option rom on a given PCI device.
318 static int
319 init_pcirom(struct pci_device *pci, int isvga, u64 *sources)
320 {
321     u16 bdf = pci->bdf;
322     dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
323             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
324             , pci->vendor, pci->device);
325     struct rom_header *rom = lookup_hardcode(pci);
326     if (! rom)
327         rom = map_pcirom(pci);
328     if (! rom)
329         // No ROM present.
330         return -1;
331     setRomSource(sources, rom, RS_PCIROM | (u32)pci);
332     return init_optionrom(rom, bdf, isvga);
333 }
334
335
336 /****************************************************************
337  * Non-VGA option rom init
338  ****************************************************************/
339
340 void
341 optionrom_setup(void)
342 {
343     if (! CONFIG_OPTIONROMS)
344         return;
345
346     dprintf(1, "Scan for option roms\n");
347     u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
348     memset(sources, 0, sizeof(sources));
349     u32 post_vga = RomEnd;
350
351     if (CONFIG_OPTIONROMS_DEPLOYED) {
352         // Option roms are already deployed on the system.
353         u32 pos = RomEnd;
354         while (pos < max_rom()) {
355             int ret = init_optionrom((void*)pos, 0, 0);
356             if (ret)
357                 pos += OPTION_ROM_ALIGN;
358             else
359                 pos = RomEnd;
360         }
361     } else {
362         // Find and deploy PCI roms.
363         struct pci_device *pci;
364         foreachpci(pci) {
365             if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver)
366                 continue;
367             init_pcirom(pci, 0, sources);
368         }
369
370         // Find and deploy CBFS roms not associated with a device.
371         run_file_roms("genroms/", 0, sources);
372     }
373
374     // All option roms found and deployed - now build BEV/BCV vectors.
375
376     u32 pos = post_vga;
377     while (pos < RomEnd) {
378         struct rom_header *rom = (void*)pos;
379         if (! is_valid_rom(rom)) {
380             pos += OPTION_ROM_ALIGN;
381             continue;
382         }
383         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
384         struct pnp_data *pnp = get_pnp_rom(rom);
385         if (! pnp) {
386             // Legacy rom.
387             boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
388                          , getRomPriority(sources, rom, 0));
389             continue;
390         }
391         // PnP rom - check for BEV and BCV boot capabilities.
392         int instance = 0;
393         while (pnp) {
394             if (pnp->bev)
395                 boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
396                              , getRomPriority(sources, rom, instance++));
397             else if (pnp->bcv)
398                 boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
399                              , getRomPriority(sources, rom, instance++));
400             else
401                 break;
402             pnp = get_pnp_next(rom, pnp);
403         }
404     }
405 }
406
407
408 /****************************************************************
409  * VGA init
410  ****************************************************************/
411
412 static int S3ResumeVgaInit;
413 int ScreenAndDebug;
414
415 // Call into vga code to turn on console.
416 void
417 vga_setup(void)
418 {
419     if (! CONFIG_OPTIONROMS)
420         return;
421
422     dprintf(1, "Scan for VGA option rom\n");
423
424     // Load some config settings that impact VGA.
425     EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
426     S3ResumeVgaInit = romfile_loadint("etc/s3-resume-vga-init", 0);
427     ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
428
429     if (CONFIG_OPTIONROMS_DEPLOYED) {
430         // Option roms are already deployed on the system.
431         init_optionrom((void*)BUILD_ROM_START, 0, 1);
432     } else {
433         // Clear option rom memory
434         memset((void*)RomEnd, 0, max_rom() - RomEnd);
435
436         // Find and deploy PCI VGA rom.
437         struct pci_device *pci;
438         foreachpci(pci) {
439             if (!is_pci_vga(pci))
440                 continue;
441             vgahook_setup(pci);
442             init_pcirom(pci, 1, NULL);
443             break;
444         }
445
446         // Find and deploy CBFS vga-style roms not associated with a device.
447         run_file_roms("vgaroms/", 1, NULL);
448     }
449
450     if (RomEnd == BUILD_ROM_START) {
451         // No VGA rom found
452         RomEnd += OPTION_ROM_ALIGN;
453         return;
454     }
455
456     enable_vga_console();
457 }
458
459 void
460 s3_resume_vga_init(void)
461 {
462     if (!S3ResumeVgaInit)
463         return;
464     struct rom_header *rom = (void*)BUILD_ROM_START;
465     if (! is_valid_rom(rom))
466         return;
467     callrom(rom, 0);
468 }