Hung-Te Lin (hungte@chromium.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/2199
-gerrit
commit ff37111b8f6db6d6f7e121760efc5dc3b86fa80a Author: Hung-Te Lin hungte@chromium.org Date: Mon Jan 28 15:53:34 2013 +0800
cbfstool: move endian utilities to common.
The 'host_endian' variable requires detection by which_endian() first, which may be easily forgot by developers; and should not be made in cbfstool.c. It's now moved to "common" and don't need initialization.
Also cleaned up some header files.
Change-Id: I13dabd1ad15d2d6657137d29138e0878040cb205 Signed-off-by: Hung-Te Lin hungte@chromium.org --- util/cbfstool/cbfs-mkstage.c | 2 +- util/cbfstool/cbfstool.c | 17 +---------------- util/cbfstool/common.c | 27 +++++++++++++++++++++++++-- util/cbfstool/common.h | 21 ++++++++++----------- 4 files changed, 37 insertions(+), 30 deletions(-)
diff --git a/util/cbfstool/cbfs-mkstage.c b/util/cbfstool/cbfs-mkstage.c index ef5182a..4374bda 100644 --- a/util/cbfstool/cbfs-mkstage.c +++ b/util/cbfstool/cbfs-mkstage.c @@ -77,7 +77,7 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output, if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) { elf_bigendian = 1; } - if (elf_bigendian != host_bigendian) { + if (elf_bigendian != is_big_endian()) { elf32_to_native = swap32; }
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 48812d9..b0e5148 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -19,11 +19,10 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA */
+#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <ctype.h> -#include <unistd.h> #include <getopt.h> #include "common.h" #include "cbfs.h" @@ -493,18 +492,6 @@ static void usage(char *name) print_supported_filetypes(); }
-/* Small, OS/libc independent runtime check for endianess */ -int host_bigendian = 0; - -static void which_endian(void) -{ - static const uint32_t inttest = 0x12345678; - uint8_t inttest_lsb = *(uint8_t *)&inttest; - if (inttest_lsb == 0x12) { - host_bigendian = 1; - } -} - int main(int argc, char **argv) { size_t i; @@ -515,8 +502,6 @@ int main(int argc, char **argv) return 1; }
- which_endian(); - param.cbfs_name = argv[1]; char *cmd = argv[2]; optind += 2; diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c index 3e48534..a6cfa22 100644 --- a/util/cbfstool/common.c +++ b/util/cbfstool/common.c @@ -19,14 +19,28 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA */
+#include <assert.h> +#include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <libgen.h> #include "common.h" #include "cbfs.h" #include "elf.h"
+/* Utilities */ + +/* Small, OS/libc independent runtime check for endianess */ +int is_big_endian(void) +{ + static const uint32_t inttest = 0x12345678; + uint8_t inttest_lsb = *(uint8_t *)&inttest; + if (inttest_lsb == 0x12) { + return 1; + } + return 0; +} + size_t getfilesize(const char *filename) { size_t size; @@ -269,11 +283,20 @@ uint64_t intfiletype(const char *name) return -1; }
+/* basename(3) may modify buffer, so we want a tiny alternative. */ +static const char *simple_basename(const char *name) { + const char *slash = strrchr(name, '/'); + if (slash) + return slash + 1; + else + return name; +} + 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((char *)filename), romsize / 1024, + simple_basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize), romsize, ntohl(master_header->offset), align, arch_to_string(arch)); diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h index 45b88af..a196d45 100644 --- a/util/cbfstool/common.h +++ b/util/cbfstool/common.h @@ -21,13 +21,7 @@ #define __CBFSTOOL_COMMON_H
#include <stdint.h> -#include "swab.h" -#ifndef __APPLE__ -#define ntohl(x) (host_bigendian?(x):swab32(x)) -#define htonl(x) (host_bigendian?(x):swab32(x)) -#endif -#define ntohll(x) (host_bigendian?(x):swab64(x)) -#define htonll(x) (host_bigendian?(x):swab64(x)) +#include "cbfs.h"
/* Message output */ extern int verbose; @@ -37,6 +31,15 @@ extern int verbose; #define INFO(x...) { if (verbose > 0) fprintf(stderr, "INFO: " x); } #define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); }
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +/* Endianess */ +#include <arpa/inet.h> /* for ntohl, htonl */ +#include "swab.h" +extern int is_big_endian(void); +#define ntohll(x) (is_big_endian() ? (x) : swab64(x)) +#define htonll(x) (is_big_endian() ? (x) : swab64(x)) + extern void *offset; extern uint32_t romsize; extern int host_bigendian; @@ -98,8 +101,4 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output, int parse_flat_binary_to_payload(unsigned char *input, unsigned char **output, int32_t input_size, uint32_t loadaddress, uint32_t entrypoint, comp_algo algo); - - -#define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0])) - #endif