Skip to content

Commit

Permalink
qtest: Move tpm_util_tis_transmit() into tpm-tis-utils.c and rename it
Browse files Browse the repository at this point in the history
To be able to remove tpm_tis_base_addr from test cases that do not really
need it move the tpm_util_tis_transmit() function into tpm-tis-utils.c and
rename it to tpm_tis_transmit().

Fix a locality parameter in a test case on the way.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Ninad Palsule <ninad@linux.ibm.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Message-id: 20230331173051.3857801-3-stefanb@linux.ibm.com
  • Loading branch information
stefanberger committed Apr 20, 2023
1 parent f0de635 commit ea5e73b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 59 deletions.
3 changes: 0 additions & 3 deletions tests/qtest/tpm-crb-swtpm-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#include "tpm-tests.h"
#include "hw/acpi/tpm.h"

/* Not used but needed for linking */
uint64_t tpm_tis_base_addr = TPM_TIS_ADDR_BASE;

typedef struct TestState {
char *src_tpm_path;
char *dst_tpm_path;
Expand Down
3 changes: 0 additions & 3 deletions tests/qtest/tpm-crb-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#include "qemu/module.h"
#include "tpm-emu.h"

/* Not used but needed for linking */
uint64_t tpm_tis_base_addr = TPM_TIS_ADDR_BASE;

#define TPM_CMD "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00"

static void tpm_crb_test(const void *data)
Expand Down
5 changes: 3 additions & 2 deletions tests/qtest/tpm-tis-device-swtpm-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "libqtest.h"
#include "qemu/module.h"
#include "tpm-tests.h"
#include "tpm-tis-util.h"
#include "hw/acpi/tpm.h"

uint64_t tpm_tis_base_addr = 0xc000000;
Expand All @@ -33,7 +34,7 @@ static void tpm_tis_swtpm_test(const void *data)
{
const TestState *ts = data;

tpm_test_swtpm_test(ts->src_tpm_path, tpm_util_tis_transfer,
tpm_test_swtpm_test(ts->src_tpm_path, tpm_tis_transfer,
"tpm-tis-device", MACHINE_OPTIONS);
}

Expand All @@ -42,7 +43,7 @@ static void tpm_tis_swtpm_migration_test(const void *data)
const TestState *ts = data;

tpm_test_swtpm_migration_test(ts->src_tpm_path, ts->dst_tpm_path, ts->uri,
tpm_util_tis_transfer, "tpm-tis-device",
tpm_tis_transfer, "tpm-tis-device",
MACHINE_OPTIONS);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/qtest/tpm-tis-swtpm-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "libqtest.h"
#include "qemu/module.h"
#include "tpm-tests.h"
#include "tpm-tis-util.h"
#include "hw/acpi/tpm.h"

uint64_t tpm_tis_base_addr = TPM_TIS_ADDR_BASE;
Expand All @@ -31,7 +32,7 @@ static void tpm_tis_swtpm_test(const void *data)
{
const TestState *ts = data;

tpm_test_swtpm_test(ts->src_tpm_path, tpm_util_tis_transfer,
tpm_test_swtpm_test(ts->src_tpm_path, tpm_tis_transfer,
"tpm-tis", NULL);
}

Expand All @@ -40,7 +41,7 @@ static void tpm_tis_swtpm_migration_test(const void *data)
const TestState *ts = data;

tpm_test_swtpm_migration_test(ts->src_tpm_path, ts->dst_tpm_path, ts->uri,
tpm_util_tis_transfer, "tpm-tis", NULL);
tpm_tis_transfer, "tpm-tis", NULL);
}

int main(int argc, char **argv)
Expand Down
47 changes: 46 additions & 1 deletion tests/qtest/tpm-tis-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void tpm_tis_test_check_localities(const void *data)
uint32_t rid;

for (locty = 0; locty < TPM_TIS_NUM_LOCALITIES; locty++) {
access = readb(TIS_REG(0, TPM_TIS_REG_ACCESS));
access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
TPM_TIS_ACCESS_TPM_ESTABLISHMENT);

Expand Down Expand Up @@ -449,3 +449,48 @@ void tpm_tis_test_check_transmit(const void *data)
writeb(TIS_REG(0, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_ACTIVE_LOCALITY);
access = readb(TIS_REG(0, TPM_TIS_REG_ACCESS));
}

void tpm_tis_transfer(QTestState *s,
const unsigned char *req, size_t req_size,
unsigned char *rsp, size_t rsp_size)
{
uint32_t sts;
uint16_t bcount;
size_t i;

/* request use of locality 0 */
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
qtest_writel(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_COMMAND_READY);

sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
bcount = (sts >> 8) & 0xffff;
g_assert_cmpint(bcount, >=, req_size);

/* transmit command */
for (i = 0; i < req_size; i++) {
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO), req[i]);
}

