xref: /aosp_15_r20/external/dtc/tests/path_offset.c (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Testcase for fdt_path_offset()
5  * Copyright (C) 2006 David Gibson, IBM Corporation.
6  */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdint.h>
11 
12 #include <libfdt.h>
13 
14 #include "tests.h"
15 #include "testdata.h"
16 
check_subnode(void * fdt,int parent,const char * name)17 static int check_subnode(void *fdt, int parent, const char *name)
18 {
19 	int offset;
20 	const struct fdt_node_header *nh;
21 	uint32_t tag;
22 
23 	verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
24 	offset = fdt_subnode_offset(fdt, parent, name);
25 	verbose_printf("offset %d...", offset);
26 	if (offset < 0)
27 		FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
28 	nh = fdt_offset_ptr(fdt, offset, sizeof(*nh));
29 	verbose_printf("pointer %p\n", nh);
30 	if (! nh)
31 		FAIL("NULL retrieving subnode \"%s\"", name);
32 
33 	tag = fdt32_to_cpu(nh->tag);
34 
35 	if (tag != FDT_BEGIN_NODE)
36 		FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
37 	if (!nodename_eq(nh->name, name))
38 		FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
39 		     nh->name, name);
40 
41 	return offset;
42 }
43 
check_path_offset(void * fdt,char * path,int offset)44 static void check_path_offset(void *fdt, char *path, int offset)
45 {
46 	int rc;
47 
48 	verbose_printf("Checking offset of \"%s\" is %d...\n", path, offset);
49 
50 	rc = fdt_path_offset(fdt, path);
51 	if (rc == offset)
52 		return;
53 
54 	if (rc < 0)
55 		FAIL("fdt_path_offset(\"%s\") failed: %s",
56 		     path,  fdt_strerror(rc));
57 	else
58 		FAIL("fdt_path_offset(\"%s\") returned incorrect offset"
59 		     " %d instead of %d", path, rc, offset);
60 }
61 
check_path_offset_namelen(void * fdt,char * path,int namelen,int offset)62 static void check_path_offset_namelen(void *fdt, char *path, int namelen,
63 				      int offset)
64 {
65 	int rc;
66 
67 	verbose_printf("Checking offset of \"%s\" [first %d characters]"
68 		       " is %d...\n", path, namelen, offset);
69 
70 	rc = fdt_path_offset_namelen(fdt, path, namelen);
71 	if (rc == offset)
72 		return;
73 
74 	if (rc < 0)
75 		FAIL("fdt_path_offset_namelen(\"%s\", %d) failed: %s",
76 		     path, namelen, fdt_strerror(rc));
77 	else
78 		FAIL("fdt_path_offset_namelen(\"%s\", %d) returned incorrect"
79 		     " offset %d instead of %d", path, namelen, rc, offset);
80 }
81 
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84 	void *fdt;
85 	int subnode1_offset, subnode2_offset;
86 	int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
87 
88 	test_init(argc, argv);
89 	fdt = load_blob_arg(argc, argv);
90 
91 	check_path_offset(fdt, "/", 0);
92 
93 	subnode1_offset = check_subnode(fdt, 0, "subnode@1");
94 	subnode2_offset = check_subnode(fdt, 0, "subnode@2");
95 
96 	check_path_offset(fdt, "/subnode@1", subnode1_offset);
97 	check_path_offset(fdt, "/subnode@2", subnode2_offset);
98 
99 	subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
100 	subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0");
101 	subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode");
102 
103 	check_path_offset(fdt, "/subnode@1/subsubnode", subsubnode1_offset);
104 	check_path_offset(fdt, "/subnode@2/subsubnode@0", subsubnode2_offset);
105 	check_path_offset(fdt, "/subnode@2/subsubnode", subsubnode2_offset2);
106 
107 	/* Test paths with extraneous separators */
108 	check_path_offset(fdt, "", -FDT_ERR_BADPATH);
109 	check_path_offset(fdt, "//", 0);
110 	check_path_offset(fdt, "///", 0);
111 	check_path_offset(fdt, "//subnode@1", subnode1_offset);
112 	check_path_offset(fdt, "/subnode@1/", subnode1_offset);
113 	check_path_offset(fdt, "//subnode@1///", subnode1_offset);
114 	check_path_offset(fdt, "/subnode@2////subsubnode", subsubnode2_offset2);
115 
116 	/* Test fdt_path_offset_namelen() */
117 	check_path_offset_namelen(fdt, "/subnode@1", -1, -FDT_ERR_BADPATH);
118 	check_path_offset_namelen(fdt, "/subnode@1", 0, -FDT_ERR_BADPATH);
119 	check_path_offset_namelen(fdt, "/subnode@1", 1, 0);
120 	check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 10, subnode1_offset);
121 	check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 11, subnode1_offset);
122 	check_path_offset_namelen(fdt, "/subnode@2TRAILINGGARBAGE", 10, subnode2_offset);
123 	check_path_offset_namelen(fdt, "/subnode@2TRAILINGGARBAGE", 11, -FDT_ERR_NOTFOUND);
124 	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 23, subsubnode2_offset2);
125 	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 22, -FDT_ERR_NOTFOUND);
126 	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 24, subsubnode2_offset2);
127 	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 25, -FDT_ERR_NOTFOUND);
128 
129 	PASS();
130 }
131