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> |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 34 | #include <endian.h> |
| 35 | #include <byteswap.h> |
| 36 | |
| 37 | #include "flat_dt.h" |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 38 | |
Jerry Van Baren | cd1da87 | 2007-03-18 16:49:24 -0400 | [diff] [blame] | 39 | /* |
| 40 | * Level of quietness |
| 41 | */ |
| 42 | int quiet; |
| 43 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 44 | static inline void die(char * str, ...) |
| 45 | { |
| 46 | va_list ap; |
| 47 | |
| 48 | va_start(ap, str); |
| 49 | fprintf(stderr, "FATAL ERROR: "); |
| 50 | vfprintf(stderr, str, ap); |
| 51 | exit(1); |
| 52 | } |
| 53 | |
| 54 | static inline void *xmalloc(size_t len) |
| 55 | { |
| 56 | void *new = malloc(len); |
| 57 | |
| 58 | if (! new) |
| 59 | die("malloc() failed\n"); |
| 60 | |
| 61 | return new; |
| 62 | } |
| 63 | |
| 64 | static inline void *xrealloc(void *p, size_t len) |
| 65 | { |
| 66 | void *new = realloc(p, len); |
| 67 | |
| 68 | if (! new) |
| 69 | die("realloc() failed (len=%d)\n", len); |
| 70 | |
| 71 | return new; |
| 72 | } |
| 73 | |
David Gibson | 03a9b9d | 2005-07-11 16:49:52 +1000 | [diff] [blame] | 74 | typedef uint8_t u8; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 75 | typedef uint16_t u16; |
| 76 | typedef uint32_t u32; |
| 77 | typedef uint64_t u64; |
| 78 | typedef u32 cell_t; |
| 79 | |
| 80 | #define cpu_to_be16(x) htons(x) |
| 81 | #define be16_to_cpu(x) ntohs(x) |
| 82 | |
| 83 | #define cpu_to_be32(x) htonl(x) |
| 84 | #define be32_to_cpu(x) ntohl(x) |
| 85 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 86 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 87 | #define cpu_to_be64(x) (x) |
| 88 | #define be64_to_cpu(x) (x) |
| 89 | #else |
| 90 | #define cpu_to_be64(x) bswap_64(x) |
| 91 | #define be64_to_cpu(x) bswap_64(x) |
| 92 | #endif |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 93 | |
| 94 | #define streq(a, b) (strcmp((a), (b)) == 0) |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 95 | #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) |
| 96 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 97 | #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) |
| 98 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 99 | |
| 100 | /* Data blobs */ |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 101 | struct fixup { |
| 102 | int offset; |
| 103 | char *ref; |
| 104 | struct fixup *next; |
| 105 | }; |
| 106 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 107 | struct data { |
| 108 | int len; |
| 109 | char *val; |
| 110 | int asize; |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 111 | struct fixup *refs; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 112 | }; |
| 113 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 114 | #define empty_data \ |
| 115 | ((struct data){.len = 0, .val = NULL, .asize = 0, .refs = NULL}) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 116 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 117 | void fixup_free(struct fixup *f); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 118 | void data_free(struct data d); |
| 119 | |
| 120 | struct data data_grow_for(struct data d, int xlen); |
| 121 | |
| 122 | struct data data_copy_mem(char *mem, int len); |
| 123 | struct data data_copy_escape_string(char *s, int len); |
| 124 | struct data data_copy_file(FILE *f, size_t len); |
| 125 | |
| 126 | struct data data_append_data(struct data d, void *p, int len); |
David Gibson | 32da475 | 2007-02-07 16:37:50 +1100 | [diff] [blame] | 127 | struct data data_merge(struct data d1, struct data d2); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 128 | struct data data_append_cell(struct data d, cell_t word); |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 129 | struct data data_append_re(struct data d, struct reserve_entry *re); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 130 | struct data data_append_addr(struct data d, u64 addr); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 131 | struct data data_append_byte(struct data d, uint8_t byte); |
| 132 | struct data data_append_zeroes(struct data d, int len); |
| 133 | struct data data_append_align(struct data d, int align); |
| 134 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 135 | struct data data_add_fixup(struct data d, char *ref); |
| 136 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 137 | int data_is_one_string(struct data d); |
| 138 | |
| 139 | /* DT constraints */ |
| 140 | |
| 141 | #define MAX_PROPNAME_LEN 31 |
| 142 | #define MAX_NODENAME_LEN 31 |
| 143 | |
| 144 | /* Live trees */ |
| 145 | struct property { |
| 146 | char *name; |
| 147 | struct data val; |
| 148 | |
| 149 | struct property *next; |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 150 | |
| 151 | char *label; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | struct node { |
| 155 | char *name; |
| 156 | struct property *proplist; |
| 157 | struct node *children; |
| 158 | |
| 159 | struct node *parent; |
| 160 | struct node *next_sibling; |
| 161 | |
| 162 | char *fullpath; |
| 163 | int basenamelen; |
| 164 | |
| 165 | cell_t phandle; |
| 166 | int addr_cells, size_cells; |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 167 | |
| 168 | char *label; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | #define for_each_property(n, p) \ |
| 172 | for ((p) = (n)->proplist; (p); (p) = (p)->next) |
| 173 | |
| 174 | #define for_each_child(n, c) \ |
| 175 | for ((c) = (n)->children; (c); (c) = (c)->next_sibling) |
| 176 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 177 | struct property *build_property(char *name, struct data val, char *label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 178 | struct property *chain_property(struct property *first, struct property *list); |
| 179 | |
| 180 | struct node *build_node(struct property *proplist, struct node *children); |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 181 | struct node *name_node(struct node *node, char *name, char *label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 182 | struct node *chain_node(struct node *first, struct node *list); |
| 183 | |
| 184 | void add_property(struct node *node, struct property *prop); |
| 185 | void add_child(struct node *parent, struct node *child); |
| 186 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 187 | int check_device_tree(struct node *dt, int outversion, int boot_cpuid_phys); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 188 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 189 | /* Boot info (tree plus memreserve information */ |
| 190 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 191 | struct reserve_info { |
| 192 | struct reserve_entry re; |
| 193 | |
| 194 | struct reserve_info *next; |
| 195 | |
| 196 | char *label; |
| 197 | }; |
| 198 | |
| 199 | struct reserve_info *build_reserve_entry(u64 start, u64 len, char *label); |
| 200 | struct reserve_info *chain_reserve_entry(struct reserve_info *first, |
| 201 | struct reserve_info *list); |
| 202 | struct reserve_info *add_reserve_entry(struct reserve_info *list, |
| 203 | struct reserve_info *new); |
| 204 | |
| 205 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 206 | struct boot_info { |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 207 | struct reserve_info *reservelist; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 208 | struct node *dt; /* the device tree */ |
| 209 | }; |
| 210 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 211 | struct boot_info *build_boot_info(struct reserve_info *reservelist, |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 212 | struct node *tree); |
| 213 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 214 | /* Flattened trees */ |
| 215 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 216 | void dt_to_blob(FILE *f, struct boot_info *bi, int version, |
| 217 | int boot_cpuid_phys); |
| 218 | void dt_to_asm(FILE *f, struct boot_info *bi, int version, |
| 219 | int boot_cpuid_phys); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 220 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 221 | struct boot_info *dt_from_blob(FILE *f); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 222 | |
| 223 | /* Tree source */ |
| 224 | |
David Gibson | 712e52e | 2005-10-26 16:56:26 +1000 | [diff] [blame] | 225 | void dt_to_source(FILE *f, struct boot_info *bi); |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 226 | struct boot_info *dt_from_source(const char *f); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 227 | |
| 228 | /* FS trees */ |
| 229 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 230 | struct boot_info *dt_from_fs(char *dirname); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 231 | |
| 232 | /* misc */ |
| 233 | |
| 234 | char *join_path(char *path, char *name); |
| 235 | void fill_fullpaths(struct node *tree, char *prefix); |
| 236 | |
| 237 | #endif /* _DTC_H */ |