@@ -69,6 +69,11 @@ static struct {
6969#define TPM2_ALG_SHA512_FLAG (1 << 3)
7070#define TPM2_ALG_SM3_256_FLAG (1 << 4)
7171
72+ static const uint8_t ZeroGuid [16 ] = { 0 };
73+
74+ static UEFI_GPT_DATA * uefi_gpt_data ;
75+ static size_t uefi_gpt_data_size ;
76+
7277/*
7378 * TPM 2 logs are written in little endian format.
7479 */
@@ -925,6 +930,89 @@ uint32_t tpm_measure_bcv_mbr(uint32_t bootdrv, const uint8_t *addr,
925930 addr + 0x1b8 , 0x48 );
926931}
927932
933+ /*
934+ * This is the first function to call when measuring a GPT table.
935+ * It allocates memory for the data to log which are 'measured' later on.
936+ */
937+ void tpm_gpt_set_lba1 (const uint8_t * addr , uint32_t length )
938+ {
939+ if (!tpm_is_working ())
940+ return ;
941+
942+ SLOF_free_mem (uefi_gpt_data , uefi_gpt_data_size );
943+
944+ uefi_gpt_data_size = sizeof (UEFI_GPT_DATA );
945+ uefi_gpt_data = SLOF_alloc_mem (uefi_gpt_data_size );
946+ if (!uefi_gpt_data )
947+ return ;
948+
949+ memcpy (& uefi_gpt_data -> EfiPartitionHeader ,
950+ addr , sizeof (uefi_gpt_data -> EfiPartitionHeader ));
951+ uefi_gpt_data -> NumberOfPartitions = 0 ;
952+ }
953+
954+ /*
955+ * This function adds a GPT entry to the data to measure. It must
956+ * be called after tpm_gpt_set_lba1.
957+ */
958+ void tpm_gpt_add_entry (const uint8_t * addr , uint32_t length )
959+ {
960+ size_t sz ;
961+ UEFI_PARTITION_ENTRY * upe = (void * )addr ;
962+ void * tmp ;
963+
964+ if (!tpm_is_working () ||
965+ !uefi_gpt_data ||
966+ length < sizeof (* upe ) ||
967+ !memcmp (upe -> partTypeGuid , ZeroGuid , sizeof (ZeroGuid )))
968+ return ;
969+
970+ sz = offset_of (UEFI_GPT_DATA , Partitions ) +
971+ (uefi_gpt_data -> NumberOfPartitions + 1 )
972+ * sizeof (UEFI_PARTITION_ENTRY );
973+ if (sz > uefi_gpt_data_size ) {
974+ tmp = SLOF_alloc_mem (sz );
975+ if (!tmp )
976+ goto err_no_mem ;
977+
978+ memcpy (tmp , uefi_gpt_data , uefi_gpt_data_size );
979+ SLOF_free_mem (uefi_gpt_data , uefi_gpt_data_size );
980+ uefi_gpt_data = tmp ;
981+ uefi_gpt_data_size = sz ;
982+ }
983+
984+ memcpy (& uefi_gpt_data -> Partitions [uefi_gpt_data -> NumberOfPartitions ],
985+ addr ,
986+ sizeof (UEFI_PARTITION_ENTRY ));
987+ uefi_gpt_data -> NumberOfPartitions ++ ;
988+
989+ return ;
990+
991+ err_no_mem :
992+ SLOF_free_mem (uefi_gpt_data , uefi_gpt_data_size );
993+ uefi_gpt_data_size = 0 ;
994+ uefi_gpt_data = NULL ;
995+ }
996+
997+ /*
998+ * tpm_measure_gpt finally measures the GPT table and adds an entry
999+ * to the log.
1000+ */
1001+ uint32_t tpm_measure_gpt (void )
1002+ {
1003+ size_t sz ;
1004+
1005+ if (!tpm_is_working ())
1006+ return TCGBIOS_GENERAL_ERROR ;
1007+
1008+ sz = offset_of (UEFI_GPT_DATA , Partitions ) +
1009+ uefi_gpt_data -> NumberOfPartitions * sizeof (UEFI_PARTITION_ENTRY );
1010+
1011+ return tpm_add_measurement_to_log (5 , EV_EFI_GPT_EVENT ,
1012+ (const char * )uefi_gpt_data , sz ,
1013+ (const uint8_t * )uefi_gpt_data , sz );
1014+ }
1015+
9281016uint32_t tpm_measure_scrtm (void )
9291017{
9301018 uint32_t rc ;
0 commit comments