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.

Change-Id: Ie767d28bdf41af38d1df0bce54bc0ada45123136
Signed-off-by: Furquan Shaikh <furquan@google.com>
Reviewed-on: http://review.coreboot.org/7303
Tested-by: build bot (Jenkins)
Reviewed-by: Edward O'Callaghan <eocallaghan@alterapraxis.com>
This commit is contained in:
Furquan Shaikh
2014-10-30 11:28:27 -07:00
parent f545d71ca2
commit cc6f84c411

View File

@@ -35,10 +35,12 @@
int parse_elf_to_stage(const struct buffer *input, struct buffer *output, int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
uint32_t arch, comp_algo algo, uint32_t *location) uint32_t arch, comp_algo algo, uint32_t *location)
{ {
struct parsed_elf pelf;
Elf64_Phdr *phdr; Elf64_Phdr *phdr;
Elf64_Ehdr ehdr; Elf64_Ehdr *ehdr;
char *buffer; char *buffer;
struct buffer outheader; struct buffer outheader;
int ret = -1;
int headers; int headers;
int i, outlen; 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); DEBUG("start: parse_elf_to_stage(location=0x%x)\n", *location);
if (elf_headers(input, arch, &ehdr, &phdr, NULL) < 0) int flags = ELF_PARSE_PHDR | ELF_PARSE_SHDR | ELF_PARSE_STRTAB;
return -1;
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_start = ~0;
data_end = 0; data_end = 0;
@@ -102,7 +111,7 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
if (buffer == NULL) { if (buffer == NULL) {
ERROR("Unable to allocate memory: %m\n"); ERROR("Unable to allocate memory: %m\n");
return -1; goto err;
} }
/* Copy the file data into the buffer */ /* Copy the file data into the buffer */
@@ -135,7 +144,7 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
"File has %zu bytes left, segment end is %zu\n", "File has %zu bytes left, segment end is %zu\n",
input->size, (size_t)(phdr[i].p_offset + phdr[i].p_filesz)); input->size, (size_t)(phdr[i].p_offset + phdr[i].p_filesz));
free(buffer); free(buffer);
return -1; goto err;
} }
memcpy(buffer + (l_start - data_start), memcpy(buffer + (l_start - data_start),
&input->data[phdr[i].p_offset + l_offset], &input->data[phdr[i].p_offset + l_offset],
@@ -147,7 +156,7 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
input->name) != 0) { input->name) != 0) {
ERROR("Unable to allocate memory: %m\n"); ERROR("Unable to allocate memory: %m\n");
free(buffer); free(buffer);
return -1; goto err;
} }
memset(output->data, 0, output->size); memset(output->data, 0, output->size);
@@ -177,7 +186,7 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
* Maybe we should just change the spec. * Maybe we should just change the spec.
*/ */
xdr_le.put32(&outheader, algo); 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.put64(&outheader, data_start);
xdr_le.put32(&outheader, outlen); xdr_le.put32(&outheader, outlen);
xdr_le.put32(&outheader, mem_end - data_start); xdr_le.put32(&outheader, mem_end - data_start);
@@ -185,5 +194,9 @@ int parse_elf_to_stage(const struct buffer *input, struct buffer *output,
if (*location) if (*location)
*location -= sizeof(struct cbfs_stage); *location -= sizeof(struct cbfs_stage);
output->size = sizeof(struct cbfs_stage) + outlen; output->size = sizeof(struct cbfs_stage) + outlen;
return 0; ret = 0;
err:
parsed_elf_destroy(&pelf);
return ret;
} }