blob: 5e59594ab301c1113c7cbfe322cb948d5799a9da [file] [log] [blame]
Rob Herringacfe84f2019-06-20 15:19:38 -06001// SPDX-License-Identifier: GPL-2.0-or-later
David Gibsonfc14dad2005-06-08 17:18:34 +10002/*
3 * (C) Copyright David Gibson <[email protected]>, IBM Corporation. 2005.
David Gibsonfc14dad2005-06-08 17:18:34 +10004 */
5
6#include "dtc.h"
7
8#include <dirent.h>
9#include <sys/stat.h>
10
David Gibson92cb9a22007-12-04 14:26:15 +110011static struct node *read_fstree(const char *dirname)
David Gibsonfc14dad2005-06-08 17:18:34 +100012{
13 DIR *d;
14 struct dirent *de;
15 struct stat st;
16 struct node *tree;
17
18 d = opendir(dirname);
David Gibsonf7ea3702008-03-06 12:16:55 +110019 if (!d)
20 die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
David Gibsonfc14dad2005-06-08 17:18:34 +100021
Julia Lawall8e20ccf2018-11-16 17:29:59 +010022 tree = build_node(NULL, NULL, NULL);
David Gibsonfc14dad2005-06-08 17:18:34 +100023
24 while ((de = readdir(d)) != NULL) {
Florian Fainelli24cb3d02014-02-01 16:41:59 +110025 char *tmpname;
David Gibsonfc14dad2005-06-08 17:18:34 +100026
27 if (streq(de->d_name, ".")
28 || streq(de->d_name, ".."))
29 continue;
30
Florian Fainelli24cb3d02014-02-01 16:41:59 +110031 tmpname = join_path(dirname, de->d_name);
David Gibson63dc9c72007-09-18 11:44:04 +100032
Marc-André Lureaubc876702019-10-09 14:20:21 +040033 if (stat(tmpname, &st) < 0)
Florian Fainelli24cb3d02014-02-01 16:41:59 +110034 die("stat(%s): %s\n", tmpname, strerror(errno));
David Gibsonfc14dad2005-06-08 17:18:34 +100035
36 if (S_ISREG(st.st_mode)) {
37 struct property *prop;
38 FILE *pfile;
39
Andrei Errapart83e606a2014-06-19 21:12:27 +100040 pfile = fopen(tmpname, "rb");
David Gibsonfc14dad2005-06-08 17:18:34 +100041 if (! pfile) {
42 fprintf(stderr,
43 "WARNING: Cannot open %s: %s\n",
Florian Fainelli24cb3d02014-02-01 16:41:59 +110044 tmpname, strerror(errno));
David Gibsonfc14dad2005-06-08 17:18:34 +100045 } else {
Jon Loeliger879e4d22008-10-03 11:12:33 -050046 prop = build_property(xstrdup(de->d_name),
David Gibsonfc14dad2005-06-08 17:18:34 +100047 data_copy_file(pfile,
Julia Lawall8e20ccf2018-11-16 17:29:59 +010048 st.st_size),
49 NULL);
David Gibsonfc14dad2005-06-08 17:18:34 +100050 add_property(tree, prop);
51 fclose(pfile);
52 }
53 } else if (S_ISDIR(st.st_mode)) {
54 struct node *newchild;
55
Florian Fainelli24cb3d02014-02-01 16:41:59 +110056 newchild = read_fstree(tmpname);
David Gibson05898c62010-02-24 18:22:17 +110057 newchild = name_node(newchild, xstrdup(de->d_name));
David Gibsonfc14dad2005-06-08 17:18:34 +100058 add_child(tree, newchild);
59 }
60
Florian Fainelli24cb3d02014-02-01 16:41:59 +110061 free(tmpname);
David Gibsonfc14dad2005-06-08 17:18:34 +100062 }
63
Martin Ettl0e89e8c2010-07-14 16:10:56 +100064 closedir(d);
David Gibsonfc14dad2005-06-08 17:18:34 +100065 return tree;
66}
67
David Gibson00fbb862016-05-31 11:58:42 +100068struct dt_info *dt_from_fs(const char *dirname)
David Gibsonfc14dad2005-06-08 17:18:34 +100069{
70 struct node *tree;
71
72 tree = read_fstree(dirname);
David Gibson05898c62010-02-24 18:22:17 +110073 tree = name_node(tree, "");
David Gibsonfc14dad2005-06-08 17:18:34 +100074
David Gibson00fbb862016-05-31 11:58:42 +100075 return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree));
David Gibsonfc14dad2005-06-08 17:18:34 +100076}