proc_devtree.c 5.0 KB

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