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+
12 /* We want the GNU version of basename() */
23 #include <sys/types.h>
28 #include <u-boot/crc.h>
31 #define CRC_SIZE sizeof(uint32_t)
33 static void usage(const char *exec_name)
35 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
37 "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"
39 "\tThe input file is in format:\n"
43 "\t-r : the environment has multiple copies in flash\n"
44 "\t-b : the target is big endian (default is little endian)\n"
45 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
46 "\t-V : print version information and exit\n"
48 "If the input file is \"-\", data is read from standard input\n",
52 long int xstrtol(const char *s)
57 tmp = strtol(s, NULL, 0);
62 fprintf(stderr, "Bad integer format: %s\n", s);
64 fprintf(stderr, "Error while parsing %s: %s\n", s,
70 int main(int argc, char **argv)
72 uint32_t crc, targetendian_crc;
73 const char *txt_filename = NULL, *bin_filename = NULL;
75 unsigned char *dataptr, *envptr;
76 unsigned char *filebuf = NULL;
77 unsigned int filesize = 0, envsize = 0, datasize = 0;
80 unsigned char padbyte = 0xff;
83 int ret = EXIT_SUCCESS;
85 struct stat txt_file_stat;
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) {
162 int readlen = sizeof(*envptr) * 4096;
163 txt_fd = STDIN_FILENO;
166 filebuf = realloc(filebuf, readlen);
168 fprintf(stderr, "Can't realloc memory for the input file buffer\n");
171 readbytes = read(txt_fd, filebuf + filesize, readlen);
173 fprintf(stderr, "Error while reading stdin: %s\n",
177 filesize += readbytes;
178 } while (readbytes == readlen);
181 txt_filename = argv[optind];
182 txt_fd = open(txt_filename, O_RDONLY);
184 fprintf(stderr, "Can't open \"%s\": %s\n",
185 txt_filename, strerror(errno));
188 /* ... and check it */
189 ret = fstat(txt_fd, &txt_file_stat);
191 fprintf(stderr, "Can't stat() on \"%s\": %s\n",
192 txt_filename, strerror(errno));
196 filesize = txt_file_stat.st_size;
198 filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
199 MAP_PRIVATE, txt_fd, 0);
200 if (filebuf == MAP_FAILED) {
201 fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
202 sizeof(*envptr) * filesize,
204 fprintf(stderr, "Falling back to read()\n");
206 filebuf = malloc(sizeof(*envptr) * filesize);
207 ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
208 if (ret != sizeof(*envptr) * filesize) {
209 fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
210 sizeof(*envptr) * filesize,
218 /* The +1 is for the additionnal ending \0. See below. */
219 if (filesize + 1 > envsize) {
220 fprintf(stderr, "The input file is larger than the environment partition size\n");
224 /* Replace newlines separating variables with \0 */
225 for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
226 if (filebuf[fp] == '\n') {
229 * Newlines at the beginning of the file ?
233 } else if (filebuf[fp-1] == '\\') {
235 * Embedded newline in a variable.
237 * The backslash was added to the envptr; rewind
238 * and replace it with a newline
243 /* End of a variable */
247 envptr[ep++] = filebuf[fp];
251 * Make sure there is a final '\0'
252 * And do it again on the next byte to mark the end of the environment.
254 if (envptr[ep-1] != '\0') {
257 * The text file doesn't have an ending newline. We need to
258 * check the env size again to make sure we have room for two \0
261 fprintf(stderr, "The environment file is too large for the target environment storage\n");
269 /* Computes the CRC and put it at the beginning of the data */
270 crc = crc32(0, envptr, envsize);
271 targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
273 memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
275 dataptr[sizeof(targetendian_crc)] = 1;
277 if (!bin_filename || strcmp(bin_filename, "-") == 0) {
278 bin_fd = STDOUT_FILENO;
280 bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
283 fprintf(stderr, "Can't open output file \"%s\": %s\n",
284 bin_filename, strerror(errno));
289 if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
290 sizeof(*dataptr) * datasize) {
291 fprintf(stderr, "write() failed: %s\n", strerror(errno));