[coreboot-gerrit] New patch to review for coreboot: replace data mapping from region_device

George Trudeau (george.trudeau@usherbrooke.ca) gerrit at coreboot.org
Fri Apr 8 20:53:05 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/14295

-gerrit

commit 1390f6faf2331a2058c6d2405057a08b2c118f4f
Author: George Trudeau <george.trudeau at usherbrooke.ca>
Date:   Mon Apr 4 00:57:31 2016 -0400

    replace data mapping from region_device
    
    Use the xdr interface in the mapping table rdev_mapper.
    It contains the mappging functions to be passed when
    calling rdev_map_data. It provides a generic interface
    to map data from region_device to local structures.
    
    Change-Id: I85c0491a235c726b6ea1935453dd360931ac4758
    Signed-off-by: George Trudeau <george.trudeau at usherbrooke.ca>
---
 src/commonlib/Makefile.inc               |  4 +++
 src/commonlib/include/commonlib/mapper.h | 27 ++++++++++++++++++++
 src/commonlib/include/commonlib/region.h |  6 +++++
 src/commonlib/mapper.c                   | 43 ++++++++++++++++++++++++++++++++
 src/commonlib/region.c                   | 27 ++++++++++++++++++++
 src/lib/cbfs.c                           | 11 ++++----
 6 files changed, 112 insertions(+), 6 deletions(-)

diff --git a/src/commonlib/Makefile.inc b/src/commonlib/Makefile.inc
index 7e0be9f..8f7cb1c 100644
--- a/src/commonlib/Makefile.inc
+++ b/src/commonlib/Makefile.inc
@@ -29,3 +29,7 @@ postcar-y += lz4_wrapper.c
 bootblock-y += xdr.c
 romstage-y += xdr.c
 ramstage-y += xdr.c
+
+bootblock-y += mapper.c
+romstage-y += mapper.c
+ramstage-y += mapper.c
diff --git a/src/commonlib/include/commonlib/mapper.h b/src/commonlib/include/commonlib/mapper.h
new file mode 100644
index 0000000..4b818f3
--- /dev/null
+++ b/src/commonlib/include/commonlib/mapper.h
@@ -0,0 +1,27 @@
+/*
+ * 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 _MAPPER_H
+#define _MAPPER_H
+
+#include <commonlib/xdr.h>
+#include <commonlib/cbfs_serialized.h> // struct cbfs_header
+
+extern struct rdev_mapper {
+	void (*cbfs_header)(struct buffer*, void*);
+	void (*int32)(struct buffer*, void*);
+} rdev_mapper;
+
+#endif
diff --git a/src/commonlib/include/commonlib/region.h b/src/commonlib/include/commonlib/region.h
index a13c66c..b8f120e 100644
--- a/src/commonlib/include/commonlib/region.h
+++ b/src/commonlib/include/commonlib/region.h
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <stddef.h>
 #include <commonlib/mem_pool.h>
+#include <commonlib/mapper.h>
 
 /*
  * Region support.
@@ -49,6 +50,11 @@ ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset,
 			size_t size);
 
 
+ssize_t rdev_map_data(const struct region_device *rd, void *data,
+			void (*map)(struct buffer*, void*),
+			size_t offset, size_t size);
+
+
 /****************************************
  *  Implementation of a region device   *
  ****************************************/
diff --git a/src/commonlib/mapper.c b/src/commonlib/mapper.c
new file mode 100644
index 0000000..fac2a16
--- /dev/null
+++ b/src/commonlib/mapper.c
@@ -0,0 +1,43 @@
+/*
+ * 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/mapper.h>
+
+static void map_cbfs_header(struct buffer *buf, void *data)
+{
+	struct cbfs_header *header = (struct cbfs_header *) data;
+
+	header->magic         = xdr_be.get32(buf);
+	header->version       = xdr_be.get32(buf);
+	header->romsize       = xdr_be.get32(buf);
+	header->bootblocksize = xdr_be.get32(buf);
+	header->align         = xdr_be.get32(buf);
+	header->offset        = xdr_be.get32(buf);
+	header->architecture  = xdr_be.get32(buf);
+	header->pad[1]        = xdr_be.get32(buf);
+}
+
+
+static void map_int32(struct buffer *buf, void *data)
+{
+	int32_t *int32 = (int32_t *) data;
+
+	*int32 = xdr_be.get32(buf);
+}
+
+struct rdev_mapper rdev_mapper = {
+	.cbfs_header = map_cbfs_header,
+	.int32       = map_int32,
+};
diff --git a/src/commonlib/region.c b/src/commonlib/region.c
index bfc5fc8..2d35f60 100644
--- a/src/commonlib/region.c
+++ b/src/commonlib/region.c
@@ -103,6 +103,33 @@ ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset,
 	return rdev->ops->readat(rdev, b, req.offset, req.size);
 }
 
+static void rdev_set_buffer(struct buffer *buf,
+			const struct region_device *rd)
+{
+	const struct mem_region_device *mdev;
+
+	mdev = container_of(rd, __typeof__(*mdev), rdev);
+
+	buf->data = mdev->base;
+}
+
+ssize_t rdev_map_data(const struct region_device *rd, void *data,
+			void (*map)(struct buffer*, void*),
+			size_t offset, size_t size)
+{
+	struct buffer buf = { .offset = offset, .size = size };
+	struct region req = { .offset = offset, .size = size };
+
+	if (!normalize_and_ok(&rd->region, &req))
+		return -1;
+
+	rdev_set_buffer(&buf, rdev_root(rd));
+
+	map(&buf, data);
+
+	return 0;
+}
+
 int rdev_chain(struct region_device *child, const struct region_device *parent,
 		size_t offset, size_t size)
 {
diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c
index e1626d7..d4c080c 100644
--- a/src/lib/cbfs.c
+++ b/src/lib/cbfs.c
@@ -20,6 +20,7 @@
 #include <boot_device.h>
 #include <cbfs.h>
 #include <commonlib/compression.h>
+#include <commonlib/mapper.h>
 #include <endian.h>
 #include <lib.h>
 #include <symbols.h>
@@ -230,17 +231,15 @@ static int cbfs_master_header_props(struct cbfs_props *props)
 	/* Find location of header using signed 32-bit offset from
 	 * end of CBFS region. */
 	offset = CONFIG_CBFS_SIZE - sizeof(int32_t);
-	if (rdev_readat(bdev, &rel_offset, offset, sizeof(int32_t)) < 0)
+	if (rdev_map_data(bdev, &rel_offset, rdev_mapper.int32,
+				offset, sizeof(int32_t)) < 0)
 		return -1;
 
 	offset = CONFIG_CBFS_SIZE + rel_offset;
-	if (rdev_readat(bdev, &header, offset, sizeof(header)) < 0)
+	if (rdev_map_data(bdev, &header, rdev_mapper.cbfs_header,
+				offset, sizeof(header)) < 0)
 		return -1;
 
-	header.magic = ntohl(header.magic);
-	header.romsize = ntohl(header.romsize);
-	header.offset = ntohl(header.offset);
-
 	if (header.magic != CBFS_HEADER_MAGIC)
 		return -1;
 



More information about the coreboot-gerrit mailing list