blob: 3e74e9b31f8ce040bb47a9447fe1eadca910b0ef [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
Gunnar Mills29414352017-10-25 11:07:43 -05004help=$'Generate Tarball with PNOR SquashFS image and MANIFEST Script
Gunnar Millse8221902017-02-03 15:59:06 -06005
Gunnar Mills29414352017-10-25 11:07:43 -05006Generates a PNOR SquashFS image from the PNOR image
7Creates a MANIFEST for image verification and recreation
Saqib Khan93433f02017-03-23 22:24:27 -05008Packages 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.)
Eddie James643e7302018-02-26 15:23:43 -060017 -s, --sign <path> Sign the image. The optional path argument specifies
18 the private key file. Defaults to the bash variable
19 PRIVATE_KEY_PATH if available, or else uses the
20 open-source private key in this script.
Gunnar Millse8221902017-02-03 15:59:06 -060021 -h, --help Display this help text and exit.
22'
Eddie James643e7302018-02-26 15:23:43 -060023
24private_key=$'-----BEGIN PRIVATE KEY-----
25MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri
26PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B
27zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k
28D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU
29zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/
30nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1
31h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt
32rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE
33x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5
34DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw
35cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE
365yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4
376ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m
38F0nIdUAhR0yTfKM=
39-----END PRIVATE KEY-----
40'
41
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -050042# Reference the ffs structures at:
43# https://github.com/open-power/hostboot/blob/master/src/usr/pnor/common/ffs_hb.H
44# https://github.com/open-power/hostboot/blob/master/src/usr/pnor/ffs.h
Gunnar Millsd4908a42017-03-07 08:25:59 -060045let ffs_entry_size=128
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -050046let vercheck_offset=112
Eddie James643e7302018-02-26 15:23:43 -060047do_sign=false
48private_key_path="${PRIVATE_KEY_PATH}"
Michael Tritz8f396732017-06-09 10:55:35 -050049outfile=""
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060050declare -a partitions=()
51tocfile="pnor.toc"
Gunnar Mills37751f92017-02-07 21:05:01 -060052
Gunnar Millse8221902017-02-03 15:59:06 -060053while [[ $# -gt 0 ]]; do
54 key="$1"
55 case $key in
Gunnar Mills37751f92017-02-07 21:05:01 -060056 -f|--file)
57 outfile="$2"
58 shift 2
59 ;;
Eddie James643e7302018-02-26 15:23:43 -060060 -s|--sign)
61 do_sign=true
62 if [[ ! -z "${2}" && "${2}" != -* ]]; then
63 private_key_path="$2"
64 shift 2
65 else
66 shift 1
67 fi
68 ;;
Gunnar Millse8221902017-02-03 15:59:06 -060069 -h|--help)
70 echo "$help"
71 exit
72 ;;
73 *)
Gunnar Millsc15b02d2017-03-03 10:28:37 -060074 pnorfile="$1"
75 shift 1
Gunnar Millse8221902017-02-03 15:59:06 -060076 ;;
77 esac
78done
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060079
Gunnar Millsc15b02d2017-03-03 10:28:37 -060080if [ ! -f "${pnorfile}" ]; then
81 echo "Please enter a valid PNOR file."
82 echo "$help"
83 exit 1
84fi
85
Michael Tritz8f396732017-06-09 10:55:35 -050086if [[ -z $outfile ]]; then
87 if [[ ${pnorfile##*.} == "pnor" ]]; then
88 outfile=`pwd`/${pnorfile##*/}.squashfs.tar
89 else
90 outfile=`pwd`/${pnorfile##*/}.pnor.squashfs.tar
91 fi
92else
93 if [[ $outfile != /* ]]; then
94 outfile=`pwd`/$outfile
95 fi
96fi
97
Gunnar Millsf8f11302017-03-08 13:11:33 -060098scratch_dir=`mktemp -d`
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060099
Eddie James643e7302018-02-26 15:23:43 -0600100if [[ "${do_sign}" == true ]]; then
101 if [[ -z "${private_key_path}" ]]; then
102 private_key_path=${scratch_dir}/OpenBMC.priv
103 echo "${private_key}" > "${private_key_path}"
104 echo "Image is NOT secure!! Signing with the open private key!"
105 else
106 if [[ ! -f "${private_key_path}" ]]; then
107 echo "Couldn't find private key ${private_key_path}."
108 exit 1
109 fi
110
111 echo "Signing with ${private_key_path}."
112 fi
113
114 public_key_file=publickey
115 public_key_path=${scratch_dir}/$public_key_file
116 openssl pkey -in "${private_key_path}" -pubout -out ${public_key_path}
117fi
118
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600119echo "Parsing PNOR TOC..."
Gunnar Millsd4908a42017-03-07 08:25:59 -0600120
Gunnar Millsf8f11302017-03-08 13:11:33 -0600121pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile}
Gunnar Mills2f27b742017-03-20 13:46:30 -0500122pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile}
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600123{
Gunnar Mills2f27b742017-03-20 13:46:30 -0500124 version=$(head -n 1 ${scratch_dir}/VERSION)
125 echo "version=$version"
Saqib Khan029b8252017-03-22 21:38:07 -0500126 extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',')
127 echo "extended_version=$extended_version"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600128 while read line; do
129 if [[ $line == "ID="* ]]; then
Gunnar Millse0ed3022017-03-02 16:16:19 -0600130 # This line looks like
131 # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600132 read -r -a fields <<< "$line"
Gunnar Millse0ed3022017-03-02 16:16:19 -0600133
Gunnar Millsd4908a42017-03-07 08:25:59 -0600134 id=${fields[0]##*=}
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500135 offset=$((${ffs_entry_size} * 10#${id} + ${vercheck_offset}))
136 vercheck=$(xxd -p -l 0x1 -seek ${offset} ${scratch_dir}/part)
Michael Tritz1bd65ac2017-05-07 17:40:14 -0500137 export flags=$(pflash --detail=$((10#$id)) -F ${pnorfile} | grep "\[" |
138 sed 's/....$//' | tr '\n' ',' | sed 's/.$//')
139 if [[ $flags != "" ]]; then
140 flags=,$flags
141 fi
142
143 if [[ $(echo $flags | grep "READONLY") == "" &&
144 $(echo $flags | grep "PRESERVED") == "" ]]; then
145 flags=$flags,READWRITE
Gunnar Millsd4908a42017-03-07 08:25:59 -0600146 fi
147
Gunnar Millse0ed3022017-03-02 16:16:19 -0600148 # Need the partition ID, name, start location, end location, and flags
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500149 echo "partition${id}=${fields[1]},${fields[2]/../,},${vercheck}${flags}"
Gunnar Millsd4908a42017-03-07 08:25:59 -0600150
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600151 # Save the partition name
152 partitions+=(${fields[1]})
153 fi
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500154 # Don't need the BACKUP_PART partition
155 done < <(pflash --info -F ${pnorfile} | grep -v "BACKUP")
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600156} > ${scratch_dir}/${tocfile}
Gunnar Mills37751f92017-02-07 21:05:01 -0600157
Gunnar Mills18f7cdb2017-02-07 16:44:19 -0600158for partition in "${partitions[@]}"; do
159 echo "Reading ${partition}..."
Gunnar Millsf8f11302017-03-08 13:11:33 -0600160 pflash --partition=${partition} \
161 --read=${scratch_dir}/${partition} \
162 -F ${pnorfile}
Gunnar Mills18f7cdb2017-02-07 16:44:19 -0600163done
Gunnar Mills37751f92017-02-07 21:05:01 -0600164
165echo "Creating SquashFS image..."
Gunnar Mills37751f92017-02-07 21:05:01 -0600166cd "${scratch_dir}"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500167mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs
Gunnar Mills37751f92017-02-07 21:05:01 -0600168
Saqib Khan93433f02017-03-23 22:24:27 -0500169echo "Creating MANIFEST for the image"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500170manifest_location="MANIFEST"
Leonel Gonzalezc14a3d22017-06-22 12:49:38 -0500171echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\nversion=$version\n\
Saqib Khan7254f0e2017-04-10 21:45:37 -0500172extended_version=$extended_version" >> $manifest_location
Saqib Khan93433f02017-03-23 22:24:27 -0500173
Eddie James643e7302018-02-26 15:23:43 -0600174if [[ "${do_sign}" == true ]]; then
175 private_key_name=$(basename "${private_key_path}")
176 key_type="${private_key_name%.*}"
177 echo KeyType="${key_type}" >> $manifest_location
178 echo HashType="RSA-SHA256" >> $manifest_location
179
180 for file in pnor.xz.squashfs $manifest_location $public_key_file; do
181 openssl dgst -sha256 -sign ${private_key_path} -out "${file}.sig" $file
182 done
183
184 additional_files="${public_key_file} *.sig"
185fi
186
Saqib Khan93433f02017-03-23 22:24:27 -0500187echo "Generating tarball to contain the SquashFS image and its MANIFEST"
Eddie James643e7302018-02-26 15:23:43 -0600188tar -cvf $outfile $manifest_location pnor.xz.squashfs $additional_files
Saqib Khan93433f02017-03-23 22:24:27 -0500189
190echo "SquashFSTarball at ${outfile}"
Gunnar Mills37751f92017-02-07 21:05:01 -0600191rm -r "${scratch_dir}"