Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  4 */
  5
  6#include "dtc.h"
  7#include "srcpos.h"
  8
  9extern FILE *yyin;
 10extern int yyparse(void);
 11extern YYLTYPE yylloc;
 12
 13struct dt_info *parser_output;
 14bool treesource_error;
 15
 16struct dt_info *dt_from_source(const char *fname)
 17{
 18	parser_output = NULL;
 19	treesource_error = false;
 20
 21	srcfile_push(fname);
 22	yyin = current_srcfile->f;
 23	yylloc.file = current_srcfile;
 24
 25	if (yyparse() != 0)
 26		die("Unable to parse input tree\n");
 27
 28	if (treesource_error)
 29		die("Syntax error parsing input tree\n");
 30
 31	return parser_output;
 32}
 33
 34static void write_prefix(FILE *f, int level)
 35{
 36	int i;
 37
 38	for (i = 0; i < level; i++)
 39		fputc('\t', f);
 40}
 41
 42static bool isstring(char c)
 43{
 44	return (isprint((unsigned char)c)
 45		|| (c == '\0')
 46		|| strchr("\a\b\t\n\v\f\r", c));
 47}
 48
 49static void write_propval_string(FILE *f, const char *s, size_t len)
 50{
 51	const char *end = s + len - 1;
 52
 53	if (!len)
 54		return;
 55
 56	assert(*end == '\0');
 57
 
 
 
 
 
 58	fprintf(f, "\"");
 59	while (s < end) {
 60		char c = *s++;
 
 
 61		switch (c) {
 62		case '\a':
 63			fprintf(f, "\\a");
 64			break;
 65		case '\b':
 66			fprintf(f, "\\b");
 67			break;
 68		case '\t':
 69			fprintf(f, "\\t");
 70			break;
 71		case '\n':
 72			fprintf(f, "\\n");
 73			break;
 74		case '\v':
 75			fprintf(f, "\\v");
 76			break;
 77		case '\f':
 78			fprintf(f, "\\f");
 79			break;
 80		case '\r':
 81			fprintf(f, "\\r");
 82			break;
 83		case '\\':
 84			fprintf(f, "\\\\");
 85			break;
 86		case '\"':
 87			fprintf(f, "\\\"");
 88			break;
 89		case '\0':
 90			fprintf(f, "\\0");
 
 
 
 
 
 
 
 
 91			break;
 92		default:
 93			if (isprint((unsigned char)c))
 94				fprintf(f, "%c", c);
 95			else
 96				fprintf(f, "\\x%02"PRIx8, c);
 97		}
 98	}
 99	fprintf(f, "\"");
 
 
 
 
 
 
100}
101
102static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
103{
104	const char *end = p + len;
105	assert(len % width == 0);
 
106
107	for (; p < end; p += width) {
108		switch (width) {
109		case 1:
110			fprintf(f, "%02"PRIx8, *(const uint8_t*)p);
111			break;
112		case 2:
113			fprintf(f, "0x%02"PRIx16, fdt16_to_cpu(*(const fdt16_t*)p));
114			break;
115		case 4:
116			fprintf(f, "0x%02"PRIx32, fdt32_to_cpu(*(const fdt32_t*)p));
117			break;
118		case 8:
119			fprintf(f, "0x%02"PRIx64, fdt64_to_cpu(*(const fdt64_t*)p));
120			break;
121		}
122		if (p + width < end)
123			fputc(' ', f);
 
 
 
124	}
125}
126
127static bool has_data_type_information(struct marker *m)
128{
129	return m->type >= TYPE_UINT8;
 
 
 
130}
131
132static struct marker *next_type_marker(struct marker *m)
133{
134	while (m && !has_data_type_information(m))
135		m = m->next;
136	return m;
137}
138
139size_t type_marker_length(struct marker *m)
140{
141	struct marker *next = next_type_marker(m->next);
 
 
 
 
142
143	if (next)
144		return next->offset - m->offset;
145	return 0;
146}
 
