dt-bindings: Pass binding directory to validation tools
[linux-2.6-block.git] / scripts / dtc / fstree.c
CommitLineData
a4da2e3e
DG
1/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21#include "dtc.h"
22
23#include <dirent.h>
24#include <sys/stat.h>
25
26static struct node *read_fstree(const char *dirname)
27{
28 DIR *d;
29 struct dirent *de;
30 struct stat st;
31 struct node *tree;
32
33 d = opendir(dirname);
ed95d745
DG
34 if (!d)
35 die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
a4da2e3e 36
c2e7075c 37 tree = build_node(NULL, NULL, NULL);
a4da2e3e
DG
38
39 while ((de = readdir(d)) != NULL) {
47605971 40 char *tmpname;
a4da2e3e
DG
41
42 if (streq(de->d_name, ".")
43 || streq(de->d_name, ".."))
44 continue;
45
47605971 46 tmpname = join_path(dirname, de->d_name);
a4da2e3e 47
47605971
RH
48 if (lstat(tmpname, &st) < 0)
49 die("stat(%s): %s\n", tmpname, strerror(errno));
a4da2e3e
DG
50
51 if (S_ISREG(st.st_mode)) {
52 struct property *prop;
53 FILE *pfile;
54
47605971 55 pfile = fopen(tmpname, "rb");
a4da2e3e
DG
56 if (! pfile) {
57 fprintf(stderr,
58 "WARNING: Cannot open %s: %s\n",
47605971 59 tmpname, strerror(errno));
a4da2e3e 60 } else {
658f29a5 61 prop = build_property(xstrdup(de->d_name),
a4da2e3e 62 data_copy_file(pfile,
c2e7075c
RH
63 st.st_size),
64 NULL);
a4da2e3e
DG
65 add_property(tree, prop);
66 fclose(pfile);
67 }
68 } else if (S_ISDIR(st.st_mode)) {
69 struct node *newchild;
70
47605971 71 newchild = read_fstree(tmpname);
658f29a5 72 newchild = name_node(newchild, xstrdup(de->d_name));
a4da2e3e
DG
73 add_child(tree, newchild);
74 }
75
47605971 76 free(tmpname);
a4da2e3e
DG
77 }
78
5e8e1cc0 79 closedir(d);
a4da2e3e
DG
80 return tree;
81}
82
6f05afcb 83struct dt_info *dt_from_fs(const char *dirname)
a4da2e3e
DG
84{
85 struct node *tree;
86
87 tree = read_fstree(dirname);
658f29a5 88 tree = name_node(tree, "");
a4da2e3e 89
6f05afcb 90 return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree));
a4da2e3e 91}