[coreboot-gerrit] New patch to review for coreboot: move xdr interface from util/cbfstool to commonlib

George Trudeau (george.trudeau@usherbrooke.ca) gerrit at coreboot.org
Fri Apr 8 20:53:04 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 e2fa4d325893226da07031541455ce52204946f5
Author: George Trudeau <george.trudeau at usherbrooke.ca>
Date:   Mon Apr 4 00:19:02 2016 -0400

    move xdr interface from util/cbfstool to commonlib
    
    First step to transform code sections endian-agnostic.
    The goal is to replace [nh]to[hn][sll] functions and remove any
    endianness checks. The xdr interface automatically orders bytes
    into native endianness, only needs to specify data representation.
    
    Change-Id: Icb3c6a7da2d253685a3bc157bc7f5a51183c9652
    Signed-off-by: George Trudeau <george.trudeau at usherbrooke.ca>
---
 src/commonlib/Makefile.inc            |   4 +
 src/commonlib/include/commonlib/xdr.h |  46 ++++++++++
 src/commonlib/xdr.c                   | 165 ++++++++++++++++++++++++++++++++++
 util/cbfstool/common.h                |  24 +----
 util/cbfstool/xdr.c                   | 152 -------------------------------
 5 files changed, 216 insertions(+), 175 deletions(-)

