blob: 065fed278cf3d8f1919d85f423e95546675067ce [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdc254f32012-01-17 08:20:51 +00002/*
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.
Simon Glassdc254f32012-01-17 08:20:51 +00007 */
8
9#include <common.h>
10#include <fdtdec.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Simon Glassdc254f32012-01-17 08:20:51 +000012#include <malloc.h>
13#include <os.h>
14
15/* The size of our test fdt blob */
16#define FDT_SIZE (16 * 1024)
17
18/**
19 * Check if an operation failed, and if so, print an error
20 *
21 * @param oper_name Name of operation
22 * @param err Error code to check
23 *
24 * @return 0 if ok, -1 if there was an error
25 */
26static int fdt_checkerr(const char *oper_name, int err)
27{
28 if (err) {
29 printf("%s: %s: %s\n", __func__, oper_name, fdt_strerror(err));
30 return -1;
31 }
32
33 return 0;
34}
35
36/**
37 * Check the result of an operation and if incorrect, print an error
38 *
39 * @param oper_name Name of operation
40 * @param expected Expected value
41 * @param value Actual value
42 *
43 * @return 0 if ok, -1 if there was an error
44 */
45static int checkval(const char *oper_name, int expected, int value)
46{
47 if (expected != value) {
48 printf("%s: %s: expected %d, but returned %d\n", __func__,
49 oper_name, expected, value);
50 return -1;
51 }
52
53 return 0;
54}
55
56#define CHECK(op) if (fdt_checkerr(#op, op)) return -1
57#define CHECKVAL(op, expected) \
58 if (checkval(#op, expected, op)) \
59 return -1
60#define CHECKOK(op) CHECKVAL(op, 0)
61
62/* maximum number of nodes / aliases to generate */
63#define MAX_NODES 20
64
65/*
66 * Make a test fdt
67 *
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
76 */
77static int make_fdt(void *fdt, int size, const char *aliases,
78 const char *nodes)
79{
80 char name[20], value[20];
81 const char *s;
Thierry Reding3db600c2019-03-21 19:10:05 +010082#if defined(DEBUG) && defined(CONFIG_SANDBOX)
Simon Glassdc254f32012-01-17 08:20:51 +000083 int fd;
Thierry Reding3db600c2019-03-21 19:10:05 +010084#endif
Simon Glassdc254f32012-01-17 08:20:51 +000085
86 CHECK(fdt_create(fdt, size));
87 CHECK(fdt_finish_reservemap(fdt));
88 CHECK(fdt_begin_node(fdt, ""));
89
90 CHECK(fdt_begin_node(fdt, "aliases"));
91 for (s = aliases; *s;) {
92 sprintf(name, "i2c%c", *s);
93 sprintf(value, "/i2c%d@0", s[1] - 'a');
94 CHECK(fdt_property_string(fdt, name, value));
95 s += 2 + (s[2] != '\0');
96 }
97 CHECK(fdt_end_node(fdt));
98
99 for (s = nodes; *s; s++) {
100 sprintf(value, "i2c%d@0", (*s & 0xdf) - 'A');
101 CHECK(fdt_begin_node(fdt, value));
102 CHECK(fdt_property_string(fdt, "compatible",
103 fdtdec_get_compatible(COMPAT_UNKNOWN)));
104 if (*s <= 'Z')
105 CHECK(fdt_property_string(fdt, "status", "disabled"));
106 CHECK(fdt_end_node(fdt));
107 }
108
109 CHECK(fdt_end_node(fdt));
110 CHECK(fdt_finish(fdt));
111 CHECK(fdt_pack(fdt));
112#if defined(DEBUG) && defined(CONFIG_SANDBOX)
113 fd = os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT | OS_O_WRONLY);
114 if (fd == -1) {
115 printf("Could not open .dtb file to write\n");
116 return -1;
117 }
118 os_write(fd, fdt, size);
119 os_close(fd);
120#endif
121 return 0;
122}
123
124static 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
131 blob = malloc(FDT_SIZE);
132 if (!blob) {
133 printf("%s: out of memory\n", __func__);
134 return 1;
135 }
136
137 printf("aliases=%s, nodes=%s, expect=%s: ", aliases, nodes, expect);
138 CHECKVAL(make_fdt(blob, FDT_SIZE, aliases, nodes), 0);
139 CHECKVAL(fdtdec_find_aliases_for_id(blob, "i2c",
140 COMPAT_UNKNOWN,
141 list, ARRAY_SIZE(list)), strlen(expect));
142
143 /* Check we got the right ones */
144 for (i = 0, s = expect; *s; s++, i++) {
145 int want = *s;
146 const char *name;
147 int got = ' ';
148
149 name = list[i] ? fdt_get_name(blob, list[i], NULL) : NULL;
150 if (name)
151 got = name[3] + 'a' - '0';
152
153 if (got != want) {
154 printf("Position %d: Expected '%c', got '%c' ('%s')\n",
155 i, want, got, name);
156 return 1;
157 }
158 }
159
160 printf("pass\n");
161 return 0;
162}
163
164static int do_test_fdtdec(cmd_tbl_t *cmdtp, int flag, int argc,
165 char * const argv[])
166{
167 /* basic tests */
168 CHECKOK(run_test("", "", ""));
169 CHECKOK(run_test("1e 3d", "", ""));
170
171 /*
172 * 'a' represents 0, 'b' represents 1, etc.
173 * The first character is the alias number, the second is the node
174 * number. So the params mean:
175 * 0a 1b : point alias 0 to node 0 (a), alias 1 to node 1(b)
176 * ab : to create nodes 0 and 1 (a and b)
177 * ab : we expect the function to return two nodes, in
178 * the order 0, 1
179 */
180 CHECKOK(run_test("0a 1b", "ab", "ab"));
181
182 CHECKOK(run_test("0a 1c", "ab", "ab"));
183 CHECKOK(run_test("1c", "ab", "ab"));
184 CHECKOK(run_test("1b", "ab", "ab"));
185 CHECKOK(run_test("0b", "ab", "ba"));
186 CHECKOK(run_test("0b 2d", "dbc", "bcd"));
187 CHECKOK(run_test("0d 3a 1c 2b", "dbac", "dcba"));
188
189 /* things with holes */
190 CHECKOK(run_test("1b 3d", "dbc", "cb d"));
191 CHECKOK(run_test("1e 3d", "dbc", "bc d"));
192
193 /* no aliases */
194 CHECKOK(run_test("", "dbac", "dbac"));
195
196 /* disabled nodes */
197 CHECKOK(run_test("0d 3a 1c 2b", "dBac", "dc a"));
198 CHECKOK(run_test("0b 2d", "DBc", "c"));
199 CHECKOK(run_test("0b 4d 2c", "DBc", " c"));
200
201 /* conflicting aliases - first one gets it */
202 CHECKOK(run_test("2a 1a 0a", "a", " a"));
203 CHECKOK(run_test("0a 1a 2a", "a", "a"));
204
205 printf("Test passed\n");
206 return 0;
207}
208
209U_BOOT_CMD(
210 test_fdtdec, 3, 1, do_test_fdtdec,
211 "test_fdtdec",
212 "Run tests for fdtdec library");