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 };
28
29 #define NUM_TREES (sizeof(trees) / sizeof(trees[0]))
30
main(int argc,char * argv[])31 int main(int argc, char *argv[])
32 {
33 int i;
34
35 if (argc != 2) {
36 fprintf(stderr, "Missing output directory argument\n");
37 return 1;
38 }
39
40 if (chdir(argv[1]) != 0) {
41 perror("chdir()");
42 return 1;
43 }
44
45 for (i = 0; i < NUM_TREES; i++) {
46 void *blob = trees[i].blob;
47 const char *filename = trees[i].filename;
48 int size;
49 int fd;
50 int ret;
51
52 size = fdt_totalsize(blob);
53
54 printf("Tree \"%s\", %d bytes\n", filename, size);
55
56 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
57 if (fd < 0)
58 perror("open()");
59
60 ret = write(fd, blob, size);
61 if (ret != size)
62 perror("write()");
63
64 close(fd);
65 }
66 exit(0);
67 }
68