Skip to content

Instantly share code, notes, and snippets.

@xiongyihui
Created December 10, 2015 10:01
Show Gist options
  • Save xiongyihui/b027847798f12d28b7b0 to your computer and use it in GitHub Desktop.
Save xiongyihui/b027847798f12d28b7b0 to your computer and use it in GitHub Desktop.
fix vector checksum value for NXP LPC1xxx Microcontroller
#!/bin/bash
#
# fix vector checksum value for NXP LPc1xxx
#
if [ $# != 1 ]; then
echo "Usage: $0 firmware.bin"
exit 1
fi
bin="$1"
if [ ! -f "$bin" ]; then
echo "Cannot open $bin"
exit 2
fi
# check if the sum of new firmware's first 8 word(4 bytes) is zero
od -N 32 -t u4 -v -w32 "$bin" > /tmp/lpc1xxx
read addr u1 u2 u3 u4 u5 u6 u7 u8 < /tmp/lpc1xxx
sum=$(($u1 + $u2 + $u3 + $u4 + $u5 + $u6 + $u7))
cks=$(((~$sum + 1) & 0xFFFFFFFF))
if [ $u8 != $cks ]; then
echo "fix the checksum of the new firmware"
# for big endian
# printf "0: %0.8X" $cks | xxd -r -g0 > /tmp/lpc1xxx
# for little endian
printf "0: %.8X" $cks | sed -E 's/0: (..)(..)(..)(..)/0: \4\3\2\1/' | xxd -r -g0 > /tmp/lpc1xxx
cp "$bin" /tmp/lpc1xxx.bin
dd bs=4 count=1 seek=7 conv=notrunc if=/tmp/lpc1xxx of=$bin
xxd -l 128 $bin
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment