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