nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0127-Core-TPM-support.patch

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