ed1787d
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
ed1787d
From: Lu Ken <ken.lu@intel.com>
ed1787d
Date: Wed, 13 Jul 2022 10:06:12 +0800
ed1787d
Subject: [PATCH] efi/tpm: Add EFI_CC_MEASUREMENT_PROTOCOL support
ed1787d
ed1787d
The EFI_CC_MEASUREMENT_PROTOCOL abstracts the measurement for virtual firmware
ed1787d
in confidential computing environment. It is similar to the EFI_TCG2_PROTOCOL.
ed1787d
It was proposed by Intel and ARM and approved by UEFI organization.
ed1787d
ed1787d
It is defined in Intel GHCI specification: https://cdrdv2.intel.com/v1/dl/getContent/726790 .
ed1787d
The EDKII header file is available at https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Protocol/CcMeasurement.h .
ed1787d
ed1787d
Signed-off-by: Lu Ken <ken.lu@intel.com>
ed1787d
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
ed1787d
(cherry picked from commit 4c76565b6cb885b7e144dc27f3612066844e2d19)
ed1787d
---
ed1787d
 grub-core/commands/efi/tpm.c |  48 ++++++++++++++
ed1787d
 include/grub/efi/cc.h        | 151 +++++++++++++++++++++++++++++++++++++++++++
ed1787d
 2 files changed, 199 insertions(+)
ed1787d
 create mode 100644 include/grub/efi/cc.h
ed1787d
ed1787d
diff --git a/grub-core/commands/efi/tpm.c b/grub-core/commands/efi/tpm.c
ed1787d
index bb59599721..ae09c1bf8b 100644
ed1787d
--- a/grub-core/commands/efi/tpm.c
ed1787d
+++ b/grub-core/commands/efi/tpm.c
ed1787d
@@ -22,6 +22,7 @@
ed1787d
 #include <grub/i18n.h>
ed1787d
 #include <grub/efi/api.h>
ed1787d
 #include <grub/efi/efi.h>
ed1787d
+#include <grub/efi/cc.h>
ed1787d
 #include <grub/efi/tpm.h>
ed1787d
 #include <grub/mm.h>
ed1787d
 #include <grub/tpm.h>
ed1787d
@@ -31,6 +32,7 @@ typedef TCG_PCR_EVENT grub_tpm_event_t;
ed1787d
 
ed1787d
 static grub_efi_guid_t tpm_guid = EFI_TPM_GUID;
ed1787d
 static grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID;
ed1787d
+static grub_efi_guid_t cc_measurement_guid = GRUB_EFI_CC_MEASUREMENT_PROTOCOL_GUID;
ed1787d
 
ed1787d
 static grub_efi_handle_t *grub_tpm_handle;
ed1787d
 static grub_uint8_t grub_tpm_version;
ed1787d
@@ -221,6 +223,50 @@ grub_tpm2_log_event (grub_efi_handle_t tpm_handle, unsigned char *buf,
ed1787d
   return grub_efi_log_event_status (status);
ed1787d
 }
ed1787d
 
ed1787d
+static void
ed1787d
+grub_cc_log_event (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
ed1787d
+		   const char *description)
ed1787d
+{
ed1787d
+  grub_efi_cc_event_t *event;
ed1787d
+  grub_efi_status_t status;
ed1787d
+  grub_efi_cc_protocol_t *cc;
ed1787d
+  grub_efi_cc_mr_index_t mr;
ed1787d
+
ed1787d
+  cc = grub_efi_locate_protocol (&cc_measurement_guid, NULL);
ed1787d
+  if (cc == NULL)
ed1787d
+    return;
ed1787d
+
ed1787d
+  status = efi_call_3 (cc->map_pcr_to_mr_index, cc, pcr, &mr;;
ed1787d
+  if (status != GRUB_EFI_SUCCESS)
ed1787d
+    {
ed1787d
+      grub_efi_log_event_status (status);
ed1787d
+      return;
ed1787d
+    }
ed1787d
+
ed1787d
+  event = grub_zalloc (sizeof (grub_efi_cc_event_t) +
ed1787d
+		       grub_strlen (description) + 1);
ed1787d
+  if (event == NULL)
ed1787d
+    {
ed1787d
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate CC event buffer"));
ed1787d
+      return;
ed1787d
+    }
ed1787d
+
ed1787d
+  event->Header.HeaderSize = sizeof (grub_efi_cc_event_header_t);
ed1787d
+  event->Header.HeaderVersion = GRUB_EFI_CC_EVENT_HEADER_VERSION;
ed1787d
+  event->Header.MrIndex = mr;
ed1787d
+  event->Header.EventType = EV_IPL;
ed1787d
+  event->Size = sizeof (*event) + grub_strlen (description) + 1;
ed1787d
+  grub_strcpy ((char *) event->Event, description);
ed1787d
+
ed1787d
+  status = efi_call_5 (cc->hash_log_extend_event, cc, 0,
ed1787d
+		       (grub_efi_physical_address_t)(grub_addr_t) buf,
ed1787d
+		       (grub_efi_uint64_t) size, event);
ed1787d
+  grub_free (event);
ed1787d
+
ed1787d
+  if (status != GRUB_EFI_SUCCESS)
ed1787d
+    grub_efi_log_event_status (status);
ed1787d
+}
ed1787d
+
ed1787d
 grub_err_t
ed1787d
 grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
ed1787d
 		    const char *description)
