ebiederm | c779889 | 2009-04-01 11:03:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2003 Eric W. Biederman <ebiederm@xmission.com> |
| 5 | * Copyright (C) 2009 Ron Minnich <rminnich@gmail.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 19 | */ |
| 20 | |
Stefan Reinauer | 1377491 | 2011-10-14 15:19:21 -0700 | [diff] [blame^] | 21 | #include <arch/byteorder.h> |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 22 | #include <console/console.h> |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 23 | #include <fallback.h> |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 24 | #include <boot/elf.h> |
| 25 | #include <boot/elf_boot.h> |
| 26 | #include <boot/coreboot_tables.h> |
| 27 | #include <ip_checksum.h> |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 28 | #include <stdint.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 31 | #include <cbfs.h> |
Myles Watson | 5817078 | 2009-10-28 16:13:28 +0000 | [diff] [blame] | 32 | #include <lib.h> |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 33 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 34 | /* Maximum physical address we can use for the coreboot bounce buffer. |
| 35 | */ |
| 36 | #ifndef MAX_ADDR |
| 37 | #define MAX_ADDR -1UL |
| 38 | #endif |
| 39 | |
| 40 | extern unsigned char _ram_seg; |
| 41 | extern unsigned char _eram_seg; |
| 42 | |
| 43 | struct segment { |
| 44 | struct segment *next; |
| 45 | struct segment *prev; |
| 46 | struct segment *phdr_next; |
| 47 | struct segment *phdr_prev; |
| 48 | unsigned long s_dstaddr; |
| 49 | unsigned long s_srcaddr; |
| 50 | unsigned long s_memsz; |
| 51 | unsigned long s_filesz; |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 52 | int compression; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct verify_callback { |
| 56 | struct verify_callback *next; |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 57 | int (*callback)(struct verify_callback *vcb, |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 58 | Elf_ehdr *ehdr, Elf_phdr *phdr, struct segment *head); |
| 59 | unsigned long desc_offset; |
| 60 | unsigned long desc_addr; |
| 61 | }; |
| 62 | |
| 63 | struct ip_checksum_vcb { |
| 64 | struct verify_callback data; |
| 65 | unsigned short ip_checksum; |
| 66 | }; |
| 67 | |
Myles Watson | 9202798 | 2009-09-23 20:32:21 +0000 | [diff] [blame] | 68 | static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload); |
| 69 | |
Patrick Georgi | d1185bf | 2009-05-26 14:00:49 +0000 | [diff] [blame] | 70 | void * cbfs_load_payload(struct lb_memory *lb_mem, const char *name) |
| 71 | { |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 72 | struct cbfs_payload *payload; |
Patrick Georgi | d1185bf | 2009-05-26 14:00:49 +0000 | [diff] [blame] | 73 | |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 74 | payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD); |
Patrick Georgi | d1185bf | 2009-05-26 14:00:49 +0000 | [diff] [blame] | 75 | if (payload == NULL) |
| 76 | return (void *) -1; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 77 | printk(BIOS_DEBUG, "Got a payload\n"); |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 78 | |
Patrick Georgi | d1185bf | 2009-05-26 14:00:49 +0000 | [diff] [blame] | 79 | selfboot(lb_mem, payload); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 80 | printk(BIOS_EMERG, "SELFBOOT RETURNED!\n"); |
Patrick Georgi | d1185bf | 2009-05-26 14:00:49 +0000 | [diff] [blame] | 81 | |
| 82 | return (void *) -1; |
| 83 | } |
| 84 | |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 85 | /* The problem: |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 86 | * Static executables all want to share the same addresses |
| 87 | * in memory because only a few addresses are reliably present on |
| 88 | * a machine, and implementing general relocation is hard. |
| 89 | * |
| 90 | * The solution: |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 91 | * - Allocate a buffer the size of the coreboot image plus additional |
| 92 | * required space. |
| 93 | * - Anything that would overwrite coreboot copy into the lower part of |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 94 | * the buffer. |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 95 | * - After loading an ELF image copy coreboot to the top of the buffer. |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 96 | * - Then jump to the loaded image. |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 97 | * |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 98 | * Benefits: |
| 99 | * - Nearly arbitrary standalone executables can be loaded. |
| 100 | * - Coreboot is preserved, so it can be returned to. |
| 101 | * - The implementation is still relatively simple, |
Zheng Bao | ba49fb7 | 2009-11-01 09:18:23 +0000 | [diff] [blame] | 102 | * and much simpler than the general case implemented in kexec. |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 103 | * |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 104 | */ |
| 105 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 106 | static unsigned long bounce_size, bounce_buffer; |
| 107 | |
Myles Watson | 9202798 | 2009-09-23 20:32:21 +0000 | [diff] [blame] | 108 | static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 109 | { |
| 110 | unsigned long lb_size; |
| 111 | unsigned long mem_entries; |
| 112 | unsigned long buffer; |
| 113 | int i; |
| 114 | lb_size = (unsigned long)(&_eram_seg - &_ram_seg); |
| 115 | /* Double coreboot size so I have somewhere to place a copy to return to */ |
Myles Watson | 9202798 | 2009-09-23 20:32:21 +0000 | [diff] [blame] | 116 | lb_size = req_size + lb_size; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 117 | mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]); |
| 118 | buffer = 0; |
| 119 | for(i = 0; i < mem_entries; i++) { |
| 120 | unsigned long mstart, mend; |
| 121 | unsigned long msize; |
| 122 | unsigned long tbuffer; |
| 123 | if (mem->map[i].type != LB_MEM_RAM) |
| 124 | continue; |
| 125 | if (unpack_lb64(mem->map[i].start) > MAX_ADDR) |
| 126 | continue; |
| 127 | if (unpack_lb64(mem->map[i].size) < lb_size) |
| 128 | continue; |
| 129 | mstart = unpack_lb64(mem->map[i].start); |
| 130 | msize = MAX_ADDR - mstart +1; |
| 131 | if (msize > unpack_lb64(mem->map[i].size)) |
| 132 | msize = unpack_lb64(mem->map[i].size); |
| 133 | mend = mstart + msize; |
| 134 | tbuffer = mend - lb_size; |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 135 | if (tbuffer < buffer) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 136 | continue; |
| 137 | buffer = tbuffer; |
| 138 | } |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 139 | bounce_buffer = buffer; |
Myles Watson | 9202798 | 2009-09-23 20:32:21 +0000 | [diff] [blame] | 140 | bounce_size = req_size; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static int valid_area(struct lb_memory *mem, unsigned long buffer, |
| 144 | unsigned long start, unsigned long len) |
| 145 | { |
| 146 | /* Check through all of the memory segments and ensure |
| 147 | * the segment that was passed in is completely contained |
| 148 | * in RAM. |
| 149 | */ |
| 150 | int i; |
| 151 | unsigned long end = start + len; |
| 152 | unsigned long mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]); |
| 153 | |
| 154 | /* See if I conflict with the bounce buffer */ |
| 155 | if (end >= buffer) { |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* Walk through the table of valid memory ranges and see if I |
| 160 | * have a match. |
| 161 | */ |
| 162 | for(i = 0; i < mem_entries; i++) { |
| 163 | uint64_t mstart, mend; |
| 164 | uint32_t mtype; |
| 165 | mtype = mem->map[i].type; |
| 166 | mstart = unpack_lb64(mem->map[i].start); |
| 167 | mend = mstart + unpack_lb64(mem->map[i].size); |
| 168 | if ((mtype == LB_MEM_RAM) && (start < mend) && (end > mstart)) { |
| 169 | break; |
| 170 | } |
| 171 | if ((mtype == LB_MEM_TABLE) && (start < mend) && (end > mstart)) { |
Stefan Reinauer | d6b4f1c | 2010-09-23 18:29:40 +0000 | [diff] [blame] | 172 | printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n"); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | } |
| 176 | if (i == mem_entries) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 177 | printk(BIOS_ERR, "No matching ram area found for range:\n"); |
| 178 | printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end); |
| 179 | printk(BIOS_ERR, "Ram areas\n"); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 180 | for(i = 0; i < mem_entries; i++) { |
| 181 | uint64_t mstart, mend; |
| 182 | uint32_t mtype; |
| 183 | mtype = mem->map[i].type; |
| 184 | mstart = unpack_lb64(mem->map[i].start); |
| 185 | mend = mstart + unpack_lb64(mem->map[i].size); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 186 | printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n", |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 187 | (unsigned long)mstart, |
| 188 | (unsigned long)mend, |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 189 | (mtype == LB_MEM_RAM)?"RAM":"Reserved"); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 190 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | return 1; |
| 195 | } |
| 196 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 197 | static const unsigned long lb_start = (unsigned long)&_ram_seg; |
| 198 | static const unsigned long lb_end = (unsigned long)&_eram_seg; |
| 199 | |
| 200 | static int overlaps_coreboot(struct segment *seg) |
| 201 | { |
| 202 | unsigned long start, end; |
| 203 | start = seg->s_dstaddr; |
| 204 | end = start + seg->s_memsz; |
| 205 | return !((end <= lb_start) || (start >= lb_end)); |
| 206 | } |
| 207 | |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 208 | static int relocate_segment(unsigned long buffer, struct segment *seg) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 209 | { |
| 210 | /* Modify all segments that want to load onto coreboot |
| 211 | * to load onto the bounce buffer instead. |
| 212 | */ |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 213 | /* ret: 1 : A new segment is inserted before the seg. |
| 214 | * 0 : A new segment is inserted after the seg, or no new one. */ |
| 215 | unsigned long start, middle, end, ret = 0; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 216 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 217 | printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n", |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 218 | lb_start, lb_end); |
| 219 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 220 | /* I don't conflict with coreboot so get out of here */ |
| 221 | if (!overlaps_coreboot(seg)) |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 222 | return 0; |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 223 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 224 | start = seg->s_dstaddr; |
| 225 | middle = start + seg->s_filesz; |
| 226 | end = start + seg->s_memsz; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 227 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 228 | printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n", |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 229 | start, middle, end); |
| 230 | |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 231 | if (seg->compression == CBFS_COMPRESS_NONE) { |
| 232 | /* Slice off a piece at the beginning |
| 233 | * that doesn't conflict with coreboot. |
| 234 | */ |
| 235 | if (start < lb_start) { |
| 236 | struct segment *new; |
| 237 | unsigned long len = lb_start - start; |
| 238 | new = malloc(sizeof(*new)); |
| 239 | *new = *seg; |
| 240 | new->s_memsz = len; |
| 241 | seg->s_memsz -= len; |
| 242 | seg->s_dstaddr += len; |
| 243 | seg->s_srcaddr += len; |
| 244 | if (seg->s_filesz > len) { |
| 245 | new->s_filesz = len; |
| 246 | seg->s_filesz -= len; |
| 247 | } else { |
| 248 | seg->s_filesz = 0; |
| 249 | } |
| 250 | |
| 251 | /* Order by stream offset */ |
| 252 | new->next = seg; |
| 253 | new->prev = seg->prev; |
| 254 | seg->prev->next = new; |
| 255 | seg->prev = new; |
| 256 | /* Order by original program header order */ |
| 257 | new->phdr_next = seg; |
| 258 | new->phdr_prev = seg->phdr_prev; |
| 259 | seg->phdr_prev->phdr_next = new; |
| 260 | seg->phdr_prev = new; |
| 261 | |
| 262 | /* compute the new value of start */ |
| 263 | start = seg->s_dstaddr; |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 264 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 265 | printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n", |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 266 | new->s_dstaddr, |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 267 | new->s_dstaddr + new->s_filesz, |
| 268 | new->s_dstaddr + new->s_memsz); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 269 | |
| 270 | ret = 1; |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 271 | } |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 272 | |
| 273 | /* Slice off a piece at the end |
| 274 | * that doesn't conflict with coreboot |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 275 | */ |
| 276 | if (end > lb_end) { |
| 277 | unsigned long len = lb_end - start; |
| 278 | struct segment *new; |
| 279 | new = malloc(sizeof(*new)); |
| 280 | *new = *seg; |
| 281 | seg->s_memsz = len; |
| 282 | new->s_memsz -= len; |
| 283 | new->s_dstaddr += len; |
| 284 | new->s_srcaddr += len; |
| 285 | if (seg->s_filesz > len) { |
| 286 | seg->s_filesz = len; |
| 287 | new->s_filesz -= len; |
| 288 | } else { |
| 289 | new->s_filesz = 0; |
| 290 | } |
| 291 | /* Order by stream offset */ |
| 292 | new->next = seg->next; |
| 293 | new->prev = seg; |
| 294 | seg->next->prev = new; |
| 295 | seg->next = new; |
| 296 | /* Order by original program header order */ |
| 297 | new->phdr_next = seg->phdr_next; |
| 298 | new->phdr_prev = seg; |
| 299 | seg->phdr_next->phdr_prev = new; |
| 300 | seg->phdr_next = new; |
| 301 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 302 | printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n", |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 303 | new->s_dstaddr, |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 304 | new->s_dstaddr + new->s_filesz, |
| 305 | new->s_dstaddr + new->s_memsz); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 306 | } |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 307 | } |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 308 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 309 | /* Now retarget this segment onto the bounce buffer */ |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 310 | /* sort of explanation: the buffer is a 1:1 mapping to coreboot. |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 311 | * so you will make the dstaddr be this buffer, and it will get copied |
| 312 | * later to where coreboot lives. |
| 313 | */ |
| 314 | seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start); |
| 315 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 316 | printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n", |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 317 | seg->s_dstaddr, |
| 318 | seg->s_dstaddr + seg->s_filesz, |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 319 | seg->s_dstaddr + seg->s_memsz); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 320 | |
| 321 | return ret; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
| 325 | static int build_self_segment_list( |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 326 | struct segment *head, |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 327 | struct lb_memory *mem, |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 328 | struct cbfs_payload *payload, u32 *entry) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 329 | { |
| 330 | struct segment *new; |
| 331 | struct segment *ptr; |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 332 | struct cbfs_payload_segment *segment, *first_segment; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 333 | memset(head, 0, sizeof(*head)); |
| 334 | head->phdr_next = head->phdr_prev = head; |
| 335 | head->next = head->prev = head; |
| 336 | first_segment = segment = &payload->segments; |
| 337 | |
| 338 | while(1) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 339 | printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 340 | switch(segment->type) { |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 341 | case PAYLOAD_SEGMENT_PARAMS: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 342 | printk(BIOS_DEBUG, " parameter section (skipped)\n"); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 343 | segment++; |
| 344 | continue; |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 345 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 346 | case PAYLOAD_SEGMENT_CODE: |
| 347 | case PAYLOAD_SEGMENT_DATA: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 348 | printk(BIOS_DEBUG, " %s (compression=%x)\n", |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 349 | segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data", |
| 350 | ntohl(segment->compression)); |
| 351 | new = malloc(sizeof(*new)); |
| 352 | new->s_dstaddr = ntohl((u32) segment->load_addr); |
| 353 | new->s_memsz = ntohl(segment->mem_len); |
| 354 | new->compression = ntohl(segment->compression); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 355 | |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 356 | new->s_srcaddr = (u32) ((unsigned char *) first_segment) + ntohl(segment->offset); |
| 357 | new->s_filesz = ntohl(segment->len); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 358 | printk(BIOS_DEBUG, " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n", |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 359 | new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz); |
| 360 | /* Clean up the values */ |
| 361 | if (new->s_filesz > new->s_memsz) { |
| 362 | new->s_filesz = new->s_memsz; |
| 363 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 364 | printk(BIOS_DEBUG, " (cleaned up) New segment addr 0x%lx size 0x%lx offset 0x%lx filesize 0x%lx\n", |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 365 | new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz); |
| 366 | break; |
| 367 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 368 | case PAYLOAD_SEGMENT_BSS: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 369 | printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *) ntohl((u32) segment->load_addr), |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 370 | ntohl(segment->mem_len)); |
| 371 | new = malloc(sizeof(*new)); |
| 372 | new->s_filesz = 0; |
| 373 | new->s_dstaddr = ntohl((u32) segment->load_addr); |
| 374 | new->s_memsz = ntohl(segment->mem_len); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 375 | break; |
| 376 | |
| 377 | case PAYLOAD_SEGMENT_ENTRY: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 378 | printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr)); |
Myles Watson | fa12b67 | 2009-04-30 22:45:41 +0000 | [diff] [blame] | 379 | *entry = ntohl((u32) segment->load_addr); |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 380 | /* Per definition, a payload always has the entry point |
| 381 | * as last segment. Thus, we use the occurence of the |
| 382 | * entry point as break condition for the loop. |
| 383 | * Can we actually just look at the number of section? |
| 384 | */ |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 385 | return 1; |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 386 | |
| 387 | default: |
| 388 | /* We found something that we don't know about. Throw |
| 389 | * hands into the sky and run away! |
| 390 | */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 391 | printk(BIOS_EMERG, "Bad segment type %x\n", segment->type); |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 392 | return -1; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 393 | } |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 394 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 395 | segment++; |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 396 | |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 397 | // FIXME: Explain what this is |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 398 | for(ptr = head->next; ptr != head; ptr = ptr->next) { |
| 399 | if (new->s_srcaddr < ntohl((u32) segment->load_addr)) |
| 400 | break; |
| 401 | } |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 402 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 403 | /* Order by stream offset */ |
| 404 | new->next = ptr; |
| 405 | new->prev = ptr->prev; |
| 406 | ptr->prev->next = new; |
| 407 | ptr->prev = new; |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 408 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 409 | /* Order by original program header order */ |
| 410 | new->phdr_next = head; |
| 411 | new->phdr_prev = head->phdr_prev; |
| 412 | head->phdr_prev->phdr_next = new; |
| 413 | head->phdr_prev = new; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 414 | } |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 415 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 416 | return 1; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static int load_self_segments( |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 420 | struct segment *head, |
| 421 | struct lb_memory *mem, |
| 422 | struct cbfs_payload *payload) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 423 | { |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 424 | struct segment *ptr; |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 425 | |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 426 | unsigned long bounce_high = lb_end; |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 427 | for(ptr = head->next; ptr != head; ptr = ptr->next) { |
| 428 | if (!overlaps_coreboot(ptr)) continue; |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 429 | if (ptr->s_dstaddr + ptr->s_memsz > bounce_high) |
| 430 | bounce_high = ptr->s_dstaddr + ptr->s_memsz; |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 431 | } |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 432 | get_bounce_buffer(mem, bounce_high - lb_start); |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 433 | if (!bounce_buffer) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 434 | printk(BIOS_ERR, "Could not find a bounce buffer...\n"); |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | for(ptr = head->next; ptr != head; ptr = ptr->next) { |
| 438 | /* Verify the memory addresses in the segment are valid */ |
| 439 | if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz)) |
| 440 | return 0; |
| 441 | } |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 442 | for(ptr = head->next; ptr != head; ptr = ptr->next) { |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 443 | unsigned char *dest, *src; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 444 | printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n", |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 445 | ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 446 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 447 | /* Modify the segment to load onto the bounce_buffer if necessary. |
| 448 | */ |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 449 | if (relocate_segment(bounce_buffer, ptr)) { |
| 450 | ptr = (ptr->prev)->prev; |
| 451 | continue; |
| 452 | } |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 453 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 454 | printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n", |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 455 | ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz); |
| 456 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 457 | /* Compute the boundaries of the segment */ |
| 458 | dest = (unsigned char *)(ptr->s_dstaddr); |
Myles Watson | fa12b67 | 2009-04-30 22:45:41 +0000 | [diff] [blame] | 459 | src = (unsigned char *)(ptr->s_srcaddr); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 460 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 461 | /* Copy data from the initial buffer */ |
| 462 | if (ptr->s_filesz) { |
Ronald G. Minnich | 671cedc | 2009-05-14 21:26:28 +0000 | [diff] [blame] | 463 | unsigned char *middle, *end; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 464 | size_t len; |
| 465 | len = ptr->s_filesz; |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 466 | switch(ptr->compression) { |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 467 | case CBFS_COMPRESS_LZMA: { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 468 | printk(BIOS_DEBUG, "using LZMA\n"); |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 469 | len = ulzma(src, dest); |
Myles Watson | 94de72b | 2010-06-01 15:19:25 +0000 | [diff] [blame] | 470 | if (!len) /* Decompression Error. */ |
| 471 | return 0; |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 472 | break; |
| 473 | } |
Stefan Reinauer | 3e4fb9d | 2011-04-21 21:26:58 +0000 | [diff] [blame] | 474 | #if CONFIG_COMPRESSED_PAYLOAD_NRV2B |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 475 | case CBFS_COMPRESS_NRV2B: { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 476 | printk(BIOS_DEBUG, "using NRV2B\n"); |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 477 | unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p); |
| 478 | unsigned long tmp; |
| 479 | len = unrv2b(src, dest, &tmp); |
| 480 | break; |
| 481 | } |
| 482 | #endif |
| 483 | case CBFS_COMPRESS_NONE: { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 484 | printk(BIOS_DEBUG, "it's not compressed!\n"); |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 485 | memcpy(dest, src, len); |
| 486 | break; |
| 487 | } |
| 488 | default: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 489 | printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression); |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 490 | return -1; |
| 491 | } |
| 492 | end = dest + ptr->s_memsz; |
| 493 | middle = dest + len; |
Myles Watson | 94de72b | 2010-06-01 15:19:25 +0000 | [diff] [blame] | 494 | printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n", |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 495 | (unsigned long)dest, |
| 496 | (unsigned long)middle, |
| 497 | (unsigned long)end, |
| 498 | (unsigned long)src); |
Ronald G. Minnich | 671cedc | 2009-05-14 21:26:28 +0000 | [diff] [blame] | 499 | |
| 500 | /* Zero the extra bytes between middle & end */ |
| 501 | if (middle < end) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 502 | printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n", |
Ronald G. Minnich | 671cedc | 2009-05-14 21:26:28 +0000 | [diff] [blame] | 503 | (unsigned long)middle, (unsigned long)(end - middle)); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 504 | |
Ronald G. Minnich | 671cedc | 2009-05-14 21:26:28 +0000 | [diff] [blame] | 505 | /* Zero the extra bytes */ |
| 506 | memset(middle, 0, end - middle); |
| 507 | } |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 508 | /* Copy the data that's outside the area that shadows coreboot_ram */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 509 | printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer); |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 510 | if ((unsigned long)end > bounce_buffer) { |
| 511 | if ((unsigned long)dest < bounce_buffer) { |
Myles Watson | 7943fe6 | 2009-10-30 02:08:07 +0000 | [diff] [blame] | 512 | unsigned char *from = dest; |
| 513 | unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest)); |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 514 | unsigned long amount = bounce_buffer-(unsigned long)dest; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 515 | printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount); |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 516 | memcpy(to, from, amount); |
| 517 | } |
| 518 | if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) { |
| 519 | unsigned long from = bounce_buffer + (lb_end - lb_start); |
| 520 | unsigned long to = lb_end; |
Myles Watson | 7943fe6 | 2009-10-30 02:08:07 +0000 | [diff] [blame] | 521 | unsigned long amount = (unsigned long)end - from; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 522 | printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount); |
Myles Watson | 7943fe6 | 2009-10-30 02:08:07 +0000 | [diff] [blame] | 523 | memcpy((char*)to, (char*)from, amount); |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | return 1; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Myles Watson | 9202798 | 2009-09-23 20:32:21 +0000 | [diff] [blame] | 531 | static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 532 | { |
Myles Watson | fa12b67 | 2009-04-30 22:45:41 +0000 | [diff] [blame] | 533 | u32 entry=0; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 534 | struct segment head; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 535 | |
| 536 | /* Preprocess the self segments */ |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 537 | if (!build_self_segment_list(&head, mem, payload, &entry)) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 538 | goto out; |
| 539 | |
| 540 | /* Load the segments */ |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 541 | if (!load_self_segments(&head, mem, payload)) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 542 | goto out; |
| 543 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 544 | printk(BIOS_SPEW, "Loaded segments\n"); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 545 | |
| 546 | /* Reset to booting from this image as late as possible */ |
| 547 | boot_successful(); |
| 548 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 549 | printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry); |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 550 | post_code(POST_ENTER_ELF_BOOT); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 551 | |
| 552 | /* Jump to kernel */ |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 553 | jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 554 | return 1; |
| 555 | |
| 556 | out: |
| 557 | return 0; |
| 558 | } |
| 559 | |