blob: e7d580f0ee8459cdd3cadcc60ccf1dba82b4948a [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);
26
David Gibsonf0517db2005-07-15 17:14:24 +100027struct boot_info *the_boot_info;
28
Jon Loeligere45e6fd2007-03-23 15:18:41 -050029struct boot_info *dt_from_source(const char *fname)
David Gibsonf0517db2005-07-15 17:14:24 +100030{
31 the_boot_info = NULL;
32
Jon Loeligere45e6fd2007-03-23 15:18:41 -050033 push_input_file(fname);
34
David Gibsonfc14dad2005-06-08 17:18:34 +100035 if (yyparse() != 0)
36 return NULL;
37
David Gibsonf0517db2005-07-15 17:14:24 +100038 fill_fullpaths(the_boot_info->dt, "");
David Gibsonfc14dad2005-06-08 17:18:34 +100039
David Gibsonf0517db2005-07-15 17:14:24 +100040 return the_boot_info;
David Gibsonfc14dad2005-06-08 17:18:34 +100041}
42
43static void write_prefix(FILE *f, int level)
44{
45 int i;
46
47 for (i = 0; i < level; i++)
48 fputc('\t', f);
49}
50
David Gibson5a98ddd2007-10-17 12:39:10 +100051int isstring(char c)
52{
53 return (isprint(c)
54 || (c == '\0')
55 || strchr("\a\b\t\n\v\f\r", c));
56}
57
David Gibson5a98ddd2007-10-17 12:39:10 +100058static void write_propval_string(FILE *f, struct data val)
59{
David Gibson92cb9a22007-12-04 14:26:15 +110060 const char *str = val.val;
David Gibson5a98ddd2007-10-17 12:39:10 +100061 int i;
David Gibsond3ea6e52007-11-07 10:22:25 +110062 int newchunk = 1;
David Gibsondc941772007-11-22 14:39:23 +110063 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +100064
65 assert(str[val.len-1] == '\0');
66
David Gibson5a98ddd2007-10-17 12:39:10 +100067 for (i = 0; i < (val.len-1); i++) {
68 char c = str[i];
69
David Gibsond3ea6e52007-11-07 10:22:25 +110070 if (newchunk) {
David Gibsondc941772007-11-22 14:39:23 +110071 while (m && (m->offset <= i)) {
72 if (m->type == LABEL) {
73 assert(m->offset == i);
74 fprintf(f, "%s: ", m->ref);
75 }
76 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +110077 }
78 fprintf(f, "\"");
79 newchunk = 0;
80 }
81
David Gibson5a98ddd2007-10-17 12:39:10 +100082 switch (c) {
83 case '\a':
84 fprintf(f, "\\a");
85 break;
86 case '\b':
87 fprintf(f, "\\b");
88 break;
89 case '\t':
90 fprintf(f, "\\t");
91 break;
92 case '\n':
93 fprintf(f, "\\n");
94 break;
95 case '\v':
96 fprintf(f, "\\v");
97 break;
98 case '\f':
99 fprintf(f, "\\f");
100 break;
101 case '\r':
102 fprintf(f, "\\r");
103 break;
104 case '\\':
105 fprintf(f, "\\\\");
106 break;
107 case '\"':
108 fprintf(f, "\\\"");
109 break;
110 case '\0':
David Gibsond3ea6e52007-11-07 10:22:25 +1100111 fprintf(f, "\", ");
112 newchunk = 1;
David Gibson5a98ddd2007-10-17 12:39:10 +1000113 break;
114 default:
115 if (isprint(c))
116 fprintf(f, "%c", c);
117 else
118 fprintf(f, "\\x%02hhx", c);
119 }
120 }
David Gibsoned01ae42007-11-07 10:21:20 +1100121 fprintf(f, "\"");
David Gibsond3ea6e52007-11-07 10:22:25 +1100122
123 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100124 for_each_marker_of_type(m, LABEL) {
125 assert (m->offset == val.len);
126 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100127 }
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 Gibsondc941772007-11-22 14:39:23 +1100134 struct marker *m = val.markers;
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 Gibsondc941772007-11-22 14:39:23 +1100138 while (m && (m->offset <= ((char *)cp - val.val))) {
139 if (m->type == LABEL) {
140 assert(m->offset == ((char *)cp - val.val));
141 fprintf(f, "%s: ", m->ref);
142 }
143 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100144 }
145
David Gibson91967ac2007-11-07 11:17:37 +1100146 fprintf(f, "0x%x", be32_to_cpu(*cp++));
David Gibson5a98ddd2007-10-17 12:39:10 +1000147 if ((void *)cp >= propend)
148 break;
149 fprintf(f, " ");
150 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100151
152 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100153 for_each_marker_of_type(m, LABEL) {
154 assert (m->offset == val.len);
155 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100156 }
David Gibsoned01ae42007-11-07 10:21:20 +1100157 fprintf(f, ">");
David Gibson5a98ddd2007-10-17 12:39:10 +1000158}
159
160static void write_propval_bytes(FILE *f, struct data val)
161{
162 void *propend = val.val + val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100163 const char *bp = val.val;
David Gibsondc941772007-11-22 14:39:23 +1100164 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +1000165
David Gibsoned01ae42007-11-07 10:21:20 +1100166 fprintf(f, "[");
David Gibson5a98ddd2007-10-17 12:39:10 +1000167 for (;;) {
David Gibsondc941772007-11-22 14:39:23 +1100168 while (m && (m->offset == (bp-val.val))) {
169 if (m->type == LABEL)
170 fprintf(f, "%s: ", m->ref);
171 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100172 }
173
David Gibson5a98ddd2007-10-17 12:39:10 +1000174 fprintf(f, "%02hhx", *bp++);
175 if ((void *)bp >= propend)
176 break;
177 fprintf(f, " ");
178 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100179
180 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100181 for_each_marker_of_type(m, LABEL) {
182 assert (m->offset == val.len);
183 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100184 }
David Gibsoned01ae42007-11-07 10:21:20 +1100185 fprintf(f, "]");
186}
187
188static void write_propval(FILE *f, struct property *prop)
189{
190 int len = prop->val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100191 const char *p = prop->val.val;
David Gibsondc941772007-11-22 14:39:23 +1100192 struct marker *m = prop->val.markers;
David Gibsoned01ae42007-11-07 10:21:20 +1100193 int nnotstring = 0, nnul = 0;
David Gibsond3ea6e52007-11-07 10:22:25 +1100194 int nnotstringlbl = 0, nnotcelllbl = 0;
David Gibsoned01ae42007-11-07 10:21:20 +1100195 int i;
196
197 if (len == 0) {
198 fprintf(f, ";\n");
199 return;
200 }
201
202 for (i = 0; i < len; i++) {
203 if (! isstring(p[i]))
204 nnotstring++;
205 if (p[i] == '\0')
206 nnul++;
207 }
208
David Gibsondc941772007-11-22 14:39:23 +1100209 for_each_marker_of_type(m, LABEL) {
210 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
David Gibsond3ea6e52007-11-07 10:22:25 +1100211 nnotstringlbl++;
David Gibsondc941772007-11-22 14:39:23 +1100212 if ((m->offset % sizeof(cell_t)) != 0)
David Gibsond3ea6e52007-11-07 10:22:25 +1100213 nnotcelllbl++;
214 }
David Gibsoned01ae42007-11-07 10:21:20 +1100215
David Gibsond3ea6e52007-11-07 10:22:25 +1100216 fprintf(f, " = ");
217 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
218 && (nnotstringlbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100219 write_propval_string(f, prop->val);
David Gibsond3ea6e52007-11-07 10:22:25 +1100220 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100221 write_propval_cells(f, prop->val);
222 } else {
223 write_propval_bytes(f, prop->val);
224 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100225
David Gibsoned01ae42007-11-07 10:21:20 +1100226 fprintf(f, ";\n");
David Gibson5a98ddd2007-10-17 12:39:10 +1000227}
David Gibsonf0517db2005-07-15 17:14:24 +1000228
David Gibson230f2532005-08-29 12:48:02 +1000229static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +1000230{
231 struct property *prop;
232 struct node *child;
233
234 write_prefix(f, level);
David Gibson02563ad2007-11-02 16:10:30 +1100235 if (tree->label)
236 fprintf(f, "%s: ", tree->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000237 if (tree->name && (*tree->name))
238 fprintf(f, "%s {\n", tree->name);
239 else
240 fprintf(f, "/ {\n");
241
242 for_each_property(tree, prop) {
David Gibson02563ad2007-11-02 16:10:30 +1100243 write_prefix(f, level+1);
244 if (prop->label)
245 fprintf(f, "%s: ", prop->label);
246 fprintf(f, "%s", prop->name);
David Gibsoned01ae42007-11-07 10:21:20 +1100247 write_propval(f, prop);
David Gibsonfc14dad2005-06-08 17:18:34 +1000248 }
249 for_each_child(tree, child) {
250 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000251 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000252 }
253 write_prefix(f, level);
254 fprintf(f, "};\n");
255}
David Gibsonf0517db2005-07-15 17:14:24 +1000256
257
David Gibson712e52e2005-10-26 16:56:26 +1000258void dt_to_source(FILE *f, struct boot_info *bi)
David Gibsonf0517db2005-07-15 17:14:24 +1000259{
David Gibsonf040d952005-10-24 18:18:38 +1000260 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000261
David Gibson91967ac2007-11-07 11:17:37 +1100262 fprintf(f, "/dts-v1/;\n\n");
263
David Gibsonf040d952005-10-24 18:18:38 +1000264 for (re = bi->reservelist; re; re = re->next) {
David Gibson02563ad2007-11-02 16:10:30 +1100265 if (re->label)
266 fprintf(f, "%s: ", re->label);
David Gibson91967ac2007-11-07 11:17:37 +1100267 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
David Gibsonf040d952005-10-24 18:18:38 +1000268 (unsigned long long)re->re.address,
David Gibson91967ac2007-11-07 11:17:37 +1100269 (unsigned long long)re->re.size);
David Gibsonf0517db2005-07-15 17:14:24 +1000270 }
271
272 write_tree_source_node(f, bi->dt, 0);
273}
274