hex2bin 的用法

直接執行不加參數即可得到使用方法:

$./hex2bin 
hex2bin v1.0.10, Copyright (C) 2012 Jacques Pelletier & contributors


usage: hex2bin [OPTIONS] filename
Options:
  -s [address]  Starting address in hex (default: 0)
  -l [length]   Maximal Length (Starting address + Length -1 is Max Address)
                File will be filled with Pattern until Max Address is reached
                Length must be a power of 2 in hexadecimal:
                     Hex      Decimal
                    1000   =    4096   (4ki)
                    2000   =    8192   (8ki)
                    4000   =   16384  (16ki)
                    8000   =   32768  (32ki)
                   10000   =   65536  (64ki)
                   20000   =  131072 (128ki)
                   40000   =  262144 (256ki)
                   80000   =  524288 (512ki)
                  100000   = 1048576   (1Mi)
                  200000   = 2097152   (2Mi)
                  400000   = 4194304   (4Mi)
                  800000   = 8388608   (8Mi)
  -e [ext]      Output filename extension (without the dot)
  -c            Enable hex file checksum verification
  -p [value]    Pad-byte value in hex (default: ff)

  -k [0|1|2]    Select checksum type
                       0 = 8-bit,
                       1 = 16-bit little endian,
                       2 = 16-bit big endian
  -r [start] [end]     Range to compute checksum over (default is min and max addresses)
  -f [address] [value] Address and value of checksum to force

舉來說,我用 sdcc 編譯所得到的檔案為 hello.ihx (Intel-HEX format),
為了得到 hello.bin 所用命令如下:

$ hex2bin -s 0000 -l 1000 -p ff hello.ihx
hex2bin v1.0.10, Copyright (C) 2012 Jacques Pelletier & contributors

Lowest address  = 00000000
Highest address = 00000FFF
Pad Byte        = FF
8-bit Checksum = 1E
$  

“-s 0000 -l 1000 -p ff” 說明了:
起始位置: 0x0000,長度:0x1000,HEX File 中未定義的的值一律填入 0xff
並自動計算出 8-bit checksum = 0x1E (只是將 bin file 中所有的值加總)

有另外一種用法,
如果你想指定 8-bit checksum 的值(例如0x55),
例如 boot loader 從 Serial Flash 將 hello.bin 讀出來後,
要先做簡單的 checksum 檢查無誤之後,才進行下一個動作,

命令如下;

$ hex2bin -s 0000 -l 1000 -p ff -k 0 -f 0fff 55 hello.ihx
hex2bin v1.0.10, Copyright (C) 2012 Jacques Pelletier & contributors

Lowest address  = 00000000
Highest address = 00000FFF
Pad Byte        = FF
8-bit Checksum = 25
Addr 00000FFF set to 2F
$ 

這命令會在 位置 0x0fff 填入 0x2f
使得 8-bit checksim 從原先的 0x25 變成指定的 0x55

compile 32-bit hex2bin application on 64-bit Linux OS

因工作上需要 hex2bin 的工具 又懶得自己寫
在網路上發現一個寫的還不錯的 hex2bin source code : hex2bin-1.0.9

其中包含了兩個程式 hex2bin and mot2bin
但使用時出現了錯誤的情形,
原來是 compile 時必須是 32-bit 架構,而我卻是在 64-bit Linux 下編譯的,
為了解決這問題,
首先安裝 32-bit gcc compiler

$ sudo zypper in libstdc++47-devel-32bit
$ sudo zypper in gcc47-32bit 

接下來修改 hex2bin 的 Makefile,在 gcc 後面加上 -m32 參數
重新編譯後即可正常使用!

GNU Cross Compiler 編譯程序

寫了一個 Makefile,可以半自動的編譯 GNU Cross Compiler
做個記錄,怕以後忘記

#TARGET      = arm-elf
#TARGET      = mips-elf
TARGET      = sparc-elf

