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