]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/mkenvimage.c
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 * SPDX-License-Identifier: GPL-2.0+
20 #include <sys/types.h>
25 #include <u-boot/crc.h>
28 #define CRC_SIZE sizeof(uint32_t)
30 static void usage(const char *exec_name
)
32 fprintf(stderr
, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
34 "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"
36 "\tThe input file is in format:\n"
40 "\tEmpty lines are skipped, and lines with a # in the first\n"
41 "\tcolumn are treated as comments (also skipped).\n"
42 "\t-r : the environment has multiple copies in flash\n"
43 "\t-b : the target is big endian (default is little endian)\n"
44 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
45 "\t-V : print version information and exit\n"
47 "If the input file is \"-\", data is read from standard input\n",
51 long int xstrtol(const char *s
)
56 tmp
= strtol(s
, NULL
, 0);
61 fprintf(stderr
, "Bad integer format: %s\n", s
);
63 fprintf(stderr
, "Error while parsing %s: %s\n", s
,
69 int main(int argc
, char **argv
)
71 uint32_t crc
, targetendian_crc
;
72 const char *txt_filename
= NULL
, *bin_filename
= NULL
;
74 unsigned char *dataptr
, *envptr
;
75 unsigned char *filebuf
= NULL
;
76 unsigned int filesize
= 0, envsize
= 0, datasize
= 0;
79 unsigned char padbyte
= 0xff;
82 int ret
= EXIT_SUCCESS
;
84 struct stat txt_file_stat
;
89 prg
= basename(argv
[0]);
91 /* Turn off getopt()'s internal error message */
94 /* Parse the cmdline */
95 while ((option
= getopt(argc
, argv
, ":s:o:rbp:hV")) != -1) {
98 datasize
= xstrtol(optarg
);
101 bin_filename
= strdup(optarg
);
103 fprintf(stderr
, "Can't strdup() the output filename\n");
114 padbyte
= xstrtol(optarg
);
120 printf("%s version %s\n", prg
, PLAIN_VERSION
);
123 fprintf(stderr
, "Missing argument for option -%c\n",
128 fprintf(stderr
, "Wrong option -%c\n", optopt
);
134 /* Check datasize and allocate the data */
136 fprintf(stderr
, "Please specify the size of the environment partition.\n");
141 dataptr
= malloc(datasize
* sizeof(*dataptr
));
143 fprintf(stderr
, "Can't alloc %d bytes for dataptr.\n",
149 * envptr points to the beginning of the actual environment (after the
150 * crc and possible `redundant' byte
152 envsize
= datasize
- (CRC_SIZE
+ redundant
);
153 envptr
= dataptr
+ CRC_SIZE
+ redundant
;
155 /* Pad the environment with the padding byte */
156 memset(envptr
, padbyte
, envsize
);
158 /* Open the input file ... */
159 if (optind
>= argc
|| strcmp(argv
[optind
], "-") == 0) {
161 int readlen
= sizeof(*envptr
) * 4096;
162 txt_fd
= STDIN_FILENO
;
165 filebuf
= realloc(filebuf
, readlen
);
167 fprintf(stderr
, "Can't realloc memory for the input file buffer\n");
170 readbytes
= read(txt_fd
, filebuf
+ filesize
, readlen
);
172 fprintf(stderr
, "Error while reading stdin: %s\n",
176 filesize
+= readbytes
;
177 } while (readbytes
== readlen
);
180 txt_filename
= argv
[optind
];
181 txt_fd
= open(txt_filename
, O_RDONLY
);
183 fprintf(stderr
, "Can't open \"%s\": %s\n",
184 txt_filename
, strerror(errno
));
187 /* ... and check it */
188 ret
= fstat(txt_fd
, &txt_file_stat
);
190 fprintf(stderr
, "Can't stat() on \"%s\": %s\n",
191 txt_filename
, strerror(errno
));
195 filesize
= txt_file_stat
.st_size
;
197 filebuf
= mmap(NULL
, sizeof(*envptr
) * filesize
, PROT_READ
,
198 MAP_PRIVATE
, txt_fd
, 0);
199 if (filebuf
== MAP_FAILED
) {
200 fprintf(stderr
, "mmap (%zu bytes) failed: %s\n",
201 sizeof(*envptr
) * filesize
,
203 fprintf(stderr
, "Falling back to read()\n");
205 filebuf
= malloc(sizeof(*envptr
) * filesize
);
206 ret
= read(txt_fd
, filebuf
, sizeof(*envptr
) * filesize
);
207 if (ret
!= sizeof(*envptr
) * filesize
) {
208 fprintf(stderr
, "Can't read the whole input file (%zu bytes): %s\n",
209 sizeof(*envptr
) * filesize
,
218 /* Parse a byte at time until reaching the file OR until the environment fills
219 * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */
220 for (fp
= 0, ep
= 0 ; fp
< filesize
&& ep
< envsize
- 1; fp
++) {
221 if (filebuf
[fp
] == '\n') {
222 if (fp
== 0 || filebuf
[fp
-1] == '\n') {
227 } else if (filebuf
[fp
-1] == '\\') {
229 * Embedded newline in a variable.
231 * The backslash was added to the envptr; rewind
232 * and replace it with a newline
237 /* End of a variable */
240 } else if ((fp
== 0 || filebuf
[fp
-1] == '\n') && filebuf
[fp
] == '#') {
241 /* Comment, skip the line. */
242 while (++fp
< filesize
&& filebuf
[fp
] != '\n')
245 envptr
[ep
++] = filebuf
[fp
];
248 /* If there are more bytes in the file still, it means the env filled up
249 * before parsing the whole file. Eat comments & whitespace here to see if
250 * there was anything meaning full left in the file, and if so, throw a error
252 for( ; fp
< filesize
; fp
++ )
254 if (filebuf
[fp
] == '\n') {
255 if (fp
== 0 || filebuf
[fp
-1] == '\n') {
256 /* Ignore blank lines */
259 } else if ((fp
== 0 || filebuf
[fp
-1] == '\n') && filebuf
[fp
] == '#') {
260 while (++fp
< filesize
&& filebuf
[fp
] != '\n')
263 fprintf(stderr
, "The environment file is too large for the target environment storage\n");
268 * Make sure there is a final '\0'
269 * And do it again on the next byte to mark the end of the environment.
271 if (envptr
[ep
-1] != '\0') {
274 * The text file doesn't have an ending newline. We need to
275 * check the env size again to make sure we have room for two \0
278 fprintf(stderr
, "The environment file is too large for the target environment storage\n");
286 /* Computes the CRC and put it at the beginning of the data */
287 crc
= crc32(0, envptr
, envsize
);
288 targetendian_crc
= bigendian
? cpu_to_be32(crc
) : cpu_to_le32(crc
);
290 memcpy(dataptr
, &targetendian_crc
, sizeof(targetendian_crc
));
292 dataptr
[sizeof(targetendian_crc
)] = 1;
294 if (!bin_filename
|| strcmp(bin_filename
, "-") == 0) {
295 bin_fd
= STDOUT_FILENO
;
297 bin_fd
= creat(bin_filename
, S_IRUSR
| S_IWUSR
| S_IRGRP
|
300 fprintf(stderr
, "Can't open output file \"%s\": %s\n",
301 bin_filename
, strerror(errno
));
306 if (write(bin_fd
, dataptr
, sizeof(*dataptr
) * datasize
) !=
307 sizeof(*dataptr
) * datasize
) {
308 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));