dtc.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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(...) printf(__VA_ARGS__)
  37. #else
  38. #define debug(...)
  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. /* Data blobs */
  57. enum markertype {
  58. REF_PHANDLE,
  59. REF_PATH,
  60. LABEL,
  61. };
  62. struct marker {
  63. enum markertype type;
  64. int offset;
  65. char *ref;
  66. struct marker *next;
  67. };
  68. struct data {
  69. int len;
  70. char *val;
  71. struct marker *markers;
  72. };
  73. #define empty_data ((struct data){ 0 /* all .members = 0 or NULL */ })
  74. #define for_each_marker(m) \
  75. for (; (m); (m) = (m)->next)
  76. #define for_each_marker_of_type(m, t) \
  77. for_each_marker(m) \
  78. if ((m)->type == (t))
  79. void data_free(struct data d);
  80. struct data data_grow_for(struct data d, int xlen);
  81. struct data data_copy_mem(const char *mem, int len);
  82. struct data data_copy_escape_string(const char *s, int len);
  83. struct data data_copy_file(FILE *f, size_t len);
  84. struct data data_append_data(struct data d, const void *p, int len);
  85. struct data data_insert_at_marker(struct data d, struct marker *m,
  86. const void *p, int len);
  87. struct data data_merge(struct data d1, struct data d2);
  88. struct data data_append_cell(struct data d, cell_t word);
  89. struct data data_append_integer(struct data d, uint64_t word, int bits);
  90. struct data data_append_re(struct data d, const struct fdt_reserve_entry *re);
  91. struct data data_append_addr(struct data d, uint64_t addr);
  92. struct data data_append_byte(struct data d, uint8_t byte);
  93. struct data data_append_zeroes(struct data d, int len);
  94. struct data data_append_align(struct data d, int align);
  95. struct data data_add_marker(struct data d, enum markertype type, char *ref);
  96. bool data_is_one_string(struct data d);
  97. /* DT constraints */
  98. #define MAX_PROPNAME_LEN 31
  99. #define MAX_NODENAME_LEN 31
  100. /* Live trees */
  101. struct label {
  102. bool deleted;
  103. char *label;
  104. struct label *next;
  105. };
  106. struct property {
  107. bool deleted;
  108. char *name;
  109. struct data val;
  110. struct property *next;
  111. struct label *labels;
  112. };
  113. struct node {
  114. bool deleted;
  115. char *name;
  116. struct property *proplist;
  117. struct node *children;
  118. struct node *parent;
  119. struct node *next_sibling;
  120. char *fullpath;
  121. int basenamelen;
  122. cell_t phandle;
  123. int addr_cells, size_cells;
  124. struct label *labels;
  125. };
  126. #define for_each_label_withdel(l0, l) \
  127. for ((l) = (l0); (l); (l) = (l)->next)
  128. #define for_each_label(l0, l) \
  129. for_each_label_withdel(l0, l) \
  130. if (!(l)->deleted)
  131. #define for_each_property_withdel(n, p) \
  132. for ((p) = (n)->proplist; (p); (p) = (p)->next)
  133. #define for_each_property(n, p) \
  134. for_each_property_withdel(n, p) \
  135. if (!(p)->deleted)
  136. #define for_each_child_withdel(n, c) \
  137. for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
  138. #define for_each_child(n, c) \
  139. for_each_child_withdel(n, c) \
  140. if (!(c)->deleted)
  141. void add_label(struct label **labels, char *label);
  142. void delete_labels(struct label **labels);
  143. struct property *build_property(char *name, struct data val);
  144. struct property *build_property_delete(char *name);
  145. struct property *chain_property(struct property *first, struct property *list);
  146. struct property *reverse_properties(struct property *first);
  147. struct node *build_node(struct property *proplist, struct node *children);
  148. struct node *build_node_delete(void);
  149. struct node *name_node(struct node *node, char *name);
  150. struct node *chain_node(struct node *first, struct node *list);
  151. struct node *merge_nodes(struct node *old_node, struct node *new_node);
  152. void add_property(struct node *node, struct property *prop);
  153. void delete_property_by_name(struct node *node, char *name);
  154. void delete_property(struct property *prop);
  155. void add_child(struct node *parent, struct node *child);
  156. void delete_node_by_name(struct node *parent, char *name);
  157. void delete_node(struct node *node);
  158. const char *get_unitname(struct node *node);
  159. struct property *get_property(struct node *node, const char *propname);
  160. cell_t propval_cell(struct property *prop);
  161. struct property *get_property_by_label(struct node *tree, const char *label,
  162. struct node **node);
  163. struct marker *get_marker_label(struct node *tree, const char *label,
  164. struct node **node, struct property **prop);
  165. struct node *get_subnode(struct node *node, const char *nodename);
  166. struct node *get_node_by_path(struct node *tree, const char *path);
  167. struct node *get_node_by_label(struct node *tree, const char *label);
  168. struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
  169. struct node *get_node_by_ref(struct node *tree, const char *ref);
  170. cell_t get_node_phandle(struct node *root, struct node *node);
  171. uint32_t guess_boot_cpuid(struct node *tree);
  172. /* Boot info (tree plus memreserve information */
  173. struct reserve_info {
  174. struct fdt_reserve_entry re;
  175. struct reserve_info *next;
  176. struct label *labels;
  177. };
  178. struct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
  179. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  180. struct reserve_info *list);
  181. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  182. struct reserve_info *new);
  183. struct boot_info {
  184. struct reserve_info *reservelist;
  185. struct node *dt; /* the device tree */
  186. uint32_t boot_cpuid_phys;
  187. };
  188. struct boot_info *build_boot_info(struct reserve_info *reservelist,
  189. struct node *tree, uint32_t boot_cpuid_phys);
  190. void sort_tree(struct boot_info *bi);
  191. /* Checks */
  192. void parse_checks_option(bool warn, bool error, const char *arg);
  193. void process_checks(bool force, struct boot_info *bi);
  194. /* Flattened trees */
  195. void dt_to_blob(FILE *f, struct boot_info *bi, int version);
  196. void dt_to_asm(FILE *f, struct boot_info *bi, int version);
  197. struct boot_info *dt_from_blob(const char *fname);
  198. /* Tree source */
  199. void dt_to_source(FILE *f, struct boot_info *bi);
  200. struct boot_info *dt_from_source(const char *f);
  201. /* FS trees */
  202. struct boot_info *dt_from_fs(const char *dirname);
  203. #endif /* _DTC_H */