6b2dd0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
0bcec2
From: Matthew Garrett <mjg59@coreos.com>
0bcec2
Date: Tue, 14 Jul 2015 17:06:35 -0700
31cddd
Subject: [PATCH] Core TPM support
0bcec2
0bcec2
Add support for performing basic TPM measurements. Right now this only
0bcec2
supports extending PCRs statically and only on UEFI and BIOS systems, but
0bcec2
will measure all modules as they're loaded.
0bcec2
---
0bcec2
 grub-core/Makefile.core.def  |   3 +
0bcec2
 grub-core/kern/dl.c          |   3 +
0bcec2
 grub-core/kern/efi/tpm.c     | 282 +++++++++++++++++++++++++++++++++++++++++++
0bcec2
 grub-core/kern/i386/pc/tpm.c | 132 ++++++++++++++++++++
0bcec2
 grub-core/kern/tpm.c         |  13 ++
0bcec2
 include/grub/efi/tpm.h       | 153 +++++++++++++++++++++++
0bcec2
 include/grub/tpm.h           |  91 ++++++++++++++
0bcec2
 grub-core/Makefile.am        |   1 +
0bcec2
 8 files changed, 678 insertions(+)
0bcec2
 create mode 100644 grub-core/kern/efi/tpm.c
0bcec2
 create mode 100644 grub-core/kern/i386/pc/tpm.c
0bcec2
 create mode 100644 grub-core/kern/tpm.c
0bcec2
 create mode 100644 include/grub/efi/tpm.h
0bcec2
 create mode 100644 include/grub/tpm.h
