mkenvimage.c 8.1 KB

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