6b2dd0f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
0bcec26
From: Matthew Garrett <mjg59@coreos.com>
0bcec26
Date: Tue, 14 Jul 2015 17:06:35 -0700
7e98da0
Subject: [PATCH] TPM support
0bcec26
0bcec26
Add support for performing basic TPM measurements. Right now this only
7e98da0
supports extending PCRs statically and only on UEFI, but will measure
7e98da0
all modules as they're loaded, kernel and initrd images, command line,
7e98da0
grub commands and multiboot images.
7e98da0
7e98da0
Signed-off-by: Matthew Garrett <mjg59@coreos.com>
7e98da0
[hdegoede: fix hash_log_extend_event prototype and compile warnings]
7e98da0
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7e98da0
[michael.marineau: fix warnings when compiling for other platforms]
7e98da0
Michael Marineau <michael.marineau@coreos.com>
7e98da0
[javierm: squash in a single patch and drop legacy BIOS support]
7e98da0
Javier Martinez Canillas <javierm@redhat.com>
0bcec26
---
7e98da0
 grub-core/Makefile.core.def           |   2 +
7e98da0
 grub-core/kern/dl.c                   |   4 +
7e98da0
 grub-core/kern/efi/tpm.c              | 274 ++++++++++++++++++++++++++++++++++
7e98da0
 grub-core/kern/tpm.c                  |  19 +++
7e98da0
 grub-core/lib/cmdline.c               |   7 +-
7e98da0
 grub-core/loader/i386/efi/linux.c     |   6 +
7e98da0
 grub-core/loader/i386/linux.c         |   6 +
7e98da0
 grub-core/loader/i386/multiboot_mbi.c |   4 +
7e98da0
 grub-core/loader/linux.c              |   4 +
7e98da0
 grub-core/loader/multiboot.c          |   3 +
7e98da0
 grub-core/loader/multiboot_mbi2.c     |   5 +-
7e98da0
 grub-core/script/execute.c            |  26 +++-
7e98da0
 include/grub/efi/tpm.h                | 153 +++++++++++++++++++
7e98da0
 include/grub/tpm.h                    |  94 ++++++++++++
7e98da0
 grub-core/Makefile.am                 |   1 +
7e98da0
 15 files changed, 604 insertions(+), 4 deletions(-)
0bcec26
 create mode 100644 grub-core/kern/efi/tpm.c
0bcec26
 create mode 100644 grub-core/kern/tpm.c
0bcec26
 create mode 100644 include/grub/efi/tpm.h
0bcec26
 create mode 100644 include/grub/tpm.h