147
148static const char *delim_start[] = {
149	[TYPE_UINT8] = "[",
150	[TYPE_UINT16] = "/bits/ 16 <",
151	[TYPE_UINT32] = "<",
152	[TYPE_UINT64] = "/bits/ 64 <",
153	[TYPE_STRING] = "",
154};
155static const char *delim_end[] = {
156	[TYPE_UINT8] = "]",
157	[TYPE_UINT16] = ">",
158	[TYPE_UINT32] = ">",
159	[TYPE_UINT64] = ">",
160	[TYPE_STRING] = "",
161};
162
163static enum markertype guess_value_type(struct property *prop)
164{
165	int len = prop->val.len;
166	const char *p = prop->val.val;
167	struct marker *m = prop->val.markers;
168	int nnotstring = 0, nnul = 0;
169	int nnotstringlbl = 0, nnotcelllbl = 0;
170	int i;
171
 
 
 
 
 
172	for (i = 0; i < len; i++) {
173		if (! isstring(p[i]))
174			nnotstring++;
175		if (p[i] == '\0')
176			nnul++;
177	}
178
179	for_each_marker_of_type(m, LABEL) {
180		if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
181			nnotstringlbl++;
182		if ((m->offset % sizeof(cell_t)) != 0)
183			nnotcelllbl++;
184	}
185
 
186	if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
187	    && (nnotstringlbl == 0)) {
188		return TYPE_STRING;
189	} else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
190		return TYPE_UINT32;
 
 
191	}
192
193	return TYPE_UINT8;
194}
195
196static void write_propval(FILE *f, struct property *prop)
197{
198	size_t len = prop->val.len;
199	struct marker *m = prop->val.markers;
200	struct marker dummy_marker;
201	enum markertype emit_type = TYPE_NONE;
202	char *srcstr;
203
204	if (len == 0) {
205		fprintf(f, ";");
206		if (annotate) {
207			srcstr = srcpos_string_first(prop->srcpos, annotate);
208			if (srcstr) {
209				fprintf(f, " /* %s */", srcstr);
210				free(srcstr);
211			}
212		}
213		fprintf(f, "\n");
214		return;
215	}
216
217	fprintf(f, " =");
218
219	if (!next_type_marker(m)) {
220		/* data type information missing, need to guess */
221		dummy_marker.type = guess_value_type(prop);
222		dummy_marker.next = prop->val.markers;
223		dummy_marker.offset = 0;
224		dummy_marker.ref = NULL;
225		m = &dummy_marker;
226	}
227
228	for_each_marker(m) {
229		size_t chunk_len = (m->next ? m->next->offset : len) - m->offset;
230		size_t data_len = type_marker_length(m) ? : len - m->offset;
231		const char *p = &prop->val.val[m->offset];
232
233		if (has_data_type_information(m)) {
234			emit_type = m->type;
235			fprintf(f, " %s", delim_start[emit_type]);
236		} else if (m->type == LABEL)
237			fprintf(f, " %s:", m->ref);
238		else if (m->offset)
239			fputc(' ', f);
240
241		if (emit_type == TYPE_NONE) {
242			assert(chunk_len == 0);
243			continue;
244		}
245
246		switch(emit_type) {
247		case TYPE_UINT16:
248			write_propval_int(f, p, chunk_len, 2);
249			break;
250		case TYPE_UINT32:
251			write_propval_int(f, p, chunk_len, 4);
252			break;
253		case TYPE_UINT64:
254			write_propval_int(f, p, chunk_len, 8);
255			break;
256		case TYPE_STRING:
257			write_propval_string(f, p, chunk_len);
258			break;
259		default:
260			write_propval_int(f, p, chunk_len, 1);
261		}
262
263		if (chunk_len == data_len) {
264			size_t pos = m->offset + chunk_len;
265			fprintf(f, pos == len ? "%s" : "%s,",
266			        delim_end[emit_type] ? : "");
267			emit_type = TYPE_NONE;
268		}
269	}
270	fprintf(f, ";");
271	if (annotate) {
272		srcstr = srcpos_string_first(prop->srcpos, annotate);
273		if (srcstr) {
274			fprintf(f, " /* %s */", srcstr);
275			free(srcstr);
276		}
277	}
278	fprintf(f, "\n");
279}
280
281static void write_tree_source_node(FILE *f, struct node *tree, int level)
282{
283	struct property *prop;
284	struct node *child;
285	struct label *l;
286	char *srcstr;
287
288	write_prefix(f, level);
289	for_each_label(tree->labels, l)
290		fprintf(f, "%s: ", l->label);
291	if (tree->name && (*tree->name))
292		fprintf(f, "%s {", tree->name);
293	else
294		fprintf(f, "/ {");
295
296	if (annotate) {
297		srcstr = srcpos_string_first(tree->srcpos, annotate);
298		if (srcstr) {
299			fprintf(f, " /* %s */", srcstr);
300			free(srcstr);
301		}
302	}
303	fprintf(f, "\n");
304
305	for_each_property(tree, prop) {
306		write_prefix(f, level+1);
307		for_each_label(prop->labels, l)
308			fprintf(f, "%s: ", l->label);
309		fprintf(f, "%s", prop->name);
310		write_propval(f, prop);
311	}
312	for_each_child(tree, child) {
313		fprintf(f, "\n");
314		write_tree_source_node(f, child, level+1);
315	}
316	write_prefix(f, level);
317	fprintf(f, "};");
318	if (annotate) {
319		srcstr = srcpos_string_last(tree->srcpos, annotate);
320		if (srcstr) {
321			fprintf(f, " /* %s */", srcstr);
322			free(srcstr);
323		}
324	}
325	fprintf(f, "\n");
326}
327
328void dt_to_source(FILE *f, struct dt_info *dti)
 
