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