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 | |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 4 | help=$'Generate Tarball with PNOR image and MANIFEST Script |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 5 | |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 6 | Generates a PNOR SquashFS image from the PNOR image for VPNOR, |
| 7 | Or use a static layout raw PNOR image, |
Gunnar Mills | 2941435 | 2017-10-25 11:07:43 -0500 | [diff] [blame] | 8 | Creates a MANIFEST for image verification and recreation |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 9 | Packages the image and MANIFEST together in a tarball |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 10 | |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 11 | usage: generate-tar [OPTION] <PNOR FILE>... |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 12 | |
| 13 | Options: |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 14 | -i, --image <squashfs|static> |
| 15 | Generate SquashFS image or use static PNOR |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 16 | -f, --file <file> Specify destination file. Defaults to |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 17 | `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 James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 24 | -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 Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 28 | -h, --help Display this help text and exit. |
| 29 | ' |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 30 | |
| 31 | private_key=$'-----BEGIN PRIVATE KEY----- |
| 32 | MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri |
| 33 | PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B |
| 34 | zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k |
| 35 | D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU |
| 36 | zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/ |
| 37 | nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1 |
| 38 | h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt |
| 39 | rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE |
| 40 | x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5 |
| 41 | DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw |
| 42 | cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE |
| 43 | 5yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4 |
| 44 | 6ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m |
| 45 | F0nIdUAhR0yTfKM= |
| 46 | -----END PRIVATE KEY----- |
| 47 | ' |
| 48 | |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 49 | # 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 Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 52 | let ffs_entry_size=128 |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 53 | let vercheck_offset=112 |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 54 | do_sign=false |
| 55 | private_key_path="${PRIVATE_KEY_PATH}" |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 56 | image_type="" |
Michael Tritz | 8f39673 | 2017-06-09 10:55:35 -0500 | [diff] [blame] | 57 | outfile="" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 58 | declare -a partitions=() |
| 59 | tocfile="pnor.toc" |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 60 | |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 61 | while [[ $# -gt 0 ]]; do |
| 62 | key="$1" |
| 63 | case $key in |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 64 | -i|--image) |
| 65 | image_type="$2" |
| 66 | shift 2 |
| 67 | ;; |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 68 | -f|--file) |
| 69 | outfile="$2" |
| 70 | shift 2 |
| 71 | ;; |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 72 | -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 Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 81 | -h|--help) |
| 82 | echo "$help" |
| 83 | exit |
| 84 | ;; |
| 85 | *) |
Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 86 | pnorfile="$1" |
| 87 | shift 1 |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 88 | ;; |
| 89 | esac |
| 90 | done |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 91 | |
Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 92 | if [ ! -f "${pnorfile}" ]; then |
| 93 | echo "Please enter a valid PNOR file." |
| 94 | echo "$help" |
| 95 | exit 1 |
| 96 | fi |
| 97 | |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 98 | if [[ "${image_type}" == "squashfs" ]]; then |
| 99 | echo "Will generate squashfs image for VPNOR" |
| 100 | elif [[ "${image_type}" == "static" ]]; then |
| 101 | echo "Will use static image for PNOR" |
| 102 | else |
| 103 | echo "Please specify the image type, \"squashfs\" or \"static\"" |
| 104 | echo |
| 105 | echo "$help" |
| 106 | exit 1 |
| 107 | fi |
| 108 | |
Michael Tritz | 8f39673 | 2017-06-09 10:55:35 -0500 | [diff] [blame] | 109 | if [[ -z $outfile ]]; then |
| 110 | if [[ ${pnorfile##*.} == "pnor" ]]; then |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 111 | outfile=`pwd`/${pnorfile##*/}.$image_type.tar |
Michael Tritz | 8f39673 | 2017-06-09 10:55:35 -0500 | [diff] [blame] | 112 | else |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 113 | 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 Tritz | 8f39673 | 2017-06-09 10:55:35 -0500 | [diff] [blame] | 118 | fi |
| 119 | else |
| 120 | if [[ $outfile != /* ]]; then |
| 121 | outfile=`pwd`/$outfile |
| 122 | fi |
| 123 | fi |
| 124 | |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 125 | |
Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 126 | scratch_dir=`mktemp -d` |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 127 | |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 128 | if [[ "${do_sign}" == true ]]; then |
| 129 | if [[ -z "${private_key_path}" ]]; then |
| 130 | private_key_path=${scratch_dir}/OpenBMC.priv |
| 131 | echo "${private_key}" > "${private_key_path}" |
| 132 | echo "Image is NOT secure!! Signing with the open private key!" |
| 133 | else |
| 134 | if [[ ! -f "${private_key_path}" ]]; then |
| 135 | echo "Couldn't find private key ${private_key_path}." |
| 136 | exit 1 |
| 137 | fi |
| 138 | |
| 139 | echo "Signing with ${private_key_path}." |
| 140 | fi |
| 141 | |
| 142 | public_key_file=publickey |
| 143 | public_key_path=${scratch_dir}/$public_key_file |
| 144 | openssl pkey -in "${private_key_path}" -pubout -out ${public_key_path} |
| 145 | fi |
| 146 | |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 147 | echo "Parsing PNOR TOC..." |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 148 | |
Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 149 | pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile} |
Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame] | 150 | pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile} |
Samuel Mendoza-Jonas | d1d1921 | 2018-06-01 14:02:24 +1000 | [diff] [blame] | 151 | version_size=$(du -k ${scratch_dir}/VERSION | head -1 | cut -f 1) |
| 152 | magic_number=$(xxd -p -l 4 ${scratch_dir}/VERSION) |
| 153 | # Check if VERSION is signed. A signed version partition will have an extra |
| 154 | # 4K header starting with the magic number 0x17082011, see: |
| 155 | # https://github.com/open-power/skiboot/blob/master/libstb/container.h#L47 |
| 156 | if [ "$version_size" == "8" -a "$magic_number" == "17082011" ]; then |
| 157 | # Advance past the STB header (4K, indexed from 1) |
| 158 | cp ${scratch_dir}/VERSION ${scratch_dir}/VERSION_FULL |
| 159 | tail --bytes=+4097 ${scratch_dir}/VERSION_FULL > ${scratch_dir}/VERSION |
| 160 | fi |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 161 | { |
Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame] | 162 | version=$(head -n 1 ${scratch_dir}/VERSION) |
| 163 | echo "version=$version" |
Saqib Khan | 029b825 | 2017-03-22 21:38:07 -0500 | [diff] [blame] | 164 | extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',') |
| 165 | echo "extended_version=$extended_version" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 166 | while read line; do |
| 167 | if [[ $line == "ID="* ]]; then |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 168 | # This line looks like |
| 169 | # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 170 | read -r -a fields <<< "$line" |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 171 | |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 172 | id=${fields[0]##*=} |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 173 | offset=$((${ffs_entry_size} * 10#${id} + ${vercheck_offset})) |
| 174 | vercheck=$(xxd -p -l 0x1 -seek ${offset} ${scratch_dir}/part) |
Michael Tritz | 1bd65ac | 2017-05-07 17:40:14 -0500 | [diff] [blame] | 175 | export flags=$(pflash --detail=$((10#$id)) -F ${pnorfile} | grep "\[" | |
| 176 | sed 's/....$//' | tr '\n' ',' | sed 's/.$//') |
| 177 | if [[ $flags != "" ]]; then |
| 178 | flags=,$flags |
| 179 | fi |
| 180 | |
| 181 | if [[ $(echo $flags | grep "READONLY") == "" && |
| 182 | $(echo $flags | grep "PRESERVED") == "" ]]; then |
| 183 | flags=$flags,READWRITE |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 184 | fi |
| 185 | |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 186 | # Need the partition ID, name, start location, end location, and flags |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 187 | echo "partition${id}=${fields[1]},${fields[2]/../,},${vercheck}${flags}" |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 188 | |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 189 | # Save the partition name |
| 190 | partitions+=(${fields[1]}) |
| 191 | fi |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 192 | # Don't need the BACKUP_PART partition |
| 193 | done < <(pflash --info -F ${pnorfile} | grep -v "BACKUP") |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 194 | } > ${scratch_dir}/${tocfile} |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 195 | |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 196 | for partition in "${partitions[@]}"; do |
| 197 | echo "Reading ${partition}..." |
Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 198 | pflash --partition=${partition} \ |
| 199 | --read=${scratch_dir}/${partition} \ |
| 200 | -F ${pnorfile} |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 201 | done |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 202 | |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 203 | manifest_location="MANIFEST" |
| 204 | files_to_sign="$manifest_location $public_key_file" |
| 205 | |
| 206 | # Go to scratch_dir |
| 207 | |
| 208 | if [[ "${image_type}" == "squashfs" ]]; then |
| 209 | echo "Creating SquashFS image..." |
| 210 | # Prepare pnor file in scratch_dir |
| 211 | cd "${scratch_dir}" |
| 212 | mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs |
| 213 | files_to_sign+=" pnor.xz.squashfs" |
| 214 | else |
| 215 | cp ${pnorfile} ${scratch_dir} |
| 216 | cd "${scratch_dir}" |
| 217 | files_to_sign+=" $(basename ${pnorfile})" |
| 218 | fi |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 219 | |
Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 220 | echo "Creating MANIFEST for the image" |
Leonel Gonzalez | c14a3d2 | 2017-06-22 12:49:38 -0500 | [diff] [blame] | 221 | 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] | 222 | extended_version=$extended_version" >> $manifest_location |
Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 223 | |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 224 | if [[ "${do_sign}" == true ]]; then |
| 225 | private_key_name=$(basename "${private_key_path}") |
| 226 | key_type="${private_key_name%.*}" |
| 227 | echo KeyType="${key_type}" >> $manifest_location |
| 228 | echo HashType="RSA-SHA256" >> $manifest_location |
| 229 | |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 230 | for file in $files_to_sign; do |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 231 | openssl dgst -sha256 -sign ${private_key_path} -out "${file}.sig" $file |
| 232 | done |
| 233 | |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 234 | additional_files="*.sig" |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 235 | fi |
| 236 | |
Lei YU | dcb3fd7 | 2019-02-21 13:19:03 +0800 | [diff] [blame] | 237 | if [[ "${image_type}" == "squashfs" ]]; then |
| 238 | echo "Generating tarball to contain the SquashFS image and its MANIFEST" |
| 239 | tar -cvf $outfile $files_to_sign $additional_files |
| 240 | echo "SquashFSTarball at ${outfile}" |
| 241 | else |
| 242 | tar -czvf $outfile $files_to_sign $additional_files |
| 243 | echo "Static layout tarball at $outfile" |
| 244 | fi |
Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 245 | |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 246 | rm -r "${scratch_dir}" |