11e8a1faeSThomas Huth /* 21e8a1faeSThomas Huth * QTest TPM utilities 31e8a1faeSThomas Huth * 41e8a1faeSThomas Huth * Copyright (c) 2018 IBM Corporation 51e8a1faeSThomas Huth * Copyright (c) 2018 Red Hat, Inc. 61e8a1faeSThomas Huth * 71e8a1faeSThomas Huth * Authors: 81e8a1faeSThomas Huth * Stefan Berger <stefanb@linux.vnet.ibm.com> 91e8a1faeSThomas Huth * Marc-André Lureau <marcandre.lureau@redhat.com> 101e8a1faeSThomas Huth * 111e8a1faeSThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or later. 121e8a1faeSThomas Huth * See the COPYING file in the top-level directory. 131e8a1faeSThomas Huth */ 141e8a1faeSThomas Huth 151e8a1faeSThomas Huth #include "qemu/osdep.h" 161e8a1faeSThomas Huth 171e8a1faeSThomas Huth #include "hw/acpi/tpm.h" 18a2ce7dbdSPaolo Bonzini #include "libqos/libqtest.h" 191e8a1faeSThomas Huth #include "tpm-util.h" 201e8a1faeSThomas Huth #include "qapi/qmp/qdict.h" 211e8a1faeSThomas Huth 221e8a1faeSThomas Huth void tpm_util_crb_transfer(QTestState *s, 231e8a1faeSThomas Huth const unsigned char *req, size_t req_size, 241e8a1faeSThomas Huth unsigned char *rsp, size_t rsp_size) 251e8a1faeSThomas Huth { 261e8a1faeSThomas Huth uint64_t caddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR); 271e8a1faeSThomas Huth uint64_t raddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR); 281e8a1faeSThomas Huth 291e8a1faeSThomas Huth qtest_writeb(s, TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1); 301e8a1faeSThomas Huth 311e8a1faeSThomas Huth qtest_memwrite(s, caddr, req, req_size); 321e8a1faeSThomas Huth 331e8a1faeSThomas Huth uint32_t sts, start = 1; 341e8a1faeSThomas Huth uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; 351e8a1faeSThomas Huth qtest_writel(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start); 361e8a1faeSThomas Huth while (true) { 371e8a1faeSThomas Huth start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START); 381e8a1faeSThomas Huth if ((start & 1) == 0) { 391e8a1faeSThomas Huth break; 401e8a1faeSThomas Huth } 411e8a1faeSThomas Huth if (g_get_monotonic_time() >= end_time) { 421e8a1faeSThomas Huth break; 431e8a1faeSThomas Huth } 441e8a1faeSThomas Huth }; 451e8a1faeSThomas Huth start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START); 461e8a1faeSThomas Huth g_assert_cmpint(start & 1, ==, 0); 471e8a1faeSThomas Huth sts = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS); 481e8a1faeSThomas Huth g_assert_cmpint(sts & 1, ==, 0); 491e8a1faeSThomas Huth 501e8a1faeSThomas Huth qtest_memread(s, raddr, rsp, rsp_size); 511e8a1faeSThomas Huth } 521e8a1faeSThomas Huth 531e8a1faeSThomas Huth void tpm_util_tis_transfer(QTestState *s, 541e8a1faeSThomas Huth const unsigned char *req, size_t req_size, 551e8a1faeSThomas Huth unsigned char *rsp, size_t rsp_size) 561e8a1faeSThomas Huth { 571e8a1faeSThomas Huth uint32_t sts; 581e8a1faeSThomas Huth uint16_t bcount; 591e8a1faeSThomas Huth size_t i; 601e8a1faeSThomas Huth 611e8a1faeSThomas Huth /* request use of locality 0 */ 621e8a1faeSThomas Huth qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE); 631e8a1faeSThomas Huth qtest_writel(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_COMMAND_READY); 641e8a1faeSThomas Huth 651e8a1faeSThomas Huth sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS)); 661e8a1faeSThomas Huth bcount = (sts >> 8) & 0xffff; 671e8a1faeSThomas Huth g_assert_cmpint(bcount, >=, req_size); 681e8a1faeSThomas Huth 691e8a1faeSThomas Huth /* transmit command */ 701e8a1faeSThomas Huth for (i = 0; i < req_size; i++) { 711e8a1faeSThomas Huth qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO), req[i]); 721e8a1faeSThomas Huth } 731e8a1faeSThomas Huth 741e8a1faeSThomas Huth /* start processing */ 751e8a1faeSThomas Huth qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_TPM_GO); 761e8a1faeSThomas Huth 771e8a1faeSThomas Huth uint64_t end_time = g_get_monotonic_time() + 50 * G_TIME_SPAN_SECOND; 781e8a1faeSThomas Huth do { 791e8a1faeSThomas Huth sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS)); 801e8a1faeSThomas Huth if ((sts & TPM_TIS_STS_DATA_AVAILABLE) != 0) { 811e8a1faeSThomas Huth break; 821e8a1faeSThomas Huth } 831e8a1faeSThomas Huth } while (g_get_monotonic_time() < end_time); 841e8a1faeSThomas Huth 851e8a1faeSThomas Huth sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS)); 861e8a1faeSThomas Huth bcount = (sts >> 8) & 0xffff; 871e8a1faeSThomas Huth 881e8a1faeSThomas Huth memset(rsp, 0, rsp_size); 891e8a1faeSThomas Huth for (i = 0; i < bcount; i++) { 901e8a1faeSThomas Huth rsp[i] = qtest_readb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO)); 911e8a1faeSThomas Huth } 921e8a1faeSThomas Huth 931e8a1faeSThomas Huth /* relinquish use of locality 0 */ 941e8a1faeSThomas Huth qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS), 951e8a1faeSThomas Huth TPM_TIS_ACCESS_ACTIVE_LOCALITY); 961e8a1faeSThomas Huth } 971e8a1faeSThomas Huth 981e8a1faeSThomas Huth void tpm_util_startup(QTestState *s, tx_func *tx) 991e8a1faeSThomas Huth { 1001e8a1faeSThomas Huth unsigned char buffer[1024]; 101*ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_startup[] = 1021e8a1faeSThomas Huth "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00"; 103*ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_startup_resp[] = 1041e8a1faeSThomas Huth "\x80\x01\x00\x00\x00\x0a\x00\x00\x00\x00"; 1051e8a1faeSThomas Huth 1061e8a1faeSThomas Huth tx(s, tpm_startup, sizeof(tpm_startup), buffer, sizeof(buffer)); 1071e8a1faeSThomas Huth 1081e8a1faeSThomas Huth g_assert_cmpmem(buffer, sizeof(tpm_startup_resp), 1091e8a1faeSThomas Huth tpm_startup_resp, sizeof(tpm_startup_resp)); 1101e8a1faeSThomas Huth } 1111e8a1faeSThomas Huth 1121e8a1faeSThomas Huth void tpm_util_pcrextend(QTestState *s, tx_func *tx) 1131e8a1faeSThomas Huth { 1141e8a1faeSThomas Huth unsigned char buffer[1024]; 115*ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_pcrextend[] = 1161e8a1faeSThomas Huth "\x80\x02\x00\x00\x00\x41\x00\x00\x01\x82\x00\x00\x00\x0a\x00\x00" 1171e8a1faeSThomas Huth "\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00" 1181e8a1faeSThomas Huth "\x0b\x74\x65\x73\x74\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" 1191e8a1faeSThomas Huth "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" 1201e8a1faeSThomas Huth "\x00"; 1211e8a1faeSThomas Huth 122*ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_pcrextend_resp[] = 1231e8a1faeSThomas Huth "\x80\x02\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" 1241e8a1faeSThomas Huth "\x01\x00\x00"; 1251e8a1faeSThomas Huth 1261e8a1faeSThomas Huth tx(s, tpm_pcrextend, sizeof(tpm_pcrextend), buffer, sizeof(buffer)); 1271e8a1faeSThomas Huth 1281e8a1faeSThomas Huth g_assert_cmpmem(buffer, sizeof(tpm_pcrextend_resp), 1291e8a1faeSThomas Huth tpm_pcrextend_resp, sizeof(tpm_pcrextend_resp)); 1301e8a1faeSThomas Huth } 1311e8a1faeSThomas Huth 1321e8a1faeSThomas Huth void tpm_util_pcrread(QTestState *s, tx_func *tx, 1331e8a1faeSThomas Huth const unsigned char *exp_resp, size_t exp_resp_size) 1341e8a1faeSThomas Huth { 1351e8a1faeSThomas Huth unsigned char buffer[1024]; 136*ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_pcrread[] = 1371e8a1faeSThomas Huth "\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b" 1381e8a1faeSThomas Huth "\x03\x00\x04\x00"; 1391e8a1faeSThomas Huth 1401e8a1faeSThomas Huth tx(s, tpm_pcrread, sizeof(tpm_pcrread), buffer, sizeof(buffer)); 1411e8a1faeSThomas Huth 142df8a7568SStefan Berger /* skip pcrUpdateCounter (14th byte) in comparison */ 143df8a7568SStefan Berger g_assert(exp_resp_size >= 15); 144df8a7568SStefan Berger g_assert_cmpmem(buffer, 13, exp_resp, 13); 145df8a7568SStefan Berger g_assert_cmpmem(&buffer[14], exp_resp_size - 14, 146df8a7568SStefan Berger &exp_resp[14], exp_resp_size - 14); 1471e8a1faeSThomas Huth } 1481e8a1faeSThomas Huth 1491e8a1faeSThomas Huth bool tpm_util_swtpm_has_tpm2(void) 1501e8a1faeSThomas Huth { 1511e8a1faeSThomas Huth bool has_tpm2 = false; 1521e8a1faeSThomas Huth char *out = NULL; 1531e8a1faeSThomas Huth static const char *argv[] = { 1541e8a1faeSThomas Huth "swtpm", "socket", "--help", NULL 1551e8a1faeSThomas Huth }; 1561e8a1faeSThomas Huth 1571e8a1faeSThomas Huth if (!g_spawn_sync(NULL /* working_dir */, 1581e8a1faeSThomas Huth (char **)argv, 1591e8a1faeSThomas Huth NULL /* envp */, 1601e8a1faeSThomas Huth G_SPAWN_SEARCH_PATH, 1611e8a1faeSThomas Huth NULL /* child_setup */, 1621e8a1faeSThomas Huth NULL /* user_data */, 1631e8a1faeSThomas Huth &out, 1641e8a1faeSThomas Huth NULL /* err */, 1651e8a1faeSThomas Huth NULL /* exit_status */, 1661e8a1faeSThomas Huth NULL)) { 1671e8a1faeSThomas Huth return false; 1681e8a1faeSThomas Huth } 1691e8a1faeSThomas Huth 1701e8a1faeSThomas Huth if (strstr(out, "--tpm2")) { 1711e8a1faeSThomas Huth has_tpm2 = true; 1721e8a1faeSThomas Huth } 1731e8a1faeSThomas Huth 1741e8a1faeSThomas Huth g_free(out); 1751e8a1faeSThomas Huth return has_tpm2; 1761e8a1faeSThomas Huth } 1771e8a1faeSThomas Huth 1781e8a1faeSThomas Huth gboolean tpm_util_swtpm_start(const char *path, GPid *pid, 1791e8a1faeSThomas Huth SocketAddress **addr, GError **error) 1801e8a1faeSThomas Huth { 1811e8a1faeSThomas Huth char *swtpm_argv_tpmstate = g_strdup_printf("dir=%s", path); 1821e8a1faeSThomas Huth char *swtpm_argv_ctrl = g_strdup_printf("type=unixio,path=%s/sock", 1831e8a1faeSThomas Huth path); 1841e8a1faeSThomas Huth gchar *swtpm_argv[] = { 1851e8a1faeSThomas Huth g_strdup("swtpm"), g_strdup("socket"), 1861e8a1faeSThomas Huth g_strdup("--tpmstate"), swtpm_argv_tpmstate, 1871e8a1faeSThomas Huth g_strdup("--ctrl"), swtpm_argv_ctrl, 1881e8a1faeSThomas Huth g_strdup("--tpm2"), 1891e8a1faeSThomas Huth NULL 1901e8a1faeSThomas Huth }; 1911e8a1faeSThomas Huth gboolean succ; 1921e8a1faeSThomas Huth unsigned i; 1931e8a1faeSThomas Huth 1941e8a1faeSThomas Huth *addr = g_new0(SocketAddress, 1); 1951e8a1faeSThomas Huth (*addr)->type = SOCKET_ADDRESS_TYPE_UNIX; 1961e8a1faeSThomas Huth (*addr)->u.q_unix.path = g_build_filename(path, "sock", NULL); 1971e8a1faeSThomas Huth 1981e8a1faeSThomas Huth succ = g_spawn_async(NULL, swtpm_argv, NULL, G_SPAWN_SEARCH_PATH, 1991e8a1faeSThomas Huth NULL, NULL, pid, error); 2001e8a1faeSThomas Huth 2011e8a1faeSThomas Huth for (i = 0; swtpm_argv[i]; i++) { 2021e8a1faeSThomas Huth g_free(swtpm_argv[i]); 2031e8a1faeSThomas Huth } 2041e8a1faeSThomas Huth 2051e8a1faeSThomas Huth return succ; 2061e8a1faeSThomas Huth } 2071e8a1faeSThomas Huth 2081e8a1faeSThomas Huth void tpm_util_swtpm_kill(GPid pid) 2091e8a1faeSThomas Huth { 2101e8a1faeSThomas Huth int n; 2111e8a1faeSThomas Huth 2121e8a1faeSThomas Huth if (!pid) { 2131e8a1faeSThomas Huth return; 2141e8a1faeSThomas Huth } 2151e8a1faeSThomas Huth 2161e8a1faeSThomas Huth g_spawn_close_pid(pid); 2171e8a1faeSThomas Huth 2181e8a1faeSThomas Huth n = kill(pid, 0); 2191e8a1faeSThomas Huth if (n < 0) { 2201e8a1faeSThomas Huth return; 2211e8a1faeSThomas Huth } 2221e8a1faeSThomas Huth 2231e8a1faeSThomas Huth kill(pid, SIGKILL); 2241e8a1faeSThomas Huth } 2251e8a1faeSThomas Huth 2261e8a1faeSThomas Huth void tpm_util_migrate(QTestState *who, const char *uri) 2271e8a1faeSThomas Huth { 2281e8a1faeSThomas Huth QDict *rsp; 2291e8a1faeSThomas Huth 2301e8a1faeSThomas Huth rsp = qtest_qmp(who, 2311e8a1faeSThomas Huth "{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", 2321e8a1faeSThomas Huth uri); 2331e8a1faeSThomas Huth g_assert(qdict_haskey(rsp, "return")); 2341e8a1faeSThomas Huth qobject_unref(rsp); 2351e8a1faeSThomas Huth } 2361e8a1faeSThomas Huth 2371e8a1faeSThomas Huth void tpm_util_wait_for_migration_complete(QTestState *who) 2381e8a1faeSThomas Huth { 2391e8a1faeSThomas Huth while (true) { 2401e8a1faeSThomas Huth QDict *rsp_return; 2411e8a1faeSThomas Huth bool completed; 2421e8a1faeSThomas Huth const char *status; 2431e8a1faeSThomas Huth 2441e8a1faeSThomas Huth qtest_qmp_send(who, "{ 'execute': 'query-migrate' }"); 2451e8a1faeSThomas Huth rsp_return = qtest_qmp_receive_success(who, NULL, NULL); 2461e8a1faeSThomas Huth status = qdict_get_str(rsp_return, "status"); 2471e8a1faeSThomas Huth completed = strcmp(status, "completed") == 0; 2481e8a1faeSThomas Huth g_assert_cmpstr(status, !=, "failed"); 2491e8a1faeSThomas Huth qobject_unref(rsp_return); 2501e8a1faeSThomas Huth if (completed) { 2511e8a1faeSThomas Huth return; 2521e8a1faeSThomas Huth } 2531e8a1faeSThomas Huth usleep(1000); 2541e8a1faeSThomas Huth } 2551e8a1faeSThomas Huth } 2561e8a1faeSThomas Huth 2571e8a1faeSThomas Huth void tpm_util_migration_start_qemu(QTestState **src_qemu, 2581e8a1faeSThomas Huth QTestState **dst_qemu, 2591e8a1faeSThomas Huth SocketAddress *src_tpm_addr, 2601e8a1faeSThomas Huth SocketAddress *dst_tpm_addr, 2611e8a1faeSThomas Huth const char *miguri, 262551cabdfSEric Auger const char *ifmodel, 263551cabdfSEric Auger const char *machine_options) 2641e8a1faeSThomas Huth { 2651e8a1faeSThomas Huth char *src_qemu_args, *dst_qemu_args; 2661e8a1faeSThomas Huth 2671e8a1faeSThomas Huth src_qemu_args = g_strdup_printf( 268551cabdfSEric Auger "%s " 2691e8a1faeSThomas Huth "-chardev socket,id=chr,path=%s " 2701e8a1faeSThomas Huth "-tpmdev emulator,id=dev,chardev=chr " 2711e8a1faeSThomas Huth "-device %s,tpmdev=dev ", 272551cabdfSEric Auger machine_options ? : "", src_tpm_addr->u.q_unix.path, ifmodel); 2731e8a1faeSThomas Huth 2741e8a1faeSThomas Huth *src_qemu = qtest_init(src_qemu_args); 2751e8a1faeSThomas Huth 2761e8a1faeSThomas Huth dst_qemu_args = g_strdup_printf( 277551cabdfSEric Auger "%s " 2781e8a1faeSThomas Huth "-chardev socket,id=chr,path=%s " 2791e8a1faeSThomas Huth "-tpmdev emulator,id=dev,chardev=chr " 2801e8a1faeSThomas Huth "-device %s,tpmdev=dev " 2811e8a1faeSThomas Huth "-incoming %s", 282551cabdfSEric Auger machine_options ? : "", 2831e8a1faeSThomas Huth dst_tpm_addr->u.q_unix.path, 2841e8a1faeSThomas Huth ifmodel, miguri); 2851e8a1faeSThomas Huth 2861e8a1faeSThomas Huth *dst_qemu = qtest_init(dst_qemu_args); 2871e8a1faeSThomas Huth 2881e8a1faeSThomas Huth free(src_qemu_args); 2891e8a1faeSThomas Huth free(dst_qemu_args); 2901e8a1faeSThomas Huth } 291