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> |
Aaron Durbin | ebf142a | 2013-03-29 16:23:23 -0500 | [diff] [blame] | 23 | #include <cpu/cpu.h> |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 27 | #include <cbfs.h> |
Myles Watson | 5817078 | 2009-10-28 16:13:28 +0000 | [diff] [blame] | 28 | #include <lib.h> |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 29 | #include <bootmem.h> |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 30 | #include <payload_loader.h> |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 31 | |
Furquan Shaikh | 20f25dd | 2014-04-22 10:41:05 -0700 | [diff] [blame] | 32 | /* from ramstage.ld: */ |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 33 | extern unsigned char _ram_seg; |
| 34 | extern unsigned char _eram_seg; |
| 35 | |
Stefan Reinauer | 1943629 | 2011-11-07 12:43:03 -0800 | [diff] [blame] | 36 | static const unsigned long lb_start = (unsigned long)&_ram_seg; |
| 37 | static const unsigned long lb_end = (unsigned long)&_eram_seg; |
| 38 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 39 | struct segment { |
| 40 | struct segment *next; |
| 41 | struct segment *prev; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 42 | unsigned long s_dstaddr; |
| 43 | unsigned long s_srcaddr; |
| 44 | unsigned long s_memsz; |
| 45 | unsigned long s_filesz; |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 46 | int compression; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 49 | /* The problem: |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 50 | * Static executables all want to share the same addresses |
| 51 | * in memory because only a few addresses are reliably present on |
| 52 | * a machine, and implementing general relocation is hard. |
| 53 | * |
| 54 | * The solution: |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 55 | * - Allocate a buffer the size of the coreboot image plus additional |
| 56 | * required space. |
| 57 | * - Anything that would overwrite coreboot copy into the lower part of |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 58 | * the buffer. |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 59 | * - 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] | 60 | * - Then jump to the loaded image. |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 61 | * |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 62 | * Benefits: |
| 63 | * - Nearly arbitrary standalone executables can be loaded. |
| 64 | * - Coreboot is preserved, so it can be returned to. |
| 65 | * - The implementation is still relatively simple, |
Zheng Bao | ba49fb7 | 2009-11-01 09:18:23 +0000 | [diff] [blame] | 66 | * and much simpler than the general case implemented in kexec. |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 67 | */ |
| 68 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 69 | static unsigned long bounce_size, bounce_buffer; |
| 70 | |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 71 | static void get_bounce_buffer(unsigned long req_size) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 72 | { |
| 73 | unsigned long lb_size; |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 74 | void *buffer; |
Aaron Durbin | e58a24b | 2014-02-24 22:11:45 -0600 | [diff] [blame] | 75 | |
| 76 | /* When the ramstage is relocatable there is no need for a bounce |
| 77 | * buffer. All payloads should not overlap the ramstage. |
| 78 | */ |
| 79 | if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) { |
| 80 | bounce_buffer = ~0UL; |
| 81 | bounce_size = 0; |
| 82 | return; |
| 83 | } |
| 84 | |
Stefan Reinauer | 1943629 | 2011-11-07 12:43:03 -0800 | [diff] [blame] | 85 | lb_size = lb_end - lb_start; |
| 86 | /* Plus coreboot size so I have somewhere |
| 87 | * to place a copy to return to. |
| 88 | */ |
Myles Watson | 9202798 | 2009-09-23 20:32:21 +0000 | [diff] [blame] | 89 | lb_size = req_size + lb_size; |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 90 | |
| 91 | buffer = bootmem_allocate_buffer(lb_size); |
| 92 | |
| 93 | printk(BIOS_SPEW, "Bounce Buffer at %p, %lu bytes\n", buffer, lb_size); |
| 94 | |
| 95 | bounce_buffer = (uintptr_t)buffer; |
Myles Watson | 9202798 | 2009-09-23 20:32:21 +0000 | [diff] [blame] | 96 | bounce_size = req_size; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 99 | static int overlaps_coreboot(struct segment *seg) |
| 100 | { |
| 101 | unsigned long start, end; |
| 102 | start = seg->s_dstaddr; |
| 103 | end = start + seg->s_memsz; |
| 104 | return !((end <= lb_start) || (start >= lb_end)); |
| 105 | } |
| 106 | |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 107 | static int relocate_segment(unsigned long buffer, struct segment *seg) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 108 | { |
| 109 | /* Modify all segments that want to load onto coreboot |
| 110 | * to load onto the bounce buffer instead. |
| 111 | */ |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 112 | /* ret: 1 : A new segment is inserted before the seg. |
Stefan Reinauer | 1943629 | 2011-11-07 12:43:03 -0800 | [diff] [blame] | 113 | * 0 : A new segment is inserted after the seg, or no new one. |
| 114 | */ |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 115 | unsigned long start, middle, end, ret = 0; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 116 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 117 | printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n", |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 118 | lb_start, lb_end); |
| 119 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 120 | /* I don't conflict with coreboot so get out of here */ |
| 121 | if (!overlaps_coreboot(seg)) |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 122 | return 0; |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 123 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 124 | start = seg->s_dstaddr; |
| 125 | middle = start + seg->s_filesz; |
| 126 | end = start + seg->s_memsz; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 127 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 128 | printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n", |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 129 | start, middle, end); |
| 130 | |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 131 | if (seg->compression == CBFS_COMPRESS_NONE) { |
| 132 | /* Slice off a piece at the beginning |
| 133 | * that doesn't conflict with coreboot. |
| 134 | */ |
| 135 | if (start < lb_start) { |
| 136 | struct segment *new; |
| 137 | unsigned long len = lb_start - start; |
| 138 | new = malloc(sizeof(*new)); |
| 139 | *new = *seg; |
| 140 | new->s_memsz = len; |
| 141 | seg->s_memsz -= len; |
| 142 | seg->s_dstaddr += len; |
| 143 | seg->s_srcaddr += len; |
| 144 | if (seg->s_filesz > len) { |
| 145 | new->s_filesz = len; |
| 146 | seg->s_filesz -= len; |
| 147 | } else { |
| 148 | seg->s_filesz = 0; |
| 149 | } |
| 150 | |
| 151 | /* Order by stream offset */ |
| 152 | new->next = seg; |
| 153 | new->prev = seg->prev; |
| 154 | seg->prev->next = new; |
| 155 | seg->prev = new; |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 156 | |
| 157 | /* compute the new value of start */ |
| 158 | start = seg->s_dstaddr; |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 159 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 160 | printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n", |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 161 | new->s_dstaddr, |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 162 | new->s_dstaddr + new->s_filesz, |
| 163 | new->s_dstaddr + new->s_memsz); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 164 | |
| 165 | ret = 1; |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 166 | } |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 167 | |
| 168 | /* Slice off a piece at the end |
| 169 | * that doesn't conflict with coreboot |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 170 | */ |
| 171 | if (end > lb_end) { |
| 172 | unsigned long len = lb_end - start; |
| 173 | struct segment *new; |
| 174 | new = malloc(sizeof(*new)); |
| 175 | *new = *seg; |
| 176 | seg->s_memsz = len; |
| 177 | new->s_memsz -= len; |
| 178 | new->s_dstaddr += len; |
| 179 | new->s_srcaddr += len; |
| 180 | if (seg->s_filesz > len) { |
| 181 | seg->s_filesz = len; |
| 182 | new->s_filesz -= len; |
| 183 | } else { |
| 184 | new->s_filesz = 0; |
| 185 | } |
| 186 | /* Order by stream offset */ |
| 187 | new->next = seg->next; |
| 188 | new->prev = seg; |
| 189 | seg->next->prev = new; |
| 190 | seg->next = new; |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 191 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 192 | printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n", |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 193 | new->s_dstaddr, |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 194 | new->s_dstaddr + new->s_filesz, |
| 195 | new->s_dstaddr + new->s_memsz); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 196 | } |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 197 | } |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 198 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 199 | /* Now retarget this segment onto the bounce buffer */ |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 200 | /* 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] | 201 | * so you will make the dstaddr be this buffer, and it will get copied |
| 202 | * later to where coreboot lives. |
| 203 | */ |
| 204 | seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start); |
| 205 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 206 | printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n", |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 207 | seg->s_dstaddr, |
| 208 | seg->s_dstaddr + seg->s_filesz, |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 209 | seg->s_dstaddr + seg->s_memsz); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 210 | |
| 211 | return ret; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
| 215 | static int build_self_segment_list( |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 216 | struct segment *head, |
Aaron Durbin | 6086e63 | 2014-02-24 21:50:24 -0600 | [diff] [blame] | 217 | struct payload *payload, uintptr_t *entry) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 218 | { |
| 219 | struct segment *new; |
| 220 | struct segment *ptr; |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 221 | struct cbfs_payload_segment *segment, *first_segment; |
Aaron Durbin | 6086e63 | 2014-02-24 21:50:24 -0600 | [diff] [blame] | 222 | struct cbfs_payload *cbfs_payload; |
| 223 | cbfs_payload = payload->backing_store.data; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 224 | memset(head, 0, sizeof(*head)); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 225 | head->next = head->prev = head; |
Aaron Durbin | 6086e63 | 2014-02-24 21:50:24 -0600 | [diff] [blame] | 226 | first_segment = segment = &cbfs_payload->segments; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 227 | |
| 228 | while(1) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 229 | 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] | 230 | switch(segment->type) { |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 231 | case PAYLOAD_SEGMENT_PARAMS: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 232 | printk(BIOS_DEBUG, " parameter section (skipped)\n"); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 233 | segment++; |
| 234 | continue; |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 235 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 236 | case PAYLOAD_SEGMENT_CODE: |
| 237 | case PAYLOAD_SEGMENT_DATA: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 238 | printk(BIOS_DEBUG, " %s (compression=%x)\n", |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 239 | segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data", |
| 240 | ntohl(segment->compression)); |
| 241 | new = malloc(sizeof(*new)); |
Stefan Reinauer | 02e75b2 | 2011-10-17 09:51:15 -0700 | [diff] [blame] | 242 | new->s_dstaddr = ntohll(segment->load_addr); |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 243 | new->s_memsz = ntohl(segment->mem_len); |
| 244 | new->compression = ntohl(segment->compression); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 245 | |
Ronald G. Minnich | ef40209 | 2013-11-11 10:36:28 -0800 | [diff] [blame] | 246 | new->s_srcaddr = (uintptr_t) |
| 247 | ((unsigned char *)first_segment) |
| 248 | + ntohl(segment->offset); |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 249 | new->s_filesz = ntohl(segment->len); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 250 | 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] | 251 | new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz); |
| 252 | /* Clean up the values */ |
| 253 | if (new->s_filesz > new->s_memsz) { |
| 254 | new->s_filesz = new->s_memsz; |
| 255 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 256 | 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] | 257 | new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz); |
| 258 | break; |
| 259 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 260 | case PAYLOAD_SEGMENT_BSS: |
Stefan Reinauer | 02e75b2 | 2011-10-17 09:51:15 -0700 | [diff] [blame] | 261 | printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *) |
| 262 | (intptr_t)ntohll(segment->load_addr), |
| 263 | ntohl(segment->mem_len)); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 264 | new = malloc(sizeof(*new)); |
| 265 | new->s_filesz = 0; |
Edward O'Callaghan | 4bab582 | 2014-03-01 09:27:37 +1100 | [diff] [blame] | 266 | new->s_srcaddr = (uintptr_t) |
| 267 | ((unsigned char *)first_segment) |
| 268 | + ntohl(segment->offset); |
Stefan Reinauer | 02e75b2 | 2011-10-17 09:51:15 -0700 | [diff] [blame] | 269 | new->s_dstaddr = ntohll(segment->load_addr); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 270 | new->s_memsz = ntohl(segment->mem_len); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 271 | break; |
| 272 | |
| 273 | case PAYLOAD_SEGMENT_ENTRY: |
Hung-Te Lin | fdfd89f | 2013-02-27 16:38:38 +0800 | [diff] [blame] | 274 | printk(BIOS_DEBUG, " Entry Point 0x%p\n", |
| 275 | (void *)(intptr_t)ntohll(segment->load_addr)); |
Stefan Reinauer | 02e75b2 | 2011-10-17 09:51:15 -0700 | [diff] [blame] | 276 | *entry = ntohll(segment->load_addr); |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 277 | /* Per definition, a payload always has the entry point |
Martin Roth | cbf2bd7 | 2013-07-09 21:51:14 -0600 | [diff] [blame] | 278 | * as last segment. Thus, we use the occurrence of the |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 279 | * entry point as break condition for the loop. |
| 280 | * Can we actually just look at the number of section? |
| 281 | */ |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 282 | return 1; |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 283 | |
| 284 | default: |
| 285 | /* We found something that we don't know about. Throw |
| 286 | * hands into the sky and run away! |
| 287 | */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 288 | printk(BIOS_EMERG, "Bad segment type %x\n", segment->type); |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 289 | return -1; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 290 | } |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 291 | |
Stefan Reinauer | c44de49 | 2012-01-11 14:07:39 -0800 | [diff] [blame] | 292 | /* We have found another CODE, DATA or BSS segment */ |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 293 | segment++; |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 294 | |
Stefan Reinauer | c44de49 | 2012-01-11 14:07:39 -0800 | [diff] [blame] | 295 | /* Find place where to insert our segment */ |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 296 | for(ptr = head->next; ptr != head; ptr = ptr->next) { |
Stefan Reinauer | 02e75b2 | 2011-10-17 09:51:15 -0700 | [diff] [blame] | 297 | if (new->s_srcaddr < ntohll(segment->load_addr)) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 298 | break; |
| 299 | } |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 300 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 301 | /* Order by stream offset */ |
| 302 | new->next = ptr; |
| 303 | new->prev = ptr->prev; |
| 304 | ptr->prev->next = new; |
| 305 | ptr->prev = new; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 306 | } |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 307 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 308 | return 1; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static int load_self_segments( |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 312 | struct segment *head, |
Aaron Durbin | 6086e63 | 2014-02-24 21:50:24 -0600 | [diff] [blame] | 313 | struct payload *payload) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 314 | { |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 315 | struct segment *ptr; |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 316 | const unsigned long one_meg = (1UL << 20); |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 317 | unsigned long bounce_high = lb_end; |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 318 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 319 | for(ptr = head->next; ptr != head; ptr = ptr->next) { |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 320 | if (bootmem_region_targets_usable_ram(ptr->s_dstaddr, |
| 321 | ptr->s_memsz)) |
| 322 | continue; |
| 323 | |
| 324 | if (ptr->s_dstaddr < one_meg && |
| 325 | (ptr->s_dstaddr + ptr->s_memsz) <= one_meg) { |
| 326 | printk(BIOS_DEBUG, |
| 327 | "Payload being loaded below 1MiB " |
| 328 | "without region being marked as RAM usable.\n"); |
| 329 | continue; |
| 330 | } |
| 331 | |
| 332 | /* Payload segment not targeting RAM. */ |
| 333 | printk(BIOS_ERR, "SELF Payload doesn't target RAM:\n"); |
| 334 | printk(BIOS_ERR, "Failed Segment: 0x%lx, %lu bytes\n", |
| 335 | ptr->s_dstaddr, ptr->s_memsz); |
| 336 | bootmem_dump_ranges(); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | for(ptr = head->next; ptr != head; ptr = ptr->next) { |
| 341 | /* |
| 342 | * Add segments to bootmem memory map before a bounce buffer is |
| 343 | * allocated so that there aren't conflicts with the actual |
| 344 | * payload. |
| 345 | */ |
| 346 | bootmem_add_range(ptr->s_dstaddr, ptr->s_memsz, |
| 347 | LB_MEM_UNUSABLE); |
| 348 | |
Stefan Reinauer | c44de49 | 2012-01-11 14:07:39 -0800 | [diff] [blame] | 349 | if (!overlaps_coreboot(ptr)) |
| 350 | continue; |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 351 | if (ptr->s_dstaddr + ptr->s_memsz > bounce_high) |
| 352 | bounce_high = ptr->s_dstaddr + ptr->s_memsz; |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 353 | } |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 354 | get_bounce_buffer(bounce_high - lb_start); |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 355 | if (!bounce_buffer) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 356 | printk(BIOS_ERR, "Could not find a bounce buffer...\n"); |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 357 | return 0; |
| 358 | } |
Aaron Durbin | e58a24b | 2014-02-24 22:11:45 -0600 | [diff] [blame] | 359 | |
| 360 | /* Update the payload's bounce buffer data used when loading. */ |
| 361 | payload->bounce.data = (void *)(uintptr_t)bounce_buffer; |
| 362 | payload->bounce.size = bounce_size; |
| 363 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 364 | for(ptr = head->next; ptr != head; ptr = ptr->next) { |
Stefan Reinauer | f64893a | 2009-07-23 22:03:14 +0000 | [diff] [blame] | 365 | unsigned char *dest, *src; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 366 | 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] | 367 | ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 368 | |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 369 | /* Modify the segment to load onto the bounce_buffer if necessary. |
| 370 | */ |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 371 | if (relocate_segment(bounce_buffer, ptr)) { |
| 372 | ptr = (ptr->prev)->prev; |
| 373 | continue; |
| 374 | } |
Patrick Georgi | 5eceb32 | 2009-05-13 16:27:25 +0000 | [diff] [blame] | 375 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 376 | 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] | 377 | ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz); |
| 378 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 379 | /* Compute the boundaries of the segment */ |
| 380 | dest = (unsigned char *)(ptr->s_dstaddr); |
Myles Watson | fa12b67 | 2009-04-30 22:45:41 +0000 | [diff] [blame] | 381 | src = (unsigned char *)(ptr->s_srcaddr); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 382 | |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 383 | /* Copy data from the initial buffer */ |
| 384 | if (ptr->s_filesz) { |
Ronald G. Minnich | 671cedc | 2009-05-14 21:26:28 +0000 | [diff] [blame] | 385 | unsigned char *middle, *end; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 386 | size_t len; |
| 387 | len = ptr->s_filesz; |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 388 | switch(ptr->compression) { |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 389 | case CBFS_COMPRESS_LZMA: { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 390 | printk(BIOS_DEBUG, "using LZMA\n"); |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 391 | len = ulzma(src, dest); |
Myles Watson | 94de72b | 2010-06-01 15:19:25 +0000 | [diff] [blame] | 392 | if (!len) /* Decompression Error. */ |
| 393 | return 0; |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 394 | break; |
| 395 | } |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 396 | case CBFS_COMPRESS_NONE: { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 397 | printk(BIOS_DEBUG, "it's not compressed!\n"); |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 398 | memcpy(dest, src, len); |
| 399 | break; |
| 400 | } |
| 401 | default: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 402 | printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression); |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 403 | return -1; |
| 404 | } |
| 405 | end = dest + ptr->s_memsz; |
| 406 | middle = dest + len; |
Myles Watson | 94de72b | 2010-06-01 15:19:25 +0000 | [diff] [blame] | 407 | printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n", |
Patrick Georgi | 369bc78 | 2009-04-25 07:32:24 +0000 | [diff] [blame] | 408 | (unsigned long)dest, |
| 409 | (unsigned long)middle, |
| 410 | (unsigned long)end, |
| 411 | (unsigned long)src); |
Ronald G. Minnich | 671cedc | 2009-05-14 21:26:28 +0000 | [diff] [blame] | 412 | |
| 413 | /* Zero the extra bytes between middle & end */ |
| 414 | if (middle < end) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 415 | 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] | 416 | (unsigned long)middle, (unsigned long)(end - middle)); |
Zheng Bao | e6ad7fa | 2009-11-05 10:02:59 +0000 | [diff] [blame] | 417 | |
Ronald G. Minnich | 671cedc | 2009-05-14 21:26:28 +0000 | [diff] [blame] | 418 | /* Zero the extra bytes */ |
| 419 | memset(middle, 0, end - middle); |
| 420 | } |
Furquan Shaikh | 20f25dd | 2014-04-22 10:41:05 -0700 | [diff] [blame] | 421 | /* Copy the data that's outside the area that shadows ramstage */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 422 | 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] | 423 | if ((unsigned long)end > bounce_buffer) { |
| 424 | if ((unsigned long)dest < bounce_buffer) { |
Myles Watson | 7943fe6 | 2009-10-30 02:08:07 +0000 | [diff] [blame] | 425 | unsigned char *from = dest; |
| 426 | unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest)); |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 427 | unsigned long amount = bounce_buffer-(unsigned long)dest; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 428 | 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] | 429 | memcpy(to, from, amount); |
| 430 | } |
| 431 | if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) { |
| 432 | unsigned long from = bounce_buffer + (lb_end - lb_start); |
| 433 | unsigned long to = lb_end; |
Myles Watson | 7943fe6 | 2009-10-30 02:08:07 +0000 | [diff] [blame] | 434 | unsigned long amount = (unsigned long)end - from; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 435 | 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] | 436 | memcpy((char*)to, (char*)from, amount); |
Patrick Georgi | 26dd71c | 2009-09-30 21:36:38 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | return 1; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 444 | void *selfload(struct payload *payload) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 445 | { |
Ronald G. Minnich | ef40209 | 2013-11-11 10:36:28 -0800 | [diff] [blame] | 446 | uintptr_t entry = 0; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 447 | struct segment head; |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 448 | |
| 449 | /* Preprocess the self segments */ |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 450 | if (!build_self_segment_list(&head, payload, &entry)) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 451 | goto out; |
| 452 | |
| 453 | /* Load the segments */ |
Aaron Durbin | ceebc05 | 2014-02-25 00:21:10 -0600 | [diff] [blame] | 454 | if (!load_self_segments(&head, payload)) |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 455 | goto out; |
| 456 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 457 | printk(BIOS_SPEW, "Loaded segments\n"); |
Ronald G. Minnich | ae63126 | 2009-04-01 10:48:39 +0000 | [diff] [blame] | 458 | |
Aaron Durbin | 001de1a | 2013-04-24 22:59:45 -0500 | [diff] [blame] | 459 | return (void *)entry; |
| 460 | |
| 461 | out: |
| 462 | return NULL; |
| 463 | } |