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