Skip to contentSkip to conversation

Enable TDX measurement to RTMR register via EFI_CC_MEASUREMENT_PROTOCOL

PATCH Superseded Lu Ken 2 participants 3 messages Dec 29, 2021 – Mar 8, 2022

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Lu Ken

author
#0 lore raw

PATCH 1/1 Enable TDX measurement to RTMR register via EFI_CC_MEASUREMENT_PROTOCOL

Intel Trust Domain Extensions(Intel TDX) refers to an Intel technology that extends Virtual Machine Extensions(VMX) and Multi-Key Total Memory Encryption(MK-TME) with a new kind of virtual machine guest called a Trust Domain(TD)[1]. A TD runs in a CPU mode that protects the confidentiality of its memory contents and its CPU state from any other software, including the hosting Virtual Machine Monitor (VMM).

Trust Domain Virtual Firmware (TDVF)[2] is required to provide TD services to
the TD guest OS. Its reference code is available at https://github.com/tianocore/edk2-staging/tree/TDVF.

To support TDX attestation, below 4 RTMR registers are used like TPM/TPM2:
- RTMR[0] is for TDVF configuration
- RTMR[1] is for the TD OS loader and kernel
- RTMR[2] is for the OS application
- RTMR[3] is reserved for special usage only

This patch adds TDX measurement via EFI_CC_MEASUREMENT_PROTOCOL[3] along with TPM/TPM2 TCG protocol.

References:
[1] https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-whitepaper-v4.pdf
[2] https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-virtual-firmware-design-guide-rev-1.pdf
[3] https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Protocol/CcMeasurement.h

---
 grub-core/commands/efi/tpm.c |  44 ++++++++++
 include/grub/efi/cc.h        | 156 +++++++++++++++++++++++++++++++++++
 2 files changed, 200 insertions(+)
 create mode 100644 include/grub/efi/cc.h
grub-core/commands/efi/tpm.c +44 -0
index a97d85368..33b70e791 100644
@@ -22,6 +22,7 @@
 #include <grub/i18n.h>
 #include <grub/efi/api.h>
 #include <grub/efi/efi.h>
+#include <grub/efi/cc.h>
 #include <grub/efi/tpm.h>
 #include <grub/mm.h>
 #include <grub/tpm.h>
@@ -31,6 +32,8 @@ typedef TCG_PCR_EVENT grub_tpm_event_t;
 
 static grub_efi_guid_t tpm_guid = EFI_TPM_GUID;
 static grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID;
+static grub_efi_guid_t tdx_guid = EFI_TPM2_GUID;
+static grub_efi_guid_t cc_measurement_guid = EFI_CC_MEASUREMENT_PROTOCOL_GUID;
 
 static grub_efi_handle_t *grub_tpm_handle;
 static grub_uint8_t grub_tpm_version;
@@ -221,6 +224,45 @@ grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,
   return grub_efi_log_event_status (status);
 }
 
+static
+grub_err_t
+grub_cc_log_event (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
+                   const char *description)
+{
+  EFI_CC_EVENT *event;
+  grub_efi_status_t status;
+  grub_efi_cc_protocol_t *cc;
+  EFI_CC_MR_INDEX mr;
+
+  cc = grub_efi_locate_protocol (&cc_measurement_guid, NULL);
+
+  if (!cc)
+    return 0;
+
+  status = efi_call_3 (cc->map_pcr_to_mr_index, cc, pcr, &mr);
+  if (status != GRUB_EFI_SUCCESS)
+    return grub_efi_log_event_status (status);
+
+  event = grub_zalloc (sizeof (EFI_CC_EVENT) + grub_strlen (description) + 1);
+  if (!event)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+                       N_ ("cannot allocate CC event buffer"));
+
+  event->Header.HeaderSize = sizeof (EFI_CC_EVENT_HEADER);
+  event->Header.HeaderVersion = EFI_CC_EVENT_HEADER_VERSION;
+  event->Header.MrIndex = mr;
+  event->Header.EventType = EV_IPL;
+  event->Size = sizeof (*event) - sizeof (event->Event)
+                + grub_strlen (description) + 1;
+  grub_memcpy (event->Event, description, grub_strlen (description) + 1);
+
+  status = efi_call_5 (cc->hash_log_extend_event, cc, 0, (unsigned long)buf,
+                       (grub_uint64_t)size, event);
+  grub_free (event);
+
+  return grub_efi_log_event_status (status);
+}
+
 grub_err_t
 grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
 		    const char *description)
@@ -228,6 +270,8 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
   grub_efi_handle_t tpm_handle;
   grub_efi_uint8_t protocol_version;
 
