blob: c23d5fdb5da3e0c2e4bded6f308ab98ec4c86d94 [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"
Saqib Khan029b8252017-03-22 21:38:07 -050057 extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',')
58 echo "extended_version=$extended_version"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060059 while read line; do
60 if [[ $line == "ID="* ]]; then
Gunnar Millse0ed3022017-03-02 16:16:19 -060061 # This line looks like
62 # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060063 read -r -a fields <<< "$line"
Gunnar Millse0ed3022017-03-02 16:16:19 -060064
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 Millsd4908a42017-03-07 08:25:59 -060072 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 Millse0ed3022017-03-02 16:16:19 -060082 # Need the partition ID, name, start location, end location, and flags
Gunnar Millsd4908a42017-03-07 08:25:59 -060083 echo "partition${id}=${fields[1]},${fields[2]/../,}${flags}"
84
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060085 # Save the partition name
86 partitions+=(${fields[1]})
87 fi
Gunnar Mills2f27b742017-03-20 13:46:30 -050088 # Don't need part, version, or backup_part partitions
89 done < <(pflash --info -F ${pnorfile} | grep -v -i "part\|version")
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060090} > ${scratch_dir}/${tocfile}
Gunnar Mills37751f92017-02-07 21:05:01 -060091
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060092for partition in "${partitions[@]}"; do
93 echo "Reading ${partition}..."
Gunnar Millsf8f11302017-03-08 13:11:33 -060094 pflash --partition=${partition} \
95 --read=${scratch_dir}/${partition} \
96 -F ${pnorfile}
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060097done
Gunnar Mills37751f92017-02-07 21:05:01 -060098
99echo "Creating SquashFS image..."
100
101cd "${scratch_dir}"
Gunnar Millsf8f11302017-03-08 13:11:33 -0600102mksquashfs ${tocfile} ${partitions[*]} ${outfile}
Gunnar Mills37751f92017-02-07 21:05:01 -0600103
104echo "SquashFS Image at ${outfile}"
105rm -r "${scratch_dir}"