blob: 41f369db59a9f372b922f6efe47bbf5c66d9802e [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
Michael Tritz8f396732017-06-09 10:55:35 -050014 `pwd`/<PNOR FILE>.pnor.squashfs.tar if unspecified.
15 (For example, "generate-squashfs my.pnor" would
16 generate `pwd`/my.pnor.squashfs.tar output.)
Gunnar Millse8221902017-02-03 15:59:06 -060017 -h, --help Display this help text and exit.
18'
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -050019# Reference the ffs structures at:
20# https://github.com/open-power/hostboot/blob/master/src/usr/pnor/common/ffs_hb.H
21# https://github.com/open-power/hostboot/blob/master/src/usr/pnor/ffs.h
Gunnar Millsd4908a42017-03-07 08:25:59 -060022let ffs_entry_size=128
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -050023let vercheck_offset=112
24let miscflags_offset=113
Michael Tritz8f396732017-06-09 10:55:35 -050025outfile=""
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060026declare -a partitions=()
27tocfile="pnor.toc"
Gunnar Mills37751f92017-02-07 21:05:01 -060028
Gunnar Millse8221902017-02-03 15:59:06 -060029while [[ $# -gt 0 ]]; do
30 key="$1"
31 case $key in
Gunnar Mills37751f92017-02-07 21:05:01 -060032 -f|--file)
33 outfile="$2"
34 shift 2
35 ;;
Gunnar Millse8221902017-02-03 15:59:06 -060036 -h|--help)
37 echo "$help"
38 exit
39 ;;
40 *)
Gunnar Millsc15b02d2017-03-03 10:28:37 -060041 pnorfile="$1"
42 shift 1
Gunnar Millse8221902017-02-03 15:59:06 -060043 ;;
44 esac
45done
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060046
Gunnar Millsc15b02d2017-03-03 10:28:37 -060047if [ ! -f "${pnorfile}" ]; then
48 echo "Please enter a valid PNOR file."
49 echo "$help"
50 exit 1
51fi
52
Michael Tritz8f396732017-06-09 10:55:35 -050053if [[ -z $outfile ]]; then
54 if [[ ${pnorfile##*.} == "pnor" ]]; then
55 outfile=`pwd`/${pnorfile##*/}.squashfs.tar
56 else
57 outfile=`pwd`/${pnorfile##*/}.pnor.squashfs.tar
58 fi
59else
60 if [[ $outfile != /* ]]; then
61 outfile=`pwd`/$outfile
62 fi
63fi
64
Gunnar Millsf8f11302017-03-08 13:11:33 -060065scratch_dir=`mktemp -d`
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060066
67echo "Parsing PNOR TOC..."
Gunnar Millsd4908a42017-03-07 08:25:59 -060068
69# Needed to get the READONLY and PRESERVED flags
70# TODO: Get READONLY and PRESERVED flags from pflash instead.
Gunnar Millsf8f11302017-03-08 13:11:33 -060071pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile}
Gunnar Mills2f27b742017-03-20 13:46:30 -050072pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile}
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060073{
Gunnar Mills2f27b742017-03-20 13:46:30 -050074 version=$(head -n 1 ${scratch_dir}/VERSION)
75 echo "version=$version"
Saqib Khan029b8252017-03-22 21:38:07 -050076 extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',')
77 echo "extended_version=$extended_version"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060078 while read line; do
79 if [[ $line == "ID="* ]]; then
Gunnar Millse0ed3022017-03-02 16:16:19 -060080 # This line looks like
81 # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060082 read -r -a fields <<< "$line"
Gunnar Millse0ed3022017-03-02 16:16:19 -060083
84 # Get any flags attached to end (e.g. [ECC])
85 flags=""
86 for flag in "${fields[@]:4}"
87 do
88 flags+=",${flag//[\[\]]/}"
89 done
90
Gunnar Millsd4908a42017-03-07 08:25:59 -060091 id=${fields[0]##*=}
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -050092 offset=$((${ffs_entry_size} * 10#${id} + ${vercheck_offset}))
93 vercheck=$(xxd -p -l 0x1 -seek ${offset} ${scratch_dir}/part)
Gunnar Millsd4908a42017-03-07 08:25:59 -060094 offset=$((${ffs_entry_size} * 10#${id} + ${miscflags_offset}))
95 miscflags=0x$(xxd -p -l 0x1 -seek ${offset} ${scratch_dir}/part)
96 if ((miscflags & 0x80)); then
97 flags+=",PRESERVED"
Adriana Kobylak9c866062017-04-13 16:12:32 -050098 elif ((miscflags & 0x40)); then
Gunnar Millsd4908a42017-03-07 08:25:59 -060099 flags+=",READONLY"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500100 else
101 flags+=",READWRITE"
Gunnar Millsd4908a42017-03-07 08:25:59 -0600102 fi
103
Gunnar Millse0ed3022017-03-02 16:16:19 -0600104 # Need the partition ID, name, start location, end location, and flags
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500105 echo "partition${id}=${fields[1]},${fields[2]/../,},${vercheck}${flags}"
Gunnar Millsd4908a42017-03-07 08:25:59 -0600106
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600107 # Save the partition name
108 partitions+=(${fields[1]})
109 fi
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500110 # Don't need the BACKUP_PART partition
111 done < <(pflash --info -F ${pnorfile} | grep -v "BACKUP")
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600112} > ${scratch_dir}/${tocfile}
Gunnar Mills37751f92017-02-07 21:05:01 -0600113
Gunnar Mills18f7cdb2017-02-07 16:44:19 -0600114for partition in "${partitions[@]}"; do
115 echo "Reading ${partition}..."
Gunnar Millsf8f11302017-03-08 13:11:33 -0600116 pflash --partition=${partition} \
117 --read=${scratch_dir}/${partition} \
118 -F ${pnorfile}
Gunnar Mills18f7cdb2017-02-07 16:44:19 -0600119done
Gunnar Mills37751f92017-02-07 21:05:01 -0600120
121echo "Creating SquashFS image..."
Gunnar Mills37751f92017-02-07 21:05:01 -0600122cd "${scratch_dir}"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500123mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs
Gunnar Mills37751f92017-02-07 21:05:01 -0600124
Saqib Khan93433f02017-03-23 22:24:27 -0500125echo "Creating MANIFEST for the image"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500126manifest_location="MANIFEST"
Leonel Gonzalezc14a3d22017-06-22 12:49:38 -0500127echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\nversion=$version\n\
Saqib Khan7254f0e2017-04-10 21:45:37 -0500128extended_version=$extended_version" >> $manifest_location
Saqib Khan93433f02017-03-23 22:24:27 -0500129
130echo "Generating tarball to contain the SquashFS image and its MANIFEST"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500131tar -cvf $outfile $manifest_location pnor.xz.squashfs
Saqib Khan93433f02017-03-23 22:24:27 -0500132
133echo "SquashFSTarball at ${outfile}"
Gunnar Mills37751f92017-02-07 21:05:01 -0600134rm -r "${scratch_dir}"