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" |
Saqib Khan | 029b825 | 2017-03-22 21:38:07 -0500 | [diff] [blame^] | 57 | extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',') |
| 58 | echo "extended_version=$extended_version" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 59 | while read line; do |
| 60 | if [[ $line == "ID="* ]]; then |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 61 | # This line looks like |
| 62 | # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 63 | read -r -a fields <<< "$line" |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 64 | |
| 65 | # Get any flags attached to end (e.g. [ECC]) |
| 66 | flags="" |
| 67 | for flag in "${fields[@]:4}" |
| 68 | do |
| 69 | flags+=",${flag//[\[\]]/}" |
| 70 | done |
| 71 | |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 72 | id=${fields[0]##*=} |
| 73 | offset=$((${ffs_entry_size} * 10#${id} + ${miscflags_offset})) |
| 74 | miscflags=0x$(xxd -p -l 0x1 -seek ${offset} ${scratch_dir}/part) |
| 75 | if ((miscflags & 0x80)); then |
| 76 | flags+=",PRESERVED" |
| 77 | fi |
| 78 | if ((miscflags & 0x40)); then |
| 79 | flags+=",READONLY" |
| 80 | fi |
| 81 | |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 82 | # Need the partition ID, name, start location, end location, and flags |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 83 | echo "partition${id}=${fields[1]},${fields[2]/../,}${flags}" |
| 84 | |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 85 | # Save the partition name |
| 86 | partitions+=(${fields[1]}) |
| 87 | fi |
Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame] | 88 | # Don't need part, version, or backup_part partitions |
| 89 | done < <(pflash --info -F ${pnorfile} | grep -v -i "part\|version") |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 90 | } > ${scratch_dir}/${tocfile} |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 91 | |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 92 | for partition in "${partitions[@]}"; do |
| 93 | echo "Reading ${partition}..." |
Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 94 | pflash --partition=${partition} \ |
| 95 | --read=${scratch_dir}/${partition} \ |
| 96 | -F ${pnorfile} |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 97 | done |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 98 | |
| 99 | echo "Creating SquashFS image..." |
| 100 | |
| 101 | cd "${scratch_dir}" |
Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 102 | mksquashfs ${tocfile} ${partitions[*]} ${outfile} |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 103 | |
| 104 | echo "SquashFS Image at ${outfile}" |
| 105 | rm -r "${scratch_dir}" |