cmd_ut_overlay.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. extern u32 __dtb_test_fdt_overlay_stacked_begin;
  19. static int ut_fdt_getprop_u32_by_index(void *fdt, const char *path,
  20. const char *name, int index,
  21. u32 *out)
  22. {
  23. const fdt32_t *val;
  24. int node_off;
  25. int len;
  26. node_off = fdt_path_offset(fdt, path);
  27. if (node_off < 0)
  28. return node_off;
  29. val = fdt_getprop(fdt, node_off, name, &len);
  30. if (!val || (len < (sizeof(uint32_t) * (index + 1))))
  31. return -FDT_ERR_NOTFOUND;
  32. *out = fdt32_to_cpu(*(val + index));
  33. return 0;
  34. }
  35. static int ut_fdt_getprop_u32(void *fdt, const char *path, const char *name,
  36. u32 *out)
  37. {
  38. return ut_fdt_getprop_u32_by_index(fdt, path, name, 0, out);
  39. }
  40. static int fdt_getprop_str(void *fdt, const char *path, const char *name,
  41. const char **out)
  42. {
  43. int node_off;
  44. int len;
  45. node_off = fdt_path_offset(fdt, path);
  46. if (node_off < 0)
  47. return node_off;
  48. *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
  49. return len < 0 ? len : 0;
  50. }
  51. static int fdt_overlay_change_int_property(struct unit_test_state *uts)
  52. {
  53. void *fdt = uts->priv;
  54. u32 val = 0;
  55. ut_assertok(ut_fdt_getprop_u32(fdt, "/test-node", "test-int-property",
  56. &val));
  57. ut_asserteq(43, val);
  58. return CMD_RET_SUCCESS;
  59. }
  60. OVERLAY_TEST(fdt_overlay_change_int_property, 0);
  61. static int fdt_overlay_change_str_property(struct unit_test_state *uts)
  62. {
  63. void *fdt = uts->priv;
  64. const char *val = NULL;
  65. ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
  66. &val));
  67. ut_asserteq_str("foobar", val);
  68. return CMD_RET_SUCCESS;
  69. }
  70. OVERLAY_TEST(fdt_overlay_change_str_property, 0);
  71. static int fdt_overlay_add_str_property(struct unit_test_state *uts)
  72. {
  73. void *fdt = uts->priv;
  74. const char *val = NULL;
  75. ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
  76. &val));
  77. ut_asserteq_str("foobar2", val);
  78. return CMD_RET_SUCCESS;
  79. }
  80. OVERLAY_TEST(fdt_overlay_add_str_property, 0);
  81. static int fdt_overlay_add_node_by_phandle(struct unit_test_state *uts)
  82. {
  83. void *fdt = uts->priv;
  84. int off;
  85. off = fdt_path_offset(fdt, "/test-node/new-node");
  86. ut_assert(off >= 0);
  87. ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
  88. return CMD_RET_SUCCESS;
  89. }
  90. OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0);
  91. static int fdt_overlay_add_node_by_path(struct unit_test_state *uts)
  92. {
  93. void *fdt = uts->priv;
  94. int off;
  95. off = fdt_path_offset(fdt, "/new-node");
  96. ut_assert(off >= 0);
  97. ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
  98. return CMD_RET_SUCCESS;
  99. }
  100. OVERLAY_TEST(fdt_overlay_add_node_by_path, 0);
  101. static int fdt_overlay_add_subnode_property(struct unit_test_state *uts)
  102. {
  103. void *fdt = uts->priv;
  104. int off;
  105. off = fdt_path_offset(fdt, "/test-node/sub-test-node");
  106. ut_assert(off >= 0);
  107. ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
  108. ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
  109. return CMD_RET_SUCCESS;
  110. }
  111. OVERLAY_TEST(fdt_overlay_add_subnode_property, 0);
  112. static int fdt_overlay_local_phandle(struct unit_test_state *uts)
  113. {
  114. uint32_t local_phandle;
  115. void *fdt = uts->priv;
  116. u32 val = 0;
  117. int off;
  118. off = fdt_path_offset(fdt, "/new-local-node");
  119. ut_assert(off >= 0);
  120. local_phandle = fdt_get_phandle(fdt, off);
  121. ut_assert(local_phandle);
  122. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  123. 0, &val));
  124. ut_asserteq(local_phandle, val);
  125. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  126. 1, &val));
  127. ut_asserteq(local_phandle, val);
  128. return CMD_RET_SUCCESS;
  129. }
  130. OVERLAY_TEST(fdt_overlay_local_phandle, 0);
  131. static int fdt_overlay_local_phandles(struct unit_test_state *uts)
  132. {
  133. uint32_t local_phandle, test_phandle;
  134. void *fdt = uts->priv;
  135. u32 val = 0;
  136. int off;
  137. off = fdt_path_offset(fdt, "/new-local-node");
  138. ut_assert(off >= 0);
  139. local_phandle = fdt_get_phandle(fdt, off);
  140. ut_assert(local_phandle);
  141. off = fdt_path_offset(fdt, "/test-node");
  142. ut_assert(off >= 0);
  143. test_phandle = fdt_get_phandle(fdt, off);
  144. ut_assert(test_phandle);
  145. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
  146. &val));
  147. ut_asserteq(test_phandle, val);
  148. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
  149. &val));
  150. ut_asserteq(local_phandle, val);
  151. return CMD_RET_SUCCESS;
  152. }
  153. OVERLAY_TEST(fdt_overlay_local_phandles, 0);
  154. static int fdt_overlay_stacked(struct unit_test_state *uts)
  155. {
  156. void *fdt = uts->priv;
  157. u32 val = 0;
  158. ut_assertok(ut_fdt_getprop_u32(fdt, "/new-local-node",
  159. "stacked-test-int-property", &val));
  160. ut_asserteq(43, val);
  161. return CMD_RET_SUCCESS;
  162. }
  163. OVERLAY_TEST(fdt_overlay_stacked, 0);
  164. int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  165. {
  166. struct unit_test *tests = ll_entry_start(struct unit_test,
  167. overlay_test);
  168. const int n_ents = ll_entry_count(struct unit_test, overlay_test);
  169. struct unit_test_state *uts;
  170. struct unit_test *test;
  171. void *fdt_base = &__dtb_test_fdt_base_begin;
  172. void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
  173. void *fdt_overlay_stacked = &__dtb_test_fdt_overlay_stacked_begin;
  174. void *fdt_base_copy, *fdt_overlay_copy, *fdt_overlay_stacked_copy;
  175. int ret = -ENOMEM;
  176. uts = calloc(1, sizeof(*uts));
  177. if (!uts)
  178. return -ENOMEM;
  179. ut_assertok(fdt_check_header(fdt_base));
  180. ut_assertok(fdt_check_header(fdt_overlay));
  181. fdt_base_copy = malloc(FDT_COPY_SIZE);
  182. if (!fdt_base_copy)
  183. goto err1;
  184. uts->priv = fdt_base_copy;
  185. fdt_overlay_copy = malloc(FDT_COPY_SIZE);
  186. if (!fdt_overlay_copy)
  187. goto err2;
  188. fdt_overlay_stacked_copy = malloc(FDT_COPY_SIZE);
  189. if (!fdt_overlay_stacked_copy)
  190. goto err3;
  191. /*
  192. * Resize the FDT to 4k so that we have room to operate on
  193. *
  194. * (and relocate it since the memory might be mapped
  195. * read-only)
  196. */
  197. ut_assertok(fdt_open_into(fdt_base, fdt_base_copy, FDT_COPY_SIZE));
  198. /*
  199. * Resize the overlay to 4k so that we have room to operate on
  200. *
  201. * (and relocate it since the memory might be mapped
  202. * read-only)
  203. */
  204. ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
  205. FDT_COPY_SIZE));
  206. /*
  207. * Resize the stacked overlay to 4k so that we have room to operate on
  208. *
  209. * (and relocate it since the memory might be mapped
  210. * read-only)
  211. */
  212. ut_assertok(fdt_open_into(fdt_overlay_stacked, fdt_overlay_stacked_copy,
  213. FDT_COPY_SIZE));
  214. /* Apply the overlay */
  215. ut_assertok(fdt_overlay_apply(fdt_base_copy, fdt_overlay_copy));
  216. /* Apply the stacked overlay */
  217. ut_assertok(fdt_overlay_apply(fdt_base_copy, fdt_overlay_stacked_copy));
  218. if (argc == 1)
  219. printf("Running %d environment tests\n", n_ents);
  220. for (test = tests; test < tests + n_ents; test++) {
  221. if (argc > 1 && strcmp(argv[1], test->name))
  222. continue;
  223. printf("Test: %s\n", test->name);
  224. uts->start = mallinfo();
  225. test->func(uts);
  226. }
  227. printf("Failures: %d\n", uts->fail_count);
  228. if (!uts->fail_count)
  229. ret = 0;
  230. else
  231. ret = CMD_RET_FAILURE;
  232. free(fdt_overlay_stacked_copy);
  233. err3:
  234. free(fdt_overlay_copy);
  235. err2:
  236. free(fdt_base_copy);
  237. err1:
  238. free(uts);
  239. return ret;
  240. }