a3bfe3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
a3bfe3
From: Javier Martinez Canillas <javierm@redhat.com>
a3bfe3
Date: Fri, 21 Sep 2018 17:51:16 +0200
a3bfe3
Subject: [PATCH] drop TPM support for legacy BIOS
a3bfe3
a3bfe3
Currently there's TPM support for both EFI and legacy BIOS.
a3bfe3
a3bfe3
A software interrupt call interface is used in legacy BIOS to communicate
a3bfe3
with the TPM chips. But with some BIOS firmwares, the machine just hangs
a3bfe3
after doing a BIOS interrupt call for the TCG_HashLogExtendEvent command.
a3bfe3
a3bfe3
It's hard to know what exactly is causing this, but the Trousers project
a3bfe3
mentions in their docs that they don't use TCG_HashLogExtendEvent [0] due
a3bfe3
the command not working reliable on some BIOS.
a3bfe3
a3bfe3
The TCG_CompactHashLogExtendEvent is less fragile, since it has a simpler
a3bfe3
interface, doesn't require to setup any data structure and doesn't return
a3bfe3
anything. So it could be used to do measurements and logs events instead.
a3bfe3
a3bfe3
But even when using this command can be a workaround on some systems, it
a3bfe3
doesn't guarantee that could not fail on others. So since the TPM support
a3bfe3
for some legacy BIOS don't work and can lead to machines failing to boot,
a3bfe3
let's just drop it and only support TPM for EFI.
a3bfe3
a3bfe3
[0]: http://trousers.sourceforge.net/grub.html
a3bfe3
a3bfe3
Resolves: rhbz#1579835
a3bfe3
a3bfe3
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
a3bfe3
---
a3bfe3
 grub-core/Makefile.core.def       |   1 -
a3bfe3
 grub-core/kern/i386/pc/tpm.c      | 145 --------------------------------------
a3bfe3
 grub-core/loader/i386/pc/linux.c  |   4 --
a3bfe3
 include/grub/tpm.h                |   2 +-
a3bfe3
 grub-core/boot/i386/pc/boot.S     |  30 +-------
a3bfe3
 grub-core/boot/i386/pc/diskboot.S |  44 ------------
a3bfe3
 6 files changed, 2 insertions(+), 224 deletions(-)
a3bfe3
 delete mode 100644 grub-core/kern/i386/pc/tpm.c
