blob: 254c131a5324f78447f5162efe974203a43a09a4 [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
Ronald G. Minnichae631262009-04-01 10:48:39 +000021#include <console/console.h>
22#include <part/fallback_boot.h>
23#include <boot/elf.h>
24#include <boot/elf_boot.h>
25#include <boot/coreboot_tables.h>
26#include <ip_checksum.h>
27#include <stream/read_bytes.h>
28#include <stdint.h>
29#include <stdlib.h>
30#include <string.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000031#include <cbfs.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000032
33#ifndef CONFIG_BIG_ENDIAN
34#define ntohl(x) ( ((x&0xff)<<24) | ((x&0xff00)<<8) | \
35 ((x&0xff0000) >> 8) | ((x&0xff000000) >> 24) )
36#else
37#define ntohl(x) (x)
38#endif
39
40/* Maximum physical address we can use for the coreboot bounce buffer.
41 */
42#ifndef MAX_ADDR
43#define MAX_ADDR -1UL
44#endif
45
46extern unsigned char _ram_seg;
47extern unsigned char _eram_seg;
48
49struct segment {
50 struct segment *next;
51 struct segment *prev;
52 struct segment *phdr_next;
53 struct segment *phdr_prev;
54 unsigned long s_dstaddr;
55 unsigned long s_srcaddr;
56 unsigned long s_memsz;
57 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000058 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000059};
60
61struct verify_callback {
62 struct verify_callback *next;
63 int (*callback)(struct verify_callback *vcb,
64 Elf_ehdr *ehdr, Elf_phdr *phdr, struct segment *head);
65 unsigned long desc_offset;
66 unsigned long desc_addr;
67};
68
69struct ip_checksum_vcb {
70 struct verify_callback data;
71 unsigned short ip_checksum;
72};
73
Patrick Georgid1185bf2009-05-26 14:00:49 +000074void * cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
75{
76 int selfboot(struct lb_memory *mem, struct cbfs_payload *payload);
Stefan Reinauerf64893a2009-07-23 22:03:14 +000077 struct cbfs_payload *payload;
Patrick Georgid1185bf2009-05-26 14:00:49 +000078
Stefan Reinauerf64893a2009-07-23 22:03:14 +000079 payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
Patrick Georgid1185bf2009-05-26 14:00:49 +000080 if (payload == NULL)
81 return (void *) -1;
82 printk_debug("Got a payload\n");
Stefan Reinauerf64893a2009-07-23 22:03:14 +000083
Patrick Georgid1185bf2009-05-26 14:00:49 +000084 selfboot(lb_mem, payload);
85 printk_emerg("SELFBOOT RETURNED!\n");
86
87 return (void *) -1;
88}
89
Ronald G. Minnichae631262009-04-01 10:48:39 +000090/* The problem:
91 * Static executables all want to share the same addresses
92 * in memory because only a few addresses are reliably present on
93 * a machine, and implementing general relocation is hard.
94 *
95 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000096 * - Allocate a buffer the size of the coreboot image plus additional
97 * required space.
98 * - Anything that would overwrite coreboot copy into the lower part of
Ronald G. Minnichae631262009-04-01 10:48:39 +000099 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000100 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000101 * - Then jump to the loaded image.
102 *
103 * Benefits:
104 * - Nearly arbitrary standalone executables can be loaded.
105 * - Coreboot is preserved, so it can be returned to.
106 * - The implementation is still relatively simple,
107 * and much simpler then the general case implemented in kexec.
108 *
109 */
110
Patrick Georgi5eceb322009-05-13 16:27:25 +0000111static unsigned long bounce_size, bounce_buffer;
112
113static void get_bounce_buffer(struct lb_memory *mem, unsigned long bounce_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000114{
115 unsigned long lb_size;
116 unsigned long mem_entries;
117 unsigned long buffer;
118 int i;
119 lb_size = (unsigned long)(&_eram_seg - &_ram_seg);
120 /* Double coreboot size so I have somewhere to place a copy to return to */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000121 lb_size = bounce_size + lb_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000122 mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
123 buffer = 0;
124 for(i = 0; i < mem_entries; i++) {
125 unsigned long mstart, mend;
126 unsigned long msize;
127 unsigned long tbuffer;
128 if (mem->map[i].type != LB_MEM_RAM)
129 continue;
130 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
131 continue;
132 if (unpack_lb64(mem->map[i].size) < lb_size)
133 continue;
134 mstart = unpack_lb64(mem->map[i].start);
135 msize = MAX_ADDR - mstart +1;
136 if (msize > unpack_lb64(mem->map[i].size))
137 msize = unpack_lb64(mem->map[i].size);
138 mend = mstart + msize;
139 tbuffer = mend - lb_size;
140 if (tbuffer < buffer)
141 continue;
142 buffer = tbuffer;
143 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000144 bounce_buffer = buffer;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000145}
146
147static int valid_area(struct lb_memory *mem, unsigned long buffer,
148 unsigned long start, unsigned long len)
149{
150 /* Check through all of the memory segments and ensure
151 * the segment that was passed in is completely contained
152 * in RAM.
153 */
154 int i;
155 unsigned long end = start + len;
156 unsigned long mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
157
158 /* See if I conflict with the bounce buffer */
159 if (end >= buffer) {
160 return 0;
161 }
162
163 /* Walk through the table of valid memory ranges and see if I
164 * have a match.
165 */
166 for(i = 0; i < mem_entries; i++) {
167 uint64_t mstart, mend;
168 uint32_t mtype;
169 mtype = mem->map[i].type;
170 mstart = unpack_lb64(mem->map[i].start);
171 mend = mstart + unpack_lb64(mem->map[i].size);
172 if ((mtype == LB_MEM_RAM) && (start < mend) && (end > mstart)) {
173 break;
174 }
175 if ((mtype == LB_MEM_TABLE) && (start < mend) && (end > mstart)) {
176 printk_err("Payload is overwriting Coreboot tables.\n");
177 break;
178 }
179 }
180 if (i == mem_entries) {
181 printk_err("No matching ram area found for range:\n");
182 printk_err(" [0x%016lx, 0x%016lx)\n", start, end);
183 printk_err("Ram areas\n");
184 for(i = 0; i < mem_entries; i++) {
185 uint64_t mstart, mend;
186 uint32_t mtype;
187 mtype = mem->map[i].type;
188 mstart = unpack_lb64(mem->map[i].start);
189 mend = mstart + unpack_lb64(mem->map[i].size);
190 printk_err(" [0x%016lx, 0x%016lx) %s\n",
191 (unsigned long)mstart,
192 (unsigned long)mend,
193 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
194
195 }
196 return 0;
197 }
198 return 1;
199}
200
Patrick Georgi5eceb322009-05-13 16:27:25 +0000201static const unsigned long lb_start = (unsigned long)&_ram_seg;
202static const unsigned long lb_end = (unsigned long)&_eram_seg;
203
204static int overlaps_coreboot(struct segment *seg)
205{
206 unsigned long start, end;
207 start = seg->s_dstaddr;
208 end = start + seg->s_memsz;
209 return !((end <= lb_start) || (start >= lb_end));
210}
211
Ronald G. Minnichae631262009-04-01 10:48:39 +0000212static void relocate_segment(unsigned long buffer, struct segment *seg)
213{
214 /* Modify all segments that want to load onto coreboot
215 * to load onto the bounce buffer instead.
216 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000217 unsigned long start, middle, end;
218
219 printk_spew("lb: [0x%016lx, 0x%016lx)\n",
220 lb_start, lb_end);
221
Patrick Georgi5eceb322009-05-13 16:27:25 +0000222 /* I don't conflict with coreboot so get out of here */
223 if (!overlaps_coreboot(seg))
224 return;
225
Ronald G. Minnichae631262009-04-01 10:48:39 +0000226 start = seg->s_dstaddr;
227 middle = start + seg->s_filesz;
228 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000229
230 printk_spew("segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
231 start, middle, end);
232
Patrick Georgi369bc782009-04-25 07:32:24 +0000233 if (seg->compression == CBFS_COMPRESS_NONE) {
234 /* Slice off a piece at the beginning
235 * that doesn't conflict with coreboot.
236 */
237 if (start < lb_start) {
238 struct segment *new;
239 unsigned long len = lb_start - start;
240 new = malloc(sizeof(*new));
241 *new = *seg;
242 new->s_memsz = len;
243 seg->s_memsz -= len;
244 seg->s_dstaddr += len;
245 seg->s_srcaddr += len;
246 if (seg->s_filesz > len) {
247 new->s_filesz = len;
248 seg->s_filesz -= len;
249 } else {
250 seg->s_filesz = 0;
251 }
252
253 /* Order by stream offset */
254 new->next = seg;
255 new->prev = seg->prev;
256 seg->prev->next = new;
257 seg->prev = new;
258 /* Order by original program header order */
259 new->phdr_next = seg;
260 new->phdr_prev = seg->phdr_prev;
261 seg->phdr_prev->phdr_next = new;
262 seg->phdr_prev = new;
263
264 /* compute the new value of start */
265 start = seg->s_dstaddr;
266
267 printk_spew(" early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
268 new->s_dstaddr,
269 new->s_dstaddr + new->s_filesz,
270 new->s_dstaddr + new->s_memsz);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000271 }
Patrick Georgi369bc782009-04-25 07:32:24 +0000272
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000273 /* Slice off a piece at the end
Patrick Georgi369bc782009-04-25 07:32:24 +0000274 * that doesn't conflict with coreboot
275 */
276 if (end > lb_end) {
277 unsigned long len = lb_end - start;
278 struct segment *new;
279 new = malloc(sizeof(*new));
280 *new = *seg;
281 seg->s_memsz = len;
282 new->s_memsz -= len;
283 new->s_dstaddr += len;
284 new->s_srcaddr += len;
285 if (seg->s_filesz > len) {
286 seg->s_filesz = len;
287 new->s_filesz -= len;
288 } else {
289 new->s_filesz = 0;
290 }
291 /* Order by stream offset */
292 new->next = seg->next;
293 new->prev = seg;
294 seg->next->prev = new;
295 seg->next = new;
296 /* Order by original program header order */
297 new->phdr_next = seg->phdr_next;
298 new->phdr_prev = seg;
299 seg->phdr_next->phdr_prev = new;
300 seg->phdr_next = new;
301
Patrick Georgi369bc782009-04-25 07:32:24 +0000302 printk_spew(" late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
303 new->s_dstaddr,
304 new->s_dstaddr + new->s_filesz,
305 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000306 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000307 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000308
Ronald G. Minnichae631262009-04-01 10:48:39 +0000309 /* Now retarget this segment onto the bounce buffer */
310 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
311 * so you will make the dstaddr be this buffer, and it will get copied
312 * later to where coreboot lives.
313 */
314 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
315
316 printk_spew(" bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
317 seg->s_dstaddr,
318 seg->s_dstaddr + seg->s_filesz,
319 seg->s_dstaddr + seg->s_memsz);
320}
321
322
323static int build_self_segment_list(
324 struct segment *head,
Patrick Georgi5eceb322009-05-13 16:27:25 +0000325 struct lb_memory *mem,
Peter Stuge483b7bb2009-04-14 07:40:01 +0000326 struct cbfs_payload *payload, u32 *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000327{
328 struct segment *new;
329 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000330 struct cbfs_payload_segment *segment, *first_segment;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000331 memset(head, 0, sizeof(*head));
332 head->phdr_next = head->phdr_prev = head;
333 head->next = head->prev = head;
334 first_segment = segment = &payload->segments;
335
336 while(1) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000337 printk_debug("Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000338 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000339 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000340 printk_debug(" parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000341 segment++;
342 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000343
Ronald G. Minnichae631262009-04-01 10:48:39 +0000344 case PAYLOAD_SEGMENT_CODE:
345 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000346 printk_debug(" %s (compression=%x)\n",
347 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
348 ntohl(segment->compression));
349 new = malloc(sizeof(*new));
350 new->s_dstaddr = ntohl((u32) segment->load_addr);
351 new->s_memsz = ntohl(segment->mem_len);
352 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000353
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000354 new->s_srcaddr = (u32) ((unsigned char *) first_segment) + ntohl(segment->offset);
355 new->s_filesz = ntohl(segment->len);
356 printk_debug(" New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
357 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
358 /* Clean up the values */
359 if (new->s_filesz > new->s_memsz) {
360 new->s_filesz = new->s_memsz;
361 }
362 printk_debug(" (cleaned up) New segment addr 0x%lx size 0x%lx offset 0x%lx filesize 0x%lx\n",
363 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
364 break;
365
Ronald G. Minnichae631262009-04-01 10:48:39 +0000366 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000367 printk_debug(" BSS 0x%p (%d byte)\n", (void *) ntohl((u32) segment->load_addr),
Ronald G. Minnichae631262009-04-01 10:48:39 +0000368 ntohl(segment->mem_len));
369 new = malloc(sizeof(*new));
370 new->s_filesz = 0;
371 new->s_dstaddr = ntohl((u32) segment->load_addr);
372 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000373 break;
374
375 case PAYLOAD_SEGMENT_ENTRY:
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000376 printk_debug(" Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
Myles Watsonfa12b672009-04-30 22:45:41 +0000377 *entry = ntohl((u32) segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000378 /* Per definition, a payload always has the entry point
379 * as last segment. Thus, we use the occurence of the
380 * entry point as break condition for the loop.
381 * Can we actually just look at the number of section?
382 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000383 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000384
385 default:
386 /* We found something that we don't know about. Throw
387 * hands into the sky and run away!
388 */
389 printk_emerg("Bad segment type %x\n", segment->type);
390 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000391 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000392
Ronald G. Minnichae631262009-04-01 10:48:39 +0000393 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000394
Ronald G. Minnichae631262009-04-01 10:48:39 +0000395 for(ptr = head->next; ptr != head; ptr = ptr->next) {
396 if (new->s_srcaddr < ntohl((u32) segment->load_addr))
397 break;
398 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000399
Ronald G. Minnichae631262009-04-01 10:48:39 +0000400 /* Order by stream offset */
401 new->next = ptr;
402 new->prev = ptr->prev;
403 ptr->prev->next = new;
404 ptr->prev = new;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000405
Ronald G. Minnichae631262009-04-01 10:48:39 +0000406 /* Order by original program header order */
407 new->phdr_next = head;
408 new->phdr_prev = head->phdr_prev;
409 head->phdr_prev->phdr_next = new;
410 head->phdr_prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000411 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000412
Ronald G. Minnichae631262009-04-01 10:48:39 +0000413 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000414}
415
416static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000417 struct segment *head,
418 struct lb_memory *mem,
419 struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000420{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000421 struct segment *ptr;
422
Patrick Georgi5eceb322009-05-13 16:27:25 +0000423 unsigned long required_bounce_size = lb_end - lb_start;
424 for(ptr = head->next; ptr != head; ptr = ptr->next) {
425 if (!overlaps_coreboot(ptr)) continue;
426 unsigned long bounce = ptr->s_dstaddr + ptr->s_memsz - lb_start;
427 if (bounce > required_bounce_size) required_bounce_size = bounce;
428 }
429 get_bounce_buffer(mem, required_bounce_size);
430 if (!bounce_buffer) {
431 printk_err("Could not find a bounce buffer...\n");
432 return 0;
433 }
434 for(ptr = head->next; ptr != head; ptr = ptr->next) {
435 /* Verify the memory addresses in the segment are valid */
436 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
437 return 0;
438 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000439 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000440 unsigned char *dest, *src;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000441 printk_debug("Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
442 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
443
Patrick Georgi5eceb322009-05-13 16:27:25 +0000444 /* Modify the segment to load onto the bounce_buffer if necessary.
445 */
446 relocate_segment(bounce_buffer, ptr);
447
448 printk_debug("Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
449 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
450
Ronald G. Minnichae631262009-04-01 10:48:39 +0000451 /* Compute the boundaries of the segment */
452 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000453 src = (unsigned char *)(ptr->s_srcaddr);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000454
455 /* Copy data from the initial buffer */
456 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000457 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000458 size_t len;
459 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000460 switch(ptr->compression) {
461#if CONFIG_COMPRESSED_PAYLOAD_LZMA==1
462 case CBFS_COMPRESS_LZMA: {
463 printk_debug("using LZMA\n");
464 unsigned long ulzma(unsigned char *src, unsigned char *dst);
465 len = ulzma(src, dest);
466 break;
467 }
468#endif
469#if CONFIG_COMPRESSED_PAYLOAD_NRV2B==1
470 case CBFS_COMPRESS_NRV2B: {
471 printk_debug("using NRV2B\n");
472 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
473 unsigned long tmp;
474 len = unrv2b(src, dest, &tmp);
475 break;
476 }
477#endif
478 case CBFS_COMPRESS_NONE: {
479 printk_debug("it's not compressed!\n");
480 memcpy(dest, src, len);
481 break;
482 }
483 default:
484 printk_info( "CBFS: Unknown compression type %d\n", ptr->compression);
485 return -1;
486 }
487 end = dest + ptr->s_memsz;
488 middle = dest + len;
489 printk_spew("[ 0x%016lx, %016lx, 0x%016lx) <- %016lx\n",
490 (unsigned long)dest,
491 (unsigned long)middle,
492 (unsigned long)end,
493 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000494
495 /* Zero the extra bytes between middle & end */
496 if (middle < end) {
497 printk_debug("Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
498 (unsigned long)middle, (unsigned long)(end - middle));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000499
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000500 /* Zero the extra bytes */
501 memset(middle, 0, end - middle);
502 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000503 }
504 }
505 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000506}
507
Peter Stuge483b7bb2009-04-14 07:40:01 +0000508int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000509{
Myles Watsonfa12b672009-04-30 22:45:41 +0000510 u32 entry=0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000511 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000512
513 /* Preprocess the self segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000514 if (!build_self_segment_list(&head, mem, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000515 goto out;
516
517 /* Load the segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000518 if (!load_self_segments(&head, mem, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000519 goto out;
520
521 printk_spew("Loaded segments\n");
522
523 /* Reset to booting from this image as late as possible */
524 boot_successful();
525
Myles Watsonfa12b672009-04-30 22:45:41 +0000526 printk_debug("Jumping to boot code at %x\n", entry);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000527 post_code(0xfe);
528
529 /* Jump to kernel */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000530 jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000531 return 1;
532
533 out:
534 return 0;
535}
536