2009-03-31 13:57:36 +02:00
|
|
|
/*
|
2009-04-14 21:48:32 +02:00
|
|
|
* cbfs-mkpayload
|
2009-03-31 13:57:36 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
|
2009-09-14 15:29:27 +02:00
|
|
|
* 2009 coresystems GmbH
|
|
|
|
* written by Patrick Georgi <patrick.georgi@coresystems.de>
|
2009-03-31 13:57:36 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2009-09-14 15:29:27 +02:00
|
|
|
#include "cbfs.h"
|
2013-01-28 15:40:10 +01:00
|
|
|
#include "elf.h"
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
int parse_elf_to_payload(const struct buffer *input,
|
|
|
|
struct buffer *output, comp_algo algo)
|
2009-03-31 13:57:36 +02:00
|
|
|
{
|
|
|
|
Elf32_Phdr *phdr;
|
2013-01-29 08:22:11 +01:00
|
|
|
Elf32_Ehdr *ehdr = (Elf32_Ehdr *)input->data;
|
2009-03-31 13:57:36 +02:00
|
|
|
Elf32_Shdr *shdr;
|
|
|
|
char *header;
|
|
|
|
char *strtab;
|
|
|
|
int headers;
|
|
|
|
int segments = 1;
|
|
|
|
int isize = 0, osize = 0;
|
|
|
|
int doffset = 0;
|
2009-04-14 02:08:34 +02:00
|
|
|
struct cbfs_payload_segment *segs;
|
2009-03-31 13:57:36 +02:00
|
|
|
int i;
|
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
if(!iself((unsigned char *)input->data)){
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("The payload file is not in ELF format!\n");
|
2012-11-16 23:48:22 +01:00
|
|
|
return -1;
|
2009-09-26 00:21:47 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
// The tool may work in architecture-independent way.
|
|
|
|
if (arch != CBFS_ARCHITECTURE_UNKNOWN &&
|
|
|
|
!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
|
2012-11-16 23:48:22 +01:00
|
|
|
!((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
|
2013-01-28 07:39:43 +01:00
|
|
|
ERROR("The payload file has the wrong architecture\n");
|
2012-11-16 23:48:22 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2009-09-26 00:21:47 +02:00
|
|
|
|
2009-09-14 15:29:27 +02:00
|
|
|
comp_func_ptr compress = compression_function(algo);
|
|
|
|
if (!compress)
|
|
|
|
return -1;
|
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
DEBUG("start: parse_elf_to_payload\n");
|
2009-03-31 13:57:36 +02:00
|
|
|
headers = ehdr->e_phnum;
|
|
|
|
header = (char *)ehdr;
|
|
|
|
|
|
|
|
phdr = (Elf32_Phdr *) & (header[ehdr->e_phoff]);
|
|
|
|
shdr = (Elf32_Shdr *) & (header[ehdr->e_shoff]);
|
|
|
|
|
|
|
|
strtab = &header[shdr[ehdr->e_shstrndx].sh_offset];
|
|
|
|
|
|
|
|
/* Count the number of headers - look for the .notes.pinfo
|
|
|
|
* section */
|
|
|
|
|
|
|
|
for (i = 0; i < ehdr->e_shnum; i++) {
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (i == ehdr->e_shstrndx)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (shdr[i].sh_size == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
name = (char *)(strtab + shdr[i].sh_name);
|
|
|
|
|
2009-09-17 17:35:08 +02:00
|
|
|
if (!strcmp(name, ".note.pinfo")) {
|
2009-03-31 13:57:36 +02:00
|
|
|
segments++;
|
2009-09-17 17:35:08 +02:00
|
|
|
isize += (unsigned int)shdr[i].sh_size;
|
|
|
|
}
|
2009-03-31 13:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, regular headers - we only care about PT_LOAD headers,
|
|
|
|
* because thats what we're actually going to load
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < headers; i++) {
|
|
|
|
if (phdr[i].p_type != PT_LOAD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Empty segments are never interesting */
|
|
|
|
if (phdr[i].p_memsz == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
isize += phdr[i].p_filesz;
|
|
|
|
|
|
|
|
segments++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a block of memory to store the data in */
|
2013-01-29 08:22:11 +01:00
|
|
|
if (buffer_create(output, (segments * sizeof(*segs)) + isize,
|
|
|
|
input->name) != 0)
|
|
|
|
return -1;
|
|
|
|
memset(output->data, 0, output->size);
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2009-04-14 02:08:34 +02:00
|
|
|
doffset = (segments * sizeof(struct cbfs_payload_segment));
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
segs = (struct cbfs_payload_segment *)output->data;
|
2009-03-31 13:57:36 +02:00
|
|
|
segments = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ehdr->e_shnum; i++) {
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (i == ehdr->e_shstrndx)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (shdr[i].sh_size == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
name = (char *)(strtab + shdr[i].sh_name);
|
|
|
|
|
|
|
|
if (!strcmp(name, ".note.pinfo")) {
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_PARAMS;
|
|
|
|
segs[segments].load_addr = 0;
|
|
|
|
segs[segments].len = (unsigned int)shdr[i].sh_size;
|
|
|
|
segs[segments].offset = doffset;
|
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
memcpy((unsigned long *)(output->data + doffset),
|
2009-03-31 13:57:36 +02:00
|
|
|
&header[shdr[i].sh_offset], shdr[i].sh_size);
|
|
|
|
|
|
|
|
doffset += segs[segments].len;
|
|
|
|
osize += segs[segments].len;
|
|
|
|
|
|
|
|
segments++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < headers; i++) {
|
|
|
|
if (phdr[i].p_type != PT_LOAD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (phdr[i].p_memsz == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (phdr[i].p_filesz == 0) {
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_BSS;
|
|
|
|
segs[segments].load_addr =
|
2011-10-21 23:24:57 +02:00
|
|
|
(uint64_t)htonll(phdr[i].p_paddr);
|
2009-09-14 15:29:27 +02:00
|
|
|
segs[segments].mem_len =
|
2011-10-21 23:24:57 +02:00
|
|
|
(uint32_t)htonl(phdr[i].p_memsz);
|
2009-03-31 13:57:36 +02:00
|
|
|
segs[segments].offset = htonl(doffset);
|
|
|
|
|
|
|
|
segments++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-11 21:40:14 +01:00
|
|
|
if (phdr[i].p_flags & PF_X)
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_CODE;
|
|
|
|
else
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_DATA;
|
2011-10-21 23:24:57 +02:00
|
|
|
segs[segments].load_addr = (uint64_t)htonll(phdr[i].p_paddr);
|
|
|
|
segs[segments].mem_len = (uint32_t)htonl(phdr[i].p_memsz);
|
2009-03-31 13:57:36 +02:00
|
|
|
segs[segments].compression = htonl(algo);
|
|
|
|
segs[segments].offset = htonl(doffset);
|
|
|
|
|
|
|
|
int len;
|
|
|
|
compress((char *)&header[phdr[i].p_offset],
|
2013-01-29 08:22:11 +01:00
|
|
|
phdr[i].p_filesz, output->data + doffset, &len);
|
2009-03-31 13:57:36 +02:00
|
|
|
segs[segments].len = htonl(len);
|
|
|
|
|
|
|
|
/* If the compressed section is larger, then use the
|
|
|
|
original stuff */
|
|
|
|
|
2009-04-05 00:18:26 +02:00
|
|
|
if ((unsigned int)len > phdr[i].p_filesz) {
|
2009-03-31 13:57:36 +02:00
|
|
|
segs[segments].compression = 0;
|
|
|
|
segs[segments].len = htonl(phdr[i].p_filesz);
|
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
memcpy(output->data + doffset,
|
2009-03-31 13:57:36 +02:00
|
|
|
&header[phdr[i].p_offset], phdr[i].p_filesz);
|
|
|
|
}
|
|
|
|
|
|
|
|
doffset += ntohl(segs[segments].len);
|
|
|
|
osize += ntohl(segs[segments].len);
|
|
|
|
|
|
|
|
segments++;
|
|
|
|
}
|
|
|
|
|
|
|
|
segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
|
2013-01-29 08:22:11 +01:00
|
|
|
segs[segments++].load_addr = htonll(ehdr->e_entry);
|
2009-03-31 13:57:36 +02:00
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
output->size = (segments * sizeof(struct cbfs_payload_segment)) + osize;
|
|
|
|
return 0;
|
2009-03-31 13:57:36 +02:00
|
|
|
}
|
2013-01-28 08:04:30 +01:00
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
int parse_flat_binary_to_payload(const struct buffer *input,
|
|
|
|
struct buffer *output,
|
2013-01-28 08:04:30 +01:00
|
|
|
uint32_t loadaddress,
|
|
|
|
uint32_t entrypoint,
|
|
|
|
comp_algo algo)
|
|
|
|
{
|
|
|
|
comp_func_ptr compress;
|
|
|
|
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");
|
2013-01-29 08:22:11 +01:00
|
|
|
if (buffer_create(output, (2 * sizeof(*segs) + input->size),
|
|
|
|
input->name) != 0)
|
2013-01-28 08:04:30 +01:00
|
|
|
return -1;
|
2013-01-29 08:22:11 +01:00
|
|
|
memset(output->data, 0, output->size);
|
2013-01-28 08:04:30 +01:00
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
segs = (struct cbfs_payload_segment *)output->data;
|
2013-01-28 08:04:30 +01:00
|
|
|
doffset = (2 * sizeof(*segs));
|
|
|
|
|
|
|
|
/* Prepare code segment */
|
|
|
|
segs[0].type = PAYLOAD_SEGMENT_CODE;
|
|
|
|
segs[0].load_addr = htonll(loadaddress);
|
2013-01-29 08:22:11 +01:00
|
|
|
segs[0].mem_len = htonl(input->size);
|
2013-01-28 08:04:30 +01:00
|
|
|
segs[0].offset = htonl(doffset);
|
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
compress(input->data, input->size, output->data + doffset, &len);
|
2013-01-28 08:04:30 +01:00
|
|
|
segs[0].compression = htonl(algo);
|
|
|
|
segs[0].len = htonl(len);
|
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
if ((unsigned int)len >= input->size) {
|
2013-01-28 08:04:30 +01:00
|
|
|
WARN("Compressing data would make it bigger - disabled.\n");
|
|
|
|
segs[0].compression = 0;
|
2013-01-29 08:22:11 +01:00
|
|
|
segs[0].len = htonl(input->size);
|
|
|
|
memcpy(output->data + doffset, input->data, input->size);
|
2013-01-28 08:04:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare entry point segment */
|
|
|
|
segs[1].type = PAYLOAD_SEGMENT_ENTRY;
|
|
|
|
segs[1].load_addr = htonll(entrypoint);
|
2013-01-29 08:22:11 +01:00
|
|
|
output->size = doffset + ntohl(segs[0].len);
|
2013-01-28 08:04:30 +01:00
|
|
|
|
2013-01-29 08:22:11 +01:00
|
|
|
return 0;
|
2013-01-28 08:04:30 +01:00
|
|
|
}
|