a3bfe3
a3bfe3
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
a3bfe3
index 701e5d32fa2..f33ff332079 100644
a3bfe3
--- a/grub-core/Makefile.core.def
a3bfe3
+++ b/grub-core/Makefile.core.def
a3bfe3
@@ -246,7 +246,6 @@ kernel = {
a3bfe3
 
a3bfe3
   i386_pc = kern/i386/pc/init.c;
a3bfe3
   i386_pc = kern/i386/pc/mmap.c;
a3bfe3
-  i386_pc = kern/i386/pc/tpm.c;
a3bfe3
   i386_pc = term/i386/pc/console.c;
a3bfe3
 
a3bfe3
   i386_qemu = bus/pci.c;
a3bfe3
diff --git a/grub-core/kern/i386/pc/tpm.c b/grub-core/kern/i386/pc/tpm.c
a3bfe3
deleted file mode 100644
a3bfe3
index f6f264aff2e..00000000000
a3bfe3
--- a/grub-core/kern/i386/pc/tpm.c
a3bfe3
+++ /dev/null
a3bfe3
@@ -1,145 +0,0 @@
a3bfe3
-#include <grub err.h="">
a3bfe3
-#include <grub i18n.h="">
a3bfe3
-#include <grub mm.h="">
a3bfe3
-#include <grub tpm.h="">
a3bfe3
-#include <grub misc.h="">
a3bfe3
-#include <grub i386="" pc="" int.h="">
a3bfe3
-
a3bfe3
-#define TCPA_MAGIC 0x41504354
a3bfe3
-
a3bfe3
-static int tpm_presence = -1;
a3bfe3
-
a3bfe3
-int tpm_present(void);
a3bfe3
-
a3bfe3
-int tpm_present(void)
a3bfe3
-{
a3bfe3
-  struct grub_bios_int_registers regs;
a3bfe3
-
a3bfe3
-  if (tpm_presence != -1)
a3bfe3
-    return tpm_presence;
a3bfe3
-
a3bfe3
-  regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
a3bfe3
-  regs.eax = 0xbb00;
a3bfe3
-  regs.ebx = TCPA_MAGIC;
a3bfe3
-  grub_bios_interrupt (0x1a, ®s);
a3bfe3
-
a3bfe3
-  if (regs.eax == 0)
a3bfe3
-    tpm_presence = 1;
a3bfe3
-  else
a3bfe3
-    tpm_presence = 0;
a3bfe3
-
a3bfe3
-  return tpm_presence;
a3bfe3
-}
a3bfe3
-
a3bfe3
-grub_err_t
a3bfe3
-grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
a3bfe3
-		 PassThroughToTPM_OutputParamBlock *outbuf)
a3bfe3
-{
a3bfe3
-  struct grub_bios_int_registers regs;
a3bfe3
-  grub_addr_t inaddr, outaddr;
a3bfe3
-
a3bfe3
-  if (!tpm_present())
a3bfe3
-    return 0;
a3bfe3
-
a3bfe3
-  inaddr = (grub_addr_t) inbuf;
a3bfe3
-  outaddr = (grub_addr_t) outbuf;
a3bfe3
-  regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
a3bfe3
-  regs.eax = 0xbb02;
a3bfe3
-  regs.ebx = TCPA_MAGIC;
a3bfe3
-  regs.ecx = 0;
a3bfe3
-  regs.edx = 0;
a3bfe3
-  regs.es = (inaddr & 0xffff0000) >> 4;
a3bfe3
-  regs.edi = inaddr & 0xffff;
a3bfe3
-  regs.ds = outaddr >> 4;
a3bfe3
-  regs.esi = outaddr & 0xf;
a3bfe3
-
a3bfe3
-  grub_bios_interrupt (0x1a, ®s);
a3bfe3
-
a3bfe3
-  if (regs.eax)
a3bfe3
-    {
a3bfe3
-	tpm_presence = 0;
a3bfe3
-	return grub_error (GRUB_ERR_IO, N_("TPM error %x, disabling TPM"), regs.eax);
a3bfe3
-    }
a3bfe3
-
a3bfe3
-  return 0;
a3bfe3
-}
a3bfe3
-
a3bfe3
-typedef struct {
a3bfe3
-	grub_uint32_t pcrindex;
a3bfe3
-	grub_uint32_t eventtype;
a3bfe3
-	grub_uint8_t digest[20];
a3bfe3
-	grub_uint32_t eventdatasize;
a3bfe3
-	grub_uint8_t event[0];
a3bfe3
-} GRUB_PACKED Event;
a3bfe3
-
a3bfe3
-typedef struct {
a3bfe3
-	grub_uint16_t ipblength;
a3bfe3
-	grub_uint16_t reserved;
a3bfe3
-	grub_uint32_t hashdataptr;
a3bfe3
-	grub_uint32_t hashdatalen;
a3bfe3
-	grub_uint32_t pcr;
a3bfe3
-	grub_uint32_t reserved2;
a3bfe3
-	grub_uint32_t logdataptr;
a3bfe3
-	grub_uint32_t logdatalen;
a3bfe3
-} GRUB_PACKED EventIncoming;
a3bfe3
-
a3bfe3
-typedef struct {
a3bfe3
-	grub_uint16_t opblength;
a3bfe3
-	grub_uint16_t reserved;
a3bfe3
-	grub_uint32_t eventnum;
a3bfe3
-	grub_uint8_t  hashvalue[20];
a3bfe3
-} GRUB_PACKED EventOutgoing;
a3bfe3
-
a3bfe3
-grub_err_t
a3bfe3
-grub_tpm_log_event(unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
a3bfe3
-		   const char *description)
a3bfe3
-{
a3bfe3
-	struct grub_bios_int_registers regs;
a3bfe3
-	EventIncoming incoming;
a3bfe3
-	EventOutgoing outgoing;
a3bfe3
-	Event *event;
a3bfe3
-	grub_uint32_t datalength;
a3bfe3
-
a3bfe3
-	if (!tpm_present())
a3bfe3
-		return 0;
a3bfe3
-
a3bfe3
-	datalength = grub_strlen(description);
a3bfe3
-	event = grub_zalloc(datalength + sizeof(Event));
a3bfe3
-	if (!event)
a3bfe3
-		return grub_error (GRUB_ERR_OUT_OF_MEMORY,
a3bfe3
-				   N_("cannot allocate TPM event buffer"));
a3bfe3
-
a3bfe3
-	event->pcrindex = pcr;
a3bfe3
-	event->eventtype = 0x0d;
a3bfe3
-	event->eventdatasize = grub_strlen(description);
a3bfe3
-	grub_memcpy(event->event, description, datalength);
a3bfe3
-
a3bfe3
-	incoming.ipblength = sizeof(incoming);
a3bfe3
-	incoming.hashdataptr = (grub_uint32_t)buf;
a3bfe3
-	incoming.hashdatalen = size;
a3bfe3
-	incoming.pcr = pcr;
a3bfe3
-	incoming.logdataptr = (grub_uint32_t)event;
a3bfe3
-	incoming.logdatalen = datalength + sizeof(Event);
a3bfe3
-
a3bfe3
-	regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
a3bfe3
-	regs.eax = 0xbb01;
a3bfe3
-	regs.ebx = TCPA_MAGIC;
a3bfe3
-	regs.ecx = 0;
a3bfe3
-	regs.edx = 0;
a3bfe3
-	regs.es = (((grub_addr_t) &incoming) & 0xffff0000) >> 4;
a3bfe3
-	regs.edi = ((grub_addr_t) &incoming) & 0xffff;
a3bfe3
-	regs.ds = (((grub_addr_t) &outgoing) & 0xffff0000) >> 4;
a3bfe3
-	regs.esi = ((grub_addr_t) &outgoing) & 0xffff;
a3bfe3
-
a3bfe3
-	grub_bios_interrupt (0x1a, ®s);
a3bfe3
-
a3bfe3
-	grub_free(event);
a3bfe3
-
a3bfe3
-	if (regs.eax)
a3bfe3
-	  {
a3bfe3
-		tpm_presence = 0;
a3bfe3
-		return grub_error (GRUB_ERR_IO, N_("TPM error %x, disabling TPM"), regs.eax);
a3bfe3
-	  }
a3bfe3
-
a3bfe3
-	return 0;
a3bfe3
-}
a3bfe3
diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c
a3bfe3
index cfff25c21b5..783a3cd93bc 100644
a3bfe3
--- a/grub-core/loader/i386/pc/linux.c
a3bfe3
+++ b/grub-core/loader/i386/pc/linux.c
a3bfe3
@@ -36,7 +36,6 @@
a3bfe3
 #include <grub lib="" cmdline.h="">
