blob: 3c24e7fa435e253172b7d33f4ebdc0e70e2e23e6 [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
David Gibson8a88ad82008-03-06 12:45:41 +110035 srcpos_file = dtc_open_file(fname, NULL);
36 yyin = srcpos_file->file;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050037
David Gibsonfc14dad2005-06-08 17:18:34 +100038 if (yyparse() != 0)
39 return NULL;
40
Scott Woodad4f54a2008-01-03 17:43:33 -060041 the_boot_info->error = treesource_error;
David Gibsonf0517db2005-07-15 17:14:24 +100042 return the_boot_info;
David Gibsonfc14dad2005-06-08 17:18:34 +100043}
44
45static void write_prefix(FILE *f, int level)
46{
47 int i;
48
49 for (i = 0; i < level; i++)
50 fputc('\t', f);
51}
52
David Gibson5a98ddd2007-10-17 12:39:10 +100053int isstring(char c)
54{
55 return (isprint(c)
56 || (c == '\0')
57 || strchr("\a\b\t\n\v\f\r", c));
58}
59
David Gibson5a98ddd2007-10-17 12:39:10 +100060static void write_propval_string(FILE *f, struct data val)
61{
David Gibson92cb9a22007-12-04 14:26:15 +110062 const char *str = val.val;
David Gibson5a98ddd2007-10-17 12:39:10 +100063 int i;
David Gibsond3ea6e52007-11-07 10:22:25 +110064 int newchunk = 1;
David Gibsondc941772007-11-22 14:39:23 +110065 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +100066
67 assert(str[val.len-1] == '\0');
68
David Gibson5a98ddd2007-10-17 12:39:10 +100069 for (i = 0; i < (val.len-1); i++) {
70 char c = str[i];
71
David Gibsond3ea6e52007-11-07 10:22:25 +110072 if (newchunk) {
David Gibsondc941772007-11-22 14:39:23 +110073 while (m && (m->offset <= i)) {
74 if (m->type == LABEL) {
75 assert(m->offset == i);
76 fprintf(f, "%s: ", m->ref);
77 }
78 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +110079 }
80 fprintf(f, "\"");
81 newchunk = 0;
82 }
83
David Gibson5a98ddd2007-10-17 12:39:10 +100084 switch (c) {
85 case '\a':
86 fprintf(f, "\\a");
87 break;
88 case '\b':
89 fprintf(f, "\\b");
90 break;
91 case '\t':
92 fprintf(f, "\\t");
93 break;
94 case '\n':
95 fprintf(f, "\\n");
96 break;
97 case '\v':
98 fprintf(f, "\\v");
99 break;
100 case '\f':
101 fprintf(f, "\\f");
102 break;
103 case '\r':
104 fprintf(f, "\\r");
105 break;
106 case '\\':
107 fprintf(f, "\\\\");
108 break;
109 case '\"':
110 fprintf(f, "\\\"");
111 break;
112 case '\0':
David Gibsond3ea6e52007-11-07 10:22:25 +1100113 fprintf(f, "\", ");
114 newchunk = 1;
David Gibson5a98ddd2007-10-17 12:39:10 +1000115 break;
116 default:
117 if (isprint(c))
118 fprintf(f, "%c", c);
119 else
120 fprintf(f, "\\x%02hhx", c);
121 }
122 }
David Gibsoned01ae42007-11-07 10:21:20 +1100123 fprintf(f, "\"");
David Gibsond3ea6e52007-11-07 10:22:25 +1100124
125 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100126 for_each_marker_of_type(m, LABEL) {
127 assert (m->offset == val.len);
128 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100129 }
David Gibson5a98ddd2007-10-17 12:39:10 +1000130}
131
132static void write_propval_cells(FILE *f, struct data val)
133{
134 void *propend = val.val + val.len;
135 cell_t *cp = (cell_t *)val.val;
David Gibsondc941772007-11-22 14:39:23 +1100136 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +1000137
David Gibsoned01ae42007-11-07 10:21:20 +1100138 fprintf(f, "<");
David Gibson5a98ddd2007-10-17 12:39:10 +1000139 for (;;) {
David Gibsondc941772007-11-22 14:39:23 +1100140 while (m && (m->offset <= ((char *)cp - val.val))) {
141 if (m->type == LABEL) {
142 assert(m->offset == ((char *)cp - val.val));
143 fprintf(f, "%s: ", m->ref);
144 }
145 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100146 }
147
David Gibson91967ac2007-11-07 11:17:37 +1100148 fprintf(f, "0x%x", be32_to_cpu(*cp++));
David Gibson5a98ddd2007-10-17 12:39:10 +1000149 if ((void *)cp >= propend)
150 break;
151 fprintf(f, " ");
152 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100153
154 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100155 for_each_marker_of_type(m, LABEL) {
156 assert (m->offset == val.len);
157 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100158 }
David Gibsoned01ae42007-11-07 10:21:20 +1100159 fprintf(f, ">");
David Gibson5a98ddd2007-10-17 12:39:10 +1000160}
161
162static void write_propval_bytes(FILE *f, struct data val)
163{
164 void *propend = val.val + val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100165 const char *bp = val.val;
David Gibsondc941772007-11-22 14:39:23 +1100166 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +1000167
David Gibsoned01ae42007-11-07 10:21:20 +1100168 fprintf(f, "[");
David Gibson5a98ddd2007-10-17 12:39:10 +1000169 for (;;) {
David Gibsondc941772007-11-22 14:39:23 +1100170 while (m && (m->offset == (bp-val.val))) {
171 if (m->type == LABEL)
172 fprintf(f, "%s: ", m->ref);
173 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100174 }
175
David Gibson5a98ddd2007-10-17 12:39:10 +1000176 fprintf(f, "%02hhx", *bp++);
177 if ((void *)bp >= propend)
178 break;
179 fprintf(f, " ");
180 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100181
182 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100183 for_each_marker_of_type(m, LABEL) {
184 assert (m->offset == val.len);
185 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100186 }
David Gibsoned01ae42007-11-07 10:21:20 +1100187 fprintf(f, "]");
188}
189
190static void write_propval(FILE *f, struct property *prop)
191{
192 int len = prop->val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100193 const char *p = prop->val.val;
David Gibsondc941772007-11-22 14:39:23 +1100194 struct marker *m = prop->val.markers;
David Gibsoned01ae42007-11-07 10:21:20 +1100195 int nnotstring = 0, nnul = 0;
David Gibsond3ea6e52007-11-07 10:22:25 +1100196 int nnotstringlbl = 0, nnotcelllbl = 0;
David Gibsoned01ae42007-11-07 10:21:20 +1100197 int i;
198
199 if (len == 0) {
200 fprintf(f, ";\n");
201 return;
202 }
203
204 for (i = 0; i < len; i++) {
205 if (! isstring(p[i]))
206 nnotstring++;
207 if (p[i] == '\0')
208 nnul++;
209 }
210
David Gibsondc941772007-11-22 14:39:23 +1100211 for_each_marker_of_type(m, LABEL) {
212 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
David Gibsond3ea6e52007-11-07 10:22:25 +1100213 nnotstringlbl++;
David Gibsondc941772007-11-22 14:39:23 +1100214 if ((m->offset % sizeof(cell_t)) != 0)
David Gibsond3ea6e52007-11-07 10:22:25 +1100215 nnotcelllbl++;
216 }
David Gibsoned01ae42007-11-07 10:21:20 +1100217
David Gibsond3ea6e52007-11-07 10:22:25 +1100218 fprintf(f, " = ");
219 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
220 && (nnotstringlbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100221 write_propval_string(f, prop->val);
David Gibsond3ea6e52007-11-07 10:22:25 +1100222 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100223 write_propval_cells(f, prop->val);
224 } else {
225 write_propval_bytes(f, prop->val);
226 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100227
David Gibsoned01ae42007-11-07 10:21:20 +1100228 fprintf(f, ";\n");
David Gibson5a98ddd2007-10-17 12:39:10 +1000229}
David Gibsonf0517db2005-07-15 17:14:24 +1000230
David Gibson230f2532005-08-29 12:48:02 +1000231static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +1000232{
233 struct property *prop;
234 struct node *child;
235
236 write_prefix(f, level);
David Gibson02563ad2007-11-02 16:10:30 +1100237 if (tree->label)
238 fprintf(f, "%s: ", tree->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000239 if (tree->name && (*tree->name))
240 fprintf(f, "%s {\n", tree->name);
241 else
242 fprintf(f, "/ {\n");
243
244 for_each_property(tree, prop) {
David Gibson02563ad2007-11-02 16:10:30 +1100245 write_prefix(f, level+1);
246 if (prop->label)
247 fprintf(f, "%s: ", prop->label);
248 fprintf(f, "%s", prop->name);
David Gibsoned01ae42007-11-07 10:21:20 +1100249 write_propval(f, prop);
David Gibsonfc14dad2005-06-08 17:18:34 +1000250 }
251 for_each_child(tree, child) {
252 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000253 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000254 }
255 write_prefix(f, level);
256 fprintf(f, "};\n");
257}
David Gibsonf0517db2005-07-15 17:14:24 +1000258
259
David Gibson712e52e2005-10-26 16:56:26 +1000260void dt_to_source(FILE *f, struct boot_info *bi)
David Gibsonf0517db2005-07-15 17:14:24 +1000261{
David Gibsonf040d952005-10-24 18:18:38 +1000262 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000263
David Gibson91967ac2007-11-07 11:17:37 +1100264 fprintf(f, "/dts-v1/;\n\n");
265
David Gibsonf040d952005-10-24 18:18:38 +1000266 for (re = bi->reservelist; re; re = re->next) {
David Gibson02563ad2007-11-02 16:10:30 +1100267 if (re->label)
268 fprintf(f, "%s: ", re->label);
David Gibson91967ac2007-11-07 11:17:37 +1100269 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
David Gibsonf040d952005-10-24 18:18:38 +1000270 (unsigned long long)re->re.address,
David Gibson91967ac2007-11-07 11:17:37 +1100271 (unsigned long long)re->re.size);
David Gibsonf0517db2005-07-15 17:14:24 +1000272 }
273
274 write_tree_source_node(f, bi->dt, 0);
275}
276