util.h 7.2 KB

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