proc_devtree.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * proc_devtree.c - handles /proc/device-tree
  3. *
  4. * Copyright 1997 Paul Mackerras
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/init.h>
  8. #include <linux/time.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/stat.h>
  12. #include <linux/string.h>
  13. #include <linux/of.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <asm/prom.h>
  17. #include <asm/uaccess.h>
  18. #include "internal.h"
  19. static inline void set_node_proc_entry(struct device_node *np,
  20. struct proc_dir_entry *de)
  21. {
  22. #ifdef HAVE_ARCH_DEVTREE_FIXUPS
  23. np->pde = de;
  24. #endif
  25. }
  26. static struct proc_dir_entry *proc_device_tree;
  27. /*
  28. * Supply data on a read from /proc/device-tree/node/property.
  29. */
  30. static int property_proc_show(struct seq_file *m, void *v)
  31. {
  32. struct property *pp = m->private;
  33. seq_write(m, pp->value, pp->length);
  34. return 0;
  35. }
  36. static int property_proc_open(struct inode *inode, struct file *file)
  37. {
  38. return single_open(file, property_proc_show, PDE(inode)->data);
  39. }
  40. static const struct file_operations property_proc_fops = {
  41. .owner = THIS_MODULE,
  42. .open = property_proc_open,
  43. .read = seq_read,
  44. .llseek = seq_lseek,
  45. .release = single_release,
  46. };
  47. /*
  48. * For a node with a name like "gc@10", we make symlinks called "gc"
  49. * and "@10" to it.
  50. */
  51. /*
  52. * Add a property to a node
  53. */
  54. static struct proc_dir_entry *
  55. __proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp,
  56. const char *name)
  57. {
  58. struct proc_dir_entry *ent;
  59. /*
  60. * Unfortunately proc_register puts each new entry
  61. * at the beginning of the list. So we rearrange them.
  62. */
  63. ent = proc_create_data(name,
  64. strncmp(name, "security-", 9) ? S_IRUGO : S_IRUSR,
  65. de, &property_proc_fops, pp);
  66. if (ent == NULL)
  67. return NULL;
  68. if (!strncmp(name, "security-", 9))
  69. ent->size = 0; /* don't leak number of password chars */
  70. else
  71. ent->size = pp->length;
  72. return ent;
  73. }
  74. void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop)
  75. {
  76. __proc_device_tree_add_prop(pde, prop, prop->name);
  77. }
  78. void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
  79. struct property *prop)
  80. {
  81. remove_proc_entry(prop->name, pde);
  82. }
  83. void proc_device_tree_update_prop(struct proc_dir_entry *pde,
  84. struct property *newprop,
  85. struct property *oldprop)
  86. {
  87. struct proc_dir_entry *ent;
  88. if (!oldprop) {
  89. proc_device_tree_add_prop(pde, newprop);
  90. return;
  91. }
  92. for (ent = pde->subdir; ent != NULL; ent = ent->next)
  93. if (ent->data == oldprop)
  94. break;
  95. if (ent == NULL) {
  96. printk(KERN_WARNING "device-tree: property \"%s\" "
  97. " does not exist\n", oldprop->name);
  98. } else {
  99. ent->data = newprop;
  100. ent->size = newprop->length;
  101. }
  102. }
  103. /*
  104. * Various dodgy firmware might give us nodes and/or properties with
  105. * conflicting names. That's generally ok, except for exporting via /proc,
  106. * so munge names here to ensure they're unique.
  107. */
  108. static int duplicate_name(struct proc_dir_entry *de, const char *name)
  109. {
  110. struct proc_dir_entry *ent;
  111. int found = 0;
  112. spin_lock(&proc_subdir_lock);
  113. for (ent = de->subdir; ent != NULL; ent = ent->next) {
  114. if (strcmp(ent->name, name) == 0) {
  115. found = 1;
  116. break;
  117. }
  118. }
  119. spin_unlock(&proc_subdir_lock);
  120. return found;
  121. }
  122. static const char *fixup_name(struct device_node *np, struct proc_dir_entry *de,
  123. const char *name)
  124. {
  125. char *fixed_name;
  126. int fixup_len = strlen(name) + 2 + 1; /* name + #x + \0 */
  127. int i = 1, size;
  128. realloc:
  129. fixed_name = kmalloc(fixup_len, GFP_KERNEL);
  130. if (fixed_name == NULL) {
  131. printk(KERN_ERR "device-tree: Out of memory trying to fixup "
  132. "name \"%s\"\n", name);
  133. return name;
  134. }
  135. retry:
  136. size = snprintf(fixed_name, fixup_len, "%s#%d", name, i);
  137. size++; /* account for NULL */
  138. if (size > fixup_len) {
  139. /* We ran out of space, free and reallocate. */
  140. kfree(fixed_name);
  141. fixup_len = size;
  142. goto realloc;
  143. }
  144. if (duplicate_name(de, fixed_name)) {
  145. /* Multiple duplicates. Retry with a different offset. */
  146. i++;
  147. goto retry;
  148. }
  149. printk(KERN_WARNING "device-tree: Duplicate name in %s, "
  150. "renamed to \"%s\"\n", np->full_name, fixed_name);
  151. return fixed_name;
  152. }
  153. /*
  154. * Process a node, adding entries for its children and its properties.
  155. */
  156. void proc_device_tree_add_node(struct device_node *np,
  157. struct proc_dir_entry *de)
  158. {
  159. struct property *pp;
  160. struct proc_dir_entry *ent;
  161. struct device_node *child;
  162. const char *p;
  163. set_node_proc_entry(np, de);
  164. for (child = NULL; (child = of_get_next_child(np, child));) {
  165. /* Use everything after the last slash, or the full name */
  166. p = strrchr(child->full_name, '/');
  167. if (!p)
  168. p = child->full_name;
  169. else
  170. ++p;
  171. if (duplicate_name(de, p))
  172. p = fixup_name(np, de, p);
  173. ent = proc_mkdir(p, de);
  174. if (ent == NULL)
  175. break;
  176. proc_device_tree_add_node(child, ent);
  177. }
  178. of_node_put(child);
  179. for (pp = np->properties; pp != NULL; pp = pp->next) {
  180. p = pp->name;
  181. if (strchr(p, '/'))
  182. continue;
  183. if (duplicate_name(de, p))
  184. p = fixup_name(np, de, p);
  185. ent = __proc_device_tree_add_prop(de, pp, p);
  186. if (ent == NULL)
  187. break;
  188. }
  189. }
  190. /*
  191. * Called on initialization to set up the /proc/device-tree subtree
  192. */
  193. void __init proc_device_tree_init(void)
  194. {
  195. struct device_node *root;
  196. proc_device_tree = proc_mkdir("device-tree", NULL);
  197. if (proc_device_tree == NULL)
  198. return;
  199. root = of_find_node_by_path("/");
  200. if (root == NULL) {
  201. pr_debug("/proc/device-tree: can't find root\n");
  202. return;
  203. }
  204. proc_device_tree_add_node(root, proc_device_tree);
  205. of_node_put(root);
  206. }