mkenvimage.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <u-boot/crc.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. #define CHUNK_SIZE 4096
  61. int main(int argc, char **argv)
  62. {
  63. uint32_t crc, targetendian_crc;
  64. const char *bin_filename = NULL;
  65. int txt_fd, bin_fd;
  66. unsigned char *dataptr, *envptr;
  67. unsigned char *filebuf = NULL;
  68. unsigned int filesize = 0, envsize = 0, datasize = 0;
  69. int bigendian = 0;
  70. int redundant = 0;
  71. unsigned char padbyte = 0xff;
  72. int readbytes = 0;
  73. int option;
  74. int ret = EXIT_SUCCESS;
  75. int fp, ep;
  76. const char *prg;
  77. prg = basename(argv[0]);
  78. /* Turn off getopt()'s internal error message */
  79. opterr = 0;
  80. /* Parse the cmdline */
  81. while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
  82. switch (option) {
  83. case 's':
  84. datasize = xstrtol(optarg);
  85. break;
  86. case 'o':
  87. bin_filename = strdup(optarg);
  88. if (!bin_filename) {
  89. fprintf(stderr, "Can't strdup() the output filename\n");
  90. return EXIT_FAILURE;
  91. }
  92. break;
  93. case 'r':
  94. redundant = 1;
  95. break;
  96. case 'b':
  97. bigendian = 1;
  98. break;
  99. case 'p':
  100. padbyte = xstrtol(optarg);
  101. break;
  102. case 'h':
  103. usage(prg);
  104. return EXIT_SUCCESS;
  105. case 'V':
  106. printf("%s version %s\n", prg, PLAIN_VERSION);
  107. return EXIT_SUCCESS;
  108. case ':':
  109. fprintf(stderr, "Missing argument for option -%c\n",
  110. optopt);
  111. usage(prg);
  112. return EXIT_FAILURE;
  113. default:
  114. fprintf(stderr, "Wrong option -%c\n", optopt);
  115. usage(prg);
  116. return EXIT_FAILURE;
  117. }
  118. }
  119. /* Check datasize and allocate the data */
  120. if (datasize == 0) {
  121. fprintf(stderr, "Please specify the size of the environment partition.\n");
  122. usage(prg);
  123. return EXIT_FAILURE;
  124. }
  125. dataptr = malloc(datasize * sizeof(*dataptr));
  126. if (!dataptr) {
  127. fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
  128. datasize);
  129. return EXIT_FAILURE;
  130. }
  131. /*
  132. * envptr points to the beginning of the actual environment (after the
  133. * crc and possible `redundant' byte
  134. */
  135. envsize = datasize - (CRC_SIZE + redundant);
  136. envptr = dataptr + CRC_SIZE + redundant;
  137. /* Pad the environment with the padding byte */
  138. memset(envptr, padbyte, envsize);
  139. /* Open the input file ... */
  140. if (optind >= argc || strcmp(argv[optind], "-") == 0) {
  141. txt_fd = STDIN_FILENO;
  142. } else {
  143. txt_fd = open(argv[optind], O_RDONLY);
  144. if (txt_fd == -1) {
  145. fprintf(stderr, "Can't open \"%s\": %s\n",
  146. argv[optind], strerror(errno));
  147. return EXIT_FAILURE;
  148. }
  149. }
  150. do {
  151. filebuf = realloc(filebuf, filesize + CHUNK_SIZE);
  152. if (!filebuf) {
  153. fprintf(stderr, "Can't realloc memory for the input file buffer\n");
  154. return EXIT_FAILURE;
  155. }
  156. readbytes = read(txt_fd, filebuf + filesize, CHUNK_SIZE);
  157. if (readbytes < 0) {
  158. fprintf(stderr, "Error while reading: %s\n",
  159. strerror(errno));
  160. return EXIT_FAILURE;
  161. }
  162. filesize += readbytes;
  163. } while (readbytes > 0);
  164. if (txt_fd != STDIN_FILENO)
  165. ret = close(txt_fd);
  166. /* Parse a byte at time until reaching the file OR until the environment fills
  167. * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */
  168. for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) {
  169. if (filebuf[fp] == '\n') {
  170. if (fp == 0 || filebuf[fp-1] == '\n') {
  171. /*
  172. * Skip empty lines.
  173. */
  174. continue;
  175. } else if (filebuf[fp-1] == '\\') {
  176. /*
  177. * Embedded newline in a variable.
  178. *
  179. * The backslash was added to the envptr; rewind
  180. * and replace it with a newline
  181. */
  182. ep--;
  183. envptr[ep++] = '\n';
  184. } else {
  185. /* End of a variable */
  186. envptr[ep++] = '\0';
  187. }
  188. } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
  189. /* Comment, skip the line. */
  190. while (++fp < filesize && filebuf[fp] != '\n')
  191. continue;
  192. } else {
  193. envptr[ep++] = filebuf[fp];
  194. }
  195. }
  196. /* If there are more bytes in the file still, it means the env filled up
  197. * before parsing the whole file. Eat comments & whitespace here to see if
  198. * there was anything meaning full left in the file, and if so, throw a error
  199. * and exit. */
  200. for( ; fp < filesize; fp++ )
  201. {
  202. if (filebuf[fp] == '\n') {
  203. if (fp == 0 || filebuf[fp-1] == '\n') {
  204. /* Ignore blank lines */
  205. continue;
  206. }
  207. } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
  208. while (++fp < filesize && filebuf[fp] != '\n')
  209. continue;
  210. } else {
  211. fprintf(stderr, "The environment file is too large for the target environment storage\n");
  212. return EXIT_FAILURE;
  213. }
  214. }
  215. /*
  216. * Make sure there is a final '\0'
  217. * And do it again on the next byte to mark the end of the environment.
  218. */
  219. if (envptr[ep-1] != '\0') {
  220. envptr[ep++] = '\0';
  221. /*
  222. * The text file doesn't have an ending newline. We need to
  223. * check the env size again to make sure we have room for two \0
  224. */
  225. if (ep >= envsize) {
  226. fprintf(stderr, "The environment file is too large for the target environment storage\n");
  227. return EXIT_FAILURE;
  228. }
  229. envptr[ep] = '\0';
  230. } else {
  231. envptr[ep] = '\0';
  232. }
  233. /* Computes the CRC and put it at the beginning of the data */
  234. crc = crc32(0, envptr, envsize);
  235. targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
  236. memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
  237. if (redundant)
  238. dataptr[sizeof(targetendian_crc)] = 1;
  239. if (!bin_filename || strcmp(bin_filename, "-") == 0) {
  240. bin_fd = STDOUT_FILENO;
  241. } else {
  242. bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
  243. S_IWGRP);
  244. if (bin_fd == -1) {
  245. fprintf(stderr, "Can't open output file \"%s\": %s\n",
  246. bin_filename, strerror(errno));
  247. return EXIT_FAILURE;
  248. }
  249. }
  250. if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
  251. sizeof(*dataptr) * datasize) {
  252. fprintf(stderr, "write() failed: %s\n", strerror(errno));
  253. return EXIT_FAILURE;
  254. }
  255. ret = close(bin_fd);
  256. return ret;
  257. }