mirror of
				https://github.com/torvalds/linux.git
				synced 2025-10-31 00:28:52 +02:00 
			
		
		
		
	 c072d2b495
			
		
	
	
		c072d2b495
		
	
	
	
	
		
			
			This adds the following commits from upstream: 52f07dcca47c dtc: Add informative error for stray identifier 9cabae6b0351 checks: Fix detection of 'i2c-bus' node 605dc044c3fe New helper to add markers 7da5d106c740 fdtput: Fix documentation about existing nodes 53c63dd421d7 dtdiff: Use input format dtb for dtbo files 84d9dd2fcbc8 dtc: Add data_insert_data function 97011d1f4e98 meson: use override_find_program/override_dependency b841391bbd08 srcpos: Define srcpos_free e0b7749c26a9 Add alloc_marker ecb21febfdd3 meson: port python bindings to build natively via meson and meson-python 7ebfcac8520e Makefile: deprecate in favor of Meson f4c53f4ebf78 Use __ASSEMBLER__ instead of __ASSEMBLY__ 205fbef17b7b Fix some typos da85f91931e5 Remove duplicated words in documentation and comments dd1b3e532d22 meson: support building libfdt without static library 1ccd232709d4 meson: don't build test programs by default ce1d8588880a tests: When building .so from -O asm output mark as non-executable stack 915daadbb62d Start with empty __local_fixups__ and __fixups__ nodes 4ea851f5a44d Let get_subnode() not return deleted nodes 175d2a564c47 Use build_root_node() instead of open-coding it 18f4f305fdd7 build: fix -Dtools=false build 267efc7d4694 checks: Warn about missing #address-cells for interrupt parents 755db115355b libfdt: Add fdt_setprop_namelen_string() bdca8612009e libfdt: Add fdt_setprop_namelen() 0f69cedc08fc libfdt_internal: fdt_find_string_len_() 56b2b30c5bd0 libfdt: add fdt_get_property_namelen_w() 1e8c5f60e127 Add clang-format config 6f183c7d9246 checks: Relax avoid_unnecessary_addr_size check to allow child ranges properties 66c7d0e6f4f3 tests/sw_tree1.c: fix unitialized saveptr 9a969f3b70b0 pylibfdt/libfdt.i: fix backwards compatibility of return values 4292b072a23a .github/workflows: update ubuntu runner to supported version 1c745a9bd169 libfdt: Remove fdt parameter from overlay_fixup_one_phandle b3bbee6b1242 libfdt: Move the SBOM authors section d1656730abfb Add a SBOM file in CycloneDX format b75515af4576 libfdt: Remove extra semi-colons outside functions 2d10aa2afe35 Bump version to v1.7.2 48795c82bdb6 pylibfdt: Don't emit warnings from swig generate C code 838f11e830e3 fdtoverlay: provide better error message for missing `/__symbols__` d1e2384185c5 pylibfdt/libfdt.i: Use SWIG_AppendOutput 18aa49a9f68d Escape spaces in depfile with backslashes. f9968fa06921 libfdt.h: whitespace consistency fixups 9b5f65fb3d8d libfdt.h: typo and consistency fixes 99031e3a4a6e Bump version to v1.7.1 3d5e376925fd setup: Move setting of srcdir down to the bottom e277553b9880 setup: Collect top-level code together 7e5a88984081 setup: Move version and full_description into a function 78b6a85c113b Tidy up some pylint warnings 3501d373f0a2 Require Python 3 The added include of string.h in libfdt_internal.h breaks the kernel overriding libfdt_env.h with its own string functions, so it is dropped. An upstream fix is pending. Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
		
			
				
	
	
		
			385 lines
		
	
	
	
		
			8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			385 lines
		
	
	
	
		
			8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| // SPDX-License-Identifier: GPL-2.0-or-later
 | |
| /*
 | |
|  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
 | |
|  */
 | |
| 
 | |
| #include "dtc.h"
 | |
| #include "srcpos.h"
 | |
| 
 | |
| extern FILE *yyin;
 | |
| extern int yyparse(void);
 | |
| extern YYLTYPE yylloc;
 | |
| 
 | |
| struct dt_info *parser_output;
 | |
| bool treesource_error;
 | |
| 
 | |
| struct dt_info *dt_from_source(const char *fname)
 | |