0bcec26
0bcec26
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
7e98da0
index 63b33490b11..3be42a95195 100644
0bcec26
--- a/grub-core/Makefile.core.def
0bcec26
+++ b/grub-core/Makefile.core.def
7e98da0
@@ -130,6 +130,7 @@ kernel = {
7e98da0
   common = kern/rescue_reader.c;
0bcec26
   common = kern/term.c;
0bcec26
   common = kern/backtrace.c;
0bcec26
+  common = kern/tpm.c;
0bcec26
 
0bcec26
   x86 = kern/i386/backtrace.c;
0bcec26
   i386_xen = kern/i386/backtrace.c;
7e98da0
@@ -198,6 +199,7 @@ kernel = {
0bcec26
   efi = kern/acpi.c;
0bcec26
   efi = kern/efi/acpi.c;
0bcec26
   efi = lib/envblk.c;
0bcec26
+  efi = kern/efi/tpm.c;
0bcec26
   i386_coreboot = kern/i386/pc/acpi.c;
0bcec26
   i386_multiboot = kern/i386/pc/acpi.c;
0bcec26
   i386_coreboot = kern/acpi.c;
0bcec26
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
7e98da0
index eb8b969cded..91105bc4677 100644
0bcec26
--- a/grub-core/kern/dl.c
0bcec26
+++ b/grub-core/kern/dl.c
0bcec26
@@ -33,6 +33,7 @@
0bcec26
 #include <grub/cache.h>
0bcec26
 #include <grub/i18n.h>
0bcec26
 #include <grub/efi/sb.h>
0bcec26
+#include <grub/tpm.h>
0bcec26
 
0bcec26
 /* Platforms where modules are in a readonly area of memory.  */
0bcec26
 #if defined(GRUB_MACHINE_QEMU)
7e98da0
@@ -829,6 +830,9 @@ grub_dl_load_file (const char *filename)
0bcec26
      opens of the same device.  */
0bcec26
   grub_file_close (file);
0bcec26
 
7e98da0
+  grub_tpm_measure(core, size, GRUB_BINARY_PCR, "grub_module", filename);
7e98da0
+  grub_print_error();
0bcec26
+
0bcec26
   mod = grub_dl_load_core (core, size);
0bcec26
   grub_free (core);
0bcec26
   if (! mod)
0bcec26
diff --git a/grub-core/kern/efi/tpm.c b/grub-core/kern/efi/tpm.c
0bcec26
new file mode 100644
7e98da0
index 00000000000..0d3ebe22e57
0bcec26
--- /dev/null
0bcec26
+++ b/grub-core/kern/efi/tpm.c
7e98da0
@@ -0,0 +1,274 @@
0bcec26
+#include <grub/err.h>
0bcec26
+#include <grub/i18n.h>
0bcec26
+#include <grub/efi/api.h>
0bcec26
+#include <grub/efi/efi.h>
0bcec26
+#include <grub/efi/tpm.h>
0bcec26
+#include <grub/mm.h>
0bcec26
+#include <grub/tpm.h>
0bcec26
+#include <grub/term.h>
0bcec26
+
0bcec26
+static grub_efi_guid_t tpm_guid = EFI_TPM_GUID;
0bcec26
+static grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID;
0bcec26
+
0bcec26
+static grub_efi_boolean_t grub_tpm_present(grub_efi_tpm_protocol_t *tpm)
0bcec26
+{
0bcec26
+  grub_efi_status_t status;
0bcec26
+  TCG_EFI_BOOT_SERVICE_CAPABILITY caps;
0bcec26
+  grub_uint32_t flags;
0bcec26
+  grub_efi_physical_address_t eventlog, lastevent;
0bcec26
+
0bcec26
+  caps.Size = (grub_uint8_t)sizeof(caps);
0bcec26
+
0bcec26
+  status = efi_call_5(tpm->status_check, tpm, &caps, &flags, &eventlog,
0bcec26
+		      &lastevent);
0bcec26
+
0bcec26
+  if (status != GRUB_EFI_SUCCESS || caps.TPMDeactivatedFlag
0bcec26
+      || !caps.TPMPresentFlag)
0bcec26
+    return 0;
0bcec26
+
0bcec26
+  return 1;
0bcec26
+}
0bcec26
+
0bcec26
+static grub_efi_boolean_t grub_tpm2_present(grub_efi_tpm2_protocol_t *tpm)
0bcec26
+{
0bcec26
+  grub_efi_status_t status;
0bcec26
+  EFI_TCG2_BOOT_SERVICE_CAPABILITY caps;
0bcec26
+
0bcec26
+  caps.Size = (grub_uint8_t)sizeof(caps);
0bcec26
+
0bcec26
+  status = efi_call_2(tpm->get_capability, tpm, &caps);
0bcec26
+
0bcec26
+  if (status != GRUB_EFI_SUCCESS || !caps.TPMPresentFlag)
0bcec26
+    return 0;
0bcec26
+
0bcec26
+  return 1;
0bcec26
+}
0bcec26
+
0bcec26
+static grub_efi_boolean_t grub_tpm_handle_find(grub_efi_handle_t *tpm_handle,
0bcec26
+					       grub_efi_uint8_t *protocol_version)
0bcec26
+{
0bcec26
+  grub_efi_handle_t *handles;
0bcec26
+  grub_efi_uintn_t num_handles;
0bcec26
+
0bcec26
+  handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm_guid, NULL,
0bcec26
+				    &num_handles);
0bcec26
+  if (handles && num_handles > 0) {
0bcec26
+    *tpm_handle = handles[0];
0bcec26
+    *protocol_version = 1;
0bcec26
+    return 1;
0bcec26
+  }
0bcec26
+
0bcec26
+  handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm2_guid, NULL,
0bcec26
+				    &num_handles);
0bcec26
+  if (handles && num_handles > 0) {
0bcec26
+    *tpm_handle = handles[0];
0bcec26
+    *protocol_version = 2;
0bcec26
+    return 1;
0bcec26
+  }
0bcec26
+
0bcec26
+  return 0;
0bcec26
+}
0bcec26
+
0bcec26
+static grub_err_t
0bcec26
+grub_tpm1_execute(grub_efi_handle_t tpm_handle,
0bcec26
+		  PassThroughToTPM_InputParamBlock *inbuf,
0bcec26
+		  PassThroughToTPM_OutputParamBlock *outbuf)
0bcec26
+{
0bcec26
+  grub_efi_status_t status;
0bcec26
+  grub_efi_tpm_protocol_t *tpm;
0bcec26
+  grub_uint32_t inhdrsize = sizeof(*inbuf) - sizeof(inbuf->TPMOperandIn);
0bcec26
+  grub_uint32_t outhdrsize = sizeof(*outbuf) - sizeof(outbuf->TPMOperandOut);
0bcec26
+
0bcec26
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
0bcec26
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
0bcec26
+
0bcec26
+  if (!grub_tpm_present(tpm))
0bcec26
+    return 0;
0bcec26
+
0bcec26
+  /* UEFI TPM protocol takes the raw operand block, no param block header */
0bcec26
+  status = efi_call_5 (tpm->pass_through_to_tpm, tpm,
0bcec26
+		       inbuf->IPBLength - inhdrsize, inbuf->TPMOperandIn,
0bcec26
+		       outbuf->OPBLength - outhdrsize, outbuf->TPMOperandOut);
0bcec26
+
0bcec26
+  switch (status) {
0bcec26
+  case GRUB_EFI_SUCCESS:
0bcec26
+    return 0;
0bcec26
+  case GRUB_EFI_DEVICE_ERROR:
0bcec26
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
0bcec26
+  case GRUB_EFI_INVALID_PARAMETER:
0bcec26
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
0bcec26
+  case GRUB_EFI_BUFFER_TOO_SMALL:
0bcec26
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
0bcec26
+  case GRUB_EFI_NOT_FOUND:
0bcec26
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
0bcec26
+  default:
0bcec26
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
0bcec26
+  }
0bcec26
+}
0bcec26
+
0bcec26
+static grub_err_t
0bcec26
+grub_tpm2_execute(grub_efi_handle_t tpm_handle,
0bcec26
+		  PassThroughToTPM_InputParamBlock *inbuf,
0bcec26
+		  PassThroughToTPM_OutputParamBlock *outbuf)
0bcec26
+{
0bcec26
+  grub_efi_status_t status;
0bcec26
+  grub_efi_tpm2_protocol_t *tpm;
0bcec26
+  grub_uint32_t inhdrsize = sizeof(*inbuf) - sizeof(inbuf->TPMOperandIn);
0bcec26
+  grub_uint32_t outhdrsize = sizeof(*outbuf) - sizeof(outbuf->TPMOperandOut);
0bcec26
+
0bcec26
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
0bcec26
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
0bcec26
+
0bcec26
+  if (!grub_tpm2_present(tpm))
0bcec26
+    return 0;
0bcec26
+
0bcec26
+  /* UEFI TPM protocol takes the raw operand block, no param block header */
0bcec26
+  status = efi_call_5 (tpm->submit_command, tpm,
0bcec26
+		       inbuf->IPBLength - inhdrsize, inbuf->TPMOperandIn,
0bcec26
+		       outbuf->OPBLength - outhdrsize, outbuf->TPMOperandOut);
0bcec26
+
0bcec26
+  switch (status) {
0bcec26
+  case GRUB_EFI_SUCCESS:
0bcec26
+    return 0;
0bcec26
+  case GRUB_EFI_DEVICE_ERROR:
0bcec26
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
0bcec26
+  case GRUB_EFI_INVALID_PARAMETER:
0bcec26
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
0bcec26
+  case GRUB_EFI_BUFFER_TOO_SMALL:
0bcec26
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
0bcec26
+  case GRUB_EFI_NOT_FOUND:
0bcec26
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
0bcec26
+  default:
0bcec26
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
0bcec26
+  }
0bcec26
+}
0bcec26
+
0bcec26
+grub_err_t
0bcec26
+grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
0bcec26
+		 PassThroughToTPM_OutputParamBlock *outbuf)
0bcec26
+{
0bcec26
+  grub_efi_handle_t tpm_handle;
0bcec26
+   grub_uint8_t protocol_version;
0bcec26
+
0bcec26
+  /* It's not a hard failure for there to be no TPM */
0bcec26
+  if (!grub_tpm_handle_find(&tpm_handle, &protocol_version))
0bcec26
+    return 0;
0bcec26
+
0bcec26
+  if (protocol_version == 1) {
0bcec26
+    return grub_tpm1_execute(tpm_handle, inbuf, outbuf);
0bcec26
+  } else {
0bcec26
+    return grub_tpm2_execute(tpm_handle, inbuf, outbuf);
0bcec26
+  }
0bcec26
+}
0bcec26
+
0bcec26
+static grub_err_t
0bcec26
+grub_tpm1_log_event(grub_efi_handle_t tpm_handle, unsigned char *buf,
0bcec26
+		    grub_size_t size, grub_uint8_t pcr,
0bcec26
+		    const char *description)
0bcec26
+{
7e98da0
+  TCG_PCR_EVENT *event;
0bcec26
+  grub_efi_status_t status;
0bcec26
+  grub_efi_tpm_protocol_t *tpm;
0bcec26
+  grub_efi_physical_address_t lastevent;
0bcec26
+  grub_uint32_t algorithm;
0bcec26
+  grub_uint32_t eventnum = 0;
0bcec26
+
0bcec26
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
0bcec26
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
0bcec26
+
0bcec26
+  if (!grub_tpm_present(tpm))
0bcec26
+    return 0;
0bcec26
+
7e98da0
+  event = grub_zalloc(sizeof (TCG_PCR_EVENT) + grub_strlen(description) + 1);
0bcec26
+  if (!event)
0bcec26
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
0bcec26
+		       N_("cannot allocate TPM event buffer"));
0bcec26
+
7e98da0
+  event->PCRIndex = pcr;
7e98da0
+  event->EventType = EV_IPL;
7e98da0
+  event->EventSize = grub_strlen(description) + 1;
7e98da0
+  grub_memcpy(event->Event, description, event->EventSize);
0bcec26
+
0bcec26
+  algorithm = TCG_ALG_SHA;
7e98da0
+  status = efi_call_7 (tpm->log_extend_event, tpm,
7e98da0
+                       (unsigned long) buf, (grub_uint64_t) size,
0bcec26
+		       algorithm, event, &eventnum, &lastevent);
0bcec26
+
0bcec26
+  switch (status) {
0bcec26
+  case GRUB_EFI_SUCCESS:
0bcec26
+    return 0;
0bcec26
+  case GRUB_EFI_DEVICE_ERROR:
0bcec26
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
0bcec26
+  case GRUB_EFI_INVALID_PARAMETER:
0bcec26
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
0bcec26
+  case GRUB_EFI_BUFFER_TOO_SMALL:
0bcec26
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
0bcec26
+  case GRUB_EFI_NOT_FOUND:
0bcec26
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
0bcec26
+  default:
0bcec26
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
0bcec26
+  }
0bcec26
+}
0bcec26
+
0bcec26
+static grub_err_t
0bcec26
+grub_tpm2_log_event(grub_efi_handle_t tpm_handle, unsigned char *buf,
0bcec26
+		   grub_size_t size, grub_uint8_t pcr,
0bcec26
+		   const char *description)
0bcec26
+{
0bcec26
+  EFI_TCG2_EVENT *event;
0bcec26
+  grub_efi_status_t status;
0bcec26
+  grub_efi_tpm2_protocol_t *tpm;
0bcec26
+
0bcec26
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
0bcec26
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
0bcec26
+
0bcec26
+  if (!grub_tpm2_present(tpm))
0bcec26
+    return 0;
0bcec26
+
0bcec26
+  event = grub_zalloc(sizeof (EFI_TCG2_EVENT) + grub_strlen(description) + 1);
0bcec26
+  if (!event)
0bcec26
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
0bcec26
+		       N_("cannot allocate TPM event buffer"));
0bcec26
+
0bcec26
+  event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);
0bcec26
+  event->Header.HeaderVersion = 1;
0bcec26
+  event->Header.PCRIndex = pcr;
0bcec26
+  event->Header.EventType = EV_IPL;
0bcec26
+  event->Size = sizeof(*event) - sizeof(event->Event) + grub_strlen(description) + 1;
0bcec26
+  grub_memcpy(event->Event, description, grub_strlen(description) + 1);
0bcec26
+
7e98da0
+  status = efi_call_5 (tpm->hash_log_extend_event, tpm, 0, (unsigned long) buf,
0bcec26
+		       (grub_uint64_t) size, event);
0bcec26
+
0bcec26
+  switch (status) {
0bcec26
+  case GRUB_EFI_SUCCESS:
0bcec26
+    return 0;
0bcec26
+  case GRUB_EFI_DEVICE_ERROR:
0bcec26
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
0bcec26
+  case GRUB_EFI_INVALID_PARAMETER:
0bcec26
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
0bcec26
+  case GRUB_EFI_BUFFER_TOO_SMALL:
0bcec26
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
0bcec26
+  case GRUB_EFI_NOT_FOUND:
0bcec26
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
0bcec26
+  default:
0bcec26
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
0bcec26
+  }
0bcec26
+}
0bcec26
+
0bcec26
+grub_err_t
0bcec26
+grub_tpm_log_event(unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
0bcec26
+		   const char *description)
0bcec26
+{
0bcec26
+  grub_efi_handle_t tpm_handle;
0bcec26
+  grub_efi_uint8_t protocol_version;
0bcec26
+
0bcec26
+  if (!grub_tpm_handle_find(&tpm_handle, &protocol_version))
0bcec26
+    return 0;
0bcec26
+
0bcec26
+  if (protocol_version == 1) {
0bcec26
+    return grub_tpm1_log_event(tpm_handle, buf, size, pcr, description);
0bcec26
+  } else {
0bcec26
+    return grub_tpm2_log_event(tpm_handle, buf, size, pcr, description);
0bcec26
+  }
0bcec26
+}
0bcec26
diff --git a/grub-core/kern/tpm.c b/grub-core/kern/tpm.c
0bcec26
new file mode 100644
7e98da0
index 00000000000..e5e8fced624
0bcec26
--- /dev/null
0bcec26
+++ b/grub-core/kern/tpm.c
7e98da0
@@ -0,0 +1,19 @@
0bcec26
+#include <grub/err.h>
0bcec26
+#include <grub/i18n.h>
0bcec26
+#include <grub/misc.h>
0bcec26
+#include <grub/mm.h>
0bcec26
+#include <grub/tpm.h>
0bcec26
+#include <grub/term.h>
0bcec26
+
0bcec26
+grub_err_t
0bcec26
+grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
7e98da0
+		  const char *kind, const char *description)
0bcec26
+{
7e98da0
+  grub_err_t ret;
7e98da0
+  char *desc = grub_xasprintf("%s %s", kind, description);
7e98da0
+  if (!desc)
7e98da0
+    return GRUB_ERR_OUT_OF_MEMORY;
7e98da0
+  ret = grub_tpm_log_event(buf, size, pcr, desc);
7e98da0
+  grub_free(desc);
7e98da0
+  return ret;
0bcec26
+}
7e98da0
diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c
7e98da0
index 970ea868c14..d5c12957cad 100644
7e98da0
--- a/grub-core/lib/cmdline.c
7e98da0
+++ b/grub-core/lib/cmdline.c
7e98da0
@@ -19,6 +19,7 @@
7e98da0
 
