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 | |
Gunnar Mills | 2941435 | 2017-10-25 11:07:43 -0500 | [diff] [blame] | 4 | help=$'Generate Tarball with PNOR SquashFS image and MANIFEST Script |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 5 | |
Gunnar Mills | 2941435 | 2017-10-25 11:07:43 -0500 | [diff] [blame] | 6 | Generates a PNOR SquashFS image from the PNOR image |
| 7 | Creates a MANIFEST for image verification and recreation |
Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 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.) |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 17 | -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 Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 21 | -h, --help Display this help text and exit. |
| 22 | ' |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 23 | |
| 24 | private_key=$'-----BEGIN PRIVATE KEY----- |
| 25 | MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri |
| 26 | PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B |
| 27 | zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k |
| 28 | D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU |
| 29 | zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/ |
| 30 | nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1 |
| 31 | h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt |
| 32 | rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE |
| 33 | x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5 |
| 34 | DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw |
| 35 | cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE |
| 36 | 5yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4 |
| 37 | 6ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m |
| 38 | F0nIdUAhR0yTfKM= |
| 39 | -----END PRIVATE KEY----- |
| 40 | ' |
| 41 | |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 42 | # 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 Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 45 | let ffs_entry_size=128 |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 46 | let vercheck_offset=112 |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 47 | do_sign=false |
| 48 | private_key_path="${PRIVATE_KEY_PATH}" |
Michael Tritz | 8f39673 | 2017-06-09 10:55:35 -0500 | [diff] [blame] | 49 | outfile="" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 50 | declare -a partitions=() |
| 51 | tocfile="pnor.toc" |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 52 | |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 53 | while [[ $# -gt 0 ]]; do |
| 54 | key="$1" |
| 55 | case $key in |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 56 | -f|--file) |
| 57 | outfile="$2" |
| 58 | shift 2 |
| 59 | ;; |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 60 | -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 Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 69 | -h|--help) |
| 70 | echo "$help" |
| 71 | exit |
| 72 | ;; |
| 73 | *) |
Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 74 | pnorfile="$1" |
| 75 | shift 1 |
Gunnar Mills | e822190 | 2017-02-03 15:59:06 -0600 | [diff] [blame] | 76 | ;; |
| 77 | esac |
| 78 | done |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 79 | |
Gunnar Mills | c15b02d | 2017-03-03 10:28:37 -0600 | [diff] [blame] | 80 | if [ ! -f "${pnorfile}" ]; then |
| 81 | echo "Please enter a valid PNOR file." |
| 82 | echo "$help" |
| 83 | exit 1 |
| 84 | fi |
| 85 | |
Michael Tritz | 8f39673 | 2017-06-09 10:55:35 -0500 | [diff] [blame] | 86 | if [[ -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 |
| 92 | else |
| 93 | if [[ $outfile != /* ]]; then |
| 94 | outfile=`pwd`/$outfile |
| 95 | fi |
| 96 | fi |
| 97 | |
Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 98 | scratch_dir=`mktemp -d` |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 99 | |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 100 | if [[ "${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} |
| 117 | fi |
| 118 | |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 119 | echo "Parsing PNOR TOC..." |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 120 | |
Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 121 | pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile} |
Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame] | 122 | pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile} |
Samuel Mendoza-Jonas | d1d1921 | 2018-06-01 14:02:24 +1000 | [diff] [blame] | 123 | version_size=$(du -k ${scratch_dir}/VERSION | head -1 | cut -f 1) |
| 124 | magic_number=$(xxd -p -l 4 ${scratch_dir}/VERSION) |
| 125 | # Check if VERSION is signed. A signed version partition will have an extra |
| 126 | # 4K header starting with the magic number 0x17082011, see: |
| 127 | # https://github.com/open-power/skiboot/blob/master/libstb/container.h#L47 |
| 128 | if [ "$version_size" == "8" -a "$magic_number" == "17082011" ]; then |
| 129 | # Advance past the STB header (4K, indexed from 1) |
| 130 | cp ${scratch_dir}/VERSION ${scratch_dir}/VERSION_FULL |
| 131 | tail --bytes=+4097 ${scratch_dir}/VERSION_FULL > ${scratch_dir}/VERSION |
| 132 | fi |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 133 | { |
Gunnar Mills | 2f27b74 | 2017-03-20 13:46:30 -0500 | [diff] [blame] | 134 | version=$(head -n 1 ${scratch_dir}/VERSION) |
| 135 | echo "version=$version" |
Saqib Khan | 029b825 | 2017-03-22 21:38:07 -0500 | [diff] [blame] | 136 | extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',') |
| 137 | echo "extended_version=$extended_version" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 138 | while read line; do |
| 139 | if [[ $line == "ID="* ]]; then |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 140 | # This line looks like |
| 141 | # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]" |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 142 | read -r -a fields <<< "$line" |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 143 | |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 144 | id=${fields[0]##*=} |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 145 | offset=$((${ffs_entry_size} * 10#${id} + ${vercheck_offset})) |
| 146 | vercheck=$(xxd -p -l 0x1 -seek ${offset} ${scratch_dir}/part) |
Michael Tritz | 1bd65ac | 2017-05-07 17:40:14 -0500 | [diff] [blame] | 147 | export flags=$(pflash --detail=$((10#$id)) -F ${pnorfile} | grep "\[" | |
| 148 | sed 's/....$//' | tr '\n' ',' | sed 's/.$//') |
| 149 | if [[ $flags != "" ]]; then |
| 150 | flags=,$flags |
| 151 | fi |
| 152 | |
| 153 | if [[ $(echo $flags | grep "READONLY") == "" && |
| 154 | $(echo $flags | grep "PRESERVED") == "" ]]; then |
| 155 | flags=$flags,READWRITE |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 156 | fi |
| 157 | |
Gunnar Mills | e0ed302 | 2017-03-02 16:16:19 -0600 | [diff] [blame] | 158 | # Need the partition ID, name, start location, end location, and flags |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 159 | echo "partition${id}=${fields[1]},${fields[2]/../,},${vercheck}${flags}" |
Gunnar Mills | d4908a4 | 2017-03-07 08:25:59 -0600 | [diff] [blame] | 160 | |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 161 | # Save the partition name |
| 162 | partitions+=(${fields[1]}) |
| 163 | fi |
Adriana Kobylak | 1e4a7f2 | 2017-07-09 16:12:36 -0500 | [diff] [blame] | 164 | # Don't need the BACKUP_PART partition |
| 165 | done < <(pflash --info -F ${pnorfile} | grep -v "BACKUP") |
Gunnar Mills | 6b5b27e | 2017-03-01 21:37:01 -0600 | [diff] [blame] | 166 | } > ${scratch_dir}/${tocfile} |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 167 | |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 168 | for partition in "${partitions[@]}"; do |
| 169 | echo "Reading ${partition}..." |
Gunnar Mills | f8f1130 | 2017-03-08 13:11:33 -0600 | [diff] [blame] | 170 | pflash --partition=${partition} \ |
| 171 | --read=${scratch_dir}/${partition} \ |
| 172 | -F ${pnorfile} |
Gunnar Mills | 18f7cdb | 2017-02-07 16:44:19 -0600 | [diff] [blame] | 173 | done |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 174 | |
| 175 | echo "Creating SquashFS image..." |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 176 | cd "${scratch_dir}" |
Adriana Kobylak | 9c86606 | 2017-04-13 16:12:32 -0500 | [diff] [blame] | 177 | mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 178 | |
Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 179 | echo "Creating MANIFEST for the image" |
Adriana Kobylak | 9c86606 | 2017-04-13 16:12:32 -0500 | [diff] [blame] | 180 | manifest_location="MANIFEST" |
Leonel Gonzalez | c14a3d2 | 2017-06-22 12:49:38 -0500 | [diff] [blame] | 181 | 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] | 182 | extended_version=$extended_version" >> $manifest_location |
Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 183 | |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 184 | if [[ "${do_sign}" == true ]]; then |
| 185 | private_key_name=$(basename "${private_key_path}") |
| 186 | key_type="${private_key_name%.*}" |
| 187 | echo KeyType="${key_type}" >> $manifest_location |
| 188 | echo HashType="RSA-SHA256" >> $manifest_location |
| 189 | |
| 190 | for file in pnor.xz.squashfs $manifest_location $public_key_file; do |
| 191 | openssl dgst -sha256 -sign ${private_key_path} -out "${file}.sig" $file |
| 192 | done |
| 193 | |
| 194 | additional_files="${public_key_file} *.sig" |
| 195 | fi |
| 196 | |
Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 197 | echo "Generating tarball to contain the SquashFS image and its MANIFEST" |
Eddie James | 643e730 | 2018-02-26 15:23:43 -0600 | [diff] [blame] | 198 | tar -cvf $outfile $manifest_location pnor.xz.squashfs $additional_files |
Saqib Khan | 93433f0 | 2017-03-23 22:24:27 -0500 | [diff] [blame] | 199 | |
| 200 | echo "SquashFSTarball at ${outfile}" |
Gunnar Mills | 37751f9 | 2017-02-07 21:05:01 -0600 | [diff] [blame] | 201 | rm -r "${scratch_dir}" |