cmd_ut_optee.c 3.7 KB

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