David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 1 | #ifndef _DTC_H |
| 2 | #define _DTC_H |
| 3 | |
| 4 | /* |
| 5 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 21 | * USA |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdarg.h> |
| 29 | #include <assert.h> |
| 30 | #include <ctype.h> |
| 31 | #include <errno.h> |
| 32 | #include <unistd.h> |
| 33 | #include <netinet/in.h> |
| 34 | |
| 35 | static inline void die(char * str, ...) |
| 36 | { |
| 37 | va_list ap; |
| 38 | |
| 39 | va_start(ap, str); |
| 40 | fprintf(stderr, "FATAL ERROR: "); |
| 41 | vfprintf(stderr, str, ap); |
| 42 | exit(1); |
| 43 | } |
| 44 | |
| 45 | static inline void *xmalloc(size_t len) |
| 46 | { |
| 47 | void *new = malloc(len); |
| 48 | |
| 49 | if (! new) |
| 50 | die("malloc() failed\n"); |
| 51 | |
| 52 | return new; |
| 53 | } |
| 54 | |
| 55 | static inline void *xrealloc(void *p, size_t len) |
| 56 | { |
| 57 | void *new = realloc(p, len); |
| 58 | |
| 59 | if (! new) |
| 60 | die("realloc() failed (len=%d)\n", len); |
| 61 | |
| 62 | return new; |
| 63 | } |
| 64 | |
| 65 | typedef uint16_t u16; |
| 66 | typedef uint32_t u32; |
| 67 | typedef uint64_t u64; |
| 68 | typedef u32 cell_t; |
| 69 | |
| 70 | #define cpu_to_be16(x) htons(x) |
| 71 | #define be16_to_cpu(x) ntohs(x) |
| 72 | |
| 73 | #define cpu_to_be32(x) htonl(x) |
| 74 | #define be32_to_cpu(x) ntohl(x) |
| 75 | |
| 76 | |
| 77 | |
| 78 | #define streq(a, b) (strcmp((a), (b)) == 0) |
| 79 | #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) |
| 80 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 81 | |
| 82 | /* Data blobs */ |
| 83 | struct data { |
| 84 | int len; |
| 85 | char *val; |
| 86 | int asize; |
| 87 | }; |
| 88 | |
| 89 | #define empty_data ((struct data){.len = 0, .val = NULL, .asize = 0}) |
| 90 | |
| 91 | void data_free(struct data d); |
| 92 | |
| 93 | struct data data_grow_for(struct data d, int xlen); |
| 94 | |
| 95 | struct data data_copy_mem(char *mem, int len); |
| 96 | struct data data_copy_escape_string(char *s, int len); |
| 97 | struct data data_copy_file(FILE *f, size_t len); |
| 98 | |
| 99 | struct data data_append_data(struct data d, void *p, int len); |
| 100 | struct data data_append_cell(struct data d, cell_t word); |
| 101 | struct data data_append_byte(struct data d, uint8_t byte); |
| 102 | struct data data_append_zeroes(struct data d, int len); |
| 103 | struct data data_append_align(struct data d, int align); |
| 104 | |
| 105 | int data_is_one_string(struct data d); |
| 106 | |
| 107 | /* DT constraints */ |
| 108 | |
| 109 | #define MAX_PROPNAME_LEN 31 |
| 110 | #define MAX_NODENAME_LEN 31 |
| 111 | |
| 112 | /* Live trees */ |
| 113 | struct property { |
| 114 | char *name; |
| 115 | struct data val; |
| 116 | |
| 117 | struct property *next; |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame^] | 118 | |
| 119 | char *label; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | struct node { |
| 123 | char *name; |
| 124 | struct property *proplist; |
| 125 | struct node *children; |
| 126 | |
| 127 | struct node *parent; |
| 128 | struct node *next_sibling; |
| 129 | |
| 130 | char *fullpath; |
| 131 | int basenamelen; |
| 132 | |
| 133 | cell_t phandle; |
| 134 | int addr_cells, size_cells; |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame^] | 135 | |
| 136 | char *label; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | #define for_each_property(n, p) \ |
| 140 | for ((p) = (n)->proplist; (p); (p) = (p)->next) |
| 141 | |
| 142 | #define for_each_child(n, c) \ |
| 143 | for ((c) = (n)->children; (c); (c) = (c)->next_sibling) |
| 144 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame^] | 145 | struct property *build_property(char *name, struct data val, char *label); |
| 146 | struct property *build_empty_property(char *name, char *label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 147 | struct property *chain_property(struct property *first, struct property *list); |
| 148 | |
| 149 | struct node *build_node(struct property *proplist, struct node *children); |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame^] | 150 | struct node *name_node(struct node *node, char *name, char *label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 151 | struct node *chain_node(struct node *first, struct node *list); |
| 152 | |
| 153 | void add_property(struct node *node, struct property *prop); |
| 154 | void add_child(struct node *parent, struct node *child); |
| 155 | |
| 156 | int check_device_tree(struct node *dt); |
| 157 | |
| 158 | /* Flattened trees */ |
| 159 | |
| 160 | enum flat_dt_format { |
| 161 | FFMT_BIN, |
| 162 | FFMT_ASM, |
| 163 | }; |
| 164 | |
| 165 | void write_dt_blob(FILE *f, struct node *tree, int version, int reservenum); |
| 166 | void write_dt_asm(FILE *f, struct node *tree, int version, int reservenum); |
| 167 | |
| 168 | struct node *dt_from_blob(FILE *f); |
| 169 | |
| 170 | /* Tree source */ |
| 171 | |
| 172 | void write_tree_source(FILE *f, struct node *tree, int level); |
| 173 | |
| 174 | struct node *dt_from_source(FILE *f); |
| 175 | |
| 176 | /* FS trees */ |
| 177 | |
| 178 | struct node *dt_from_fs(char *dirname); |
| 179 | |
| 180 | /* misc */ |
| 181 | |
| 182 | char *join_path(char *path, char *name); |
| 183 | void fill_fullpaths(struct node *tree, char *prefix); |
| 184 | |
| 185 | #endif /* _DTC_H */ |