blob: f62041f0e32bb5b5790f4bb8e8382c61afa2ba8d [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
20
21#include "dtc.h"
Jon Loeligere45e6fd2007-03-23 15:18:41 -050022#include "srcpos.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100023
David Gibsonfc14dad2005-06-08 17:18:34 +100024extern FILE *yyin;
25extern int yyparse(void);
David Gibsonf0517db2005-07-15 17:14:24 +100026extern void yyerror(char const *);
David Gibsonfc14dad2005-06-08 17:18:34 +100027
David Gibsonf0517db2005-07-15 17:14:24 +100028struct boot_info *the_boot_info;
29
Jon Loeligere45e6fd2007-03-23 15:18:41 -050030struct boot_info *dt_from_source(const char *fname)
David Gibsonf0517db2005-07-15 17:14:24 +100031{
32 the_boot_info = NULL;
33
Jon Loeligere45e6fd2007-03-23 15:18:41 -050034 push_input_file(fname);
35
David Gibsonfc14dad2005-06-08 17:18:34 +100036 if (yyparse() != 0)
37 return NULL;
38
David Gibsonf0517db2005-07-15 17:14:24 +100039 fill_fullpaths(the_boot_info->dt, "");
David Gibsonfc14dad2005-06-08 17:18:34 +100040
David Gibsonf0517db2005-07-15 17:14:24 +100041 return the_boot_info;
David Gibsonfc14dad2005-06-08 17:18:34 +100042}
43
44static void write_prefix(FILE *f, int level)
45{
46 int i;
47
48 for (i = 0; i < level; i++)
49 fputc('\t', f);
50}
51
52enum proptype {
53 PROP_EMPTY,
54 PROP_STRING,
55 PROP_CELLS,
56 PROP_BYTES,
57};
58
David Gibson5a98ddd2007-10-17 12:39:10 +100059int isstring(char c)
60{
61 return (isprint(c)
62 || (c == '\0')
63 || strchr("\a\b\t\n\v\f\r", c));
64}
65
David Gibsonfc14dad2005-06-08 17:18:34 +100066static enum proptype guess_type(struct property *prop)
67{
68 int len = prop->val.len;
69 char *p = prop->val.val;
David Gibson5a98ddd2007-10-17 12:39:10 +100070 int nnotstring = 0, nnul = 0;
David Gibsonfc14dad2005-06-08 17:18:34 +100071 int i;
72
73 if (len == 0)
74 return PROP_EMPTY;
75
76 for (i = 0; i < len; i++) {
David Gibson5a98ddd2007-10-17 12:39:10 +100077 if (! isstring(p[i]))
78 nnotstring++;
79 if (p[i] == '\0')
80 nnul++;
David Gibsonfc14dad2005-06-08 17:18:34 +100081 }
82
David Gibson5a98ddd2007-10-17 12:39:10 +100083 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul)))
David Gibsonfc14dad2005-06-08 17:18:34 +100084 return PROP_STRING;
85 else if ((len % sizeof(cell_t)) == 0)
86 return PROP_CELLS;
87 else
88 return PROP_BYTES;
David Gibsonfc14dad2005-06-08 17:18:34 +100089}
90
David Gibson5a98ddd2007-10-17 12:39:10 +100091static void write_propval_string(FILE *f, struct data val)
92{
93 char *str = val.val;
94 int i;
95
96 assert(str[val.len-1] == '\0');
97
98 fprintf(f, " = \"");
99 for (i = 0; i < (val.len-1); i++) {
100 char c = str[i];
101
102 switch (c) {
103 case '\a':
104 fprintf(f, "\\a");
105 break;
106 case '\b':
107 fprintf(f, "\\b");
108 break;
109 case '\t':
110 fprintf(f, "\\t");
111 break;
112 case '\n':
113 fprintf(f, "\\n");
114 break;
115 case '\v':
116 fprintf(f, "\\v");
117 break;
118 case '\f':
119 fprintf(f, "\\f");
120 break;
121 case '\r':
122 fprintf(f, "\\r");
123 break;
124 case '\\':
125 fprintf(f, "\\\\");
126 break;
127 case '\"':
128 fprintf(f, "\\\"");
129 break;
130 case '\0':
131 fprintf(f, "\", \"");
132 break;
133 default:
134 if (isprint(c))
135 fprintf(f, "%c", c);
136 else
137 fprintf(f, "\\x%02hhx", c);
138 }
139 }
140 fprintf(f, "\";\n");
141}
142
143static void write_propval_cells(FILE *f, struct data val)
144{
145 void *propend = val.val + val.len;
146 cell_t *cp = (cell_t *)val.val;
147
148 fprintf(f, " = <");
149 for (;;) {
150 fprintf(f, "%x", be32_to_cpu(*cp++));
151 if ((void *)cp >= propend)
152 break;
153 fprintf(f, " ");
154 }
155 fprintf(f, ">;\n");
156}
157
158static void write_propval_bytes(FILE *f, struct data val)
159{
160 void *propend = val.val + val.len;
161 char *bp = val.val;
162
163 fprintf(f, " = [");
164 for (;;) {
165 fprintf(f, "%02hhx", *bp++);
166 if ((void *)bp >= propend)
167 break;
168 fprintf(f, " ");
169 }
170 fprintf(f, "];\n");
171}
David Gibsonf0517db2005-07-15 17:14:24 +1000172
David Gibson230f2532005-08-29 12:48:02 +1000173static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +1000174{
175 struct property *prop;
176 struct node *child;
177
178 write_prefix(f, level);
179 if (tree->name && (*tree->name))
180 fprintf(f, "%s {\n", tree->name);
181 else
182 fprintf(f, "/ {\n");
183
184 for_each_property(tree, prop) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000185 enum proptype type;
186
187 write_prefix(f, level);
188 fprintf(f, "\t%s", prop->name);
189 type = guess_type(prop);
David Gibsonfc14dad2005-06-08 17:18:34 +1000190
191 switch (type) {
192 case PROP_EMPTY:
193 fprintf(f, ";\n");
194 break;
195
196 case PROP_STRING:
David Gibson5a98ddd2007-10-17 12:39:10 +1000197 write_propval_string(f, prop->val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000198 break;
199
200 case PROP_CELLS:
David Gibson5a98ddd2007-10-17 12:39:10 +1000201 write_propval_cells(f, prop->val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000202 break;
203
204 case PROP_BYTES:
David Gibson5a98ddd2007-10-17 12:39:10 +1000205 write_propval_bytes(f, prop->val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000206 break;
207 }
208 }
209 for_each_child(tree, child) {
210 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000211 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000212 }
213 write_prefix(f, level);
214 fprintf(f, "};\n");
215}
David Gibsonf0517db2005-07-15 17:14:24 +1000216
217
David Gibson712e52e2005-10-26 16:56:26 +1000218void dt_to_source(FILE *f, struct boot_info *bi)
David Gibsonf0517db2005-07-15 17:14:24 +1000219{
David Gibsonf040d952005-10-24 18:18:38 +1000220 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000221
David Gibsonf040d952005-10-24 18:18:38 +1000222 for (re = bi->reservelist; re; re = re->next) {
David Gibsonf0517db2005-07-15 17:14:24 +1000223 fprintf(f, "/memreserve/\t%016llx-%016llx;\n",
David Gibsonf040d952005-10-24 18:18:38 +1000224 (unsigned long long)re->re.address,
225 (unsigned long long)(re->re.address + re->re.size - 1));
David Gibsonf0517db2005-07-15 17:14:24 +1000226 }
227
228 write_tree_source_node(f, bi->dt, 0);
229}
230