util.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 *dup = xmalloc(len);
  39. memcpy(dup, s, len);
  40. return dup;
  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. int 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(*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. if (!buf) {
  191. ret = ENOMEM;
  192. break;
  193. }
  194. }
  195. ret = read(fd, &buf[offset], bufsize - offset);
  196. if (ret < 0) {
  197. ret = errno;
  198. break;
  199. }
  200. offset += ret;
  201. } while (ret != 0);
  202. /* Clean up, including closing stdin; return errno on error */
  203. close(fd);
  204. if (ret)
  205. free(buf);
  206. else
  207. *buffp = buf;
  208. *len = bufsize;
  209. return ret;
  210. }
  211. int utilfdt_read_err(const char *filename, char **buffp)
  212. {
  213. off_t len;
  214. return utilfdt_read_err_len(filename, buffp, &len);
  215. }
  216. char *utilfdt_read_len(const char *filename, off_t *len)
  217. {
  218. char *buff;
  219. int ret = utilfdt_read_err_len(filename, &buff, len);
  220. if (ret) {
  221. fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
  222. strerror(ret));
  223. return NULL;
  224. }
  225. /* Successful read */
  226. return buff;
  227. }
  228. char *utilfdt_read(const char *filename)
  229. {
  230. off_t len;
  231. return utilfdt_read_len(filename, &len);
  232. }
  233. int utilfdt_write_err(const char *filename, const void *blob)
  234. {
  235. int fd = 1; /* assume stdout */
  236. int totalsize;
  237. int offset;
  238. int ret = 0;
  239. const char *ptr = blob;
  240. if (strcmp(filename, "-") != 0) {
  241. fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  242. if (fd < 0)
  243. return errno;
  244. }
  245. totalsize = fdt_totalsize(blob);
  246. offset = 0;
  247. while (offset < totalsize) {
  248. ret = write(fd, ptr + offset, totalsize - offset);
  249. if (ret < 0) {
  250. ret = -errno;
  251. break;
  252. }
  253. offset += ret;
  254. }
  255. /* Close the file/stdin; return errno on error */
  256. if (fd != 1)
  257. close(fd);
  258. return ret < 0 ? -ret : 0;
  259. }
  260. int utilfdt_write(const char *filename, const void *blob)
  261. {
  262. int ret = utilfdt_write_err(filename, blob);
  263. if (ret) {
  264. fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
  265. strerror(ret));
  266. }
  267. return ret ? -1 : 0;
  268. }
  269. int utilfdt_decode_type(const char *fmt, int *type, int *size)
  270. {
  271. int qualifier = 0;
  272. if (!*fmt)
  273. return -1;
  274. /* get the conversion qualifier */
  275. *size = -1;
  276. if (strchr("hlLb", *fmt)) {
  277. qualifier = *fmt++;
  278. if (qualifier == *fmt) {
  279. switch (*fmt++) {
  280. /* TODO: case 'l': qualifier = 'L'; break;*/
  281. case 'h':
  282. qualifier = 'b';
  283. break;
  284. }
  285. }
  286. }
  287. /* we should now have a type */
  288. if ((*fmt == '\0') || !strchr("iuxs", *fmt))
  289. return -1;
  290. /* convert qualifier (bhL) to byte size */
  291. if (*fmt != 's')
  292. *size = qualifier == 'b' ? 1 :
  293. qualifier == 'h' ? 2 :
  294. qualifier == 'l' ? 4 : -1;
  295. *type = *fmt++;
  296. /* that should be it! */
  297. if (*fmt)
  298. return -1;
  299. return 0;
  300. }
  301. void utilfdt_print_data(const char *data, int len)
  302. {
  303. int i;
  304. const char *p = data;
  305. const char *s;
  306. /* no data, don't print */
  307. if (len == 0)
  308. return;
  309. if (util_is_printable_string(data, len)) {
  310. printf(" = ");
  311. s = data;
  312. do {
  313. printf("\"%s\"", s);
  314. s += strlen(s) + 1;
  315. if (s < data + len)
  316. printf(", ");
  317. } while (s < data + len);
  318. } else if ((len % 4) == 0) {
  319. const uint32_t *cell = (const uint32_t *)data;
  320. printf(" = <");
  321. for (i = 0; i < len; i += 4)
  322. printf("0x%08x%s", fdt32_to_cpu(cell[i]),
  323. i < (len - 4) ? " " : "");
  324. printf(">");
  325. } else {
  326. printf(" = [");
  327. for (i = 0; i < len; i++)
  328. printf("%02x%s", *p++, i < len - 1 ? " " : "");
  329. printf("]");
  330. }
  331. }
  332. void util_version(void)
  333. {
  334. printf("Version: %s\n", DTC_VERSION);
  335. exit(0);
  336. }
  337. void util_usage(const char *errmsg, const char *synopsis,
  338. const char *short_opts, struct option const long_opts[],
  339. const char * const opts_help[])
  340. {
  341. FILE *fp = errmsg ? stderr : stdout;
  342. const char a_arg[] = "<arg>";
  343. size_t a_arg_len = strlen(a_arg) + 1;
  344. size_t i;
  345. int optlen;
  346. fprintf(fp,
  347. "Usage: %s\n"
  348. "\n"
  349. "Options: -[%s]\n", synopsis, short_opts);
  350. /* prescan the --long opt length to auto-align */
  351. optlen = 0;
  352. for (i = 0; long_opts[i].name; ++i) {
  353. /* +1 is for space between --opt and help text */
  354. int l = strlen(long_opts[i].name) + 1;
  355. if (long_opts[i].has_arg == a_argument)
  356. l += a_arg_len;
  357. if (optlen < l)
  358. optlen = l;
  359. }
  360. for (i = 0; long_opts[i].name; ++i) {
  361. /* helps when adding new applets or options */
  362. assert(opts_help[i] != NULL);
  363. /* first output the short flag if it has one */
  364. if (long_opts[i].val > '~')
  365. fprintf(fp, " ");
  366. else
  367. fprintf(fp, " -%c, ", long_opts[i].val);
  368. /* then the long flag */
  369. if (long_opts[i].has_arg == no_argument)
  370. fprintf(fp, "--%-*s", optlen, long_opts[i].name);
  371. else
  372. fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
  373. (int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
  374. /* finally the help text */
  375. fprintf(fp, "%s\n", opts_help[i]);
  376. }
  377. if (errmsg) {
  378. fprintf(fp, "\nError: %s\n", errmsg);
  379. exit(EXIT_FAILURE);
  380. } else
  381. exit(EXIT_SUCCESS);
  382. }