Various fixes to cbfstool.
- add ntohll and htonll (as coreboot parses 64bit fields now)
- use the same byte swapping code across platforms
- detect endianess early
- fix lots of warnings
- Don't override CFLAGS in Makefile
Change-Id: Iaea02ff7a31ab6a95fd47858d0efd9af764a3e5f
Signed-off-by: Stefan Reinauer <reinauer@google.com>
Reviewed-on: http://review.coreboot.org/313
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
diff --git a/util/cbfstool/Makefile b/util/cbfstool/Makefile
index 843e677..0b61342 100644
--- a/util/cbfstool/Makefile
+++ b/util/cbfstool/Makefile
@@ -1,9 +1,10 @@
obj ?= $(shell pwd)
-HOSTCXX ?= g++
-HOSTCC ?= gcc
-CFLAGS ?= -g
-
+HOSTCXX ?= g++
+HOSTCC ?= gcc
+CFLAGS ?= -g -Wall
+CXXFLAGS ?=-DCOMPACT $(CFLAGS)
+LDFLAGS ?= -g
BINARY:=$(obj)/cbfstool
@@ -44,9 +45,5 @@
tags:
ctags *.[ch]
-CXXFLAGS=-DCOMPACT -g
-CFLAGS=-g
-LDFLAGS=-g
-
$(obj)/cbfstool:$(COMMON)
$(HOSTCXX) $(LDFLAGS) -o $@ $^
diff --git a/util/cbfstool/cbfs-mkpayload.c b/util/cbfstool/cbfs-mkpayload.c
index baa92ff..ff6479d 100644
--- a/util/cbfstool/cbfs-mkpayload.c
+++ b/util/cbfstool/cbfs-mkpayload.c
@@ -152,9 +152,9 @@
if (phdr[i].p_filesz == 0) {
segs[segments].type = PAYLOAD_SEGMENT_BSS;
segs[segments].load_addr =
- (unsigned long long)htonl(phdr[i].p_paddr);
+ (uint64_t)htonll(phdr[i].p_paddr);
segs[segments].mem_len =
- (unsigned int)htonl(phdr[i].p_memsz);
+ (uint32_t)htonl(phdr[i].p_memsz);
segs[segments].offset = htonl(doffset);
segments++;
@@ -162,8 +162,8 @@
}
segs[segments].type = PAYLOAD_SEGMENT_DATA;
- segs[segments].load_addr = (unsigned int)htonl(phdr[i].p_paddr);
- segs[segments].mem_len = (unsigned int)htonl(phdr[i].p_memsz);
+ segs[segments].load_addr = (uint64_t)htonll(phdr[i].p_paddr);
+ segs[segments].mem_len = (uint32_t)htonl(phdr[i].p_memsz);
segs[segments].compression = htonl(algo);
segs[segments].offset = htonl(doffset);
@@ -190,7 +190,7 @@
}
segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
- segs[segments++].load_addr = (unsigned long long)htonl(ehdr->e_entry);
+ segs[segments++].load_addr = (uint64_t)htonll(ehdr->e_entry);
*output = sptr;
diff --git a/util/cbfstool/cbfs-mkstage.c b/util/cbfstool/cbfs-mkstage.c
index 2f81da07..c9163ef 100644
--- a/util/cbfstool/cbfs-mkstage.c
+++ b/util/cbfstool/cbfs-mkstage.c
@@ -36,10 +36,12 @@
return x;
}
-unsigned int swap32(unsigned int x)
+/* This is a wrapper around the swab32() macro to make it
+ * usable for the current implementation of parse_elf_to_stage()
+ */
+static unsigned int swap32(unsigned int x)
{
- return ((x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) |
- (x << 24));
+ return swab32(x);
}
unsigned int (*elf32_to_native) (unsigned int) = idemp;
@@ -59,7 +61,6 @@
unsigned int data_start, data_end, mem_end;
int elf_bigendian = 0;
- int host_bigendian = 0;
comp_func_ptr compress = compression_function(algo);
if (!compress)
@@ -73,11 +74,6 @@
if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
elf_bigendian = 1;
}
- char test[4] = "1234";
- uint32_t inttest = *(uint32_t *) test;
- if (inttest == 0x31323334) {
- host_bigendian = 1;
- }
if (elf_bigendian != host_bigendian) {
elf32_to_native = swap32;
}
@@ -171,10 +167,10 @@
stage = (struct cbfs_stage *)out;
- stage->load = data_start;
+ stage->load = data_start; /* FIXME: htonll */
stage->memlen = mem_end - data_start;
stage->compression = algo;
- stage->entry = ehdr->e_entry;
+ stage->entry = ehdr->e_entry; /* FIXME: htonll */
compress(buffer, data_end - data_start,
(char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len);
diff --git a/util/cbfstool/cbfs.h b/util/cbfstool/cbfs.h
index 5c93838a..cdd0030 100644
--- a/util/cbfstool/cbfs.h
+++ b/util/cbfstool/cbfs.h
@@ -29,7 +29,7 @@
} __attribute__ ((packed));
struct cbfs_file {
- char magic[8];
+ uint8_t magic[8];
uint32_t len;
uint32_t type;
uint32_t checksum;
@@ -37,11 +37,11 @@
} __attribute__ ((packed));
struct cbfs_stage {
- unsigned int compression;
- unsigned long long entry;
- unsigned long long load;
- unsigned int len;
- unsigned int memlen;
+ uint32_t compression;
+ uint64_t entry;
+ uint64_t load;
+ uint32_t len;
+ uint32_t memlen;
} __attribute__ ((packed));
#define PAYLOAD_SEGMENT_CODE 0x45444F43
@@ -51,12 +51,12 @@
#define PAYLOAD_SEGMENT_ENTRY 0x52544E45
struct cbfs_payload_segment {
- unsigned int type;
- unsigned int compression;
- unsigned int offset;
- unsigned long long load_addr;
- unsigned int len;
- unsigned int mem_len;
+ uint32_t type;
+ uint32_t compression;
+ uint32_t offset;
+ uint64_t load_addr;
+ uint32_t len;
+ uint32_t mem_len;
} __attribute__ ((packed));
struct cbfs_payload {
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c
index f017b2e..b8abb51 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -19,7 +19,9 @@
*/
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
#include "common.h"
#include "cbfs.h"
@@ -196,7 +198,6 @@
static int cbfs_create(int argc, char **argv)
{
char *romname = argv[1];
- char *cmd = argv[2];
if (argc < 5) {
printf("not enough arguments to 'create'.\n");
return 1;
@@ -239,7 +240,6 @@
static int cbfs_print(int argc, char **argv)
{
char *romname = argv[1];
- char *cmd = argv[2];
void *rom = loadrom(romname);
if (rom == NULL) {
@@ -254,7 +254,6 @@
static int cbfs_extract(int argc, char **argv)
{
char *romname = argv[1];
- char *cmd = argv[2];
void *rom = loadrom(romname);
if (rom == NULL) {
@@ -271,7 +270,7 @@
return extract_file_from_cbfs(romname, argv[3], argv[4]);
}
-struct command commands[] = {
+static const struct command commands[] = {
{CMD_ADD, "add", cbfs_add},
{CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
{CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
@@ -281,7 +280,7 @@
{CMD_EXTRACT, "extract", cbfs_extract},
};
-void usage(void)
+static void usage(void)
{
printf
("cbfstool: Management utility for CBFS formatted ROM images\n\n"
@@ -302,6 +301,20 @@
print_supported_filetypes();
}
+/* Small, OS/libc independent runtime check
+ * for endianess
+ */
+int host_bigendian = 0;
+
+static void which_endian(void)
+{
+ char test[4] = "1234";
+ uint32_t inttest = *(uint32_t *) test;
+ if (inttest == 0x31323334) {
+ host_bigendian = 1;
+ }
+}
+
int main(int argc, char **argv)
{
int i;
@@ -311,6 +324,8 @@
return 1;
}
+ which_endian();
+
char *cmd = argv[2];
for (i = 0; i < ARRAY_SIZE(commands); i++) {
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index cbf0757..df74421 100644
--- a/util/cbfstool/common.c
+++ b/util/cbfstool/common.c
@@ -26,7 +26,7 @@
#include "cbfs.h"
#include "elf.h"
-#define dprintf
+#define dprintf(x...)
uint32_t getfilesize(const char *filename)
{
@@ -118,7 +118,7 @@
struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
{
struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
- strncpy(nextfile->magic, "LARCHIVE", 8);
+ strncpy((char *)(nextfile->magic), "LARCHIVE", 8);
nextfile->len = htonl(size);
nextfile->type = htonl(0xffffffff);
nextfile->checksum = 0; // FIXME?
@@ -231,6 +231,7 @@
uint32_t length = ntohl(thisfile->len);
// Locate the file name
char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
+
// It's not the file we are looking for..
if (strcmp(fname, payloadname) != 0)
{
@@ -267,7 +268,8 @@
// We'll only dump one file.
return 0;
}
-
+ printf("File %s not found.\n", payloadname);
+ return 1;
}
@@ -332,8 +334,7 @@
thisfile->len =
htonl(location - current -
ntohl(thisfile->offset));
- struct cbfs_file *nextfile =
- cbfs_create_empty_file(location,
+ cbfs_create_empty_file(location,
length -
(location -
current));
@@ -376,7 +377,7 @@
}
memset(newdata, 0xff, *datasize + headersize);
struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
- strncpy(nextfile->magic, "LARCHIVE", 8);
+ strncpy((char *)(nextfile->magic), "LARCHIVE", 8);
nextfile->len = htonl(*datasize);
nextfile->type = htonl(type);
nextfile->checksum = 0; // FIXME?
@@ -421,8 +422,7 @@
recalculate_rom_geometry(romarea);
- struct cbfs_file *one_empty_file =
- cbfs_create_empty_file((0 - romsize) & 0xffffffff,
+ cbfs_create_empty_file((0 - romsize) & 0xffffffff,
romsize - bootblocksize -
sizeof(struct cbfs_header) -
sizeof(struct cbfs_file) - 16);
@@ -439,7 +439,7 @@
uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
const char *filename, uint32_t alignment)
{
- void *rom = loadrom(romfile);
+ loadrom(romfile);
int filename_size = strlen(filename);
int headersize =
diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h
index 8f397d8..7034e0c 100644
--- a/util/cbfstool/common.h
+++ b/util/cbfstool/common.h
@@ -17,23 +17,23 @@
*/
#include <stdint.h>
-#ifndef WIN32
-#include <arpa/inet.h>
-#else
-#define ntohl(x) (((x)>>24) | ((x)<<24) | (((x)>>8)&0xff00) | (((x)<<8)&0xff0000))
-#define htonl ntohl
-#endif
+#include "swab.h"
+#define ntohl(x) (host_bigendian?(x):swab32(x))
+#define htonl(x) (host_bigendian?(x):swab32(x))
+#define ntohll(x) (host_bigendian?(x):swab64(x))
+#define htonll(x) (host_bigendian?(x):swab64(x))
extern void *offset;
extern struct cbfs_header *master_header;
extern uint32_t phys_start, phys_end, align, romsize;
+extern int host_bigendian;
-static void *phys_to_virt(uint32_t addr)
+static inline void *phys_to_virt(uint32_t addr)
{
return offset + addr;
}
-static uint32_t virt_to_phys(void *addr)
+static inline uint32_t virt_to_phys(void *addr)
{
return (unsigned long)(addr - offset) & 0xffffffff;
}
diff --git a/util/cbfstool/swab.h b/util/cbfstool/swab.h
new file mode 100644
index 0000000..c5e4370
--- /dev/null
+++ b/util/cbfstool/swab.h
@@ -0,0 +1,44 @@
+#ifndef _SWAB_H
+#define _SWAB_H
+
+/*
+ * linux/byteorder/swab.h
+ * Byte-swapping, independently from CPU endianness
+ * swabXX[ps]?(foo)
+ *
+ * Francois-Rene Rideau <fare@tunes.org> 19971205
+ * separated swab functions from cpu_to_XX,
+ * to clean up support for bizarre-endian architectures.
+ *
+ * See asm-i386/byteorder.h and suches for examples of how to provide
+ * architecture-dependent optimized versions
+ *
+ */
+
+/* casts are necessary for constants, because we never know how for sure
+ * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
+ */
+#define swab16(x) \
+ ((unsigned short)( \
+ (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \
+ (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
+
+#define swab32(x) \
+ ((unsigned int)( \
+ (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
+ (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) | \
+ (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) | \
+ (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) ))
+
+#define swab64(x) \
+ ((uint64_t)( \
+ (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
+ (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
+ (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
+ (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
+ (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
+ (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
+ (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
+ (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) ))
+
+#endif /* _SWAB_H */