/* start processing */
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_TPM_GO);

uint64_t end_time = g_get_monotonic_time() + 50 * G_TIME_SPAN_SECOND;
do {
sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
if ((sts & TPM_TIS_STS_DATA_AVAILABLE) != 0) {
break;
}
} while (g_get_monotonic_time() < end_time);

sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
bcount = (sts >> 8) & 0xffff;

memset(rsp, 0, rsp_size);
for (i = 0; i < bcount; i++) {
rsp[i] = qtest_readb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO));
}

/* relinquish use of locality 0 */
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS),
TPM_TIS_ACCESS_ACTIVE_LOCALITY);
}
4 changes: 4 additions & 0 deletions tests/qtest/tpm-tis-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ void tpm_tis_test_check_access_reg_seize(const void *data);
void tpm_tis_test_check_access_reg_release(const void *data);
void tpm_tis_test_check_transmit(const void *data);

void tpm_tis_transfer(QTestState *s,
const unsigned char *req, size_t req_size,
unsigned char *rsp, size_t rsp_size);

#endif /* TESTS_TPM_TIS_UTIL_H */
45 changes: 0 additions & 45 deletions tests/qtest/tpm-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,51 +51,6 @@ void tpm_util_crb_transfer(QTestState *s,
qtest_memread(s, raddr, rsp, rsp_size);
}

void tpm_util_tis_transfer(QTestState *s,
const unsigned char *req, size_t req_size,
unsigned char *rsp, size_t rsp_size)
{
uint32_t sts;
uint16_t bcount;
size_t i;

/* request use of locality 0 */
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
qtest_writel(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_COMMAND_READY);

sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
bcount = (sts >> 8) & 0xffff;
g_assert_cmpint(bcount, >=, req_size);

/* transmit command */
for (i = 0; i < req_size; i++) {
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO), req[i]);
}

/* start processing */
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_TPM_GO);

uint64_t end_time = g_get_monotonic_time() + 50 * G_TIME_SPAN_SECOND;
do {
sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
if ((sts & TPM_TIS_STS_DATA_AVAILABLE) != 0) {
break;
}
} while (g_get_monotonic_time() < end_time);

sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
bcount = (sts >> 8) & 0xffff;

memset(rsp, 0, rsp_size);
for (i = 0; i < bcount; i++) {
rsp[i] = qtest_readb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO));
}

/* relinquish use of locality 0 */
qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS),
TPM_TIS_ACCESS_ACTIVE_LOCALITY);
}

void tpm_util_startup(QTestState *s, tx_func *tx)
{
unsigned char buffer[1024];
Expand Down
3 changes: 0 additions & 3 deletions tests/qtest/tpm-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ typedef void (tx_func)(QTestState *s,
void tpm_util_crb_transfer(QTestState *s,
const unsigned char *req, size_t req_size,
unsigned char *rsp, size_t rsp_size);
void tpm_util_tis_transfer(QTestState *s,
const unsigned char *req, size_t req_size,
unsigned char *rsp, size_t rsp_size);

void tpm_util_startup(QTestState *s, tx_func *tx);
void tpm_util_pcrextend(QTestState *s, tx_func *tx);
Expand Down

0 comments on commit ea5e73b

Please sign in to comment.