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