| {
 | |
| 	parser_output = NULL;
 | |
| 	treesource_error = false;
 | |
| 
 | |
| 	srcfile_push(fname);
 | |
| 	yyin = current_srcfile->f;
 | |
| 	yylloc.file = current_srcfile;
 | |
| 
 | |
| 	if (yyparse() != 0)
 | |
| 		die("Unable to parse input tree\n");
 | |
| 
 | |
| 	if (treesource_error)
 | |
| 		die("Syntax error parsing input tree\n");
 | |
| 
 | |
| 	return parser_output;
 | |
| }
 | |
| 
 | |
| static void write_prefix(FILE *f, int level)
 | |
| {
 | |
| 	int i;
 | |
| 
 | |
| 	for (i = 0; i < level; i++)
 | |
| 		fputc('\t', f);
 | |
| }
 | |
| 
 | |
| static bool isstring(char c)
 | |
| {
 | |
| 	return (isprint((unsigned char)c)
 | |
| 		|| (c == '\0')
 | |
| 		|| strchr("\a\b\t\n\v\f\r", c));
 | |
| }
 | |
| 
 | |
| static void write_propval_string(FILE *f, const char *s, size_t len)
 | |
| {
 | |
| 	const char *end = s + len - 1;
 | |
| 
 | |
| 	if (!len)
 | |
| 		return;
 | |
| 
 | |
| 	assert(*end == '\0');
 | |
| 
 | |
| 	fprintf(f, "\"");
 | |
| 	while (s < end) {
 | |
| 		char c = *s++;
 | |
| 		switch (c) {
 | |
| 		case '\a':
 | |
| 			fprintf(f, "\\a");
 | |
| 			break;
 | |
| 		case '\b':
 | |
| 			fprintf(f, "\\b");
 | |
| 			break;
 | |
| 		case '\t':
 | |
| 			fprintf(f, "\\t");
 | |
| 			break;
 | |
| 		case '\n':
 | |
| 			fprintf(f, "\\n");
 | |
| 			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, "\\0");
 | |
| 			break;
 | |
| 		default:
 | |
| 			if (isprint((unsigned char)c))
 | |
| 				fprintf(f, "%c", c);
 | |
| 			else
 | |
| 				fprintf(f, "\\x%02"PRIx8, c);
 | |
| 		}
 | |
| 	}
 | |
| 	fprintf(f, "\"");
 | |
| }
 | |
| 
 | |
| static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
 | |
| {
 | |
| 	const char *end = p + len;
 | |
| 	assert(len % width == 0);
 | |
| 
 | |
| 	for (; p < end; p += width) {
 | |
| 		switch (width) {
 | |
| 		case 1:
 | |
| 			fprintf(f, "%02"PRIx8, *(const uint8_t*)p);
 | |
| 			break;
 | |
| 		case 2:
 | |
| 			fprintf(f, "0x%02"PRIx16, dtb_ld16(p));
 | |
| 			break;
 | |
| 		case 4:
 | |
| 			fprintf(f, "0x%02"PRIx32, dtb_ld32(p));
 | |
| 			break;
 | |
| 		case 8:
 | |
| 			fprintf(f, "0x%02"PRIx64, dtb_ld64(p));
 | |
| 			break;
 | |
| 		}
 | |
| 		if (p + width < end)
 | |
| 			fputc(' ', f);
 | |
| 	}
 | |
| }
 | |
| 
 | |
| static const char *delim_start[] = {
 | |
| 	[TYPE_UINT8] = "[",
 | |
| 	[TYPE_UINT16] = "/bits/ 16 <",
 | |
| 	[TYPE_UINT32] = "<",
 | |
| 	[TYPE_UINT64] = "/bits/ 64 <",
 | |
| 	[TYPE_STRING] = "",
 | |
| };
 | |
| static const char *delim_end[] = {
 | |
| 	[TYPE_UINT8] = "]",
 | |
| 	[TYPE_UINT16] = ">",
 | |
| 	[TYPE_UINT32] = ">",
 | |
| 	[TYPE_UINT64] = ">",
 | |
| 	[TYPE_STRING] = "",
 | |
| };
 | |
| 
 | |
| /*
 | |
|  * The invariants in the marker list are:
 | |
|  *  - offsets are non-strictly monotonically increasing
 | |
|  *  - for a single offset there is at most one type marker
 | |
|  *  - for a single offset that has both a type marker and non-type markers, the
 | |
|  *    type marker appears before the others.
 | |
|  */
 | |
| static struct marker **add_marker(struct marker **mi,
 | |
| 				  enum markertype type, unsigned int offset, char *ref)
 | |