+  grub_cc_log_event(buf, size, pcr, description);
+
   if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
     return 0;
 
include/grub/efi/cc.h new file +156 -0

156 lines not on this page · raw message

Signature · 1 line
2.27.0
#1 lore raw

grub-core/commands/efi/tpm.c

Quoted 35 lines · Lu Ken

Intel Trust Domain Extensions(Intel TDX) refers to an Intel technology that extends Virtual Machine Extensions(VMX) and Multi-Key Total Memory Encryption(MK-TME) with a new kind of virtual machine guest called a Trust Domain(TD)[1]. A TD runs in a CPU mode that protects the confidentiality of its memory contents and its CPU state from any other software, including the hosting Virtual Machine Monitor (VMM).

Trust Domain Virtual Firmware (TDVF)[2] is required to provide TD services to
the TD guest OS. Its reference code is available at https://github.com/tianocore/edk2-staging/tree/TDVF.

To support TDX attestation, below 4 RTMR registers are used like TPM/TPM2:
- RTMR[0] is for TDVF configuration
- RTMR[1] is for the TD OS loader and kernel
- RTMR[2] is for the OS application
- RTMR[3] is reserved for special usage only

This patch adds TDX measurement via EFI_CC_MEASUREMENT_PROTOCOL[3] along with TPM/TPM2 TCG protocol.

References:
[1] https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-whitepaper-v4.pdf
[2] https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-virtual-firmware-design-guide-rev-1.pdf
[3] https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Protocol/CcMeasurement.h

---
 grub-core/commands/efi/tpm.c |  44 ++++++++++
 include/grub/efi/cc.h        | 156 +++++++++++++++++++++++++++++++++++
 2 files changed, 200 insertions(+)
 create mode 100644 include/grub/efi/cc.h
grub-core/commands/efi/tpm.c +0 -0
index a97d85368..33b70e791 100644

This is Intel specific thing (is not it?) and should not be build into generic TPM module. Please create separate TDX module, e.g., grub-core/commands/i386/efi/tdx.c.

Quoted 24 lines
grub-core/commands/efi/tpm.c +6 -0
@@ -22,6 +22,7 @@
 #include <grub/i18n.h>
 #include <grub/efi/api.h>
 #include <grub/efi/efi.h>
+#include <grub/efi/cc.h>
 #include <grub/efi/tpm.h>
 #include <grub/mm.h>
 #include <grub/tpm.h>
@@ -31,6 +32,8 @@ typedef TCG_PCR_EVENT grub_tpm_event_t;
 
 static grub_efi_guid_t tpm_guid = EFI_TPM_GUID;
 static grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID;
+static grub_efi_guid_t tdx_guid = EFI_TPM2_GUID;
+static grub_efi_guid_t cc_measurement_guid = EFI_CC_MEASUREMENT_PROTOCOL_GUID;
 
 static grub_efi_handle_t *grub_tpm_handle;
 static grub_uint8_t grub_tpm_version;
@@ -221,6 +224,45 @@ grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,
   return grub_efi_log_event_status (status);
 }
 
+static
+grub_err_t
+grub_cc_log_event (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,

grub-core/commands/efi/tpm.c grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, · line 239

Quoted 10 lines
grub-core/commands/efi/tpm.c +10 -0
+                   const char *description)
+{
+  EFI_CC_EVENT *event;
+  grub_efi_status_t status;
+  grub_efi_cc_protocol_t *cc;
+  EFI_CC_MR_INDEX mr;
+
+  cc = grub_efi_locate_protocol (&cc_measurement_guid, NULL);
+
+  if (!cc)

Please use NULL explicitly, i.e., if (cc == NULL)

grub-core/commands/efi/tpm.c grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, · line 247

Quoted 8 lines
grub-core/commands/efi/tpm.c +8 -0
+    return 0;
+
+  status = efi_call_3 (cc->map_pcr_to_mr_index, cc, pcr, &mr);
+  if (status != GRUB_EFI_SUCCESS)
+    return grub_efi_log_event_status (status);
+
+  event = grub_zalloc (sizeof (EFI_CC_EVENT) + grub_strlen (description) + 1);
+  if (!event)

Ditto.

grub-core/commands/efi/tpm.c grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, · line 259

Quoted 12 lines
grub-core/commands/efi/tpm.c +12 -0
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+                       N_ ("cannot allocate CC event buffer"));
+
+  event->Header.HeaderSize = sizeof (EFI_CC_EVENT_HEADER);
+  event->Header.HeaderVersion = EFI_CC_EVENT_HEADER_VERSION;
+  event->Header.MrIndex = mr;
+  event->Header.EventType = EV_IPL;
+  event->Size = sizeof (*event) - sizeof (event->Event)
+                + grub_strlen (description) + 1;
+  grub_memcpy (event->Event, description, grub_strlen (description) + 1);
+
+  status = efi_call_5 (cc->hash_log_extend_event, cc, 0, (unsigned long)buf,

What does 0 mean? Could use a constant here?

Additionally, "(unsigned long)buf" cast looks incorrect. I think it should be "grub_efi_physical_address_t (buf)".

grub-core/commands/efi/tpm.c grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, · line 260

Quoted 1 line
grub-core/commands/efi/tpm.c +1 -0
+                       (grub_uint64_t)size, event);

