Hung-Te Lin (hungte@chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/2197
-gerrit
commit f3a8ddd63de4e9f039e0ef94626b4e8fdbf6bebe Author: Hung-Te Lin hungte@chromium.org Date: Mon Jan 28 15:04:30 2013 +0800
cbfs: move flat-binary parsing to cbfs-mkpayload.
The ELF parsing and payload building in add-flat-binary command should be isolated just like mkpayload and mkstage.
Since it's for creating a payload in the end, move payload processing to cbfs-mkpayload.c.
Change-Id: Ia7bd2e6160507c0a1e8e20bc1d08397ce9826e0d Signed-off-by: Hung-Te Lin hungte@chromium.org --- util/cbfstool/cbfs-mkpayload.c | 58 ++++++++++++++++++++++++++++++++++++++---- util/cbfstool/cbfs-mkstage.c | 6 +---- util/cbfstool/cbfstool.c | 52 ++++++++----------------------------- util/cbfstool/common.h | 16 ++++++++---- 4 files changed, 76 insertions(+), 56 deletions(-)
diff --git a/util/cbfstool/cbfs-mkpayload.c b/util/cbfstool/cbfs-mkpayload.c index fc119d2..584ddb6 100644 --- a/util/cbfstool/cbfs-mkpayload.c +++ b/util/cbfstool/cbfs-mkpayload.c @@ -22,13 +22,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#include "elf.h" -#include <fcntl.h> -#include <getopt.h> -#include <sys/stat.h>
#include "common.h" +#include "elf.h" #include "cbfs.h"
int parse_elf_to_payload(unsigned char *input, unsigned char **output, @@ -206,3 +202,55 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output, err: return -1; } + +int parse_flat_binary_to_payload(unsigned char *input, unsigned char **output, + int32_t input_size, + uint32_t loadaddress, + uint32_t entrypoint, + comp_algo algo) +{ + comp_func_ptr compress; + unsigned char *payload; + struct cbfs_payload_segment *segs; + int doffset, len = 0; + + compress = compression_function(algo); + if (!compress) + return -1; + + DEBUG("start: parse_flat_binary_to_payload\n"); + + /* FIXME compressed file size might be bigger than original file */ + payload = calloc((2 * sizeof(struct cbfs_payload_segment)) + input_size, 1); + if (payload == NULL) { + ERROR("Could not allocate memory.\n"); + return -1; + } + + segs = (struct cbfs_payload_segment *)payload; + doffset = (2 * sizeof(*segs)); + + /* Prepare code segment */ + segs[0].type = PAYLOAD_SEGMENT_CODE; + segs[0].load_addr = htonll(loadaddress); + segs[0].mem_len = htonl(input_size); + segs[0].offset = htonl(doffset); + + /* TODO what if compressed file size is bigger than original? */ + compress((char*)input, input_size, (char*)payload + doffset, &len); + segs[0].compression = htonl(algo); + segs[0].len = htonl(len); + + if ((unsigned int)len >= input_size) { + segs[0].compression = 0; + segs[0].len = htonl(input_size); + memcpy(payload + doffset, input, input_size); + } + + /* prepare entry point segment */ + segs[1].type = PAYLOAD_SEGMENT_ENTRY; + segs[1].load_addr = htonll(entrypoint); + *output = payload; + + return doffset + ntohl(segs[0].len); +} diff --git a/util/cbfstool/cbfs-mkstage.c b/util/cbfstool/cbfs-mkstage.c index 743ea38..ef5182a 100644 --- a/util/cbfstool/cbfs-mkstage.c +++ b/util/cbfstool/cbfs-mkstage.c @@ -23,14 +23,10 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#include "elf.h" -#include <fcntl.h> -#include <getopt.h> -#include <sys/stat.h>
#include "common.h" #include "cbfs.h" +#include "elf.h"
static unsigned int idemp(unsigned int x) { diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 5e3ebf9..9aff4ca 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -231,12 +231,8 @@ static int cbfs_add_stage(void) static int cbfs_add_flat_binary(void) { uint32_t filesize = 0; - uint32_t final_size; void *rom, *filedata, *cbfsfile; unsigned char *payload; - comp_func_ptr compress; - struct cbfs_payload_segment *segs; - int doffset, len = 0;
if (!param.filename) { ERROR("You need to specify -f/--filename.\n"); @@ -260,10 +256,6 @@ static int cbfs_add_flat_binary(void) return 1; }
- compress = compression_function(param.algo); - if (!compress) - return 1; - rom = loadrom(param.cbfs_name); if (rom == NULL) { ERROR("Could not load ROM image '%s'.\n", @@ -279,47 +271,25 @@ static int cbfs_add_flat_binary(void) return 1; }
- /* FIXME compressed file size might be bigger than original file */ - payload = calloc((2 * sizeof(struct cbfs_payload_segment)) + filesize, 1); - if (payload == NULL) { - ERROR("Could not allocate memory.\n"); - free(filedata); + filesize = parse_flat_binary_to_payload(filedata, &payload, + filesize, + param.loadaddress, + param.entrypoint, + param.algo); + if (filesize <= 0) { + ERROR("Adding payload '%s' failed.\n", + param.filename); free(rom); return 1; }
- segs = (struct cbfs_payload_segment *)payload; - doffset = (2 * sizeof(struct cbfs_payload_segment)); - - /* Prepare code segment */ - segs[0].type = PAYLOAD_SEGMENT_CODE; - segs[0].load_addr = (uint64_t)htonll(param.loadaddress); - segs[0].mem_len = (uint32_t)htonl(filesize); - segs[0].offset = (uint32_t)htonl(doffset); - - compress(filedata, filesize, (char *)(payload + doffset), &len); - segs[0].compression = htonl(param.algo); - segs[0].len = htonl(len); - - if ((unsigned int)len >= filesize) { - segs[0].compression = 0; - segs[0].len = htonl(filesize); - memcpy((char *)(payload + doffset), filedata, filesize); - } - - /* prepare entry point segment */ - segs[1].type = PAYLOAD_SEGMENT_ENTRY; - segs[1].load_addr = (uint64_t)htonll(param.entrypoint); - - final_size = doffset + ntohl(segs[0].len); - cbfsfile = - create_cbfs_file(param.name, payload, &final_size, - CBFS_COMPONENT_PAYLOAD, ¶m.baseaddress); + cbfsfile = create_cbfs_file(param.name, payload, &filesize, + CBFS_COMPONENT_PAYLOAD, ¶m.baseaddress);
free(filedata); free(payload);
- if (add_file_to_cbfs(cbfsfile, final_size, param.baseaddress)) { + if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) { ERROR("Adding payload '%s' failed.\n", param.filename); free(cbfsfile); diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h index bb18ee1..7b92179 100644 --- a/util/cbfstool/common.h +++ b/util/cbfstool/common.h @@ -72,11 +72,6 @@ comp_func_ptr compression_function(comp_algo algo);
uint64_t intfiletype(const char *name);
-int parse_elf_to_payload(unsigned char *input, unsigned char **output, - comp_algo algo); -int parse_elf_to_stage(unsigned char *input, unsigned char **output, - comp_algo algo, uint32_t * location); - void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, uint32_t type, uint32_t * location);
@@ -94,6 +89,17 @@ uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
void print_supported_filetypes(void);
+/* cbfs-mkstage.c */ +int parse_elf_to_stage(unsigned char *input, unsigned char **output, + comp_algo algo, uint32_t * location); +/* cbfs-mkpayload.c */ +int parse_elf_to_payload(unsigned char *input, unsigned char **output, + comp_algo algo); +int parse_flat_binary_to_payload(unsigned char *input, unsigned char **output, + int32_t input_size, uint32_t loadaddress, + uint32_t entrypoint, comp_algo algo); + + #define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
#endif