mkenvimage.c 7.4 KB

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