openwrt固件修改(squashfs+jffs2格式固件)
脚本如下:
#!/bin/sh
sudo echo "Starting..."
MKSQSHFS4='./bin/mksquashfs4'
PADJFFS2='./bin/padjffs2'
case "$1" in
'extract'|'e')
offset1=`grep -oba hsqs $2 | grep -oP '[0-9]*(?=:hsqs)'`
offset2=`wc -c $2 | grep -oP '[0-9]*(?= )'`
size2=`expr $offset2 - $offset1`
#echo $offset1 " " $offset2 " " $size2
dd if=$2 of=kernel.bin bs=1 ibs=1 count=$offset1
dd if=$2 of=secondchunk.bin bs=1 ibs=1 count=$size2 skip=$offset1
sudo rm -rf squashfs-root 2>&1
sudo unsquashfs -d squashfs-root secondchunk.bin
rm secondchunk.bin
;;
'create'|'c')
sudo $MKSQSHFS4 ./squashfs-root ./newsecondchunk.bin -nopad -noappend -root-owned -comp xz -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2 -b 256k -processors 1
sudo chown $USER ./newsecondchunk.bin
cat kernel.bin newsecondchunk.bin > $2
$PADJFFS2 $2
rm newsecondchunk.bin
;;
*)
echo 'run
"modify-firmware.sh extract firmware.bin"
You will find file "kernel.bin" and folder "squashfs-root".
Modify "squashfs-root" as you like,after everything is done,run
"modify-firmware.sh create newfirmware.bin"
And you will get a modified firmware named newfirmware.bin.
'
;;
esac
说明:
首先你要有一个linux系统,需要安装squashfs-tools
MKSQSHFS4='./bin/mksquashfs4' 和 PADJFFS2='./bin/padjffs2'用于指明mksquashfs4和padjffs2两个程序的位置。这两个程序可在编译过openwrt的源码文件夹或者openwrt sdk里找到,位置是staging_dir/host/bin/
把脚本modify-firmware.sh和固件firmware.bin放在一个文件夹下,运行modify-firmware.sh e firmware.bin,按提示输入sudo密码,会生成squashfs-root,这个文件夹就是解压后的固件根文件系统,此时可在该文件夹内删除或者添加文件(需要root权限)。修改完成后运行modify-firmware.sh c newfirmware.bin生成新固件newfirmware.bin