blob: df3df7e4bb950111173885709dfa25eb5720dddd [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
David Gibson5a98ddd2007-10-17 12:39:10 +100052int isstring(char c)
53{
54 return (isprint(c)
55 || (c == '\0')
56 || strchr("\a\b\t\n\v\f\r", c));
57}
58
David Gibson5a98ddd2007-10-17 12:39:10 +100059static void write_propval_string(FILE *f, struct data val)
60{
61 char *str = val.val;
62 int i;
David Gibsond3ea6e52007-11-07 10:22:25 +110063 int newchunk = 1;
64 struct fixup *l = val.labels;
David Gibson5a98ddd2007-10-17 12:39:10 +100065
66 assert(str[val.len-1] == '\0');
67
David Gibson5a98ddd2007-10-17 12:39:10 +100068 for (i = 0; i < (val.len-1); i++) {
69 char c = str[i];
70
David Gibsond3ea6e52007-11-07 10:22:25 +110071 if (newchunk) {
72 while (l && (l->offset <= i)) {
73 assert(l->offset == i);
74 fprintf(f, "%s: ", l->ref);
75 l = l->next;
76 }
77 fprintf(f, "\"");
78 newchunk = 0;
79 }
80
David Gibson5a98ddd2007-10-17 12:39:10 +100081 switch (c) {
82 case '\a':
83 fprintf(f, "\\a");
84 break;
85 case '\b':
86 fprintf(f, "\\b");
87 break;
88 case '\t':
89 fprintf(f, "\\t");
90 break;
91 case '\n':
92 fprintf(f, "\\n");
93 break;
94 case '\v':
95 fprintf(f, "\\v");
96 break;
97 case '\f':
98 fprintf(f, "\\f");
99 break;
100 case '\r':
101 fprintf(f, "\\r");
102 break;
103 case '\\':
104 fprintf(f, "\\\\");
105 break;
106 case '\"':
107 fprintf(f, "\\\"");
108 break;
109 case '\0':
David Gibsond3ea6e52007-11-07 10:22:25 +1100110 fprintf(f, "\", ");
111 newchunk = 1;
David Gibson5a98ddd2007-10-17 12:39:10 +1000112 break;
113 default:
114 if (isprint(c))
115 fprintf(f, "%c", c);
116 else
117 fprintf(f, "\\x%02hhx", c);
118 }
119 }
David Gibsoned01ae42007-11-07 10:21:20 +1100120 fprintf(f, "\"");
David Gibsond3ea6e52007-11-07 10:22:25 +1100121
122 /* Wrap up any labels at the end of the value */
123 while (l) {
124 assert (l->offset == val.len);
125 fprintf(f, " %s:", l->ref);
126 l = l->next;
127 }
David Gibson5a98ddd2007-10-17 12:39:10 +1000128}
129
130static void write_propval_cells(FILE *f, struct data val)
131{
132 void *propend = val.val + val.len;
133 cell_t *cp = (cell_t *)val.val;
David Gibsond3ea6e52007-11-07 10:22:25 +1100134 struct fixup *l = val.labels;
David Gibson5a98ddd2007-10-17 12:39:10 +1000135
David Gibsoned01ae42007-11-07 10:21:20 +1100136 fprintf(f, "<");
David Gibson5a98ddd2007-10-17 12:39:10 +1000137 for (;;) {
David Gibsond3ea6e52007-11-07 10:22:25 +1100138 while (l && (l->offset <= ((char *)cp - val.val))) {
139 assert(l->offset == ((char *)cp - val.val));
140 fprintf(f, "%s: ", l->ref);
141 l = l->next;
142 }
143
David Gibson91967ac2007-11-07 11:17:37 +1100144 fprintf(f, "0x%x", be32_to_cpu(*cp++));
David Gibson5a98ddd2007-10-17 12:39:10 +1000145 if ((void *)cp >= propend)
146 break;
147 fprintf(f, " ");
148 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100149
150 /* Wrap up any labels at the end of the value */
151 while (l) {
152 assert (l->offset == val.len);
153 fprintf(f, " %s:", l->ref);
154 l = l->next;
155 }
David Gibsoned01ae42007-11-07 10:21:20 +1100156 fprintf(f, ">");
David Gibson5a98ddd2007-10-17 12:39:10 +1000157}
158
159static void write_propval_bytes(FILE *f, struct data val)
160{
161 void *propend = val.val + val.len;
162 char *bp = val.val;
David Gibsond3ea6e52007-11-07 10:22:25 +1100163 struct fixup *l = val.labels;
David Gibson5a98ddd2007-10-17 12:39:10 +1000164
David Gibsoned01ae42007-11-07 10:21:20 +1100165 fprintf(f, "[");
David Gibson5a98ddd2007-10-17 12:39:10 +1000166 for (;;) {
David Gibsond3ea6e52007-11-07 10:22:25 +1100167 while (l && (l->offset == (bp-val.val))) {
168 fprintf(f, "%s: ", l->ref);
169 l = l->next;
170 }
171
David Gibson5a98ddd2007-10-17 12:39:10 +1000172 fprintf(f, "%02hhx", *bp++);
173 if ((void *)bp >= propend)
174 break;
175 fprintf(f, " ");
176 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100177
178 /* Wrap up any labels at the end of the value */
179 while (l) {
180 assert (l->offset == val.len);
181 fprintf(f, " %s:", l->ref);
182 l = l->next;
183 }
David Gibsoned01ae42007-11-07 10:21:20 +1100184 fprintf(f, "]");
185}
186
187static void write_propval(FILE *f, struct property *prop)
188{
189 int len = prop->val.len;
190 char *p = prop->val.val;
David Gibsond3ea6e52007-11-07 10:22:25 +1100191 struct fixup *l;
David Gibsoned01ae42007-11-07 10:21:20 +1100192 int nnotstring = 0, nnul = 0;
David Gibsond3ea6e52007-11-07 10:22:25 +1100193 int nnotstringlbl = 0, nnotcelllbl = 0;
David Gibsoned01ae42007-11-07 10:21:20 +1100194 int i;
195
196 if (len == 0) {
197 fprintf(f, ";\n");
198 return;
199 }
200
201 for (i = 0; i < len; i++) {
202 if (! isstring(p[i]))
203 nnotstring++;
204 if (p[i] == '\0')
205 nnul++;
206 }
207
David Gibsond3ea6e52007-11-07 10:22:25 +1100208 for (l = prop->val.labels; l; l = l->next) {
209 if ((l->offset > 0) && (prop->val.val[l->offset - 1] != '\0'))
210 nnotstringlbl++;
211 if ((l->offset % sizeof(cell_t)) != 0)
212 nnotcelllbl++;
213 }
David Gibsoned01ae42007-11-07 10:21:20 +1100214
David Gibsond3ea6e52007-11-07 10:22:25 +1100215 fprintf(f, " = ");
216 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
217 && (nnotstringlbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100218 write_propval_string(f, prop->val);
David Gibsond3ea6e52007-11-07 10:22:25 +1100219 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100220 write_propval_cells(f, prop->val);
221 } else {
222 write_propval_bytes(f, prop->val);
223 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100224
David Gibsoned01ae42007-11-07 10:21:20 +1100225 fprintf(f, ";\n");
David Gibson5a98ddd2007-10-17 12:39:10 +1000226}
David Gibsonf0517db2005-07-15 17:14:24 +1000227
David Gibson230f2532005-08-29 12:48:02 +1000228static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +1000229{
230 struct property *prop;
231 struct node *child;
232
233 write_prefix(f, level);
David Gibson02563ad2007-11-02 16:10:30 +1100234 if (tree->label)
235 fprintf(f, "%s: ", tree->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000236 if (tree->name && (*tree->name))
237 fprintf(f, "%s {\n", tree->name);
238 else
239 fprintf(f, "/ {\n");
240
241 for_each_property(tree, prop) {
David Gibson02563ad2007-11-02 16:10:30 +1100242 write_prefix(f, level+1);
243 if (prop->label)
244 fprintf(f, "%s: ", prop->label);
245 fprintf(f, "%s", prop->name);
David Gibsoned01ae42007-11-07 10:21:20 +1100246 write_propval(f, prop);
David Gibsonfc14dad2005-06-08 17:18:34 +1000247 }
248 for_each_child(tree, child) {
249 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000250 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000251 }
252 write_prefix(f, level);
253 fprintf(f, "};\n");
254}
David Gibsonf0517db2005-07-15 17:14:24 +1000255
256
David Gibson712e52e2005-10-26 16:56:26 +1000257void dt_to_source(FILE *f, struct boot_info *bi)
David Gibsonf0517db2005-07-15 17:14:24 +1000258{
David Gibsonf040d952005-10-24 18:18:38 +1000259 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000260
David Gibson91967ac2007-11-07 11:17:37 +1100261 fprintf(f, "/dts-v1/;\n\n");
262
David Gibsonf040d952005-10-24 18:18:38 +1000263 for (re = bi->reservelist; re; re = re->next) {
David Gibson02563ad2007-11-02 16:10:30 +1100264 if (re->label)
265 fprintf(f, "%s: ", re->label);
David Gibson91967ac2007-11-07 11:17:37 +1100266 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
David Gibsonf040d952005-10-24 18:18:38 +1000267 (unsigned long long)re->re.address,
David Gibson91967ac2007-11-07 11:17:37 +1100268 (unsigned long long)re->re.size);
David Gibsonf0517db2005-07-15 17:14:24 +1000269 }
270
271 write_tree_source_node(f, bi->dt, 0);
272}
273