diff --git a/util/cbfstool/Makefile.inc b/util/cbfstool/Makefile.inc index 0a445b5942..4651f512a3 100644 --- a/util/cbfstool/Makefile.inc +++ b/util/cbfstool/Makefile.inc @@ -100,12 +100,14 @@ include $(top)/util/cbfstool/fpt_formats/Makefile.inc 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 bde3a8c165..51fa9d0fbc 100644 --- a/util/cbfstool/cse_fpt.c +++ b/util/cbfstool/cse_fpt.c @@ -64,30 +64,6 @@ static void usage(const char *name) 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 eda8a07930..3f9c1855b4 100644 --- a/util/cbfstool/cse_fpt.h +++ b/util/cbfstool/cse_fpt.h @@ -7,12 +7,11 @@ #include #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 @@ struct fpt_hdr_ops { 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 0000000000..c60005a21a --- /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 + +#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 0000000000..58ba128751 --- /dev/null +++ b/util/cbfstool/cse_helpers.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include + +#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 cc49671d19..c8bd1040e3 100644 --- a/util/cbfstool/cse_serger.c +++ b/util/cbfstool/cse_serger.c @@ -165,56 +165,6 @@ static int subpart_get_type_from_name(const char *name) 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 b60a9c7a0a..8727f59422 100644 --- a/util/cbfstool/cse_serger.h +++ b/util/cbfstool/cse_serger.h @@ -7,14 +7,12 @@ #include #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_hdr_ops subpart_hdr_2_ops; 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__ */