Skip to content

Commit 6ed0e0f

Browse files
Stephen Cprekwghoffa
authored andcommitted
Use hash of HBB sw signatures as SALT entry for HBI hash page table
Change-Id: Iaa583cf12a5bdafc7f51d374a992b175c0a36663 RTC: 157314 Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/28362 Tested-by: Jenkins Server <pfd-jenkins+hostboot@us.ibm.com> Reviewed-by: Michael Baiocchi <mbaiocch@us.ibm.com> Tested-by: Jenkins OP Build CI <op-jenkins+hostboot@us.ibm.com> Tested-by: Jenkins OP HW <op-hw-jenkins+hostboot@us.ibm.com> Tested-by: FSP CI Jenkins <fsp-CI-jenkins+hostboot@us.ibm.com> Reviewed-by: Nicholas E. Bofferding <bofferdn@us.ibm.com> Reviewed-by: William G. Hoffa <wghoffa@us.ibm.com>
1 parent c9954a1 commit 6ed0e0f

File tree

3 files changed

+148
-10
lines changed

3 files changed

+148
-10
lines changed

src/build/buildpnor/PnorUtils.pm

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ package PnorUtils;
2828

2929
use Exporter 'import';
3030
@EXPORT_OK = qw(loadPnorLayout getNumber traceErr trace run_command PAGE_SIZE
31-
loadBinFiles findLayoutKeyByEyeCatch checkSpaceConstraints);
31+
loadBinFiles findLayoutKeyByEyeCatch checkSpaceConstraints
32+
getSwSignatures getBinDataFromFile);
3233
use strict;
3334

3435
my $TRAC_ERR = 0;
@@ -397,4 +398,84 @@ sub adjustHbiPhysSize
397398
}
398399
}
399400

401+
###############################################################################
402+
# getSwSignatures - Extracts concatenation of sw signatures from secure
403+
# container header. Simplified to skip around to the data
404+
# needed.
405+
################################################################################
406+
sub getSwSignatures
407+
{
408+
my ($i_file) = @_;
409+
410+
# Constants defined in ROM code.
411+
use constant ecid_size => 16; #bytes
412+
use constant sw_key_size => 132; #bytes
413+
414+
# Offsets defined in secure boot PLDD.
415+
# Relative offset are based on the previous constant
416+
use constant sw_key_count_offset => 450; #bytes
417+
use constant relative_offset_to_hw_ecid_count => 73; #bytes
418+
# Offset assuming Default of ECID count = 0 and SW count = 1
419+
use constant relative_offset_to_sw_ecid_count => 626; #bytes
420+
# Offset assuming Default of ECID count = 0
421+
use constant relative_offset_to_sw_signatures => 1; #bytes
422+
423+
# Header info
424+
my $sw_key_count = 0;
425+
my $hw_ecid_count = 0;
426+
my $sw_ecid_count = 0;
427+
my $sw_signatures = 0;
428+
429+
# Get header data from file
430+
my $header_data = getBinDataFromFile($i_file);
431+
432+
# get sw key count
433+
my $cur_offset = sw_key_count_offset;
434+
$sw_key_count = unpack("x$cur_offset C", $header_data);
435+
436+
# get hw ecid counts
437+
$cur_offset += relative_offset_to_hw_ecid_count;
438+
$hw_ecid_count = unpack("x$cur_offset C", $header_data);
439+
440+
# Variable size elements of a secure header
441+
# Note 1 sw_key is already considered in above constants
442+
my $num_optional_keys = ($sw_key_count > 1) ?
443+
($sw_key_count - 1) : 0;
444+
my $variable_size_offset = ($num_optional_keys * sw_key_size)
445+
+ ($hw_ecid_count * ecid_size);
446+
447+
# get sw ecid count
448+
$cur_offset += relative_offset_to_sw_ecid_count + $variable_size_offset;
449+
$sw_ecid_count = unpack("x$cur_offset C", $header_data);
450+
451+
# Variable size elements of a secure header
452+
$variable_size_offset = ($sw_ecid_count * ecid_size);
453+
454+
# get sw signatures
455+
$cur_offset += relative_offset_to_sw_signatures + $variable_size_offset;
456+
# Get concatenation of all possible sw signatures
457+
$sw_signatures = substr($header_data, $cur_offset,
458+
$sw_key_count*sw_key_size);
459+
460+
return $sw_signatures;
461+
}
462+
463+
###############################################################################
464+
# getBinDataFromFile - Extracts binary data from a given file into a variable
465+
################################################################################
466+
sub getBinDataFromFile
467+
{
468+
my ($i_file) = @_;
469+
470+
my $data = 0;
471+
open (BINFILE, "<", $i_file) or die "Error opening file $i_file: $!\n";
472+
binmode BINFILE;
473+
read(BINFILE,$data,PAGE_SIZE);
474+
die "Error reading of $i_file failed" if $!;
475+
close(BINFILE);
476+
die "Error closing $i_file failed" if $!;
477+
478+
return $data;
479+
}
480+
400481
1;

