mkenvimage.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /* We want the GNU version of basename() */
  28. #define _GNU_SOURCE
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stdint.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/mman.h>
  39. #include "compiler.h"
  40. #include <u-boot/crc.h>
  41. #include <version.h>
  42. #define CRC_SIZE sizeof(uint32_t)
  43. static void usage(const char *exec_name)
  44. {
  45. fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
  46. "\n"
  47. "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"
  48. "\n"
  49. "\tThe input file is in format:\n"
  50. "\t\tkey1=value1\n"
  51. "\t\tkey2=value2\n"
  52. "\t\t...\n"
  53. "\t-r : the environment has multiple copies in flash\n"
  54. "\t-b : the target is big endian (default is little endian)\n"
  55. "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
  56. "\t-V : print version information and exit\n"
  57. "\n"
  58. "If the input file is \"-\", data is read from standard input\n",
  59. exec_name);
  60. }
  61. long int xstrtol(const char *s)
  62. {
  63. long int tmp;
  64. errno = 0;
  65. tmp = strtol(s, NULL, 0);
  66. if (!errno)
  67. return tmp;
  68. if (errno == ERANGE)
  69. fprintf(stderr, "Bad integer format: %s\n", s);
  70. else
  71. fprintf(stderr, "Error while parsing %s: %s\n", s,
  72. strerror(errno));
  73. exit(EXIT_FAILURE);
  74. }
  75. int main(int argc, char **argv)
  76. {
  77. uint32_t crc, targetendian_crc;
  78. const char *txt_filename = NULL, *bin_filename = NULL;
  79. int txt_fd, bin_fd;
  80. unsigned char *dataptr, *envptr;
  81. unsigned char *filebuf = NULL;
  82. unsigned int filesize = 0, envsize = 0, datasize = 0;
  83. int bigendian = 0;
  84. int redundant = 0;
  85. unsigned char padbyte = 0xff;
  86. int option;
  87. int ret = EXIT_SUCCESS;
  88. struct stat txt_file_stat;
  89. int fp, ep;
  90. const char *prg;
  91. prg = basename(argv[0]);
  92. /* Turn off getopt()'s internal error message */
  93. opterr = 0;
  94. /* Parse the cmdline */
  95. while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
  96. switch (option) {
  97. case 's':
  98. datasize = xstrtol(optarg);
  99. break;
  100. case 'o':
  101. bin_filename = strdup(optarg);
  102. if (!bin_filename) {
  103. fprintf(stderr, "Can't strdup() the output filename\n");
  104. return EXIT_FAILURE;
  105. }
  106. break;
  107. case 'r':
  108. redundant = 1;
  109. break;
  110. case 'b':
  111. bigendian = 1;
  112. break;
  113. case 'p':
  114. padbyte = xstrtol(optarg);
  115. break;
  116. case 'h':
  117. usage(prg);
  118. return EXIT_SUCCESS;
  119. case 'V':
  120. printf("%s version %s\n", prg, PLAIN_VERSION);
  121. return EXIT_SUCCESS;
  122. case ':':
  123. fprintf(stderr, "Missing argument for option -%c\n",
  124. optopt);
  125. usage(prg);
  126. return EXIT_FAILURE;
  127. default:
  128. fprintf(stderr, "Wrong option -%c\n", optopt);
  129. usage(prg);
  130. return EXIT_FAILURE;
  131. }
  132. }
  133. /* Check datasize and allocate the data */
  134. if (datasize == 0) {
  135. fprintf(stderr, "Please specify the size of the environment partition.\n");
  136. usage(prg);
  137. return EXIT_FAILURE;
  138. }
  139. dataptr = malloc(datasize * sizeof(*dataptr));
  140. if (!dataptr) {
  141. fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
  142. datasize);
  143. return EXIT_FAILURE;
  144. }
  145. /*
  146. * envptr points to the beginning of the actual environment (after the
  147. * crc and possible `redundant' byte
  148. */
  149. envsize = datasize - (CRC_SIZE + redundant);
  150. envptr = dataptr + CRC_SIZE + redundant;
  151. /* Pad the environment with the padding byte */
  152. memset(envptr, padbyte, envsize);
  153. /* Open the input file ... */
  154. if (optind >= argc || strcmp(argv[optind], "-") == 0) {
  155. int readbytes = 0;
  156. int readlen = sizeof(*envptr) * 4096;
  157. txt_fd = STDIN_FILENO;
  158. do {
  159. filebuf = realloc(filebuf, readlen);
  160. if (!filebuf) {
  161. fprintf(stderr, "Can't realloc memory for the input file buffer\n");
  162. return EXIT_FAILURE;
  163. }
  164. readbytes = read(txt_fd, filebuf + filesize, readlen);
  165. if (errno) {
  166. fprintf(stderr, "Error while reading stdin: %s\n",
  167. strerror(errno));
  168. return EXIT_FAILURE;
  169. }
  170. filesize += readbytes;
  171. } while (readbytes == readlen);
  172. } else {
  173. txt_filename = argv[optind];
  174. txt_fd = open(txt_filename, O_RDONLY);
  175. if (txt_fd == -1) {
  176. fprintf(stderr, "Can't open \"%s\": %s\n",
  177. txt_filename, strerror(errno));
  178. return EXIT_FAILURE;
  179. }
  180. /* ... and check it */
  181. ret = fstat(txt_fd, &txt_file_stat);
  182. if (ret == -1) {
  183. fprintf(stderr, "Can't stat() on \"%s\": %s\n",
  184. txt_filename, strerror(errno));
  185. return EXIT_FAILURE;
  186. }
  187. filesize = txt_file_stat.st_size;
  188. filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
  189. MAP_PRIVATE, txt_fd, 0);
  190. if (filebuf == MAP_FAILED) {
  191. fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
  192. sizeof(*envptr) * filesize,
  193. strerror(errno));
  194. fprintf(stderr, "Falling back to read()\n");
  195. filebuf = malloc(sizeof(*envptr) * filesize);
  196. ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
  197. if (ret != sizeof(*envptr) * filesize) {
  198. fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
  199. sizeof(*envptr) * filesize,
  200. strerror(errno));
  201. return EXIT_FAILURE;
  202. }
  203. }
  204. ret = close(txt_fd);
  205. }
  206. /* The +1 is for the additionnal ending \0. See below. */
  207. if (filesize + 1 > envsize) {
  208. fprintf(stderr, "The input file is larger than the environment partition size\n");
  209. return EXIT_FAILURE;
  210. }
  211. /* Replace newlines separating variables with \0 */
  212. for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
  213. if (filebuf[fp] == '\n') {
  214. if (ep == 0) {
  215. /*
  216. * Newlines at the beginning of the file ?
  217. * Ignore them.
  218. */
  219. continue;
  220. } else if (filebuf[fp-1] == '\\') {
  221. /*
  222. * Embedded newline in a variable.
  223. *
  224. * The backslash was added to the envptr; rewind
  225. * and replace it with a newline
  226. */
  227. ep--;
  228. envptr[ep++] = '\n';
  229. } else {
  230. /* End of a variable */
  231. envptr[ep++] = '\0';
  232. }
  233. } else {
  234. envptr[ep++] = filebuf[fp];
  235. }
  236. }
  237. /*
  238. * Make sure there is a final '\0'
  239. * And do it again on the next byte to mark the end of the environment.
  240. */
  241. if (envptr[ep-1] != '\0') {
  242. envptr[ep++] = '\0';
  243. /*
  244. * The text file doesn't have an ending newline. We need to
  245. * check the env size again to make sure we have room for two \0
  246. */
  247. if (ep >= envsize) {
  248. fprintf(stderr, "The environment file is too large for the target environment storage\n");
  249. return EXIT_FAILURE;
  250. }
  251. envptr[ep] = '\0';
  252. } else {
  253. envptr[ep] = '\0';
  254. }
  255. /* Computes the CRC and put it at the beginning of the data */
  256. crc = crc32(0, envptr, envsize);
  257. targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
  258. memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
  259. if (redundant)
  260. dataptr[sizeof(targetendian_crc)] = 1;
  261. if (!bin_filename || strcmp(bin_filename, "-") == 0) {
  262. bin_fd = STDOUT_FILENO;
  263. } else {
  264. bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
  265. S_IWGRP);
  266. if (bin_fd == -1) {
  267. fprintf(stderr, "Can't open output file \"%s\": %s\n",
  268. bin_filename, strerror(errno));
  269. return EXIT_FAILURE;
  270. }
  271. }
  272. if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
  273. sizeof(*dataptr) * datasize) {
  274. fprintf(stderr, "write() failed: %s\n", strerror(errno));
  275. return EXIT_FAILURE;
  276. }
  277. ret = close(bin_fd);
  278. return ret;
  279. }