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