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