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