David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| 3 | * |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 4 | * |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "dtc.h" |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 22 | |
| 23 | #define FTF_FULLPATH 0x1 |
| 24 | #define FTF_VARALIGN 0x2 |
| 25 | #define FTF_NAMEPROPS 0x4 |
| 26 | #define FTF_BOOTCPUID 0x8 |
| 27 | #define FTF_STRTABSIZE 0x10 |
David Gibson | 46c88df | 2007-03-14 11:02:40 +1100 | [diff] [blame] | 28 | #define FTF_STRUCTSIZE 0x20 |
Milton Miller | ce24322 | 2007-06-09 23:21:31 -0500 | [diff] [blame] | 29 | #define FTF_NOPS 0x40 |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 30 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 31 | static struct version_info { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 32 | int version; |
| 33 | int last_comp_version; |
| 34 | int hdr_size; |
| 35 | int flags; |
| 36 | } version_table[] = { |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 37 | {1, 1, FDT_V1_SIZE, |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 38 | FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS}, |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 39 | {2, 1, FDT_V2_SIZE, |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 40 | FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID}, |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 41 | {3, 1, FDT_V3_SIZE, |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 42 | FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE}, |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 43 | {16, 16, FDT_V3_SIZE, |
Milton Miller | ce24322 | 2007-06-09 23:21:31 -0500 | [diff] [blame] | 44 | FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS}, |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 45 | {17, 16, FDT_V17_SIZE, |
Milton Miller | ce24322 | 2007-06-09 23:21:31 -0500 | [diff] [blame] | 46 | FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS}, |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | struct emitter { |
| 50 | void (*cell)(void *, cell_t); |
| 51 | void (*string)(void *, char *, int); |
| 52 | void (*align)(void *, int); |
| 53 | void (*data)(void *, struct data); |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 54 | void (*beginnode)(void *, const char *); |
| 55 | void (*endnode)(void *, const char *); |
| 56 | void (*property)(void *, const char *); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static void bin_emit_cell(void *e, cell_t val) |
| 60 | { |
| 61 | struct data *dtbuf = e; |
| 62 | |
| 63 | *dtbuf = data_append_cell(*dtbuf, val); |
| 64 | } |
| 65 | |
| 66 | static void bin_emit_string(void *e, char *str, int len) |
| 67 | { |
| 68 | struct data *dtbuf = e; |
| 69 | |
| 70 | if (len == 0) |
| 71 | len = strlen(str); |
| 72 | |
| 73 | *dtbuf = data_append_data(*dtbuf, str, len); |
| 74 | *dtbuf = data_append_byte(*dtbuf, '\0'); |
| 75 | } |
| 76 | |
| 77 | static void bin_emit_align(void *e, int a) |
| 78 | { |
| 79 | struct data *dtbuf = e; |
| 80 | |
| 81 | *dtbuf = data_append_align(*dtbuf, a); |
| 82 | } |
| 83 | |
| 84 | static void bin_emit_data(void *e, struct data d) |
| 85 | { |
| 86 | struct data *dtbuf = e; |
| 87 | |
| 88 | *dtbuf = data_append_data(*dtbuf, d.val, d.len); |
| 89 | } |
| 90 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 91 | static void bin_emit_beginnode(void *e, const char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 92 | { |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 93 | bin_emit_cell(e, FDT_BEGIN_NODE); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 94 | } |
| 95 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 96 | static void bin_emit_endnode(void *e, const char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 97 | { |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 98 | bin_emit_cell(e, FDT_END_NODE); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 99 | } |
| 100 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 101 | static void bin_emit_property(void *e, const char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 102 | { |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 103 | bin_emit_cell(e, FDT_PROP); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 104 | } |
| 105 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 106 | static struct emitter bin_emitter = { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 107 | .cell = bin_emit_cell, |
| 108 | .string = bin_emit_string, |
| 109 | .align = bin_emit_align, |
| 110 | .data = bin_emit_data, |
| 111 | .beginnode = bin_emit_beginnode, |
| 112 | .endnode = bin_emit_endnode, |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 113 | .property = bin_emit_property, |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 114 | }; |
| 115 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 116 | static void emit_label(FILE *f, const char *prefix, const char *label) |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 117 | { |
| 118 | fprintf(f, "\t.globl\t%s_%s\n", prefix, label); |
| 119 | fprintf(f, "%s_%s:\n", prefix, label); |
| 120 | fprintf(f, "_%s_%s:\n", prefix, label); |
| 121 | } |
| 122 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 123 | static void emit_offset_label(FILE *f, const char *label, int offset) |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame] | 124 | { |
| 125 | fprintf(f, "\t.globl\t%s\n", label); |
| 126 | fprintf(f, "%s\t= . + %d\n", label, offset); |
| 127 | } |
| 128 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 129 | static void asm_emit_cell(void *e, cell_t val) |
| 130 | { |
| 131 | FILE *f = e; |
| 132 | |
Mark A. Greer | 7a9f663 | 2006-03-15 18:59:24 -0700 | [diff] [blame] | 133 | fprintf(f, "\t.long\t0x%x\n", val); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void asm_emit_string(void *e, char *str, int len) |
| 137 | { |
| 138 | FILE *f = e; |
Jon Loeliger | 2d50f8f | 2007-10-18 09:46:52 -0500 | [diff] [blame] | 139 | char c = 0; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 140 | |
| 141 | if (len != 0) { |
| 142 | /* XXX: ewww */ |
| 143 | c = str[len]; |
| 144 | str[len] = '\0'; |
| 145 | } |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 146 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 147 | fprintf(f, "\t.string\t\"%s\"\n", str); |
| 148 | |
| 149 | if (len != 0) { |
| 150 | str[len] = c; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void asm_emit_align(void *e, int a) |
| 155 | { |
| 156 | FILE *f = e; |
| 157 | |
| 158 | fprintf(f, "\t.balign\t%d\n", a); |
| 159 | } |
| 160 | |
| 161 | static void asm_emit_data(void *e, struct data d) |
| 162 | { |
| 163 | FILE *f = e; |
| 164 | int off = 0; |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 165 | struct marker *m; |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame] | 166 | |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 167 | m = d.markers; |
| 168 | while (m) { |
| 169 | if (m->type == LABEL) |
| 170 | emit_offset_label(f, m->ref, m->offset); |
| 171 | m = m->next; |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame] | 172 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 173 | |
| 174 | while ((d.len - off) >= sizeof(u32)) { |
| 175 | fprintf(f, "\t.long\t0x%x\n", |
| 176 | be32_to_cpu(*((u32 *)(d.val+off)))); |
| 177 | off += sizeof(u32); |
| 178 | } |
| 179 | |
| 180 | if ((d.len - off) >= sizeof(u16)) { |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 181 | fprintf(f, "\t.short\t0x%hx\n", |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 182 | be16_to_cpu(*((u16 *)(d.val+off)))); |
| 183 | off += sizeof(u16); |
| 184 | } |
| 185 | |
| 186 | if ((d.len - off) >= 1) { |
| 187 | fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]); |
| 188 | off += 1; |
| 189 | } |
| 190 | |
| 191 | assert(off == d.len); |
| 192 | } |
| 193 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 194 | static void asm_emit_beginnode(void *e, const char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 195 | { |
| 196 | FILE *f = e; |
| 197 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 198 | if (label) { |
| 199 | fprintf(f, "\t.globl\t%s\n", label); |
| 200 | fprintf(f, "%s:\n", label); |
| 201 | } |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 202 | fprintf(f, "\t.long\tFDT_BEGIN_NODE\n"); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 203 | } |
| 204 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 205 | static void asm_emit_endnode(void *e, const char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 206 | { |
| 207 | FILE *f = e; |
| 208 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 209 | fprintf(f, "\t.long\tFDT_END_NODE\n"); |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 210 | if (label) { |
| 211 | fprintf(f, "\t.globl\t%s_end\n", label); |
| 212 | fprintf(f, "%s_end:\n", label); |
| 213 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 214 | } |
| 215 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 216 | static void asm_emit_property(void *e, const char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 217 | { |
| 218 | FILE *f = e; |
| 219 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 220 | if (label) { |
| 221 | fprintf(f, "\t.globl\t%s\n", label); |
| 222 | fprintf(f, "%s:\n", label); |
| 223 | } |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 224 | fprintf(f, "\t.long\tFDT_PROP\n"); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 225 | } |
| 226 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 227 | static struct emitter asm_emitter = { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 228 | .cell = asm_emit_cell, |
| 229 | .string = asm_emit_string, |
| 230 | .align = asm_emit_align, |
| 231 | .data = asm_emit_data, |
| 232 | .beginnode = asm_emit_beginnode, |
| 233 | .endnode = asm_emit_endnode, |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 234 | .property = asm_emit_property, |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 235 | }; |
| 236 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 237 | static int stringtable_insert(struct data *d, const char *str) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 238 | { |
| 239 | int i; |
| 240 | |
| 241 | /* FIXME: do this more efficiently? */ |
| 242 | |
| 243 | for (i = 0; i < d->len; i++) { |
| 244 | if (streq(str, d->val + i)) |
| 245 | return i; |
| 246 | } |
| 247 | |
| 248 | *d = data_append_data(*d, str, strlen(str)+1); |
David Gibson | a6c6957 | 2005-07-11 17:09:42 +1000 | [diff] [blame] | 249 | return i; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void flatten_tree(struct node *tree, struct emitter *emit, |
| 253 | void *etarget, struct data *strbuf, |
| 254 | struct version_info *vi) |
| 255 | { |
| 256 | struct property *prop; |
| 257 | struct node *child; |
| 258 | int seen_name_prop = 0; |
| 259 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 260 | emit->beginnode(etarget, tree->label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 261 | |
| 262 | if (vi->flags & FTF_FULLPATH) |
| 263 | emit->string(etarget, tree->fullpath, 0); |
| 264 | else |
| 265 | emit->string(etarget, tree->name, 0); |
| 266 | |
| 267 | emit->align(etarget, sizeof(cell_t)); |
| 268 | |
| 269 | for_each_property(tree, prop) { |
| 270 | int nameoff; |
| 271 | |
| 272 | if (streq(prop->name, "name")) |
| 273 | seen_name_prop = 1; |
| 274 | |
| 275 | nameoff = stringtable_insert(strbuf, prop->name); |
| 276 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 277 | emit->property(etarget, prop->label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 278 | emit->cell(etarget, prop->val.len); |
| 279 | emit->cell(etarget, nameoff); |
| 280 | |
| 281 | if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8)) |
| 282 | emit->align(etarget, 8); |
| 283 | |
| 284 | emit->data(etarget, prop->val); |
| 285 | emit->align(etarget, sizeof(cell_t)); |
| 286 | } |
| 287 | |
| 288 | if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) { |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 289 | emit->property(etarget, NULL); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 290 | emit->cell(etarget, tree->basenamelen+1); |
| 291 | emit->cell(etarget, stringtable_insert(strbuf, "name")); |
| 292 | |
| 293 | if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8)) |
| 294 | emit->align(etarget, 8); |
| 295 | |
| 296 | emit->string(etarget, tree->name, tree->basenamelen); |
David Gibson | 4191613 | 2005-08-25 15:39:09 +1000 | [diff] [blame] | 297 | emit->align(etarget, sizeof(cell_t)); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | for_each_child(tree, child) { |
| 301 | flatten_tree(child, emit, etarget, strbuf, vi); |
| 302 | } |
| 303 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 304 | emit->endnode(etarget, tree->label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 305 | } |
| 306 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 307 | static struct data flatten_reserve_list(struct reserve_info *reservelist, |
| 308 | struct version_info *vi) |
| 309 | { |
| 310 | struct reserve_info *re; |
| 311 | struct data d = empty_data; |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 312 | static struct fdt_reserve_entry null_re = {0,0}; |
Jerry Van Baren | 4384b23 | 2007-04-04 22:04:33 -0400 | [diff] [blame] | 313 | int j; |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 314 | |
| 315 | for (re = reservelist; re; re = re->next) { |
| 316 | d = data_append_re(d, &re->re); |
| 317 | } |
Jerry Van Baren | 4384b23 | 2007-04-04 22:04:33 -0400 | [diff] [blame] | 318 | /* |
| 319 | * Add additional reserved slots if the user asked for them. |
| 320 | */ |
| 321 | for (j = 0; j < reservenum; j++) { |
| 322 | d = data_append_re(d, &null_re); |
| 323 | } |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 324 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 325 | return d; |
| 326 | } |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 327 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 328 | static void make_fdt_header(struct fdt_header *fdt, |
| 329 | struct version_info *vi, |
| 330 | int reservesize, int dtsize, int strsize, |
| 331 | int boot_cpuid_phys) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 332 | { |
David Gibson | 47f23de | 2005-07-11 17:19:26 +1000 | [diff] [blame] | 333 | int reserve_off; |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 334 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 335 | reservesize += sizeof(struct fdt_reserve_entry); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 336 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 337 | memset(fdt, 0xff, sizeof(*fdt)); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 338 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 339 | fdt->magic = cpu_to_be32(FDT_MAGIC); |
| 340 | fdt->version = cpu_to_be32(vi->version); |
| 341 | fdt->last_comp_version = cpu_to_be32(vi->last_comp_version); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 342 | |
David Gibson | 47f23de | 2005-07-11 17:19:26 +1000 | [diff] [blame] | 343 | /* Reserve map should be doubleword aligned */ |
| 344 | reserve_off = ALIGN(vi->hdr_size, 8); |
| 345 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 346 | fdt->off_mem_rsvmap = cpu_to_be32(reserve_off); |
| 347 | fdt->off_dt_struct = cpu_to_be32(reserve_off + reservesize); |
| 348 | fdt->off_dt_strings = cpu_to_be32(reserve_off + reservesize |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 349 | + dtsize); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 350 | fdt->totalsize = cpu_to_be32(reserve_off + reservesize + dtsize + strsize); |
Jerry Van Baren | 4384b23 | 2007-04-04 22:04:33 -0400 | [diff] [blame] | 351 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 352 | if (vi->flags & FTF_BOOTCPUID) |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 353 | fdt->boot_cpuid_phys = cpu_to_be32(boot_cpuid_phys); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 354 | if (vi->flags & FTF_STRTABSIZE) |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 355 | fdt->size_dt_strings = cpu_to_be32(strsize); |
David Gibson | 46c88df | 2007-03-14 11:02:40 +1100 | [diff] [blame] | 356 | if (vi->flags & FTF_STRUCTSIZE) |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 357 | fdt->size_dt_struct = cpu_to_be32(dtsize); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 358 | } |
| 359 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 360 | void dt_to_blob(FILE *f, struct boot_info *bi, int version, |
| 361 | int boot_cpuid_phys) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 362 | { |
| 363 | struct version_info *vi = NULL; |
| 364 | int i; |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 365 | struct data blob = empty_data; |
| 366 | struct data reservebuf = empty_data; |
| 367 | struct data dtbuf = empty_data; |
| 368 | struct data strbuf = empty_data; |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 369 | struct fdt_header fdt; |
Kumar Gala | 2b7dc8d | 2007-11-28 10:21:12 -0600 | [diff] [blame] | 370 | int padlen = 0; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 371 | |
| 372 | for (i = 0; i < ARRAY_SIZE(version_table); i++) { |
| 373 | if (version_table[i].version == version) |
| 374 | vi = &version_table[i]; |
| 375 | } |
| 376 | if (!vi) |
| 377 | die("Unknown device tree blob version %d\n", version); |
| 378 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 379 | flatten_tree(bi->dt, &bin_emitter, &dtbuf, &strbuf, vi); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 380 | bin_emit_cell(&dtbuf, FDT_END); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 381 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 382 | reservebuf = flatten_reserve_list(bi->reservelist, vi); |
| 383 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 384 | /* Make header */ |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 385 | make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len, |
| 386 | boot_cpuid_phys); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 387 | |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 388 | /* |
Jerry Van Baren | 7ea144f | 2007-04-19 22:22:35 -0400 | [diff] [blame] | 389 | * If the user asked for more space than is used, adjust the totalsize. |
| 390 | */ |
Kumar Gala | 2b7dc8d | 2007-11-28 10:21:12 -0600 | [diff] [blame] | 391 | if (minsize > 0) { |
| 392 | padlen = minsize - be32_to_cpu(fdt.totalsize); |
| 393 | if ((padlen < 0) && (quiet < 1)) |
Jerry Van Baren | 7ea144f | 2007-04-19 22:22:35 -0400 | [diff] [blame] | 394 | fprintf(stderr, |
| 395 | "Warning: blob size %d >= minimum size %d\n", |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 396 | be32_to_cpu(fdt.totalsize), minsize); |
Jerry Van Baren | 7ea144f | 2007-04-19 22:22:35 -0400 | [diff] [blame] | 397 | } |
| 398 | |
Kumar Gala | 2b7dc8d | 2007-11-28 10:21:12 -0600 | [diff] [blame] | 399 | if (padsize > 0) |
| 400 | padlen = padsize; |
| 401 | |
Kumar Gala | 80c72a8 | 2007-12-04 17:36:08 -0600 | [diff] [blame] | 402 | if (padlen > 0) { |
| 403 | int tsize = be32_to_cpu(fdt.totalsize); |
| 404 | tsize += padlen; |
| 405 | fdt.totalsize = cpu_to_be32(tsize); |
| 406 | } |
| 407 | |
Jerry Van Baren | 7ea144f | 2007-04-19 22:22:35 -0400 | [diff] [blame] | 408 | /* |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 409 | * Assemble the blob: start with the header, add with alignment |
| 410 | * the reserve buffer, add the reserve map terminating zeroes, |
| 411 | * the device tree itself, and finally the strings. |
| 412 | */ |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 413 | blob = data_append_data(blob, &fdt, sizeof(fdt)); |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 414 | blob = data_append_align(blob, 8); |
| 415 | blob = data_merge(blob, reservebuf); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 416 | blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry)); |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 417 | blob = data_merge(blob, dtbuf); |
| 418 | blob = data_merge(blob, strbuf); |
David Gibson | 47f23de | 2005-07-11 17:19:26 +1000 | [diff] [blame] | 419 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 420 | /* |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 421 | * If the user asked for more space than is used, pad out the blob. |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 422 | */ |
Kumar Gala | 80c72a8 | 2007-12-04 17:36:08 -0600 | [diff] [blame] | 423 | if (padlen > 0) |
Jerry Van Baren | 7ea144f | 2007-04-19 22:22:35 -0400 | [diff] [blame] | 424 | blob = data_append_zeroes(blob, padlen); |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 425 | |
| 426 | fwrite(blob.val, blob.len, 1, f); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 427 | |
| 428 | if (ferror(f)) |
| 429 | die("Error writing device tree blob: %s\n", strerror(errno)); |
| 430 | |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 431 | /* |
| 432 | * data_merge() frees the right-hand element so only the blob |
| 433 | * remains to be freed. |
| 434 | */ |
| 435 | data_free(blob); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 436 | } |
| 437 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 438 | static void dump_stringtable_asm(FILE *f, struct data strbuf) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 439 | { |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 440 | const char *p; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 441 | int len; |
| 442 | |
| 443 | p = strbuf.val; |
| 444 | |
| 445 | while (p < (strbuf.val + strbuf.len)) { |
| 446 | len = strlen(p); |
| 447 | fprintf(f, "\t.string \"%s\"\n", p); |
| 448 | p += len+1; |
| 449 | } |
| 450 | } |
| 451 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 452 | void dt_to_asm(FILE *f, struct boot_info *bi, int version, int boot_cpuid_phys) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 453 | { |
| 454 | struct version_info *vi = NULL; |
| 455 | int i; |
| 456 | struct data strbuf = empty_data; |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 457 | struct reserve_info *re; |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 458 | const char *symprefix = "dt"; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 459 | |
| 460 | for (i = 0; i < ARRAY_SIZE(version_table); i++) { |
| 461 | if (version_table[i].version == version) |
| 462 | vi = &version_table[i]; |
| 463 | } |
| 464 | if (!vi) |
| 465 | die("Unknown device tree blob version %d\n", version); |
| 466 | |
| 467 | fprintf(f, "/* autogenerated by dtc, do not edit */\n\n"); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 468 | fprintf(f, "#define FDT_MAGIC 0x%x\n", FDT_MAGIC); |
| 469 | fprintf(f, "#define FDT_BEGIN_NODE 0x%x\n", FDT_BEGIN_NODE); |
| 470 | fprintf(f, "#define FDT_END_NODE 0x%x\n", FDT_END_NODE); |
| 471 | fprintf(f, "#define FDT_PROP 0x%x\n", FDT_PROP); |
| 472 | fprintf(f, "#define FDT_END 0x%x\n", FDT_END); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 473 | fprintf(f, "\n"); |
| 474 | |
| 475 | emit_label(f, symprefix, "blob_start"); |
| 476 | emit_label(f, symprefix, "header"); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 477 | fprintf(f, "\t.long\tFDT_MAGIC\t\t\t\t/* magic */\n"); |
Milton Miller | 7f45666 | 2007-07-07 01:18:53 -0500 | [diff] [blame] | 478 | fprintf(f, "\t.long\t_%s_blob_abs_end - _%s_blob_start\t/* totalsize */\n", |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 479 | symprefix, symprefix); |
Milton Miller | 7f45666 | 2007-07-07 01:18:53 -0500 | [diff] [blame] | 480 | fprintf(f, "\t.long\t_%s_struct_start - _%s_blob_start\t/* off_dt_struct */\n", |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 481 | symprefix, symprefix); |
Milton Miller | 7f45666 | 2007-07-07 01:18:53 -0500 | [diff] [blame] | 482 | fprintf(f, "\t.long\t_%s_strings_start - _%s_blob_start\t/* off_dt_strings */\n", |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 483 | symprefix, symprefix); |
Milton Miller | 7f45666 | 2007-07-07 01:18:53 -0500 | [diff] [blame] | 484 | fprintf(f, "\t.long\t_%s_reserve_map - _%s_blob_start\t/* off_dt_strings */\n", |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 485 | symprefix, symprefix); |
Milton Miller | 7f45666 | 2007-07-07 01:18:53 -0500 | [diff] [blame] | 486 | fprintf(f, "\t.long\t%d\t\t\t\t\t/* version */\n", vi->version); |
| 487 | fprintf(f, "\t.long\t%d\t\t\t\t\t/* last_comp_version */\n", |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 488 | vi->last_comp_version); |
| 489 | |
| 490 | if (vi->flags & FTF_BOOTCPUID) |
Milton Miller | 7f45666 | 2007-07-07 01:18:53 -0500 | [diff] [blame] | 491 | fprintf(f, "\t.long\t%i\t\t\t\t\t/* boot_cpuid_phys */\n", |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 492 | boot_cpuid_phys); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 493 | |
| 494 | if (vi->flags & FTF_STRTABSIZE) |
| 495 | fprintf(f, "\t.long\t_%s_strings_end - _%s_strings_start\t/* size_dt_strings */\n", |
| 496 | symprefix, symprefix); |
| 497 | |
Milton Miller | 81fda8a | 2007-07-07 01:18:47 -0500 | [diff] [blame] | 498 | if (vi->flags & FTF_STRUCTSIZE) |
| 499 | fprintf(f, "\t.long\t_%s_struct_end - _%s_struct_start\t/* size_dt_struct */\n", |
| 500 | symprefix, symprefix); |
| 501 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 502 | /* |
| 503 | * Reserve map entries. |
| 504 | * Align the reserve map to a doubleword boundary. |
| 505 | * Each entry is an (address, size) pair of u64 values. |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 506 | * Always supply a zero-sized temination entry. |
| 507 | */ |
David Gibson | 47f23de | 2005-07-11 17:19:26 +1000 | [diff] [blame] | 508 | asm_emit_align(f, 8); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 509 | emit_label(f, symprefix, "reserve_map"); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 510 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 511 | fprintf(f, "/* Memory reserve map from source file */\n"); |
Jon Loeliger | 05ae3d8 | 2006-04-19 11:58:45 -0500 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * Use .long on high and low halfs of u64s to avoid .quad |
| 515 | * as it appears .quad isn't available in some assemblers. |
| 516 | */ |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 517 | for (re = bi->reservelist; re; re = re->next) { |
Milton Miller | d429033 | 2007-07-07 01:18:49 -0500 | [diff] [blame] | 518 | if (re->label) { |
| 519 | fprintf(f, "\t.globl\t%s\n", re->label); |
| 520 | fprintf(f, "%s:\n", re->label); |
| 521 | } |
Milton Miller | 445d55d | 2007-07-07 01:18:54 -0500 | [diff] [blame] | 522 | fprintf(f, "\t.long\t0x%08x, 0x%08x\n", |
Jon Loeliger | 05ae3d8 | 2006-04-19 11:58:45 -0500 | [diff] [blame] | 523 | (unsigned int)(re->re.address >> 32), |
| 524 | (unsigned int)(re->re.address & 0xffffffff)); |
Milton Miller | 445d55d | 2007-07-07 01:18:54 -0500 | [diff] [blame] | 525 | fprintf(f, "\t.long\t0x%08x, 0x%08x\n", |
Jon Loeliger | 05ae3d8 | 2006-04-19 11:58:45 -0500 | [diff] [blame] | 526 | (unsigned int)(re->re.size >> 32), |
| 527 | (unsigned int)(re->re.size & 0xffffffff)); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 528 | } |
Jerry Van Baren | ca25e54 | 2007-04-17 18:14:41 -0400 | [diff] [blame] | 529 | for (i = 0; i < reservenum; i++) { |
| 530 | fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n"); |
| 531 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 532 | |
Jon Loeliger | 05ae3d8 | 2006-04-19 11:58:45 -0500 | [diff] [blame] | 533 | fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n"); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 534 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 535 | emit_label(f, symprefix, "struct_start"); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 536 | flatten_tree(bi->dt, &asm_emitter, f, &strbuf, vi); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 537 | fprintf(f, "\t.long\tFDT_END\n"); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 538 | emit_label(f, symprefix, "struct_end"); |
| 539 | |
| 540 | emit_label(f, symprefix, "strings_start"); |
| 541 | dump_stringtable_asm(f, strbuf); |
| 542 | emit_label(f, symprefix, "strings_end"); |
| 543 | |
| 544 | emit_label(f, symprefix, "blob_end"); |
| 545 | |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 546 | /* |
| 547 | * If the user asked for more space than is used, pad it out. |
| 548 | */ |
| 549 | if (minsize > 0) { |
| 550 | fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n", |
| 551 | minsize, symprefix, symprefix); |
| 552 | } |
Kumar Gala | 2b7dc8d | 2007-11-28 10:21:12 -0600 | [diff] [blame] | 553 | if (padsize > 0) { |
| 554 | fprintf(f, "\t.space\t%d, 0\n", padsize); |
| 555 | } |
Jerry Van Baren | 86c01ee | 2007-04-18 21:59:51 -0400 | [diff] [blame] | 556 | emit_label(f, symprefix, "blob_abs_end"); |
| 557 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 558 | data_free(strbuf); |
| 559 | } |
| 560 | |
| 561 | struct inbuf { |
| 562 | char *base, *limit, *ptr; |
| 563 | }; |
| 564 | |
| 565 | static void inbuf_init(struct inbuf *inb, void *base, void *limit) |
| 566 | { |
| 567 | inb->base = base; |
| 568 | inb->limit = limit; |
| 569 | inb->ptr = inb->base; |
| 570 | } |
| 571 | |
| 572 | static void flat_read_chunk(struct inbuf *inb, void *p, int len) |
| 573 | { |
| 574 | if ((inb->ptr + len) > inb->limit) |
| 575 | die("Premature end of data parsing flat device tree\n"); |
| 576 | |
| 577 | memcpy(p, inb->ptr, len); |
| 578 | |
| 579 | inb->ptr += len; |
| 580 | } |
| 581 | |
| 582 | static u32 flat_read_word(struct inbuf *inb) |
| 583 | { |
| 584 | u32 val; |
| 585 | |
| 586 | assert(((inb->ptr - inb->base) % sizeof(val)) == 0); |
| 587 | |
| 588 | flat_read_chunk(inb, &val, sizeof(val)); |
| 589 | |
| 590 | return be32_to_cpu(val); |
| 591 | } |
| 592 | |
| 593 | static void flat_realign(struct inbuf *inb, int align) |
| 594 | { |
| 595 | int off = inb->ptr - inb->base; |
| 596 | |
| 597 | inb->ptr = inb->base + ALIGN(off, align); |
| 598 | if (inb->ptr > inb->limit) |
| 599 | die("Premature end of data parsing flat device tree\n"); |
| 600 | } |
| 601 | |
| 602 | static char *flat_read_string(struct inbuf *inb) |
| 603 | { |
| 604 | int len = 0; |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 605 | const char *p = inb->ptr; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 606 | char *str; |
| 607 | |
| 608 | do { |
| 609 | if (p >= inb->limit) |
| 610 | die("Premature end of data parsing flat device tree\n"); |
| 611 | len++; |
| 612 | } while ((*p++) != '\0'); |
| 613 | |
| 614 | str = strdup(inb->ptr); |
| 615 | |
| 616 | inb->ptr += len; |
| 617 | |
| 618 | flat_realign(inb, sizeof(u32)); |
| 619 | |
| 620 | return str; |
| 621 | } |
| 622 | |
| 623 | static struct data flat_read_data(struct inbuf *inb, int len) |
| 624 | { |
| 625 | struct data d = empty_data; |
| 626 | |
| 627 | if (len == 0) |
| 628 | return empty_data; |
| 629 | |
| 630 | d = data_grow_for(d, len); |
| 631 | d.len = len; |
| 632 | |
| 633 | flat_read_chunk(inb, d.val, len); |
| 634 | |
| 635 | flat_realign(inb, sizeof(u32)); |
| 636 | |
| 637 | return d; |
| 638 | } |
| 639 | |
| 640 | static char *flat_read_stringtable(struct inbuf *inb, int offset) |
| 641 | { |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 642 | const char *p; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 643 | |
| 644 | p = inb->base + offset; |
| 645 | while (1) { |
David Gibson | 4ddf7c0 | 2005-08-19 16:11:11 +1000 | [diff] [blame] | 646 | if (p >= inb->limit || p < inb->base) |
David Gibson | 7ee3ffd | 2005-07-11 16:45:57 +1000 | [diff] [blame] | 647 | die("String offset %d overruns string table\n", |
| 648 | offset); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 649 | |
| 650 | if (*p == '\0') |
| 651 | break; |
| 652 | |
| 653 | p++; |
| 654 | } |
| 655 | |
| 656 | return strdup(inb->base + offset); |
| 657 | } |
| 658 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 659 | static struct property *flat_read_property(struct inbuf *dtbuf, |
| 660 | struct inbuf *strbuf, int flags) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 661 | { |
| 662 | u32 proplen, stroff; |
| 663 | char *name; |
| 664 | struct data val; |
| 665 | |
| 666 | proplen = flat_read_word(dtbuf); |
| 667 | stroff = flat_read_word(dtbuf); |
| 668 | |
| 669 | name = flat_read_stringtable(strbuf, stroff); |
| 670 | |
| 671 | if ((flags & FTF_VARALIGN) && (proplen >= 8)) |
| 672 | flat_realign(dtbuf, 8); |
| 673 | |
| 674 | val = flat_read_data(dtbuf, proplen); |
| 675 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 676 | return build_property(name, val, NULL); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 677 | } |
| 678 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 679 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 680 | static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb) |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 681 | { |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 682 | struct reserve_info *reservelist = NULL; |
| 683 | struct reserve_info *new; |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 684 | const char *p; |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 685 | struct fdt_reserve_entry re; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 686 | |
| 687 | /* |
| 688 | * Each entry is a pair of u64 (addr, size) values for 4 cell_t's. |
| 689 | * List terminates at an entry with size equal to zero. |
| 690 | * |
| 691 | * First pass, count entries. |
| 692 | */ |
| 693 | p = inb->ptr; |
David Gibson | 6c0f367 | 2005-08-29 13:36:15 +1000 | [diff] [blame] | 694 | while (1) { |
| 695 | flat_read_chunk(inb, &re, sizeof(re)); |
Michael Neuling | 332c536 | 2006-07-07 23:28:10 +1000 | [diff] [blame] | 696 | re.address = be64_to_cpu(re.address); |
| 697 | re.size = be64_to_cpu(re.size); |
David Gibson | 6c0f367 | 2005-08-29 13:36:15 +1000 | [diff] [blame] | 698 | if (re.size == 0) |
| 699 | break; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 700 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 701 | new = build_reserve_entry(re.address, re.size, NULL); |
| 702 | reservelist = add_reserve_entry(reservelist, new); |
David Gibson | 6c0f367 | 2005-08-29 13:36:15 +1000 | [diff] [blame] | 703 | } |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 704 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 705 | return reservelist; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 709 | static char *nodename_from_path(const char *ppath, const char *cpath) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 710 | { |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 711 | const char *lslash; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 712 | int plen; |
| 713 | |
| 714 | lslash = strrchr(cpath, '/'); |
| 715 | if (! lslash) |
| 716 | return NULL; |
| 717 | |
| 718 | plen = lslash - cpath; |
| 719 | |
| 720 | if (streq(cpath, "/") && streq(ppath, "")) |
| 721 | return ""; |
| 722 | |
| 723 | if ((plen == 0) && streq(ppath, "/")) |
| 724 | return strdup(lslash+1); |
| 725 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 726 | if (! strneq(ppath, cpath, plen)) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 727 | return NULL; |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 728 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 729 | return strdup(lslash+1); |
| 730 | } |
| 731 | |
David Gibson | fa5b520 | 2008-02-27 13:45:13 +1100 | [diff] [blame] | 732 | static int find_basenamelen(const char *name) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 733 | { |
David Gibson | fa5b520 | 2008-02-27 13:45:13 +1100 | [diff] [blame] | 734 | const char *atpos = strchr(name, '@'); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 735 | |
| 736 | if (atpos) |
David Gibson | fa5b520 | 2008-02-27 13:45:13 +1100 | [diff] [blame] | 737 | return atpos - name; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 738 | else |
David Gibson | fa5b520 | 2008-02-27 13:45:13 +1100 | [diff] [blame] | 739 | return strlen(name); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static struct node *unflatten_tree(struct inbuf *dtbuf, |
| 743 | struct inbuf *strbuf, |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 744 | const char *parent_path, int flags) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 745 | { |
| 746 | struct node *node; |
| 747 | u32 val; |
| 748 | |
| 749 | node = build_node(NULL, NULL); |
| 750 | |
| 751 | if (flags & FTF_FULLPATH) { |
| 752 | node->fullpath = flat_read_string(dtbuf); |
| 753 | node->name = nodename_from_path(parent_path, node->fullpath); |
| 754 | |
| 755 | if (! node->name) |
| 756 | die("Path \"%s\" is not valid as a child of \"%s\"\n", |
| 757 | node->fullpath, parent_path); |
| 758 | } else { |
| 759 | node->name = flat_read_string(dtbuf); |
| 760 | node->fullpath = join_path(parent_path, node->name); |
| 761 | } |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 762 | |
David Gibson | fa5b520 | 2008-02-27 13:45:13 +1100 | [diff] [blame] | 763 | node->basenamelen = find_basenamelen(node->name); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 764 | |
| 765 | do { |
| 766 | struct property *prop; |
| 767 | struct node *child; |
| 768 | |
| 769 | val = flat_read_word(dtbuf); |
| 770 | switch (val) { |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 771 | case FDT_PROP: |
David Gibson | 592ea58 | 2007-09-04 10:43:03 +1000 | [diff] [blame] | 772 | if (node->children) |
| 773 | fprintf(stderr, "Warning: Flat tree input has " |
| 774 | "subnodes preceding a property.\n"); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 775 | prop = flat_read_property(dtbuf, strbuf, flags); |
| 776 | add_property(node, prop); |
| 777 | break; |
| 778 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 779 | case FDT_BEGIN_NODE: |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 780 | child = unflatten_tree(dtbuf,strbuf, node->fullpath, |
| 781 | flags); |
| 782 | add_child(node, child); |
| 783 | break; |
| 784 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 785 | case FDT_END_NODE: |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 786 | break; |
| 787 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 788 | case FDT_END: |
| 789 | die("Premature FDT_END in device tree blob\n"); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 790 | break; |
| 791 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 792 | case FDT_NOP: |
David Gibson | 0738774 | 2007-06-26 11:30:47 +1000 | [diff] [blame] | 793 | if (!(flags & FTF_NOPS)) |
| 794 | fprintf(stderr, "Warning: NOP tag found in flat tree" |
| 795 | " version <16\n"); |
Milton Miller | ce24322 | 2007-06-09 23:21:31 -0500 | [diff] [blame] | 796 | |
David Gibson | 0738774 | 2007-06-26 11:30:47 +1000 | [diff] [blame] | 797 | /* Ignore */ |
Milton Miller | ce24322 | 2007-06-09 23:21:31 -0500 | [diff] [blame] | 798 | break; |
| 799 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 800 | default: |
| 801 | die("Invalid opcode word %08x in device tree blob\n", |
| 802 | val); |
| 803 | } |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 804 | } while (val != FDT_END_NODE); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 805 | |
| 806 | return node; |
| 807 | } |
| 808 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 809 | |
| 810 | struct boot_info *dt_from_blob(FILE *f) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 811 | { |
David Gibson | 46c88df | 2007-03-14 11:02:40 +1100 | [diff] [blame] | 812 | u32 magic, totalsize, version, size_str, size_dt; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 813 | u32 off_dt, off_str, off_mem_rsvmap; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 814 | int rc; |
| 815 | char *blob; |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 816 | struct fdt_header *fdt; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 817 | char *p; |
| 818 | struct inbuf dtbuf, strbuf; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 819 | struct inbuf memresvbuf; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 820 | int sizeleft; |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 821 | struct reserve_info *reservelist; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 822 | struct node *tree; |
| 823 | u32 val; |
| 824 | int flags = 0; |
| 825 | |
| 826 | rc = fread(&magic, sizeof(magic), 1, f); |
| 827 | if (ferror(f)) |
| 828 | die("Error reading DT blob magic number: %s\n", |
| 829 | strerror(errno)); |
| 830 | if (rc < 1) { |
| 831 | if (feof(f)) |
| 832 | die("EOF reading DT blob magic number\n"); |
| 833 | else |
| 834 | die("Mysterious short read reading magic number\n"); |
| 835 | } |
| 836 | |
| 837 | magic = be32_to_cpu(magic); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 838 | if (magic != FDT_MAGIC) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 839 | die("Blob has incorrect magic number\n"); |
| 840 | |
| 841 | rc = fread(&totalsize, sizeof(totalsize), 1, f); |
| 842 | if (ferror(f)) |
| 843 | die("Error reading DT blob size: %s\n", strerror(errno)); |
| 844 | if (rc < 1) { |
| 845 | if (feof(f)) |
| 846 | die("EOF reading DT blob size\n"); |
| 847 | else |
| 848 | die("Mysterious short read reading blob size\n"); |
| 849 | } |
| 850 | |
| 851 | totalsize = be32_to_cpu(totalsize); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 852 | if (totalsize < FDT_V1_SIZE) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 853 | die("DT blob size (%d) is too small\n", totalsize); |
| 854 | |
| 855 | blob = xmalloc(totalsize); |
| 856 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 857 | fdt = (struct fdt_header *)blob; |
| 858 | fdt->magic = cpu_to_be32(magic); |
| 859 | fdt->totalsize = cpu_to_be32(totalsize); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 860 | |
| 861 | sizeleft = totalsize - sizeof(magic) - sizeof(totalsize); |
| 862 | p = blob + sizeof(magic) + sizeof(totalsize); |
| 863 | |
| 864 | while (sizeleft) { |
| 865 | if (feof(f)) |
| 866 | die("EOF before reading %d bytes of DT blob\n", |
| 867 | totalsize); |
| 868 | |
| 869 | rc = fread(p, 1, sizeleft, f); |
| 870 | if (ferror(f)) |
| 871 | die("Error reading DT blob: %s\n", |
| 872 | strerror(errno)); |
| 873 | |
| 874 | sizeleft -= rc; |
| 875 | p += rc; |
| 876 | } |
| 877 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 878 | off_dt = be32_to_cpu(fdt->off_dt_struct); |
| 879 | off_str = be32_to_cpu(fdt->off_dt_strings); |
| 880 | off_mem_rsvmap = be32_to_cpu(fdt->off_mem_rsvmap); |
| 881 | version = be32_to_cpu(fdt->version); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 882 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 883 | if (off_mem_rsvmap >= totalsize) |
| 884 | die("Mem Reserve structure offset exceeds total size\n"); |
| 885 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 886 | if (off_dt >= totalsize) |
| 887 | die("DT structure offset exceeds total size\n"); |
| 888 | |
| 889 | if (off_str > totalsize) |
| 890 | die("String table offset exceeds total size\n"); |
| 891 | |
David Gibson | bf94497 | 2007-08-31 16:21:23 +1000 | [diff] [blame] | 892 | size_str = -1; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 893 | if (version >= 3) { |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 894 | size_str = be32_to_cpu(fdt->size_dt_strings); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 895 | if (off_str+size_str > totalsize) |
| 896 | die("String table extends past total size\n"); |
| 897 | } |
David Gibson | 46c88df | 2007-03-14 11:02:40 +1100 | [diff] [blame] | 898 | |
| 899 | if (version >= 17) { |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 900 | size_dt = be32_to_cpu(fdt->size_dt_struct); |
David Gibson | 46c88df | 2007-03-14 11:02:40 +1100 | [diff] [blame] | 901 | if (off_dt+size_dt > totalsize) |
| 902 | die("Structure block extends past total size\n"); |
| 903 | } |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 904 | |
David Gibson | 46c88df | 2007-03-14 11:02:40 +1100 | [diff] [blame] | 905 | if (version < 16) { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 906 | flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN; |
Milton Miller | ce24322 | 2007-06-09 23:21:31 -0500 | [diff] [blame] | 907 | } else { |
| 908 | flags |= FTF_NOPS; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 909 | } |
| 910 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 911 | inbuf_init(&memresvbuf, |
| 912 | blob + off_mem_rsvmap, blob + totalsize); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 913 | inbuf_init(&dtbuf, blob + off_dt, blob + totalsize); |
David Gibson | bf94497 | 2007-08-31 16:21:23 +1000 | [diff] [blame] | 914 | if (size_str >= 0) |
| 915 | inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str); |
| 916 | else |
| 917 | inbuf_init(&strbuf, blob + off_str, blob + totalsize); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 918 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 919 | reservelist = flat_read_mem_reserve(&memresvbuf); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 920 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 921 | val = flat_read_word(&dtbuf); |
| 922 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 923 | if (val != FDT_BEGIN_NODE) |
| 924 | die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 925 | |
| 926 | tree = unflatten_tree(&dtbuf, &strbuf, "", flags); |
| 927 | |
| 928 | val = flat_read_word(&dtbuf); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 929 | if (val != FDT_END) |
| 930 | die("Device tree blob doesn't end with FDT_END\n"); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 931 | |
| 932 | free(blob); |
| 933 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 934 | return build_boot_info(reservelist, tree); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 935 | } |