| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 1 | #!/bin/bash | 
| Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 2 | set -eo pipefail | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 3 |  | 
 | 4 | help=$'Generate SquashFS image Script | 
 | 5 |  | 
 | 6 | Generates a SquashFS image from the PNOR image | 
 | 7 |  | 
| Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 8 | usage: generate-squashfs [OPTION] <PNOR FILE>... | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 9 |  | 
 | 10 | Options: | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 11 |    -f, --file <file>      Specify destination file. Defaults to | 
 | 12 |                           `pwd`/pnor.xz.squashfs if unspecified. | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 13 |    -h, --help             Display this help text and exit. | 
 | 14 | ' | 
 | 15 |  | 
| Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 16 | let ffs_entry_size=128 | 
 | 17 | let miscflags_offset=112 | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 18 | outfile=`pwd`"/pnor.xz.squashfs" | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 19 | declare -a partitions=() | 
 | 20 | tocfile="pnor.toc" | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 21 |  | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 22 | while [[ $# -gt 0 ]]; do | 
 | 23 |   key="$1" | 
 | 24 |   case $key in | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 25 |     -f|--file) | 
 | 26 |       outfile="$2" | 
 | 27 |       shift 2 | 
 | 28 |       ;; | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 29 |     -h|--help) | 
 | 30 |       echo "$help" | 
 | 31 |       exit | 
 | 32 |       ;; | 
 | 33 |     *) | 
| Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 34 |       pnorfile="$1" | 
 | 35 |       shift 1 | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 36 |       ;; | 
 | 37 |   esac | 
 | 38 | done | 
| Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 39 |  | 
| Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 40 | if [ ! -f "${pnorfile}" ]; then | 
 | 41 |   echo "Please enter a valid PNOR file." | 
 | 42 |   echo "$help" | 
 | 43 |   exit 1 | 
 | 44 | fi | 
 | 45 |  | 
| Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 46 | scratch_dir=`mktemp -d` | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 47 |  | 
 | 48 | echo "Parsing PNOR TOC..." | 
| Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 49 |  | 
 | 50 | # Needed to get the READONLY and PRESERVED flags | 
 | 51 | # TODO: Get READONLY and PRESERVED flags from pflash instead. | 
| Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 52 | pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile} | 
| Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame^] | 53 | pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile} | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 54 | { | 
| Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame^] | 55 |   version=$(head -n 1 ${scratch_dir}/VERSION) | 
 | 56 |   echo "version=$version" | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 57 |   while read line; do | 
 | 58 |     if [[ $line == "ID="* ]]; then | 
| Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 59 |         # This line looks like | 
 | 60 |         # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]" | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 61 |         read -r -a fields <<< "$line" | 
| Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 62 |  | 
 | 63 |         # Get any flags attached to end (e.g. [ECC]) | 
 | 64 |         flags="" | 
 | 65 |         for flag in "${fields[@]:4}" | 
 | 66 |         do | 
 | 67 |           flags+=",${flag//[\[\]]/}" | 
 | 68 |         done | 
 | 69 |  | 
| Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 70 |         id=${fields[0]##*=} | 
 | 71 |         offset=$((${ffs_entry_size} * 10#${id} + ${miscflags_offset})) | 
 | 72 |         miscflags=0x$(xxd -p -l  0x1 -seek ${offset} ${scratch_dir}/part) | 
 | 73 |         if ((miscflags & 0x80)); then | 
 | 74 |           flags+=",PRESERVED" | 
 | 75 |         fi | 
 | 76 |         if ((miscflags & 0x40)); then | 
 | 77 |           flags+=",READONLY" | 
 | 78 |         fi | 
 | 79 |  | 
| Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 80 |         # Need the partition ID, name, start location, end location, and flags | 
| Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 81 |         echo  "partition${id}=${fields[1]},${fields[2]/../,}${flags}" | 
 | 82 |  | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 83 |         # Save the partition name | 
 | 84 |         partitions+=(${fields[1]}) | 
 | 85 |     fi | 
| Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame^] | 86 |   # Don't need part, version, or backup_part partitions | 
 | 87 |   done < <(pflash --info -F ${pnorfile} | grep -v -i "part\|version") | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 88 | } > ${scratch_dir}/${tocfile} | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 89 |  | 
| Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 90 | for partition in "${partitions[@]}"; do | 
 | 91 |   echo "Reading ${partition}..." | 
| Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 92 |   pflash --partition=${partition} \ | 
 | 93 |     --read=${scratch_dir}/${partition} \ | 
 | 94 |     -F ${pnorfile} | 
| Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 95 | done | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 96 |  | 
 | 97 | echo "Creating SquashFS image..." | 
 | 98 |  | 
 | 99 | cd "${scratch_dir}" | 
| Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 100 | mksquashfs ${tocfile} ${partitions[*]} ${outfile} | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 101 |  | 
 | 102 | echo "SquashFS Image at ${outfile}" | 
 | 103 | rm -r "${scratch_dir}" |