ensure correct byte ordering for cbfs segment list
Decode each cbfs_payload_segment into native byte order during segments iteration. Note : List ordering has been changed, segments are now always inserted at the end. cbfs_serialized.h PAYLOAD_SEGMENT definitions have been changed to their standard order (big-endian). Change-Id: Icb3c6a7da2d253685a3bc157bc7f5a51183c9652 Signed-off-by: George Trudeau <george.trudeau@usherbrooke.ca> Reviewed-on: https://review.coreboot.org/14294 Tested-by: build bot (Jenkins) Tested-by: Raptor Engineering Automated Test Stand <noreply@raptorengineeringinc.com> Reviewed-by: Aaron Durbin <adurbin@chromium.org>
This commit is contained in:
parent
4488d7371a
commit
c1b98b909c
2 changed files with 60 additions and 45 deletions
|
@ -175,11 +175,11 @@ struct cbfs_payload {
|
||||||
struct cbfs_payload_segment segments;
|
struct cbfs_payload_segment segments;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define PAYLOAD_SEGMENT_CODE 0x45444F43
|
#define PAYLOAD_SEGMENT_CODE 0x434F4445
|
||||||
#define PAYLOAD_SEGMENT_DATA 0x41544144
|
#define PAYLOAD_SEGMENT_DATA 0x44415441
|
||||||
#define PAYLOAD_SEGMENT_BSS 0x20535342
|
#define PAYLOAD_SEGMENT_BSS 0x42535320
|
||||||
#define PAYLOAD_SEGMENT_PARAMS 0x41524150
|
#define PAYLOAD_SEGMENT_PARAMS 0x50415241
|
||||||
#define PAYLOAD_SEGMENT_ENTRY 0x52544E45
|
#define PAYLOAD_SEGMENT_ENTRY 0x454E5452
|
||||||
|
|
||||||
struct cbfs_optionrom {
|
struct cbfs_optionrom {
|
||||||
uint32_t compression;
|
uint32_t compression;
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
*
|
*
|
||||||
* Copyright (C) 2003 Eric W. Biederman <ebiederm@xmission.com>
|
* Copyright (C) 2003 Eric W. Biederman <ebiederm@xmission.com>
|
||||||
* Copyright (C) 2009 Ron Minnich <rminnich@gmail.com>
|
* Copyright (C) 2009 Ron Minnich <rminnich@gmail.com>
|
||||||
|
* Copyright (C) 2016 George Trudeau <george.trudeau@usherbrooke.ca>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -15,9 +16,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <commonlib/compression.h>
|
#include <commonlib/compression.h>
|
||||||
|
#include <commonlib/endian.h>
|
||||||
#include <console/console.h>
|
#include <console/console.h>
|
||||||
#include <cpu/cpu.h>
|
#include <cpu/cpu.h>
|
||||||
#include <endian.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -209,42 +210,62 @@ static int relocate_segment(unsigned long buffer, struct segment *seg)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Decode a serialized cbfs payload segment
|
||||||
|
* from memory into native endianness.
|
||||||
|
*/
|
||||||
|
static void cbfs_decode_payload_segment(struct cbfs_payload_segment *segment,
|
||||||
|
const struct cbfs_payload_segment *src)
|
||||||
|
{
|
||||||
|
segment->type = read_be32(&src->type);
|
||||||
|
segment->compression = read_be32(&src->compression);
|
||||||
|
segment->offset = read_be32(&src->offset);
|
||||||
|
segment->load_addr = read_be64(&src->load_addr);
|
||||||
|
segment->len = read_be32(&src->len);
|
||||||
|
segment->mem_len = read_be32(&src->mem_len);
|
||||||
|
}
|
||||||
|
|
||||||
static int build_self_segment_list(
|
static int build_self_segment_list(
|
||||||
struct segment *head,
|
struct segment *head,
|
||||||
struct cbfs_payload *cbfs_payload, uintptr_t *entry)
|
struct cbfs_payload *cbfs_payload, uintptr_t *entry)
|
||||||
{
|
{
|
||||||
struct segment *new;
|
struct segment *new;
|
||||||
struct segment *ptr;
|
struct cbfs_payload_segment *current_segment, *first_segment, segment;
|
||||||
struct cbfs_payload_segment *segment, *first_segment;
|
|
||||||
memset(head, 0, sizeof(*head));
|
memset(head, 0, sizeof(*head));
|
||||||
head->next = head->prev = head;
|
head->next = head->prev = head;
|
||||||
first_segment = segment = &cbfs_payload->segments;
|
|
||||||
|
|
||||||
while(1) {
|
first_segment = &cbfs_payload->segments;
|
||||||
printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
|
|
||||||
switch(segment->type) {
|
for (current_segment = first_segment;; ++current_segment) {
|
||||||
|
printk(BIOS_DEBUG,
|
||||||
|
"Loading segment from rom address 0x%p\n",
|
||||||
|
current_segment);
|
||||||
|
|
||||||
|
cbfs_decode_payload_segment(&segment, current_segment);
|
||||||
|
|
||||||
|
switch (segment.type) {
|
||||||
case PAYLOAD_SEGMENT_PARAMS:
|
case PAYLOAD_SEGMENT_PARAMS:
|
||||||
printk(BIOS_DEBUG, " parameter section (skipped)\n");
|
printk(BIOS_DEBUG, " parameter section (skipped)\n");
|
||||||
segment++;
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case PAYLOAD_SEGMENT_CODE:
|
case PAYLOAD_SEGMENT_CODE:
|
||||||
case PAYLOAD_SEGMENT_DATA:
|
case PAYLOAD_SEGMENT_DATA:
|
||||||
printk(BIOS_DEBUG, " %s (compression=%x)\n",
|
printk(BIOS_DEBUG, " %s (compression=%x)\n",
|
||||||
segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
|
segment.type == PAYLOAD_SEGMENT_CODE
|
||||||
ntohl(segment->compression));
|
? "code" : "data", segment.compression);
|
||||||
new = malloc(sizeof(*new));
|
|
||||||
new->s_dstaddr = ntohll(segment->load_addr);
|
|
||||||
new->s_memsz = ntohl(segment->mem_len);
|
|
||||||
new->compression = ntohl(segment->compression);
|
|
||||||
|
|
||||||
|
new = malloc(sizeof(*new));
|
||||||
|
new->s_dstaddr = segment.load_addr;
|
||||||
|
new->s_memsz = segment.mem_len;
|
||||||
|
new->compression = segment.compression;
|
||||||
new->s_srcaddr = (uintptr_t)
|
new->s_srcaddr = (uintptr_t)
|
||||||
((unsigned char *)first_segment)
|
((unsigned char *)first_segment)
|
||||||
+ ntohl(segment->offset);
|
+ segment.offset;
|
||||||
new->s_filesz = ntohl(segment->len);
|
new->s_filesz = segment.len;
|
||||||
|
|
||||||
printk(BIOS_DEBUG, " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
|
printk(BIOS_DEBUG, " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
|
||||||
new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
|
new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
|
||||||
|
|
||||||
/* Clean up the values */
|
/* Clean up the values */
|
||||||
if (new->s_filesz > new->s_memsz) {
|
if (new->s_filesz > new->s_memsz) {
|
||||||
new->s_filesz = new->s_memsz;
|
new->s_filesz = new->s_memsz;
|
||||||
|
@ -256,21 +277,22 @@ static int build_self_segment_list(
|
||||||
|
|
||||||
case PAYLOAD_SEGMENT_BSS:
|
case PAYLOAD_SEGMENT_BSS:
|
||||||
printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
|
printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
|
||||||
(intptr_t)ntohll(segment->load_addr),
|
(intptr_t)segment.load_addr, segment.mem_len);
|
||||||
ntohl(segment->mem_len));
|
|
||||||
new = malloc(sizeof(*new));
|
new = malloc(sizeof(*new));
|
||||||
new->s_filesz = 0;
|
new->s_filesz = 0;
|
||||||
new->s_srcaddr = (uintptr_t)
|
new->s_srcaddr = (uintptr_t)
|
||||||
((unsigned char *)first_segment)
|
((unsigned char *)first_segment)
|
||||||
+ ntohl(segment->offset);
|
+ segment.offset;
|
||||||
new->s_dstaddr = ntohll(segment->load_addr);
|
new->s_dstaddr = segment.load_addr;
|
||||||
new->s_memsz = ntohl(segment->mem_len);
|
new->s_memsz = segment.mem_len;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PAYLOAD_SEGMENT_ENTRY:
|
case PAYLOAD_SEGMENT_ENTRY:
|
||||||
printk(BIOS_DEBUG, " Entry Point 0x%p\n",
|
printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *)
|
||||||
(void *)(intptr_t)ntohll(segment->load_addr));
|
(intptr_t)segment.load_addr);
|
||||||
*entry = ntohll(segment->load_addr);
|
|
||||||
|
*entry = segment.load_addr;
|
||||||
/* Per definition, a payload always has the entry point
|
/* Per definition, a payload always has the entry point
|
||||||
* as last segment. Thus, we use the occurrence of the
|
* as last segment. Thus, we use the occurrence of the
|
||||||
* entry point as break condition for the loop.
|
* entry point as break condition for the loop.
|
||||||
|
@ -282,24 +304,17 @@ static int build_self_segment_list(
|
||||||
/* We found something that we don't know about. Throw
|
/* We found something that we don't know about. Throw
|
||||||
* hands into the sky and run away!
|
* hands into the sky and run away!
|
||||||
*/
|
*/
|
||||||
printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
|
printk(BIOS_EMERG, "Bad segment type %x\n",
|
||||||
|
segment.type);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We have found another CODE, DATA or BSS segment */
|
/* We have found another CODE, DATA or BSS segment */
|
||||||
segment++;
|
/* Insert new segment at the end of the list */
|
||||||
|
new->next = head;
|
||||||
/* Find place where to insert our segment */
|
new->prev = head->prev;
|
||||||
for(ptr = head->next; ptr != head; ptr = ptr->next) {
|
head->prev->next = new;
|
||||||
if (new->s_srcaddr < ntohll(segment->load_addr))
|
head->prev = new;
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Order by stream offset */
|
|
||||||
new->next = ptr;
|
|
||||||
new->prev = ptr->prev;
|
|
||||||
ptr->prev->next = new;
|
|
||||||
ptr->prev = new;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in a new issue