mkenvimage.c 8.2 KB

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