"(grub_efi_uint64_t) size" instead of "(grub_uint64_t)size"

Quoted 23 lines
grub-core/commands/efi/tpm.c +7 -0
+  grub_free (event);
+
+  return grub_efi_log_event_status (status);
+}
+
 grub_err_t
 grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
 		    const char *description)
@@ -228,6 +270,8 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
   grub_efi_handle_t tpm_handle;
   grub_efi_uint8_t protocol_version;
 
+  grub_cc_log_event(buf, size, pcr, description);
+
   if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
     return 0;
 
include/grub/efi/cc.h new file +0 -0
new file mode 100644
index 000000000..e49cdf5d9

include/grub/efi/cc.h line 3

Quoted 3 lines
include/grub/efi/cc.h new file +3 -0
@@ -0,0 +1,156 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2015  Free Software Foundation, Inc.

s/2015/2022/

Quoted 7 lines
include/grub/efi/cc.h new file +7 -0
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,

include/grub/efi/cc.h line 20

Quoted 10 lines
include/grub/efi/cc.h new file +10 -0
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_EFI_CC_HEADER
+#define GRUB_EFI_CC_HEADER 1

Include all headers here which define all types, structures, etc. used below.

include/grub/efi/cc.h line 22

Quoted 1 line
include/grub/efi/cc.h new file +1 -0
+#define EFI_CC_MEASUREMENT_PROTOCOL_GUID \

s/EFI_CC_MEASUREMENT_PROTOCOL_GUID/GRUB_EFI_CC_MEASUREMENT_PROTOCOL_GUID/

include/grub/efi/cc.h line 31

Quoted 9 lines
include/grub/efi/cc.h new file +9 -0
+  { 0x96751a3d, 0x72f4, 0x41a6, \
+    { 0xa7, 0x94, 0xed, 0x5d, 0x0e, 0x67, 0xae, 0x6b } \
+  };
+
+typedef struct
+{
+  grub_efi_uint8_t Major;
+  grub_efi_uint8_t Minor;
+} EFI_CC_VERSION;

Are you entirely sure this structure should not be packed? And I can see similar thing in the EDK2 implementation.

And in general the definition should look more or less like this:

struct grub_efi_cc_version
{
grub_efi_uint8_t major;
grub_efi_uint8_t minor;
};
typedef struct grub_efi_cc_version_t;

include/grub/efi/cc.h line 38

Quoted 7 lines
include/grub/efi/cc.h new file +7 -0
+
+/*
+ * EFI_CC Type/SubType definition
+ */
+#define EFI_CC_TYPE_NONE 0
+#define EFI_CC_TYPE_SEV 1
+#define EFI_CC_TYPE_TDX 2

Please add "GRUB_" prefix to this definitions and align values in one column with tabs.

include/grub/efi/cc.h line 44

Quoted 5 lines
include/grub/efi/cc.h new file +5 -0
+typedef struct
+{
+  grub_efi_uint8_t Type;
+  grub_efi_uint8_t SubType;
+} EFI_CC_TYPE;

Same questions and suggestions like in case of EFI_CC_VERSION struct.

include/grub/efi/cc.h line 46

Quoted 1 line
include/grub/efi/cc.h new file +1 -0
+typedef grub_efi_uint32_t EFI_CC_EVENT_LOG_BITMAP;

typedef grub_efi_uint32_t grub_efi_cc_event_log_bitmap;

And please update all typedefs below accordingly.

