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