blob: 56c1b3816c62121fb9861e22e531edcc7b80a1c5 [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
Mark A. Greer7a9f6632006-03-15 18:59:24 -0700124 fprintf(f, "\t.long\t0x%x\n", val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000125}
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
David Gibsonf040d952005-10-24 18:18:38 +1000290static 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 Gibsonfc14dad2005-06-08 17:18:34 +1000302static void make_bph(struct boot_param_header *bph,
David Gibsonf0517db2005-07-15 17:14:24 +1000303 struct version_info *vi,
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000304 int reservesize, int dtsize, int strsize,
305 int boot_cpuid_phys)
David Gibsonfc14dad2005-06-08 17:18:34 +1000306{
David Gibson47f23de2005-07-11 17:19:26 +1000307 int reserve_off;
David Gibsonf040d952005-10-24 18:18:38 +1000308
309 reservesize += sizeof(struct reserve_entry);
David Gibsonfc14dad2005-06-08 17:18:34 +1000310
311 memset(bph, 0xff, sizeof(*bph));
312
313 bph->magic = cpu_to_be32(OF_DT_HEADER);
David Gibsondffc2a82005-08-25 14:47:20 +1000314 bph->version = cpu_to_be32(vi->version);
315 bph->last_comp_version = cpu_to_be32(vi->last_comp_version);
David Gibsonfc14dad2005-06-08 17:18:34 +1000316
David Gibson47f23de2005-07-11 17:19:26 +1000317 /* 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 Gibsonfc14dad2005-06-08 17:18:34 +1000323 + dtsize);
David Gibson47f23de2005-07-11 17:19:26 +1000324 bph->totalsize = cpu_to_be32(reserve_off + reservesize
David Gibsonfc14dad2005-06-08 17:18:34 +1000325 + dtsize + strsize);
326
327 if (vi->flags & FTF_BOOTCPUID)
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000328 bph->boot_cpuid_phys = cpu_to_be32(boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000329 if (vi->flags & FTF_STRTABSIZE)
330 bph->size_dt_strings = cpu_to_be32(strsize);
331}
332
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000333void dt_to_blob(FILE *f, struct boot_info *bi, int version,
334 int boot_cpuid_phys)
David Gibsonfc14dad2005-06-08 17:18:34 +1000335{
336 struct version_info *vi = NULL;
337 int i;
338 struct data dtbuf = empty_data;
339 struct data strbuf = empty_data;
David Gibsonf040d952005-10-24 18:18:38 +1000340 struct data reservebuf;
David Gibsonfc14dad2005-06-08 17:18:34 +1000341 struct boot_param_header bph;
David Gibsonf0517db2005-07-15 17:14:24 +1000342 struct reserve_entry termre = {.address = 0, .size = 0};
David Gibsonfc14dad2005-06-08 17:18:34 +1000343
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 Gibsonf0517db2005-07-15 17:14:24 +1000354 flatten_tree(bi->dt, &bin_emitter, &dtbuf, &strbuf, vi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000355 bin_emit_cell(&dtbuf, OF_DT_END);
356
David Gibsonf040d952005-10-24 18:18:38 +1000357 reservebuf = flatten_reserve_list(bi->reservelist, vi);
358
David Gibsonf0517db2005-07-15 17:14:24 +1000359 /* Make header */
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000360 make_bph(&bph, vi, reservebuf.len, dtbuf.len, strbuf.len,
361 boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000362
David Gibson586606e2005-07-14 11:27:24 +1000363 fwrite(&bph, vi->hdr_size, 1, f);
364
David Gibson47f23de2005-07-11 17:19:26 +1000365 /* 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 Gibsonf0517db2005-07-15 17:14:24 +1000369 /*
370 * Reserve map entries.
David Gibsonf0517db2005-07-15 17:14:24 +1000371 * Each entry is an (address, size) pair of u64 values.
372 * Always supply a zero-sized temination entry.
373 */
David Gibsonf040d952005-10-24 18:18:38 +1000374 fwrite(reservebuf.val, reservebuf.len, 1, f);
David Gibsonf0517db2005-07-15 17:14:24 +1000375 fwrite(&termre, sizeof(termre), 1, f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000376
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 Gibson230f2532005-08-29 12:48:02 +1000387static void dump_stringtable_asm(FILE *f, struct data strbuf)
David Gibsonfc14dad2005-06-08 17:18:34 +1000388{
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 Neuling38e8f8f2006-05-31 08:31:51 +1000401void dt_to_asm(FILE *f, struct boot_info *bi, int version, int boot_cpuid_phys)
David Gibsonfc14dad2005-06-08 17:18:34 +1000402{
403 struct version_info *vi = NULL;
404 int i;
405 struct data strbuf = empty_data;
David Gibsonf040d952005-10-24 18:18:38 +1000406 struct reserve_info *re;
David Gibsonfc14dad2005-06-08 17:18:34 +1000407 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 Neuling38e8f8f2006-05-31 08:31:51 +1000440 fprintf(f, "\t.long\t%i\t/*boot_cpuid_phys*/\n",
441 boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000442
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 Gibsonf0517db2005-07-15 17:14:24 +1000447 /*
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 Gibsonf0517db2005-07-15 17:14:24 +1000451 * Always supply a zero-sized temination entry.
452 */
David Gibson47f23de2005-07-11 17:19:26 +1000453 asm_emit_align(f, 8);
David Gibsonfc14dad2005-06-08 17:18:34 +1000454 emit_label(f, symprefix, "reserve_map");
David Gibsonf0517db2005-07-15 17:14:24 +1000455
David Gibsonf040d952005-10-24 18:18:38 +1000456 fprintf(f, "/* Memory reserve map from source file */\n");
Jon Loeliger05ae3d82006-04-19 11:58:45 -0500457
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 Gibsonf040d952005-10-24 18:18:38 +1000462 for (re = bi->reservelist; re; re = re->next) {
Jon Loeliger05ae3d82006-04-19 11:58:45 -0500463 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 Gibsonfc14dad2005-06-08 17:18:34 +1000469 }
470
Jon Loeliger05ae3d82006-04-19 11:58:45 -0500471 fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000472
David Gibsonfc14dad2005-06-08 17:18:34 +1000473 emit_label(f, symprefix, "struct_start");
David Gibsonf0517db2005-07-15 17:14:24 +1000474 flatten_tree(bi->dt, &asm_emitter, f, &strbuf, vi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000475 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
487struct inbuf {
488 char *base, *limit, *ptr;
489};
490
491static 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
498static 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
508static 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
519static 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
528static 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
549static 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
566static char *flat_read_stringtable(struct inbuf *inb, int offset)
567{
568 char *p;
569
570 p = inb->base + offset;
571 while (1) {
David Gibson4ddf7c02005-08-19 16:11:11 +1000572 if (p >= inb->limit || p < inb->base)
David Gibson7ee3ffd2005-07-11 16:45:57 +1000573 die("String offset %d overruns string table\n",
574 offset);
David Gibsonfc14dad2005-06-08 17:18:34 +1000575
576 if (*p == '\0')
577 break;
578
579 p++;
580 }
581
582 return strdup(inb->base + offset);
583}
584
David Gibson230f2532005-08-29 12:48:02 +1000585static struct property *flat_read_property(struct inbuf *dtbuf,
586 struct inbuf *strbuf, int flags)
David Gibsonfc14dad2005-06-08 17:18:34 +1000587{
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 Gibson4102d842005-06-16 14:36:37 +1000602 return build_property(name, val, NULL);
David Gibsonfc14dad2005-06-08 17:18:34 +1000603}
604
David Gibsonf0517db2005-07-15 17:14:24 +1000605
David Gibsonf040d952005-10-24 18:18:38 +1000606static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
David Gibsonf0517db2005-07-15 17:14:24 +1000607{
David Gibsonf040d952005-10-24 18:18:38 +1000608 struct reserve_info *reservelist = NULL;
609 struct reserve_info *new;
David Gibsonf0517db2005-07-15 17:14:24 +1000610 char *p;
David Gibson6c0f3672005-08-29 13:36:15 +1000611 struct reserve_entry re;
David Gibsonf0517db2005-07-15 17:14:24 +1000612
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 Gibson6c0f3672005-08-29 13:36:15 +1000620 while (1) {
621 flat_read_chunk(inb, &re, sizeof(re));
622 if (re.size == 0)
623 break;
David Gibsonf0517db2005-07-15 17:14:24 +1000624
David Gibsonf040d952005-10-24 18:18:38 +1000625 new = build_reserve_entry(re.address, re.size, NULL);
626 reservelist = add_reserve_entry(reservelist, new);
David Gibson6c0f3672005-08-29 13:36:15 +1000627 }
David Gibsonf0517db2005-07-15 17:14:24 +1000628
David Gibsonf040d952005-10-24 18:18:38 +1000629 return reservelist;
David Gibsonf0517db2005-07-15 17:14:24 +1000630}
631
632
David Gibsonfc14dad2005-06-08 17:18:34 +1000633static 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 Gibson81f2e892005-06-16 17:04:00 +1000650 if (! strneq(ppath, cpath, plen))
David Gibsonfc14dad2005-06-08 17:18:34 +1000651 return NULL;
652
653 return strdup(lslash+1);
654}
655
656static const char PROPCHAR[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,._+*#?-";
657static const char UNITCHAR[] = "0123456789abcdef,";
658
659static 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
681static 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 Gibsonf0517db2005-07-15 17:14:24 +1000740
741struct boot_info *dt_from_blob(FILE *f)
David Gibsonfc14dad2005-06-08 17:18:34 +1000742{
David Gibsonf0517db2005-07-15 17:14:24 +1000743 u32 magic, totalsize, version, size_str;
744 u32 off_dt, off_str, off_mem_rsvmap;
David Gibsonfc14dad2005-06-08 17:18:34 +1000745 int rc;
746 char *blob;
747 struct boot_param_header *bph;
748 char *p;
749 struct inbuf dtbuf, strbuf;
David Gibsonf0517db2005-07-15 17:14:24 +1000750 struct inbuf memresvbuf;
David Gibsonfc14dad2005-06-08 17:18:34 +1000751 int sizeleft;
David Gibsonf040d952005-10-24 18:18:38 +1000752 struct reserve_info *reservelist;
David Gibsonfc14dad2005-06-08 17:18:34 +1000753 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 Gibsonf0517db2005-07-15 17:14:24 +1000811 off_mem_rsvmap = be32_to_cpu(bph->off_mem_rsvmap);
David Gibsonfc14dad2005-06-08 17:18:34 +1000812 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 Gibsonf0517db2005-07-15 17:14:24 +1000818 fprintf(stderr, "\toff_mem_rsvmap:\t\t0x%x\n", off_mem_rsvmap);
David Gibsonfc14dad2005-06-08 17:18:34 +1000819 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 Gibsonf0517db2005-07-15 17:14:24 +1000823 if (off_mem_rsvmap >= totalsize)
824 die("Mem Reserve structure offset exceeds total size\n");
825
David Gibsonfc14dad2005-06-08 17:18:34 +1000826 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 Gibsonf0517db2005-07-15 17:14:24 +1000847 inbuf_init(&memresvbuf,
848 blob + off_mem_rsvmap, blob + totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000849 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 Gibsonf040d952005-10-24 18:18:38 +1000855 reservelist = flat_read_mem_reserve(&memresvbuf);
David Gibsonf0517db2005-07-15 17:14:24 +1000856
David Gibsonfc14dad2005-06-08 17:18:34 +1000857 val = flat_read_word(&dtbuf);
858
859 if (val != OF_DT_BEGIN_NODE)
David Gibson41916132005-08-25 15:39:09 +1000860 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 +1000861
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 Gibsonf040d952005-10-24 18:18:38 +1000870 return build_boot_info(reservelist, tree);
David Gibsonfc14dad2005-06-08 17:18:34 +1000871}