blob: 980bda70b18a5fdf755fe2111e198db56d0733f4 [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;
Scott Woodad4f54a2008-01-03 17:43:33 -060028int treesource_error;
David Gibsonf0517db2005-07-15 17:14:24 +100029
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;
Scott Woodad4f54a2008-01-03 17:43:33 -060033 treesource_error = 0;
David Gibsonf0517db2005-07-15 17:14:24 +100034
Jon Loeligere45e6fd2007-03-23 15:18:41 -050035 push_input_file(fname);
36
David Gibsonfc14dad2005-06-08 17:18:34 +100037 if (yyparse() != 0)
38 return NULL;
39
David Gibsonf0517db2005-07-15 17:14:24 +100040 fill_fullpaths(the_boot_info->dt, "");
David Gibsonfc14dad2005-06-08 17:18:34 +100041
Scott Woodad4f54a2008-01-03 17:43:33 -060042 the_boot_info->error = treesource_error;
David Gibsonf0517db2005-07-15 17:14:24 +100043 return the_boot_info;
David Gibsonfc14dad2005-06-08 17:18:34 +100044}
45
46static void write_prefix(FILE *f, int level)
47{
48 int i;
49
50 for (i = 0; i < level; i++)
51 fputc('\t', f);
52}
53
David Gibson5a98ddd2007-10-17 12:39:10 +100054int isstring(char c)
55{
56 return (isprint(c)
57 || (c == '\0')
58 || strchr("\a\b\t\n\v\f\r", c));
59}
60
David Gibson5a98ddd2007-10-17 12:39:10 +100061static void write_propval_string(FILE *f, struct data val)
62{
David Gibson92cb9a22007-12-04 14:26:15 +110063 const char *str = val.val;
David Gibson5a98ddd2007-10-17 12:39:10 +100064 int i;
David Gibsond3ea6e52007-11-07 10:22:25 +110065 int newchunk = 1;
David Gibsondc941772007-11-22 14:39:23 +110066 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +100067
68 assert(str[val.len-1] == '\0');
69
David Gibson5a98ddd2007-10-17 12:39:10 +100070 for (i = 0; i < (val.len-1); i++) {
71 char c = str[i];
72
David Gibsond3ea6e52007-11-07 10:22:25 +110073 if (newchunk) {
David Gibsondc941772007-11-22 14:39:23 +110074 while (m && (m->offset <= i)) {
75 if (m->type == LABEL) {
76 assert(m->offset == i);
77 fprintf(f, "%s: ", m->ref);
78 }
79 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +110080 }
81 fprintf(f, "\"");
82 newchunk = 0;
83 }
84
David Gibson5a98ddd2007-10-17 12:39:10 +100085 switch (c) {
86 case '\a':
87 fprintf(f, "\\a");
88 break;
89 case '\b':
90 fprintf(f, "\\b");
91 break;
92 case '\t':
93 fprintf(f, "\\t");
94 break;
95 case '\n':
96 fprintf(f, "\\n");
97 break;
98 case '\v':
99 fprintf(f, "\\v");
100 break;
101 case '\f':
102 fprintf(f, "\\f");
103 break;
104 case '\r':
105 fprintf(f, "\\r");
106 break;
107 case '\\':
108 fprintf(f, "\\\\");
109 break;
110 case '\"':
111 fprintf(f, "\\\"");
112 break;
113 case '\0':
David Gibsond3ea6e52007-11-07 10:22:25 +1100114 fprintf(f, "\", ");
115 newchunk = 1;
David Gibson5a98ddd2007-10-17 12:39:10 +1000116 break;
117 default:
118 if (isprint(c))
119 fprintf(f, "%c", c);
120 else
121 fprintf(f, "\\x%02hhx", c);
122 }
123 }
David Gibsoned01ae42007-11-07 10:21:20 +1100124 fprintf(f, "\"");
David Gibsond3ea6e52007-11-07 10:22:25 +1100125
126 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100127 for_each_marker_of_type(m, LABEL) {
128 assert (m->offset == val.len);
129 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100130 }
David Gibson5a98ddd2007-10-17 12:39:10 +1000131}
132
133static void write_propval_cells(FILE *f, struct data val)
134{
135 void *propend = val.val + val.len;
136 cell_t *cp = (cell_t *)val.val;
David Gibsondc941772007-11-22 14:39:23 +1100137 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +1000138
David Gibsoned01ae42007-11-07 10:21:20 +1100139 fprintf(f, "<");
David Gibson5a98ddd2007-10-17 12:39:10 +1000140 for (;;) {
David Gibsondc941772007-11-22 14:39:23 +1100141 while (m && (m->offset <= ((char *)cp - val.val))) {
142 if (m->type == LABEL) {
143 assert(m->offset == ((char *)cp - val.val));
144 fprintf(f, "%s: ", m->ref);
145 }
146 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100147 }
148
David Gibson91967ac2007-11-07 11:17:37 +1100149 fprintf(f, "0x%x", be32_to_cpu(*cp++));
David Gibson5a98ddd2007-10-17 12:39:10 +1000150 if ((void *)cp >= propend)
151 break;
152 fprintf(f, " ");
153 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100154
155 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100156 for_each_marker_of_type(m, LABEL) {
157 assert (m->offset == val.len);
158 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100159 }
David Gibsoned01ae42007-11-07 10:21:20 +1100160 fprintf(f, ">");
David Gibson5a98ddd2007-10-17 12:39:10 +1000161}
162
163static void write_propval_bytes(FILE *f, struct data val)
164{
165 void *propend = val.val + val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100166 const char *bp = val.val;
David Gibsondc941772007-11-22 14:39:23 +1100167 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +1000168
David Gibsoned01ae42007-11-07 10:21:20 +1100169 fprintf(f, "[");
David Gibson5a98ddd2007-10-17 12:39:10 +1000170 for (;;) {
David Gibsondc941772007-11-22 14:39:23 +1100171 while (m && (m->offset == (bp-val.val))) {
172 if (m->type == LABEL)
173 fprintf(f, "%s: ", m->ref);
174 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100175 }
176
David Gibson5a98ddd2007-10-17 12:39:10 +1000177 fprintf(f, "%02hhx", *bp++);
178 if ((void *)bp >= propend)
179 break;
180 fprintf(f, " ");
181 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100182
183 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100184 for_each_marker_of_type(m, LABEL) {
185 assert (m->offset == val.len);
186 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100187 }
David Gibsoned01ae42007-11-07 10:21:20 +1100188 fprintf(f, "]");
189}
190
191static void write_propval(FILE *f, struct property *prop)
192{
193 int len = prop->val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100194 const char *p = prop->val.val;
David Gibsondc941772007-11-22 14:39:23 +1100195 struct marker *m = prop->val.markers;
David Gibsoned01ae42007-11-07 10:21:20 +1100196 int nnotstring = 0, nnul = 0;
David Gibsond3ea6e52007-11-07 10:22:25 +1100197 int nnotstringlbl = 0, nnotcelllbl = 0;
David Gibsoned01ae42007-11-07 10:21:20 +1100198 int i;
199
200 if (len == 0) {
201 fprintf(f, ";\n");
202 return;
203 }
204
205 for (i = 0; i < len; i++) {
206 if (! isstring(p[i]))
207 nnotstring++;
208 if (p[i] == '\0')
209 nnul++;
210 }
211
David Gibsondc941772007-11-22 14:39:23 +1100212 for_each_marker_of_type(m, LABEL) {
213 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
David Gibsond3ea6e52007-11-07 10:22:25 +1100214 nnotstringlbl++;
David Gibsondc941772007-11-22 14:39:23 +1100215 if ((m->offset % sizeof(cell_t)) != 0)
David Gibsond3ea6e52007-11-07 10:22:25 +1100216 nnotcelllbl++;
217 }
David Gibsoned01ae42007-11-07 10:21:20 +1100218
David Gibsond3ea6e52007-11-07 10:22:25 +1100219 fprintf(f, " = ");
220 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
221 && (nnotstringlbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100222 write_propval_string(f, prop->val);
David Gibsond3ea6e52007-11-07 10:22:25 +1100223 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100224 write_propval_cells(f, prop->val);
225 } else {
226 write_propval_bytes(f, prop->val);
227 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100228
David Gibsoned01ae42007-11-07 10:21:20 +1100229 fprintf(f, ";\n");
David Gibson5a98ddd2007-10-17 12:39:10 +1000230}
David Gibsonf0517db2005-07-15 17:14:24 +1000231
David Gibson230f2532005-08-29 12:48:02 +1000232static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +1000233{
234 struct property *prop;
235 struct node *child;
236
237 write_prefix(f, level);
David Gibson02563ad2007-11-02 16:10:30 +1100238 if (tree->label)
239 fprintf(f, "%s: ", tree->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000240 if (tree->name && (*tree->name))
241 fprintf(f, "%s {\n", tree->name);
242 else
243 fprintf(f, "/ {\n");
244
245 for_each_property(tree, prop) {
David Gibson02563ad2007-11-02 16:10:30 +1100246 write_prefix(f, level+1);
247 if (prop->label)
248 fprintf(f, "%s: ", prop->label);
249 fprintf(f, "%s", prop->name);
David Gibsoned01ae42007-11-07 10:21:20 +1100250 write_propval(f, prop);
David Gibsonfc14dad2005-06-08 17:18:34 +1000251 }
252 for_each_child(tree, child) {
253 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000254 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000255 }
256 write_prefix(f, level);
257 fprintf(f, "};\n");
258}
David Gibsonf0517db2005-07-15 17:14:24 +1000259
260
David Gibson712e52e2005-10-26 16:56:26 +1000261void dt_to_source(FILE *f, struct boot_info *bi)
David Gibsonf0517db2005-07-15 17:14:24 +1000262{
David Gibsonf040d952005-10-24 18:18:38 +1000263 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000264
David Gibson91967ac2007-11-07 11:17:37 +1100265 fprintf(f, "/dts-v1/;\n\n");
266
David Gibsonf040d952005-10-24 18:18:38 +1000267 for (re = bi->reservelist; re; re = re->next) {
David Gibson02563ad2007-11-02 16:10:30 +1100268 if (re->label)
269 fprintf(f, "%s: ", re->label);
David Gibson91967ac2007-11-07 11:17:37 +1100270 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
David Gibsonf040d952005-10-24 18:18:38 +1000271 (unsigned long long)re->re.address,
David Gibson91967ac2007-11-07 11:17:37 +1100272 (unsigned long long)re->re.size);
David Gibsonf0517db2005-07-15 17:14:24 +1000273 }
274
275 write_tree_source_node(f, bi->dt, 0);
276}
277