Blame view

scripts/dtc/treesource.c 5.91 KB
a4da2e3ec   David Gibson   [POWERPC] Merge d...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /*
   * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
   *
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
   * published by the Free Software Foundation; either version 2 of the
   * License, or (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   *  General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
   *                                                                   USA
   */
  
  #include "dtc.h"
  #include "srcpos.h"
  
  extern FILE *yyin;
  extern int yyparse(void);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
26
27
  
  struct boot_info *the_boot_info;
ed95d7450   David Gibson   powerpc: Update i...
28
  int treesource_error;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
29
30
31
32
  
  struct boot_info *dt_from_source(const char *fname)
  {
  	the_boot_info = NULL;
ed95d7450   David Gibson   powerpc: Update i...
33
  	treesource_error = 0;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
34

658f29a51   John Bonesio   of/flattree: Upda...
35
36
  	srcfile_push(fname);
  	yyin = current_srcfile->f;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
37
38
  
  	if (yyparse() != 0)
ed95d7450   David Gibson   powerpc: Update i...
39
40
  		die("Unable to parse input tree
  ");
a4da2e3ec   David Gibson   [POWERPC] Merge d...
41

ed95d7450   David Gibson   powerpc: Update i...
42
43
44
  	if (treesource_error)
  		die("Syntax error parsing input tree
  ");
a4da2e3ec   David Gibson   [POWERPC] Merge d...
45
46
47
48
49
50
51
52
53
54
55
  
  	return the_boot_info;
  }
  
  static void write_prefix(FILE *f, int level)
  {
  	int i;
  
  	for (i = 0; i < level; i++)
  		fputc('\t', f);
  }
5ccd99154   Josh Triplett   dtc: Mark various...
56
  static int isstring(char c)
a4da2e3ec   David Gibson   [POWERPC] Merge d...
57
58
59
60
61
62
63
64
65
66
67
  {
  	return (isprint(c)
  		|| (c == '\0')
  		|| strchr("\a\b\t
  \v\f\r", c));
  }
  
  static void write_propval_string(FILE *f, struct data val)
  {
  	const char *str = val.val;
  	int i;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
68
69
70
  	struct marker *m = val.markers;
  
  	assert(str[val.len-1] == '\0');
658f29a51   John Bonesio   of/flattree: Upda...
71
72
73
74
75
76
  	while (m && (m->offset == 0)) {
  		if (m->type == LABEL)
  			fprintf(f, "%s: ", m->ref);
  		m = m->next;
  	}
  	fprintf(f, "\"");
a4da2e3ec   David Gibson   [POWERPC] Merge d...
77
78
  	for (i = 0; i < (val.len-1); i++) {
  		char c = str[i];
a4da2e3ec   David Gibson   [POWERPC] Merge d...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  		switch (c) {
  		case '\a':
  			fprintf(f, "\\a");
  			break;
  		case '\b':
  			fprintf(f, "\\b");
  			break;
  		case '\t':
  			fprintf(f, "\\t");
  			break;
  		case '
  ':
  			fprintf(f, "\
  ");
  			break;
  		case '\v':
  			fprintf(f, "\\v");
  			break;
  		case '\f':
  			fprintf(f, "\\f");
  			break;
  		case '\r':
  			fprintf(f, "\\r");
  			break;
  		case '\\':
  			fprintf(f, "\\\\");
  			break;
  		case '\"':
  			fprintf(f, "\\\"");
  			break;
  		case '\0':
  			fprintf(f, "\", ");
658f29a51   John Bonesio   of/flattree: Upda...
111
112
113
114
115
116
117
118
  			while (m && (m->offset < i)) {
  				if (m->type == LABEL) {
  					assert(m->offset == (i+1));
  					fprintf(f, "%s: ", m->ref);
  				}
  				m = m->next;
  			}
  			fprintf(f, "\"");
a4da2e3ec   David Gibson   [POWERPC] Merge d...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  			break;
  		default:
  			if (isprint(c))
  				fprintf(f, "%c", c);
  			else
  				fprintf(f, "\\x%02hhx", c);
  		}
  	}
  	fprintf(f, "\"");
  
  	/* Wrap up any labels at the end of the value */
  	for_each_marker_of_type(m, LABEL) {
  		assert (m->offset == val.len);
  		fprintf(f, " %s:", m->ref);
  	}
  }
  
  static void write_propval_cells(FILE *f, struct data val)
  {
  	void *propend = val.val + val.len;
  	cell_t *cp = (cell_t *)val.val;
  	struct marker *m = val.markers;
  
  	fprintf(f, "<");
  	for (;;) {
  		while (m && (m->offset <= ((char *)cp - val.val))) {
  			if (m->type == LABEL) {
  				assert(m->offset == ((char *)cp - val.val));
  				fprintf(f, "%s: ", m->ref);
  			}
  			m = m->next;
  		}
ed95d7450   David Gibson   powerpc: Update i...
151
  		fprintf(f, "0x%x", fdt32_to_cpu(*cp++));
a4da2e3ec   David Gibson   [POWERPC] Merge d...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  		if ((void *)cp >= propend)
  			break;
  		fprintf(f, " ");
  	}
  
  	/* Wrap up any labels at the end of the value */
  	for_each_marker_of_type(m, LABEL) {
  		assert (m->offset == val.len);
  		fprintf(f, " %s:", m->ref);
  	}
  	fprintf(f, ">");
  }
  
  static void write_propval_bytes(FILE *f, struct data val)
  {
  	void *propend = val.val + val.len;
  	const char *bp = val.val;
  	struct marker *m = val.markers;
  
  	fprintf(f, "[");
  	for (;;) {
  		while (m && (m->offset == (bp-val.val))) {
  			if (m->type == LABEL)
  				fprintf(f, "%s: ", m->ref);
  			m = m->next;
  		}
  
  		fprintf(f, "%02hhx", *bp++);
ed95d7450   David Gibson   powerpc: Update i...
180
  		if ((const void *)bp >= propend)
a4da2e3ec   David Gibson   [POWERPC] Merge d...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  			break;
  		fprintf(f, " ");
  	}
  
  	/* Wrap up any labels at the end of the value */
  	for_each_marker_of_type(m, LABEL) {
  		assert (m->offset == val.len);
  		fprintf(f, " %s:", m->ref);
  	}
  	fprintf(f, "]");
  }
  
  static void write_propval(FILE *f, struct property *prop)
  {
  	int len = prop->val.len;
  	const char *p = prop->val.val;
  	struct marker *m = prop->val.markers;
  	int nnotstring = 0, nnul = 0;
  	int nnotstringlbl = 0, nnotcelllbl = 0;
  	int i;
  
  	if (len == 0) {
  		fprintf(f, ";
  ");
  		return;
  	}
  
  	for (i = 0; i < len; i++) {
  		if (! isstring(p[i]))
  			nnotstring++;
  		if (p[i] == '\0')
  			nnul++;
  	}
  
  	for_each_marker_of_type(m, LABEL) {
  		if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
  			nnotstringlbl++;
  		if ((m->offset % sizeof(cell_t)) != 0)
  			nnotcelllbl++;
  	}
  
  	fprintf(f, " = ");
  	if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
  	    && (nnotstringlbl == 0)) {
  		write_propval_string(f, prop->val);
  	} else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
  		write_propval_cells(f, prop->val);
  	} else {
  		write_propval_bytes(f, prop->val);
  	}
  
  	fprintf(f, ";
  ");
  }
  
  static void write_tree_source_node(FILE *f, struct node *tree, int level)
  {
  	struct property *prop;
  	struct node *child;
658f29a51   John Bonesio   of/flattree: Upda...
240
  	struct label *l;
a4da2e3ec   David Gibson   [POWERPC] Merge d...
241
242
  
  	write_prefix(f, level);
658f29a51   John Bonesio   of/flattree: Upda...
243
244
  	for_each_label(tree->labels, l)
  		fprintf(f, "%s: ", l->label);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
245
246
247
248
249
250
251
252
253
  	if (tree->name && (*tree->name))
  		fprintf(f, "%s {
  ", tree->name);
  	else
  		fprintf(f, "/ {
  ");
  
  	for_each_property(tree, prop) {
  		write_prefix(f, level+1);
658f29a51   John Bonesio   of/flattree: Upda...
254
255
  		for_each_label(prop->labels, l)
  			fprintf(f, "%s: ", l->label);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
  		fprintf(f, "%s", prop->name);
  		write_propval(f, prop);
  	}
  	for_each_child(tree, child) {
  		fprintf(f, "
  ");
  		write_tree_source_node(f, child, level+1);
  	}
  	write_prefix(f, level);
  	fprintf(f, "};
  ");
  }
  
  
  void dt_to_source(FILE *f, struct boot_info *bi)
  {
  	struct reserve_info *re;
  
  	fprintf(f, "/dts-v1/;
  
  ");
  
  	for (re = bi->reservelist; re; re = re->next) {
658f29a51   John Bonesio   of/flattree: Upda...
279
280
281
282
  		struct label *l;
  
  		for_each_label(re->labels, l)
  			fprintf(f, "%s: ", l->label);
a4da2e3ec   David Gibson   [POWERPC] Merge d...
283
284
285
286
287
288
289
290
  		fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;
  ",
  			(unsigned long long)re->re.address,
  			(unsigned long long)re->re.size);
  	}
  
  	write_tree_source_node(f, bi->dt, 0);
  }