blob: 60cda6ae9e8af04ed62ce63ebcdb66515ebc4749 [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.
ebiedermc7798892009-04-01 11:03:32 +000015 */
16
Ronald G. Minnichae631262009-04-01 10:48:39 +000017#include <console/console.h>
Aaron Durbinebf142a2013-03-29 16:23:23 -050018#include <cpu/cpu.h>
Julius Werner9ff8f6f2015-02-23 14:31:09 -080019#include <endian.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000020#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
Julius Wernerec5e5e02014-08-20 15:29:56 -070023#include <symbols.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000024#include <cbfs.h>
Myles Watson58170782009-10-28 16:13:28 +000025#include <lib.h>
Aaron Durbinceebc052014-02-25 00:21:10 -060026#include <bootmem.h>
Aaron Durbin04654a22015-03-17 11:43:44 -050027#include <program_loading.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000028
Julius Wernerec5e5e02014-08-20 15:29:56 -070029static const unsigned long lb_start = (unsigned long)&_program;
30static const unsigned long lb_end = (unsigned long)&_eprogram;
Stefan Reinauer19436292011-11-07 12:43:03 -080031
Ronald G. Minnichae631262009-04-01 10:48:39 +000032struct segment {
33 struct segment *next;
34 struct segment *prev;
Ronald G. Minnichae631262009-04-01 10:48:39 +000035 unsigned long s_dstaddr;
36 unsigned long s_srcaddr;
37 unsigned long s_memsz;
38 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000039 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000040};
41
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000042/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000043 * 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 Reinauerf64893a2009-07-23 22:03:14 +000048 * - 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 Baoe6ad7fa2009-11-05 10:02:59 +000051 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000052 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000053 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000054 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000055 * 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 Baoba49fb72009-11-01 09:18:23 +000059 * and much simpler than the general case implemented in kexec.
Ronald G. Minnichae631262009-04-01 10:48:39 +000060 */
61
Patrick Georgi5eceb322009-05-13 16:27:25 +000062static unsigned long bounce_size, bounce_buffer;
63
Aaron Durbinceebc052014-02-25 00:21:10 -060064static void get_bounce_buffer(unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +000065{
66 unsigned long lb_size;
Aaron Durbinceebc052014-02-25 00:21:10 -060067 void *buffer;
Aaron Durbine58a24b2014-02-24 22:11:45 -060068
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 Reinauer19436292011-11-07 12:43:03 -080078 lb_size = lb_end - lb_start;
79 /* Plus coreboot size so I have somewhere
80 * to place a copy to return to.
81 */
Myles Watson92027982009-09-23 20:32:21 +000082 lb_size = req_size + lb_size;
Aaron Durbinceebc052014-02-25 00:21:10 -060083
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 Watson92027982009-09-23 20:32:21 +000089 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +000090}
91
Patrick Georgi5eceb322009-05-13 16:27:25 +000092static 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 Baoe6ad7fa2009-11-05 10:02:59 +0000100static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000101{
102 /* Modify all segments that want to load onto coreboot
103 * to load onto the bounce buffer instead.
104 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000105 /* ret: 1 : A new segment is inserted before the seg.
Stefan Reinauer19436292011-11-07 12:43:03 -0800106 * 0 : A new segment is inserted after the seg, or no new one.
107 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000108 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000109
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000110 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000111 lb_start, lb_end);
112
Patrick Georgi5eceb322009-05-13 16:27:25 +0000113 /* I don't conflict with coreboot so get out of here */
114 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000115 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000116
Ronald G. Minnichae631262009-04-01 10:48:39 +0000117 start = seg->s_dstaddr;
118 middle = start + seg->s_filesz;
119 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000120
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000121 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000122 start, middle, end);
123
Patrick Georgi369bc782009-04-25 07:32:24 +0000124 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 Georgi369bc782009-04-25 07:32:24 +0000149
150 /* compute the new value of start */
151 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000152
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000153 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000154 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000155 new->s_dstaddr + new->s_filesz,
156 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000157
158 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000159 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000160
161 /* Slice off a piece at the end
162 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000163 */
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 Georgi369bc782009-04-25 07:32:24 +0000184
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000185 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000186 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000187 new->s_dstaddr + new->s_filesz,
188 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000189 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000190 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000191
Ronald G. Minnichae631262009-04-01 10:48:39 +0000192 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000193 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000194 * 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 Reinauerc02b4fc2010-03-22 11:42:32 +0000199 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000200 seg->s_dstaddr,
201 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000202 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000203
204 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000205}
206
207
208static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000209 struct segment *head,
Aaron Durbin899d13d2015-05-15 23:39:23 -0500210 struct cbfs_payload *cbfs_payload, uintptr_t *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000211{
212 struct segment *new;
213 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000214 struct cbfs_payload_segment *segment, *first_segment;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000215 memset(head, 0, sizeof(*head));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000216 head->next = head->prev = head;
Aaron Durbin6086e632014-02-24 21:50:24 -0600217 first_segment = segment = &cbfs_payload->segments;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000218
219 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000220 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000221 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000222 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000223 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000224 segment++;
225 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000226
Ronald G. Minnichae631262009-04-01 10:48:39 +0000227 case PAYLOAD_SEGMENT_CODE:
228 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000229 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000230 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
231 ntohl(segment->compression));
232 new = malloc(sizeof(*new));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700233 new->s_dstaddr = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000234 new->s_memsz = ntohl(segment->mem_len);
235 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000236
Ronald G. Minnichef402092013-11-11 10:36:28 -0800237 new->s_srcaddr = (uintptr_t)
238 ((unsigned char *)first_segment)
239 + ntohl(segment->offset);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000240 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000241 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 +0000242 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 Bendebury9e208bc2014-05-13 15:43:58 -0700246 printk(BIOS_DEBUG,
247 " cleaned up filesize 0x%lx\n",
248 new->s_filesz);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000249 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000250 break;
251
Ronald G. Minnichae631262009-04-01 10:48:39 +0000252 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700253 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
254 (intptr_t)ntohll(segment->load_addr),
255 ntohl(segment->mem_len));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000256 new = malloc(sizeof(*new));
257 new->s_filesz = 0;
Edward O'Callaghan4bab5822014-03-01 09:27:37 +1100258 new->s_srcaddr = (uintptr_t)
259 ((unsigned char *)first_segment)
260 + ntohl(segment->offset);
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700261 new->s_dstaddr = ntohll(segment->load_addr);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000262 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000263 break;
264
265 case PAYLOAD_SEGMENT_ENTRY:
Hung-Te Linfdfd89f2013-02-27 16:38:38 +0800266 printk(BIOS_DEBUG, " Entry Point 0x%p\n",
267 (void *)(intptr_t)ntohll(segment->load_addr));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700268 *entry = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000269 /* Per definition, a payload always has the entry point
Martin Rothcbf2bd72013-07-09 21:51:14 -0600270 * as last segment. Thus, we use the occurrence of the
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000271 * entry point as break condition for the loop.
272 * Can we actually just look at the number of section?
273 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000274 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000275
276 default:
277 /* We found something that we don't know about. Throw
278 * hands into the sky and run away!
279 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000280 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000281 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000282 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000283
Stefan Reinauerc44de492012-01-11 14:07:39 -0800284 /* We have found another CODE, DATA or BSS segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000285 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000286
Stefan Reinauerc44de492012-01-11 14:07:39 -0800287 /* Find place where to insert our segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000288 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700289 if (new->s_srcaddr < ntohll(segment->load_addr))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000290 break;
291 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000292
Ronald G. Minnichae631262009-04-01 10:48:39 +0000293 /* Order by stream offset */
294 new->next = ptr;
295 new->prev = ptr->prev;
296 ptr->prev->next = new;
297 ptr->prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000298 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000299
Ronald G. Minnichae631262009-04-01 10:48:39 +0000300 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000301}
302
303static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000304 struct segment *head,
Aaron Durbince9efe02015-03-20 16:37:12 -0500305 struct prog *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000306{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000307 struct segment *ptr;
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500308 struct segment *last_non_empty;
Aaron Durbinceebc052014-02-25 00:21:10 -0600309 const unsigned long one_meg = (1UL << 20);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000310 unsigned long bounce_high = lb_end;
Aaron Durbinceebc052014-02-25 00:21:10 -0600311
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500312 /* 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 Georgi5eceb322009-05-13 16:27:25 +0000318 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Aaron Durbinceebc052014-02-25 00:21:10 -0600319 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 Reinauerc44de492012-01-11 14:07:39 -0800348 if (!overlaps_coreboot(ptr))
349 continue;
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000350 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
351 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000352 }
Aaron Durbinceebc052014-02-25 00:21:10 -0600353 get_bounce_buffer(bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000354 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000355 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000356 return 0;
357 }
Aaron Durbine58a24b2014-02-24 22:11:45 -0600358
Patrick Georgi5eceb322009-05-13 16:27:25 +0000359 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000360 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000361 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000362 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000363
Patrick Georgi5eceb322009-05-13 16:27:25 +0000364 /* Modify the segment to load onto the bounce_buffer if necessary.
365 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000366 if (relocate_segment(bounce_buffer, ptr)) {
367 ptr = (ptr->prev)->prev;
368 continue;
369 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000370
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000371 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000372 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
373
Ronald G. Minnichae631262009-04-01 10:48:39 +0000374 /* Compute the boundaries of the segment */
375 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000376 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000377
Ronald G. Minnichae631262009-04-01 10:48:39 +0000378 /* Copy data from the initial buffer */
379 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000380 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000381 size_t len;
382 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000383 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000384 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000385 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000386 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000387 if (!len) /* Decompression Error. */
388 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000389 break;
390 }
Patrick Georgi369bc782009-04-25 07:32:24 +0000391 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000392 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000393 memcpy(dest, src, len);
394 break;
395 }
396 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000397 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000398 return -1;
399 }
400 end = dest + ptr->s_memsz;
401 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000402 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000403 (unsigned long)dest,
404 (unsigned long)middle,
405 (unsigned long)end,
406 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000407
408 /* Zero the extra bytes between middle & end */
409 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000410 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000411 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000412
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000413 /* Zero the extra bytes */
414 memset(middle, 0, end - middle);
415 }
Furquan Shaikh20f25dd2014-04-22 10:41:05 -0700416 /* Copy the data that's outside the area that shadows ramstage */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000417 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000418 if ((unsigned long)end > bounce_buffer) {
419 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000420 unsigned char *from = dest;
421 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000422 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000423 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000424 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 Watson7943fe62009-10-30 02:08:07 +0000429 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000430 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000431 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000432 }
433 }
Ionela Voinescu00903e52015-01-09 13:14:20 +0000434
435 /*
436 * Each architecture can perform additonal operations
437 * on the loaded segment
438 */
Aaron Durbin6e76fff2015-03-20 09:42:05 -0500439 arch_segment_loaded((uintptr_t)dest, ptr->s_memsz,
440 last_non_empty == ptr ? SEG_FINAL : 0);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000441 }
442 }
Ionela Voinescu00903e52015-01-09 13:14:20 +0000443
Ronald G. Minnichae631262009-04-01 10:48:39 +0000444 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000445}
446
Aaron Durbince9efe02015-03-20 16:37:12 -0500447void *selfload(struct prog *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000448{
Ronald G. Minnichef402092013-11-11 10:36:28 -0800449 uintptr_t entry = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000450 struct segment head;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500451 void *data;
452
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500453 data = rdev_mmap_full(prog_rdev(payload));
Aaron Durbin899d13d2015-05-15 23:39:23 -0500454
455 if (data == NULL)
456 return NULL;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000457
458 /* Preprocess the self segments */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500459 if (!build_self_segment_list(&head, data, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000460 goto out;
461
462 /* Load the segments */
Aaron Durbinceebc052014-02-25 00:21:10 -0600463 if (!load_self_segments(&head, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000464 goto out;
465
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000466 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000467
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500468 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 Durbin899d13d2015-05-15 23:39:23 -0500472
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 Durbin001de1a2013-04-24 22:59:45 -0500476 return (void *)entry;
477
478out:
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500479 rdev_munmap(prog_rdev(payload), data);
Aaron Durbin001de1a2013-04-24 22:59:45 -0500480 return NULL;
481}