diff --git a/src/commonlib/Makefile.inc b/src/commonlib/Makefile.inc
index 7c14f7c..7e0be9f 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 += xdr.c
+romstage-y += xdr.c
+ramstage-y += xdr.c
diff --git a/src/commonlib/include/commonlib/xdr.h b/src/commonlib/include/commonlib/xdr.h
new file mode 100644
index 0000000..80d6671
--- /dev/null
+++ b/src/commonlib/include/commonlib/xdr.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2009 coresystems GmbH
+ *                 written by Patrick Georgi <patrick.georgi at coresystems.de>
+ * Copyright (C) 2012 Google, Inc.
+ *
+ * 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 _XDR_H_
+#define _XDR_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+/* Buffer and file I/O */
+struct buffer {
+	char *name;
+	char *data;
+	size_t offset;
+	size_t size;
+};
+
+struct xdr {
+	uint8_t (*get8)(struct buffer *input);
+	uint16_t (*get16)(struct buffer *input);
+	uint32_t (*get32)(struct buffer *input);
+	uint64_t (*get64)(struct buffer *input);
+	void (*put8)(struct buffer *input, uint8_t val);
+	void (*put16)(struct buffer *input, uint16_t val);
+	void (*put32)(struct buffer *input, uint32_t val);
+	void (*put64)(struct buffer *input, uint64_t val);
+};
+
+extern struct xdr xdr_le, xdr_be;
+
+size_t bgets(struct buffer *input, void *output, size_t len);
+size_t bputs(struct buffer *b, const void *data, size_t len);
+
+#endif
diff --git a/src/commonlib/xdr.c b/src/commonlib/xdr.c
new file mode 100644
index 0000000..03fa081
--- /dev/null
+++ b/src/commonlib/xdr.c
@@ -0,0 +1,165 @@
+ /*
+ * cbfstool, CLI utility for CBFS file manipulation
+ *
+ * Copyright 2013 Google Inc.
+ *
+ * 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 <stdint.h>
+#include <string.h>
+
+#include <commonlib/xdr.h>
+
+size_t bgets(struct buffer *input, void *output, size_t len)
+{
+	len = input->size < len ? input->size : len;
+	memmove(output, input->data, len);
+	input->data += len;
+	input->size -= len;
+
+	return len;
+}
+
+size_t bputs(struct buffer *b, const void *data, size_t len)
+{
+	memmove(&b->data[b->size], data, len);
+	b->size += len;
+
+	return len;
+}
+
+/* The assumption in all this code is that we're given a pointer to enough data.
+ * Hence, we do not check for underflow.
+ */
+static uint8_t get8(struct buffer *input)
+{
+	uint8_t ret = *input->data++;
+
+	input->size--;
+
+	return ret;
+}
+
+static uint16_t get16be(struct buffer *input)
+{
+	uint16_t ret;
+
+	ret = get8(input) << 8;
+	ret |= get8(input);
+
+	return ret;
+}
+
+static uint32_t get32be(struct buffer *input)
+{
+	uint32_t ret;
+
+	ret = get16be(input) << 16;
+	ret |= get16be(input);
+
+	return ret;
+}
+
+static uint64_t get64be(struct buffer *input)
+{
+	uint64_t ret;
+
+	ret = get32be(input);
+	ret <<= 32;
+	ret |= get32be(input);
+
+	return ret;
+}
+
+static void put8(struct buffer *input, uint8_t val)
+{
+	input->data[input->size] = val;
+	input->size++;
+}
+
+static void put16be(struct buffer *input, uint16_t val)
+{
+	put8(input, val >> 8);
+	put8(input, val);
+}
+
+static void put32be(struct buffer *input, uint32_t val)
+{
+	put16be(input, val >> 16);
+	put16be(input, val);
+}
+
+static void put64be(struct buffer *input, uint64_t val)
+{
+	put32be(input, val >> 32);
+	put32be(input, val);
+}
+
+static uint16_t get16le(struct buffer *input)
+{
+	uint16_t ret;
+
+	ret = get8(input);
+	ret |= get8(input) << 8;
+
+	return ret;
+}
+
+static uint32_t get32le(struct buffer *input)
+{
+	uint32_t ret;
+
+	ret = get16le(input);
+	ret |= get16le(input) << 16;
+
+	return ret;
+}
+
+static uint64_t get64le(struct buffer *input)
+{
+	uint64_t ret;
+	uint32_t low;
+
+	low = get32le(input);
+	ret = get32le(input);
+	ret <<= 32;
+	ret |= low;
+
+	return ret;
+}
+
+static void put16le(struct buffer *input, uint16_t val)
+{
+	put8(input, val);
+	put8(input, val >> 8);
+}
+
+static void put32le(struct buffer *input, uint32_t val)
+{
+	put16le(input, val);
+	put16le(input, val >> 16);
+}
+
+static void put64le(struct buffer *input, uint64_t val)
+{
+	put32le(input, val);
+	put32le(input, val >> 32);
+}
+
+struct xdr xdr_be = {
+	get8, get16be, get32be, get64be,
+	put8, put16be, put32be, put64be
+};
+
+struct xdr xdr_le = {
+	get8, get16le, get32le, get64le,
+	put8, put16le, put32le, put64le
+};
diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h
index ea6f3cf..22e11fd 100644
--- a/util/cbfstool/common.h
+++ b/util/cbfstool/common.h
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <assert.h>
 
+#include <commonlib/xdr.h>
 #include <console/console.h>
 
 /* Endianess */
@@ -44,14 +45,6 @@ static inline uint32_t align_up(uint32_t value, uint32_t align)
 	return value;
 }
 
-/* Buffer and file I/O */
-struct buffer {
-	char *name;
-	char *data;
-	size_t offset;
-	size_t size;
-};
-
 static inline void *buffer_get(const struct buffer *b)
 {
 	return b->data;
@@ -204,21 +197,6 @@ int do_lzma_compress(char *in, int in_len, char *out, int *out_len);
 int do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len,
 			size_t *actual_size);
 
