Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -eo pipefail |
| 3 | |
Patrick Williams | a821b89 | 2022-12-08 06:23:33 -0600 | [diff] [blame] | 4 | help=$(cat <<EOF |
| 5 | Generate Tarball with PSU image and MANIFEST Script |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 6 | usage: generate-psu-tar [OPTION] <parameter>... |
| 7 | Options: |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 8 | --image <file> PSU FW image |
| 9 | --version <version> PSU FW version |
| 10 | --model <model> PSU FW model |
| 11 | --manufacture <version> PSU FW manufacture |
| 12 | --machineName <machineName> Optionally specify the target machine name of this image. |
| 13 | --outfile <filename> Outfile name |
| 14 | For example : -o psufw.tar |
| 15 | The default outfile name is image.tar,and |
| 16 | "image" is what you input. |
| 17 | --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. |
| 21 | --help Display this help text and exit. |
Patrick Williams | a821b89 | 2022-12-08 06:23:33 -0600 | [diff] [blame] | 22 | EOF |
| 23 | ) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 24 | |
| 25 | private_key=$'-----BEGIN PRIVATE KEY----- |
| 26 | MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri |
| 27 | PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B |
| 28 | zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k |
| 29 | D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU |
| 30 | zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/ |
| 31 | nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1 |
| 32 | h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt |
| 33 | rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE |
| 34 | x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5 |
| 35 | DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw |
| 36 | cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE |
| 37 | 5yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4 |
| 38 | 6ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m |
| 39 | F0nIdUAhR0yTfKM= |
| 40 | -----END PRIVATE KEY----- |
| 41 | ' |
| 42 | |
| 43 | do_sign=false |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 44 | # shellcheck disable=SC2153 |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 45 | private_key_path="${PRIVATE_KEY_PATH}" |
| 46 | image="" |
| 47 | outfile="" |
| 48 | version="" |
| 49 | model="" |
| 50 | manufacture="" |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 51 | machineName="" |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 52 | |
| 53 | |
| 54 | while [[ $# -gt 0 ]]; do |
| 55 | key="$1" |
| 56 | case $key in |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 57 | --image) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 58 | image="$2" |
| 59 | shift 2 |
| 60 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 61 | --version) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 62 | version="$2" |
| 63 | shift 2 |
| 64 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 65 | --model) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 66 | model="$2" |
| 67 | shift 2 |
| 68 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 69 | --manufacture) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 70 | manufacture="$2" |
| 71 | shift 2 |
| 72 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 73 | --machineName) |
| 74 | machineName="$2" |
| 75 | shift 2 |
| 76 | ;; |
| 77 | --outfile) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 78 | outfile="$2" |
| 79 | shift 2 |
| 80 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 81 | --sign) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 82 | do_sign=true |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 83 | if [[ -n "${2}" && "${2}" != -* ]]; then |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 84 | private_key_path="$2" |
| 85 | shift 2 |
| 86 | else |
| 87 | shift 1 |
| 88 | fi |
| 89 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 90 | --help) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 91 | echo "$help" |
| 92 | exit |
| 93 | ;; |
| 94 | *) |
| 95 | echo "Please enter the correct parameters." |
| 96 | echo "$help" |
| 97 | exit 1 |
| 98 | ;; |
| 99 | esac |
| 100 | done |
| 101 | |
| 102 | if [ ! -f "${image}" ]; then |
| 103 | echo "Please enter a valid PSU FW image file." |
| 104 | echo "$help" |
| 105 | exit 1 |
| 106 | fi |
| 107 | |
| 108 | if [ -z "${version}" ]; then |
| 109 | echo "Please enter a valid PSU FW image version." |
| 110 | echo "$help" |
| 111 | exit 1 |
| 112 | fi |
| 113 | |
| 114 | |
| 115 | if [ -z "${model}" ]; then |
| 116 | echo "Please enter a valid PSU FW image model." |
| 117 | echo "$help" |
| 118 | exit 1 |
| 119 | fi |
| 120 | |
| 121 | if [ -z "${manufacture}" ]; then |
| 122 | echo "Please enter a valid PSU FW image manufacture." |
| 123 | echo "$help" |
| 124 | exit 1 |
| 125 | fi |
| 126 | |
| 127 | if [ -z "${outfile}" ]; then |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 128 | outfile=$(pwd)/$image.tar |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 129 | else |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 130 | outfile=$(pwd)/$outfile |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 131 | fi |
| 132 | |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 133 | scratch_dir=$(mktemp -d) |
| 134 | # shellcheck disable=SC2064 |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 135 | trap "{ rm -r ${scratch_dir}; }" EXIT |
| 136 | |
| 137 | if [[ "${do_sign}" == true ]]; then |
| 138 | if [[ -z "${private_key_path}" ]]; then |
| 139 | private_key_path=${scratch_dir}/OpenBMC.priv |
| 140 | echo "${private_key}" > "${private_key_path}" |
| 141 | echo "Image is NOT secure!! Signing with the open private key!" |
| 142 | else |
| 143 | if [[ ! -f "${private_key_path}" ]]; then |
| 144 | echo "Couldn't find private key ${private_key_path}." |
| 145 | exit 1 |
| 146 | fi |
| 147 | |
| 148 | echo "Signing with ${private_key_path}." |
| 149 | fi |
| 150 | |
| 151 | public_key_file=publickey |
| 152 | public_key_path=${scratch_dir}/$public_key_file |
| 153 | openssl pkey -in "${private_key_path}" -pubout -out "${public_key_path}" |
| 154 | |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 155 | cp "${private_key_path}" "${scratch_dir}/private_key" |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 156 | |
| 157 | fi |
| 158 | |
| 159 | manifest_location="MANIFEST" |
| 160 | files_to_sign="$manifest_location $public_key_file $image" |
| 161 | |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 162 | cp "${image}" "${scratch_dir}" |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 163 | cd "${scratch_dir}" |
| 164 | |
| 165 | echo "Creating MANIFEST for the image" |
| 166 | echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU\nversion=$version\n\ |
| 167 | extended_version=model=$model,manufacture=$manufacture" > $manifest_location |
| 168 | |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 169 | if [[ -n "${machineName}" ]]; then |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 170 | echo -e "MachineName=${machineName}" >> $manifest_location |
| 171 | fi |
| 172 | |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 173 | if [[ "${do_sign}" == true ]]; then |
| 174 | private_key_name=$(basename "${private_key_path}") |
| 175 | key_type="${private_key_name%.*}" |
| 176 | echo KeyType="${key_type}" >> $manifest_location |
| 177 | echo HashType="RSA-SHA256" >> $manifest_location |
| 178 | |
| 179 | for file in $files_to_sign; do |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 180 | openssl dgst -sha256 -sign private_key -out "${file}.sig" "$file" |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 181 | done |
| 182 | |
| 183 | additional_files="*.sig" |
| 184 | fi |
| 185 | |
Chicago Duan | b58af7e | 2023-01-03 14:08:29 +0800 | [diff] [blame^] | 186 | # shellcheck disable=SC2086 |
| 187 | # Do not quote the files variables since they list multiple files |
| 188 | # and tar would assume to be a single file name within quotes |
| 189 | tar -cvf $outfile $files_to_sign $additional_files |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 190 | echo "PSU FW tarball at $outfile" |
| 191 | exit |