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