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