2 * (C) Copyright 2011 Free Electrons
3 * David Wagner <david.wagner@free-electrons.com>
5 * Inspired from envcrc.c:
7 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
9 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 /* We want the GNU version of basename() */
39 #include <sys/types.h>
44 #include <u-boot/crc.h>
47 #define CRC_SIZE sizeof(uint32_t)
49 static void usage(const char *exec_name)
51 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
53 "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"
55 "\tThe input file is in format:\n"
59 "\t-r : the environment has multiple copies in flash\n"
60 "\t-b : the target is big endian (default is little endian)\n"
61 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
62 "\t-V : print version information and exit\n"
64 "If the input file is \"-\", data is read from standard input\n",
68 long int xstrtol(const char *s)
73 tmp = strtol(s, NULL, 0);
78 fprintf(stderr, "Bad integer format: %s\n", s);
80 fprintf(stderr, "Error while parsing %s: %s\n", s,
86 int main(int argc, char **argv)
88 uint32_t crc, targetendian_crc;
89 const char *txt_filename = NULL, *bin_filename = NULL;
91 unsigned char *dataptr, *envptr;
92 unsigned char *filebuf = NULL;
93 unsigned int filesize = 0, envsize = 0, datasize = 0;
96 unsigned char padbyte = 0xff;
99 int ret = EXIT_SUCCESS;
101 struct stat txt_file_stat;
106 prg = basename(argv[0]);
108 /* Turn off getopt()'s internal error message */
111 /* Parse the cmdline */
112 while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
115 datasize = xstrtol(optarg);
118 bin_filename = strdup(optarg);
120 fprintf(stderr, "Can't strdup() the output filename\n");
131 padbyte = xstrtol(optarg);
137 printf("%s version %s\n", prg, PLAIN_VERSION);
140 fprintf(stderr, "Missing argument for option -%c\n",
145 fprintf(stderr, "Wrong option -%c\n", optopt);
151 /* Check datasize and allocate the data */
153 fprintf(stderr, "Please specify the size of the environment partition.\n");
158 dataptr = malloc(datasize * sizeof(*dataptr));
160 fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
166 * envptr points to the beginning of the actual environment (after the
167 * crc and possible `redundant' byte
169 envsize = datasize - (CRC_SIZE + redundant);
170 envptr = dataptr + CRC_SIZE + redundant;
172 /* Pad the environment with the padding byte */
173 memset(envptr, padbyte, envsize);
175 /* Open the input file ... */
176 if (optind >= argc || strcmp(argv[optind], "-") == 0) {
178 int readlen = sizeof(*envptr) * 4096;
179 txt_fd = STDIN_FILENO;
182 filebuf = realloc(filebuf, readlen);
184 fprintf(stderr, "Can't realloc memory for the input file buffer\n");
187 readbytes = read(txt_fd, filebuf + filesize, readlen);
189 fprintf(stderr, "Error while reading stdin: %s\n",
193 filesize += readbytes;
194 } while (readbytes == readlen);
197 txt_filename = argv[optind];
198 txt_fd = open(txt_filename, O_RDONLY);
200 fprintf(stderr, "Can't open \"%s\": %s\n",
201 txt_filename, strerror(errno));
204 /* ... and check it */
205 ret = fstat(txt_fd, &txt_file_stat);
207 fprintf(stderr, "Can't stat() on \"%s\": %s\n",
208 txt_filename, strerror(errno));
212 filesize = txt_file_stat.st_size;
214 filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
215 MAP_PRIVATE, txt_fd, 0);
216 if (filebuf == MAP_FAILED) {
217 fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
218 sizeof(*envptr) * filesize,
220 fprintf(stderr, "Falling back to read()\n");
222 filebuf = malloc(sizeof(*envptr) * filesize);
223 ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
224 if (ret != sizeof(*envptr) * filesize) {
225 fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
226 sizeof(*envptr) * filesize,
234 /* The +1 is for the additionnal ending \0. See below. */
235 if (filesize + 1 > envsize) {
236 fprintf(stderr, "The input file is larger than the environment partition size\n");
240 /* Replace newlines separating variables with \0 */
241 for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
242 if (filebuf[fp] == '\n') {
245 * Newlines at the beginning of the file ?
249 } else if (filebuf[fp-1] == '\\') {
251 * Embedded newline in a variable.
253 * The backslash was added to the envptr; rewind
254 * and replace it with a newline
259 /* End of a variable */
263 envptr[ep++] = filebuf[fp];
267 * Make sure there is a final '\0'
268 * And do it again on the next byte to mark the end of the environment.
270 if (envptr[ep-1] != '\0') {
273 * The text file doesn't have an ending newline. We need to
274 * check the env size again to make sure we have room for two \0
277 fprintf(stderr, "The environment file is too large for the target environment storage\n");
285 /* Computes the CRC and put it at the beginning of the data */
286 crc = crc32(0, envptr, envsize);
287 targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
289 memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
291 dataptr[sizeof(targetendian_crc)] = 1;
293 if (!bin_filename || strcmp(bin_filename, "-") == 0) {
294 bin_fd = STDOUT_FILENO;
296 bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
299 fprintf(stderr, "Can't open output file \"%s\": %s\n",
300 bin_filename, strerror(errno));
305 if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
306 sizeof(*dataptr) * datasize) {
307 fprintf(stderr, "write() failed: %s\n", strerror(errno));