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