7e98da0
 #include <grub/lib/cmdline.h>
7e98da0
 #include <grub/misc.h>
7e98da0
+#include <grub/tpm.h>
7e98da0
 
7e98da0
 static int
7e98da0
 is_hex(char c)
7e98da0
@@ -79,7 +80,7 @@ int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
7e98da0
 {
7e98da0
   int i, space;
7e98da0
   unsigned int arg_size;
7e98da0
-  char *c;
7e98da0
+  char *c, *orig = buf;
7e98da0
 
7e98da0
   for (i = 0; i < argc; i++)
7e98da0
     {
7e98da0
@@ -125,5 +126,9 @@ int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
7e98da0
 
7e98da0
   *buf = 0;
7e98da0
 
7e98da0
+  grub_tpm_measure ((void *)orig, grub_strlen (orig), GRUB_ASCII_PCR,
7e98da0
+		    "grub_kernel_cmdline", orig);
7e98da0
+  grub_print_error();
7e98da0
+
7e98da0
   return i;
7e98da0
 }
7e98da0
diff --git a/grub-core/loader/i386/efi/linux.c b/grub-core/loader/i386/efi/linux.c
7e98da0
index 800c3e54022..ea9f5134e67 100644
7e98da0
--- a/grub-core/loader/i386/efi/linux.c
7e98da0
+++ b/grub-core/loader/i386/efi/linux.c
7e98da0
@@ -27,6 +27,7 @@
7e98da0
 #include <grub/lib/cmdline.h>
7e98da0
 #include <grub/efi/efi.h>
7e98da0
 #include <grub/efi/linux.h>
7e98da0
+#include <grub/tpm.h>
7e98da0
 
7e98da0
 GRUB_MOD_LICENSE ("GPLv3+");
7e98da0
 
7e98da0
@@ -131,6 +132,8 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
7e98da0
                         argv[i]);
