[coreboot-gerrit] Patch set updated for coreboot: ensure correct byte ordering for cbfs segment list

George Trudeau (george.trudeau@usherbrooke.ca) gerrit at coreboot.org
Fri Apr 15 01:24:21 CEST 2016


George Trudeau (george.trudeau at usherbrooke.ca) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/14294

-gerrit

commit ab911a37c3d7ebc79f1a8bd9e4e028b2e46a644a
Author: George Trudeau <george.trudeau at usherbrooke.ca>
Date:   Mon Apr 4 00:19:02 2016 -0400

    ensure correct byte ordering for cbfs segment list
    
    Decode each cbfs_payload_segment into native byte order during
    segments iteration.
    
    Change-Id: Icb3c6a7da2d253685a3bc157bc7f5a51183c9652
    Signed-off-by: George Trudeau <george.trudeau at usherbrooke.ca>
---
 src/commonlib/Makefile.inc                     |  4 ++
 src/commonlib/cbfs_decoder.c                   | 31 ++++++++++++
 src/commonlib/include/commonlib/cbfs_decoder.h | 23 +++++++++
 src/lib/selfboot.c                             | 66 +++++++++++++++-----------
 4 files changed, 96 insertions(+), 28 deletions(-)

diff --git a/src/commonlib/Makefile.inc b/src/commonlib/Makefile.inc
index 7c14f7c..7a201a7 100644
--- a/src/commonlib/Makefile.inc
+++ b/src/commonlib/Makefile.inc
@@ -25,3 +25,7 @@ verstage-y += lz4_wrapper.c
 romstage-y += lz4_wrapper.c
 ramstage-y += lz4_wrapper.c
 postcar-y += lz4_wrapper.c
+
+bootblock-y += cbfs_decoder.c
+romstage-y += cbfs_decoder.c
+ramstage-y += cbfs_decoder.c
diff --git a/src/commonlib/cbfs_decoder.c b/src/commonlib/cbfs_decoder.c
new file mode 100644
index 0000000..2f84429
--- /dev/null
+++ b/src/commonlib/cbfs_decoder.c
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 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
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <commonlib/cbfs_decoder.h>
+#include <commonlib/endian.h>
+
+void decode_cbfs_payload_segment(struct cbfs_payload_segment *segment,
+		const void *data)
+{
+	const uint32_t *src = (uint32_t *)data;
+
+	segment->type        = read_be32(src++);
+	segment->compression = read_be32(src++);
+	segment->offset      = read_be32(src++);
+	segment->load_addr   = read_be64(src++);
+	src++;
+	segment->len         = read_be32(src++);
+	segment->mem_len     = read_be32(src++);
+}
diff --git a/src/commonlib/include/commonlib/cbfs_decoder.h b/src/commonlib/include/commonlib/cbfs_decoder.h
new file mode 100644
index 0000000..17b0975
--- /dev/null
+++ b/src/commonlib/include/commonlib/cbfs_decoder.h
@@ -0,0 +1,23 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright 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
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef _COMMONLIB_CBFS_DECODER_H_
+#define _COMMONLIB_CBFS_DECODER_H_
+
+#include <commonlib/cbfs_serialized.h>
+
+void decode_cbfs_payload_segment(struct cbfs_payload_segment *, const void *);
+
+#endif
diff --git a/src/lib/selfboot.c b/src/lib/selfboot.c
index 23eda14..550fcbb 100644
--- a/src/lib/selfboot.c
+++ b/src/lib/selfboot.c
@@ -14,6 +14,7 @@
  * GNU General Public License for more details.
  */
 
+#include <commonlib/cbfs_decoder.h>
 #include <commonlib/compression.h>
 #include <console/console.h>
 #include <cpu/cpu.h>
@@ -209,42 +210,49 @@ static int relocate_segment(unsigned long buffer, struct segment *seg)
 	return ret;
 }
 
-
 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 segment *new, *ptr;
+	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;
+
+	first_segment = current_segment = &cbfs_payload->segments;
+	decode_cbfs_payload_segment(&segment, current_segment);
 
 	while(1) {
-		printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
-		switch(segment->type) {
+		printk(BIOS_DEBUG,
+			"Loading segment from rom address 0x%p\n",
+			current_segment);
+
+		switch (segment.type) {
 		case PAYLOAD_SEGMENT_PARAMS:
 			printk(BIOS_DEBUG, "  parameter section (skipped)\n");
-			segment++;
+			decode_cbfs_payload_segment(&segment,
+				++current_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);
+					((unsigned char *)first_segment)
+					+ 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;
@@ -256,21 +264,22 @@ static int build_self_segment_list(
 
 		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,16 +291,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++;
+		decode_cbfs_payload_segment(&segment, ++current_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))
+			if (new->s_srcaddr < segment.load_addr)
 				break;
 		}
 



More information about the coreboot-gerrit mailing list