cmd_ut_overlay.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2016 NextThing Co
  3. * Copyright (c) 2016 Free Electrons
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <errno.h>
  10. #include <malloc.h>
  11. #include <linux/sizes.h>
  12. #include <test/ut.h>
  13. #include <test/overlay.h>
  14. /* 4k ought to be enough for anybody */
  15. #define FDT_COPY_SIZE (4 * SZ_1K)
  16. extern u32 __dtb_test_fdt_base_begin;
  17. extern u32 __dtb_test_fdt_overlay_begin;
  18. static int fdt_getprop_u32_by_index(void *fdt, const char *path,
  19. const char *name, int index,
  20. u32 *out)
  21. {
  22. const fdt32_t *val;
  23. int node_off;
  24. int len;
  25. node_off = fdt_path_offset(fdt, path);
  26. if (node_off < 0)
  27. return node_off;
  28. val = fdt_getprop(fdt, node_off, name, &len);
  29. if (!val || (len < (sizeof(uint32_t) * (index + 1))))
  30. return -FDT_ERR_NOTFOUND;
  31. *out = fdt32_to_cpu(*(val + index));
  32. return 0;
  33. }
  34. static int fdt_getprop_u32(void *fdt, const char *path, const char *name,
  35. u32 *out)
  36. {
  37. return fdt_getprop_u32_by_index(fdt, path, name, 0, out);
  38. }
  39. static int fdt_getprop_str(void *fdt, const char *path, const char *name,
  40. const char **out)
  41. {
  42. int node_off;
  43. int len;
  44. node_off = fdt_path_offset(fdt, path);
  45. if (node_off < 0)
  46. return node_off;
  47. *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
  48. return len < 0 ? len : 0;
  49. }
  50. static int fdt_overlay_change_int_property(struct unit_test_state *uts)
  51. {
  52. void *fdt = uts->priv;
  53. u32 val = 0;
  54. ut_assertok(fdt_getprop_u32(fdt, "/test-node", "test-int-property",
  55. &val));
  56. ut_asserteq(43, val);
  57. return CMD_RET_SUCCESS;
  58. }
  59. OVERLAY_TEST(fdt_overlay_change_int_property, 0);
  60. static int fdt_overlay_change_str_property(struct unit_test_state *uts)
  61. {
  62. void *fdt = uts->priv;
  63. const char *val = NULL;
  64. ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
  65. &val));
  66. ut_asserteq_str("foobar", val);
  67. return CMD_RET_SUCCESS;
  68. }
  69. OVERLAY_TEST(fdt_overlay_change_str_property, 0);
  70. static int fdt_overlay_add_str_property(struct unit_test_state *uts)
  71. {
  72. void *fdt = uts->priv;
  73. const char *val = NULL;
  74. ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
  75. &val));
  76. ut_asserteq_str("foobar2", val);
  77. return CMD_RET_SUCCESS;
  78. }
  79. OVERLAY_TEST(fdt_overlay_add_str_property, 0);
  80. static int fdt_overlay_add_node_by_phandle(struct unit_test_state *uts)
  81. {
  82. void *fdt = uts->priv;
  83. int off;
  84. off = fdt_path_offset(fdt, "/test-node/new-node");
  85. ut_assert(off >= 0);
  86. ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
  87. return CMD_RET_SUCCESS;
  88. }
  89. OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0);
  90. static int fdt_overlay_add_node_by_path(struct unit_test_state *uts)
  91. {
  92. void *fdt = uts->priv;
  93. int off;
  94. off = fdt_path_offset(fdt, "/new-node");
  95. ut_assert(off >= 0);
  96. ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
  97. return CMD_RET_SUCCESS;
  98. }
  99. OVERLAY_TEST(fdt_overlay_add_node_by_path, 0);
  100. static int fdt_overlay_add_subnode_property(struct unit_test_state *uts)
  101. {
  102. void *fdt = uts->priv;
  103. int off;
  104. off = fdt_path_offset(fdt, "/test-node/sub-test-node");
  105. ut_assert(off >= 0);
  106. ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
  107. ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
  108. return CMD_RET_SUCCESS;
  109. }
  110. OVERLAY_TEST(fdt_overlay_add_subnode_property, 0);
  111. static int fdt_overlay_local_phandle(struct unit_test_state *uts)
  112. {
  113. uint32_t local_phandle;
  114. void *fdt = uts->priv;
  115. u32 val = 0;
  116. int off;
  117. off = fdt_path_offset(fdt, "/new-local-node");
  118. ut_assert(off >= 0);
  119. local_phandle = fdt_get_phandle(fdt, off);
  120. ut_assert(local_phandle);
  121. ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  122. 0, &val));
  123. ut_asserteq(local_phandle, val);
  124. ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  125. 1, &val));
  126. ut_asserteq(local_phandle, val);
  127. return CMD_RET_SUCCESS;
  128. }
  129. OVERLAY_TEST(fdt_overlay_local_phandle, 0);
  130. static int fdt_overlay_local_phandles(struct unit_test_state *uts)
  131. {
  132. uint32_t local_phandle, test_phandle;
  133. void *fdt = uts->priv;
  134. u32 val = 0;
  135. int off;
  136. off = fdt_path_offset(fdt, "/new-local-node");
  137. ut_assert(off >= 0);
  138. local_phandle = fdt_get_phandle(fdt, off);
  139. ut_assert(local_phandle);
  140. off = fdt_path_offset(fdt, "/test-node");
  141. ut_assert(off >= 0);
  142. test_phandle = fdt_get_phandle(fdt, off);
  143. ut_assert(test_phandle);
  144. ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
  145. &val));
  146. ut_asserteq(test_phandle, val);
  147. ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
  148. &val));
  149. ut_asserteq(local_phandle, val);
  150. return CMD_RET_SUCCESS;
  151. }
  152. OVERLAY_TEST(fdt_overlay_local_phandles, 0);
  153. int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  154. {
  155. struct unit_test *tests = ll_entry_start(struct unit_test,
  156. overlay_test);
  157. const int n_ents = ll_entry_count(struct unit_test, overlay_test);
  158. struct unit_test_state *uts;
  159. struct unit_test *test;
  160. void *fdt_base = &__dtb_test_fdt_base_begin;
  161. void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
  162. void *fdt_base_copy, *fdt_overlay_copy;
  163. uts = calloc(1, sizeof(*uts));
  164. if (!uts)
  165. return -ENOMEM;
  166. ut_assertok(fdt_check_header(fdt_base));
  167. ut_assertok(fdt_check_header(fdt_overlay));
  168. fdt_base_copy = malloc(FDT_COPY_SIZE);
  169. if (!fdt_base_copy)
  170. return -ENOMEM;
  171. uts->priv = fdt_base_copy;
  172. fdt_overlay_copy = malloc(FDT_COPY_SIZE);
  173. if (!fdt_overlay_copy)
  174. return -ENOMEM;
  175. /*
  176. * Resize the FDT to 4k so that we have room to operate on
  177. *
  178. * (and relocate it since the memory might be mapped
  179. * read-only)
  180. */
  181. ut_assertok(fdt_open_into(fdt_base, fdt_base_copy, FDT_COPY_SIZE));
  182. /*
  183. * Resize the overlay to 4k so that we have room to operate on
  184. *
  185. * (and relocate it since the memory might be mapped
  186. * read-only)
  187. */
  188. ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
  189. FDT_COPY_SIZE));
  190. /* Apply the overlay */
  191. ut_assertok(fdt_overlay_apply(fdt_base_copy, fdt_overlay_copy));
  192. if (argc == 1)
  193. printf("Running %d environment tests\n", n_ents);
  194. for (test = tests; test < tests + n_ents; test++) {
  195. if (argc > 1 && strcmp(argv[1], test->name))
  196. continue;
  197. printf("Test: %s\n", test->name);
  198. uts->start = mallinfo();
  199. test->func(uts);
  200. }
  201. printf("Failures: %d\n", uts->fail_count);
  202. free(fdt_overlay_copy);
  203. free(fdt_base_copy);
  204. free(uts);
  205. return uts->fail_count ? CMD_RET_FAILURE : 0;
  206. }