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
31cddd6
Subject: [PATCH] Core TPM support
0bcec26
0bcec26
Add support for performing basic TPM measurements. Right now this only
0bcec26
supports extending PCRs statically and only on UEFI and BIOS systems, but
0bcec26
will measure all modules as they're loaded.
0bcec26
---
0bcec26
 grub-core/Makefile.core.def  |   3 +
0bcec26
 grub-core/kern/dl.c          |   3 +
0bcec26
 grub-core/kern/efi/tpm.c     | 282 +++++++++++++++++++++++++++++++++++++++++++
0bcec26
 grub-core/kern/i386/pc/tpm.c | 132 ++++++++++++++++++++
0bcec26
 grub-core/kern/tpm.c         |  13 ++
0bcec26
 include/grub/efi/tpm.h       | 153 +++++++++++++++++++++++
0bcec26
 include/grub/tpm.h           |  91 ++++++++++++++
0bcec26
 grub-core/Makefile.am        |   1 +
0bcec26
 8 files changed, 678 insertions(+)
0bcec26
 create mode 100644 grub-core/kern/efi/tpm.c
0bcec26
 create mode 100644 grub-core/kern/i386/pc/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
15a2072
index cb24f92a431..420831bc89e 100644
0bcec26
--- a/grub-core/Makefile.core.def
0bcec26
+++ b/grub-core/Makefile.core.def
0bcec26
@@ -131,6 +131,7 @@ kernel = {
0bcec26
   common = kern/term.c;
0bcec26
   common = kern/qsort.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;
0bcec26
@@ -199,6 +200,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;
15a2072
@@ -245,6 +247,7 @@ kernel = {
0bcec26
 
0bcec26
   i386_pc = kern/i386/pc/init.c;
0bcec26
   i386_pc = kern/i386/pc/mmap.c;
0bcec26
+  i386_pc = kern/i386/pc/tpm.c;
0bcec26
   i386_pc = term/i386/pc/console.c;
0bcec26
 
0bcec26
   i386_qemu = bus/pci.c;
0bcec26
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
0bcec26
index eb8b969cded..387d1e6446d 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)
0bcec26
@@ -829,6 +830,8 @@ grub_dl_load_file (const char *filename)
0bcec26
      opens of the same device.  */
0bcec26
   grub_file_close (file);
0bcec26
 
0bcec26
+  grub_tpm_measure(core, size, GRUB_TPM_PCR, filename);
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
0bcec26
index 00000000000..c9fb3c133f3
0bcec26
--- /dev/null
0bcec26
+++ b/grub-core/kern/efi/tpm.c
0bcec26
@@ -0,0 +1,282 @@
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
+typedef struct {
0bcec26
+	grub_uint32_t pcrindex;
0bcec26
+	grub_uint32_t eventtype;
0bcec26
+	grub_uint8_t digest[20];
0bcec26
+	grub_uint32_t eventsize;
0bcec26
+	grub_uint8_t event[1];
0bcec26
+} Event;
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
+{
0bcec26
+  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
+
0bcec26
+  event = grub_zalloc(sizeof (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->pcrindex = pcr;
0bcec26
+  event->eventtype = EV_IPL;
0bcec26
+  event->eventsize = grub_strlen(description) + 1;
0bcec26
+  grub_memcpy(event->event, description, event->eventsize);
0bcec26
+
0bcec26
+  algorithm = TCG_ALG_SHA;
0bcec26
+  status = efi_call_7 (tpm->log_extend_event, tpm, 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
+
0bcec26
+  status = efi_call_5 (tpm->hash_log_extend_event, tpm, 0, 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/i386/pc/tpm.c b/grub-core/kern/i386/pc/tpm.c
0bcec26
new file mode 100644
0bcec26
index 00000000000..8c6c1e6ece2
0bcec26
--- /dev/null
0bcec26
+++ b/grub-core/kern/i386/pc/tpm.c
0bcec26
@@ -0,0 +1,132 @@
0bcec26
+#include <grub/err.h>
0bcec26
+#include <grub/i18n.h>
0bcec26
+#include <grub/mm.h>
0bcec26
+#include <grub/tpm.h>
0bcec26
+#include <grub/misc.h>
0bcec26
+#include <grub/i386/pc/int.h>
0bcec26
+
0bcec26
+#define TCPA_MAGIC 0x41504354
0bcec26
+
0bcec26
+int tpm_present(void);
0bcec26
+
0bcec26
+int tpm_present(void)
0bcec26
+{
0bcec26
+  struct grub_bios_int_registers regs;
0bcec26
+
0bcec26
+  regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
0bcec26
+  regs.eax = 0xbb00;
0bcec26
+  regs.ebx = TCPA_MAGIC;
0bcec26
+  grub_bios_interrupt (0x1a, ®s;;
0bcec26
+
0bcec26
+  if (regs.eax == 0)
0bcec26
+    return 1;
0bcec26
+
0bcec26
+  return 0;
0bcec26
+}
0bcec26
+
0bcec26
+grub_err_t
0bcec26
+grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
0bcec26
+		 PassThroughToTPM_OutputParamBlock *outbuf)
0bcec26
+{
0bcec26
+  struct grub_bios_int_registers regs;
0bcec26
+  grub_addr_t inaddr, outaddr;
0bcec26
+
0bcec26
+  if (!tpm_present())
0bcec26
+    return 0;
0bcec26
+
0bcec26
+  inaddr = (grub_addr_t) inbuf;
0bcec26
+  outaddr = (grub_addr_t) outbuf;
0bcec26
+  regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
0bcec26
+  regs.eax = 0xbb02;
0bcec26
+  regs.ebx = TCPA_MAGIC;
0bcec26
+  regs.ecx = 0;
0bcec26
+  regs.edx = 0;
0bcec26
+  regs.es = (inaddr & 0xffff0000) >> 4;
0bcec26
+  regs.edi = inaddr & 0xffff;
0bcec26
+  regs.ds = outaddr >> 4;
0bcec26
+  regs.esi = outaddr & 0xf;
0bcec26
+
0bcec26
+  grub_bios_interrupt (0x1a, ®s;;
0bcec26
+
0bcec26
+  if (regs.eax)
0bcec26
+    return grub_error (GRUB_ERR_IO, N_("TPM error %x\n"), regs.eax);
0bcec26
+
0bcec26
+  return 0;
0bcec26
+}
0bcec26
+
0bcec26
+typedef struct {
0bcec26
+	grub_uint32_t pcrindex;
0bcec26
+	grub_uint32_t eventtype;
0bcec26
+	grub_uint8_t digest[20];
0bcec26
+	grub_uint32_t eventdatasize;
0bcec26
+	grub_uint8_t event[0];
0bcec26
+} GRUB_PACKED Event;
0bcec26
+
0bcec26
+typedef struct {
0bcec26
+	grub_uint16_t ipblength;
0bcec26
+	grub_uint16_t reserved;
0bcec26
+	grub_uint32_t hashdataptr;
0bcec26
+	grub_uint32_t hashdatalen;
0bcec26
+	grub_uint32_t pcr;
0bcec26
+	grub_uint32_t reserved2;
0bcec26
+	grub_uint32_t logdataptr;
0bcec26
+	grub_uint32_t logdatalen;
0bcec26
+} GRUB_PACKED EventIncoming;
0bcec26
+
0bcec26
+typedef struct {
0bcec26
+	grub_uint16_t opblength;
0bcec26
+	grub_uint16_t reserved;
0bcec26
+	grub_uint32_t eventnum;
0bcec26
+	grub_uint8_t  hashvalue[20];
0bcec26
+} GRUB_PACKED EventOutgoing;
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
+	struct grub_bios_int_registers regs;
0bcec26
+	EventIncoming incoming;
0bcec26
+	EventOutgoing outgoing;
0bcec26
+	Event *event;
0bcec26
+	grub_uint32_t datalength;
0bcec26
+
0bcec26
+	if (!tpm_present())
0bcec26
+		return 0;
0bcec26
+
0bcec26
+	datalength = grub_strlen(description);
0bcec26
+	event = grub_zalloc(datalength + sizeof(Event));
0bcec26
+	if (!event)
0bcec26
+		return grub_error (GRUB_ERR_OUT_OF_MEMORY,
0bcec26
+				   N_("cannot allocate TPM event buffer"));
0bcec26
+
0bcec26
+	event->pcrindex = pcr;
0bcec26
+	event->eventtype = 0x0d;
0bcec26
+	event->eventdatasize = grub_strlen(description);
0bcec26
+	grub_memcpy(event->event, description, datalength);
0bcec26
+
0bcec26
+	incoming.ipblength = sizeof(incoming);
0bcec26
+	incoming.hashdataptr = (grub_uint32_t)buf;
0bcec26
+	incoming.hashdatalen = size;
0bcec26
+	incoming.pcr = pcr;
0bcec26
+	incoming.logdataptr = (grub_uint32_t)event;
0bcec26
+	incoming.logdatalen = datalength + sizeof(Event);
0bcec26
+
0bcec26
+	regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
0bcec26
+	regs.eax = 0xbb01;
0bcec26
+	regs.ebx = TCPA_MAGIC;
0bcec26
+	regs.ecx = 0;
0bcec26
+	regs.edx = 0;
0bcec26
+	regs.es = (((grub_addr_t) &incoming) & 0xffff0000) >> 4;
0bcec26
+	regs.edi = ((grub_addr_t) &incoming) & 0xffff;
0bcec26
+	regs.ds = (((grub_addr_t) &outgoing) & 0xffff0000) >> 4;
0bcec26
+	regs.esi = ((grub_addr_t) &outgoing) & 0xffff;
0bcec26
+
0bcec26
+	grub_bios_interrupt (0x1a, ®s;;
0bcec26
+
0bcec26
+	grub_free(event);
0bcec26
+
0bcec26
+	if (regs.eax)
0bcec26
+		return grub_error (GRUB_ERR_IO, N_("TPM error %x\n"), regs.eax);
0bcec26
+
0bcec26
+	return 0;
0bcec26
+}
0bcec26
diff --git a/grub-core/kern/tpm.c b/grub-core/kern/tpm.c
0bcec26
new file mode 100644
0bcec26
index 00000000000..1a991876c83
0bcec26
--- /dev/null
0bcec26
+++ b/grub-core/kern/tpm.c
0bcec26
@@ -0,0 +1,13 @@
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,
0bcec26
+		  const char *description)
0bcec26
+{
0bcec26
+  return grub_tpm_log_event(buf, size, pcr, description);
0bcec26
+}
0bcec26
diff --git a/include/grub/efi/tpm.h b/include/grub/efi/tpm.h
0bcec26
new file mode 100644
0bcec26
index 00000000000..e2aff4a3c22
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,
0bcec26
+					      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
0bcec26
index 00000000000..40d3cf65ba6
0bcec26
--- /dev/null
0bcec26
+++ b/include/grub/tpm.h
0bcec26
@@ -0,0 +1,91 @@
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
+
0bcec26
+#define GRUB_TPM_PCR 9
0bcec26
+#define GRUB_KERNEL_PCR 10
0bcec26
+#define GRUB_INITRD_PCR 11
0bcec26
+#define GRUB_CMDLINE_PCR 12
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,
0bcec26
+					  grub_uint8_t pcr,
0bcec26
+					  const char *description);
0bcec26
+#if defined (GRUB_MACHINE_EFI) || defined (GRUB_MACHINE_PCBIOS)
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
0bcec26
+static inline grub_err_t grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
0bcec26
+					  PassThroughToTPM_OutputParamBlock *outbuf) { return 0; };
0bcec26
+static inline grub_err_t grub_tpm_log_event(unsigned char *buf,
0bcec26
+					    grub_size_t size,
0bcec26
+					    grub_uint8_t pcr,
0bcec26
+					    const char *description)
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