Add TPM support to SeaBIOS
v2 cover Add TPM support to SeaBIOS
The following set of patches add TPM and Trusted Computing support to SeaBIOS. In particular the patches add:
- a TPM driver for the Qemu's TPM TIS emulation (not yet in Qemu git)
- ACPI support for the TPM device (SSDT table)
- ACPI support for measurement logging (TCPA table)
- Support for initialzation of the TPM
- Support for the TCG BIOS extensions (1ah handler [ah = 0xbb]) (used by trusted grub; http://trousers.sourceforge.net/grub.html)
- Static Root of Trusted for Measurement (SRTM) support
- Support for S3 resume (sends command to TPM upon resume)
- TPM-specific menu for controlling aspects of the TPM
- [An optional test suite for the TIS interface]
All implementations necessarily follow specifications.
- v2:
- following Kevin's comment
- refactoring code so that every patch compiles
Regards,
Stefan
PATCH v2 3/8 Add public get_rsdp function
This patch adds a global get_rsdp() function call and refactors find_resume_vector() to call it.
--- src/acpi.c | 12 ++++++++++-- src/acpi.h | 1 + 2 files changed, 11 insertions(+), 2 deletions(-)
src/acpi.c +10 -2
@@ -706,11 +706,19 @@ acpi_bios_init(void)dprintf (1 ,"ACPI tables: RSDP=%p RSDT=%p\n" , rsdp, rsdt); } -u32 -find_resume_vector (void ) +struct rsdp_descriptor * +get_rsdp (void ) {dprintf (4 ,"rsdp=%p\n" , RsdpAddr);if (!RsdpAddr || RsdpAddr->signature !=RSDP_SIGNATURE ) +return 0 ; +return RsdpAddr; +} + +u32 +find_resume_vector (void ) +{ +if (!get_rsdp ())return 0 ;struct rsdt_descriptor_rev1 *rsdt = (void *)RsdpAddr->rsdt_physical_address;dprintf (4 ,"rsdt=%p\n" , rsdt);
src/acpi.h +1 -0
@@ -4,6 +4,7 @@#include "types.h" // u32 void acpi_bios_init (void ); +struct rsdp_descriptor *get_rsdp ();u32 find_resume_vector (void );#define RSDP_SIGNATURE 0x2052545020445352LL // "RSD PTR "
PATCH v2 1/8 Add an implementation for a TPM TIS driver
In reply to Stefan BergerThis patch adds an implementation of a TPM TIS driver for the TPM TIS emulation supported by Qemu (patches posted, not in git yet). Usage of the driver is broken up into several functions. The driver is cleanly separated from the rest of the code through an interface holding pointers to the driver's functions. A client using this driver first probes whether the TPM TIS interface is available (probe function) and then invokes the interface function to initialze the interface and send requests and receive responses.
Possible future extensions *could* include a virtio interface for the TPM with a corresponding driver here.
v2:
- adapted tpm_drivers.c to be under LGPLv3
--- src/tpm_drivers.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tpm_drivers.h | 55 +++++++++++++++ 2 files changed, 253 insertions(+)
src/tpm_drivers.c new file +198 -0
@@ -0,0 +1,198 @@ +// Implementation of a TPM driver for the TPM TIS interface +// +// Copyright (C) 2006-2011 IBM Corporation +// Copyright (C) 2006-2011 Stefan Berger <[email protected]> +// +// This file may be distributed under the terms of the GNU LGPLv3 license. + +#include "config.h" +#include "util.h" +#include "tpm_drivers.h" +#include "tcgbios.h" + +/* if device is not there, return '0', '1' otherwise */ +static u32 tis_probe (void ) +{ +u32 rc =0 ; +u32 didvid =readl (TIS_REG (0 ,TIS_REG_DID_VID )); + +if ((didvid !=0 ) && (didvid !=0xffffffff )) + rc =1 ; + +return rc; +} + +static u32 tis_init (void ) +{ +writeb (TIS_REG (0 ,TIS_REG_INT_ENABLE ),0 ); + +return 1 ; +} + +static u32 tis_wait_sts (u8 locty,u32 time,u8 mask,u8 expect) +{ +u32 rc =1 ; + +while (time >0 ) { +u8 sts =readb (TIS_REG (locty,TIS_REG_STS )); +if ((sts & mask) == expect) { + rc =0 ; +break ; + } +msleep (1 ); + time--; + } +return rc; +} + +static u32 tis_activate (u8 locty) +{ +u32 rc =0 ; +u8 acc; +int l; + +if (!(readb (TIS_REG (locty,TIS_REG_ACCESS )) & +TIS_ACCESS_ACTIVE_LOCALITY )) { +/* release locality in use top-downwards */ +for (l =4 ; l >=0 ; l--) +writeb (TIS_REG (l,TIS_REG_ACCESS ), +TIS_ACCESS_ACTIVE_LOCALITY ); + } + +/* request access to locality */ +writeb (TIS_REG (locty,TIS_REG_ACCESS ),TIS_ACCESS_REQUEST_USE ); + + acc =readb (TIS_REG (locty,TIS_REG_ACCESS )); +if ((acc &TIS_ACCESS_ACTIVE_LOCALITY )) { +writeb (TIS_REG (locty,TIS_REG_STS ),TIS_STS_COMMAND_READY ); + rc =tis_wait_sts (locty,1000 , +TIS_STS_COMMAND_READY ,TIS_STS_COMMAND_READY ); + } + +return rc; +} + +static u32 tis_find_active_locality (void ) +{ +u8 locty; + +for (locty =0 ; locty <=4 ; locty++) { +if ((readb (TIS_REG (locty,TIS_REG_ACCESS )) & +TIS_ACCESS_ACTIVE_LOCALITY )) +return locty; + } + +tis_activate (0 ); + +return 0 ; +} + +static u32 tis_ready (void ) +{ +u32 rc =0 ; +u8 locty =tis_find_active_locality (); + +writeb (TIS_REG (locty,TIS_REG_STS ),TIS_STS_COMMAND_READY ); + rc =tis_wait_sts (locty,1000 , +TIS_STS_COMMAND_READY ,TIS_STS_COMMAND_READY ); + +return rc; +} + +static u32 tis_senddata (const u8 *const data,u32 len) +{ +u32 rc =0 ; +u32 offset =0 ; +u32 end =0 ; +u16 burst =0 ; +u32 ctr =0 ; +u8 locty =tis_find_active_locality (); + +do { +while (burst ==0 && ctr <2000 ) { + burst =readl (TIS_REG (locty,TIS_REG_STS )) >>8 ; +if (burst ==0 ) { +msleep (1 ); + ctr++; + } + } + +if (burst ==0 ) { + rc =TCG_RESPONSE_TIMEOUT ; +break ; + } + +while (1 ) { +writeb (TIS_REG (locty,TIS_REG_DATA_FIFO ), data[offset++]); + burst--; + +if (burst ==0 || offset == len) +break ; + } + +if (offset == len) + end =1 ; + }while (end ==0 ); + +return rc; +} + +static u32 tis_readresp (u8 *buffer,u32 *len) +{ +u32 rc =0 ; +u32 offset =0 ; +u32 sts; +u8 locty =tis_find_active_locality (); + +while (offset < *len) { + buffer[offset] =readb (TIS_REG (locty,TIS_REG_DATA_FIFO )); + offset++; + sts =readb (TIS_REG (locty,TIS_REG_STS )); +/* data left ? */ +if ((sts &TIS_STS_DATA_AVAILABLE ) ==0 ) +break ; + } + + *len = offset; + +return rc; +} + + +static u32 tis_waitdatavalid (void ) +{ +u32 rc =0 ; +u8 locty =tis_find_active_locality (); + +if (tis_wait_sts (locty,1000 ,TIS_STS_VALID ,TIS_STS_VALID ) !=0 ) + rc =TCG_NO_RESPONSE ; + +return rc; +} + +static u32 tis_waitrespready (u32 timeout) +{ +u32 rc =0 ; +u8 locty =tis_find_active_locality (); + +writeb (TIS_REG (locty ,TIS_REG_STS ),TIS_STS_TPM_GO ); + +if (tis_wait_sts (locty, timeout, +TIS_STS_DATA_AVAILABLE ,TIS_STS_DATA_AVAILABLE ) !=0 ) + rc =TCG_NO_RESPONSE ; + +return rc; +} + +struct tpm_driver tpm_drivers[TPM_NUM_DRIVERS ] = { + { + .probe = tis_probe, + .init = tis_init, + .activate = tis_activate, + .ready = tis_ready, + .senddata = tis_senddata, + .readresp = tis_readresp, + .waitdatavalid = tis_waitdatavalid, + .waitrespready = tis_waitrespready, + }, +};
src/tpm_drivers.h new file +55 -0
55 lines not on this page · raw message
PATCH v2 4/8 Implementation of the TCG BIOS extensions
In reply to Stefan BergerThis patch implements the main part of the TCG BIOS extensions. It provides the following functionality:
- initialization of the TCPA ACPI table used for logging of measurements
- initialization of the TPM by sending a sequence of commands to it
- proper setup of the TPM once the BIOS hands over control to the bootloader
- support for S3 resume; BIOS sends TPM_Startup(ST_STATE) to TPM
- enable configuration of SeaBIOS to be built with TCGBIOS extensions depending on COREBOOT not being selected
All TCG BIOS extensions are activated with CONFIG_TCGBIOS.
Structures that are needed in subsequent patches are also included in tcgbios.h at this point.
-v2:
- replace mssleep() with calls to msleep()
- Moving Kconfig patch to this file
- converting code to call dprintf(DEBUG_tcg, ...)
- use the get_rsdp call to get hold of the RSDP
- use util.c:checksum()
- Adapting tcgbios.c to be under LGPLv3
- using if (!CONFIG_TCGBIOS) everywhere
--- src/Kconfig | 8 + src/boot.c | 2 src/config.h | 1 src/post.c | 5 src/resume.c | 2 src/tcgbios.c | 394 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tcgbios.h | 312 +++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 3 8 files changed, 727 insertions(+)
src/util.h +3 -0
@@ -497,4 +497,7 @@ extern u8 BiosChecksum;// version (auto generated file out/version.c) extern const char VERSION []; +// TCG BIOS extensions +void tcpa_interrupt_handler16 (struct bregs *regs); +#endif // util.h
src/post.c +5 -0
@@ -25,6 +25,7 @@#include "paravirt.h" // qemu_cfg_port_probe #include "ps2port.h" // ps2port_setup #include "virtio-blk.h" // virtio_blk_setup +#include "tcgbios.h" // tcpa_* /****************************************************************
@@ -238,6 +239,10 @@ maininit(void)mouse_setup ();init_bios_tables (); +// Initialize tpm (after acpi tables were written) +tcpa_acpi_init (); +tcpa_startup (); +// Run vga option rom vga_setup ();
src/boot.c +2 -0
@@ -14,6 +14,7 @@#include "cmos.h" // inb_cmos #include "paravirt.h" // romfile_loadfile #include "pci.h" //pci_bdf_to_* +#include "tcgbios.h" // tcpa_* /****************************************************************
@@ -449,6 +450,7 @@ boot_prep(void)// Allow user to modify BCV/IPL order. interactive_bootmenu ();wait_threads (); +tcpa_leave_bios ();// Map drives and populate BEV list struct bootentry_s *pos = BootList;
src/resume.c +2 -0
@@ -10,6 +10,7 @@#include "biosvar.h" // struct bios_data_area_s #include "bregs.h" // struct bregs #include "acpi.h" // find_resume_vector +#include "tcgbios.h" // tcpa_s3_resume // Reset DMA controller void
@@ -116,6 +117,7 @@ s3_resume(void)if (s3_resume_vector) {dprintf (1 ,"Jump to resume vector (%x)\n" , s3_resume_vector); br.code =FLATPTR_TO_SEGOFF ((void *)s3_resume_vector); +tcpa_s3_resume (); }else {dprintf (1 ,"No resume vector set!\n" );// Jump to the post vector to restart with a normal boot.
src/config.h +1 -0
@@ -119,5 +119,6 @@#define DEBUG_unimplemented 2#define DEBUG_invalid 3#define DEBUG_thread 2 +#define DEBUG_tcg 20#endif // config.h
src/Kconfig +8 -0
@@ -314,6 +314,14 @@ menu "BIOS interfaces" default n help Disable A20 on 16bit boot. + + config TCGBIOS + depends on !COREBOOT + bool "TPM support and TCG BIOS extensions" + default y + help + Provide TPM support along with TCG BIOS extensions + endmenu menu "BIOS Tables"
src/tcgbios.h +312 -0
325 lines not on this page · raw message
src/tcgbios.c +394 -0
409 lines not on this page · raw message
PATCH v2 5/8 Support for BIOS interrupt handler
In reply to Stefan BergerThis patch implements the TCG BIOS interrupt handler 1ah. It is for example used by trusted grub.
This patch adds an implementation of SHA1 (following NIST specs., IETF RFC 3147 and Wikipedia) for speeding up measurements of code. Trusted Grub for example makes use of this interface and measures (calculates SHA1) of the Linux kernel and initrd. Those files can be rather large and hunting their bytes through the TIS interface as part of the int handler commands invoked by trusted grub does take quite some time due to the many vmexits the interface is creating (one per byte).
There is also a threshold for the size of data to hash (100k) below which the TPM is used and above the internal faster SHA1 algorithm is used.
-v2:
- use if (!CONFIG_TCGBIOS) everywhere
- put sha1() into its own file
- move rol() and bswap64() into util.h
- move inthandler invocation from stacks.c into handle_1abb()
--- Makefile | 2 src/Kconfig | 8 src/clock.c | 12 + src/sha1.c | 139 ++++++++++++++ src/sha1.h | 8 src/tcgbios.c | 545 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tcgbios.h | 4 src/util.h | 31 +++ 8 files changed, 748 insertions(+), 1 deletion(-)
src/tcgbios.c +545 -0
@@ -15,6 +15,7 @@#include "util.h" // printf, get_keystroke #include "tcgbios.h" // tcpa_*, prototypes #include "acpi.h" // RSDP_SIGNATURE, rsdt_descriptor +#include "sha1.h" // sha1 static const u8 Startup_ST_CLEAR[2 ] = {0x00 ,TPM_ST_CLEAR };
@@ -463,4 +464,548 @@ err_exit: } +static int +isValidPcpes (struct pcpes *pcpes) +{ +return (pcpes->eventtype !=0 ); +} + + +static u8 * +get_lasa_last_ptr (u16 *entry_count,u8 **lasa_next) +{ +struct pcpes *pcpes; +u32 laml; +u8 *lasa_base =get_lasa_base_ptr (&laml); +u8 *lasa_last =NULL ; +u8 *end = lasa_base + laml; +u32 size; + +if (entry_count) + *entry_count =0 ; + +if (!lasa_base) +return NULL ; + +while (lasa_base < end) { + pcpes = (struct pcpes *)lasa_base; +if (!isValidPcpes (pcpes)) +break ; +if (entry_count) + (*entry_count)++; + size = pcpes->eventdatasize + offsetof(struct pcpes , event); + lasa_last = lasa_base; + lasa_base += size; + } + +if (lasa_next) + *lasa_next = lasa_base; + +return lasa_last; +} + + +#if CONFIG_TPM_FOR_SHA1 + +static u32 +sha1_calc (const u8 *data,u32 length,u8 *hash) +{ +u32 rc; +u32 returnCode; +struct tpm_res_sha1start start; +struct tpm_res_sha1complete complete; +u32 blocks = length /64 ; +u32 rest = length &0x3f ; +u32 numbytes, numbytes_no; +u32 offset =0 ; + +if (length >100 *1024 ) +return sha1 (data, length, hash); + + rc =build_and_send_cmd (TPM_ORD_SHA1Start, +NULL ,0 , + (u8 *)&start, +sizeof (struct tpm_res_sha1start ), + &returnCode); + +if (rc || returnCode) + goto err_exit; + +while (blocks >0 ) { + + numbytes =ntohl (start.max_num_bytes); +if (numbytes > blocks *64 ) + numbytes = blocks *64 ; + + numbytes_no =htonl (numbytes); + + rc =build_and_send_cmd_od (TPM_ORD_SHA1Update, + (u8 *)&numbytes_no,sizeof (numbytes_no), +NULL ,0 , &returnCode, + &data[offset], numbytes); + +if (rc || returnCode) + goto err_exit; + + offset += numbytes; + blocks -= (numbytes /64 ); + } + + numbytes_no =htonl (rest); + + rc =build_and_send_cmd_od (TPM_ORD_SHA1Complete, + (u8 *)&numbytes_no,sizeof (numbytes_no), + (u8 *)&complete, +sizeof (struct tpm_res_sha1complete ), + &returnCode, + &data[offset], rest); + +if (rc || returnCode) + goto err_exit; + +memcpy (hash, complete.hash,sizeof (complete.hash)); + +return 0 ; + +err_exit: +dprintf (DEBUG_tcg,"TCGBIOS: TPM SHA1 malfunctioning.\n" ); + + tcpa_state.tpm_working =0 ; +if (rc) +return rc; +return TCG_TCG_COMMAND_ERROR ; +} + +#else + +static u32 +sha1_calc (const u8 *data,u32 length,u8 *hash) +{ +return sha1 (data, length, hash); +} + +#endif /* CONFIG_TPM_FOR_SHA1 */ + + +/* +* Extend the ACPI log with the given entry by copying the +* entry data into the log. +* Input +* Pointer to the structure to be copied into the log +* +* Output: +* lower 16 bits of return code contain entry number +* if entry number is '0', then upper 16 bits contain error code. +*/ +static u32 +tcpa_extend_acpi_log (void *entry_ptr,u16 *entry_count) +{ +u32 laml, size; +u8 *lasa_base =get_lasa_base_ptr (&laml), *lasa_next; +struct pcpes *pcpes = (struct pcpes *)entry_ptr; + +get_lasa_last_ptr (entry_count, &lasa_next); + +dprintf (DEBUG_tcg,"TCGBIOS: LASA_BASE = %p, LASA_NEXT = %p\n" ,lasa_base, lasa_next); + +if (lasa_next ==NULL || laml ==0 ) +return TCG_PC_LOGOVERFLOW ; + + size = pcpes->eventdatasize + offsetof(struct pcpes , event); + +if ((lasa_next + size - lasa_base) > laml) { +dprintf (DEBUG_tcg,"TCGBIOS: LOG OVERFLOW: size = %d\n" , size); +return TCG_PC_LOGOVERFLOW ; + } + +memcpy (lasa_next, entry_ptr, size); + + (*entry_count)++; + +return 0 ; +} + + +static u32 +is_preboot_if_shutdown (void ) +{ +return tcpa_state.if_shutdown; +} + + +static u32 +shutdown_preboot_interface (void ) +{ +u32 rc =0 ; + +if (!is_preboot_if_shutdown ()) { + tcpa_state.if_shutdown =1 ; + }else { + rc =TCG_INTERFACE_SHUTDOWN ; + } + +return rc; +} + + +static void +tcpa_shutdown (void ) +{ +reset_acpi_log (); +shutdown_preboot_interface (); +} + + +static u32 +pass_through_to_tpm (struct pttti *pttti,struct pttto *pttto) +{ +u32 rc =0 ; +u32 resbuflen =0 ; +struct tpm_req_header *trh; +u8 locty =0 ; +struct iovec iovec[2 ]; +const u32 *tmp; + +if (is_preboot_if_shutdown ()) { + rc =TCG_INTERFACE_SHUTDOWN ; + goto err_exit; + } + + trh = (struct tpm_req_header *)pttti->tpmopin; + +if (pttti->ipblength <sizeof (struct pttti ) +TPM_REQ_HEADER_SIZE || + pttti->opblength <sizeof (struct pttto ) || +ntohl (trh->totlen) +sizeof (struct pttti ) > pttti->ipblength ) { + rc =TCG_INVALID_INPUT_PARA ; + goto err_exit; + } + + resbuflen = pttti->opblength - offsetof(struct pttto , tpmopout); + + iovec[0 ].data = pttti->tpmopin; + tmp = (const u32 *)&((u8 *)iovec[0 ].data)[2 ]; + iovec[0 ].length =htonl (*tmp); + + iovec[1 ].data =NULL ; + iovec[1 ].length =0 ; + + rc =transmit (locty, iovec, pttto->tpmopout, &resbuflen); +if (rc) + goto err_exit; + + pttto->opblength = offsetof(struct pttto , tpmopout) + resbuflen; + pttto->reserved =0 ; + +err_exit: +if (rc !=0 ) { + pttto->opblength =4 ; + pttto->reserved =0 ; + } + +return rc; +} + + +static u32 +tpm_extend (u8 *hash,u32 pcrindex) +{ +u32 rc; +struct pttto_extend pttto; +struct pttti_extend pttti = { + .pttti = { + .ipblength =sizeof (struct pttti_extend ), + .opblength =sizeof (struct pttto_extend ), + }, + .req = { + .tag =htons (0xc1 ), + .totlen =htonl (sizeof (pttti.req)), + .ordinal =htonl (TPM_ORD_Extend), + .pcrindex =htonl (pcrindex), + }, + }; + +memcpy (pttti.req.digest, hash,sizeof (pttti.req.digest)); + + rc =pass_through_to_tpm (&pttti.pttti, &pttto.pttto); + +if (rc ==0 ) { +if (pttto.pttto.opblength <TPM_RSP_HEADER_SIZE || + pttto.pttto.opblength != +sizeof (struct pttto ) +ntohl (pttto.rsp.totlen) || +ntohs (pttto.rsp.tag) !=0xc4 ) { + rc =TCG_FATAL_COM_ERROR ; + } + } + +if (rc) +tcpa_shutdown (); + +return rc; +} + + +static u32 +hash_all (const struct hai *hai,u8 *hash) +{ +if (is_preboot_if_shutdown () !=0 ) +return TCG_INTERFACE_SHUTDOWN ; + +if (hai->ipblength !=sizeof (struct hai ) || + hai->hashdataptr ==0 || + hai->hashdatalen ==0 || + hai->algorithmid !=TPM_ALG_SHA ) +return TCG_INVALID_INPUT_PARA ; + +return sha1_calc ((const u8 *)hai->hashdataptr, hai->hashdatalen, hash); +} + + +static u32 +hash_log_event (const struct hlei *hlei,struct hleo *hleo) +{ +u32 rc =0 ; +u16 size; +struct pcpes *pcpes; +u16 entry_count; + +if (is_preboot_if_shutdown () !=0 ) { + rc =TCG_INTERFACE_SHUTDOWN ; + goto err_exit; + } + + size = hlei->ipblength; +if (size !=sizeof (*hlei)) { + rc =TCG_INVALID_INPUT_PARA ; + goto err_exit; + } + + pcpes = (struct pcpes *)hlei->logdataptr; + +if (pcpes->pcrindex >=24 || + pcpes->pcrindex != hlei->pcrindex || + pcpes->eventtype != hlei->logeventtype) { + rc =TCG_INVALID_INPUT_PARA ; + goto err_exit; + } + +if ((hlei->hashdataptr !=0 ) && (hlei->hashdatalen !=0 )) { + rc =sha1_calc ((const u8 *)hlei->hashdataptr, + hlei->hashdatalen, pcpes->digest); +if (rc) +return rc; + } + + rc =tcpa_extend_acpi_log ((void *)hlei->logdataptr, &entry_count); +if (rc) + goto err_exit; + +/* updating the log was fine */ + hleo->opblength =sizeof (struct hleo ); + hleo->reserved =0 ; + hleo->eventnumber = entry_count; + +err_exit: +if (rc !=0 ) { + hleo->opblength =2 ; + hleo->reserved =0 ; + } + +return rc; +} + + +static u32 +hash_log_extend_event (const struct hleei_short *hleei_s,struct hleeo *hleeo) +{ +u32 rc =0 ; +struct hleo hleo; +struct hleei_long *hleei_l = (struct hleei_long *)hleei_s; +const void *logdataptr; +u32 logdatalen; +struct pcpes *pcpes; + +/* short or long version? */ +switch (hleei_s->ipblength) { +case sizeof (struct hleei_short ): +/* short */ + logdataptr = hleei_s->logdataptr; + logdatalen = hleei_s->logdatalen; +break ; + +case sizeof (struct hleei_long ): +/* long */ + logdataptr = hleei_l->logdataptr; + logdatalen = hleei_l->logdatalen; +break ; + +default : +/* bad input block */ + rc =TCG_INVALID_INPUT_PARA ; + goto err_exit; + } + + pcpes = (struct pcpes *)logdataptr; + +struct hlei hlei = { + .ipblength =sizeof (hlei), + .hashdataptr = hleei_s->hashdataptr, + .hashdatalen = hleei_s->hashdatalen, + .pcrindex = hleei_s->pcrindex, + .logeventtype= pcpes->eventtype, + .logdataptr = logdataptr, + .logdatalen = logdatalen, + }; + + rc =hash_log_event (&hlei, &hleo); +if (rc) + goto err_exit; + + hleeo->opblength =sizeof (struct hleeo ); + hleeo->reserved =0 ; + hleeo->eventnumber = hleo.eventnumber; + + rc =tpm_extend (pcpes->digest, hleei_s->pcrindex); + +err_exit: +if (rc !=0 ) { + hleeo->opblength =4 ; + hleeo->reserved =0 ; + } + +return rc; + +} + + +static u32 +tss (struct ti *ti,struct to *to) +{ +u32 rc =0 ; + +if (is_preboot_if_shutdown () ==0 ) { + rc =TCG_PC_UNSUPPORTED ; + }else { + rc =TCG_INTERFACE_SHUTDOWN ; + } + + to->opblength =sizeof (struct to ); + to->reserved =0 ; + +return rc; +} + + +static u32 +compact_hash_log_extend_event (u8 *buffer, +u32 info, +u32 length, +u32 pcrindex, +u32 *edx_ptr) +{ +u32 rc =0 ; +struct hleeo hleeo; +struct pcpes pcpes = { + .pcrindex = pcrindex, + .eventtype =EV_COMPACT_HASH , + .eventdatasize =sizeof (info), + .event = info, + }; +struct hleei_short hleei = { + .ipblength =sizeof (hleei), + .hashdataptr = buffer, + .hashdatalen = length, + .pcrindex = pcrindex, + .logdataptr = &pcpes, + .logdatalen =sizeof (pcpes), + }; + + rc =hash_log_extend_event (&hleei, &hleeo); +if (rc ==0 ) + *edx_ptr = hleeo.eventnumber; + +return rc; +} + + +void VISIBLE32FLAT +tcpa_interrupt_handler32 (struct bregs *regs) +{ +if (!CONFIG_TCGBIOS ) +return ; + +switch ((enum irq_ids )regs->al) { +case TCG_StatusCheck: +if (is_tpm_present () ==0 ) { +/* no TPM available */ + regs->eax =TCG_PC_TPM_NOT_PRESENT ; + }else { + regs->eax =0 ; + regs->ebx =TCG_MAGIC ; + regs->ch =TCG_VERSION_MAJOR ; + regs->cl =TCG_VERSION_MINOR ; + regs->edx =0x0 ; + regs->esi = (u32 )get_lasa_base_ptr (NULL ); + regs->edi = + (u32 )get_lasa_last_ptr (NULL ,NULL ); +set_cf (regs,0 );; + } +break ; + +case TCG_HashLogExtendEvent: + regs->eax = +hash_log_extend_event ( + (struct hleei_short *)input_buf32 (regs), + (struct hleeo *)output_buf32 (regs)); +set_cf (regs,0 ); +break ; + +case TCG_PassThroughToTPM: + regs->eax = +pass_through_to_tpm ((struct pttti *)input_buf32 (regs), + (struct pttto *)output_buf32 (regs)); +set_cf (regs,0 ); +break ; + +case TCG_ShutdownPreBootInterface: + regs->eax =shutdown_preboot_interface (); +set_cf (regs,0 ); +break ; + +case TCG_HashLogEvent: + regs->eax =hash_log_event ((struct hlei *)input_buf32 (regs), + (struct hleo *)output_buf32 (regs)); +set_cf (regs,0 ); +break ; + +case TCG_HashAll: + regs->eax = +hash_all ((struct hai *)input_buf32 (regs), + (u8 *)output_buf32 (regs)); +set_cf (regs,0 ); +break ; + +case TCG_TSS : + regs->eax =tss ((struct ti *)input_buf32 (regs), + (struct to *)output_buf32 (regs)); +set_cf (regs,0 ); +break ; + +case TCG_CompactHashLogExtendEvent: + regs->eax = +compact_hash_log_extend_event ((u8 *)input_buf32 (regs), + regs->esi, + regs->ecx, + regs->edx, + ®s->edx); +set_cf (regs,0 ); +break ; + +default : +set_cf (regs,1 ); + } + +return ; +} + +
src/clock.c +12 -0
@@ -13,6 +13,7 @@#include "bregs.h" // struct bregs #include "biosvar.h" // GET_GLOBAL #include "usb-hid.h" // usb_check_event +#include "tcgbios.h" // _cfunc32flat_tcpa_interrupt_handler32 // RTC register flags #define RTC_A_UIP 0x80
@@ -434,6 +435,16 @@ handle_1a07(struct bregs *regs)set_success (regs); } +static void +handle_1abb (struct bregs *regs) +{ +if (!CONFIG_TCGBIOS ) +return ; + +dprintf (DEBUG_tcg,"16: Calling tcpa_interrupt_handler\n" ); +call32 (_cfunc32flat_tcpa_interrupt_handler32, (u32 )regs,0 ); +} +// Unsupported static void handle_1aXX (struct bregs *regs)
@@ -456,6 +467,7 @@ handle_1a(struct bregs *regs)case 0x06 :handle_1a06 (regs);break ;case 0x07 :handle_1a07 (regs);break ;case 0xb1 :handle_1ab1 (regs);break ; +case 0xbb :handle_1abb (regs);break ;default :handle_1aXX (regs);break ; } }
src/Kconfig +8 -0
@@ -322,6 +322,14 @@ menu "BIOS interfaces" help Provide TPM support along with TCG BIOS extensions + config TPM_FOR_SHA1 + depends on TCGBIOS + bool "Use the TPM for SHA1 calculations" + default y + help + Either you may use the TPM for SHA1 calculations or + use the internal sha1 algorithm to do it (faster). + endmenu menu "BIOS Tables"
Makefile +1 -1
@@ -20,7 +20,7 @@ SRC16=$(SRCBOTH) system.c disk.c font.c SRC32FLAT=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \ acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \ lzmadecode.c bootsplash.c jpeg.c usb-hub.c paravirt.c dev-i440fx.c \ - pci_region.c tcgbios.c tpm_drivers.c + pci_region.c tcgbios.c tpm_drivers.c sha1.c SRC32SEG=util.c output.c pci.c pcibios.c apm.c stacks.c cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc \
src/sha1.c new file +139 -0
139 lines not on this page · raw message
src/sha1.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef __SHA1_H +#define __SHA1_H + +#include "types.h" // u32 + +u32 sha1 (const u8 *data,u32 length,u8 *hash); + +#endif // sha1.h
src/util.h +31 -0
@@ -500,4 +500,35 @@ extern const char VERSION[];// TCG BIOS extensions void tcpa_interrupt_handler16 (struct bregs *regs); + +static inline u32 rol (u32 val,u16 rol) +{ +u32 res; + + __asm__ __volatile__ ("rol %%cl, %%eax" + :"=a" (res) + :"a" (val),"c" (rol)); + +return res; +} + + +static inline u64 bswap_64 (u64 val) +{ +u32 hi = (u32)(val >>32 ); +u32 lo = (u32 )val; + + __asm__ __volatile__ ("bswap %%eax" + :"=a" (lo) + :"a" (lo)); + + __asm__ __volatile__ ("bswap %%eax" + :"=a" (hi) + :"a" (hi)); + +return ((u64 )lo <<32 | hi); +} + + +#endif // util.h
src/tcgbios.h +4 -0
@@ -359,6 +359,10 @@ enum ipltype { IPL_EL_TORITO_2 }; + +void tcpa_interrupt_handler32 (struct bregs *regs); +void _cfunc32flat_tcpa_interrupt_handler32 (struct bregs *regs); +void tcpa_acpi_init (void );int has_working_tpm (void );u32 tcpa_startup (void );
PATCH v2 6/8 Add measurement code to the BIOS
In reply to Stefan BergerThis patch adds invocactions of functions that measure various parts of the code and data through various parts of the BIOS code. It follows TCG specifications on what needs to be measured. It also adds the implementation of the called functions.
Reference for what needs to be measured can be found in section 3.2.2++ in
The first measurements are done once the ACPI tables have been initialized. The whole 0xe and 0xf segment are measure for measuring the 'POST'.
Once booted into Linux, the current measurements produce the following logs which can be found in /sys/kernel/security/tpm0/ascii_bios_measurements. The below log also shows measurements from trusted grub.
0 0f9a2ad992c04a24e081b2a49b984616c4358386 01 [POST CODE]
1 3fb240d2a04085a4e84f81e4398e070ed5a18163 06 [SMBIOS]
2 cc812353fc277c1fab99e0b721752a1392984566 06 [Option ROM]
2 9dbd87163112e5670378abe4510491259a61f411 05 [Start Option ROM Scan] 2 6f74e357331b8dee11bbad85f27bc66cb873106c 06 [Option ROM]
2 5626eb7ac05c7231e46d7461e7d3839b03ae9fad 06 [Option ROM]
4 c1e25c3f6b0dc78d57296aa2870ca6f782ccf80f 05 [Calling INT 19h] 0 d9be6524a5f5047db5866813acf3277892a7a30a 04 []
1 d9be6524a5f5047db5866813acf3277892a7a30a 04 []
2 d9be6524a5f5047db5866813acf3277892a7a30a 04 []
3 d9be6524a5f5047db5866813acf3277892a7a30a 04 []
4 d9be6524a5f5047db5866813acf3277892a7a30a 04 []
5 d9be6524a5f5047db5866813acf3277892a7a30a 04 []
6 d9be6524a5f5047db5866813acf3277892a7a30a 04 []
7 d9be6524a5f5047db5866813acf3277892a7a30a 04 []
4 8cf2fe6c87d4d0b2998a43da630292e6d85ee8b6 05 [Booting BCV device 80h (HDD)] 4 5dff94459a3e2d13a433ef94afdc306144565bf7 0d [IPL]
5 d1b33afde65ad47502332af957c60f20c84c1edc 0e [IPL Partition Data] 4 487ce764b527ccad17f1d04243d0136fa981e6c4 0d [IPL]
4 91d285e4dead566324c8938a3cc75803f462d9a1 0d [IPL]
4 8ba79ac98bb491524fef29defc724daaf6263d35 0d [IPL]
4 c591c15b82e4ff30e7383a4ff1ef3b41b38521ac 06 []
4 8cdc27ec545eda33fbba1e8b8dae4da5c7206972 04 [Grub Event Separator] 5 8cdc27ec545eda33fbba1e8b8dae4da5c7206972 04 [Grub Event Separator] 5 e8673b9e14b02dc12d8ccfd0176bca7a3de7fc3c 0e [IPL Partition Data] 5 0163e375a0af7525c5dac1a8e74b277359e40d1d 1105 []
8 4be30f67c3d48ab7f04d9c0fd07f06d4c68379be 1205 []
8 54c83965978de9708d026016ecb0e70660e04388 1305 []
5 2431ed60130faeaf3a045f21963f71cacd46a029 04 [OS Event Separator] 8 2431ed60130faeaf3a045f21963f71cacd46a029 04 [OS Event Separator] 8 f3973cae05d6e2055062119d6e6e1e077b7df876 1005 []
v2:
- dropping call to tcpa_measure_post
- converting tcpa_option_rom and tcpa_ipl functions to get pointers rather than segments passed
- introduce public get_smbios_entry_point() function and use it rather than searching for the entry point
- use dprintf(DEBUG_tcg, ...)
--- src/boot.c | 6 src/cdrom.c | 10 + src/optionroms.c | 4 src/post.c | 4 src/smbios.c | 12 + src/smbios.h | 1 src/tcgbios.c | 359 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tcgbios.h | 8 + 8 files changed, 404 insertions(+)
src/post.c +4 -0
@@ -190,6 +190,9 @@ init_hw(void) void VISIBLE32FLATstartBoot (void) { +tcpa_calling_int19h (); +tcpa_add_event_separators (); +// Clear low-memory allocations (required by PMM spec). memset ((void *)BUILD_STACK_ADDR ,0 ,BUILD_EBDA_MINIMUM -BUILD_STACK_ADDR );
@@ -242,6 +245,7 @@ maininit(void)// Initialize tpm (after acpi tables were written) tcpa_acpi_init ();tcpa_startup (); +tcpa_smbios_measure ();// Run vga option rom vga_setup ();
src/optionroms.c +4 -0
@@ -14,6 +14,7 @@#include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA #include "boot.h" // IPL #include "paravirt.h" // qemu_cfg_* +#include "tcgbios.h" // tcpa_* /****************************************************************
@@ -134,6 +135,7 @@ is_valid_rom(struct rom_header *rom)if (CONFIG_OPTIONROMS_CHECKSUM )return 0 ; } +tcpa_option_rom (rom, len);return 1 ; }
@@ -385,6 +387,8 @@ optionrom_setup(void)memset (sources,0 ,sizeof (sources));u32 post_vga = RomEnd; +tcpa_start_option_rom_scan (); +if (CONFIG_OPTIONROMS_DEPLOYED ) {// Option roms are already deployed on the system. u32 pos = RomEnd;
src/boot.c +6 -0
@@ -533,6 +533,10 @@ boot_disk(u8 bootdrv, int checksig) } } +tcpa_add_bootdevice (0 , bootdrv); +/* specs: 8.2.3 steps 4 and 5 */ +tcpa_ipl (IPL_BCV ,MAKE_FLATPTR (bootseg,0 ),512 ); +/* Canonicalize bootseg:bootip */ u16 bootip = (bootseg &0x0fff ) <<4 ; bootseg &=0xf000 ;
@@ -620,6 +624,8 @@ do_boot(u16 seq_nr)break ; } +tcpa_returned_via_int18h (); +// Boot failed: invoke the boot recovery function struct bregs br;memset (&br,0 ,sizeof (br));
src/cdrom.c +10 -0
@@ -11,6 +11,7 @@#include "biosvar.h" // GET_EBDA #include "ata.h" // ATA_CMD_REQUEST_SENSE #include "blockcmd.h" // CDB_CMD_REQUEST_SENSE +#include "tcgbios.h" // tcpa_* /****************************************************************
@@ -293,6 +294,11 @@ cdrom_boot(struct drive_s *drive_g)if (buffer[0x20 ] !=0x88 )return 11 ;// Bootable +/* specs: 8.2.3 step 5 and 8.2.5.6, measure El Torito boot catalog */ +/* measure 2048 bytes (one sector) */ +tcpa_add_bootdevice (1 ,0 ); +tcpa_ipl (IPL_EL_TORITO_2 ,MAKE_FLATPTR (GET_SEG (SS ), buffer),2048 ); +u16 ebda_seg =get_ebda_seg ();u8 media = buffer[0x21 ];SET_EBDA2 (ebda_seg, cdemu.media, media);
@@ -319,6 +325,10 @@ cdrom_boot(struct drive_s *drive_g)if (ret)return 12 ; +/* specs: 8.2.3 step 4 and 8.2.5.6, measure El Torito boot image */ +/* measure 1st 512 bytes */ +tcpa_ipl (IPL_EL_TORITO_1 ,MAKE_FLATPTR (boot_segment,0 ),512 ); +if (media ==0 ) {// No emulation requested - return success. SET_EBDA2 (ebda_seg, cdemu.emulated_extdrive,EXTSTART_CD + cdid);
src/tcgbios.c +359 -0
375 lines not on this page · raw message
src/smbios.c +12 -0
@@ -10,6 +10,8 @@#include "paravirt.h" // qemu_cfg_smbios_load_field #include "smbios.h" // struct smbios_entry_point +static struct smbios_entry_point *smbios_ep; + static void smbios_entry_point_init(u16 max_structure_size, u16 structure_table_length,
@@ -24,6 +26,7 @@ smbios_entry_point_init(u16 max_structurfree (finaltable);return ; } + smbios_ep = ep;memcpy (finaltable, structure_table_address, structure_table_length);memcpy (ep->anchor_string,"_SM_" ,4 );
@@ -512,3 +515,12 @@ smbios_init(void)smbios_entry_point_init (max_struct_size, p - start, start, nr_structs);free (start); } + +struct smbios_entry_point * +get_smbios_entry_point (void ) +{ +if (!smbios_ep ||memcmp (smbios_ep->anchor_string,"_SM_" ,4 ) !=0 ) +return 0 ; + +return smbios_ep; +}
src/smbios.h +1 -0
@@ -3,6 +3,7 @@// smbios.c void smbios_init (void ); +struct smbios_entry_point *get_smbios_entry_point ();/* SMBIOS entry point -- must be written to a 16-bit aligned address between 0xf0000 and 0xfffff.
src/tcgbios.h +8 -0
@@ -368,6 +368,14 @@ int has_working_tpm(void);u32 tcpa_startup (void );u32 tcpa_leave_bios (void );u32 tcpa_s3_resume (void ); +u32 tcpa_calling_int19h (void ); +u32 tcpa_returned_via_int18h (void ); +u32 tcpa_add_bootdevice (u32 bootcd,u32 bootdrv); +u32 tcpa_add_event_separators (void ); +u32 tcpa_ipl (enum ipltype bootcd,const u8 *addr,u32 count); +u32 tcpa_start_option_rom_scan (void ); +u32 tcpa_option_rom (const void *addr,u32 len); +u32 tcpa_smbios_measure (void );#endif /* TCGBIOS_H */
PATCH v2 7/8 Add a menu for TPM control
In reply to Stefan BergerThis patch provides an addtional menu entry that enables the user to control certain aspects of the TPM.
If a working TPM has been detected, the top level BIOS menu will look like this:
Press F12 for boot menu.
Press F11 to TPM menu.
Upon pressing F11 the TPM menu will be shown:
1. Enable TPM
2. Disable TPM
3. Activate TPM
4. Deactivate TPM
5. Clear ownership
6. Allow installation of owner
7. Prevent installation of owner
Escape for previous menu.
TPM is enabled, active, does not have an owner but one can be installed.
The code for the above 7 menu items is part of this patch.
v2:
- use if (!CONFIG_TCGBIOS) ... in all non-static functions
- use dprintf(DEBUG_tcg, ...) for printing of debugging info
- removing some code used for a future patch
--- src/boot.c | 10 + src/tcgbios.c | 502 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tcgbios.h | 1 3 files changed, 512 insertions(+), 1 deletion(-)
src/tcgbios.c +502 -0
@@ -75,6 +75,10 @@ static tcpa_state_t tcpa_state = {extern struct tpm_driver tpm_drivers[]; +typedef struct { +u8 op; +}tpm_bios_cfg_t ; +/******************************************************** Extensions for TCG-enabled BIOS
@@ -1368,3 +1372,501 @@ tcpa_ipl(enum ipltype bootcd, const u8 * +static u32 +assert_physical_presence (void ) +{ +u32 rc =0 ; +u32 returnCode; + + rc =build_and_send_cmd (TPM_ORD_PhysicalPresence, + PhysicalPresence_CMD_ENABLE, +sizeof (PhysicalPresence_CMD_ENABLE), +NULL ,10 , &returnCode); + +dprintf (DEBUG_tcg, +"Return code from TSC_PhysicalPresence(CMD_ENABLE) = 0x%08x\n" , + returnCode); + +if (rc || returnCode) + goto err_exit; + + rc =build_and_send_cmd (TPM_ORD_PhysicalPresence, + PhysicalPresence_PRESENT, +sizeof (PhysicalPresence_PRESENT), +NULL ,10 , &returnCode); + +dprintf (DEBUG_tcg, +"Return code from TSC_PhysicalPresence(PRESENT) = 0x%08x\n" , + returnCode); + +if (rc || returnCode) + goto err_exit; + +return 0 ; + +err_exit: +dprintf (DEBUG_tcg,"TCGBIOS: TPM malfunctioning (line %d).\n" , __LINE__); + + tcpa_state.tpm_working =0 ; +if (rc) +return rc; +return TCG_TCG_COMMAND_ERROR ; +} + + +static u32 +read_permanent_flags (char *buf,int buf_len) +{ +u32 rc; +u32 returnCode; +struct tpm_res_getcap_perm_flags pf; + +memset (buf,0x0 , buf_len); + + rc =build_and_send_cmd (TPM_ORD_GetCapability, + GetCapability_Permanent_Flags, +sizeof (GetCapability_Permanent_Flags), + (u8 *)&pf, +sizeof (struct tpm_res_getcap_perm_flags ), + &returnCode); + +dprintf (DEBUG_tcg,"TCGBIOS: Return code from TPM_GetCapability() = 0x%08x\n" , + returnCode); + +if (rc || returnCode) + goto err_exit; + +memcpy (buf, &pf.perm_flags, buf_len); + +return 0 ; + +err_exit: +dprintf (DEBUG_tcg,"TCGBIOS: TPM malfunctioning (line %d).\n" , __LINE__); + + tcpa_state.tpm_working =0 ; +if (rc) +return rc; +return TCG_TCG_COMMAND_ERROR ; +} + + +static u32 +read_has_owner (u8 *has_owner) +{ +u32 rc; +u32 returnCode; +struct tpm_res_getcap_ownerauth oauth; + + rc =build_and_send_cmd (TPM_ORD_GetCapability, + GetCapability_OwnerAuth, +sizeof (GetCapability_OwnerAuth), + (u8 *)&oauth, +sizeof (struct tpm_res_getcap_ownerauth ), + &returnCode); + +dprintf (DEBUG_tcg,"TCGBIOS: Return code from TPM_GetCapability() = 0x%08x\n" , + returnCode); + +if (rc || returnCode) + goto err_exit; + + *has_owner = oauth.flag; + +return 0 ; + +err_exit: +dprintf (DEBUG_tcg,"TCGBIOS: TPM malfunctioning (line %d).\n" , __LINE__); + + tcpa_state.tpm_working =0 ; +if (rc) +return rc; +return TCG_TCG_COMMAND_ERROR ; +} + + +static u32 +disable_tpm (int disable,int verbose) +{ +u32 rc; +u32 returnCode; +struct tpm_permanent_flags pf; + + rc =read_permanent_flags ((char *)&pf,sizeof (pf)); +if (rc) +return rc; + +if (!!pf.flags[PERM_FLAG_IDX_DISABLE ] == !!disable) { +if (verbose) +printf ("TPM is already %s.\n," , + disable ?"disabled" :"enabled" ); +return 0 ; + } + + rc =assert_physical_presence (); +if (rc) { +dprintf (DEBUG_tcg,"TCGBIOS: Asserting physical presence failed.\n" ); +return rc; + } + + rc =build_and_send_cmd (disable ? TPM_ORD_PhysicalDisable + : TPM_ORD_PhysicalEnable, +NULL ,0 ,NULL ,10 , &returnCode); +dprintf (DEBUG_tcg,"Return code from TPM_Physical%sable = 0x%08x\n" , + disable ?"Dis" :"En" , returnCode); + +if (rc || returnCode) + goto err_exit; + + +return 0 ; + +err_exit: +dprintf (DEBUG_tcg,"TCGBIOS: %sabling the TPM failed.\n" , + disable ?"Dis" :"En" ); +dprintf (DEBUG_tcg,"TCGBIOS: TPM malfunctioning (line %d).\n" , __LINE__); + + tcpa_state.tpm_working =0 ; +if (rc) +return rc; +return TCG_TCG_COMMAND_ERROR ; +} + + +static u32 +deactivate_tpm (int deactivate,int allow_reset,int verbose) +{ +u32 rc; +u32 returnCode; +struct tpm_permanent_flags pf; + + rc =read_permanent_flags ((char *)&pf,sizeof (pf)); +if (rc) +return rc; + +if (!!pf.flags[PERM_FLAG_IDX_DEACTIVATED ] == !!deactivate) { +if (verbose) +printf ("TPM is already %s.\n" , + deactivate ?"deactivated" :"activated" ); +return 0 ; + } + +if (pf.flags[PERM_FLAG_IDX_DISABLE ]) { +if (verbose) +printf ("TPM must first be enabled.\n" ); +return 0 ; + } + + rc =assert_physical_presence (); +if (rc) { +dprintf (DEBUG_tcg,"TCGBIOS: Asserting physical presence failed.\n" ); +return rc; + } + + rc =build_and_send_cmd (TPM_ORD_PhysicalSetDeactivated, + deactivate ? CommandFlag_TRUE + : CommandFlag_FALSE, + deactivate ?sizeof (CommandFlag_TRUE) + :sizeof (CommandFlag_FALSE), +NULL ,10 , &returnCode); + +dprintf (DEBUG_tcg, +"Return code from PhysicalSetDeactivated(%d) = 0x%08x\n" , + deactivate ?1 :0 , returnCode); + +if (rc || returnCode) + goto err_exit; + +if (!deactivate && allow_reset) { +if (verbose) { +printf ("Requiring a reboot to activate the TPM.\n" ); + +msleep (2000 ); + } +reset_vector (); + } + +return 0 ; + +err_exit: +dprintf (DEBUG_tcg,"TCGBIOS: TPM malfunctioning (line %d).\n" , __LINE__); + + tcpa_state.tpm_working =0 ; +if (rc) +return rc; +return TCG_TCG_COMMAND_ERROR ; +} + + +static u32 +enable_activate (int allow_reset,int verbose) +{ +u32 rc; + + rc =disable_tpm (0 , verbose); +if (rc) +return rc; + + rc =deactivate_tpm (0 , allow_reset, verbose); + +return rc; +} + + +static u32 +force_clear (int enable_activate_before,int enable_activate_after,int verbose) +{ +u32 rc; +u32 returnCode; +u8 has_owner; + + rc =read_has_owner (&has_owner); +if (rc) +return rc; +if (!has_owner) { +if (verbose) +printf ("TPM does not have an owner.\n" ); +return 0 ; + } + +if (enable_activate_before) { + rc =enable_activate (0 , verbose); +if (rc) { +dprintf (DEBUG_tcg, +"TCGBIOS: Enabling/activating the TPM failed.\n" ); +return rc; + } + } + + rc =assert_physical_presence (); +if (rc) { +dprintf (DEBUG_tcg,"TCGBIOS: Asserting physical presence failed.\n" ); +return rc; + } + + rc =build_and_send_cmd (TPM_ORD_ForceClear, +NULL ,0 ,NULL ,10 , &returnCode); + +dprintf (DEBUG_tcg,"Return code from TPM_ForceClear() = 0x%08x\n" , + returnCode); + +if (rc || returnCode) + goto err_exit; + +if (!enable_activate_after) { +if (verbose) +printf ("Owner successfully cleared.\n" +"You will need to enable/activate the TPM again.\n\n" ); +return 0 ; + } + +enable_activate (1 , verbose); + +return 0 ; + +err_exit: +dprintf (DEBUG_tcg,"TCGBIOS: TPM malfunctioning (line %d).\n" , __LINE__); + + tcpa_state.tpm_working =0 ; +if (rc) +return rc; +return TCG_TCG_COMMAND_ERROR ; +} + + +static u32 +set_owner_install (int allow,int verbose) +{ +u32 rc, returnCode; +u8 has_owner; +struct tpm_permanent_flags pf; + + rc =read_has_owner (&has_owner); +if (rc) +return rc; +if (has_owner) { +if (verbose) +printf ("Must first remove owner.\n" ); +return 0 ; + } + + rc =read_permanent_flags ((char *)&pf,sizeof (pf)); +if (rc) +return rc; + +if (pf.flags[PERM_FLAG_IDX_DISABLE ]) { +if (verbose) +printf ("TPM must first be enable.\n" ); +return 0 ; + } + + rc =assert_physical_presence (); +if (rc) { +dprintf (DEBUG_tcg,"TCGBIOS: Asserting physical presence failed.\n" ); +return rc; + } + + rc =build_and_send_cmd (TPM_ORD_SetOwnerInstall, + (allow) ? CommandFlag_TRUE : + CommandFlag_FALSE, +sizeof (CommandFlag_TRUE), +NULL ,10 , &returnCode); + +dprintf (DEBUG_tcg,"Return code from TPM_SetOwnerInstall() = 0x%08x\n" , + returnCode); + +if (rc || returnCode) + goto err_exit; + +if (verbose) +printf ("Installation of owner %s.\n" , allow ?"enabled" :"disabled" ); + +return 0 ; + +err_exit: +dprintf (DEBUG_tcg,"TCGBIOS: TPM malfunctioning (line %d).\n" , __LINE__); + tcpa_state.tpm_working =0 ; +if (rc) +return rc; +return TCG_TCG_COMMAND_ERROR ; +} + + +static void +show_tpm_state (void ) +{ +struct tpm_permanent_flags pf; +u8 has_owner; + +if (read_permanent_flags ((char *)&pf,sizeof (pf)) || +read_has_owner (&has_owner)) +return ; + +printf ("TPM is " ); + +if (pf.flags[PERM_FLAG_IDX_DISABLE ]) +printf ("disabled" ); +else +printf ("enabled" ); + +if (pf.flags[PERM_FLAG_IDX_DEACTIVATED ]) +printf (", deactivated" ); +else +printf (", active" ); + +if (has_owner) +printf (" and has an owner.\n" ); +else { +printf (", does not have an owner " ); +if (pf.flags[PERM_FLAG_IDX_OWNERSHIP ]) +printf ("but one can be installed.\n" ); +else +printf ("and an owner cannot be installed.\n" ); + } + +} + + +static u32 +tcpa_process_cfg (const tpm_bios_cfg_t *cfg,int verbose) +{ +u32 rc =0 ; + +switch (cfg->op) { +case 0 :/* no-op */ +break ; + +case 1 : + rc =disable_tpm (0 , verbose); +break ; + +case 2 : + rc =disable_tpm (1 , verbose); +break ; + +case 3 : + rc =deactivate_tpm (0 ,1 , verbose); +break ; + +case 4 : + rc =deactivate_tpm (1 ,1 , verbose); +break ; + +case 5 : + rc =force_clear (1 ,0 , verbose); +break ; + +case 6 : + rc =set_owner_install (1 , verbose); +break ; + +case 7 : + rc =set_owner_install (0 , verbose); +break ; + +default : +break ; + } + +if (rc) +printf ("Op %d: An error occurred: 0x%x\n" , cfg->op, rc); + +return rc; +} + + +void +tcpa_menu (void ) +{ +if (!CONFIG_TCGBIOS ) +return ; + +int show_menu =1 ; +int scan_code; +u32 rc; +tpm_bios_cfg_t cfg = { + .op =0 , + }; + +while (get_keystroke (0 ) >=0 ) + ; +wait_threads (); + +for (;;) { +if (show_menu) { +printf ("1. Enable TPM\n" +"2. Disable TPM\n" +"3. Activate TPM\n" +"4. Deactivate TPM\n" +"5. Clear ownership\n" +"6. Allow installation of owner\n" +"7. Prevent installation of owner\n" +"Escape for previous menu.\n" ); + show_menu =0 ; +show_tpm_state (); + } + + cfg.op =0 ; + + scan_code =get_keystroke (1000 ); + +switch (scan_code) { +case 1 : +// ESC +return ; + case 2 ... 8: + cfg.op = scan_code -1 ; +break ; +default : +continue ; + } + + rc =tcpa_process_cfg (&cfg,1 ); + +if (rc) +printf ("An error occurred: 0x%x\n" , rc); + + show_menu =1 ; + } +} + +
src/boot.c +9 -1
@@ -364,11 +364,19 @@ interactive_bootmenu(void)while (get_keystroke (0 ) >=0 ) ; -printf ("Press F12 for boot menu.\n\n" ); +again: +printf ("Press F12 for boot menu.\n" ); +if (has_working_tpm ()) +printf ("Press F11 to TPM menu.\n" ); +printf ("\n" );enable_bootsplash ();int scan_code =get_keystroke (CONFIG_BOOTMENU_WAIT );disable_bootsplash (); +if (has_working_tpm () && scan_code ==0x85 ) { +tcpa_menu (); + goto again; + }if (scan_code !=0x86 )/* not F12 */ return ;
src/tcgbios.h +1 -0
@@ -376,6 +376,7 @@ u32 tcpa_ipl(enum ipltype bootcd, constu32 tcpa_start_option_rom_scan (void );u32 tcpa_option_rom (const void *addr,u32 len);u32 tcpa_smbios_measure (void ); +void tcpa_menu (void );#endif /* TCGBIOS_H */
PATCH v2 8/8 Optional tests for the TIS interface
In reply to Stefan BergerThis patch adds an optional test suite (CONFIG_TIS_TEST) for the TIS interface to SeaBIOS. If compiled into the BIOS, it can be invoked through the TPM-specific menu item 8.
1. Enable TPM
2. Disable TPM
3. Activate TPM
4. Deactivate TPM
5. Clear ownership
6. Allow installation of owner
7. Prevent installation of owner
8. TIS test
I would like to see this code become part of the SeaBIOS code base but I understand that a test suite in a BIOS is not the right place... Nevertheless, for testing the TIS emulation in Qemu, I am posting it here. The test suite fills up the available BIOS space from 92.6% at the previous patch to 98.4%.
--- Makefile | 2 src/Kconfig | 7 src/tcgbios.c | 34 +- src/tis_test.c | 846 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tis_test.h | 52 +++ 5 files changed, 932 insertions(+), 9 deletions(-)
Makefile +1 -1
@@ -20,7 +20,7 @@ SRC16=$(SRCBOTH) system.c disk.c font.c SRC32FLAT=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \ acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \ lzmadecode.c bootsplash.c jpeg.c usb-hub.c paravirt.c dev-i440fx.c \ - pci_region.c tcgbios.c tpm_drivers.c sha1.c + pci_region.c tcgbios.c tpm_drivers.c sha1.c tis_test.c SRC32SEG=util.c output.c pci.c pcibios.c apm.c stacks.c cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc \
src/tcgbios.c +26 -8
@@ -18,6 +18,9 @@#include "sha1.h" // sha1 #include "smbios.h" // get_smbios_entry_point +#ifdef CONFIG_TIS_TEST +#include "tis_test.h" +#endif static const u8 Startup_ST_CLEAR[2 ] = {0x00 ,TPM_ST_CLEAR };static const u8 Startup_ST_STATE[2 ] = {0x00 ,TPM_ST_STATE };
@@ -696,6 +699,10 @@ pass_through_to_tpm(struct pttti *pttti, iovec[1 ].data =NULL ; iovec[1 ].length =0 ; +#ifdef CONFIG_TIS_TEST + locty = pttti->reserved; +#endif + rc =transmit (locty, iovec, pttto->tpmopout, &resbuflen);if (rc) goto err_exit;
@@ -1731,26 +1738,29 @@ err_exit: } -static void +static int show_tpm_state (void ) { +int state =0 ;struct tpm_permanent_flags pf;u8 has_owner;if (read_permanent_flags ((char *)&pf,sizeof (pf)) ||read_has_owner (&has_owner)) -return ; +return ~0 ;printf ("TPM is " ); -if (pf.flags[PERM_FLAG_IDX_DISABLE ]) +if (pf.flags[PERM_FLAG_IDX_DISABLE ]) {printf ("disabled" ); -else + state |=1 <<PERM_FLAG_IDX_DISABLE ; + }else printf ("enabled" ); -if (pf.flags[PERM_FLAG_IDX_DEACTIVATED ]) +if (pf.flags[PERM_FLAG_IDX_DEACTIVATED ]) {printf (", deactivated" ); -else + state |=1 <<PERM_FLAG_IDX_DEACTIVATED ; + }else printf (", active" );if (has_owner)
@@ -1763,6 +1773,7 @@ show_tpm_state(void)printf ("and an owner cannot be installed.\n" ); } +return state; }
@@ -1821,7 +1832,7 @@ tcpa_menu(void)return ;int show_menu =1 ; -int scan_code; +int scan_code, state;u32 rc;tpm_bios_cfg_t cfg = { .op =0 ,
@@ -1840,9 +1851,12 @@ tcpa_menu(void) "5. Clear ownership\n" "6. Allow installation of owner\n" "7. Prevent installation of owner\n" +#ifdef CONFIG_TIS_TEST + "8. TIS test\n" +#endif "Escape for previous menu.\n"); show_menu =0 ; -show_tpm_state (); + state =show_tpm_state (); } cfg.op =0 ;
@@ -1856,6 +1870,10 @@ tcpa_menu(void) case 2 ... 8: cfg.op = scan_code -1 ;break ; +#ifdef CONFIG_TIS_TEST +case 9 : +tis_test (state); +#endif default :continue ; }
src/tis_test.c new file +846 -0
846 lines not on this page · raw message
src/tis_test.h new file +52 -0
52 lines not on this page · raw message
src/Kconfig +7 -0
@@ -330,6 +330,13 @@ menu "BIOS interfaces" Either you may use the TPM for SHA1 calculations or use the internal sha1 algorithm to do it (faster). + config TIS_TEST + depends on TCGBIOS + bool "TPM TIS test" + default n + help + Test cases for the TPM TIS interface + endmenu menu "BIOS Tables"
PATCH v2 2/8 Provide ACPI SSDT table for TPM device + S3 resume support
In reply to Stefan BergerThis patch provides ACPI support for the TPM device. It probes for the TPM device and only if a TPM device is found then the TPM's SSDT and TCPA table are created. This patch also connects them to the RSDT.
Since the logging area in the TCPA table requires 64kb, the memory reserved for ACPI tables (config.h) is increased to 96kb.
This patch requires the subsequent patch for it to compile and work.
The IRQ description in the TPM's SSDT is commented since it will be 'safer' to run the TPM in polling mode - the Linux TPM TIS driver for example has too many issues when run in interrupt mode.
The description of the TCPA (client) table can be found here:
The compiled SSDT description is also part of this patch.
v2:
- Increasing the CONFIG_MAX_HIGHTABLE to 96kb
- Adding cut-down tcgbios.c|h to keep SeaBIOS compiling
- Build tpm_drivers.c and tcgbios.c
-> TPM's SSDT and TCPA tables are now visible in Linux
--- Makefile | 11 ++++++- src/acpi-tpm-ssdt.dsl | 22 +++++++++++++++ src/acpi-tpm-ssdt.hex | 26 ++++++++++++++++++ src/acpi.c | 41 ++++++++++++++++++++++++++++ src/acpi.h | 20 +++++++++++++ src/config.h | 2 - src/tcgbios.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/tcgbios.h | 57 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 248 insertions(+), 3 deletions(-)
src/acpi-tpm-ssdt.dsl new file +22 -0
@@ -0,0 +1,22 @@ +DefinitionBlock ( + "acpi-tpm-ssdt.aml",// Output Filename + "SSDT", // Signature + 0x01, // SSDT Compliance Revision + "BXPC", // OEMID + "BXSSDT", // TABLE ID + 0x1 // OEM Revision + ) +{ + /* TPM with emulated TPM TIS interface */ + Device (TPM) { + Name (_HID, EisaID ("PNP0C31")) + Name (_CRS, ResourceTemplate () + { + Memory32Fixed (ReadWrite, 0xFED40000, 0x00005000) + //IRQNoFlags () {11} + }) + Method (_STA, 0, NotSerialized) { + Return (0x0F) + } + } +}
src/acpi-tpm-ssdt.hex new file +26 -0
@@ -0,0 +1,26 @@ +/* + * + * Intel ACPI Component Architecture + * ASL Optimizing Compiler version 20101013-64 [Nov 21 2010] + * Copyright (c) 2000 - 2010 Intel Corporation + * + * Compilation of "out/.dsl.i" - Mon Apr 4 15:44:33 2011 + * + * C source code output + * AML code block contains 0x56 bytes + * + */ +unsigned char AmlCode_TPM[] = +{ + 0x53,0x53,0x44,0x54,0x56,0x00,0x00,0x00, /* 00000000 "SSDTV..." */ + 0x01,0x13,0x42,0x58,0x50,0x43,0x00,0x00, /* 00000008 "..BXPC.." */ + 0x42,0x58,0x53,0x53,0x44,0x54,0x00,0x00, /* 00000010 "BXSSDT.." */ + 0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C, /* 00000018 "....INTL" */ + 0x13,0x10,0x10,0x20,0x5B,0x82,0x30,0x54, /* 00000020 "... [.0T" */ + 0x50,0x4D,0x5F,0x08,0x5F,0x48,0x49,0x44, /* 00000028 "PM_._HID" */ + 0x0C,0x41,0xD0,0x0C,0x31,0x08,0x5F,0x43, /* 00000030 ".A..1._C" */ + 0x52,0x53,0x11,0x11,0x0A,0x0E,0x86,0x09, /* 00000038 "RS......" */ + 0x00,0x01,0x00,0x00,0xD4,0xFE,0x00,0x50, /* 00000040 ".......P" */ + 0x00,0x00,0x79,0x00,0x14,0x09,0x5F,0x53, /* 00000048 "..y..._S" */ + 0x54,0x41,0x00,0xA4,0x0A,0x0F /* 00000050 "TA...." */ +};
Makefile +9 -2
@@ -20,7 +20,7 @@ SRC16=$(SRCBOTH) system.c disk.c font.c SRC32FLAT=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \ acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \ lzmadecode.c bootsplash.c jpeg.c usb-hub.c paravirt.c dev-i440fx.c \ - pci_region.c + pci_region.c tcgbios.c tpm_drivers.c SRC32SEG=util.c output.c pci.c pcibios.c apm.c stacks.c cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc \
@@ -192,13 +192,20 @@ $(OUT)vgabios.bin: $(OUT)vgabios.bin.raw $(Q)./tools/buildrom.py $< $@ ####### dsdt build rules +src/acpi-tpm-ssdt.hex: src/acpi-tpm-ssdt.dsl + @echo "Compiling TPM SSDT" + $(Q)cpp -P $< > $(OUT)$*.dsl.i + $(Q)iasl -tc -p $(OUT)$* $(OUT)$*.dsl.i + $(Q)cp $(OUT)$*.hex $@ + $(Q)sed -i 's/AmlCode/AmlCode_TPM/' $@ + src/%.hex: src/%.dsl @echo "Compiling DSDT" $(Q)cpp -P $< > $(OUT)$*.dsl.i $(Q)iasl -tc -p $(OUT)$* $(OUT)$*.dsl.i $(Q)cp $(OUT)$*.hex $@ -$(OUT)ccode32flat.o: src/acpi-dsdt.hex +$(OUT)ccode32flat.o: src/acpi-dsdt.hex src/acpi-tpm-ssdt.hex ####### Kconfig rules export HOSTCC := $(CC)
src/acpi.c +41 -0
59 lines not on this page · raw message
src/acpi.h +20 -0
@@ -98,4 +98,24 @@ struct fadt_descriptor_rev1#endif } PACKED; + +struct rsdt_descriptor { +ACPI_TABLE_HEADER_DEF + u32 entry[1]; +}PACKED ; + +#define TCPA_SIGNATURE 0x41504354 +struct tcpa_descriptor_rev2 +{ +ACPI_TABLE_HEADER_DEF + u16 platform_class; +u32 laml; +u64 lasa; +}PACKED ; + +/* TCPA ACPI definitions */ +#define TCPA_ACPI_CLASS_CLIENT 0 +#define TCPA_ACPI_CLASS_SERVER 1 + +#endif // acpi.h
src/config.h +1 -1
@@ -26,7 +26,7 @@// Space to reserve in f-segment for dynamic allocations #define CONFIG_MAX_BIOSTABLE 2048// Space to reserve in high-memory for tables -#define CONFIG_MAX_HIGHTABLE (64*1024) +#define CONFIG_MAX_HIGHTABLE (96*1024)// Largest supported externaly facing drive id #define CONFIG_MAX_EXTDRIVE 16
src/tcgbios.c new file +72 -0
72 lines not on this page · raw message
src/tcgbios.h new file +57 -0
57 lines not on this page · raw message
Quoted 2 lines · Stefan Berger
The following set of patches add TPM and Trusted Computing support to SeaBIOS. In particular the patches add:
Thanks Stefan.
In general, it looks okay to me. Since this depends on a qemu change, though, I'd like to see a qemu/kvm maintaner ack (or the dependent changes committed in qemu) before committing to SeaBIOS.
Some minor nits:
Patch 4:
Review
Quoted 2 lines
(quoted diff) +2
-0+// TCG BIOS extensions +void tcpa_interrupt_handler16(struct bregs *regs);
This looks stale.
Patch 5:
Review
Quoted 6 lines
(quoted diff) +6
-0+#else + +static u32 +sha1_calc(const u8 *data, u32 length, u8 *hash) +{ + return sha1(data, length, hash);
I'd prefer to avoid #ifdefs - it should be possible to do something like:
sha1_calc(...) {
if (CONFIG_TPM_FOR_SHA1)
return tpm_sha1_calc(...);
return sha1(...);
}Finally, can you briefly describe what services that require TPM support will now be available to end-users with this series?
-Kevin
Quoted 10 lines · Kevin O'Connor
Quoted 2 lines · Stefan Berger
The following set of patches add TPM and Trusted Computing support to SeaBIOS. In particular the patches add:
Thanks Stefan.
In general, it looks okay to me. Since this depends on a qemu change, though, I'd like to see a qemu/kvm maintaner ack (or the dependent changes committed in qemu) before committing to SeaBIOS.
Quoted 6 lines
Some minor nits:
Patch 4:
Review
Quoted 2 lines
(quoted diff) +2
-0+// TCG BIOS extensions +void tcpa_interrupt_handler16(struct bregs *regs);This looks stale.
Right... missed that one.
Quoted 10 lines
Patch 5:
Review
Quoted 6 lines
(quoted diff) +6
-0+#else + +static u32 +sha1_calc(const u8 *data, u32 length, u8 *hash) +{ + return sha1(data, length, hash);I'd prefer to avoid #ifdefs - it should be possible to do something like:
Quoted 6 lines
sha1_calc(...) {
if (CONFIG_TPM_FOR_SHA1)
return tpm_sha1_calc(...);
return sha1(...);
}Will fix.
Quoted 3 lines
Finally, can you briefly describe what services that require TPM support will now be available to end-users with this series?
I'll send a V3 tomorrow explaining in each patch what the effect should be.
Stefan