PKG_DIR     = $(CURDIR)/PKG
SRC_DIR     = $(CURDIR)/SRC
BUILD_DIR   = $(CURDIR)/BUILD-$(TARGET)
TARGET_DIR  = /srv/xfile/compiler/$(TARGET)-4.7.0

BINUTILS_VER = binutils-2.22
GCC_VER      = gcc-4.7.0
MPC_VER      = mpc-0.9
MPFR_VER     = mpfr-3.1.0
GMP_VER      = gmp-5.0.4
NEWLIB_VER   = newlib-1.20.0
GDB_VER      = gdb-7.3.1
INSIGHT_VER  = insight-6.8-1

GCC_PACKAGE        = $(PKG_DIR)/gcc/$(GCC_VER).tar.bz2
GCC_SRC_DIR        = $(SRC_DIR)/$(GCC_VER)
GCC_BUILD_DIR      = $(BUILD_DIR)/$(GCC_VER)

GMP_PACKAGE        = $(PKG_DIR)/gmp/$(GMP_VER).tar.bz2
GMP_SRC_DIR        = $(SRC_DIR)/$(GMP_VER)
GMP_BUILD_DIR      = $(BUILD_DIR)/$(GMP_VER)

MPFR_PACKAGE       = $(PKG_DIR)/mpfr/$(MPFR_VER).tar.bz2
MPFR_SRC_DIR        = $(SRC_DIR)/$(MPFR_VER)
MPFR_BUILD_DIR      = $(BUILD_DIR)/$(MPFR_VER)

MPC_PACKAGE        = $(PKG_DIR)/mpc/$(MPC_VER).tar.gz
MPC_SRC_DIR        = $(SRC_DIR)/$(MPC_VER)
MPC_BUILD_DIR      = $(BUILD_DIR)/$(MPC_VER)

BINUTILS_PACKAGE   = $(PKG_DIR)/binutils/$(BINUTILS_VER).tar.bz2
BINUTILS_SRC_DIR   = $(SRC_DIR)/$(BINUTILS_VER)
BINUTILS_BUILD_DIR = $(BUILD_DIR)/$(BINUTILS_VER)

GDB_PACKAGE        = $(PKG_DIR)/gdb/$(GDB_VER).tar.gz
GDB_SRC_DIR        = $(SRC_DIR)/$(GDB_VER)
GDB_BUILD_DIR      = $(BUILD_DIR)/$(GDB_VER)

NEWLIB_PACKAGE     = $(PKG_DIR)/newlib/$(NEWLIB_VER).tar.gz
NEWLIB_SRC_DIR     = $(SRC_DIR)/$(NEWLIB_VER)
NEWLIB_BUILD_DIR   = $(BUILD_DIR)/$(NEWLIB_VER)

INSIGHT_PACKAGE    = $(PKG_DIR)/insight/$(INSIGHT_VER).tar.bz2
INSIGHT_SRC_DIR    = $(SRC_DIR)/$(INSIGHT_VER)
INSIGHT_BUILD_DIR  = $(BUILD_DIR)/$(INSIGHT_VER)

CONFIG_FLAGS       = --target=$(TARGET) 
                     --prefix=$(TARGET_DIR)
note:
        @echo ----------------------------------------
        @echo make binutils    # Build binutils
        @echo make gcc-p0      # Build GMP, MPFR, MPC
        @echo make gcc-p1      # Build GCC Pass-1
        @echo make newlib      # Build newlib
        @echo make gcc-p2      # Build GCC Pass-2
        @echo make insight     # Build insight
        @echo ----------------------------------------

all: binutils gcc-p0 gcc-p1 newlib gcc-p2 insight

dir : src_dir

# Source Decompress

src_dir: $(BINUTILS_SRC_DIR) $(NEWLIB_SRC_DIR) $(GCC_SRC_DIR) $(GDB_SRC_DIR) $(INSIGHT_SRC_DIR) $(GMP_SRC_DIR) $(MPFR_SRC_DIR) $(MPC_SRC_DIR)

