Blame SOURCES/0317-ibmvtpm-Add-support-for-trusted-boot-using-a-vTPM-2..patch

e28c09
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
e28c09
From: Stefan Berger <stefanb@linux.ibm.com>
b35c50
Date: Mon, 6 Feb 2023 10:03:25 -0500
e28c09
Subject: [PATCH] ibmvtpm: Add support for trusted boot using a vTPM 2.0
e28c09
e28c09
Add support for trusted boot using a vTPM 2.0 on the IBM IEEE1275
e28c09
PowerPC platform. With this patch grub now measures text and binary data
e28c09
into the TPM's PCRs 8 and 9 in the same way as the x86_64 platform
e28c09
does.
e28c09
e28c09
This patch requires Daniel Axtens's patches for claiming more memory.
e28c09
b35c50
Note: The tpm_init() function cannot be called from GRUB_MOD_INIT() since
b35c50
it does not find the device nodes upon module initialization and
b35c50
therefore the call to tpm_init() must be deferred to grub_tpm_measure().
b35c50
e28c09
For vTPM support to work on PowerVM, system driver levels 1010.30
e28c09
or 1020.00 are required.
e28c09
e28c09
Note: Previous versions of firmware levels with the 2hash-ext-log
e28c09
API call have a bug that, once this API call is invoked, has the
e28c09
effect of disabling the vTPM driver under Linux causing an error
e28c09
message to be displayed in the Linux kernel log. Those users will
e28c09
have to update their machines to the firmware levels mentioned
e28c09
above.
e28c09
e28c09
Cc: Eric Snowberg <eric.snowberg@oracle.com>
e28c09
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
b35c50
Signed-off-by: Daniel Axtens <dja@axtens.net>
b35c50
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b35c50
(cherry picked from commit 2aa5ef83743dfea79377309ff4f5e9c9a55de355)
e28c09
---
e28c09
 grub-core/Makefile.core.def           |   7 ++
b35c50
 grub-core/commands/ieee1275/ibmvtpm.c | 155 ++++++++++++++++++++++++++++++++++
e28c09
 include/grub/ieee1275/ieee1275.h      |   3 +
e28c09
 docs/grub.texi                        |   3 +-
b35c50
 4 files changed, 167 insertions(+), 1 deletion(-)
e28c09
 create mode 100644 grub-core/commands/ieee1275/ibmvtpm.c
e28c09
e28c09
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
b35c50
index f21da23213..02ea718652 100644
e28c09
--- a/grub-core/Makefile.core.def
e28c09
+++ b/grub-core/Makefile.core.def
b35c50
@@ -1175,6 +1175,13 @@ module = {
e28c09
   enable = powerpc_ieee1275;
e28c09
 };
e28c09
 
