arabesc
Occasional Visitor
During NG firmware script patching research I've realized that stock
Here is a test:
The buggy output is:
And the expected output is:
The issue seems to be not critical for the stock firmware scripts because they don't use the sed insert command.
The bug has been fixed in the commit 826c85f3828c30b772d307c6b60ff3be744cecc2 to the
There is no need to fix the bug if you are not going to use the sed insert command!
I've made a small binary patch for the
It works for a current and a few previous Voxel's firmware versions.
The patch can be applied with the following command sequence:
I haven't found a way to apply a binary patch in the stock firmware environment without additional dependencies, so the method requires the
I've written a more advanced script to patch
busybox/sed
tool has a bug - the insert command doesn't work properly.Here is a test:
# printf "1\n3" | /bin/sed '/3/i 2' | hexdump -C
The buggy output is:
00000000 31 0a 32 01 33 |1.2.3|
And the expected output is:
00000000 31 0a 32 0a 33 |1.2.3|
The issue seems to be not critical for the stock firmware scripts because they don't use the sed insert command.
The bug has been fixed in the commit 826c85f3828c30b772d307c6b60ff3be744cecc2 to the
busybox
repo but NG firmware uses an older version without the fix.There is no need to fix the bug if you are not going to use the sed insert command!
I've made a small binary patch for the
/bin/busybox
that as I hope fixes the issue:0120a0e3081099e5 -> 0a20a0e3081099e5
It works for a current and a few previous Voxel's firmware versions.
The patch can be applied with the following command sequence:
# hexdump -ve '1/1 "%.2x"' /bin/busybox | sed 's/0120a0e3081099e5/0a20a0e3081099e5/' | sed 's/\([0-9a-f]\{1,60\}\)/\1\n/g' | xxd -r -ps > busybox
I haven't found a way to apply a binary patch in the stock firmware environment without additional dependencies, so the method requires the
xxd
tool from the Entware.I've written a more advanced script to patch
busybox
, it makes some additional checks before and after patching. The script may require some minor adaptation to your environment and then the call to the script may be added to the /mnt/optware/autorun/scripts/post-mount.sh
script to automatically patch /bin/busybox
after every firmware update.
Bash:
#!/bin/sh
BASE_PATH="/tmp/mnt/optware/config"
INIT_LOG="$BASE_PATH/init.log"
XXD_SYS="/opt/bin/xxd"
BUSYBOX_SYS="/bin/busybox"
BUSYBOX_BAK="$BASE_PATH/scripts/backup/busybox.bak"
BUSYBOX_NEW="$BASE_PATH/scripts/backup/busybox.new"
SED_SYS="/bin/sed"
SED_TEST="$BASE_PATH/scripts/backup/sed"
/bin/echo "patch-busybox.sh" >> "$INIT_LOG"
if [ ! -x "$XXD_SYS" ]; then
/bin/echo "There is no $XXD_SYS" >> "$INIT_LOG"
exit 1
fi
if [ ! -x "$BUSYBOX_SYS" ]; then
/bin/echo "There is no $BUSYBOX_SYS" >> "$INIT_LOG";
exit 1
fi
if [ ! -h "$SED_SYS" ]; then
/bin/echo "There is no $SED_SYS" >> "$INIT_LOG"
exit 1
fi
result=`/usr/bin/printf "1\n3" | "$SED_SYS" '/3/i 2' | "$XXD_SYS" -p -seek 3 -l 1`
if [ "$result" == "0a" ]; then
/bin/echo "Busybox is already patched" >> "$INIT_LOG"
exit 0
fi
if ! /bin/cp -fp "$BUSYBOX_SYS" "$BUSYBOX_BAK"; then
/bin/echo "Failed to copy $BUSYBOX_SYS to $BUSYBOX_BAK" >> "$INIT_LOG"
exit 1
fi
if ! /usr/bin/diff "$BUSYBOX_SYS" "$BUSYBOX_BAK" > /dev/null 2>&1; then
/bin/echo "$BUSYBOX_BAK is corrupted" >> "$INIT_LOG"
/bin/rm -f "$BUSYBOX_BAK"
exit 1
fi
/usr/bin/hexdump -ve '1/1 "%.2x"' $BUSYBOX_BAK | $SED_SYS 's/0120a0e3081099e5/0a20a0e3081099e5/' | $SED_SYS 's/\([0-9a-f]\{1,60\}\)/\1\n/g' | $XXD_SYS -r -ps > $BUSYBOX_NEW
if [ ! $? ] || [ ! -f "$BUSYBOX_NEW" ]; then
/bin/echo "Failed to patch $BUSYBOX_BAK" >> "$INIT_LOG"
/bin/rm -f "$BUSYBOX_NEW" "$BUSYBOX_BAK"
exit 1
fi
if ! /bin/chmod 755 "$BUSYBOX_NEW"; then
/bin/echo "Failed to set access mode to $BUSYBOX_BAK" >> "$INIT_LOG"
/bin/rm -f "$BUSYBOX_NEW" "$BUSYBOX_BAK"
exit 1
fi
if ! /bin/ln -sf "$BUSYBOX_NEW" "$SED_TEST"; then
/bin/echo "Failed to link $BUSYBOX_BAK to $SED_TEST" >> "$INIT_LOG"
/bin/rm -f "$SED_TEST" "$BUSYBOX_NEW" "$BUSYBOX_BAK"
exit 1
fi
result=`/usr/bin/printf "1\n3" | "$SED_TEST" '/3/i 2' | "$XXD_SYS" -p -seek 3 -l 1`
if [ "$result" != "0a" ]; then
/bin/echo "Busybox patch doesn't work" >> "$INIT_LOG"
/bin/rm -f "$SED_TEST" "$BUSYBOX_NEW" "$BUSYBOX_BAK"
exit 1
fi
if ! /bin/cp -fp "$BUSYBOX_NEW" "$BUSYBOX_SYS"; then
/bin/echo "Failed to copy $BUSYBOX_NEW to $BUSYBOX_SYS" >> "$INIT_LOG"
/bin/rm -f "$SED_TEST" "$BUSYBOX_NEW" "$BUSYBOX_BAK"
exit 1
fi
/bin/rm -f "$SED_TEST" "$BUSYBOX_NEW"
/bin/echo "Busybox has been successfully patched" >> "$INIT_LOG"
exit 0