util.c 8.9 KB

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