Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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