]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib/fdtdec_test.c
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 * SPDX-License-Identifier: GPL-2.0+
15 /* The size of our test fdt blob */
16 #define FDT_SIZE (16 * 1024)
19 * Check if an operation failed, and if so, print an error
21 * @param oper_name Name of operation
22 * @param err Error code to check
24 * @return 0 if ok, -1 if there was an error
26 static int fdt_checkerr(const char *oper_name
, int err
)
29 printf("%s: %s: %s\n", __func__
, oper_name
, fdt_strerror(err
));
37 * Check the result of an operation and if incorrect, print an error
39 * @param oper_name Name of operation
40 * @param expected Expected value
41 * @param value Actual value
43 * @return 0 if ok, -1 if there was an error
45 static int checkval(const char *oper_name
, int expected
, int value
)
47 if (expected
!= value
) {
48 printf("%s: %s: expected %d, but returned %d\n", __func__
,
49 oper_name
, expected
, value
);
56 #define CHECK(op) if (fdt_checkerr(#op, op)) return -1
57 #define CHECKVAL(op, expected) \
58 if (checkval(#op, expected, op)) \
60 #define CHECKOK(op) CHECKVAL(op, 0)
62 /* maximum number of nodes / aliases to generate */
68 * @param fdt Device tree pointer
69 * @param size Size of device tree blob
70 * @param aliases Specifies alias assignments. Format is a list of items
71 * separated by space. Items are #a where
72 * # is the alias number
73 * a is the node to point to
74 * @param nodes Specifies nodes to generate (a=0, b=1), upper case
75 * means to create a disabled node
77 static int make_fdt(void *fdt
, int size
, const char *aliases
,
80 char name
[20], value
[20];
84 CHECK(fdt_create(fdt
, size
));
85 CHECK(fdt_finish_reservemap(fdt
));
86 CHECK(fdt_begin_node(fdt
, ""));
88 CHECK(fdt_begin_node(fdt
, "aliases"));
89 for (s
= aliases
; *s
;) {
90 sprintf(name
, "i2c%c", *s
);
91 sprintf(value
, "/i2c%d@0", s
[1] - 'a');
92 CHECK(fdt_property_string(fdt
, name
, value
));
93 s
+= 2 + (s
[2] != '\0');
95 CHECK(fdt_end_node(fdt
));
97 for (s
= nodes
; *s
; s
++) {
98 sprintf(value
, "i2c%d@0", (*s
& 0xdf) - 'A');
99 CHECK(fdt_begin_node(fdt
, value
));
100 CHECK(fdt_property_string(fdt
, "compatible",
101 fdtdec_get_compatible(COMPAT_UNKNOWN
)));
103 CHECK(fdt_property_string(fdt
, "status", "disabled"));
104 CHECK(fdt_end_node(fdt
));
107 CHECK(fdt_end_node(fdt
));
108 CHECK(fdt_finish(fdt
));
109 CHECK(fdt_pack(fdt
));
110 #if defined(DEBUG) && defined(CONFIG_SANDBOX)
111 fd
= os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT
| OS_O_WRONLY
);
113 printf("Could not open .dtb file to write\n");
116 os_write(fd
, fdt
, size
);
122 static int run_test(const char *aliases
, const char *nodes
, const char *expect
)
129 blob
= malloc(FDT_SIZE
);
131 printf("%s: out of memory\n", __func__
);
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",
139 list
, ARRAY_SIZE(list
)), strlen(expect
));
141 /* Check we got the right ones */
142 for (i
= 0, s
= expect
; *s
; s
++, i
++) {
147 name
= list
[i
] ? fdt_get_name(blob
, list
[i
], NULL
) : NULL
;
149 got
= name
[3] + 'a' - '0';
152 printf("Position %d: Expected '%c', got '%c' ('%s')\n",
162 static int do_test_fdtdec(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
166 CHECKOK(run_test("", "", ""));
167 CHECKOK(run_test("1e 3d", "", ""));
170 * 'a' represents 0, 'b' represents 1, etc.
171 * The first character is the alias number, the second is the node
172 * number. So the params mean:
173 * 0a 1b : point alias 0 to node 0 (a), alias 1 to node 1(b)
174 * ab : to create nodes 0 and 1 (a and b)
175 * ab : we expect the function to return two nodes, in
178 CHECKOK(run_test("0a 1b", "ab", "ab"));
180 CHECKOK(run_test("0a 1c", "ab", "ab"));
181 CHECKOK(run_test("1c", "ab", "ab"));
182 CHECKOK(run_test("1b", "ab", "ab"));
183 CHECKOK(run_test("0b", "ab", "ba"));
184 CHECKOK(run_test("0b 2d", "dbc", "bcd"));
185 CHECKOK(run_test("0d 3a 1c 2b", "dbac", "dcba"));
187 /* things with holes */
188 CHECKOK(run_test("1b 3d", "dbc", "cb d"));
189 CHECKOK(run_test("1e 3d", "dbc", "bc d"));
192 CHECKOK(run_test("", "dbac", "dbac"));
195 CHECKOK(run_test("0d 3a 1c 2b", "dBac", "dc a"));
196 CHECKOK(run_test("0b 2d", "DBc", "c"));
197 CHECKOK(run_test("0b 4d 2c", "DBc", " c"));
199 /* conflicting aliases - first one gets it */
200 CHECKOK(run_test("2a 1a 0a", "a", " a"));
201 CHECKOK(run_test("0a 1a 2a", "a", "a"));
203 printf("Test passed\n");
208 test_fdtdec
, 3, 1, do_test_fdtdec
,
210 "Run tests for fdtdec library");