fdtdec_test.c 8.5 KB

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