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 | |
David Gibson | 03a9b9d | 2005-07-11 16:49:52 +1000 | [diff] [blame^] | 65 | typedef uint8_t u8; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 66 | typedef uint16_t u16; |
| 67 | typedef uint32_t u32; |
| 68 | typedef uint64_t u64; |
| 69 | typedef u32 cell_t; |
| 70 | |
| 71 | #define cpu_to_be16(x) htons(x) |
| 72 | #define be16_to_cpu(x) ntohs(x) |
| 73 | |
| 74 | #define cpu_to_be32(x) htonl(x) |
| 75 | #define be32_to_cpu(x) ntohl(x) |
| 76 | |
| 77 | |
| 78 | |
| 79 | #define streq(a, b) (strcmp((a), (b)) == 0) |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 80 | #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) |
| 81 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 82 | #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) |
| 83 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 84 | |
| 85 | /* Data blobs */ |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 86 | struct fixup { |
| 87 | int offset; |
| 88 | char *ref; |
| 89 | struct fixup *next; |
| 90 | }; |
| 91 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 92 | struct data { |
| 93 | int len; |
| 94 | char *val; |
| 95 | int asize; |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 96 | struct fixup *refs; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 97 | }; |
| 98 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 99 | #define empty_data \ |
| 100 | ((struct data){.len = 0, .val = NULL, .asize = 0, .refs = NULL}) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 101 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 102 | void fixup_free(struct fixup *f); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 103 | void data_free(struct data d); |
| 104 | |
| 105 | struct data data_grow_for(struct data d, int xlen); |
| 106 | |
| 107 | struct data data_copy_mem(char *mem, int len); |
| 108 | struct data data_copy_escape_string(char *s, int len); |
| 109 | struct data data_copy_file(FILE *f, size_t len); |
| 110 | |
| 111 | struct data data_append_data(struct data d, void *p, int len); |
| 112 | struct data data_append_cell(struct data d, cell_t word); |
| 113 | struct data data_append_byte(struct data d, uint8_t byte); |
| 114 | struct data data_append_zeroes(struct data d, int len); |
| 115 | struct data data_append_align(struct data d, int align); |
| 116 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 117 | struct data data_add_fixup(struct data d, char *ref); |
| 118 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 119 | int data_is_one_string(struct data d); |
| 120 | |
| 121 | /* DT constraints */ |
| 122 | |
| 123 | #define MAX_PROPNAME_LEN 31 |
| 124 | #define MAX_NODENAME_LEN 31 |
| 125 | |
| 126 | /* Live trees */ |
| 127 | struct property { |
| 128 | char *name; |
| 129 | struct data val; |
| 130 | |
| 131 | struct property *next; |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 132 | |
| 133 | char *label; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | struct node { |
| 137 | char *name; |
| 138 | struct property *proplist; |
| 139 | struct node *children; |
| 140 | |
| 141 | struct node *parent; |
| 142 | struct node *next_sibling; |
| 143 | |
| 144 | char *fullpath; |
| 145 | int basenamelen; |
| 146 | |
| 147 | cell_t phandle; |
| 148 | int addr_cells, size_cells; |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 149 | |
| 150 | char *label; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | #define for_each_property(n, p) \ |
| 154 | for ((p) = (n)->proplist; (p); (p) = (p)->next) |
| 155 | |
| 156 | #define for_each_child(n, c) \ |
| 157 | for ((c) = (n)->children; (c); (c) = (c)->next_sibling) |
| 158 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 159 | struct property *build_property(char *name, struct data val, char *label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 160 | struct property *chain_property(struct property *first, struct property *list); |
| 161 | |
| 162 | struct node *build_node(struct property *proplist, struct node *children); |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 163 | struct node *name_node(struct node *node, char *name, char *label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 164 | struct node *chain_node(struct node *first, struct node *list); |
| 165 | |
| 166 | void add_property(struct node *node, struct property *prop); |
| 167 | void add_child(struct node *parent, struct node *child); |
| 168 | |
| 169 | int check_device_tree(struct node *dt); |
| 170 | |
| 171 | /* Flattened trees */ |
| 172 | |
| 173 | enum flat_dt_format { |
| 174 | FFMT_BIN, |
| 175 | FFMT_ASM, |
| 176 | }; |
| 177 | |
| 178 | void write_dt_blob(FILE *f, struct node *tree, int version, int reservenum); |
| 179 | void write_dt_asm(FILE *f, struct node *tree, int version, int reservenum); |
| 180 | |
| 181 | struct node *dt_from_blob(FILE *f); |
| 182 | |
| 183 | /* Tree source */ |
| 184 | |
| 185 | void write_tree_source(FILE *f, struct node *tree, int level); |
| 186 | |
| 187 | struct node *dt_from_source(FILE *f); |
| 188 | |
| 189 | /* FS trees */ |
| 190 | |
| 191 | struct node *dt_from_fs(char *dirname); |
| 192 | |
| 193 | /* misc */ |
| 194 | |
| 195 | char *join_path(char *path, char *name); |
| 196 | void fill_fullpaths(struct node *tree, char *prefix); |
| 197 | |
| 198 | #endif /* _DTC_H */ |