blob: 1e7eeba47ff6fbfa22c93faf8ad53fc98ffd96de [file] [log] [blame]
David Gibsona4da2e32007-12-18 15:06:42 +11001/*
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);
David Gibsoned95d742008-08-07 12:24:17 +100034 if (!d)
35 die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
David Gibsona4da2e32007-12-18 15:06:42 +110036
Rob Herringc2e70752018-11-28 18:37:35 -060037 tree = build_node(NULL, NULL, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +110038
39 while ((de = readdir(d)) != NULL) {
Rob Herring47605972015-04-29 16:00:05 -050040 char *tmpname;
David Gibsona4da2e32007-12-18 15:06:42 +110041
42 if (streq(de->d_name, ".")
43 || streq(de->d_name, ".."))
44 continue;
45
Rob Herring47605972015-04-29 16:00:05 -050046 tmpname = join_path(dirname, de->d_name);
David Gibsona4da2e32007-12-18 15:06:42 +110047
Rob Herring47605972015-04-29 16:00:05 -050048 if (lstat(tmpname, &st) < 0)
49 die("stat(%s): %s\n", tmpname, strerror(errno));
David Gibsona4da2e32007-12-18 15:06:42 +110050
51 if (S_ISREG(st.st_mode)) {
52 struct property *prop;
53 FILE *pfile;
54
Rob Herring47605972015-04-29 16:00:05 -050055 pfile = fopen(tmpname, "rb");
David Gibsona4da2e32007-12-18 15:06:42 +110056 if (! pfile) {
57 fprintf(stderr,
58 "WARNING: Cannot open %s: %s\n",
Rob Herring47605972015-04-29 16:00:05 -050059 tmpname, strerror(errno));
David Gibsona4da2e32007-12-18 15:06:42 +110060 } else {
John Bonesio658f29a2010-11-17 15:28:20 -080061 prop = build_property(xstrdup(de->d_name),
David Gibsona4da2e32007-12-18 15:06:42 +110062 data_copy_file(pfile,
Rob Herringc2e70752018-11-28 18:37:35 -060063 st.st_size),
64 NULL);
David Gibsona4da2e32007-12-18 15:06:42 +110065 add_property(tree, prop);
66 fclose(pfile);
67 }
68 } else if (S_ISDIR(st.st_mode)) {
69 struct node *newchild;
70
Rob Herring47605972015-04-29 16:00:05 -050071 newchild = read_fstree(tmpname);
John Bonesio658f29a2010-11-17 15:28:20 -080072 newchild = name_node(newchild, xstrdup(de->d_name));
David Gibsona4da2e32007-12-18 15:06:42 +110073 add_child(tree, newchild);
74 }
75
Rob Herring47605972015-04-29 16:00:05 -050076 free(tmpname);
David Gibsona4da2e32007-12-18 15:06:42 +110077 }
78
Martin Ettl5e8e1cc2010-02-21 09:31:44 +010079 closedir(d);
David Gibsona4da2e32007-12-18 15:06:42 +110080 return tree;
81}
82
Rob Herring6f05afc2017-01-04 10:45:20 -060083struct dt_info *dt_from_fs(const char *dirname)
David Gibsona4da2e32007-12-18 15:06:42 +110084{
85 struct node *tree;
86
87 tree = read_fstree(dirname);
John Bonesio658f29a2010-11-17 15:28:20 -080088 tree = name_node(tree, "");
David Gibsona4da2e32007-12-18 15:06:42 +110089
Rob Herring6f05afc2017-01-04 10:45:20 -060090 return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree));
David Gibsona4da2e32007-12-18 15:06:42 +110091}