15a2072
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a2072
From: Matthew Garrett <mjg59@coreos.com>
15a2072
Date: Sun, 9 Aug 2015 15:48:51 -0700
15a2072
Subject: [PATCH] Add BIOS boot measurement
15a2072
15a2072
Measure the on-disk grub core on BIOS systems - unlike UEFI, the firmware
15a2072
can't do this stage for us.
15a2072
---
15a2072
 grub-core/boot/i386/pc/boot.S     | 30 +++++++++++++++++++++++++-
15a2072
 grub-core/boot/i386/pc/diskboot.S | 44 +++++++++++++++++++++++++++++++++++++++
15a2072
 2 files changed, 73 insertions(+), 1 deletion(-)
15a2072
15a2072
diff --git a/grub-core/boot/i386/pc/boot.S b/grub-core/boot/i386/pc/boot.S
15a2072
index ea167fe1206..c1df86dec0b 100644
15a2072
--- a/grub-core/boot/i386/pc/boot.S
15a2072
+++ b/grub-core/boot/i386/pc/boot.S
15a2072
@@ -24,11 +24,14 @@
15a2072
  *  defines for the code go here
15a2072
  */
15a2072
 
15a2072
+#define TPM 1
15a2072
+
15a2072
 	/* Print message string */
15a2072
 #define MSG(x)	movw $x, %si; call LOCAL(message)
15a2072
 #define ERR(x)	movw $x, %si; jmp LOCAL(error_message)
15a2072
 
15a2072
 	.macro floppy
15a2072
+#ifndef TPM
15a2072
 part_start:
15a2072
 
15a2072
 LOCAL(probe_values):
15a2072
@@ -85,6 +88,7 @@ fd_probe_error_string:	.asciz "Floppy"
15a2072
 	movb	MACRO_DOLLAR(79), %ch
15a2072
 
15a2072
 	jmp	LOCAL(final_init)
15a2072
+#endif
15a2072
 	.endm
15a2072
 
15a2072
 	.macro scratch
15a2072
@@ -252,6 +256,7 @@ real_start:
15a2072
 	/* set %si to the disk address packet */
15a2072
 	movw	$disk_address_packet, %si
15a2072
 
15a2072
+#ifndef TPM
15a2072
 	/* check if LBA is supported */
15a2072
 	movb	$0x41, %ah
15a2072
 	movw	$0x55aa, %bx
15a2072
@@ -271,6 +276,7 @@ real_start:
15a2072
 
15a2072
 	andw	$1, %cx
15a2072
 	jz	LOCAL(chs_mode)
15a2072
+#endif
15a2072
 
15a2072
 LOCAL(lba_mode):
15a2072
 	xorw	%ax, %ax
15a2072
@@ -314,6 +320,9 @@ LOCAL(lba_mode):
15a2072
 	jmp	LOCAL(copy_buffer)
15a2072
 
15a2072
 LOCAL(chs_mode):
15a2072
+#ifdef TPM
15a2072
+	jmp	LOCAL(general_error)
15a2072
+#else
15a2072
 	/*
15a2072
 	 *  Determine the hard disk geometry from the BIOS!
15a2072
 	 *  We do this first, so that LS-120 IDE floppies work correctly.
15a2072
@@ -425,7 +434,7 @@ setup_sectors:
15a2072
 	jc	LOCAL(read_error)
15a2072
 
15a2072
 	movw	%es, %bx
15a2072
-
15a2072
+#endif /* TPM */
15a2072
 LOCAL(copy_buffer):
15a2072
 	/*
15a2072
 	 * We need to save %cx and %si because the startup code in
15a2072
@@ -448,6 +457,25 @@ LOCAL(copy_buffer):
15a2072
 	popw	%ds
15a2072
 	popa
15a2072
 
15a2072
+#ifdef TPM
15a2072
+	pusha
15a2072
+
15a2072
+	movw	$0xBB00, %ax		/* TCG_StatusCheck */
15a2072
+	int	$0x1A
15a2072
+	test	%eax, %eax
15a2072
+	jnz	boot			/* No TPM or TPM deactivated */
15a2072
+
15a2072
+	movw	$0xBB07, %ax		/* TCG_CompactHashLogExtendEvent */
15a2072
+	movw	$GRUB_BOOT_MACHINE_KERNEL_ADDR, %di
15a2072
+	xorl	%esi, %esi
15a2072
+	movl	$0x41504354, %ebx	/* TCPA */
15a2072
+	movl	$0x200, %ecx		/* Measure 512 bytes */
15a2072
+	movl	$0x8, %edx		/* PCR 8 */
15a2072
+	int	$0x1A
15a2072
+
15a2072
+	popa
15a2072
+#endif
15a2072
+boot:
15a2072
 	/* boot kernel */
