util.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2011 The Chromium Authors, All Rights Reserved.
  4. * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
  5. *
  6. * util_is_printable_string contributed by
  7. * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
  8. */
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include <string.h>
  14. #include <assert.h>
  15. #include <inttypes.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <unistd.h>
  19. #include "libfdt.h"
  20. #include "util.h"
  21. #include "version_gen.h"
  22. char *xstrdup(const char *s)
  23. {
  24. int len = strlen(s) + 1;
  25. char *d = xmalloc(len);
  26. memcpy(d, s, len);
  27. return d;
  28. }
  29. int xavsprintf_append(char **strp, const char *fmt, va_list ap)
  30. {
  31. int n, size = 0; /* start with 128 bytes */
  32. char *p;
  33. va_list ap_copy;
  34. p = *strp;
  35. if (p)
  36. size = strlen(p);
  37. va_copy(ap_copy, ap);
  38. n = vsnprintf(NULL, 0, fmt, ap_copy) + 1;
  39. va_end(ap_copy);
  40. p = xrealloc(p, size + n);
  41. n = vsnprintf(p + size, n, fmt, ap);
  42. *strp = p;
  43. return strlen(p);
  44. }
  45. int xasprintf_append(char **strp, const char *fmt, ...)
  46. {
  47. int n;
  48. va_list ap;
  49. va_start(ap, fmt);
  50. n = xavsprintf_append(strp, fmt, ap);
  51. va_end(ap);
  52. return n;
  53. }
  54. int xasprintf(char **strp, const char *fmt, ...)
  55. {
  56. int n;
  57. va_list ap;
  58. *strp = NULL;
  59. va_start(ap, fmt);
  60. n = xavsprintf_append(strp, fmt, ap);
  61. va_end(ap);
  62. return n;
  63. }
  64. char *join_path(const char *path, const char *name)
  65. {
  66. int lenp = strlen(path);
  67. int lenn = strlen(name);
  68. int len;
  69. int needslash = 1;
  70. char *str;
  71. len = lenp + lenn + 2;
  72. if ((lenp > 0) && (path[lenp-1] == '/')) {
  73. needslash = 0;
  74. len--;
  75. }
  76. str = xmalloc(len);
  77. memcpy(str, path, lenp);
  78. if (needslash) {
  79. str[lenp] = '/';
  80. lenp++;
  81. }
  82. memcpy(str+lenp, name, lenn+1);
  83. return str;
  84. }
  85. bool util_is_printable_string(const void *data, int len)
  86. {
  87. const char *s = data;
  88. const char *ss, *se;
  89. /* zero length is not */
  90. if (len == 0)
  91. return 0;
  92. /* must terminate with zero */
  93. if (s[len - 1] != '\0')
  94. return 0;
  95. se = s + len;
  96. while (s < se) {
  97. ss = s;
  98. while (s < se && *s && isprint((unsigned char)*s))
  99. s++;
  100. /* not zero, or not done yet */
  101. if (*s != '\0' || s == ss)
  102. return 0;
  103. s++;
  104. }
  105. return 1;
  106. }
  107. /*
  108. * Parse a octal encoded character starting at index i in string s. The
  109. * resulting character will be returned and the index i will be updated to
  110. * point at the character directly after the end of the encoding, this may be
  111. * the '\0' terminator of the string.
  112. */
  113. static char get_oct_char(const char *s, int *i)
  114. {
  115. char x[4];
  116. char *endx;
  117. long val;
  118. x[3] = '\0';
  119. strncpy(x, s + *i, 3);
  120. val = strtol(x, &endx, 8);
  121. assert(endx > x);
  122. (*i) += endx - x;
  123. return val;
  124. }
  125. /*
  126. * Parse a hexadecimal encoded character starting at index i in string s. The
  127. * resulting character will be returned and the index i will be updated to
  128. * point at the character directly after the end of the encoding, this may be
  129. * the '\0' terminator of the string.
  130. */
  131. static char get_hex_char(const char *s, int *i)
  132. {
  133. char x[3];
  134. char *endx;
  135. long val;
  136. x[2] = '\0';
  137. strncpy(x, s + *i, 2);
  138. val = strtol(x, &endx, 16);
  139. if (!(endx > x))
  140. die("\\x used with no following hex digits\n");
  141. (*i) += endx - x;
  142. return val;
  143. }
  144. char get_escape_char(const char *s, int *i)
  145. {
  146. char c = s[*i];
  147. int j = *i + 1;
  148. char val;
  149. switch (c) {
  150. case 'a':
  151. val = '\a';
  152. break;
  153. case 'b':
  154. val = '\b';
  155. break;
  156. case 't':
  157. val = '\t';
  158. break;
  159. case 'n':
  160. val = '\n';
  161. break;
  162. case 'v':
  163. val = '\v';
  164. break;
  165. case 'f':
  166. val = '\f';
  167. break;
  168. case 'r':
  169. val = '\r';
  170. break;
  171. case '0':
  172. case '1':
  173. case '2':
  174. case '3':
  175. case '4':
  176. case '5':
  177. case '6':
  178. case '7':
  179. j--; /* need to re-read the first digit as
  180. * part of the octal value */
  181. val = get_oct_char(s, &j);
  182. break;
  183. case 'x':
  184. val = get_hex_char(s, &j);
  185. break;
  186. default:
  187. val = c;
  188. }
  189. (*i) = j;
  190. return val;
  191. }
  192. int utilfdt_read_err(const char *filename, char **buffp, size_t *len)
  193. {
  194. int fd = 0; /* assume stdin */
  195. char *buf = NULL;
  196. size_t bufsize = 1024, offset = 0;
  197. int ret = 0;
  198. *buffp = NULL;
  199. if (strcmp(filename, "-") != 0) {
  200. fd = open(filename, O_RDONLY);
  201. if (fd < 0)
  202. return errno;
  203. }
  204. /* Loop until we have read everything */
  205. buf = xmalloc(bufsize);
  206. do {
  207. /* Expand the buffer to hold the next chunk */
  208. if (offset == bufsize) {
  209. bufsize *= 2;
  210. buf = xrealloc(buf, bufsize);
  211. }
  212. ret = read(fd, &buf[offset], bufsize - offset);
  213. if (ret < 0) {
  214. ret = errno;
  215. break;
  216. }
  217. offset += ret;
  218. } while (ret != 0);
  219. /* Clean up, including closing stdin; return errno on error */
  220. close(fd);
  221. if (ret)
  222. free(buf);
  223. else
  224. *buffp = buf;
  225. if (len)
  226. *len = bufsize;
  227. return ret;
  228. }
  229. char *utilfdt_read(const char *filename, size_t *len)
  230. {
  231. char *buff;
  232. int ret = utilfdt_read_err(filename, &buff, len);
  233. if (ret) {
  234. fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
  235. strerror(ret));
  236. return NULL;
  237. }
  238. /* Successful read */
  239. return buff;
  240. }
  241. int utilfdt_write_err(const char *filename, const void *blob)
  242. {
  243. int fd = 1; /* assume stdout */
  244. int totalsize;
  245. int offset;
  246. int ret = 0;
  247. const char *ptr = blob;
  248. if (strcmp(filename, "-") != 0) {
  249. fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  250. if (fd < 0)
  251. return errno;
  252. }
  253. totalsize = fdt_totalsize(blob);
  254. offset = 0;
  255. while (offset < totalsize) {
  256. ret = write(fd, ptr + offset, totalsize - offset);
  257. if (ret < 0) {
  258. ret = -errno;
  259. break;
  260. }
  261. offset += ret;
  262. }
  263. /* Close the file/stdin; return errno on error */
  264. if (fd != 1)
  265. close(fd);
  266. return ret < 0 ? -ret : 0;
  267. }
  268. int utilfdt_write(const char *filename, const void *blob)
  269. {
  270. int ret = utilfdt_write_err(filename, blob);
  271. if (ret) {
  272. fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
  273. strerror(ret));
  274. }
  275. return ret ? -1 : 0;
  276. }
  277. int utilfdt_decode_type(const char *fmt, int *type, int *size)
  278. {
  279. int qualifier = 0;
  280. if (!*fmt)
  281. return -1;
  282. /* get the conversion qualifier */
  283. *size = -1;
  284. if (strchr("hlLb", *fmt)) {
  285. qualifier = *fmt++;
  286. if (qualifier == *fmt) {
  287. switch (*fmt++) {
  288. /* TODO: case 'l': qualifier = 'L'; break;*/
  289. case 'h':
  290. qualifier = 'b';
  291. break;
  292. }
  293. }
  294. }
  295. /* we should now have a type */
  296. if ((*fmt == '\0') || !strchr("iuxs", *fmt))
  297. return -1;
  298. /* convert qualifier (bhL) to byte size */
  299. if (*fmt != 's')
  300. *size = qualifier == 'b' ? 1 :
  301. qualifier == 'h' ? 2 :
  302. qualifier == 'l' ? 4 : -1;
  303. *type = *fmt++;
  304. /* that should be it! */
  305. if (*fmt)
  306. return -1;
  307. return 0;
  308. }
  309. void utilfdt_print_data(const char *data, int len)
  310. {
  311. int i;
  312. const char *s;
  313. /* no data, don't print */
  314. if (len == 0)
  315. return;
  316. if (util_is_printable_string(data, len)) {
  317. printf(" = ");
  318. s = data;
  319. do {
  320. printf("\"%s\"", s);
  321. s += strlen(s) + 1;
  322. if (s < data + len)
  323. printf(", ");
  324. } while (s < data + len);
  325. } else if ((len % 4) == 0) {
  326. const fdt32_t *cell = (const fdt32_t *)data;
  327. printf(" = <");
  328. for (i = 0, len /= 4; i < len; i++)
  329. printf("0x%08" PRIx32 "%s", fdt32_to_cpu(cell[i]),
  330. i < (len - 1) ? " " : "");
  331. printf(">");
  332. } else {
  333. const unsigned char *p = (const unsigned char *)data;
  334. printf(" = [");
  335. for (i = 0; i < len; i++)
  336. printf("%02x%s", *p++, i < len - 1 ? " " : "");
  337. printf("]");
  338. }
  339. }
  340. void NORETURN util_version(void)
  341. {
  342. printf("Version: %s\n", DTC_VERSION);
  343. exit(0);
  344. }
  345. void NORETURN util_usage(const char *errmsg, const char *synopsis,
  346. const char *short_opts,
  347. struct option const long_opts[],
  348. const char * const opts_help[])
  349. {
  350. FILE *fp = errmsg ? stderr : stdout;
  351. const char a_arg[] = "<arg>";
  352. size_t a_arg_len = strlen(a_arg) + 1;
  353. size_t i;
  354. int optlen;
  355. fprintf(fp,
  356. "Usage: %s\n"
  357. "\n"
  358. "Options: -[%s]\n", synopsis, short_opts);
  359. /* prescan the --long opt length to auto-align */
  360. optlen = 0;
  361. for (i = 0; long_opts[i].name; ++i) {
  362. /* +1 is for space between --opt and help text */
  363. int l = strlen(long_opts[i].name) + 1;
  364. if (long_opts[i].has_arg == a_argument)
  365. l += a_arg_len;
  366. if (optlen < l)
  367. optlen = l;
  368. }
  369. for (i = 0; long_opts[i].name; ++i) {
  370. /* helps when adding new applets or options */
  371. assert(opts_help[i] != NULL);
  372. /* first output the short flag if it has one */
  373. if (long_opts[i].val > '~')
  374. fprintf(fp, " ");
  375. else
  376. fprintf(fp, " -%c, ", long_opts[i].val);
  377. /* then the long flag */
  378. if (long_opts[i].has_arg == no_argument)
  379. fprintf(fp, "--%-*s", optlen, long_opts[i].name);
  380. else
  381. fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
  382. (int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
  383. /* finally the help text */
  384. fprintf(fp, "%s\n", opts_help[i]);
  385. }
  386. if (errmsg) {
  387. fprintf(fp, "\nError: %s\n", errmsg);
  388. exit(EXIT_FAILURE);
  389. } else
  390. exit(EXIT_SUCCESS);
  391. }