fdtdec_test.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Some very basic tests for fdtdec, accessed through test_fdtdec command.
  4. * They are easiest to use with sandbox.
  5. *
  6. * Copyright (c) 2011 The Chromium OS Authors.
  7. */
  8. #include <common.h>
  9. #include <fdtdec.h>
  10. #include <linux/libfdt.h>
  11. #include <malloc.h>
  12. #include <os.h>
  13. /* The size of our test fdt blob */
  14. #define FDT_SIZE (16 * 1024)
  15. #define CHECK(op) ({ \
  16. int err = op; \
  17. if (err < 0) { \
  18. printf("%s: %s: %s\n", __func__, #op, \
  19. fdt_strerror(err)); \
  20. return err; \
  21. } \
  22. \
  23. err; \
  24. })
  25. #define CHECKVAL(op, expected) ({ \
  26. int err = op; \
  27. if (err != expected) { \
  28. printf("%s: %s: expected %d, but returned %d\n",\
  29. __func__, #op, expected, err); \
  30. return err; \
  31. } \
  32. \
  33. err; \
  34. })
  35. #define CHECKOK(op) CHECKVAL(op, 0)
  36. /* maximum number of nodes / aliases to generate */
  37. #define MAX_NODES 20
  38. /*
  39. * Make a test fdt
  40. *
  41. * @param fdt Device tree pointer
  42. * @param size Size of device tree blob
  43. * @param aliases Specifies alias assignments. Format is a list of items
  44. * separated by space. Items are #a where
  45. * # is the alias number
  46. * a is the node to point to
  47. * @param nodes Specifies nodes to generate (a=0, b=1), upper case
  48. * means to create a disabled node
  49. */
  50. static int make_fdt(void *fdt, int size, const char *aliases,
  51. const char *nodes)
  52. {
  53. char name[20], value[20];
  54. const char *s;
  55. #if defined(DEBUG) && defined(CONFIG_SANDBOX)
  56. int fd;
  57. #endif
  58. CHECK(fdt_create(fdt, size));
  59. CHECK(fdt_finish_reservemap(fdt));
  60. CHECK(fdt_begin_node(fdt, ""));
  61. CHECK(fdt_begin_node(fdt, "aliases"));
  62. for (s = aliases; *s;) {
  63. sprintf(name, "i2c%c", *s);
  64. sprintf(value, "/i2c%d@0", s[1] - 'a');
  65. CHECK(fdt_property_string(fdt, name, value));
  66. s += 2 + (s[2] != '\0');
  67. }
  68. CHECK(fdt_end_node(fdt));
  69. for (s = nodes; *s; s++) {
  70. sprintf(value, "i2c%d@0", (*s & 0xdf) - 'A');
  71. CHECK(fdt_begin_node(fdt, value));
  72. CHECK(fdt_property_string(fdt, "compatible",
  73. fdtdec_get_compatible(COMPAT_UNKNOWN)));
  74. if (*s <= 'Z')
  75. CHECK(fdt_property_string(fdt, "status", "disabled"));
  76. CHECK(fdt_end_node(fdt));
  77. }
  78. CHECK(fdt_end_node(fdt));
  79. CHECK(fdt_finish(fdt));
  80. CHECK(fdt_pack(fdt));
  81. #if defined(DEBUG) && defined(CONFIG_SANDBOX)
  82. fd = os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT | OS_O_WRONLY);
  83. if (fd == -1) {
  84. printf("Could not open .dtb file to write\n");
  85. return -1;
  86. }
  87. os_write(fd, fdt, size);
  88. os_close(fd);
  89. #endif
  90. return 0;
  91. }
  92. static int run_test(const char *aliases, const char *nodes, const char *expect)
  93. {
  94. int list[MAX_NODES];
  95. const char *s;
  96. void *blob;
  97. int i;
  98. blob = malloc(FDT_SIZE);
  99. if (!blob) {
  100. printf("%s: out of memory\n", __func__);
  101. return 1;
  102. }
  103. printf("aliases=%s, nodes=%s, expect=%s: ", aliases, nodes, expect);
  104. CHECKVAL(make_fdt(blob, FDT_SIZE, aliases, nodes), 0);
  105. CHECKVAL(fdtdec_find_aliases_for_id(blob, "i2c",
  106. COMPAT_UNKNOWN,
  107. list, ARRAY_SIZE(list)), (int)strlen(expect));
  108. /* Check we got the right ones */
  109. for (i = 0, s = expect; *s; s++, i++) {
  110. int want = *s;
  111. const char *name;
  112. int got = ' ';
  113. name = list[i] ? fdt_get_name(blob, list[i], NULL) : NULL;
  114. if (name)
  115. got = name[3] + 'a' - '0';
  116. if (got != want) {
  117. printf("Position %d: Expected '%c', got '%c' ('%s')\n",
  118. i, want, got, name);
  119. return 1;
  120. }
  121. }
  122. printf("pass\n");
  123. free(blob);
  124. return 0;
  125. }
  126. static int make_fdt_carveout_device(void *fdt, uint32_t na, uint32_t ns)
  127. {
  128. const char *basename = "/display";
  129. struct fdt_memory carveout = {
  130. #ifdef CONFIG_PHYS_64BIT
  131. .start = 0x180000000,
  132. .end = 0x18fffffff,
  133. #else
  134. .start = 0x80000000,
  135. .end = 0x8fffffff,
  136. #endif
  137. };
  138. fdt32_t cells[4], *ptr = cells;
  139. uint32_t upper, lower;
  140. fdt_size_t size;
  141. char name[32];
  142. int offset;
  143. /* store one or two address cells */
  144. upper = upper_32_bits(carveout.start);
  145. lower = lower_32_bits(carveout.start);
  146. if (na > 1 && upper > 0)
  147. snprintf(name, sizeof(name), "%s@%x,%x", basename, upper,
  148. lower);
  149. else
  150. snprintf(name, sizeof(name), "%s@%x", basename, lower);
  151. if (na > 1)
  152. *ptr++ = cpu_to_fdt32(upper);
  153. *ptr++ = cpu_to_fdt32(lower);
  154. /* store one or two size cells */
  155. size = carveout.end - carveout.start + 1;
  156. upper = upper_32_bits(size);
  157. lower = lower_32_bits(size);
  158. if (ns > 1)
  159. *ptr++ = cpu_to_fdt32(upper);
  160. *ptr++ = cpu_to_fdt32(lower);
  161. offset = CHECK(fdt_add_subnode(fdt, 0, name + 1));
  162. CHECK(fdt_setprop(fdt, offset, "reg", cells, (na + ns) * sizeof(*cells)));
  163. return fdtdec_set_carveout(fdt, name, "memory-region", 0,
  164. "framebuffer", &carveout);
  165. }
  166. static int check_fdt_carveout(void *fdt, uint32_t address_cells,
  167. uint32_t size_cells)
  168. {
  169. #ifdef CONFIG_PHYS_64BIT
  170. const char *name = "/display@1,80000000";
  171. const struct fdt_memory expected = {
  172. .start = 0x180000000,
  173. .end = 0x18fffffff,
  174. };
  175. #else
  176. const char *name = "/display@80000000";
  177. const struct fdt_memory expected = {
  178. .start = 0x80000000,
  179. .end = 0x8fffffff,
  180. };
  181. #endif
  182. struct fdt_memory carveout;
  183. printf("carveout: %pap-%pap na=%u ns=%u: ", &expected.start,
  184. &expected.end, address_cells, size_cells);
  185. CHECK(fdtdec_get_carveout(fdt, name, "memory-region", 0, &carveout));
  186. if ((carveout.start != expected.start) ||
  187. (carveout.end != expected.end)) {
  188. printf("carveout: %pap-%pap, expected %pap-%pap\n",
  189. &carveout.start, &carveout.end,
  190. &expected.start, &expected.end);
  191. return 1;
  192. }
  193. printf("pass\n");
  194. return 0;
  195. }
  196. static int make_fdt_carveout(void *fdt, int size, uint32_t address_cells,
  197. uint32_t size_cells)
  198. {
  199. fdt32_t na = cpu_to_fdt32(address_cells);
  200. fdt32_t ns = cpu_to_fdt32(size_cells);
  201. #if defined(DEBUG) && defined(CONFIG_SANDBOX)
  202. char filename[512];
  203. int fd;
  204. #endif
  205. int err;
  206. CHECK(fdt_create(fdt, size));
  207. CHECK(fdt_finish_reservemap(fdt));
  208. CHECK(fdt_begin_node(fdt, ""));
  209. CHECK(fdt_property(fdt, "#address-cells", &na, sizeof(na)));
  210. CHECK(fdt_property(fdt, "#size-cells", &ns, sizeof(ns)));
  211. CHECK(fdt_end_node(fdt));
  212. CHECK(fdt_finish(fdt));
  213. CHECK(fdt_pack(fdt));
  214. CHECK(fdt_open_into(fdt, fdt, FDT_SIZE));
  215. err = make_fdt_carveout_device(fdt, address_cells, size_cells);
  216. #if defined(DEBUG) && defined(CONFIG_SANDBOX)
  217. snprintf(filename, sizeof(filename), "/tmp/fdtdec-carveout-%u-%u.dtb",
  218. address_cells, size_cells);
  219. fd = os_open(filename, OS_O_CREAT | OS_O_WRONLY);
  220. if (fd < 0) {
  221. printf("could not open .dtb file to write\n");
  222. goto out;
  223. }
  224. os_write(fd, fdt, size);
  225. os_close(fd);
  226. out:
  227. #endif
  228. return err;
  229. }
  230. static int check_carveout(void)
  231. {
  232. void *fdt;
  233. fdt = malloc(FDT_SIZE);
  234. if (!fdt) {
  235. printf("%s: out of memory\n", __func__);
  236. return 1;
  237. }
  238. #ifndef CONFIG_PHYS_64BIT
  239. CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 1), 0);
  240. CHECKOK(check_fdt_carveout(fdt, 1, 1));
  241. CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 2), 0);
  242. CHECKOK(check_fdt_carveout(fdt, 1, 2));
  243. #else
  244. CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 1), -FDT_ERR_BADVALUE);
  245. CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 1, 2), -FDT_ERR_BADVALUE);
  246. #endif
  247. CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 2, 1), 0);
  248. CHECKOK(check_fdt_carveout(fdt, 2, 1));
  249. CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 2, 2), 0);
  250. CHECKOK(check_fdt_carveout(fdt, 2, 2));
  251. free(fdt);
  252. return 0;
  253. }
  254. static int do_test_fdtdec(cmd_tbl_t *cmdtp, int flag, int argc,
  255. char * const argv[])
  256. {
  257. /* basic tests */
  258. CHECKOK(run_test("", "", ""));
  259. CHECKOK(run_test("1e 3d", "", ""));
  260. /*
  261. * 'a' represents 0, 'b' represents 1, etc.
  262. * The first character is the alias number, the second is the node
  263. * number. So the params mean:
  264. * 0a 1b : point alias 0 to node 0 (a), alias 1 to node 1(b)
  265. * ab : to create nodes 0 and 1 (a and b)
  266. * ab : we expect the function to return two nodes, in
  267. * the order 0, 1
  268. */
  269. CHECKOK(run_test("0a 1b", "ab", "ab"));
  270. CHECKOK(run_test("0a 1c", "ab", "ab"));
  271. CHECKOK(run_test("1c", "ab", "ab"));
  272. CHECKOK(run_test("1b", "ab", "ab"));
  273. CHECKOK(run_test("0b", "ab", "ba"));
  274. CHECKOK(run_test("0b 2d", "dbc", "bcd"));
  275. CHECKOK(run_test("0d 3a 1c 2b", "dbac", "dcba"));
  276. /* things with holes */
  277. CHECKOK(run_test("1b 3d", "dbc", "cb d"));
  278. CHECKOK(run_test("1e 3d", "dbc", "bc d"));
  279. /* no aliases */
  280. CHECKOK(run_test("", "dbac", "dbac"));
  281. /* disabled nodes */
  282. CHECKOK(run_test("0d 3a 1c 2b", "dBac", "dc a"));
  283. CHECKOK(run_test("0b 2d", "DBc", "c"));
  284. CHECKOK(run_test("0b 4d 2c", "DBc", " c"));
  285. /* conflicting aliases - first one gets it */
  286. CHECKOK(run_test("2a 1a 0a", "a", " a"));
  287. CHECKOK(run_test("0a 1a 2a", "a", "a"));
  288. CHECKOK(check_carveout());
  289. printf("Test passed\n");
  290. return 0;
  291. }
  292. U_BOOT_CMD(
  293. test_fdtdec, 3, 1, do_test_fdtdec,
  294. "test_fdtdec",
  295. "Run tests for fdtdec library");