David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| 3 | * |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 4 | * |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 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. |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 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 |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "dtc.h" |
| 22 | |
| 23 | #include <dirent.h> |
| 24 | #include <sys/stat.h> |
| 25 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 26 | static struct node *read_fstree(const char *dirname) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 27 | { |
| 28 | DIR *d; |
| 29 | struct dirent *de; |
| 30 | struct stat st; |
| 31 | struct node *tree; |
| 32 | |
| 33 | d = opendir(dirname); |
David Gibson | f7ea370 | 2008-03-06 12:16:55 +1100 | [diff] [blame] | 34 | if (!d) |
| 35 | die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno)); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 36 | |
| 37 | tree = build_node(NULL, NULL); |
| 38 | |
| 39 | while ((de = readdir(d)) != NULL) { |
| 40 | char *tmpnam; |
| 41 | |
| 42 | if (streq(de->d_name, ".") |
| 43 | || streq(de->d_name, "..")) |
| 44 | continue; |
| 45 | |
| 46 | tmpnam = join_path(dirname, de->d_name); |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 47 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 48 | if (lstat(tmpnam, &st) < 0) |
| 49 | die("stat(%s): %s\n", tmpnam, strerror(errno)); |
| 50 | |
| 51 | if (S_ISREG(st.st_mode)) { |
| 52 | struct property *prop; |
| 53 | FILE *pfile; |
| 54 | |
| 55 | pfile = fopen(tmpnam, "r"); |
| 56 | if (! pfile) { |
| 57 | fprintf(stderr, |
| 58 | "WARNING: Cannot open %s: %s\n", |
| 59 | tmpnam, strerror(errno)); |
| 60 | } else { |
Jon Loeliger | 879e4d2 | 2008-10-03 11:12:33 -0500 | [diff] [blame] | 61 | prop = build_property(xstrdup(de->d_name), |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 62 | data_copy_file(pfile, |
David Gibson | 05898c6 | 2010-02-24 18:22:17 +1100 | [diff] [blame] | 63 | st.st_size)); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 64 | add_property(tree, prop); |
| 65 | fclose(pfile); |
| 66 | } |
| 67 | } else if (S_ISDIR(st.st_mode)) { |
| 68 | struct node *newchild; |
| 69 | |
| 70 | newchild = read_fstree(tmpnam); |
David Gibson | 05898c6 | 2010-02-24 18:22:17 +1100 | [diff] [blame] | 71 | newchild = name_node(newchild, xstrdup(de->d_name)); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 72 | add_child(tree, newchild); |
| 73 | } |
| 74 | |
| 75 | free(tmpnam); |
| 76 | } |
| 77 | |
Martin Ettl | 0e89e8c | 2010-07-14 16:10:56 +1000 | [diff] [blame^] | 78 | closedir(d); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 79 | return tree; |
| 80 | } |
| 81 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 82 | struct boot_info *dt_from_fs(const char *dirname) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 83 | { |
| 84 | struct node *tree; |
| 85 | |
| 86 | tree = read_fstree(dirname); |
David Gibson | 05898c6 | 2010-02-24 18:22:17 +1100 | [diff] [blame] | 87 | tree = name_node(tree, ""); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 88 | |
David Gibson | 15ad6d8 | 2010-02-19 15:50:50 +1100 | [diff] [blame] | 89 | return build_boot_info(NULL, tree, guess_boot_cpuid(tree)); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 90 | } |
| 91 | |