proc_devtree.c 5.3 KB

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