@@ -61,13 +61,21 @@ def get_sha256_hash_of_wolfboot_image(file_path: str):
6161 return data [4 :4 + l ]
6262 data = data [4 + l :]
6363
64+ def get_sym_addr (elf_file : str , sym_name : str ) -> int :
65+ """
66+ get the address of a symbol from ELF file
67+ """
68+ symbols = subprocess .check_output (['nm' , elf_file ]).split (b'\n ' )
69+ matches = list (filter (lambda x : sym_name .encode () in x , symbols ))
70+ if not matches :
71+ return None
72+ return int (matches [0 ].split (b' ' )[0 ], 16 )
73+
6474def get_keystore_sym_addr () -> int :
6575 """
6676 get the address of symbol keystore from ELF file image
6777 """
68- symbols = subprocess .check_output (['nm' , 'stage1/loader_stage1.elf' ]).split (b'\n ' )
69- _start_keystore = int (list (filter (lambda x : b'_start_keystore' in x , symbols ))[0 ].split (b' ' )[0 ], 16 )
70- return _start_keystore
78+ return get_sym_addr ('stage1/loader_stage1.elf' , '_start_keystore' )
7179
7280def pcr_extend (pcr : bytearray , data : bytearray ) -> bytearray :
7381 """
@@ -95,23 +103,25 @@ def pcr_extend(pcr: bytearray, data: bytearray) -> bytearray:
95103
96104 pcr0 = bytearray (b'\x00 ' * 32 )
97105 if args .target == 'qemu' :
106+ # self_extend_pcr() in boot_x86_fsp.c
107+ # Hashes from _start_keystore to end of 4GB (keystore + vectors)
98108 keystore_addr = get_keystore_sym_addr ()
99109 keystore_off = addr_to_off (keystore_addr , image_size = len (image ))
100110 ibb = image [keystore_off :]
101- h = hashlib .sha256 ()
102- h .update (ibb )
103- pcr0_data_hash = h .digest ()
104- pcr0 = pcr_extend (b'\x00 ' * 32 , pcr0_data_hash )
111+ pcr0 = pcr_extend (pcr0 , get_sha256_hash (ibb ))
105112
106113 print (f"Initial PCR0: { pcr0 .hex ()} " )
107114
108115 is_stage1_auth_enabled = get_config_value (config , 'STAGE1_AUTH' ) == '1'
109116 print (f"stage1 auth is { 'enabled' if is_stage1_auth_enabled else 'disabled' } " )
110117
111- if is_stage1_auth_enabled :
118+ is_measured_boot = get_config_value (config , 'MEASURED_BOOT' ) == '1'
119+
120+ # wolfBoot_image_measure() extends PCR with wolfboot image hash
121+ if is_measured_boot :
112122 wb_hash = get_sha256_hash_of_wolfboot_image ('stage1/wolfboot_raw_v1_signed.bin' )
113123 pcr0 = pcr_extend (pcr0 , wb_hash )
114- print (f"PCR0 after wolfboot: { pcr0 .hex ()} " )
124+ print (f"PCR0 after wolfboot image measure : { pcr0 .hex ()} " )
115125
116126 # the pcrdigest needed by policy_sign tool is the hash of the concatenation of all PCRs involved in the policy.
117127 # we have only one PCR here
0 commit comments