1204433Sraj#ifndef _UTIL_H
2204433Sraj#define _UTIL_H
3204433Sraj
4238742Simp#include <stdarg.h>
5266130Sian#include <stdbool.h>
6266130Sian#include <getopt.h>
7238742Simp
8204433Sraj/*
9238742Simp * Copyright 2011 The Chromium Authors, All Rights Reserved.
10204433Sraj * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
11204433Sraj *
12204433Sraj * This program is free software; you can redistribute it and/or
13204433Sraj * modify it under the terms of the GNU General Public License as
14204433Sraj * published by the Free Software Foundation; either version 2 of the
15204433Sraj * License, or (at your option) any later version.
16204433Sraj *
17204433Sraj *  This program is distributed in the hope that it will be useful,
18204433Sraj *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19204433Sraj *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20204433Sraj *  General Public License for more details.
21204433Sraj *
22204433Sraj *  You should have received a copy of the GNU General Public License
23204433Sraj *  along with this program; if not, write to the Free Software
24204433Sraj *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
25204433Sraj *                                                                   USA
26204433Sraj */
27204433Sraj
28266130Sian#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
29266130Sian
30266130Sianstatic inline void __attribute__((noreturn)) die(const char *str, ...)
31204433Sraj{
32204433Sraj	va_list ap;
33204433Sraj
34204433Sraj	va_start(ap, str);
35204433Sraj	fprintf(stderr, "FATAL ERROR: ");
36204433Sraj	vfprintf(stderr, str, ap);
37204433Sraj	exit(1);
38204433Sraj}
39204433Sraj
40204433Srajstatic inline void *xmalloc(size_t len)
41204433Sraj{
42204433Sraj	void *new = malloc(len);
43204433Sraj
44204433Sraj	if (!new)
45204433Sraj		die("malloc() failed\n");
46204433Sraj
47204433Sraj	return new;
48204433Sraj}
49204433Sraj
50204433Srajstatic inline void *xrealloc(void *p, size_t len)
51204433Sraj{
52204433Sraj	void *new = realloc(p, len);
53204433Sraj
54204433Sraj	if (!new)
55204433Sraj		die("realloc() failed (len=%d)\n", len);
56204433Sraj
57204433Sraj	return new;
58204433Sraj}
59204433Sraj
60204433Srajextern char *xstrdup(const char *s);
61238742Simpextern char *join_path(const char *path, const char *name);
62204433Sraj
63238742Simp/**
64266130Sian * Check a property of a given length to see if it is all printable and
65266130Sian * has a valid terminator. The property can contain either a single string,
66266130Sian * or multiple strings each of non-zero length.
67238742Simp *
68238742Simp * @param data	The string to check
69238742Simp * @param len	The string length including terminator
70266130Sian * @return 1 if a valid printable string, 0 if not
71266130Sian */
72266130Sianbool util_is_printable_string(const void *data, int len);
73238742Simp
74238742Simp/*
75238742Simp * Parse an escaped character starting at index i in string s.  The resulting
76238742Simp * character will be returned and the index i will be updated to point at the
77238742Simp * character directly after the end of the encoding, this may be the '\0'
78238742Simp * terminator of the string.
79238742Simp */
80238742Simpchar get_escape_char(const char *s, int *i);
81238742Simp
82238742Simp/**
83238742Simp * Read a device tree file into a buffer. This will report any errors on
84238742Simp * stderr.
85238742Simp *
86238742Simp * @param filename	The filename to read, or - for stdin
87238742Simp * @return Pointer to allocated buffer containing fdt, or NULL on error
88238742Simp */
89238742Simpchar *utilfdt_read(const char *filename);
90238742Simp
91238742Simp/**
92266130Sian * Like utilfdt_read(), but also passes back the size of the file read.
93266130Sian *
94266130Sian * @param len		If non-NULL, the amount of data we managed to read
95266130Sian */
96266130Sianchar *utilfdt_read_len(const char *filename, off_t *len);
97266130Sian
98266130Sian/**
99238742Simp * Read a device tree file into a buffer. Does not report errors, but only
100238742Simp * returns them. The value returned can be passed to strerror() to obtain
101238742Simp * an error message for the user.
102238742Simp *
103238742Simp * @param filename	The filename to read, or - for stdin
104238742Simp * @param buffp		Returns pointer to buffer containing fdt
105238742Simp * @return 0 if ok, else an errno value representing the error
106238742Simp */
107238742Simpint utilfdt_read_err(const char *filename, char **buffp);
108238742Simp
109266130Sian/**
110266130Sian * Like utilfdt_read_err(), but also passes back the size of the file read.
111266130Sian *
112266130Sian * @param len		If non-NULL, the amount of data we managed to read
113266130Sian */
114266130Sianint utilfdt_read_err_len(const char *filename, char **buffp, off_t *len);
115238742Simp
116238742Simp/**
117238742Simp * Write a device tree buffer to a file. This will report any errors on
118238742Simp * stderr.
119238742Simp *
120238742Simp * @param filename	The filename to write, or - for stdout
121238742Simp * @param blob		Poiner to buffer containing fdt
122238742Simp * @return 0 if ok, -1 on error
123238742Simp */
124238742Simpint utilfdt_write(const char *filename, const void *blob);
125238742Simp
126238742Simp/**
127238742Simp * Write a device tree buffer to a file. Does not report errors, but only
128238742Simp * returns them. The value returned can be passed to strerror() to obtain
129238742Simp * an error message for the user.
130238742Simp *
131238742Simp * @param filename	The filename to write, or - for stdout
132238742Simp * @param blob		Poiner to buffer containing fdt
133238742Simp * @return 0 if ok, else an errno value representing the error
134238742Simp */
135238742Simpint utilfdt_write_err(const char *filename, const void *blob);
136238742Simp
137238742Simp/**
138238742Simp * Decode a data type string. The purpose of this string
139238742Simp *
140238742Simp * The string consists of an optional character followed by the type:
141238742Simp *	Modifier characters:
142238742Simp *		hh or b	1 byte
143238742Simp *		h	2 byte
144238742Simp *		l	4 byte, default
145238742Simp *
146238742Simp *	Type character:
147238742Simp *		s	string
148238742Simp *		i	signed integer
149238742Simp *		u	unsigned integer
150238742Simp *		x	hex
151238742Simp *
152238742Simp * TODO: Implement ll modifier (8 bytes)
153238742Simp * TODO: Implement o type (octal)
154238742Simp *
155238742Simp * @param fmt		Format string to process
156238742Simp * @param type		Returns type found(s/d/u/x), or 0 if none
157238742Simp * @param size		Returns size found(1,2,4,8) or 4 if none
158238742Simp * @return 0 if ok, -1 on error (no type given, or other invalid format)
159238742Simp */
160238742Simpint utilfdt_decode_type(const char *fmt, int *type, int *size);
161238742Simp
162238742Simp/*
163238742Simp * This is a usage message fragment for the -t option. It is the format
164238742Simp * supported by utilfdt_decode_type.
165238742Simp */
166238742Simp
167238742Simp#define USAGE_TYPE_MSG \
168238742Simp	"<type>\ts=string, i=int, u=unsigned, x=hex\n" \
169238742Simp	"\tOptional modifier prefix:\n" \
170266130Sian	"\t\thh or b=byte, h=2 byte, l=4 byte (default)";
171238742Simp
172266130Sian/**
173266130Sian * Print property data in a readable format to stdout
174266130Sian *
175266130Sian * Properties that look like strings will be printed as strings. Otherwise
176266130Sian * the data will be displayed either as cells (if len is a multiple of 4
177266130Sian * bytes) or bytes.
178266130Sian *
179266130Sian * If len is 0 then this function does nothing.
180266130Sian *
181266130Sian * @param data	Pointers to property data
182266130Sian * @param len	Length of property data
183266130Sian */
184266130Sianvoid utilfdt_print_data(const char *data, int len);
185266130Sian
186266130Sian/**
187266130Sian * Show source version and exit
188266130Sian */
189266130Sianvoid util_version(void) __attribute__((noreturn));
190266130Sian
191266130Sian/**
192266130Sian * Show usage and exit
193266130Sian *
194266130Sian * This helps standardize the output of various utils.  You most likely want
195266130Sian * to use the usage() helper below rather than call this.
196266130Sian *
197266130Sian * @param errmsg	If non-NULL, an error message to display
198266130Sian * @param synopsis	The initial example usage text (and possible examples)
199266130Sian * @param short_opts	The string of short options
200266130Sian * @param long_opts	The structure of long options
201266130Sian * @param opts_help	An array of help strings (should align with long_opts)
202266130Sian */
203266130Sianvoid util_usage(const char *errmsg, const char *synopsis,
204266130Sian		const char *short_opts, struct option const long_opts[],
205266130Sian		const char * const opts_help[]) __attribute__((noreturn));
206266130Sian
207266130Sian/**
208266130Sian * Show usage and exit
209266130Sian *
210266130Sian * If you name all your usage variables with usage_xxx, then you can call this
211266130Sian * help macro rather than expanding all arguments yourself.
212266130Sian *
213266130Sian * @param errmsg	If non-NULL, an error message to display
214266130Sian */
215266130Sian#define usage(errmsg) \
216266130Sian	util_usage(errmsg, usage_synopsis, usage_short_opts, \
217266130Sian		   usage_long_opts, usage_opts_help)
218266130Sian
219266130Sian/**
220266130Sian * Call getopt_long() with standard options
221266130Sian *
222266130Sian * Since all util code runs getopt in the same way, provide a helper.
223266130Sian */
224266130Sian#define util_getopt_long() getopt_long(argc, argv, usage_short_opts, \
225266130Sian				       usage_long_opts, NULL)
226266130Sian
227266130Sian/* Helper for aligning long_opts array */
228266130Sian#define a_argument required_argument
229266130Sian
230266130Sian/* Helper for usage_short_opts string constant */
231266130Sian#define USAGE_COMMON_SHORT_OPTS "hV"
232266130Sian
233266130Sian/* Helper for usage_long_opts option array */
234266130Sian#define USAGE_COMMON_LONG_OPTS \
235266130Sian	{"help",      no_argument, NULL, 'h'}, \
236266130Sian	{"version",   no_argument, NULL, 'V'}, \
237266130Sian	{NULL,        no_argument, NULL, 0x0}
238266130Sian
239266130Sian/* Helper for usage_opts_help array */
240266130Sian#define USAGE_COMMON_OPTS_HELP \
241266130Sian	"Print this help and exit", \
242266130Sian	"Print version and exit", \
243266130Sian	NULL
244266130Sian
245266130Sian/* Helper for getopt case statements */
246266130Sian#define case_USAGE_COMMON_FLAGS \
247266130Sian	case 'h': usage(NULL); \
248266130Sian	case 'V': util_version(); \
249266130Sian	case '?': usage("unknown option");
250266130Sian
251204433Sraj#endif /* _UTIL_H */
252