xref: /aosp_15_r20/external/dtc/tests/references.c (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1*cd60bc56SAndroid Build Coastguard Worker // SPDX-License-Identifier: LGPL-2.1-or-later
2*cd60bc56SAndroid Build Coastguard Worker /*
3*cd60bc56SAndroid Build Coastguard Worker  * libfdt - Flat Device Tree manipulation
4*cd60bc56SAndroid Build Coastguard Worker  *	Testcase for phandle references in dtc
5*cd60bc56SAndroid Build Coastguard Worker  * Copyright (C) 2006 David Gibson, IBM Corporation.
6*cd60bc56SAndroid Build Coastguard Worker  */
7*cd60bc56SAndroid Build Coastguard Worker #include <stdlib.h>
8*cd60bc56SAndroid Build Coastguard Worker #include <stdio.h>
9*cd60bc56SAndroid Build Coastguard Worker #include <string.h>
10*cd60bc56SAndroid Build Coastguard Worker #include <stdint.h>
11*cd60bc56SAndroid Build Coastguard Worker 
12*cd60bc56SAndroid Build Coastguard Worker #include <libfdt.h>
13*cd60bc56SAndroid Build Coastguard Worker 
14*cd60bc56SAndroid Build Coastguard Worker #include "tests.h"
15*cd60bc56SAndroid Build Coastguard Worker #include "testdata.h"
16*cd60bc56SAndroid Build Coastguard Worker 
check_ref(const void * fdt,int node,uint32_t checkref)17*cd60bc56SAndroid Build Coastguard Worker static void check_ref(const void *fdt, int node, uint32_t checkref)
18*cd60bc56SAndroid Build Coastguard Worker {
19*cd60bc56SAndroid Build Coastguard Worker 	const fdt32_t *p;
20*cd60bc56SAndroid Build Coastguard Worker 	uint32_t ref;
21*cd60bc56SAndroid Build Coastguard Worker 	int len;
22*cd60bc56SAndroid Build Coastguard Worker 
23*cd60bc56SAndroid Build Coastguard Worker 	p = fdt_getprop(fdt, node, "ref", &len);
24*cd60bc56SAndroid Build Coastguard Worker 	if (!p)
25*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_getprop(%d, \"ref\"): %s", node, fdt_strerror(len));
26*cd60bc56SAndroid Build Coastguard Worker 	if (len != sizeof(*p))
27*cd60bc56SAndroid Build Coastguard Worker 		FAIL("'ref' in node at %d has wrong size (%d instead of %zd)",
28*cd60bc56SAndroid Build Coastguard Worker 		     node, len, sizeof(*p));
29*cd60bc56SAndroid Build Coastguard Worker 	ref = fdt32_to_cpu(*p);
30*cd60bc56SAndroid Build Coastguard Worker 	if (ref != checkref)
31*cd60bc56SAndroid Build Coastguard Worker 		FAIL("'ref' in node at %d has value 0x%x instead of 0x%x",
32*cd60bc56SAndroid Build Coastguard Worker 		     node, ref, checkref);
33*cd60bc56SAndroid Build Coastguard Worker 
34*cd60bc56SAndroid Build Coastguard Worker 	p = fdt_getprop(fdt, node, "lref", &len);
35*cd60bc56SAndroid Build Coastguard Worker 	if (!p)
36*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_getprop(%d, \"lref\"): %s", node, fdt_strerror(len));
37*cd60bc56SAndroid Build Coastguard Worker 	if (len != sizeof(*p))
38*cd60bc56SAndroid Build Coastguard Worker 		FAIL("'lref' in node at %d has wrong size (%d instead of %zd)",
39*cd60bc56SAndroid Build Coastguard Worker 		     node, len, sizeof(*p));
40*cd60bc56SAndroid Build Coastguard Worker 	ref = fdt32_to_cpu(*p);
41*cd60bc56SAndroid Build Coastguard Worker 	if (ref != checkref)
42*cd60bc56SAndroid Build Coastguard Worker 		FAIL("'lref' in node at %d has value 0x%x instead of 0x%x",
43*cd60bc56SAndroid Build Coastguard Worker 		     node, ref, checkref);
44*cd60bc56SAndroid Build Coastguard Worker }
45*cd60bc56SAndroid Build Coastguard Worker 
check_rref(const void * fdt)46*cd60bc56SAndroid Build Coastguard Worker static void check_rref(const void *fdt)
47*cd60bc56SAndroid Build Coastguard Worker {
48*cd60bc56SAndroid Build Coastguard Worker 	const fdt32_t *p;
49*cd60bc56SAndroid Build Coastguard Worker 	uint32_t ref;
50*cd60bc56SAndroid Build Coastguard Worker 	int len;
51*cd60bc56SAndroid Build Coastguard Worker 
52*cd60bc56SAndroid Build Coastguard Worker 	p = fdt_getprop(fdt, 0, "rref", &len);
53*cd60bc56SAndroid Build Coastguard Worker 	if (!p)
54*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_getprop(0, \"rref\"): %s", fdt_strerror(len));
55*cd60bc56SAndroid Build Coastguard Worker 	if (len != sizeof(*p))
56*cd60bc56SAndroid Build Coastguard Worker 		FAIL("'rref' in root node has wrong size (%d instead of %zd)",
57*cd60bc56SAndroid Build Coastguard Worker 		     len, sizeof(*p));
58*cd60bc56SAndroid Build Coastguard Worker 	ref = fdt32_to_cpu(*p);
59*cd60bc56SAndroid Build Coastguard Worker 	if (ref != fdt_get_phandle(fdt, 0))
60*cd60bc56SAndroid Build Coastguard Worker 		FAIL("'rref' in root node has value 0x%x instead of 0x0", ref);
61*cd60bc56SAndroid Build Coastguard Worker }
62*cd60bc56SAndroid Build Coastguard Worker 
main(int argc,char * argv[])63*cd60bc56SAndroid Build Coastguard Worker int main(int argc, char *argv[])
64*cd60bc56SAndroid Build Coastguard Worker {
65*cd60bc56SAndroid Build Coastguard Worker 	void *fdt;
66*cd60bc56SAndroid Build Coastguard Worker 	int n1, n2, n3, n4, n5, n6, err;
67*cd60bc56SAndroid Build Coastguard Worker 	uint32_t h1, h2, h4, h5, h6, hn;
68*cd60bc56SAndroid Build Coastguard Worker 
69*cd60bc56SAndroid Build Coastguard Worker 	test_init(argc, argv);
70*cd60bc56SAndroid Build Coastguard Worker 	fdt = load_blob_arg(argc, argv);
71*cd60bc56SAndroid Build Coastguard Worker 
72*cd60bc56SAndroid Build Coastguard Worker 	n1 = fdt_path_offset(fdt, "/node1");
73*cd60bc56SAndroid Build Coastguard Worker 	if (n1 < 0)
74*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_path_offset(/node1): %s", fdt_strerror(n1));
75*cd60bc56SAndroid Build Coastguard Worker 	n2 = fdt_path_offset(fdt, "/node2");
76*cd60bc56SAndroid Build Coastguard Worker 	if (n2 < 0)
77*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_path_offset(/node2): %s", fdt_strerror(n2));
78*cd60bc56SAndroid Build Coastguard Worker 	n3 = fdt_path_offset(fdt, "/node3");
79*cd60bc56SAndroid Build Coastguard Worker 	if (n3 < 0)
80*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_path_offset(/node3): %s", fdt_strerror(n3));
81*cd60bc56SAndroid Build Coastguard Worker 	n4 = fdt_path_offset(fdt, "/node4");
82*cd60bc56SAndroid Build Coastguard Worker 	if (n4 < 0)
83*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_path_offset(/node4): %s", fdt_strerror(n4));
84*cd60bc56SAndroid Build Coastguard Worker 	n5 = fdt_path_offset(fdt, "/node5");
85*cd60bc56SAndroid Build Coastguard Worker 	if (n5 < 0)
86*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_path_offset(/node5): %s", fdt_strerror(n5));
87*cd60bc56SAndroid Build Coastguard Worker 	n6 = fdt_path_offset(fdt, "/node6");
88*cd60bc56SAndroid Build Coastguard Worker 	if (n6 < 0)
89*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_path_offset(/node6): %s", fdt_strerror(n6));
90*cd60bc56SAndroid Build Coastguard Worker 
91*cd60bc56SAndroid Build Coastguard Worker 	h1 = fdt_get_phandle(fdt, n1);
92*cd60bc56SAndroid Build Coastguard Worker 	h2 = fdt_get_phandle(fdt, n2);
93*cd60bc56SAndroid Build Coastguard Worker 	h4 = fdt_get_phandle(fdt, n4);
94*cd60bc56SAndroid Build Coastguard Worker 	h5 = fdt_get_phandle(fdt, n5);
95*cd60bc56SAndroid Build Coastguard Worker 	h6 = fdt_get_phandle(fdt, n6);
96*cd60bc56SAndroid Build Coastguard Worker 
97*cd60bc56SAndroid Build Coastguard Worker 	if (h1 != 0x2000)
98*cd60bc56SAndroid Build Coastguard Worker 		FAIL("/node1 has wrong phandle, 0x%x instead of 0x%x",
99*cd60bc56SAndroid Build Coastguard Worker 		     h1, 0x2000);
100*cd60bc56SAndroid Build Coastguard Worker 	if (h2 != 0x1)
101*cd60bc56SAndroid Build Coastguard Worker 		FAIL("/node2 has wrong phandle, 0x%x instead of 0x%x",
102*cd60bc56SAndroid Build Coastguard Worker 		     h2, 0x1);
103*cd60bc56SAndroid Build Coastguard Worker 	if (h6 != FDT_MAX_PHANDLE)
104*cd60bc56SAndroid Build Coastguard Worker 		FAIL("/node6 has wrong phandle, 0x%x instead of 0x%x",
105*cd60bc56SAndroid Build Coastguard Worker 		     h6, FDT_MAX_PHANDLE);
106*cd60bc56SAndroid Build Coastguard Worker 	if ((h4 == 0x2000) || (h4 == 0x1) || (h4 == 0))
107*cd60bc56SAndroid Build Coastguard Worker 		FAIL("/node4 has bad phandle, 0x%x", h4);
108*cd60bc56SAndroid Build Coastguard Worker 
109*cd60bc56SAndroid Build Coastguard Worker 	if ((h5 == 0) || (h5 == ~0U))
110*cd60bc56SAndroid Build Coastguard Worker 		FAIL("/node5 has bad phandle, 0x%x", h5);
111*cd60bc56SAndroid Build Coastguard Worker 	if ((h5 == h4) || (h5 == h2) || (h5 == h1))
112*cd60bc56SAndroid Build Coastguard Worker 		FAIL("/node5 has duplicate phandle, 0x%x", h5);
113*cd60bc56SAndroid Build Coastguard Worker 
114*cd60bc56SAndroid Build Coastguard Worker 	/*
115*cd60bc56SAndroid Build Coastguard Worker 	 * /node6 has phandle FDT_MAX_PHANDLE, so fdt_generate_phandle() is
116*cd60bc56SAndroid Build Coastguard Worker 	 * expected to fail.
117*cd60bc56SAndroid Build Coastguard Worker 	 */
118*cd60bc56SAndroid Build Coastguard Worker 	err = fdt_generate_phandle(fdt, &hn);
119*cd60bc56SAndroid Build Coastguard Worker 	if (err != -FDT_ERR_NOPHANDLES)
120*cd60bc56SAndroid Build Coastguard Worker 		FAIL("generated invalid phandle 0x%x\n", hn);
121*cd60bc56SAndroid Build Coastguard Worker 
122*cd60bc56SAndroid Build Coastguard Worker 	check_ref(fdt, n1, h2);
123*cd60bc56SAndroid Build Coastguard Worker 	check_ref(fdt, n2, h1);
124*cd60bc56SAndroid Build Coastguard Worker 	check_ref(fdt, n3, h4);
125*cd60bc56SAndroid Build Coastguard Worker 
126*cd60bc56SAndroid Build Coastguard Worker 	check_rref(fdt);
127*cd60bc56SAndroid Build Coastguard Worker 
128*cd60bc56SAndroid Build Coastguard Worker 	PASS();
129*cd60bc56SAndroid Build Coastguard Worker }
130