329{
330	struct reserve_info *re;
331
332	fprintf(f, "/dts-v1/;\n\n");
333
334	for (re = dti->reservelist; re; re = re->next) {
335		struct label *l;
336
337		for_each_label(re->labels, l)
338			fprintf(f, "%s: ", l->label);
339		fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
340			(unsigned long long)re->address,
341			(unsigned long long)re->size);
342	}
343
344	write_tree_source_node(f, dti->dt, 0);
345}
v3.1
 
  1/*
  2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
  3 *
  4 *
  5 * 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.
 14 *
 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
 19 */
 20
 21#include "dtc.h"
 22#include "srcpos.h"
 23
 24extern FILE *yyin;
 25extern int yyparse(void);
 
 26
 27struct boot_info *the_boot_info;
 28int treesource_error;
 29
 30struct boot_info *dt_from_source(const char *fname)
 31{
 32	the_boot_info = NULL;
 33	treesource_error = 0;
 34
 35	srcfile_push(fname);
 36	yyin = current_srcfile->f;
 
 37
 38	if (yyparse() != 0)
 39		die("Unable to parse input tree\n");
 40
 41	if (treesource_error)
 42		die("Syntax error parsing input tree\n");
 43
 44	return the_boot_info;
 45}
 46
 47static void write_prefix(FILE *f, int level)
 48{
 49	int i;
 50
 51	for (i = 0; i < level; i++)
 52		fputc('\t', f);
 53}
 54
 55static int isstring(char c)
 56{
 57	return (isprint(c)
 58		|| (c == '\0')
 59		|| strchr("\a\b\t\n\v\f\r", c));
 60}
 61
 62static void write_propval_string(FILE *f, struct data val)
 63{
 64	const char *str = val.val;
 65	int i;
 66	struct marker *m = val.markers;
 
 67
 68	assert(str[val.len-1] == '\0');
 69
 70	while (m && (m->offset == 0)) {
 71		if (m->type == LABEL)
 72			fprintf(f, "%s: ", m->ref);
 73		m = m->next;
 74	}
 75	fprintf(f, "\"");
 76
 77	for (i = 0; i < (val.len-1); i++) {
 78		char c = str[i];
 79
 80		switch (c) {
 81		case '\a':
 82			fprintf(f, "\\a");
 83			break;
 84		case '\b':
 85			fprintf(f, "\\b");
 86			break;
 87		case '\t':
 88			fprintf(f, "\\t");
 89			break;
 90		case '\n':
 91			fprintf(f, "\\n");
 92			break;
 93		case '\v':
 94			fprintf(f, "\\v");
 95			break;
 96		case '\f':
 97			fprintf(f, "\\f");
 98			break;
 99		case '\r':
100			fprintf(f, "\\r");
101			break;
102		case '\\':
103			fprintf(f, "\\\\");
104			break;
105		case '\"':
106			fprintf(f, "\\\"");
107			break;
108		case '\0':
109			fprintf(f, "\", ");
110			while (m && (m->offset < i)) {
111				if (m->type == LABEL) {
112					assert(m->offset == (i+1));
113					fprintf(f, "%s: ", m->ref);
114				}
115				m = m->next;
116			}
117			fprintf(f, "\"");
118			break;
119		default:
120			if (isprint(c))
121				fprintf(f, "%c", c);
122			else
123				fprintf(f, "\\x%02hhx", c);
124		}
125	}
126	fprintf(f, "\"");
127
128	/* Wrap up any labels at the end of the value */
129	for_each_marker_of_type(m, LABEL) {
130		assert (m->offset == val.len);
131		fprintf(f, " %s:", m->ref);
132	}
133}
134
135static void write_propval_cells(FILE *f, struct data val)
136{
137	void *propend = val.val + val.len;
138	cell_t *cp = (cell_t *)val.val;
139	struct marker *m = val.markers;
140
141	fprintf(f, "<");
142	for (;;) {
143		while (m && (m->offset <= ((char *)cp - val.val))) {
144			if (m->type == LABEL) {
145				assert(m->offset == ((char *)cp - val.val));
146				fprintf(f, "%s: ", m->ref);
147			}
148			m = m->next;
 
 
 
 
 
 
149		}
150
151		fprintf(f, "0x%x", fdt32_to_cpu(*cp++));
152		if ((void *)cp >= propend)
153			break;
154		fprintf(f, " ");
155	}
 
156
157	/* Wrap up any labels at the end of the value */
158	for_each_marker_of_type(m, LABEL) {
159		assert (m->offset == val.len);
160		fprintf(f, " %s:", m->ref);
161	}
162	fprintf(f, ">");
163}
164
165static void write_propval_bytes(FILE *f, struct data val)
166{
167	void *propend = val.val + val.len;
168	const char *bp = val.val;
169	struct marker *m = val.markers;
 
170
171	fprintf(f, "[");
172	for (;;) {
173		while (m && (m->offset == (bp-val.val))) {
174			if (m->type == LABEL)
175				fprintf(f, "%s: ", m->ref);
176			m = m->next;
177		}
178
179		fprintf(f, "%02hhx", *bp++);
180		if ((const void *)bp >= propend)
181			break;
182		fprintf(f, " ");
183	}
184
185	/* Wrap up any labels at the end of the value */
186	for_each_marker_of_type(m, LABEL) {
187		assert (m->offset == val.len);
188		fprintf(f, " %s:", m->ref);
189	}
190	fprintf(f, "]");
191}
 
 
 
 
 
 
 
