diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/cbfstool/Makefile | 13 | ||||
-rw-r--r-- | util/cbfstool/cbfs-mkpayload.c | 10 | ||||
-rw-r--r-- | util/cbfstool/cbfs-mkstage.c | 18 | ||||
-rw-r--r-- | util/cbfstool/cbfs.h | 24 | ||||
-rw-r--r-- | util/cbfstool/cbfstool.c | 25 | ||||
-rw-r--r-- | util/cbfstool/common.c | 18 | ||||
-rw-r--r-- | util/cbfstool/common.h | 16 | ||||
-rw-r--r-- | util/cbfstool/swab.h | 44 |
8 files changed, 110 insertions, 58 deletions
diff --git a/util/cbfstool/Makefile b/util/cbfstool/Makefile index 843e677503..0b61342bd2 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 @@ clean: 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 baa92ff237..ff6479dc66 100644 --- a/util/cbfstool/cbfs-mkpayload.c +++ b/util/cbfstool/cbfs-mkpayload.c @@ -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 = - (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 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output, } 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 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output, } 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 2f81da07d5..c9163ef6cf 100644 --- a/util/cbfstool/cbfs-mkstage.c +++ b/util/cbfstool/cbfs-mkstage.c @@ -36,10 +36,12 @@ unsigned int idemp(unsigned int 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; @@ -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; - int host_bigendian = 0; 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; } - 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 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output, 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 5c93838a36..cdd003062f 100644 --- a/util/cbfstool/cbfs.h +++ b/util/cbfstool/cbfs.h @@ -29,7 +29,7 @@ struct cbfs_header { } __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 @@ struct cbfs_file { } __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 @@ struct cbfs_stage { #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 f017b2e2e0..b8abb515bf 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_add_stage(int argc, char **argv) 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_locate(int argc, char **argv) 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_print(int argc, char **argv) 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 @@ static int cbfs_extract(int argc, char **argv) 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 @@ struct command commands[] = { {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 @@ void usage(void) 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 @@ int main(int argc, char **argv) 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 cbf0757aaa..df744217d4 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 @@ 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); - 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 @@ 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)); + // 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; } - + 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)); - struct cbfs_file *nextfile = - cbfs_create_empty_file(location, + cbfs_create_empty_file(location, 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; - 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 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize, 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 @@ 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) { - 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 8f397d8e65..7034e0c1cf 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 0000000000..c5e43709a6 --- /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 */ |