]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - tools/mkenvimage.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011 Free Electrons
4 * David Wagner <david.wagner@free-electrons.com>
6 * Inspired from envcrc.c:
8 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
17 #include <u-boot/crc.h>
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 #define CHUNK_SIZE 4096
71 int main(int argc
, char **argv
)
73 uint32_t crc
, targetendian_crc
;
74 const char *bin_filename
= NULL
;
76 unsigned char *dataptr
, *envptr
;
77 unsigned char *filebuf
= NULL
;
78 unsigned int filesize
= 0, envsize
= 0, datasize
= 0;
81 unsigned char padbyte
= 0xff;
85 int ret
= EXIT_SUCCESS
;
90 prg
= basename(argv
[0]);
92 /* Turn off getopt()'s internal error message */
95 /* Parse the cmdline */
96 while ((option
= getopt(argc
, argv
, ":s:o:rbp:hV")) != -1) {
99 datasize
= xstrtol(optarg
);
102 bin_filename
= strdup(optarg
);
104 fprintf(stderr
, "Can't strdup() the output filename\n");
115 padbyte
= xstrtol(optarg
);
121 printf("%s version %s\n", prg
, PLAIN_VERSION
);
124 fprintf(stderr
, "Missing argument for option -%c\n",
129 fprintf(stderr
, "Wrong option -%c\n", optopt
);
135 /* Check datasize and allocate the data */
137 fprintf(stderr
, "Please specify the size of the environment partition.\n");
142 dataptr
= malloc(datasize
* sizeof(*dataptr
));
144 fprintf(stderr
, "Can't alloc %d bytes for dataptr.\n",
150 * envptr points to the beginning of the actual environment (after the
151 * crc and possible `redundant' byte
153 envsize
= datasize
- (CRC_SIZE
+ redundant
);
154 envptr
= dataptr
+ CRC_SIZE
+ redundant
;
156 /* Pad the environment with the padding byte */
157 memset(envptr
, padbyte
, envsize
);
159 /* Open the input file ... */
160 if (optind
>= argc
|| strcmp(argv
[optind
], "-") == 0) {
161 txt_fd
= STDIN_FILENO
;
163 txt_fd
= open(argv
[optind
], O_RDONLY
);
165 fprintf(stderr
, "Can't open \"%s\": %s\n",
166 argv
[optind
], strerror(errno
));
172 filebuf
= realloc(filebuf
, filesize
+ CHUNK_SIZE
);
174 fprintf(stderr
, "Can't realloc memory for the input file buffer\n");
177 readbytes
= read(txt_fd
, filebuf
+ filesize
, CHUNK_SIZE
);
179 fprintf(stderr
, "Error while reading: %s\n",
183 filesize
+= readbytes
;
184 } while (readbytes
> 0);
186 if (txt_fd
!= STDIN_FILENO
)
189 /* Parse a byte at time until reaching the file OR until the environment fills
190 * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */
191 for (fp
= 0, ep
= 0 ; fp
< filesize
&& ep
< envsize
- 1; fp
++) {
192 if (filebuf
[fp
] == '\n') {
193 if (fp
== 0 || filebuf
[fp
-1] == '\n') {
198 } else if (filebuf
[fp
-1] == '\\') {
200 * Embedded newline in a variable.
202 * The backslash was added to the envptr; rewind
203 * and replace it with a newline
208 /* End of a variable */
211 } else if ((fp
== 0 || filebuf
[fp
-1] == '\n') && filebuf
[fp
] == '#') {
212 /* Comment, skip the line. */
213 while (++fp
< filesize
&& filebuf
[fp
] != '\n')
216 envptr
[ep
++] = filebuf
[fp
];
219 /* If there are more bytes in the file still, it means the env filled up
220 * before parsing the whole file. Eat comments & whitespace here to see if
221 * there was anything meaning full left in the file, and if so, throw a error
223 for( ; fp
< filesize
; fp
++ )
225 if (filebuf
[fp
] == '\n') {
226 if (fp
== 0 || filebuf
[fp
-1] == '\n') {
227 /* Ignore blank lines */
230 } else if ((fp
== 0 || filebuf
[fp
-1] == '\n') && filebuf
[fp
] == '#') {
231 while (++fp
< filesize
&& filebuf
[fp
] != '\n')
234 fprintf(stderr
, "The environment file is too large for the target environment storage\n");
239 * Make sure there is a final '\0'
240 * And do it again on the next byte to mark the end of the environment.
242 if (envptr
[ep
-1] != '\0') {
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
249 fprintf(stderr
, "The environment file is too large for the target environment storage\n");
257 /* Computes the CRC and put it at the beginning of the data */
258 crc
= crc32(0, envptr
, envsize
);
259 targetendian_crc
= bigendian
? cpu_to_be32(crc
) : cpu_to_le32(crc
);
261 memcpy(dataptr
, &targetendian_crc
, sizeof(targetendian_crc
));
263 dataptr
[sizeof(targetendian_crc
)] = 1;
265 if (!bin_filename
|| strcmp(bin_filename
, "-") == 0) {
266 bin_fd
= STDOUT_FILENO
;
268 bin_fd
= creat(bin_filename
, S_IRUSR
| S_IWUSR
| S_IRGRP
|
271 fprintf(stderr
, "Can't open output file \"%s\": %s\n",
272 bin_filename
, strerror(errno
));
277 if (write(bin_fd
, dataptr
, sizeof(*dataptr
) * datasize
) !=
278 sizeof(*dataptr
) * datasize
) {
279 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));