7e98da0
           goto fail;
7e98da0
         }
7e98da0
+      grub_tpm_measure (ptr, cursize, GRUB_BINARY_PCR, "grub_linuxefi", "Initrd");
7e98da0
+      grub_print_error();
7e98da0
       ptr += cursize;
7e98da0
       grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
7e98da0
       ptr += ALIGN_UP_OVERHEAD (cursize, 4);
7e98da0
@@ -195,6 +198,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
7e98da0
       goto fail;
7e98da0
     }
7e98da0
 
7e98da0
+  grub_tpm_measure (kernel, filelen, GRUB_BINARY_PCR, "grub_linuxefi", "Kernel");
7e98da0
+  grub_print_error();
7e98da0
+
7e98da0
   rc = grub_linuxefi_secure_validate (kernel, filelen);
7e98da0
   if (rc < 0)
7e98da0
     {
7e98da0
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
7e98da0
index c84747ea857..76304f05700 100644
7e98da0
--- a/grub-core/loader/i386/linux.c
7e98da0
+++ b/grub-core/loader/i386/linux.c
7e98da0
@@ -36,6 +36,7 @@
7e98da0
 #include <grub/lib/cmdline.h>
7e98da0
 #include <grub/linux.h>
7e98da0
 #include <grub/efi/sb.h>
7e98da0
+#include <grub/tpm.h>
7e98da0
 
7e98da0
 GRUB_MOD_LICENSE ("GPLv3+");
7e98da0
 
7e98da0
@@ -724,7 +725,11 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
7e98da0
       goto fail;
7e98da0
     }
7e98da0
 
7e98da0
+  grub_tpm_measure (kernel, len, GRUB_BINARY_PCR, "grub_linux", "Kernel");
7e98da0
+  grub_print_error();
7e98da0
+
7e98da0
   grub_memcpy (&lh, kernel, sizeof (lh));
7e98da0
+
7e98da0
   kernel_offset = sizeof (lh);
7e98da0
 
7e98da0
   if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
7e98da0
@@ -1038,6 +1043,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
7e98da0
 
7e98da0
   len = prot_file_size;
7e98da0
   grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
7e98da0
+  kernel_offset += len;
7e98da0
 
7e98da0
   if (grub_errno == GRUB_ERR_NONE)
