Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 1 | // Coreboot interface support. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 6 | |
| 7 | #include "memmap.h" // add_e820 |
| 8 | #include "util.h" // dprintf |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 9 | #include "pci.h" // struct pir_header |
| 10 | #include "acpi.h" // struct rsdp_descriptor |
Kevin O'Connor | 3574644 | 2009-02-27 20:54:51 -0500 | [diff] [blame] | 11 | #include "mptable.h" // MPTABLE_SIGNATURE |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 12 | #include "biosvar.h" // GET_EBDA |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 13 | #include "lzmadecode.h" // LzmaDecode |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 14 | |
| 15 | |
| 16 | /**************************************************************** |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 17 | * BIOS table copying |
| 18 | ****************************************************************/ |
| 19 | |
| 20 | static void |
| 21 | copy_pir(void *pos) |
| 22 | { |
| 23 | struct pir_header *p = pos; |
| 24 | if (p->signature != PIR_SIGNATURE) |
| 25 | return; |
Kevin O'Connor | 51358db | 2008-12-28 21:50:29 -0500 | [diff] [blame] | 26 | if (PirOffset) |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 27 | return; |
| 28 | if (p->size < sizeof(*p)) |
| 29 | return; |
| 30 | if (checksum(pos, p->size) != 0) |
| 31 | return; |
| 32 | bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16); |
| 33 | if (bios_table_cur_addr + p->size > bios_table_end_addr) { |
| 34 | dprintf(1, "No room to copy PIR table!\n"); |
| 35 | return; |
| 36 | } |
| 37 | dprintf(1, "Copying PIR from %p to %x\n", pos, bios_table_cur_addr); |
| 38 | memcpy((void*)bios_table_cur_addr, pos, p->size); |
Kevin O'Connor | 51358db | 2008-12-28 21:50:29 -0500 | [diff] [blame] | 39 | PirOffset = bios_table_cur_addr - BUILD_BIOS_ADDR; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 40 | bios_table_cur_addr += p->size; |
| 41 | } |
| 42 | |
Kevin O'Connor | e10e6f8 | 2008-06-21 19:46:43 -0400 | [diff] [blame] | 43 | static void |
| 44 | copy_mptable(void *pos) |
| 45 | { |
| 46 | struct mptable_floating_s *p = pos; |
Kevin O'Connor | 3574644 | 2009-02-27 20:54:51 -0500 | [diff] [blame] | 47 | if (p->signature != MPTABLE_SIGNATURE) |
Kevin O'Connor | e10e6f8 | 2008-06-21 19:46:43 -0400 | [diff] [blame] | 48 | return; |
| 49 | if (!p->physaddr) |
| 50 | return; |
| 51 | if (checksum(pos, sizeof(*p)) != 0) |
| 52 | return; |
| 53 | u32 length = p->length * 16; |
| 54 | bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16); |
Kevin O'Connor | 47e2093 | 2009-02-27 21:04:09 -0500 | [diff] [blame] | 55 | u16 mpclength = ((struct mptable_config_s *)p->physaddr)->length; |
| 56 | if (bios_table_cur_addr + length + mpclength > bios_table_end_addr) { |
Kevin O'Connor | e10e6f8 | 2008-06-21 19:46:43 -0400 | [diff] [blame] | 57 | dprintf(1, "No room to copy MPTABLE!\n"); |
| 58 | return; |
| 59 | } |
Kevin O'Connor | 47e2093 | 2009-02-27 21:04:09 -0500 | [diff] [blame] | 60 | dprintf(1, "Copying MPTABLE from %p/%x to %x\n" |
| 61 | , pos, p->physaddr, bios_table_cur_addr); |
Kevin O'Connor | e10e6f8 | 2008-06-21 19:46:43 -0400 | [diff] [blame] | 62 | memcpy((void*)bios_table_cur_addr, pos, length); |
Kevin O'Connor | 47e2093 | 2009-02-27 21:04:09 -0500 | [diff] [blame] | 63 | struct mptable_floating_s *newp = (void*)bios_table_cur_addr; |
| 64 | newp->physaddr = bios_table_cur_addr + length; |
| 65 | newp->checksum = 0; |
| 66 | newp->checksum = -checksum(newp, sizeof(*newp)); |
| 67 | memcpy((void*)bios_table_cur_addr + length, (void*)p->physaddr, mpclength); |
| 68 | bios_table_cur_addr += length + mpclength; |
Kevin O'Connor | e10e6f8 | 2008-06-21 19:46:43 -0400 | [diff] [blame] | 69 | } |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 70 | |
| 71 | static void |
| 72 | copy_acpi_rsdp(void *pos) |
| 73 | { |
Kevin O'Connor | 9967ab7 | 2008-12-18 21:57:33 -0500 | [diff] [blame] | 74 | if (RsdpAddr) |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 75 | return; |
| 76 | struct rsdp_descriptor *p = pos; |
Kevin O'Connor | 9967ab7 | 2008-12-18 21:57:33 -0500 | [diff] [blame] | 77 | if (p->signature != RSDP_SIGNATURE) |
| 78 | return; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 79 | u32 length = 20; |
| 80 | if (checksum(pos, length) != 0) |
| 81 | return; |
| 82 | if (p->revision > 1) { |
| 83 | length = p->length; |
| 84 | if (checksum(pos, length) != 0) |
| 85 | return; |
| 86 | } |
| 87 | bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16); |
| 88 | if (bios_table_cur_addr + length > bios_table_end_addr) { |
| 89 | dprintf(1, "No room to copy ACPI RSDP table!\n"); |
| 90 | return; |
| 91 | } |
| 92 | dprintf(1, "Copying ACPI RSDP from %p to %x\n", pos, bios_table_cur_addr); |
Kevin O'Connor | 9967ab7 | 2008-12-18 21:57:33 -0500 | [diff] [blame] | 93 | RsdpAddr = (void*)bios_table_cur_addr; |
| 94 | memcpy(RsdpAddr, pos, length); |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 95 | bios_table_cur_addr += length; |
| 96 | } |
| 97 | |
| 98 | // Attempt to find (and relocate) any standard bios tables found in a |
| 99 | // given address range. |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 100 | static void |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 101 | scan_tables(u32 start, u32 size) |
| 102 | { |
| 103 | void *p = (void*)ALIGN(start, 16); |
| 104 | void *end = (void*)start + size; |
| 105 | for (; p<end; p += 16) { |
| 106 | copy_pir(p); |
Kevin O'Connor | e10e6f8 | 2008-06-21 19:46:43 -0400 | [diff] [blame] | 107 | copy_mptable(p); |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 108 | copy_acpi_rsdp(p); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /**************************************************************** |
| 114 | * Memory map |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 115 | ****************************************************************/ |
| 116 | |
| 117 | struct cb_header { |
| 118 | u32 signature; |
| 119 | u32 header_bytes; |
| 120 | u32 header_checksum; |
| 121 | u32 table_bytes; |
| 122 | u32 table_checksum; |
| 123 | u32 table_entries; |
| 124 | }; |
| 125 | |
| 126 | #define CB_SIGNATURE 0x4f49424C // "LBIO" |
| 127 | |
| 128 | struct cb_memory_range { |
| 129 | u64 start; |
| 130 | u64 size; |
| 131 | u32 type; |
| 132 | }; |
| 133 | |
| 134 | #define CB_MEM_TABLE 16 |
| 135 | |
| 136 | struct cb_memory { |
| 137 | u32 tag; |
| 138 | u32 size; |
| 139 | struct cb_memory_range map[0]; |
| 140 | }; |
| 141 | |
| 142 | #define CB_TAG_MEMORY 0x01 |
| 143 | |
| 144 | #define MEM_RANGE_COUNT(_rec) \ |
| 145 | (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0])) |
| 146 | |
Kevin O'Connor | 071f154 | 2009-03-21 13:17:21 -0400 | [diff] [blame] | 147 | struct cb_forward { |
| 148 | u32 tag; |
| 149 | u32 size; |
| 150 | u64 forward; |
| 151 | }; |
| 152 | |
| 153 | #define CB_TAG_FORWARD 0x11 |
| 154 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 155 | static u16 |
| 156 | ipchksum(char *buf, int count) |
| 157 | { |
| 158 | u16 *p = (u16*)buf; |
| 159 | u32 sum = 0; |
| 160 | while (count > 1) { |
| 161 | sum += *p++; |
| 162 | count -= 2; |
| 163 | } |
| 164 | if (count) |
| 165 | sum += *(u8*)p; |
| 166 | sum = (sum >> 16) + (sum & 0xffff); |
| 167 | sum += (sum >> 16); |
| 168 | return ~sum; |
| 169 | } |
| 170 | |
| 171 | // Try to locate the coreboot header in a given address range. |
| 172 | static struct cb_header * |
| 173 | find_cb_header(char *addr, int len) |
| 174 | { |
| 175 | char *end = addr + len; |
| 176 | for (; addr < end; addr += 16) { |
| 177 | struct cb_header *cbh = (struct cb_header *)addr; |
| 178 | if (cbh->signature != CB_SIGNATURE) |
| 179 | continue; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 180 | if (! cbh->table_bytes) |
| 181 | continue; |
| 182 | if (ipchksum(addr, sizeof(*cbh)) != 0) |
| 183 | continue; |
| 184 | if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes) |
| 185 | != cbh->table_checksum) |
| 186 | continue; |
| 187 | return cbh; |
| 188 | } |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | // Try to find the coreboot memory table in the given coreboot table. |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 193 | static void * |
| 194 | find_cb_subtable(struct cb_header *cbh, u32 tag) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 195 | { |
| 196 | char *tbl = (char *)cbh + sizeof(*cbh); |
| 197 | int i; |
| 198 | for (i=0; i<cbh->table_entries; i++) { |
| 199 | struct cb_memory *cbm = (struct cb_memory *)tbl; |
| 200 | tbl += cbm->size; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 201 | if (cbm->tag == tag) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 202 | return cbm; |
| 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | // Populate max ram and e820 map info by scanning for a coreboot table. |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 208 | static void |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 209 | coreboot_fill_map() |
| 210 | { |
| 211 | dprintf(3, "Attempting to find coreboot table\n"); |
Kevin O'Connor | ef3d882 | 2009-02-05 19:51:12 -0500 | [diff] [blame] | 212 | |
| 213 | // Init variables set in coreboot table memory scan. |
| 214 | PirOffset = 0; |
| 215 | RsdpAddr = 0; |
| 216 | |
| 217 | // Find coreboot table. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 218 | struct cb_header *cbh = find_cb_header(0, 0x1000); |
| 219 | if (!cbh) |
| 220 | goto fail; |
Kevin O'Connor | 071f154 | 2009-03-21 13:17:21 -0400 | [diff] [blame] | 221 | struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD); |
| 222 | if (cbf) { |
| 223 | dprintf(3, "Found coreboot table forwarder.\n"); |
| 224 | cbh = find_cb_header((char *)((u32)cbf->forward), 0x100); |
| 225 | if (!cbh) |
| 226 | goto fail; |
| 227 | } |
| 228 | dprintf(3, "Now attempting to find coreboot memory map\n"); |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 229 | struct cb_memory *cbm = find_cb_subtable(cbh, CB_TAG_MEMORY); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 230 | if (!cbm) |
| 231 | goto fail; |
| 232 | |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 233 | u64 maxram = 0, maxram_over4G = 0; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 234 | int i, count = MEM_RANGE_COUNT(cbm); |
| 235 | for (i=0; i<count; i++) { |
| 236 | struct cb_memory_range *m = &cbm->map[i]; |
| 237 | u32 type = m->type; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 238 | if (type == CB_MEM_TABLE) { |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 239 | type = E820_RESERVED; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 240 | scan_tables(m->start, m->size); |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 241 | } else if (type == E820_ACPI || type == E820_RAM) { |
| 242 | u64 end = m->start + m->size; |
| 243 | if (end > 0x100000000ull) { |
| 244 | end -= 0x100000000ull; |
| 245 | if (end > maxram_over4G) |
| 246 | maxram_over4G = end; |
| 247 | } else if (end > maxram) |
| 248 | maxram = end; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 249 | } |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 250 | add_e820(m->start, m->size, type); |
| 251 | } |
| 252 | |
Kevin O'Connor | e791636 | 2008-12-28 22:03:17 -0500 | [diff] [blame] | 253 | RamSize = maxram; |
| 254 | RamSizeOver4G = maxram_over4G; |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 255 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 256 | // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this |
| 257 | // confuses grub. So, override it. |
| 258 | add_e820(0, 16*1024, E820_RAM); |
| 259 | |
Kevin O'Connor | 1d247db | 2008-08-29 21:19:53 -0400 | [diff] [blame] | 260 | // XXX - just create dummy smbios table for now - should detect if |
| 261 | // smbios/dmi table is found from coreboot and use that instead. |
| 262 | smbios_init(); |
| 263 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 264 | return; |
| 265 | |
| 266 | fail: |
| 267 | // No table found.. Use 16Megs as a dummy value. |
| 268 | dprintf(1, "Unable to find coreboot table!\n"); |
Kevin O'Connor | e791636 | 2008-12-28 22:03:17 -0500 | [diff] [blame] | 269 | RamSize = 16*1024*1024; |
| 270 | RamSizeOver4G = 0; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 271 | add_e820(0, 16*1024*1024, E820_RAM); |
| 272 | return; |
| 273 | } |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 274 | |
| 275 | |
| 276 | /**************************************************************** |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 277 | * ulzma |
| 278 | ****************************************************************/ |
| 279 | |
| 280 | static int |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 281 | ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen) |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 282 | { |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 283 | dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst); |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 284 | CLzmaDecoderState state; |
| 285 | int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE); |
| 286 | if (ret != LZMA_RESULT_OK) { |
| 287 | dprintf(1, "LzmaDecodeProperties error - %d\n", ret); |
| 288 | return -1; |
| 289 | } |
| 290 | u8 scratch[15980]; |
| 291 | int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); |
| 292 | if (need > sizeof(scratch)) { |
| 293 | dprintf(1, "LzmaDecode need %d have %d\n", need, sizeof(scratch)); |
| 294 | return -1; |
| 295 | } |
| 296 | state.Probs = (CProb *)scratch; |
| 297 | |
| 298 | u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE); |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 299 | if (dstlen > maxlen) { |
| 300 | dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen); |
| 301 | return -1; |
| 302 | } |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 303 | u32 inProcessed, outProcessed; |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 304 | ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 305 | , &inProcessed, dst, dstlen, &outProcessed); |
| 306 | if (ret) { |
| 307 | dprintf(1, "LzmaDecode returned %d\n", ret); |
| 308 | return -1; |
| 309 | } |
| 310 | return dstlen; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /**************************************************************** |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 315 | * Coreboot flash format |
| 316 | ****************************************************************/ |
| 317 | |
| 318 | // XXX - optimize |
| 319 | #define ntohl(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | \ |
| 320 | (((x)&0xff0000) >> 8) | (((x)&0xff000000) >> 24)) |
| 321 | #define htonl(x) ntohl(x) |
| 322 | |
| 323 | #define CBFS_HEADER_MAGIC 0x4F524243 |
| 324 | #define CBFS_HEADPTR_ADDR 0xFFFFFFFc |
| 325 | #define CBFS_VERSION1 0x31313131 |
| 326 | |
| 327 | struct cbfs_header { |
| 328 | u32 magic; |
| 329 | u32 version; |
| 330 | u32 romsize; |
| 331 | u32 bootblocksize; |
| 332 | u32 align; |
| 333 | u32 offset; |
| 334 | u32 pad[2]; |
| 335 | } PACKED; |
| 336 | |
| 337 | static struct cbfs_header *CBHDR; |
| 338 | |
| 339 | static void |
| 340 | cbfs_setup() |
| 341 | { |
| 342 | if (! CONFIG_COREBOOT_FLASH) |
| 343 | return; |
| 344 | |
| 345 | CBHDR = *(void **)CBFS_HEADPTR_ADDR; |
| 346 | if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) { |
| 347 | dprintf(1, "Unable to find CBFS (got %x not %x)\n" |
| 348 | , CBHDR->magic, htonl(CBFS_HEADER_MAGIC)); |
| 349 | CBHDR = NULL; |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | dprintf(1, "Found CBFS header at %p\n", CBHDR); |
| 354 | } |
| 355 | |
| 356 | #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE |
| 357 | |
| 358 | struct cbfs_file { |
| 359 | u64 magic; |
| 360 | u32 len; |
| 361 | u32 type; |
| 362 | u32 checksum; |
| 363 | u32 offset; |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 364 | char filename[0]; |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 365 | } PACKED; |
| 366 | |
| 367 | static struct cbfs_file * |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 368 | cbfs_search(struct cbfs_file *file) |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 369 | { |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 370 | for (;;) { |
| 371 | if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize))) |
| 372 | return NULL; |
Kevin O'Connor | 214f6c2 | 2009-04-26 21:27:15 -0400 | [diff] [blame] | 373 | u64 magic = file->magic; |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 374 | if (magic == CBFS_FILE_MAGIC) { |
| 375 | dprintf(5, "Found CBFS file %s\n", file->filename); |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 376 | return file; |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 377 | } |
Kevin O'Connor | 214f6c2 | 2009-04-26 21:27:15 -0400 | [diff] [blame] | 378 | if (magic == 0) |
| 379 | return NULL; |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 380 | file = (void*)file + ntohl(CBHDR->align); |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 384 | static struct cbfs_file * |
| 385 | cbfs_getfirst() |
| 386 | { |
| 387 | if (! CBHDR) |
| 388 | return NULL; |
| 389 | return cbfs_search((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset))); |
| 390 | } |
| 391 | |
| 392 | static struct cbfs_file * |
| 393 | cbfs_getnext(struct cbfs_file *file) |
| 394 | { |
| 395 | file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align)); |
| 396 | return cbfs_search(file); |
| 397 | } |
| 398 | |
| 399 | static struct cbfs_file * |
| 400 | cbfs_findfile(const char *fname) |
| 401 | { |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 402 | dprintf(3, "Searching CBFS for %s\n", fname); |
| 403 | struct cbfs_file *file; |
| 404 | for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) { |
Kevin O'Connor | 38d1a34 | 2009-04-18 16:59:47 -0400 | [diff] [blame] | 405 | if (strcmp(fname, file->filename) == 0) |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 406 | return file; |
| 407 | } |
| 408 | return NULL; |
| 409 | } |
| 410 | |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 411 | static int |
| 412 | data_copy(u8 *dst, u32 maxlen, const u8 *src, u32 srclen) |
| 413 | { |
| 414 | dprintf(3, "Copying data %d@%p to %d@%p\n", srclen, src, maxlen, dst); |
| 415 | if (srclen > maxlen) { |
| 416 | dprintf(1, "File too big to copy\n"); |
| 417 | return -1; |
| 418 | } |
| 419 | memcpy(dst, src, srclen); |
| 420 | return srclen; |
| 421 | } |
| 422 | |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 423 | // Copy a file to memory (uncompressing if necessary) |
| 424 | static int |
| 425 | cbfs_copyfile(void *dst, u32 maxlen, const char *fname) |
| 426 | { |
| 427 | dprintf(3, "Searching CBFS for data file %s\n", fname); |
| 428 | int fnlen = strlen(fname); |
| 429 | struct cbfs_file *file; |
| 430 | for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) { |
| 431 | if (memcmp(fname, file->filename, fnlen) != 0) |
| 432 | continue; |
| 433 | u32 size = ntohl(file->len); |
| 434 | void *src = (void*)file + ntohl(file->offset); |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 435 | if (file->filename[fnlen] == '\0') |
| 436 | return data_copy(dst, maxlen, src, size); |
| 437 | if (strcmp(&file->filename[fnlen], ".lzma") == 0) |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 438 | return ulzma(dst, maxlen, src, size); |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 439 | } |
| 440 | return -1; |
| 441 | } |
| 442 | |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 443 | const char * |
| 444 | cbfs_findNprefix(const char *prefix, int n) |
| 445 | { |
| 446 | if (! CONFIG_COREBOOT_FLASH) |
| 447 | return NULL; |
| 448 | |
| 449 | dprintf(3, "Searching CBFS for prefix %s\n", prefix); |
| 450 | int len = strlen(prefix); |
| 451 | struct cbfs_file *file; |
| 452 | for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) { |
Kevin O'Connor | 38d1a34 | 2009-04-18 16:59:47 -0400 | [diff] [blame] | 453 | if (memcmp(prefix, file->filename, len) == 0) { |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 454 | if (n <= 0) |
| 455 | return file->filename; |
| 456 | n--; |
| 457 | } |
| 458 | } |
| 459 | return NULL; |
| 460 | } |
| 461 | |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 462 | static char |
| 463 | getHex(u8 x) |
| 464 | { |
| 465 | if (x <= 9) |
| 466 | return '0' + x; |
| 467 | return 'a' + x - 10; |
| 468 | } |
| 469 | |
| 470 | static u32 |
| 471 | hexify4(u16 x) |
| 472 | { |
| 473 | return ((getHex(x&0xf) << 24) |
| 474 | | (getHex((x>>4)&0xf) << 16) |
| 475 | | (getHex((x>>8)&0xf) << 8) |
| 476 | | (getHex(x>>12))); |
| 477 | } |
| 478 | |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 479 | int |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 480 | cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev) |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 481 | { |
| 482 | if (! CONFIG_COREBOOT_FLASH) |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 483 | return -1; |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 484 | |
| 485 | char fname[17]; |
| 486 | // Ughh - poor man's sprintf of "pci%04x,%04x.rom" |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 487 | *(u32*)fname = 0x20696370; // "pci " |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 488 | *(u32*)&fname[3] = hexify4(vendev); |
| 489 | fname[7] = ','; |
| 490 | *(u32*)&fname[8] = hexify4(vendev >> 16); |
| 491 | *(u32*)&fname[12] = 0x6d6f722e; // ".rom" |
| 492 | fname[16] = '\0'; |
| 493 | |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 494 | return cbfs_copyfile(dst, maxlen, fname); |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 497 | struct cbfs_file * |
| 498 | cbfs_copy_gen_optionrom(void *dst, u32 maxlen, struct cbfs_file *file) |
| 499 | { |
| 500 | if (! CONFIG_COREBOOT_FLASH) |
| 501 | return NULL; |
| 502 | if (! file) |
| 503 | file = cbfs_getfirst(); |
| 504 | else |
| 505 | file = cbfs_getnext(file); |
| 506 | for (; file; file = cbfs_getnext(file)) { |
| 507 | if (memcmp("genroms/", file->filename, 8) != 0) |
| 508 | continue; |
| 509 | u32 size = ntohl(file->len); |
| 510 | void *src = (void*)file + ntohl(file->offset); |
| 511 | int fnamelen = strlen(file->filename); |
| 512 | int rv; |
| 513 | if (fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0) |
| 514 | rv = ulzma(dst, maxlen, src, size); |
| 515 | else |
| 516 | rv = data_copy(dst, maxlen, src, size); |
| 517 | if (rv >= 0) |
| 518 | return file; |
| 519 | } |
| 520 | return NULL; |
| 521 | } |
| 522 | |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 523 | struct cbfs_payload_segment { |
| 524 | u32 type; |
| 525 | u32 compression; |
| 526 | u32 offset; |
| 527 | u64 load_addr; |
| 528 | u32 len; |
| 529 | u32 mem_len; |
| 530 | } PACKED; |
| 531 | |
| 532 | #define PAYLOAD_SEGMENT_BSS 0x20535342 |
| 533 | #define PAYLOAD_SEGMENT_ENTRY 0x52544E45 |
| 534 | |
| 535 | #define CBFS_COMPRESS_NONE 0 |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 536 | #define CBFS_COMPRESS_LZMA 1 |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 537 | |
| 538 | struct cbfs_payload { |
| 539 | struct cbfs_payload_segment segments[1]; |
| 540 | }; |
| 541 | |
| 542 | void |
| 543 | cbfs_run_payload(const char *filename) |
| 544 | { |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 545 | if (! CONFIG_COREBOOT_FLASH) |
| 546 | return; |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 547 | dprintf(1, "Run %s\n", filename); |
| 548 | struct cbfs_file *file = cbfs_findfile(filename); |
| 549 | if (!file) |
| 550 | return; |
| 551 | struct cbfs_payload *pay = (void*)file + ntohl(file->offset); |
| 552 | struct cbfs_payload_segment *seg = pay->segments; |
| 553 | for (;;) { |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 554 | void *src = (void*)pay + ntohl(seg->offset); |
| 555 | void *dest = (void*)ntohl((u32)seg->load_addr); |
| 556 | u32 src_len = ntohl(seg->len); |
| 557 | u32 dest_len = ntohl(seg->mem_len); |
| 558 | switch (seg->type) { |
| 559 | case PAYLOAD_SEGMENT_BSS: |
| 560 | dprintf(3, "BSS segment %d@%p\n", dest_len, dest); |
| 561 | memset(dest, 0, dest_len); |
| 562 | break; |
| 563 | case PAYLOAD_SEGMENT_ENTRY: { |
| 564 | dprintf(1, "Calling addr %p\n", dest); |
| 565 | void (*func)() = dest; |
| 566 | func(); |
| 567 | return; |
| 568 | } |
| 569 | default: |
| 570 | dprintf(3, "Segment %x %d@%p -> %d@%p\n" |
| 571 | , seg->type, src_len, src, dest_len, dest); |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 572 | if (seg->compression == htonl(CBFS_COMPRESS_NONE)) { |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 573 | if (src_len > dest_len) |
| 574 | src_len = dest_len; |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 575 | memcpy(dest, src, src_len); |
| 576 | } else if (CONFIG_LZMA |
| 577 | && seg->compression == htonl(CBFS_COMPRESS_LZMA)) { |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 578 | int ret = ulzma(dest, dest_len, src, src_len); |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 579 | if (ret < 0) |
| 580 | return; |
| 581 | src_len = ret; |
| 582 | } else { |
| 583 | dprintf(1, "No support for compression type %x\n" |
| 584 | , seg->compression); |
| 585 | return; |
| 586 | } |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 587 | if (dest_len > src_len) |
| 588 | memset(dest + src_len, 0, dest_len - src_len); |
| 589 | break; |
| 590 | } |
| 591 | seg++; |
| 592 | } |
| 593 | } |
| 594 | |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 595 | void |
| 596 | coreboot_setup(void) |
| 597 | { |
| 598 | coreboot_fill_map(); |
| 599 | cbfs_setup(); |
| 600 | } |