0bcec2
0bcec2
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
15a207
index cb24f92a431..420831bc89e 100644
0bcec2
--- a/grub-core/Makefile.core.def
0bcec2
+++ b/grub-core/Makefile.core.def
0bcec2
@@ -131,6 +131,7 @@ kernel = {
0bcec2
   common = kern/term.c;
0bcec2
   common = kern/qsort.c;
0bcec2
   common = kern/backtrace.c;
0bcec2
+  common = kern/tpm.c;
0bcec2
 
0bcec2
   x86 = kern/i386/backtrace.c;
0bcec2
   i386_xen = kern/i386/backtrace.c;
0bcec2
@@ -199,6 +200,7 @@ kernel = {
0bcec2
   efi = kern/acpi.c;
0bcec2
   efi = kern/efi/acpi.c;
0bcec2
   efi = lib/envblk.c;
0bcec2
+  efi = kern/efi/tpm.c;
0bcec2
   i386_coreboot = kern/i386/pc/acpi.c;
0bcec2
   i386_multiboot = kern/i386/pc/acpi.c;
0bcec2
   i386_coreboot = kern/acpi.c;
15a207
@@ -245,6 +247,7 @@ kernel = {
0bcec2
 
0bcec2
   i386_pc = kern/i386/pc/init.c;
0bcec2
   i386_pc = kern/i386/pc/mmap.c;
0bcec2
+  i386_pc = kern/i386/pc/tpm.c;
0bcec2
   i386_pc = term/i386/pc/console.c;
0bcec2
 
0bcec2
   i386_qemu = bus/pci.c;
0bcec2
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
0bcec2
index eb8b969cded..387d1e6446d 100644
0bcec2
--- a/grub-core/kern/dl.c
0bcec2
+++ b/grub-core/kern/dl.c
0bcec2
@@ -33,6 +33,7 @@
0bcec2
 #include <grub cache.h="">
0bcec2
 #include <grub i18n.h="">
0bcec2
 #include <grub efi="" sb.h="">
0bcec2
+#include <grub tpm.h="">
0bcec2
 
0bcec2
 /* Platforms where modules are in a readonly area of memory.  */
0bcec2
 #if defined(GRUB_MACHINE_QEMU)
0bcec2
@@ -829,6 +830,8 @@ grub_dl_load_file (const char *filename)
0bcec2
      opens of the same device.  */
0bcec2
   grub_file_close (file);
0bcec2
 
0bcec2
+  grub_tpm_measure(core, size, GRUB_TPM_PCR, filename);
0bcec2
+
0bcec2
   mod = grub_dl_load_core (core, size);
0bcec2
   grub_free (core);
0bcec2
   if (! mod)
0bcec2
diff --git a/grub-core/kern/efi/tpm.c b/grub-core/kern/efi/tpm.c
0bcec2
new file mode 100644
0bcec2
index 00000000000..c9fb3c133f3
0bcec2
--- /dev/null
0bcec2
+++ b/grub-core/kern/efi/tpm.c
0bcec2
@@ -0,0 +1,282 @@
0bcec2
+#include <grub err.h="">
0bcec2
+#include <grub i18n.h="">
0bcec2
+#include <grub efi="" api.h="">
0bcec2
+#include <grub efi="" efi.h="">
0bcec2
+#include <grub efi="" tpm.h="">
0bcec2
+#include <grub mm.h="">
0bcec2
+#include <grub tpm.h="">
0bcec2
+#include <grub term.h="">
0bcec2
+
0bcec2
+static grub_efi_guid_t tpm_guid = EFI_TPM_GUID;
0bcec2
+static grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID;
0bcec2
+
0bcec2
+static grub_efi_boolean_t grub_tpm_present(grub_efi_tpm_protocol_t *tpm)
0bcec2
+{
0bcec2
+  grub_efi_status_t status;
0bcec2
+  TCG_EFI_BOOT_SERVICE_CAPABILITY caps;
0bcec2
+  grub_uint32_t flags;
0bcec2
+  grub_efi_physical_address_t eventlog, lastevent;
0bcec2
+
0bcec2
+  caps.Size = (grub_uint8_t)sizeof(caps);
0bcec2
+
0bcec2
+  status = efi_call_5(tpm->status_check, tpm, &caps, &flags, &eventlog,
0bcec2
+		      &lastevent);
0bcec2
+
0bcec2
+  if (status != GRUB_EFI_SUCCESS || caps.TPMDeactivatedFlag
0bcec2
+      || !caps.TPMPresentFlag)
0bcec2
+    return 0;
0bcec2
+
0bcec2
+  return 1;
0bcec2
+}
0bcec2
+
0bcec2
+static grub_efi_boolean_t grub_tpm2_present(grub_efi_tpm2_protocol_t *tpm)
0bcec2
+{
0bcec2
+  grub_efi_status_t status;
0bcec2
+  EFI_TCG2_BOOT_SERVICE_CAPABILITY caps;
0bcec2
+
0bcec2
+  caps.Size = (grub_uint8_t)sizeof(caps);
0bcec2
+
0bcec2
+  status = efi_call_2(tpm->get_capability, tpm, &caps);
0bcec2
+
0bcec2
+  if (status != GRUB_EFI_SUCCESS || !caps.TPMPresentFlag)
0bcec2
+    return 0;
0bcec2
+
0bcec2
+  return 1;
0bcec2
+}
0bcec2
+
0bcec2
+static grub_efi_boolean_t grub_tpm_handle_find(grub_efi_handle_t *tpm_handle,
0bcec2
+					       grub_efi_uint8_t *protocol_version)
0bcec2
+{
0bcec2
+  grub_efi_handle_t *handles;
0bcec2
+  grub_efi_uintn_t num_handles;
0bcec2
+
0bcec2
+  handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm_guid, NULL,
0bcec2
+				    &num_handles);
0bcec2
+  if (handles && num_handles > 0) {
0bcec2
+    *tpm_handle = handles[0];
0bcec2
+    *protocol_version = 1;
0bcec2
+    return 1;
0bcec2
+  }
0bcec2
+
0bcec2
+  handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &tpm2_guid, NULL,
0bcec2
+				    &num_handles);
0bcec2
+  if (handles && num_handles > 0) {
0bcec2
+    *tpm_handle = handles[0];
0bcec2
+    *protocol_version = 2;
0bcec2
+    return 1;
0bcec2
+  }
0bcec2
+
0bcec2
+  return 0;
0bcec2
+}
0bcec2
+
0bcec2
+static grub_err_t
0bcec2
+grub_tpm1_execute(grub_efi_handle_t tpm_handle,
0bcec2
+		  PassThroughToTPM_InputParamBlock *inbuf,
0bcec2
+		  PassThroughToTPM_OutputParamBlock *outbuf)
0bcec2
+{
0bcec2
+  grub_efi_status_t status;
0bcec2
+  grub_efi_tpm_protocol_t *tpm;
0bcec2
+  grub_uint32_t inhdrsize = sizeof(*inbuf) - sizeof(inbuf->TPMOperandIn);
0bcec2
+  grub_uint32_t outhdrsize = sizeof(*outbuf) - sizeof(outbuf->TPMOperandOut);
0bcec2
+
0bcec2
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
0bcec2
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
0bcec2
+
0bcec2
+  if (!grub_tpm_present(tpm))
0bcec2
+    return 0;
0bcec2
+
0bcec2
+  /* UEFI TPM protocol takes the raw operand block, no param block header */
0bcec2
+  status = efi_call_5 (tpm->pass_through_to_tpm, tpm,
0bcec2
+		       inbuf->IPBLength - inhdrsize, inbuf->TPMOperandIn,
0bcec2
+		       outbuf->OPBLength - outhdrsize, outbuf->TPMOperandOut);
0bcec2
+
0bcec2
+  switch (status) {
0bcec2
+  case GRUB_EFI_SUCCESS:
0bcec2
+    return 0;
0bcec2
+  case GRUB_EFI_DEVICE_ERROR:
0bcec2
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
0bcec2
+  case GRUB_EFI_INVALID_PARAMETER:
0bcec2
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
0bcec2
+  case GRUB_EFI_BUFFER_TOO_SMALL:
0bcec2
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
0bcec2
+  case GRUB_EFI_NOT_FOUND:
0bcec2
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
0bcec2
+  default:
0bcec2
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
0bcec2
+  }
0bcec2
+}
0bcec2
+
0bcec2
+static grub_err_t
0bcec2
+grub_tpm2_execute(grub_efi_handle_t tpm_handle,
0bcec2
+		  PassThroughToTPM_InputParamBlock *inbuf,
0bcec2
+		  PassThroughToTPM_OutputParamBlock *outbuf)
0bcec2
+{
0bcec2
+  grub_efi_status_t status;
0bcec2
+  grub_efi_tpm2_protocol_t *tpm;
0bcec2
+  grub_uint32_t inhdrsize = sizeof(*inbuf) - sizeof(inbuf->TPMOperandIn);
0bcec2
+  grub_uint32_t outhdrsize = sizeof(*outbuf) - sizeof(outbuf->TPMOperandOut);
0bcec2
+
0bcec2
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
0bcec2
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
0bcec2
+
0bcec2
+  if (!grub_tpm2_present(tpm))
0bcec2
+    return 0;
0bcec2
+
0bcec2
+  /* UEFI TPM protocol takes the raw operand block, no param block header */
0bcec2
+  status = efi_call_5 (tpm->submit_command, tpm,
0bcec2
+		       inbuf->IPBLength - inhdrsize, inbuf->TPMOperandIn,
0bcec2
+		       outbuf->OPBLength - outhdrsize, outbuf->TPMOperandOut);
0bcec2
+
0bcec2
+  switch (status) {
0bcec2
+  case GRUB_EFI_SUCCESS:
0bcec2
+    return 0;
0bcec2
+  case GRUB_EFI_DEVICE_ERROR:
0bcec2
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
0bcec2
+  case GRUB_EFI_INVALID_PARAMETER:
0bcec2
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
0bcec2
+  case GRUB_EFI_BUFFER_TOO_SMALL:
0bcec2
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
0bcec2
+  case GRUB_EFI_NOT_FOUND:
0bcec2
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
0bcec2
+  default:
0bcec2
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
0bcec2
+  }
0bcec2
+}
0bcec2
+
0bcec2
+grub_err_t
0bcec2
+grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
0bcec2
+		 PassThroughToTPM_OutputParamBlock *outbuf)
0bcec2
+{
0bcec2
+  grub_efi_handle_t tpm_handle;
0bcec2
+   grub_uint8_t protocol_version;
0bcec2
+
0bcec2
+  /* It's not a hard failure for there to be no TPM */
0bcec2
+  if (!grub_tpm_handle_find(&tpm_handle, &protocol_version))
0bcec2
+    return 0;
0bcec2
+
0bcec2
+  if (protocol_version == 1) {
0bcec2
+    return grub_tpm1_execute(tpm_handle, inbuf, outbuf);
0bcec2
+  } else {
0bcec2
+    return grub_tpm2_execute(tpm_handle, inbuf, outbuf);
0bcec2
+  }
0bcec2
+}
0bcec2
+
0bcec2
+typedef struct {
0bcec2
+	grub_uint32_t pcrindex;
0bcec2
+	grub_uint32_t eventtype;
0bcec2
+	grub_uint8_t digest[20];
0bcec2
+	grub_uint32_t eventsize;
0bcec2
+	grub_uint8_t event[1];
0bcec2
+} Event;
0bcec2
+
0bcec2
+
0bcec2
+static grub_err_t
0bcec2
+grub_tpm1_log_event(grub_efi_handle_t tpm_handle, unsigned char *buf,
0bcec2
+		    grub_size_t size, grub_uint8_t pcr,
0bcec2
+		    const char *description)
0bcec2
+{
0bcec2
+  Event *event;
0bcec2
+  grub_efi_status_t status;
0bcec2
+  grub_efi_tpm_protocol_t *tpm;
0bcec2
+  grub_efi_physical_address_t lastevent;
0bcec2
+  grub_uint32_t algorithm;
0bcec2
+  grub_uint32_t eventnum = 0;
0bcec2
+
0bcec2
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm_guid,
0bcec2
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
0bcec2
+
0bcec2
+  if (!grub_tpm_present(tpm))
0bcec2
+    return 0;
0bcec2
+
0bcec2
+  event = grub_zalloc(sizeof (Event) + grub_strlen(description) + 1);
0bcec2
+  if (!event)
0bcec2
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
0bcec2
+		       N_("cannot allocate TPM event buffer"));
0bcec2
+
0bcec2
+  event->pcrindex = pcr;
0bcec2
+  event->eventtype = EV_IPL;
0bcec2
+  event->eventsize = grub_strlen(description) + 1;
0bcec2
+  grub_memcpy(event->event, description, event->eventsize);
0bcec2
+
0bcec2
+  algorithm = TCG_ALG_SHA;
0bcec2
+  status = efi_call_7 (tpm->log_extend_event, tpm, buf, (grub_uint64_t) size,
0bcec2
+		       algorithm, event, &eventnum, &lastevent);
0bcec2
+
0bcec2
+  switch (status) {
0bcec2
+  case GRUB_EFI_SUCCESS:
0bcec2
+    return 0;
0bcec2
+  case GRUB_EFI_DEVICE_ERROR:
0bcec2
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
0bcec2
+  case GRUB_EFI_INVALID_PARAMETER:
0bcec2
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
0bcec2
+  case GRUB_EFI_BUFFER_TOO_SMALL:
0bcec2
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
0bcec2
+  case GRUB_EFI_NOT_FOUND:
0bcec2
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
0bcec2
+  default:
0bcec2
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
0bcec2
+  }
0bcec2
+}
0bcec2
+
0bcec2
+static grub_err_t
0bcec2
+grub_tpm2_log_event(grub_efi_handle_t tpm_handle, unsigned char *buf,
0bcec2
+		   grub_size_t size, grub_uint8_t pcr,
0bcec2
+		   const char *description)
0bcec2
+{
0bcec2
+  EFI_TCG2_EVENT *event;
0bcec2
+  grub_efi_status_t status;
0bcec2
+  grub_efi_tpm2_protocol_t *tpm;
0bcec2
+
0bcec2
+  tpm = grub_efi_open_protocol (tpm_handle, &tpm2_guid,
0bcec2
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
0bcec2
+
0bcec2
+  if (!grub_tpm2_present(tpm))
0bcec2
+    return 0;
0bcec2
+
0bcec2
+  event = grub_zalloc(sizeof (EFI_TCG2_EVENT) + grub_strlen(description) + 1);
0bcec2
+  if (!event)
0bcec2
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
0bcec2
+		       N_("cannot allocate TPM event buffer"));
0bcec2
+
0bcec2
+  event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);
0bcec2
+  event->Header.HeaderVersion = 1;
0bcec2
+  event->Header.PCRIndex = pcr;
0bcec2
+  event->Header.EventType = EV_IPL;
0bcec2
+  event->Size = sizeof(*event) - sizeof(event->Event) + grub_strlen(description) + 1;
0bcec2
+  grub_memcpy(event->Event, description, grub_strlen(description) + 1);
0bcec2
+
0bcec2
+  status = efi_call_5 (tpm->hash_log_extend_event, tpm, 0, buf,
0bcec2
+		       (grub_uint64_t) size, event);
0bcec2
+
0bcec2
+  switch (status) {
0bcec2
+  case GRUB_EFI_SUCCESS:
0bcec2
+    return 0;
0bcec2
+  case GRUB_EFI_DEVICE_ERROR:
0bcec2
+    return grub_error (GRUB_ERR_IO, N_("Command failed"));
0bcec2
+  case GRUB_EFI_INVALID_PARAMETER:
0bcec2
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Invalid parameter"));
0bcec2
+  case GRUB_EFI_BUFFER_TOO_SMALL:
0bcec2
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Output buffer too small"));
0bcec2
+  case GRUB_EFI_NOT_FOUND:
0bcec2
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("TPM unavailable"));
0bcec2
+  default:
0bcec2
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("Unknown TPM error"));
0bcec2
+  }
0bcec2
+}
0bcec2
+
0bcec2
+grub_err_t
0bcec2
+grub_tpm_log_event(unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
0bcec2
+		   const char *description)
0bcec2
+{
0bcec2
+  grub_efi_handle_t tpm_handle;
0bcec2
+  grub_efi_uint8_t protocol_version;
0bcec2
+
0bcec2
+  if (!grub_tpm_handle_find(&tpm_handle, &protocol_version))
0bcec2
+    return 0;
0bcec2
+
0bcec2
+  if (protocol_version == 1) {
0bcec2
+    return grub_tpm1_log_event(tpm_handle, buf, size, pcr, description);
0bcec2
+  } else {
0bcec2
+    return grub_tpm2_log_event(tpm_handle, buf, size, pcr, description);
0bcec2
+  }
0bcec2
+}
0bcec2
diff --git a/grub-core/kern/i386/pc/tpm.c b/grub-core/kern/i386/pc/tpm.c
0bcec2
new file mode 100644
0bcec2
index 00000000000..8c6c1e6ece2
0bcec2
--- /dev/null
0bcec2
+++ b/grub-core/kern/i386/pc/tpm.c
0bcec2
@@ -0,0 +1,132 @@
0bcec2
+#include <grub err.h="">
0bcec2
+#include <grub i18n.h="">
0bcec2
+#include <grub mm.h="">
0bcec2
+#include <grub tpm.h="">
0bcec2
+#include <grub misc.h="">
0bcec2
+#include <grub i386="" pc="" int.h="">
0bcec2
+
0bcec2
+#define TCPA_MAGIC 0x41504354
0bcec2
+
0bcec2
+int tpm_present(void);
0bcec2
+
0bcec2
+int tpm_present(void)
0bcec2
+{
0bcec2
+  struct grub_bios_int_registers regs;
0bcec2
+
0bcec2
+  regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
0bcec2
+  regs.eax = 0xbb00;
0bcec2
+  regs.ebx = TCPA_MAGIC;
0bcec2
+  grub_bios_interrupt (0x1a, ®s);
0bcec2
+
0bcec2
+  if (regs.eax == 0)
0bcec2
+    return 1;
0bcec2
+
0bcec2
+  return 0;
0bcec2
+}
0bcec2
+
0bcec2
+grub_err_t
0bcec2
+grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
0bcec2
+		 PassThroughToTPM_OutputParamBlock *outbuf)
0bcec2
+{
0bcec2
+  struct grub_bios_int_registers regs;
0bcec2
+  grub_addr_t inaddr, outaddr;
0bcec2
+
0bcec2
+  if (!tpm_present())
0bcec2
+    return 0;
0bcec2
+
0bcec2
+  inaddr = (grub_addr_t) inbuf;
0bcec2
+  outaddr = (grub_addr_t) outbuf;
0bcec2
+  regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
0bcec2
+  regs.eax = 0xbb02;
0bcec2
+  regs.ebx = TCPA_MAGIC;
0bcec2
+  regs.ecx = 0;
0bcec2
+  regs.edx = 0;
0bcec2
+  regs.es = (inaddr & 0xffff0000) >> 4;
0bcec2
+  regs.edi = inaddr & 0xffff;
0bcec2
+  regs.ds = outaddr >> 4;
0bcec2
+  regs.esi = outaddr & 0xf;
0bcec2
+
0bcec2
+  grub_bios_interrupt (0x1a, ®s);
0bcec2
+
0bcec2
+  if (regs.eax)
0bcec2
+    return grub_error (GRUB_ERR_IO, N_("TPM error %x\n"), regs.eax);
0bcec2
+
0bcec2
+  return 0;
0bcec2
+}
0bcec2
+
0bcec2
+typedef struct {
0bcec2
+	grub_uint32_t pcrindex;
0bcec2
+	grub_uint32_t eventtype;
0bcec2
+	grub_uint8_t digest[20];
0bcec2
+	grub_uint32_t eventdatasize;
0bcec2
+	grub_uint8_t event[0];
0bcec2
+} GRUB_PACKED Event;
0bcec2
+
0bcec2
+typedef struct {
0bcec2
+	grub_uint16_t ipblength;
0bcec2
+	grub_uint16_t reserved;
0bcec2
+	grub_uint32_t hashdataptr;
0bcec2
+	grub_uint32_t hashdatalen;
0bcec2
+	grub_uint32_t pcr;
0bcec2
+	grub_uint32_t reserved2;
0bcec2
+	grub_uint32_t logdataptr;
0bcec2
+	grub_uint32_t logdatalen;
0bcec2
+} GRUB_PACKED EventIncoming;
0bcec2
+
0bcec2
+typedef struct {
0bcec2
+	grub_uint16_t opblength;
0bcec2
+	grub_uint16_t reserved;
0bcec2
+	grub_uint32_t eventnum;
0bcec2
+	grub_uint8_t  hashvalue[20];
0bcec2
+} GRUB_PACKED EventOutgoing;
0bcec2
+
0bcec2
+grub_err_t
0bcec2
+grub_tpm_log_event(unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
0bcec2
+		   const char *description)
0bcec2
+{
0bcec2
+	struct grub_bios_int_registers regs;
0bcec2
+	EventIncoming incoming;
0bcec2
+	EventOutgoing outgoing;
0bcec2
+	Event *event;
0bcec2
+	grub_uint32_t datalength;
0bcec2
+
0bcec2
+	if (!tpm_present())
0bcec2
+		return 0;
0bcec2
+
0bcec2
+	datalength = grub_strlen(description);
0bcec2
+	event = grub_zalloc(datalength + sizeof(Event));
0bcec2
+	if (!event)
0bcec2
+		return grub_error (GRUB_ERR_OUT_OF_MEMORY,
0bcec2
+				   N_("cannot allocate TPM event buffer"));
0bcec2
+
0bcec2
+	event->pcrindex = pcr;
0bcec2
+	event->eventtype = 0x0d;
0bcec2
+	event->eventdatasize = grub_strlen(description);
0bcec2
+	grub_memcpy(event->event, description, datalength);
0bcec2
+
0bcec2
+	incoming.ipblength = sizeof(incoming);
0bcec2
+	incoming.hashdataptr = (grub_uint32_t)buf;
0bcec2
+	incoming.hashdatalen = size;
0bcec2
+	incoming.pcr = pcr;
0bcec2
+	incoming.logdataptr = (grub_uint32_t)event;
0bcec2
+	incoming.logdatalen = datalength + sizeof(Event);
0bcec2
+
0bcec2
+	regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
0bcec2
+	regs.eax = 0xbb01;
0bcec2
+	regs.ebx = TCPA_MAGIC;
0bcec2
+	regs.ecx = 0;
0bcec2
+	regs.edx = 0;
0bcec2
+	regs.es = (((grub_addr_t) &incoming) & 0xffff0000) >> 4;
0bcec2
+	regs.edi = ((grub_addr_t) &incoming) & 0xffff;
0bcec2
+	regs.ds = (((grub_addr_t) &outgoing) & 0xffff0000) >> 4;
0bcec2
+	regs.esi = ((grub_addr_t) &outgoing) & 0xffff;
0bcec2
+
0bcec2
+	grub_bios_interrupt (0x1a, ®s);
0bcec2
+
0bcec2
+	grub_free(event);
0bcec2
+
0bcec2
+	if (regs.eax)
0bcec2
+		return grub_error (GRUB_ERR_IO, N_("TPM error %x\n"), regs.eax);
0bcec2
+
0bcec2
+	return 0;
0bcec2
+}
0bcec2
diff --git a/grub-core/kern/tpm.c b/grub-core/kern/tpm.c
0bcec2
new file mode 100644
0bcec2
index 00000000000..1a991876c83
0bcec2
--- /dev/null
0bcec2
+++ b/grub-core/kern/tpm.c
0bcec2
@@ -0,0 +1,13 @@
0bcec2
+#include <grub err.h="">
0bcec2
+#include <grub i18n.h="">
0bcec2
+#include <grub misc.h="">
0bcec2
+#include <grub mm.h="">
0bcec2
+#include <grub tpm.h="">
0bcec2
+#include <grub term.h="">
0bcec2
+
0bcec2
+grub_err_t
0bcec2
+grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
0bcec2
+		  const char *description)
0bcec2
+{
0bcec2
+  return grub_tpm_log_event(buf, size, pcr, description);
0bcec2
+}
0bcec2
diff --git a/include/grub/efi/tpm.h b/include/grub/efi/tpm.h
0bcec2
new file mode 100644
0bcec2
index 00000000000..e2aff4a3c22
0bcec2
--- /dev/null
0bcec2
+++ b/include/grub/efi/tpm.h
0bcec2
@@ -0,0 +1,153 @@
0bcec2
+/*
0bcec2
+ *  GRUB  --  GRand Unified Bootloader
0bcec2
+ *  Copyright (C) 2015  Free Software Foundation, Inc.
0bcec2
+ *
0bcec2
+ *  GRUB is free software: you can redistribute it and/or modify
0bcec2
+ *  it under the terms of the GNU General Public License as published by
0bcec2
+ *  the Free Software Foundation, either version 3 of the License, or
0bcec2
+ *  (at your option) any later version.
0bcec2
+ *
0bcec2
+ *  GRUB is distributed in the hope that it will be useful,
0bcec2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0bcec2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0bcec2
+ *  GNU General Public License for more details.
0bcec2
+ *
0bcec2
+ *  You should have received a copy of the GNU General Public License
0bcec2
+ *  along with GRUB.  If not, see <http: www.gnu.org="" licenses=""/>.
0bcec2
+ */
0bcec2
+
0bcec2
+#ifndef GRUB_EFI_TPM_HEADER
0bcec2
+#define GRUB_EFI_TPM_HEADER 1
0bcec2
+
0bcec2
+#define EFI_TPM_GUID {0xf541796d, 0xa62e, 0x4954, {0xa7, 0x75, 0x95, 0x84, 0xf6, 0x1b, 0x9c, 0xdd }};
0bcec2
+#define EFI_TPM2_GUID {0x607f766c, 0x7455, 0x42be, {0x93, 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f }};
0bcec2
+
0bcec2
+typedef struct {
0bcec2
+  grub_efi_uint8_t Major;
0bcec2
+  grub_efi_uint8_t Minor;
0bcec2
+  grub_efi_uint8_t RevMajor;
0bcec2
+  grub_efi_uint8_t RevMinor;
0bcec2
+} TCG_VERSION;
0bcec2
+
0bcec2
+typedef struct _TCG_EFI_BOOT_SERVICE_CAPABILITY {
0bcec2
+  grub_efi_uint8_t          Size;                /// Size of this structure.
0bcec2
+  TCG_VERSION    StructureVersion;
0bcec2
+  TCG_VERSION    ProtocolSpecVersion;
0bcec2
+  grub_efi_uint8_t          HashAlgorithmBitmap; /// Hash algorithms .
0bcec2
+  char        TPMPresentFlag;      /// 00h = TPM not present.
0bcec2
+  char        TPMDeactivatedFlag;  /// 01h = TPM currently deactivated.
0bcec2
+} TCG_EFI_BOOT_SERVICE_CAPABILITY;
0bcec2
+
0bcec2
+typedef struct {
0bcec2
+  grub_efi_uint32_t PCRIndex;
0bcec2
+  grub_efi_uint32_t EventType;
0bcec2
+  grub_efi_uint8_t digest[20];
0bcec2
+  grub_efi_uint32_t EventSize;
0bcec2
+  grub_efi_uint8_t  Event[1];
0bcec2
+} TCG_PCR_EVENT;
0bcec2
+
0bcec2
+struct grub_efi_tpm_protocol
0bcec2
+{
0bcec2
+  grub_efi_status_t (*status_check) (struct grub_efi_tpm_protocol *this,
0bcec2
+				     TCG_EFI_BOOT_SERVICE_CAPABILITY *ProtocolCapability,
0bcec2
+				     grub_efi_uint32_t *TCGFeatureFlags,
0bcec2
+				     grub_efi_physical_address_t *EventLogLocation,
0bcec2
+				     grub_efi_physical_address_t *EventLogLastEntry);
0bcec2
+  grub_efi_status_t (*hash_all) (struct grub_efi_tpm_protocol *this,
0bcec2
+				 grub_efi_uint8_t *HashData,
0bcec2
+				 grub_efi_uint64_t HashLen,
0bcec2
+				 grub_efi_uint32_t AlgorithmId,
0bcec2
+				 grub_efi_uint64_t *HashedDataLen,
0bcec2
+				 grub_efi_uint8_t **HashedDataResult);
0bcec2
+  grub_efi_status_t (*log_event) (struct grub_efi_tpm_protocol *this,
0bcec2
+				  TCG_PCR_EVENT *TCGLogData,
0bcec2
+				  grub_efi_uint32_t *EventNumber,
0bcec2
+				  grub_efi_uint32_t Flags);
0bcec2
+  grub_efi_status_t (*pass_through_to_tpm) (struct grub_efi_tpm_protocol *this,
0bcec2
+					    grub_efi_uint32_t TpmInputParameterBlockSize,
0bcec2
+					    grub_efi_uint8_t *TpmInputParameterBlock,
0bcec2
+					    grub_efi_uint32_t TpmOutputParameterBlockSize,
0bcec2
+					    grub_efi_uint8_t *TpmOutputParameterBlock);
0bcec2
+  grub_efi_status_t (*log_extend_event) (struct grub_efi_tpm_protocol *this,
0bcec2
+					 grub_efi_physical_address_t HashData,
0bcec2
+					 grub_efi_uint64_t HashDataLen,
0bcec2
+					 grub_efi_uint32_t AlgorithmId,
0bcec2
+					 TCG_PCR_EVENT *TCGLogData,
0bcec2
+					 grub_efi_uint32_t *EventNumber,
0bcec2
+					 grub_efi_physical_address_t *EventLogLastEntry);
0bcec2
+};
0bcec2
+
0bcec2
+typedef struct grub_efi_tpm_protocol grub_efi_tpm_protocol_t;
0bcec2
+
0bcec2
+typedef grub_efi_uint32_t EFI_TCG2_EVENT_LOG_BITMAP;
0bcec2
+typedef grub_efi_uint32_t EFI_TCG2_EVENT_LOG_FORMAT;
0bcec2
+typedef grub_efi_uint32_t EFI_TCG2_EVENT_ALGORITHM_BITMAP;
0bcec2
+
0bcec2
+typedef struct tdEFI_TCG2_VERSION {
0bcec2
+  grub_efi_uint8_t Major;
0bcec2
+  grub_efi_uint8_t Minor;
0bcec2
+} GRUB_PACKED EFI_TCG2_VERSION;
0bcec2
+
0bcec2
+typedef struct tdEFI_TCG2_BOOT_SERVICE_CAPABILITY {
0bcec2
+  grub_efi_uint8_t Size;
0bcec2
+  EFI_TCG2_VERSION StructureVersion;
0bcec2
+  EFI_TCG2_VERSION ProtocolVersion;
0bcec2
+  EFI_TCG2_EVENT_ALGORITHM_BITMAP HashAlgorithmBitmap;
0bcec2
+  EFI_TCG2_EVENT_LOG_BITMAP SupportedEventLogs;
0bcec2
+  grub_efi_boolean_t TPMPresentFlag;
0bcec2
+  grub_efi_uint16_t MaxCommandSize;
0bcec2
+  grub_efi_uint16_t MaxResponseSize;
0bcec2
+  grub_efi_uint32_t ManufacturerID;
0bcec2
+  grub_efi_uint32_t NumberOfPcrBanks;
0bcec2
+  EFI_TCG2_EVENT_ALGORITHM_BITMAP ActivePcrBanks;
0bcec2
+} EFI_TCG2_BOOT_SERVICE_CAPABILITY;
0bcec2
+
0bcec2
+typedef grub_efi_uint32_t TCG_PCRINDEX;
0bcec2
+typedef grub_efi_uint32_t TCG_EVENTTYPE;
0bcec2
+
0bcec2
+typedef struct tdEFI_TCG2_EVENT_HEADER {
0bcec2
+  grub_efi_uint32_t HeaderSize;
0bcec2
+  grub_efi_uint16_t HeaderVersion;
0bcec2
+  TCG_PCRINDEX PCRIndex;
0bcec2
+  TCG_EVENTTYPE EventType;
0bcec2
+} GRUB_PACKED EFI_TCG2_EVENT_HEADER;
0bcec2
+
0bcec2
+typedef struct tdEFI_TCG2_EVENT {
0bcec2
+  grub_efi_uint32_t Size;
0bcec2
+  EFI_TCG2_EVENT_HEADER Header;
0bcec2
+  grub_efi_uint8_t Event[1];
0bcec2
+} GRUB_PACKED EFI_TCG2_EVENT;
0bcec2
+
0bcec2
+struct grub_efi_tpm2_protocol
0bcec2
+{
0bcec2
+  grub_efi_status_t (*get_capability) (struct grub_efi_tpm2_protocol *this,
0bcec2
+				       EFI_TCG2_BOOT_SERVICE_CAPABILITY *ProtocolCapability);
0bcec2
+  grub_efi_status_t (*get_event_log) (struct grub_efi_tpm2_protocol *this,
0bcec2
+				      EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
0bcec2
+				      grub_efi_physical_address_t *EventLogLocation,
0bcec2
+				      grub_efi_physical_address_t *EventLogLastEntry,
0bcec2
+				      grub_efi_boolean_t *EventLogTruncated);
0bcec2
+  grub_efi_status_t (*hash_log_extend_event) (struct grub_efi_tpm2_protocol *this,
0bcec2
+					      grub_efi_uint64_t Flags,
0bcec2
+					      grub_efi_physical_address_t *DataToHash,
0bcec2
+					      grub_efi_uint64_t DataToHashLen,
0bcec2
+					      EFI_TCG2_EVENT *EfiTcgEvent);
0bcec2
+  grub_efi_status_t (*submit_command) (struct grub_efi_tpm2_protocol *this,
0bcec2
+				       grub_efi_uint32_t InputParameterBlockSize,
0bcec2
+				       grub_efi_uint8_t *InputParameterBlock,
0bcec2
+				       grub_efi_uint32_t OutputParameterBlockSize,
0bcec2
+				       grub_efi_uint8_t *OutputParameterBlock);
0bcec2
+  grub_efi_status_t (*get_active_pcr_blanks) (struct grub_efi_tpm2_protocol *this,
0bcec2
+					      grub_efi_uint32_t *ActivePcrBanks);
0bcec2
+  grub_efi_status_t (*set_active_pcr_banks) (struct grub_efi_tpm2_protocol *this,
0bcec2
+					     grub_efi_uint32_t ActivePcrBanks);
0bcec2
+  grub_efi_status_t (*get_result_of_set_active_pcr_banks) (struct grub_efi_tpm2_protocol *this,
0bcec2
+							   grub_efi_uint32_t *OperationPresent,
0bcec2
+							   grub_efi_uint32_t *Response);
0bcec2
+};
0bcec2
+
0bcec2
+typedef struct grub_efi_tpm2_protocol grub_efi_tpm2_protocol_t;
0bcec2
+
0bcec2
+#define TCG_ALG_SHA 0x00000004
0bcec2
+
0bcec2
+#endif
0bcec2
diff --git a/include/grub/tpm.h b/include/grub/tpm.h
0bcec2
new file mode 100644
0bcec2
index 00000000000..40d3cf65ba6
0bcec2
--- /dev/null
0bcec2
+++ b/include/grub/tpm.h
0bcec2
@@ -0,0 +1,91 @@
0bcec2
+/*
0bcec2
+ *  GRUB  --  GRand Unified Bootloader
0bcec2
+ *  Copyright (C) 2015  Free Software Foundation, Inc.
0bcec2
+ *
0bcec2
+ *  GRUB is free software: you can redistribute it and/or modify
0bcec2
+ *  it under the terms of the GNU General Public License as published by
0bcec2
+ *  the Free Software Foundation, either version 3 of the License, or
0bcec2
+ *  (at your option) any later version.
0bcec2
+ *
0bcec2
+ *  GRUB is distributed in the hope that it will be useful,
0bcec2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0bcec2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0bcec2
+ *  GNU General Public License for more details.
0bcec2
+ *
0bcec2
+ *  You should have received a copy of the GNU General Public License
0bcec2
+ *  along with GRUB.  If not, see <http: www.gnu.org="" licenses=""/>.
0bcec2
+ */
0bcec2
+
0bcec2
+#ifndef GRUB_TPM_HEADER
0bcec2
+#define GRUB_TPM_HEADER 1
0bcec2
+
0bcec2
+#define SHA1_DIGEST_SIZE 20
0bcec2
+
0bcec2
+#define TPM_BASE 0x0
0bcec2
+#define TPM_SUCCESS TPM_BASE
0bcec2
+#define TPM_AUTHFAIL (TPM_BASE + 0x1)
0bcec2
+#define TPM_BADINDEX (TPM_BASE + 0x2)
0bcec2
+
0bcec2
+#define GRUB_TPM_PCR 9
0bcec2
+#define GRUB_KERNEL_PCR 10
0bcec2
+#define GRUB_INITRD_PCR 11
0bcec2
+#define GRUB_CMDLINE_PCR 12
0bcec2
+
0bcec2
+#define TPM_TAG_RQU_COMMAND 0x00C1
0bcec2
+#define TPM_ORD_Extend 0x14
0bcec2
+
0bcec2
+#define EV_IPL 0x0d
0bcec2
+
0bcec2
+/* TCG_PassThroughToTPM Input Parameter Block */
0bcec2
+typedef struct {
0bcec2
+        grub_uint16_t IPBLength;
0bcec2
+        grub_uint16_t Reserved1;
0bcec2
+        grub_uint16_t OPBLength;
0bcec2
+        grub_uint16_t Reserved2;
0bcec2
+        grub_uint8_t TPMOperandIn[1];
0bcec2
+} GRUB_PACKED PassThroughToTPM_InputParamBlock;
0bcec2
+
0bcec2
+/* TCG_PassThroughToTPM Output Parameter Block */
0bcec2
+typedef struct {
0bcec2
+        grub_uint16_t OPBLength;
0bcec2
+        grub_uint16_t Reserved;
0bcec2
+        grub_uint8_t TPMOperandOut[1];
0bcec2
+} GRUB_PACKED PassThroughToTPM_OutputParamBlock;
0bcec2
+
0bcec2
+typedef struct {
0bcec2
+        grub_uint16_t tag;
0bcec2
+        grub_uint32_t paramSize;
0bcec2
+        grub_uint32_t ordinal;
0bcec2
+        grub_uint32_t pcrNum;
0bcec2
+        grub_uint8_t inDigest[SHA1_DIGEST_SIZE];                /* The 160 bit value representing the event to be recorded. */
0bcec2
+} GRUB_PACKED ExtendIncoming;
0bcec2
+
0bcec2
+/* TPM_Extend Outgoing Operand */
0bcec2
+typedef struct {
0bcec2
+        grub_uint16_t tag;
0bcec2
+        grub_uint32_t paramSize;
0bcec2
+        grub_uint32_t returnCode;
0bcec2
+        grub_uint8_t outDigest[SHA1_DIGEST_SIZE];               /* The PCR value after execution of the command. */
0bcec2
+} GRUB_PACKED ExtendOutgoing;
0bcec2
+
0bcec2
+grub_err_t EXPORT_FUNC(grub_tpm_measure) (unsigned char *buf, grub_size_t size,
0bcec2
+					  grub_uint8_t pcr,
0bcec2
+					  const char *description);
0bcec2
+#if defined (GRUB_MACHINE_EFI) || defined (GRUB_MACHINE_PCBIOS)
0bcec2
+grub_err_t grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
0bcec2
+			    PassThroughToTPM_OutputParamBlock *outbuf);
0bcec2
+grub_err_t grub_tpm_log_event(unsigned char *buf, grub_size_t size,
0bcec2
+			      grub_uint8_t pcr, const char *description);
0bcec2
+#else
0bcec2
+static inline grub_err_t grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
0bcec2
+					  PassThroughToTPM_OutputParamBlock *outbuf) { return 0; };
0bcec2
+static inline grub_err_t grub_tpm_log_event(unsigned char *buf,
0bcec2
+					    grub_size_t size,
0bcec2
+					    grub_uint8_t pcr,
0bcec2
+					    const char *description)
0bcec2
+{
0bcec2
+	return 0;
0bcec2
+};
0bcec2
+#endif
0bcec2
+
0bcec2
+#endif
0bcec2
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
15a207
index f36200bd688..3781bb9cbb9 100644
0bcec2
--- a/grub-core/Makefile.am
0bcec2
+++ b/grub-core/Makefile.am
0bcec2
@@ -94,6 +94,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
0bcec2
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
0bcec2
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h
0bcec2
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/net.h
0bcec2
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/tpm.h
0bcec2
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/memory.h
0bcec2
 
0bcec2
 if COND_i386_pc