build_dir: $(BINUTILS_BUILD_DIR) $(NEWLIB_BUILD_DIR) $(GCC_BUILD_DIR) $(GDB_BUILD_DIR)

# ------------- BINUTIL ----------------

BINUTILS_CONF = $(CONFIG_FLAGS) --disable-nls

binutils: $(BINUTILS_BUILD_DIR)
        @echo "****** Build $(BINUTILS_VER) ***********"
        (cd $(BINUTILS_BUILD_DIR); $(BINUTILS_SRC_DIR)/configure $(BINUTILS_CONF);)
        (cd $(BINUTILS_BUILD_DIR); make all install)

$(BINUTILS_SRC_DIR): $(SRC_DIR)
        @echo "****** Decompress $(BINUTILS_PACKAGE) ***********"
        -@mkdir $(BINUTILS_SRC_DIR)
        (cd $(SRC_DIR); tar -jxvf $(BINUTILS_PACKAGE))

$(BINUTILS_BUILD_DIR): $(BUILD_DIR)
        @echo "****** create directory $(BINUTILS_BUILD_DIR) ***********"
        -@mkdir $(BINUTILS_BUILD_DIR)

# ------------- NEWLIB ----------------

NEWLIB_CONF  = $(CONFIG_FLAGS) --disable-nls

newlib: $(NEWLIB_BUILD_DIR)
        @echo "****** Build $(NEWLIB_VER) ***********"
        (set path = $(TARGET_DIR)/bin;cd $(NEWLIB_BUILD_DIR);$(NEWLIB_SRC_DIR)/configure $(NEWLIB_CONF);)
        (cd $(NEWLIB_BUILD_DIR); make all install)

$(NEWLIB_SRC_DIR): $(SRC_DIR)
        @echo "****** Decompress $(NEWLIB_PACKAGE) ***********"
        (cd $(SRC_DIR); tar -zxvf $(NEWLIB_PACKAGE))

$(NEWLIB_BUILD_DIR): $(BUILD_DIR)
        @echo "****** create directory $(NEWLIB_BUILD_DIR) ***********"
        -@mkdir $(NEWLIB_BUILD_DIR)

# ------------- GDB ----------------

GDB_CONF = $(CONFIG_FLAGS) --disable-nls

gdb: $(GDB_BUILD_DIR)
        @echo "****** Build $(GDB_VER) ***********"
        (cd $(GDB_BUILD_DIR); $(GDB_SRC_DIR)/configure $(GDB_CONF);)
        (cd $(GDB_BUILD_DIR); make all install))

$(GDB_SRC_DIR): $(SRC_DIR)
        @echo "****** Decompress $(GDB_PACKAGE) ***********"
        (cd $(SRC_DIR); tar -zxvf $(GDB_PACKAGE))

$(GDB_BUILD_DIR): $(BUILD_DIR)
        @echo "****** create directory $(GDB_BUILD_DIR) ***********"
        -@mkdir $(GDB_BUILD_DIR)

# ------------- INSIGHT ----------------
INSIGHT_CONF = $(CONFIG_FLAGS) --disable-nls --disable-werror

insight: $(INSIGHT_BUILD_DIR)
        @echo "****** Build $(INSIGHT_VER) ***********"
        (cd $(INSIGHT_BUILD_DIR); $(INSIGHT_SRC_DIR)/configure $(INSIGHT_CONF);)
        (cd $(INSIGHT_BUILD_DIR); make all install)

$(INSIGHT_SRC_DIR): $(SRC_DIR)
        @echo "****** Decompress $(INSIGHT_PACKAGE) ***********"
        (cd $(SRC_DIR); tar -jxvf $(INSIGHT_PACKAGE))

