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