15a2072
 	jmp	*(LOCAL(kernel_address))
15a2072
 
15a2072
diff --git a/grub-core/boot/i386/pc/diskboot.S b/grub-core/boot/i386/pc/diskboot.S
15a2072
index 68d31de0c4c..f4744ec6fcb 100644
15a2072
--- a/grub-core/boot/i386/pc/diskboot.S
15a2072
+++ b/grub-core/boot/i386/pc/diskboot.S
15a2072
@@ -19,6 +19,8 @@
15a2072
 #include <grub/symbol.h>
15a2072
 #include <grub/machine/boot.h>
15a2072
 
15a2072
+#define TPM 1
15a2072
+
15a2072
 /*
15a2072
  *  defines for the code go here
15a2072
  */
15a2072
@@ -53,6 +55,21 @@ _start:
15a2072
 	/* this sets up for the first run through "bootloop" */
15a2072
 	movw	$LOCAL(firstlist), %di
15a2072
 
15a2072
+#ifdef TPM
15a2072
+        /* clear EAX to remove potential garbage */
15a2072
+	xorl    %eax, %eax
15a2072
+	/* 8(%di) = number of sectors to read */
15a2072
+	movw    8(%di), %ax
15a2072
+
15a2072
+	/* Multiply number of sectors to read with 512 bytes. EAX is 32bit
15a2072
+	* which is large enough to hold values of up to 4GB. I doubt there
15a2072
+	* will ever be a core.img larger than that. ;-) */
15a2072
+	shll    $9, %eax
15a2072
+
15a2072
+	/* write result to bytes_to_measure var */
15a2072
+	movl    %eax, bytes_to_measure
15a2072
+#endif
15a2072
+
15a2072
 	/* save the sector number of the second sector in %ebp */
15a2072
 	movl	(%di), %ebp
15a2072
 
15a2072
@@ -290,6 +307,29 @@ LOCAL(copy_buffer):
15a2072
 /* END OF MAIN LOOP */
15a2072
 
15a2072
 LOCAL(bootit):
15a2072
+#ifdef TPM
15a2072
+	pusha
15a2072
+	movw	$0xBB07, %ax		/* TCG_CompactHashLogExtendEvent */
15a2072
+
15a2072
+	movw	$0x0, %bx
15a2072
+	movw	%bx, %es
15a2072
+
15a2072
+	/* We've already measured the first 512 bytes, now measure the rest */
15a2072
+	xorl	%edi, %edi
15a2072
+	movw	$(GRUB_BOOT_MACHINE_KERNEL_ADDR + 0x200), %di
15a2072
+
15a2072
+	movl	$0x41504354, %ebx	/* EBX = "TCPA" */
15a2072
+
15a2072
+	/* %ecx = The length, in bytes, of the buffer to measure  */
15a2072
+	movl	$bytes_to_measure, %esi
15a2072
+	movl	(%esi), %ecx
15a2072
+	xorl	%esi, %esi
15a2072
+	movl	$0x9, %edx		/* PCR 9 */
15a2072
+
15a2072
+	int	$0x1A
15a2072
+
15a2072
+	popa
15a2072
+#endif
15a2072
 	/* print a newline */
15a2072
 	MSG(notification_done)
15a2072
 	popw	%dx	/* this makes sure %dl is our "boot" drive */
15a2072
@@ -324,6 +364,10 @@ geometry_error_string:	.asciz "Geom"
15a2072
 read_error_string:	.asciz "Read"
15a2072
 general_error_string:	.asciz " Error"
15a2072
 
15a2072
+#ifdef TPM
15a2072
+bytes_to_measure:	.long 0
15a2072
+#endif
15a2072
+
15a2072
 /*
15a2072
  * message: write the string pointed to by %si
15a2072
  *