blob: bbd3041e36f4e5a2ac909fbe101ecfdd91b6345b [file] [log] [blame]
David Wagner65a7f2d2011-09-26 03:26:34 +00001/*
2 * (C) Copyright 2011 Free Electrons
3 * David Wagner <david.wagner@free-electrons.com>
4 *
5 * Inspired from envcrc.c:
6 * (C) Copyright 2001
7 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
David Wagner65a7f2d2011-09-26 03:26:34 +000010 */
11
12#include <errno.h>
13#include <fcntl.h>
14#include <stdio.h>
David Wagner3f923cb2012-01-13 13:27:35 +000015#include <stdlib.h>
David Wagner65a7f2d2011-09-26 03:26:34 +000016#include <stdint.h>
17#include <string.h>
18#include <unistd.h>
Andreas Bießmann03ddd4d2012-06-28 08:01:58 +020019#include <libgen.h>
David Wagner65a7f2d2011-09-26 03:26:34 +000020#include <sys/types.h>
21#include <sys/stat.h>
David Wagner957ec692012-01-13 13:27:38 +000022#include <sys/mman.h>
David Wagner65a7f2d2011-09-26 03:26:34 +000023
David Wagner3f923cb2012-01-13 13:27:35 +000024#include "compiler.h"
David Wagner65a7f2d2011-09-26 03:26:34 +000025#include <u-boot/crc.h>
Horst Kronstorferfa8df722011-12-05 00:55:26 +000026#include <version.h>
David Wagner65a7f2d2011-09-26 03:26:34 +000027
28#define CRC_SIZE sizeof(uint32_t)
29
30static void usage(const char *exec_name)
31{
David Wagner7ddfb602012-01-13 13:27:34 +000032 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
David Wagner65a7f2d2011-09-26 03:26:34 +000033 "\n"
David Wagner7ddfb602012-01-13 13:27:34 +000034 "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"
David Wagner65a7f2d2011-09-26 03:26:34 +000035 "\n"
36 "\tThe input file is in format:\n"
37 "\t\tkey1=value1\n"
38 "\t\tkey2=value2\n"
39 "\t\t...\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"
David Wagner7ddfb602012-01-13 13:27:34 +000042 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
Horst Kronstorferfa8df722011-12-05 00:55:26 +000043 "\t-V : print version information and exit\n"
David Wagner65a7f2d2011-09-26 03:26:34 +000044 "\n"
45 "If the input file is \"-\", data is read from standard input\n",
46 exec_name);
47}
48
David Wagner111d9502012-01-13 13:27:36 +000049long int xstrtol(const char *s)
50{
51 long int tmp;
52
53 errno = 0;
54 tmp = strtol(s, NULL, 0);
55 if (!errno)
56 return tmp;
57
58 if (errno == ERANGE)
59 fprintf(stderr, "Bad integer format: %s\n", s);
60 else
61 fprintf(stderr, "Error while parsing %s: %s\n", s,
62 strerror(errno));
63
64 exit(EXIT_FAILURE);
65}
66
David Wagner65a7f2d2011-09-26 03:26:34 +000067int main(int argc, char **argv)
68{
69 uint32_t crc, targetendian_crc;
70 const char *txt_filename = NULL, *bin_filename = NULL;
71 int txt_fd, bin_fd;
72 unsigned char *dataptr, *envptr;
73 unsigned char *filebuf = NULL;
74 unsigned int filesize = 0, envsize = 0, datasize = 0;
75 int bigendian = 0;
76 int redundant = 0;
77 unsigned char padbyte = 0xff;
78
79 int option;
80 int ret = EXIT_SUCCESS;
81
82 struct stat txt_file_stat;
83
84 int fp, ep;
Horst Kronstorfera0c82902011-12-21 10:39:39 +000085 const char *prg;
86
87 prg = basename(argv[0]);
David Wagner65a7f2d2011-09-26 03:26:34 +000088
Horst Kronstorferab6d2492011-12-05 00:55:24 +000089 /* Turn off getopt()'s internal error message */
90 opterr = 0;
91
David Wagner65a7f2d2011-09-26 03:26:34 +000092 /* Parse the cmdline */
Horst Kronstorferfa8df722011-12-05 00:55:26 +000093 while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
David Wagner65a7f2d2011-09-26 03:26:34 +000094 switch (option) {
95 case 's':
David Wagner111d9502012-01-13 13:27:36 +000096 datasize = xstrtol(optarg);
David Wagner65a7f2d2011-09-26 03:26:34 +000097 break;
98 case 'o':
99 bin_filename = strdup(optarg);
100 if (!bin_filename) {
David Wagner7ddfb602012-01-13 13:27:34 +0000101 fprintf(stderr, "Can't strdup() the output filename\n");
David Wagner65a7f2d2011-09-26 03:26:34 +0000102 return EXIT_FAILURE;
103 }
104 break;
105 case 'r':
106 redundant = 1;
107 break;
108 case 'b':
109 bigendian = 1;
110 break;
111 case 'p':
David Wagner111d9502012-01-13 13:27:36 +0000112 padbyte = xstrtol(optarg);
David Wagner65a7f2d2011-09-26 03:26:34 +0000113 break;
114 case 'h':
Horst Kronstorfera0c82902011-12-21 10:39:39 +0000115 usage(prg);
David Wagner65a7f2d2011-09-26 03:26:34 +0000116 return EXIT_SUCCESS;
Horst Kronstorferfa8df722011-12-05 00:55:26 +0000117 case 'V':
118 printf("%s version %s\n", prg, PLAIN_VERSION);
119 return EXIT_SUCCESS;
Horst Kronstorferab6d2492011-12-05 00:55:24 +0000120 case ':':
121 fprintf(stderr, "Missing argument for option -%c\n",
Horst Kronstorfer405bfbc2011-12-24 00:41:58 +0000122 optopt);
Wolfgang Denka23b7de2012-03-01 21:19:40 +0000123 usage(prg);
Horst Kronstorferab6d2492011-12-05 00:55:24 +0000124 return EXIT_FAILURE;
David Wagner65a7f2d2011-09-26 03:26:34 +0000125 default:
Horst Kronstorfer405bfbc2011-12-24 00:41:58 +0000126 fprintf(stderr, "Wrong option -%c\n", optopt);
Horst Kronstorfera0c82902011-12-21 10:39:39 +0000127 usage(prg);
David Wagner65a7f2d2011-09-26 03:26:34 +0000128 return EXIT_FAILURE;
129 }
130 }
131
132 /* Check datasize and allocate the data */
133 if (datasize == 0) {
David Wagner7ddfb602012-01-13 13:27:34 +0000134 fprintf(stderr, "Please specify the size of the environment partition.\n");
Horst Kronstorfera0c82902011-12-21 10:39:39 +0000135 usage(prg);
David Wagner65a7f2d2011-09-26 03:26:34 +0000136 return EXIT_FAILURE;
137 }
138
139 dataptr = malloc(datasize * sizeof(*dataptr));
140 if (!dataptr) {
David Wagner7ddfb602012-01-13 13:27:34 +0000141 fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
142 datasize);
David Wagner65a7f2d2011-09-26 03:26:34 +0000143 return EXIT_FAILURE;
144 }
145
146 /*
147 * envptr points to the beginning of the actual environment (after the
David Wagner7ddfb602012-01-13 13:27:34 +0000148 * crc and possible `redundant' byte
David Wagner65a7f2d2011-09-26 03:26:34 +0000149 */
150 envsize = datasize - (CRC_SIZE + redundant);
151 envptr = dataptr + CRC_SIZE + redundant;
152
153 /* Pad the environment with the padding byte */
154 memset(envptr, padbyte, envsize);
155
156 /* Open the input file ... */
David Wagnerbc3177a2012-01-13 13:27:37 +0000157 if (optind >= argc || strcmp(argv[optind], "-") == 0) {
David Wagner65a7f2d2011-09-26 03:26:34 +0000158 int readbytes = 0;
David Wagnerbc3177a2012-01-13 13:27:37 +0000159 int readlen = sizeof(*envptr) * 4096;
David Wagner65a7f2d2011-09-26 03:26:34 +0000160 txt_fd = STDIN_FILENO;
161
162 do {
163 filebuf = realloc(filebuf, readlen);
David Wagner111d9502012-01-13 13:27:36 +0000164 if (!filebuf) {
165 fprintf(stderr, "Can't realloc memory for the input file buffer\n");
166 return EXIT_FAILURE;
167 }
David Wagner65a7f2d2011-09-26 03:26:34 +0000168 readbytes = read(txt_fd, filebuf + filesize, readlen);
David Wagner111d9502012-01-13 13:27:36 +0000169 if (errno) {
170 fprintf(stderr, "Error while reading stdin: %s\n",
171 strerror(errno));
172 return EXIT_FAILURE;
173 }
David Wagner65a7f2d2011-09-26 03:26:34 +0000174 filesize += readbytes;
175 } while (readbytes == readlen);
176
177 } else {
David Wagnerbc3177a2012-01-13 13:27:37 +0000178 txt_filename = argv[optind];
David Wagner65a7f2d2011-09-26 03:26:34 +0000179 txt_fd = open(txt_filename, O_RDONLY);
180 if (txt_fd == -1) {
181 fprintf(stderr, "Can't open \"%s\": %s\n",
182 txt_filename, strerror(errno));
183 return EXIT_FAILURE;
184 }
185 /* ... and check it */
186 ret = fstat(txt_fd, &txt_file_stat);
187 if (ret == -1) {
David Wagner7ddfb602012-01-13 13:27:34 +0000188 fprintf(stderr, "Can't stat() on \"%s\": %s\n",
189 txt_filename, strerror(errno));
David Wagner65a7f2d2011-09-26 03:26:34 +0000190 return EXIT_FAILURE;
191 }
192
193 filesize = txt_file_stat.st_size;
David Wagner957ec692012-01-13 13:27:38 +0000194
195 filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
196 MAP_PRIVATE, txt_fd, 0);
197 if (filebuf == MAP_FAILED) {
Dirk Behmea5de6692012-04-05 23:15:07 +0000198 fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
David Wagner957ec692012-01-13 13:27:38 +0000199 sizeof(*envptr) * filesize,
200 strerror(errno));
201 fprintf(stderr, "Falling back to read()\n");
202
203 filebuf = malloc(sizeof(*envptr) * filesize);
204 ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
205 if (ret != sizeof(*envptr) * filesize) {
Dirk Behmea5de6692012-04-05 23:15:07 +0000206 fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
David Wagner957ec692012-01-13 13:27:38 +0000207 sizeof(*envptr) * filesize,
208 strerror(errno));
209
210 return EXIT_FAILURE;
211 }
David Wagner65a7f2d2011-09-26 03:26:34 +0000212 }
213 ret = close(txt_fd);
214 }
David Wagner7ddfb602012-01-13 13:27:34 +0000215 /* 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");
David Wagner65a7f2d2011-09-26 03:26:34 +0000218 return EXIT_FAILURE;
219 }
220
221 /* Replace newlines separating variables with \0 */
222 for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
223 if (filebuf[fp] == '\n') {
David Wagnerdafad3c2011-12-20 14:59:29 +0000224 if (ep == 0) {
David Wagner65a7f2d2011-09-26 03:26:34 +0000225 /*
David Wagnerdafad3c2011-12-20 14:59:29 +0000226 * Newlines at the beginning of the file ?
227 * Ignore them.
David Wagner65a7f2d2011-09-26 03:26:34 +0000228 */
229 continue;
230 } else if (filebuf[fp-1] == '\\') {
231 /*
232 * Embedded newline in a variable.
233 *
David Wagnerdafad3c2011-12-20 14:59:29 +0000234 * The backslash was added to the envptr; rewind
235 * and replace it with a newline
David Wagner65a7f2d2011-09-26 03:26:34 +0000236 */
237 ep--;
238 envptr[ep++] = '\n';
239 } else {
240 /* End of a variable */
241 envptr[ep++] = '\0';
242 }
David Wagner65a7f2d2011-09-26 03:26:34 +0000243 } else {
244 envptr[ep++] = filebuf[fp];
245 }
246 }
247 /*
248 * Make sure there is a final '\0'
249 * And do it again on the next byte to mark the end of the environment.
250 */
251 if (envptr[ep-1] != '\0') {
252 envptr[ep++] = '\0';
253 /*
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
256 */
257 if (ep >= envsize) {
David Wagner7ddfb602012-01-13 13:27:34 +0000258 fprintf(stderr, "The environment file is too large for the target environment storage\n");
David Wagner65a7f2d2011-09-26 03:26:34 +0000259 return EXIT_FAILURE;
260 }
261 envptr[ep] = '\0';
262 } else {
263 envptr[ep] = '\0';
264 }
265
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);
269
David Wagner99cae312012-01-13 13:27:40 +0000270 memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
271 if (redundant)
272 dataptr[sizeof(targetendian_crc)] = 1;
David Wagner65a7f2d2011-09-26 03:26:34 +0000273
David Wagnerbc3177a2012-01-13 13:27:37 +0000274 if (!bin_filename || strcmp(bin_filename, "-") == 0) {
275 bin_fd = STDOUT_FILENO;
276 } else {
Mike Frysinger92e144f2012-07-18 16:59:45 +0000277 bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
278 S_IWGRP);
David Wagnerbc3177a2012-01-13 13:27:37 +0000279 if (bin_fd == -1) {
280 fprintf(stderr, "Can't open output file \"%s\": %s\n",
281 bin_filename, strerror(errno));
282 return EXIT_FAILURE;
283 }
David Wagner65a7f2d2011-09-26 03:26:34 +0000284 }
285
286 if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
287 sizeof(*dataptr) * datasize) {
288 fprintf(stderr, "write() failed: %s\n", strerror(errno));
289 return EXIT_FAILURE;
290 }
291
292 ret = close(bin_fd);
293
294 return ret;
295}