7e98da0
     {
7e98da0
diff --git a/grub-core/loader/i386/multiboot_mbi.c b/grub-core/loader/i386/multiboot_mbi.c
7e98da0
index dc98dbcae25..ca85358f771 100644
7e98da0
--- a/grub-core/loader/i386/multiboot_mbi.c
7e98da0
+++ b/grub-core/loader/i386/multiboot_mbi.c
7e98da0
@@ -36,6 +36,7 @@
7e98da0
 #include <grub/net.h>
7e98da0
 #include <grub/i18n.h>
7e98da0
 #include <grub/lib/cmdline.h>
7e98da0
+#include <grub/tpm.h>
7e98da0
 
7e98da0
 #ifdef GRUB_MACHINE_EFI
7e98da0
 #include <grub/efi/efi.h>
7e98da0
@@ -173,6 +174,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
7e98da0
       return grub_errno;
7e98da0
     }
7e98da0
 
7e98da0
+  grub_tpm_measure((unsigned char*)buffer, len, GRUB_BINARY_PCR, "grub_multiboot", filename);
7e98da0
+  grub_print_error();
7e98da0
+
7e98da0
   header = find_header (buffer, len);
7e98da0
 
7e98da0
   if (header == 0)
7e98da0
diff --git a/grub-core/loader/linux.c b/grub-core/loader/linux.c
7e98da0
index be6fa0f4d45..c2c7cfcd0fd 100644
7e98da0
--- a/grub-core/loader/linux.c
7e98da0
+++ b/grub-core/loader/linux.c
7e98da0
@@ -4,6 +4,7 @@
7e98da0
 #include <grub/misc.h>
7e98da0
 #include <grub/file.h>
7e98da0
 #include <grub/mm.h>
7e98da0
+#include <grub/tpm.h>
7e98da0
 
7e98da0
 struct newc_head
7e98da0
 {
7e98da0
@@ -288,6 +289,9 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
7e98da0
 	  grub_initrd_close (initrd_ctx);
7e98da0
 	  return grub_errno;
7e98da0
 	}
7e98da0
+      grub_tpm_measure (ptr, cursize, GRUB_BINARY_PCR, "grub_initrd", "Initrd");
7e98da0
+      grub_print_error();
7e98da0
+
7e98da0
       ptr += cursize;
7e98da0
     }
7e98da0
   if (newc)
7e98da0
diff --git a/grub-core/loader/multiboot.c b/grub-core/loader/multiboot.c
7e98da0
index 26df46a4161..9a8dae5565b 100644
7e98da0
--- a/grub-core/loader/multiboot.c
7e98da0
+++ b/grub-core/loader/multiboot.c
7e98da0
@@ -51,6 +51,7 @@
7e98da0
 #include <grub/memory.h>
7e98da0
 #include <grub/i18n.h>
7e98da0
 #include <grub/efi/sb.h>
7e98da0
+#include <grub/tpm.h>
7e98da0
 
7e98da0
 GRUB_MOD_LICENSE ("GPLv3+");
7e98da0
 
7e98da0
@@ -440,6 +441,8 @@ grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
7e98da0
     }
7e98da0
 
7e98da0
   grub_file_close (file);
7e98da0
+  grub_tpm_measure (module, size, GRUB_BINARY_PCR, "grub_multiboot", argv[0]);
7e98da0
+  grub_print_error();
7e98da0
   return GRUB_ERR_NONE;
7e98da0
 }
7e98da0
 
7e98da0
diff --git a/grub-core/loader/multiboot_mbi2.c b/grub-core/loader/multiboot_mbi2.c
7e98da0
index 4df6595954d..54078455e2f 100644
7e98da0
--- a/grub-core/loader/multiboot_mbi2.c
7e98da0
+++ b/grub-core/loader/multiboot_mbi2.c
7e98da0
@@ -36,6 +36,7 @@
7e98da0
 #include <grub/i18n.h>
7e98da0
 #include <grub/net.h>
7e98da0
 #include <grub/lib/cmdline.h>
7e98da0
+#include <grub/tpm.h>
7e98da0
 
7e98da0
 #if defined (GRUB_MACHINE_EFI)
7e98da0
 #include <grub/efi/efi.h>
7e98da0
@@ -131,8 +132,10 @@ grub_multiboot2_load (grub_file_t file, const char *filename)
7e98da0
 
7e98da0
   COMPILE_TIME_ASSERT (MULTIBOOT_HEADER_ALIGN % 4 == 0);
7e98da0
 
7e98da0
+  grub_tpm_measure ((unsigned char *)mld.buffer, len, GRUB_BINARY_PCR, "grub_multiboot", filename);
7e98da0
+  grub_print_error();
7e98da0
+
7e98da0
   header = find_header (mld.buffer, len);
7e98da0
-
7e98da0
   if (header == 0)
7e98da0
     {
7e98da0
       grub_free (mld.buffer);
7e98da0
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
7e98da0
index cf6cd6601d6..93965777138 100644
7e98da0
--- a/grub-core/script/execute.c
7e98da0
+++ b/grub-core/script/execute.c
7e98da0
@@ -30,6 +30,7 @@
7e98da0
 #ifdef GRUB_MACHINE_IEEE1275
7e98da0
 #include <grub/ieee1275/ieee1275.h>
7e98da0
 #endif
7e98da0
+#include <grub/tpm.h>
7e98da0
 
7e98da0
 /* Max digits for a char is 3 (0xFF is 255), similarly for an int it
7e98da0
    is sizeof (int) * 3, and one extra for a possible -ve sign.  */
7e98da0
@@ -967,8 +968,9 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
7e98da0
   grub_err_t ret = 0;
7e98da0
   grub_script_function_t func = 0;
7e98da0
   char errnobuf[18];
7e98da0
-  char *cmdname;
7e98da0
-  int argc;
7e98da0
+  char *cmdname, *cmdstring;
7e98da0
+  int argc, offset = 0, cmdlen = 0;
7e98da0
+  unsigned int i;
7e98da0
   char **args;
7e98da0
   int invert;
7e98da0
   struct grub_script_argv argv = { 0, 0, 0 };
7e98da0
@@ -977,6 +979,26 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
7e98da0
   if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0])
