[SeaBIOS] [PATCH v8 6/8] Add a menu for TPM control

Stefan Berger stefanb at us.ibm.com
Wed Jul 2 17:38:50 CEST 2014


This 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.

v6:
  - passing durations of commands to the transmission function

v5:
  - fixed an indentation

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

Signed-off-by: Stefan Berger <stefanb at linux.vnet.ibm.com>
---
 src/boot.c    |  14 +-
 src/tcgbios.c | 568 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/tcgbios.h |  31 ++++
 src/util.h    |   1 +
 4 files changed, 611 insertions(+), 3 deletions(-)

diff --git a/src/boot.c b/src/boot.c
index 91edf9c..5533529 100644
--- a/src/boot.c
+++ b/src/boot.c
@@ -427,7 +427,7 @@ get_raw_keystroke(void)
 }
 
 // Read a keystroke - waiting up to 'msec' milliseconds.
-static int
+int
 get_keystroke(int msec)
 {
     u32 end = irqtimer_calc(msec);
@@ -461,13 +461,21 @@ interactive_bootmenu(void)
 
     char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
     int menukey = romfile_loadint("etc/boot-menu-key", 0x86);
-    printf("%s", bootmsg ?: "\nPress F12 for boot menu.\n\n");
-    free(bootmsg);
+again:
+    printf("%s", bootmsg ?: "\nPress F12 for boot menu.\n");
+    if (detected_tpm())
+        printf("Press F11 to TPM menu.\n");
+    printf("\n");
 
     u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
     enable_bootsplash();
     int scan_code = get_keystroke(menutime);
     disable_bootsplash();
+    if (detected_tpm() && scan_code == 0x85) {
+         tcpa_menu();
+         goto again;
+    }
+    free(bootmsg);
     if (scan_code != menukey)
         return;
 
diff --git a/src/tcgbios.c b/src/tcgbios.c
index bfa46e0..5be9bcd 100644
--- a/src/tcgbios.c
+++ b/src/tcgbios.c
@@ -23,6 +23,7 @@
 #include "std/acpi.h"  // RSDP_SIGNATURE, rsdt_descriptor
 #include "sha1.h" // sha1
 #include "std/smbios.h"
+#include "stacks.h" // wait_threads
 
 
 static const u8 Startup_ST_CLEAR[2] = { 0x00, TPM_ST_CLEAR };
@@ -41,6 +42,11 @@ static const u8 GetCapability_Permanent_Flags[12] = {
     0x00, 0x00, 0x01, 0x08
 };
 
+static const u8 GetCapability_STClear_Flags[12] = {
+    0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04,
+    0x00, 0x00, 0x01, 0x09
+};
+
 static const u8 GetCapability_OwnerAuth[12] = {
     0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04,
     0x00, 0x00, 0x01, 0x11
@@ -88,6 +94,10 @@ static tcpa_state_t tcpa_state = {
     .tpm_driver_to_use = TPM_INVALID_DRIVER,
 };
 
+typedef struct {
+    u8  op;
+} tpm_bios_cfg_t;
+
 
 /********************************************************
   Extensions for TCG-enabled BIOS
@@ -1421,6 +1431,564 @@ tcpa_ipl(enum ipltype bootcd, const u8 *addr, u32 length)
 }
 
 
+static u32
+read_stclear_flags(char *buf, int buf_len)
+{
+    u32 rc;
+    u32 returnCode;
+    struct tpm_res_getcap_stclear_flags stcf;
+
+    memset(buf, 0x0, buf_len);
+
+    rc = build_and_send_cmd(TPM_ORD_GetCapability,
+                            GetCapability_STClear_Flags,
+                            sizeof(GetCapability_STClear_Flags),
+                            (u8 *)&stcf,
+                            sizeof(struct tpm_res_getcap_stclear_flags),
+                            &returnCode, TPM_DURATION_TYPE_SHORT);
+
+    dprintf(DEBUG_tcg, "TCGBIOS: Return code from TPM_GetCapability() "
+            "= 0x%08x\n", returnCode);
+
+    if (rc || returnCode)
+        goto err_exit;
+
+    memcpy(buf, &stcf.stclear_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
+assert_physical_presence(int verbose)
+{
+    u32 rc = 0;
+    u32 returnCode;
+    struct tpm_stclear_flags stcf;
+
+    rc = read_stclear_flags((char *)&stcf, sizeof(stcf));
+    if (rc) {
+        dprintf(DEBUG_tcg,
+                "Error reading STClear flags: 0x%08x\n", rc);
+        return rc;
+    }
+
+    if (stcf.flags[STCLEAR_FLAG_IDX_PHYSICAL_PRESENCE])
+        /* physical presence already asserted */
+        return 0;
+
+    rc = build_and_send_cmd(TPM_ORD_PhysicalPresence,
+                            PhysicalPresence_CMD_ENABLE,
+                            sizeof(PhysicalPresence_CMD_ENABLE),
+                            NULL, 10, &returnCode, TPM_DURATION_TYPE_SHORT);
+
+    dprintf(DEBUG_tcg,
+           "Return code from TSC_PhysicalPresence(CMD_ENABLE) = 0x%08x\n",
+           returnCode);
+
+    if (rc || returnCode) {
+        if (verbose)
+            printf("Error: Could not enable physical presence.\n\n");
+        goto err_exit;
+    }
+
+    rc = build_and_send_cmd(TPM_ORD_PhysicalPresence,
+                            PhysicalPresence_PRESENT,
+                            sizeof(PhysicalPresence_PRESENT),
+                            NULL, 10, &returnCode, TPM_DURATION_TYPE_SHORT);
+
+    dprintf(DEBUG_tcg,
+           "Return code from TSC_PhysicalPresence(PRESENT) = 0x%08x\n",
+           returnCode);
+
+    if (rc || returnCode) {
+        if (verbose)
+            printf("Error: Could not set presence flag.\n\n");
+        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, TPM_DURATION_TYPE_SHORT);
+
+    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, TPM_DURATION_TYPE_SHORT);
+
+    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(verbose);
+    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,
+                            TPM_DURATION_TYPE_SHORT);
+    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(verbose);
+    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, TPM_DURATION_TYPE_SHORT);
+
+    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);
+        }
+        extern void reset_vector(void) __noreturn;
+        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(verbose);
+    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,
+                            TPM_DURATION_TYPE_SHORT);
+
+    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(verbose);
+    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, TPM_DURATION_TYPE_SHORT);
+
+    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 scan_code;
+    u32 rc;
+    tpm_bios_cfg_t cfg = {
+        .op  = 0,
+    };
+
+    while (get_keystroke(0) >= 0)
+        ;
+    wait_threads();
+
+    for (;;) {
+        if (has_working_tpm()) {
+            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");
+        } else {
+            printf("TPM is not working correctly.\n");
+        }
+
+        printf("Escape for previous menu.\n");
+
+        if (has_working_tpm()) {
+            show_tpm_state();
+        }
+
+        cfg.op = 0;
+
+        while ((scan_code = get_keystroke(1000)) == ~0)
+            ;
+
+        switch (scan_code) {
+        case 1:
+            // ESC
+            return;
+        case 2 ... 8:
+            cfg.op = scan_code - 1;
+            break;
+        default:
+            continue;
+        }
+
+        if (has_working_tpm()) {
+            rc = tcpa_process_cfg(&cfg, 1);
+
+            if (rc)
+                printf("An error occurred: 0x%x\n", rc);
+        }
+    }
+}
+
 
 u32
 tcpa_s3_resume(void)