-/* xdr.c */
-struct xdr {
-	uint8_t (*get8)(struct buffer *input);
-	uint16_t (*get16)(struct buffer *input);
-	uint32_t (*get32)(struct buffer *input);
-	uint64_t (*get64)(struct buffer *input);
-	void (*put8)(struct buffer *input, uint8_t val);
-	void (*put16)(struct buffer *input, uint16_t val);
-	void (*put32)(struct buffer *input, uint32_t val);
-	void (*put64)(struct buffer *input, uint64_t val);
-};
-
-extern struct xdr xdr_le, xdr_be;
-size_t bgets(struct buffer *input, void *output, size_t len);
-size_t bputs(struct buffer *b, const void *data, size_t len);
 
 /* Returns a 0-terminated string containing a hex representation of
  * len bytes starting at data.
diff --git a/util/cbfstool/xdr.c b/util/cbfstool/xdr.c
deleted file mode 100644
index 06cc91f..0000000
--- a/util/cbfstool/xdr.c
+++ /dev/null
@@ -1,152 +0,0 @@
- /*
- * cbfstool, CLI utility for CBFS file manipulation
- *
- * Copyright 2013 Google Inc.
- *
- * 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 <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <unistd.h>
-#include <stdint.h>
-#include "common.h"
-
-size_t bgets(struct buffer *input, void *output, size_t len)
-{
-	len = input->size < len ? input->size : len;
-	memmove(output, input->data, len);
-	input->data += len;
-	input->size -= len;
-	return len;
-}
-
-size_t bputs(struct buffer *b, const void *data, size_t len)
-{
-	memmove(&b->data[b->size], data, len);
-	b->size += len;
-	return len;
-}
-
-/* The assumption in all this code is that we're given a pointer to enough data.
- * Hence, we do not check for underflow.
- */
-static uint8_t get8(struct buffer *input)
-{
-	uint8_t ret = *input->data++;
-	input->size--;
-	return ret;
-}
-
-static uint16_t get16be(struct buffer *input)
-{
-	uint16_t ret;
-	ret = get8(input) << 8;
-	ret |= get8(input);
-	return ret;
-}
-
-static uint32_t get32be(struct buffer *input)
-{
-	uint32_t ret;
-	ret = get16be(input) << 16;
-	ret |= get16be(input);
-	return ret;
-}
-
-static uint64_t get64be(struct buffer *input)
-{
-	uint64_t ret;
-	ret = get32be(input);
-	ret <<= 32;
-	ret |= get32be(input);
-	return ret;
-}
-
-static void put8(struct buffer *input, uint8_t val)
-{
-	input->data[input->size] = val;
-	input->size++;
-}
-
-static void put16be(struct buffer *input, uint16_t val)
-{
-	put8(input, val >> 8);
-	put8(input, val);
-}
-
-static void put32be(struct buffer *input, uint32_t val)
-{
-	put16be(input, val >> 16);
-	put16be(input, val);
-}
-
-static void put64be(struct buffer *input, uint64_t val)
-{
-	put32be(input, val >> 32);
-	put32be(input, val);
-}
-
-static uint16_t get16le(struct buffer *input)
-{
-	uint16_t ret;
-	ret = get8(input);
-	ret |= get8(input) << 8;
-	return ret;
-}
-
-static uint32_t get32le(struct buffer *input)
-{
-	uint32_t ret;
-	ret = get16le(input);
-	ret |= get16le(input) << 16;
-	return ret;
-}
-
-static uint64_t get64le(struct buffer *input)
-{
-	uint64_t ret;
-	uint32_t low;
-	low = get32le(input);
-	ret = get32le(input);
-	ret <<= 32;
-	ret |= low;
-	return ret;
-}
-
-static void put16le(struct buffer *input, uint16_t val)
-{
-	put8(input, val);
-	put8(input, val >> 8);
-}
-
-static void put32le(struct buffer *input, uint32_t val)
-{
-	put16le(input, val);
-	put16le(input, val >> 16);
-}
-
-static void put64le(struct buffer *input, uint64_t val)
-{
-	put32le(input, val);
-	put32le(input, val >> 32);
-}
-
-struct xdr xdr_be = {
-	get8, get16be, get32be, get64be,
-	put8, put16be, put32be, put64be
-};
-
-struct xdr xdr_le = {
-	get8, get16le, get32le, get64le,
-	put8, put16le, put32le, put64le
-};



More information about the coreboot-gerrit mailing list