7e98da0
     return grub_errno;
7e98da0
 
7e98da0
+  for (i = 0; i < argv.argc; i++) {
7e98da0
+	  cmdlen += grub_strlen (argv.args[i]) + 1;
7e98da0
+  }
7e98da0
+
7e98da0
+  cmdstring = grub_malloc (cmdlen);
7e98da0
+  if (!cmdstring)
7e98da0
+  {
7e98da0
+	  return grub_error (GRUB_ERR_OUT_OF_MEMORY,
7e98da0
+			     N_("cannot allocate command buffer"));
7e98da0
+  }
7e98da0
+
7e98da0
+  for (i = 0; i < argv.argc; i++) {
7e98da0
+	  offset += grub_snprintf (cmdstring + offset, cmdlen - offset, "%s ",
7e98da0
+				   argv.args[i]);
7e98da0
+  }
7e98da0
+  cmdstring[cmdlen-1]= '\0';
7e98da0
+  grub_tpm_measure ((unsigned char *)cmdstring, cmdlen, GRUB_ASCII_PCR,
7e98da0
+		    "grub_cmd", cmdstring);
7e98da0
+  grub_print_error();
7e98da0
+  grub_free(cmdstring);
7e98da0
   invert = 0;
7e98da0
   argc = argv.argc - 1;
7e98da0
   args = argv.args + 1;
