util/cse*: Add cse_helpers.{c,h}
This change moves `read_member` and `write_member` helper functions
out of cse_fpt.c and cse_serger.c into cse_helpers.c to avoid
duplication.
BUG=b:189177186,b:189167923
Change-Id: I7b646b29c9058d892bb0fc9824ef1b4340d2510c
Signed-off-by: Furquan Shaikh <furquan@google.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/58201
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
diff --git a/util/cbfstool/Makefile.inc b/util/cbfstool/Makefile.inc
index 0a445b5..4651f51 100644
--- a/util/cbfstool/Makefile.inc
+++ b/util/cbfstool/Makefile.inc
@@ -100,12 +100,14 @@
cse_fpt_obj :=
cse_fpt_obj += cse_fpt.o
cse_fpt_obj += common.o
+cse_fpt_obj += cse_helpers.o
cse_fpt_obj += $(foreach var, $(fpt_formats_obj), $(var))
include $(top)/util/cbfstool/bpdt_formats/Makefile.inc
cse_serger_obj :=
cse_serger_obj += cse_serger.o
cse_serger_obj += common.o
+cse_serger_obj += cse_helpers.o
cse_serger_obj += $(foreach var, $(bpdt_formats_obj), $(var))
TOOLCFLAGS ?= -Werror -Wall -Wextra -Wshadow
diff --git a/util/cbfstool/cse_fpt.c b/util/cbfstool/cse_fpt.c
index bde3a8c..51fa9d0 100644
--- a/util/cbfstool/cse_fpt.c
+++ b/util/cbfstool/cse_fpt.c
@@ -64,30 +64,6 @@
name, name);
}
-void read_member(struct buffer *buff, void *dst, size_t size)
-{
- uint8_t *src = buffer_get(buff);
-
- switch (size) {
- case 1:
- *(uint8_t *)dst = read_le8(src);
- break;
- case 2:
- *(uint16_t *)dst = read_le16(src);
- break;
- case 4:
- *(uint32_t *)dst = read_le32(src);
- break;
- case 8:
- *(uint64_t *)dst = read_le64(src);
- break;
- default:
- memcpy(dst, src, size);
- }
-
- buffer_seek(buff, size);
-}
-
static int get_fpt_buff(struct buffer *input_buff, struct buffer *fpt_buff)
{
/*
diff --git a/util/cbfstool/cse_fpt.h b/util/cbfstool/cse_fpt.h
index eda8a07..3f9c185 100644
--- a/util/cbfstool/cse_fpt.h
+++ b/util/cbfstool/cse_fpt.h
@@ -7,12 +7,11 @@
#include <commonlib/region.h>
#include "common.h"
+#include "cse_helpers.h"
#define FPT_MARKER "$FPT"
#define FPT_ENTRY_VERSION 0x10
-#define READ_MEMBER(_buff, _x) read_member(_buff, &(_x), sizeof(_x))
-
enum fpt_hdr_version {
FPT_HDR_VERSION_20 = 0x20,
FPT_HDR_VERSION_21 = 0x21,
@@ -32,6 +31,4 @@
extern const struct fpt_hdr_ops fpt_hdr_20_ops;
extern const struct fpt_hdr_ops fpt_hdr_21_ops;
-void read_member(struct buffer *buff, void *dst, size_t size);
-
#endif /* __CBFSTOOL_CSE_FPT_H__ */
diff --git a/util/cbfstool/cse_helpers.c b/util/cbfstool/cse_helpers.c
new file mode 100644
index 0000000..c60005a
--- /dev/null
+++ b/util/cbfstool/cse_helpers.c
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Helper functions for cse_serger and cse_fpt */
+
+#include <commonlib/endian.h>
+
+#include "cse_helpers.h"
+
+void write_member(struct buffer *buff, void *src, size_t size)
+{
+ void *dst = buffer_get(buff);
+
+ switch (size) {
+ case 1:
+ write_le8(dst, *(uint8_t *)src);
+ break;
+ case 2:
+ write_le16(dst, *(uint16_t *)src);
+ break;
+ case 4:
+ write_le32(dst, *(uint32_t *)src);
+ break;
+ case 8:
+ write_le64(dst, *(uint64_t *)src);
+ break;
+ default:
+ memcpy(dst, src, size);
+ break;
+ }
+
+ buffer_seek(buff, size);
+}
+
+void read_member(struct buffer *buff, void *dst, size_t size)
+{
+ const void *src = buffer_get(buff);
+
+ switch (size) {
+ case 1:
+ *(uint8_t *)dst = read_le8(src);
+ break;
+ case 2:
+ *(uint16_t *)dst = read_le16(src);
+ break;
+ case 4:
+ *(uint32_t *)dst = read_le32(src);
+ break;
+ case 8:
+ *(uint64_t *)dst = read_le64(src);
+ break;
+ default:
+ memcpy(dst, src, size);
+ break;
+ }
+
+ buffer_seek(buff, size);
+}
diff --git a/util/cbfstool/cse_helpers.h b/util/cbfstool/cse_helpers.h
new file mode 100644
index 0000000..58ba128
--- /dev/null
+++ b/util/cbfstool/cse_helpers.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <stddef.h>
+
+#include "common.h"
+
+#define READ_MEMBER(_buff, _x) read_member(_buff, &(_x), sizeof(_x))
+#define WRITE_MEMBER(_buff, _x) write_member(_buff, &(_x), sizeof(_x))
+
+/* Write data from `src` of length `size` to buffer in little-endian format and seek the buffer
+ forward by `size` bytes. */
+void write_member(struct buffer *buff, void *src, size_t size);
+
+/* Read data from buffer in little-endian format of length `size` to `dst` and seek the buffer
+ forward by `size` bytes. */
+void read_member(struct buffer *buff, void *dst, size_t size);
diff --git a/util/cbfstool/cse_serger.c b/util/cbfstool/cse_serger.c
index cc49671..c8bd104 100644
--- a/util/cbfstool/cse_serger.c
+++ b/util/cbfstool/cse_serger.c
@@ -165,56 +165,6 @@
return -1;
}
-void write_member(struct buffer *buff, void *src, size_t size)
-{
- void *dst = buffer_get(buff);
-
- switch (size) {
- case 1:
- write_le8(dst, *(uint8_t *)src);
- break;
- case 2:
- write_le16(dst, *(uint16_t *)src);
- break;
- case 4:
- write_le32(dst, *(uint32_t *)src);
- break;
- case 8:
- write_le64(dst, *(uint64_t *)src);
- break;
- default:
- memcpy(dst, src, size);
- break;
- }
-
- buffer_seek(buff, size);
-}
-
-void read_member(struct buffer *buff, void *dst, size_t size)
-{
- const void *src = buffer_get(buff);
-
- switch (size) {
- case 1:
- *(uint8_t *)dst = read_le8(src);
- break;
- case 2:
- *(uint16_t *)dst = read_le16(src);
- break;
- case 4:
- *(uint32_t *)dst = read_le32(src);
- break;
- case 8:
- *(uint64_t *)dst = read_le64(src);
- break;
- default:
- memcpy(dst, src, size);
- break;
- }
-
- buffer_seek(buff, size);
-}
-
static const struct bpdt_ops *get_bpdt_ops(const struct buffer *buff)
{
assert(buff || params.version_str);
diff --git a/util/cbfstool/cse_serger.h b/util/cbfstool/cse_serger.h
index b60a9c7..8727f59 100644
--- a/util/cbfstool/cse_serger.h
+++ b/util/cbfstool/cse_serger.h
@@ -7,14 +7,12 @@
#include <commonlib/region.h>
#include "common.h"
+#include "cse_helpers.h"
#define BPDT_SIGNATURE (0x000055AA)
#define BUFF_SIZE_ALIGN (4 * KiB)
-#define READ_MEMBER(_buff, _x) read_member(_buff, &(_x), sizeof(_x))
-#define WRITE_MEMBER(_buff, _x) write_member(_buff, &(_x), sizeof(_x))
-
enum bpdt_version {
BPDT_VERSION_1_6 = 1,
BPDT_VERSION_1_7 = 2,
@@ -89,7 +87,4 @@
extern const struct subpart_entry_ops subpart_entry_1_ops;
-void read_member(struct buffer *buff, void *dst, size_t size);
-void write_member(struct buffer *buff, void *src, size_t size);
-
#endif /* __CBFSTOOL_CSE_SERGER_H__ */