| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 1 | #!/bin/bash | 
| Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 2 | set -eo pipefail | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 3 |  | 
| Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 4 | help=$'Generate Tarball with SquashFS image and MANIFEST Script | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 5 |  | 
 | 6 | Generates a SquashFS image from the PNOR image | 
| Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 7 | Creates a MANIFEST for image verification | 
 | 8 | Packages the SquashFS image and MANIFEST together in a tarball | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 9 |  | 
| Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 10 | usage: generate-squashfs [OPTION] <PNOR FILE>... | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 11 |  | 
 | 12 | Options: | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 13 |    -f, --file <file>      Specify destination file. Defaults to | 
| Michael Tritz | 8f39673 | 2017-06-09 10:55:35 -0500 | [diff] [blame] | 14 |                           `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 Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 17 |    -h, --help             Display this help text and exit. | 
 | 18 | ' | 
| Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 19 | # 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 Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 22 | let ffs_entry_size=128 | 
| Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 23 | let vercheck_offset=112 | 
| Michael Tritz | 8f39673 | 2017-06-09 10:55:35 -0500 | [diff] [blame] | 24 | outfile="" | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 25 | declare -a partitions=() | 
 | 26 | tocfile="pnor.toc" | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 27 |  | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 28 | while [[ $# -gt 0 ]]; do | 
 | 29 |   key="$1" | 
 | 30 |   case $key in | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 31 |     -f|--file) | 
 | 32 |       outfile="$2" | 
 | 33 |       shift 2 | 
 | 34 |       ;; | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 35 |     -h|--help) | 
 | 36 |       echo "$help" | 
 | 37 |       exit | 
 | 38 |       ;; | 
 | 39 |     *) | 
| Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 40 |       pnorfile="$1" | 
 | 41 |       shift 1 | 
| Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 42 |       ;; | 
 | 43 |   esac | 
 | 44 | done | 
| Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 45 |  | 
| Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 46 | if [ ! -f "${pnorfile}" ]; then | 
 | 47 |   echo "Please enter a valid PNOR file." | 
 | 48 |   echo "$help" | 
 | 49 |   exit 1 | 
 | 50 | fi | 
 | 51 |  | 
| Michael Tritz | 8f39673 | 2017-06-09 10:55:35 -0500 | [diff] [blame] | 52 | if [[ -z $outfile ]]; then | 
 | 53 |     if [[ ${pnorfile##*.} == "pnor" ]]; then | 
 | 54 |         outfile=`pwd`/${pnorfile##*/}.squashfs.tar | 
 | 55 |     else | 
 | 56 |         outfile=`pwd`/${pnorfile##*/}.pnor.squashfs.tar | 
 | 57 |     fi | 
 | 58 | else | 
 | 59 |     if [[ $outfile != /* ]]; then | 
 | 60 |         outfile=`pwd`/$outfile | 
 | 61 |     fi | 
 | 62 | fi | 
 | 63 |  | 
| Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 64 | scratch_dir=`mktemp -d` | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 65 |  | 
 | 66 | echo "Parsing PNOR TOC..." | 
| Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 67 |  | 
| Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 68 | pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile} | 
| Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame] | 69 | pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile} | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 70 | { | 
| Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame] | 71 |   version=$(head -n 1 ${scratch_dir}/VERSION) | 
 | 72 |   echo "version=$version" | 
| Saqib Khan | 029b825 | 2017-03-22 21:38:07 -0500 | [diff] [blame] | 73 |   extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',') | 
 | 74 |   echo "extended_version=$extended_version" | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 75 |   while read line; do | 
 | 76 |     if [[ $line == "ID="* ]]; then | 
| Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 77 |         # This line looks like | 
 | 78 |         # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]" | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 79 |         read -r -a fields <<< "$line" | 
| Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 80 |  | 
| Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 81 |         id=${fields[0]##*=} | 
| Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 82 |         offset=$((${ffs_entry_size} * 10#${id} + ${vercheck_offset})) | 
 | 83 |         vercheck=$(xxd -p -l  0x1 -seek ${offset} ${scratch_dir}/part) | 
| Michael Tritz | 1bd65ac | 2017-05-07 17:40:14 -0500 | [diff] [blame] | 84 |         export flags=$(pflash --detail=$((10#$id)) -F ${pnorfile} | grep "\[" | | 
 | 85 |                 sed 's/....$//' | tr '\n' ',' | sed 's/.$//') | 
 | 86 |         if [[ $flags != "" ]]; then | 
 | 87 |             flags=,$flags | 
 | 88 |         fi | 
 | 89 |  | 
 | 90 |         if [[ $(echo $flags | grep "READONLY") == "" && | 
 | 91 |               $(echo $flags | grep "PRESERVED") == "" ]]; then | 
 | 92 |             flags=$flags,READWRITE | 
| Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 93 |         fi | 
 | 94 |  | 
| Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 95 |         # Need the partition ID, name, start location, end location, and flags | 
| Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 96 |         echo  "partition${id}=${fields[1]},${fields[2]/../,},${vercheck}${flags}" | 
| Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 97 |  | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 98 |         # Save the partition name | 
 | 99 |         partitions+=(${fields[1]}) | 
 | 100 |     fi | 
| Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 101 |   # Don't need the BACKUP_PART partition | 
 | 102 |   done < <(pflash --info -F ${pnorfile} | grep -v "BACKUP") | 
| Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 103 | } > ${scratch_dir}/${tocfile} | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 104 |  | 
| Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 105 | for partition in "${partitions[@]}"; do | 
 | 106 |   echo "Reading ${partition}..." | 
| Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 107 |   pflash --partition=${partition} \ | 
 | 108 |     --read=${scratch_dir}/${partition} \ | 
 | 109 |     -F ${pnorfile} | 
| Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 110 | done | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 111 |  | 
 | 112 | echo "Creating SquashFS image..." | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 113 | cd "${scratch_dir}" | 
| Adriana Kobylak | 9c86606 | 2017-04-13 16:12:32 -0500 | [diff] [blame] | 114 | mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 115 |  | 
| Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 116 | echo "Creating MANIFEST for the image" | 
| Adriana Kobylak | 9c86606 | 2017-04-13 16:12:32 -0500 | [diff] [blame] | 117 | manifest_location="MANIFEST" | 
| Leonel Gonzalez | c14a3d2 | 2017-06-22 12:49:38 -0500 | [diff] [blame] | 118 | echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\nversion=$version\n\ | 
| Saqib Khan | 7254f0e | 2017-04-10 21:45:37 -0500 | [diff] [blame] | 119 | extended_version=$extended_version" >> $manifest_location | 
| Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 120 |  | 
 | 121 | echo "Generating tarball to contain the SquashFS image and its MANIFEST" | 
| Adriana Kobylak | 9c86606 | 2017-04-13 16:12:32 -0500 | [diff] [blame] | 122 | tar -cvf $outfile $manifest_location pnor.xz.squashfs | 
| Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 123 |  | 
 | 124 | echo "SquashFSTarball at ${outfile}" | 
| Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 125 | rm -r "${scratch_dir}" |