cbfstool: 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 the add-flat-binary command creates a payload in the end , move payload
processing to cbfs-mkpayload.c.
To test:
cbfstool coreboot.rom add-flat-binary -f u-boot.bin -n fallback/payload \
-l 0x100000 -e 0x100020
To verify, get output from "cbfstool coreboot.rom print -v":
fallback/payload 0x73ccc0 payload 124920
INFO: code (no compression, offset: 0x38, load: 0x1110000, length:..)
Change-Id: Ia7bd2e6160507c0a1e8e20bc1d08397ce9826e0d
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: http://review.coreboot.org/2197
Tested-by: build bot (Jenkins)
Reviewed-by: David Hendricks <dhendrix@chromium.org>
diff --git a/util/cbfstool/cbfs-mkpayload.c b/util/cbfstool/cbfs-mkpayload.c
index da04a75..060e9ee 100644
--- a/util/cbfstool/cbfs-mkpayload.c
+++ b/util/cbfstool/cbfs-mkpayload.c
@@ -202,3 +202,56 @@
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 and
+ * causing buffer overflow. */
+ 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);
+
+ 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) {
+ WARN("Compressing data would make it bigger - disabled.\n");
+ 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/cbfstool.c b/util/cbfstool/cbfstool.c
index 2857264..1ad270b 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -234,12 +234,8 @@
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");
@@ -263,10 +259,6 @@
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",
@@ -282,47 +274,24 @@
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);
+ free(filedata);
+
+ if ((int)filesize <= 0) {
+ ERROR("Adding payload '%s' failed.\n",
+ param.filename);
free(rom);
return 1;
}
+ cbfsfile = create_cbfs_file(param.name, payload, &filesize,
+ CBFS_COMPONENT_PAYLOAD, ¶m.baseaddress);
- 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);
-
- 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..bd5ef65 100644
--- a/util/cbfstool/common.h
+++ b/util/cbfstool/common.h
@@ -72,8 +72,13 @@
uint64_t intfiletype(const char *name);
+/* 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);
+/* cbfs-mkstage.c */
int parse_elf_to_stage(unsigned char *input, unsigned char **output,
comp_algo algo, uint32_t * location);