diff --git a/src/tcgbios.h b/src/tcgbios.h
index 1965a05..b01300a 100644
--- a/src/tcgbios.h
+++ b/src/tcgbios.h
@@ -323,6 +323,36 @@ struct tpm_res_getcap_perm_flags {
 } PACKED;
 
 
+struct tpm_req_getcap_stclear_flags {
+    TPM_REQ_HEADER
+    u32    capArea;
+    u32    subCapSize;
+    u32    subCap;
+} PACKED;
+
+
+struct tpm_stclear_flags {
+    u16    tag;
+    u8     flags[5];
+} PACKED;
+
+
+enum stclearFlagsIndex {
+    STCLEAR_FLAG_IDX_DEACTIVATED = 0,
+    STCLEAR_FLAG_IDX_DISABLE_FORCE_CLEAR,
+    STCLEAR_FLAG_IDX_PHYSICAL_PRESENCE,
+    STCLEAR_FLAG_IDX_PHYSICAL_PRESENCE_LOCK,
+    STCLEAR_FLAG_IDX_GLOBAL_LOCK,
+};
+
+
+struct tpm_res_getcap_stclear_flags {
+    TPM_RSP_HEADER
+    u32    size;
+    struct tpm_stclear_flags stclear_flags;
+} PACKED;
+
+
 struct tpm_res_getcap_ownerauth {
     TPM_RSP_HEADER
     u32    size;
@@ -390,6 +420,7 @@ 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);
+void tcpa_menu(void);
 
 
 #endif /* TCGBIOS_H */
diff --git a/src/util.h b/src/util.h
index b795ffb..68d5026 100644
--- a/src/util.h
+++ b/src/util.h
@@ -36,6 +36,7 @@ int bootprio_find_pci_rom(struct pci_device *pci, int instance);
 int bootprio_find_named_rom(const char *name, int instance);
 struct usbdevice_s;
 int bootprio_find_usb(struct usbdevice_s *usbdev, int lun);
+int get_keystroke(int msec);
 
 // bootsplash.c
 void enable_vga_console(void);
-- 
1.8.1.4




More information about the SeaBIOS mailing list