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