Quoted 6 lines
include/grub/efi/cc.h new file +6 -0
+typedef grub_efi_uint32_t EFI_CC_EVENT_LOG_FORMAT;
+typedef grub_efi_uint32_t EFI_CC_EVENT_ALGORITHM_BITMAP;
+typedef grub_efi_uint32_t EFI_CC_MR_INDEX;
+
+/*
+ * Intel TDX measure register index

include/grub/efi/cc.h line 62

Quoted 10 lines
include/grub/efi/cc.h new file +10 -0
+ */
+#define TDX_MR_INDEX_MRTD 0
+#define TDX_MR_INDEX_RTMR0 1
+#define TDX_MR_INDEX_RTMR1 2
+#define TDX_MR_INDEX_RTMR2 3
+#define TDX_MR_INDEX_RTMR3 4
+
+#define EFI_CC_EVENT_LOG_FORMAT_TCG_2 0x00000002
+#define EFI_CC_BOOT_HASH_ALG_SHA384 0x00000004
+#define EFI_CC_EVENT_HEADER_VERSION 1

Please add to all defines above "GRUB_" prefix.

Quoted 13 lines
include/grub/efi/cc.h new file +13 -0
+typedef struct tdEFI_CC_EVENT_HEADER
+{
+  /*
+   * Size of the event header itself (sizeof(EFI_TD_EVENT_HEADER))
+   */
+  grub_efi_uint32_t HeaderSize;
+
+  /*
+   * Header version. For this version of this specification,
+   * the value shall be 1.
+   */
+  grub_efi_uint16_t HeaderVersion;
+

include/grub/efi/cc.h line 86

Quoted 10 lines
include/grub/efi/cc.h new file +10 -0
+  /*
+   * Index of the MR that shall be extended
+   */
+  EFI_CC_MR_INDEX MrIndex;
+
+  /*
+   * Type of the event that shall be extended (and optionally logged)
+   */
+  grub_efi_uint32_t EventType;
+} GRUB_PACKED EFI_CC_EVENT_HEADER;

Please define this type as I showed above.

include/grub/efi/cc.h line 97

Quoted 10 lines
include/grub/efi/cc.h new file +10 -0
+typedef struct tdEFI_CC_EVENT
+{
+  /*
+   * Total size of the event including the Size component, the header and the
+   * Event data.
+   */
+  grub_efi_uint32_t Size;
+  EFI_CC_EVENT_HEADER Header;
+  grub_efi_uint8_t Event[1];
+} GRUB_PACKED EFI_CC_EVENT;

Ditto. Additionally, please align member names in one column.

Quoted 24 lines
include/grub/efi/cc.h new file +24 -0
+typedef struct
+{
+  /* Allocated size of the structure */
+  grub_efi_uint8_t Size;
+
+  /*
+   * Version of the EFI_CC_BOOT_SERVICE_CAPABILITY structure itself.
+   * For this version of the protocol, the Major version shall be set to 1
+   * and the Minor version shall be set to 1.
+   */
+  EFI_CC_VERSION StructureVersion;
+
+  /*
+   * Version of the EFI TD protocol.
+   * For this version of the protocol, the Major version shall be set to 1
+   * and the Minor version shall be set to 1.
+   */
+  EFI_CC_VERSION ProtocolVersion;
+
+  /*
+   * Supported hash algorithms
+   */
+  EFI_CC_EVENT_ALGORITHM_BITMAP HashAlgorithmBitmap;
+

include/grub/efi/cc.h line 132

Quoted 10 lines
include/grub/efi/cc.h new file +10 -0
+  /*
+   * Bitmap of supported event log formats
+   */
+  EFI_CC_EVENT_LOG_BITMAP SupportedEventLogs;
+
+  /*
+   * Indicates the CC type
+   */
+  EFI_CC_TYPE CcType;
+} EFI_CC_BOOT_SERVICE_CAPABILITY;

Same comments as above.

Quoted 4 lines
include/grub/efi/cc.h new file +4 -0
+struct grub_efi_cc_protocol
+{
+  grub_efi_status_t (*get_capability) (
+      struct grub_efi_cc_protocol *this,

include/grub/efi/cc.h line 147

Quoted 10 lines
include/grub/efi/cc.h new file +10 -0
+      EFI_CC_BOOT_SERVICE_CAPABILITY *ProtocolCapability);
+  grub_efi_status_t (*get_event_log) (
+      struct grub_efi_cc_protocol *this,
+      EFI_CC_EVENT_LOG_FORMAT EventLogFormat,
+      grub_efi_physical_address_t *EventLogLocation,
+      grub_efi_physical_address_t *EventLogLastEntry,
+      grub_efi_boolean_t *EventLogTruncated);
+  grub_efi_status_t (*hash_log_extend_event) (
+      struct grub_efi_cc_protocol *this, grub_efi_uint64_t Flags,
+      grub_efi_physical_address_t DataToHash, grub_efi_uint64_t DataToHashLen,

Could you put one argument in one line like, e.g., for get_event_log function?

include/grub/efi/cc.h line 153

Quoted 6 lines
include/grub/efi/cc.h new file +6 -0
+      EFI_CC_EVENT *EfiCcEvent);
+  grub_efi_status_t (*map_pcr_to_mr_index) (struct grub_efi_cc_protocol *this,
+                                            grub_efi_uint32_t PcrIndex,
+                                            EFI_CC_MR_INDEX *MrIndex);
+};
+

