是否可以就地替换位于另一个二进制文件中的二进制文件?例如,压缩的firmware.bin文件包含:
DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------
392 0x188 uImage header, header size: 64 bytes, header CRC: 0x15075729, created: 1969-12-31 23:59:59, image size: 1572736 bytes, Data Address: 0x20008000, Entry Point: 0x20008000, data CRC: 0x1DCD72E0, OS: Linux, CPU: ARM, image type: OS Kernel Image, compression type: none, image name: "abcd_rom_bin"
13596 0x351C gzip compressed data, maximum compression, from Unix, last modified: 2017-08-02 06:04:47
1573192 0x180148 uImage header, header size: 64 bytes, header CRC: 0x6FFB9B98, created: 1969-12-31 23:59:59, image size: 8376320 bytes, Data Address: 0x0, Entry Point: 0x0, data CRC: 0xC95886CF, OS: Linux, CPU: ARM, image type: Filesystem Image, compression type: none, image name: "abcd_rom_bin"
1573256 0x180188 Squashfs filesystem, little endian, non-standard signature, version 3.1, size: 8372772 bytes, 1028 inodes, blocksize: 131072 bytes, created: 2017-08-02 06:39:51
这些二进制文件之一还包含 busybox 二进制文件,有没有办法在不完全解压的情况下替换它?
答案1
file.gz
如果你想在偏移量 13596 处写入内容firmware.bin
(覆盖已经存在的内容),你可以这样做:
zsh
:
zmodload zsh/system
{ sysseek -u1 13596 && cat; } < file.gz 1<> firmware.bin
ksh93
:
cat < file.gz 1<> firmware.bin >#((13596))
dd
,任何 shell,但一次读取和写入一个字节
dd conv=notrunc bs=1 seek=13596 if=file.gz of=firmware.bin
GNU dd
,任何shell,更高效
dd bs=64k conv=notrunc oflag=seek_bytes seek=13596 if=file.gz of=firmware.bin
或者用零填充到 1573192 - 13596 的长度(用于算术扩展的 POSIX sh 语法):
dd bs=64k conv=notrunc,sync bs="$((1573192 - 13596))" count=1 \
oflag=seek_bytes seek=13596 if=file.gz of=firmware.bin
现在您file.gz
最多需要与gzip
其中已有的 ped 文件一样大。如果那些标头包含不同部分的校验和,那么您还需要计算和更新校验和。