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 | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 15 | outfile=`pwd`"/pnor.xz.squashfs" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 16 | declare -a partitions=() |
| 17 | tocfile="pnor.toc" |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 18 | |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 19 | while [[ $# -gt 0 ]]; do |
| 20 | key="$1" |
| 21 | case $key in |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 22 | -f|--file) |
| 23 | outfile="$2" |
| 24 | shift 2 |
| 25 | ;; |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 26 | -h|--help) |
| 27 | echo "$help" |
| 28 | exit |
| 29 | ;; |
| 30 | *) |
Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame^] | 31 | pnorfile="$1" |
| 32 | shift 1 |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 33 | ;; |
| 34 | esac |
| 35 | done |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 36 | |
Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame^] | 37 | if [ ! -f "${pnorfile}" ]; then |
| 38 | echo "Please enter a valid PNOR file." |
| 39 | echo "$help" |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 43 | scratch_dir=`mktemp -d` || exit 1 |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 44 | |
| 45 | echo "Parsing PNOR TOC..." |
| 46 | { |
| 47 | while read line; do |
| 48 | if [[ $line == "ID="* ]]; then |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 49 | # This line looks like |
| 50 | # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 51 | read -r -a fields <<< "$line" |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 52 | |
| 53 | # Get any flags attached to end (e.g. [ECC]) |
| 54 | flags="" |
| 55 | for flag in "${fields[@]:4}" |
| 56 | do |
| 57 | flags+=",${flag//[\[\]]/}" |
| 58 | done |
| 59 | |
| 60 | # Need the partition ID, name, start location, end location, and flags |
| 61 | echo "partition${fields[0]##*=}=${fields[1]},${fields[2]/../,}${flags}" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 62 | # Save the partition name |
| 63 | partitions+=(${fields[1]}) |
| 64 | fi |
Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame^] | 65 | done < <(pflash --info -F ${pnorfile}) |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 66 | } > ${scratch_dir}/${tocfile} |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 67 | |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 68 | for partition in "${partitions[@]}"; do |
| 69 | echo "Reading ${partition}..." |
Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame^] | 70 | pflash_cmd="pflash --partition=${partition} --read=${scratch_dir}/${partition} |
| 71 | -F ${pnorfile}" |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 72 | ${pflash_cmd} || exit 1 |
| 73 | done |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 74 | |
| 75 | echo "Creating SquashFS image..." |
| 76 | |
| 77 | cd "${scratch_dir}" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 78 | squashfs_cmd="mksquashfs ${tocfile} ${partitions[*]} ${outfile}" |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 79 | ${squashfs_cmd} || exit 1 |
| 80 | |
| 81 | echo "SquashFS Image at ${outfile}" |
| 82 | rm -r "${scratch_dir}" |