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
19 #include <sys/types.h>
24 #include <u-boot/crc.h>
27 #define CRC_SIZE sizeof(uint32_t)
29 static void usage(const char *exec_name)
31 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
33 "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"
35 "\tThe input file is in format:\n"
39 "\tEmpty lines are skipped, and lines with a # in the first\n"
40 "\tcolumn are treated as comments (also skipped).\n"
41 "\t-r : the environment has multiple copies in flash\n"
42 "\t-b : the target is big endian (default is little endian)\n"
43 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
44 "\t-V : print version information and exit\n"
46 "If the input file is \"-\", data is read from standard input\n",
50 long int xstrtol(const char *s)
55 tmp = strtol(s, NULL, 0);
60 fprintf(stderr, "Bad integer format: %s\n", s);
62 fprintf(stderr, "Error while parsing %s: %s\n", s,
68 int main(int argc, char **argv)
70 uint32_t crc, targetendian_crc;
71 const char *txt_filename = NULL, *bin_filename = NULL;
73 unsigned char *dataptr, *envptr;
74 unsigned char *filebuf = NULL;
75 unsigned int filesize = 0, envsize = 0, datasize = 0;
78 unsigned char padbyte = 0xff;
81 int ret = EXIT_SUCCESS;
83 struct stat txt_file_stat;
88 prg = basename(argv[0]);
90 /* Turn off getopt()'s internal error message */
93 /* Parse the cmdline */
94 while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
97 datasize = xstrtol(optarg);
100 bin_filename = strdup(optarg);
102 fprintf(stderr, "Can't strdup() the output filename\n");
113 padbyte = xstrtol(optarg);
119 printf("%s version %s\n", prg, PLAIN_VERSION);
122 fprintf(stderr, "Missing argument for option -%c\n",
127 fprintf(stderr, "Wrong option -%c\n", optopt);
133 /* Check datasize and allocate the data */
135 fprintf(stderr, "Please specify the size of the environment partition.\n");
140 dataptr = malloc(datasize * sizeof(*dataptr));
142 fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
148 * envptr points to the beginning of the actual environment (after the
149 * crc and possible `redundant' byte
151 envsize = datasize - (CRC_SIZE + redundant);
152 envptr = dataptr + CRC_SIZE + redundant;
154 /* Pad the environment with the padding byte */
155 memset(envptr, padbyte, envsize);
157 /* Open the input file ... */
158 if (optind >= argc || strcmp(argv[optind], "-") == 0) {
160 int readlen = sizeof(*envptr) * 4096;
161 txt_fd = STDIN_FILENO;
164 filebuf = realloc(filebuf, filesize + readlen);
166 fprintf(stderr, "Can't realloc memory for the input file buffer\n");
169 readbytes = read(txt_fd, filebuf + filesize, readlen);
171 fprintf(stderr, "Error while reading stdin: %s\n",
175 filesize += readbytes;
176 } while (readbytes == readlen);
179 txt_filename = argv[optind];
180 txt_fd = open(txt_filename, O_RDONLY);
182 fprintf(stderr, "Can't open \"%s\": %s\n",
183 txt_filename, strerror(errno));
186 /* ... and check it */
187 ret = fstat(txt_fd, &txt_file_stat);
189 fprintf(stderr, "Can't stat() on \"%s\": %s\n",
190 txt_filename, strerror(errno));
194 filesize = txt_file_stat.st_size;
196 filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
197 MAP_PRIVATE, txt_fd, 0);
198 if (filebuf == MAP_FAILED) {
199 fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
200 sizeof(*envptr) * filesize,
202 fprintf(stderr, "Falling back to read()\n");
204 filebuf = malloc(sizeof(*envptr) * filesize);
205 ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
206 if (ret != sizeof(*envptr) * filesize) {
207 fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
208 sizeof(*envptr) * filesize,
217 /* Parse a byte at time until reaching the file OR until the environment fills
218 * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */
219 for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) {
220 if (filebuf[fp] == '\n') {
221 if (fp == 0 || filebuf[fp-1] == '\n') {
226 } else if (filebuf[fp-1] == '\\') {
228 * Embedded newline in a variable.
230 * The backslash was added to the envptr; rewind
231 * and replace it with a newline
236 /* End of a variable */
239 } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
240 /* Comment, skip the line. */
241 while (++fp < filesize && filebuf[fp] != '\n')
244 envptr[ep++] = filebuf[fp];
247 /* If there are more bytes in the file still, it means the env filled up
248 * before parsing the whole file. Eat comments & whitespace here to see if
249 * there was anything meaning full left in the file, and if so, throw a error
251 for( ; fp < filesize; fp++ )
253 if (filebuf[fp] == '\n') {
254 if (fp == 0 || filebuf[fp-1] == '\n') {
255 /* Ignore blank lines */
258 } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
259 while (++fp < filesize && filebuf[fp] != '\n')
262 fprintf(stderr, "The environment file is too large for the target environment storage\n");
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));