Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -eo pipefail |
| 3 | |
| 4 | help=$'Generate Tarball with Bios image and MANIFEST Script |
| 5 | |
| 6 | Generates a Bios image tarball from given file as input. |
| 7 | Creates a MANIFEST for image verification and recreation |
| 8 | Packages the image and MANIFEST together in a tarball |
| 9 | |
| 10 | usage: gen-bios-tar [OPTION] <Bios FILE>... |
| 11 | |
| 12 | Options: |
| 13 | -o, --out <file> Specify destination file. Defaults to |
| 14 | `pwd`/obmc-bios.tar.gz if unspecified. |
| 15 | -s, --sign <path> Sign the image. The optional path argument specifies |
| 16 | the private key file. Defaults to the bash variable |
| 17 | PRIVATE_KEY_PATH if available, or else uses the |
| 18 | open-source private key in this script. |
| 19 | -m, --machine <name> Optionally specify the target machine name of this |
| 20 | image. |
| 21 | -v, --version <name> Specify the version of bios image file |
| 22 | -h, --help Display this help text and exit. |
| 23 | ' |
| 24 | |
| 25 | ################################################################# |
| 26 | # It's the OpenBMC "public" private key (currently under |
| 27 | # meta-phosphor/recipes-phosphor/flash/files/OpenBMC.priv): |
| 28 | # https://gerrit.openbmc-project.xyz/c/openbmc/openbmc/+/8949/15/ |
| 29 | # meta-phosphor/common/recipes-phosphor/flash/files/OpenBMC.priv |
| 30 | # |
| 31 | ################################################################# |
| 32 | private_key=$'-----BEGIN PRIVATE KEY----- |
| 33 | MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri |
| 34 | PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B |
| 35 | zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k |
| 36 | D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU |
| 37 | zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/ |
| 38 | nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1 |
| 39 | h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt |
| 40 | rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE |
| 41 | x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5 |
| 42 | DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw |
| 43 | cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE |
| 44 | 5yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4 |
| 45 | 6ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m |
| 46 | F0nIdUAhR0yTfKM= |
| 47 | -----END PRIVATE KEY----- |
| 48 | ' |
| 49 | |
| 50 | do_sign=false |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 51 | PRIVATE_KEY_PATH=${PRIVATE_KEY_PATH:-} |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 52 | private_key_path="${PRIVATE_KEY_PATH}" |
| 53 | outfile="" |
| 54 | machine="" |
| 55 | version="" |
| 56 | |
| 57 | while [[ $# -gt 0 ]]; do |
| 58 | key="$1" |
| 59 | case $key in |
| 60 | -o|--out) |
| 61 | outfile="$2" |
| 62 | shift 2 |
| 63 | ;; |
| 64 | -s|--sign) |
| 65 | do_sign=true |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 66 | if [[ -n "${2}" && "${2}" != -* ]]; then |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 67 | private_key_path="$2" |
| 68 | shift 2 |
| 69 | else |
| 70 | shift 1 |
| 71 | fi |
| 72 | ;; |
| 73 | -m|--machine) |
| 74 | machine="$2" |
| 75 | shift 2 |
| 76 | ;; |
| 77 | -v|--version) |
| 78 | version="$2" |
| 79 | shift 2 |
| 80 | ;; |
| 81 | -h|--help) |
| 82 | echo "$help" |
| 83 | exit |
| 84 | ;; |
| 85 | -*) |
| 86 | echo "Unrecognised option $1" |
| 87 | echo "$help" |
| 88 | exit |
| 89 | ;; |
| 90 | *) |
| 91 | file="$1" |
| 92 | shift 1 |
| 93 | ;; |
| 94 | esac |
| 95 | done |
| 96 | |
| 97 | if [ ! -f "${file}" ]; then |
| 98 | echo "${file} not found, Please enter a valid Bios image file" |
| 99 | echo "$help" |
| 100 | exit 1 |
| 101 | fi |
| 102 | |
| 103 | if [[ -z $version ]]; then |
| 104 | echo "Please provide version of image with -v option" |
| 105 | exit 1 |
| 106 | fi |
| 107 | |
| 108 | if [[ -z $outfile ]]; then |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 109 | outfile=$(pwd)/obmc-bios.tar.gz |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 110 | else |
| 111 | if [[ $outfile != /* ]]; then |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 112 | outfile=$(pwd)/$outfile |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 113 | fi |
| 114 | fi |
| 115 | |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 116 | scratch_dir=$(mktemp -d) |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 117 | # Remove the temp directory on exit. |
| 118 | # The files in the temp directory may contain read-only files, so add |
| 119 | # --interactive=never to skip the prompt. |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 120 | trap '{ rm -r --interactive=never ${scratch_dir}; }' EXIT |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 121 | |
| 122 | if [[ "${do_sign}" == true ]]; then |
| 123 | if [[ -z "${private_key_path}" ]]; then |
| 124 | private_key_path=${scratch_dir}/OpenBMC.priv |
| 125 | echo "${private_key}" > "${private_key_path}" |
| 126 | echo "Image is NOT secure!! Signing with the open private key!" |
| 127 | else |
| 128 | if [[ ! -f "${private_key_path}" ]]; then |
| 129 | echo "Couldn't find private key ${private_key_path}." |
| 130 | exit 1 |
| 131 | fi |
| 132 | |
| 133 | echo "Signing with ${private_key_path}." |
| 134 | fi |
| 135 | |
| 136 | public_key_file=publickey |
| 137 | public_key_path=${scratch_dir}/$public_key_file |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 138 | openssl pkey -in "${private_key_path}" -pubout -out "${public_key_path}" |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 139 | fi |
| 140 | |
| 141 | manifest_location="MANIFEST" |
| 142 | files_to_sign="$manifest_location $public_key_file" |
| 143 | |
| 144 | # Go to scratch_dir |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 145 | cp "${file}" "${scratch_dir}" |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 146 | cd "${scratch_dir}" |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 147 | files_to_sign+=" $(basename "${file}")" |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 148 | |
| 149 | echo "Creating MANIFEST for the image" |
| 150 | echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\n\ |
| 151 | version=$version" > $manifest_location |
| 152 | |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 153 | if [[ -n "${machine}" ]]; then |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 154 | echo -e "MachineName=${machine}" >> $manifest_location |
| 155 | fi |
| 156 | |
| 157 | if [[ "${do_sign}" == true ]]; then |
| 158 | private_key_name=$(basename "${private_key_path}") |
| 159 | key_type="${private_key_name%.*}" |
| 160 | echo KeyType="${key_type}" >> $manifest_location |
| 161 | echo HashType="RSA-SHA256" >> $manifest_location |
| 162 | |
| 163 | for file in $files_to_sign; do |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 164 | openssl dgst -sha256 -sign "${private_key_path}" -out "${file}.sig" "$file" |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 165 | done |
| 166 | |
| 167 | additional_files="*.sig" |
| 168 | fi |
| 169 | |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 170 | tar -czvf "$outfile" "$files_to_sign" "$additional_files" |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 171 | echo "Bios image tarball is at $outfile" |