| {
 | |
| 	struct marker *nm;
 | |
| 
 | |
| 	while (*mi && (*mi)->offset < offset)
 | |
| 		mi = &(*mi)->next;
 | |
| 
 | |
| 	if (*mi && (*mi)->offset == offset && is_type_marker((*mi)->type)) {
 | |
| 		if (is_type_marker(type))
 | |
| 			return mi;
 | |
| 		mi = &(*mi)->next;
 | |
| 	}
 | |
| 
 | |
| 	if (*mi && (*mi)->offset == offset && type == (*mi)->type)
 | |
| 		return mi;
 | |
| 
 | |
| 	nm = xmalloc(sizeof(*nm));
 | |
| 	nm->type = type;
 | |
| 	nm->offset = offset;
 | |
| 	nm->ref = ref;
 | |
| 	nm->next = *mi;
 | |
| 	*mi = nm;
 | |
| 
 | |
| 	return &nm->next;
 | |
| }
 | |
| 
 | |
| static void add_string_markers(struct property *prop)
 | |
| {
 | |
| 	int l, len = prop->val.len;
 | |
| 	const char *p = prop->val.val;
 | |
| 	struct marker **mi = &prop->val.markers;
 | |
| 
 | |
| 	for (l = strlen(p) + 1; l < len; l += strlen(p + l) + 1)
 | |
| 		mi = add_marker(mi, TYPE_STRING, l, NULL);
 | |
| }
 | |
| 
 | |
| static enum markertype guess_value_type(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;
 | |
| 
 | |
| 	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++;
 | |
| 	}
 | |
| 
 | |
| 	if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul <= (len-nnul))
 | |
| 	    && (nnotstringlbl == 0)) {
 | |
| 		if (nnul > 1)
 | |
| 			add_string_markers(prop);
 | |
| 		return TYPE_STRING;
 | |
| 	} else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
 | |
| 		return TYPE_UINT32;
 | |
| 	}
 | |
| 
 | |
| 	return TYPE_UINT8;
 | |
| }
 | |
| 
 | |
| static void write_propval(FILE *f, struct property *prop)
 | |
| {
 | |
| 	size_t len = prop->val.len;
 | |
| 	struct marker *m = prop->val.markers;
 | |
| 	struct marker dummy_marker;
 | |
| 	enum markertype emit_type = TYPE_NONE;
 | |
| 	char *srcstr;
 | |
| 
 | |
| 	if (len == 0) {
 | |
| 		fprintf(f, ";");
 | |
| 		if (annotate) {
 | |
| 			srcstr = srcpos_string_first(prop->srcpos, annotate);
 | |
| 			if (srcstr) {
 | |
| 				fprintf(f, " /* %s */", srcstr);
 | |
| 				free(srcstr);
 | |
| 			}
 | |
| 		}
 | |
| 		fprintf(f, "\n");
 | |
| 		return;
 | |
| 	}
 | |
| 
 | |
| 	fprintf(f, " =");
 | |
| 
 | |
| 	if (!next_type_marker(m)) {
 | |
| 		/* data type information missing, need to guess */
 | |
| 		dummy_marker.type = guess_value_type(prop);
 | |
| 		dummy_marker.next = prop->val.markers;
 | |
| 		dummy_marker.offset = 0;
 | |
| 		dummy_marker.ref = NULL;
 | |
| 		m = &dummy_marker;
 | |
| 	}
 | |
| 
 | |
| 	for_each_marker(m) {
 | |
| 		size_t chunk_len = (m->next ? m->next->offset : len) - m->offset;
 | |
| 		size_t data_len = type_marker_length(m) ? : len - m->offset;
 | |
| 		const char *p = &prop->val.val[m->offset];
 | |
| 		struct marker *m_phandle;
 | |
| 
 | |
| 		if (is_type_marker(m->type)) {
 | |
| 			emit_type = m->type;
 | |
| 			fprintf(f, " %s", delim_start[emit_type]);
 | |
| 		} else if (m->type == LABEL)
 | |
| 			fprintf(f, " %s:", m->ref);
 | |
| 
 | |
| 		if (emit_type == TYPE_NONE || chunk_len == 0)
 | |
| 			continue;
 | |
| 
 | |
| 		switch(emit_type) {
 | |
| 		case TYPE_UINT16:
 | |
| 			write_propval_int(f, p, chunk_len, 2);
 | |
| 			break;
 | |
| 		case TYPE_UINT32:
 | |
| 			m_phandle = prop->val.markers;
 | |
| 			for_each_marker_of_type(m_phandle, REF_PHANDLE)
 | |
| 				if (m->offset == m_phandle->offset)
 | |
| 					break;
 | |
| 
 | |
| 			if (m_phandle) {
 | |
| 				if (m_phandle->ref[0] == '/')
 | |
| 					fprintf(f, "&{%s}", m_phandle->ref);
 | |
| 				else
 | |
| 					fprintf(f, "&%s", m_phandle->ref);
 | |
| 				if (chunk_len > 4) {
 | |
| 					fputc(' ', f);
 | |
| 					write_propval_int(f, p + 4, chunk_len - 4, 4);
 | |
| 				}
 | |
| 			} else {
 | |
| 				write_propval_int(f, p, chunk_len, 4);
 | |
| 			}
 | |
| 			if (data_len > chunk_len)
 | |
| 				fputc(' ', f);
 | |
| 			break;
 | |
| 		case TYPE_UINT64:
 | |
| 			write_propval_int(f, p, chunk_len, 8);
 | |
| 			break;
 | |
| 		case TYPE_STRING:
 | |
| 			write_propval_string(f, p, chunk_len);
 | |
| 			break;
 | |
| 		default:
 | |
| 			write_propval_int(f, p, chunk_len, 1);
 | |
| 		}
 | |
| 
 | |
| 		if (chunk_len == data_len) {
 | |
| 			size_t pos = m->offset + chunk_len;
 | |
| 			fprintf(f, pos == len ? "%s" : "%s,",
 | |
| 			        delim_end[emit_type] ? : "");
 | |
| 			emit_type = TYPE_NONE;
 | |
| 		}
 | |
| 	}
 | |
| 	fprintf(f, ";");
 | |
| 	if (annotate) {
 | |
| 		srcstr = srcpos_string_first(prop->srcpos, annotate);
 | |
| 		if (srcstr) {
 | |
| 			fprintf(f, " /* %s */", srcstr);
 | |
| 			free(srcstr);
 | |
| 		}
 | |
| 	}
 | |
| 	fprintf(f, "\n");
 | |
| }
 | |
