xref: /netbsd-src/external/gpl2/dtc/dist/tests/check_path.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: check_path.c,v 1.1.1.1 2017/06/08 15:59:26 skrll Exp $	*/
2 
3 /*
4  * libfdt - Flat Device Tree manipulation
5  *	Testcase for node existence
6  * Copyright (C) 2016 Konsulko Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <stdio.h>
24 
25 #include <libfdt.h>
26 
27 #include "tests.h"
28 
29 #define CHECK(code) \
30 	{ \
31 		int err = (code); \
32 		if (err) \
33 			FAIL(#code ": %s", fdt_strerror(err)); \
34 	}
35 
36 /* 4k ought to be enough for anybody */
37 #define FDT_COPY_SIZE	(4 * 1024)
38 
39 static void *open_dt(char *path)
40 {
41 	void *dt, *copy;
42 
43 	dt = load_blob(path);
44 	copy = xmalloc(FDT_COPY_SIZE);
45 
46 	/*
47 	 * Resize our DTs to 4k so that we have room to operate on
48 	 */
49 	CHECK(fdt_open_into(dt, copy, FDT_COPY_SIZE));
50 
51 	return copy;
52 }
53 
54 int main(int argc, char *argv[])
55 {
56 	void *fdt_base;
57 	int fail_config, exists, check_exists;
58 
59 	test_init(argc, argv);
60 	fail_config = 0;
61 
62 	if (argc != 4)
63 		fail_config = 1;
64 
65 	if (!fail_config) {
66 		if (!strcmp(argv[2], "exists"))
67 			check_exists = 1;
68 		else if (!strcmp(argv[2], "not-exists"))
69 			check_exists = 0;
70 		else
71 			fail_config = 1;
72 	}
73 
74 	if (fail_config)
75 		CONFIG("Usage: %s <base dtb> <[exists|not-exists]> <node-path>", argv[0]);
76 
77 	fdt_base = open_dt(argv[1]);
78 
79 	exists = fdt_path_offset(fdt_base, argv[3]) >= 0;
80 
81 	if (exists == check_exists)
82 		PASS();
83 	else
84 		FAIL();
85 }
86