mkenvimage.c 7.3 KB

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