blob: 4a82b405b68e73ee6d0f3502d09392d2edd55136 [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
Saqib Khan93433f02017-03-23 22:24:27 -05004help=$'Generate Tarball with SquashFS image and MANIFEST Script
Gunnar Millse8221902017-02-03 15:59:06 -06005
6Generates a SquashFS image from the PNOR image
Saqib Khan93433f02017-03-23 22:24:27 -05007Creates a MANIFEST for image verification
8Packages the SquashFS image and MANIFEST together in a tarball
Gunnar Millse8221902017-02-03 15:59:06 -06009
Gunnar Millsc15b02d2017-03-03 10:28:37 -060010usage: generate-squashfs [OPTION] <PNOR FILE>...
Gunnar Millse8221902017-02-03 15:59:06 -060011
12Options:
Gunnar Mills37751f92017-02-07 21:05:01 -060013 -f, --file <file> Specify destination file. Defaults to
Saqib Khan93433f02017-03-23 22:24:27 -050014 `pwd`/pnor.squashfs.tar if unspecified.
Gunnar Millse8221902017-02-03 15:59:06 -060015 -h, --help Display this help text and exit.
16'
17
Gunnar Millsd4908a42017-03-07 08:25:59 -060018let ffs_entry_size=128
19let miscflags_offset=112
Saqib Khan93433f02017-03-23 22:24:27 -050020outfile=`pwd`"/pnor.squashfs.tar"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060021declare -a partitions=()
22tocfile="pnor.toc"
Gunnar Mills37751f92017-02-07 21:05:01 -060023
Gunnar Millse8221902017-02-03 15:59:06 -060024while [[ $# -gt 0 ]]; do
25 key="$1"
26 case $key in
Gunnar Mills37751f92017-02-07 21:05:01 -060027 -f|--file)
28 outfile="$2"
29 shift 2
30 ;;
Gunnar Millse8221902017-02-03 15:59:06 -060031 -h|--help)
32 echo "$help"
33 exit
34 ;;
35 *)
Gunnar Millsc15b02d2017-03-03 10:28:37 -060036 pnorfile="$1"
37 shift 1
Gunnar Millse8221902017-02-03 15:59:06 -060038 ;;
39 esac
40done
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060041
Gunnar Millsc15b02d2017-03-03 10:28:37 -060042if [ ! -f "${pnorfile}" ]; then
43 echo "Please enter a valid PNOR file."
44 echo "$help"
45 exit 1
46fi
47
Gunnar Millsf8f11302017-03-08 13:11:33 -060048scratch_dir=`mktemp -d`
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060049
50echo "Parsing PNOR TOC..."
Gunnar Millsd4908a42017-03-07 08:25:59 -060051
52# Needed to get the READONLY and PRESERVED flags
53# TODO: Get READONLY and PRESERVED flags from pflash instead.
Gunnar Millsf8f11302017-03-08 13:11:33 -060054pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile}
Gunnar Mills2f27b742017-03-20 13:46:30 -050055pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile}
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060056{
Gunnar Mills2f27b742017-03-20 13:46:30 -050057 version=$(head -n 1 ${scratch_dir}/VERSION)
58 echo "version=$version"
Saqib Khan029b8252017-03-22 21:38:07 -050059 extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',')
60 echo "extended_version=$extended_version"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060061 while read line; do
62 if [[ $line == "ID="* ]]; then
Gunnar Millse0ed3022017-03-02 16:16:19 -060063 # This line looks like
64 # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060065 read -r -a fields <<< "$line"
Gunnar Millse0ed3022017-03-02 16:16:19 -060066
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 Millsd4908a42017-03-07 08:25:59 -060074 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"
Adriana Kobylak9c866062017-04-13 16:12:32 -050079 elif ((miscflags & 0x40)); then
Gunnar Millsd4908a42017-03-07 08:25:59 -060080 flags+=",READONLY"
Adriana Kobylak9c866062017-04-13 16:12:32 -050081 else
82 flags+=",READWRITE"
Gunnar Millsd4908a42017-03-07 08:25:59 -060083 fi
84
Gunnar Millse0ed3022017-03-02 16:16:19 -060085 # Need the partition ID, name, start location, end location, and flags
Gunnar Millsd4908a42017-03-07 08:25:59 -060086 echo "partition${id}=${fields[1]},${fields[2]/../,}${flags}"
87
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060088 # Save the partition name
89 partitions+=(${fields[1]})
90 fi
Gunnar Mills2f27b742017-03-20 13:46:30 -050091 # Don't need part, version, or backup_part partitions
92 done < <(pflash --info -F ${pnorfile} | grep -v -i "part\|version")
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060093} > ${scratch_dir}/${tocfile}
Gunnar Mills37751f92017-02-07 21:05:01 -060094
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060095for partition in "${partitions[@]}"; do
96 echo "Reading ${partition}..."
Gunnar Millsf8f11302017-03-08 13:11:33 -060097 pflash --partition=${partition} \
98 --read=${scratch_dir}/${partition} \
99 -F ${pnorfile}
Gunnar Mills18f7cdb2017-02-07 16:44:19 -0600100done
Gunnar Mills37751f92017-02-07 21:05:01 -0600101
102echo "Creating SquashFS image..."
Gunnar Mills37751f92017-02-07 21:05:01 -0600103cd "${scratch_dir}"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500104mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs
Gunnar Mills37751f92017-02-07 21:05:01 -0600105
Saqib Khan93433f02017-03-23 22:24:27 -0500106echo "Creating MANIFEST for the image"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500107manifest_location="MANIFEST"
Saqib Khan7254f0e2017-04-10 21:45:37 -0500108echo -e "purpose=host\nversion=$version\n\
109extended_version=$extended_version" >> $manifest_location
Saqib Khan93433f02017-03-23 22:24:27 -0500110
111echo "Generating tarball to contain the SquashFS image and its MANIFEST"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500112tar -cvf $outfile $manifest_location pnor.xz.squashfs
Saqib Khan93433f02017-03-23 22:24:27 -0500113
114echo "SquashFSTarball at ${outfile}"
Gunnar Mills37751f92017-02-07 21:05:01 -0600115rm -r "${scratch_dir}"