Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 1 | // Option rom scanning code. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2002 MandrakeSoft S.A. |
| 5 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 7 | |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 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 |
Kevin O'Connor | 9dea590 | 2013-09-14 20:23:54 -0400 | [diff] [blame] | 14 | #include "malloc.h" // rom_confirm |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 15 | #include "output.h" // dprintf |
Kevin O'Connor | 41639f8 | 2013-09-14 19:37:36 -0400 | [diff] [blame] | 16 | #include "romfile.h" // romfile_loadint |
Kevin O'Connor | 3df600b | 2013-09-14 19:28:55 -0400 | [diff] [blame] | 17 | #include "stacks.h" // farcall16big |
Kevin O'Connor | 8fb3a5e | 2013-09-14 22:27:14 -0400 | [diff] [blame] | 18 | #include "std/optionrom.h" // struct rom_header |
Kevin O'Connor | 4f790aa | 2013-09-14 23:04:08 -0400 | [diff] [blame] | 19 | #include "std/pnpbios.h" // PNP_SIGNATURE |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 20 | #include "string.h" // memset |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 21 | #include "util.h" // get_pnp_offset |
Stefan Berger | 2aff1c1 | 2015-05-26 15:48:33 -0400 | [diff] [blame] | 22 | #include "tcgbios.h" // tpm_* |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 23 | |
tpearson@raptorengineeringinc.com | d23eba6 | 2015-02-17 15:04:17 -0600 | [diff] [blame] | 24 | static int EnforceChecksum, S3ResumeVga, RunPCIroms; |
| 25 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 26 | |
| 27 | /**************************************************************** |
| 28 | * Helper functions |
| 29 | ****************************************************************/ |
| 30 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 31 | // Execute a given option rom. |
| 32 | static void |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 33 | __callrom(struct rom_header *rom, u16 offset, u16 bdf) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 34 | { |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 35 | u16 seg = FLATPTR_TO_SEG(rom); |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 36 | dprintf(1, "Running option rom at %04x:%04x\n", seg, offset); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 37 | |
| 38 | struct bregs br; |
| 39 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | f8e800d | 2009-09-24 21:01:16 -0400 | [diff] [blame] | 40 | br.flags = F_IF; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 41 | br.ax = bdf; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 42 | br.bx = 0xffff; |
| 43 | br.dx = 0xffff; |
| 44 | br.es = SEG_BIOS; |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 45 | br.di = get_pnp_offset(); |
Kevin O'Connor | 9f98542 | 2009-09-09 11:34:39 -0400 | [diff] [blame] | 46 | br.code = SEGOFF(seg, offset); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 47 | start_preempt(); |
Kevin O'Connor | c7ffbac | 2012-03-25 11:04:10 -0400 | [diff] [blame] | 48 | farcall16big(&br); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 49 | finish_preempt(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 52 | // Execute a given option rom at the standard entry vector. |
David Woodhouse | 5c8dd96 | 2013-01-25 19:31:47 -0600 | [diff] [blame] | 53 | void |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 54 | callrom(struct rom_header *rom, u16 bdf) |
| 55 | { |
| 56 | __callrom(rom, OPTION_ROM_INITVECTOR, bdf); |
| 57 | } |
| 58 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 59 | // Execute a BCV option rom registered via add_bcv(). |
| 60 | void |
| 61 | call_bcv(u16 seg, u16 ip) |
| 62 | { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 63 | __callrom(MAKE_FLATPTR(seg, 0), ip, 0); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 66 | // Verify that an option rom looks valid |
| 67 | static int |
| 68 | is_valid_rom(struct rom_header *rom) |
| 69 | { |
Kevin O'Connor | c36273f | 2009-04-16 20:43:07 -0400 | [diff] [blame] | 70 | dprintf(6, "Checking rom %p (sig %x size %d)\n" |
| 71 | , rom, rom->signature, rom->size); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 72 | if (rom->signature != OPTION_ROM_SIGNATURE) |
| 73 | return 0; |
Kevin O'Connor | 674e460 | 2009-04-13 19:19:22 -0400 | [diff] [blame] | 74 | if (! rom->size) |
| 75 | return 0; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 76 | u32 len = rom->size * 512; |
Kevin O'Connor | 94fd47e | 2009-02-15 15:21:10 -0500 | [diff] [blame] | 77 | u8 sum = checksum(rom, len); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 78 | if (sum != 0) { |
| 79 | dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n" |
| 80 | , rom, len, sum); |
Kevin O'Connor | e010d85 | 2011-07-05 20:47:35 -0400 | [diff] [blame] | 81 | if (EnforceChecksum) |
Kevin O'Connor | cc97564 | 2011-03-06 19:22:46 -0500 | [diff] [blame] | 82 | return 0; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 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 | { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 91 | struct pnp_data *pnp = (void*)((u8*)rom + rom->pnpoffset); |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 92 | if (pnp->signature != PNP_SIGNATURE) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 93 | return NULL; |
| 94 | return pnp; |
| 95 | } |
| 96 | |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 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; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 103 | pnp = (void*)((u8*)rom + pnp->nextoffset); |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 104 | if (pnp->signature != PNP_SIGNATURE) |
Kevin O'Connor | 3f57b5c | 2008-12-20 23:32:16 -0500 | [diff] [blame] | 105 | return NULL; |
| 106 | return pnp; |
| 107 | } |
| 108 | |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 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 | { |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 113 | struct pci_data *pd = (void*)((u32)rom + rom->pcioffset); |
| 114 | if (pd->signature != PCI_ROM_SIGNATURE) |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 115 | return NULL; |
Kevin O'Connor | 97dce0f | 2013-02-15 22:46:09 -0500 | [diff] [blame] | 116 | if (rom->pcioffset & 3) |
| 117 | dprintf(1, "WARNING! Found unaligned PCI rom (vd=%04x:%04x)\n" |
| 118 | , pd->vendor, pd->device); |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 119 | return pd; |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 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; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 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); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 135 | |
Stefan Berger | 5deeec1 | 2015-06-09 19:56:33 -0400 | [diff] [blame] | 136 | tpm_option_rom(newrom, rom->size * 512); |
| 137 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 138 | if (isvga || get_pnp_rom(newrom)) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 139 | // Only init vga and PnP roms here. |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 140 | callrom(newrom, bdf); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 141 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 142 | return rom_confirm(newrom->size * 512); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 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 | |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 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) |
Kevin O'Connor | fce9189 | 2011-07-09 14:47:47 -0400 | [diff] [blame] | 161 | return bootprio_find_pci_rom((void*)(u32)source, instance); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 162 | struct romfile_s *file = (void*)(u32)source; |
| 163 | return bootprio_find_named_rom(file->name, instance); |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 164 | } |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 165 | |
Kevin O'Connor | c1de91b | 2011-07-02 13:50:21 -0400 | [diff] [blame] | 166 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 167 | /**************************************************************** |
| 168 | * Roms in CBFS |
| 169 | ****************************************************************/ |
| 170 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 171 | static struct rom_header * |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 172 | deploy_romfile(struct romfile_s *file) |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 173 | { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 174 | u32 size = file->size; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 175 | struct rom_header *rom = rom_reserve(size); |
| 176 | if (!rom) { |
| 177 | warn_noalloc(); |
| 178 | return NULL; |
| 179 | } |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 180 | int ret = file->copy(file, rom, size); |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 181 | if (ret <= 0) |
| 182 | return NULL; |
| 183 | return rom; |
| 184 | } |
| 185 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 186 | // Run all roms in a given CBFS directory. |
| 187 | static void |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 188 | run_file_roms(const char *prefix, int isvga, u64 *sources) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 189 | { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 190 | struct romfile_s *file = NULL; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 191 | for (;;) { |
Kevin O'Connor | e230426 | 2010-06-13 16:05:17 -0400 | [diff] [blame] | 192 | file = romfile_findprefix(prefix, file); |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 193 | if (!file) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 194 | break; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 195 | struct rom_header *rom = deploy_romfile(file); |
| 196 | if (rom) { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 197 | setRomSource(sources, rom, (u32)file); |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 198 | init_optionrom(rom, 0, isvga); |
| 199 | } |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /**************************************************************** |
| 205 | * PCI roms |
| 206 | ****************************************************************/ |
| 207 | |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 208 | // Verify device is a vga device with legacy address decoding enabled. |
Alex Williamson | 7adfd71 | 2013-03-20 10:58:47 -0600 | [diff] [blame] | 209 | int |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 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 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 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 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 242 | // Map the option rom of a given PCI device. |
| 243 | static struct rom_header * |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 244 | map_pcirom(struct pci_device *pci) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 245 | { |
Kevin O'Connor | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 246 | dprintf(6, "Attempting to map option rom on dev %pP\n", pci); |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 247 | |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 248 | if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) { |
| 249 | dprintf(6, "Skipping non-normal pci device (type=%x)\n" |
| 250 | , pci->header_type); |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 251 | return NULL; |
| 252 | } |
| 253 | |
Kevin O'Connor | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 254 | u16 bdf = pci->bdf; |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 255 | u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS); |
| 256 | pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE); |
| 257 | u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 258 | |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 259 | dprintf(6, "Option rom sizing returned %x %x\n", orig, sz); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 260 | orig &= ~PCI_ROM_ADDRESS_ENABLE; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 261 | if (!sz || sz == 0xffffffff) |
| 262 | goto fail; |
| 263 | |
Kevin O'Connor | 64cf2fb | 2009-04-18 17:01:02 -0400 | [diff] [blame] | 264 | if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) { |
| 265 | // Don't try to map to a pci addresses at its max, in the last |
| 266 | // 4MiB of ram, or the first 16MiB of ram. |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 267 | dprintf(6, "Preset rom address doesn't look valid\n"); |
| 268 | goto fail; |
| 269 | } |
| 270 | |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 271 | // Looks like a rom - enable it. |
| 272 | pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 273 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 274 | struct rom_header *rom = (void*)orig; |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 275 | for (;;) { |
Kevin O'Connor | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 276 | dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x bdf=%pP)\n" |
| 277 | , rom, pci->vendor, pci->device, pci); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 278 | if (rom->signature != OPTION_ROM_SIGNATURE) { |
| 279 | dprintf(6, "No option rom signature (got %x)\n", rom->signature); |
| 280 | goto fail; |
| 281 | } |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 282 | struct pci_data *pd = get_pci_rom(rom); |
| 283 | if (! pd) { |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 284 | dprintf(6, "No valid pci signature found\n"); |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 285 | goto fail; |
| 286 | } |
Kevin O'Connor | 62eea67 | 2008-12-06 11:57:45 -0500 | [diff] [blame] | 287 | |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 288 | if (pd->vendor == pci->vendor && pd->device == pci->device |
| 289 | && pd->type == PCIROM_CODETYPE_X86) |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 290 | // A match |
| 291 | break; |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 292 | dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n" |
| 293 | , pd->vendor, pd->device, pd->type); |
| 294 | if (pd->indicator & 0x80) { |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 295 | dprintf(6, "No more images left\n"); |
| 296 | goto fail; |
| 297 | } |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 298 | rom = (void*)((u32)rom + pd->ilen * 512); |
Kevin O'Connor | e5a36d5 | 2008-11-12 20:10:13 -0500 | [diff] [blame] | 299 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 300 | |
Kevin O'Connor | 5a1b828 | 2008-11-29 20:39:06 -0500 | [diff] [blame] | 301 | rom = copy_rom(rom); |
| 302 | pci_config_writel(bdf, PCI_ROM_ADDRESS, orig); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 303 | return rom; |
| 304 | fail: |
| 305 | // Not valid - restore original and exit. |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 306 | pci_config_writel(bdf, PCI_ROM_ADDRESS, orig); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | // Attempt to map and initialize the option rom on a given PCI device. |
Kevin O'Connor | eeca36c | 2015-08-10 15:37:13 -0400 | [diff] [blame] | 311 | static void |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 312 | init_pcirom(struct pci_device *pci, int isvga, u64 *sources) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 313 | { |
Kevin O'Connor | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 314 | dprintf(4, "Attempting to init PCI bdf %pP (vd %04x:%04x)\n" |
| 315 | , pci, pci->vendor, pci->device); |
Kevin O'Connor | eeca36c | 2015-08-10 15:37:13 -0400 | [diff] [blame] | 316 | |
| 317 | char fname[17]; |
| 318 | snprintf(fname, sizeof(fname), "pci%04x,%04x.rom" |
| 319 | , pci->vendor, pci->device); |
| 320 | struct romfile_s *file = romfile_find(fname); |
| 321 | struct rom_header *rom = NULL; |
| 322 | if (file) |
| 323 | rom = deploy_romfile(file); |
| 324 | else if (RunPCIroms > 1 || (RunPCIroms == 1 && isvga)) |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 325 | rom = map_pcirom(pci); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 326 | if (! rom) |
| 327 | // No ROM present. |
Kevin O'Connor | eeca36c | 2015-08-10 15:37:13 -0400 | [diff] [blame] | 328 | return; |
Kevin O'Connor | fce9189 | 2011-07-09 14:47:47 -0400 | [diff] [blame] | 329 | setRomSource(sources, rom, RS_PCIROM | (u32)pci); |
Kevin O'Connor | 7b67300 | 2016-02-03 03:03:15 -0500 | [diff] [blame] | 330 | init_optionrom(rom, pci->bdf, isvga); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | |
| 334 | /**************************************************************** |
| 335 | * Non-VGA option rom init |
| 336 | ****************************************************************/ |
| 337 | |
| 338 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 339 | optionrom_setup(void) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 340 | { |
| 341 | if (! CONFIG_OPTIONROMS) |
| 342 | return; |
| 343 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 344 | dprintf(1, "Scan for option roms\n"); |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 345 | u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN]; |
| 346 | memset(sources, 0, sizeof(sources)); |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 347 | u32 post_vga = rom_get_last(); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 348 | |
| 349 | if (CONFIG_OPTIONROMS_DEPLOYED) { |
| 350 | // Option roms are already deployed on the system. |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 351 | u32 pos = post_vga; |
Kevin O'Connor | 3733f6f | 2013-02-17 12:44:23 -0500 | [diff] [blame] | 352 | while (pos < rom_get_max()) { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 353 | int ret = init_optionrom((void*)pos, 0, 0); |
| 354 | if (ret) |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 355 | pos += OPTION_ROM_ALIGN; |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 356 | else |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 357 | pos = rom_get_last(); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 358 | } |
| 359 | } else { |
| 360 | // Find and deploy PCI roms. |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 361 | struct pci_device *pci; |
| 362 | foreachpci(pci) { |
Kevin O'Connor | 76b5e71 | 2011-06-21 22:52:51 -0400 | [diff] [blame] | 363 | if (pci->class == PCI_CLASS_DISPLAY_VGA || pci->have_driver) |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 364 | continue; |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 365 | init_pcirom(pci, 0, sources); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 366 | } |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 367 | |
| 368 | // Find and deploy CBFS roms not associated with a device. |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 369 | run_file_roms("genroms/", 0, sources); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 370 | } |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 371 | rom_reserve(0); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 372 | |
| 373 | // All option roms found and deployed - now build BEV/BCV vectors. |
| 374 | |
| 375 | u32 pos = post_vga; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 376 | while (pos < rom_get_last()) { |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 377 | struct rom_header *rom = (void*)pos; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 378 | if (! is_valid_rom(rom)) { |
| 379 | pos += OPTION_ROM_ALIGN; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 380 | continue; |
| 381 | } |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 382 | pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN); |
| 383 | struct pnp_data *pnp = get_pnp_rom(rom); |
| 384 | if (! pnp) { |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 385 | // Legacy rom. |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 386 | boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0 |
| 387 | , getRomPriority(sources, rom, 0)); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 388 | continue; |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 389 | } |
Kevin O'Connor | 1e15725 | 2012-01-14 11:55:35 -0500 | [diff] [blame] | 390 | // PnP rom - check for BEV and BCV boot capabilities. |
| 391 | int instance = 0; |
| 392 | while (pnp) { |
| 393 | if (pnp->bev) |
| 394 | boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname |
| 395 | , getRomPriority(sources, rom, instance++)); |
| 396 | else if (pnp->bcv) |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 397 | boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname |
| 398 | , getRomPriority(sources, rom, instance++)); |
Kevin O'Connor | 1e15725 | 2012-01-14 11:55:35 -0500 | [diff] [blame] | 399 | else |
| 400 | break; |
| 401 | pnp = get_pnp_next(rom, pnp); |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 402 | } |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 406 | |
| 407 | /**************************************************************** |
| 408 | * VGA init |
| 409 | ****************************************************************/ |
| 410 | |
Kevin O'Connor | 422263d | 2011-07-05 20:56:07 -0400 | [diff] [blame] | 411 | int ScreenAndDebug; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 412 | struct rom_header *VgaROM; |
Kevin O'Connor | 8b0c509 | 2011-07-05 20:50:32 -0400 | [diff] [blame] | 413 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 414 | // Call into vga code to turn on console. |
| 415 | void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 416 | vgarom_setup(void) |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 417 | { |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 418 | if (! CONFIG_OPTIONROMS) |
| 419 | return; |
| 420 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 421 | dprintf(1, "Scan for VGA option rom\n"); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 422 | |
Kevin O'Connor | 422263d | 2011-07-05 20:56:07 -0400 | [diff] [blame] | 423 | // Load some config settings that impact VGA. |
Kevin O'Connor | e010d85 | 2011-07-05 20:47:35 -0400 | [diff] [blame] | 424 | EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1); |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 425 | S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", CONFIG_QEMU); |
tpearson@raptorengineeringinc.com | d23eba6 | 2015-02-17 15:04:17 -0600 | [diff] [blame] | 426 | RunPCIroms = romfile_loadint("etc/pci-optionrom-exec", 2); |
Kevin O'Connor | 422263d | 2011-07-05 20:56:07 -0400 | [diff] [blame] | 427 | ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1); |
Kevin O'Connor | e010d85 | 2011-07-05 20:47:35 -0400 | [diff] [blame] | 428 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 429 | if (CONFIG_OPTIONROMS_DEPLOYED) { |
| 430 | // Option roms are already deployed on the system. |
Kevin O'Connor | e773930 | 2009-07-26 19:16:09 -0400 | [diff] [blame] | 431 | init_optionrom((void*)BUILD_ROM_START, 0, 1); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 432 | } else { |
Kevin O'Connor | e826465 | 2010-09-15 22:06:19 -0400 | [diff] [blame] | 433 | // Clear option rom memory |
Kevin O'Connor | 3733f6f | 2013-02-17 12:44:23 -0500 | [diff] [blame] | 434 | memset((void*)BUILD_ROM_START, 0, rom_get_max() - BUILD_ROM_START); |
Kevin O'Connor | e826465 | 2010-09-15 22:06:19 -0400 | [diff] [blame] | 435 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 436 | // Find and deploy PCI VGA rom. |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 437 | struct pci_device *pci; |
| 438 | foreachpci(pci) { |
| 439 | if (!is_pci_vga(pci)) |
| 440 | continue; |
Kevin O'Connor | c1de91b | 2011-07-02 13:50:21 -0400 | [diff] [blame] | 441 | vgahook_setup(pci); |
Kevin O'Connor | 862d5fb | 2011-06-20 22:19:17 -0400 | [diff] [blame] | 442 | init_pcirom(pci, 1, NULL); |
| 443 | break; |
| 444 | } |
Kevin O'Connor | 09880da | 2009-06-17 20:35:41 -0400 | [diff] [blame] | 445 | |
| 446 | // Find and deploy CBFS vga-style roms not associated with a device. |
Kevin O'Connor | bca3a87 | 2010-12-24 14:42:42 -0500 | [diff] [blame] | 447 | run_file_roms("vgaroms/", 1, NULL); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 448 | } |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 449 | rom_reserve(0); |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 450 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 451 | if (rom_get_last() == BUILD_ROM_START) |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 452 | // No VGA rom found |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 453 | return; |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 454 | |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 455 | VgaROM = (void*)BUILD_ROM_START; |
Kevin O'Connor | afbed1b | 2010-06-28 07:34:53 -0400 | [diff] [blame] | 456 | enable_vga_console(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 457 | } |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 458 | |
| 459 | void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 460 | s3_resume_vga(void) |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 461 | { |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 462 | if (!S3ResumeVga) |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 463 | return; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 464 | if (!VgaROM || ! is_valid_rom(VgaROM)) |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 465 | return; |
Kevin O'Connor | 5e01908 | 2012-05-20 21:11:43 -0400 | [diff] [blame] | 466 | callrom(VgaROM, 0); |
Kevin O'Connor | d282af7 | 2009-07-04 04:10:32 -0400 | [diff] [blame] | 467 | } |