xref: /OK3568_Linux_fs/u-boot/scripts/dtc/fstree.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or
6*4882a593Smuzhiyun  * modify it under the terms of the GNU General Public License as
7*4882a593Smuzhiyun  * published by the Free Software Foundation; either version 2 of the
8*4882a593Smuzhiyun  * License, or (at your option) any later version.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *  This program is distributed in the hope that it will be useful,
11*4882a593Smuzhiyun  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12*4882a593Smuzhiyun  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*4882a593Smuzhiyun  *  General Public License for more details.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  *  You should have received a copy of the GNU General Public License
16*4882a593Smuzhiyun  *  along with this program; if not, write to the Free Software
17*4882a593Smuzhiyun  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18*4882a593Smuzhiyun  *                                                                   USA
19*4882a593Smuzhiyun  */
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include "dtc.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <dirent.h>
24*4882a593Smuzhiyun #include <sys/stat.h>
25*4882a593Smuzhiyun 
read_fstree(const char * dirname)26*4882a593Smuzhiyun static struct node *read_fstree(const char *dirname)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun 	DIR *d;
29*4882a593Smuzhiyun 	struct dirent *de;
30*4882a593Smuzhiyun 	struct stat st;
31*4882a593Smuzhiyun 	struct node *tree;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	d = opendir(dirname);
34*4882a593Smuzhiyun 	if (!d)
35*4882a593Smuzhiyun 		die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	tree = build_node(NULL, NULL);
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	while ((de = readdir(d)) != NULL) {
40*4882a593Smuzhiyun 		char *tmpname;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 		if (streq(de->d_name, ".")
43*4882a593Smuzhiyun 		    || streq(de->d_name, ".."))
44*4882a593Smuzhiyun 			continue;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 		tmpname = join_path(dirname, de->d_name);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 		if (lstat(tmpname, &st) < 0)
49*4882a593Smuzhiyun 			die("stat(%s): %s\n", tmpname, strerror(errno));
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 		if (S_ISREG(st.st_mode)) {
52*4882a593Smuzhiyun 			struct property *prop;
53*4882a593Smuzhiyun 			FILE *pfile;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 			pfile = fopen(tmpname, "rb");
56*4882a593Smuzhiyun 			if (! pfile) {
57*4882a593Smuzhiyun 				fprintf(stderr,
58*4882a593Smuzhiyun 					"WARNING: Cannot open %s: %s\n",
59*4882a593Smuzhiyun 					tmpname, strerror(errno));
60*4882a593Smuzhiyun 			} else {
61*4882a593Smuzhiyun 				prop = build_property(xstrdup(de->d_name),
62*4882a593Smuzhiyun 						      data_copy_file(pfile,
63*4882a593Smuzhiyun 								     st.st_size));
64*4882a593Smuzhiyun 				add_property(tree, prop);
65*4882a593Smuzhiyun 				fclose(pfile);
66*4882a593Smuzhiyun 			}
67*4882a593Smuzhiyun 		} else if (S_ISDIR(st.st_mode)) {
68*4882a593Smuzhiyun 			struct node *newchild;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 			newchild = read_fstree(tmpname);
71*4882a593Smuzhiyun 			newchild = name_node(newchild, xstrdup(de->d_name));
72*4882a593Smuzhiyun 			add_child(tree, newchild);
73*4882a593Smuzhiyun 		}
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 		free(tmpname);
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	closedir(d);
79*4882a593Smuzhiyun 	return tree;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
dt_from_fs(const char * dirname)82*4882a593Smuzhiyun struct dt_info *dt_from_fs(const char *dirname)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	struct node *tree;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	tree = read_fstree(dirname);
87*4882a593Smuzhiyun 	tree = name_node(tree, "");
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree));
90*4882a593Smuzhiyun }
91