This patch adds support for measurements provided by Qemu via the
firmware interface. In the case where Qemu was started with the -kernel,
-initrd and -append command lines, Qemu hashes the kernel and initrd files
as well as the command line parameters and provides the measurements to
SeaBIOS via the firmware interface. SeaBIOS then processes the individual
measurements and extends the TPM's PCRs as well as writes logs about those
measurements.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>

---
 src/boot.c     |    1 
 src/paravirt.c |   14 ++++++++++
 src/paravirt.h |    4 +++
 src/tcgbios.c  |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/tcgbios.h  |   16 ++++++++++++
 5 files changed, 109 insertions(+)

Index: seabios/src/tcgbios.c
===================================================================
--- seabios.orig/src/tcgbios.c
+++ seabios/src/tcgbios.c
@@ -17,6 +17,7 @@
 #include "acpi.h"  // RSDP_SIGNATURE, rsdt_descriptor
 #include "sha1.h"  // sha1
 #include "smbios.h" // get_smbios_entry_point
+#include "paravirt.h" // QEMU_CFG_KERNEL_*, QEMU_CFG_INITRD_*
 
 
 static const u8 Startup_ST_CLEAR[2] = { 0x00, TPM_ST_CLEAR };
@@ -1392,6 +1393,79 @@ tcpa_smbios_measure(void)
 }
 
 
+u32 tcpa_process_firmware_cfg(void)
+{
+    if (!CONFIG_TCGBIOS)
+        return 0;
+
+    if (!has_working_tpm())
+        return TCG_GENERAL_ERROR;
+
+    void *ptr = (void *)0x100000;
+    u16 ctr = 0;
+
+    u32 len = qemu_cfg_get_u32(QEMU_CFG_TPM_MEASURE_SIZE);
+
+    if (len) {
+        qemu_cfg_copy(QEMU_CFG_TPM_MEASURE_DATA, ptr, len);
+
+        TPMMsrHdr *hdr = ptr;
+        u32 idx = sizeof(*hdr);
+
+        if (hdr->rev < 1)
+            return TCG_GENERAL_ERROR;
+
+        /* since the first revision (1) we have numTPMMsrEntries */
+        while (idx < hdr->totlen && ctr < hdr->numTPMMsrEntries) {
+            TPMMsrEntry *entry = (void *)hdr + idx;
+
+            idx += entry->len;
+
+            struct hleo hleo;
+
+            u8 _pcpes[offsetof(struct pcpes, event) + 400];
+            struct pcpes *pcpes = (struct pcpes *)_pcpes;
+
+            pcpes->pcrindex = entry->pcrindex;
+            pcpes->eventtype = entry->type;
+            if (entry->eventdatasize < 400) {
+                pcpes->eventdatasize = entry->eventdatasize;
+                memcpy(&pcpes->event, &entry->event, entry->eventdatasize);
+            } else {
+                pcpes->eventdatasize = 0;
+            }
+            memcpy(pcpes->digest, entry->digest, sizeof(pcpes->digest));
+
+            struct hlei hlei = {
+                .ipblength   = sizeof(hlei),
+                .hashdataptr = NULL,
+                .hashdatalen = 0,
+                .pcrindex    = entry->pcrindex,
+                .logeventtype= entry->type,
+                .logdataptr  = pcpes,
+                .logdatalen  = pcpes->eventdatasize +
+                               offsetof(struct pcpes, event),
+            };
+
+            u32 rc = hash_log_event(&hlei, &hleo);
+            if (rc)
+                goto err_exit;
+
+            rc = tpm_extend(entry->digest, entry->pcrindex);
+            if (rc)
+                goto err_exit;
+
+            ctr++;
+        }
+    }
+
+    return 0;
+
+err_exit:
+    return 1;
+}
+
+
 /*
  * Add a measurement to the log in support of 8.2.5.3
  * Creates two log entries
Index: seabios/src/boot.c
===================================================================
--- seabios.orig/src/boot.c
+++ seabios/src/boot.c
@@ -610,6 +610,7 @@ static void
 boot_rom(u32 vector)
 {
     printf("Booting from ROM...\n");
+    tcpa_process_firmware_cfg();
     struct segoff_s so;
     so.segoff = vector;
     call_boot_entry(so, 0);
Index: seabios/src/paravirt.c
===================================================================
--- seabios.orig/src/paravirt.c
+++ seabios/src/paravirt.c
@@ -61,6 +61,20 @@ void qemu_cfg_port_probe(void)
     dprintf(4, "qemu_cfg_present=%d\n", qemu_cfg_present);
 }
 
+u32 qemu_cfg_get_u32(int e)
+{
+    u32 i;
+
+    qemu_cfg_read_entry(&i, e, sizeof(i));
+
+    return i;
+}
+
+void qemu_cfg_copy(int e, void *buf, int len)
+{
+    qemu_cfg_read_entry(buf, e, len);
+}
+
 void qemu_cfg_get_uuid(u8 *uuid)
 {
     if (!qemu_cfg_present)
Index: seabios/src/paravirt.h
===================================================================
--- seabios.orig/src/paravirt.h
+++ seabios/src/paravirt.h
@@ -33,6 +33,8 @@ static inline int kvm_para_available(voi
 #define QEMU_CFG_BOOT_MENU		0x0e
 #define QEMU_CFG_MAX_CPUS		0x0f
 #define QEMU_CFG_FILE_DIR               0x19
+#define QEMU_CFG_TPM_MEASURE_SIZE       0x1a
+#define QEMU_CFG_TPM_MEASURE_DATA       0x1b
 #define QEMU_CFG_ARCH_LOCAL		0x8000
 #define QEMU_CFG_ACPI_TABLES		(QEMU_CFG_ARCH_LOCAL + 0)
 #define QEMU_CFG_SMBIOS_ENTRIES		(QEMU_CFG_ARCH_LOCAL + 1)
@@ -42,6 +44,8 @@ static inline int kvm_para_available(voi
 extern int qemu_cfg_present;
 
 void qemu_cfg_port_probe(void);
+u32 qemu_cfg_get_u32(int e);
+void qemu_cfg_copy(int e, void *buf, int len);
 int qemu_cfg_show_boot_menu(void);
 void qemu_cfg_get_uuid(u8 *uuid);
 int qemu_cfg_irq0_override(void);
Index: seabios/src/tcgbios.h
===================================================================
--- seabios.orig/src/tcgbios.h
+++ seabios/src/tcgbios.h
@@ -390,7 +390,23 @@ u32 tcpa_ipl(enum ipltype bootcd, const 
 u32 tcpa_start_option_rom_scan(void);
 u32 tcpa_option_rom(const void *addr, u32 len);
 u32 tcpa_smbios_measure(void);
+u32 tcpa_process_firmware_cfg(void);
 void tcpa_menu(void);
 
 
+typedef struct TPMMsrHdr {
+    u16 rev;
+    u32 totlen;
+    u16 numTPMMsrEntries;
+} __attribute__((packed)) TPMMsrHdr;
+
+typedef struct TPMMsrEntry {
+    u32 len;
+    u32 pcrindex;
+    u32 type;
+    u8 digest[SHA1_BUFSIZE];
+    u32 eventdatasize;
+    u32 event;
+} __attribute__((packed)) TPMMsrEntry;
+
 #endif /* TCGBIOS_H */


