blob: a6a7767976364498a99b1a3c6c7d4d8f6071446a [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{
David Gibson92cb9a22007-12-04 14:26:15 +110061 const char *str = val.val;
David Gibson5a98ddd2007-10-17 12:39:10 +100062 int i;
David Gibsond3ea6e52007-11-07 10:22:25 +110063 int newchunk = 1;
David Gibsondc941772007-11-22 14:39:23 +110064 struct marker *m = val.markers;
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) {
David Gibsondc941772007-11-22 14:39:23 +110072 while (m && (m->offset <= i)) {
73 if (m->type == LABEL) {
74 assert(m->offset == i);
75 fprintf(f, "%s: ", m->ref);
76 }
77 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +110078 }
79 fprintf(f, "\"");
80 newchunk = 0;
81 }
82
David Gibson5a98ddd2007-10-17 12:39:10 +100083 switch (c) {
84 case '\a':
85 fprintf(f, "\\a");
86 break;
87 case '\b':
88 fprintf(f, "\\b");
89 break;
90 case '\t':
91 fprintf(f, "\\t");
92 break;
93 case '\n':
94 fprintf(f, "\\n");
95 break;
96 case '\v':
97 fprintf(f, "\\v");
98 break;
99 case '\f':
100 fprintf(f, "\\f");
101 break;
102 case '\r':
103 fprintf(f, "\\r");
104 break;
105 case '\\':
106 fprintf(f, "\\\\");
107 break;
108 case '\"':
109 fprintf(f, "\\\"");
110 break;
111 case '\0':
David Gibsond3ea6e52007-11-07 10:22:25 +1100112 fprintf(f, "\", ");
113 newchunk = 1;
David Gibson5a98ddd2007-10-17 12:39:10 +1000114 break;
115 default:
116 if (isprint(c))
117 fprintf(f, "%c", c);
118 else
119 fprintf(f, "\\x%02hhx", c);
120 }
121 }
David Gibsoned01ae42007-11-07 10:21:20 +1100122 fprintf(f, "\"");
David Gibsond3ea6e52007-11-07 10:22:25 +1100123
124 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100125 for_each_marker_of_type(m, LABEL) {
126 assert (m->offset == val.len);
127 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100128 }
David Gibson5a98ddd2007-10-17 12:39:10 +1000129}
130
131static void write_propval_cells(FILE *f, struct data val)
132{
133 void *propend = val.val + val.len;
134 cell_t *cp = (cell_t *)val.val;
David Gibsondc941772007-11-22 14:39:23 +1100135 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +1000136
David Gibsoned01ae42007-11-07 10:21:20 +1100137 fprintf(f, "<");
David Gibson5a98ddd2007-10-17 12:39:10 +1000138 for (;;) {
David Gibsondc941772007-11-22 14:39:23 +1100139 while (m && (m->offset <= ((char *)cp - val.val))) {
140 if (m->type == LABEL) {
141 assert(m->offset == ((char *)cp - val.val));
142 fprintf(f, "%s: ", m->ref);
143 }
144 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100145 }
146
David Gibson91967ac2007-11-07 11:17:37 +1100147 fprintf(f, "0x%x", be32_to_cpu(*cp++));
David Gibson5a98ddd2007-10-17 12:39:10 +1000148 if ((void *)cp >= propend)
149 break;
150 fprintf(f, " ");
151 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100152
153 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100154 for_each_marker_of_type(m, LABEL) {
155 assert (m->offset == val.len);
156 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100157 }
David Gibsoned01ae42007-11-07 10:21:20 +1100158 fprintf(f, ">");
David Gibson5a98ddd2007-10-17 12:39:10 +1000159}
160
161static void write_propval_bytes(FILE *f, struct data val)
162{
163 void *propend = val.val + val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100164 const char *bp = val.val;
David Gibsondc941772007-11-22 14:39:23 +1100165 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +1000166
David Gibsoned01ae42007-11-07 10:21:20 +1100167 fprintf(f, "[");
David Gibson5a98ddd2007-10-17 12:39:10 +1000168 for (;;) {
David Gibsondc941772007-11-22 14:39:23 +1100169 while (m && (m->offset == (bp-val.val))) {
170 if (m->type == LABEL)
171 fprintf(f, "%s: ", m->ref);
172 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100173 }
174
David Gibson5a98ddd2007-10-17 12:39:10 +1000175 fprintf(f, "%02hhx", *bp++);
176 if ((void *)bp >= propend)
177 break;
178 fprintf(f, " ");
179 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100180
181 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100182 for_each_marker_of_type(m, LABEL) {
183 assert (m->offset == val.len);
184 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100185 }
David Gibsoned01ae42007-11-07 10:21:20 +1100186 fprintf(f, "]");
187}
188
189static void write_propval(FILE *f, struct property *prop)
190{
191 int len = prop->val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100192 const char *p = prop->val.val;
David Gibsondc941772007-11-22 14:39:23 +1100193 struct marker *m = prop->val.markers;
David Gibsoned01ae42007-11-07 10:21:20 +1100194 int nnotstring = 0, nnul = 0;
David Gibsond3ea6e52007-11-07 10:22:25 +1100195 int nnotstringlbl = 0, nnotcelllbl = 0;
David Gibsoned01ae42007-11-07 10:21:20 +1100196 int i;
197
198 if (len == 0) {
199 fprintf(f, ";\n");
200 return;
201 }
202
203 for (i = 0; i < len; i++) {
204 if (! isstring(p[i]))
205 nnotstring++;
206 if (p[i] == '\0')
207 nnul++;
208 }
209
David Gibsondc941772007-11-22 14:39:23 +1100210 for_each_marker_of_type(m, LABEL) {
211 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
David Gibsond3ea6e52007-11-07 10:22:25 +1100212 nnotstringlbl++;
David Gibsondc941772007-11-22 14:39:23 +1100213 if ((m->offset % sizeof(cell_t)) != 0)
David Gibsond3ea6e52007-11-07 10:22:25 +1100214 nnotcelllbl++;
215 }
David Gibsoned01ae42007-11-07 10:21:20 +1100216
David Gibsond3ea6e52007-11-07 10:22:25 +1100217 fprintf(f, " = ");
218 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
219 && (nnotstringlbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100220 write_propval_string(f, prop->val);
David Gibsond3ea6e52007-11-07 10:22:25 +1100221 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100222 write_propval_cells(f, prop->val);
223 } else {
224 write_propval_bytes(f, prop->val);
225 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100226
David Gibsoned01ae42007-11-07 10:21:20 +1100227 fprintf(f, ";\n");
David Gibson5a98ddd2007-10-17 12:39:10 +1000228}
David Gibsonf0517db2005-07-15 17:14:24 +1000229
David Gibson230f2532005-08-29 12:48:02 +1000230static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +1000231{
232 struct property *prop;
233 struct node *child;
234
235 write_prefix(f, level);
David Gibson02563ad2007-11-02 16:10:30 +1100236 if (tree->label)
237 fprintf(f, "%s: ", tree->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000238 if (tree->name && (*tree->name))
239 fprintf(f, "%s {\n", tree->name);
240 else
241 fprintf(f, "/ {\n");
242
243 for_each_property(tree, prop) {
David Gibson02563ad2007-11-02 16:10:30 +1100244 write_prefix(f, level+1);
245 if (prop->label)
246 fprintf(f, "%s: ", prop->label);
247 fprintf(f, "%s", prop->name);
David Gibsoned01ae42007-11-07 10:21:20 +1100248 write_propval(f, prop);
David Gibsonfc14dad2005-06-08 17:18:34 +1000249 }
250 for_each_child(tree, child) {
251 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000252 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000253 }
254 write_prefix(f, level);
255 fprintf(f, "};\n");
256}
David Gibsonf0517db2005-07-15 17:14:24 +1000257
258
David Gibson712e52e2005-10-26 16:56:26 +1000259void dt_to_source(FILE *f, struct boot_info *bi)
David Gibsonf0517db2005-07-15 17:14:24 +1000260{
David Gibsonf040d952005-10-24 18:18:38 +1000261 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000262
David Gibson91967ac2007-11-07 11:17:37 +1100263 fprintf(f, "/dts-v1/;\n\n");
264
David Gibsonf040d952005-10-24 18:18:38 +1000265 for (re = bi->reservelist; re; re = re->next) {
David Gibson02563ad2007-11-02 16:10:30 +1100266 if (re->label)
267 fprintf(f, "%s: ", re->label);
David Gibson91967ac2007-11-07 11:17:37 +1100268 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
David Gibsonf040d952005-10-24 18:18:38 +1000269 (unsigned long long)re->re.address,
David Gibson91967ac2007-11-07 11:17:37 +1100270 (unsigned long long)re->re.size);
David Gibsonf0517db2005-07-15 17:14:24 +1000271 }
272
273 write_tree_source_node(f, bi->dt, 0);
274}
275