mkenvimage.c 7.5 KB

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