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