mkenvimage.c 8.0 KB

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