#!/bin/bash # Remove junk bytes from AVM image files (http://freetz.org/ticket/1614) # Author: Alexander Kriegisch (http://scrum-master.de) INPUT_FILE="$1" OUTPUT_FILE="$2" SFK_TOOL="$(dirname "$0")/sfk" HEADER_MAGIC_HEX="3C1A8000375A038003400008" INTRA_MAGIC_TXT="NMI Boot" INTRA_MAGIC_HEX="4E4D4920 426F6F74" if [ $# -ne 2 -o ! -e "$INPUT_FILE" ]; then echo "Usage: ${0##*/} input.image output.image" exit 2 fi hexdumps="$("$SFK_TOOL" hexfind "$INPUT_FILE" -bin /$HEADER_MAGIC_HEX/ -quiet -context=32)" offsets=($(echo "$hexdumps" | sed -nr 's/.*hit at offset 0x(.*)/\1/p')) count=${#offsets[@]} case $count in 0) echo "No junk header found" >&2; exit 1 ;; 1) echo "Junk header found, inspecting ..." ;; *) echo "Multiple junk headers found, inspecting ..." ;; esac for (( i=0; i&2 continue ;; esac echo "Junk header found at V$junkver offset 0x$offs" if [ "$img_size_junk" != "$img_size_file" ]; then echo "Image size encoded in junk block does not match real file size" continue fi echo -n "Image size encoded in junk block matches real file size, removing junk bytes ... " head -c $((0x$offs)) "$INPUT_FILE" > "$OUTPUT_FILE" tail -c $((0x${img_size_file} - 0x$offs + 8)) "$INPUT_FILE" >> "$OUTPUT_FILE" echo "done." exit 0 done echo "No junk bytes found" >&2 exit 1