192
193static void write_propval(FILE *f, struct property *prop)
194{
195	int len = prop->val.len;
196	const char *p = prop->val.val;
197	struct marker *m = prop->val.markers;
198	int nnotstring = 0, nnul = 0;
199	int nnotstringlbl = 0, nnotcelllbl = 0;
200	int i;
201
202	if (len == 0) {
203		fprintf(f, ";\n");
204		return;
205	}
206
207	for (i = 0; i < len; i++) {
208		if (! isstring(p[i]))
209			nnotstring++;
210		if (p[i] == '\0')
211			nnul++;
212	}
213
214	for_each_marker_of_type(m, LABEL) {
215		if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
216			nnotstringlbl++;
217		if ((m->offset % sizeof(cell_t)) != 0)
218			nnotcelllbl++;
219	}
220
221	fprintf(f, " = ");
222	if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
223	    && (nnotstringlbl == 0)) {
224		write_propval_string(f, prop->val);
225	} else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
226		write_propval_cells(f, prop->val);
227	} else {
228		write_propval_bytes(f, prop->val);
229	}
230
231	fprintf(f, ";\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232}
233
234static void write_tree_source_node(FILE *f, struct node *tree, int level)
235{
236	struct property *prop;
237	struct node *child;
238	struct label *l;
 
239
240	write_prefix(f, level);
241	for_each_label(tree->labels, l)
242		fprintf(f, "%s: ", l->label);
243	if (tree->name && (*tree->name))
244		fprintf(f, "%s {\n", tree->name);
245	else
246		fprintf(f, "/ {\n");
 
 
 
 
 
 
 
 
 
247
248	for_each_property(tree, prop) {
249		write_prefix(f, level+1);
250		for_each_label(prop->labels, l)
251			fprintf(f, "%s: ", l->label);
252		fprintf(f, "%s", prop->name);
253		write_propval(f, prop);
254	}
255	for_each_child(tree, child) {
256		fprintf(f, "\n");
257		write_tree_source_node(f, child, level+1);
258	}
259	write_prefix(f, level);
260	fprintf(f, "};\n");
 
 
 
 
 
 
 
 
261}
262
263
264void dt_to_source(FILE *f, struct boot_info *bi)
265{
266	struct reserve_info *re;
267
268	fprintf(f, "/dts-v1/;\n\n");
269
270	for (re = bi->reservelist; re; re = re->next) {
271		struct label *l;
272
273		for_each_label(re->labels, l)
274			fprintf(f, "%s: ", l->label);
275		fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
276			(unsigned long long)re->re.address,
277			(unsigned long long)re->re.size);
278	}
279
280	write_tree_source_node(f, bi->dt, 0);
281}
282