dtc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #ifndef _DTC_H
  2. #define _DTC_H
  3. /*
  4. * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  20. * USA
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include <stdarg.h>
  28. #include <assert.h>
  29. #include <ctype.h>
  30. #include <errno.h>
  31. #include <unistd.h>
  32. #include <libfdt_env.h>
  33. #include <fdt.h>
  34. #include "util.h"
  35. #ifdef DEBUG
  36. #define debug(fmt,args...) printf(fmt, ##args)
  37. #else
  38. #define debug(fmt,args...)
  39. #endif
  40. #define DEFAULT_FDT_VERSION 17
  41. /*
  42. * Command line options
  43. */
  44. extern int quiet; /* Level of quietness */
  45. extern int reservenum; /* Number of memory reservation slots */
  46. extern int minsize; /* Minimum blob size */
  47. extern int padsize; /* Additional padding to blob */
  48. extern int phandle_format; /* Use linux,phandle or phandle properties */
  49. #define PHANDLE_LEGACY 0x1
  50. #define PHANDLE_EPAPR 0x2
  51. #define PHANDLE_BOTH 0x3
  52. typedef uint32_t cell_t;
  53. #define streq(a, b) (strcmp((a), (b)) == 0)
  54. #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
  55. #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
  56. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  57. /* Data blobs */
  58. enum markertype {
  59. REF_PHANDLE,
  60. REF_PATH,
  61. LABEL,
  62. };
  63. struct marker {
  64. enum markertype type;
  65. int offset;
  66. char *ref;
  67. struct marker *next;
  68. };
  69. struct data {
  70. int len;
  71. char *val;
  72. struct marker *markers;
  73. };
  74. #define empty_data ((struct data){ /* all .members = 0 or NULL */ })
  75. #define for_each_marker(m) \
  76. for (; (m); (m) = (m)->next)
  77. #define for_each_marker_of_type(m, t) \
  78. for_each_marker(m) \
  79. if ((m)->type == (t))
  80. void data_free(struct data d);
  81. struct data data_grow_for(struct data d, int xlen);
  82. struct data data_copy_mem(const char *mem, int len);
  83. struct data data_copy_escape_string(const char *s, int len);
  84. struct data data_copy_file(FILE *f, size_t len);
  85. struct data data_append_data(struct data d, const void *p, int len);
  86. struct data data_insert_at_marker(struct data d, struct marker *m,
  87. const void *p, int len);
  88. struct data data_merge(struct data d1, struct data d2);
  89. struct data data_append_cell(struct data d, cell_t word);
  90. struct data data_append_integer(struct data d, uint64_t word, int bits);
  91. struct data data_append_re(struct data d, const struct fdt_reserve_entry *re);
  92. struct data data_append_addr(struct data d, uint64_t addr);
  93. struct data data_append_byte(struct data d, uint8_t byte);
  94. struct data data_append_zeroes(struct data d, int len);
  95. struct data data_append_align(struct data d, int align);
  96. struct data data_add_marker(struct data d, enum markertype type, char *ref);
  97. int data_is_one_string(struct data d);
  98. /* DT constraints */
  99. #define MAX_PROPNAME_LEN 31
  100. #define MAX_NODENAME_LEN 31
  101. /* Live trees */
  102. struct label {
  103. int deleted;
  104. char *label;
  105. struct label *next;
  106. };
  107. struct property {
  108. int deleted;
  109. char *name;
  110. struct data val;
  111. struct property *next;
  112. struct label *labels;
  113. };
  114. struct node {
  115. int deleted;
  116. char *name;
  117. struct property *proplist;
  118. struct node *children;
  119. struct node *parent;
  120. struct node *next_sibling;
  121. char *fullpath;
  122. int basenamelen;
  123. cell_t phandle;
  124. int addr_cells, size_cells;
  125. struct label *labels;
  126. };
  127. static inline struct label *for_each_label_next(struct label *l)
  128. {
  129. do {
  130. l = l->next;
  131. } while (l && l->deleted);
  132. return l;
  133. }
  134. #define for_each_label(l0, l) \
  135. for ((l) = (l0); (l); (l) = for_each_label_next(l))
  136. #define for_each_label_withdel(l0, l) \
  137. for ((l) = (l0); (l); (l) = (l)->next)
  138. static inline struct property *for_each_property_next(struct property *p)
  139. {
  140. do {
  141. p = p->next;
  142. } while (p && p->deleted);
  143. return p;
  144. }
  145. #define for_each_property(n, p) \
  146. for ((p) = (n)->proplist; (p); (p) = for_each_property_next(p))
  147. #define for_each_property_withdel(n, p) \
  148. for ((p) = (n)->proplist; (p); (p) = (p)->next)
  149. static inline struct node *for_each_child_next(struct node *c)
  150. {
  151. do {
  152. c = c->next_sibling;
  153. } while (c && c->deleted);
  154. return c;
  155. }
  156. #define for_each_child(n, c) \
  157. for ((c) = (n)->children; (c); (c) = for_each_child_next(c))
  158. #define for_each_child_withdel(n, c) \
  159. for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
  160. void add_label(struct label **labels, char *label);
  161. void delete_labels(struct label **labels);
  162. struct property *build_property(char *name, struct data val);
  163. struct property *build_property_delete(char *name);
  164. struct property *chain_property(struct property *first, struct property *list);
  165. struct property *reverse_properties(struct property *first);
  166. struct node *build_node(struct property *proplist, struct node *children);
  167. struct node *build_node_delete(void);
  168. struct node *name_node(struct node *node, char *name);
  169. struct node *chain_node(struct node *first, struct node *list);
  170. struct node *merge_nodes(struct node *old_node, struct node *new_node);
  171. void add_property(struct node *node, struct property *prop);
  172. void delete_property_by_name(struct node *node, char *name);
  173. void delete_property(struct property *prop);
  174. void add_child(struct node *parent, struct node *child);
  175. void delete_node_by_name(struct node *parent, char *name);
  176. void delete_node(struct node *node);
  177. const char *get_unitname(struct node *node);
  178. struct property *get_property(struct node *node, const char *propname);
  179. cell_t propval_cell(struct property *prop);
  180. struct property *get_property_by_label(struct node *tree, const char *label,
  181. struct node **node);
  182. struct marker *get_marker_label(struct node *tree, const char *label,
  183. struct node **node, struct property **prop);
  184. struct node *get_subnode(struct node *node, const char *nodename);
  185. struct node *get_node_by_path(struct node *tree, const char *path);
  186. struct node *get_node_by_label(struct node *tree, const char *label);
  187. struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
  188. struct node *get_node_by_ref(struct node *tree, const char *ref);
  189. cell_t get_node_phandle(struct node *root, struct node *node);
  190. uint32_t guess_boot_cpuid(struct node *tree);
  191. /* Boot info (tree plus memreserve information */
  192. struct reserve_info {
  193. struct fdt_reserve_entry re;
  194. struct reserve_info *next;
  195. struct label *labels;
  196. };
  197. struct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
  198. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  199. struct reserve_info *list);
  200. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  201. struct reserve_info *new);
  202. struct boot_info {
  203. struct reserve_info *reservelist;
  204. struct node *dt; /* the device tree */
  205. uint32_t boot_cpuid_phys;
  206. };
  207. struct boot_info *build_boot_info(struct reserve_info *reservelist,
  208. struct node *tree, uint32_t boot_cpuid_phys);
  209. void sort_tree(struct boot_info *bi);
  210. /* Checks */
  211. void parse_checks_option(bool warn, bool error, const char *optarg);
  212. void process_checks(int force, struct boot_info *bi);
  213. /* Flattened trees */
  214. void dt_to_blob(FILE *f, struct boot_info *bi, int version);
  215. void dt_to_asm(FILE *f, struct boot_info *bi, int version);
  216. struct boot_info *dt_from_blob(const char *fname);
  217. /* Tree source */
  218. void dt_to_source(FILE *f, struct boot_info *bi);
  219. struct boot_info *dt_from_source(const char *f);
  220. /* FS trees */
  221. struct boot_info *dt_from_fs(const char *dirname);
  222. #endif /* _DTC_H */