dtc.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <inttypes.h>
  33. #include <libfdt_env.h>
  34. #include <fdt.h>
  35. #include "util.h"
  36. #ifdef DEBUG
  37. #define debug(...) printf(__VA_ARGS__)
  38. #else
  39. #define debug(...)
  40. #endif
  41. #define DEFAULT_FDT_VERSION 17
  42. /*
  43. * Command line options
  44. */
  45. extern int quiet; /* Level of quietness */
  46. extern int reservenum; /* Number of memory reservation slots */
  47. extern int minsize; /* Minimum blob size */
  48. extern int padsize; /* Additional padding to blob */
  49. extern int alignsize; /* Additional padding to blob accroding to the alignsize */
  50. extern int phandle_format; /* Use linux,phandle or phandle properties */
  51. extern int generate_symbols; /* generate symbols for nodes with labels */
  52. extern int generate_fixups; /* generate fixups */
  53. extern int auto_label_aliases; /* auto generate labels -> aliases */
  54. #define PHANDLE_LEGACY 0x1
  55. #define PHANDLE_EPAPR 0x2
  56. #define PHANDLE_BOTH 0x3
  57. typedef uint32_t cell_t;
  58. #define streq(a, b) (strcmp((a), (b)) == 0)
  59. #define strstarts(s, prefix) (strncmp((s), (prefix), strlen(prefix)) == 0)
  60. #define strprefixeq(a, n, b) (strlen(b) == (n) && (memcmp(a, b, n) == 0))
  61. #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
  62. /* Data blobs */
  63. enum markertype {
  64. REF_PHANDLE,
  65. REF_PATH,
  66. LABEL,
  67. };
  68. struct marker {
  69. enum markertype type;
  70. int offset;
  71. char *ref;
  72. struct marker *next;
  73. };
  74. struct data {
  75. int len;
  76. char *val;
  77. struct marker *markers;
  78. };
  79. #define empty_data ((struct data){ 0 /* all .members = 0 or NULL */ })
  80. #define for_each_marker(m) \
  81. for (; (m); (m) = (m)->next)
  82. #define for_each_marker_of_type(m, t) \
  83. for_each_marker(m) \
  84. if ((m)->type == (t))
  85. void data_free(struct data d);
  86. struct data data_grow_for(struct data d, int xlen);
  87. struct data data_copy_mem(const char *mem, int len);
  88. struct data data_copy_escape_string(const char *s, int len);
  89. struct data data_copy_file(FILE *f, size_t len);
  90. struct data data_append_data(struct data d, const void *p, int len);
  91. struct data data_insert_at_marker(struct data d, struct marker *m,
  92. const void *p, int len);
  93. struct data data_merge(struct data d1, struct data d2);
  94. struct data data_append_cell(struct data d, cell_t word);
  95. struct data data_append_integer(struct data d, uint64_t word, int bits);
  96. struct data data_append_re(struct data d, uint64_t address, uint64_t size);
  97. struct data data_append_addr(struct data d, uint64_t addr);
  98. struct data data_append_byte(struct data d, uint8_t byte);
  99. struct data data_append_zeroes(struct data d, int len);
  100. struct data data_append_align(struct data d, int align);
  101. struct data data_add_marker(struct data d, enum markertype type, char *ref);
  102. bool data_is_one_string(struct data d);
  103. /* DT constraints */
  104. #define MAX_PROPNAME_LEN 31
  105. #define MAX_NODENAME_LEN 31
  106. /* Live trees */
  107. struct label {
  108. bool deleted;
  109. char *label;
  110. struct label *next;
  111. };
  112. struct bus_type {
  113. const char *name;
  114. };
  115. struct property {
  116. bool deleted;
  117. char *name;
  118. struct data val;
  119. struct property *next;
  120. struct label *labels;
  121. };
  122. struct node {
  123. bool deleted;
  124. char *name;
  125. struct property *proplist;
  126. struct node *children;
  127. struct node *parent;
  128. struct node *next_sibling;
  129. char *fullpath;
  130. int basenamelen;
  131. cell_t phandle;
  132. int addr_cells, size_cells;
  133. struct label *labels;
  134. const struct bus_type *bus;
  135. };
  136. #define for_each_label_withdel(l0, l) \
  137. for ((l) = (l0); (l); (l) = (l)->next)
  138. #define for_each_label(l0, l) \
  139. for_each_label_withdel(l0, l) \
  140. if (!(l)->deleted)
  141. #define for_each_property_withdel(n, p) \
  142. for ((p) = (n)->proplist; (p); (p) = (p)->next)
  143. #define for_each_property(n, p) \
  144. for_each_property_withdel(n, p) \
  145. if (!(p)->deleted)
  146. #define for_each_child_withdel(n, c) \
  147. for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
  148. #define for_each_child(n, c) \
  149. for_each_child_withdel(n, c) \
  150. if (!(c)->deleted)
  151. void add_label(struct label **labels, char *label);
  152. void delete_labels(struct label **labels);
  153. struct property *build_property(char *name, struct data val);
  154. struct property *build_property_delete(char *name);
  155. struct property *chain_property(struct property *first, struct property *list);
  156. struct property *reverse_properties(struct property *first);
  157. struct node *build_node(struct property *proplist, struct node *children);
  158. struct node *build_node_delete(void);
  159. struct node *name_node(struct node *node, char *name);
  160. struct node *chain_node(struct node *first, struct node *list);
  161. struct node *merge_nodes(struct node *old_node, struct node *new_node);
  162. struct node *add_orphan_node(struct node *old_node, struct node *new_node, char *ref);
  163. void add_property(struct node *node, struct property *prop);
  164. void delete_property_by_name(struct node *node, char *name);
  165. void delete_property(struct property *prop);
  166. void add_child(struct node *parent, struct node *child);
  167. void delete_node_by_name(struct node *parent, char *name);
  168. void delete_node(struct node *node);
  169. void append_to_property(struct node *node,
  170. char *name, const void *data, int len);
  171. const char *get_unitname(struct node *node);
  172. struct property *get_property(struct node *node, const char *propname);
  173. cell_t propval_cell(struct property *prop);
  174. cell_t propval_cell_n(struct property *prop, int n);
  175. struct property *get_property_by_label(struct node *tree, const char *label,
  176. struct node **node);
  177. struct marker *get_marker_label(struct node *tree, const char *label,
  178. struct node **node, struct property **prop);
  179. struct node *get_subnode(struct node *node, const char *nodename);
  180. struct node *get_node_by_path(struct node *tree, const char *path);
  181. struct node *get_node_by_label(struct node *tree, const char *label);
  182. struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
  183. struct node *get_node_by_ref(struct node *tree, const char *ref);
  184. cell_t get_node_phandle(struct node *root, struct node *node);
  185. uint32_t guess_boot_cpuid(struct node *tree);
  186. /* Boot info (tree plus memreserve information */
  187. struct reserve_info {
  188. uint64_t address, size;
  189. struct reserve_info *next;
  190. struct label *labels;
  191. };
  192. struct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
  193. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  194. struct reserve_info *list);
  195. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  196. struct reserve_info *new);
  197. struct dt_info {
  198. unsigned int dtsflags;
  199. struct reserve_info *reservelist;
  200. uint32_t boot_cpuid_phys;
  201. struct node *dt; /* the device tree */
  202. const char *outname; /* filename being written to, "-" for stdout */
  203. };
  204. /* DTS version flags definitions */
  205. #define DTSF_V1 0x0001 /* /dts-v1/ */
  206. #define DTSF_PLUGIN 0x0002 /* /plugin/ */
  207. struct dt_info *build_dt_info(unsigned int dtsflags,
  208. struct reserve_info *reservelist,
  209. struct node *tree, uint32_t boot_cpuid_phys);
  210. void sort_tree(struct dt_info *dti);
  211. void generate_label_tree(struct dt_info *dti, char *name, bool allocph);
  212. void generate_fixups_tree(struct dt_info *dti, char *name);
  213. void generate_local_fixups_tree(struct dt_info *dti, char *name);
  214. /* Checks */
  215. void parse_checks_option(bool warn, bool error, const char *arg);
  216. void process_checks(bool force, struct dt_info *dti);
  217. /* Flattened trees */
  218. void dt_to_blob(FILE *f, struct dt_info *dti, int version);
  219. void dt_to_asm(FILE *f, struct dt_info *dti, int version);
  220. struct dt_info *dt_from_blob(const char *fname);
  221. /* Tree source */
  222. void dt_to_source(FILE *f, struct dt_info *dti);
  223. struct dt_info *dt_from_source(const char *f);
  224. /* FS trees */
  225. struct dt_info *dt_from_fs(const char *dirname);
  226. #endif /* DTC_H */