ed1787d
@@ -228,6 +274,8 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
ed1787d
   grub_efi_handle_t tpm_handle;
ed1787d
   grub_efi_uint8_t protocol_version;
ed1787d
 
ed1787d
+  grub_cc_log_event(buf, size, pcr, description);
ed1787d
+
ed1787d
   if (!grub_tpm_handle_find (&tpm_handle, &protocol_version))
ed1787d
     return 0;
ed1787d
 
ed1787d
diff --git a/include/grub/efi/cc.h b/include/grub/efi/cc.h
ed1787d
new file mode 100644
ed1787d
index 0000000000..8960306890
ed1787d
--- /dev/null
ed1787d
+++ b/include/grub/efi/cc.h
ed1787d
@@ -0,0 +1,151 @@
ed1787d
+/*
ed1787d
+ *  GRUB  --  GRand Unified Bootloader
ed1787d
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
ed1787d
+ *
ed1787d
+ *  GRUB is free software: you can redistribute it and/or modify
ed1787d
+ *  it under the terms of the GNU General Public License as published by
ed1787d
+ *  the Free Software Foundation, either version 3 of the License, or
ed1787d
+ *  (at your option) any later version.
ed1787d
+ *
ed1787d
+ *  GRUB is distributed in the hope that it will be useful,
ed1787d
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
ed1787d
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ed1787d
+ *  GNU General Public License for more details.
ed1787d
+ *
ed1787d
+ *  You should have received a copy of the GNU General Public License
ed1787d
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
ed1787d
+ */
ed1787d
+
ed1787d
+#ifndef GRUB_EFI_CC_H
ed1787d
+#define GRUB_EFI_CC_H 1
ed1787d
+
ed1787d
+#include <grub/efi/api.h>
ed1787d
+#include <grub/efi/efi.h>
ed1787d
+#include <grub/err.h>
ed1787d
+
ed1787d
+#define GRUB_EFI_CC_MEASUREMENT_PROTOCOL_GUID \
ed1787d
+  { 0x96751a3d, 0x72f4, 0x41a6, \
ed1787d
+    { 0xa7, 0x94, 0xed, 0x5d, 0x0e, 0x67, 0xae, 0x6b } \
ed1787d
+  };
ed1787d
+
ed1787d
+struct grub_efi_cc_version
ed1787d
+{
ed1787d
+  grub_efi_uint8_t Major;
ed1787d
+  grub_efi_uint8_t Minor;
ed1787d
+};
ed1787d
+typedef struct grub_efi_cc_version grub_efi_cc_version_t;
ed1787d
+
ed1787d
+/* EFI_CC Type/SubType definition. */
ed1787d
+#define GRUB_EFI_CC_TYPE_NONE	0
ed1787d
+#define GRUB_EFI_CC_TYPE_SEV	1
ed1787d
+#define GRUB_EFI_CC_TYPE_TDX	2
ed1787d
+
ed1787d
+struct grub_efi_cc_type
ed1787d
+{
ed1787d
+  grub_efi_uint8_t Type;
ed1787d
+  grub_efi_uint8_t SubType;
ed1787d
+};
ed1787d
+typedef struct grub_efi_cc_type grub_efi_cc_type_t;
ed1787d
+
ed1787d
+typedef grub_efi_uint32_t grub_efi_cc_event_log_bitmap_t;
ed1787d
+typedef grub_efi_uint32_t grub_efi_cc_event_log_format_t;
ed1787d
+typedef grub_efi_uint32_t grub_efi_cc_event_algorithm_bitmap_t;
ed1787d
+typedef grub_efi_uint32_t grub_efi_cc_mr_index_t;
ed1787d
+
ed1787d
+/* Intel TDX measure register index. */
ed1787d
+#define GRUB_TDX_MR_INDEX_MRTD	0
ed1787d
+#define GRUB_TDX_MR_INDEX_RTMR0	1
ed1787d
+#define GRUB_TDX_MR_INDEX_RTMR1	2
ed1787d
+#define GRUB_TDX_MR_INDEX_RTMR2	3
ed1787d
+#define GRUB_TDX_MR_INDEX_RTMR3	4
ed1787d
+
ed1787d
+#define GRUB_EFI_CC_EVENT_LOG_FORMAT_TCG_2	0x00000002
ed1787d
+#define GRUB_EFI_CC_BOOT_HASH_ALG_SHA384	0x00000004
ed1787d
+#define GRUB_EFI_CC_EVENT_HEADER_VERSION	1
ed1787d
+
ed1787d
+struct grub_efi_cc_event_header
ed1787d
+{
ed1787d
+  /* Size of the event header itself (sizeof(EFI_TD_EVENT_HEADER)). */
ed1787d
+  grub_efi_uint32_t      HeaderSize;
ed1787d
+
ed1787d
+  /*
ed1787d
+   * Header version. For this version of this specification,
ed1787d
+   * the value shall be 1.
ed1787d
+   */
ed1787d
+  grub_efi_uint16_t      HeaderVersion;
ed1787d
+
ed1787d
+  /* Index of the MR that shall be extended. */
ed1787d
+  grub_efi_cc_mr_index_t MrIndex;
ed1787d
+
ed1787d
+  /* Type of the event that shall be extended (and optionally logged). */
ed1787d
+  grub_efi_uint32_t      EventType;
ed1787d
+} GRUB_PACKED;
ed1787d
+typedef struct grub_efi_cc_event_header grub_efi_cc_event_header_t;
ed1787d
+
ed1787d
+struct grub_efi_cc_event
ed1787d
+{
ed1787d
+  /* Total size of the event including the Size component, the header and the Event data. */
ed1787d
+  grub_efi_uint32_t          Size;
ed1787d
+  grub_efi_cc_event_header_t Header;
ed1787d
+  grub_efi_uint8_t           Event[0];
ed1787d
+} GRUB_PACKED;
ed1787d
+typedef struct grub_efi_cc_event grub_efi_cc_event_t;
ed1787d
+
ed1787d
+struct grub_efi_cc_boot_service_capability
ed1787d
+{
ed1787d
+  /* Allocated size of the structure. */
ed1787d
+  grub_efi_uint8_t                     Size;
ed1787d
+
ed1787d
+  /*
ed1787d
+   * Version of the grub_efi_cc_boot_service_capability_t structure itself.
ed1787d
+   * For this version of the protocol, the Major version shall be set to 1
ed1787d
+   * and the Minor version shall be set to 1.
ed1787d
+   */
ed1787d
+  grub_efi_cc_version_t                StructureVersion;
ed1787d
+
ed1787d
+  /*
ed1787d
+   * Version of the EFI TD protocol.
ed1787d
+   * For this version of the protocol, the Major version shall be set to 1
ed1787d
+   * and the Minor version shall be set to 1.
ed1787d
+   */
ed1787d
+  grub_efi_cc_version_t                ProtocolVersion;
ed1787d
+
ed1787d
+  /* Supported hash algorithms. */
ed1787d
+  grub_efi_cc_event_algorithm_bitmap_t HashAlgorithmBitmap;
ed1787d
+
ed1787d
+  /* Bitmap of supported event log formats. */
ed1787d
+  grub_efi_cc_event_log_bitmap_t       SupportedEventLogs;
ed1787d
+
ed1787d
+  /* Indicates the CC type. */
ed1787d
+  grub_efi_cc_type_t CcType;
ed1787d
+};
ed1787d
+typedef struct grub_efi_cc_boot_service_capability grub_efi_cc_boot_service_capability_t;
ed1787d
+
ed1787d
+struct grub_efi_cc_protocol
ed1787d
+{
ed1787d
+  grub_efi_status_t
ed1787d
+  (*get_capability) (struct grub_efi_cc_protocol *this,
ed1787d
+		     grub_efi_cc_boot_service_capability_t *ProtocolCapability);
ed1787d
+
ed1787d
+  grub_efi_status_t
ed1787d
+  (*get_event_log) (struct grub_efi_cc_protocol *this,
ed1787d
+		    grub_efi_cc_event_log_format_t EventLogFormat,
ed1787d
+		    grub_efi_physical_address_t *EventLogLocation,
ed1787d
+		    grub_efi_physical_address_t *EventLogLastEntry,
ed1787d
+		    grub_efi_boolean_t *EventLogTruncated);
ed1787d
+
ed1787d
+  grub_efi_status_t
ed1787d
+  (*hash_log_extend_event) (struct grub_efi_cc_protocol *this,
ed1787d
+			    grub_efi_uint64_t Flags,
ed1787d
+			    grub_efi_physical_address_t DataToHash,
ed1787d
+			    grub_efi_uint64_t DataToHashLen,
ed1787d
+			    grub_efi_cc_event_t *EfiCcEvent);
ed1787d
+
ed1787d
+  grub_efi_status_t
ed1787d
+  (*map_pcr_to_mr_index) (struct grub_efi_cc_protocol *this,
ed1787d
+			  grub_efi_uint32_t PcrIndex,
ed1787d
+			  grub_efi_cc_mr_index_t *MrIndex);
ed1787d
+};
ed1787d
+typedef struct grub_efi_cc_protocol grub_efi_cc_protocol_t;
ed1787d
+
ed1787d
+#endif