blob: 14515e4cf20c1b9490bdd98f83f9d97c576a35ba [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 Millsd4908a42017-03-07 08:25:59 -060053
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060054{
55 while read line; do
56 if [[ $line == "ID="* ]]; then
Gunnar Millse0ed3022017-03-02 16:16:19 -060057 # This line looks like
58 # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060059 read -r -a fields <<< "$line"
Gunnar Millse0ed3022017-03-02 16:16:19 -060060
61 # Get any flags attached to end (e.g. [ECC])
62 flags=""
63 for flag in "${fields[@]:4}"
64 do
65 flags+=",${flag//[\[\]]/}"
66 done
67
Gunnar Millsd4908a42017-03-07 08:25:59 -060068 id=${fields[0]##*=}
69 offset=$((${ffs_entry_size} * 10#${id} + ${miscflags_offset}))
70 miscflags=0x$(xxd -p -l 0x1 -seek ${offset} ${scratch_dir}/part)
71 if ((miscflags & 0x80)); then
72 flags+=",PRESERVED"
73 fi
74 if ((miscflags & 0x40)); then
75 flags+=",READONLY"
76 fi
77
Gunnar Millse0ed3022017-03-02 16:16:19 -060078 # Need the partition ID, name, start location, end location, and flags
Gunnar Millsd4908a42017-03-07 08:25:59 -060079 echo "partition${id}=${fields[1]},${fields[2]/../,}${flags}"
80
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060081 # Save the partition name
82 partitions+=(${fields[1]})
83 fi
Gunnar Millsd4908a42017-03-07 08:25:59 -060084 done < <(pflash --info -F ${pnorfile} | grep -v -i "part")
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060085} > ${scratch_dir}/${tocfile}
Gunnar Mills37751f92017-02-07 21:05:01 -060086
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060087for partition in "${partitions[@]}"; do
88 echo "Reading ${partition}..."
Gunnar Millsf8f11302017-03-08 13:11:33 -060089 pflash --partition=${partition} \
90 --read=${scratch_dir}/${partition} \
91 -F ${pnorfile}
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060092done
Gunnar Mills37751f92017-02-07 21:05:01 -060093
94echo "Creating SquashFS image..."
95
96cd "${scratch_dir}"
Gunnar Millsf8f11302017-03-08 13:11:33 -060097mksquashfs ${tocfile} ${partitions[*]} ${outfile}
Gunnar Mills37751f92017-02-07 21:05:01 -060098
99echo "SquashFS Image at ${outfile}"
100rm -r "${scratch_dir}"