-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgen-bios-tar
executable file
·221 lines (198 loc) · 7.68 KB
/
gen-bios-tar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash
set -eo pipefail
help=$(cat <<EOF
Generate Tarball with Bios image and MANIFEST Script
Generates a Bios image tarball from given file as input.
Creates a MANIFEST for image verification and recreation
Packages the image and MANIFEST together in a tarball
usage: gen-bios-tar [OPTION] <Bios FILE>...
Options:
-o, --out <file> Specify destination file. Defaults to
$(pwd)/obmc-bios.tar.gz if unspecified.
-s, --sign <path> Sign the image. The optional path argument specifies
the private key file. Defaults to the bash variable
PRIVATE_KEY_PATH if available, or else uses the
open-source private key in this script.
-m, --machine <name> Optionally specify the target machine name of this
image.
-v, --version <name> Specify the version of bios image file
-e, --extended <name> Specify the extended version of bios image file
-h, --help Display this help text and exit.
EOF
)
#################################################################
# It's the OpenBMC "public" private key (currently under
# meta-phosphor/recipes-phosphor/flash/files/OpenBMC.priv):
# https://gerrit.openbmc-project.xyz/c/openbmc/openbmc/+/8949/15/
# meta-phosphor/common/recipes-phosphor/flash/files/OpenBMC.priv
#
#################################################################
private_key=$'-----BEGIN PR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----END PRIVATE KEY-----
'
do_sign=false
PRIVATE_KEY_PATH=${PRIVATE_KEY_PATH:-}
private_key_path="${PRIVATE_KEY_PATH}"
outfile=""
machine=""
version=""
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-o|--out)
outfile="$2"
shift 2
;;
-s|--sign)
do_sign=true
if [[ -n "${2}" && "${2}" != -* ]]; then
private_key_path="$2"
shift 2
else
shift 1
fi
;;
-m|--machine)
machine="$2"
shift 2
;;
-v|--version)
version="$2"
shift 2
;;
-e|--extended)
extended="$2"
shift 2
;;
-h|--help)
echo "$help"
exit
;;
-*)
echo "Unrecognised option $1"
echo "$help"
exit
;;
*)
file="$1"
shift 1
;;
esac
done
if [ ! -f "${file}" ]; then
echo "${file} not found, Please enter a valid Bios image file"
echo "$help"
exit 1
fi
if [[ -z $version ]]; then
echo "Please provide version of image with -v option"
exit 1
fi
if [[ -z $outfile ]]; then
outfile=$(pwd)/obmc-bios.tar.gz
else
if [[ $outfile != /* ]]; then
outfile=$(pwd)/$outfile
fi
fi
scratch_dir=$(mktemp -d)
# Remove the temp directory on exit.
# The files in the temp directory may contain read-only files, so add
# --interactive=never to skip the prompt.
trap '{ rm -r --interactive=never ${scratch_dir}; }' EXIT
if [[ "${do_sign}" == true ]]; then
if [[ -z "${private_key_path}" ]]; then
private_key_path=${scratch_dir}/OpenBMC.priv
echo "${private_key}" > "${private_key_path}"
echo "Image is NOT secure!! Signing with the open private key!"
else
if [[ ! -f "${private_key_path}" ]]; then
echo "Couldn't find private key ${private_key_path}."
exit 1
fi
echo "Signing with ${private_key_path}."
fi
public_key_file=publickey
public_key_path=${scratch_dir}/$public_key_file
openssl pkey -in "${private_key_path}" -pubout -out "${public_key_path}"
fi
manifest_location="MANIFEST"
files_to_sign="$manifest_location $public_key_file"
# Go to scratch_dir
cp "${file}" "${scratch_dir}"
cd "${scratch_dir}"
files_to_sign+=" $(basename "${file}")"
echo "Creating MANIFEST for the image"
echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\n\
version=$version" > $manifest_location
if [[ -n "${extended}" ]]; then
echo -e "ExtendedVersion=\"${extended}\"" >> $manifest_location
fi
if [[ -n "${machine}" ]]; then
echo -e "MachineName=${machine}" >> $manifest_location
fi
if [[ "${do_sign}" == true ]]; then
private_key_name=$(basename "${private_key_path}")
key_type="${private_key_name%.*}"
echo KeyType="${key_type}" >> $manifest_location
echo HashType="RSA-SHA256" >> $manifest_location
for file in $files_to_sign; do
openssl dgst -sha256 -sign "${private_key_path}" -out "${file}.sig" "$file"
done
additional_files="*.sig"
fi
# shellcheck disable=SC2086
# Do not quote the files variables since they list multiple files
# and tar would assume to be a single file name within quotes
tar -czvf $outfile $files_to_sign $additional_files
echo "Bios image tarball is at $outfile"