Various fixes to cbfstool.
authorStefan Reinauer <reinauer@chromium.org>
Fri, 21 Oct 2011 21:24:57 +0000 (14:24 -0700)
committerStefan Reinauer <stefan.reinauer@coreboot.org>
Mon, 24 Oct 2011 18:29:29 +0000 (20:29 +0200)
- 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>
util/cbfstool/Makefile
util/cbfstool/cbfs-mkpayload.c
util/cbfstool/cbfs-mkstage.c
util/cbfstool/cbfs.h
util/cbfstool/cbfstool.c
util/cbfstool/common.c
util/cbfstool/common.h
util/cbfstool/swab.h [new file with mode: 0644]

index 843e6775031f11e1eaafefd878a4811fcbec569f..0b61342bd2379919ced89579f4e07f1d4ba8ee63 100644 (file)
@@ -1,9 +1,10 @@
 obj ?= $(shell pwd)
 
 obj ?= $(shell pwd)
 
-HOSTCXX ?= g++
-HOSTCC ?= gcc
-CFLAGS ?= -g
-
+HOSTCXX  ?= g++
+HOSTCC   ?= gcc
+CFLAGS   ?= -g -Wall
+CXXFLAGS ?=-DCOMPACT $(CFLAGS)
+LDFLAGS  ?= -g
 
 BINARY:=$(obj)/cbfstool
 
 
 BINARY:=$(obj)/cbfstool
 
@@ -44,9 +45,5 @@ clean:
 tags:
        ctags *.[ch]
 
 tags:
        ctags *.[ch]
 
-CXXFLAGS=-DCOMPACT -g
-CFLAGS=-g
-LDFLAGS=-g
-
 $(obj)/cbfstool:$(COMMON)
        $(HOSTCXX) $(LDFLAGS) -o $@ $^
 $(obj)/cbfstool:$(COMMON)
        $(HOSTCXX) $(LDFLAGS) -o $@ $^
index baa92ff2375629256959330773e43397067fa0af..ff6479dc660e82f2455eabd3523573e3bd6b4b86 100644 (file)
@@ -152,9 +152,9 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
                if (phdr[i].p_filesz == 0) {
                        segs[segments].type = PAYLOAD_SEGMENT_BSS;
                        segs[segments].load_addr =
                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 =
                        segs[segments].mem_len =
-                           (unsigned int)htonl(phdr[i].p_memsz);
+                           (uint32_t)htonl(phdr[i].p_memsz);
                        segs[segments].offset = htonl(doffset);
 
                        segments++;
                        segs[segments].offset = htonl(doffset);
 
                        segments++;
@@ -162,8 +162,8 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
                }
 
                segs[segments].type = PAYLOAD_SEGMENT_DATA;
                }
 
                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);
 
                segs[segments].compression = htonl(algo);
                segs[segments].offset = htonl(doffset);
 
@@ -190,7 +190,7 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
        }
 
        segs[segments].type = PAYLOAD_SEGMENT_ENTRY;
        }
 
        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;
 
 
        *output = sptr;
 
index 2f81da07d5e8137a0bacc65a44f95296ba29d420..c9163ef6cf585237c0a6b346914a64f386de5a37 100644 (file)
@@ -36,10 +36,12 @@ unsigned int idemp(unsigned int x)
        return x;
 }
 
        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;
 }
 
 unsigned int (*elf32_to_native) (unsigned int) = idemp;
@@ -59,7 +61,6 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
        unsigned int data_start, data_end, mem_end;
 
        int elf_bigendian = 0;
        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)
 
        comp_func_ptr compress = compression_function(algo);
        if (!compress)
@@ -73,11 +74,6 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
        if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
                elf_bigendian = 1;
        }
        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;
        }
        if (elf_bigendian != host_bigendian) {
                elf32_to_native = swap32;
        }
