blob: 0eff0ce579a5e9f47171ac87615c417f7e9df25f [file] [log] [blame]
Gunnar Mills6b5b27e2017-03-01 21:37:01 -06001#!/bin/bash
Gunnar Millse8221902017-02-03 15:59:06 -06002
3help=$'Generate SquashFS image Script
4
5Generates a SquashFS image from the PNOR image
6
Gunnar Millsc15b02d2017-03-03 10:28:37 -06007usage: generate-squashfs [OPTION] <PNOR FILE>...
Gunnar Millse8221902017-02-03 15:59:06 -06008
9Options:
Gunnar Mills37751f92017-02-07 21:05:01 -060010 -f, --file <file> Specify destination file. Defaults to
11 `pwd`/pnor.xz.squashfs if unspecified.
Gunnar Millse8221902017-02-03 15:59:06 -060012 -h, --help Display this help text and exit.
13'
14
Gunnar Millsd4908a42017-03-07 08:25:59 -060015let ffs_entry_size=128
16let miscflags_offset=112
Gunnar Mills37751f92017-02-07 21:05:01 -060017outfile=`pwd`"/pnor.xz.squashfs"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060018declare -a partitions=()
19tocfile="pnor.toc"
Gunnar Mills37751f92017-02-07 21:05:01 -060020
Gunnar Millse8221902017-02-03 15:59:06 -060021while [[ $# -gt 0 ]]; do
22 key="$1"
23 case $key in
Gunnar Mills37751f92017-02-07 21:05:01 -060024 -f|--file)
25 outfile="$2"
26 shift 2
27 ;;
Gunnar Millse8221902017-02-03 15:59:06 -060028 -h|--help)
29 echo "$help"
30 exit
31 ;;
32 *)
Gunnar Millsc15b02d2017-03-03 10:28:37 -060033 pnorfile="$1"
34 shift 1
Gunnar Millse8221902017-02-03 15:59:06 -060035 ;;
36 esac
37done
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060038
Gunnar Millsc15b02d2017-03-03 10:28:37 -060039if [ ! -f "${pnorfile}" ]; then
40 echo "Please enter a valid PNOR file."
41 echo "$help"
42 exit 1
43fi
44
Gunnar Mills37751f92017-02-07 21:05:01 -060045scratch_dir=`mktemp -d` || exit 1
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060046
47echo "Parsing PNOR TOC..."
Gunnar Millsd4908a42017-03-07 08:25:59 -060048
49# Needed to get the READONLY and PRESERVED flags
50# TODO: Get READONLY and PRESERVED flags from pflash instead.
51pflash_cmd="pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile}"
52${pflash_cmd} || exit 1
53
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 Millsc15b02d2017-03-03 10:28:37 -060089 pflash_cmd="pflash --partition=${partition} --read=${scratch_dir}/${partition}
90 -F ${pnorfile}"
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060091 ${pflash_cmd} || exit 1
92done
Gunnar Mills37751f92017-02-07 21:05:01 -060093
94echo "Creating SquashFS image..."
95
96cd "${scratch_dir}"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060097squashfs_cmd="mksquashfs ${tocfile} ${partitions[*]} ${outfile}"
Gunnar Mills37751f92017-02-07 21:05:01 -060098${squashfs_cmd} || exit 1
99
100echo "SquashFS Image at ${outfile}"
101rm -r "${scratch_dir}"