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