1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Testcase for #address-cells and #size-cells handling
5  * Copyright (C) 2014 David Gibson, <david@gibson.dropbear.id.au>
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_node(const void * fdt,const char * path,int ac,int sc)17 static void check_node(const void *fdt, const char *path, int ac, int sc)
18 {
19 	int offset;
20 	int xac, xsc;
21 
22 	offset = fdt_path_offset(fdt, path);
23 	if (offset < 0)
24 		FAIL("Couldn't find path %s", path);
25 
26 	xac = fdt_address_cells(fdt, offset);
27 	xsc = fdt_size_cells(fdt, offset);
28 
29 	if (xac != ac)
30 		FAIL("Address cells for %s is %d instead of %d\n",
31 		     path, xac, ac);
32 	if (xsc != sc)
33 		FAIL("Size cells for %s is %d instead of %d\n",
34 		     path, xsc, sc);
35 }
36 
main(int argc,char * argv[])37 int main(int argc, char *argv[])
38 {
39 	void *fdt;
40 
41 	if (argc != 2)
42 		CONFIG("Usage: %s <dtb file>\n", argv[0]);
43 
44 	test_init(argc, argv);
45 	fdt = load_blob(argv[1]);
46 
47 	check_node(fdt, "/", 2, 2);
48 	check_node(fdt, "/identity-bus@0", 2, 1);
49 	check_node(fdt, "/simple-bus@1000000", 2, 1);
50 	check_node(fdt, "/discrete-bus@2000000", 1, 0);
51 	check_node(fdt, "/c0", -FDT_ERR_BADNCELLS, -FDT_ERR_BADNCELLS);
52 	check_node(fdt, "/c1", -FDT_ERR_BADNCELLS, -FDT_ERR_BADNCELLS);
53 	check_node(fdt, "/c2", -FDT_ERR_BADNCELLS, -FDT_ERR_BADNCELLS);
54 	check_node(fdt, "/c3", -FDT_ERR_BADNCELLS, 0);
55 	PASS();
56 }
57