^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "dtc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static struct node *read_fstree(const char *dirname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) DIR *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct dirent *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct node *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) d = opendir(dirname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) tree = build_node(NULL, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) while ((de = readdir(d)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) char *tmpname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (streq(de->d_name, ".")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) || streq(de->d_name, ".."))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) tmpname = join_path(dirname, de->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (stat(tmpname, &st) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) die("stat(%s): %s\n", tmpname, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (S_ISREG(st.st_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) FILE *pfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) pfile = fopen(tmpname, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (! pfile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) "WARNING: Cannot open %s: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) tmpname, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) prop = build_property(xstrdup(de->d_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) data_copy_file(pfile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) st.st_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) add_property(tree, prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) fclose(pfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) } else if (S_ISDIR(st.st_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct node *newchild;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) newchild = read_fstree(tmpname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) newchild = name_node(newchild, xstrdup(de->d_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) add_child(tree, newchild);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) free(tmpname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) closedir(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct dt_info *dt_from_fs(const char *dirname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct node *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) tree = read_fstree(dirname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) tree = name_node(tree, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }