proc_devtree.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. for (ent = pde->subdir; ent != NULL; ent = ent->next)
  89. if (ent->data == oldprop)
  90. break;
  91. if (ent == NULL) {
  92. printk(KERN_WARNING "device-tree: property \"%s\" "
  93. " does not exist\n", oldprop->name);
  94. } else {
  95. ent->data = newprop;
  96. ent->size = newprop->length;
  97. }
  98. }
  99. /*
  100. * Various dodgy firmware might give us nodes and/or properties with
  101. * conflicting names. That's generally ok, except for exporting via /proc,
  102. * so munge names here to ensure they're unique.
  103. */
  104. static int duplicate_name(struct proc_dir_entry *de, const char *name)
  105. {
  106. struct proc_dir_entry *ent;
  107. int found = 0;
  108. spin_lock(&proc_subdir_lock);
  109. for (ent = de->subdir; ent != NULL; ent = ent->next) {
  110. if (strcmp(ent->name, name) == 0) {
  111. found = 1;
  112. break;
  113. }
  114. }
  115. spin_unlock(&proc_subdir_lock);
  116. return found;
  117. }
  118. static const char *fixup_name(struct device_node *np, struct proc_dir_entry *de,
  119. const char *name)
  120. {
  121. char *fixed_name;
  122. int fixup_len = strlen(name) + 2 + 1; /* name + #x + \0 */
  123. int i = 1, size;
  124. realloc:
  125. fixed_name = kmalloc(fixup_len, GFP_KERNEL);
  126. if (fixed_name == NULL) {
  127. printk(KERN_ERR "device-tree: Out of memory trying to fixup "
  128. "name \"%s\"\n", name);
  129. return name;
  130. }
  131. retry:
  132. size = snprintf(fixed_name, fixup_len, "%s#%d", name, i);
  133. size++; /* account for NULL */
  134. if (size > fixup_len) {
  135. /* We ran out of space, free and reallocate. */
  136. kfree(fixed_name);
  137. fixup_len = size;
  138. goto realloc;
  139. }
  140. if (duplicate_name(de, fixed_name)) {
  141. /* Multiple duplicates. Retry with a different offset. */
  142. i++;
  143. goto retry;
  144. }
  145. printk(KERN_WARNING "device-tree: Duplicate name in %s, "
  146. "renamed to \"%s\"\n", np->full_name, fixed_name);
  147. return fixed_name;
  148. }
  149. /*
  150. * Process a node, adding entries for its children and its properties.
  151. */
  152. void proc_device_tree_add_node(struct device_node *np,
  153. struct proc_dir_entry *de)
  154. {
  155. struct property *pp;
  156. struct proc_dir_entry *ent;
  157. struct device_node *child;
  158. const char *p;
  159. set_node_proc_entry(np, de);
  160. for (child = NULL; (child = of_get_next_child(np, child));) {
  161. /* Use everything after the last slash, or the full name */
  162. p = strrchr(child->full_name, '/');
  163. if (!p)
  164. p = child->full_name;
  165. else
  166. ++p;
  167. if (duplicate_name(de, p))
  168. p = fixup_name(np, de, p);
  169. ent = proc_mkdir(p, de);
  170. if (ent == NULL)
  171. break;
  172. proc_device_tree_add_node(child, ent);
  173. }
  174. of_node_put(child);
  175. for (pp = np->properties; pp != NULL; pp = pp->next) {
  176. p = pp->name;
  177. if (strchr(p, '/'))
  178. continue;
  179. if (duplicate_name(de, p))
  180. p = fixup_name(np, de, p);
  181. ent = __proc_device_tree_add_prop(de, pp, p);
  182. if (ent == NULL)
  183. break;
  184. }
  185. }
  186. /*
  187. * Called on initialization to set up the /proc/device-tree subtree
  188. */
  189. void __init proc_device_tree_init(void)
  190. {
  191. struct device_node *root;
  192. proc_device_tree = proc_mkdir("device-tree", NULL);
  193. if (proc_device_tree == NULL)
  194. return;
  195. root = of_find_node_by_path("/");
  196. if (root == NULL) {
  197. pr_debug("/proc/device-tree: can't find root\n");
  198. return;
  199. }
  200. proc_device_tree_add_node(root, proc_device_tree);
  201. of_node_put(root);
  202. }