xref: /OK3568_Linux_fs/kernel/scripts/dtc/util.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun #ifndef UTIL_H
3*4882a593Smuzhiyun #define UTIL_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <stdlib.h>
6*4882a593Smuzhiyun #include <stdarg.h>
7*4882a593Smuzhiyun #include <stdbool.h>
8*4882a593Smuzhiyun #include <getopt.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun  * Copyright 2011 The Chromium Authors, All Rights Reserved.
12*4882a593Smuzhiyun  * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #ifdef __GNUC__
16*4882a593Smuzhiyun #ifdef __clang__
17*4882a593Smuzhiyun #define PRINTF(i, j)	__attribute__((format (printf, i, j)))
18*4882a593Smuzhiyun #else
19*4882a593Smuzhiyun #define PRINTF(i, j)	__attribute__((format (gnu_printf, i, j)))
20*4882a593Smuzhiyun #endif
21*4882a593Smuzhiyun #define NORETURN	__attribute__((noreturn))
22*4882a593Smuzhiyun #else
23*4882a593Smuzhiyun #define PRINTF(i, j)
24*4882a593Smuzhiyun #define NORETURN
25*4882a593Smuzhiyun #endif
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define stringify(s)	stringify_(s)
30*4882a593Smuzhiyun #define stringify_(s)	#s
31*4882a593Smuzhiyun 
die(const char * str,...)32*4882a593Smuzhiyun static inline void NORETURN PRINTF(1, 2) die(const char *str, ...)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	va_list ap;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	va_start(ap, str);
37*4882a593Smuzhiyun 	fprintf(stderr, "FATAL ERROR: ");
38*4882a593Smuzhiyun 	vfprintf(stderr, str, ap);
39*4882a593Smuzhiyun 	va_end(ap);
40*4882a593Smuzhiyun 	exit(1);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
xmalloc(size_t len)43*4882a593Smuzhiyun static inline void *xmalloc(size_t len)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	void *new = malloc(len);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	if (!new)
48*4882a593Smuzhiyun 		die("malloc() failed\n");
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	return new;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
xrealloc(void * p,size_t len)53*4882a593Smuzhiyun static inline void *xrealloc(void *p, size_t len)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	void *new = realloc(p, len);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (!new)
58*4882a593Smuzhiyun 		die("realloc() failed (len=%zd)\n", len);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	return new;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun extern char *xstrdup(const char *s);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun extern int PRINTF(2, 3) xasprintf(char **strp, const char *fmt, ...);
66*4882a593Smuzhiyun extern int PRINTF(2, 3) xasprintf_append(char **strp, const char *fmt, ...);
67*4882a593Smuzhiyun extern int xavsprintf_append(char **strp, const char *fmt, va_list ap);
68*4882a593Smuzhiyun extern char *join_path(const char *path, const char *name);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /**
71*4882a593Smuzhiyun  * Check a property of a given length to see if it is all printable and
72*4882a593Smuzhiyun  * has a valid terminator. The property can contain either a single string,
73*4882a593Smuzhiyun  * or multiple strings each of non-zero length.
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * @param data	The string to check
76*4882a593Smuzhiyun  * @param len	The string length including terminator
77*4882a593Smuzhiyun  * @return 1 if a valid printable string, 0 if not
78*4882a593Smuzhiyun  */
79*4882a593Smuzhiyun bool util_is_printable_string(const void *data, int len);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun  * Parse an escaped character starting at index i in string s.  The resulting
83*4882a593Smuzhiyun  * character will be returned and the index i will be updated to point at the
84*4882a593Smuzhiyun  * character directly after the end of the encoding, this may be the '\0'
85*4882a593Smuzhiyun  * terminator of the string.
86*4882a593Smuzhiyun  */
87*4882a593Smuzhiyun char get_escape_char(const char *s, int *i);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /**
90*4882a593Smuzhiyun  * Read a device tree file into a buffer. This will report any errors on
91*4882a593Smuzhiyun  * stderr.
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * @param filename	The filename to read, or - for stdin
94*4882a593Smuzhiyun  * @param len		If non-NULL, the amount of data we managed to read
95*4882a593Smuzhiyun  * @return Pointer to allocated buffer containing fdt, or NULL on error
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun char *utilfdt_read(const char *filename, size_t *len);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun  * Read a device tree file into a buffer. Does not report errors, but only
101*4882a593Smuzhiyun  * returns them. The value returned can be passed to strerror() to obtain
102*4882a593Smuzhiyun  * an error message for the user.
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * @param filename	The filename to read, or - for stdin
105*4882a593Smuzhiyun  * @param buffp		Returns pointer to buffer containing fdt
106*4882a593Smuzhiyun  * @param len		If non-NULL, the amount of data we managed to read
107*4882a593Smuzhiyun  * @return 0 if ok, else an errno value representing the error
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun int utilfdt_read_err(const char *filename, char **buffp, size_t *len);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /**
112*4882a593Smuzhiyun  * Write a device tree buffer to a file. This will report any errors on
113*4882a593Smuzhiyun  * stderr.
114*4882a593Smuzhiyun  *
115*4882a593Smuzhiyun  * @param filename	The filename to write, or - for stdout
116*4882a593Smuzhiyun  * @param blob		Pointer to buffer containing fdt
117*4882a593Smuzhiyun  * @return 0 if ok, -1 on error
118*4882a593Smuzhiyun  */
119*4882a593Smuzhiyun int utilfdt_write(const char *filename, const void *blob);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun /**
122*4882a593Smuzhiyun  * Write a device tree buffer to a file. Does not report errors, but only
123*4882a593Smuzhiyun  * returns them. The value returned can be passed to strerror() to obtain
124*4882a593Smuzhiyun  * an error message for the user.
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  * @param filename	The filename to write, or - for stdout
127*4882a593Smuzhiyun  * @param blob		Pointer to buffer containing fdt
128*4882a593Smuzhiyun  * @return 0 if ok, else an errno value representing the error
129*4882a593Smuzhiyun  */
130*4882a593Smuzhiyun int utilfdt_write_err(const char *filename, const void *blob);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun  * Decode a data type string. The purpose of this string
134*4882a593Smuzhiyun  *
135*4882a593Smuzhiyun  * The string consists of an optional character followed by the type:
136*4882a593Smuzhiyun  *	Modifier characters:
137*4882a593Smuzhiyun  *		hh or b	1 byte
138*4882a593Smuzhiyun  *		h	2 byte
139*4882a593Smuzhiyun  *		l	4 byte, default
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  *	Type character:
142*4882a593Smuzhiyun  *		s	string
143*4882a593Smuzhiyun  *		i	signed integer
144*4882a593Smuzhiyun  *		u	unsigned integer
145*4882a593Smuzhiyun  *		x	hex
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * TODO: Implement ll modifier (8 bytes)
148*4882a593Smuzhiyun  * TODO: Implement o type (octal)
149*4882a593Smuzhiyun  *
150*4882a593Smuzhiyun  * @param fmt		Format string to process
151*4882a593Smuzhiyun  * @param type		Returns type found(s/d/u/x), or 0 if none
152*4882a593Smuzhiyun  * @param size		Returns size found(1,2,4,8) or 4 if none
153*4882a593Smuzhiyun  * @return 0 if ok, -1 on error (no type given, or other invalid format)
154*4882a593Smuzhiyun  */
155*4882a593Smuzhiyun int utilfdt_decode_type(const char *fmt, int *type, int *size);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun  * This is a usage message fragment for the -t option. It is the format
159*4882a593Smuzhiyun  * supported by utilfdt_decode_type.
160*4882a593Smuzhiyun  */
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun #define USAGE_TYPE_MSG \
163*4882a593Smuzhiyun 	"<type>\ts=string, i=int, u=unsigned, x=hex\n" \
164*4882a593Smuzhiyun 	"\tOptional modifier prefix:\n" \
165*4882a593Smuzhiyun 	"\t\thh or b=byte, h=2 byte, l=4 byte (default)";
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun  * Print property data in a readable format to stdout
169*4882a593Smuzhiyun  *
170*4882a593Smuzhiyun  * Properties that look like strings will be printed as strings. Otherwise
171*4882a593Smuzhiyun  * the data will be displayed either as cells (if len is a multiple of 4
172*4882a593Smuzhiyun  * bytes) or bytes.
173*4882a593Smuzhiyun  *
174*4882a593Smuzhiyun  * If len is 0 then this function does nothing.
175*4882a593Smuzhiyun  *
176*4882a593Smuzhiyun  * @param data	Pointers to property data
177*4882a593Smuzhiyun  * @param len	Length of property data
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun void utilfdt_print_data(const char *data, int len);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun /**
182*4882a593Smuzhiyun  * Show source version and exit
183*4882a593Smuzhiyun  */
184*4882a593Smuzhiyun void NORETURN util_version(void);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /**
187*4882a593Smuzhiyun  * Show usage and exit
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * This helps standardize the output of various utils.  You most likely want
190*4882a593Smuzhiyun  * to use the usage() helper below rather than call this.
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * @param errmsg	If non-NULL, an error message to display
193*4882a593Smuzhiyun  * @param synopsis	The initial example usage text (and possible examples)
194*4882a593Smuzhiyun  * @param short_opts	The string of short options
195*4882a593Smuzhiyun  * @param long_opts	The structure of long options
196*4882a593Smuzhiyun  * @param opts_help	An array of help strings (should align with long_opts)
197*4882a593Smuzhiyun  */
198*4882a593Smuzhiyun void NORETURN util_usage(const char *errmsg, const char *synopsis,
199*4882a593Smuzhiyun 			 const char *short_opts,
200*4882a593Smuzhiyun 			 struct option const long_opts[],
201*4882a593Smuzhiyun 			 const char * const opts_help[]);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /**
204*4882a593Smuzhiyun  * Show usage and exit
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * If you name all your usage variables with usage_xxx, then you can call this
207*4882a593Smuzhiyun  * help macro rather than expanding all arguments yourself.
208*4882a593Smuzhiyun  *
209*4882a593Smuzhiyun  * @param errmsg	If non-NULL, an error message to display
210*4882a593Smuzhiyun  */
211*4882a593Smuzhiyun #define usage(errmsg) \
212*4882a593Smuzhiyun 	util_usage(errmsg, usage_synopsis, usage_short_opts, \
213*4882a593Smuzhiyun 		   usage_long_opts, usage_opts_help)
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun /**
216*4882a593Smuzhiyun  * Call getopt_long() with standard options
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  * Since all util code runs getopt in the same way, provide a helper.
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun #define util_getopt_long() getopt_long(argc, argv, usage_short_opts, \
221*4882a593Smuzhiyun 				       usage_long_opts, NULL)
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun /* Helper for aligning long_opts array */
224*4882a593Smuzhiyun #define a_argument required_argument
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /* Helper for usage_short_opts string constant */
227*4882a593Smuzhiyun #define USAGE_COMMON_SHORT_OPTS "hV"
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun /* Helper for usage_long_opts option array */
230*4882a593Smuzhiyun #define USAGE_COMMON_LONG_OPTS \
231*4882a593Smuzhiyun 	{"help",      no_argument, NULL, 'h'}, \
232*4882a593Smuzhiyun 	{"version",   no_argument, NULL, 'V'}, \
233*4882a593Smuzhiyun 	{NULL,        no_argument, NULL, 0x0}
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun /* Helper for usage_opts_help array */
236*4882a593Smuzhiyun #define USAGE_COMMON_OPTS_HELP \
237*4882a593Smuzhiyun 	"Print this help and exit", \
238*4882a593Smuzhiyun 	"Print version and exit", \
239*4882a593Smuzhiyun 	NULL
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun /* Helper for getopt case statements */
242*4882a593Smuzhiyun #define case_USAGE_COMMON_FLAGS \
243*4882a593Smuzhiyun 	case 'h': usage(NULL); \
244*4882a593Smuzhiyun 	case 'V': util_version(); \
245*4882a593Smuzhiyun 	case '?': usage("unknown option");
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun #endif /* UTIL_H */
248