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>
This commit is contained in:
Furquan Shaikh 2021-10-08 15:18:33 -07:00
parent d7fb6a90e1
commit 555f040772
7 changed files with 76 additions and 84 deletions

View File

@ -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

View File

@ -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)
{
/*

View File

@ -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 @@ 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__ */

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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_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__ */