blob: 4c5329040f7c31610aa9aee72324b1aef9221b74 [file] [log] [blame]
Gunnar Mills6b5b27e2017-03-01 21:37:01 -06001#!/bin/bash
Gunnar Millsf8f11302017-03-08 13:11:33 -06002set -eo pipefail
Gunnar Millse8221902017-02-03 15:59:06 -06003
4help=$'Generate SquashFS image Script
5
6Generates a SquashFS image from the PNOR image
7
Gunnar Millsc15b02d2017-03-03 10:28:37 -06008usage: generate-squashfs [OPTION] <PNOR FILE>...
Gunnar Millse8221902017-02-03 15:59:06 -06009
10Options:
Gunnar Mills37751f92017-02-07 21:05:01 -060011 -f, --file <file> Specify destination file. Defaults to
12 `pwd`/pnor.xz.squashfs if unspecified.
Gunnar Millse8221902017-02-03 15:59:06 -060013 -h, --help Display this help text and exit.
14'
15
Gunnar Millsd4908a42017-03-07 08:25:59 -060016let ffs_entry_size=128
17let miscflags_offset=112
Gunnar Mills37751f92017-02-07 21:05:01 -060018outfile=`pwd`"/pnor.xz.squashfs"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060019declare -a partitions=()
20tocfile="pnor.toc"
Gunnar Mills37751f92017-02-07 21:05:01 -060021
Gunnar Millse8221902017-02-03 15:59:06 -060022while [[ $# -gt 0 ]]; do
23 key="$1"
24 case $key in
Gunnar Mills37751f92017-02-07 21:05:01 -060025 -f|--file)
26 outfile="$2"
27 shift 2
28 ;;
Gunnar Millse8221902017-02-03 15:59:06 -060029 -h|--help)
30 echo "$help"
31 exit
32 ;;
33 *)
Gunnar Millsc15b02d2017-03-03 10:28:37 -060034 pnorfile="$1"
35 shift 1
Gunnar Millse8221902017-02-03 15:59:06 -060036 ;;
37 esac
38done
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060039
Gunnar Millsc15b02d2017-03-03 10:28:37 -060040if [ ! -f "${pnorfile}" ]; then
41 echo "Please enter a valid PNOR file."
42 echo "$help"
43 exit 1
44fi
45
Gunnar Millsf8f11302017-03-08 13:11:33 -060046scratch_dir=`mktemp -d`
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060047
48echo "Parsing PNOR TOC..."
Gunnar Millsd4908a42017-03-07 08:25:59 -060049
50# Needed to get the READONLY and PRESERVED flags
51# TODO: Get READONLY and PRESERVED flags from pflash instead.
Gunnar Millsf8f11302017-03-08 13:11:33 -060052pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile}
Gunnar Mills2f27b742017-03-20 13:46:30 -050053pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile}
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060054{
Gunnar Mills2f27b742017-03-20 13:46:30 -050055 version=$(head -n 1 ${scratch_dir}/VERSION)
56 echo "version=$version"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060057 while read line; do
58 if [[ $line == "ID="* ]]; then
Gunnar Millse0ed3022017-03-02 16:16:19 -060059 # This line looks like
60 # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060061 read -r -a fields <<< "$line"
Gunnar Millse0ed3022017-03-02 16:16:19 -060062
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 Millsd4908a42017-03-07 08:25:59 -060070 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 Millse0ed3022017-03-02 16:16:19 -060080 # Need the partition ID, name, start location, end location, and flags
Gunnar Millsd4908a42017-03-07 08:25:59 -060081 echo "partition${id}=${fields[1]},${fields[2]/../,}${flags}"
82
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060083 # Save the partition name
84 partitions+=(${fields[1]})
85 fi
Gunnar Mills2f27b742017-03-20 13:46:30 -050086 # Don't need part, version, or backup_part partitions
87 done < <(pflash --info -F ${pnorfile} | grep -v -i "part\|version")
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060088} > ${scratch_dir}/${tocfile}
Gunnar Mills37751f92017-02-07 21:05:01 -060089
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060090for partition in "${partitions[@]}"; do
91 echo "Reading ${partition}..."
Gunnar Millsf8f11302017-03-08 13:11:33 -060092 pflash --partition=${partition} \
93 --read=${scratch_dir}/${partition} \
94 -F ${pnorfile}
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060095done
Gunnar Mills37751f92017-02-07 21:05:01 -060096
97echo "Creating SquashFS image..."
98
99cd "${scratch_dir}"
Gunnar Millsf8f11302017-03-08 13:11:33 -0600100mksquashfs ${tocfile} ${partitions[*]} ${outfile}
Gunnar Mills37751f92017-02-07 21:05:01 -0600101
102echo "SquashFS Image at ${outfile}"
103rm -r "${scratch_dir}"