blob: f9d9d6d2d78629522bfd7c6bd074a7397823f824 [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 Mills37751f92017-02-07 21:05:01 -060015outfile=`pwd`"/pnor.xz.squashfs"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060016declare -a partitions=()
17tocfile="pnor.toc"
Gunnar Mills37751f92017-02-07 21:05:01 -060018
Gunnar Millse8221902017-02-03 15:59:06 -060019while [[ $# -gt 0 ]]; do
20 key="$1"
21 case $key in
Gunnar Mills37751f92017-02-07 21:05:01 -060022 -f|--file)
23 outfile="$2"
24 shift 2
25 ;;
Gunnar Millse8221902017-02-03 15:59:06 -060026 -h|--help)
27 echo "$help"
28 exit
29 ;;
30 *)
Gunnar Millsc15b02d2017-03-03 10:28:37 -060031 pnorfile="$1"
32 shift 1
Gunnar Millse8221902017-02-03 15:59:06 -060033 ;;
34 esac
35done
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060036
Gunnar Millsc15b02d2017-03-03 10:28:37 -060037if [ ! -f "${pnorfile}" ]; then
38 echo "Please enter a valid PNOR file."
39 echo "$help"
40 exit 1
41fi
42
Gunnar Mills37751f92017-02-07 21:05:01 -060043scratch_dir=`mktemp -d` || exit 1
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060044
45echo "Parsing PNOR TOC..."
46{
47 while read line; do
48 if [[ $line == "ID="* ]]; then
Gunnar Millse0ed3022017-03-02 16:16:19 -060049 # This line looks like
50 # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060051 read -r -a fields <<< "$line"
Gunnar Millse0ed3022017-03-02 16:16:19 -060052
53 # Get any flags attached to end (e.g. [ECC])
54 flags=""
55 for flag in "${fields[@]:4}"
56 do
57 flags+=",${flag//[\[\]]/}"
58 done
59
60 # Need the partition ID, name, start location, end location, and flags
61 echo "partition${fields[0]##*=}=${fields[1]},${fields[2]/../,}${flags}"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060062 # Save the partition name
63 partitions+=(${fields[1]})
64 fi
Gunnar Millsc15b02d2017-03-03 10:28:37 -060065 done < <(pflash --info -F ${pnorfile})
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060066} > ${scratch_dir}/${tocfile}
Gunnar Mills37751f92017-02-07 21:05:01 -060067
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060068for partition in "${partitions[@]}"; do
69 echo "Reading ${partition}..."
Gunnar Millsc15b02d2017-03-03 10:28:37 -060070 pflash_cmd="pflash --partition=${partition} --read=${scratch_dir}/${partition}
71 -F ${pnorfile}"
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060072 ${pflash_cmd} || exit 1
73done
Gunnar Mills37751f92017-02-07 21:05:01 -060074
75echo "Creating SquashFS image..."
76
77cd "${scratch_dir}"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060078squashfs_cmd="mksquashfs ${tocfile} ${partitions[*]} ${outfile}"
Gunnar Mills37751f92017-02-07 21:05:01 -060079${squashfs_cmd} || exit 1
80
81echo "SquashFS Image at ${outfile}"
82rm -r "${scratch_dir}"