blob: d948f33e35805d730eca850bc794257d1c69b9ea [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
Lei YUdcb3fd72019-02-21 13:19:03 +08004help=$'Generate Tarball with PNOR image and MANIFEST Script
Gunnar Millse8221902017-02-03 15:59:06 -06005
Lei YUdcb3fd72019-02-21 13:19:03 +08006Generates a PNOR SquashFS image from the PNOR image for VPNOR,
7Or use a static layout raw PNOR image,
Gunnar Mills29414352017-10-25 11:07:43 -05008Creates a MANIFEST for image verification and recreation
Lei YUdcb3fd72019-02-21 13:19:03 +08009Packages the image and MANIFEST together in a tarball
Gunnar Millse8221902017-02-03 15:59:06 -060010
Lei YUdcb3fd72019-02-21 13:19:03 +080011usage: generate-tar [OPTION] <PNOR FILE>...
Gunnar Millse8221902017-02-03 15:59:06 -060012
13Options:
Lei YUdcb3fd72019-02-21 13:19:03 +080014 -i, --image <squashfs|static>
15 Generate SquashFS image or use static PNOR
Gunnar Mills37751f92017-02-07 21:05:01 -060016 -f, --file <file> Specify destination file. Defaults to
Lei YUdcb3fd72019-02-21 13:19:03 +080017 `pwd`/<PNOR FILE>.pnor.<image_type>.tar[.gz] if
18 unspecified.
19 (For example,
20 * "generate-tar -i squashfs my.pnor" would generate
21 `pwd`/my.pnor.squashfs.tar
22 * "generate-tar -i static my.pnor" would generate
23 `pwd`/my.pnor.static.tar.gz)
Eddie James643e7302018-02-26 15:23:43 -060024 -s, --sign <path> Sign the image. The optional path argument specifies
25 the private key file. Defaults to the bash variable
26 PRIVATE_KEY_PATH if available, or else uses the
27 open-source private key in this script.
Gunnar Millse8221902017-02-03 15:59:06 -060028 -h, --help Display this help text and exit.
29'
Eddie James643e7302018-02-26 15:23:43 -060030
31private_key=$'-----BEGIN PRIVATE KEY-----
32MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri
33PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B
34zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k
35D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU
36zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/
37nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1
38h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt
39rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE
40x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5
41DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw
42cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE
435yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4
446ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m
45F0nIdUAhR0yTfKM=
46-----END PRIVATE KEY-----
47'
48
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -050049# Reference the ffs structures at:
50# https://github.com/open-power/hostboot/blob/master/src/usr/pnor/common/ffs_hb.H
51# https://github.com/open-power/hostboot/blob/master/src/usr/pnor/ffs.h
Gunnar Millsd4908a42017-03-07 08:25:59 -060052let ffs_entry_size=128
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -050053let vercheck_offset=112
Eddie James643e7302018-02-26 15:23:43 -060054do_sign=false
55private_key_path="${PRIVATE_KEY_PATH}"
Lei YUdcb3fd72019-02-21 13:19:03 +080056image_type=""
Michael Tritz8f396732017-06-09 10:55:35 -050057outfile=""
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060058declare -a partitions=()
59tocfile="pnor.toc"
Gunnar Mills37751f92017-02-07 21:05:01 -060060
Gunnar Millse8221902017-02-03 15:59:06 -060061while [[ $# -gt 0 ]]; do
62 key="$1"
63 case $key in
Lei YUdcb3fd72019-02-21 13:19:03 +080064 -i|--image)
65 image_type="$2"
66 shift 2
67 ;;
Gunnar Mills37751f92017-02-07 21:05:01 -060068 -f|--file)
69 outfile="$2"
70 shift 2
71 ;;
Eddie James643e7302018-02-26 15:23:43 -060072 -s|--sign)
73 do_sign=true
74 if [[ ! -z "${2}" && "${2}" != -* ]]; then
75 private_key_path="$2"
76 shift 2
77 else
78 shift 1
79 fi
80 ;;
Gunnar Millse8221902017-02-03 15:59:06 -060081 -h|--help)
82 echo "$help"
83 exit
84 ;;
85 *)
Gunnar Millsc15b02d2017-03-03 10:28:37 -060086 pnorfile="$1"
87 shift 1
Gunnar Millse8221902017-02-03 15:59:06 -060088 ;;
89 esac
90done
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060091
Gunnar Millsc15b02d2017-03-03 10:28:37 -060092if [ ! -f "${pnorfile}" ]; then
93 echo "Please enter a valid PNOR file."
94 echo "$help"
95 exit 1
96fi
97
Lei YUdcb3fd72019-02-21 13:19:03 +080098if [[ "${image_type}" == "squashfs" ]]; then
99 echo "Will generate squashfs image for VPNOR"
100elif [[ "${image_type}" == "static" ]]; then
101 echo "Will use static image for PNOR"
102else
103 echo "Please specify the image type, \"squashfs\" or \"static\""
104 echo
105 echo "$help"
106 exit 1
107fi
108
Michael Tritz8f396732017-06-09 10:55:35 -0500109if [[ -z $outfile ]]; then
110 if [[ ${pnorfile##*.} == "pnor" ]]; then
Lei YUdcb3fd72019-02-21 13:19:03 +0800111 outfile=`pwd`/${pnorfile##*/}.$image_type.tar
Michael Tritz8f396732017-06-09 10:55:35 -0500112 else
Lei YUdcb3fd72019-02-21 13:19:03 +0800113 outfile=`pwd`/${pnorfile##*/}.pnor.$image_type.tar
114 fi
115 if [[ "${image_type}" == "static" ]]; then
116 # Append .gz so the tarball is compressed
117 outfile=$outfile.gz
Michael Tritz8f396732017-06-09 10:55:35 -0500118 fi
119else
120 if [[ $outfile != /* ]]; then
121 outfile=`pwd`/$outfile
122 fi
123fi
124
Lei YUdcb3fd72019-02-21 13:19:03 +0800125
Gunnar Millsf8f11302017-03-08 13:11:33 -0600126scratch_dir=`mktemp -d`
Lei YU00d8ade2020-01-14 11:30:04 +0800127# Remove the temp directory on exit.
128# The files in the temp directory may contain read-only files, so add
129# --interactive=never to skip the prompt.
130trap "{ rm -r --interactive=never ${scratch_dir}; }" EXIT
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600131
Eddie James643e7302018-02-26 15:23:43 -0600132if [[ "${do_sign}" == true ]]; then
133 if [[ -z "${private_key_path}" ]]; then
134 private_key_path=${scratch_dir}/OpenBMC.priv
135 echo "${private_key}" > "${private_key_path}"
136 echo "Image is NOT secure!! Signing with the open private key!"
137 else
138 if [[ ! -f "${private_key_path}" ]]; then
139 echo "Couldn't find private key ${private_key_path}."
140 exit 1
141 fi
142
143 echo "Signing with ${private_key_path}."
144 fi
145
146 public_key_file=publickey
147 public_key_path=${scratch_dir}/$public_key_file
148 openssl pkey -in "${private_key_path}" -pubout -out ${public_key_path}
149fi
150
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600151echo "Parsing PNOR TOC..."
Gunnar Millsd4908a42017-03-07 08:25:59 -0600152
Adriana Kobylak851bc062019-06-05 14:33:06 -0500153pnor_dir="${scratch_dir}/pnor"
154mkdir ${pnor_dir}
155
156pflash --partition=part --read=${pnor_dir}/part -F ${pnorfile}
157pflash --partition=VERSION --read=${pnor_dir}/VERSION -F ${pnorfile}
Oliver O'Hallorand23b5b72019-07-30 17:46:11 +1000158version_size=$(wc -c ${pnor_dir}/VERSION | cut -d' ' -f 1)
Adriana Kobylak851bc062019-06-05 14:33:06 -0500159magic_number=$(xxd -p -l 4 ${pnor_dir}/VERSION)
Samuel Mendoza-Jonasd1d19212018-06-01 14:02:24 +1000160# Check if VERSION is signed. A signed version partition will have an extra
161# 4K header starting with the magic number 0x17082011, see:
162# https://github.com/open-power/skiboot/blob/master/libstb/container.h#L47
Oliver O'Hallorand23b5b72019-07-30 17:46:11 +1000163if [ "$version_size" == "8192" -a "$magic_number" == "17082011" ]; then
Samuel Mendoza-Jonasd1d19212018-06-01 14:02:24 +1000164 # Advance past the STB header (4K, indexed from 1)
Adriana Kobylak851bc062019-06-05 14:33:06 -0500165 cp ${pnor_dir}/VERSION ${pnor_dir}/VERSION_FULL
166 tail --bytes=+4097 ${pnor_dir}/VERSION_FULL > ${pnor_dir}/VERSION
Samuel Mendoza-Jonasd1d19212018-06-01 14:02:24 +1000167fi
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600168{
Adriana Kobylak851bc062019-06-05 14:33:06 -0500169 version=$(head -n 1 ${pnor_dir}/VERSION)
Gunnar Mills2f27b742017-03-20 13:46:30 -0500170 echo "version=$version"
Adriana Kobylak851bc062019-06-05 14:33:06 -0500171 extended_version=$(echo $(tail -n +2 ${pnor_dir}/VERSION)|tr ' ' ',')
Saqib Khan029b8252017-03-22 21:38:07 -0500172 echo "extended_version=$extended_version"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600173 while read line; do
174 if [[ $line == "ID="* ]]; then
Gunnar Millse0ed3022017-03-02 16:16:19 -0600175 # This line looks like
176 # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600177 read -r -a fields <<< "$line"
Gunnar Millse0ed3022017-03-02 16:16:19 -0600178
Gunnar Millsd4908a42017-03-07 08:25:59 -0600179 id=${fields[0]##*=}
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500180 offset=$((${ffs_entry_size} * 10#${id} + ${vercheck_offset}))
Adriana Kobylak851bc062019-06-05 14:33:06 -0500181 vercheck=$(xxd -p -l 0x1 -seek ${offset} ${pnor_dir}/part)
Michael Tritz1bd65ac2017-05-07 17:40:14 -0500182 export flags=$(pflash --detail=$((10#$id)) -F ${pnorfile} | grep "\[" |
183 sed 's/....$//' | tr '\n' ',' | sed 's/.$//')
184 if [[ $flags != "" ]]; then
185 flags=,$flags
186 fi
187
188 if [[ $(echo $flags | grep "READONLY") == "" &&
189 $(echo $flags | grep "PRESERVED") == "" ]]; then
190 flags=$flags,READWRITE
Gunnar Millsd4908a42017-03-07 08:25:59 -0600191 fi
192
Gunnar Millse0ed3022017-03-02 16:16:19 -0600193 # Need the partition ID, name, start location, end location, and flags
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500194 echo "partition${id}=${fields[1]},${fields[2]/../,},${vercheck}${flags}"
Gunnar Millsd4908a42017-03-07 08:25:59 -0600195
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600196 # Save the partition name
197 partitions+=(${fields[1]})
198 fi
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500199 # Don't need the BACKUP_PART partition
200 done < <(pflash --info -F ${pnorfile} | grep -v "BACKUP")
Adriana Kobylak851bc062019-06-05 14:33:06 -0500201} > ${pnor_dir}/${tocfile}
Gunnar Mills37751f92017-02-07 21:05:01 -0600202
Gunnar Mills18f7cdb2017-02-07 16:44:19 -0600203for partition in "${partitions[@]}"; do
204 echo "Reading ${partition}..."
Gunnar Millsf8f11302017-03-08 13:11:33 -0600205 pflash --partition=${partition} \
Adriana Kobylak851bc062019-06-05 14:33:06 -0500206 --read=${pnor_dir}/${partition} \
Gunnar Millsf8f11302017-03-08 13:11:33 -0600207 -F ${pnorfile}
Gunnar Mills18f7cdb2017-02-07 16:44:19 -0600208done
Gunnar Mills37751f92017-02-07 21:05:01 -0600209
Lei YUdcb3fd72019-02-21 13:19:03 +0800210manifest_location="MANIFEST"
211files_to_sign="$manifest_location $public_key_file"
212
213# Go to scratch_dir
214
215if [[ "${image_type}" == "squashfs" ]]; then
216 echo "Creating SquashFS image..."
Adriana Kobylak851bc062019-06-05 14:33:06 -0500217 # Prepare pnor file in ${pnor_dir}
218 cd "${pnor_dir}"
Adriana Kobylak85f25402019-10-09 13:56:59 -0500219 # Set permissions of partition files to read only
220 chmod 440 *
Adriana Kobylakddf2b742019-09-11 10:31:09 -0500221 mksquashfs ${tocfile} ${partitions[*]} ${scratch_dir}/pnor.xz.squashfs -all-root
Lei YUdcb3fd72019-02-21 13:19:03 +0800222 cd "${scratch_dir}"
Lei YUdcb3fd72019-02-21 13:19:03 +0800223 files_to_sign+=" pnor.xz.squashfs"
224else
225 cp ${pnorfile} ${scratch_dir}
226 cd "${scratch_dir}"
227 files_to_sign+=" $(basename ${pnorfile})"
228fi
Gunnar Mills37751f92017-02-07 21:05:01 -0600229
Saqib Khan93433f02017-03-23 22:24:27 -0500230echo "Creating MANIFEST for the image"
Leonel Gonzalezc14a3d22017-06-22 12:49:38 -0500231echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\nversion=$version\n\
Saqib Khan7254f0e2017-04-10 21:45:37 -0500232extended_version=$extended_version" >> $manifest_location
Saqib Khan93433f02017-03-23 22:24:27 -0500233
Eddie James643e7302018-02-26 15:23:43 -0600234if [[ "${do_sign}" == true ]]; then
235 private_key_name=$(basename "${private_key_path}")
236 key_type="${private_key_name%.*}"
237 echo KeyType="${key_type}" >> $manifest_location
238 echo HashType="RSA-SHA256" >> $manifest_location
239
Lei YUdcb3fd72019-02-21 13:19:03 +0800240 for file in $files_to_sign; do
Eddie James643e7302018-02-26 15:23:43 -0600241 openssl dgst -sha256 -sign ${private_key_path} -out "${file}.sig" $file
242 done
243
Lei YUdcb3fd72019-02-21 13:19:03 +0800244 additional_files="*.sig"
Eddie James643e7302018-02-26 15:23:43 -0600245fi
246
Lei YUdcb3fd72019-02-21 13:19:03 +0800247if [[ "${image_type}" == "squashfs" ]]; then
248 echo "Generating tarball to contain the SquashFS image and its MANIFEST"
249 tar -cvf $outfile $files_to_sign $additional_files
250 echo "SquashFSTarball at ${outfile}"
251else
252 tar -czvf $outfile $files_to_sign $additional_files
253 echo "Static layout tarball at $outfile"
254fi
Saqib Khan93433f02017-03-23 22:24:27 -0500255