blob: 62b8903840e2cf70cdda5d21fc858caaf64caf2b [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
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 Gibsonab870ca2005-06-23 15:45:13 +100022#include "flat_dt.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100023
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 Gibson230f2532005-08-29 12:48:02 +100030static struct version_info {
David Gibsonfc14dad2005-06-08 17:18:34 +100031 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
46struct 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 Gibson4102d842005-06-16 14:36:37 +100051 void (*beginnode)(void *, char *);
52 void (*endnode)(void *, char *);
53 void (*property)(void *, char *);
David Gibsonfc14dad2005-06-08 17:18:34 +100054};
55
56static 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
63static 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
74static void bin_emit_align(void *e, int a)
75{
76 struct data *dtbuf = e;
77
78 *dtbuf = data_append_align(*dtbuf, a);
79}
80
81static 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 Gibson4102d842005-06-16 14:36:37 +100088static void bin_emit_beginnode(void *e, char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +100089{
90 bin_emit_cell(e, OF_DT_BEGIN_NODE);
91}
92
David Gibson4102d842005-06-16 14:36:37 +100093static void bin_emit_endnode(void *e, char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +100094{
95 bin_emit_cell(e, OF_DT_END_NODE);
96}
97
David Gibson4102d842005-06-16 14:36:37 +100098static void bin_emit_property(void *e, char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +100099{
100 bin_emit_cell(e, OF_DT_PROP);
101}
102
David Gibson230f2532005-08-29 12:48:02 +1000103static struct emitter bin_emitter = {
David Gibsonfc14dad2005-06-08 17:18:34 +1000104 .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 Gibson230f2532005-08-29 12:48:02 +1000113static void emit_label(FILE *f, char *prefix, char *label)
David Gibson4102d842005-06-16 14:36:37 +1000114{
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 Gibsonfc14dad2005-06-08 17:18:34 +1000120static void asm_emit_cell(void *e, cell_t val)
121{
122 FILE *f = e;
123
124 fprintf(f, "\t.long\t0x%x\n", be32_to_cpu(val));
125}
126
127static 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
145static 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
152static 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 Gibson4102d842005-06-16 14:36:37 +1000177static void asm_emit_beginnode(void *e, char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +1000178{
179 FILE *f = e;
180
David Gibson4102d842005-06-16 14:36:37 +1000181 if (label) {
182 fprintf(f, "\t.globl\t%s\n", label);
183 fprintf(f, "%s:\n", label);
184 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000185 fprintf(f, "\t.long\tOF_DT_BEGIN_NODE\n");
186}
187
David Gibson4102d842005-06-16 14:36:37 +1000188static void asm_emit_endnode(void *e, char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +1000189{
190 FILE *f = e;
191
192 fprintf(f, "\t.long\tOF_DT_END_NODE\n");
David Gibson4102d842005-06-16 14:36:37 +1000193 if (label) {
194 fprintf(f, "\t.globl\t%s_end\n", label);
195 fprintf(f, "%s_end:\n", label);
196 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000197}
198
David Gibson4102d842005-06-16 14:36:37 +1000199static void asm_emit_property(void *e, char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +1000200{
201 FILE *f = e;
202
David Gibson4102d842005-06-16 14:36:37 +1000203 if (label) {
204 fprintf(f, "\t.globl\t%s\n", label);
205 fprintf(f, "%s:\n", label);
206 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000207 fprintf(f, "\t.long\tOF_DT_PROP\n");
208}
209
David Gibson230f2532005-08-29 12:48:02 +1000210static struct emitter asm_emitter = {
David Gibsonfc14dad2005-06-08 17:18:34 +1000211 .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
220static 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 Gibsona6c69572005-07-11 17:09:42 +1000232 return i;
David Gibsonfc14dad2005-06-08 17:18:34 +1000233}
234
235static 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 Gibson4102d842005-06-16 14:36:37 +1000243 emit->beginnode(etarget, tree->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000244
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 Gibson4102d842005-06-16 14:36:37 +1000260 emit->property(etarget, prop->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000261 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 Gibson4102d842005-06-16 14:36:37 +1000272 emit->property(etarget, NULL);
David Gibsonfc14dad2005-06-08 17:18:34 +1000273 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 Gibson41916132005-08-25 15:39:09 +1000280 emit->align(etarget, sizeof(cell_t));
David Gibsonfc14dad2005-06-08 17:18:34 +1000281 }
282
283 for_each_child(tree, child) {
284 flatten_tree(child, emit, etarget, strbuf, vi);
285 }
286
David Gibson4102d842005-06-16 14:36:37 +1000287 emit->endnode(etarget, tree->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000288}
289
290static void make_bph(struct boot_param_header *bph,
David Gibsonf0517db2005-07-15 17:14:24 +1000291 struct version_info *vi,
292 struct data *mem_reserve_data,
293 int dtsize, int strsize)
David Gibsonfc14dad2005-06-08 17:18:34 +1000294{
David Gibson47f23de2005-07-11 17:19:26 +1000295 int reserve_off;
David Gibsonf0517db2005-07-15 17:14:24 +1000296 int reservenum = mem_reserve_data->len / sizeof(struct reserve_entry);
David Gibsonfc14dad2005-06-08 17:18:34 +1000297 int reservesize = (reservenum+1) * sizeof(struct reserve_entry);
298
299 memset(bph, 0xff, sizeof(*bph));
300
301 bph->magic = cpu_to_be32(OF_DT_HEADER);
David Gibsondffc2a82005-08-25 14:47:20 +1000302 bph->version = cpu_to_be32(vi->version);
303 bph->last_comp_version = cpu_to_be32(vi->last_comp_version);
David Gibsonfc14dad2005-06-08 17:18:34 +1000304
David Gibson47f23de2005-07-11 17:19:26 +1000305 /* Reserve map should be doubleword aligned */
306 reserve_off = ALIGN(vi->hdr_size, 8);
307
308 bph->off_mem_rsvmap = cpu_to_be32(reserve_off);
309 bph->off_dt_struct = cpu_to_be32(reserve_off + reservesize);
310 bph->off_dt_strings = cpu_to_be32(reserve_off + reservesize
David Gibsonfc14dad2005-06-08 17:18:34 +1000311 + dtsize);
David Gibson47f23de2005-07-11 17:19:26 +1000312 bph->totalsize = cpu_to_be32(reserve_off + reservesize
David Gibsonfc14dad2005-06-08 17:18:34 +1000313 + dtsize + strsize);
314
315 if (vi->flags & FTF_BOOTCPUID)
316 bph->boot_cpuid_phys = 0xfeedbeef;
317 if (vi->flags & FTF_STRTABSIZE)
318 bph->size_dt_strings = cpu_to_be32(strsize);
319}
320
David Gibsonf0517db2005-07-15 17:14:24 +1000321void write_dt_blob(FILE *f, struct boot_info *bi, int version)
David Gibsonfc14dad2005-06-08 17:18:34 +1000322{
323 struct version_info *vi = NULL;
324 int i;
325 struct data dtbuf = empty_data;
326 struct data strbuf = empty_data;
327 struct boot_param_header bph;
David Gibsonf0517db2005-07-15 17:14:24 +1000328 struct reserve_entry termre = {.address = 0, .size = 0};
David Gibsonfc14dad2005-06-08 17:18:34 +1000329
330 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
331 if (version_table[i].version == version)
332 vi = &version_table[i];
333 }
334 if (!vi)
335 die("Unknown device tree blob version %d\n", version);
336
337 dtbuf = empty_data;
338 strbuf = empty_data;
339
David Gibsonf0517db2005-07-15 17:14:24 +1000340 flatten_tree(bi->dt, &bin_emitter, &dtbuf, &strbuf, vi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000341 bin_emit_cell(&dtbuf, OF_DT_END);
342
David Gibsonf0517db2005-07-15 17:14:24 +1000343 /* Make header */
344 make_bph(&bph, vi, &bi->mem_reserve_data, dtbuf.len, strbuf.len);
David Gibsonfc14dad2005-06-08 17:18:34 +1000345
David Gibson586606e2005-07-14 11:27:24 +1000346 fwrite(&bph, vi->hdr_size, 1, f);
347
David Gibson47f23de2005-07-11 17:19:26 +1000348 /* Align the reserve map to an 8 byte boundary */
349 for (i = vi->hdr_size; i < be32_to_cpu(bph.off_mem_rsvmap); i++)
350 fputc(0, f);
351
David Gibsonf0517db2005-07-15 17:14:24 +1000352 /*
353 * Reserve map entries.
354 * Since the blob is relocatable, the address of the map is not
355 * determinable here, so no entry is made for the DT itself.
356 * Each entry is an (address, size) pair of u64 values.
357 * Always supply a zero-sized temination entry.
358 */
359 fwrite(bi->mem_reserve_data.val, bi->mem_reserve_data.len, 1, f);
360 fwrite(&termre, sizeof(termre), 1, f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000361
362 fwrite(dtbuf.val, dtbuf.len, 1, f);
363 fwrite(strbuf.val, strbuf.len, 1, f);
364
365 if (ferror(f))
366 die("Error writing device tree blob: %s\n", strerror(errno));
367
368 data_free(dtbuf);
369 data_free(strbuf);
370}
371
David Gibson230f2532005-08-29 12:48:02 +1000372static void dump_stringtable_asm(FILE *f, struct data strbuf)
David Gibsonfc14dad2005-06-08 17:18:34 +1000373{
374 char *p;
375 int len;
376
377 p = strbuf.val;
378
379 while (p < (strbuf.val + strbuf.len)) {
380 len = strlen(p);
381 fprintf(f, "\t.string \"%s\"\n", p);
382 p += len+1;
383 }
384}
385
David Gibsonf0517db2005-07-15 17:14:24 +1000386void write_dt_asm(FILE *f, struct boot_info *bi, int version)
David Gibsonfc14dad2005-06-08 17:18:34 +1000387{
388 struct version_info *vi = NULL;
389 int i;
390 struct data strbuf = empty_data;
391 char *symprefix = "dt";
392
393 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
394 if (version_table[i].version == version)
395 vi = &version_table[i];
396 }
397 if (!vi)
398 die("Unknown device tree blob version %d\n", version);
399
400 fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
401 fprintf(f, "#define OF_DT_HEADER 0x%x\n", OF_DT_HEADER);
402 fprintf(f, "#define OF_DT_BEGIN_NODE 0x%x\n", OF_DT_BEGIN_NODE);
403 fprintf(f, "#define OF_DT_END_NODE 0x%x\n", OF_DT_END_NODE);
404 fprintf(f, "#define OF_DT_PROP 0x%x\n", OF_DT_PROP);
405 fprintf(f, "#define OF_DT_END 0x%x\n", OF_DT_END);
406 fprintf(f, "\n");
407
408 emit_label(f, symprefix, "blob_start");
409 emit_label(f, symprefix, "header");
410 fprintf(f, "\t.long\tOF_DT_HEADER /* magic */\n");
411 fprintf(f, "\t.long\t_%s_blob_end - _%s_blob_start /* totalsize */\n",
412 symprefix, symprefix);
413 fprintf(f, "\t.long\t_%s_struct_start - _%s_blob_start /* off_dt_struct */\n",
414 symprefix, symprefix);
415 fprintf(f, "\t.long\t_%s_strings_start - _%s_blob_start /* off_dt_strings */\n",
416 symprefix, symprefix);
417 fprintf(f, "\t.long\t_%s_reserve_map - _%s_blob_start /* off_dt_strings */\n",
418 symprefix, symprefix);
419 fprintf(f, "\t.long\t%d /* version */\n", vi->version);
420 fprintf(f, "\t.long\t%d /* last_comp_version */\n",
421 vi->last_comp_version);
422
423 if (vi->flags & FTF_BOOTCPUID)
424 fprintf(f, "\t.long\t0xdeadbeef\t/*boot_cpuid_phys*/\n");
425
426 if (vi->flags & FTF_STRTABSIZE)
427 fprintf(f, "\t.long\t_%s_strings_end - _%s_strings_start\t/* size_dt_strings */\n",
428 symprefix, symprefix);
429
David Gibsonf0517db2005-07-15 17:14:24 +1000430 /*
431 * Reserve map entries.
432 * Align the reserve map to a doubleword boundary.
433 * Each entry is an (address, size) pair of u64 values.
434 * Since the ASM file variant can relocate and compute the address
435 * and size of the the device tree itself, and an entry for it.
436 * Always supply a zero-sized temination entry.
437 */
David Gibson47f23de2005-07-11 17:19:26 +1000438 asm_emit_align(f, 8);
David Gibsonfc14dad2005-06-08 17:18:34 +1000439 emit_label(f, symprefix, "reserve_map");
David Gibsonfc14dad2005-06-08 17:18:34 +1000440 fprintf(f, "\t.long\t0, _%s_blob_start\n", symprefix);
441 fprintf(f, "\t.long\t0, _%s_blob_end - _%s_blob_start\n",
442 symprefix, symprefix);
David Gibsonf0517db2005-07-15 17:14:24 +1000443
444 if (bi->mem_reserve_data.len > 0) {
445 fprintf(f, "/* Memory reserve map from source file */\n");
446 asm_emit_data(f, bi->mem_reserve_data);
David Gibsonfc14dad2005-06-08 17:18:34 +1000447 }
448
David Gibsonf0517db2005-07-15 17:14:24 +1000449 fprintf(f, "\t.llong\t0\n");
450 fprintf(f, "\t.llong\t0\n");
451
David Gibsonfc14dad2005-06-08 17:18:34 +1000452 emit_label(f, symprefix, "struct_start");
David Gibsonf0517db2005-07-15 17:14:24 +1000453 flatten_tree(bi->dt, &asm_emitter, f, &strbuf, vi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000454 fprintf(f, "\t.long\tOF_DT_END\n");
455 emit_label(f, symprefix, "struct_end");
456
457 emit_label(f, symprefix, "strings_start");
458 dump_stringtable_asm(f, strbuf);
459 emit_label(f, symprefix, "strings_end");
460
461 emit_label(f, symprefix, "blob_end");
462
463 data_free(strbuf);
464}
465
466struct inbuf {
467 char *base, *limit, *ptr;
468};
469
470static void inbuf_init(struct inbuf *inb, void *base, void *limit)
471{
472 inb->base = base;
473 inb->limit = limit;
474 inb->ptr = inb->base;
475}
476
477static void flat_read_chunk(struct inbuf *inb, void *p, int len)
478{
479 if ((inb->ptr + len) > inb->limit)
480 die("Premature end of data parsing flat device tree\n");
481
482 memcpy(p, inb->ptr, len);
483
484 inb->ptr += len;
485}
486
487static u32 flat_read_word(struct inbuf *inb)
488{
489 u32 val;
490
491 assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
492
493 flat_read_chunk(inb, &val, sizeof(val));
494
495 return be32_to_cpu(val);
496}
497
498static void flat_realign(struct inbuf *inb, int align)
499{
500 int off = inb->ptr - inb->base;
501
502 inb->ptr = inb->base + ALIGN(off, align);
503 if (inb->ptr > inb->limit)
504 die("Premature end of data parsing flat device tree\n");
505}
506
507static char *flat_read_string(struct inbuf *inb)
508{
509 int len = 0;
510 char *p = inb->ptr;
511 char *str;
512
513 do {
514 if (p >= inb->limit)
515 die("Premature end of data parsing flat device tree\n");
516 len++;
517 } while ((*p++) != '\0');
518
519 str = strdup(inb->ptr);
520
521 inb->ptr += len;
522
523 flat_realign(inb, sizeof(u32));
524
525 return str;
526}
527
528static struct data flat_read_data(struct inbuf *inb, int len)
529{
530 struct data d = empty_data;
531
532 if (len == 0)
533 return empty_data;
534
535 d = data_grow_for(d, len);
536 d.len = len;
537
538 flat_read_chunk(inb, d.val, len);
539
540 flat_realign(inb, sizeof(u32));
541
542 return d;
543}
544
545static char *flat_read_stringtable(struct inbuf *inb, int offset)
546{
547 char *p;
548
549 p = inb->base + offset;
550 while (1) {
David Gibson4ddf7c02005-08-19 16:11:11 +1000551 if (p >= inb->limit || p < inb->base)
David Gibson7ee3ffd2005-07-11 16:45:57 +1000552 die("String offset %d overruns string table\n",
553 offset);
David Gibsonfc14dad2005-06-08 17:18:34 +1000554
555 if (*p == '\0')
556 break;
557
558 p++;
559 }
560
561 return strdup(inb->base + offset);
562}
563
David Gibson230f2532005-08-29 12:48:02 +1000564static struct property *flat_read_property(struct inbuf *dtbuf,
565 struct inbuf *strbuf, int flags)
David Gibsonfc14dad2005-06-08 17:18:34 +1000566{
567 u32 proplen, stroff;
568 char *name;
569 struct data val;
570
571 proplen = flat_read_word(dtbuf);
572 stroff = flat_read_word(dtbuf);
573
574 name = flat_read_stringtable(strbuf, stroff);
575
576 if ((flags & FTF_VARALIGN) && (proplen >= 8))
577 flat_realign(dtbuf, 8);
578
579 val = flat_read_data(dtbuf, proplen);
580
David Gibson4102d842005-06-16 14:36:37 +1000581 return build_property(name, val, NULL);
David Gibsonfc14dad2005-06-08 17:18:34 +1000582}
583
David Gibsonf0517db2005-07-15 17:14:24 +1000584
585static struct data flat_read_mem_reserve(struct inbuf *inb)
586{
587 char *p;
588 int len = 0;
589 int done = 0;
590 cell_t cells[4];
591 struct data d;
592
593 d = empty_data;
594
595 /*
596 * Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
597 * List terminates at an entry with size equal to zero.
598 *
599 * First pass, count entries.
600 */
601 p = inb->ptr;
602 do {
603 flat_read_chunk(inb, &cells[0], 4 * sizeof(cell_t));
604 if (cells[2] == 0 && cells[3] == 0) {
605 done = 1;
606 } else {
607 ++len;
608 }
609 } while (!done);
610
611 /*
612 * Back up for pass two, reading the whole data value.
613 */
614 inb->ptr = p;
615 d = flat_read_data(inb, len * 4 * sizeof(cell_t));
616
617 return d;
618}
619
620
David Gibsonfc14dad2005-06-08 17:18:34 +1000621static char *nodename_from_path(char *ppath, char *cpath)
622{
623 char *lslash;
624 int plen;
625
626 lslash = strrchr(cpath, '/');
627 if (! lslash)
628 return NULL;
629
630 plen = lslash - cpath;
631
632 if (streq(cpath, "/") && streq(ppath, ""))
633 return "";
634
635 if ((plen == 0) && streq(ppath, "/"))
636 return strdup(lslash+1);
637
David Gibson81f2e892005-06-16 17:04:00 +1000638 if (! strneq(ppath, cpath, plen))
David Gibsonfc14dad2005-06-08 17:18:34 +1000639 return NULL;
640
641 return strdup(lslash+1);
642}
643
644static const char PROPCHAR[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,._+*#?-";
645static const char UNITCHAR[] = "0123456789abcdef,";
646
647static int check_node_name(char *name)
648{
649 char *atpos;
650 int basenamelen;
651
652 atpos = strrchr(name, '@');
653
654 if (atpos)
655 basenamelen = atpos - name;
656 else
657 basenamelen = strlen(name);
658
659 if (strspn(name, PROPCHAR) < basenamelen)
660 return -1;
661
662 if (atpos
663 && ((basenamelen + 1 + strspn(atpos+1, UNITCHAR)) < strlen(name)))
664 return -1;
665
666 return basenamelen;
667}
668
669static struct node *unflatten_tree(struct inbuf *dtbuf,
670 struct inbuf *strbuf,
671 char *parent_path, int flags)
672{
673 struct node *node;
674 u32 val;
675
676 node = build_node(NULL, NULL);
677
678 if (flags & FTF_FULLPATH) {
679 node->fullpath = flat_read_string(dtbuf);
680 node->name = nodename_from_path(parent_path, node->fullpath);
681
682 if (! node->name)
683 die("Path \"%s\" is not valid as a child of \"%s\"\n",
684 node->fullpath, parent_path);
685 } else {
686 node->name = flat_read_string(dtbuf);
687 node->fullpath = join_path(parent_path, node->name);
688 }
689
690 node->basenamelen = check_node_name(node->name);
691 if (node->basenamelen < 0) {
692 fprintf(stderr, "Warning \"%s\" has incorrect format\n", node->name);
693 }
694
695 do {
696 struct property *prop;
697 struct node *child;
698
699 val = flat_read_word(dtbuf);
700 switch (val) {
701 case OF_DT_PROP:
702 prop = flat_read_property(dtbuf, strbuf, flags);
703 add_property(node, prop);
704 break;
705
706 case OF_DT_BEGIN_NODE:
707 child = unflatten_tree(dtbuf,strbuf, node->fullpath,
708 flags);
709 add_child(node, child);
710 break;
711
712 case OF_DT_END_NODE:
713 break;
714
715 case OF_DT_END:
716 die("Premature OF_DT_END in device tree blob\n");
717 break;
718
719 default:
720 die("Invalid opcode word %08x in device tree blob\n",
721 val);
722 }
723 } while (val != OF_DT_END_NODE);
724
725 return node;
726}
727
David Gibsonf0517db2005-07-15 17:14:24 +1000728
729struct boot_info *dt_from_blob(FILE *f)
David Gibsonfc14dad2005-06-08 17:18:34 +1000730{
David Gibsonf0517db2005-07-15 17:14:24 +1000731 u32 magic, totalsize, version, size_str;
732 u32 off_dt, off_str, off_mem_rsvmap;
David Gibsonfc14dad2005-06-08 17:18:34 +1000733 int rc;
734 char *blob;
735 struct boot_param_header *bph;
736 char *p;
737 struct inbuf dtbuf, strbuf;
David Gibsonf0517db2005-07-15 17:14:24 +1000738 struct inbuf memresvbuf;
David Gibsonfc14dad2005-06-08 17:18:34 +1000739 int sizeleft;
David Gibsonf0517db2005-07-15 17:14:24 +1000740 struct data mem_reserve_data;
David Gibsonfc14dad2005-06-08 17:18:34 +1000741 struct node *tree;
742 u32 val;
743 int flags = 0;
744
745 rc = fread(&magic, sizeof(magic), 1, f);
746 if (ferror(f))
747 die("Error reading DT blob magic number: %s\n",
748 strerror(errno));
749 if (rc < 1) {
750 if (feof(f))
751 die("EOF reading DT blob magic number\n");
752 else
753 die("Mysterious short read reading magic number\n");
754 }
755
756 magic = be32_to_cpu(magic);
757 if (magic != OF_DT_HEADER)
758 die("Blob has incorrect magic number\n");
759
760 rc = fread(&totalsize, sizeof(totalsize), 1, f);
761 if (ferror(f))
762 die("Error reading DT blob size: %s\n", strerror(errno));
763 if (rc < 1) {
764 if (feof(f))
765 die("EOF reading DT blob size\n");
766 else
767 die("Mysterious short read reading blob size\n");
768 }
769
770 totalsize = be32_to_cpu(totalsize);
771 if (totalsize < BPH_V1_SIZE)
772 die("DT blob size (%d) is too small\n", totalsize);
773
774 blob = xmalloc(totalsize);
775
776 bph = (struct boot_param_header *)blob;
777 bph->magic = cpu_to_be32(magic);
778 bph->totalsize = cpu_to_be32(totalsize);
779
780 sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
781 p = blob + sizeof(magic) + sizeof(totalsize);
782
783 while (sizeleft) {
784 if (feof(f))
785 die("EOF before reading %d bytes of DT blob\n",
786 totalsize);
787
788 rc = fread(p, 1, sizeleft, f);
789 if (ferror(f))
790 die("Error reading DT blob: %s\n",
791 strerror(errno));
792
793 sizeleft -= rc;
794 p += rc;
795 }
796
797 off_dt = be32_to_cpu(bph->off_dt_struct);
798 off_str = be32_to_cpu(bph->off_dt_strings);
David Gibsonf0517db2005-07-15 17:14:24 +1000799 off_mem_rsvmap = be32_to_cpu(bph->off_mem_rsvmap);
David Gibsonfc14dad2005-06-08 17:18:34 +1000800 version = be32_to_cpu(bph->version);
801
802 fprintf(stderr, "\tmagic:\t\t\t0x%x\n", magic);
803 fprintf(stderr, "\ttotalsize:\t\t%d\n", totalsize);
804 fprintf(stderr, "\toff_dt_struct:\t\t0x%x\n", off_dt);
805 fprintf(stderr, "\toff_dt_strings:\t\t0x%x\n", off_str);
David Gibsonf0517db2005-07-15 17:14:24 +1000806 fprintf(stderr, "\toff_mem_rsvmap:\t\t0x%x\n", off_mem_rsvmap);
David Gibsonfc14dad2005-06-08 17:18:34 +1000807 fprintf(stderr, "\tversion:\t\t0x%x\n", version );
808 fprintf(stderr, "\tlast_comp_version:\t0x%x\n",
809 be32_to_cpu(bph->last_comp_version));
810
David Gibsonf0517db2005-07-15 17:14:24 +1000811 if (off_mem_rsvmap >= totalsize)
812 die("Mem Reserve structure offset exceeds total size\n");
813
David Gibsonfc14dad2005-06-08 17:18:34 +1000814 if (off_dt >= totalsize)
815 die("DT structure offset exceeds total size\n");
816
817 if (off_str > totalsize)
818 die("String table offset exceeds total size\n");
819
820 if (version >= 2)
821 fprintf(stderr, "\tboot_cpuid_phys:\t0x%x\n",
822 be32_to_cpu(bph->boot_cpuid_phys));
823
824 if (version >= 3) {
825 size_str = be32_to_cpu(bph->size_dt_strings);
826 fprintf(stderr, "\tsize_dt_strings:\t%d\n", size_str);
827 if (off_str+size_str > totalsize)
828 die("String table extends past total size\n");
829 }
830
831 if (version < 0x10) {
832 flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
833 }
834
David Gibsonf0517db2005-07-15 17:14:24 +1000835 inbuf_init(&memresvbuf,
836 blob + off_mem_rsvmap, blob + totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000837 inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
838 inbuf_init(&strbuf, blob + off_str, blob + totalsize);
839
840 if (version >= 3)
841 strbuf.limit = strbuf.base + size_str;
842
David Gibsonf0517db2005-07-15 17:14:24 +1000843 mem_reserve_data = flat_read_mem_reserve(&memresvbuf);
844
David Gibsonfc14dad2005-06-08 17:18:34 +1000845 val = flat_read_word(&dtbuf);
846
847 if (val != OF_DT_BEGIN_NODE)
David Gibson41916132005-08-25 15:39:09 +1000848 die("Device tree blob doesn't begin with OF_DT_BEGIN_NODE (begins with 0x%08x)\n", val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000849
850 tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
851
852 val = flat_read_word(&dtbuf);
853 if (val != OF_DT_END)
854 die("Device tree blob doesn't end with OF_DT_END\n");
855
856 free(blob);
857
David Gibsonf0517db2005-07-15 17:14:24 +1000858 return build_boot_info(mem_reserve_data, tree);
David Gibsonfc14dad2005-06-08 17:18:34 +1000859}