fdtdec_test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Some very basic tests for fdtdec, accessed through test_fdtdec command.
  3. * They are easiest to use with sandbox.
  4. *
  5. * Copyright (c) 2011 The Chromium OS Authors.
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <fdtdec.h>
  26. #include <libfdt.h>
  27. #include <malloc.h>
  28. #include <os.h>
  29. /* The size of our test fdt blob */
  30. #define FDT_SIZE (16 * 1024)
  31. /**
  32. * Check if an operation failed, and if so, print an error
  33. *
  34. * @param oper_name Name of operation
  35. * @param err Error code to check
  36. *
  37. * @return 0 if ok, -1 if there was an error
  38. */
  39. static int fdt_checkerr(const char *oper_name, int err)
  40. {
  41. if (err) {
  42. printf("%s: %s: %s\n", __func__, oper_name, fdt_strerror(err));
  43. return -1;
  44. }
  45. return 0;
  46. }
  47. /**
  48. * Check the result of an operation and if incorrect, print an error
  49. *
  50. * @param oper_name Name of operation
  51. * @param expected Expected value
  52. * @param value Actual value
  53. *
  54. * @return 0 if ok, -1 if there was an error
  55. */
  56. static int checkval(const char *oper_name, int expected, int value)
  57. {
  58. if (expected != value) {
  59. printf("%s: %s: expected %d, but returned %d\n", __func__,
  60. oper_name, expected, value);
  61. return -1;
  62. }
  63. return 0;
  64. }
  65. #define CHECK(op) if (fdt_checkerr(#op, op)) return -1
  66. #define CHECKVAL(op, expected) \
  67. if (checkval(#op, expected, op)) \
  68. return -1
  69. #define CHECKOK(op) CHECKVAL(op, 0)
  70. /* maximum number of nodes / aliases to generate */
  71. #define MAX_NODES 20
  72. /*
  73. * Make a test fdt
  74. *
  75. * @param fdt Device tree pointer
  76. * @param size Size of device tree blob
  77. * @param aliases Specifies alias assignments. Format is a list of items
  78. * separated by space. Items are #a where
  79. * # is the alias number
  80. * a is the node to point to
  81. * @param nodes Specifies nodes to generate (a=0, b=1), upper case
  82. * means to create a disabled node
  83. */
  84. static int make_fdt(void *fdt, int size, const char *aliases,
  85. const char *nodes)
  86. {
  87. char name[20], value[20];
  88. const char *s;
  89. int fd;
  90. CHECK(fdt_create(fdt, size));
  91. CHECK(fdt_finish_reservemap(fdt));
  92. CHECK(fdt_begin_node(fdt, ""));
  93. CHECK(fdt_begin_node(fdt, "aliases"));
  94. for (s = aliases; *s;) {
  95. sprintf(name, "i2c%c", *s);
  96. sprintf(value, "/i2c%d@0", s[1] - 'a');
  97. CHECK(fdt_property_string(fdt, name, value));
  98. s += 2 + (s[2] != '\0');
  99. }
  100. CHECK(fdt_end_node(fdt));
  101. for (s = nodes; *s; s++) {
  102. sprintf(value, "i2c%d@0", (*s & 0xdf) - 'A');
  103. CHECK(fdt_begin_node(fdt, value));
  104. CHECK(fdt_property_string(fdt, "compatible",
  105. fdtdec_get_compatible(COMPAT_UNKNOWN)));
  106. if (*s <= 'Z')
  107. CHECK(fdt_property_string(fdt, "status", "disabled"));
  108. CHECK(fdt_end_node(fdt));
  109. }
  110. CHECK(fdt_end_node(fdt));
  111. CHECK(fdt_finish(fdt));
  112. CHECK(fdt_pack(fdt));
  113. #if defined(DEBUG) && defined(CONFIG_SANDBOX)
  114. fd = os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT | OS_O_WRONLY);
  115. if (fd == -1) {
  116. printf("Could not open .dtb file to write\n");
  117. return -1;
  118. }
  119. os_write(fd, fdt, size);
  120. os_close(fd);
  121. #endif
  122. return 0;
  123. }
  124. static int run_test(const char *aliases, const char *nodes, const char *expect)
  125. {
  126. int list[MAX_NODES];
  127. const char *s;
  128. void *blob;
  129. int i;
  130. blob = malloc(FDT_SIZE);
  131. if (!blob) {
  132. printf("%s: out of memory\n", __func__);
  133. return 1;
  134. }
  135. printf("aliases=%s, nodes=%s, expect=%s: ", aliases, nodes, expect);
  136. CHECKVAL(make_fdt(blob, FDT_SIZE, aliases, nodes), 0);
  137. CHECKVAL(fdtdec_find_aliases_for_id(blob, "i2c",
  138. COMPAT_UNKNOWN,
  139. list, ARRAY_SIZE(list)), strlen(expect));
  140. /* Check we got the right ones */
  141. for (i = 0, s = expect; *s; s++, i++) {
  142. int want = *s;
  143. const char *name;
  144. int got = ' ';
  145. name = list[i] ? fdt_get_name(blob, list[i], NULL) : NULL;
  146. if (name)
  147. got = name[3] + 'a' - '0';
  148. if (got != want) {
  149. printf("Position %d: Expected '%c', got '%c' ('%s')\n",
  150. i, want, got, name);
  151. return 1;
  152. }
  153. }
  154. printf("pass\n");
  155. return 0;
  156. }
  157. static int do_test_fdtdec(cmd_tbl_t *cmdtp, int flag, int argc,
  158. char * const argv[])
  159. {
  160. /* basic tests */
  161. CHECKOK(run_test("", "", ""));
  162. CHECKOK(run_test("1e 3d", "", ""));
  163. /*
  164. * 'a' represents 0, 'b' represents 1, etc.
  165. * The first character is the alias number, the second is the node
  166. * number. So the params mean:
  167. * 0a 1b : point alias 0 to node 0 (a), alias 1 to node 1(b)
  168. * ab : to create nodes 0 and 1 (a and b)
  169. * ab : we expect the function to return two nodes, in
  170. * the order 0, 1
  171. */
  172. CHECKOK(run_test("0a 1b", "ab", "ab"));
  173. CHECKOK(run_test("0a 1c", "ab", "ab"));
  174. CHECKOK(run_test("1c", "ab", "ab"));
  175. CHECKOK(run_test("1b", "ab", "ab"));
  176. CHECKOK(run_test("0b", "ab", "ba"));
  177. CHECKOK(run_test("0b 2d", "dbc", "bcd"));
  178. CHECKOK(run_test("0d 3a 1c 2b", "dbac", "dcba"));
  179. /* things with holes */
  180. CHECKOK(run_test("1b 3d", "dbc", "cb d"));
  181. CHECKOK(run_test("1e 3d", "dbc", "bc d"));
  182. /* no aliases */
  183. CHECKOK(run_test("", "dbac", "dbac"));
  184. /* disabled nodes */
  185. CHECKOK(run_test("0d 3a 1c 2b", "dBac", "dc a"));
  186. CHECKOK(run_test("0b 2d", "DBc", "c"));
  187. CHECKOK(run_test("0b 4d 2c", "DBc", " c"));
  188. /* conflicting aliases - first one gets it */
  189. CHECKOK(run_test("2a 1a 0a", "a", " a"));
  190. CHECKOK(run_test("0a 1a 2a", "a", "a"));
  191. printf("Test passed\n");
  192. return 0;
  193. }
  194. U_BOOT_CMD(
  195. test_fdtdec, 3, 1, do_test_fdtdec,
  196. "test_fdtdec",
  197. "Run tests for fdtdec library");