11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 29fffb55fSDavid Gibson /* 39fffb55fSDavid Gibson * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. 49fffb55fSDavid Gibson */ 59fffb55fSDavid Gibson 69fffb55fSDavid Gibson #include "dtc.h" 79fffb55fSDavid Gibson 89fffb55fSDavid Gibson #include <dirent.h> 99fffb55fSDavid Gibson #include <sys/stat.h> 109fffb55fSDavid Gibson 119fffb55fSDavid Gibson static struct node *read_fstree(const char *dirname) 129fffb55fSDavid Gibson { 139fffb55fSDavid Gibson DIR *d; 149fffb55fSDavid Gibson struct dirent *de; 159fffb55fSDavid Gibson struct stat st; 169fffb55fSDavid Gibson struct node *tree; 179fffb55fSDavid Gibson 189fffb55fSDavid Gibson d = opendir(dirname); 199fffb55fSDavid Gibson if (!d) 209fffb55fSDavid Gibson die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno)); 219fffb55fSDavid Gibson 22c2e7075cSRob Herring tree = build_node(NULL, NULL, NULL); 239fffb55fSDavid Gibson 249fffb55fSDavid Gibson while ((de = readdir(d)) != NULL) { 2547605971SRob Herring char *tmpname; 269fffb55fSDavid Gibson 279fffb55fSDavid Gibson if (streq(de->d_name, ".") 289fffb55fSDavid Gibson || streq(de->d_name, "..")) 299fffb55fSDavid Gibson continue; 309fffb55fSDavid Gibson 3147605971SRob Herring tmpname = join_path(dirname, de->d_name); 329fffb55fSDavid Gibson 33*0cec114eSRob Herring if (stat(tmpname, &st) < 0) 3447605971SRob Herring die("stat(%s): %s\n", tmpname, strerror(errno)); 359fffb55fSDavid Gibson 369fffb55fSDavid Gibson if (S_ISREG(st.st_mode)) { 379fffb55fSDavid Gibson struct property *prop; 389fffb55fSDavid Gibson FILE *pfile; 399fffb55fSDavid Gibson 4047605971SRob Herring pfile = fopen(tmpname, "rb"); 419fffb55fSDavid Gibson if (! pfile) { 429fffb55fSDavid Gibson fprintf(stderr, 439fffb55fSDavid Gibson "WARNING: Cannot open %s: %s\n", 4447605971SRob Herring tmpname, strerror(errno)); 459fffb55fSDavid Gibson } else { 46658f29a5SJohn Bonesio prop = build_property(xstrdup(de->d_name), 479fffb55fSDavid Gibson data_copy_file(pfile, 48c2e7075cSRob Herring st.st_size), 49c2e7075cSRob Herring NULL); 509fffb55fSDavid Gibson add_property(tree, prop); 519fffb55fSDavid Gibson fclose(pfile); 529fffb55fSDavid Gibson } 539fffb55fSDavid Gibson } else if (S_ISDIR(st.st_mode)) { 549fffb55fSDavid Gibson struct node *newchild; 559fffb55fSDavid Gibson 5647605971SRob Herring newchild = read_fstree(tmpname); 57658f29a5SJohn Bonesio newchild = name_node(newchild, xstrdup(de->d_name)); 589fffb55fSDavid Gibson add_child(tree, newchild); 599fffb55fSDavid Gibson } 609fffb55fSDavid Gibson 6147605971SRob Herring free(tmpname); 629fffb55fSDavid Gibson } 639fffb55fSDavid Gibson 645e8e1cc0SMartin Ettl closedir(d); 659fffb55fSDavid Gibson return tree; 669fffb55fSDavid Gibson } 679fffb55fSDavid Gibson 686f05afcbSRob Herring struct dt_info *dt_from_fs(const char *dirname) 699fffb55fSDavid Gibson { 709fffb55fSDavid Gibson struct node *tree; 719fffb55fSDavid Gibson 729fffb55fSDavid Gibson tree = read_fstree(dirname); 73658f29a5SJohn Bonesio tree = name_node(tree, ""); 749fffb55fSDavid Gibson 756f05afcbSRob Herring return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree)); 769fffb55fSDavid Gibson } 77