blob: a82a843367e310a11fd20ed1e9e8f27549ce126e [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
23 -h, --help Display this help text and exit.
Patrick Williams780c9302022-12-08 06:34:04 -060024EOF
25)
Vijay Khemka18918692020-02-12 14:23:40 -080026
27#################################################################
28# It's the OpenBMC "public" private key (currently under
29# meta-phosphor/recipes-phosphor/flash/files/OpenBMC.priv):
30# https://gerrit.openbmc-project.xyz/c/openbmc/openbmc/+/8949/15/
31# meta-phosphor/common/recipes-phosphor/flash/files/OpenBMC.priv
32#
33#################################################################
34private_key=$'-----BEGIN PRIVATE KEY-----
35MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri
36PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B
37zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k
38D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU
39zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/
40nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1
41h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt
42rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE
43x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5
44DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw
45cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE
465yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4
476ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m
48F0nIdUAhR0yTfKM=
49-----END PRIVATE KEY-----
50'
51
52do_sign=false
Isaac Kurtha1d2f862022-01-31 14:10:31 -060053PRIVATE_KEY_PATH=${PRIVATE_KEY_PATH:-}
Vijay Khemka18918692020-02-12 14:23:40 -080054private_key_path="${PRIVATE_KEY_PATH}"
55outfile=""
56machine=""
57version=""
58
59while [[ $# -gt 0 ]]; do
60 key="$1"
61 case $key in
62 -o|--out)
63 outfile="$2"
64 shift 2
65 ;;
66 -s|--sign)
67 do_sign=true
Isaac Kurtha1d2f862022-01-31 14:10:31 -060068 if [[ -n "${2}" && "${2}" != -* ]]; then
Vijay Khemka18918692020-02-12 14:23:40 -080069 private_key_path="$2"
70 shift 2
71 else
72 shift 1
73 fi
74 ;;
75 -m|--machine)
76 machine="$2"
77 shift 2
78 ;;
79 -v|--version)
80 version="$2"
81 shift 2
82 ;;
83 -h|--help)
84 echo "$help"
85 exit
86 ;;
87 -*)
88 echo "Unrecognised option $1"
89 echo "$help"
90 exit
91 ;;
92 *)
93 file="$1"
94 shift 1
95 ;;
96 esac
97done
98
99if [ ! -f "${file}" ]; then
100 echo "${file} not found, Please enter a valid Bios image file"
101 echo "$help"
102 exit 1
103fi
104
105if [[ -z $version ]]; then
106 echo "Please provide version of image with -v option"
107 exit 1
108fi
109
110if [[ -z $outfile ]]; then
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600111 outfile=$(pwd)/obmc-bios.tar.gz
Vijay Khemka18918692020-02-12 14:23:40 -0800112else
113 if [[ $outfile != /* ]]; then
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600114 outfile=$(pwd)/$outfile
Vijay Khemka18918692020-02-12 14:23:40 -0800115 fi
116fi
117
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600118scratch_dir=$(mktemp -d)
Vijay Khemka18918692020-02-12 14:23:40 -0800119# Remove the temp directory on exit.
120# The files in the temp directory may contain read-only files, so add
121# --interactive=never to skip the prompt.
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600122trap '{ rm -r --interactive=never ${scratch_dir}; }' EXIT
Vijay Khemka18918692020-02-12 14:23:40 -0800123
124if [[ "${do_sign}" == true ]]; then
125 if [[ -z "${private_key_path}" ]]; then
126 private_key_path=${scratch_dir}/OpenBMC.priv
127 echo "${private_key}" > "${private_key_path}"
128 echo "Image is NOT secure!! Signing with the open private key!"
129 else
130 if [[ ! -f "${private_key_path}" ]]; then
131 echo "Couldn't find private key ${private_key_path}."
132 exit 1
133 fi
134
135 echo "Signing with ${private_key_path}."
136 fi
137
138 public_key_file=publickey
139 public_key_path=${scratch_dir}/$public_key_file
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600140 openssl pkey -in "${private_key_path}" -pubout -out "${public_key_path}"
Vijay Khemka18918692020-02-12 14:23:40 -0800141fi
142
143manifest_location="MANIFEST"
144files_to_sign="$manifest_location $public_key_file"
145
146# Go to scratch_dir
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600147cp "${file}" "${scratch_dir}"
Vijay Khemka18918692020-02-12 14:23:40 -0800148cd "${scratch_dir}"
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600149files_to_sign+=" $(basename "${file}")"
Vijay Khemka18918692020-02-12 14:23:40 -0800150
151echo "Creating MANIFEST for the image"
152echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\n\
153version=$version" > $manifest_location
154
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600155if [[ -n "${machine}" ]]; then
Vijay Khemka18918692020-02-12 14:23:40 -0800156 echo -e "MachineName=${machine}" >> $manifest_location
157fi
158
159if [[ "${do_sign}" == true ]]; then
160 private_key_name=$(basename "${private_key_path}")
161 key_type="${private_key_name%.*}"
162 echo KeyType="${key_type}" >> $manifest_location
163 echo HashType="RSA-SHA256" >> $manifest_location
164
165 for file in $files_to_sign; do
Isaac Kurtha1d2f862022-01-31 14:10:31 -0600166 openssl dgst -sha256 -sign "${private_key_path}" -out "${file}.sig" "$file"
Vijay Khemka18918692020-02-12 14:23:40 -0800167 done
168
169 additional_files="*.sig"
170fi
171
Glukhov Mikhail124e63b2022-08-04 10:16:29 +0300172# shellcheck disable=SC2086
173# Do not quote the files variables since they list multiple files
174# and tar would assume to be a single file name within quotes
175tar -czvf $outfile $files_to_sign $additional_files
Vijay Khemka18918692020-02-12 14:23:40 -0800176echo "Bios image tarball is at $outfile"