dtc.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #define for_each_label_withdel(l0, l) \
  128. for ((l) = (l0); (l); (l) = (l)->next)
  129. #define for_each_label(l0, l) \
  130. for_each_label_withdel(l0, l) \
  131. if (!(l)->deleted)
  132. #define for_each_property_withdel(n, p) \
  133. for ((p) = (n)->proplist; (p); (p) = (p)->next)
  134. #define for_each_property(n, p) \
  135. for_each_property_withdel(n, p) \
  136. if (!(p)->deleted)
  137. #define for_each_child_withdel(n, c) \
  138. for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
  139. #define for_each_child(n, c) \
  140. for_each_child_withdel(n, c) \
  141. if (!(c)->deleted)
  142. void add_label(struct label **labels, char *label);
  143. void delete_labels(struct label **labels);
  144. struct property *build_property(char *name, struct data val);
  145. struct property *build_property_delete(char *name);
  146. struct property *chain_property(struct property *first, struct property *list);
  147. struct property *reverse_properties(struct property *first);
  148. struct node *build_node(struct property *proplist, struct node *children);
  149. struct node *build_node_delete(void);
  150. struct node *name_node(struct node *node, char *name);
  151. struct node *chain_node(struct node *first, struct node *list);
  152. struct node *merge_nodes(struct node *old_node, struct node *new_node);
  153. void add_property(struct node *node, struct property *prop);
  154. void delete_property_by_name(struct node *node, char *name);
  155. void delete_property(struct property *prop);
  156. void add_child(struct node *parent, struct node *child);
  157. void delete_node_by_name(struct node *parent, char *name);
  158. void delete_node(struct node *node);
  159. const char *get_unitname(struct node *node);
  160. struct property *get_property(struct node *node, const char *propname);
  161. cell_t propval_cell(struct property *prop);
  162. struct property *get_property_by_label(struct node *tree, const char *label,
  163. struct node **node);
  164. struct marker *get_marker_label(struct node *tree, const char *label,
  165. struct node **node, struct property **prop);
  166. struct node *get_subnode(struct node *node, const char *nodename);
  167. struct node *get_node_by_path(struct node *tree, const char *path);
  168. struct node *get_node_by_label(struct node *tree, const char *label);
  169. struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
  170. struct node *get_node_by_ref(struct node *tree, const char *ref);
  171. cell_t get_node_phandle(struct node *root, struct node *node);
  172. uint32_t guess_boot_cpuid(struct node *tree);
  173. /* Boot info (tree plus memreserve information */
  174. struct reserve_info {
  175. struct fdt_reserve_entry re;
  176. struct reserve_info *next;
  177. struct label *labels;
  178. };
  179. struct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
  180. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  181. struct reserve_info *list);
  182. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  183. struct reserve_info *new);
  184. struct boot_info {
  185. struct reserve_info *reservelist;
  186. struct node *dt; /* the device tree */
  187. uint32_t boot_cpuid_phys;
  188. };
  189. struct boot_info *build_boot_info(struct reserve_info *reservelist,
  190. struct node *tree, uint32_t boot_cpuid_phys);
  191. void sort_tree(struct boot_info *bi);
  192. /* Checks */
  193. void parse_checks_option(bool warn, bool error, const char *optarg);
  194. void process_checks(int force, struct boot_info *bi);
  195. /* Flattened trees */
  196. void dt_to_blob(FILE *f, struct boot_info *bi, int version);
  197. void dt_to_asm(FILE *f, struct boot_info *bi, int version);
  198. struct boot_info *dt_from_blob(const char *fname);
  199. /* Tree source */
  200. void dt_to_source(FILE *f, struct boot_info *bi);
  201. struct boot_info *dt_from_source(const char *f);
  202. /* FS trees */
  203. struct boot_info *dt_from_fs(const char *dirname);
  204. #endif /* _DTC_H */