1243730Srwatson/* SPDX-License-Identifier: GPL-2.0-or-later */
2243730Srwatson#ifndef DTC_H
3243730Srwatson#define DTC_H
4243730Srwatson
5243730Srwatson/*
6243730Srwatson * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
7243730Srwatson */
8243730Srwatson
9243730Srwatson#include <stdio.h>
10243730Srwatson#include <string.h>
11243730Srwatson#include <stdlib.h>
12243730Srwatson#include <stdint.h>
13243730Srwatson#include <stdbool.h>
14243730Srwatson#include <stdarg.h>
15243730Srwatson#include <assert.h>
16243730Srwatson#include <ctype.h>
17243730Srwatson#include <errno.h>
18243730Srwatson#include <unistd.h>
19243730Srwatson#include <inttypes.h>
20243730Srwatson
21243730Srwatson#include <libfdt_env.h>
22243730Srwatson#include <fdt.h>
23243730Srwatson
24243730Srwatson#include "util.h"
25243730Srwatson
26243730Srwatson#ifdef DEBUG
27243730Srwatson#define debug(...)	printf(__VA_ARGS__)
28243730Srwatson#else
29243730Srwatson#define debug(...)
30243730Srwatson#endif
31243730Srwatson
32243730Srwatson#define DEFAULT_FDT_VERSION	17
33243730Srwatson
34243730Srwatson/*
35243730Srwatson * Command line options
36243730Srwatson */
37243730Srwatsonextern int quiet;		/* Level of quietness */
38243730Srwatsonextern unsigned int reservenum;	/* Number of memory reservation slots */
39243730Srwatsonextern int minsize;		/* Minimum blob size */
40243730Srwatsonextern int padsize;		/* Additional padding to blob */
41243730Srwatsonextern int alignsize;		/* Additional padding to blob accroding to the alignsize */
42243730Srwatsonextern int phandle_format;	/* Use linux,phandle or phandle properties */
43243730Srwatsonextern int generate_symbols;	/* generate symbols for nodes with labels */
44243730Srwatsonextern int generate_fixups;	/* generate fixups */
45243730Srwatsonextern int auto_label_aliases;	/* auto generate labels -> aliases */
46243730Srwatsonextern int annotate;		/* annotate .dts with input source location */
47243730Srwatson
48243730Srwatson#define PHANDLE_LEGACY	0x1
49243730Srwatson#define PHANDLE_EPAPR	0x2
50243730Srwatson#define PHANDLE_BOTH	0x3
51243730Srwatson
52243730Srwatsontypedef uint32_t cell_t;
53243730Srwatson
54243730Srwatsonstatic inline bool phandle_is_valid(cell_t phandle)
55243730Srwatson{
56243730Srwatson	return phandle != 0 && phandle != ~0U;
57243730Srwatson}
58243730Srwatson
59243730Srwatsonstatic inline uint16_t dtb_ld16(const void *p)
60243730Srwatson{
61243730Srwatson	const uint8_t *bp = (const uint8_t *)p;
62243730Srwatson
63243730Srwatson	return ((uint16_t)bp[0] << 8)
64243730Srwatson		| bp[1];
65243730Srwatson}
66243730Srwatson
67243730Srwatsonstatic inline uint32_t dtb_ld32(const void *p)
68243730Srwatson{
69243730Srwatson	const uint8_t *bp = (const uint8_t *)p;
70243730Srwatson
71243730Srwatson	return ((uint32_t)bp[0] << 24)
72243730Srwatson		| ((uint32_t)bp[1] << 16)
73243730Srwatson		| ((uint32_t)bp[2] << 8)
74243730Srwatson		| bp[3];
75243730Srwatson}
76243730Srwatson
77243730Srwatsonstatic inline uint64_t dtb_ld64(const void *p)
78243730Srwatson{
79243730Srwatson	const uint8_t *bp = (const uint8_t *)p;
80243730Srwatson
81243730Srwatson	return ((uint64_t)bp[0] << 56)
82243730Srwatson		| ((uint64_t)bp[1] << 48)
83243730Srwatson		| ((uint64_t)bp[2] << 40)
84243730Srwatson		| ((uint64_t)bp[3] << 32)
85243730Srwatson		| ((uint64_t)bp[4] << 24)
86243730Srwatson		| ((uint64_t)bp[5] << 16)
87243730Srwatson		| ((uint64_t)bp[6] << 8)
88243730Srwatson		| bp[7];
89243730Srwatson}
90243730Srwatson
91243730Srwatson#define streq(a, b)	(strcmp((a), (b)) == 0)
92243730Srwatson#define strstarts(s, prefix)	(strncmp((s), (prefix), strlen(prefix)) == 0)
93243730Srwatson#define strprefixeq(a, n, b)	(strlen(b) == (n) && (memcmp(a, b, n) == 0))
94243730Srwatsonstatic inline bool strends(const char *str, const char *suffix)
95243730Srwatson{
96243730Srwatson	unsigned int len, suffix_len;
97243730Srwatson
98243730Srwatson	len = strlen(str);
99243730Srwatson	suffix_len = strlen(suffix);
100243730Srwatson	if (len < suffix_len)
101243730Srwatson		return false;
102243730Srwatson	return streq(str + len - suffix_len, suffix);
103243730Srwatson}
104243730Srwatson
105243730Srwatson#define ALIGN(x, a)	(((x) + (a) - 1) & ~((a) - 1))
106243730Srwatson
107243730Srwatson/* Data blobs */
108243730Srwatsonenum markertype {
109243730Srwatson	TYPE_NONE,
110243730Srwatson	REF_PHANDLE,
111243730Srwatson	REF_PATH,
112243730Srwatson	LABEL,
113243730Srwatson	TYPE_UINT8,
114243730Srwatson	TYPE_UINT16,
115243730Srwatson	TYPE_UINT32,
116243730Srwatson	TYPE_UINT64,
117243730Srwatson	TYPE_STRING,
118243730Srwatson};
119243730Srwatson
120243730Srwatsonstatic inline bool is_type_marker(enum markertype type)
121243730Srwatson{
122243730Srwatson	return type >= TYPE_UINT8;
123243730Srwatson}
124243730Srwatson
125243730Srwatsonextern const char *markername(enum markertype markertype);
126243730Srwatson
127243730Srwatsonstruct  marker {
128243730Srwatson	enum markertype type;
129243730Srwatson	unsigned int offset;
130243730Srwatson	char *ref;
131243730Srwatson	struct marker *next;
132243730Srwatson};
133243730Srwatson
134243730Srwatsonstruct data {
135243730Srwatson	unsigned int len;
136243730Srwatson	char *val;
137243730Srwatson	struct marker *markers;
138243730Srwatson};
139243730Srwatson
140243730Srwatson
141243730Srwatson#define empty_data ((struct data){ 0 /* all .members = 0 or NULL */ })
142243730Srwatson
143243730Srwatson#define for_each_marker(m) \
144243730Srwatson	for (; (m); (m) = (m)->next)
145243730Srwatson#define for_each_marker_of_type(m, t) \
146243730Srwatson	for_each_marker(m) \
147243730Srwatson		if ((m)->type == (t))
148243730Srwatson
149243730Srwatsonstatic inline struct marker *next_type_marker(struct marker *m)
150243730Srwatson{
151	for_each_marker(m)
152		if (is_type_marker(m->type))
153			break;
154	return m;
155}
156
157static inline size_t type_marker_length(struct marker *m)
158{
159	struct marker *next = next_type_marker(m->next);
160
161	if (next)
162		return next->offset - m->offset;
163	return 0;
164}
165
166void data_free(struct data d);
167
168struct data data_grow_for(struct data d, unsigned int xlen);
169
170struct data data_copy_mem(const char *mem, int len);
171struct data data_copy_escape_string(const char *s, int len);
172struct data data_copy_file(FILE *f, size_t len);
173
174struct data data_append_data(struct data d, const void *p, int len);
175struct data data_insert_at_marker(struct data d, struct marker *m,
176				  const void *p, int len);
177struct data data_merge(struct data d1, struct data d2);
178struct data data_append_cell(struct data d, cell_t word);
179struct data data_append_integer(struct data d, uint64_t word, int bits);
180struct data data_append_re(struct data d, uint64_t address, uint64_t size);
181struct data data_append_addr(struct data d, uint64_t addr);
182struct data data_append_byte(struct data d, uint8_t byte);
183struct data data_append_zeroes(struct data d, int len);
184struct data data_append_align(struct data d, int align);
185
186struct data data_add_marker(struct data d, enum markertype type, char *ref);
187
188bool data_is_one_string(struct data d);
189
190/* DT constraints */
191
192#define MAX_PROPNAME_LEN	31
193#define MAX_NODENAME_LEN	31
194
195/* Live trees */
196struct label {
197	bool deleted;
198	char *label;
199	struct label *next;
200};
201
202struct bus_type {
203	const char *name;
204};
205
206struct property {
207	bool deleted;
208	char *name;
209	struct data val;
210
211	struct property *next;
212
213	struct label *labels;
214	struct srcpos *srcpos;
215};
216
217struct node {
218	bool deleted;
219	char *name;
220	struct property *proplist;
221	struct node *children;
222
223	struct node *parent;
224	struct node *next_sibling;
225
226	char *fullpath;
227	int basenamelen;
228
229	cell_t phandle;
230	int addr_cells, size_cells;
231
232	struct label *labels;
233	const struct bus_type *bus;
234	struct srcpos *srcpos;
235
236	bool omit_if_unused, is_referenced;
237};
238
239#define for_each_label_withdel(l0, l) \
240	for ((l) = (l0); (l); (l) = (l)->next)
241
242#define for_each_label(l0, l) \
243	for_each_label_withdel(l0, l) \
244		if (!(l)->deleted)
245
246#define for_each_property_withdel(n, p) \
247	for ((p) = (n)->proplist; (p); (p) = (p)->next)
248
249#define for_each_property(n, p) \
250	for_each_property_withdel(n, p) \
251		if (!(p)->deleted)
252
253#define for_each_child_withdel(n, c) \
254	for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
255
256#define for_each_child(n, c) \
257	for_each_child_withdel(n, c) \
258		if (!(c)->deleted)
259
260void add_label(struct label **labels, char *label);
261void delete_labels(struct label **labels);
262
263struct property *build_property(char *name, struct data val,
264				struct srcpos *srcpos);
265struct property *build_property_delete(char *name);
266struct property *chain_property(struct property *first, struct property *list);
267struct property *reverse_properties(struct property *first);
268
269struct node *build_node(struct property *proplist, struct node *children,
270			struct srcpos *srcpos);
271struct node *build_node_delete(struct srcpos *srcpos);
272struct node *name_node(struct node *node, char *name);
273struct node *omit_node_if_unused(struct node *node);
274struct node *reference_node(struct node *node);
275struct node *chain_node(struct node *first, struct node *list);
276struct node *merge_nodes(struct node *old_node, struct node *new_node);
277struct node *add_orphan_node(struct node *old_node, struct node *new_node, char *ref);
278
279void add_property(struct node *node, struct property *prop);
280void delete_property_by_name(struct node *node, char *name);
281void delete_property(struct property *prop);
282void add_child(struct node *parent, struct node *child);
283void delete_node_by_name(struct node *parent, char *name);
284void delete_node(struct node *node);
285void append_to_property(struct node *node,
286			char *name, const void *data, int len,
287			enum markertype type);
288
289const char *get_unitname(struct node *node);
290struct property *get_property(struct node *node, const char *propname);
291cell_t propval_cell(struct property *prop);
292cell_t propval_cell_n(struct property *prop, unsigned int n);
293struct property *get_property_by_label(struct node *tree, const char *label,
294				       struct node **node);
295struct marker *get_marker_label(struct node *tree, const char *label,
296				struct node **node, struct property **prop);
297struct node *get_subnode(struct node *node, const char *nodename);
298struct node *get_node_by_path(struct node *tree, const char *path);
299struct node *get_node_by_label(struct node *tree, const char *label);
300struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
301struct node *get_node_by_ref(struct node *tree, const char *ref);
302cell_t get_node_phandle(struct node *root, struct node *node);
303
304uint32_t guess_boot_cpuid(struct node *tree);
305
306/* Boot info (tree plus memreserve information */
307
308struct reserve_info {
309	uint64_t address, size;
310
311	struct reserve_info *next;
312
313	struct label *labels;
314};
315
316struct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
317struct reserve_info *chain_reserve_entry(struct reserve_info *first,
318					 struct reserve_info *list);
319struct reserve_info *add_reserve_entry(struct reserve_info *list,
320				       struct reserve_info *new);
321
322
323struct dt_info {
324	unsigned int dtsflags;
325	struct reserve_info *reservelist;
326	uint32_t boot_cpuid_phys;
327	struct node *dt;		/* the device tree */
328	const char *outname;		/* filename being written to, "-" for stdout */
329};
330
331/* DTS version flags definitions */
332#define DTSF_V1		0x0001	/* /dts-v1/ */
333#define DTSF_PLUGIN	0x0002	/* /plugin/ */
334
335struct dt_info *build_dt_info(unsigned int dtsflags,
336			      struct reserve_info *reservelist,
337			      struct node *tree, uint32_t boot_cpuid_phys);
338void sort_tree(struct dt_info *dti);
339void generate_label_tree(struct dt_info *dti, char *name, bool allocph);
340void generate_fixups_tree(struct dt_info *dti, char *name);
341void generate_local_fixups_tree(struct dt_info *dti, char *name);
342
343/* Checks */
344
345void parse_checks_option(bool warn, bool error, const char *arg);
346void process_checks(bool force, struct dt_info *dti);
347
348/* Flattened trees */
349
350void dt_to_blob(FILE *f, struct dt_info *dti, int version);
351void dt_to_asm(FILE *f, struct dt_info *dti, int version);
352
353struct dt_info *dt_from_blob(const char *fname);
354
355/* Tree source */
356
357void dt_to_source(FILE *f, struct dt_info *dti);
358struct dt_info *dt_from_source(const char *f);
359
360/* YAML source */
361
362void dt_to_yaml(FILE *f, struct dt_info *dti);
363
364/* FS trees */
365
366struct dt_info *dt_from_fs(const char *dirname);
367
368#endif /* DTC_H */
369