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