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