@@ -171,10 +167,10 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
 
        stage = (struct cbfs_stage *)out;
 
 
        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->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);
 
        compress(buffer, data_end - data_start,
                 (char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len);
index 5c93838a36b1ccc0e947a923714369b572d681e5..cdd003062fff7ada5d0af8ef622fc120b9c2002c 100644 (file)
@@ -29,7 +29,7 @@ struct cbfs_header {
 } __attribute__ ((packed));
 
 struct cbfs_file {
 } __attribute__ ((packed));
 
 struct cbfs_file {
-       char magic[8];
+       uint8_t magic[8];
        uint32_t len;
        uint32_t type;
        uint32_t checksum;
        uint32_t len;
        uint32_t type;
        uint32_t checksum;
@@ -37,11 +37,11 @@ struct cbfs_file {
 } __attribute__ ((packed));
 
 struct cbfs_stage {
 } __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
 } __attribute__ ((packed));
 
 #define PAYLOAD_SEGMENT_CODE   0x45444F43
@@ -51,12 +51,12 @@ struct cbfs_stage {
 #define PAYLOAD_SEGMENT_ENTRY  0x52544E45
 
 struct cbfs_payload_segment {
 #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 {
 } __attribute__ ((packed));
 
 struct cbfs_payload {
index f017b2e2e07f3d58e2c1d938b0e4ba09edd0693b..b8abb515bf42cdddcb3dbb9a2f7b49c9958468ac 100644 (file)
@@ -19,7 +19,9 @@
  */
 
 #include <stdio.h>
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <string.h>
+#include <ctype.h>
 #include "common.h"
 #include "cbfs.h"
 
 #include "common.h"
 #include "cbfs.h"
 
@@ -196,7 +198,6 @@ static int cbfs_add_stage(int argc, char **argv)
 static int cbfs_create(int argc, char **argv)
 {
        char *romname = argv[1];
 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;
        if (argc < 5) {
                printf("not enough arguments to 'create'.\n");
                return 1;
@@ -239,7 +240,6 @@ static int cbfs_locate(int argc, char **argv)
 static int cbfs_print(int argc, char **argv)
 {
        char *romname = argv[1];
 static int cbfs_print(int argc, char **argv)
 {
        char *romname = argv[1];
-       char *cmd = argv[2];
        void *rom = loadrom(romname);
 
        if (rom == NULL) {
        void *rom = loadrom(romname);
 
        if (rom == NULL) {
@@ -254,7 +254,6 @@ static int cbfs_print(int argc, char **argv)
 static int cbfs_extract(int argc, char **argv)
 {
        char *romname = argv[1];
 static int cbfs_extract(int argc, char **argv)
 {
        char *romname = argv[1];
-       char *cmd = argv[2];
        void *rom = loadrom(romname);
 
        if (rom == NULL) {
        void *rom = loadrom(romname);
 
        if (rom == NULL) {
@@ -271,7 +270,7 @@ static int cbfs_extract(int argc, char **argv)
        return extract_file_from_cbfs(romname, argv[3], argv[4]);
 }
 
        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},
        {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 @@ struct command commands[] = {
        {CMD_EXTRACT, "extract", cbfs_extract},
 };
 
        {CMD_EXTRACT, "extract", cbfs_extract},
 };
 
-void usage(void)
+static void usage(void)
 {
        printf
            ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
 {
        printf
            ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
@@ -302,6 +301,20 @@ void usage(void)
        print_supported_filetypes();
 }
 
        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;
 int main(int argc, char **argv)
 {
        int i;
@@ -311,6 +324,8 @@ int main(int argc, char **argv)
                return 1;
        }
 
                return 1;
        }
 
+       which_endian();
+
        char *cmd = argv[2];
 
        for (i = 0; i < ARRAY_SIZE(commands); i++) {
        char *cmd = argv[2];
 
        for (i = 0; i < ARRAY_SIZE(commands); i++) {
index cbf0757aaab152ba045394fd583b45d75df868ea..df744217d493fabe145a8b5e7cb7dc09a0ea984b 100644 (file)
@@ -26,7 +26,7 @@
 #include "cbfs.h"
 #include "elf.h"
 
 #include "cbfs.h"
 #include "elf.h"
 
-#define dprintf
+#define dprintf(x...)
 
 uint32_t getfilesize(const char *filename)
 {
 
 uint32_t getfilesize(const char *filename)
 {
@@ -118,7 +118,7 @@ int cbfs_file_header(uint32_t physaddr)
 struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
 {
        struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
 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?
        nextfile->len = htonl(size);
        nextfile->type = htonl(0xffffffff);
        nextfile->checksum = 0; // FIXME?
@@ -231,6 +231,7 @@ int extract_file_from_cbfs(const char *filename, const char *payloadname, const
                uint32_t length = ntohl(thisfile->len);
                // Locate the file name
                char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
                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)
                {
                // It's not the file we are looking for..
                if (strcmp(fname, payloadname) != 0)
                {
@@ -267,7 +268,8 @@ int extract_file_from_cbfs(const char *filename, const char *payloadname, const
                // We'll only dump one file.
                return 0;
        }
                // We'll only dump one file.
                return 0;
        }
-
+       printf("File %s not found.\n", payloadname);
+       return 1;
 }
 
 
 }
 
 
@@ -332,8 +334,7 @@ int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
                                        thisfile->len =
                                            htonl(location - current -
                                                  ntohl(thisfile->offset));
                                        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));
                                                                   length -
                                                                   (location -
                                                                    current));
@@ -376,7 +377,7 @@ void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
        }
        memset(newdata, 0xff, *datasize + headersize);
        struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
        }
        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?
        nextfile->len = htonl(*datasize);
        nextfile->type = htonl(type);
        nextfile->checksum = 0; // FIXME?
@@ -421,8 +422,7 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize,
 
        recalculate_rom_geometry(romarea);
 
 
        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);
                                   romsize - bootblocksize -
                                   sizeof(struct cbfs_header) -
                                   sizeof(struct cbfs_file) - 16);
@@ -439,7 +439,7 @@ static int in_segment(int addr, int size, int gran)
 uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
                            const char *filename, uint32_t alignment)
 {
 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 =
        int filename_size = strlen(filename);
 
        int headersize =
index 8f397d8e65690632dbbfda1815bd07231d383a79..7034e0c1cf4e221c3ce645c06e5a70b0d1f4f01b 100644 (file)
  */
 
 #include <stdint.h>
  */
 
 #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 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;
 }
 
 {
        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;
 }
 {
        return (unsigned long)(addr - offset) & 0xffffffff;
 }
diff --git a/util/cbfstool/swab.h b/util/cbfstool/swab.h
new file mode 100644 (file)
index 0000000..c5e4370
--- /dev/null
@@ -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 */