blob: 6b0c974f96ae5100a9e489b49c2400b9ee4f221d [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"
22
23struct node *device_tree;
24
25extern FILE *yyin;
26extern int yyparse(void);
27
28struct node *dt_from_source(FILE *f)
29{
30 yyin = f;
31 if (yyparse() != 0)
32 return NULL;
33
34 fill_fullpaths(device_tree, "");
35
36 return device_tree;
37}
38
39static void write_prefix(FILE *f, int level)
40{
41 int i;
42
43 for (i = 0; i < level; i++)
44 fputc('\t', f);
45}
46
47enum proptype {
48 PROP_EMPTY,
49 PROP_STRING,
50 PROP_CELLS,
51 PROP_BYTES,
52};
53
54static enum proptype guess_type(struct property *prop)
55{
56 int len = prop->val.len;
57 char *p = prop->val.val;
58 int nnoprint = 0;
59 int i;
60
61 if (len == 0)
62 return PROP_EMPTY;
63
64 for (i = 0; i < len; i++) {
65 if (! isprint(p[i]))
66 nnoprint++;
67 }
68
69 if ((nnoprint == 1) && (p[len-1] == '\0'))
70 return PROP_STRING;
71 else if ((len % sizeof(cell_t)) == 0)
72 return PROP_CELLS;
73 else
74 return PROP_BYTES;
75
76}
77
78void write_tree_source(FILE *f, struct node *tree, int level)
79{
80 struct property *prop;
81 struct node *child;
82
83 write_prefix(f, level);
84 if (tree->name && (*tree->name))
85 fprintf(f, "%s {\n", tree->name);
86 else
87 fprintf(f, "/ {\n");
88
89 for_each_property(tree, prop) {
90 cell_t *cp;
91 char *bp;
92 void *propend;
93 enum proptype type;
94
95 write_prefix(f, level);
96 fprintf(f, "\t%s", prop->name);
97 type = guess_type(prop);
98 propend = prop->val.val + prop->val.len;
99
100 switch (type) {
101 case PROP_EMPTY:
102 fprintf(f, ";\n");
103 break;
104
105 case PROP_STRING:
106 fprintf(f, " = \"%s\";\n", (char *)prop->val.val);
107 break;
108
109 case PROP_CELLS:
110 fprintf(f, " = <");
111 cp = (cell_t *)prop->val.val;
112 for (;;) {
113 fprintf(f, "%x", be32_to_cpu(*cp++));
114 if ((void *)cp >= propend)
115 break;
116 fprintf(f, " ");
117 }
118 fprintf(f, ">;\n");
119 break;
120
121 case PROP_BYTES:
122 fprintf(f, " = [");
123 bp = prop->val.val;
124 for (;;) {
125 fprintf(f, "%02hhx", *bp++);
126 if ((void *)bp >= propend)
127 break;
128 fprintf(f, " ");
129 }
130 fprintf(f, "];\n");
131 break;
132 }
133 }
134 for_each_child(tree, child) {
135 fprintf(f, "\n");
136 write_tree_source(f, child, level+1);
137 }
138 write_prefix(f, level);
139 fprintf(f, "};\n");
140}