cmd_ut_overlay.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <image.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <linux/sizes.h>
  14. #include <test/ut.h>
  15. #include <test/overlay.h>
  16. #include <test/suites.h>
  17. /* 4k ought to be enough for anybody */
  18. #define FDT_COPY_SIZE (4 * SZ_1K)
  19. extern u32 __dtb_test_fdt_base_begin;
  20. extern u32 __dtb_test_fdt_overlay_begin;
  21. extern u32 __dtb_test_fdt_overlay_stacked_begin;
  22. static void *fdt;
  23. static int ut_fdt_getprop_u32_by_index(void *fdt, const char *path,
  24. const char *name, int index,
  25. u32 *out)
  26. {
  27. const fdt32_t *val;
  28. int node_off;
  29. int len;
  30. node_off = fdt_path_offset(fdt, path);
  31. if (node_off < 0)
  32. return node_off;
  33. val = fdt_getprop(fdt, node_off, name, &len);
  34. if (!val || (len < (sizeof(uint32_t) * (index + 1))))
  35. return -FDT_ERR_NOTFOUND;
  36. *out = fdt32_to_cpu(*(val + index));
  37. return 0;
  38. }
  39. static int ut_fdt_getprop_u32(void *fdt, const char *path, const char *name,
  40. u32 *out)
  41. {
  42. return ut_fdt_getprop_u32_by_index(fdt, path, name, 0, out);
  43. }
  44. static int fdt_getprop_str(void *fdt, const char *path, const char *name,
  45. const char **out)
  46. {
  47. int node_off;
  48. int len;
  49. node_off = fdt_path_offset(fdt, path);
  50. if (node_off < 0)
  51. return node_off;
  52. *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
  53. return len < 0 ? len : 0;
  54. }
  55. static int fdt_overlay_change_int_property(struct unit_test_state *uts)
  56. {
  57. u32 val = 0;
  58. ut_assertok(ut_fdt_getprop_u32(fdt, "/test-node", "test-int-property",
  59. &val));
  60. ut_asserteq(43, val);
  61. return CMD_RET_SUCCESS;
  62. }
  63. OVERLAY_TEST(fdt_overlay_change_int_property, 0);
  64. static int fdt_overlay_change_str_property(struct unit_test_state *uts)
  65. {
  66. const char *val = NULL;
  67. ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
  68. &val));
  69. ut_asserteq_str("foobar", val);
  70. return CMD_RET_SUCCESS;
  71. }
  72. OVERLAY_TEST(fdt_overlay_change_str_property, 0);
  73. static int fdt_overlay_add_str_property(struct unit_test_state *uts)
  74. {
  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. 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. 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. int off;
  103. off = fdt_path_offset(fdt, "/test-node/sub-test-node");
  104. ut_assert(off >= 0);
  105. ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
  106. ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
  107. return CMD_RET_SUCCESS;
  108. }
  109. OVERLAY_TEST(fdt_overlay_add_subnode_property, 0);
  110. static int fdt_overlay_local_phandle(struct unit_test_state *uts)
  111. {
  112. uint32_t local_phandle;
  113. u32 val = 0;
  114. int off;
  115. off = fdt_path_offset(fdt, "/new-local-node");
  116. ut_assert(off >= 0);
  117. local_phandle = fdt_get_phandle(fdt, off);
  118. ut_assert(local_phandle);
  119. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  120. 0, &val));
  121. ut_asserteq(local_phandle, val);
  122. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
  123. 1, &val));
  124. ut_asserteq(local_phandle, val);
  125. return CMD_RET_SUCCESS;
  126. }
  127. OVERLAY_TEST(fdt_overlay_local_phandle, 0);
  128. static int fdt_overlay_local_phandles(struct unit_test_state *uts)
  129. {
  130. uint32_t local_phandle, test_phandle;
  131. u32 val = 0;
  132. int off;
  133. off = fdt_path_offset(fdt, "/new-local-node");
  134. ut_assert(off >= 0);
  135. local_phandle = fdt_get_phandle(fdt, off);
  136. ut_assert(local_phandle);
  137. off = fdt_path_offset(fdt, "/test-node");
  138. ut_assert(off >= 0);
  139. test_phandle = fdt_get_phandle(fdt, off);
  140. ut_assert(test_phandle);
  141. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
  142. &val));
  143. ut_asserteq(test_phandle, val);
  144. ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
  145. &val));
  146. ut_asserteq(local_phandle, val);
  147. return CMD_RET_SUCCESS;
  148. }
  149. OVERLAY_TEST(fdt_overlay_local_phandles, 0);
  150. static int fdt_overlay_stacked(struct unit_test_state *uts)
  151. {
  152. u32 val = 0;
  153. ut_assertok(ut_fdt_getprop_u32(fdt, "/new-local-node",
  154. "stacked-test-int-property", &val));
  155. ut_asserteq(43, val);
  156. return CMD_RET_SUCCESS;
  157. }
  158. OVERLAY_TEST(fdt_overlay_stacked, 0);
  159. int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  160. {
  161. struct unit_test *tests = ll_entry_start(struct unit_test,
  162. overlay_test);
  163. const int n_ents = ll_entry_count(struct unit_test, overlay_test);
  164. struct unit_test_state *uts;
  165. void *fdt_base = &__dtb_test_fdt_base_begin;
  166. void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
  167. void *fdt_overlay_stacked = &__dtb_test_fdt_overlay_stacked_begin;
  168. void *fdt_overlay_copy, *fdt_overlay_stacked_copy;
  169. int ret = -ENOMEM;
  170. uts = calloc(1, sizeof(*uts));
  171. if (!uts)
  172. return -ENOMEM;
  173. ut_assertok(fdt_check_header(fdt_base));
  174. ut_assertok(fdt_check_header(fdt_overlay));
  175. fdt = malloc(FDT_COPY_SIZE);
  176. if (!fdt)
  177. goto err1;
  178. fdt_overlay_copy = malloc(FDT_COPY_SIZE);
  179. if (!fdt_overlay_copy)
  180. goto err2;
  181. fdt_overlay_stacked_copy = malloc(FDT_COPY_SIZE);
  182. if (!fdt_overlay_stacked_copy)
  183. goto err3;
  184. /*
  185. * Resize the FDT to 4k so that we have room to operate on
  186. *
  187. * (and relocate it since the memory might be mapped
  188. * read-only)
  189. */
  190. ut_assertok(fdt_open_into(fdt_base, fdt, FDT_COPY_SIZE));
  191. /*
  192. * Resize the overlay 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_overlay, fdt_overlay_copy,
  198. FDT_COPY_SIZE));
  199. /*
  200. * Resize the stacked overlay to 4k so that we have room to operate on
  201. *
  202. * (and relocate it since the memory might be mapped
  203. * read-only)
  204. */
  205. ut_assertok(fdt_open_into(fdt_overlay_stacked, fdt_overlay_stacked_copy,
  206. FDT_COPY_SIZE));
  207. /* Apply the overlay */
  208. ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_copy));
  209. /* Apply the stacked overlay */
  210. ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_stacked_copy));
  211. ret = cmd_ut_category("overlay", "", tests, n_ents, argc, argv);
  212. free(fdt_overlay_stacked_copy);
  213. err3:
  214. free(fdt_overlay_copy);
  215. err2:
  216. free(fdt);
  217. err1:
  218. return ret;
  219. }