cmd_ut_overlay.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 void *fdt;
  21. static int ut_fdt_getprop_u32_by_index(void *fdt, const char *path,
  22. const char *name, int index,
  23. u32 *out)
  24. {
  25. const fdt32_t *val;
  26. int node_off;
  27. int len;
  28. node_off = fdt_path_offset(fdt, path);
  29. if (node_off < 0)
  30. return node_off;
  31. val = fdt_getprop(fdt, node_off, name, &len);
  32. if (!val || (len < (sizeof(uint32_t) * (index + 1))))
  33. return -FDT_ERR_NOTFOUND;
  34. *out = fdt32_to_cpu(*(val + index));
  35. return 0;
  36. }
  37. static int ut_fdt_getprop_u32(void *fdt, const char *path, const char *name,
  38. u32 *out)
  39. {
  40. return ut_fdt_getprop_u32_by_index(fdt, path, name, 0, out);
  41. }
  42. static int fdt_getprop_str(void *fdt, const char *path, const char *name,
  43. const char **out)
  44. {
  45. int node_off;
  46. int len;
  47. node_off = fdt_path_offset(fdt, path);
  48. if (node_off < 0)
  49. return node_off;
  50. *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
  51. return len < 0 ? len : 0;
  52. }
  53. static int fdt_overlay_change_int_property(struct unit_test_state *uts)
  54. {
  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. 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. 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. int off;
  83. off = fdt_path_offset(fdt, "/test-node/new-node");
  84. ut_assert(off >= 0);
  85. ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
  86. return CMD_RET_SUCCESS;
  87. }
  88. OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0);
  89. static int fdt_overlay_add_node_by_path(struct unit_test_state *uts)
  90. {
  91. int off;
  92. off = fdt_path_offset(fdt, "/new-node");
  93. ut_assert(off >= 0);
  94. ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
  95. return CMD_RET_SUCCESS;
  96. }
  97. OVERLAY_TEST(fdt_overlay_add_node_by_path, 0);
  98. static int fdt_overlay_add_subnode_property(struct unit_test_state *uts)
  99. {
  100. int off;
  101. off = fdt_path_offset(fdt, "/test-node/sub-test-node");
  102. ut_assert(off >= 0);
  103. ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
  104. ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
  105. return CMD_RET_SUCCESS;
  106. }
  107. OVERLAY_TEST(fdt_overlay_add_subnode_property, 0);
  108. static int fdt_overlay_local_phandle(struct unit_test_state *uts)
  109. {
  110. uint32_t local_phandle;
  111. u32 val = 0;
  112. int off;
  113. off = fdt_path_offset(fdt, "/new-local-node");
  114. ut_assert(off >= 0);
  115. local_phandle = fdt_get_phandle(fdt, off);
  116. ut_assert(local_phandle);
  117. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  118. 0, &val));
  119. ut_asserteq(local_phandle, val);
  120. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  121. 1, &val));
  122. ut_asserteq(local_phandle, val);
  123. return CMD_RET_SUCCESS;
  124. }
  125. OVERLAY_TEST(fdt_overlay_local_phandle, 0);
  126. static int fdt_overlay_local_phandles(struct unit_test_state *uts)
  127. {
  128. uint32_t local_phandle, test_phandle;
  129. u32 val = 0;
  130. int off;
  131. off = fdt_path_offset(fdt, "/new-local-node");
  132. ut_assert(off >= 0);
  133. local_phandle = fdt_get_phandle(fdt, off);
  134. ut_assert(local_phandle);
  135. off = fdt_path_offset(fdt, "/test-node");
  136. ut_assert(off >= 0);
  137. test_phandle = fdt_get_phandle(fdt, off);
  138. ut_assert(test_phandle);
  139. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
  140. &val));
  141. ut_asserteq(test_phandle, val);
  142. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
  143. &val));
  144. ut_asserteq(local_phandle, val);
  145. return CMD_RET_SUCCESS;
  146. }
  147. OVERLAY_TEST(fdt_overlay_local_phandles, 0);
  148. static int fdt_overlay_stacked(struct unit_test_state *uts)
  149. {
  150. u32 val = 0;
  151. ut_assertok(ut_fdt_getprop_u32(fdt, "/new-local-node",
  152. "stacked-test-int-property", &val));
  153. ut_asserteq(43, val);
  154. return CMD_RET_SUCCESS;
  155. }
  156. OVERLAY_TEST(fdt_overlay_stacked, 0);
  157. int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  158. {
  159. struct unit_test *tests = ll_entry_start(struct unit_test,
  160. overlay_test);
  161. const int n_ents = ll_entry_count(struct unit_test, overlay_test);
  162. struct unit_test_state *uts;
  163. void *fdt_base = &__dtb_test_fdt_base_begin;
  164. void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
  165. void *fdt_overlay_stacked = &__dtb_test_fdt_overlay_stacked_begin;
  166. void *fdt_overlay_copy, *fdt_overlay_stacked_copy;
  167. int ret = -ENOMEM;
  168. uts = calloc(1, sizeof(*uts));
  169. if (!uts)
  170. return -ENOMEM;
  171. ut_assertok(fdt_check_header(fdt_base));
  172. ut_assertok(fdt_check_header(fdt_overlay));
  173. fdt = malloc(FDT_COPY_SIZE);
  174. if (!fdt)
  175. goto err1;
  176. fdt_overlay_copy = malloc(FDT_COPY_SIZE);
  177. if (!fdt_overlay_copy)
  178. goto err2;
  179. fdt_overlay_stacked_copy = malloc(FDT_COPY_SIZE);
  180. if (!fdt_overlay_stacked_copy)
  181. goto err3;
  182. /*
  183. * Resize the FDT 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_base, fdt, FDT_COPY_SIZE));
  189. /*
  190. * Resize the overlay to 4k so that we have room to operate on
  191. *
  192. * (and relocate it since the memory might be mapped
  193. * read-only)
  194. */
  195. ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
  196. FDT_COPY_SIZE));
  197. /*
  198. * Resize the stacked overlay to 4k so that we have room to operate on
  199. *
  200. * (and relocate it since the memory might be mapped
  201. * read-only)
  202. */
  203. ut_assertok(fdt_open_into(fdt_overlay_stacked, fdt_overlay_stacked_copy,
  204. FDT_COPY_SIZE));
  205. /* Apply the overlay */
  206. ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_copy));
  207. /* Apply the stacked overlay */
  208. ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_stacked_copy));
  209. ret = cmd_ut_category("overlay", "", tests, n_ents, argc, argv);
  210. free(fdt_overlay_stacked_copy);
  211. err3:
  212. free(fdt_overlay_copy);
  213. err2:
  214. free(fdt);
  215. err1:
  216. return ret;
  217. }