mkenvimage.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. "\t-r : the environment has multiple copies in flash\n"
  37. "\t-b : the target is big endian (default is little endian)\n"
  38. "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
  39. "\t-V : print version information and exit\n"
  40. "\n"
  41. "If the input file is \"-\", data is read from standard input\n",
  42. exec_name);
  43. }
  44. long int xstrtol(const char *s)
  45. {
  46. long int tmp;
  47. errno = 0;
  48. tmp = strtol(s, NULL, 0);
  49. if (!errno)
  50. return tmp;
  51. if (errno == ERANGE)
  52. fprintf(stderr, "Bad integer format: %s\n", s);
  53. else
  54. fprintf(stderr, "Error while parsing %s: %s\n", s,
  55. strerror(errno));
  56. exit(EXIT_FAILURE);
  57. }
  58. int main(int argc, char **argv)
  59. {
  60. uint32_t crc, targetendian_crc;
  61. const char *txt_filename = NULL, *bin_filename = NULL;
  62. int txt_fd, bin_fd;
  63. unsigned char *dataptr, *envptr;
  64. unsigned char *filebuf = NULL;
  65. unsigned int filesize = 0, envsize = 0, datasize = 0;
  66. int bigendian = 0;
  67. int redundant = 0;
  68. unsigned char padbyte = 0xff;
  69. int option;
  70. int ret = EXIT_SUCCESS;
  71. struct stat txt_file_stat;
  72. int fp, ep;
  73. const char *prg;
  74. prg = basename(argv[0]);
  75. /* Turn off getopt()'s internal error message */
  76. opterr = 0;
  77. /* Parse the cmdline */
  78. while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
  79. switch (option) {
  80. case 's':
  81. datasize = xstrtol(optarg);
  82. break;
  83. case 'o':
  84. bin_filename = strdup(optarg);
  85. if (!bin_filename) {
  86. fprintf(stderr, "Can't strdup() the output filename\n");
  87. return EXIT_FAILURE;
  88. }
  89. break;
  90. case 'r':
  91. redundant = 1;
  92. break;
  93. case 'b':
  94. bigendian = 1;
  95. break;
  96. case 'p':
  97. padbyte = xstrtol(optarg);
  98. break;
  99. case 'h':
  100. usage(prg);
  101. return EXIT_SUCCESS;
  102. case 'V':
  103. printf("%s version %s\n", prg, PLAIN_VERSION);
  104. return EXIT_SUCCESS;
  105. case ':':
  106. fprintf(stderr, "Missing argument for option -%c\n",
  107. optopt);
  108. usage(prg);
  109. return EXIT_FAILURE;
  110. default:
  111. fprintf(stderr, "Wrong option -%c\n", optopt);
  112. usage(prg);
  113. return EXIT_FAILURE;
  114. }
  115. }
  116. /* Check datasize and allocate the data */
  117. if (datasize == 0) {
  118. fprintf(stderr, "Please specify the size of the environment partition.\n");
  119. usage(prg);
  120. return EXIT_FAILURE;
  121. }
  122. dataptr = malloc(datasize * sizeof(*dataptr));
  123. if (!dataptr) {
  124. fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
  125. datasize);
  126. return EXIT_FAILURE;
  127. }
  128. /*
  129. * envptr points to the beginning of the actual environment (after the
  130. * crc and possible `redundant' byte
  131. */
  132. envsize = datasize - (CRC_SIZE + redundant);
  133. envptr = dataptr + CRC_SIZE + redundant;
  134. /* Pad the environment with the padding byte */
  135. memset(envptr, padbyte, envsize);
  136. /* Open the input file ... */
  137. if (optind >= argc || strcmp(argv[optind], "-") == 0) {
  138. int readbytes = 0;
  139. int readlen = sizeof(*envptr) * 4096;
  140. txt_fd = STDIN_FILENO;
  141. do {
  142. filebuf = realloc(filebuf, readlen);
  143. if (!filebuf) {
  144. fprintf(stderr, "Can't realloc memory for the input file buffer\n");
  145. return EXIT_FAILURE;
  146. }
  147. readbytes = read(txt_fd, filebuf + filesize, readlen);
  148. if (errno) {
  149. fprintf(stderr, "Error while reading stdin: %s\n",
  150. strerror(errno));
  151. return EXIT_FAILURE;
  152. }
  153. filesize += readbytes;
  154. } while (readbytes == readlen);
  155. } else {
  156. txt_filename = argv[optind];
  157. txt_fd = open(txt_filename, O_RDONLY);
  158. if (txt_fd == -1) {
  159. fprintf(stderr, "Can't open \"%s\": %s\n",
  160. txt_filename, strerror(errno));
  161. return EXIT_FAILURE;
  162. }
  163. /* ... and check it */
  164. ret = fstat(txt_fd, &txt_file_stat);
  165. if (ret == -1) {
  166. fprintf(stderr, "Can't stat() on \"%s\": %s\n",
  167. txt_filename, strerror(errno));
  168. return EXIT_FAILURE;
  169. }
  170. filesize = txt_file_stat.st_size;
  171. filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
  172. MAP_PRIVATE, txt_fd, 0);
  173. if (filebuf == MAP_FAILED) {
  174. fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
  175. sizeof(*envptr) * filesize,
  176. strerror(errno));
  177. fprintf(stderr, "Falling back to read()\n");
  178. filebuf = malloc(sizeof(*envptr) * filesize);
  179. ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
  180. if (ret != sizeof(*envptr) * filesize) {
  181. fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
  182. sizeof(*envptr) * filesize,
  183. strerror(errno));
  184. return EXIT_FAILURE;
  185. }
  186. }
  187. ret = close(txt_fd);
  188. }
  189. /* The +1 is for the additionnal ending \0. See below. */
  190. if (filesize + 1 > envsize) {
  191. fprintf(stderr, "The input file is larger than the environment partition size\n");
  192. return EXIT_FAILURE;
  193. }
  194. /* Replace newlines separating variables with \0 */
  195. for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
  196. if (filebuf[fp] == '\n') {
  197. if (ep == 0) {
  198. /*
  199. * Newlines at the beginning of the file ?
  200. * Ignore them.
  201. */
  202. continue;
  203. } else if (filebuf[fp-1] == '\\') {
  204. /*
  205. * Embedded newline in a variable.
  206. *
  207. * The backslash was added to the envptr; rewind
  208. * and replace it with a newline
  209. */
  210. ep--;
  211. envptr[ep++] = '\n';
  212. } else {
  213. /* End of a variable */
  214. envptr[ep++] = '\0';
  215. }
  216. } else {
  217. envptr[ep++] = filebuf[fp];
  218. }
  219. }
  220. /*
  221. * Make sure there is a final '\0'
  222. * And do it again on the next byte to mark the end of the environment.
  223. */
  224. if (envptr[ep-1] != '\0') {
  225. envptr[ep++] = '\0';
  226. /*
  227. * The text file doesn't have an ending newline. We need to
  228. * check the env size again to make sure we have room for two \0
  229. */
  230. if (ep >= envsize) {
  231. fprintf(stderr, "The environment file is too large for the target environment storage\n");
  232. return EXIT_FAILURE;
  233. }
  234. envptr[ep] = '\0';
  235. } else {
  236. envptr[ep] = '\0';
  237. }
  238. /* Computes the CRC and put it at the beginning of the data */
  239. crc = crc32(0, envptr, envsize);
  240. targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
  241. memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
  242. if (redundant)
  243. dataptr[sizeof(targetendian_crc)] = 1;
  244. if (!bin_filename || strcmp(bin_filename, "-") == 0) {
  245. bin_fd = STDOUT_FILENO;
  246. } else {
  247. bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
  248. S_IWGRP);
  249. if (bin_fd == -1) {
  250. fprintf(stderr, "Can't open output file \"%s\": %s\n",
  251. bin_filename, strerror(errno));
  252. return EXIT_FAILURE;
  253. }
  254. }
  255. if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
  256. sizeof(*dataptr) * datasize) {
  257. fprintf(stderr, "write() failed: %s\n", strerror(errno));
  258. return EXIT_FAILURE;
  259. }
  260. ret = close(bin_fd);
  261. return ret;
  262. }