blob: 54d1cf3f26c2f01093412cc92736b9bf66835878 [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
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 Williamsa821b892022-12-08 06:23:33 -060022EOF
23)
Chicago Duan99ac18a2019-12-12 15:20:51 +080024
25private_key=$'-----BEGIN PRIVATE KEY-----
26MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri
27PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B
28zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k
29D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU
30zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/
31nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1
32h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt
33rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE
34x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5
35DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw
36cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE
375yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4
386ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m
39F0nIdUAhR0yTfKM=
40-----END PRIVATE KEY-----
41'
42
43do_sign=false
Patrick Williams31905472022-12-05 08:09:44 -060044# shellcheck disable=SC2153
Chicago Duan99ac18a2019-12-12 15:20:51 +080045private_key_path="${PRIVATE_KEY_PATH}"
46image=""
47outfile=""
48version=""
49model=""
50manufacture=""
Chicago Duand48ae5f2020-01-01 16:43:46 +080051machineName=""
Chicago Duan99ac18a2019-12-12 15:20:51 +080052
53
54while [[ $# -gt 0 ]]; do
55 key="$1"
56 case $key in
Chicago Duand48ae5f2020-01-01 16:43:46 +080057 --image)
Chicago Duan99ac18a2019-12-12 15:20:51 +080058 image="$2"
59 shift 2
60 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +080061 --version)
Chicago Duan99ac18a2019-12-12 15:20:51 +080062 version="$2"
63 shift 2
64 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +080065 --model)
Chicago Duan99ac18a2019-12-12 15:20:51 +080066 model="$2"
67 shift 2
68 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +080069 --manufacture)
Chicago Duan99ac18a2019-12-12 15:20:51 +080070 manufacture="$2"
71 shift 2
72 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +080073 --machineName)
74 machineName="$2"
75 shift 2
76 ;;
77 --outfile)
Chicago Duan99ac18a2019-12-12 15:20:51 +080078 outfile="$2"
79 shift 2
80 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +080081 --sign)
Chicago Duan99ac18a2019-12-12 15:20:51 +080082 do_sign=true
Patrick Williams31905472022-12-05 08:09:44 -060083 if [[ -n "${2}" && "${2}" != -* ]]; then
Chicago Duan99ac18a2019-12-12 15:20:51 +080084 private_key_path="$2"
85 shift 2
86 else
87 shift 1
88 fi
89 ;;
Chicago Duand48ae5f2020-01-01 16:43:46 +080090 --help)
Chicago Duan99ac18a2019-12-12 15:20:51 +080091 echo "$help"
92 exit
93 ;;
94 *)
95 echo "Please enter the correct parameters."
96 echo "$help"
97 exit 1
98 ;;
99 esac
100done
101
102if [ ! -f "${image}" ]; then
103 echo "Please enter a valid PSU FW image file."
104 echo "$help"
105 exit 1
106fi
107
108if [ -z "${version}" ]; then
109 echo "Please enter a valid PSU FW image version."
110 echo "$help"
111 exit 1
112fi
113
114
115if [ -z "${model}" ]; then
116 echo "Please enter a valid PSU FW image model."
117 echo "$help"
118 exit 1
119fi
120
121if [ -z "${manufacture}" ]; then
122 echo "Please enter a valid PSU FW image manufacture."
123 echo "$help"
124 exit 1
125fi
126
127if [ -z "${outfile}" ]; then
Patrick Williams31905472022-12-05 08:09:44 -0600128 outfile=$(pwd)/$image.tar
Chicago Duan99ac18a2019-12-12 15:20:51 +0800129else
Patrick Williams31905472022-12-05 08:09:44 -0600130 outfile=$(pwd)/$outfile
Chicago Duan99ac18a2019-12-12 15:20:51 +0800131fi
132
Patrick Williams31905472022-12-05 08:09:44 -0600133scratch_dir=$(mktemp -d)
134# shellcheck disable=SC2064
Chicago Duan99ac18a2019-12-12 15:20:51 +0800135trap "{ rm -r ${scratch_dir}; }" EXIT
136
137if [[ "${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 Williams31905472022-12-05 08:09:44 -0600155 cp "${private_key_path}" "${scratch_dir}/private_key"
Chicago Duan99ac18a2019-12-12 15:20:51 +0800156
157fi
158
159manifest_location="MANIFEST"
160files_to_sign="$manifest_location $public_key_file $image"
161
Patrick Williams31905472022-12-05 08:09:44 -0600162cp "${image}" "${scratch_dir}"
Chicago Duan99ac18a2019-12-12 15:20:51 +0800163cd "${scratch_dir}"
164
165echo "Creating MANIFEST for the image"
166echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU\nversion=$version\n\
167extended_version=model=$model,manufacture=$manufacture" > $manifest_location
168
Patrick Williams31905472022-12-05 08:09:44 -0600169if [[ -n "${machineName}" ]]; then
Chicago Duand48ae5f2020-01-01 16:43:46 +0800170 echo -e "MachineName=${machineName}" >> $manifest_location
171fi
172
Chicago Duan99ac18a2019-12-12 15:20:51 +0800173if [[ "${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 Williams31905472022-12-05 08:09:44 -0600180 openssl dgst -sha256 -sign private_key -out "${file}.sig" "$file"
Chicago Duan99ac18a2019-12-12 15:20:51 +0800181 done
182
183 additional_files="*.sig"
184fi
185
Patrick Williams31905472022-12-05 08:09:44 -0600186tar -cvf "$outfile" "$files_to_sign" "$additional_files"
Chicago Duan99ac18a2019-12-12 15:20:51 +0800187echo "PSU FW tarball at $outfile"
188exit