0bcec26
diff --git a/include/grub/efi/tpm.h b/include/grub/efi/tpm.h
0bcec26
new file mode 100644
7e98da0
index 00000000000..63d8a0fe714
0bcec26
--- /dev/null
0bcec26
+++ b/include/grub/efi/tpm.h
0bcec26
@@ -0,0 +1,153 @@
0bcec26
+/*
0bcec26
+ *  GRUB  --  GRand Unified Bootloader
0bcec26
+ *  Copyright (C) 2015  Free Software Foundation, Inc.
0bcec26
+ *
0bcec26
+ *  GRUB is free software: you can redistribute it and/or modify
0bcec26
+ *  it under the terms of the GNU General Public License as published by
0bcec26
+ *  the Free Software Foundation, either version 3 of the License, or
0bcec26
+ *  (at your option) any later version.
0bcec26
+ *
0bcec26
+ *  GRUB is distributed in the hope that it will be useful,
0bcec26
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0bcec26
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0bcec26
+ *  GNU General Public License for more details.
0bcec26
+ *
0bcec26
+ *  You should have received a copy of the GNU General Public License
0bcec26
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
0bcec26
+ */
0bcec26
+
0bcec26
+#ifndef GRUB_EFI_TPM_HEADER
0bcec26
+#define GRUB_EFI_TPM_HEADER 1
0bcec26
+
0bcec26
+#define EFI_TPM_GUID {0xf541796d, 0xa62e, 0x4954, {0xa7, 0x75, 0x95, 0x84, 0xf6, 0x1b, 0x9c, 0xdd }};
0bcec26
+#define EFI_TPM2_GUID {0x607f766c, 0x7455, 0x42be, {0x93, 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f }};
0bcec26
+
0bcec26
+typedef struct {
0bcec26
+  grub_efi_uint8_t Major;
0bcec26
+  grub_efi_uint8_t Minor;
0bcec26
+  grub_efi_uint8_t RevMajor;
0bcec26
+  grub_efi_uint8_t RevMinor;
0bcec26
+} TCG_VERSION;
0bcec26
+
0bcec26
+typedef struct _TCG_EFI_BOOT_SERVICE_CAPABILITY {
0bcec26
+  grub_efi_uint8_t          Size;                /// Size of this structure.
0bcec26
+  TCG_VERSION    StructureVersion;
0bcec26
+  TCG_VERSION    ProtocolSpecVersion;
0bcec26
+  grub_efi_uint8_t          HashAlgorithmBitmap; /// Hash algorithms .
0bcec26
+  char        TPMPresentFlag;      /// 00h = TPM not present.
0bcec26
+  char        TPMDeactivatedFlag;  /// 01h = TPM currently deactivated.
0bcec26
+} TCG_EFI_BOOT_SERVICE_CAPABILITY;
0bcec26
+
0bcec26
+typedef struct {
0bcec26
+  grub_efi_uint32_t PCRIndex;
0bcec26
+  grub_efi_uint32_t EventType;
0bcec26
+  grub_efi_uint8_t digest[20];
0bcec26
+  grub_efi_uint32_t EventSize;
0bcec26
+  grub_efi_uint8_t  Event[1];
0bcec26
+} TCG_PCR_EVENT;
0bcec26
+
0bcec26
+struct grub_efi_tpm_protocol
0bcec26
+{
0bcec26
+  grub_efi_status_t (*status_check) (struct grub_efi_tpm_protocol *this,
0bcec26
+				     TCG_EFI_BOOT_SERVICE_CAPABILITY *ProtocolCapability,
0bcec26
+				     grub_efi_uint32_t *TCGFeatureFlags,
0bcec26
+				     grub_efi_physical_address_t *EventLogLocation,
0bcec26
+				     grub_efi_physical_address_t *EventLogLastEntry);
0bcec26
+  grub_efi_status_t (*hash_all) (struct grub_efi_tpm_protocol *this,
0bcec26
+				 grub_efi_uint8_t *HashData,
0bcec26
+				 grub_efi_uint64_t HashLen,
0bcec26
+				 grub_efi_uint32_t AlgorithmId,
0bcec26
+				 grub_efi_uint64_t *HashedDataLen,
0bcec26
+				 grub_efi_uint8_t **HashedDataResult);
0bcec26
+  grub_efi_status_t (*log_event) (struct grub_efi_tpm_protocol *this,
0bcec26
+				  TCG_PCR_EVENT *TCGLogData,
0bcec26
+				  grub_efi_uint32_t *EventNumber,
0bcec26
+				  grub_efi_uint32_t Flags);
0bcec26
+  grub_efi_status_t (*pass_through_to_tpm) (struct grub_efi_tpm_protocol *this,
0bcec26
+					    grub_efi_uint32_t TpmInputParameterBlockSize,
0bcec26
+					    grub_efi_uint8_t *TpmInputParameterBlock,
0bcec26
+					    grub_efi_uint32_t TpmOutputParameterBlockSize,
0bcec26
+					    grub_efi_uint8_t *TpmOutputParameterBlock);
0bcec26
+  grub_efi_status_t (*log_extend_event) (struct grub_efi_tpm_protocol *this,
0bcec26
+					 grub_efi_physical_address_t HashData,
0bcec26
+					 grub_efi_uint64_t HashDataLen,
0bcec26
+					 grub_efi_uint32_t AlgorithmId,
0bcec26
+					 TCG_PCR_EVENT *TCGLogData,
0bcec26
+					 grub_efi_uint32_t *EventNumber,
0bcec26
+					 grub_efi_physical_address_t *EventLogLastEntry);
0bcec26
+};
0bcec26
+
0bcec26
+typedef struct grub_efi_tpm_protocol grub_efi_tpm_protocol_t;
0bcec26
+
0bcec26
+typedef grub_efi_uint32_t EFI_TCG2_EVENT_LOG_BITMAP;
0bcec26
+typedef grub_efi_uint32_t EFI_TCG2_EVENT_LOG_FORMAT;
0bcec26
+typedef grub_efi_uint32_t EFI_TCG2_EVENT_ALGORITHM_BITMAP;
0bcec26
+
0bcec26
+typedef struct tdEFI_TCG2_VERSION {
0bcec26
+  grub_efi_uint8_t Major;
0bcec26
+  grub_efi_uint8_t Minor;
0bcec26
+} GRUB_PACKED EFI_TCG2_VERSION;
0bcec26
+
0bcec26
+typedef struct tdEFI_TCG2_BOOT_SERVICE_CAPABILITY {
0bcec26
+  grub_efi_uint8_t Size;
0bcec26
+  EFI_TCG2_VERSION StructureVersion;
0bcec26
+  EFI_TCG2_VERSION ProtocolVersion;
0bcec26
+  EFI_TCG2_EVENT_ALGORITHM_BITMAP HashAlgorithmBitmap;
0bcec26
+  EFI_TCG2_EVENT_LOG_BITMAP SupportedEventLogs;
0bcec26
+  grub_efi_boolean_t TPMPresentFlag;
0bcec26
+  grub_efi_uint16_t MaxCommandSize;
0bcec26
+  grub_efi_uint16_t MaxResponseSize;
0bcec26
+  grub_efi_uint32_t ManufacturerID;
0bcec26
+  grub_efi_uint32_t NumberOfPcrBanks;
0bcec26
+  EFI_TCG2_EVENT_ALGORITHM_BITMAP ActivePcrBanks;
0bcec26
+} EFI_TCG2_BOOT_SERVICE_CAPABILITY;
0bcec26
+
0bcec26
+typedef grub_efi_uint32_t TCG_PCRINDEX;
0bcec26
+typedef grub_efi_uint32_t TCG_EVENTTYPE;
0bcec26
+
0bcec26
+typedef struct tdEFI_TCG2_EVENT_HEADER {
0bcec26
+  grub_efi_uint32_t HeaderSize;
0bcec26
+  grub_efi_uint16_t HeaderVersion;
0bcec26
+  TCG_PCRINDEX PCRIndex;
0bcec26
+  TCG_EVENTTYPE EventType;
0bcec26
+} GRUB_PACKED EFI_TCG2_EVENT_HEADER;
0bcec26
+
0bcec26
+typedef struct tdEFI_TCG2_EVENT {
0bcec26
+  grub_efi_uint32_t Size;
0bcec26
+  EFI_TCG2_EVENT_HEADER Header;
0bcec26
+  grub_efi_uint8_t Event[1];
0bcec26
+} GRUB_PACKED EFI_TCG2_EVENT;
0bcec26
+
0bcec26
+struct grub_efi_tpm2_protocol
0bcec26
+{
0bcec26
+  grub_efi_status_t (*get_capability) (struct grub_efi_tpm2_protocol *this,
0bcec26
+				       EFI_TCG2_BOOT_SERVICE_CAPABILITY *ProtocolCapability);
0bcec26
+  grub_efi_status_t (*get_event_log) (struct grub_efi_tpm2_protocol *this,
0bcec26
+				      EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
0bcec26
+				      grub_efi_physical_address_t *EventLogLocation,
0bcec26
+				      grub_efi_physical_address_t *EventLogLastEntry,
0bcec26
+				      grub_efi_boolean_t *EventLogTruncated);
0bcec26
+  grub_efi_status_t (*hash_log_extend_event) (struct grub_efi_tpm2_protocol *this,
0bcec26
+					      grub_efi_uint64_t Flags,
7e98da0
+					      grub_efi_physical_address_t DataToHash,
0bcec26
+					      grub_efi_uint64_t DataToHashLen,
0bcec26
+					      EFI_TCG2_EVENT *EfiTcgEvent);
0bcec26
+  grub_efi_status_t (*submit_command) (struct grub_efi_tpm2_protocol *this,
0bcec26
+				       grub_efi_uint32_t InputParameterBlockSize,
0bcec26
+				       grub_efi_uint8_t *InputParameterBlock,
0bcec26
+				       grub_efi_uint32_t OutputParameterBlockSize,
0bcec26
+				       grub_efi_uint8_t *OutputParameterBlock);
0bcec26
+  grub_efi_status_t (*get_active_pcr_blanks) (struct grub_efi_tpm2_protocol *this,
0bcec26
+					      grub_efi_uint32_t *ActivePcrBanks);
0bcec26
+  grub_efi_status_t (*set_active_pcr_banks) (struct grub_efi_tpm2_protocol *this,
0bcec26
+					     grub_efi_uint32_t ActivePcrBanks);
0bcec26
+  grub_efi_status_t (*get_result_of_set_active_pcr_banks) (struct grub_efi_tpm2_protocol *this,
0bcec26
+							   grub_efi_uint32_t *OperationPresent,
0bcec26
+							   grub_efi_uint32_t *Response);
0bcec26
+};
0bcec26
+
0bcec26
+typedef struct grub_efi_tpm2_protocol grub_efi_tpm2_protocol_t;
0bcec26
+
0bcec26
+#define TCG_ALG_SHA 0x00000004
0bcec26
+
0bcec26
+#endif
0bcec26
diff --git a/include/grub/tpm.h b/include/grub/tpm.h
0bcec26
new file mode 100644
7e98da0
index 00000000000..ce52be4ff7f
0bcec26
--- /dev/null
0bcec26
+++ b/include/grub/tpm.h
7e98da0
@@ -0,0 +1,94 @@
0bcec26
+/*
0bcec26
+ *  GRUB  --  GRand Unified Bootloader
0bcec26
+ *  Copyright (C) 2015  Free Software Foundation, Inc.
0bcec26
+ *
0bcec26
+ *  GRUB is free software: you can redistribute it and/or modify
0bcec26
+ *  it under the terms of the GNU General Public License as published by
0bcec26
+ *  the Free Software Foundation, either version 3 of the License, or
0bcec26
+ *  (at your option) any later version.
0bcec26
+ *
0bcec26
+ *  GRUB is distributed in the hope that it will be useful,
0bcec26
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0bcec26
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0bcec26
+ *  GNU General Public License for more details.
0bcec26
+ *
0bcec26
+ *  You should have received a copy of the GNU General Public License
0bcec26
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
0bcec26
+ */
0bcec26
+
0bcec26
+#ifndef GRUB_TPM_HEADER
0bcec26
+#define GRUB_TPM_HEADER 1
0bcec26
+
0bcec26
+#define SHA1_DIGEST_SIZE 20
0bcec26
+
0bcec26
+#define TPM_BASE 0x0
0bcec26
+#define TPM_SUCCESS TPM_BASE
0bcec26
+#define TPM_AUTHFAIL (TPM_BASE + 0x1)
0bcec26
+#define TPM_BADINDEX (TPM_BASE + 0x2)
0bcec26
+
7e98da0
+#define GRUB_ASCII_PCR 8
7e98da0
+#define GRUB_BINARY_PCR 9
0bcec26
+
0bcec26
+#define TPM_TAG_RQU_COMMAND 0x00C1
0bcec26
+#define TPM_ORD_Extend 0x14
0bcec26
+
0bcec26
+#define EV_IPL 0x0d
0bcec26
+
0bcec26
+/* TCG_PassThroughToTPM Input Parameter Block */
0bcec26
+typedef struct {
0bcec26
+        grub_uint16_t IPBLength;
0bcec26
+        grub_uint16_t Reserved1;
0bcec26
+        grub_uint16_t OPBLength;
0bcec26
+        grub_uint16_t Reserved2;
0bcec26
+        grub_uint8_t TPMOperandIn[1];
0bcec26
+} GRUB_PACKED PassThroughToTPM_InputParamBlock;
0bcec26
+
0bcec26
+/* TCG_PassThroughToTPM Output Parameter Block */
0bcec26
+typedef struct {
0bcec26
+        grub_uint16_t OPBLength;
0bcec26
+        grub_uint16_t Reserved;
0bcec26
+        grub_uint8_t TPMOperandOut[1];
0bcec26
+} GRUB_PACKED PassThroughToTPM_OutputParamBlock;
0bcec26
+
0bcec26
+typedef struct {
0bcec26
+        grub_uint16_t tag;
0bcec26
+        grub_uint32_t paramSize;
0bcec26
+        grub_uint32_t ordinal;
0bcec26
+        grub_uint32_t pcrNum;
0bcec26
+        grub_uint8_t inDigest[SHA1_DIGEST_SIZE];                /* The 160 bit value representing the event to be recorded. */
0bcec26
+} GRUB_PACKED ExtendIncoming;
0bcec26
+
0bcec26
+/* TPM_Extend Outgoing Operand */
0bcec26
+typedef struct {
0bcec26
+        grub_uint16_t tag;
0bcec26
+        grub_uint32_t paramSize;
0bcec26
+        grub_uint32_t returnCode;
0bcec26
+        grub_uint8_t outDigest[SHA1_DIGEST_SIZE];               /* The PCR value after execution of the command. */
0bcec26
+} GRUB_PACKED ExtendOutgoing;
0bcec26
+
0bcec26
+grub_err_t EXPORT_FUNC(grub_tpm_measure) (unsigned char *buf, grub_size_t size,
7e98da0
+					  grub_uint8_t pcr, const char *kind,
0bcec26
+					  const char *description);
7e98da0
+#if defined (GRUB_MACHINE_EFI)
0bcec26
+grub_err_t grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
0bcec26
+			    PassThroughToTPM_OutputParamBlock *outbuf);
0bcec26
+grub_err_t grub_tpm_log_event(unsigned char *buf, grub_size_t size,
0bcec26
+			      grub_uint8_t pcr, const char *description);
0bcec26
+#else
7e98da0
+static inline grub_err_t grub_tpm_execute(
7e98da0
+	PassThroughToTPM_InputParamBlock *inbuf __attribute__ ((unused)),
7e98da0
+	PassThroughToTPM_OutputParamBlock *outbuf __attribute__ ((unused)))
7e98da0
+{
7e98da0
+	return 0;
7e98da0
+};
7e98da0
+static inline grub_err_t grub_tpm_log_event(
7e98da0
+	unsigned char *buf __attribute__ ((unused)),
7e98da0
+	grub_size_t size __attribute__ ((unused)),
7e98da0
+	grub_uint8_t pcr __attribute__ ((unused)),
7e98da0
+	const char *description __attribute__ ((unused)))
0bcec26
+{
0bcec26
+	return 0;
0bcec26
+};
0bcec26
+#endif
0bcec26
+
0bcec26
+#endif
0bcec26
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
15a2072
index f36200bd688..3781bb9cbb9 100644
0bcec26
--- a/grub-core/Makefile.am
0bcec26
+++ b/grub-core/Makefile.am
0bcec26
@@ -94,6 +94,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
0bcec26
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
0bcec26
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h
0bcec26
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/net.h
0bcec26
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/tpm.h
0bcec26
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/memory.h
0bcec26
 
0bcec26
 if COND_i386_pc