mkenvimage.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /* Parse the cmdline */
  75. while ((option = getopt(argc, argv, "s:o:rbp:h")) != -1) {
  76. switch (option) {
  77. case 's':
  78. datasize = strtol(optarg, NULL, 0);
  79. break;
  80. case 'o':
  81. bin_filename = strdup(optarg);
  82. if (!bin_filename) {
  83. fprintf(stderr, "Can't strdup() the output "
  84. "filename\n");
  85. return EXIT_FAILURE;
  86. }
  87. break;
  88. case 'r':
  89. redundant = 1;
  90. break;
  91. case 'b':
  92. bigendian = 1;
  93. break;
  94. case 'p':
  95. padbyte = strtol(optarg, NULL, 0);
  96. break;
  97. case 'h':
  98. usage(argv[0]);
  99. return EXIT_SUCCESS;
  100. default:
  101. fprintf(stderr, "Wrong option -%c\n", option);
  102. usage(argv[0]);
  103. return EXIT_FAILURE;
  104. }
  105. }
  106. /* Check datasize and allocate the data */
  107. if (datasize == 0) {
  108. fprintf(stderr,
  109. "Please specify the size of the envrionnment "
  110. "partition.\n");
  111. usage(argv[0]);
  112. return EXIT_FAILURE;
  113. }
  114. dataptr = malloc(datasize * sizeof(*dataptr));
  115. if (!dataptr) {
  116. fprintf(stderr, "Can't alloc dataptr.\n");
  117. return EXIT_FAILURE;
  118. }
  119. /*
  120. * envptr points to the beginning of the actual environment (after the
  121. * crc and possible `redundant' bit
  122. */
  123. envsize = datasize - (CRC_SIZE + redundant);
  124. envptr = dataptr + CRC_SIZE + redundant;
  125. /* Pad the environment with the padding byte */
  126. memset(envptr, padbyte, envsize);
  127. /* Open the input file ... */
  128. if (optind >= argc) {
  129. fprintf(stderr, "Please specify an input filename\n");
  130. return EXIT_FAILURE;
  131. }
  132. txt_filename = argv[optind];
  133. if (strcmp(txt_filename, "-") == 0) {
  134. int readbytes = 0;
  135. int readlen = sizeof(*envptr) * 2048;
  136. txt_fd = STDIN_FILENO;
  137. do {
  138. filebuf = realloc(filebuf, readlen);
  139. readbytes = read(txt_fd, filebuf + filesize, readlen);
  140. filesize += readbytes;
  141. } while (readbytes == readlen);
  142. } else {
  143. txt_fd = open(txt_filename, O_RDONLY);
  144. if (txt_fd == -1) {
  145. fprintf(stderr, "Can't open \"%s\": %s\n",
  146. txt_filename, strerror(errno));
  147. return EXIT_FAILURE;
  148. }
  149. /* ... and check it */
  150. ret = fstat(txt_fd, &txt_file_stat);
  151. if (ret == -1) {
  152. fprintf(stderr, "Can't stat() on \"%s\": "
  153. "%s\n", txt_filename, strerror(errno));
  154. return EXIT_FAILURE;
  155. }
  156. filesize = txt_file_stat.st_size;
  157. /* Read the raw input file and transform it */
  158. filebuf = malloc(sizeof(*envptr) * filesize);
  159. ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
  160. if (ret != sizeof(*envptr) * filesize) {
  161. fprintf(stderr, "Can't read the whole input file\n");
  162. return EXIT_FAILURE;
  163. }
  164. ret = close(txt_fd);
  165. }
  166. /*
  167. * The right test to do is "=>" (not ">") because of the additionnal
  168. * ending \0. See below.
  169. */
  170. if (filesize >= envsize) {
  171. fprintf(stderr, "The input file is larger than the "
  172. "envrionnment partition size\n");
  173. return EXIT_FAILURE;
  174. }
  175. /* Replace newlines separating variables with \0 */
  176. for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
  177. if (filebuf[fp] == '\n') {
  178. if (fp == 0) {
  179. /*
  180. * Newline at the beggining of the file ?
  181. * Ignore it.
  182. */
  183. continue;
  184. } else if (filebuf[fp-1] == '\\') {
  185. /*
  186. * Embedded newline in a variable.
  187. *
  188. * The backslash was added to the envptr ;
  189. * rewind and replace it with a newline
  190. */
  191. ep--;
  192. envptr[ep++] = '\n';
  193. } else {
  194. /* End of a variable */
  195. envptr[ep++] = '\0';
  196. }
  197. } else if (filebuf[fp] == '#') {
  198. if (fp != 0 && filebuf[fp-1] == '\n') {
  199. /* This line is a comment, let's skip it */
  200. while (fp < txt_file_stat.st_size && fp++ &&
  201. filebuf[fp] != '\n');
  202. } else {
  203. envptr[ep++] = filebuf[fp];
  204. }
  205. } else {
  206. envptr[ep++] = filebuf[fp];
  207. }
  208. }
  209. /*
  210. * Make sure there is a final '\0'
  211. * And do it again on the next byte to mark the end of the environment.
  212. */
  213. if (envptr[ep-1] != '\0') {
  214. envptr[ep++] = '\0';
  215. /*
  216. * The text file doesn't have an ending newline. We need to
  217. * check the env size again to make sure we have room for two \0
  218. */
  219. if (ep >= envsize) {
  220. fprintf(stderr, "The environment file is too large for "
  221. "the target environment storage\n");
  222. return EXIT_FAILURE;
  223. }
  224. envptr[ep] = '\0';
  225. } else {
  226. envptr[ep] = '\0';
  227. }
  228. /* Computes the CRC and put it at the beginning of the data */
  229. crc = crc32(0, envptr, envsize);
  230. targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
  231. memcpy(dataptr, &targetendian_crc, sizeof(uint32_t));
  232. bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  233. if (bin_fd == -1) {
  234. fprintf(stderr, "Can't open output file \"%s\": %s\n",
  235. bin_filename, strerror(errno));
  236. return EXIT_FAILURE;
  237. }
  238. if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
  239. sizeof(*dataptr) * datasize) {
  240. fprintf(stderr, "write() failed: %s\n", strerror(errno));
  241. return EXIT_FAILURE;
  242. }
  243. ret = close(bin_fd);
  244. return ret;
  245. }