src/build/buildpnor/genPnorImages.pl

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
use Cwd qw(abs_path cwd);
3131
use lib dirname abs_path($0);
3232
use PnorUtils qw(loadPnorLayout getNumber traceErr trace run_command PAGE_SIZE
33-
loadBinFiles findLayoutKeyByEyeCatch checkSpaceConstraints);
33+
loadBinFiles findLayoutKeyByEyeCatch checkSpaceConstraints
34+
getSwSignatures getBinDataFromFile);
3435
use Getopt::Long qw(:config pass_through);
3536

3637
################################################################################
@@ -88,6 +89,35 @@
8889
exit 0;
8990
}
9091

92+
# Hardcoded defined order that binfiles should be handled.
93+
my %partitionDeps = ( HBB => 0,
94+
HBI => 1);
95+
96+
# Custom sort to ensure images are handled in the correct dependency order.
97+
# If a dependency is not specified in the hash used, use default behavior.
98+
sub partitionDepSort
99+
{
100+
# If $a exists but $b does not, set $a < $b
101+
if (exists $partitionDeps{$a} && !exists $partitionDeps{$b})
102+
{
103+
-1
104+
}
105+
# If $a does not exists but $b does, set $a > $b
106+
elsif (!exists $partitionDeps{$a} && exists $partitionDeps{$b})
107+
{
108+
1
109+
}
110+
# If both $a and $b exist, actually compare values.
111+
elsif (exists $partitionDeps{$a} && exists $partitionDeps{$b})
112+
{
113+
if ($partitionDeps{$a} < $partitionDeps{$b}) {-1}
114+
elsif ($partitionDeps{$a} > $partitionDeps{$b}) {1}
115+
else {0}
116+
}
117+
# If neither $a or $b have a dependency, order doesn't matter
118+
else {0}
119+
}
120+
91121
################################################################################
92122
# main
93123
################################################################################
@@ -173,14 +203,15 @@ sub manipulateImages
173203
VFS_MODULE_TABLE => => "$bin_dir/$parallelPrefix.vfs_module_table.bin",
174204
TEMP_BIN => "$bin_dir/$parallelPrefix.temp.bin",
175205
PAYLOAD_TEXT => "$bin_dir/$parallelPrefix.payload_text.bin",
176-
PROTECTED_PAYLOAD => "$bin_dir/$parallelPrefix.protected_payload.bin"
206+
PROTECTED_PAYLOAD => "$bin_dir/$parallelPrefix.protected_payload.bin",
207+
HBB_SW_SIG_FILE => "$bin_dir/$parallelPrefix.hbb_sw_sig.bin"
177208
);
178209

179210
# Partitions that have a hash page table at the beginning of the section
180211
# for secureboot purposes.
181212
my %hashPageTablePartitions = (HBI => 1);
182213

