11891869fSVijay Khemka#!/bin/bash
21891869fSVijay Khemkaset -eo pipefail
31891869fSVijay Khemka
4780c930eSPatrick Williamshelp=$(cat <<EOF
5780c930eSPatrick WilliamsGenerate Tarball with Bios image and MANIFEST Script
61891869fSVijay Khemka
71891869fSVijay KhemkaGenerates a Bios image tarball from given file as input.
81891869fSVijay KhemkaCreates a MANIFEST for image verification and recreation
91891869fSVijay KhemkaPackages the image and MANIFEST together in a tarball
101891869fSVijay Khemka
111891869fSVijay Khemkausage: gen-bios-tar [OPTION] <Bios FILE>...
121891869fSVijay Khemka
131891869fSVijay KhemkaOptions:
141891869fSVijay Khemka   -o, --out <file>       Specify destination file. Defaults to
15780c930eSPatrick Williams                          $(pwd)/obmc-bios.tar.gz if unspecified.
161891869fSVijay Khemka   -s, --sign <path>      Sign the image. The optional path argument specifies
171891869fSVijay Khemka                          the private key file. Defaults to the bash variable
181891869fSVijay Khemka                          PRIVATE_KEY_PATH if available, or else uses the
191891869fSVijay Khemka                          open-source private key in this script.
201891869fSVijay Khemka   -m, --machine <name>   Optionally specify the target machine name of this
211891869fSVijay Khemka                          image.
221891869fSVijay Khemka   -v, --version <name>   Specify the version of bios image file
233363f655SGlukhov Mikhail   -e, --extended <name>  Specify the extended version of bios image file
241891869fSVijay Khemka   -h, --help             Display this help text and exit.
25780c930eSPatrick WilliamsEOF
26780c930eSPatrick Williams)
271891869fSVijay Khemka
281891869fSVijay Khemka#################################################################
291891869fSVijay Khemka# It's the OpenBMC "public" private key (currently under
301891869fSVijay Khemka# meta-phosphor/recipes-phosphor/flash/files/OpenBMC.priv):
311891869fSVijay Khemka# https://gerrit.openbmc-project.xyz/c/openbmc/openbmc/+/8949/15/
321891869fSVijay Khemka# meta-phosphor/common/recipes-phosphor/flash/files/OpenBMC.priv
331891869fSVijay Khemka#
341891869fSVijay Khemka#################################################################
351891869fSVijay Khemkaprivate_key=$'-----BEGIN PRIVATE KEY-----
36*e2dac256SRashmica GuptaMIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQDO7vWM6ZOylO6T
37*e2dac256SRashmica GuptalxDiRWgKCFauAxMVM4A7NmgZxfV73xqTzAtIzzF9CIKUEhqMT4LhNy1rU2oUUivH
38*e2dac256SRashmica Gupta4BZO2oC6yFafEPVla2oYAeWtXJmQTixYgJplKQCtLzFtb57DQJpl9Od0RVFC61yg
39*e2dac256SRashmica Gupta4LihsiENnRjncLPP7OkL68ssiELu+WxazDtewmVYQEHOMWQa2zDh9yrWsblLAyo4
40*e2dac256SRashmica GuptagsG9rdwWjqIUnZkoeJuT7zks4Jes4qAtQUuZhCxMcvwvOq8od3e4nLVFnyUOGMpE
41*e2dac256SRashmica GuptajdDDGddKh5e+9BJGqkfsLCT9UFTYi62Ifuxl1Gp963cZLapJS0CneHIrG3gapiCO
42*e2dac256SRashmica Guptaea4TH/xySfts2jpl7DnEIUo3Qs0rZrOLQDLVVXvRJvHdsVoZcSRCD8Jf0YTQ6tT7
43*e2dac256SRashmica Guptaj3ejUOH8j3DGkgKRHGtH7yyCgmDyPYVQdEu3q4E1NBwODAyxppYGBqkwpSGRnf6q
44*e2dac256SRashmica GuptaE6i5rQJuoPCm3rb9ZmoHVzeb8HW3ofYfNnZNuae/2LdEtJ/cFs8ILXP5AKiaKpmS
45*e2dac256SRashmica GuptaH4GB8GUcgRMITGI1pcXhevO2wHi0ReJkme+m5GpuVIg9LpTs6YvvNoFeu6ux7up/
46*e2dac256SRashmica GuptaTjTa+Zdy3tLUZvvIIlS2Asu32uiGBFjZ2WiLeHq+ScaDsSyEJQkoYLqv+3MoPKhy
47*e2dac256SRashmica GuptaiX4yanZCwG3yx68RDe9qQBt6WXqAEwIDAQABAoICAA6OxAqKQiA9lv0eEwuAC34t
48*e2dac256SRashmica GuptaMP/j6ntC2MIRpUgu44K34tRD9gVEwjwEFb+Z+HEnhNMYQSM8RomwcDELBDa+63B4
49*e2dac256SRashmica GuptaeJOPK1xbrqaKt6A3E/yRa1A8l+AG/uuwFr+WqyocSOBkVsYYvEtDaIxO0t5ZPDcL
50*e2dac256SRashmica Guptadr2NcbDuf0Sd7XiwC1lphaRrmr+jWGLZfmellN/IzMsQytw4u4rZ6aX5GO0hpoqV
51*e2dac256SRashmica GuptatTRTE/vDZFqHaVPNZw48ET2tysY9hKpKKpCeBcWIhg0gRSZlOEOiHdStz2JyVnGB
52*e2dac256SRashmica GuptaUX0XCZQcFZw5TM7fUGC9jtM77qCJTYaXQpUsX77xQtalRA7hS1VAm6i6SbNBvE4j
53*e2dac256SRashmica Guptale2JWBmOCbh9A/Dqnxh/3R2ZpsbffMG3O4KLkObquP8QGsItxwZYm4Bo37/HS6ki
54*e2dac256SRashmica Gupta1Ilym014DUT1+mTybi+2TjlPgh2LeECo1lbgFRJ/p9Vs7TJV2jeVPKDHF72cVvp2
55*e2dac256SRashmica GuptaLy6dGQVtBPz7/HpvxwYA5TJstNWgiU1sZPwgVzhgeZEKt5DO3Da6DVoXt96/tHk4
56*e2dac256SRashmica Gupta577MA9P4qYLJldq5rfFW3IeyGUjLZ1yLmmDWj2Hz2IQe/YqrHHQH5UO6xxhrjuCa
57*e2dac256SRashmica GuptaFrAOv7wJxmmbhZ9AptmR0PnmTU79LM8gxyNY9nn0R+XMJd/3UvrrjzJWw+LUGdhC
58*e2dac256SRashmica GuptazG0pF0JGMb71wHwlmmUpAoIBAQDyUHAcpgWtCl70qVpHnAG6WnsRP6ZS0dtBjqnN
59*e2dac256SRashmica Guptal9GachlNLOyQHfKcs7Vj7dkrcumSTbZ7zzgsazQcwfJMEvVq2RS8iTrtKAq+yYmj
60*e2dac256SRashmica GuptauejaJ3gAuiaU5Shv9PL+P25zGlwJtqEADYjiSrzO1ZRgM1M31tlH7f0wjYIrV6LP
61*e2dac256SRashmica Gupta5P5HYFVDFujsiWY9oUZ+PFaCWPRQ2P47zF3ocbiqD9TbFSwNfq5B2WPLCURExQAh
62*e2dac256SRashmica GuptaXT/GH8Devz2M79CWKjvDMQutZ8dcLZx6JNhpre5JoNbAxE2u9jIM++srSb41zArE
63*e2dac256SRashmica Gupta2lcy2tqNAWJV5jhFz5DZ451kvz7AKLaehWYj7D+TvX4V4UwpAoIBAQDanvWQCoop
64*e2dac256SRashmica GuptaLoh5s5tYwHB/iFE+jzVvyrytfRcDIjrBaCDCQrObgh/fllk44h9DpKHbM4nYlMpU
65*e2dac256SRashmica GuptaGXOsQCVkE/SiJakzeXfoecCJ3ebp+mlBILs79AWlfETJFLx1FKJQOQoraoICVFxx
66*e2dac256SRashmica GuptajcI/OvGlABRLvRYhNnq22//IgLlyKWfO0yS6FASIjriIjDwibKZqHt+DJ98Lv2d+
67*e2dac256SRashmica Gupta1fsOw/Ai7e2e3nK0+2vmXusZAwpcPuTYb+5LOnHE7GbPecuM22BSTiUAekoHq3/k
68*e2dac256SRashmica GuptafdYw+od4B4BJB0bAnRVu7y+TQXRkZ462RvP05AYXUzudjknv48mFuBslrybr3f50
69*e2dac256SRashmica GuptaaIwtFADeJrHbAoIBAQCYcEwnabaWZrjX+BZoiFd58eQMNNugrI7fzi06vrDJFdCf
70*e2dac256SRashmica GuptaAY0NGRoAxPlvFTmTIOaZ+LO9bd5r60FMeiLBAwhLoKdv+HEOsysXXVhunM1FOKFA
71*e2dac256SRashmica Gupta69rLvuJSlGmt0x/b35BZOABPNTSRD+15vVlrr75BmbL1kl2/BrcGJ0qwuOHS62KY
72*e2dac256SRashmica GuptaIziDXejpCqV7UuAlfmqs1eYSnn3RdoFy0yTYcphVIQXlPSqPl5PQI5LyamRtcpp2
73*e2dac256SRashmica GuptaRx8ko9W4MneIUzmCbJA5iCQxny5aRWZsAXg4qwYn9JAGJRGMGQdFdsirkKRcxNvK
74*e2dac256SRashmica Gupta6zz+xydNm8gHmy7wK3QBlVtVnJxmKwDQI9zHTQYJAoIBAQDVKrXJ81zv9r1/3U8V
75*e2dac256SRashmica Gupta5N5MnACL/VtfW9FJYHU1ywR7XSrEAAHdGa42dwUcX++YJ0ji0YgRNFNsWTzesdVD
76*e2dac256SRashmica GuptalemsyQgIduIiPcUtKL9lWZOTu3SVasSurVLstllj1/DERDnUR4/o8ZUJ6+2Bddn0
77*e2dac256SRashmica GuptaxvUDPKX9UH+rGSx4tnscA5+CnYJsJeSdunvYONTRxBsn0l6iJhhn/gPOOpsHtKnL
78*e2dac256SRashmica GuptahS9y/vfd3GFDST33L23EsFa3a7xwgdY460D8AIgnGij7V9Lgel0AyYp0ovZc34uD
79*e2dac256SRashmica Guptaz9yYWI32dbRWbMZ40RPKaudOeDSbjlMaH0A7ymfxjqwKxI9D2VscFWNs4hv8QErw
80*e2dac256SRashmica GuptaUc6NAoIBAQCmWWyARU/x4m7K4vNAWjD2Qx6R7PAdLs/6ZBqWw0RSpRHlYr73Qggw
81*e2dac256SRashmica GuptadCK+LrsR0O7y1KW2WUfrJmzHEdFQEYcZ1vZWeN85dMLVhYmazSXlaIegRE4FmMUa
82*e2dac256SRashmica GuptaDbzViUJA60Y4D/l6QWqdhxdZZe81QgqyLPXAv/e5esxRIi8yvEYhCx4pq7QtWYBm
83*e2dac256SRashmica GuptatvQnPaZd8emlKARivF2ecGDlWzhf4NotDDtFRT4jOHZKUC58uVjbiXJ445Vimqlb
84*e2dac256SRashmica GuptaDa7noVGwDQ93Ib1qyAilzFY5gWDeMyCnSQpnlVRHQ/8vlwLDsZs1kVau6k8WlcpM
85*e2dac256SRashmica GuptaJbmuCRNy7YvALVTzyQsQ4yw87BoONt1L
861891869fSVijay Khemka-----END PRIVATE KEY-----
871891869fSVijay Khemka'
881891869fSVijay Khemka
891891869fSVijay Khemkado_sign=false
90a1d2f861SIsaac KurthPRIVATE_KEY_PATH=${PRIVATE_KEY_PATH:-}
911891869fSVijay Khemkaprivate_key_path="${PRIVATE_KEY_PATH}"
921891869fSVijay Khemkaoutfile=""
931891869fSVijay Khemkamachine=""
941891869fSVijay Khemkaversion=""
951891869fSVijay Khemka
961891869fSVijay Khemkawhile [[ $# -gt 0 ]]; do
971891869fSVijay Khemka  key="$1"
981891869fSVijay Khemka  case $key in
991891869fSVijay Khemka    -o|--out)
1001891869fSVijay Khemka      outfile="$2"
1011891869fSVijay Khemka      shift 2
1021891869fSVijay Khemka      ;;
1031891869fSVijay Khemka    -s|--sign)
1041891869fSVijay Khemka      do_sign=true
105a1d2f861SIsaac Kurth      if [[ -n "${2}"  && "${2}" != -* ]]; then
1061891869fSVijay Khemka        private_key_path="$2"
1071891869fSVijay Khemka        shift 2
1081891869fSVijay Khemka      else
1091891869fSVijay Khemka        shift 1
1101891869fSVijay Khemka      fi
1111891869fSVijay Khemka      ;;
1121891869fSVijay Khemka    -m|--machine)
1131891869fSVijay Khemka      machine="$2"
1141891869fSVijay Khemka      shift 2
1151891869fSVijay Khemka      ;;
1161891869fSVijay Khemka    -v|--version)
1171891869fSVijay Khemka      version="$2"
1181891869fSVijay Khemka      shift 2
1191891869fSVijay Khemka      ;;
1203363f655SGlukhov Mikhail    -e|--extended)
1213363f655SGlukhov Mikhail      extended="$2"
1223363f655SGlukhov Mikhail      shift 2
1233363f655SGlukhov Mikhail      ;;
1241891869fSVijay Khemka    -h|--help)
1251891869fSVijay Khemka      echo "$help"
1261891869fSVijay Khemka      exit
1271891869fSVijay Khemka      ;;
1281891869fSVijay Khemka    -*)
1291891869fSVijay Khemka      echo "Unrecognised option $1"
1301891869fSVijay Khemka      echo "$help"
1311891869fSVijay Khemka      exit
1321891869fSVijay Khemka      ;;
1331891869fSVijay Khemka    *)
1341891869fSVijay Khemka      file="$1"
1351891869fSVijay Khemka      shift 1
1361891869fSVijay Khemka      ;;
1371891869fSVijay Khemka  esac
1381891869fSVijay Khemkadone
1391891869fSVijay Khemka
1401891869fSVijay Khemkaif [ ! -f "${file}" ]; then
1411891869fSVijay Khemka  echo "${file} not found, Please enter a valid Bios image file"
1421891869fSVijay Khemka  echo "$help"
1431891869fSVijay Khemka  exit 1
1441891869fSVijay Khemkafi
1451891869fSVijay Khemka
1461891869fSVijay Khemkaif [[ -z $version ]]; then
1471891869fSVijay Khemka  echo "Please provide version of image with -v option"
1481891869fSVijay Khemka  exit 1
1491891869fSVijay Khemkafi
1501891869fSVijay Khemka
1511891869fSVijay Khemkaif [[ -z $outfile ]]; then
152a1d2f861SIsaac Kurth  outfile=$(pwd)/obmc-bios.tar.gz
1531891869fSVijay Khemkaelse
1541891869fSVijay Khemka  if [[ $outfile != /* ]]; then
155a1d2f861SIsaac Kurth    outfile=$(pwd)/$outfile
1561891869fSVijay Khemka  fi
1571891869fSVijay Khemkafi
1581891869fSVijay Khemka
159a1d2f861SIsaac Kurthscratch_dir=$(mktemp -d)
1601891869fSVijay Khemka# Remove the temp directory on exit.
1611891869fSVijay Khemka# The files in the temp directory may contain read-only files, so add
1621891869fSVijay Khemka# --interactive=never to skip the prompt.
163a1d2f861SIsaac Kurthtrap '{ rm -r --interactive=never ${scratch_dir}; }' EXIT
1641891869fSVijay Khemka
1651891869fSVijay Khemkaif [[ "${do_sign}" == true ]]; then
1661891869fSVijay Khemka  if [[ -z "${private_key_path}" ]]; then
1671891869fSVijay Khemka    private_key_path=${scratch_dir}/OpenBMC.priv
1681891869fSVijay Khemka    echo "${private_key}" > "${private_key_path}"
1691891869fSVijay Khemka    echo "Image is NOT secure!! Signing with the open private key!"
1701891869fSVijay Khemka  else
1711891869fSVijay Khemka    if [[ ! -f "${private_key_path}" ]]; then
1721891869fSVijay Khemka      echo "Couldn't find private key ${private_key_path}."
1731891869fSVijay Khemka      exit 1
1741891869fSVijay Khemka    fi
1751891869fSVijay Khemka
1761891869fSVijay Khemka    echo "Signing with ${private_key_path}."
1771891869fSVijay Khemka  fi
1781891869fSVijay Khemka
1791891869fSVijay Khemka  public_key_file=publickey
1801891869fSVijay Khemka  public_key_path=${scratch_dir}/$public_key_file
181a1d2f861SIsaac Kurth  openssl pkey -in "${private_key_path}" -pubout -out "${public_key_path}"
1821891869fSVijay Khemkafi
1831891869fSVijay Khemka
1841891869fSVijay Khemkamanifest_location="MANIFEST"
1851891869fSVijay Khemkafiles_to_sign="$manifest_location $public_key_file"
1861891869fSVijay Khemka
1871891869fSVijay Khemka# Go to scratch_dir
188a1d2f861SIsaac Kurthcp "${file}" "${scratch_dir}"
1891891869fSVijay Khemkacd "${scratch_dir}"
190a1d2f861SIsaac Kurthfiles_to_sign+=" $(basename "${file}")"
1911891869fSVijay Khemka
1921891869fSVijay Khemkaecho "Creating MANIFEST for the image"
1931891869fSVijay Khemkaecho -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\n\
1941891869fSVijay Khemkaversion=$version" > $manifest_location
1951891869fSVijay Khemka
1963363f655SGlukhov Mikhailif [[ -n "${extended}" ]]; then
1973363f655SGlukhov Mikhail    echo -e "ExtendedVersion=\"${extended}\"" >> $manifest_location
1983363f655SGlukhov Mikhailfi
1993363f655SGlukhov Mikhail
200a1d2f861SIsaac Kurthif [[ -n "${machine}" ]]; then
2011891869fSVijay Khemka    echo -e "MachineName=${machine}" >> $manifest_location
2021891869fSVijay Khemkafi
2031891869fSVijay Khemka
2041891869fSVijay Khemkaif [[ "${do_sign}" == true ]]; then
2051891869fSVijay Khemka  private_key_name=$(basename "${private_key_path}")
2061891869fSVijay Khemka  key_type="${private_key_name%.*}"
2071891869fSVijay Khemka  echo KeyType="${key_type}" >> $manifest_location
2081891869fSVijay Khemka  echo HashType="RSA-SHA256" >> $manifest_location
2091891869fSVijay Khemka
2101891869fSVijay Khemka  for file in $files_to_sign; do
211a1d2f861SIsaac Kurth    openssl dgst -sha256 -sign "${private_key_path}" -out "${file}.sig" "$file"
2121891869fSVijay Khemka  done
2131891869fSVijay Khemka
2141891869fSVijay Khemka  additional_files="*.sig"
2151891869fSVijay Khemkafi
2161891869fSVijay Khemka
217124e63beSGlukhov Mikhail# shellcheck disable=SC2086
218124e63beSGlukhov Mikhail# Do not quote the files variables since they list multiple files
219124e63beSGlukhov Mikhail# and tar would assume to be a single file name within quotes
220124e63beSGlukhov Mikhailtar -czvf $outfile $files_to_sign $additional_files
2211891869fSVijay Khemkaecho "Bios image tarball is at $outfile"
222