dtc.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <stdarg.h>
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <libfdt_env.h>
  32. #include <fdt.h>
  33. #include "util.h"
  34. #ifdef DEBUG
  35. #define debug(fmt,args...) printf(fmt, ##args)
  36. #else
  37. #define debug(fmt,args...)
  38. #endif
  39. #define DEFAULT_FDT_VERSION 17
  40. /*
  41. * Command line options
  42. */
  43. extern int quiet; /* Level of quietness */
  44. extern int reservenum; /* Number of memory reservation slots */
  45. extern int minsize; /* Minimum blob size */
  46. extern int padsize; /* Additional padding to blob */
  47. extern int phandle_format; /* Use linux,phandle or phandle properties */
  48. #define PHANDLE_LEGACY 0x1
  49. #define PHANDLE_EPAPR 0x2
  50. #define PHANDLE_BOTH 0x3
  51. typedef uint32_t cell_t;
  52. #define streq(a, b) (strcmp((a), (b)) == 0)
  53. #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
  54. #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
  55. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  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){ /* 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_re(struct data d, const struct fdt_reserve_entry *re);
  90. struct data data_append_addr(struct data d, uint64_t addr);
  91. struct data data_append_byte(struct data d, uint8_t byte);
  92. struct data data_append_zeroes(struct data d, int len);
  93. struct data data_append_align(struct data d, int align);
  94. struct data data_add_marker(struct data d, enum markertype type, char *ref);
  95. int data_is_one_string(struct data d);
  96. /* DT constraints */
  97. #define MAX_PROPNAME_LEN 31
  98. #define MAX_NODENAME_LEN 31
  99. /* Live trees */
  100. struct label {
  101. char *label;
  102. struct label *next;
  103. };
  104. struct property {
  105. char *name;
  106. struct data val;
  107. struct property *next;
  108. struct label *labels;
  109. };
  110. struct node {
  111. char *name;
  112. struct property *proplist;
  113. struct node *children;
  114. struct node *parent;
  115. struct node *next_sibling;
  116. char *fullpath;
  117. int basenamelen;
  118. cell_t phandle;
  119. int addr_cells, size_cells;
  120. struct label *labels;
  121. };
  122. #define for_each_label(l0, l) \
  123. for ((l) = (l0); (l); (l) = (l)->next)
  124. #define for_each_property(n, p) \
  125. for ((p) = (n)->proplist; (p); (p) = (p)->next)
  126. #define for_each_child(n, c) \
  127. for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
  128. void add_label(struct label **labels, char *label);
  129. struct property *build_property(char *name, struct data val);
  130. struct property *chain_property(struct property *first, struct property *list);
  131. struct property *reverse_properties(struct property *first);
  132. struct node *build_node(struct property *proplist, struct node *children);
  133. struct node *name_node(struct node *node, char *name);
  134. struct node *chain_node(struct node *first, struct node *list);
  135. struct node *merge_nodes(struct node *old_node, struct node *new_node);
  136. void add_property(struct node *node, struct property *prop);
  137. void add_child(struct node *parent, struct node *child);
  138. const char *get_unitname(struct node *node);
  139. struct property *get_property(struct node *node, const char *propname);
  140. cell_t propval_cell(struct property *prop);
  141. struct property *get_property_by_label(struct node *tree, const char *label,
  142. struct node **node);
  143. struct marker *get_marker_label(struct node *tree, const char *label,
  144. struct node **node, struct property **prop);
  145. struct node *get_subnode(struct node *node, const char *nodename);
  146. struct node *get_node_by_path(struct node *tree, const char *path);
  147. struct node *get_node_by_label(struct node *tree, const char *label);
  148. struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
  149. struct node *get_node_by_ref(struct node *tree, const char *ref);
  150. cell_t get_node_phandle(struct node *root, struct node *node);
  151. uint32_t guess_boot_cpuid(struct node *tree);
  152. /* Boot info (tree plus memreserve information */
  153. struct reserve_info {
  154. struct fdt_reserve_entry re;
  155. struct reserve_info *next;
  156. struct label *labels;
  157. };
  158. struct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
  159. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  160. struct reserve_info *list);
  161. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  162. struct reserve_info *new);
  163. struct boot_info {
  164. struct reserve_info *reservelist;
  165. struct node *dt; /* the device tree */
  166. uint32_t boot_cpuid_phys;
  167. };
  168. struct boot_info *build_boot_info(struct reserve_info *reservelist,
  169. struct node *tree, uint32_t boot_cpuid_phys);
  170. void sort_tree(struct boot_info *bi);
  171. /* Checks */
  172. void process_checks(int force, struct boot_info *bi);
  173. /* Flattened trees */
  174. void dt_to_blob(FILE *f, struct boot_info *bi, int version);
  175. void dt_to_asm(FILE *f, struct boot_info *bi, int version);
  176. struct boot_info *dt_from_blob(const char *fname);
  177. /* Tree source */
  178. void dt_to_source(FILE *f, struct boot_info *bi);
  179. struct boot_info *dt_from_source(const char *f);
  180. /* FS trees */
  181. struct boot_info *dt_from_fs(const char *dirname);
  182. #endif /* _DTC_H */