blob: 71f4e5e18fd412e847f7965be258040144c3abaa [file] [log] [blame]
Gunnar Mills6b5b27e2017-03-01 21:37:01 -06001#!/bin/bash
Gunnar Millsf8f11302017-03-08 13:11:33 -06002set -eo pipefail
Gunnar Millse8221902017-02-03 15:59:06 -06003
Gunnar Mills29414352017-10-25 11:07:43 -05004help=$'Generate Tarball with PNOR SquashFS image and MANIFEST Script
Gunnar Millse8221902017-02-03 15:59:06 -06005
Gunnar Mills29414352017-10-25 11:07:43 -05006Generates a PNOR SquashFS image from the PNOR image
7Creates a MANIFEST for image verification and recreation
Saqib Khan93433f02017-03-23 22:24:27 -05008Packages the SquashFS image and MANIFEST together in a tarball
Gunnar Millse8221902017-02-03 15:59:06 -06009
Gunnar Millsc15b02d2017-03-03 10:28:37 -060010usage: generate-squashfs [OPTION] <PNOR FILE>...
Gunnar Millse8221902017-02-03 15:59:06 -060011
12Options:
Gunnar Mills37751f92017-02-07 21:05:01 -060013 -f, --file <file> Specify destination file. Defaults to
Michael Tritz8f396732017-06-09 10:55:35 -050014 `pwd`/<PNOR FILE>.pnor.squashfs.tar if unspecified.
15 (For example, "generate-squashfs my.pnor" would
16 generate `pwd`/my.pnor.squashfs.tar output.)
Eddie James643e7302018-02-26 15:23:43 -060017 -s, --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.
Gunnar Millse8221902017-02-03 15:59:06 -060021 -h, --help Display this help text and exit.
22'
Eddie James643e7302018-02-26 15:23:43 -060023
24private_key=$'-----BEGIN PRIVATE KEY-----
25MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPvSDLu6slkP1gri
26PaeQXL9ysD69J/HjbBCIQ0RPfeWBb75US1tRTjPP0Ub8CtH8ExVf8iF1ulsZA78B
27zIjBYZVp9pyD6LbpZ/hjV7rIH6dTNhoVpdA+F8LzmQ7cyhHG8l2JMvdunwF2uX5k
28D4WDcZt/ITKZNQNavPtmIyD5HprdAgMBAAECgYEAuQkTSi5ZNpAoWz76xtGRFSwU
29zUT4wQi3Mz6tDtjKTYXasiQGa0dHC1M9F8fDu6BZ9W7W4Dc9hArRcdzEighuxoI/
30nZI/0uL89iUEywnDEIHuS6D5JlZaj86/nx9YvQnO8F/seM+MX0EAWVrd5wC7aAF1
31h6Fu7ykZB4ggUjQAWwECQQD+AUiDOEO+8btLJ135dQfSGc5VFcZiequnKWVm6uXt
32rX771hEYjYMjLqWGFg9G4gE3GuABM5chMINuQQUivy8tAkEA/cxfy19XkjtqcMgE
33x/UDt6Nr+Ky/tk+4Y65WxPRDas0uxFOPk/vEjgVmz1k/TAy9G4giisluTvtmltr5
34DCLocQJBAJnRHx9PiD7uVhRJz6/L/iNuOzPtTsi+Loq5F83+O6T15qsM1CeBMsOw
35cM5FN5UeMcwz+yjfHAsePMkcmMaU7jUCQHlg9+N8upXuIo7Dqj2zOU7nMmkgvSNE
365yuNImRZabC3ZolwaTdd7nf5r1y1Eyec5Ag5yENV6JKPe1Xkbb1XKJECQDngA0h4
376ATvfP1Vrx4CbP11eKXbCsZ9OGPHSgyvVjn68oY5ZP3uPsIattoN7dE2BRfuJm7m
38F0nIdUAhR0yTfKM=
39-----END PRIVATE KEY-----
40'
41
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -050042# Reference the ffs structures at:
43# https://github.com/open-power/hostboot/blob/master/src/usr/pnor/common/ffs_hb.H
44# https://github.com/open-power/hostboot/blob/master/src/usr/pnor/ffs.h
Gunnar Millsd4908a42017-03-07 08:25:59 -060045let ffs_entry_size=128
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -050046let vercheck_offset=112
Eddie James643e7302018-02-26 15:23:43 -060047do_sign=false
48private_key_path="${PRIVATE_KEY_PATH}"
Michael Tritz8f396732017-06-09 10:55:35 -050049outfile=""
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060050declare -a partitions=()
51tocfile="pnor.toc"
Gunnar Mills37751f92017-02-07 21:05:01 -060052
Gunnar Millse8221902017-02-03 15:59:06 -060053while [[ $# -gt 0 ]]; do
54 key="$1"
55 case $key in
Gunnar Mills37751f92017-02-07 21:05:01 -060056 -f|--file)
57 outfile="$2"
58 shift 2
59 ;;
Eddie James643e7302018-02-26 15:23:43 -060060 -s|--sign)
61 do_sign=true
62 if [[ ! -z "${2}" && "${2}" != -* ]]; then
63 private_key_path="$2"
64 shift 2
65 else
66 shift 1
67 fi
68 ;;
Gunnar Millse8221902017-02-03 15:59:06 -060069 -h|--help)
70 echo "$help"
71 exit
72 ;;
73 *)
Gunnar Millsc15b02d2017-03-03 10:28:37 -060074 pnorfile="$1"
75 shift 1
Gunnar Millse8221902017-02-03 15:59:06 -060076 ;;
77 esac
78done
Gunnar Mills18f7cdb2017-02-07 16:44:19 -060079
Gunnar Millsc15b02d2017-03-03 10:28:37 -060080if [ ! -f "${pnorfile}" ]; then
81 echo "Please enter a valid PNOR file."
82 echo "$help"
83 exit 1
84fi
85
Michael Tritz8f396732017-06-09 10:55:35 -050086if [[ -z $outfile ]]; then
87 if [[ ${pnorfile##*.} == "pnor" ]]; then
88 outfile=`pwd`/${pnorfile##*/}.squashfs.tar
89 else
90 outfile=`pwd`/${pnorfile##*/}.pnor.squashfs.tar
91 fi
92else
93 if [[ $outfile != /* ]]; then
94 outfile=`pwd`/$outfile
95 fi
96fi
97
Gunnar Millsf8f11302017-03-08 13:11:33 -060098scratch_dir=`mktemp -d`
Gunnar Mills6b5b27e2017-03-01 21:37:01 -060099
Eddie James643e7302018-02-26 15:23:43 -0600100if [[ "${do_sign}" == true ]]; then
101 if [[ -z "${private_key_path}" ]]; then
102 private_key_path=${scratch_dir}/OpenBMC.priv
103 echo "${private_key}" > "${private_key_path}"
104 echo "Image is NOT secure!! Signing with the open private key!"
105 else
106 if [[ ! -f "${private_key_path}" ]]; then
107 echo "Couldn't find private key ${private_key_path}."
108 exit 1
109 fi
110
111 echo "Signing with ${private_key_path}."
112 fi
113
114 public_key_file=publickey
115 public_key_path=${scratch_dir}/$public_key_file
116 openssl pkey -in "${private_key_path}" -pubout -out ${public_key_path}
117fi
118
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600119echo "Parsing PNOR TOC..."
Gunnar Millsd4908a42017-03-07 08:25:59 -0600120
Gunnar Millsf8f11302017-03-08 13:11:33 -0600121pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile}
Gunnar Mills2f27b742017-03-20 13:46:30 -0500122pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile}
Samuel Mendoza-Jonasd1d19212018-06-01 14:02:24 +1000123version_size=$(du -k ${scratch_dir}/VERSION | head -1 | cut -f 1)
124magic_number=$(xxd -p -l 4 ${scratch_dir}/VERSION)
125# Check if VERSION is signed. A signed version partition will have an extra
126# 4K header starting with the magic number 0x17082011, see:
127# https://github.com/open-power/skiboot/blob/master/libstb/container.h#L47
128if [ "$version_size" == "8" -a "$magic_number" == "17082011" ]; then
129 # Advance past the STB header (4K, indexed from 1)
130 cp ${scratch_dir}/VERSION ${scratch_dir}/VERSION_FULL
131 tail --bytes=+4097 ${scratch_dir}/VERSION_FULL > ${scratch_dir}/VERSION
132fi
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600133{
Gunnar Mills2f27b742017-03-20 13:46:30 -0500134 version=$(head -n 1 ${scratch_dir}/VERSION)
135 echo "version=$version"
Saqib Khan029b8252017-03-22 21:38:07 -0500136 extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',')
137 echo "extended_version=$extended_version"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600138 while read line; do
139 if [[ $line == "ID="* ]]; then
Gunnar Millse0ed3022017-03-02 16:16:19 -0600140 # This line looks like
141 # "ID=05 MVPD 000d9000..00169000 (actual=00090000) [ECC]"
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600142 read -r -a fields <<< "$line"
Gunnar Millse0ed3022017-03-02 16:16:19 -0600143
Gunnar Millsd4908a42017-03-07 08:25:59 -0600144 id=${fields[0]##*=}
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500145 offset=$((${ffs_entry_size} * 10#${id} + ${vercheck_offset}))
146 vercheck=$(xxd -p -l 0x1 -seek ${offset} ${scratch_dir}/part)
Michael Tritz1bd65ac2017-05-07 17:40:14 -0500147 export flags=$(pflash --detail=$((10#$id)) -F ${pnorfile} | grep "\[" |
148 sed 's/....$//' | tr '\n' ',' | sed 's/.$//')
149 if [[ $flags != "" ]]; then
150 flags=,$flags
151 fi
152
153 if [[ $(echo $flags | grep "READONLY") == "" &&
154 $(echo $flags | grep "PRESERVED") == "" ]]; then
155 flags=$flags,READWRITE
Gunnar Millsd4908a42017-03-07 08:25:59 -0600156 fi
157
Gunnar Millse0ed3022017-03-02 16:16:19 -0600158 # Need the partition ID, name, start location, end location, and flags
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500159 echo "partition${id}=${fields[1]},${fields[2]/../,},${vercheck}${flags}"
Gunnar Millsd4908a42017-03-07 08:25:59 -0600160
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600161 # Save the partition name
162 partitions+=(${fields[1]})
163 fi
Adriana Kobylak1e4a7f22017-07-09 16:12:36 -0500164 # Don't need the BACKUP_PART partition
165 done < <(pflash --info -F ${pnorfile} | grep -v "BACKUP")
Gunnar Mills6b5b27e2017-03-01 21:37:01 -0600166} > ${scratch_dir}/${tocfile}
Gunnar Mills37751f92017-02-07 21:05:01 -0600167
Gunnar Mills18f7cdb2017-02-07 16:44:19 -0600168for partition in "${partitions[@]}"; do
169 echo "Reading ${partition}..."
Gunnar Millsf8f11302017-03-08 13:11:33 -0600170 pflash --partition=${partition} \
171 --read=${scratch_dir}/${partition} \
172 -F ${pnorfile}
Gunnar Mills18f7cdb2017-02-07 16:44:19 -0600173done
Gunnar Mills37751f92017-02-07 21:05:01 -0600174
175echo "Creating SquashFS image..."
Gunnar Mills37751f92017-02-07 21:05:01 -0600176cd "${scratch_dir}"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500177mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs
Gunnar Mills37751f92017-02-07 21:05:01 -0600178
Saqib Khan93433f02017-03-23 22:24:27 -0500179echo "Creating MANIFEST for the image"
Adriana Kobylak9c866062017-04-13 16:12:32 -0500180manifest_location="MANIFEST"
Leonel Gonzalezc14a3d22017-06-22 12:49:38 -0500181echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\nversion=$version\n\
Saqib Khan7254f0e2017-04-10 21:45:37 -0500182extended_version=$extended_version" >> $manifest_location
Saqib Khan93433f02017-03-23 22:24:27 -0500183
Eddie James643e7302018-02-26 15:23:43 -0600184if [[ "${do_sign}" == true ]]; then
185 private_key_name=$(basename "${private_key_path}")
186 key_type="${private_key_name%.*}"
187 echo KeyType="${key_type}" >> $manifest_location
188 echo HashType="RSA-SHA256" >> $manifest_location
189
190 for file in pnor.xz.squashfs $manifest_location $public_key_file; do
191 openssl dgst -sha256 -sign ${private_key_path} -out "${file}.sig" $file
192 done
193
194 additional_files="${public_key_file} *.sig"
195fi
196
Saqib Khan93433f02017-03-23 22:24:27 -0500197echo "Generating tarball to contain the SquashFS image and its MANIFEST"
Eddie James643e7302018-02-26 15:23:43 -0600198tar -cvf $outfile $manifest_location pnor.xz.squashfs $additional_files
Saqib Khan93433f02017-03-23 22:24:27 -0500199
200echo "SquashFSTarball at ${outfile}"
Gunnar Mills37751f92017-02-07 21:05:01 -0600201rm -r "${scratch_dir}"