Blame SOURCES/0293-efi-tpm-Add-EFI_CC_MEASUREMENT_PROTOCOL-support.patch

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