e28c09
+module = {
e28c09
+  name = tpm;
e28c09
+  common = commands/tpm.c;
e28c09
+  ieee1275 = commands/ieee1275/ibmvtpm.c;
e28c09
+  enable = powerpc_ieee1275;
e28c09
+};
e28c09
+
e28c09
 module = {
e28c09
   name = terminal;
e28c09
   common = commands/terminal.c;
e28c09
diff --git a/grub-core/commands/ieee1275/ibmvtpm.c b/grub-core/commands/ieee1275/ibmvtpm.c
e28c09
new file mode 100644
b35c50
index 0000000000..239942d27e
e28c09
--- /dev/null
e28c09
+++ b/grub-core/commands/ieee1275/ibmvtpm.c
b35c50
@@ -0,0 +1,155 @@
e28c09
+/*
e28c09
+ *  GRUB  --  GRand Unified Bootloader
b35c50
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
b35c50
+ *  Copyright (C) 2022  IBM Corporation
e28c09
+ *
e28c09
+ *  GRUB is free software: you can redistribute it and/or modify
e28c09
+ *  it under the terms of the GNU General Public License as published by
e28c09
+ *  the Free Software Foundation, either version 3 of the License, or
e28c09
+ *  (at your option) any later version.
e28c09
+ *
e28c09
+ *  GRUB is distributed in the hope that it will be useful,
e28c09
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
e28c09
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e28c09
+ *  GNU General Public License for more details.
e28c09
+ *
e28c09
+ *  You should have received a copy of the GNU General Public License
e28c09
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
e28c09
+ *
e28c09
+ *  IBM vTPM support code.
e28c09
+ */
e28c09
+
e28c09
+#include <grub/err.h>
e28c09
+#include <grub/types.h>
e28c09
+#include <grub/tpm.h>
e28c09
+#include <grub/ieee1275/ieee1275.h>
e28c09
+#include <grub/mm.h>
e28c09
+#include <grub/misc.h>
e28c09
+
e28c09
+static grub_ieee1275_ihandle_t tpm_ihandle;
e28c09
+static grub_uint8_t tpm_version;
e28c09
+
b35c50
+#define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_ihandle_t) 0)
e28c09
+
e28c09
+static void
e28c09
+tpm_get_tpm_version (void)
e28c09
+{
e28c09
+  grub_ieee1275_phandle_t vtpm;
e28c09
+  char buffer[20];
e28c09
+
e28c09
+  if (!grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm) &&
e28c09
+      !grub_ieee1275_get_property (vtpm, "compatible", buffer,
e28c09
+				   sizeof (buffer), NULL) &&
e28c09
+      !grub_strcmp (buffer, "IBM,vtpm20"))
e28c09
+    tpm_version = 2;
e28c09
+}
e28c09
+
e28c09
+static grub_err_t
e28c09
+tpm_init (void)
e28c09
+{
e28c09
+  static int init_success = 0;
e28c09
+
e28c09
+  if (!init_success)
e28c09
+    {
b35c50
+      if (grub_ieee1275_open ("/vdevice/vtpm", &tpm_ihandle) < 0)
b35c50
+	{
b35c50
+	  tpm_ihandle = IEEE1275_IHANDLE_INVALID;
b35c50
+	  return GRUB_ERR_UNKNOWN_DEVICE;
b35c50
+	}
e28c09
+
e28c09
+      init_success = 1;
e28c09
+
e28c09
+      tpm_get_tpm_version ();
e28c09
+    }
e28c09
+
e28c09
+  return GRUB_ERR_NONE;
e28c09
+}
e28c09
+
e28c09
+static int
e28c09
+ibmvtpm_2hash_ext_log (grub_uint8_t pcrindex,
e28c09
+		       grub_uint32_t eventtype,
e28c09
+		       const char *description,
e28c09
+		       grub_size_t description_size,
e28c09
+		       void *buf, grub_size_t size)
e28c09
+{
e28c09
+  struct tpm_2hash_ext_log
e28c09
+  {
e28c09
+    struct grub_ieee1275_common_hdr common;
e28c09
+    grub_ieee1275_cell_t method;
e28c09
+    grub_ieee1275_cell_t ihandle;
e28c09
+    grub_ieee1275_cell_t size;
e28c09
+    grub_ieee1275_cell_t buf;
e28c09
+    grub_ieee1275_cell_t description_size;
e28c09
+    grub_ieee1275_cell_t description;
e28c09
+    grub_ieee1275_cell_t eventtype;
e28c09
+    grub_ieee1275_cell_t pcrindex;
e28c09
+    grub_ieee1275_cell_t catch_result;
e28c09
+    grub_ieee1275_cell_t rc;
b35c50
+  };
b35c50
+  struct tpm_2hash_ext_log args;
e28c09
+
e28c09
+  INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 2);
e28c09
+  args.method = (grub_ieee1275_cell_t) "2hash-ext-log";
e28c09
+  args.ihandle = tpm_ihandle;
e28c09
+  args.pcrindex = pcrindex;
e28c09
+  args.eventtype = eventtype;
e28c09
+  args.description = (grub_ieee1275_cell_t) description;
e28c09
+  args.description_size = description_size;
e28c09
+  args.buf = (grub_ieee1275_cell_t) buf;
e28c09
+  args.size = (grub_ieee1275_cell_t) size;
e28c09
+
e28c09
+  if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
e28c09
+    return -1;
e28c09
+
e28c09
+  /*
e28c09
+   * catch_result is set if firmware does not support 2hash-ext-log
e28c09
+   * rc is GRUB_IEEE1275_CELL_FALSE (0) on failure
e28c09
+   */
e28c09
+  if ((args.catch_result) || args.rc == GRUB_IEEE1275_CELL_FALSE)
e28c09
+    return -1;
e28c09
+
e28c09
+  return 0;
e28c09
+}
e28c09
+
e28c09
+static grub_err_t
b35c50
+tpm2_log_event (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
e28c09
+		const char *description)
e28c09
+{
e28c09
+  static int error_displayed = 0;
b35c50
+  int rc;
e28c09
+
b35c50
+  rc = ibmvtpm_2hash_ext_log (pcr, EV_IPL,
b35c50
+			      description, grub_strlen(description) + 1,
b35c50
+			      buf, size);
b35c50
+  if (rc && !error_displayed)
e28c09
+    {
e28c09
+      error_displayed++;
e28c09
+      return grub_error (GRUB_ERR_BAD_DEVICE,
b35c50
+			 "2HASH-EXT-LOG failed: Firmware is likely too old.\n");
e28c09
+    }
e28c09
+
e28c09
+  return GRUB_ERR_NONE;
e28c09
+}
e28c09
+
e28c09
+grub_err_t
e28c09
+grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
e28c09
+		  const char *description)
e28c09
+{
b35c50
+  /*
b35c50
+   * Call tpm_init() 'late' rather than from GRUB_MOD_INIT() so that device nodes
b35c50
+   * can be found.
b35c50
+   */
b35c50
+  grub_err_t err = tpm_init ();
e28c09
+
e28c09
+  /* Absence of a TPM isn't a failure. */
e28c09
+  if (err != GRUB_ERR_NONE)
e28c09
+    return GRUB_ERR_NONE;
e28c09
+
e28c09
+  grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n",
e28c09
+		pcr, size, description);
e28c09
+
e28c09
+  if (tpm_version == 2)
e28c09
+    return tpm2_log_event (buf, size, pcr, description);
e28c09
+
e28c09
+  return GRUB_ERR_NONE;
e28c09
+}
e28c09
diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h
b35c50
index 560c968460..27b9cf259b 100644
e28c09
--- a/include/grub/ieee1275/ieee1275.h
e28c09
+++ b/include/grub/ieee1275/ieee1275.h
e28c09
@@ -24,6 +24,9 @@
e28c09
 #include <grub/types.h>
e28c09
 #include <grub/machine/ieee1275.h>
e28c09
 
e28c09
+#define GRUB_IEEE1275_CELL_FALSE       ((grub_ieee1275_cell_t) 0)
e28c09
+#define GRUB_IEEE1275_CELL_TRUE        ((grub_ieee1275_cell_t) -1)
e28c09
+
e28c09
 struct grub_ieee1275_mem_region
e28c09
 {
e28c09
   unsigned int start;
e28c09
diff --git a/docs/grub.texi b/docs/grub.texi
b35c50
index 1750b72ee9..825278a7f3 100644
e28c09
--- a/docs/grub.texi
e28c09
+++ b/docs/grub.texi
b35c50
@@ -6235,7 +6235,8 @@ tpm module is loaded. As such it is recommended that the tpm module be built
e28c09
 into @file{core.img} in order to avoid a potential gap in measurement between
e28c09
 @file{core.img} being loaded and the tpm module being loaded.
e28c09
 
e28c09
-Measured boot is currently only supported on EFI platforms.
e28c09
+Measured boot is currently only supported on EFI and IBM IEEE1275 PowerPC
e28c09
+platforms.
e28c09
 
e28c09
 @node Lockdown
e28c09
 @section Lockdown when booting on a secure setup