blob: 727e040cc186bb4c78511da28dbec5490e187f02 [file] [log] [blame]
Vijay Khemka18918692020-02-12 14:23:40 -08001#!/bin/bash
2set -eo pipefail
3
Patrick Williams780c9302022-12-08 06:34:04 -06004help=$(cat <<EOF
5Generate Tarball with Bios image and MANIFEST Script
Vijay Khemka18918692020-02-12 14:23:40 -08006
7Generates a Bios image tarball from given file as input.
8Creates a MANIFEST for image verification and recreation
9Packages the image and MANIFEST together in a tarball
10
11usage: gen-bios-tar [OPTION] <Bios FILE>...
12
13Options:
14 -o, --out <file> Specify destination file. Defaults to
Patrick Williams780c9302022-12-08 06:34:04 -060015 $(pwd)/obmc-bios.tar.gz if unspecified.
Vijay Khemka18918692020-02-12 14:23:40 -080016 -s, --sign <path> Sign the image. The optional path argument specifies
17 the private key file. Defaults to the bash variable
18 PRIVATE_KEY_PATH if available, or else uses the
19 open-source private key in this script.
20 -m, --machine <name> Optionally specify the target machine name of this
21 image.
22 -v, --version <name> Specify the version of bios image file
Glukhov Mikhail3363f652023-02-15 12:31:38 +030023 -e, --extended <name> Specify the extended version of bios image file
Vijay Khemka18918692020-02-12 14:23:40 -080024 -h, --help Display this help text and exit.
Patrick Williams780c9302022-12-08 06:34:04 -060025EOF
26)
Vijay Khemka18918692020-02-12 14:23:40 -080027
28#################################################################
29# It's the OpenBMC "public" private key (currently under
30# meta-phosphor/recipes-phosphor/flash/files/OpenBMC.priv):
31# https://gerrit.openbmc-project.xyz/c/openbmc/openbmc/+/8949/15/
32# meta-phosphor/common/recipes-phosphor/flash/files/OpenBMC.priv
33#
34#################################################################
35private_key=$'-----BEGIN PRIVATE KEY-----
36MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri
37PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B
38zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k
39D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU
40zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/
41nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1
42h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt
43rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE
44x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5
45DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw
46cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE
475yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4
486ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m
49F0nIdUAhR0yTfKM=
50-----END PRIVATE KEY-----
51'
52
53do_sign=false
Isaac Kurtha1d2f862022-01-31 14:10:31 -060054PRIVATE_KEY_PATH=${PRIVATE_KEY_PATH:-}
Vijay Khemka18918692020-02-12 14:23:40 -080055private_key_path="${PRIVATE_KEY_PATH}"
56outfile=""
57machine=""
58version=""
59
60while [[ $# -gt 0 ]]; do
61 key="$1"
62 case $key in
63 -o|--out)
64 outfile="$2"
65 shift 2
66 ;;
67 -s|--sign)
68 do_sign=true
Isaac Kurtha1d2f862022-01-31 14:10:31 -060069 if [[ -n "${2}" && "${2}" != -* ]]; then
Vijay Khemka18918692020-02-12 14:23:40 -080070 private_key_path="$2"
71 shift 2
72 else
73 shift 1
74 fi
75 ;;
76 -m|--machine)
77 machine="$2"
78 shift 2
79 ;;
80 -v|--version)
81 version="$2"
82 shift 2
83 ;;
Glukhov Mikhail3363f652023-02-15 12:31:38 +030084 -e|--extended)
85 extended="$2"
86 shift 2
87 ;;
Vijay Khemka18918692020-02-12 14:23:40 -080088 -h|--help)
89 echo "$help"
90 exit
91 ;;
92 -*)
93 echo "Unrecognised option $1"
94 echo "$help"
95 exit
96 ;;
97 *)
98 file="$1"
99 shift 1
100 ;;
101 esac
102done
103
104if [ ! -f "${file}" ]; then
105 echo "${file} not found, Please enter a valid Bios image file"
106 echo "$help"
107 exit 1
108fi
109
110if [[ -z $version ]]; then
111 echo "Please provide version of image with -v option"
112 exit 1
113fi
114
115if [[ -z $outfile ]]; then
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600116 outfile=$(pwd)/obmc-bios.tar.gz
Vijay Khemka18918692020-02-12 14:23:40 -0800117else
118 if [[ $outfile != /* ]]; then
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600119 outfile=$(pwd)/$outfile
Vijay Khemka18918692020-02-12 14:23:40 -0800120 fi
121fi
122
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600123scratch_dir=$(mktemp -d)
Vijay Khemka18918692020-02-12 14:23:40 -0800124# Remove the temp directory on exit.
125# The files in the temp directory may contain read-only files, so add
126# --interactive=never to skip the prompt.
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600127trap '{ rm -r --interactive=never ${scratch_dir}; }' EXIT
Vijay Khemka18918692020-02-12 14:23:40 -0800128
129if [[ "${do_sign}" == true ]]; then
130 if [[ -z "${private_key_path}" ]]; then
131 private_key_path=${scratch_dir}/OpenBMC.priv
132 echo "${private_key}" > "${private_key_path}"
133 echo "Image is NOT secure!! Signing with the open private key!"
134 else
135 if [[ ! -f "${private_key_path}" ]]; then
136 echo "Couldn't find private key ${private_key_path}."
137 exit 1
138 fi
139
140 echo "Signing with ${private_key_path}."
141 fi
142
143 public_key_file=publickey
144 public_key_path=${scratch_dir}/$public_key_file
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600145 openssl pkey -in "${private_key_path}" -pubout -out "${public_key_path}"
Vijay Khemka18918692020-02-12 14:23:40 -0800146fi
147
148manifest_location="MANIFEST"
149files_to_sign="$manifest_location $public_key_file"
150
151# Go to scratch_dir
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600152cp "${file}" "${scratch_dir}"
Vijay Khemka18918692020-02-12 14:23:40 -0800153cd "${scratch_dir}"
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600154files_to_sign+=" $(basename "${file}")"
Vijay Khemka18918692020-02-12 14:23:40 -0800155
156echo "Creating MANIFEST for the image"
157echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\n\
158version=$version" > $manifest_location
159
Glukhov Mikhail3363f652023-02-15 12:31:38 +0300160if [[ -n "${extended}" ]]; then
161 echo -e "ExtendedVersion=\"${extended}\"" >> $manifest_location
162fi
163
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600164if [[ -n "${machine}" ]]; then
Vijay Khemka18918692020-02-12 14:23:40 -0800165 echo -e "MachineName=${machine}" >> $manifest_location
166fi
167
168if [[ "${do_sign}" == true ]]; then
169 private_key_name=$(basename "${private_key_path}")
170 key_type="${private_key_name%.*}"
171 echo KeyType="${key_type}" >> $manifest_location
172 echo HashType="RSA-SHA256" >> $manifest_location
173
174 for file in $files_to_sign; do
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600175 openssl dgst -sha256 -sign "${private_key_path}" -out "${file}.sig" "$file"
Vijay Khemka18918692020-02-12 14:23:40 -0800176 done
177
178 additional_files="*.sig"
179fi
180
Glukhov Mikhail124e63b2022-08-04 10:16:29 +0300181# shellcheck disable=SC2086
182# Do not quote the files variables since they list multiple files
183# and tar would assume to be a single file name within quotes
184tar -czvf $outfile $files_to_sign $additional_files
Vijay Khemka18918692020-02-12 14:23:40 -0800185echo "Bios image tarball is at $outfile"