a3bfe3
 #include <grub linux.h="">
a3bfe3
 #include <grub efi="" sb.h="">
a3bfe3
-#include <grub tpm.h="">
a3bfe3
 
a3bfe3
 GRUB_MOD_LICENSE ("GPLv3+");
a3bfe3
 
a3bfe3
@@ -162,9 +161,6 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
a3bfe3
       goto fail;
a3bfe3
     }
a3bfe3
 
a3bfe3
-  grub_tpm_measure (kernel, len, GRUB_BINARY_PCR, "grub_linux16", "Kernel");
a3bfe3
-  grub_print_error();
a3bfe3
-
a3bfe3
   grub_memcpy (&lh, kernel, sizeof (lh));
a3bfe3
   kernel_offset = sizeof (lh);
a3bfe3
 
a3bfe3
diff --git a/include/grub/tpm.h b/include/grub/tpm.h
a3bfe3
index 972a5edc836..ce52be4ff7f 100644
a3bfe3
--- a/include/grub/tpm.h
a3bfe3
+++ b/include/grub/tpm.h
a3bfe3
@@ -69,7 +69,7 @@ typedef struct {
a3bfe3
 grub_err_t EXPORT_FUNC(grub_tpm_measure) (unsigned char *buf, grub_size_t size,
a3bfe3
 					  grub_uint8_t pcr, const char *kind,
a3bfe3
 					  const char *description);
a3bfe3
-#if defined (GRUB_MACHINE_EFI) || defined (GRUB_MACHINE_PCBIOS)
a3bfe3
+#if defined (GRUB_MACHINE_EFI)
a3bfe3
 grub_err_t grub_tpm_execute(PassThroughToTPM_InputParamBlock *inbuf,
a3bfe3
 			    PassThroughToTPM_OutputParamBlock *outbuf);
a3bfe3
 grub_err_t grub_tpm_log_event(unsigned char *buf, grub_size_t size,
a3bfe3
diff --git a/grub-core/boot/i386/pc/boot.S b/grub-core/boot/i386/pc/boot.S
a3bfe3
index acab37369ae..ea167fe1206 100644
a3bfe3
--- a/grub-core/boot/i386/pc/boot.S
a3bfe3
+++ b/grub-core/boot/i386/pc/boot.S
a3bfe3
@@ -24,14 +24,11 @@
a3bfe3
  *  defines for the code go here
a3bfe3
  */
a3bfe3
 
a3bfe3
-#define TPM 1
a3bfe3
-
a3bfe3
 	/* Print message string */
a3bfe3
 #define MSG(x)	movw $x, %si; call LOCAL(message)
a3bfe3
 #define ERR(x)	movw $x, %si; jmp LOCAL(error_message)
a3bfe3
 
a3bfe3
 	.macro floppy
a3bfe3
-#ifndef TPM
a3bfe3
 part_start:
a3bfe3
 
a3bfe3
 LOCAL(probe_values):
a3bfe3
@@ -88,7 +85,6 @@ fd_probe_error_string:	.asciz "Floppy"
a3bfe3
 	movb	MACRO_DOLLAR(79), %ch
a3bfe3
 
a3bfe3
 	jmp	LOCAL(final_init)
a3bfe3
-#endif
a3bfe3
 	.endm
a3bfe3
 
a3bfe3
 	.macro scratch
a3bfe3
@@ -256,7 +252,6 @@ real_start:
a3bfe3
 	/* set %si to the disk address packet */
a3bfe3
 	movw	$disk_address_packet, %si
a3bfe3
 
a3bfe3
-#ifndef TPM
a3bfe3
 	/* check if LBA is supported */
a3bfe3
 	movb	$0x41, %ah
a3bfe3
 	movw	$0x55aa, %bx
a3bfe3
@@ -276,7 +271,6 @@ real_start:
a3bfe3
 
a3bfe3
 	andw	$1, %cx
a3bfe3
 	jz	LOCAL(chs_mode)
a3bfe3
-#endif
a3bfe3
 
a3bfe3
 LOCAL(lba_mode):
a3bfe3
 	xorw	%ax, %ax
a3bfe3
@@ -320,9 +314,6 @@ LOCAL(lba_mode):
a3bfe3
 	jmp	LOCAL(copy_buffer)
a3bfe3
 
a3bfe3
 LOCAL(chs_mode):
a3bfe3
-#ifdef TPM
a3bfe3
-	jmp	LOCAL(general_error)
a3bfe3
-#else
a3bfe3
 	/*
a3bfe3
 	 *  Determine the hard disk geometry from the BIOS!
a3bfe3
 	 *  We do this first, so that LS-120 IDE floppies work correctly.
a3bfe3
@@ -434,7 +425,7 @@ setup_sectors:
a3bfe3
 	jc	LOCAL(read_error)
a3bfe3
 
a3bfe3
 	movw	%es, %bx
a3bfe3
-#endif /* TPM */
a3bfe3
+
a3bfe3
 LOCAL(copy_buffer):
a3bfe3
 	/*
a3bfe3
 	 * We need to save %cx and %si because the startup code in
a3bfe3
@@ -457,25 +448,6 @@ LOCAL(copy_buffer):
a3bfe3
 	popw	%ds
a3bfe3
 	popa
a3bfe3
 
a3bfe3
-#ifdef TPM
a3bfe3
-	pusha
a3bfe3
-
a3bfe3
-	movw	$0xBB00, %ax		/* TCG_StatusCheck */
a3bfe3
-	int	$0x1A
a3bfe3
-	test	%eax, %eax
a3bfe3
-	jnz	boot			/* No TPM or TPM deactivated */
a3bfe3
-
a3bfe3
-	movw	$0xBB07, %ax		/* TCG_CompactHashLogExtendEvent */
a3bfe3
-	movw	$GRUB_BOOT_MACHINE_KERNEL_ADDR, %di
a3bfe3
-	xorl	%esi, %esi
a3bfe3
-	movl	$0x41504354, %ebx	/* TCPA */
a3bfe3
-	movl	$0x200, %ecx		/* Measure 512 bytes */
a3bfe3
-	movl	$0x8, %edx		/* PCR 8 */
a3bfe3
-	int	$0x1A
a3bfe3
-
a3bfe3
-boot:
a3bfe3
-	popa
a3bfe3
-#endif
a3bfe3
 	/* boot kernel */
a3bfe3
 	jmp	*(LOCAL(kernel_address))
a3bfe3
 
a3bfe3
diff --git a/grub-core/boot/i386/pc/diskboot.S b/grub-core/boot/i386/pc/diskboot.S
a3bfe3
index f4744ec6fcb..68d31de0c4c 100644
a3bfe3
--- a/grub-core/boot/i386/pc/diskboot.S
a3bfe3
+++ b/grub-core/boot/i386/pc/diskboot.S
a3bfe3
@@ -19,8 +19,6 @@
a3bfe3
 #include <grub symbol.h="">
a3bfe3
 #include <grub machine="" boot.h="">
a3bfe3
 
a3bfe3
-#define TPM 1
a3bfe3
-
a3bfe3
 /*
a3bfe3
  *  defines for the code go here
a3bfe3
  */
a3bfe3
@@ -55,21 +53,6 @@ _start:
a3bfe3
 	/* this sets up for the first run through "bootloop" */
a3bfe3
 	movw	$LOCAL(firstlist), %di
a3bfe3
 
a3bfe3
-#ifdef TPM
a3bfe3
-        /* clear EAX to remove potential garbage */
a3bfe3
-	xorl    %eax, %eax
a3bfe3
-	/* 8(%di) = number of sectors to read */
a3bfe3
-	movw    8(%di), %ax
a3bfe3
-
a3bfe3
-	/* Multiply number of sectors to read with 512 bytes. EAX is 32bit
a3bfe3
-	* which is large enough to hold values of up to 4GB. I doubt there
a3bfe3
-	* will ever be a core.img larger than that. ;-) */
a3bfe3
-	shll    $9, %eax
a3bfe3
-
a3bfe3
-	/* write result to bytes_to_measure var */
a3bfe3
-	movl    %eax, bytes_to_measure
a3bfe3
-#endif
a3bfe3
-
a3bfe3
 	/* save the sector number of the second sector in %ebp */
a3bfe3
 	movl	(%di), %ebp
a3bfe3
 
a3bfe3
@@ -307,29 +290,6 @@ LOCAL(copy_buffer):
a3bfe3
 /* END OF MAIN LOOP */
a3bfe3
 
a3bfe3
 LOCAL(bootit):
a3bfe3
-#ifdef TPM
a3bfe3
-	pusha
a3bfe3
-	movw	$0xBB07, %ax		/* TCG_CompactHashLogExtendEvent */
a3bfe3
-
a3bfe3
-	movw	$0x0, %bx
a3bfe3
-	movw	%bx, %es
a3bfe3
-
a3bfe3
-	/* We've already measured the first 512 bytes, now measure the rest */
a3bfe3
-	xorl	%edi, %edi
a3bfe3
-	movw	$(GRUB_BOOT_MACHINE_KERNEL_ADDR + 0x200), %di
a3bfe3
-
a3bfe3
-	movl	$0x41504354, %ebx	/* EBX = "TCPA" */
a3bfe3
-
a3bfe3
-	/* %ecx = The length, in bytes, of the buffer to measure  */
a3bfe3
-	movl	$bytes_to_measure, %esi
a3bfe3
-	movl	(%esi), %ecx
a3bfe3
-	xorl	%esi, %esi
a3bfe3
-	movl	$0x9, %edx		/* PCR 9 */
a3bfe3
-
a3bfe3
-	int	$0x1A
a3bfe3
-
a3bfe3
-	popa
a3bfe3
-#endif
a3bfe3
 	/* print a newline */
a3bfe3
 	MSG(notification_done)
a3bfe3
 	popw	%dx	/* this makes sure %dl is our "boot" drive */
a3bfe3
@@ -364,10 +324,6 @@ geometry_error_string:	.asciz "Geom"
a3bfe3
 read_error_string:	.asciz "Read"
a3bfe3
 general_error_string:	.asciz " Error"
a3bfe3
 
a3bfe3
-#ifdef TPM
a3bfe3
-bytes_to_measure:	.long 0
a3bfe3
-#endif
a3bfe3
-
a3bfe3
 /*
a3bfe3
  * message: write the string pointed to by %si
a3bfe3
  *