blob: db9bef8319cacc18fcd63b0f4a4ffb41c3b0dedd [file] [log] [blame]
Chicago Duan99ac18a2019-12-12 15:20:51 +08001#!/bin/bash
2set -eo pipefail
3
Patrick Williamsa821b892022-12-08 06:23:33 -06004help=$(cat <<EOF
5Generate Tarball with PSU image and MANIFEST Script
Chicago Duan99ac18a2019-12-12 15:20:51 +08006usage: generate-psu-tar [OPTION] <parameter>...
7Options:
Chicago Duand48ae5f2020-01-01 16:43:46 +08008 --image <file> PSU FW image
9 --version <version> PSU FW version
10 --model <model> PSU FW model
Chicago Duan4ba2edc2023-01-06 16:10:59 +080011 --manufacturer <version> PSU FW manufacturer
Chicago Duand48ae5f2020-01-01 16:43:46 +080012 --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 Williamsa821b892022-12-08 06:23:33 -060022EOF
23)
Chicago Duan99ac18a2019-12-12 15:20:51 +080024
25private_key=$'-----BEGIN PRIVATE KEY-----
Rashmica Gupta935f5512024-07-29 16:19:02 +100026MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQDO7vWM6ZOylO6T
27lxDiRWgKCFauAxMVM4A7NmgZxfV73xqTzAtIzzF9CIKUEhqMT4LhNy1rU2oUUivH
284BZO2oC6yFafEPVla2oYAeWtXJmQTixYgJplKQCtLzFtb57DQJpl9Od0RVFC61yg
294LihsiENnRjncLPP7OkL68ssiELu+WxazDtewmVYQEHOMWQa2zDh9yrWsblLAyo4
30gsG9rdwWjqIUnZkoeJuT7zks4Jes4qAtQUuZhCxMcvwvOq8od3e4nLVFnyUOGMpE
31jdDDGddKh5e+9BJGqkfsLCT9UFTYi62Ifuxl1Gp963cZLapJS0CneHIrG3gapiCO
32ea4TH/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AoIBAQDVKrXJ81zv9r1/3U8V
655N5MnACL/VtfW9FJYHU1ywR7XSrEAAHdGa42dwUcX++YJ0ji0YgRNFNsWTzesdVD
66lemsyQgIduIiPcUtKL9lWZOTu3SVasSurVLstllj1/DERDnUR4/o8ZUJ6+2Bddn0
67xvUDPKX9UH+rGSx4tnscA5+CnYJsJeSdunvYONTRxBsn0l6iJhhn/gPOOpsHtKnL
68hS9y/vfd3GFDST33L23EsFa3a7xwgdY460D8AIgnGij7V9Lgel0AyYp0ovZc34uD
69z9yYWI32dbRWbMZ40RPKaudOeDSbjlMaH0A7ymfxjqwKxI9D2VscFWNs4hv8QErw
70Uc6NAoIBAQCmWWyARU/x4m7K4vNAWjD2Qx6R7PAdLs/6ZBqWw0RSpRHlYr73Qggw
71dCK+LrsR0O7y1KW2WUfrJmzHEdFQEYcZ1vZWeN85dMLVhYmazSXlaIegRE4FmMUa
72DbzViUJA60Y4D/l6QWqdhxdZZe81QgqyLPXAv/e5esxRIi8yvEYhCx4pq7QtWYBm
73tvQnPaZd8emlKARivF2ecGDlWzhf4NotDDtFRT4jOHZKUC58uVjbiXJ445Vimqlb
74Da7noVGwDQ93Ib1qyAilzFY5gWDeMyCnSQpnlVRHQ/8vlwLDsZs1kVau6k8WlcpM
75JbmuCRNy7YvALVTzyQsQ4yw87BoONt1L
Chicago Duan99ac18a2019-12-12 15:20:51 +080076-----END PRIVATE KEY-----
77'
78
79do_sign=false
Patrick Williams31905472022-12-05 08:09:44 -060080# shellcheck disable=SC2153
Chicago Duan99ac18a2019-12-12 15:20:51 +080081private_key_path="${PRIVATE_KEY_PATH}"
82image=""
83outfile=""
84version=""
85model=""
Chicago Duan4ba2edc2023-01-06 16:10:59 +080086manufacturer=""
Chicago Duand48ae5f2020-01-01 16:43:46 +080087machineName=""
Chicago Duan99ac18a2019-12-12 15:20:51 +080088
89
90while [[ $# -gt 0 ]]; do
91 key="$1"
92 case $key in
Chicago Duand48ae5f2020-01-01 16:43:46 +080093 --image)
Chicago Duan99ac18a2019-12-12 15:20:51 +080094 image="$2"
95 shift 2
96 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +080097 --version)
Chicago Duan99ac18a2019-12-12 15:20:51 +080098 version="$2"
99 shift 2
100 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +0800101 --model)
Chicago Duan99ac18a2019-12-12 15:20:51 +0800102 model="$2"
103 shift 2
104 ;;
Chicago Duan4ba2edc2023-01-06 16:10:59 +0800105 --manufacturer)
106 manufacturer="$2"
Chicago Duan99ac18a2019-12-12 15:20:51 +0800107 shift 2
108 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +0800109 --machineName)
110 machineName="$2"
111 shift 2
112 ;;
113 --outfile)
Chicago Duan99ac18a2019-12-12 15:20:51 +0800114 outfile="$2"
115 shift 2
116 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +0800117 --sign)
Chicago Duan99ac18a2019-12-12 15:20:51 +0800118 do_sign=true
Patrick Williams31905472022-12-05 08:09:44 -0600119 if [[ -n "${2}" && "${2}" != -* ]]; then
Chicago Duan99ac18a2019-12-12 15:20:51 +0800120 private_key_path="$2"
121 shift 2
122 else
123 shift 1
124 fi
125 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +0800126 --help)
Chicago Duan99ac18a2019-12-12 15:20:51 +0800127 echo "$help"
128 exit
129 ;;
130 *)
131 echo "Please enter the correct parameters."
132 echo "$help"
133 exit 1
134 ;;
135 esac
136done
137
138if [ ! -f "${image}" ]; then
139 echo "Please enter a valid PSU FW image file."
140 echo "$help"
141 exit 1
142fi
143
144if [ -z "${version}" ]; then
145 echo "Please enter a valid PSU FW image version."
146 echo "$help"
147 exit 1
148fi
149
150
151if [ -z "${model}" ]; then
152 echo "Please enter a valid PSU FW image model."
153 echo "$help"
154 exit 1
155fi
156
Chicago Duan4ba2edc2023-01-06 16:10:59 +0800157if [ -z "${manufacturer}" ]; then
158 echo "Please enter a valid PSU FW image manufacturer."
Chicago Duan99ac18a2019-12-12 15:20:51 +0800159 echo "$help"
160 exit 1
161fi
162
163if [ -z "${outfile}" ]; then
Patrick Williams31905472022-12-05 08:09:44 -0600164 outfile=$(pwd)/$image.tar
Chicago Duan99ac18a2019-12-12 15:20:51 +0800165else
Patrick Williams31905472022-12-05 08:09:44 -0600166 outfile=$(pwd)/$outfile
Chicago Duan99ac18a2019-12-12 15:20:51 +0800167fi
168
Patrick Williams31905472022-12-05 08:09:44 -0600169scratch_dir=$(mktemp -d)
170# shellcheck disable=SC2064
Chicago Duan99ac18a2019-12-12 15:20:51 +0800171trap "{ rm -r ${scratch_dir}; }" EXIT
172
173if [[ "${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 Williams31905472022-12-05 08:09:44 -0600191 cp "${private_key_path}" "${scratch_dir}/private_key"
Chicago Duan99ac18a2019-12-12 15:20:51 +0800192
193fi
194
195manifest_location="MANIFEST"
196files_to_sign="$manifest_location $public_key_file $image"
197
Patrick Williams31905472022-12-05 08:09:44 -0600198cp "${image}" "${scratch_dir}"
Chicago Duan99ac18a2019-12-12 15:20:51 +0800199cd "${scratch_dir}"
200
201echo "Creating MANIFEST for the image"
202echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU\nversion=$version\n\
Chicago Duan4ba2edc2023-01-06 16:10:59 +0800203extended_version=model=$model,manufacturer=$manufacturer" > $manifest_location
Chicago Duan99ac18a2019-12-12 15:20:51 +0800204
Patrick Williams31905472022-12-05 08:09:44 -0600205if [[ -n "${machineName}" ]]; then
Chicago Duand48ae5f2020-01-01 16:43:46 +0800206 echo -e "MachineName=${machineName}" >> $manifest_location
207fi
208
Chicago Duan99ac18a2019-12-12 15:20:51 +0800209if [[ "${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 Williams31905472022-12-05 08:09:44 -0600216 openssl dgst -sha256 -sign private_key -out "${file}.sig" "$file"
Chicago Duan99ac18a2019-12-12 15:20:51 +0800217 done
218
219 additional_files="*.sig"
220fi
221
Chicago Duanb58af7e2023-01-03 14:08:29 +0800222# 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
225tar -cvf $outfile $files_to_sign $additional_files
Chicago Duan99ac18a2019-12-12 15:20:51 +0800226echo "PSU FW tarball at $outfile"
227exit