blob: 5e59594ab301c1113c7cbfe322cb948d5799a9da [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Gibsona4da2e32007-12-18 15:06:42 +11002/*
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
David Gibsona4da2e32007-12-18 15:06:42 +11004 */
5
6#include "dtc.h"
7
8#include <dirent.h>
9#include <sys/stat.h>
10
11static 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(dirname);
David Gibsoned95d742008-08-07 12:24:17 +100019 if (!d)
20 die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
David Gibsona4da2e32007-12-18 15:06:42 +110021
Rob Herringc2e70752018-11-28 18:37:35 -060022 tree = build_node(NULL, NULL, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +110023
24 while ((de = readdir(d)) != NULL) {
Rob Herring47605972015-04-29 16:00:05 -050025 char *tmpname;
David Gibsona4da2e32007-12-18 15:06:42 +110026
27 if (streq(de->d_name, ".")
28 || streq(de->d_name, ".."))
29 continue;
30
Rob Herring47605972015-04-29 16:00:05 -050031 tmpname = join_path(dirname, de->d_name);
David Gibsona4da2e32007-12-18 15:06:42 +110032
Rob Herring0cec1142019-12-26 15:36:47 -070033 if (stat(tmpname, &st) < 0)
Rob Herring47605972015-04-29 16:00:05 -050034 die("stat(%s): %s\n", tmpname, strerror(errno));
David Gibsona4da2e32007-12-18 15:06:42 +110035
36 if (S_ISREG(st.st_mode)) {
37 struct property *prop;
38 FILE *pfile;
39
Rob Herring47605972015-04-29 16:00:05 -050040 pfile = fopen(tmpname, "rb");
David Gibsona4da2e32007-12-18 15:06:42 +110041 if (! pfile) {
42 fprintf(stderr,
43 "WARNING: Cannot open %s: %s\n",
Rob Herring47605972015-04-29 16:00:05 -050044 tmpname, strerror(errno));
David Gibsona4da2e32007-12-18 15:06:42 +110045 } else {
John Bonesio658f29a2010-11-17 15:28:20 -080046 prop = build_property(xstrdup(de->d_name),
David Gibsona4da2e32007-12-18 15:06:42 +110047 data_copy_file(pfile,
Rob Herringc2e70752018-11-28 18:37:35 -060048 st.st_size),
49 NULL);
David Gibsona4da2e32007-12-18 15:06:42 +110050 add_property(tree, prop);
51 fclose(pfile);
52 }
53 } else if (S_ISDIR(st.st_mode)) {
54 struct node *newchild;
55
Rob Herring47605972015-04-29 16:00:05 -050056 newchild = read_fstree(tmpname);
John Bonesio658f29a2010-11-17 15:28:20 -080057 newchild = name_node(newchild, xstrdup(de->d_name));
David Gibsona4da2e32007-12-18 15:06:42 +110058 add_child(tree, newchild);
59 }
60
Rob Herring47605972015-04-29 16:00:05 -050061 free(tmpname);
David Gibsona4da2e32007-12-18 15:06:42 +110062 }
63
Martin Ettl5e8e1cc2010-02-21 09:31:44 +010064 closedir(d);
David Gibsona4da2e32007-12-18 15:06:42 +110065 return tree;
66}
67
Rob Herring6f05afc2017-01-04 10:45:20 -060068struct dt_info *dt_from_fs(const char *dirname)
David Gibsona4da2e32007-12-18 15:06:42 +110069{
70 struct node *tree;
71
72 tree = read_fstree(dirname);
John Bonesio658f29a2010-11-17 15:28:20 -080073 tree = name_node(tree, "");
David Gibsona4da2e32007-12-18 15:06:42 +110074
Rob Herring6f05afc2017-01-04 10:45:20 -060075 return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree));
David Gibsona4da2e32007-12-18 15:06:42 +110076}