2 * Some very basic tests for fdtdec, accessed through test_fdtdec command.
3 * They are easiest to use with sandbox.
5 * Copyright (c) 2011 The Chromium OS Authors.
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
31 /* The size of our test fdt blob */
32 #define FDT_SIZE (16 * 1024)
35 * Check if an operation failed, and if so, print an error
37 * @param oper_name Name of operation
38 * @param err Error code to check
40 * @return 0 if ok, -1 if there was an error
42 static int fdt_checkerr(const char *oper_name, int err)
45 printf("%s: %s: %s\n", __func__, oper_name, fdt_strerror(err));
53 * Check the result of an operation and if incorrect, print an error
55 * @param oper_name Name of operation
56 * @param expected Expected value
57 * @param value Actual value
59 * @return 0 if ok, -1 if there was an error
61 static int checkval(const char *oper_name, int expected, int value)
63 if (expected != value) {
64 printf("%s: %s: expected %d, but returned %d\n", __func__,
65 oper_name, expected, value);
72 #define CHECK(op) if (fdt_checkerr(#op, op)) return -1
73 #define CHECKVAL(op, expected) \
74 if (checkval(#op, expected, op)) \
76 #define CHECKOK(op) CHECKVAL(op, 0)
78 /* maximum number of nodes / aliases to generate */
84 * @param fdt Device tree pointer
85 * @param size Size of device tree blob
86 * @param aliases Specifies alias assignments. Format is a list of items
87 * separated by space. Items are #a where
88 * # is the alias number
89 * a is the node to point to
90 * @param nodes Specifies nodes to generate (a=0, b=1), upper case
91 * means to create a disabled node
93 static int make_fdt(void *fdt, int size, const char *aliases,
96 char name[20], value[20];
100 CHECK(fdt_create(fdt, size));
101 CHECK(fdt_finish_reservemap(fdt));
102 CHECK(fdt_begin_node(fdt, ""));
104 CHECK(fdt_begin_node(fdt, "aliases"));
105 for (s = aliases; *s;) {
106 sprintf(name, "i2c%c", *s);
107 sprintf(value, "/i2c%d@0", s[1] - 'a');
108 CHECK(fdt_property_string(fdt, name, value));
109 s += 2 + (s[2] != '\0');
111 CHECK(fdt_end_node(fdt));
113 for (s = nodes; *s; s++) {
114 sprintf(value, "i2c%d@0", (*s & 0xdf) - 'A');
115 CHECK(fdt_begin_node(fdt, value));
116 CHECK(fdt_property_string(fdt, "compatible",
117 fdtdec_get_compatible(COMPAT_UNKNOWN)));
119 CHECK(fdt_property_string(fdt, "status", "disabled"));
120 CHECK(fdt_end_node(fdt));
123 CHECK(fdt_end_node(fdt));
124 CHECK(fdt_finish(fdt));
125 CHECK(fdt_pack(fdt));
126 #if defined(DEBUG) && defined(CONFIG_SANDBOX)
127 fd = os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT | OS_O_WRONLY);
129 printf("Could not open .dtb file to write\n");
132 os_write(fd, fdt, size);
138 static int run_test(const char *aliases, const char *nodes, const char *expect)
145 blob = malloc(FDT_SIZE);
147 printf("%s: out of memory\n", __func__);
151 printf("aliases=%s, nodes=%s, expect=%s: ", aliases, nodes, expect);
152 CHECKVAL(make_fdt(blob, FDT_SIZE, aliases, nodes), 0);
153 CHECKVAL(fdtdec_find_aliases_for_id(blob, "i2c",
155 list, ARRAY_SIZE(list)), strlen(expect));
157 /* Check we got the right ones */
158 for (i = 0, s = expect; *s; s++, i++) {
163 name = list[i] ? fdt_get_name(blob, list[i], NULL) : NULL;
165 got = name[3] + 'a' - '0';
168 printf("Position %d: Expected '%c', got '%c' ('%s')\n",
178 static int do_test_fdtdec(cmd_tbl_t *cmdtp, int flag, int argc,
182 CHECKOK(run_test("", "", ""));
183 CHECKOK(run_test("1e 3d", "", ""));
186 * 'a' represents 0, 'b' represents 1, etc.
187 * The first character is the alias number, the second is the node
188 * number. So the params mean:
189 * 0a 1b : point alias 0 to node 0 (a), alias 1 to node 1(b)
190 * ab : to create nodes 0 and 1 (a and b)
191 * ab : we expect the function to return two nodes, in
194 CHECKOK(run_test("0a 1b", "ab", "ab"));
196 CHECKOK(run_test("0a 1c", "ab", "ab"));
197 CHECKOK(run_test("1c", "ab", "ab"));
198 CHECKOK(run_test("1b", "ab", "ab"));
199 CHECKOK(run_test("0b", "ab", "ba"));
200 CHECKOK(run_test("0b 2d", "dbc", "bcd"));
201 CHECKOK(run_test("0d 3a 1c 2b", "dbac", "dcba"));
203 /* things with holes */
204 CHECKOK(run_test("1b 3d", "dbc", "cb d"));
205 CHECKOK(run_test("1e 3d", "dbc", "bc d"));
208 CHECKOK(run_test("", "dbac", "dbac"));
211 CHECKOK(run_test("0d 3a 1c 2b", "dBac", "dc a"));
212 CHECKOK(run_test("0b 2d", "DBc", "c"));
213 CHECKOK(run_test("0b 4d 2c", "DBc", " c"));
215 /* conflicting aliases - first one gets it */
216 CHECKOK(run_test("2a 1a 0a", "a", " a"));
217 CHECKOK(run_test("0a 1a 2a", "a", "a"));
219 printf("Test passed\n");
224 test_fdtdec, 3, 1, do_test_fdtdec,
226 "Run tests for fdtdec library");