| 
 | |
| static void write_tree_source_node(FILE *f, struct node *tree, int level)
 | |
| {
 | |
| 	struct property *prop;
 | |
| 	struct node *child;
 | |
| 	struct label *l;
 | |
| 	char *srcstr;
 | |
| 
 | |
| 	write_prefix(f, level);
 | |
| 	for_each_label(tree->labels, l)
 | |
| 		fprintf(f, "%s: ", l->label);
 | |
| 	if (tree->name && (*tree->name))
 | |
| 		fprintf(f, "%s {", tree->name);
 | |
| 	else
 | |
| 		fprintf(f, "/ {");
 | |
| 
 | |
| 	if (annotate) {
 | |
| 		srcstr = srcpos_string_first(tree->srcpos, annotate);
 | |
| 		if (srcstr) {
 | |
| 			fprintf(f, " /* %s */", srcstr);
 | |
| 			free(srcstr);
 | |
| 		}
 | |
| 	}
 | |
| 	fprintf(f, "\n");
 | |
| 
 | |
| 	for_each_property(tree, prop) {
 | |
| 		write_prefix(f, level+1);
 | |
| 		for_each_label(prop->labels, l)
 | |
| 			fprintf(f, "%s: ", l->label);
 | |
| 		fprintf(f, "%s", prop->name);
 | |
| 		write_propval(f, prop);
 | |
| 	}
 | |
| 	for_each_child(tree, child) {
 | |
| 		fprintf(f, "\n");
 | |
| 		write_tree_source_node(f, child, level+1);
 | |
| 	}
 | |
| 	write_prefix(f, level);
 | |
| 	fprintf(f, "};");
 | |
| 	if (annotate) {
 | |
| 		srcstr = srcpos_string_last(tree->srcpos, annotate);
 | |
| 		if (srcstr) {
 | |
| 			fprintf(f, " /* %s */", srcstr);
 | |
| 			free(srcstr);
 | |
| 		}
 | |
| 	}
 | |
| 	fprintf(f, "\n");
 | |
| }
 | |
| 
 | |
| void dt_to_source(FILE *f, struct dt_info *dti)
 | |
| {
 | |
| 	struct reserve_info *re;
 | |
| 
 | |
| 	fprintf(f, "/dts-v1/;\n\n");
 | |
| 
 | |
| 	for (re = dti->reservelist; re; re = re->next) {
 | |
| 		struct label *l;
 | |
| 
 | |
| 		for_each_label(re->labels, l)
 | |
| 			fprintf(f, "%s: ", l->label);
 | |
| 		fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
 | |
| 			(unsigned long long)re->address,
 | |
| 			(unsigned long long)re->size);
 | |
| 	}
 | |
| 
 | |
| 	write_tree_source_node(f, dti->dt, 0);
 | |
| }
 |