Heiko Stuebner | 1c9bb9b | 2019-10-23 16:46:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019, Theobroma Systems Design und Consulting GmbH |
| 4 | */ |
| 5 | |
Heiko Stuebner | 1c9bb9b | 2019-10-23 16:46:41 +0200 | [diff] [blame] | 6 | #include <command.h> |
| 7 | #include <errno.h> |
| 8 | #include <fdt_support.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Heiko Stuebner | 1c9bb9b | 2019-10-23 16:46:41 +0200 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <tee/optee.h> |
| 12 | |
| 13 | #include <linux/sizes.h> |
| 14 | |
| 15 | #include <test/ut.h> |
| 16 | #include <test/optee.h> |
| 17 | #include <test/suites.h> |
| 18 | |
| 19 | /* 4k ought to be enough for anybody */ |
| 20 | #define FDT_COPY_SIZE (4 * SZ_1K) |
| 21 | |
| 22 | extern u32 __dtb_test_optee_base_begin; |
| 23 | extern u32 __dtb_test_optee_optee_begin; |
| 24 | extern u32 __dtb_test_optee_no_optee_begin; |
| 25 | |
| 26 | static void *fdt; |
| 27 | static bool expect_success; |
| 28 | |
| 29 | static int optee_fdt_firmware(struct unit_test_state *uts) |
| 30 | { |
| 31 | const void *prop; |
| 32 | int offs, len; |
| 33 | |
| 34 | offs = fdt_path_offset(fdt, "/firmware/optee"); |
| 35 | ut_assert(expect_success ? offs >= 0 : offs < 0); |
| 36 | |
| 37 | /* only continue if we have an optee node */ |
| 38 | if (offs < 0) |
| 39 | return CMD_RET_SUCCESS; |
| 40 | |
| 41 | prop = fdt_getprop(fdt, offs, "compatible", &len); |
| 42 | ut_assertok(strncmp((const char *)prop, "linaro,optee-tz", len)); |
| 43 | |
| 44 | prop = fdt_getprop(fdt, offs, "method", &len); |
| 45 | ut_assert(strncmp(prop, "hvc", 3) == 0 || strncmp(prop, "smc", 3) == 0); |
| 46 | |
| 47 | return CMD_RET_SUCCESS; |
| 48 | } |
| 49 | OPTEE_TEST(optee_fdt_firmware, 0); |
| 50 | |
| 51 | static int optee_fdt_protected_memory(struct unit_test_state *uts) |
| 52 | { |
| 53 | int offs, subnode; |
| 54 | bool found; |
| 55 | |
| 56 | offs = fdt_path_offset(fdt, "/firmware/optee"); |
| 57 | ut_assert(expect_success ? offs >= 0 : offs < 0); |
| 58 | |
| 59 | /* only continue if we have an optee node */ |
| 60 | if (offs < 0) |
| 61 | return CMD_RET_SUCCESS; |
| 62 | |
| 63 | /* optee inserts its memory regions as reserved-memory nodes */ |
| 64 | offs = fdt_subnode_offset(fdt, 0, "reserved-memory"); |
| 65 | ut_assert(offs >= 0); |
| 66 | |
| 67 | subnode = fdt_first_subnode(fdt, offs); |
| 68 | ut_assert(subnode); |
| 69 | |
| 70 | found = 0; |
| 71 | while (subnode >= 0) { |
| 72 | const char *name = fdt_get_name(fdt, subnode, NULL); |
| 73 | struct fdt_resource res; |
| 74 | |
| 75 | ut_assert(name); |
| 76 | |
| 77 | /* only handle optee reservations */ |
| 78 | if (strncmp(name, "optee", 5)) |
| 79 | continue; |
| 80 | |
| 81 | found = true; |
| 82 | |
| 83 | /* check if this subnode has a reg property */ |
| 84 | ut_assertok(fdt_get_resource(fdt, subnode, "reg", 0, &res)); |
| 85 | subnode = fdt_next_subnode(fdt, subnode); |
| 86 | } |
| 87 | |
| 88 | ut_assert(found); |
| 89 | |
| 90 | return CMD_RET_SUCCESS; |
| 91 | } |
| 92 | OPTEE_TEST(optee_fdt_protected_memory, 0); |
| 93 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 94 | int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Heiko Stuebner | 1c9bb9b | 2019-10-23 16:46:41 +0200 | [diff] [blame] | 95 | { |
Simon Glass | b50211f | 2021-03-07 17:35:10 -0700 | [diff] [blame] | 96 | struct unit_test *tests = UNIT_TEST_SUITE_START(optee_test); |
| 97 | const int n_ents = UNIT_TEST_SUITE_COUNT(optee_test); |
Heiko Stuebner | 1c9bb9b | 2019-10-23 16:46:41 +0200 | [diff] [blame] | 98 | struct unit_test_state *uts; |
| 99 | void *fdt_optee = &__dtb_test_optee_optee_begin; |
| 100 | void *fdt_no_optee = &__dtb_test_optee_no_optee_begin; |
| 101 | void *fdt_base = &__dtb_test_optee_base_begin; |
| 102 | int ret = -ENOMEM; |
| 103 | |
| 104 | uts = calloc(1, sizeof(*uts)); |
| 105 | if (!uts) |
| 106 | return -ENOMEM; |
| 107 | |
| 108 | ut_assertok(fdt_check_header(fdt_base)); |
| 109 | ut_assertok(fdt_check_header(fdt_optee)); |
| 110 | ut_assertok(fdt_check_header(fdt_no_optee)); |
| 111 | |
| 112 | fdt = malloc(FDT_COPY_SIZE); |
| 113 | if (!fdt) |
| 114 | return ret; |
| 115 | |
| 116 | /* |
| 117 | * Resize the FDT to 4k so that we have room to operate on |
| 118 | * |
| 119 | * (and relocate it since the memory might be mapped |
| 120 | * read-only) |
| 121 | */ |
| 122 | ut_assertok(fdt_open_into(fdt_base, fdt, FDT_COPY_SIZE)); |
| 123 | |
| 124 | /* |
| 125 | * (1) Try to copy optee nodes from empty dt. |
| 126 | * This should still run successfully. |
| 127 | */ |
| 128 | ut_assertok(optee_copy_fdt_nodes(fdt_no_optee, fdt)); |
| 129 | |
| 130 | expect_success = false; |
Philippe Reynes | 1f99f84 | 2019-12-17 19:07:04 +0100 | [diff] [blame] | 131 | ret = cmd_ut_category("optee", "", tests, n_ents, argc, argv); |
Heiko Stuebner | 1c9bb9b | 2019-10-23 16:46:41 +0200 | [diff] [blame] | 132 | |
| 133 | /* (2) Try to copy optee nodes from prefilled dt */ |
| 134 | ut_assertok(optee_copy_fdt_nodes(fdt_optee, fdt)); |
| 135 | |
| 136 | expect_success = true; |
Philippe Reynes | 1f99f84 | 2019-12-17 19:07:04 +0100 | [diff] [blame] | 137 | ret = cmd_ut_category("optee", "", tests, n_ents, argc, argv); |
Heiko Stuebner | 1c9bb9b | 2019-10-23 16:46:41 +0200 | [diff] [blame] | 138 | |
| 139 | /* (3) Try to copy OP-TEE nodes into a already filled DT */ |
| 140 | ut_assertok(fdt_open_into(fdt_optee, fdt, FDT_COPY_SIZE)); |
| 141 | ut_assertok(optee_copy_fdt_nodes(fdt_optee, fdt)); |
| 142 | |
| 143 | expect_success = true; |
Philippe Reynes | 1f99f84 | 2019-12-17 19:07:04 +0100 | [diff] [blame] | 144 | ret = cmd_ut_category("optee", "", tests, n_ents, argc, argv); |
Heiko Stuebner | 1c9bb9b | 2019-10-23 16:46:41 +0200 | [diff] [blame] | 145 | |
| 146 | free(fdt); |
| 147 | return ret; |
| 148 | } |