Blame util.h

Packit Service 0ee8e1
/* SPDX-License-Identifier: GPL-2.0-or-later */
Packit 2ad57b
#ifndef UTIL_H
Packit 2ad57b
#define UTIL_H
Packit 2ad57b
Packit 2ad57b
#include <stdarg.h>
Packit 2ad57b
#include <stdbool.h>
Packit 2ad57b
#include <getopt.h>
Packit 2ad57b
Packit 2ad57b
/*
Packit 2ad57b
 * Copyright 2011 The Chromium Authors, All Rights Reserved.
Packit 2ad57b
 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
Packit 2ad57b
 */
Packit 2ad57b
Packit 2ad57b
#ifdef __GNUC__
Packit Service 0ee8e1
#ifdef __clang__
Packit 2ad57b
#define PRINTF(i, j)	__attribute__((format (printf, i, j)))
Packit Service 0ee8e1
#else
Packit Service 0ee8e1
#define PRINTF(i, j)	__attribute__((format (gnu_printf, i, j)))
Packit Service 0ee8e1
#endif
Packit 2ad57b
#define NORETURN	__attribute__((noreturn))
Packit 2ad57b
#else
Packit 2ad57b
#define PRINTF(i, j)
Packit 2ad57b
#define NORETURN
Packit 2ad57b
#endif
Packit 2ad57b
Packit 2ad57b
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Packit 2ad57b
Packit 2ad57b
#define stringify(s)	stringify_(s)
Packit 2ad57b
#define stringify_(s)	#s
Packit 2ad57b
Packit 2ad57b
static inline void NORETURN PRINTF(1, 2) die(const char *str, ...)
Packit 2ad57b
{
Packit 2ad57b
	va_list ap;
Packit 2ad57b
Packit 2ad57b
	va_start(ap, str);
Packit 2ad57b
	fprintf(stderr, "FATAL ERROR: ");
Packit 2ad57b
	vfprintf(stderr, str, ap);
Packit 2ad57b
	va_end(ap);
Packit 2ad57b
	exit(1);
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
static inline void *xmalloc(size_t len)
Packit 2ad57b
{
Packit 2ad57b
	void *new = malloc(len);
Packit 2ad57b
Packit 2ad57b
	if (!new)
Packit 2ad57b
		die("malloc() failed\n");
Packit 2ad57b
Packit 2ad57b
	return new;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
static inline void *xrealloc(void *p, size_t len)
Packit 2ad57b
{
Packit 2ad57b
	void *new = realloc(p, len);
Packit 2ad57b
Packit 2ad57b
	if (!new)
Packit 2ad57b
		die("realloc() failed (len=%zd)\n", len);
Packit 2ad57b
Packit 2ad57b
	return new;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
extern char *xstrdup(const char *s);
Packit 2ad57b
Packit 2ad57b
extern int PRINTF(2, 3) xasprintf(char **strp, const char *fmt, ...);
Packit Service 0ee8e1
extern int PRINTF(2, 3) xasprintf_append(char **strp, const char *fmt, ...);
Packit Service 0ee8e1
extern int xavsprintf_append(char **strp, const char *fmt, va_list ap);
Packit 2ad57b
extern char *join_path(const char *path, const char *name);
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Check a property of a given length to see if it is all printable and
Packit 2ad57b
 * has a valid terminator. The property can contain either a single string,
Packit 2ad57b
 * or multiple strings each of non-zero length.
Packit 2ad57b
 *
Packit 2ad57b
 * @param data	The string to check
Packit 2ad57b
 * @param len	The string length including terminator
Packit 2ad57b
 * @return 1 if a valid printable string, 0 if not
Packit 2ad57b
 */
Packit 2ad57b
bool util_is_printable_string(const void *data, int len);
Packit 2ad57b
Packit 2ad57b
/*
Packit 2ad57b
 * Parse an escaped character starting at index i in string s.  The resulting
Packit 2ad57b
 * character will be returned and the index i will be updated to point at the
Packit 2ad57b
 * character directly after the end of the encoding, this may be the '\0'
Packit 2ad57b
 * terminator of the string.
Packit 2ad57b
 */
Packit 2ad57b
char get_escape_char(const char *s, int *i);
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Read a device tree file into a buffer. This will report any errors on
Packit 2ad57b
 * stderr.
Packit 2ad57b
 *
Packit 2ad57b
 * @param filename	The filename to read, or - for stdin
Packit 2ad57b
 * @param len		If non-NULL, the amount of data we managed to read
Packit Service 0ee8e1
 * @return Pointer to allocated buffer containing fdt, or NULL on error
Packit 2ad57b
 */
Packit Service 0ee8e1
char *utilfdt_read(const char *filename, size_t *len);
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Read a device tree file into a buffer. Does not report errors, but only
Packit 2ad57b
 * returns them. The value returned can be passed to strerror() to obtain
Packit 2ad57b
 * an error message for the user.
Packit 2ad57b
 *
Packit 2ad57b
 * @param filename	The filename to read, or - for stdin
Packit 2ad57b
 * @param buffp		Returns pointer to buffer containing fdt
Packit 2ad57b
 * @param len		If non-NULL, the amount of data we managed to read
Packit Service 0ee8e1
 * @return 0 if ok, else an errno value representing the error
Packit 2ad57b
 */
Packit Service 0ee8e1
int utilfdt_read_err(const char *filename, char **buffp, size_t *len);
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Write a device tree buffer to a file. This will report any errors on
Packit 2ad57b
 * stderr.
Packit 2ad57b
 *
Packit 2ad57b
 * @param filename	The filename to write, or - for stdout
Packit Service 0ee8e1
 * @param blob		Pointer to buffer containing fdt
Packit 2ad57b
 * @return 0 if ok, -1 on error
Packit 2ad57b
 */
Packit 2ad57b
int utilfdt_write(const char *filename, const void *blob);
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Write a device tree buffer to a file. Does not report errors, but only
Packit 2ad57b
 * returns them. The value returned can be passed to strerror() to obtain
Packit 2ad57b
 * an error message for the user.
Packit 2ad57b
 *
Packit 2ad57b
 * @param filename	The filename to write, or - for stdout
Packit Service 0ee8e1
 * @param blob		Pointer to buffer containing fdt
Packit 2ad57b
 * @return 0 if ok, else an errno value representing the error
Packit 2ad57b
 */
Packit 2ad57b
int utilfdt_write_err(const char *filename, const void *blob);
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Decode a data type string. The purpose of this string
Packit 2ad57b
 *
Packit 2ad57b
 * The string consists of an optional character followed by the type:
Packit 2ad57b
 *	Modifier characters:
Packit 2ad57b
 *		hh or b	1 byte
Packit 2ad57b
 *		h	2 byte
Packit 2ad57b
 *		l	4 byte, default
Packit 2ad57b
 *
Packit 2ad57b
 *	Type character:
Packit 2ad57b
 *		s	string
Packit 2ad57b
 *		i	signed integer
Packit 2ad57b
 *		u	unsigned integer
Packit 2ad57b
 *		x	hex
Packit 2ad57b
 *
Packit 2ad57b
 * TODO: Implement ll modifier (8 bytes)
Packit 2ad57b
 * TODO: Implement o type (octal)
Packit 2ad57b
 *
Packit 2ad57b
 * @param fmt		Format string to process
Packit 2ad57b
 * @param type		Returns type found(s/d/u/x), or 0 if none
Packit 2ad57b
 * @param size		Returns size found(1,2,4,8) or 4 if none
Packit 2ad57b
 * @return 0 if ok, -1 on error (no type given, or other invalid format)
Packit 2ad57b
 */
Packit 2ad57b
int utilfdt_decode_type(const char *fmt, int *type, int *size);
Packit 2ad57b
Packit 2ad57b
/*
Packit 2ad57b
 * This is a usage message fragment for the -t option. It is the format
Packit 2ad57b
 * supported by utilfdt_decode_type.
Packit 2ad57b
 */
Packit 2ad57b
Packit 2ad57b
#define USAGE_TYPE_MSG \
Packit 2ad57b
	"<type>\ts=string, i=int, u=unsigned, x=hex\n" \
Packit 2ad57b
	"\tOptional modifier prefix:\n" \
Packit 2ad57b
	"\t\thh or b=byte, h=2 byte, l=4 byte (default)";
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Print property data in a readable format to stdout
Packit 2ad57b
 *
Packit 2ad57b
 * Properties that look like strings will be printed as strings. Otherwise
Packit 2ad57b
 * the data will be displayed either as cells (if len is a multiple of 4
Packit 2ad57b
 * bytes) or bytes.
Packit 2ad57b
 *
Packit 2ad57b
 * If len is 0 then this function does nothing.
Packit 2ad57b
 *
Packit 2ad57b
 * @param data	Pointers to property data
Packit 2ad57b
 * @param len	Length of property data
Packit 2ad57b
 */
Packit 2ad57b
void utilfdt_print_data(const char *data, int len);
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Show source version and exit
Packit 2ad57b
 */
Packit 2ad57b
void NORETURN util_version(void);
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Show usage and exit
Packit 2ad57b
 *
Packit 2ad57b
 * This helps standardize the output of various utils.  You most likely want
Packit 2ad57b
 * to use the usage() helper below rather than call this.
Packit 2ad57b
 *
Packit 2ad57b
 * @param errmsg	If non-NULL, an error message to display
Packit 2ad57b
 * @param synopsis	The initial example usage text (and possible examples)
Packit 2ad57b
 * @param short_opts	The string of short options
Packit 2ad57b
 * @param long_opts	The structure of long options
Packit 2ad57b
 * @param opts_help	An array of help strings (should align with long_opts)
Packit 2ad57b
 */
Packit 2ad57b
void NORETURN util_usage(const char *errmsg, const char *synopsis,
Packit 2ad57b
			 const char *short_opts,
Packit 2ad57b
			 struct option const long_opts[],
Packit 2ad57b
			 const char * const opts_help[]);
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Show usage and exit
Packit 2ad57b
 *
Packit 2ad57b
 * If you name all your usage variables with usage_xxx, then you can call this
Packit 2ad57b
 * help macro rather than expanding all arguments yourself.
Packit 2ad57b
 *
Packit 2ad57b
 * @param errmsg	If non-NULL, an error message to display
Packit 2ad57b
 */
Packit 2ad57b
#define usage(errmsg) \
Packit 2ad57b
	util_usage(errmsg, usage_synopsis, usage_short_opts, \
Packit 2ad57b
		   usage_long_opts, usage_opts_help)
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Call getopt_long() with standard options
Packit 2ad57b
 *
Packit 2ad57b
 * Since all util code runs getopt in the same way, provide a helper.
Packit 2ad57b
 */
Packit 2ad57b
#define util_getopt_long() getopt_long(argc, argv, usage_short_opts, \
Packit 2ad57b
				       usage_long_opts, NULL)
Packit 2ad57b
Packit 2ad57b
/* Helper for aligning long_opts array */
Packit 2ad57b
#define a_argument required_argument
Packit 2ad57b
Packit 2ad57b
/* Helper for usage_short_opts string constant */
Packit 2ad57b
#define USAGE_COMMON_SHORT_OPTS "hV"
Packit 2ad57b
Packit 2ad57b
/* Helper for usage_long_opts option array */
Packit 2ad57b
#define USAGE_COMMON_LONG_OPTS \
Packit 2ad57b
	{"help",      no_argument, NULL, 'h'}, \
Packit 2ad57b
	{"version",   no_argument, NULL, 'V'}, \
Packit 2ad57b
	{NULL,        no_argument, NULL, 0x0}
Packit 2ad57b
Packit 2ad57b
/* Helper for usage_opts_help array */
Packit 2ad57b
#define USAGE_COMMON_OPTS_HELP \
Packit 2ad57b
	"Print this help and exit", \
Packit 2ad57b
	"Print version and exit", \
Packit 2ad57b
	NULL
Packit 2ad57b
Packit 2ad57b
/* Helper for getopt case statements */
Packit 2ad57b
#define case_USAGE_COMMON_FLAGS \
Packit 2ad57b
	case 'h': usage(NULL); \
Packit 2ad57b
	case 'V': util_version(); \
Packit 2ad57b
	case '?': usage("unknown option");
Packit 2ad57b
Packit 2ad57b
#endif /* UTIL_H */