cbfstool: Convert cbfs-mkstage.c into pelf

Change cbfs-mkstage to use parsed elf instead of calling elf_headers. That
allows us to have access to the complete elf including the string table.

BUG=None
BRANCH=None
TEST=Compiles successfully for falco and creates coreboot.rom image that boots
fine on falco.

Change-Id: I222ef8afa5e1fcbb54ebb45e804bb341a796872d
Signed-off-by: Furquan Shaikh <furquan@google.com>
Reviewed-on: https://chromium-review.googlesource.com/226167
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Tested-by: Furquan Shaikh <furquan@chromium.org>
Reviewed-by: Patrick Georgi <pgeorgi@chromium.org>
Commit-Queue: Furquan Shaikh <furquan@chromium.org>
This commit is contained in:
Furquan Shaikh 2014-10-28 21:42:45 -07:00 committed by chrome-internal-fetch
commit 7360f476c3

View file

@ -35,10 +35,12 @@
int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
comp_algo algo, uint32_t *location)
{
struct parsed_elf pelf;
Elf64_Phdr *phdr;
Elf64_Ehdr ehdr;
Elf64_Ehdr *ehdr;
char *buffer;
struct buffer outheader;
int ret = -1;
int headers;
int i, outlen;
@ -50,10 +52,17 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
DEBUG("start: parse_elf_to_stage(location=0x%x)\n", *location);
if (elf_headers(input, &ehdr, &phdr, NULL) < 0)
return -1;
int flags = ELF_PARSE_PHDR | ELF_PARSE_SHDR | ELF_PARSE_STRTAB;
headers = ehdr.e_phnum;
if (parse_elf(input, &pelf, flags)) {
ERROR("Couldn't parse ELF\n");
return -1;
}
ehdr = &pelf.ehdr;
phdr = &pelf.phdr[0];
headers = ehdr->e_phnum;
data_start = ~0;
data_end = 0;
@ -102,7 +111,7 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
if (buffer == NULL) {
ERROR("Unable to allocate memory: %m\n");
return -1;
goto err;
}
/* Copy the file data into the buffer */
@ -134,7 +143,7 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
ERROR("Underflow copying out the segment."
"File has %ld bytes left, segment end is %ld\n",
input->size, phdr[i].p_offset + phdr[i].p_filesz);
return -1;
goto err;
}
memcpy(buffer + (l_start - data_start),
&input->data[phdr[i].p_offset + l_offset],
@ -146,7 +155,7 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
input->name) != 0) {
ERROR("Unable to allocate memory: %m\n");
free(buffer);
return -1;
goto err;
}
memset(output->data, 0, output->size);
@ -176,7 +185,7 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
* Maybe we should just change the spec.
*/
xdr_le.put32(&outheader, algo);
xdr_le.put64(&outheader, ehdr.e_entry);
xdr_le.put64(&outheader, ehdr->e_entry);
xdr_le.put64(&outheader, data_start);
xdr_le.put32(&outheader, outlen);
xdr_le.put32(&outheader, mem_end - data_start);
@ -184,5 +193,9 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
if (*location)
*location -= sizeof(struct cbfs_stage);
output->size = sizeof(struct cbfs_stage) + outlen;
return 0;
ret = 0;
err:
parsed_elf_destroy(&pelf);
return ret;
}