#/bin/sh # # Extract the files from MTD partition UserDef (/dev/mtdblock1) that were # assembled with collect. # # Each file consists of # # 4 bytes: file size S (little endian) # S bytes: file data # 1 byte: length L of the file name # 1 byte: '=' (ignored) # L bytes: file name # 0..3 bytes: padding (with 0) to get a 32-bit alignment for the next size # # The sequence ends if a file size of 0 is found. # # Remark: # If the size is 0xFFFFFFFF, then the script stops, too. This is typically an # empty UserDef. TEMP=/tmp/spread.tmp # Convert binary data (1-4 bytes) coming in on stdin to eight hex digits tohex() { hexdump -e \"%08x\" } # Copy part of file $TEMP to stdout # $1: Offset # $2: Length extract() { tail -c +$(($1 + 1)) $TEMP | head -c $2 } # Go to target directory cd /root echo "Restore configuration files to $(pwd)" # Copy UserDef into a file so that we can access it easier cp /dev/mtdblock1 $TEMP offset=0 count=0 # Extract the files while size=$(extract $offset 4 | tohex) [ $size != FFFFFFFF ] && [ $size != 00000000 ] do size=$((0x$size)) len=$((0x$(extract $(($offset + 4 + $size)) 1 | tohex))) fname=$(extract $(($offset + 4 + $size + 2)) $len) echo "Found '$fname' with size $size" extract $(($offset + 4)) $size > $fname offset=$(($offset + 4 + $size + 2 + $len)) offset=$((($offset + 3) & ~3)) count=$(($count + 1)) done # Remove the temporary file again rm $TEMP # Rename this script so that it is not executed again on next start. # Attention: This is only possible if the rootfs is mounted read/write. mv /etc/init.d/S01restoreconfig /etc/init.d/done-S01restoreconfig echo "Restoring configuration done, found $count file(s)"