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" 16*daa8bb57SThomas Huth #include <glib/gstdio.h> 171e8a1faeSThomas Huth 181e8a1faeSThomas Huth #include "hw/acpi/tpm.h" 19907b5105SMarc-André Lureau #include "libqtest.h" 201e8a1faeSThomas Huth #include "tpm-util.h" 211e8a1faeSThomas Huth #include "qapi/qmp/qdict.h" 221e8a1faeSThomas Huth 231e8a1faeSThomas Huth void tpm_util_crb_transfer(QTestState *s, 241e8a1faeSThomas Huth const unsigned char *req, size_t req_size, 251e8a1faeSThomas Huth unsigned char *rsp, size_t rsp_size) 261e8a1faeSThomas Huth { 271e8a1faeSThomas Huth uint64_t caddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR); 281e8a1faeSThomas Huth uint64_t raddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR); 291e8a1faeSThomas Huth 301e8a1faeSThomas Huth qtest_writeb(s, TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1); 311e8a1faeSThomas Huth 321e8a1faeSThomas Huth qtest_memwrite(s, caddr, req, req_size); 331e8a1faeSThomas Huth 341e8a1faeSThomas Huth uint32_t sts, start = 1; 351e8a1faeSThomas Huth uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; 361e8a1faeSThomas Huth qtest_writel(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start); 371e8a1faeSThomas Huth while (true) { 381e8a1faeSThomas Huth start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START); 391e8a1faeSThomas Huth if ((start & 1) == 0) { 401e8a1faeSThomas Huth break; 411e8a1faeSThomas Huth } 421e8a1faeSThomas Huth if (g_get_monotonic_time() >= end_time) { 431e8a1faeSThomas Huth break; 441e8a1faeSThomas Huth } 451e8a1faeSThomas Huth }; 461e8a1faeSThomas Huth start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START); 471e8a1faeSThomas Huth g_assert_cmpint(start & 1, ==, 0); 481e8a1faeSThomas Huth sts = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS); 491e8a1faeSThomas Huth g_assert_cmpint(sts & 1, ==, 0); 501e8a1faeSThomas Huth 511e8a1faeSThomas Huth qtest_memread(s, raddr, rsp, rsp_size); 521e8a1faeSThomas Huth } 531e8a1faeSThomas Huth 541e8a1faeSThomas Huth void tpm_util_tis_transfer(QTestState *s, 551e8a1faeSThomas Huth const unsigned char *req, size_t req_size, 561e8a1faeSThomas Huth unsigned char *rsp, size_t rsp_size) 571e8a1faeSThomas Huth { 581e8a1faeSThomas Huth uint32_t sts; 591e8a1faeSThomas Huth uint16_t bcount; 601e8a1faeSThomas Huth size_t i; 611e8a1faeSThomas Huth 621e8a1faeSThomas Huth /* request use of locality 0 */ 631e8a1faeSThomas Huth qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE); 641e8a1faeSThomas Huth qtest_writel(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_COMMAND_READY); 651e8a1faeSThomas Huth 661e8a1faeSThomas Huth sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS)); 671e8a1faeSThomas Huth bcount = (sts >> 8) & 0xffff; 681e8a1faeSThomas Huth g_assert_cmpint(bcount, >=, req_size); 691e8a1faeSThomas Huth 701e8a1faeSThomas Huth /* transmit command */ 711e8a1faeSThomas Huth for (i = 0; i < req_size; i++) { 721e8a1faeSThomas Huth qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO), req[i]); 731e8a1faeSThomas Huth } 741e8a1faeSThomas Huth 751e8a1faeSThomas Huth /* start processing */ 761e8a1faeSThomas Huth qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_TPM_GO); 771e8a1faeSThomas Huth 781e8a1faeSThomas Huth uint64_t end_time = g_get_monotonic_time() + 50 * G_TIME_SPAN_SECOND; 791e8a1faeSThomas Huth do { 801e8a1faeSThomas Huth sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS)); 811e8a1faeSThomas Huth if ((sts & TPM_TIS_STS_DATA_AVAILABLE) != 0) { 821e8a1faeSThomas Huth break; 831e8a1faeSThomas Huth } 841e8a1faeSThomas Huth } while (g_get_monotonic_time() < end_time); 851e8a1faeSThomas Huth 861e8a1faeSThomas Huth sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS)); 871e8a1faeSThomas Huth bcount = (sts >> 8) & 0xffff; 881e8a1faeSThomas Huth 891e8a1faeSThomas Huth memset(rsp, 0, rsp_size); 901e8a1faeSThomas Huth for (i = 0; i < bcount; i++) { 911e8a1faeSThomas Huth rsp[i] = qtest_readb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO)); 921e8a1faeSThomas Huth } 931e8a1faeSThomas Huth 941e8a1faeSThomas Huth /* relinquish use of locality 0 */ 951e8a1faeSThomas Huth qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS), 961e8a1faeSThomas Huth TPM_TIS_ACCESS_ACTIVE_LOCALITY); 971e8a1faeSThomas Huth } 981e8a1faeSThomas Huth 991e8a1faeSThomas Huth void tpm_util_startup(QTestState *s, tx_func *tx) 1001e8a1faeSThomas Huth { 1011e8a1faeSThomas Huth unsigned char buffer[1024]; 102ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_startup[] = 1031e8a1faeSThomas Huth "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00"; 104ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_startup_resp[] = 1051e8a1faeSThomas Huth "\x80\x01\x00\x00\x00\x0a\x00\x00\x00\x00"; 1061e8a1faeSThomas Huth 1071e8a1faeSThomas Huth tx(s, tpm_startup, sizeof(tpm_startup), buffer, sizeof(buffer)); 1081e8a1faeSThomas Huth 1091e8a1faeSThomas Huth g_assert_cmpmem(buffer, sizeof(tpm_startup_resp), 1101e8a1faeSThomas Huth tpm_startup_resp, sizeof(tpm_startup_resp)); 1111e8a1faeSThomas Huth } 1121e8a1faeSThomas Huth 1131e8a1faeSThomas Huth void tpm_util_pcrextend(QTestState *s, tx_func *tx) 1141e8a1faeSThomas Huth { 1151e8a1faeSThomas Huth unsigned char buffer[1024]; 116ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_pcrextend[] = 1171e8a1faeSThomas Huth "\x80\x02\x00\x00\x00\x41\x00\x00\x01\x82\x00\x00\x00\x0a\x00\x00" 1181e8a1faeSThomas Huth "\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00" 1191e8a1faeSThomas Huth "\x0b\x74\x65\x73\x74\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" 1201e8a1faeSThomas Huth "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" 1211e8a1faeSThomas Huth "\x00"; 1221e8a1faeSThomas Huth 123ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_pcrextend_resp[] = 1241e8a1faeSThomas Huth "\x80\x02\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" 1251e8a1faeSThomas Huth "\x01\x00\x00"; 1261e8a1faeSThomas Huth 1271e8a1faeSThomas Huth tx(s, tpm_pcrextend, sizeof(tpm_pcrextend), buffer, sizeof(buffer)); 1281e8a1faeSThomas Huth 1291e8a1faeSThomas Huth g_assert_cmpmem(buffer, sizeof(tpm_pcrextend_resp), 1301e8a1faeSThomas Huth tpm_pcrextend_resp, sizeof(tpm_pcrextend_resp)); 1311e8a1faeSThomas Huth } 1321e8a1faeSThomas Huth 1331e8a1faeSThomas Huth void tpm_util_pcrread(QTestState *s, tx_func *tx, 1341e8a1faeSThomas Huth const unsigned char *exp_resp, size_t exp_resp_size) 1351e8a1faeSThomas Huth { 1361e8a1faeSThomas Huth unsigned char buffer[1024]; 137ed943cc9SPhilippe Mathieu-Daudé static const unsigned char tpm_pcrread[] = 1381e8a1faeSThomas Huth "\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b" 1391e8a1faeSThomas Huth "\x03\x00\x04\x00"; 1401e8a1faeSThomas Huth 1411e8a1faeSThomas Huth tx(s, tpm_pcrread, sizeof(tpm_pcrread), buffer, sizeof(buffer)); 1421e8a1faeSThomas Huth 143df8a7568SStefan Berger /* skip pcrUpdateCounter (14th byte) in comparison */ 144df8a7568SStefan Berger g_assert(exp_resp_size >= 15); 145df8a7568SStefan Berger g_assert_cmpmem(buffer, 13, exp_resp, 13); 146df8a7568SStefan Berger g_assert_cmpmem(&buffer[14], exp_resp_size - 14, 147df8a7568SStefan Berger &exp_resp[14], exp_resp_size - 14); 1481e8a1faeSThomas Huth } 1491e8a1faeSThomas Huth 1501e8a1faeSThomas Huth bool tpm_util_swtpm_has_tpm2(void) 1511e8a1faeSThomas Huth { 1521e8a1faeSThomas Huth bool has_tpm2 = false; 1531e8a1faeSThomas Huth char *out = NULL; 1541e8a1faeSThomas Huth static const char *argv[] = { 1551e8a1faeSThomas Huth "swtpm", "socket", "--help", NULL 1561e8a1faeSThomas Huth }; 1571e8a1faeSThomas Huth 1581e8a1faeSThomas Huth if (!g_spawn_sync(NULL /* working_dir */, 1591e8a1faeSThomas Huth (char **)argv, 1601e8a1faeSThomas Huth NULL /* envp */, 1611e8a1faeSThomas Huth G_SPAWN_SEARCH_PATH, 1621e8a1faeSThomas Huth NULL /* child_setup */, 1631e8a1faeSThomas Huth NULL /* user_data */, 1641e8a1faeSThomas Huth &out, 1651e8a1faeSThomas Huth NULL /* err */, 1661e8a1faeSThomas Huth NULL /* exit_status */, 1671e8a1faeSThomas Huth NULL)) { 1681e8a1faeSThomas Huth return false; 1691e8a1faeSThomas Huth } 1701e8a1faeSThomas Huth 1711e8a1faeSThomas Huth if (strstr(out, "--tpm2")) { 1721e8a1faeSThomas Huth has_tpm2 = true; 1731e8a1faeSThomas Huth } 1741e8a1faeSThomas Huth 1751e8a1faeSThomas Huth g_free(out); 1761e8a1faeSThomas Huth return has_tpm2; 1771e8a1faeSThomas Huth } 1781e8a1faeSThomas Huth 1791e8a1faeSThomas Huth gboolean tpm_util_swtpm_start(const char *path, GPid *pid, 1801e8a1faeSThomas Huth SocketAddress **addr, GError **error) 1811e8a1faeSThomas Huth { 1821e8a1faeSThomas Huth char *swtpm_argv_tpmstate = g_strdup_printf("dir=%s", path); 1831e8a1faeSThomas Huth char *swtpm_argv_ctrl = g_strdup_printf("type=unixio,path=%s/sock", 1841e8a1faeSThomas Huth path); 1851e8a1faeSThomas Huth gchar *swtpm_argv[] = { 1861e8a1faeSThomas Huth g_strdup("swtpm"), g_strdup("socket"), 1871e8a1faeSThomas Huth g_strdup("--tpmstate"), swtpm_argv_tpmstate, 1881e8a1faeSThomas Huth g_strdup("--ctrl"), swtpm_argv_ctrl, 1891e8a1faeSThomas Huth g_strdup("--tpm2"), 1901e8a1faeSThomas Huth NULL 1911e8a1faeSThomas Huth }; 1921e8a1faeSThomas Huth gboolean succ; 1931e8a1faeSThomas Huth unsigned i; 1941e8a1faeSThomas Huth 1951e8a1faeSThomas Huth *addr = g_new0(SocketAddress, 1); 1961e8a1faeSThomas Huth (*addr)->type = SOCKET_ADDRESS_TYPE_UNIX; 1971e8a1faeSThomas Huth (*addr)->u.q_unix.path = g_build_filename(path, "sock", NULL); 1981e8a1faeSThomas Huth 1991e8a1faeSThomas Huth succ = g_spawn_async(NULL, swtpm_argv, NULL, G_SPAWN_SEARCH_PATH, 2001e8a1faeSThomas Huth NULL, NULL, pid, error); 2011e8a1faeSThomas Huth 2021e8a1faeSThomas Huth for (i = 0; swtpm_argv[i]; i++) { 2031e8a1faeSThomas Huth g_free(swtpm_argv[i]); 2041e8a1faeSThomas Huth } 2051e8a1faeSThomas Huth 2061e8a1faeSThomas Huth return succ; 2071e8a1faeSThomas Huth } 2081e8a1faeSThomas Huth 2091e8a1faeSThomas Huth void tpm_util_swtpm_kill(GPid pid) 2101e8a1faeSThomas Huth { 2111e8a1faeSThomas Huth int n; 2121e8a1faeSThomas Huth 2131e8a1faeSThomas Huth if (!pid) { 2141e8a1faeSThomas Huth return; 2151e8a1faeSThomas Huth } 2161e8a1faeSThomas Huth 2171e8a1faeSThomas Huth g_spawn_close_pid(pid); 2181e8a1faeSThomas Huth 2191e8a1faeSThomas Huth n = kill(pid, 0); 2201e8a1faeSThomas Huth if (n < 0) { 2211e8a1faeSThomas Huth return; 2221e8a1faeSThomas Huth } 2231e8a1faeSThomas Huth 2241e8a1faeSThomas Huth kill(pid, SIGKILL); 2251e8a1faeSThomas Huth } 2261e8a1faeSThomas Huth 2271e8a1faeSThomas Huth void tpm_util_migrate(QTestState *who, const char *uri) 2281e8a1faeSThomas Huth { 2291e8a1faeSThomas Huth QDict *rsp; 2301e8a1faeSThomas Huth 2311e8a1faeSThomas Huth rsp = qtest_qmp(who, 2321e8a1faeSThomas Huth "{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", 2331e8a1faeSThomas Huth uri); 2341e8a1faeSThomas Huth g_assert(qdict_haskey(rsp, "return")); 2351e8a1faeSThomas Huth qobject_unref(rsp); 2361e8a1faeSThomas Huth } 2371e8a1faeSThomas Huth 2381e8a1faeSThomas Huth void tpm_util_wait_for_migration_complete(QTestState *who) 2391e8a1faeSThomas Huth { 2401e8a1faeSThomas Huth while (true) { 241bb1a5b97SMaxim Levitsky QDict *rsp; 2421e8a1faeSThomas Huth QDict *rsp_return; 2431e8a1faeSThomas Huth bool completed; 2441e8a1faeSThomas Huth const char *status; 2451e8a1faeSThomas Huth 246bb1a5b97SMaxim Levitsky rsp = qtest_qmp(who, "{ 'execute': 'query-migrate' }"); 247bb1a5b97SMaxim Levitsky g_assert(qdict_haskey(rsp, "return")); 248bb1a5b97SMaxim Levitsky rsp_return = qdict_get_qdict(rsp, "return"); 249bb1a5b97SMaxim Levitsky 250bb1a5b97SMaxim Levitsky g_assert(!qdict_haskey(rsp_return, "error")); 2511e8a1faeSThomas Huth status = qdict_get_str(rsp_return, "status"); 2521e8a1faeSThomas Huth completed = strcmp(status, "completed") == 0; 2531e8a1faeSThomas Huth g_assert_cmpstr(status, !=, "failed"); 254f8e3df09SStefan Berger qobject_unref(rsp); 2551e8a1faeSThomas Huth if (completed) { 2561e8a1faeSThomas Huth return; 2571e8a1faeSThomas Huth } 2581e8a1faeSThomas Huth usleep(1000); 2591e8a1faeSThomas Huth } 2601e8a1faeSThomas Huth } 2611e8a1faeSThomas Huth 2621e8a1faeSThomas Huth void tpm_util_migration_start_qemu(QTestState **src_qemu, 2631e8a1faeSThomas Huth QTestState **dst_qemu, 2641e8a1faeSThomas Huth SocketAddress *src_tpm_addr, 2651e8a1faeSThomas Huth SocketAddress *dst_tpm_addr, 2661e8a1faeSThomas Huth const char *miguri, 267551cabdfSEric Auger const char *ifmodel, 268551cabdfSEric Auger const char *machine_options) 2691e8a1faeSThomas Huth { 2701e8a1faeSThomas Huth char *src_qemu_args, *dst_qemu_args; 2711e8a1faeSThomas Huth 2721e8a1faeSThomas Huth src_qemu_args = g_strdup_printf( 273551cabdfSEric Auger "%s " 2741e8a1faeSThomas Huth "-chardev socket,id=chr,path=%s " 2751e8a1faeSThomas Huth "-tpmdev emulator,id=dev,chardev=chr " 2761e8a1faeSThomas Huth "-device %s,tpmdev=dev ", 277551cabdfSEric Auger machine_options ? : "", src_tpm_addr->u.q_unix.path, ifmodel); 2781e8a1faeSThomas Huth 2791e8a1faeSThomas Huth *src_qemu = qtest_init(src_qemu_args); 2801e8a1faeSThomas Huth 2811e8a1faeSThomas Huth dst_qemu_args = g_strdup_printf( 282551cabdfSEric Auger "%s " 2831e8a1faeSThomas Huth "-chardev socket,id=chr,path=%s " 2841e8a1faeSThomas Huth "-tpmdev emulator,id=dev,chardev=chr " 2851e8a1faeSThomas Huth "-device %s,tpmdev=dev " 2861e8a1faeSThomas Huth "-incoming %s", 287551cabdfSEric Auger machine_options ? : "", 2881e8a1faeSThomas Huth dst_tpm_addr->u.q_unix.path, 2891e8a1faeSThomas Huth ifmodel, miguri); 2901e8a1faeSThomas Huth 2911e8a1faeSThomas Huth *dst_qemu = qtest_init(dst_qemu_args); 2921e8a1faeSThomas Huth 293e7b13acdSPeter Maydell g_free(src_qemu_args); 294e7b13acdSPeter Maydell g_free(dst_qemu_args); 2951e8a1faeSThomas Huth } 296*daa8bb57SThomas Huth 297*daa8bb57SThomas Huth /* Remove directory with remainders of swtpm */ 298*daa8bb57SThomas Huth void tpm_util_rmdir(const char *path) 299*daa8bb57SThomas Huth { 300*daa8bb57SThomas Huth char *filename; 301*daa8bb57SThomas Huth int ret; 302*daa8bb57SThomas Huth 303*daa8bb57SThomas Huth filename = g_strdup_printf("%s/tpm2-00.permall", path); 304*daa8bb57SThomas Huth g_unlink(filename); 305*daa8bb57SThomas Huth g_free(filename); 306*daa8bb57SThomas Huth 307*daa8bb57SThomas Huth filename = g_strdup_printf("%s/.lock", path); 308*daa8bb57SThomas Huth g_unlink(filename); 309*daa8bb57SThomas Huth g_free(filename); 310*daa8bb57SThomas Huth 311*daa8bb57SThomas Huth ret = g_rmdir(path); 312*daa8bb57SThomas Huth g_assert(!ret); 313*daa8bb57SThomas Huth } 314