util.c 8.7 KB

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