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----- |
Rashmica Gupta | e2dac25 | 2024-07-18 18:01:32 +1000 | [diff] [blame] | 36 | MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQDO7vWM6ZOylO6T |
| 37 | lxDiRWgKCFauAxMVM4A7NmgZxfV73xqTzAtIzzF9CIKUEhqMT4LhNy1rU2oUUivH |
| 38 | 4BZO2oC6yFafEPVla2oYAeWtXJmQTixYgJplKQCtLzFtb57DQJpl9Od0RVFC61yg |
| 39 | 4LihsiENnRjncLPP7OkL68ssiELu+WxazDtewmVYQEHOMWQa2zDh9yrWsblLAyo4 |
| 40 | gsG9rdwWjqIUnZkoeJuT7zks4Jes4qAtQUuZhCxMcvwvOq8od3e4nLVFnyUOGMpE |
| 41 | jdDDGddKh5e+9BJGqkfsLCT9UFTYi62Ifuxl1Gp963cZLapJS0CneHIrG3gapiCO |
| 42 | ea4TH/xySfts2jpl7DnEIUo3Qs0rZrOLQDLVVXvRJvHdsVoZcSRCD8Jf0YTQ6tT7 |
| 43 | j3ejUOH8j3DGkgKRHGtH7yyCgmDyPYVQdEu3q4E1NBwODAyxppYGBqkwpSGRnf6q |
| 44 | E6i5rQJuoPCm3rb9ZmoHVzeb8HW3ofYfNnZNuae/2LdEtJ/cFs8ILXP5AKiaKpmS |
| 45 | H4GB8GUcgRMITGI1pcXhevO2wHi0ReJkme+m5GpuVIg9LpTs6YvvNoFeu6ux7up/ |
| 46 | TjTa+Zdy3tLUZvvIIlS2Asu32uiGBFjZ2WiLeHq+ScaDsSyEJQkoYLqv+3MoPKhy |
| 47 | iX4yanZCwG3yx68RDe9qQBt6WXqAEwIDAQABAoICAA6OxAqKQiA9lv0eEwuAC34t |
| 48 | MP/j6ntC2MIRpUgu44K34tRD9gVEwjwEFb+Z+HEnhNMYQSM8RomwcDELBDa+63B4 |
| 49 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vWQCoop |
| 64 | Loh5s5tYwHB/iFE+jzVvyrytfRcDIjrBaCDCQrObgh/fllk44h9DpKHbM4nYlMpU |
| 65 | GXOsQCVkE/SiJakzeXfoecCJ3ebp+mlBILs79AWlfETJFLx1FKJQOQoraoICVFxx |
| 66 | jcI/OvGlABRLvRYhNnq22//IgLlyKWfO0yS6FASIjriIjDwibKZqHt+DJ98Lv2d+ |
| 67 | 1fsOw/Ai7e2e3nK0+2vmXusZAwpcPuTYb+5LOnHE7GbPecuM22BSTiUAekoHq3/k |
| 68 | fdYw+od4B4BJB0bAnRVu7y+TQXRkZ462RvP05AYXUzudjknv48mFuBslrybr3f50 |
| 69 | aIwtFADeJrHbAoIBAQCYcEwnabaWZrjX+BZoiFd58eQMNNugrI7fzi06vrDJFdCf |
| 70 | AY0NGRoAxPlvFTmTIOaZ+LO9bd5r60FMeiLBAwhLoKdv+HEOsysXXVhunM1FOKFA |
| 71 | 69rLvuJSlGmt0x/b35BZOABPNTSRD+15vVlrr75BmbL1kl2/BrcGJ0qwuOHS62KY |
| 72 | IziDXejpCqV7UuAlfmqs1eYSnn3RdoFy0yTYcphVIQXlPSqPl5PQI5LyamRtcpp2 |
| 73 | Rx8ko9W4MneIUzmCbJA5iCQxny5aRWZsAXg4qwYn9JAGJRGMGQdFdsirkKRcxNvK |
| 74 | 6zz+xydNm8gHmy7wK3QBlVtVnJxmKwDQI9zHTQYJAoIBAQDVKrXJ81zv9r1/3U8V |
| 75 | 5N5MnACL/VtfW9FJYHU1ywR7XSrEAAHdGa42dwUcX++YJ0ji0YgRNFNsWTzesdVD |
| 76 | lemsyQgIduIiPcUtKL9lWZOTu3SVasSurVLstllj1/DERDnUR4/o8ZUJ6+2Bddn0 |
| 77 | xvUDPKX9UH+rGSx4tnscA5+CnYJsJeSdunvYONTRxBsn0l6iJhhn/gPOOpsHtKnL |
| 78 | hS9y/vfd3GFDST33L23EsFa3a7xwgdY460D8AIgnGij7V9Lgel0AyYp0ovZc34uD |
| 79 | z9yYWI32dbRWbMZ40RPKaudOeDSbjlMaH0A7ymfxjqwKxI9D2VscFWNs4hv8QErw |
| 80 | Uc6NAoIBAQCmWWyARU/x4m7K4vNAWjD2Qx6R7PAdLs/6ZBqWw0RSpRHlYr73Qggw |
| 81 | dCK+LrsR0O7y1KW2WUfrJmzHEdFQEYcZ1vZWeN85dMLVhYmazSXlaIegRE4FmMUa |
| 82 | DbzViUJA60Y4D/l6QWqdhxdZZe81QgqyLPXAv/e5esxRIi8yvEYhCx4pq7QtWYBm |
| 83 | tvQnPaZd8emlKARivF2ecGDlWzhf4NotDDtFRT4jOHZKUC58uVjbiXJ445Vimqlb |
| 84 | Da7noVGwDQ93Ib1qyAilzFY5gWDeMyCnSQpnlVRHQ/8vlwLDsZs1kVau6k8WlcpM |
| 85 | JbmuCRNy7YvALVTzyQsQ4yw87BoONt1L |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 86 | -----END PRIVATE KEY----- |
| 87 | ' |
| 88 | |
| 89 | do_sign=false |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 90 | PRIVATE_KEY_PATH=${PRIVATE_KEY_PATH:-} |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 91 | private_key_path="${PRIVATE_KEY_PATH}" |
| 92 | outfile="" |
| 93 | machine="" |
| 94 | version="" |
| 95 | |
| 96 | while [[ $# -gt 0 ]]; do |
| 97 | key="$1" |
| 98 | case $key in |
| 99 | -o|--out) |
| 100 | outfile="$2" |
| 101 | shift 2 |
| 102 | ;; |
| 103 | -s|--sign) |
| 104 | do_sign=true |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 105 | if [[ -n "${2}" && "${2}" != -* ]]; then |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 106 | private_key_path="$2" |
| 107 | shift 2 |
| 108 | else |
| 109 | shift 1 |
| 110 | fi |
| 111 | ;; |
| 112 | -m|--machine) |
| 113 | machine="$2" |
| 114 | shift 2 |
| 115 | ;; |
| 116 | -v|--version) |
| 117 | version="$2" |
| 118 | shift 2 |
| 119 | ;; |
Glukhov Mikhail | 3363f65 | 2023-02-15 12:31:38 +0300 | [diff] [blame] | 120 | -e|--extended) |
| 121 | extended="$2" |
| 122 | shift 2 |
| 123 | ;; |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 124 | -h|--help) |
| 125 | echo "$help" |
| 126 | exit |
| 127 | ;; |
| 128 | -*) |
| 129 | echo "Unrecognised option $1" |
| 130 | echo "$help" |
| 131 | exit |
| 132 | ;; |
| 133 | *) |
| 134 | file="$1" |
| 135 | shift 1 |
| 136 | ;; |
| 137 | esac |
| 138 | done |
| 139 | |
| 140 | if [ ! -f "${file}" ]; then |
| 141 | echo "${file} not found, Please enter a valid Bios image file" |
| 142 | echo "$help" |
| 143 | exit 1 |
| 144 | fi |
| 145 | |
| 146 | if [[ -z $version ]]; then |
| 147 | echo "Please provide version of image with -v option" |
| 148 | exit 1 |
| 149 | fi |
| 150 | |
| 151 | if [[ -z $outfile ]]; then |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 152 | outfile=$(pwd)/obmc-bios.tar.gz |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 153 | else |
| 154 | if [[ $outfile != /* ]]; then |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 155 | outfile=$(pwd)/$outfile |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 156 | fi |
| 157 | fi |
| 158 | |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 159 | scratch_dir=$(mktemp -d) |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 160 | # Remove the temp directory on exit. |
| 161 | # The files in the temp directory may contain read-only files, so add |
| 162 | # --interactive=never to skip the prompt. |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 163 | trap '{ rm -r --interactive=never ${scratch_dir}; }' EXIT |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 164 | |
| 165 | if [[ "${do_sign}" == true ]]; then |
| 166 | if [[ -z "${private_key_path}" ]]; then |
| 167 | private_key_path=${scratch_dir}/OpenBMC.priv |
| 168 | echo "${private_key}" > "${private_key_path}" |
| 169 | echo "Image is NOT secure!! Signing with the open private key!" |
| 170 | else |
| 171 | if [[ ! -f "${private_key_path}" ]]; then |
| 172 | echo "Couldn't find private key ${private_key_path}." |
| 173 | exit 1 |
| 174 | fi |
| 175 | |
| 176 | echo "Signing with ${private_key_path}." |
| 177 | fi |
| 178 | |
| 179 | public_key_file=publickey |
| 180 | public_key_path=${scratch_dir}/$public_key_file |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 181 | openssl pkey -in "${private_key_path}" -pubout -out "${public_key_path}" |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 182 | fi |
| 183 | |
| 184 | manifest_location="MANIFEST" |
| 185 | files_to_sign="$manifest_location $public_key_file" |
| 186 | |
| 187 | # Go to scratch_dir |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 188 | cp "${file}" "${scratch_dir}" |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 189 | cd "${scratch_dir}" |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 190 | files_to_sign+=" $(basename "${file}")" |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 191 | |
| 192 | echo "Creating MANIFEST for the image" |
| 193 | echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\n\ |
| 194 | version=$version" > $manifest_location |
| 195 | |
Glukhov Mikhail | 3363f65 | 2023-02-15 12:31:38 +0300 | [diff] [blame] | 196 | if [[ -n "${extended}" ]]; then |
| 197 | echo -e "ExtendedVersion=\"${extended}\"" >> $manifest_location |
| 198 | fi |
| 199 | |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 200 | if [[ -n "${machine}" ]]; then |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 201 | echo -e "MachineName=${machine}" >> $manifest_location |
| 202 | fi |
| 203 | |
| 204 | if [[ "${do_sign}" == true ]]; then |
| 205 | private_key_name=$(basename "${private_key_path}") |
| 206 | key_type="${private_key_name%.*}" |
| 207 | echo KeyType="${key_type}" >> $manifest_location |
| 208 | echo HashType="RSA-SHA256" >> $manifest_location |
| 209 | |
| 210 | for file in $files_to_sign; do |
Isaac Kurth | a1d2f86 | 2022-01-31 14:10:31 -0600 | [diff] [blame] | 211 | openssl dgst -sha256 -sign "${private_key_path}" -out "${file}.sig" "$file" |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 212 | done |
| 213 | |
| 214 | additional_files="*.sig" |
| 215 | fi |
| 216 | |
Glukhov Mikhail | 124e63b | 2022-08-04 10:16:29 +0300 | [diff] [blame] | 217 | # shellcheck disable=SC2086 |
| 218 | # Do not quote the files variables since they list multiple files |
| 219 | # and tar would assume to be a single file name within quotes |
| 220 | tar -czvf $outfile $files_to_sign $additional_files |
Vijay Khemka | 1891869 | 2020-02-12 14:23:40 -0800 | [diff] [blame] | 221 | echo "Bios image tarball is at $outfile" |