183-
foreach my $key ( keys %{$i_binFilesRef})
214+
foreach my $key (sort partitionDepSort keys %{$i_binFilesRef})
184215
{
185216
my $layoutKey = findLayoutKeyByEyeCatch($key, \%$i_pnorLayoutRef);
186217
my $eyeCatch = $sectionHash{$layoutKey}{eyeCatch};
@@ -213,7 +244,16 @@ sub manipulateImages
213244
{
214245
if (exists $hashPageTablePartitions{$eyeCatch})
215246
{
216-
$tempImages{hashPageTable} = genHashPageTable($bin_file, $eyeCatch);
247+
if ($eyeCatch eq "HBI")
248+
{
249+
# Pass HBB sw signatures as the salt entry.
250+
$tempImages{hashPageTable} = genHashPageTable($bin_file, $eyeCatch,
251+
getBinDataFromFile($tempImages{HBB_SW_SIG_FILE}));
252+
}
253+
else
254+
{
255+
$tempImages{hashPageTable} = genHashPageTable($bin_file, $eyeCatch);
256+
}
217257
}
218258
# Add hash page table
219259
if ($tempImages{hashPageTable} ne "" && -e $tempImages{hashPageTable})
@@ -231,7 +271,6 @@ sub manipulateImages
231271
my $padSize = PAGE_SIZE - (($hashPageTableSize + VFS_MODULE_TABLE_MAX_SIZE) % PAGE_SIZE);
232272
run_command("dd if=/dev/zero bs=$padSize count=1 | tr \"\\000\" \"\\377\" >> $tempImages{hashPageTable} ");
233273

234-
235274
# Payload text section
236275
run_command("cat $tempImages{hashPageTable} $tempImages{VFS_MODULE_TABLE} > $tempImages{PAYLOAD_TEXT} ");
237276
}
@@ -260,6 +299,13 @@ sub manipulateImages
260299
if($eyeCatch eq "HBB")
261300
{
262301
run_command("echo \"000000000007EF8000000000080000000000000008280000\" | xxd -r -ps -seek 6 - $tempImages{HDR_PHASE}");
302+
# Save off HBB sw signatures for use by HBI
303+
open (HBB_SW_SIG_FILE, ">", $tempImages{HBB_SW_SIG_FILE}) or die "Error opening file $tempImages{HBB_SW_SIG_FILE}: $!\n";
304+
binmode HBB_SW_SIG_FILE;
305+
print HBB_SW_SIG_FILE getSwSignatures($tempImages{HDR_PHASE});
306+
die "Error reading of $tempImages{HBB_SW_SIG_FILE} failed" if $!;
307+
close HBB_SW_SIG_FILE;
308+
die "Error closing of $tempImages{HBB_SW_SIG_FILE} failed" if $!;
263309
}
264310
}
265311
# Add simiple version header
@@ -425,7 +471,7 @@ sub truncate_sha
425471
################################################################################
426472
sub genHashPageTable
427473
{
428-
my ($bin_file, $eyeCatch) = @_;
474+
my ($bin_file, $eyeCatch, $saltData) = @_;
429475

430476
# Open the file
431477
my $hashPageTableFile = "$bin_dir/$eyeCatch.page_hash_table";
@@ -437,11 +483,20 @@ sub genHashPageTable
437483

438484
# Enter Salt as first entry
439485
my $salt_entry = 0;
440-
for (my $i = 0; $i < SHA_TRUNCATE_SIZE; $i++)
486+
if (defined $saltData)
487+
{
488+
# Use input salt data
489+
$salt_entry = truncate_sha(sha512($saltData));
490+
}
491+
else
441492
{
442-
$salt_entry .= sha512(rand(0x7FFFFFFFFFFFFFFF));
493+
# Generate random salt data
494+
for (my $i = 0; $i < SHA_TRUNCATE_SIZE; $i++)
495+
{
496+
$salt_entry .= sha512(rand(0x7FFFFFFFFFFFFFFF));
497+
}
498+
$salt_entry = truncate_sha(sha512($salt_entry));
443499
}
444-
$salt_entry = truncate_sha(sha512($salt_entry));
445500
my @hashes = ($salt_entry);
446501
print OUTBINFILE $salt_entry;
447502

src/build/mkrules/hbfw/img/makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ GEN_PNOR_IMAGE_SCRIPT = ${genPnorImages.pl:P}
8888
# Paramemter passed into GEN_PNOR_IMAGE_SCRIPT. Note zero filled images pass
8989
# EMPTY as their file name. This is so the script knows it needs to generate
9090
# them, rather than use an input.
91+
# Note: HBI depends on HBB for sw signatures. Ensure that both are passed into
92+
# the same --systemBinFiles parameter for genPnorImages
9193
GEN_DEFAULT_BIN_FILES = HBB=${BASE_IMAGE},HBI=${EXT_IMAGE},TEST=EMPTY,TESTRO=EMPTY,HBEL=EMPTY,GUARD=EMPTY,GLOBAL=EMPTY,PAYLOAD=EMPTY,CVPD=EMPTY,MVPD=EMPTY,DJVPD=EMPTY
9294
DEFAULT_PNOR_LAYOUT = ${defaultPnorLayout.xml:P}
9395

0 commit comments

Comments
 (0)