$(INSIGHT_BUILD_DIR): $(BUILD_DIR)
        @echo "****** create directory $(INSIGHT_BUILD_DIR) ***********"
        -@mkdir $(INSIGHT_BUILD_DIR)

# ------------- gcc-p0 ----------------

gcc-p0: gmp mpfr mpc

# ------------- GMP ----------------

GMP_CONF = --prefix=$(TARGET_DIR)

gmp: $(GMP_BUILD_DIR)
        @echo "****** Build $(GMP_VER) ***********"
        (cd $(GMP_BUILD_DIR); $(GMP_SRC_DIR)/configure $(GMP_CONF);)
        (cd $(GMP_BUILD_DIR); make all install)

$(GMP_SRC_DIR): $(SRC_DIR)
        @echo "****** Decompress $(INSIGHT_PACKAGE) ***********"
        (cd $(SRC_DIR); tar -jxvf $(GMP_PACKAGE))

$(GMP_BUILD_DIR): $(BUILD_DIR)
        @echo "****** create directory $(GMP_BUILD_DIR) ***********"
        -@mkdir $(GMP_BUILD_DIR)

# ------------- MPFR ----------------

MPFR_CONF = --prefix=$(TARGET_DIR) 
            --with-gmp=$(TARGET_DIR)

mpfr: $(MPFR_BUILD_DIR)
        @echo "****** Build $(MPFR_VER) ***********"
        (cd $(MPFR_BUILD_DIR); $(MPFR_SRC_DIR)/configure $(MPFR_CONF);)
        (cd $(MPFR_BUILD_DIR); make all install)

$(MPFR_SRC_DIR): $(SRC_DIR)
        @echo "****** Decompress $(MPFR_PACKAGE) ***********"
        (cd $(SRC_DIR); tar -jxvf $(MPFR_PACKAGE))

$(MPFR_BUILD_DIR): $(BUILD_DIR)
        @echo "****** create directory $(MPFR_BUILD_DIR) ***********"
        -@mkdir $(MPFR_BUILD_DIR)

# ------------- MPC ----------------

MPC_CONF = --prefix=$(TARGET_DIR)   
           --with-gmp=$(TARGET_DIR) 
           --with-mpfr=$(TARGET_DIR)

mpc: $(MPC_BUILD_DIR)
        @echo "****** Build $(MPC_VER) ***********"
        (cd $(MPC_BUILD_DIR); $(MPC_SRC_DIR)/configure $(MPC_CONF);)
        (cd $(MPC_BUILD_DIR); make all install)

$(MPC_SRC_DIR): $(SRC_DIR)
        @echo "****** Decompress $(MPC_PACKAGE) ***********"
        (cd $(SRC_DIR); tar -zxvf $(MPC_PACKAGE))

$(MPC_BUILD_DIR): $(BUILD_DIR)
        @echo "****** create directory $(MPC_BUILD_DIR) ***********"
        -@mkdir $(MPC_BUILD_DIR)

# ------------- GCC ----------------
GCC_CONF = $(CONFIG_FLAGS)          
           --enable-interwork       
           --disable-multilib       
           --disable-nls            
           --disable-shared         
           --disable-threads        
           --disable-decimal-float  
           --disable-libmudflap     
           --disable-libssp         
           --disable-libgomp        
           --disable-libquadmath    
           --without-ppl            
           --without-cloog          
           --enable-languages=c,c++ 
           --with-newlib            
           --with-headers=$(NEWLIB_SRC_DIR)/newlib/libc/include 
           --with-gmp=$(TARGET_DIR)  
           --with-mpfr=$(TARGET_DIR) 
           --with-mpc=$(TARGET_DIR) 
           --enable-obsolete        

#           --with-mpfr-include=$(GCC_SRC_DIR)/mpfr/src 
#           --with-mpfr-lib=$(GCC_SRC_DIR)/mpfr/src/.lib

