/kern_oII/scripts/dtc/dtc.h

http://omnia2droid.googlecode.com/ · C++ Header · 246 lines · 151 code · 64 blank · 31 comment · 9 complexity · bc957586a6179d5a61526fa2205c4b31 MD5 · raw file

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