1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * dumptrees - utility for libfdt testing
4  *
5  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2006.
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <stdint.h>
12 
13 #include <libfdt.h>
14 
15 #include "testdata.h"
16 
17 static struct {
18 	void *blob;
19 	const char *filename;
20 } trees[] = {
21 #define TREE(name)	{ &name, #name ".dtb" }
22 	TREE(test_tree1),
23 	TREE(bad_node_char), TREE(bad_node_format), TREE(bad_prop_char),
24 	TREE(ovf_size_strings),
25 	TREE(truncated_property), TREE(truncated_string),
26 	TREE(truncated_memrsv),
27 	TREE(two_roots),
28 	TREE(named_root)
29 };
30 
31 #define NUM_TREES	(sizeof(trees) / sizeof(trees[0]))
32 
main(int argc,char * argv[])33 int main(int argc, char *argv[])
34 {
35 	int i;
36 
37 	if (argc != 2) {
38 	    fprintf(stderr, "Missing output directory argument\n");
39 	    return 1;
40 	}
41 
42 	if (chdir(argv[1]) != 0) {
43 	    perror("chdir()");
44 	    return 1;
45 	}
46 
47 	for (i = 0; i < NUM_TREES; i++) {
48 		void *blob = trees[i].blob;
49 		const char *filename = trees[i].filename;
50 		int size;
51 		int fd;
52 		int ret;
53 
54 		size = fdt_totalsize(blob);
55 
56 		printf("Tree \"%s\", %d bytes\n", filename, size);
57 
58 		fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
59 		if (fd < 0)
60 			perror("open()");
61 
62 		ret = write(fd, blob, size);
63 		if (ret != size)
64 			perror("write()");
65 
66 		close(fd);
67 	}
68 	exit(0);
69 }
70