summaryrefslogtreecommitdiff
path: root/util/cbfstool/common.c
diff options
context:
space:
mode:
authorRonald G. Minnich <rminnich@google.com>2013-12-03 11:13:35 -0800
committerAlexandru Gagniuc <mr.nuke.me@gmail.com>2014-01-29 20:03:44 +0100
commitaa2f739ae87386b8a29068ecfdc2b25bcf4a19ca (patch)
tree7d69d6c6edc92c633ff6138f6d8b626151522c0a /util/cbfstool/common.c
parent8449b5e0a9e5eca3c91c1d9351d5f21eaa0ba582 (diff)
downloadcoreboot-aa2f739ae87386b8a29068ecfdc2b25bcf4a19ca.tar.xz
cbfs: fix issues with word size and endianness.
Add XDR functions and use them to convert the ELF headers to native headers, using the Elf64 structs to ensure we accomodate all word sizes. Also, use these XDR functions for output. This may seem overly complex but it turned out to be much the easiest way to do this. Note that the basic elf parsing function in cbfs-mkstage.c now works over all ELF files, for all architectures, endian, and word size combinations. At the same time, the basic elf parsing in cbfs-mkstage.c is a loop that has no architecture-specific conditionals. Add -g to the LDFLAGS while we're here. It's on the CFLAGS so there is no harm done. This code has been tested on all chromebooks that use coreboot to date. I added most of the extra checks from ChromeOS and they triggered a lot of warnings, hence the other changes. I had to take -Wshadow back out due to the many errors it triggers in LZMA. BUG=None TEST=Build and boot for Peppy; works fine. Build and boot for nyan, works fine. Build for qemu targets and armv8 targets. BRANCH=None Change-Id: I5a4cee9854799189115ac701e22efc406a8d902f Signed-off-by: Ronald G. Minnich <rminnich@google.com> Reviewed-on: https://chromium-review.googlesource.com/178606 Reviewed-by: Ronald Minnich <rminnich@chromium.org> Commit-Queue: Ronald Minnich <rminnich@chromium.org> Tested-by: Ronald Minnich <rminnich@chromium.org> Reviewed-on: http://review.coreboot.org/4817 Reviewed-by: Alexandru Gagniuc <mr.nuke.me@gmail.com> Tested-by: build bot (Jenkins) Reviewed-by: Ronald G. Minnich <rminnich@gmail.com>
Diffstat (limited to 'util/cbfstool/common.c')
-rw-r--r--util/cbfstool/common.c51
1 files changed, 27 insertions, 24 deletions
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index 03345df1eb..8f38a4a27d 100644
--- a/util/cbfstool/common.c
+++ b/util/cbfstool/common.c
@@ -144,10 +144,13 @@ void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
return content;
}
-static struct cbfs_header *master_header;
+/* N.B.: there are declarations here that were shadowed in functions.
+ * Hence the rather clumsy cbfstool_ prefixes.
+ */
+static struct cbfs_header *cbfstool_master_header;
static uint32_t phys_start, phys_end, align;
uint32_t romsize;
-void *offset;
+void *cbfstool_offset;
uint32_t arch = CBFS_ARCHITECTURE_UNKNOWN;
static struct {
@@ -194,19 +197,19 @@ int find_master_header(void *romarea, size_t size)
{
size_t offset;
- if (master_header)
+ if (cbfstool_master_header)
return 0;
for (offset = 0; offset < size - sizeof(struct cbfs_header); offset++) {
struct cbfs_header *tmp = romarea + offset;
if (tmp->magic == ntohl(CBFS_HEADER_MAGIC)) {
- master_header = tmp;
+ cbfstool_master_header = tmp;
break;
}
}
- return master_header ? 0 : 1;
+ return cbfstool_master_header ? 0 : 1;
}
void recalculate_rom_geometry(void *romarea)
@@ -217,25 +220,25 @@ void recalculate_rom_geometry(void *romarea)
}
/* Update old headers */
- if (master_header->version == CBFS_HEADER_VERSION1 &&
- ntohl(master_header->architecture) == CBFS_ARCHITECTURE_UNKNOWN) {
+ if (cbfstool_master_header->version == CBFS_HEADER_VERSION1 &&
+ ntohl(cbfstool_master_header->architecture) == CBFS_ARCHITECTURE_UNKNOWN) {
DEBUG("Updating CBFS master header to version 2\n");
- master_header->architecture = htonl(CBFS_ARCHITECTURE_X86);
+ cbfstool_master_header->architecture = htonl(CBFS_ARCHITECTURE_X86);
}
- arch = ntohl(master_header->architecture);
+ arch = ntohl(cbfstool_master_header->architecture);
switch (arch) {
case CBFS_ARCHITECTURE_ARMV7:
- offset = romarea;
- phys_start = (0 + ntohl(master_header->offset)) & 0xffffffff;
+ cbfstool_offset = romarea;
+ phys_start = (0 + ntohl(cbfstool_master_header->offset)) & 0xffffffff;
phys_end = romsize & 0xffffffff;
break;
case CBFS_ARCHITECTURE_X86:
- offset = romarea + romsize - 0x100000000ULL;
- phys_start = (0 - romsize + ntohl(master_header->offset)) &
+ cbfstool_offset = romarea + romsize - 0x100000000ULL;
+ phys_start = (0 - romsize + ntohl(cbfstool_master_header->offset)) &
0xffffffff;
- phys_end = (0 - ntohl(master_header->bootblocksize) -
+ phys_end = (0 - ntohl(cbfstool_master_header->bootblocksize) -
sizeof(struct cbfs_header)) & 0xffffffff;
break;
default:
@@ -243,7 +246,7 @@ void recalculate_rom_geometry(void *romarea)
exit(1);
}
- align = ntohl(master_header->align);
+ align = ntohl(cbfstool_master_header->align);
}
void *loadrom(const char *filename)
@@ -351,8 +354,8 @@ void print_cbfs_directory(const char *filename)
printf
("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\n"
"alignment: %d bytes, architecture: %s\n\n",
- basename(name), romsize / 1024, ntohl(master_header->bootblocksize),
- romsize, ntohl(master_header->offset), align, arch_to_string(arch));
+ basename(name), romsize / 1024, ntohl(cbfstool_master_header->bootblocksize),
+ romsize, ntohl(cbfstool_master_header->offset), align, arch_to_string(arch));
free(name);
printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
uint32_t current = phys_start;
@@ -364,12 +367,12 @@ void print_cbfs_directory(const char *filename)
struct cbfs_file *thisfile =
(struct cbfs_file *)phys_to_virt(current);
uint32_t length = ntohl(thisfile->len);
- char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
+ const char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
if (strlen(fname) == 0)
fname = "(empty)";
printf("%-30s 0x%-8x %-12s %d\n", fname,
- current - phys_start + ntohl(master_header->offset),
+ current - phys_start + ntohl(cbfstool_master_header->offset),
strfiletype(ntohl(thisfile->type)), length);
/* note the components of the subheader are in host order ... */
@@ -664,7 +667,7 @@ void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
}
int create_cbfs_image(const char *romfile, uint32_t _romsize,
- const char *bootblock, uint32_t align, uint32_t offs)
+ const char *bootblock, uint32_t create_align, uint32_t offs)
{
uint32_t bootblocksize = 0;
struct cbfs_header *master_header;
@@ -679,8 +682,8 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize,
}
memset(romarea, 0xff, romsize);
- if (align == 0)
- align = 64;
+ if (create_align == 0)
+ create_align = 64;
bootblk = loadfile(bootblock, &bootblocksize,
romarea + romsize, SEEK_END);
@@ -695,7 +698,7 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize,
switch (arch) {
case CBFS_ARCHITECTURE_ARMV7:
/* Set up physical/virtual mapping */
- offset = romarea;
+ cbfstool_offset = romarea;
/*
* The initial jump instruction and bootblock will be placed
@@ -746,7 +749,7 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize,
case CBFS_ARCHITECTURE_X86:
// Set up physical/virtual mapping
- offset = romarea + romsize - 0x100000000ULL;
+ cbfstool_offset = romarea + romsize - 0x100000000ULL;
loadfile(bootblock, &bootblocksize, romarea + romsize,
SEEK_END);