Please drop this empty line.

include/grub/efi/cc.h line 154

Quoted 1 line
include/grub/efi/cc.h new file +1 -0
+typedef struct grub_efi_cc_protocol grub_efi_cc_protocol_t;

Daniel

author
#2 lore raw

Thanks Kiper's review and comment, sorry for later response since vacation. Please see my answer in below, and will submit new patch soon.

Quoted 48 lines

-----Original Message-----
From: Daniel Kiper <[email protected]>
Sent: Friday, February 25, 2022 2:47 AM
To: Lu, Ken <[email protected]>
Cc: [email protected]; Xu, Min M <[email protected]>
Subject: Re: [PATCH] Enable TDX measurement to RTMR register via EFI_CC_MEASUREMENT_PROTOCOL

Quoted 10 lines · Lu Ken

Intel Trust Domain Extensions(Intel TDX) refers to an Intel technology that extends Virtual Machine Extensions(VMX) and Multi-Key Total Memory
Encryption(MK-TME) with a new kind of virtual machine guest called a Trust Domain(TD)[1]. A TD runs in a CPU mode that protects the confidentiality of its memory contents and its CPU state from any other software, including the hosting Virtual Machine Monitor (VMM).

Trust Domain Virtual Firmware (TDVF)[2] is required to provide TD services to the TD guest OS. Its reference code is available at

https://github.com/tianocore/edk2-staging/tree/TDVF.

Quoted 20 lines

To support TDX attestation, below 4 RTMR registers are used like TPM/TPM2:
- RTMR[0] is for TDVF configuration
- RTMR[1] is for the TD OS loader and kernel
- RTMR[2] is for the OS application
- RTMR[3] is reserved for special usage only

This patch adds TDX measurement via EFI_CC_MEASUREMENT_PROTOCOL[3] along with TPM/TPM2 TCG protocol.

References:
[1]
https://software.intel.com/content/dam/develop/external/us/en/document
s/tdx-whitepaper-v4.pdf [2]
https://software.intel.com/content/dam/develop/external/us/en/document
s/tdx-virtual-firmware-design-guide-rev-1.pdf
[3]
https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Protocol/
CcMeasurement.h

Quoted 4 lines
---
 grub-core/commands/efi/tpm.c |  44 ++++++++++
 include/grub/efi/cc.h        | 156

+++++++++++++++++++++++++++++++++++

Quoted 3 lines
 2 files changed, 200 insertions(+)
 create mode 100644 include/grub/efi/cc.h
Quoted 8 lines

grub-core/commands/efi/tpm.c

Quoted 4 lines
(quoted diff) +0 -0

b/grub-core/commands/efi/tpm.c index a97d85368..33b70e791 100644

grub-core/commands/efi/tpm.c +0 -0

This is Intel specific thing (is not it?) and should not be build into generic TPM module. Please create separate TDX module, e.g., grub- core/commands/i386/efi/tdx.c.

[Lu, Ken] EFI_CC_MEASUREMENT_PROTOCOL is not Intel specific, but agreement between AMD/Intel and ARM, please see https://www.mail-archive.com/[email protected]/msg38720.html

Quoted 22 lines
Quoted 22 lines
grub-core/commands/efi/tpm.c +4 -0
@@ -22,6 +22,7 @@
 #include <grub/i18n.h>
 #include <grub/efi/api.h>
 #include <grub/efi/efi.h>
+#include <grub/efi/cc.h>
 #include <grub/efi/tpm.h>
 #include <grub/mm.h>
 #include <grub/tpm.h>
@@ -31,6 +32,8 @@ typedef TCG_PCR_EVENT grub_tpm_event_t;
 
 static grub_efi_guid_t tpm_guid = EFI_TPM_GUID;  static
 grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID;
+static grub_efi_guid_t tdx_guid = EFI_TPM2_GUID; static
+grub_efi_guid_t cc_measurement_guid =
+EFI_CC_MEASUREMENT_PROTOCOL_GUID;
 
 static grub_efi_handle_t *grub_tpm_handle;  static grub_uint8_t

grub_tpm_version; @@ -221,6 +224,45 @@ grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,
return grub_efi_log_event_status (status); }

Quoted 14 lines

grub-core/commands/efi/tpm.c grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf, · line 239

