summaryrefslogtreecommitdiffstats
path: root/commands/of_diff.c
blob: ec039776cf34a391112eb37fd5a0852b451426a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// SPDX-License-Identifier: GPL-2.0-only
// SPDX-FileCopyrightText: © 2019 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix

/* of_diff.c - compare device tree files */

#include <common.h>
#include <fs.h>
#include <libfile.h>
#include <of.h>
#include <command.h>
#include <malloc.h>
#include <complete.h>
#include <errno.h>
#include <linux/err.h>

static struct device_node *get_tree(const char *filename, struct device_node *root)
{
	struct device_node *node;
	void *fdt;
	size_t size;
	int ret;

	if (!strcmp(filename, "-")) {
		node = of_get_root_node();
		if (!node)
			return ERR_PTR(-ENOENT);

		return of_copy_node(NULL, node);
	}

	if (!strcmp(filename, "+")) {
		node = of_get_root_node();
		if (!node)
			return ERR_PTR(-ENOENT);

		node = of_copy_node(NULL, root);

		of_fix_tree(node);

		return node;
	}

	ret = read_file_2(filename, &size, &fdt, FILESIZE_MAX);
	if (ret)
		return ERR_PTR(ret);

	node = of_unflatten_dtb(fdt);

	free(fdt);

	return node;
}

static int do_of_diff(int argc, char *argv[])
{
	int ret = 0;
	struct device_node *a, *b, *root;

	if (argc < 3)
		return COMMAND_ERROR_USAGE;

	root = of_get_root_node();
	a = get_tree(argv[1], root);
	b = get_tree(argv[2], root);

	if (IS_ERR(a)) {
		printf("Cannot read %s: %pe\n", argv[1], a);
		ret = COMMAND_ERROR;
		a = NULL;
		goto out;
	}

	if (IS_ERR(b)) {
		printf("Cannot read %s: %pe\n", argv[2], b);
		ret = COMMAND_ERROR;
		b = NULL;
		goto out;
	}

	of_diff(a, b, 0);

	ret = 0;
out:
	if (a && a != root)
		of_delete_node(a);
	if (b && b != root)
		of_delete_node(b);

	return ret;
}

BAREBOX_CMD_HELP_START(of_diff)
BAREBOX_CMD_HELP_TEXT("This command prints a diff between two given device trees.")
BAREBOX_CMD_HELP_TEXT("The device trees are given as dtb files or:")
BAREBOX_CMD_HELP_TEXT("'-' to compare against the barebox live tree, or")
BAREBOX_CMD_HELP_TEXT("'+' to compare against the fixed barebox live tree")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(of_diff)
	.cmd		= do_of_diff,
	BAREBOX_CMD_DESC("diff device trees")
	BAREBOX_CMD_OPTS("<a> <b>")
	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
	BAREBOX_CMD_HELP(cmd_of_diff_help)
BAREBOX_CMD_END