Simon Glass | 11a075c | 2022-08-30 21:05:36 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2022 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
Simon Glass | 11a075c | 2022-08-30 21:05:36 -0600 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <tpm_api.h> |
| 9 | #include <dm/test.h> |
| 10 | #include <test/test.h> |
| 11 | #include <test/ut.h> |
| 12 | |
Simon Glass | eef107e | 2023-02-21 06:24:51 -0700 | [diff] [blame] | 13 | /* |
| 14 | * get_tpm_version() - Get a TPM of the given version |
| 15 | * |
| 16 | * @version: Version to get |
| 17 | * @devp: Returns the TPM device |
| 18 | * Returns: 0 if OK, -ENODEV if not found |
| 19 | */ |
| 20 | static int get_tpm_version(enum tpm_version version, struct udevice **devp) |
| 21 | { |
| 22 | struct udevice *dev; |
| 23 | |
| 24 | /* |
| 25 | * For now we have to probe each TPM, since the version is set up in |
| 26 | * of_to_plat(). We could require TPMs to declare their version when |
| 27 | * probed, to avoid this |
| 28 | */ |
| 29 | uclass_foreach_dev_probe(UCLASS_TPM, dev) { |
| 30 | if (tpm_get_version(dev) == version) { |
| 31 | *devp = dev; |
| 32 | return 0; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return -ENODEV; |
| 37 | } |
| 38 | |
| 39 | /* Basic test of initing a TPM */ |
| 40 | static int test_tpm_init(struct unit_test_state *uts, enum tpm_version version) |
| 41 | { |
| 42 | struct udevice *dev; |
| 43 | |
| 44 | /* check probe success */ |
| 45 | ut_assertok(get_tpm_version(version, &dev)); |
| 46 | |
| 47 | ut_assertok(tpm_init(dev)); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Simon Glass | 11a075c | 2022-08-30 21:05:36 -0600 | [diff] [blame] | 52 | static int dm_test_tpm(struct unit_test_state *uts) |
| 53 | { |
Simon Glass | eef107e | 2023-02-21 06:24:51 -0700 | [diff] [blame] | 54 | ut_assertok(test_tpm_init(uts, TPM_V1)); |
| 55 | ut_assertok(test_tpm_init(uts, TPM_V2)); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | DM_TEST(dm_test_tpm, UT_TESTF_SCAN_FDT); |
| 60 | |
| 61 | /* Test report_state */ |
| 62 | static int dm_test_tpm_report_state(struct unit_test_state *uts) |
| 63 | { |
Simon Glass | 11a075c | 2022-08-30 21:05:36 -0600 | [diff] [blame] | 64 | struct udevice *dev; |
| 65 | char buf[50]; |
| 66 | |
| 67 | /* check probe success */ |
Simon Glass | eef107e | 2023-02-21 06:24:51 -0700 | [diff] [blame] | 68 | ut_assertok(get_tpm_version(TPM_V2, &dev)); |
Simon Glass | 11a075c | 2022-08-30 21:05:36 -0600 | [diff] [blame] | 69 | |
| 70 | ut_assert(tpm_report_state(dev, buf, sizeof(buf))); |
| 71 | ut_asserteq_str("init_done=0", buf); |
| 72 | |
Ilias Apalodimas | b6a2fae | 2023-02-18 17:21:22 +0200 | [diff] [blame] | 73 | ut_assertok(tpm_auto_start(dev)); |
Simon Glass | 11a075c | 2022-08-30 21:05:36 -0600 | [diff] [blame] | 74 | |
| 75 | ut_assert(tpm_report_state(dev, buf, sizeof(buf))); |
| 76 | ut_asserteq_str("init_done=1", buf); |
| 77 | |
| 78 | return 0; |
| 79 | } |
Simon Glass | eef107e | 2023-02-21 06:24:51 -0700 | [diff] [blame] | 80 | DM_TEST(dm_test_tpm_report_state, UT_TESTF_SCAN_FDT); |
Simon Glass | 3467ed2 | 2023-02-21 06:24:52 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * test_tpm_autostart() - check the tpm_auto_start() call |
| 84 | * |
| 85 | * @uts: Unit test state |
| 86 | * @version: TPM version to use |
| 87 | * @reinit: true to call tpm_init() first |
| 88 | * Returns 0 if OK, non-zero on failure |
| 89 | */ |
| 90 | static int test_tpm_autostart(struct unit_test_state *uts, |
| 91 | enum tpm_version version, bool reinit) |
| 92 | { |
| 93 | struct udevice *dev; |
| 94 | |
| 95 | /* check probe success */ |
| 96 | ut_assertok(get_tpm_version(version, &dev)); |
| 97 | |
| 98 | if (reinit) |
| 99 | ut_assertok(tpm_init(dev)); |
Ilias Apalodimas | 83e0d21 | 2023-06-01 09:18:44 +0300 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * tpm_auto_start will rerun tpm_init() if reinit, but handles the |
| 103 | * -EBUSY return code internally. |
| 104 | */ |
Simon Glass | 3467ed2 | 2023-02-21 06:24:52 -0700 | [diff] [blame] | 105 | ut_assertok(tpm_auto_start(dev)); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int dm_test_tpm_autostart(struct unit_test_state *uts) |
| 111 | { |
| 112 | ut_assertok(test_tpm_autostart(uts, TPM_V1, false)); |
| 113 | ut_assertok(test_tpm_autostart(uts, TPM_V2, false)); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | DM_TEST(dm_test_tpm_autostart, UT_TESTF_SCAN_FDT); |
| 118 | |
| 119 | static int dm_test_tpm_autostart_reinit(struct unit_test_state *uts) |
| 120 | { |
| 121 | ut_assertok(test_tpm_autostart(uts, TPM_V1, true)); |
| 122 | ut_assertok(test_tpm_autostart(uts, TPM_V2, true)); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | DM_TEST(dm_test_tpm_autostart_reinit, UT_TESTF_SCAN_FDT); |