cbfstool: Use cbfs_image API for "add-*" (add-payload, add-stage, ...) commands.

add-payload, add-stage, and add-flat-binary are now all using cbfs_image API.
To test:
	cbfstool coreboot.rom add-stage -f FILE -n fallback/romstage -b 0xXXXX
	cbfstool coreboot.rom add-payload -f FILE -n fallback/pyload
And compare with old cbfstool.

Verified to boot on ARM(snow) and X86(qemu-i386).

Change-Id: If65cb495c476ef6f9d90c778531f0c3caf178281
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: http://review.coreboot.org/2220
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
This commit is contained in:
Hung-Te Lin 2013-01-29 15:22:11 +08:00 committed by Stefan Reinauer
parent 5f3eb26d85
commit c13e4bf3e1
4 changed files with 116 additions and 246 deletions

View File

@ -27,15 +27,14 @@
#include "cbfs.h" #include "cbfs.h"
#include "elf.h" #include "elf.h"
int parse_elf_to_payload(unsigned char *input, unsigned char **output, int parse_elf_to_payload(const struct buffer *input,
comp_algo algo) struct buffer *output, comp_algo algo)
{ {
Elf32_Phdr *phdr; Elf32_Phdr *phdr;
Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input->data;
Elf32_Shdr *shdr; Elf32_Shdr *shdr;
char *header; char *header;
char *strtab; char *strtab;
unsigned char *sptr;
int headers; int headers;
int segments = 1; int segments = 1;
int isize = 0, osize = 0; int isize = 0, osize = 0;
@ -43,12 +42,14 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
struct cbfs_payload_segment *segs; struct cbfs_payload_segment *segs;
int i; int i;
if(!iself(input)){ if(!iself((unsigned char *)input->data)){
ERROR("The payload file is not in ELF format!\n"); ERROR("The payload file is not in ELF format!\n");
return -1; return -1;
} }
if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) && // The tool may work in architecture-independent way.
if (arch != CBFS_ARCHITECTURE_UNKNOWN &&
!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
!((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) { !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
ERROR("The payload file has the wrong architecture\n"); ERROR("The payload file has the wrong architecture\n");
return -1; return -1;
@ -58,6 +59,7 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
if (!compress) if (!compress)
return -1; return -1;
DEBUG("start: parse_elf_to_payload\n");
headers = ehdr->e_phnum; headers = ehdr->e_phnum;
header = (char *)ehdr; header = (char *)ehdr;
@ -104,15 +106,14 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
} }
/* Allocate a block of memory to store the data in */ /* Allocate a block of memory to store the data in */
if (buffer_create(output, (segments * sizeof(*segs)) + isize,
input->name) != 0)
return -1;
memset(output->data, 0, output->size);
sptr =
calloc((segments * sizeof(struct cbfs_payload_segment)) + isize, 1);
doffset = (segments * sizeof(struct cbfs_payload_segment)); doffset = (segments * sizeof(struct cbfs_payload_segment));
if (sptr == NULL) segs = (struct cbfs_payload_segment *)output->data;
goto err;
segs = (struct cbfs_payload_segment *)sptr;
segments = 0; segments = 0;
for (i = 0; i < ehdr->e_shnum; i++) { for (i = 0; i < ehdr->e_shnum; i++) {
@ -132,7 +133,7 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
segs[segments].len = (unsigned int)shdr[i].sh_size; segs[segments].len = (unsigned int)shdr[i].sh_size;
segs[segments].offset = doffset; segs[segments].offset = doffset;
memcpy((unsigned long *)(sptr + doffset), memcpy((unsigned long *)(output->data + doffset),
&header[shdr[i].sh_offset], shdr[i].sh_size); &header[shdr[i].sh_offset], shdr[i].sh_size);
doffset += segs[segments].len; doffset += segs[segments].len;
@ -172,7 +173,7 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
int len; int len;
compress((char *)&header[phdr[i].p_offset], compress((char *)&header[phdr[i].p_offset],
phdr[i].p_filesz, (char *)(sptr + doffset), &len); phdr[i].p_filesz, output->data + doffset, &len);
segs[segments].len = htonl(len); segs[segments].len = htonl(len);
/* If the compressed section is larger, then use the /* If the compressed section is larger, then use the
@ -182,7 +183,7 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
segs[segments].compression = 0; segs[segments].compression = 0;
segs[segments].len = htonl(phdr[i].p_filesz); segs[segments].len = htonl(phdr[i].p_filesz);
memcpy((char *)(sptr + doffset), memcpy(output->data + doffset,
&header[phdr[i].p_offset], phdr[i].p_filesz); &header[phdr[i].p_offset], phdr[i].p_filesz);
} }
@ -193,24 +194,19 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
} }
segs[segments].type = PAYLOAD_SEGMENT_ENTRY; segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
segs[segments++].load_addr = (uint64_t)htonll(ehdr->e_entry); segs[segments++].load_addr = htonll(ehdr->e_entry);
*output = sptr; output->size = (segments * sizeof(struct cbfs_payload_segment)) + osize;
return 0;
return (segments * sizeof(struct cbfs_payload_segment)) + osize;
err:
return -1;
} }
int parse_flat_binary_to_payload(unsigned char *input, unsigned char **output, int parse_flat_binary_to_payload(const struct buffer *input,
int32_t input_size, struct buffer *output,
uint32_t loadaddress, uint32_t loadaddress,
uint32_t entrypoint, uint32_t entrypoint,
comp_algo algo) comp_algo algo)
{ {
comp_func_ptr compress; comp_func_ptr compress;
unsigned char *payload;
struct cbfs_payload_segment *segs; struct cbfs_payload_segment *segs;
int doffset, len = 0; int doffset, len = 0;
@ -219,39 +215,35 @@ int parse_flat_binary_to_payload(unsigned char *input, unsigned char **output,
return -1; return -1;
DEBUG("start: parse_flat_binary_to_payload\n"); DEBUG("start: parse_flat_binary_to_payload\n");
if (buffer_create(output, (2 * sizeof(*segs) + input->size),
/* FIXME compressed file size might be bigger than original file and input->name) != 0)
* 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; return -1;
} memset(output->data, 0, output->size);
segs = (struct cbfs_payload_segment *)payload; segs = (struct cbfs_payload_segment *)output->data;
doffset = (2 * sizeof(*segs)); doffset = (2 * sizeof(*segs));
/* Prepare code segment */ /* Prepare code segment */
segs[0].type = PAYLOAD_SEGMENT_CODE; segs[0].type = PAYLOAD_SEGMENT_CODE;
segs[0].load_addr = htonll(loadaddress); segs[0].load_addr = htonll(loadaddress);
segs[0].mem_len = htonl(input_size); segs[0].mem_len = htonl(input->size);
segs[0].offset = htonl(doffset); segs[0].offset = htonl(doffset);
compress((char*)input, input_size, (char*)payload + doffset, &len); compress(input->data, input->size, output->data + doffset, &len);
segs[0].compression = htonl(algo); segs[0].compression = htonl(algo);
segs[0].len = htonl(len); segs[0].len = htonl(len);
if ((unsigned int)len >= input_size) { if ((unsigned int)len >= input->size) {
WARN("Compressing data would make it bigger - disabled.\n"); WARN("Compressing data would make it bigger - disabled.\n");
segs[0].compression = 0; segs[0].compression = 0;
segs[0].len = htonl(input_size); segs[0].len = htonl(input->size);
memcpy(payload + doffset, input, input_size); memcpy(output->data + doffset, input->data, input->size);
} }
/* prepare entry point segment */ /* prepare entry point segment */
segs[1].type = PAYLOAD_SEGMENT_ENTRY; segs[1].type = PAYLOAD_SEGMENT_ENTRY;
segs[1].load_addr = htonll(entrypoint); segs[1].load_addr = htonll(entrypoint);
*output = payload; output->size = doffset + ntohl(segs[0].len);
return doffset + ntohl(segs[0].len); return 0;
} }

View File

@ -44,13 +44,12 @@ static unsigned int swap32(unsigned int x)
unsigned int (*elf32_to_native) (unsigned int) = idemp; unsigned int (*elf32_to_native) (unsigned int) = idemp;
/* returns size of result, or -1 if error */ /* returns size of result, or -1 if error */
int parse_elf_to_stage(unsigned char *input, unsigned char **output, int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
comp_algo algo, uint32_t * location) comp_algo algo, uint32_t *location)
{ {
Elf32_Phdr *phdr; Elf32_Phdr *phdr;
Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input->data;
char *header, *buffer; char *header, *buffer;
unsigned char *out;
int headers; int headers;
int i; int i;
@ -63,12 +62,15 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
if (!compress) if (!compress)
return -1; return -1;
if (!iself(input)) { DEBUG("start: parse_elf_to_stage(location=0x%x)\n", *location);
if (!iself((unsigned char *)input->data)) {
ERROR("The stage file is not in ELF format!\n"); ERROR("The stage file is not in ELF format!\n");
return -1; return -1;
} }
if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) && // The tool may work in architecture-independent way.
if (arch != CBFS_ARCHITECTURE_UNKNOWN &&
!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
!((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) { !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
ERROR("The stage file has the wrong architecture\n"); ERROR("The stage file has the wrong architecture\n");
return -1; return -1;
@ -162,28 +164,26 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
} }
/* Now make the output buffer */ /* Now make the output buffer */
out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1); if (buffer_create(output, sizeof(*stage) + data_end - data_start,
input->name) != 0) {
if (out == NULL) {
ERROR("Unable to allocate memory: %m\n"); ERROR("Unable to allocate memory: %m\n");
return -1; return -1;
} }
memset(output->data, 0, output->size);
stage = (struct cbfs_stage *)out; stage = (struct cbfs_stage *)output->data;
stage->load = data_start; /* FIXME: htonll */ stage->load = data_start; /* FIXME: htonll */
stage->memlen = mem_end - data_start; stage->memlen = mem_end - data_start;
stage->compression = algo; stage->compression = algo;
stage->entry = ehdr->e_entry; /* FIXME: htonll */ stage->entry = ehdr->e_entry; /* FIXME: htonll */
compress(buffer, data_end - data_start, compress(buffer, data_end - data_start, (output->data + sizeof(*stage)),
(char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len); (int *)&stage->len);
free(buffer); free(buffer);
*output = out;
if (*location) if (*location)
*location -= sizeof(struct cbfs_stage); *location -= sizeof(struct cbfs_stage);
return sizeof(struct cbfs_stage) + stage->len; output->size = sizeof(*stage) + stage->len;
return 0;
} }

View File

@ -58,7 +58,7 @@ static struct param {
.algo = CBFS_COMPRESS_NONE, .algo = CBFS_COMPRESS_NONE,
}; };
typedef int (*convert_buffer_t)(struct buffer *buffer); typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset);
static int cbfs_add_component(const char *cbfs_name, static int cbfs_add_component(const char *cbfs_name,
const char *filename, const char *filename,
@ -89,7 +89,7 @@ static int cbfs_add_component(const char *cbfs_name,
return 1; return 1;
} }
if (convert && convert(&buffer) != 0) { if (convert && convert(&buffer, &offset) != 0) {
ERROR("Failed to parse file '%s'.\n", filename); ERROR("Failed to parse file '%s'.\n", filename);
buffer_delete(&buffer); buffer_delete(&buffer);
return 1; return 1;
@ -126,6 +126,42 @@ static int cbfs_add_component(const char *cbfs_name,
return 0; return 0;
} }
static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset) {
struct buffer output;
if (parse_elf_to_stage(buffer, &output, param.algo, offset) != 0)
return -1;
buffer_delete(buffer);
// direct assign, no dupe.
memcpy(buffer, &output, sizeof(*buffer));
return 0;
}
static int cbfstool_convert_mkpayload(struct buffer *buffer, uint32_t *offset) {
struct buffer output;
if (parse_elf_to_payload(buffer, &output, param.algo) != 0)
return -1;
buffer_delete(buffer);
// direct assign, no dupe.
memcpy(buffer, &output, sizeof(*buffer));
return 0;
}
static int cbfstool_convert_mkflatpayload(struct buffer *buffer,
uint32_t *offset) {
struct buffer output;
if (parse_flat_binary_to_payload(buffer, &output,
param.loadaddress,
param.entrypoint,
param.algo) != 0) {
return -1;
}
buffer_delete(buffer);
// direct assign, no dupe.
memcpy(buffer, &output, sizeof(*buffer));
return 0;
}
static int cbfs_add(void) static int cbfs_add(void)
{ {
return cbfs_add_component(param.cbfs_name, return cbfs_add_component(param.cbfs_name,
@ -136,204 +172,44 @@ static int cbfs_add(void)
NULL); NULL);
} }
static int cbfs_add_payload(void)
{
uint32_t filesize = 0;
void *rom, *filedata, *cbfsfile;
unsigned char *payload;
if (!param.filename) {
ERROR("You need to specify -f/--filename.\n");
return 1;
}
if (!param.name) {
ERROR("You need to specify -n/--name.\n");
return 1;
}
rom = loadrom(param.cbfs_name);
if (rom == NULL) {
ERROR("Could not load ROM image '%s'.\n",
param.cbfs_name);
return 1;
}
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
if (filedata == NULL) {
ERROR("Could not load file '%s'.\n",
param.filename);
free(rom);
return 1;
}
filesize = parse_elf_to_payload(filedata, &payload, param.algo);
if (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);
free(filedata);
free(payload);
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
ERROR("Adding payload '%s' failed.\n",
param.filename);
free(cbfsfile);
free(rom);
return 1;
}
if (writerom(param.cbfs_name, rom, romsize)) {
free(cbfsfile);
free(rom);
return 1;
}
free(cbfsfile);
free(rom);
return 0;
}
static int cbfs_add_stage(void) static int cbfs_add_stage(void)
{ {
uint32_t filesize = 0; return cbfs_add_component(param.cbfs_name,
void *rom, *filedata, *cbfsfile; param.filename,
unsigned char *stage; param.name,
CBFS_COMPONENT_STAGE,
param.baseaddress,
cbfstool_convert_mkstage);
}
if (!param.filename) { static int cbfs_add_payload(void)
ERROR("You need to specify -f/--filename.\n"); {
return 1; return cbfs_add_component(param.cbfs_name,
} param.filename,
param.name,
if (!param.name) { CBFS_COMPONENT_PAYLOAD,
ERROR("You need to specify -n/--name.\n"); param.baseaddress,
return 1; cbfstool_convert_mkpayload);
}
rom = loadrom(param.cbfs_name);
if (rom == NULL) {
ERROR("Could not load ROM image '%s'.\n",
param.cbfs_name);
return 1;
}
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
if (filedata == NULL) {
ERROR("Could not load file '%s'.\n",
param.filename);
free(rom);
return 1;
}
filesize = parse_elf_to_stage(filedata, &stage, param.algo, &param.baseaddress);
cbfsfile = create_cbfs_file(param.name, stage, &filesize,
CBFS_COMPONENT_STAGE, &param.baseaddress);
free(filedata);
free(stage);
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
ERROR("Adding stage '%s' failed.\n",
param.filename);
free(cbfsfile);
free(rom);
return 1;
}
if (writerom(param.cbfs_name, rom, romsize)) {
free(cbfsfile);
free(rom);
return 1;
}
free(cbfsfile);
free(rom);
return 0;
} }
static int cbfs_add_flat_binary(void) static int cbfs_add_flat_binary(void)
{ {
uint32_t filesize = 0;
void *rom, *filedata, *cbfsfile;
unsigned char *payload;
if (!param.filename) {
ERROR("You need to specify -f/--filename.\n");
return 1;
}
if (!param.name) {
ERROR("You need to specify -n/--name.\n");
return 1;
}
if (param.loadaddress == 0) { if (param.loadaddress == 0) {
ERROR("You need to specify a valid " ERROR("You need to specify a valid "
"-l/--load-address.\n"); "-l/--load-address.\n");
return 1; return 1;
} }
if (param.entrypoint == 0) { if (param.entrypoint == 0) {
ERROR("You need to specify a valid " ERROR("You need to specify a valid "
"-e/--entry-point.\n"); "-e/--entry-point.\n");
return 1; return 1;
} }
return cbfs_add_component(param.cbfs_name,
rom = loadrom(param.cbfs_name); param.filename,
if (rom == NULL) { param.name,
ERROR("Could not load ROM image '%s'.\n", CBFS_COMPONENT_PAYLOAD,
param.cbfs_name); param.baseaddress,
return 1; cbfstool_convert_mkflatpayload);
}
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
if (filedata == NULL) {
ERROR("Could not load file '%s'.\n",
param.filename);
free(rom);
return 1;
}
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);
free(payload);
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
ERROR("Adding payload '%s' failed.\n",
param.filename);
free(cbfsfile);
free(rom);
return 1;
}
if (writerom(param.cbfs_name, rom, romsize)) {
free(cbfsfile);
free(rom);
return 1;
}
free(cbfsfile);
free(rom);
return 0;
} }
static int cbfs_remove(void) static int cbfs_remove(void)

View File

@ -98,14 +98,16 @@ comp_func_ptr compression_function(comp_algo algo);
uint64_t intfiletype(const char *name); uint64_t intfiletype(const char *name);
/* cbfs-mkpayload.c */ /* cbfs-mkpayload.c */
int parse_elf_to_payload(unsigned char *input, unsigned char **output, int parse_elf_to_payload(const struct buffer *input,
comp_algo algo); struct buffer *output, comp_algo algo);
int parse_flat_binary_to_payload(unsigned char *input, unsigned char **output, int parse_flat_binary_to_payload(const struct buffer *input,
int32_t input_size, uint32_t loadaddress, struct buffer *output,
uint32_t entrypoint, comp_algo algo); uint32_t loadaddress,
uint32_t entrypoint,
comp_algo algo);
/* cbfs-mkstage.c */ /* cbfs-mkstage.c */
int parse_elf_to_stage(unsigned char *input, unsigned char **output, int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
comp_algo algo, uint32_t * location); comp_algo algo, uint32_t *location);
void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
uint32_t type, uint32_t * location); uint32_t type, uint32_t * location);