proc_devtree.c 5.2 KB

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