Search code examples
xxd

how to use xxd revert to start from an offset instead of from 0 address?


Let me explain the question by an example.

I first use memtool md -b 0x58040000+128K > rom.hex to dump the option rom of my PCIe card. The content looks like this (notice the address column starts from 0x58040000):

58040000: 55 aa 75 e9 9d 02 00 00 00 00 00 00 00 00 00 00    U.u.............
58040010: 00 00 00 00 00 00 00 00 40 02 00 00 00 00 49 42    [email protected]
58040020: 4d b2 74 92 00 00 00 00 00 00 00 00 00 00 00 04    M.t.............
58040030: 20 37 36 31 32 39 35 35 32 30 00 00 00 00 00 00     761295520......
58040040: 95 02 00 00 00 00 00 00 1c 02 00 00 00 00 00 00    ................
58040050: 31 31 2f 32 35 2f 32 30 20 30 34 3a 31 35 00 00    11/25/20 04:15..
58040060: 39 00 00 00 e9 a8 03 00 e9 b7 03 00 00 00 f4 00    9...............
58040070: 00 17 00 00 00 d0 01 00 18 16 20 e1 02 80 7e 00    .......... ...~.
...

Then I need to convert the hex dump to binary (you may wondering why not just use dd if=/dev/mem to get the binary in the first place? Well, on some platforms dd reports wrong address errors, for unknown reasons -- which is another question).

I use xxd -r to convert the hex dump to binary: xxd -r rom.hex rom.bin.

The "problem" is that the generated rom.bin is very big (1476788224 bytes), because xxd seems convert from address 0 rather than from 0x58040000 (what I expected).

I need to take the 3rd step to strip off the rom.bin (using dd e.g.), to get a 128K binary rom.

So my question is, how can I tell xxd to just convert the data in the hex dump, ignoring the address column? I tried several command options of xxd and did not success yet.

Thanks.


Solution

  • You can use xxd -r -s "-0x58040000" rom.hex > rom.bin to achieve that.

    -s tells xxd an offset should be considered (which is negative in your case), and by using > rom.bin instead of rom.bin, the command erases previous data of the file. In my WSL environment, rom.bin only rewrites the start of the file.