blob: ae7d06c3c49235a54f204316518542a3289e0132 [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * 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.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
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
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
20
21#include "dtc.h"
22
23#include <dirent.h>
24#include <sys/stat.h>
25
David Gibson92cb9a22007-12-04 14:26:15 +110026static struct node *read_fstree(const char *dirname)
David Gibsonfc14dad2005-06-08 17:18:34 +100027{
28 DIR *d;
29 struct dirent *de;
30 struct stat st;
31 struct node *tree;
32
33 d = opendir(dirname);
David Gibsonf7ea3702008-03-06 12:16:55 +110034 if (!d)
35 die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
David Gibsonfc14dad2005-06-08 17:18:34 +100036
37 tree = build_node(NULL, NULL);
38
39 while ((de = readdir(d)) != NULL) {
Florian Fainelli24cb3d02014-02-01 16:41:59 +110040 char *tmpname;
David Gibsonfc14dad2005-06-08 17:18:34 +100041
42 if (streq(de->d_name, ".")
43 || streq(de->d_name, ".."))
44 continue;
45
Florian Fainelli24cb3d02014-02-01 16:41:59 +110046 tmpname = join_path(dirname, de->d_name);
David Gibson63dc9c72007-09-18 11:44:04 +100047
Florian Fainelli24cb3d02014-02-01 16:41:59 +110048 if (lstat(tmpname, &st) < 0)
49 die("stat(%s): %s\n", tmpname, strerror(errno));
David Gibsonfc14dad2005-06-08 17:18:34 +100050
51 if (S_ISREG(st.st_mode)) {
52 struct property *prop;
53 FILE *pfile;
54
Andrei Errapart83e606a2014-06-19 21:12:27 +100055 pfile = fopen(tmpname, "rb");
David Gibsonfc14dad2005-06-08 17:18:34 +100056 if (! pfile) {
57 fprintf(stderr,
58 "WARNING: Cannot open %s: %s\n",
Florian Fainelli24cb3d02014-02-01 16:41:59 +110059 tmpname, strerror(errno));
David Gibsonfc14dad2005-06-08 17:18:34 +100060 } else {
Jon Loeliger879e4d22008-10-03 11:12:33 -050061 prop = build_property(xstrdup(de->d_name),
David Gibsonfc14dad2005-06-08 17:18:34 +100062 data_copy_file(pfile,
David Gibson05898c62010-02-24 18:22:17 +110063 st.st_size));
David Gibsonfc14dad2005-06-08 17:18:34 +100064 add_property(tree, prop);
65 fclose(pfile);
66 }
67 } else if (S_ISDIR(st.st_mode)) {
68 struct node *newchild;
69
Florian Fainelli24cb3d02014-02-01 16:41:59 +110070 newchild = read_fstree(tmpname);
David Gibson05898c62010-02-24 18:22:17 +110071 newchild = name_node(newchild, xstrdup(de->d_name));
David Gibsonfc14dad2005-06-08 17:18:34 +100072 add_child(tree, newchild);
73 }
74
Florian Fainelli24cb3d02014-02-01 16:41:59 +110075 free(tmpname);
David Gibsonfc14dad2005-06-08 17:18:34 +100076 }
77
Martin Ettl0e89e8c2010-07-14 16:10:56 +100078 closedir(d);
David Gibsonfc14dad2005-06-08 17:18:34 +100079 return tree;
80}
81
David Gibson00fbb862016-05-31 11:58:42 +100082struct dt_info *dt_from_fs(const char *dirname)
David Gibsonfc14dad2005-06-08 17:18:34 +100083{
84 struct node *tree;
85
86 tree = read_fstree(dirname);
David Gibson05898c62010-02-24 18:22:17 +110087 tree = name_node(tree, "");
David Gibsonfc14dad2005-06-08 17:18:34 +100088
David Gibson00fbb862016-05-31 11:58:42 +100089 return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree));
David Gibsonfc14dad2005-06-08 17:18:34 +100090}