diff options
Diffstat (limited to 'scripts/dtc/util.h')
| -rw-r--r-- | scripts/dtc/util.h | 107 |
1 files changed, 102 insertions, 5 deletions
diff --git a/scripts/dtc/util.h b/scripts/dtc/util.h index c8eb45d9f04b..8f40b4499359 100644 --- a/scripts/dtc/util.h +++ b/scripts/dtc/util.h | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | #define _UTIL_H | 2 | #define _UTIL_H |
| 3 | 3 | ||
| 4 | #include <stdarg.h> | 4 | #include <stdarg.h> |
| 5 | #include <getopt.h> | ||
| 5 | 6 | ||
| 6 | /* | 7 | /* |
| 7 | * Copyright 2011 The Chromium Authors, All Rights Reserved. | 8 | * Copyright 2011 The Chromium Authors, All Rights Reserved. |
| @@ -23,7 +24,9 @@ | |||
| 23 | * USA | 24 | * USA |
| 24 | */ | 25 | */ |
| 25 | 26 | ||
| 26 | static inline void __attribute__((noreturn)) die(char * str, ...) | 27 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 28 | |||
| 29 | static inline void __attribute__((noreturn)) die(const char *str, ...) | ||
| 27 | { | 30 | { |
| 28 | va_list ap; | 31 | va_list ap; |
| 29 | 32 | ||
| @@ -57,12 +60,14 @@ extern char *xstrdup(const char *s); | |||
| 57 | extern char *join_path(const char *path, const char *name); | 60 | extern char *join_path(const char *path, const char *name); |
| 58 | 61 | ||
| 59 | /** | 62 | /** |
| 60 | * Check a string of a given length to see if it is all printable and | 63 | * Check a property of a given length to see if it is all printable and |
| 61 | * has a valid terminator. | 64 | * has a valid terminator. The property can contain either a single string, |
| 65 | * or multiple strings each of non-zero length. | ||
| 62 | * | 66 | * |
| 63 | * @param data The string to check | 67 | * @param data The string to check |
| 64 | * @param len The string length including terminator | 68 | * @param len The string length including terminator |
| 65 | * @return 1 if a valid printable string, 0 if not */ | 69 | * @return 1 if a valid printable string, 0 if not |
| 70 | */ | ||
| 66 | int util_is_printable_string(const void *data, int len); | 71 | int util_is_printable_string(const void *data, int len); |
| 67 | 72 | ||
| 68 | /* | 73 | /* |
| @@ -83,6 +88,13 @@ char get_escape_char(const char *s, int *i); | |||
| 83 | char *utilfdt_read(const char *filename); | 88 | char *utilfdt_read(const char *filename); |
| 84 | 89 | ||
| 85 | /** | 90 | /** |
| 91 | * Like utilfdt_read(), but also passes back the size of the file read. | ||
| 92 | * | ||
| 93 | * @param len If non-NULL, the amount of data we managed to read | ||
| 94 | */ | ||
| 95 | char *utilfdt_read_len(const char *filename, off_t *len); | ||
| 96 | |||
| 97 | /** | ||
| 86 | * Read a device tree file into a buffer. Does not report errors, but only | 98 | * Read a device tree file into a buffer. Does not report errors, but only |
| 87 | * returns them. The value returned can be passed to strerror() to obtain | 99 | * returns them. The value returned can be passed to strerror() to obtain |
| 88 | * an error message for the user. | 100 | * an error message for the user. |
| @@ -93,6 +105,12 @@ char *utilfdt_read(const char *filename); | |||
| 93 | */ | 105 | */ |
| 94 | int utilfdt_read_err(const char *filename, char **buffp); | 106 | int utilfdt_read_err(const char *filename, char **buffp); |
| 95 | 107 | ||
| 108 | /** | ||
| 109 | * Like utilfdt_read_err(), but also passes back the size of the file read. | ||
| 110 | * | ||
| 111 | * @param len If non-NULL, the amount of data we managed to read | ||
| 112 | */ | ||
| 113 | int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len); | ||
| 96 | 114 | ||
| 97 | /** | 115 | /** |
| 98 | * Write a device tree buffer to a file. This will report any errors on | 116 | * Write a device tree buffer to a file. This will report any errors on |
| @@ -148,6 +166,85 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size); | |||
| 148 | #define USAGE_TYPE_MSG \ | 166 | #define USAGE_TYPE_MSG \ |
| 149 | "<type>\ts=string, i=int, u=unsigned, x=hex\n" \ | 167 | "<type>\ts=string, i=int, u=unsigned, x=hex\n" \ |
| 150 | "\tOptional modifier prefix:\n" \ | 168 | "\tOptional modifier prefix:\n" \ |
| 151 | "\t\thh or b=byte, h=2 byte, l=4 byte (default)\n"; | 169 | "\t\thh or b=byte, h=2 byte, l=4 byte (default)"; |
| 170 | |||
| 171 | /** | ||
| 172 | * Print property data in a readable format to stdout | ||
| 173 | * | ||
| 174 | * Properties that look like strings will be printed as strings. Otherwise | ||
| 175 | * the data will be displayed either as cells (if len is a multiple of 4 | ||
| 176 | * bytes) or bytes. | ||
| 177 | * | ||
| 178 | * If len is 0 then this function does nothing. | ||
| 179 | * | ||
| 180 | * @param data Pointers to property data | ||
| 181 | * @param len Length of property data | ||
| 182 | */ | ||
| 183 | void utilfdt_print_data(const char *data, int len); | ||
| 184 | |||
| 185 | /** | ||
| 186 | * Show source version and exit | ||
| 187 | */ | ||
| 188 | void util_version(void) __attribute__((noreturn)); | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Show usage and exit | ||
| 192 | * | ||
| 193 | * This helps standardize the output of various utils. You most likely want | ||
| 194 | * to use the usage() helper below rather than call this. | ||
| 195 | * | ||
| 196 | * @param errmsg If non-NULL, an error message to display | ||
| 197 | * @param synopsis The initial example usage text (and possible examples) | ||
| 198 | * @param short_opts The string of short options | ||
| 199 | * @param long_opts The structure of long options | ||
| 200 | * @param opts_help An array of help strings (should align with long_opts) | ||
| 201 | */ | ||
| 202 | void util_usage(const char *errmsg, const char *synopsis, | ||
| 203 | const char *short_opts, struct option const long_opts[], | ||
| 204 | const char * const opts_help[]) __attribute__((noreturn)); | ||
| 205 | |||
| 206 | /** | ||
| 207 | * Show usage and exit | ||
| 208 | * | ||
| 209 | * If you name all your usage variables with usage_xxx, then you can call this | ||
| 210 | * help macro rather than expanding all arguments yourself. | ||
| 211 | * | ||
| 212 | * @param errmsg If non-NULL, an error message to display | ||
| 213 | */ | ||
| 214 | #define usage(errmsg) \ | ||
| 215 | util_usage(errmsg, usage_synopsis, usage_short_opts, \ | ||
| 216 | usage_long_opts, usage_opts_help) | ||
| 217 | |||
| 218 | /** | ||
| 219 | * Call getopt_long() with standard options | ||
| 220 | * | ||
| 221 | * Since all util code runs getopt in the same way, provide a helper. | ||
| 222 | */ | ||
| 223 | #define util_getopt_long() getopt_long(argc, argv, usage_short_opts, \ | ||
| 224 | usage_long_opts, NULL) | ||
| 225 | |||
| 226 | /* Helper for aligning long_opts array */ | ||
| 227 | #define a_argument required_argument | ||
| 228 | |||
| 229 | /* Helper for usage_short_opts string constant */ | ||
| 230 | #define USAGE_COMMON_SHORT_OPTS "hV" | ||
| 231 | |||
| 232 | /* Helper for usage_long_opts option array */ | ||
| 233 | #define USAGE_COMMON_LONG_OPTS \ | ||
| 234 | {"help", no_argument, NULL, 'h'}, \ | ||
| 235 | {"version", no_argument, NULL, 'V'}, \ | ||
| 236 | {NULL, no_argument, NULL, 0x0} | ||
| 237 | |||
| 238 | /* Helper for usage_opts_help array */ | ||
| 239 | #define USAGE_COMMON_OPTS_HELP \ | ||
| 240 | "Print this help and exit", \ | ||
| 241 | "Print version and exit", \ | ||
| 242 | NULL | ||
| 243 | |||
| 244 | /* Helper for getopt case statements */ | ||
| 245 | #define case_USAGE_COMMON_FLAGS \ | ||
| 246 | case 'h': usage(NULL); \ | ||
| 247 | case 'V': util_version(); \ | ||
| 248 | case '?': usage("unknown option"); | ||
| 152 | 249 | ||
| 153 | #endif /* _UTIL_H */ | 250 | #endif /* _UTIL_H */ |
