summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/cbfstool/cbfs-mkpayload.c53
-rw-r--r--util/cbfstool/cbfstool.c57
-rw-r--r--util/cbfstool/common.h5
3 files changed, 71 insertions, 44 deletions
diff --git a/util/cbfstool/cbfs-mkpayload.c b/util/cbfstool/cbfs-mkpayload.c
index da04a75785..060e9ee283 100644
--- a/util/cbfstool/cbfs-mkpayload.c
+++ b/util/cbfstool/cbfs-mkpayload.c
@@ -202,3 +202,56 @@ 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 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 2857264d26..1ad270b057 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -234,12 +234,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");
@@ -263,10 +259,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",
@@ -282,47 +274,24 @@ 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);
+ 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, &param.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, &param.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 bb18ee1b9d..bd5ef65f16 100644
--- a/util/cbfstool/common.h
+++ b/util/cbfstool/common.h
@@ -72,8 +72,13 @@ comp_func_ptr compression_function(comp_algo algo);
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);