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>
This commit is contained in:
parent
4d87d4e09b
commit
05dccae75d
|
@ -202,3 +202,56 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
|
||||||
err:
|
err:
|
||||||
return -1;
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -234,12 +234,8 @@ static int cbfs_add_stage(void)
|
||||||
static int cbfs_add_flat_binary(void)
|
static int cbfs_add_flat_binary(void)
|
||||||
{
|
{
|
||||||
uint32_t filesize = 0;
|
uint32_t filesize = 0;
|
||||||
uint32_t final_size;
|
|
||||||
void *rom, *filedata, *cbfsfile;
|
void *rom, *filedata, *cbfsfile;
|
||||||
unsigned char *payload;
|
unsigned char *payload;
|
||||||
comp_func_ptr compress;
|
|
||||||
struct cbfs_payload_segment *segs;
|
|
||||||
int doffset, len = 0;
|
|
||||||
|
|
||||||
if (!param.filename) {
|
if (!param.filename) {
|
||||||
ERROR("You need to specify -f/--filename.\n");
|
ERROR("You need to specify -f/--filename.\n");
|
||||||
|
@ -263,10 +259,6 @@ static int cbfs_add_flat_binary(void)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
compress = compression_function(param.algo);
|
|
||||||
if (!compress)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
rom = loadrom(param.cbfs_name);
|
rom = loadrom(param.cbfs_name);
|
||||||
if (rom == NULL) {
|
if (rom == NULL) {
|
||||||
ERROR("Could not load ROM image '%s'.\n",
|
ERROR("Could not load ROM image '%s'.\n",
|
||||||
|
@ -282,47 +274,24 @@ static int cbfs_add_flat_binary(void)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* FIXME compressed file size might be bigger than original file */
|
filesize = parse_flat_binary_to_payload(filedata, &payload,
|
||||||
payload = calloc((2 * sizeof(struct cbfs_payload_segment)) + filesize, 1);
|
filesize,
|
||||||
if (payload == NULL) {
|
param.loadaddress,
|
||||||
ERROR("Could not allocate memory.\n");
|
param.entrypoint,
|
||||||
free(filedata);
|
param.algo);
|
||||||
|
free(filedata);
|
||||||
|
|
||||||
|
if ((int)filesize <= 0) {
|
||||||
|
ERROR("Adding payload '%s' failed.\n",
|
||||||
|
param.filename);
|
||||||
free(rom);
|
free(rom);
|
||||||
return 1;
|
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);
|
free(payload);
|
||||||
|
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
|
||||||
if (add_file_to_cbfs(cbfsfile, final_size, param.baseaddress)) {
|
|
||||||
ERROR("Adding payload '%s' failed.\n",
|
ERROR("Adding payload '%s' failed.\n",
|
||||||
param.filename);
|
param.filename);
|
||||||
free(cbfsfile);
|
free(cbfsfile);
|
||||||
|
|
|
@ -72,8 +72,13 @@ comp_func_ptr compression_function(comp_algo algo);
|
||||||
|
|
||||||
uint64_t intfiletype(const char *name);
|
uint64_t intfiletype(const char *name);
|
||||||
|
|
||||||
|
/* cbfs-mkpayload.c */
|
||||||
int parse_elf_to_payload(unsigned char *input, unsigned char **output,
|
int parse_elf_to_payload(unsigned char *input, unsigned char **output,
|
||||||
comp_algo algo);
|
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,
|
int parse_elf_to_stage(unsigned char *input, unsigned char **output,
|
||||||
comp_algo algo, uint32_t * location);
|
comp_algo algo, uint32_t * location);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue