blob: ef1b986036c89ee8da15d517c49891ecdba5dab6 [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>
George Trudeauc1b98b92016-04-04 00:19:02 -04006 * Copyright (C) 2016 George Trudeau <george.trudeau@usherbrooke.ca>
ebiedermc7798892009-04-01 11:03:32 +00007 *
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.
ebiedermc7798892009-04-01 11:03:32 +000016 */
17
Julius Werner09f29212015-09-29 13:51:35 -070018#include <commonlib/compression.h>
George Trudeauc1b98b92016-04-04 00:19:02 -040019#include <commonlib/endian.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000020#include <console/console.h>
Aaron Durbinebf142a2013-03-29 16:23:23 -050021#include <cpu/cpu.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000022#include <stdint.h>
23#include <stdlib.h>
24#include <string.h>
Julius Wernerec5e5e02014-08-20 15:29:56 -070025#include <symbols.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000026#include <cbfs.h>
Myles Watson58170782009-10-28 16:13:28 +000027#include <lib.h>
Aaron Durbinceebc052014-02-25 00:21:10 -060028#include <bootmem.h>
Aaron Durbin04654a22015-03-17 11:43:44 -050029#include <program_loading.h>
Julius Werner09f29212015-09-29 13:51:35 -070030#include <timestamp.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000031
Julius Wernerec5e5e02014-08-20 15:29:56 -070032static const unsigned long lb_start = (unsigned long)&_program;
33static const unsigned long lb_end = (unsigned long)&_eprogram;
Stefan Reinauer19436292011-11-07 12:43:03 -080034
Ronald G. Minnichae631262009-04-01 10:48:39 +000035struct segment {
36 struct segment *next;
37 struct segment *prev;
Ronald G. Minnichae631262009-04-01 10:48:39 +000038 unsigned long s_dstaddr;
39 unsigned long s_srcaddr;
40 unsigned long s_memsz;
41 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000042 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000043};
44
Aaron Durbinedfcce82016-07-11 12:16:08 -050045static 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
53static 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 Baoe6ad7fa2009-11-05 10:02:59 +000061/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000062 * 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 Reinauerf64893a2009-07-23 22:03:14 +000067 * - 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 Baoe6ad7fa2009-11-05 10:02:59 +000070 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000071 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000072 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000073 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000074 * 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 Baoba49fb72009-11-01 09:18:23 +000078 * and much simpler than the general case implemented in kexec.
Ronald G. Minnichae631262009-04-01 10:48:39 +000079 */
80
Patrick Georgi5eceb322009-05-13 16:27:25 +000081static unsigned long bounce_size, bounce_buffer;
82
Aaron Durbinceebc052014-02-25 00:21:10 -060083static void get_bounce_buffer(unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +000084{
85 unsigned long lb_size;
Aaron Durbinceebc052014-02-25 00:21:10 -060086 void *buffer;
Aaron Durbine58a24b2014-02-24 22:11:45 -060087
88 /* When the ramstage is relocatable there is no need for a bounce
89 * buffer. All payloads should not overlap the ramstage.
90 */
Simon Glass7ae73fc2016-08-27 12:18:38 -060091 if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE) ||
92 !arch_supports_bounce_buffer()) {
Aaron Durbine58a24b2014-02-24 22:11:45 -060093 bounce_buffer = ~0UL;
94 bounce_size = 0;
95 return;
96 }
97
Stefan Reinauer19436292011-11-07 12:43:03 -080098 lb_size = lb_end - lb_start;
99 /* Plus coreboot size so I have somewhere
100 * to place a copy to return to.
101 */
Myles Watson92027982009-09-23 20:32:21 +0000102 lb_size = req_size + lb_size;
Aaron Durbinceebc052014-02-25 00:21:10 -0600103
104 buffer = bootmem_allocate_buffer(lb_size);
105
106 printk(BIOS_SPEW, "Bounce Buffer at %p, %lu bytes\n", buffer, lb_size);
107
108 bounce_buffer = (uintptr_t)buffer;
Myles Watson92027982009-09-23 20:32:21 +0000109 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000110}
111
Patrick Georgi5eceb322009-05-13 16:27:25 +0000112static int overlaps_coreboot(struct segment *seg)
113{
114 unsigned long start, end;
115 start = seg->s_dstaddr;
116 end = start + seg->s_memsz;
117 return !((end <= lb_start) || (start >= lb_end));
118}
119
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000120static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000121{
122 /* Modify all segments that want to load onto coreboot
123 * to load onto the bounce buffer instead.
124 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000125 /* ret: 1 : A new segment is inserted before the seg.
Stefan Reinauer19436292011-11-07 12:43:03 -0800126 * 0 : A new segment is inserted after the seg, or no new one.
127 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000128 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000129
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000130 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000131 lb_start, lb_end);
132
Patrick Georgi5eceb322009-05-13 16:27:25 +0000133 /* I don't conflict with coreboot so get out of here */
134 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000135 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000136
Vladimir Serbinenkof0d39c42016-02-19 16:44:22 +0100137 if (!arch_supports_bounce_buffer())
Lee Leahy38768c32017-03-09 14:07:18 -0800138 die("bounce buffer not supported");
Vladimir Serbinenkof0d39c42016-02-19 16:44:22 +0100139
Ronald G. Minnichae631262009-04-01 10:48:39 +0000140 start = seg->s_dstaddr;
141 middle = start + seg->s_filesz;
142 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000143
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000144 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000145 start, middle, end);
146
Patrick Georgi369bc782009-04-25 07:32:24 +0000147 if (seg->compression == CBFS_COMPRESS_NONE) {
148 /* Slice off a piece at the beginning
149 * that doesn't conflict with coreboot.
150 */
151 if (start < lb_start) {
152 struct segment *new;
153 unsigned long len = lb_start - start;
154 new = malloc(sizeof(*new));
155 *new = *seg;
156 new->s_memsz = len;
157 seg->s_memsz -= len;
158 seg->s_dstaddr += len;
159 seg->s_srcaddr += len;
160 if (seg->s_filesz > len) {
161 new->s_filesz = len;
162 seg->s_filesz -= len;
163 } else {
164 seg->s_filesz = 0;
165 }
166
167 /* Order by stream offset */
Aaron Durbinedfcce82016-07-11 12:16:08 -0500168 segment_insert_before(seg, new);
Patrick Georgi369bc782009-04-25 07:32:24 +0000169
170 /* compute the new value of start */
171 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000172
Lee Leahy73402172017-03-10 15:23:24 -0800173 printk(BIOS_SPEW,
174 " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000175 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000176 new->s_dstaddr + new->s_filesz,
177 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000178
179 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000180 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000181
182 /* Slice off a piece at the end
183 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000184 */
185 if (end > lb_end) {
186 unsigned long len = lb_end - start;
187 struct segment *new;
188 new = malloc(sizeof(*new));
189 *new = *seg;
190 seg->s_memsz = len;
191 new->s_memsz -= len;
192 new->s_dstaddr += len;
193 new->s_srcaddr += len;
194 if (seg->s_filesz > len) {
195 seg->s_filesz = len;
196 new->s_filesz -= len;
197 } else {
198 new->s_filesz = 0;
199 }
200 /* Order by stream offset */
Aaron Durbinedfcce82016-07-11 12:16:08 -0500201 segment_insert_after(seg, new);
Patrick Georgi369bc782009-04-25 07:32:24 +0000202
Lee Leahy73402172017-03-10 15:23:24 -0800203 printk(BIOS_SPEW,
204 " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000205 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000206 new->s_dstaddr + new->s_filesz,
207 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000208 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000209 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000210
Ronald G. Minnichae631262009-04-01 10:48:39 +0000211 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000212 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000213 * so you will make the dstaddr be this buffer, and it will get copied
214 * later to where coreboot lives.
215 */
216 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
217
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000218 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000219 seg->s_dstaddr,
220 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000221 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000222
223 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000224}
225
George Trudeauc1b98b92016-04-04 00:19:02 -0400226/* Decode a serialized cbfs payload segment
227 * from memory into native endianness.
228 */
229static void cbfs_decode_payload_segment(struct cbfs_payload_segment *segment,
230 const struct cbfs_payload_segment *src)
231{
232 segment->type = read_be32(&src->type);
233 segment->compression = read_be32(&src->compression);
234 segment->offset = read_be32(&src->offset);
235 segment->load_addr = read_be64(&src->load_addr);
236 segment->len = read_be32(&src->len);
237 segment->mem_len = read_be32(&src->mem_len);
238}
Ronald G. Minnichae631262009-04-01 10:48:39 +0000239
240static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000241 struct segment *head,
Aaron Durbin899d13d2015-05-15 23:39:23 -0500242 struct cbfs_payload *cbfs_payload, uintptr_t *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000243{
244 struct segment *new;
George Trudeauc1b98b92016-04-04 00:19:02 -0400245 struct cbfs_payload_segment *current_segment, *first_segment, segment;
246
Ronald G. Minnichae631262009-04-01 10:48:39 +0000247 memset(head, 0, sizeof(*head));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000248 head->next = head->prev = head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000249
George Trudeauc1b98b92016-04-04 00:19:02 -0400250 first_segment = &cbfs_payload->segments;
251
252 for (current_segment = first_segment;; ++current_segment) {
253 printk(BIOS_DEBUG,
Elyes HAOUAS91e0e3c2016-07-30 15:51:13 +0200254 "Loading segment from ROM address 0x%p\n",
George Trudeauc1b98b92016-04-04 00:19:02 -0400255 current_segment);
256
257 cbfs_decode_payload_segment(&segment, current_segment);
258
259 switch (segment.type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000260 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000261 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000262 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000263
Ronald G. Minnichae631262009-04-01 10:48:39 +0000264 case PAYLOAD_SEGMENT_CODE:
265 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000266 printk(BIOS_DEBUG, " %s (compression=%x)\n",
George Trudeauc1b98b92016-04-04 00:19:02 -0400267 segment.type == PAYLOAD_SEGMENT_CODE
268 ? "code" : "data", segment.compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000269
George Trudeauc1b98b92016-04-04 00:19:02 -0400270 new = malloc(sizeof(*new));
271 new->s_dstaddr = segment.load_addr;
272 new->s_memsz = segment.mem_len;
273 new->compression = segment.compression;
Ronald G. Minnichef402092013-11-11 10:36:28 -0800274 new->s_srcaddr = (uintptr_t)
275 ((unsigned char *)first_segment)
George Trudeauc1b98b92016-04-04 00:19:02 -0400276 + segment.offset;
277 new->s_filesz = segment.len;
278
Lee Leahy73402172017-03-10 15:23:24 -0800279 printk(BIOS_DEBUG,
280 " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
281 new->s_dstaddr, new->s_memsz, new->s_srcaddr,
282 new->s_filesz);
George Trudeauc1b98b92016-04-04 00:19:02 -0400283
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000284 /* Clean up the values */
285 if (new->s_filesz > new->s_memsz) {
286 new->s_filesz = new->s_memsz;
Vadim Bendebury9e208bc2014-05-13 15:43:58 -0700287 printk(BIOS_DEBUG,
George Trudeauc1b98b92016-04-04 00:19:02 -0400288 " cleaned up filesize 0x%lx\n",
289 new->s_filesz);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000290 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000291 break;
292
Ronald G. Minnichae631262009-04-01 10:48:39 +0000293 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700294 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
George Trudeauc1b98b92016-04-04 00:19:02 -0400295 (intptr_t)segment.load_addr, segment.mem_len);
296
Ronald G. Minnichae631262009-04-01 10:48:39 +0000297 new = malloc(sizeof(*new));
298 new->s_filesz = 0;
Edward O'Callaghan4bab5822014-03-01 09:27:37 +1100299 new->s_srcaddr = (uintptr_t)
300 ((unsigned char *)first_segment)
George Trudeauc1b98b92016-04-04 00:19:02 -0400301 + segment.offset;
302 new->s_dstaddr = segment.load_addr;
303 new->s_memsz = segment.mem_len;
Aaron Durbin044fb532016-07-11 15:36:50 -0500304 new->compression = CBFS_COMPRESS_NONE;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000305 break;
306
307 case PAYLOAD_SEGMENT_ENTRY:
George Trudeauc1b98b92016-04-04 00:19:02 -0400308 printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *)
309 (intptr_t)segment.load_addr);
310
311 *entry = segment.load_addr;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000312 /* Per definition, a payload always has the entry point
Martin Rothcbf2bd72013-07-09 21:51:14 -0600313 * as last segment. Thus, we use the occurrence of the
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000314 * entry point as break condition for the loop.
315 * Can we actually just look at the number of section?
316 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000317 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000318
319 default:
320 /* We found something that we don't know about. Throw
321 * hands into the sky and run away!
322 */
George Trudeauc1b98b92016-04-04 00:19:02 -0400323 printk(BIOS_EMERG, "Bad segment type %x\n",
324 segment.type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000325 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000326 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000327
Stefan Reinauerc44de492012-01-11 14:07:39 -0800328 /* We have found another CODE, DATA or BSS segment */
George Trudeauc1b98b92016-04-04 00:19:02 -0400329 /* Insert new segment at the end of the list */
Aaron Durbinedfcce82016-07-11 12:16:08 -0500330 segment_insert_before(head, new);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000331 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000332
Ronald G. Minnichae631262009-04-01 10:48:39 +0000333 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000334}
335
Simon Glass78c63862016-09-04 16:37:04 -0600336static int payload_targets_usable_ram(struct segment *head)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000337{
Aaron Durbinceebc052014-02-25 00:21:10 -0600338 const unsigned long one_meg = (1UL << 20);
Simon Glass78c63862016-09-04 16:37:04 -0600339 struct segment *ptr;
Aaron Durbinceebc052014-02-25 00:21:10 -0600340
Simon Glass78c63862016-09-04 16:37:04 -0600341 for (ptr = head->next; ptr != head; ptr = ptr->next) {
Aaron Durbinceebc052014-02-25 00:21:10 -0600342 if (bootmem_region_targets_usable_ram(ptr->s_dstaddr,
Simon Glass78c63862016-09-04 16:37:04 -0600343 ptr->s_memsz))
Aaron Durbinceebc052014-02-25 00:21:10 -0600344 continue;
345
346 if (ptr->s_dstaddr < one_meg &&
347 (ptr->s_dstaddr + ptr->s_memsz) <= one_meg) {
348 printk(BIOS_DEBUG,
Simon Glass78c63862016-09-04 16:37:04 -0600349 "Payload being loaded at below 1MiB "
Aaron Durbinceebc052014-02-25 00:21:10 -0600350 "without region being marked as RAM usable.\n");
351 continue;
352 }
353
354 /* Payload segment not targeting RAM. */
355 printk(BIOS_ERR, "SELF Payload doesn't target RAM:\n");
356 printk(BIOS_ERR, "Failed Segment: 0x%lx, %lu bytes\n",
357 ptr->s_dstaddr, ptr->s_memsz);
358 bootmem_dump_ranges();
359 return 0;
360 }
361
Simon Glass78c63862016-09-04 16:37:04 -0600362 return 1;
363}
364
Simon Glass7ae73fc2016-08-27 12:18:38 -0600365static int load_self_segments(struct segment *head, struct prog *payload,
366 bool check_regions)
Simon Glass78c63862016-09-04 16:37:04 -0600367{
368 struct segment *ptr;
369 unsigned long bounce_high = lb_end;
370
Simon Glass7ae73fc2016-08-27 12:18:38 -0600371 if (check_regions) {
372 if (!payload_targets_usable_ram(head))
373 return 0;
374 }
Simon Glass78c63862016-09-04 16:37:04 -0600375
Lee Leahy45fde702017-03-08 18:02:24 -0800376 for (ptr = head->next; ptr != head; ptr = ptr->next) {
Aaron Durbinceebc052014-02-25 00:21:10 -0600377 /*
378 * Add segments to bootmem memory map before a bounce buffer is
379 * allocated so that there aren't conflicts with the actual
380 * payload.
381 */
Simon Glass7ae73fc2016-08-27 12:18:38 -0600382 if (check_regions) {
383 bootmem_add_range(ptr->s_dstaddr, ptr->s_memsz,
384 LB_MEM_UNUSABLE);
385 }
Aaron Durbinceebc052014-02-25 00:21:10 -0600386
Stefan Reinauerc44de492012-01-11 14:07:39 -0800387 if (!overlaps_coreboot(ptr))
388 continue;
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000389 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
390 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000391 }
Aaron Durbinceebc052014-02-25 00:21:10 -0600392 get_bounce_buffer(bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000393 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000394 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000395 return 0;
396 }
Aaron Durbine58a24b2014-02-24 22:11:45 -0600397
Lee Leahy45fde702017-03-08 18:02:24 -0800398 for (ptr = head->next; ptr != head; ptr = ptr->next) {
Aaron Durbin044fb532016-07-11 15:36:50 -0500399 unsigned char *dest, *src, *middle, *end;
400 size_t len, memsz;
Lee Leahy73402172017-03-10 15:23:24 -0800401 printk(BIOS_DEBUG,
402 "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000403 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000404
Lee Leahy73402172017-03-10 15:23:24 -0800405 /* Modify the segment to load onto the bounce_buffer if
406 * necessary.
Patrick Georgi5eceb322009-05-13 16:27:25 +0000407 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000408 if (relocate_segment(bounce_buffer, ptr)) {
409 ptr = (ptr->prev)->prev;
410 continue;
411 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000412
Lee Leahy73402172017-03-10 15:23:24 -0800413 printk(BIOS_DEBUG,
414 "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000415 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
416
Ronald G. Minnichae631262009-04-01 10:48:39 +0000417 /* Compute the boundaries of the segment */
418 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000419 src = (unsigned char *)(ptr->s_srcaddr);
Aaron Durbin044fb532016-07-11 15:36:50 -0500420 len = ptr->s_filesz;
421 memsz = ptr->s_memsz;
422 end = dest + memsz;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000423
Ronald G. Minnichae631262009-04-01 10:48:39 +0000424 /* Copy data from the initial buffer */
Lee Leahy45fde702017-03-08 18:02:24 -0800425 switch (ptr->compression) {
Lee Leahye20a3192017-03-09 16:21:34 -0800426 case CBFS_COMPRESS_LZMA: {
427 printk(BIOS_DEBUG, "using LZMA\n");
428 timestamp_add_now(TS_START_ULZMA);
429 len = ulzman(src, len, dest, memsz);
430 timestamp_add_now(TS_END_ULZMA);
431 if (!len) /* Decompression Error. */
432 return 0;
433 break;
434 }
435 case CBFS_COMPRESS_LZ4: {
436 printk(BIOS_DEBUG, "using LZ4\n");
437 timestamp_add_now(TS_START_ULZ4F);
438 len = ulz4fn(src, len, dest, memsz);
439 timestamp_add_now(TS_END_ULZ4F);
440 if (!len) /* Decompression Error. */
441 return 0;
442 break;
443 }
444 case CBFS_COMPRESS_NONE: {
445 printk(BIOS_DEBUG, "it's not compressed!\n");
446 memcpy(dest, src, len);
447 break;
448 }
449 default:
Lee Leahy73402172017-03-10 15:23:24 -0800450 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n",
451 ptr->compression);
Lee Leahye20a3192017-03-09 16:21:34 -0800452 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000453 }
Aaron Durbin044fb532016-07-11 15:36:50 -0500454 /* Calculate middle after any changes to len. */
455 middle = dest + len;
456 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
457 (unsigned long)dest,
458 (unsigned long)middle,
459 (unsigned long)end,
460 (unsigned long)src);
461
462 /* Zero the extra bytes between middle & end */
463 if (middle < end) {
Lee Leahy73402172017-03-10 15:23:24 -0800464 printk(BIOS_DEBUG,
465 "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
466 (unsigned long)middle,
467 (unsigned long)(end - middle));
Aaron Durbin044fb532016-07-11 15:36:50 -0500468
469 /* Zero the extra bytes */
470 memset(middle, 0, end - middle);
471 }
472
Lee Leahy73402172017-03-10 15:23:24 -0800473 /* Copy the data that's outside the area that shadows ramstage
474 */
475 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest,
476 end, bounce_buffer);
Aaron Durbin044fb532016-07-11 15:36:50 -0500477 if ((unsigned long)end > bounce_buffer) {
478 if ((unsigned long)dest < bounce_buffer) {
479 unsigned char *from = dest;
Lee Leahy73402172017-03-10 15:23:24 -0800480 unsigned char *to = (unsigned char *)
481 (lb_start - (bounce_buffer
482 - (unsigned long)dest));
483 unsigned long amount = bounce_buffer
484 - (unsigned long)dest;
485 printk(BIOS_DEBUG,
486 "move prefix around: from %p, to %p, amount: %lx\n",
487 from, to, amount);
Aaron Durbin044fb532016-07-11 15:36:50 -0500488 memcpy(to, from, amount);
489 }
Lee Leahy73402172017-03-10 15:23:24 -0800490 if ((unsigned long)end > bounce_buffer + (lb_end
491 - lb_start)) {
492 unsigned long from = bounce_buffer + (lb_end
493 - lb_start);
Aaron Durbin044fb532016-07-11 15:36:50 -0500494 unsigned long to = lb_end;
Lee Leahy73402172017-03-10 15:23:24 -0800495 unsigned long amount =
496 (unsigned long)end - from;
497 printk(BIOS_DEBUG,
498 "move suffix around: from %lx, to %lx, amount: %lx\n",
499 from, to, amount);
Lee Leahyb2d834a2017-03-08 16:52:22 -0800500 memcpy((char *)to, (char *)from, amount);
Aaron Durbin044fb532016-07-11 15:36:50 -0500501 }
502 }
503
504 /*
505 * Each architecture can perform additonal operations
506 * on the loaded segment
507 */
508 prog_segment_loaded((uintptr_t)dest, ptr->s_memsz,
509 ptr->next == head ? SEG_FINAL : 0);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000510 }
Ionela Voinescu00903e52015-01-09 13:14:20 +0000511
Ronald G. Minnichae631262009-04-01 10:48:39 +0000512 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000513}
514
Simon Glass7ae73fc2016-08-27 12:18:38 -0600515void *selfload(struct prog *payload, bool check_regions)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000516{
Ronald G. Minnichef402092013-11-11 10:36:28 -0800517 uintptr_t entry = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000518 struct segment head;
Aaron Durbin899d13d2015-05-15 23:39:23 -0500519 void *data;
520
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500521 data = rdev_mmap_full(prog_rdev(payload));
Aaron Durbin899d13d2015-05-15 23:39:23 -0500522
523 if (data == NULL)
524 return NULL;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000525
526 /* Preprocess the self segments */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500527 if (!build_self_segment_list(&head, data, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000528 goto out;
529
530 /* Load the segments */
Simon Glass7ae73fc2016-08-27 12:18:38 -0600531 if (!load_self_segments(&head, payload, check_regions))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000532 goto out;
533
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000534 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000535
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500536 rdev_munmap(prog_rdev(payload), data);
537
538 /* Update the payload's area with the bounce buffer information. */
539 prog_set_area(payload, (void *)(uintptr_t)bounce_buffer, bounce_size);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500540
Aaron Durbin001de1a2013-04-24 22:59:45 -0500541 return (void *)entry;
542
543out:
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500544 rdev_munmap(prog_rdev(payload), data);
Aaron Durbin001de1a2013-04-24 22:59:45 -0500545 return NULL;
546}