1 /*
2  * proc_devtree.c - handles /proc/device-tree
3  *
4  * Copyright 1997 Paul Mackerras
5  */
6 #include <linux/errno.h>
7 #include <linux/sched.h>
8 #include <linux/proc_fs.h>
9 #include <linux/stat.h>
10 #include <linux/string.h>
11 #include <asm/prom.h>
12 #include <asm/uaccess.h>
13 
14 static struct proc_dir_entry *proc_device_tree;
15 
16 /*
17  * Supply data on a read from /proc/device-tree/node/property.
18  */
property_read_proc(char * page,char ** start,off_t off,int count,int * eof,void * data)19 static int property_read_proc(char *page, char **start, off_t off,
20 			      int count, int *eof, void *data)
21 {
22 	struct property *pp = data;
23 	int n;
24 
25 	if (off >= pp->length) {
26 		*eof = 1;
27 		return 0;
28 	}
29 	n = pp->length - off;
30 	if (n > count)
31 		n = count;
32 	else
33 		*eof = 1;
34 	memcpy(page, pp->value + off, n);
35 	*start = page;
36 	return n;
37 }
38 
39 /*
40  * For a node with a name like "gc@10", we make symlinks called "gc"
41  * and "@10" to it.
42  */
43 
44 /*
45  * Process a node, adding entries for its children and its properties.
46  */
add_node(struct device_node * np,struct proc_dir_entry * de)47 static void add_node(struct device_node *np, struct proc_dir_entry *de)
48 {
49 	struct property *pp;
50 	struct proc_dir_entry *ent;
51 	struct device_node *child, *sib;
52 	const char *p, *at;
53 	int l;
54 	struct proc_dir_entry *list, **lastp, *al;
55 
56 	lastp = &list;
57 	for (pp = np->properties; pp != 0; pp = pp->next) {
58 		/*
59 		 * Unfortunately proc_register puts each new entry
60 		 * at the beginning of the list.  So we rearrange them.
61 		 */
62 		ent = create_proc_read_entry(pp->name, strncmp(pp->name, "security-", 9) ?
63 					     S_IRUGO : S_IRUSR, de, property_read_proc, pp);
64 		if (ent == 0)
65 			break;
66 		if (!strncmp(pp->name, "security-", 9))
67 		     ent->size = 0; /* don't leak number of password chars */
68 		else
69 		     ent->size = pp->length;
70 		*lastp = ent;
71 		lastp = &ent->next;
72 	}
73 	for (child = np->child; child != 0; child = child->sibling) {
74 		p = strrchr(child->full_name, '/');
75 		if (p == 0)
76 			p = child->full_name;
77 		else
78 			++p;
79 		/* chop off '@0' if the name ends with that */
80 		l = strlen(p);
81 		if (l > 2 && p[l-2] == '@' && p[l-1] == '0')
82 			l -= 2;
83 		ent = proc_mkdir(p, de);
84 		if (ent == 0)
85 			break;
86 		*lastp = ent;
87 		lastp = &ent->next;
88 		add_node(child, ent);
89 
90 		/*
91 		 * If we left the address part on the name, consider
92 		 * adding symlinks from the name and address parts.
93 		 */
94 		if (p[l] != 0 || (at = strchr(p, '@')) == 0)
95 			continue;
96 
97 		/*
98 		 * If this is the first node with a given name property,
99 		 * add a symlink with the name property as its name.
100 		 */
101 		for (sib = np->child; sib != child; sib = sib->sibling)
102 			if (sib->name && strcmp(sib->name, child->name) == 0)
103 				break;
104 		if (sib == child && strncmp(p, child->name, l) != 0) {
105 			al = proc_symlink(child->name, de, ent->name);
106 			if (al == 0)
107 				break;
108 			*lastp = al;
109 			lastp = &al->next;
110 		}
111 
112 		/*
113 		 * Add another directory with the @address part as its name.
114 		 */
115 		al = proc_symlink(at, de, ent->name);
116 		if (al == 0)
117 			break;
118 		*lastp = al;
119 		lastp = &al->next;
120 	}
121 	*lastp = 0;
122 	de->subdir = list;
123 }
124 
125 /*
126  * Called on initialization to set up the /proc/device-tree subtree
127  */
proc_device_tree_init(void)128 void proc_device_tree_init(void)
129 {
130 	struct device_node *root;
131 	if ( !have_of )
132 		return;
133 	proc_device_tree = proc_mkdir("device-tree", 0);
134 	if (proc_device_tree == 0)
135 		return;
136 	root = find_path_device("/");
137 	if (root == 0) {
138 		printk(KERN_ERR "/proc/device-tree: can't find root\n");
139 		return;
140 	}
141 	add_node(root, proc_device_tree);
142 }
143