]>
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 "\t-r : the environment has multiple copies in flash\n"
41 "\t-b : the target is big endian (default is little endian)\n"
42 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
43 "\t-V : print version information and exit\n"
45 "If the input file is \"-\", data is read from standard input\n",
49 long int xstrtol(const char *s
)
54 tmp
= strtol(s
, NULL
, 0);
59 fprintf(stderr
, "Bad integer format: %s\n", s
);
61 fprintf(stderr
, "Error while parsing %s: %s\n", s
,
67 int main(int argc
, char **argv
)
69 uint32_t crc
, targetendian_crc
;
70 const char *txt_filename
= NULL
, *bin_filename
= NULL
;
72 unsigned char *dataptr
, *envptr
;
73 unsigned char *filebuf
= NULL
;
74 unsigned int filesize
= 0, envsize
= 0, datasize
= 0;
77 unsigned char padbyte
= 0xff;
80 int ret
= EXIT_SUCCESS
;
82 struct stat txt_file_stat
;
87 prg
= basename(argv
[0]);
89 /* Turn off getopt()'s internal error message */
92 /* Parse the cmdline */
93 while ((option
= getopt(argc
, argv
, ":s:o:rbp:hV")) != -1) {
96 datasize
= xstrtol(optarg
);
99 bin_filename
= strdup(optarg
);
101 fprintf(stderr
, "Can't strdup() the output filename\n");
112 padbyte
= xstrtol(optarg
);
118 printf("%s version %s\n", prg
, PLAIN_VERSION
);
121 fprintf(stderr
, "Missing argument for option -%c\n",
126 fprintf(stderr
, "Wrong option -%c\n", optopt
);
132 /* Check datasize and allocate the data */
134 fprintf(stderr
, "Please specify the size of the environment partition.\n");
139 dataptr
= malloc(datasize
* sizeof(*dataptr
));
141 fprintf(stderr
, "Can't alloc %d bytes for dataptr.\n",
147 * envptr points to the beginning of the actual environment (after the
148 * crc and possible `redundant' byte
150 envsize
= datasize
- (CRC_SIZE
+ redundant
);
151 envptr
= dataptr
+ CRC_SIZE
+ redundant
;
153 /* Pad the environment with the padding byte */
154 memset(envptr
, padbyte
, envsize
);
156 /* Open the input file ... */
157 if (optind
>= argc
|| strcmp(argv
[optind
], "-") == 0) {
159 int readlen
= sizeof(*envptr
) * 4096;
160 txt_fd
= STDIN_FILENO
;
163 filebuf
= realloc(filebuf
, readlen
);
165 fprintf(stderr
, "Can't realloc memory for the input file buffer\n");
168 readbytes
= read(txt_fd
, filebuf
+ filesize
, readlen
);
170 fprintf(stderr
, "Error while reading stdin: %s\n",
174 filesize
+= readbytes
;
175 } while (readbytes
== readlen
);
178 txt_filename
= argv
[optind
];
179 txt_fd
= open(txt_filename
, O_RDONLY
);
181 fprintf(stderr
, "Can't open \"%s\": %s\n",
182 txt_filename
, strerror(errno
));
185 /* ... and check it */
186 ret
= fstat(txt_fd
, &txt_file_stat
);
188 fprintf(stderr
, "Can't stat() on \"%s\": %s\n",
189 txt_filename
, strerror(errno
));
193 filesize
= txt_file_stat
.st_size
;
195 filebuf
= mmap(NULL
, sizeof(*envptr
) * filesize
, PROT_READ
,
196 MAP_PRIVATE
, txt_fd
, 0);
197 if (filebuf
== MAP_FAILED
) {
198 fprintf(stderr
, "mmap (%zu bytes) failed: %s\n",
199 sizeof(*envptr
) * filesize
,
201 fprintf(stderr
, "Falling back to read()\n");
203 filebuf
= malloc(sizeof(*envptr
) * filesize
);
204 ret
= read(txt_fd
, filebuf
, sizeof(*envptr
) * filesize
);
205 if (ret
!= sizeof(*envptr
) * filesize
) {
206 fprintf(stderr
, "Can't read the whole input file (%zu bytes): %s\n",
207 sizeof(*envptr
) * filesize
,
215 /* The +1 is for the additionnal ending \0. See below. */
216 if (filesize
+ 1 > envsize
) {
217 fprintf(stderr
, "The input file is larger than the environment partition size\n");
221 /* Replace newlines separating variables with \0 */
222 for (fp
= 0, ep
= 0 ; fp
< filesize
; fp
++) {
223 if (filebuf
[fp
] == '\n') {
226 * Newlines at the beginning of the file ?
230 } else if (filebuf
[fp
-1] == '\\') {
232 * Embedded newline in a variable.
234 * The backslash was added to the envptr; rewind
235 * and replace it with a newline
240 /* End of a variable */
244 envptr
[ep
++] = filebuf
[fp
];
248 * Make sure there is a final '\0'
249 * And do it again on the next byte to mark the end of the environment.
251 if (envptr
[ep
-1] != '\0') {
254 * The text file doesn't have an ending newline. We need to
255 * check the env size again to make sure we have room for two \0
258 fprintf(stderr
, "The environment file is too large for the target environment storage\n");
266 /* Computes the CRC and put it at the beginning of the data */
267 crc
= crc32(0, envptr
, envsize
);
268 targetendian_crc
= bigendian
? cpu_to_be32(crc
) : cpu_to_le32(crc
);
270 memcpy(dataptr
, &targetendian_crc
, sizeof(targetendian_crc
));
272 dataptr
[sizeof(targetendian_crc
)] = 1;
274 if (!bin_filename
|| strcmp(bin_filename
, "-") == 0) {
275 bin_fd
= STDOUT_FILENO
;
277 bin_fd
= creat(bin_filename
, S_IRUSR
| S_IWUSR
| S_IRGRP
|
280 fprintf(stderr
, "Can't open output file \"%s\": %s\n",
281 bin_filename
, strerror(errno
));
286 if (write(bin_fd
, dataptr
, sizeof(*dataptr
) * datasize
) !=
287 sizeof(*dataptr
) * datasize
) {
288 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));