1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | /* |
3 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
4 | */ |
5 | |
6 | #include "dtc.h" |
7 | |
8 | #include <dirent.h> |
9 | #include <sys/stat.h> |
10 | |
11 | static struct node *read_fstree(const char *dirname) |
12 | { |
13 | DIR *d; |
14 | struct dirent *de; |
15 | struct stat st; |
16 | struct node *tree; |
17 | |
18 | d = opendir(name: dirname); |
19 | if (!d) |
20 | die(str: "Couldn't opendir() \"%s\": %s\n" , dirname, strerror(errno)); |
21 | |
22 | tree = build_node(NULL, NULL, NULL); |
23 | |
24 | while ((de = readdir(dirp: d)) != NULL) { |
25 | char *tmpname; |
26 | |
27 | if (streq(de->d_name, "." ) |
28 | || streq(de->d_name, ".." )) |
29 | continue; |
30 | |
31 | tmpname = join_path(path: dirname, name: de->d_name); |
32 | |
33 | if (stat(file: tmpname, buf: &st) < 0) |
34 | die(str: "stat(%s): %s\n" , tmpname, strerror(errno)); |
35 | |
36 | if (S_ISREG(st.st_mode)) { |
37 | struct property *prop; |
38 | FILE *pfile; |
39 | |
40 | pfile = fopen(filename: tmpname, modes: "rb" ); |
41 | if (! pfile) { |
42 | fprintf(stderr, |
43 | format: "WARNING: Cannot open %s: %s\n" , |
44 | tmpname, strerror(errno)); |
45 | } else { |
46 | prop = build_property(name: xstrdup(s: de->d_name), |
47 | val: data_copy_file(f: pfile, |
48 | len: st.st_size), |
49 | NULL); |
50 | add_property(node: tree, prop); |
51 | fclose(stream: pfile); |
52 | } |
53 | } else if (S_ISDIR(st.st_mode)) { |
54 | struct node *newchild; |
55 | |
56 | newchild = read_fstree(dirname: tmpname); |
57 | newchild = name_node(node: newchild, name: xstrdup(s: de->d_name)); |
58 | add_child(parent: tree, child: newchild); |
59 | } |
60 | |
61 | free(ptr: tmpname); |
62 | } |
63 | |
64 | closedir(dirp: d); |
65 | return tree; |
66 | } |
67 | |
68 | struct dt_info *dt_from_fs(const char *dirname) |
69 | { |
70 | struct node *tree; |
71 | |
72 | tree = read_fstree(dirname); |
73 | tree = name_node(node: tree, name: "" ); |
74 | |
75 | return build_dt_info(DTSF_V1, NULL, tree, boot_cpuid_phys: guess_boot_cpuid(tree)); |
76 | } |
77 | |