Quoted 12 lines
grub-core/commands/efi/tpm.c +12 -0
+static
+grub_err_t
+grub_cc_log_event (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
+                   const char *description) {
+  EFI_CC_EVENT *event;
+  grub_efi_status_t status;
+  grub_efi_cc_protocol_t *cc;
+  EFI_CC_MR_INDEX mr;
+
+  cc = grub_efi_locate_protocol (&cc_measurement_guid, NULL);
+
+  if (!cc)

Please use NULL explicitly, i.e., if (cc == NULL)

[Lu, Ken] Thanks for your comment, will fix in next version.

Quoted 11 lines

grub-core/commands/efi/tpm.c grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,

Quoted 9 lines
grub-core/commands/efi/tpm.c +8 -0
+    return 0;
+
+  status = efi_call_3 (cc->map_pcr_to_mr_index, cc, pcr, &mr);  if
+ (status != GRUB_EFI_SUCCESS)
+    return grub_efi_log_event_status (status);
+
+  event = grub_zalloc (sizeof (EFI_CC_EVENT) + grub_strlen
+ (description) + 1);  if (!event)

Ditto.

[Lu, Ken] Thanks for your comment, will fix in next version.

Quoted 15 lines

grub-core/commands/efi/tpm.c grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,

Quoted 13 lines
grub-core/commands/efi/tpm.c +12 -0
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+                       N_ ("cannot allocate CC event buffer"));
+
+  event->Header.HeaderSize = sizeof (EFI_CC_EVENT_HEADER);
+ event->Header.HeaderVersion = EFI_CC_EVENT_HEADER_VERSION;
+ event->Header.MrIndex = mr;  event->Header.EventType = EV_IPL;
+ event->Size = sizeof (*event) - sizeof (event->Event)
+                + grub_strlen (description) + 1;  grub_memcpy
+ (event->Event, description, grub_strlen (description) + 1);
+
+  status = efi_call_5 (cc->hash_log_extend_event, cc, 0, (unsigned
+ long)buf,

What does 0 mean? Could use a constant here?

[Lu, Ken] It is defined at https://github.com/tianocore/edk2/blob/62fa37fe7b9df3c54a2d9d90aed9ff0e817ee0c6/MdePkg/Include/Protocol/CcMeasurement.h#L197 from the TCG specification, https://trustedcomputinggroup.org/wp-content/uploads/EFI-Protocol-Specification-rev13-160330final.pdf page 33, the flag value could be 0 or below:

#define EFI_TCG2_EXTEND_ONLY 0x0000000000000001
#define PE_COFF_IMAGE 0x0000000000000010

Similar code for TPM at https://git.savannah.gnu.org/cgit/grub.git/tree/grub-core/commands/efi/tpm.c?h=grub-2.06#n217

Quoted 3 lines

Additionally, "(unsigned long)buf" cast looks incorrect. I think it should be "grub_efi_physical_address_t (buf)".

[Lu, Ken] Thanks for your comment, yes, grub_efi_physical_address_t is much better.

Quoted 4 lines
Quoted 2 lines

+ (grub_uint64_t)size, event);

"(grub_efi_uint64_t) size" instead of "(grub_uint64_t)size"

[Lu, Ken] Thanks for your comment, will do

Quoted 18 lines

grub-core/commands/efi/tpm.c grub_tpm_measure (unsigned char *buf, grub_size_t

Quoted 9 lines
grub-core/commands/efi/tpm.c +4 -0
+  grub_free (event);
+
+  return grub_efi_log_event_status (status); }
+
 grub_err_t
 grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
 		    const char *description)
