From 3cfacbf1961accff8670997368b403d8068ad94c Mon Sep 17 00:00:00 2001 From: Hung-Te Lin Date: Wed, 30 Jan 2013 00:43:46 +0800 Subject: cbfstool: Add buffer management API. Many functions in cbfstool need to deal with a memory buffer - both location and size. Right now it's made by different ways: for ROM image using global variable (romsize, master_header); and in cbfs-* using return value for size and char** to return memory location. This may cause bugs like assuming incorrect return types, ex: uint32_t file_size = parse(); // which returns "-1" on error if (file_size <= 0) { ... And the parse error will never be caught. We can simplify this by introducing a buffer API, to change unsigned int do_something(char *input, size_t len, char **output, ...) into int do_something(struct buffer *input, struct buffer *output, ...) The buffer API will be used by further commits. Change-Id: Iaddaeb109f08be6be84c6728d72c6a043b0e7a9f Signed-off-by: Hung-Te Lin Reviewed-on: http://review.coreboot.org/2205 Tested-by: build bot (Jenkins) Reviewed-by: Stefan Reinauer --- util/cbfstool/common.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'util/cbfstool/common.h') diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h index 5315e69bed..a859e103f2 100644 --- a/util/cbfstool/common.h +++ b/util/cbfstool/common.h @@ -21,6 +21,7 @@ #define __CBFSTOOL_COMMON_H #include +#include /* Endianess */ #include "swab.h" @@ -40,6 +41,27 @@ extern int verbose; #define INFO(x...) { if (verbose > 0) fprintf(stderr, "INFO: " x); } #define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); } +/* Buffer and file I/O */ +struct buffer { + char *name; + char *data; + size_t size; +}; + +/* Creates an empty memory buffer with given size. + * Returns 0 on success, otherwise non-zero. */ +int buffer_create(struct buffer *buffer, size_t size, const char *name); + +/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */ +int buffer_from_file(struct buffer *buffer, const char *filename); + +/* Writes memory buffer content into file. + * Returns 0 on success, otherwise non-zero. */ +int buffer_write_file(struct buffer *buffer, const char *filename); + +/* Destroys a memory buffer. */ +void buffer_delete(struct buffer *buffer); + extern void *offset; extern uint32_t romsize; extern int host_bigendian; -- cgit v1.2.3