fdtdec_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  16. * Check if an operation failed, and if so, print an error
  17. *
  18. * @param oper_name Name of operation
  19. * @param err Error code to check
  20. *
  21. * @return 0 if ok, -1 if there was an error
  22. */
  23. static int fdt_checkerr(const char *oper_name, int err)
  24. {
  25. if (err) {
  26. printf("%s: %s: %s\n", __func__, oper_name, fdt_strerror(err));
  27. return -1;
  28. }
  29. return 0;
  30. }
  31. /**
  32. * Check the result of an operation and if incorrect, print an error
  33. *
  34. * @param oper_name Name of operation
  35. * @param expected Expected value
  36. * @param value Actual value
  37. *
  38. * @return 0 if ok, -1 if there was an error
  39. */
  40. static int checkval(const char *oper_name, int expected, int value)
  41. {
  42. if (expected != value) {
  43. printf("%s: %s: expected %d, but returned %d\n", __func__,
  44. oper_name, expected, value);
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. #define CHECK(op) if (fdt_checkerr(#op, op)) return -1
  50. #define CHECKVAL(op, expected) \
  51. if (checkval(#op, expected, op)) \
  52. return -1
  53. #define CHECKOK(op) CHECKVAL(op, 0)
  54. /* maximum number of nodes / aliases to generate */
  55. #define MAX_NODES 20
  56. /*
  57. * Make a test fdt
  58. *
  59. * @param fdt Device tree pointer
  60. * @param size Size of device tree blob
  61. * @param aliases Specifies alias assignments. Format is a list of items
  62. * separated by space. Items are #a where
  63. * # is the alias number
  64. * a is the node to point to
  65. * @param nodes Specifies nodes to generate (a=0, b=1), upper case
  66. * means to create a disabled node
  67. */
  68. static int make_fdt(void *fdt, int size, const char *aliases,
  69. const char *nodes)
  70. {
  71. char name[20], value[20];
  72. const char *s;
  73. int fd;
  74. CHECK(fdt_create(fdt, size));
  75. CHECK(fdt_finish_reservemap(fdt));
  76. CHECK(fdt_begin_node(fdt, ""));
  77. CHECK(fdt_begin_node(fdt, "aliases"));
  78. for (s = aliases; *s;) {
  79. sprintf(name, "i2c%c", *s);
  80. sprintf(value, "/i2c%d@0", s[1] - 'a');
  81. CHECK(fdt_property_string(fdt, name, value));
  82. s += 2 + (s[2] != '\0');
  83. }
  84. CHECK(fdt_end_node(fdt));
  85. for (s = nodes; *s; s++) {
  86. sprintf(value, "i2c%d@0", (*s & 0xdf) - 'A');
  87. CHECK(fdt_begin_node(fdt, value));
  88. CHECK(fdt_property_string(fdt, "compatible",
  89. fdtdec_get_compatible(COMPAT_UNKNOWN)));
  90. if (*s <= 'Z')
  91. CHECK(fdt_property_string(fdt, "status", "disabled"));
  92. CHECK(fdt_end_node(fdt));
  93. }
  94. CHECK(fdt_end_node(fdt));
  95. CHECK(fdt_finish(fdt));
  96. CHECK(fdt_pack(fdt));
  97. #if defined(DEBUG) && defined(CONFIG_SANDBOX)
  98. fd = os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT | OS_O_WRONLY);
  99. if (fd == -1) {
  100. printf("Could not open .dtb file to write\n");
  101. return -1;
  102. }
  103. os_write(fd, fdt, size);
  104. os_close(fd);
  105. #endif
  106. return 0;
  107. }
  108. static int run_test(const char *aliases, const char *nodes, const char *expect)
  109. {
  110. int list[MAX_NODES];
  111. const char *s;
  112. void *blob;
  113. int i;
  114. blob = malloc(FDT_SIZE);
  115. if (!blob) {
  116. printf("%s: out of memory\n", __func__);
  117. return 1;
  118. }
  119. printf("aliases=%s, nodes=%s, expect=%s: ", aliases, nodes, expect);
  120. CHECKVAL(make_fdt(blob, FDT_SIZE, aliases, nodes), 0);
  121. CHECKVAL(fdtdec_find_aliases_for_id(blob, "i2c",
  122. COMPAT_UNKNOWN,
  123. list, ARRAY_SIZE(list)), strlen(expect));
  124. /* Check we got the right ones */
  125. for (i = 0, s = expect; *s; s++, i++) {
  126. int want = *s;
  127. const char *name;
  128. int got = ' ';
  129. name = list[i] ? fdt_get_name(blob, list[i], NULL) : NULL;
  130. if (name)
  131. got = name[3] + 'a' - '0';
  132. if (got != want) {
  133. printf("Position %d: Expected '%c', got '%c' ('%s')\n",
  134. i, want, got, name);
  135. return 1;
  136. }
  137. }
  138. printf("pass\n");
  139. return 0;
  140. }
  141. static int do_test_fdtdec(cmd_tbl_t *cmdtp, int flag, int argc,
  142. char * const argv[])
  143. {
  144. /* basic tests */
  145. CHECKOK(run_test("", "", ""));
  146. CHECKOK(run_test("1e 3d", "", ""));
  147. /*
  148. * 'a' represents 0, 'b' represents 1, etc.
  149. * The first character is the alias number, the second is the node
  150. * number. So the params mean:
  151. * 0a 1b : point alias 0 to node 0 (a), alias 1 to node 1(b)
  152. * ab : to create nodes 0 and 1 (a and b)
  153. * ab : we expect the function to return two nodes, in
  154. * the order 0, 1
  155. */
  156. CHECKOK(run_test("0a 1b", "ab", "ab"));
  157. CHECKOK(run_test("0a 1c", "ab", "ab"));
  158. CHECKOK(run_test("1c", "ab", "ab"));
  159. CHECKOK(run_test("1b", "ab", "ab"));
  160. CHECKOK(run_test("0b", "ab", "ba"));
  161. CHECKOK(run_test("0b 2d", "dbc", "bcd"));
  162. CHECKOK(run_test("0d 3a 1c 2b", "dbac", "dcba"));
  163. /* things with holes */
  164. CHECKOK(run_test("1b 3d", "dbc", "cb d"));
  165. CHECKOK(run_test("1e 3d", "dbc", "bc d"));
  166. /* no aliases */
  167. CHECKOK(run_test("", "dbac", "dbac"));
  168. /* disabled nodes */
  169. CHECKOK(run_test("0d 3a 1c 2b", "dBac", "dc a"));
  170. CHECKOK(run_test("0b 2d", "DBc", "c"));
  171. CHECKOK(run_test("0b 4d 2c", "DBc", " c"));
  172. /* conflicting aliases - first one gets it */
  173. CHECKOK(run_test("2a 1a 0a", "a", " a"));
  174. CHECKOK(run_test("0a 1a 2a", "a", "a"));
  175. printf("Test passed\n");
  176. return 0;
  177. }
  178. U_BOOT_CMD(
  179. test_fdtdec, 3, 1, do_test_fdtdec,
  180. "test_fdtdec",
  181. "Run tests for fdtdec library");