Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include "dtc.h" |
| 7 | |
| 8 | #include <dirent.h> |
| 9 | #include <sys/stat.h> |
| 10 | |
| 11 | static 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 Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 19 | if (!d) |
| 20 | die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno)); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 21 | |
Rob Herring | c2e7075 | 2018-11-28 18:37:35 -0600 | [diff] [blame] | 22 | tree = build_node(NULL, NULL, NULL); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 23 | |
| 24 | while ((de = readdir(d)) != NULL) { |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 25 | char *tmpname; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 26 | |
| 27 | if (streq(de->d_name, ".") |
| 28 | || streq(de->d_name, "..")) |
| 29 | continue; |
| 30 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 31 | tmpname = join_path(dirname, de->d_name); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 32 | |
Rob Herring | 0cec114 | 2019-12-26 15:36:47 -0700 | [diff] [blame] | 33 | if (stat(tmpname, &st) < 0) |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 34 | die("stat(%s): %s\n", tmpname, strerror(errno)); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 35 | |
| 36 | if (S_ISREG(st.st_mode)) { |
| 37 | struct property *prop; |
| 38 | FILE *pfile; |
| 39 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 40 | pfile = fopen(tmpname, "rb"); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 41 | if (! pfile) { |
| 42 | fprintf(stderr, |
| 43 | "WARNING: Cannot open %s: %s\n", |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 44 | tmpname, strerror(errno)); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 45 | } else { |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 46 | prop = build_property(xstrdup(de->d_name), |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 47 | data_copy_file(pfile, |
Rob Herring | c2e7075 | 2018-11-28 18:37:35 -0600 | [diff] [blame] | 48 | st.st_size), |
| 49 | NULL); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 50 | add_property(tree, prop); |
| 51 | fclose(pfile); |
| 52 | } |
| 53 | } else if (S_ISDIR(st.st_mode)) { |
| 54 | struct node *newchild; |
| 55 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 56 | newchild = read_fstree(tmpname); |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 57 | newchild = name_node(newchild, xstrdup(de->d_name)); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 58 | add_child(tree, newchild); |
| 59 | } |
| 60 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 61 | free(tmpname); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 62 | } |
| 63 | |
Martin Ettl | 5e8e1cc | 2010-02-21 09:31:44 +0100 | [diff] [blame] | 64 | closedir(d); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 65 | return tree; |
| 66 | } |
| 67 | |
Rob Herring | 6f05afc | 2017-01-04 10:45:20 -0600 | [diff] [blame] | 68 | struct dt_info *dt_from_fs(const char *dirname) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 69 | { |
| 70 | struct node *tree; |
| 71 | |
| 72 | tree = read_fstree(dirname); |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 73 | tree = name_node(tree, ""); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 74 | |
Rob Herring | 6f05afc | 2017-01-04 10:45:20 -0600 | [diff] [blame] | 75 | return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree)); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 76 | } |