Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 1 | // Coreboot interface support. |
| 2 | // |
Kevin O'Connor | 2929c35 | 2009-07-25 13:48:27 -0400 | [diff] [blame] | 3 | // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 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 | |
Kevin O'Connor | 135f3f6 | 2013-09-14 23:57:26 -0400 | [diff] [blame] | 7 | #include "block.h" // MAXDESCSIZE |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 8 | #include "byteorder.h" // be32_to_cpu |
| 9 | #include "config.h" // CONFIG_* |
Kevin O'Connor | 5d369d8 | 2013-09-02 20:48:46 -0400 | [diff] [blame] | 10 | #include "hw/pci.h" // pci_probe_devices |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 11 | #include "lzmadecode.h" // LzmaDecode |
Kevin O'Connor | 9dea590 | 2013-09-14 20:23:54 -0400 | [diff] [blame] | 12 | #include "malloc.h" // free |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 13 | #include "memmap.h" // add_e820 |
| 14 | #include "output.h" // dprintf |
Gerd Hoffmann | 1f20982 | 2013-06-03 08:06:27 +0200 | [diff] [blame] | 15 | #include "paravirt.h" // PlatformRunningOn |
Kevin O'Connor | 41639f8 | 2013-09-14 19:37:36 -0400 | [diff] [blame] | 16 | #include "romfile.h" // romfile_findprefix |
Kevin O'Connor | 3df600b | 2013-09-14 19:28:55 -0400 | [diff] [blame] | 17 | #include "stacks.h" // yield |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 18 | #include "string.h" // memset |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 19 | #include "util.h" // coreboot_preinit |
Kevin O'Connor | a2a86e2 | 2013-02-13 19:35:12 -0500 | [diff] [blame] | 20 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 21 | |
| 22 | /**************************************************************** |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 23 | * Memory map |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 24 | ****************************************************************/ |
| 25 | |
| 26 | struct cb_header { |
| 27 | u32 signature; |
| 28 | u32 header_bytes; |
| 29 | u32 header_checksum; |
| 30 | u32 table_bytes; |
| 31 | u32 table_checksum; |
| 32 | u32 table_entries; |
| 33 | }; |
| 34 | |
| 35 | #define CB_SIGNATURE 0x4f49424C // "LBIO" |
| 36 | |
| 37 | struct cb_memory_range { |
| 38 | u64 start; |
| 39 | u64 size; |
| 40 | u32 type; |
| 41 | }; |
| 42 | |
| 43 | #define CB_MEM_TABLE 16 |
| 44 | |
| 45 | struct cb_memory { |
| 46 | u32 tag; |
| 47 | u32 size; |
| 48 | struct cb_memory_range map[0]; |
| 49 | }; |
| 50 | |
| 51 | #define CB_TAG_MEMORY 0x01 |
| 52 | |
| 53 | #define MEM_RANGE_COUNT(_rec) \ |
| 54 | (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0])) |
| 55 | |
Kevin O'Connor | 22e1b91 | 2009-07-19 18:52:46 -0400 | [diff] [blame] | 56 | struct cb_mainboard { |
| 57 | u32 tag; |
| 58 | u32 size; |
| 59 | u8 vendor_idx; |
| 60 | u8 part_idx; |
| 61 | char strings[0]; |
| 62 | }; |
| 63 | |
| 64 | #define CB_TAG_MAINBOARD 0x0003 |
| 65 | |
Kevin O'Connor | 071f154 | 2009-03-21 13:17:21 -0400 | [diff] [blame] | 66 | struct cb_forward { |
| 67 | u32 tag; |
| 68 | u32 size; |
| 69 | u64 forward; |
| 70 | }; |
| 71 | |
| 72 | #define CB_TAG_FORWARD 0x11 |
| 73 | |
Gerd Hoffmann | ebf03f7 | 2013-06-24 11:24:57 +0200 | [diff] [blame] | 74 | struct cb_cbmem_ref { |
| 75 | u32 tag; |
| 76 | u32 size; |
| 77 | u64 cbmem_addr; |
| 78 | }; |
| 79 | |
| 80 | #define CB_TAG_CBMEM_CONSOLE 0x17 |
| 81 | |
| 82 | struct cbmem_console { |
| 83 | u32 buffer_size; |
| 84 | u32 buffer_cursor; |
| 85 | u8 buffer_body[0]; |
| 86 | } PACKED; |
| 87 | static struct cbmem_console *cbcon = NULL; |
| 88 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 89 | static u16 |
| 90 | ipchksum(char *buf, int count) |
| 91 | { |
| 92 | u16 *p = (u16*)buf; |
| 93 | u32 sum = 0; |
| 94 | while (count > 1) { |
| 95 | sum += *p++; |
| 96 | count -= 2; |
| 97 | } |
| 98 | if (count) |
| 99 | sum += *(u8*)p; |
| 100 | sum = (sum >> 16) + (sum & 0xffff); |
| 101 | sum += (sum >> 16); |
| 102 | return ~sum; |
| 103 | } |
| 104 | |
| 105 | // Try to locate the coreboot header in a given address range. |
| 106 | static struct cb_header * |
| 107 | find_cb_header(char *addr, int len) |
| 108 | { |
| 109 | char *end = addr + len; |
| 110 | for (; addr < end; addr += 16) { |
| 111 | struct cb_header *cbh = (struct cb_header *)addr; |
| 112 | if (cbh->signature != CB_SIGNATURE) |
| 113 | continue; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 114 | if (! cbh->table_bytes) |
| 115 | continue; |
| 116 | if (ipchksum(addr, sizeof(*cbh)) != 0) |
| 117 | continue; |
| 118 | if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes) |
| 119 | != cbh->table_checksum) |
| 120 | continue; |
| 121 | return cbh; |
| 122 | } |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | // 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] | 127 | static void * |
| 128 | find_cb_subtable(struct cb_header *cbh, u32 tag) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 129 | { |
| 130 | char *tbl = (char *)cbh + sizeof(*cbh); |
| 131 | int i; |
| 132 | for (i=0; i<cbh->table_entries; i++) { |
| 133 | struct cb_memory *cbm = (struct cb_memory *)tbl; |
| 134 | tbl += cbm->size; |
Kevin O'Connor | 93479e4 | 2008-06-12 22:22:43 -0400 | [diff] [blame] | 135 | if (cbm->tag == tag) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 136 | return cbm; |
| 137 | } |
| 138 | return NULL; |
| 139 | } |
| 140 | |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 141 | static struct cb_memory *CBMemTable; |
Julian Pidancet | 0214515 | 2012-02-05 04:51:06 +0000 | [diff] [blame] | 142 | const char *CBvendor = "", *CBpart = ""; |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 143 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 144 | // Populate max ram and e820 map info by scanning for a coreboot table. |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 145 | void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 146 | coreboot_preinit(void) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 147 | { |
Kevin O'Connor | a2a86e2 | 2013-02-13 19:35:12 -0500 | [diff] [blame] | 148 | if (!CONFIG_COREBOOT) |
| 149 | return; |
| 150 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 151 | dprintf(3, "Attempting to find coreboot table\n"); |
Kevin O'Connor | ef3d882 | 2009-02-05 19:51:12 -0500 | [diff] [blame] | 152 | |
Kevin O'Connor | ef3d882 | 2009-02-05 19:51:12 -0500 | [diff] [blame] | 153 | // Find coreboot table. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 154 | struct cb_header *cbh = find_cb_header(0, 0x1000); |
| 155 | if (!cbh) |
| 156 | goto fail; |
Kevin O'Connor | 071f154 | 2009-03-21 13:17:21 -0400 | [diff] [blame] | 157 | struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD); |
| 158 | if (cbf) { |
| 159 | dprintf(3, "Found coreboot table forwarder.\n"); |
| 160 | cbh = find_cb_header((char *)((u32)cbf->forward), 0x100); |
| 161 | if (!cbh) |
| 162 | goto fail; |
| 163 | } |
| 164 | dprintf(3, "Now attempting to find coreboot memory map\n"); |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 165 | struct cb_memory *cbm = CBMemTable = find_cb_subtable(cbh, CB_TAG_MEMORY); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 166 | if (!cbm) |
| 167 | goto fail; |
| 168 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 169 | int i, count = MEM_RANGE_COUNT(cbm); |
| 170 | for (i=0; i<count; i++) { |
| 171 | struct cb_memory_range *m = &cbm->map[i]; |
| 172 | u32 type = m->type; |
Kevin O'Connor | f85e4bc | 2013-02-19 01:33:45 -0500 | [diff] [blame] | 173 | if (type == CB_MEM_TABLE) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 174 | type = E820_RESERVED; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 175 | add_e820(m->start, m->size, type); |
| 176 | } |
| 177 | |
| 178 | // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this |
| 179 | // confuses grub. So, override it. |
| 180 | add_e820(0, 16*1024, E820_RAM); |
| 181 | |
Gerd Hoffmann | ebf03f7 | 2013-06-24 11:24:57 +0200 | [diff] [blame] | 182 | struct cb_cbmem_ref *cbref = find_cb_subtable(cbh, CB_TAG_CBMEM_CONSOLE); |
| 183 | if (cbref) { |
| 184 | cbcon = (void*)(u32)cbref->cbmem_addr; |
| 185 | dprintf(1, "----- [ seabios log starts here ] -----\n"); |
| 186 | dprintf(1, "Found coreboot cbmem console @ %llx\n", cbref->cbmem_addr); |
| 187 | } |
| 188 | |
Kevin O'Connor | 22e1b91 | 2009-07-19 18:52:46 -0400 | [diff] [blame] | 189 | struct cb_mainboard *cbmb = find_cb_subtable(cbh, CB_TAG_MAINBOARD); |
| 190 | if (cbmb) { |
Kevin O'Connor | c1de91b | 2011-07-02 13:50:21 -0400 | [diff] [blame] | 191 | CBvendor = &cbmb->strings[cbmb->vendor_idx]; |
| 192 | CBpart = &cbmb->strings[cbmb->part_idx]; |
| 193 | dprintf(1, "Found mainboard %s %s\n", CBvendor, CBpart); |
Kevin O'Connor | 22e1b91 | 2009-07-19 18:52:46 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 196 | return; |
| 197 | |
| 198 | fail: |
| 199 | // No table found.. Use 16Megs as a dummy value. |
| 200 | dprintf(1, "Unable to find coreboot table!\n"); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 201 | add_e820(0, 16*1024*1024, E820_RAM); |
| 202 | return; |
| 203 | } |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 204 | |
Kevin O'Connor | 4cd522e | 2013-11-29 12:14:34 -0500 | [diff] [blame] | 205 | void coreboot_debug_putc(char c) |
Gerd Hoffmann | ebf03f7 | 2013-06-24 11:24:57 +0200 | [diff] [blame] | 206 | { |
Gerd Hoffmann | c83e7c2 | 2013-06-25 15:45:30 +0200 | [diff] [blame] | 207 | if (!CONFIG_DEBUG_COREBOOT) |
Gerd Hoffmann | ebf03f7 | 2013-06-24 11:24:57 +0200 | [diff] [blame] | 208 | return; |
| 209 | if (!cbcon) |
| 210 | return; |
| 211 | if (cbcon->buffer_cursor == cbcon->buffer_size) |
| 212 | return; |
| 213 | cbcon->buffer_body[cbcon->buffer_cursor++] = c; |
| 214 | } |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 215 | |
| 216 | /**************************************************************** |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 217 | * BIOS table copying |
| 218 | ****************************************************************/ |
| 219 | |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 220 | // Attempt to find (and relocate) any standard bios tables found in a |
| 221 | // given address range. |
| 222 | static void |
| 223 | scan_tables(u32 start, u32 size) |
| 224 | { |
| 225 | void *p = (void*)ALIGN(start, 16); |
| 226 | void *end = (void*)start + size; |
Kevin O'Connor | 4d053eb | 2012-06-09 13:08:02 -0400 | [diff] [blame] | 227 | for (; p<end; p += 16) |
| 228 | copy_table(p); |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void |
Kevin O'Connor | a2a86e2 | 2013-02-13 19:35:12 -0500 | [diff] [blame] | 232 | coreboot_platform_setup(void) |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 233 | { |
Kevin O'Connor | a2a86e2 | 2013-02-13 19:35:12 -0500 | [diff] [blame] | 234 | if (!CONFIG_COREBOOT) |
| 235 | return; |
| 236 | pci_probe_devices(); |
| 237 | |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 238 | struct cb_memory *cbm = CBMemTable; |
Kevin O'Connor | a2a86e2 | 2013-02-13 19:35:12 -0500 | [diff] [blame] | 239 | if (!cbm) |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 240 | return; |
| 241 | |
| 242 | dprintf(3, "Relocating coreboot bios tables\n"); |
| 243 | |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 244 | // Scan CB_MEM_TABLE areas for bios tables. |
| 245 | int i, count = MEM_RANGE_COUNT(cbm); |
| 246 | for (i=0; i<count; i++) { |
| 247 | struct cb_memory_range *m = &cbm->map[i]; |
| 248 | if (m->type == CB_MEM_TABLE) |
| 249 | scan_tables(m->start, m->size); |
| 250 | } |
David Woodhouse | dbdb773 | 2013-02-05 16:14:20 +0000 | [diff] [blame] | 251 | |
David Woodhouse | d304fe4 | 2013-02-23 00:24:48 +0000 | [diff] [blame] | 252 | find_acpi_features(); |
Kevin O'Connor | df5a5fd | 2009-07-28 20:46:03 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | |
| 256 | /**************************************************************** |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 257 | * ulzma |
| 258 | ****************************************************************/ |
| 259 | |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 260 | // Uncompress data in flash to an area of memory. |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 261 | static int |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 262 | ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen) |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 263 | { |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 264 | 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] | 265 | CLzmaDecoderState state; |
| 266 | int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE); |
| 267 | if (ret != LZMA_RESULT_OK) { |
| 268 | dprintf(1, "LzmaDecodeProperties error - %d\n", ret); |
| 269 | return -1; |
| 270 | } |
| 271 | u8 scratch[15980]; |
| 272 | int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); |
| 273 | if (need > sizeof(scratch)) { |
Kevin O'Connor | 311f887 | 2010-12-26 14:16:22 -0500 | [diff] [blame] | 274 | dprintf(1, "LzmaDecode need %d have %d\n", need, (unsigned int)sizeof(scratch)); |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 275 | return -1; |
| 276 | } |
| 277 | state.Probs = (CProb *)scratch; |
| 278 | |
| 279 | u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE); |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 280 | if (dstlen > maxlen) { |
| 281 | dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen); |
| 282 | return -1; |
| 283 | } |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 284 | u32 inProcessed, outProcessed; |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 285 | ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 286 | , &inProcessed, dst, dstlen, &outProcessed); |
| 287 | if (ret) { |
| 288 | dprintf(1, "LzmaDecode returned %d\n", ret); |
| 289 | return -1; |
| 290 | } |
| 291 | return dstlen; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | /**************************************************************** |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 296 | * Coreboot flash format |
| 297 | ****************************************************************/ |
| 298 | |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 299 | #define CBFS_HEADER_MAGIC 0x4F524243 |
| 300 | #define CBFS_HEADPTR_ADDR 0xFFFFFFFc |
| 301 | #define CBFS_VERSION1 0x31313131 |
| 302 | |
| 303 | struct cbfs_header { |
| 304 | u32 magic; |
| 305 | u32 version; |
| 306 | u32 romsize; |
| 307 | u32 bootblocksize; |
| 308 | u32 align; |
| 309 | u32 offset; |
| 310 | u32 pad[2]; |
| 311 | } PACKED; |
| 312 | |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 313 | #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE |
| 314 | |
| 315 | struct cbfs_file { |
| 316 | u64 magic; |
| 317 | u32 len; |
| 318 | u32 type; |
| 319 | u32 checksum; |
| 320 | u32 offset; |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 321 | char filename[0]; |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 322 | } PACKED; |
| 323 | |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 324 | struct cbfs_romfile_s { |
| 325 | struct romfile_s file; |
| 326 | struct cbfs_file *fhdr; |
| 327 | void *data; |
| 328 | u32 rawsize, flags; |
| 329 | }; |
| 330 | |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 331 | // Copy a file to memory (uncompressing if necessary) |
| 332 | static int |
| 333 | cbfs_copyfile(struct romfile_s *file, void *dst, u32 maxlen) |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 334 | { |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 335 | if (!CONFIG_COREBOOT_FLASH) |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 336 | return -1; |
| 337 | |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 338 | struct cbfs_romfile_s *cfile; |
| 339 | cfile = container_of(file, struct cbfs_romfile_s, file); |
| 340 | u32 size = cfile->rawsize; |
| 341 | void *src = cfile->data; |
| 342 | if (cfile->flags) { |
Kevin O'Connor | 3403696 | 2009-12-05 18:51:53 -0500 | [diff] [blame] | 343 | // Compressed - copy to temp ram and uncompress it. |
Kevin O'Connor | b4525a0 | 2010-07-27 01:14:11 -0400 | [diff] [blame] | 344 | void *temp = malloc_tmphigh(size); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 345 | if (!temp) { |
| 346 | warn_noalloc(); |
Kevin O'Connor | 3403696 | 2009-12-05 18:51:53 -0500 | [diff] [blame] | 347 | return -1; |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 348 | } |
Kevin O'Connor | b4525a0 | 2010-07-27 01:14:11 -0400 | [diff] [blame] | 349 | iomemcpy(temp, src, size); |
Kevin O'Connor | 3403696 | 2009-12-05 18:51:53 -0500 | [diff] [blame] | 350 | int ret = ulzma(dst, maxlen, temp, size); |
| 351 | yield(); |
| 352 | free(temp); |
| 353 | return ret; |
| 354 | } |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 355 | |
| 356 | // Not compressed. |
| 357 | dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst); |
| 358 | if (size > maxlen) { |
Kevin O'Connor | cfdc13f | 2010-02-14 13:07:54 -0500 | [diff] [blame] | 359 | warn_noalloc(); |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 360 | return -1; |
| 361 | } |
Kevin O'Connor | 3403696 | 2009-12-05 18:51:53 -0500 | [diff] [blame] | 362 | iomemcpy(dst, src, size); |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 363 | return size; |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 364 | } |
| 365 | |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 366 | void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 367 | coreboot_cbfs_init(void) |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 368 | { |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 369 | if (!CONFIG_COREBOOT_FLASH) |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 370 | return; |
| 371 | |
| 372 | struct cbfs_header *hdr = *(void **)CBFS_HEADPTR_ADDR; |
Kevin O'Connor | b306459 | 2012-08-14 21:20:10 -0400 | [diff] [blame] | 373 | if (hdr->magic != cpu_to_be32(CBFS_HEADER_MAGIC)) { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 374 | dprintf(1, "Unable to find CBFS (ptr=%p; got %x not %x)\n" |
Kevin O'Connor | b306459 | 2012-08-14 21:20:10 -0400 | [diff] [blame] | 375 | , hdr, hdr->magic, cpu_to_be32(CBFS_HEADER_MAGIC)); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 376 | return; |
| 377 | } |
| 378 | dprintf(1, "Found CBFS header at %p\n", hdr); |
| 379 | |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 380 | struct cbfs_file *fhdr = (void *)(0 - be32_to_cpu(hdr->romsize) |
| 381 | + be32_to_cpu(hdr->offset)); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 382 | for (;;) { |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 383 | if (fhdr < (struct cbfs_file *)(0xFFFFFFFF - be32_to_cpu(hdr->romsize))) |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 384 | break; |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 385 | u64 magic = fhdr->magic; |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 386 | if (magic != CBFS_FILE_MAGIC) |
| 387 | break; |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 388 | struct cbfs_romfile_s *cfile = malloc_tmp(sizeof(*cfile)); |
| 389 | if (!cfile) { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 390 | warn_noalloc(); |
| 391 | break; |
| 392 | } |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 393 | memset(cfile, 0, sizeof(*cfile)); |
| 394 | strtcpy(cfile->file.name, fhdr->filename, sizeof(cfile->file.name)); |
| 395 | cfile->file.size = cfile->rawsize = be32_to_cpu(fhdr->len); |
| 396 | cfile->fhdr = fhdr; |
| 397 | cfile->file.copy = cbfs_copyfile; |
| 398 | cfile->data = (void*)fhdr + be32_to_cpu(fhdr->offset); |
| 399 | int len = strlen(cfile->file.name); |
| 400 | if (len > 5 && strcmp(&cfile->file.name[len-5], ".lzma") == 0) { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 401 | // Using compression. |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 402 | cfile->flags = 1; |
| 403 | cfile->file.name[len-5] = '\0'; |
| 404 | cfile->file.size = *(u32*)(cfile->data + LZMA_PROPERTIES_SIZE); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 405 | } |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 406 | romfile_add(&cfile->file); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 407 | |
Kevin O'Connor | 1a113e1 | 2013-08-02 14:12:09 -0400 | [diff] [blame] | 408 | fhdr = (void*)ALIGN((u32)cfile->data + cfile->rawsize |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 409 | , be32_to_cpu(hdr->align)); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 413 | struct cbfs_payload_segment { |
| 414 | u32 type; |
| 415 | u32 compression; |
| 416 | u32 offset; |
| 417 | u64 load_addr; |
| 418 | u32 len; |
| 419 | u32 mem_len; |
| 420 | } PACKED; |
| 421 | |
| 422 | #define PAYLOAD_SEGMENT_BSS 0x20535342 |
| 423 | #define PAYLOAD_SEGMENT_ENTRY 0x52544E45 |
| 424 | |
| 425 | #define CBFS_COMPRESS_NONE 0 |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 426 | #define CBFS_COMPRESS_LZMA 1 |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 427 | |
| 428 | struct cbfs_payload { |
| 429 | struct cbfs_payload_segment segments[1]; |
| 430 | }; |
| 431 | |
| 432 | void |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 433 | cbfs_run_payload(struct cbfs_file *fhdr) |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 434 | { |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 435 | if (!CONFIG_COREBOOT_FLASH || !fhdr) |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 436 | return; |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 437 | dprintf(1, "Run %s\n", fhdr->filename); |
| 438 | struct cbfs_payload *pay = (void*)fhdr + be32_to_cpu(fhdr->offset); |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 439 | struct cbfs_payload_segment *seg = pay->segments; |
| 440 | for (;;) { |
Kevin O'Connor | b306459 | 2012-08-14 21:20:10 -0400 | [diff] [blame] | 441 | void *src = (void*)pay + be32_to_cpu(seg->offset); |
| 442 | void *dest = (void*)(u32)be64_to_cpu(seg->load_addr); |
| 443 | u32 src_len = be32_to_cpu(seg->len); |
| 444 | u32 dest_len = be32_to_cpu(seg->mem_len); |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 445 | switch (seg->type) { |
| 446 | case PAYLOAD_SEGMENT_BSS: |
| 447 | dprintf(3, "BSS segment %d@%p\n", dest_len, dest); |
| 448 | memset(dest, 0, dest_len); |
| 449 | break; |
| 450 | case PAYLOAD_SEGMENT_ENTRY: { |
| 451 | dprintf(1, "Calling addr %p\n", dest); |
| 452 | void (*func)() = dest; |
| 453 | func(); |
| 454 | return; |
| 455 | } |
| 456 | default: |
| 457 | dprintf(3, "Segment %x %d@%p -> %d@%p\n" |
| 458 | , seg->type, src_len, src, dest_len, dest); |
Kevin O'Connor | b306459 | 2012-08-14 21:20:10 -0400 | [diff] [blame] | 459 | if (seg->compression == cpu_to_be32(CBFS_COMPRESS_NONE)) { |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 460 | if (src_len > dest_len) |
| 461 | src_len = dest_len; |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 462 | memcpy(dest, src, src_len); |
| 463 | } else if (CONFIG_LZMA |
Kevin O'Connor | b306459 | 2012-08-14 21:20:10 -0400 | [diff] [blame] | 464 | && seg->compression == cpu_to_be32(CBFS_COMPRESS_LZMA)) { |
Kevin O'Connor | cbd6ca0 | 2009-04-27 21:51:13 -0400 | [diff] [blame] | 465 | int ret = ulzma(dest, dest_len, src, src_len); |
Kevin O'Connor | 592323f | 2009-04-26 23:17:49 -0400 | [diff] [blame] | 466 | if (ret < 0) |
| 467 | return; |
| 468 | src_len = ret; |
| 469 | } else { |
| 470 | dprintf(1, "No support for compression type %x\n" |
| 471 | , seg->compression); |
| 472 | return; |
| 473 | } |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 474 | if (dest_len > src_len) |
| 475 | memset(dest + src_len, 0, dest_len - src_len); |
| 476 | break; |
| 477 | } |
| 478 | seg++; |
| 479 | } |
| 480 | } |
| 481 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 482 | // Register payloads in "img/" directory with boot system. |
Kevin O'Connor | 89a1efd | 2011-01-08 12:24:39 -0500 | [diff] [blame] | 483 | void |
| 484 | cbfs_payload_setup(void) |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 485 | { |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 486 | if (!CONFIG_COREBOOT_FLASH) |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 487 | return; |
| 488 | struct romfile_s *file = NULL; |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 489 | for (;;) { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 490 | file = romfile_findprefix("img/", file); |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 491 | if (!file) |
| 492 | break; |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 493 | struct cbfs_romfile_s *cfile; |
| 494 | cfile = container_of(file, struct cbfs_romfile_s, file); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 495 | const char *filename = file->name; |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 496 | char *desc = znprintf(MAXDESCSIZE, "Payload [%s]", &filename[4]); |
Kevin O'Connor | 4158c8c | 2013-03-30 09:12:11 -0400 | [diff] [blame] | 497 | boot_add_cbfs(cfile->fhdr, desc, bootprio_find_named_rom(filename, 0)); |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 498 | } |
| 499 | } |