xref: /aosp_15_r20/external/dtc/tests/sw_states.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 error handling with sequential write states
5*cd60bc56SAndroid Build Coastguard Worker  * Copyright (C) 2018 David Gibson, Red Hat Inc.
6*cd60bc56SAndroid Build Coastguard Worker  */
7*cd60bc56SAndroid Build Coastguard Worker 
8*cd60bc56SAndroid Build Coastguard Worker #include <stdlib.h>
9*cd60bc56SAndroid Build Coastguard Worker #include <stdio.h>
10*cd60bc56SAndroid Build Coastguard Worker #include <string.h>
11*cd60bc56SAndroid Build Coastguard Worker #include <ctype.h>
12*cd60bc56SAndroid Build Coastguard Worker #include <stdint.h>
13*cd60bc56SAndroid Build Coastguard Worker 
14*cd60bc56SAndroid Build Coastguard Worker #include <libfdt.h>
15*cd60bc56SAndroid Build Coastguard Worker 
16*cd60bc56SAndroid Build Coastguard Worker #include "tests.h"
17*cd60bc56SAndroid Build Coastguard Worker #include "testdata.h"
18*cd60bc56SAndroid Build Coastguard Worker 
19*cd60bc56SAndroid Build Coastguard Worker #define SPACE	65536
20*cd60bc56SAndroid Build Coastguard Worker 
21*cd60bc56SAndroid Build Coastguard Worker #define CHECK_OK(code)							\
22*cd60bc56SAndroid Build Coastguard Worker 	do {								\
23*cd60bc56SAndroid Build Coastguard Worker 		verbose_printf(" OK: %s\n", #code);			\
24*cd60bc56SAndroid Build Coastguard Worker 		err = (code);						\
25*cd60bc56SAndroid Build Coastguard Worker 		if (err)						\
26*cd60bc56SAndroid Build Coastguard Worker 			FAIL(#code ": %s", fdt_strerror(err));		\
27*cd60bc56SAndroid Build Coastguard Worker 	} while (0)
28*cd60bc56SAndroid Build Coastguard Worker 
29*cd60bc56SAndroid Build Coastguard Worker #define CHECK_BADSTATE(code)						\
30*cd60bc56SAndroid Build Coastguard Worker 	do {								\
31*cd60bc56SAndroid Build Coastguard Worker 		verbose_printf("BAD: %s\n", #code);			\
32*cd60bc56SAndroid Build Coastguard Worker 		err = (code);						\
33*cd60bc56SAndroid Build Coastguard Worker 		if (err == 0)						\
34*cd60bc56SAndroid Build Coastguard Worker 			FAIL(#code ": succeeded in bad state");		\
35*cd60bc56SAndroid Build Coastguard Worker 		else if (err != -FDT_ERR_BADSTATE)			\
36*cd60bc56SAndroid Build Coastguard Worker 			FAIL(#code ": %s", fdt_strerror(err));		\
37*cd60bc56SAndroid Build Coastguard Worker 	} while (0)
38*cd60bc56SAndroid Build Coastguard Worker 
main(int argc,char * argv[])39*cd60bc56SAndroid Build Coastguard Worker int main(int argc, char *argv[])
40*cd60bc56SAndroid Build Coastguard Worker {
41*cd60bc56SAndroid Build Coastguard Worker 	void *fdt = NULL;
42*cd60bc56SAndroid Build Coastguard Worker 	int err;
43*cd60bc56SAndroid Build Coastguard Worker 
44*cd60bc56SAndroid Build Coastguard Worker 	test_init(argc, argv);
45*cd60bc56SAndroid Build Coastguard Worker 
46*cd60bc56SAndroid Build Coastguard Worker 	fdt = xmalloc(SPACE);
47*cd60bc56SAndroid Build Coastguard Worker 
48*cd60bc56SAndroid Build Coastguard Worker 	err = fdt_create(fdt, SPACE);
49*cd60bc56SAndroid Build Coastguard Worker 	if (err)
50*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_create(): %s", fdt_strerror(err));
51*cd60bc56SAndroid Build Coastguard Worker 
52*cd60bc56SAndroid Build Coastguard Worker 	/* Memory reserve state */
53*cd60bc56SAndroid Build Coastguard Worker 
54*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_begin_node(fdt, ""));
55*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_property_string(fdt, "bad-str", "TEST_STRING_1"));
56*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_end_node(fdt));
57*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_finish(fdt));
58*cd60bc56SAndroid Build Coastguard Worker 
59*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_add_reservemap_entry(fdt, TEST_ADDR_1, TEST_SIZE_1));
60*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_add_reservemap_entry(fdt, TEST_ADDR_2, TEST_SIZE_2));
61*cd60bc56SAndroid Build Coastguard Worker 
62*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_begin_node(fdt, ""));
63*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_property_string(fdt, "bad-str", "TEST_STRING_1"));
64*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_end_node(fdt));
65*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_finish(fdt));
66*cd60bc56SAndroid Build Coastguard Worker 
67*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_finish_reservemap(fdt));
68*cd60bc56SAndroid Build Coastguard Worker 
69*cd60bc56SAndroid Build Coastguard Worker 	/* Structure state */
70*cd60bc56SAndroid Build Coastguard Worker 
71*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_add_reservemap_entry(fdt, TEST_ADDR_1, TEST_SIZE_1));
72*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_finish_reservemap(fdt));
73*cd60bc56SAndroid Build Coastguard Worker 
74*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_begin_node(fdt, ""));
75*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_string(fdt, "compatible", "test_tree1"));
76*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_u32(fdt, "prop-int", TEST_VALUE_1));
77*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_u64(fdt, "prop-int64", TEST_VALUE64_1));
78*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_string(fdt, "prop-str", TEST_STRING_1));
79*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_u32(fdt, "#address-cells", 1));
80*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_u32(fdt, "#size-cells", 0));
81*cd60bc56SAndroid Build Coastguard Worker 
82*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_begin_node(fdt, "subnode@1"));
83*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_string(fdt, "compatible", "subnode1"));
84*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_u32(fdt, "reg", 1));
85*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_1));
86*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_begin_node(fdt, "subsubnode"));
87*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property(fdt, "compatible", "subsubnode1\0subsubnode",
88*cd60bc56SAndroid Build Coastguard Worker 			   23));
89*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_1));
90*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_end_node(fdt));
91*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_begin_node(fdt, "ss1"));
92*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_end_node(fdt));
93*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_end_node(fdt));
94*cd60bc56SAndroid Build Coastguard Worker 
95*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_begin_node(fdt, "subnode@2"));
96*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_u32(fdt, "reg", 2));
97*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_cell(fdt, "linux,phandle", PHANDLE_1));
98*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_2));
99*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_u32(fdt, "#address-cells", 1));
100*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_u32(fdt, "#size-cells", 0));
101*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_begin_node(fdt, "subsubnode@0"));
102*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_u32(fdt, "reg", 0));
103*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_cell(fdt, "phandle", PHANDLE_2));
104*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property(fdt, "compatible", "subsubnode2\0subsubnode",
105*cd60bc56SAndroid Build Coastguard Worker 			   23));
106*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_2));
107*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_end_node(fdt));
108*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_begin_node(fdt, "ss2"));
109*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_end_node(fdt));
110*cd60bc56SAndroid Build Coastguard Worker 
111*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_end_node(fdt));
112*cd60bc56SAndroid Build Coastguard Worker 
113*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_end_node(fdt));
114*cd60bc56SAndroid Build Coastguard Worker 
115*cd60bc56SAndroid Build Coastguard Worker 	CHECK_OK(fdt_finish(fdt));
116*cd60bc56SAndroid Build Coastguard Worker 
117*cd60bc56SAndroid Build Coastguard Worker 	/* Completed state */
118*cd60bc56SAndroid Build Coastguard Worker 
119*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_add_reservemap_entry(fdt, TEST_ADDR_1, TEST_SIZE_1));
120*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_finish_reservemap(fdt));
121*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_begin_node(fdt, ""));
122*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_property_string(fdt, "bad-str", "TEST_STRING_1"));
123*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_end_node(fdt));
124*cd60bc56SAndroid Build Coastguard Worker 	CHECK_BADSTATE(fdt_finish(fdt));
125*cd60bc56SAndroid Build Coastguard Worker 
126*cd60bc56SAndroid Build Coastguard Worker 	PASS();
127*cd60bc56SAndroid Build Coastguard Worker }
128