@@ -228,6 +270,8 @@ grub_tpm_measure (unsigned char *buf, grub_size_t

size, grub_uint8_t pcr,

Quoted 8 lines
grub-core/commands/efi/tpm.c +2 -0
   grub_efi_handle_t tpm_handle;
   grub_efi_uint8_t protocol_version;
 
+  grub_cc_log_event(buf, size, pcr, description);
+
   if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
     return 0;
 
Quoted 10 lines

include/grub/efi/cc.h line 3

Quoted 8 lines
include/grub/efi/cc.h new file renamed from include/grub/efi/cc.h +0 -0

mode 100644 index 000000000..e49cdf5d9

include/grub/efi/cc.h new file +3 -0
@@ -0,0 +1,156 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2015  Free Software Foundation, Inc.

s/2015/2022/

[Lu, Ken] Thanks for your comment, will fix.

Quoted 13 lines
Quoted 13 lines
include/grub/efi/cc.h new file +12 -0
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published
+ by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
Quoted 8 lines

include/grub/efi/cc.h line 20

Quoted 6 lines
include/grub/efi/cc.h new file +6 -0
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_EFI_CC_HEADER
+#define GRUB_EFI_CC_HEADER 1

Include all headers here which define all types, structures, etc. used below.

[Lu, Ken] Thanks for your comment, will fix.

Quoted 6 lines

include/grub/efi/cc.h line 22

Quoted 2 lines
include/grub/efi/cc.h new file +1 -0
+#define EFI_CC_MEASUREMENT_PROTOCOL_GUID \

s/EFI_CC_MEASUREMENT_PROTOCOL_GUID/GRUB_EFI_CC_MEASUREMENT_PR OTOCOL_GUID/

Quoted 11 lines

include/grub/efi/cc.h line 31

Quoted 8 lines
include/grub/efi/cc.h new file +8 -0
+  { 0x96751a3d, 0x72f4, 0x41a6, \
+    { 0xa7, 0x94, 0xed, 0x5d, 0x0e, 0x67, 0xae, 0x6b } \  };
+
+typedef struct
+{
+  grub_efi_uint8_t Major;
+  grub_efi_uint8_t Minor;
+} EFI_CC_VERSION;

Are you entirely sure this structure should not be packed? And I can see similar thing in the EDK2 implementation.

[Lu, Ken] Confirmed with EDK2 owner @Xu, Min M, this structure is not required for pack. Both EFI_TCG2_VERSION and EFI_CC_VERSION are not packed.

Quoted 10 lines

And in general the definition should look more or less like this:

struct grub_efi_cc_version
{
grub_efi_uint8_t major;
grub_efi_uint8_t minor;
};
typedef struct grub_efi_cc_version_t;

Quoted 10 lines

include/grub/efi/cc.h line 38

Quoted 7 lines
include/grub/efi/cc.h new file +7 -0
+
+/*
+ * EFI_CC Type/SubType definition
+ */
+#define EFI_CC_TYPE_NONE 0
+#define EFI_CC_TYPE_SEV 1
+#define EFI_CC_TYPE_TDX 2

Please add "GRUB_" prefix to this definitions and align values in one column with tabs.

[Lu, Ken] Thanks for your comment, will fix.

Quoted 8 lines

include/grub/efi/cc.h line 44

Quoted 6 lines
include/grub/efi/cc.h new file +5 -0
+typedef struct
+{
+  grub_efi_uint8_t Type;
+  grub_efi_uint8_t SubType;
+} EFI_CC_TYPE;

Same questions and suggestions like in case of EFI_CC_VERSION struct.

[Lu, Ken] Like EFI_CC_VERSION, it is not required to pack after confirmed with edk2 owner.

Quoted 6 lines

include/grub/efi/cc.h line 46

Quoted 2 lines
include/grub/efi/cc.h new file +1 -0
+typedef grub_efi_uint32_t EFI_CC_EVENT_LOG_BITMAP;

typedef grub_efi_uint32_t grub_efi_cc_event_log_bitmap;

And please update all typedefs below accordingly.

[Lu, Ken] Thanks for your comment, will fix.

Quoted 11 lines
Quoted 11 lines
include/grub/efi/cc.h new file +10 -0
+typedef grub_efi_uint32_t EFI_CC_EVENT_LOG_FORMAT; typedef
+grub_efi_uint32_t EFI_CC_EVENT_ALGORITHM_BITMAP; typedef
+grub_efi_uint32_t EFI_CC_MR_INDEX;
+
+/*
+ * Intel TDX measure register index
+ */
+#define TDX_MR_INDEX_MRTD 0
+#define TDX_MR_INDEX_RTMR0 1
+#define TDX_MR_INDEX_RTMR1 2
Quoted 8 lines

include/grub/efi/cc.h

Quoted 6 lines
include/grub/efi/cc.h new file +6 -0
+#define TDX_MR_INDEX_RTMR2 3
+#define TDX_MR_INDEX_RTMR3 4
+
+#define EFI_CC_EVENT_LOG_FORMAT_TCG_2 0x00000002 #define
+EFI_CC_BOOT_HASH_ALG_SHA384 0x00000004 #define
+EFI_CC_EVENT_HEADER_VERSION 1

Please add to all defines above "GRUB_" prefix.

[Lu, Ken] Thanks for your comment, will fix.

Quoted 18 lines
Quoted 18 lines
include/grub/efi/cc.h new file +17 -0
+typedef struct tdEFI_CC_EVENT_HEADER
+{
+  /*
+   * Size of the event header itself (sizeof(EFI_TD_EVENT_HEADER))
+   */
+  grub_efi_uint32_t HeaderSize;
+
+  /*
+   * Header version. For this version of this specification,
+   * the value shall be 1.
+   */
+  grub_efi_uint16_t HeaderVersion;
+
+  /*
+   * Index of the MR that shall be extended
+   */
+  EFI_CC_MR_INDEX MrIndex;
Quoted 8 lines

include/grub/efi/cc.h line 86

Quoted 6 lines
include/grub/efi/cc.h new file +6 -0
+
+  /*
+   * Type of the event that shall be extended (and optionally logged)
+   */
+  grub_efi_uint32_t EventType;
+} GRUB_PACKED EFI_CC_EVENT_HEADER;

Please define this type as I showed above.

[Lu, Ken] Thanks for your comment, will fix.

Quoted 60 lines

include/grub/efi/cc.h line 97

Quoted 11 lines
include/grub/efi/cc.h new file +10 -0
+typedef struct tdEFI_CC_EVENT
+{
+  /*
+   * Total size of the event including the Size component, the header and the
+   * Event data.
+   */
+  grub_efi_uint32_t Size;
+  EFI_CC_EVENT_HEADER Header;
+  grub_efi_uint8_t Event[1];
+} GRUB_PACKED EFI_CC_EVENT;

Ditto. Additionally, please align member names in one column.

Quoted 24 lines
include/grub/efi/cc.h new file +24 -0
+typedef struct
+{
+  /* Allocated size of the structure */
+  grub_efi_uint8_t Size;
+
+  /*
+   * Version of the EFI_CC_BOOT_SERVICE_CAPABILITY structure itself.
+   * For this version of the protocol, the Major version shall be set to 1
+   * and the Minor version shall be set to 1.
+   */
+  EFI_CC_VERSION StructureVersion;
+
+  /*
+   * Version of the EFI TD protocol.
+   * For this version of the protocol, the Major version shall be set to 1
+   * and the Minor version shall be set to 1.
+   */
+  EFI_CC_VERSION ProtocolVersion;
+
+  /*
+   * Supported hash algorithms
+   */
+  EFI_CC_EVENT_ALGORITHM_BITMAP HashAlgorithmBitmap;
+

include/grub/efi/cc.h line 132

Quoted 10 lines
include/grub/efi/cc.h new file +10 -0
+  /*
+   * Bitmap of supported event log formats
+   */
+  EFI_CC_EVENT_LOG_BITMAP SupportedEventLogs;
+
+  /*
+   * Indicates the CC type
+   */
+  EFI_CC_TYPE CcType;
+} EFI_CC_BOOT_SERVICE_CAPABILITY;

Same comments as above.

Quoted 9 lines
include/grub/efi/cc.h new file +9 -0
+struct grub_efi_cc_protocol
+{
+  grub_efi_status_t (*get_capability) (
+      struct grub_efi_cc_protocol *this,
+      EFI_CC_BOOT_SERVICE_CAPABILITY *ProtocolCapability);
+  grub_efi_status_t (*get_event_log) (
+      struct grub_efi_cc_protocol *this,
+      EFI_CC_EVENT_LOG_FORMAT EventLogFormat,
+      grub_efi_physical_address_t *EventLogLocation,
Quoted 8 lines

include/grub/efi/cc.h

Quoted 6 lines
include/grub/efi/cc.h new file +6 -0
+      grub_efi_physical_address_t *EventLogLastEntry,
+      grub_efi_boolean_t *EventLogTruncated);
+  grub_efi_status_t (*hash_log_extend_event) (
+      struct grub_efi_cc_protocol *this, grub_efi_uint64_t Flags,
+      grub_efi_physical_address_t DataToHash, grub_efi_uint64_t
+DataToHashLen,

Could you put one argument in one line like, e.g., for get_event_log function?

[Lu, Ken] Thanks for your comment, will fix.

Quoted 10 lines

include/grub/efi/cc.h line 143

Quoted 3 lines
include/grub/efi/cc.h new file +2 -0
+      EFI_CC_EVENT *EfiCcEvent);
+  grub_efi_status_t (*map_pcr_to_mr_index) (struct grub_efi_cc_protocol

*this,

include/grub/efi/cc.h line 153

Quoted 4 lines
include/grub/efi/cc.h new file +4 -0
+                                            grub_efi_uint32_t PcrIndex,
+                                            EFI_CC_MR_INDEX
+*MrIndex); };
+

Please drop this empty line.

[Lu, Ken] Thanks for your comment, will fix.

Quoted 4 lines

include/grub/efi/cc.h line 154

Quoted 2 lines
include/grub/efi/cc.h new file +1 -0
+typedef struct grub_efi_cc_protocol grub_efi_cc_protocol_t;

Daniel