3030use Cwd qw( abs_path cwd) ;
3131use lib dirname abs_path($0 );
3232use PnorUtils qw( loadPnorLayout getNumber traceErr trace run_command PAGE_SIZE
33- loadBinFiles findLayoutKeyByEyeCatch checkSpaceConstraints) ;
33+ loadBinFiles findLayoutKeyByEyeCatch checkSpaceConstraints
34+ getSwSignatures getBinDataFromFile) ;
3435use Getopt::Long qw( :config pass_through) ;
3536
3637# ###############################################################################
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# ###############################################################################
426472sub 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
0 commit comments