blob: d73b3f56af5fe1c0c0592bbc26eb658db63f0889 [file] [log] [blame]
ebiedermc7798892009-04-01 11:03:32 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2003 Eric W. Biederman <ebiederm@xmission.com>
5 * Copyright (C) 2009 Ron Minnich <rminnich@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
19 */
20
Stefan Reinauer13774912011-10-14 15:19:21 -070021#include <arch/byteorder.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000022#include <console/console.h>
Aaron Durbinebf142a2013-03-29 16:23:23 -050023#include <cpu/cpu.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000024#include <stdint.h>
25#include <stdlib.h>
26#include <string.h>
Julius Wernerec5e5e02014-08-20 15:29:56 -070027#include <symbols.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000028#include <cbfs.h>
Myles Watson58170782009-10-28 16:13:28 +000029#include <lib.h>
Aaron Durbinceebc052014-02-25 00:21:10 -060030#include <bootmem.h>
Aaron Durbin04654a22015-03-17 11:43:44 -050031#include <program_loading.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000032
Julius Wernerec5e5e02014-08-20 15:29:56 -070033static const unsigned long lb_start = (unsigned long)&_program;
34static const unsigned long lb_end = (unsigned long)&_eprogram;
Stefan Reinauer19436292011-11-07 12:43:03 -080035
Ronald G. Minnichae631262009-04-01 10:48:39 +000036struct segment {
37 struct segment *next;
38 struct segment *prev;
Ronald G. Minnichae631262009-04-01 10:48:39 +000039 unsigned long s_dstaddr;
40 unsigned long s_srcaddr;
41 unsigned long s_memsz;
42 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000043 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000044};
45
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000046/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000047 * Static executables all want to share the same addresses
48 * in memory because only a few addresses are reliably present on
49 * a machine, and implementing general relocation is hard.
50 *
51 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000052 * - Allocate a buffer the size of the coreboot image plus additional
53 * required space.
54 * - Anything that would overwrite coreboot copy into the lower part of
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000055 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000056 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000057 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000058 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000059 * Benefits:
60 * - Nearly arbitrary standalone executables can be loaded.
61 * - Coreboot is preserved, so it can be returned to.
62 * - The implementation is still relatively simple,
Zheng Baoba49fb72009-11-01 09:18:23 +000063 * and much simpler than the general case implemented in kexec.
Ronald G. Minnichae631262009-04-01 10:48:39 +000064 */
65
Patrick Georgi5eceb322009-05-13 16:27:25 +000066static unsigned long bounce_size, bounce_buffer;
67
Aaron Durbinceebc052014-02-25 00:21:10 -060068static void get_bounce_buffer(unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +000069{
70 unsigned long lb_size;
Aaron Durbinceebc052014-02-25 00:21:10 -060071 void *buffer;
Aaron Durbine58a24b2014-02-24 22:11:45 -060072
73 /* When the ramstage is relocatable there is no need for a bounce
74 * buffer. All payloads should not overlap the ramstage.
75 */
76 if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
77 bounce_buffer = ~0UL;
78 bounce_size = 0;
79 return;
80 }
81
Stefan Reinauer19436292011-11-07 12:43:03 -080082 lb_size = lb_end - lb_start;
83 /* Plus coreboot size so I have somewhere
84 * to place a copy to return to.
85 */
Myles Watson92027982009-09-23 20:32:21 +000086 lb_size = req_size + lb_size;
Aaron Durbinceebc052014-02-25 00:21:10 -060087
88 buffer = bootmem_allocate_buffer(lb_size);
89
90 printk(BIOS_SPEW, "Bounce Buffer at %p, %lu bytes\n", buffer, lb_size);
91
92 bounce_buffer = (uintptr_t)buffer;
Myles Watson92027982009-09-23 20:32:21 +000093 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +000094}
95
Patrick Georgi5eceb322009-05-13 16:27:25 +000096static int overlaps_coreboot(struct segment *seg)
97{
98 unsigned long start, end;
99 start = seg->s_dstaddr;
100 end = start + seg->s_memsz;
101 return !((end <= lb_start) || (start >= lb_end));
102}
103
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000104static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000105{
106 /* Modify all segments that want to load onto coreboot
107 * to load onto the bounce buffer instead.
108 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000109 /* ret: 1 : A new segment is inserted before the seg.
Stefan Reinauer19436292011-11-07 12:43:03 -0800110 * 0 : A new segment is inserted after the seg, or no new one.
111 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000112 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000113
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000114 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000115 lb_start, lb_end);
116
Patrick Georgi5eceb322009-05-13 16:27:25 +0000117 /* I don't conflict with coreboot so get out of here */
118 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000119 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000120
Ronald G. Minnichae631262009-04-01 10:48:39 +0000121 start = seg->s_dstaddr;
122 middle = start + seg->s_filesz;
123 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000124
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000125 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000126 start, middle, end);
127
Patrick Georgi369bc782009-04-25 07:32:24 +0000128 if (seg->compression == CBFS_COMPRESS_NONE) {
129 /* Slice off a piece at the beginning
130 * that doesn't conflict with coreboot.
131 */
132 if (start < lb_start) {
133 struct segment *new;
134 unsigned long len = lb_start - start;
135 new = malloc(sizeof(*new));
136 *new = *seg;
137 new->s_memsz = len;
138 seg->s_memsz -= len;
139 seg->s_dstaddr += len;
140 seg->s_srcaddr += len;
141 if (seg->s_filesz > len) {
142 new->s_filesz = len;
143 seg->s_filesz -= len;
144 } else {
145 seg->s_filesz = 0;
146 }
147
148 /* Order by stream offset */
149 new->next = seg;
150 new->prev = seg->prev;
151 seg->prev->next = new;
152 seg->prev = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000153
154 /* compute the new value of start */
155 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000156
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000157 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000158 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000159 new->s_dstaddr + new->s_filesz,
160 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000161
162 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000163 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000164
165 /* Slice off a piece at the end
166 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000167 */
168 if (end > lb_end) {
169 unsigned long len = lb_end - start;
170 struct segment *new;
171 new = malloc(sizeof(*new));
172 *new = *seg;
173 seg->s_memsz = len;
174 new->s_memsz -= len;
175 new->s_dstaddr += len;
176 new->s_srcaddr += len;
177 if (seg->s_filesz > len) {
178 seg->s_filesz = len;
179 new->s_filesz -= len;
180 } else {
181 new->s_filesz = 0;
182 }
183 /* Order by stream offset */
184 new->next = seg->next;
185 new->prev = seg;
186 seg->next->prev = new;
187 seg->next = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000188
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000189 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000190 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000191 new->s_dstaddr + new->s_filesz,
192 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000193 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000194 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000195
Ronald G. Minnichae631262009-04-01 10:48:39 +0000196 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000197 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000198 * so you will make the dstaddr be this buffer, and it will get copied
199 * later to where coreboot lives.
200 */
201 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
202
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000203 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000204 seg->s_dstaddr,
205 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000206 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000207
208 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000209}
210
211
212static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000213 struct segment *head,
Aaron Durbince9efe02015-03-20 16:37:12 -0500214 struct prog *payload, uintptr_t *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000215{
216 struct segment *new;
217 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000218 struct cbfs_payload_segment *segment, *first_segment;
Aaron Durbin6086e632014-02-24 21:50:24 -0600219 struct cbfs_payload *cbfs_payload;
Aaron Durbince9efe02015-03-20 16:37:12 -0500220 cbfs_payload = prog_start(payload);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000221 memset(head, 0, sizeof(*head));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000222 head->next = head->prev = head;
Aaron Durbin6086e632014-02-24 21:50:24 -0600223 first_segment = segment = &cbfs_payload->segments;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000224
225 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000226 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000227 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000228 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000229 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000230 segment++;
231 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000232
Ronald G. Minnichae631262009-04-01 10:48:39 +0000233 case PAYLOAD_SEGMENT_CODE:
234 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000235 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000236 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
237 ntohl(segment->compression));
238 new = malloc(sizeof(*new));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700239 new->s_dstaddr = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000240 new->s_memsz = ntohl(segment->mem_len);
241 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000242
Ronald G. Minnichef402092013-11-11 10:36:28 -0800243 new->s_srcaddr = (uintptr_t)
244 ((unsigned char *)first_segment)
245 + ntohl(segment->offset);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000246 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000247 printk(BIOS_DEBUG, " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000248 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
249 /* Clean up the values */
250 if (new->s_filesz > new->s_memsz) {
251 new->s_filesz = new->s_memsz;
Vadim Bendebury9e208bc2014-05-13 15:43:58 -0700252 printk(BIOS_DEBUG,
253 " cleaned up filesize 0x%lx\n",
254 new->s_filesz);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000255 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000256 break;
257
Ronald G. Minnichae631262009-04-01 10:48:39 +0000258 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700259 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
260 (intptr_t)ntohll(segment->load_addr),
261 ntohl(segment->mem_len));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000262 new = malloc(sizeof(*new));
263 new->s_filesz = 0;
Edward O'Callaghan4bab5822014-03-01 09:27:37 +1100264 new->s_srcaddr = (uintptr_t)
265 ((unsigned char *)first_segment)
266 + ntohl(segment->offset);
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700267 new->s_dstaddr = ntohll(segment->load_addr);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000268 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000269 break;
270
271 case PAYLOAD_SEGMENT_ENTRY:
Hung-Te Linfdfd89f2013-02-27 16:38:38 +0800272 printk(BIOS_DEBUG, " Entry Point 0x%p\n",
273 (void *)(intptr_t)ntohll(segment->load_addr));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700274 *entry = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000275 /* Per definition, a payload always has the entry point
Martin Rothcbf2bd72013-07-09 21:51:14 -0600276 * as last segment. Thus, we use the occurrence of the
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000277 * entry point as break condition for the loop.
278 * Can we actually just look at the number of section?
279 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000280 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000281
282 default:
283 /* We found something that we don't know about. Throw
284 * hands into the sky and run away!
285 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000286 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000287 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000288 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000289
Stefan Reinauerc44de492012-01-11 14:07:39 -0800290 /* We have found another CODE, DATA or BSS segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000291 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000292
Stefan Reinauerc44de492012-01-11 14:07:39 -0800293 /* Find place where to insert our segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000294 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700295 if (new->s_srcaddr < ntohll(segment->load_addr))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000296 break;
297 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000298
Ronald G. Minnichae631262009-04-01 10:48:39 +0000299 /* Order by stream offset */
300 new->next = ptr;
301 new->prev = ptr->prev;
302 ptr->prev->next = new;
303 ptr->prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000304 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000305
Ronald G. Minnichae631262009-04-01 10:48:39 +0000306 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000307}
308
309static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000310 struct segment *head,
Aaron Durbince9efe02015-03-20 16:37:12 -0500311 struct prog *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000312{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000313 struct segment *ptr;
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500314 struct segment *last_non_empty;
Aaron Durbinceebc052014-02-25 00:21:10 -0600315 const unsigned long one_meg = (1UL << 20);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000316 unsigned long bounce_high = lb_end;
Aaron Durbinceebc052014-02-25 00:21:10 -0600317
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500318 /* Determine last non-empty loaded segment. */
319 last_non_empty = NULL;
320 for(ptr = head->next; ptr != head; ptr = ptr->next)
321 if (ptr->s_filesz != 0)
322 last_non_empty = ptr;
323
Patrick Georgi5eceb322009-05-13 16:27:25 +0000324 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Aaron Durbinceebc052014-02-25 00:21:10 -0600325 if (bootmem_region_targets_usable_ram(ptr->s_dstaddr,
326 ptr->s_memsz))
327 continue;
328
329 if (ptr->s_dstaddr < one_meg &&
330 (ptr->s_dstaddr + ptr->s_memsz) <= one_meg) {
331 printk(BIOS_DEBUG,
332 "Payload being loaded below 1MiB "
333 "without region being marked as RAM usable.\n");
334 continue;
335 }
336
337 /* Payload segment not targeting RAM. */
338 printk(BIOS_ERR, "SELF Payload doesn't target RAM:\n");
339 printk(BIOS_ERR, "Failed Segment: 0x%lx, %lu bytes\n",
340 ptr->s_dstaddr, ptr->s_memsz);
341 bootmem_dump_ranges();
342 return 0;
343 }
344
345 for(ptr = head->next; ptr != head; ptr = ptr->next) {
346 /*
347 * Add segments to bootmem memory map before a bounce buffer is
348 * allocated so that there aren't conflicts with the actual
349 * payload.
350 */
351 bootmem_add_range(ptr->s_dstaddr, ptr->s_memsz,
352 LB_MEM_UNUSABLE);
353
Stefan Reinauerc44de492012-01-11 14:07:39 -0800354 if (!overlaps_coreboot(ptr))
355 continue;
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000356 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
357 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000358 }
Aaron Durbinceebc052014-02-25 00:21:10 -0600359 get_bounce_buffer(bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000360 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000361 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000362 return 0;
363 }
Aaron Durbine58a24b2014-02-24 22:11:45 -0600364
Patrick Georgi5eceb322009-05-13 16:27:25 +0000365 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000366 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000367 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000368 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000369
Patrick Georgi5eceb322009-05-13 16:27:25 +0000370 /* Modify the segment to load onto the bounce_buffer if necessary.
371 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000372 if (relocate_segment(bounce_buffer, ptr)) {
373 ptr = (ptr->prev)->prev;
374 continue;
375 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000376
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000377 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000378 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
379
Ronald G. Minnichae631262009-04-01 10:48:39 +0000380 /* Compute the boundaries of the segment */
381 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000382 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000383
Ronald G. Minnichae631262009-04-01 10:48:39 +0000384 /* Copy data from the initial buffer */
385 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000386 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000387 size_t len;
388 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000389 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000390 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000391 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000392 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000393 if (!len) /* Decompression Error. */
394 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000395 break;
396 }
Patrick Georgi369bc782009-04-25 07:32:24 +0000397 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000398 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000399 memcpy(dest, src, len);
400 break;
401 }
402 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000403 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000404 return -1;
405 }
406 end = dest + ptr->s_memsz;
407 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000408 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000409 (unsigned long)dest,
410 (unsigned long)middle,
411 (unsigned long)end,
412 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000413
414 /* Zero the extra bytes between middle & end */
415 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000416 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000417 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000418
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000419 /* Zero the extra bytes */
420 memset(middle, 0, end - middle);
421 }
Furquan Shaikh20f25dd2014-04-22 10:41:05 -0700422 /* Copy the data that's outside the area that shadows ramstage */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000423 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000424 if ((unsigned long)end > bounce_buffer) {
425 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000426 unsigned char *from = dest;
427 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000428 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000429 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000430 memcpy(to, from, amount);
431 }
432 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
433 unsigned long from = bounce_buffer + (lb_end - lb_start);
434 unsigned long to = lb_end;
Myles Watson7943fe62009-10-30 02:08:07 +0000435 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000436 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000437 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000438 }
439 }
Ionela Voinescu00903e52015-01-09 13:14:20 +0000440
441 /*
442 * Each architecture can perform additonal operations
443 * on the loaded segment
444 */
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500445 arch_segment_loaded((uintptr_t)dest, ptr->s_memsz,
446 last_non_empty == ptr ? SEG_FINAL : 0);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000447 }
448 }
Ionela Voinescu00903e52015-01-09 13:14:20 +0000449
Aaron Durbince9efe02015-03-20 16:37:12 -0500450 /* Update the payload's area with the bounce buffer information. */
451 prog_set_area(payload, (void *)(uintptr_t)bounce_buffer, bounce_size);
452
Ronald G. Minnichae631262009-04-01 10:48:39 +0000453 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000454}
455
Aaron Durbince9efe02015-03-20 16:37:12 -0500456void *selfload(struct prog *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000457{
Ronald G. Minnichef402092013-11-11 10:36:28 -0800458 uintptr_t entry = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000459 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000460
461 /* Preprocess the self segments */
Aaron Durbinceebc052014-02-25 00:21:10 -0600462 if (!build_self_segment_list(&head, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000463 goto out;
464
465 /* Load the segments */
Aaron Durbinceebc052014-02-25 00:21:10 -0600466 if (!load_self_segments(&head, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000467 goto out;
468
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000469 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000470
Aaron Durbin001de1a2013-04-24 22:59:45 -0500471 return (void *)entry;
472
473out:
474 return NULL;
475}