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 |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 11 | --manufacturer <version> PSU FW manufacturer |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 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----- |
Rashmica Gupta | 935f551 | 2024-07-29 16:19:02 +1000 | [diff] [blame^] | 26 | MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQDO7vWM6ZOylO6T |
| 27 | lxDiRWgKCFauAxMVM4A7NmgZxfV73xqTzAtIzzF9CIKUEhqMT4LhNy1rU2oUUivH |
| 28 | 4BZO2oC6yFafEPVla2oYAeWtXJmQTix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NotDDtFRT4jOHZKUC58uVjbiXJ445Vimqlb |
| 74 | Da7noVGwDQ93Ib1qyAilzFY5gWDeMyCnSQpnlVRHQ/8vlwLDsZs1kVau6k8WlcpM |
| 75 | JbmuCRNy7YvALVTzyQsQ4yw87BoONt1L |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 76 | -----END PRIVATE KEY----- |
| 77 | ' |
| 78 | |
| 79 | do_sign=false |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 80 | # shellcheck disable=SC2153 |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 81 | private_key_path="${PRIVATE_KEY_PATH}" |
| 82 | image="" |
| 83 | outfile="" |
| 84 | version="" |
| 85 | model="" |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 86 | manufacturer="" |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 87 | machineName="" |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 88 | |
| 89 | |
| 90 | while [[ $# -gt 0 ]]; do |
| 91 | key="$1" |
| 92 | case $key in |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 93 | --image) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 94 | image="$2" |
| 95 | shift 2 |
| 96 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 97 | --version) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 98 | version="$2" |
| 99 | shift 2 |
| 100 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 101 | --model) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 102 | model="$2" |
| 103 | shift 2 |
| 104 | ;; |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 105 | --manufacturer) |
| 106 | manufacturer="$2" |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 107 | shift 2 |
| 108 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 109 | --machineName) |
| 110 | machineName="$2" |
| 111 | shift 2 |
| 112 | ;; |
| 113 | --outfile) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 114 | outfile="$2" |
| 115 | shift 2 |
| 116 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 117 | --sign) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 118 | do_sign=true |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 119 | if [[ -n "${2}" && "${2}" != -* ]]; then |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 120 | private_key_path="$2" |
| 121 | shift 2 |
| 122 | else |
| 123 | shift 1 |
| 124 | fi |
| 125 | ;; |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 126 | --help) |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 127 | echo "$help" |
| 128 | exit |
| 129 | ;; |
| 130 | *) |
| 131 | echo "Please enter the correct parameters." |
| 132 | echo "$help" |
| 133 | exit 1 |
| 134 | ;; |
| 135 | esac |
| 136 | done |
| 137 | |
| 138 | if [ ! -f "${image}" ]; then |
| 139 | echo "Please enter a valid PSU FW image file." |
| 140 | echo "$help" |
| 141 | exit 1 |
| 142 | fi |
| 143 | |
| 144 | if [ -z "${version}" ]; then |
| 145 | echo "Please enter a valid PSU FW image version." |
| 146 | echo "$help" |
| 147 | exit 1 |
| 148 | fi |
| 149 | |
| 150 | |
| 151 | if [ -z "${model}" ]; then |
| 152 | echo "Please enter a valid PSU FW image model." |
| 153 | echo "$help" |
| 154 | exit 1 |
| 155 | fi |
| 156 | |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 157 | if [ -z "${manufacturer}" ]; then |
| 158 | echo "Please enter a valid PSU FW image manufacturer." |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 159 | echo "$help" |
| 160 | exit 1 |
| 161 | fi |
| 162 | |
| 163 | if [ -z "${outfile}" ]; then |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 164 | outfile=$(pwd)/$image.tar |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 165 | else |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 166 | outfile=$(pwd)/$outfile |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 167 | fi |
| 168 | |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 169 | scratch_dir=$(mktemp -d) |
| 170 | # shellcheck disable=SC2064 |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 171 | trap "{ rm -r ${scratch_dir}; }" EXIT |
| 172 | |
| 173 | if [[ "${do_sign}" == true ]]; then |
| 174 | if [[ -z "${private_key_path}" ]]; then |
| 175 | private_key_path=${scratch_dir}/OpenBMC.priv |
| 176 | echo "${private_key}" > "${private_key_path}" |
| 177 | echo "Image is NOT secure!! Signing with the open private key!" |
| 178 | else |
| 179 | if [[ ! -f "${private_key_path}" ]]; then |
| 180 | echo "Couldn't find private key ${private_key_path}." |
| 181 | exit 1 |
| 182 | fi |
| 183 | |
| 184 | echo "Signing with ${private_key_path}." |
| 185 | fi |
| 186 | |
| 187 | public_key_file=publickey |
| 188 | public_key_path=${scratch_dir}/$public_key_file |
| 189 | openssl pkey -in "${private_key_path}" -pubout -out "${public_key_path}" |
| 190 | |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 191 | cp "${private_key_path}" "${scratch_dir}/private_key" |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 192 | |
| 193 | fi |
| 194 | |
| 195 | manifest_location="MANIFEST" |
| 196 | files_to_sign="$manifest_location $public_key_file $image" |
| 197 | |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 198 | cp "${image}" "${scratch_dir}" |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 199 | cd "${scratch_dir}" |
| 200 | |
| 201 | echo "Creating MANIFEST for the image" |
| 202 | echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU\nversion=$version\n\ |
Chicago Duan | 4ba2edc | 2023-01-06 16:10:59 +0800 | [diff] [blame] | 203 | extended_version=model=$model,manufacturer=$manufacturer" > $manifest_location |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 204 | |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 205 | if [[ -n "${machineName}" ]]; then |
Chicago Duan | d48ae5f | 2020-01-01 16:43:46 +0800 | [diff] [blame] | 206 | echo -e "MachineName=${machineName}" >> $manifest_location |
| 207 | fi |
| 208 | |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 209 | if [[ "${do_sign}" == true ]]; then |
| 210 | private_key_name=$(basename "${private_key_path}") |
| 211 | key_type="${private_key_name%.*}" |
| 212 | echo KeyType="${key_type}" >> $manifest_location |
| 213 | echo HashType="RSA-SHA256" >> $manifest_location |
| 214 | |
| 215 | for file in $files_to_sign; do |
Patrick Williams | 3190547 | 2022-12-05 08:09:44 -0600 | [diff] [blame] | 216 | openssl dgst -sha256 -sign private_key -out "${file}.sig" "$file" |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 217 | done |
| 218 | |
| 219 | additional_files="*.sig" |
| 220 | fi |
| 221 | |
Chicago Duan | b58af7e | 2023-01-03 14:08:29 +0800 | [diff] [blame] | 222 | # shellcheck disable=SC2086 |
| 223 | # Do not quote the files variables since they list multiple files |
| 224 | # and tar would assume to be a single file name within quotes |
| 225 | tar -cvf $outfile $files_to_sign $additional_files |
Chicago Duan | 99ac18a | 2019-12-12 15:20:51 +0800 | [diff] [blame] | 226 | echo "PSU FW tarball at $outfile" |
| 227 | exit |