From c1b98b909c1d3bbd4eb74140769b118c98a9eb70 Mon Sep 17 00:00:00 2001 From: George Trudeau Date: Mon, 4 Apr 2016 00:19:02 -0400 Subject: 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 Reviewed-on: https://review.coreboot.org/14294 Tested-by: build bot (Jenkins) Tested-by: Raptor Engineering Automated Test Stand Reviewed-by: Aaron Durbin --- src/commonlib/include/commonlib/cbfs_serialized.h | 10 +-- src/lib/selfboot.c | 95 +++++++++++++---------- 2 files changed, 60 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/commonlib/include/commonlib/cbfs_serialized.h b/src/commonlib/include/commonlib/cbfs_serialized.h index c1a1c3b542..c01ba1a020 100644 --- a/src/commonlib/include/commonlib/cbfs_serialized.h +++ b/src/commonlib/include/commonlib/cbfs_serialized.h @@ -175,11 +175,11 @@ struct cbfs_payload { struct cbfs_payload_segment segments; }; -#define PAYLOAD_SEGMENT_CODE 0x45444F43 -#define PAYLOAD_SEGMENT_DATA 0x41544144 -#define PAYLOAD_SEGMENT_BSS 0x20535342 -#define PAYLOAD_SEGMENT_PARAMS 0x41524150 -#define PAYLOAD_SEGMENT_ENTRY 0x52544E45 +#define PAYLOAD_SEGMENT_CODE 0x434F4445 +#define PAYLOAD_SEGMENT_DATA 0x44415441 +#define PAYLOAD_SEGMENT_BSS 0x42535320 +#define PAYLOAD_SEGMENT_PARAMS 0x50415241 +#define PAYLOAD_SEGMENT_ENTRY 0x454E5452 struct cbfs_optionrom { uint32_t compression; diff --git a/src/lib/selfboot.c b/src/lib/selfboot.c index 23eda14dff..75d67251b1 100644 --- a/src/lib/selfboot.c +++ b/src/lib/selfboot.c @@ -3,6 +3,7 @@ * * Copyright (C) 2003 Eric W. Biederman * Copyright (C) 2009 Ron Minnich + * Copyright (C) 2016 George Trudeau * * 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 @@ -15,9 +16,9 @@ */ #include +#include #include #include -#include #include #include #include @@ -209,68 +210,89 @@ static int relocate_segment(unsigned long buffer, struct segment *seg) 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( struct segment *head, struct cbfs_payload *cbfs_payload, uintptr_t *entry) { struct segment *new; - struct segment *ptr; - struct cbfs_payload_segment *segment, *first_segment; + struct cbfs_payload_segment *current_segment, *first_segment, segment; + memset(head, 0, sizeof(*head)); head->next = head->prev = head; - first_segment = segment = &cbfs_payload->segments; - while(1) { - printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment); - switch(segment->type) { + first_segment = &cbfs_payload->segments; + + 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: printk(BIOS_DEBUG, " parameter section (skipped)\n"); - segment++; continue; case PAYLOAD_SEGMENT_CODE: case PAYLOAD_SEGMENT_DATA: printk(BIOS_DEBUG, " %s (compression=%x)\n", - segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data", - ntohl(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); + segment.type == PAYLOAD_SEGMENT_CODE + ? "code" : "data", 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) ((unsigned char *)first_segment) - + ntohl(segment->offset); - new->s_filesz = ntohl(segment->len); + + segment.offset; + new->s_filesz = segment.len; + 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); + /* Clean up the values */ if (new->s_filesz > new->s_memsz) { new->s_filesz = new->s_memsz; printk(BIOS_DEBUG, - " cleaned up filesize 0x%lx\n", - new->s_filesz); + " cleaned up filesize 0x%lx\n", + new->s_filesz); } break; case PAYLOAD_SEGMENT_BSS: printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *) - (intptr_t)ntohll(segment->load_addr), - ntohl(segment->mem_len)); + (intptr_t)segment.load_addr, segment.mem_len); + new = malloc(sizeof(*new)); new->s_filesz = 0; new->s_srcaddr = (uintptr_t) ((unsigned char *)first_segment) - + ntohl(segment->offset); - new->s_dstaddr = ntohll(segment->load_addr); - new->s_memsz = ntohl(segment->mem_len); + + segment.offset; + new->s_dstaddr = segment.load_addr; + new->s_memsz = segment.mem_len; break; case PAYLOAD_SEGMENT_ENTRY: - printk(BIOS_DEBUG, " Entry Point 0x%p\n", - (void *)(intptr_t)ntohll(segment->load_addr)); - *entry = ntohll(segment->load_addr); + printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *) + (intptr_t)segment.load_addr); + + *entry = segment.load_addr; /* Per definition, a payload always has the entry point * as last segment. Thus, we use the occurrence of the * 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 * 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; } /* We have found another CODE, DATA or BSS segment */ - segment++; - - /* Find place where to insert our segment */ - for(ptr = head->next; ptr != head; ptr = ptr->next) { - if (new->s_srcaddr < ntohll(segment->load_addr)) - break; - } - - /* Order by stream offset */ - new->next = ptr; - new->prev = ptr->prev; - ptr->prev->next = new; - ptr->prev = new; + /* Insert new segment at the end of the list */ + new->next = head; + new->prev = head->prev; + head->prev->next = new; + head->prev = new; } return 1; -- cgit v1.2.3