dtc.h 6.8 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 <stdarg.h>
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <netinet/in.h>
  32. #include <endian.h>
  33. #include <byteswap.h>
  34. #include <fdt.h>
  35. #define DEFAULT_FDT_VERSION 17
  36. /*
  37. * Command line options
  38. */
  39. extern int quiet; /* Level of quietness */
  40. extern int reservenum; /* Number of memory reservation slots */
  41. extern int minsize; /* Minimum blob size */
  42. extern int padsize; /* Additional padding to blob */
  43. static inline void __attribute__((noreturn)) die(char * str, ...)
  44. {
  45. va_list ap;
  46. va_start(ap, str);
  47. fprintf(stderr, "FATAL ERROR: ");
  48. vfprintf(stderr, str, ap);
  49. exit(1);
  50. }
  51. static inline void *xmalloc(size_t len)
  52. {
  53. void *new = malloc(len);
  54. if (! new)
  55. die("malloc() failed\n");
  56. return new;
  57. }
  58. static inline void *xrealloc(void *p, size_t len)
  59. {
  60. void *new = realloc(p, len);
  61. if (! new)
  62. die("realloc() failed (len=%d)\n", len);
  63. return new;
  64. }
  65. typedef uint8_t u8;
  66. typedef uint16_t u16;
  67. typedef uint32_t u32;
  68. typedef uint64_t u64;
  69. typedef u32 cell_t;
  70. #define cpu_to_be16(x) htons(x)
  71. #define be16_to_cpu(x) ntohs(x)
  72. #define cpu_to_be32(x) htonl(x)
  73. #define be32_to_cpu(x) ntohl(x)
  74. #if __BYTE_ORDER == __BIG_ENDIAN
  75. #define cpu_to_be64(x) (x)
  76. #define be64_to_cpu(x) (x)
  77. #else
  78. #define cpu_to_be64(x) bswap_64(x)
  79. #define be64_to_cpu(x) bswap_64(x)
  80. #endif
  81. #define streq(a, b) (strcmp((a), (b)) == 0)
  82. #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
  83. #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
  84. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  85. /* Data blobs */
  86. enum markertype {
  87. REF_PHANDLE,
  88. REF_PATH,
  89. LABEL,
  90. };
  91. struct marker {
  92. enum markertype type;
  93. int offset;
  94. char *ref;
  95. struct marker *next;
  96. };
  97. struct data {
  98. int len;
  99. char *val;
  100. int asize;
  101. struct marker *markers;
  102. };
  103. #define empty_data ((struct data){ /* all .members = 0 or NULL */ })
  104. #define for_each_marker(m) \
  105. for (; (m); (m) = (m)->next)
  106. #define for_each_marker_of_type(m, t) \
  107. for_each_marker(m) \
  108. if ((m)->type == (t))
  109. void data_free(struct data d);
  110. struct data data_grow_for(struct data d, int xlen);
  111. struct data data_copy_mem(const char *mem, int len);
  112. struct data data_copy_escape_string(const char *s, int len);
  113. struct data data_copy_file(FILE *f, size_t len);
  114. struct data data_append_data(struct data d, const void *p, int len);
  115. struct data data_insert_at_marker(struct data d, struct marker *m,
  116. const void *p, int len);
  117. struct data data_merge(struct data d1, struct data d2);
  118. struct data data_append_cell(struct data d, cell_t word);
  119. struct data data_append_re(struct data d, const struct fdt_reserve_entry *re);
  120. struct data data_append_addr(struct data d, u64 addr);
  121. struct data data_append_byte(struct data d, uint8_t byte);
  122. struct data data_append_zeroes(struct data d, int len);
  123. struct data data_append_align(struct data d, int align);
  124. struct data data_add_marker(struct data d, enum markertype type, char *ref);
  125. int data_is_one_string(struct data d);
  126. /* DT constraints */
  127. #define MAX_PROPNAME_LEN 31
  128. #define MAX_NODENAME_LEN 31
  129. /* Live trees */
  130. struct property {
  131. char *name;
  132. struct data val;
  133. struct property *next;
  134. char *label;
  135. };
  136. struct node {
  137. char *name;
  138. struct property *proplist;
  139. struct node *children;
  140. struct node *parent;
  141. struct node *next_sibling;
  142. char *fullpath;
  143. int basenamelen;
  144. cell_t phandle;
  145. int addr_cells, size_cells;
  146. char *label;
  147. };
  148. #define for_each_property(n, p) \
  149. for ((p) = (n)->proplist; (p); (p) = (p)->next)
  150. #define for_each_child(n, c) \
  151. for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
  152. struct property *build_property(char *name, struct data val, char *label);
  153. struct property *chain_property(struct property *first, struct property *list);
  154. struct property *reverse_properties(struct property *first);
  155. struct node *build_node(struct property *proplist, struct node *children);
  156. struct node *name_node(struct node *node, char *name, char *label);
  157. struct node *chain_node(struct node *first, struct node *list);
  158. void add_property(struct node *node, struct property *prop);
  159. void add_child(struct node *parent, struct node *child);
  160. const char *get_unitname(struct node *node);
  161. struct property *get_property(struct node *node, const char *propname);
  162. cell_t propval_cell(struct property *prop);
  163. struct node *get_subnode(struct node *node, const char *nodename);
  164. struct node *get_node_by_path(struct node *tree, const char *path);
  165. struct node *get_node_by_label(struct node *tree, const char *label);
  166. struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
  167. struct node *get_node_by_ref(struct node *tree, const char *ref);
  168. cell_t get_node_phandle(struct node *root, struct node *node);
  169. /* Boot info (tree plus memreserve information */
  170. struct reserve_info {
  171. struct fdt_reserve_entry re;
  172. struct reserve_info *next;
  173. char *label;
  174. };
  175. struct reserve_info *build_reserve_entry(u64 start, u64 len, char *label);
  176. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  177. struct reserve_info *list);
  178. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  179. struct reserve_info *new);
  180. struct boot_info {
  181. struct reserve_info *reservelist;
  182. struct node *dt; /* the device tree */
  183. };
  184. struct boot_info *build_boot_info(struct reserve_info *reservelist,
  185. struct node *tree);
  186. /* Checks */
  187. void process_checks(int force, struct boot_info *bi,
  188. int checkflag, int outversion, int boot_cpuid_phys);
  189. /* Flattened trees */
  190. void dt_to_blob(FILE *f, struct boot_info *bi, int version,
  191. int boot_cpuid_phys);
  192. void dt_to_asm(FILE *f, struct boot_info *bi, int version,
  193. int boot_cpuid_phys);
  194. struct boot_info *dt_from_blob(FILE *f);
  195. /* Tree source */
  196. void dt_to_source(FILE *f, struct boot_info *bi);
  197. struct boot_info *dt_from_source(const char *f);
  198. /* FS trees */
  199. struct boot_info *dt_from_fs(const char *dirname);
  200. /* misc */
  201. char *join_path(const char *path, const char *name);
  202. void fill_fullpaths(struct node *tree, const char *prefix);
  203. #endif /* _DTC_H */