cmd_ut_overlay.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2016 NextThing Co
  4. * Copyright (c) 2016 Free Electrons
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <errno.h>
  9. #include <fdt_support.h>
  10. #include <malloc.h>
  11. #include <linux/sizes.h>
  12. #include <test/ut.h>
  13. #include <test/overlay.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_fdt_base_begin;
  18. extern u32 __dtb_test_fdt_overlay_begin;
  19. extern u32 __dtb_test_fdt_overlay_stacked_begin;
  20. static int ut_fdt_getprop_u32_by_index(void *fdt, const char *path,
  21. const char *name, int index,
  22. u32 *out)
  23. {
  24. const fdt32_t *val;
  25. int node_off;
  26. int len;
  27. node_off = fdt_path_offset(fdt, path);
  28. if (node_off < 0)
  29. return node_off;
  30. val = fdt_getprop(fdt, node_off, name, &len);
  31. if (!val || (len < (sizeof(uint32_t) * (index + 1))))
  32. return -FDT_ERR_NOTFOUND;
  33. *out = fdt32_to_cpu(*(val + index));
  34. return 0;
  35. }
  36. static int ut_fdt_getprop_u32(void *fdt, const char *path, const char *name,
  37. u32 *out)
  38. {
  39. return ut_fdt_getprop_u32_by_index(fdt, path, name, 0, out);
  40. }
  41. static int fdt_getprop_str(void *fdt, const char *path, const char *name,
  42. const char **out)
  43. {
  44. int node_off;
  45. int len;
  46. node_off = fdt_path_offset(fdt, path);
  47. if (node_off < 0)
  48. return node_off;
  49. *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
  50. return len < 0 ? len : 0;
  51. }
  52. static int fdt_overlay_change_int_property(struct unit_test_state *uts)
  53. {
  54. void *fdt = uts->priv;
  55. u32 val = 0;
  56. ut_assertok(ut_fdt_getprop_u32(fdt, "/test-node", "test-int-property",
  57. &val));
  58. ut_asserteq(43, val);
  59. return CMD_RET_SUCCESS;
  60. }
  61. OVERLAY_TEST(fdt_overlay_change_int_property, 0);
  62. static int fdt_overlay_change_str_property(struct unit_test_state *uts)
  63. {
  64. void *fdt = uts->priv;
  65. const char *val = NULL;
  66. ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
  67. &val));
  68. ut_asserteq_str("foobar", val);
  69. return CMD_RET_SUCCESS;
  70. }
  71. OVERLAY_TEST(fdt_overlay_change_str_property, 0);
  72. static int fdt_overlay_add_str_property(struct unit_test_state *uts)
  73. {
  74. void *fdt = uts->priv;
  75. const char *val = NULL;
  76. ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
  77. &val));
  78. ut_asserteq_str("foobar2", val);
  79. return CMD_RET_SUCCESS;
  80. }
  81. OVERLAY_TEST(fdt_overlay_add_str_property, 0);
  82. static int fdt_overlay_add_node_by_phandle(struct unit_test_state *uts)
  83. {
  84. void *fdt = uts->priv;
  85. int off;
  86. off = fdt_path_offset(fdt, "/test-node/new-node");
  87. ut_assert(off >= 0);
  88. ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
  89. return CMD_RET_SUCCESS;
  90. }
  91. OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0);
  92. static int fdt_overlay_add_node_by_path(struct unit_test_state *uts)
  93. {
  94. void *fdt = uts->priv;
  95. int off;
  96. off = fdt_path_offset(fdt, "/new-node");
  97. ut_assert(off >= 0);
  98. ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
  99. return CMD_RET_SUCCESS;
  100. }
  101. OVERLAY_TEST(fdt_overlay_add_node_by_path, 0);
  102. static int fdt_overlay_add_subnode_property(struct unit_test_state *uts)
  103. {
  104. void *fdt = uts->priv;
  105. int off;
  106. off = fdt_path_offset(fdt, "/test-node/sub-test-node");
  107. ut_assert(off >= 0);
  108. ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
  109. ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
  110. return CMD_RET_SUCCESS;
  111. }
  112. OVERLAY_TEST(fdt_overlay_add_subnode_property, 0);
  113. static int fdt_overlay_local_phandle(struct unit_test_state *uts)
  114. {
  115. uint32_t local_phandle;
  116. void *fdt = uts->priv;
  117. u32 val = 0;
  118. int off;
  119. off = fdt_path_offset(fdt, "/new-local-node");
  120. ut_assert(off >= 0);
  121. local_phandle = fdt_get_phandle(fdt, off);
  122. ut_assert(local_phandle);
  123. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  124. 0, &val));
  125. ut_asserteq(local_phandle, val);
  126. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  127. 1, &val));
  128. ut_asserteq(local_phandle, val);
  129. return CMD_RET_SUCCESS;
  130. }
  131. OVERLAY_TEST(fdt_overlay_local_phandle, 0);
  132. static int fdt_overlay_local_phandles(struct unit_test_state *uts)
  133. {
  134. uint32_t local_phandle, test_phandle;
  135. void *fdt = uts->priv;
  136. u32 val = 0;
  137. int off;
  138. off = fdt_path_offset(fdt, "/new-local-node");
  139. ut_assert(off >= 0);
  140. local_phandle = fdt_get_phandle(fdt, off);
  141. ut_assert(local_phandle);
  142. off = fdt_path_offset(fdt, "/test-node");
  143. ut_assert(off >= 0);
  144. test_phandle = fdt_get_phandle(fdt, off);
  145. ut_assert(test_phandle);
  146. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
  147. &val));
  148. ut_asserteq(test_phandle, val);
  149. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
  150. &val));
  151. ut_asserteq(local_phandle, val);
  152. return CMD_RET_SUCCESS;
  153. }
  154. OVERLAY_TEST(fdt_overlay_local_phandles, 0);
  155. static int fdt_overlay_stacked(struct unit_test_state *uts)
  156. {
  157. void *fdt = uts->priv;
  158. u32 val = 0;
  159. ut_assertok(ut_fdt_getprop_u32(fdt, "/new-local-node",
  160. "stacked-test-int-property", &val));
  161. ut_asserteq(43, val);
  162. return CMD_RET_SUCCESS;
  163. }
  164. OVERLAY_TEST(fdt_overlay_stacked, 0);
  165. int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  166. {
  167. struct unit_test *tests = ll_entry_start(struct unit_test,
  168. overlay_test);
  169. const int n_ents = ll_entry_count(struct unit_test, overlay_test);
  170. struct unit_test_state *uts;
  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. ret = cmd_ut_category("overlay", tests, n_ents, argc, argv);
  219. free(fdt_overlay_stacked_copy);
  220. err3:
  221. free(fdt_overlay_copy);
  222. err2:
  223. free(fdt_base_copy);
  224. err1:
  225. free(uts);
  226. return ret;
  227. }