mkenvimage.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * (C) Copyright 2011 Free Electrons
  3. * David Wagner <david.wagner@free-electrons.com>
  4. *
  5. * Inspired from envcrc.c:
  6. * (C) Copyright 2001
  7. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <libgen.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <sys/mman.h>
  22. #include "compiler.h"
  23. #include <u-boot/crc.h>
  24. #include <version.h>
  25. #define CRC_SIZE sizeof(uint32_t)
  26. static void usage(const char *exec_name)
  27. {
  28. fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
  29. "\n"
  30. "This tool takes a key=value input file (same as would a `printenv' show) and generates the corresponding environment image, ready to be flashed.\n"
  31. "\n"
  32. "\tThe input file is in format:\n"
  33. "\t\tkey1=value1\n"
  34. "\t\tkey2=value2\n"
  35. "\t\t...\n"
  36. "\tEmpty lines are skipped, and lines with a # in the first\n"
  37. "\tcolumn are treated as comments (also skipped).\n"
  38. "\t-r : the environment has multiple copies in flash\n"
  39. "\t-b : the target is big endian (default is little endian)\n"
  40. "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
  41. "\t-V : print version information and exit\n"
  42. "\n"
  43. "If the input file is \"-\", data is read from standard input\n",
  44. exec_name);
  45. }
  46. long int xstrtol(const char *s)
  47. {
  48. long int tmp;
  49. errno = 0;
  50. tmp = strtol(s, NULL, 0);
  51. if (!errno)
  52. return tmp;
  53. if (errno == ERANGE)
  54. fprintf(stderr, "Bad integer format: %s\n", s);
  55. else
  56. fprintf(stderr, "Error while parsing %s: %s\n", s,
  57. strerror(errno));
  58. exit(EXIT_FAILURE);
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. uint32_t crc, targetendian_crc;
  63. const char *txt_filename = NULL, *bin_filename = NULL;
  64. int txt_fd, bin_fd;
  65. unsigned char *dataptr, *envptr;
  66. unsigned char *filebuf = NULL;
  67. unsigned int filesize = 0, envsize = 0, datasize = 0;
  68. int bigendian = 0;
  69. int redundant = 0;
  70. unsigned char padbyte = 0xff;
  71. int option;
  72. int ret = EXIT_SUCCESS;
  73. struct stat txt_file_stat;
  74. int fp, ep;
  75. const char *prg;
  76. prg = basename(argv[0]);
  77. /* Turn off getopt()'s internal error message */
  78. opterr = 0;
  79. /* Parse the cmdline */
  80. while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
  81. switch (option) {
  82. case 's':
  83. datasize = xstrtol(optarg);
  84. break;
  85. case 'o':
  86. bin_filename = strdup(optarg);
  87. if (!bin_filename) {
  88. fprintf(stderr, "Can't strdup() the output filename\n");
  89. return EXIT_FAILURE;
  90. }
  91. break;
  92. case 'r':
  93. redundant = 1;
  94. break;
  95. case 'b':
  96. bigendian = 1;
  97. break;
  98. case 'p':
  99. padbyte = xstrtol(optarg);
  100. break;
  101. case 'h':
  102. usage(prg);
  103. return EXIT_SUCCESS;
  104. case 'V':
  105. printf("%s version %s\n", prg, PLAIN_VERSION);
  106. return EXIT_SUCCESS;
  107. case ':':
  108. fprintf(stderr, "Missing argument for option -%c\n",
  109. optopt);
  110. usage(prg);
  111. return EXIT_FAILURE;
  112. default:
  113. fprintf(stderr, "Wrong option -%c\n", optopt);
  114. usage(prg);
  115. return EXIT_FAILURE;
  116. }
  117. }
  118. /* Check datasize and allocate the data */
  119. if (datasize == 0) {
  120. fprintf(stderr, "Please specify the size of the environment partition.\n");
  121. usage(prg);
  122. return EXIT_FAILURE;
  123. }
  124. dataptr = malloc(datasize * sizeof(*dataptr));
  125. if (!dataptr) {
  126. fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
  127. datasize);
  128. return EXIT_FAILURE;
  129. }
  130. /*
  131. * envptr points to the beginning of the actual environment (after the
  132. * crc and possible `redundant' byte
  133. */
  134. envsize = datasize - (CRC_SIZE + redundant);
  135. envptr = dataptr + CRC_SIZE + redundant;
  136. /* Pad the environment with the padding byte */
  137. memset(envptr, padbyte, envsize);
  138. /* Open the input file ... */
  139. if (optind >= argc || strcmp(argv[optind], "-") == 0) {
  140. int readbytes = 0;
  141. int readlen = sizeof(*envptr) * 4096;
  142. txt_fd = STDIN_FILENO;
  143. do {
  144. filebuf = realloc(filebuf, readlen);
  145. if (!filebuf) {
  146. fprintf(stderr, "Can't realloc memory for the input file buffer\n");
  147. return EXIT_FAILURE;
  148. }
  149. readbytes = read(txt_fd, filebuf + filesize, readlen);
  150. if (errno) {
  151. fprintf(stderr, "Error while reading stdin: %s\n",
  152. strerror(errno));
  153. return EXIT_FAILURE;
  154. }
  155. filesize += readbytes;
  156. } while (readbytes == readlen);
  157. } else {
  158. txt_filename = argv[optind];
  159. txt_fd = open(txt_filename, O_RDONLY);
  160. if (txt_fd == -1) {
  161. fprintf(stderr, "Can't open \"%s\": %s\n",
  162. txt_filename, strerror(errno));
  163. return EXIT_FAILURE;
  164. }
  165. /* ... and check it */
  166. ret = fstat(txt_fd, &txt_file_stat);
  167. if (ret == -1) {
  168. fprintf(stderr, "Can't stat() on \"%s\": %s\n",
  169. txt_filename, strerror(errno));
  170. return EXIT_FAILURE;
  171. }
  172. filesize = txt_file_stat.st_size;
  173. filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
  174. MAP_PRIVATE, txt_fd, 0);
  175. if (filebuf == MAP_FAILED) {
  176. fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
  177. sizeof(*envptr) * filesize,
  178. strerror(errno));
  179. fprintf(stderr, "Falling back to read()\n");
  180. filebuf = malloc(sizeof(*envptr) * filesize);
  181. ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
  182. if (ret != sizeof(*envptr) * filesize) {
  183. fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
  184. sizeof(*envptr) * filesize,
  185. strerror(errno));
  186. return EXIT_FAILURE;
  187. }
  188. }
  189. ret = close(txt_fd);
  190. }
  191. /* Parse a byte at time until reaching the file OR until the environment fills
  192. * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */
  193. for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) {
  194. if (filebuf[fp] == '\n') {
  195. if (fp == 0 || filebuf[fp-1] == '\n') {
  196. /*
  197. * Skip empty lines.
  198. */
  199. continue;
  200. } else if (filebuf[fp-1] == '\\') {
  201. /*
  202. * Embedded newline in a variable.
  203. *
  204. * The backslash was added to the envptr; rewind
  205. * and replace it with a newline
  206. */
  207. ep--;
  208. envptr[ep++] = '\n';
  209. } else {
  210. /* End of a variable */
  211. envptr[ep++] = '\0';
  212. }
  213. } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
  214. /* Comment, skip the line. */
  215. while (++fp < filesize && filebuf[fp] != '\n')
  216. continue;
  217. } else {
  218. envptr[ep++] = filebuf[fp];
  219. }
  220. }
  221. /* If there are more bytes in the file still, it means the env filled up
  222. * before parsing the whole file. Eat comments & whitespace here to see if
  223. * there was anything meaning full left in the file, and if so, throw a error
  224. * and exit. */
  225. for( ; fp < filesize; fp++ )
  226. {
  227. if (filebuf[fp] == '\n') {
  228. if (fp == 0 || filebuf[fp-1] == '\n') {
  229. /* Ignore blank lines */
  230. continue;
  231. }
  232. } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
  233. while (++fp < filesize && filebuf[fp] != '\n')
  234. continue;
  235. } else {
  236. fprintf(stderr, "The environment file is too large for the target environment storage\n");
  237. return EXIT_FAILURE;
  238. }
  239. }
  240. /*
  241. * Make sure there is a final '\0'
  242. * And do it again on the next byte to mark the end of the environment.
  243. */
  244. if (envptr[ep-1] != '\0') {
  245. envptr[ep++] = '\0';
  246. /*
  247. * The text file doesn't have an ending newline. We need to
  248. * check the env size again to make sure we have room for two \0
  249. */
  250. if (ep >= envsize) {
  251. fprintf(stderr, "The environment file is too large for the target environment storage\n");
  252. return EXIT_FAILURE;
  253. }
  254. envptr[ep] = '\0';
  255. } else {
  256. envptr[ep] = '\0';
  257. }
  258. /* Computes the CRC and put it at the beginning of the data */
  259. crc = crc32(0, envptr, envsize);
  260. targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
  261. memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
  262. if (redundant)
  263. dataptr[sizeof(targetendian_crc)] = 1;
  264. if (!bin_filename || strcmp(bin_filename, "-") == 0) {
  265. bin_fd = STDOUT_FILENO;
  266. } else {
  267. bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
  268. S_IWGRP);
  269. if (bin_fd == -1) {
  270. fprintf(stderr, "Can't open output file \"%s\": %s\n",
  271. bin_filename, strerror(errno));
  272. return EXIT_FAILURE;
  273. }
  274. }
  275. if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
  276. sizeof(*dataptr) * datasize) {
  277. fprintf(stderr, "write() failed: %s\n", strerror(errno));
  278. return EXIT_FAILURE;
  279. }
  280. ret = close(bin_fd);
  281. return ret;
  282. }