• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 < 0)
52 		FAIL("fdt_path_offset(\"%s\") failed: %s",
53 		     path,  fdt_strerror(rc));
54 	if (rc != offset)
55 		FAIL("fdt_path_offset(\"%s\") returned incorrect offset"
56 		     " %d instead of %d", path, rc, offset);
57 }
58 
check_path_offset_namelen(void * fdt,char * path,int namelen,int offset)59 static void check_path_offset_namelen(void *fdt, char *path, int namelen,
60 				      int offset)
61 {
62 	int rc;
63 
64 	verbose_printf("Checking offset of \"%s\" [first %d characters]"
65 		       " is %d...\n", path, namelen, offset);
66 
67 	rc = fdt_path_offset_namelen(fdt, path, namelen);
68 	if (rc == offset)
69 		return;
70 
71 	if (rc < 0)
72 		FAIL("fdt_path_offset_namelen(\"%s\", %d) failed: %s",
73 		     path, namelen, fdt_strerror(rc));
74 	else
75 		FAIL("fdt_path_offset_namelen(\"%s\", %d) returned incorrect"
76 		     " offset %d instead of %d", path, namelen, rc, offset);
77 }
78 
main(int argc,char * argv[])79 int main(int argc, char *argv[])
80 {
81 	void *fdt;
82 	int subnode1_offset, subnode2_offset;
83 	int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
84 
85 	test_init(argc, argv);
86 	fdt = load_blob_arg(argc, argv);
87 
88 	check_path_offset(fdt, "/", 0);
89 
90 	subnode1_offset = check_subnode(fdt, 0, "subnode@1");
91 	subnode2_offset = check_subnode(fdt, 0, "subnode@2");
92 
93 	check_path_offset(fdt, "/subnode@1", subnode1_offset);
94 	check_path_offset(fdt, "/subnode@2", subnode2_offset);
95 
96 	subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
97 	subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0");
98 	subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode");
99 
100 	check_path_offset(fdt, "/subnode@1/subsubnode", subsubnode1_offset);
101 	check_path_offset(fdt, "/subnode@2/subsubnode@0", subsubnode2_offset);
102 	check_path_offset(fdt, "/subnode@2/subsubnode", subsubnode2_offset2);
103 
104 	/* Test paths with extraneous separators */
105 	check_path_offset(fdt, "//", 0);
106 	check_path_offset(fdt, "///", 0);
107 	check_path_offset(fdt, "//subnode@1", subnode1_offset);
108 	check_path_offset(fdt, "/subnode@1/", subnode1_offset);
109 	check_path_offset(fdt, "//subnode@1///", subnode1_offset);
110 	check_path_offset(fdt, "/subnode@2////subsubnode", subsubnode2_offset2);
111 
112 	/* Test fdt_path_offset_namelen() */
113 	check_path_offset_namelen(fdt, "/subnode@1", 1, 0);
114 	check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 10, subnode1_offset);
115 	check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 11, subnode1_offset);
116 	check_path_offset_namelen(fdt, "/subnode@2TRAILINGGARBAGE", 10, subnode2_offset);
117 	check_path_offset_namelen(fdt, "/subnode@2TRAILINGGARBAGE", 11, -FDT_ERR_NOTFOUND);
118 	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 23, subsubnode2_offset2);
119 	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 22, -FDT_ERR_NOTFOUND);
120 	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 24, subsubnode2_offset2);
121 	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 25, -FDT_ERR_NOTFOUND);
122 
123 	PASS();
124 }
125