mkenvimage.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #define CHUNK_SIZE 4096
  60. int main(int argc, char **argv)
  61. {
  62. uint32_t crc, targetendian_crc;
  63. const char *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 readbytes = 0;
  72. int option;
  73. int ret = EXIT_SUCCESS;
  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. txt_fd = STDIN_FILENO;
  141. } else {
  142. txt_fd = open(argv[optind], O_RDONLY);
  143. if (txt_fd == -1) {
  144. fprintf(stderr, "Can't open \"%s\": %s\n",
  145. argv[optind], strerror(errno));
  146. return EXIT_FAILURE;
  147. }
  148. }
  149. do {
  150. filebuf = realloc(filebuf, filesize + CHUNK_SIZE);
  151. if (!filebuf) {
  152. fprintf(stderr, "Can't realloc memory for the input file buffer\n");
  153. return EXIT_FAILURE;
  154. }
  155. readbytes = read(txt_fd, filebuf + filesize, CHUNK_SIZE);
  156. if (readbytes < 0) {
  157. fprintf(stderr, "Error while reading: %s\n",
  158. strerror(errno));
  159. return EXIT_FAILURE;
  160. }
  161. filesize += readbytes;
  162. } while (readbytes > 0);
  163. if (txt_fd != STDIN_FILENO)
  164. ret = close(txt_fd);
  165. /* Parse a byte at time until reaching the file OR until the environment fills
  166. * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */
  167. for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) {
  168. if (filebuf[fp] == '\n') {
  169. if (fp == 0 || filebuf[fp-1] == '\n') {
  170. /*
  171. * Skip empty lines.
  172. */
  173. continue;
  174. } else if (filebuf[fp-1] == '\\') {
  175. /*
  176. * Embedded newline in a variable.
  177. *
  178. * The backslash was added to the envptr; rewind
  179. * and replace it with a newline
  180. */
  181. ep--;
  182. envptr[ep++] = '\n';
  183. } else {
  184. /* End of a variable */
  185. envptr[ep++] = '\0';
  186. }
  187. } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
  188. /* Comment, skip the line. */
  189. while (++fp < filesize && filebuf[fp] != '\n')
  190. continue;
  191. } else {
  192. envptr[ep++] = filebuf[fp];
  193. }
  194. }
  195. /* If there are more bytes in the file still, it means the env filled up
  196. * before parsing the whole file. Eat comments & whitespace here to see if
  197. * there was anything meaning full left in the file, and if so, throw a error
  198. * and exit. */
  199. for( ; fp < filesize; fp++ )
  200. {
  201. if (filebuf[fp] == '\n') {
  202. if (fp == 0 || filebuf[fp-1] == '\n') {
  203. /* Ignore blank lines */
  204. continue;
  205. }
  206. } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
  207. while (++fp < filesize && filebuf[fp] != '\n')
  208. continue;
  209. } else {
  210. fprintf(stderr, "The environment file is too large for the target environment storage\n");
  211. return EXIT_FAILURE;
  212. }
  213. }
  214. /*
  215. * Make sure there is a final '\0'
  216. * And do it again on the next byte to mark the end of the environment.
  217. */
  218. if (envptr[ep-1] != '\0') {
  219. envptr[ep++] = '\0';
  220. /*
  221. * The text file doesn't have an ending newline. We need to
  222. * check the env size again to make sure we have room for two \0
  223. */
  224. if (ep >= envsize) {
  225. fprintf(stderr, "The environment file is too large for the target environment storage\n");
  226. return EXIT_FAILURE;
  227. }
  228. envptr[ep] = '\0';
  229. } else {
  230. envptr[ep] = '\0';
  231. }
  232. /* Computes the CRC and put it at the beginning of the data */
  233. crc = crc32(0, envptr, envsize);
  234. targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
  235. memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
  236. if (redundant)
  237. dataptr[sizeof(targetendian_crc)] = 1;
  238. if (!bin_filename || strcmp(bin_filename, "-") == 0) {
  239. bin_fd = STDOUT_FILENO;
  240. } else {
  241. bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
  242. S_IWGRP);
  243. if (bin_fd == -1) {
  244. fprintf(stderr, "Can't open output file \"%s\": %s\n",
  245. bin_filename, strerror(errno));
  246. return EXIT_FAILURE;
  247. }
  248. }
  249. if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
  250. sizeof(*dataptr) * datasize) {
  251. fprintf(stderr, "write() failed: %s\n", strerror(errno));
  252. return EXIT_FAILURE;
  253. }
  254. ret = close(bin_fd);
  255. return ret;
  256. }