|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +DIR="$1" |
| 4 | +NEXT_PARAM="" |
| 5 | + |
| 6 | +if [ "$1" == "-h" ] |
| 7 | +then |
| 8 | + echo "Usage: $0 [FMK directory] [-nopad | -min]" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +if [ "$DIR" == "" ] || [ "$DIR" == "-nopad" ] || [ "$DIR" == "-min" ] |
| 13 | +then |
| 14 | + DIR="fmk" |
| 15 | + NEXT_PARAM="$1" |
| 16 | +else |
| 17 | + NEXT_PARAM="$2" |
| 18 | +fi |
| 19 | + |
| 20 | +# Need to extract file systems as ROOT |
| 21 | +if [ "$UID" != "0" ] |
| 22 | +then |
| 23 | + SUDO="sudo" |
| 24 | +else |
| 25 | + SUDO="" |
| 26 | +fi |
| 27 | + |
| 28 | +# Order matters here! |
| 29 | +eval $(cat shared-ng.inc) |
| 30 | +eval $(cat $CONFLOG) |
| 31 | +FSOUT="$DIR/new-filesystem.$FS_TYPE" |
| 32 | + |
| 33 | +echo -e "Firmware Mod Kit (build-ng) $VERSION, (c)2012 Craig Heffner, Jeremy Collake\nhttp://www.bitsum.com\n" |
| 34 | + |
| 35 | +if [ ! -d "$DIR" ] |
| 36 | +then |
| 37 | + echo -e "Usage: $0 [build directory] [-nopad]\n" |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +# Check if FMK has been built, and if not, build it |
| 42 | +if [ ! -e "./src/crcalc/crcalc" ] |
| 43 | +then |
| 44 | + echo "Firmware-Mod-Kit has not been built yet. Building..." |
| 45 | + cd src && ./configure && make |
| 46 | + |
| 47 | + if [ $? -eq 0 ] |
| 48 | + then |
| 49 | + cd - |
| 50 | + else |
| 51 | + echo "Build failed! Quitting..." |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +fi |
| 55 | + |
| 56 | +echo "Building new $FS_TYPE file system..." |
| 57 | + |
| 58 | +# Clean up any previously created files |
| 59 | +rm -rf "$FWOUT" "$FSOUT" |
| 60 | + |
| 61 | +# Build the appropriate file system |
| 62 | +case $FS_TYPE in |
| 63 | + "squashfs") |
| 64 | + # Mksquashfs 4.0 tools don't support the -le option; little endian is built by default |
| 65 | + if [ "$(echo $MKFS | grep 'squashfs-4.0')" != "" ] && [ "$ENDIANESS" == "-le" ] |
| 66 | + then |
| 67 | + ENDIANESS="" |
| 68 | + fi |
| 69 | + |
| 70 | + # Increasing the block size minimizes the resulting image size. Max block size of 1MB. |
| 71 | + if [ "$NEXT_PARAM" == "-min" ] |
| 72 | + then |
| 73 | + BS="-b $((1024*1024))" |
| 74 | + else |
| 75 | + BS="" |
| 76 | + fi |
| 77 | + |
| 78 | + # Check for squashfs 4.0 realtek, which requires the -comp option to build lzma images. |
| 79 | + if [ "$(echo $MKFS | grep 'squashfs-4.0-realtek')" != "" ] && [ "$FS_COMPRESSION" == "lzma" ] |
| 80 | + then |
| 81 | + COMP="-comp lzma" |
| 82 | + else |
| 83 | + COMP="" |
| 84 | + fi |
| 85 | + |
| 86 | + $SUDO $MKFS "$ROOTFS" "$FSOUT" $ENDIANESS $BS $COMP -all-root |
| 87 | + ;; |
| 88 | + "cramfs") |
| 89 | + $SUDO $MKFS "$ROOTFS" "$FSOUT" |
| 90 | + if [ "$ENDIANESS" == "-be" ] |
| 91 | + then |
| 92 | + mv "$FSOUT" "$FSOUT.le" |
| 93 | + ./src/cramfsswap/cramfsswap "$FSOUT.le" "$FSOUT" |
| 94 | + rm -f "$FSOUT.le" |
| 95 | + fi |
| 96 | + ;; |
| 97 | + *) |
| 98 | + echo "Unsupported file system '$FS_TYPE'!" |
| 99 | + ;; |
| 100 | +esac |
| 101 | + |
| 102 | +if [ ! -e $FSOUT ] |
| 103 | +then |
| 104 | + echo "Failed to create new file system! Quitting..." |
| 105 | + exit 1 |
| 106 | +fi |
| 107 | + |
| 108 | +# Append the new file system to the first part of the original firmware file |
| 109 | +cp $HEADER_IMAGE $FWOUT |
| 110 | +$SUDO cat $FSOUT >> $FWOUT |
| 111 | + |
| 112 | +# Calculate and create any filler bytes required between the end of the file system and the footer / EOF. |
| 113 | +CUR_SIZE=$(ls -l $FWOUT | awk '{print $5}') |
| 114 | +((FILLER_SIZE=$FW_SIZE-$CUR_SIZE-$FOOTER_SIZE)) |
| 115 | + |
| 116 | +if [ "$FILLER_SIZE" -lt 0 ] |
| 117 | +then |
| 118 | + echo "ERROR: New firmware image will be larger than original image!" |
| 119 | + echo " Building firmware images larger than the original can brick your device!" |
| 120 | + echo " Try re-running with the -min option, or remove any unnecessary files from the file system." |
| 121 | + echo " Refusing to create new firmware image." |
| 122 | + echo "" |
| 123 | + echo " Original file size: $FW_SIZE" |
| 124 | + echo " Current file size: $CUR_SIZE" |
| 125 | + echo "" |
| 126 | + echo " Quitting..." |
| 127 | + rm -f "$FWOUT" "$FSOUT" |
| 128 | + exit 1 |
| 129 | +else |
| 130 | + if [ "$NEXT_PARAM" != "-nopad" ]; then |
| 131 | + echo "Remaining free bytes in firmware image: $FILLER_SIZE" |
| 132 | + perl -e "print \"\xFF\"x$FILLER_SIZE" >> "$FWOUT" |
| 133 | + else |
| 134 | + echo "Padding of firmware image disabled via -nopad" |
| 135 | + fi |
| 136 | +fi |
| 137 | + |
| 138 | +# Append the footer to the new firmware image, if there is any footer |
| 139 | +if [ "$FOOTER_SIZE" -gt "0" ] |
| 140 | +then |
| 141 | + cat $FOOTER_IMAGE >> "$FWOUT" |
| 142 | +fi |
| 143 | + |
| 144 | +# Calculate new checksum values for the firmware header |
| 145 | +./src/crcalc/crcalc "$FWOUT" "$BINLOG" |
| 146 | + |
| 147 | +if [ $? -eq 0 ] |
| 148 | +then |
| 149 | + echo -n "Finished! " |
| 150 | +else |
| 151 | + echo -n "Firmware header not supported; firmware checksums may be incorrect. " |
| 152 | +fi |
| 153 | + |
| 154 | +echo "New firmware image has been saved to: $FWOUT" |
0 commit comments