gcc-p1: $(GCC_BUILD_DIR)
        @echo "****** Build $(GCC_VER) Phase-1  ***********"
        (cd $(GCC_BUILD_DIR); $(GCC_SRC_DIR)/configure $(GCC_CONF);)
        (cd $(GCC_BUILD_DIR); make all-gcc install-gcc)

gcc-p2: 
        @echo "****** Build $(GCC_VER) Phase-2  ***********"
        (cd $(GCC_BUILD_DIR); make all install)

$(GCC_SRC_DIR): $(SRC_DIR)
        @echo "****** Decompress $(GCC_PACKAGE) ***********"
        (cd $(SRC_DIR); tar -jxvf $(GCC_PACKAGE))

$(GCC_BUILD_DIR): $(BUILD_DIR)
        @echo "****** create directory $(GCC_BUILD_DIR) ***********"
        -@mkdir $(GCC_BUILD_DIR)

# ------------- misc ----------------

$(SRC_DIR):
        @echo "****** create directory $(SRC_DIR) ***********"
        -@mkdir $(SRC_DIR)

$(BUILD_DIR):
        @echo "****** create directory $(BUILD_DIR) ***********"
        -@mkdir $(BUILD_DIR)

$(PKG_DIR):
        @echo ERROR! No $(GNU_DIR) found!
        exit

clean: clean-build clean-src

clean-src:
        @rm -rf $(SRC_DIR)

clean-build:
        @rm -rf BUILD-*

Issues while build insight-6.8-1 with –target=spare-elf configuration

使用 insight-6.8-1

% ../insight-6.8-1/configure --target=sparc-elf --prefix=$OPT/sparc-elf-4.7.0 --disable-nls --disable-werror

會出現以下 error messages:

...
/GNU/SRC/insight-6.8-1/sim/erc32/float.c:173:2: warning: #warning no fpu trap support for this target [-Wcpp]
gcc -DHAVE_CONFIG_H     -DPROFILE=1 -DWITH_PROFILE=-1          -DDEFAULT_INLINE=0           -DSTAT -DFAST_UART -DIUREV0 -DMECREV0   -I. -I/GNU/SRC/insight-6.8-1/sim/erc32 -I../common -I/GNU/SRC/insight-6.8-1/sim/erc32/../common -I../../include -I/GNU/SRC/insight-6.8-1/sim/erc32/../../include -I../../bfd -I/GNU/SRC/insight-6.8-1/sim/erc32/../../bfd -I../../opcodes -I/GNU/SRC/insight-6.8-1/sim/erc32/../../opcodes  -g -O2 -o sis 
  sis.o exec.o erc32.o func.o help.o float.o  ../../bfd/libbfd.a ../../opcodes/libopcodes.a  ../../libiberty/libiberty.a -lnsl  ../../readline/libreadline.a  -lm 
../../readline/libreadline.a(display.o): In function `cr':
/GNU/SRC/insight-6.8-1/readline/display.c:2205: undefined reference to `tputs'
../../readline/libreadline.a(display.o): In function `_rl_move_cursor_relative':
/GNU/SRC/insight-6.8-1/readline/display.c:1719: undefined reference to `tputs'
/GNU/SRC/insight-6.8-1/readline/display.c:1745: undefined reference to `tputs'
../../readline/libreadline.a(display.o): In function `_rl_move_vert':
/GNU/SRC/insight-6.8-1/readline/display.c:1783: undefined reference to `tputs'
...

查了很久之後,發現是 link erc32 時少了 ncurses 這個 library,也就是少了 -lncurses。
解法:patch insight-6.8-1 source code:
edit ./insight-6.8-1/sim/erc32/Makefile.in

 
Line.25:    SIM_EXTRA_LIBS = $(READLINE_LIB) $(TERMCAP_LIB) -lm -lncurses

重新編譯安裝就OK了!

% ../insight-6.8-1/configure --target=sparc-elf --prefix=$OPT/sparc-elf-4.7.0 --disable-nls --disable-werror
% make
% make install