util.h revision 261215
1204433Sraj#ifndef _UTIL_H 2204433Sraj#define _UTIL_H 3204433Sraj 4238742Simp#include <stdarg.h> 5261215Simp#include <stdbool.h> 6261215Simp#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 28261215Simp#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 29261215Simp 30261215Simpstatic 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/** 64261215Simp * Check a property of a given length to see if it is all printable and 65261215Simp * has a valid terminator. The property can contain either a single string, 66261215Simp * or multiple strings each of non-zero length. 67238742Simp * 68238742Simp * @param data The string to check 69238742Simp * @param len The string length including terminator 70261215Simp * @return 1 if a valid printable string, 0 if not 71261215Simp */ 72261215Simpbool 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/** 92261215Simp * Like utilfdt_read(), but also passes back the size of the file read. 93261215Simp * 94261215Simp * @param len If non-NULL, the amount of data we managed to read 95261215Simp */ 96261215Simpchar *utilfdt_read_len(const char *filename, off_t *len); 97261215Simp 98261215Simp/** 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 109261215Simp/** 110261215Simp * Like utilfdt_read_err(), but also passes back the size of the file read. 111261215Simp * 112261215Simp * @param len If non-NULL, the amount of data we managed to read 113261215Simp */ 114261215Simpint 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" \ 170261215Simp "\t\thh or b=byte, h=2 byte, l=4 byte (default)"; 171238742Simp 172261215Simp/** 173261215Simp * Print property data in a readable format to stdout 174261215Simp * 175261215Simp * Properties that look like strings will be printed as strings. Otherwise 176261215Simp * the data will be displayed either as cells (if len is a multiple of 4 177261215Simp * bytes) or bytes. 178261215Simp * 179261215Simp * If len is 0 then this function does nothing. 180261215Simp * 181261215Simp * @param data Pointers to property data 182261215Simp * @param len Length of property data 183261215Simp */ 184261215Simpvoid utilfdt_print_data(const char *data, int len); 185261215Simp 186261215Simp/** 187261215Simp * Show source version and exit 188261215Simp */ 189261215Simpvoid util_version(void) __attribute__((noreturn)); 190261215Simp 191261215Simp/** 192261215Simp * Show usage and exit 193261215Simp * 194261215Simp * This helps standardize the output of various utils. You most likely want 195261215Simp * to use the usage() helper below rather than call this. 196261215Simp * 197261215Simp * @param errmsg If non-NULL, an error message to display 198261215Simp * @param synopsis The initial example usage text (and possible examples) 199261215Simp * @param short_opts The string of short options 200261215Simp * @param long_opts The structure of long options 201261215Simp * @param opts_help An array of help strings (should align with long_opts) 202261215Simp */ 203261215Simpvoid util_usage(const char *errmsg, const char *synopsis, 204261215Simp const char *short_opts, struct option const long_opts[], 205261215Simp const char * const opts_help[]) __attribute__((noreturn)); 206261215Simp 207261215Simp/** 208261215Simp * Show usage and exit 209261215Simp * 210261215Simp * If you name all your usage variables with usage_xxx, then you can call this 211261215Simp * help macro rather than expanding all arguments yourself. 212261215Simp * 213261215Simp * @param errmsg If non-NULL, an error message to display 214261215Simp */ 215261215Simp#define usage(errmsg) \ 216261215Simp util_usage(errmsg, usage_synopsis, usage_short_opts, \ 217261215Simp usage_long_opts, usage_opts_help) 218261215Simp 219261215Simp/** 220261215Simp * Call getopt_long() with standard options 221261215Simp * 222261215Simp * Since all util code runs getopt in the same way, provide a helper. 223261215Simp */ 224261215Simp#define util_getopt_long() getopt_long(argc, argv, usage_short_opts, \ 225261215Simp usage_long_opts, NULL) 226261215Simp 227261215Simp/* Helper for aligning long_opts array */ 228261215Simp#define a_argument required_argument 229261215Simp 230261215Simp/* Helper for usage_short_opts string constant */ 231261215Simp#define USAGE_COMMON_SHORT_OPTS "hV" 232261215Simp 233261215Simp/* Helper for usage_long_opts option array */ 234261215Simp#define USAGE_COMMON_LONG_OPTS \ 235261215Simp {"help", no_argument, NULL, 'h'}, \ 236261215Simp {"version", no_argument, NULL, 'V'}, \ 237261215Simp {NULL, no_argument, NULL, 0x0} 238261215Simp 239261215Simp/* Helper for usage_opts_help array */ 240261215Simp#define USAGE_COMMON_OPTS_HELP \ 241261215Simp "Print this help and exit", \ 242261215Simp "Print version and exit", \ 243261215Simp NULL 244261215Simp 245261215Simp/* Helper for getopt case statements */ 246261215Simp#define case_USAGE_COMMON_FLAGS \ 247261215Simp case 'h': usage(NULL); \ 248261215Simp case 'V': util_version(); \ 249261215Simp case '?': usage("unknown option"); 250261215Simp 251204433Sraj#endif /* _UTIL_H */ 252