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