blob: 4001d2f517d78040653a2c51c5194e70552fc123 [file] [log] [blame]
David Wagnera6337e62011-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 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +000028/* We want the GNU version of basename() */
29#define _GNU_SOURCE
30
David Wagnera6337e62011-09-26 03:26:34 +000031#include <errno.h>
32#include <fcntl.h>
33#include <stdio.h>
David Wagnerd1acdae2012-01-13 13:27:35 +000034#include <stdlib.h>
David Wagnera6337e62011-09-26 03:26:34 +000035#include <stdint.h>
36#include <string.h>
37#include <unistd.h>
Andreas Bießmann558cd992012-06-28 08:01:58 +020038#include <libgen.h>
David Wagnera6337e62011-09-26 03:26:34 +000039#include <sys/types.h>
40#include <sys/stat.h>
David Wagner6ee39f82012-01-13 13:27:38 +000041#include <sys/mman.h>
David Wagnera6337e62011-09-26 03:26:34 +000042
David Wagnerd1acdae2012-01-13 13:27:35 +000043#include "compiler.h"
David Wagnera6337e62011-09-26 03:26:34 +000044#include <u-boot/crc.h>
Horst Kronstorfer18954202011-12-05 00:55:26 +000045#include <version.h>
David Wagnera6337e62011-09-26 03:26:34 +000046
47#define CRC_SIZE sizeof(uint32_t)
48
Vladimir Yakovlev8b6a4952012-07-07 10:05:06 +000049#ifdef __MINGW32__
50#define FILE_PERM (S_IRUSR | S_IWUSR)
51#else
52#define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP |\
53 S_IWGRP)
54#endif
55
David Wagnera6337e62011-09-26 03:26:34 +000056static void usage(const char *exec_name)
57{
David Wagner8a1b8fc2012-01-13 13:27:34 +000058 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
David Wagnera6337e62011-09-26 03:26:34 +000059 "\n"
David Wagner8a1b8fc2012-01-13 13:27:34 +000060 "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 Wagnera6337e62011-09-26 03:26:34 +000061 "\n"
62 "\tThe input file is in format:\n"
63 "\t\tkey1=value1\n"
64 "\t\tkey2=value2\n"
65 "\t\t...\n"
66 "\t-r : the environment has multiple copies in flash\n"
67 "\t-b : the target is big endian (default is little endian)\n"
David Wagner8a1b8fc2012-01-13 13:27:34 +000068 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
Horst Kronstorfer18954202011-12-05 00:55:26 +000069 "\t-V : print version information and exit\n"
David Wagnera6337e62011-09-26 03:26:34 +000070 "\n"
71 "If the input file is \"-\", data is read from standard input\n",
72 exec_name);
73}
74
David Wagner3d0f9bd2012-01-13 13:27:36 +000075long int xstrtol(const char *s)
76{
77 long int tmp;
78
79 errno = 0;
80 tmp = strtol(s, NULL, 0);
81 if (!errno)
82 return tmp;
83
84 if (errno == ERANGE)
85 fprintf(stderr, "Bad integer format: %s\n", s);
86 else
87 fprintf(stderr, "Error while parsing %s: %s\n", s,
88 strerror(errno));
89
90 exit(EXIT_FAILURE);
91}
92
David Wagnera6337e62011-09-26 03:26:34 +000093int main(int argc, char **argv)
94{
95 uint32_t crc, targetendian_crc;
96 const char *txt_filename = NULL, *bin_filename = NULL;
97 int txt_fd, bin_fd;
98 unsigned char *dataptr, *envptr;
99 unsigned char *filebuf = NULL;
100 unsigned int filesize = 0, envsize = 0, datasize = 0;
101 int bigendian = 0;
102 int redundant = 0;
103 unsigned char padbyte = 0xff;
104
105 int option;
106 int ret = EXIT_SUCCESS;
107
108 struct stat txt_file_stat;
109
110 int fp, ep;
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000111 const char *prg;
112
113 prg = basename(argv[0]);
David Wagnera6337e62011-09-26 03:26:34 +0000114
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000115 /* Turn off getopt()'s internal error message */
116 opterr = 0;
117
David Wagnera6337e62011-09-26 03:26:34 +0000118 /* Parse the cmdline */
Horst Kronstorfer18954202011-12-05 00:55:26 +0000119 while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
David Wagnera6337e62011-09-26 03:26:34 +0000120 switch (option) {
121 case 's':
David Wagner3d0f9bd2012-01-13 13:27:36 +0000122 datasize = xstrtol(optarg);
David Wagnera6337e62011-09-26 03:26:34 +0000123 break;
124 case 'o':
125 bin_filename = strdup(optarg);
126 if (!bin_filename) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000127 fprintf(stderr, "Can't strdup() the output filename\n");
David Wagnera6337e62011-09-26 03:26:34 +0000128 return EXIT_FAILURE;
129 }
130 break;
131 case 'r':
132 redundant = 1;
133 break;
134 case 'b':
135 bigendian = 1;
136 break;
137 case 'p':
David Wagner3d0f9bd2012-01-13 13:27:36 +0000138 padbyte = xstrtol(optarg);
David Wagnera6337e62011-09-26 03:26:34 +0000139 break;
140 case 'h':
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000141 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000142 return EXIT_SUCCESS;
Horst Kronstorfer18954202011-12-05 00:55:26 +0000143 case 'V':
144 printf("%s version %s\n", prg, PLAIN_VERSION);
145 return EXIT_SUCCESS;
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000146 case ':':
147 fprintf(stderr, "Missing argument for option -%c\n",
Horst Kronstorfer72ebafb2011-12-24 00:41:58 +0000148 optopt);
Wolfgang Denke758a5c2012-03-01 21:19:40 +0000149 usage(prg);
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000150 return EXIT_FAILURE;
David Wagnera6337e62011-09-26 03:26:34 +0000151 default:
Horst Kronstorfer72ebafb2011-12-24 00:41:58 +0000152 fprintf(stderr, "Wrong option -%c\n", optopt);
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000153 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000154 return EXIT_FAILURE;
155 }
156 }
157
158 /* Check datasize and allocate the data */
159 if (datasize == 0) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000160 fprintf(stderr, "Please specify the size of the environment partition.\n");
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000161 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000162 return EXIT_FAILURE;
163 }
164
165 dataptr = malloc(datasize * sizeof(*dataptr));
166 if (!dataptr) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000167 fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
168 datasize);
David Wagnera6337e62011-09-26 03:26:34 +0000169 return EXIT_FAILURE;
170 }
171
172 /*
173 * envptr points to the beginning of the actual environment (after the
David Wagner8a1b8fc2012-01-13 13:27:34 +0000174 * crc and possible `redundant' byte
David Wagnera6337e62011-09-26 03:26:34 +0000175 */
176 envsize = datasize - (CRC_SIZE + redundant);
177 envptr = dataptr + CRC_SIZE + redundant;
178
179 /* Pad the environment with the padding byte */
180 memset(envptr, padbyte, envsize);
181
182 /* Open the input file ... */
David Wagner48995b52012-01-13 13:27:37 +0000183 if (optind >= argc || strcmp(argv[optind], "-") == 0) {
David Wagnera6337e62011-09-26 03:26:34 +0000184 int readbytes = 0;
David Wagner48995b52012-01-13 13:27:37 +0000185 int readlen = sizeof(*envptr) * 4096;
David Wagnera6337e62011-09-26 03:26:34 +0000186 txt_fd = STDIN_FILENO;
187
188 do {
189 filebuf = realloc(filebuf, readlen);
David Wagner3d0f9bd2012-01-13 13:27:36 +0000190 if (!filebuf) {
191 fprintf(stderr, "Can't realloc memory for the input file buffer\n");
192 return EXIT_FAILURE;
193 }
David Wagnera6337e62011-09-26 03:26:34 +0000194 readbytes = read(txt_fd, filebuf + filesize, readlen);
David Wagner3d0f9bd2012-01-13 13:27:36 +0000195 if (errno) {
196 fprintf(stderr, "Error while reading stdin: %s\n",
197 strerror(errno));
198 return EXIT_FAILURE;
199 }
David Wagnera6337e62011-09-26 03:26:34 +0000200 filesize += readbytes;
201 } while (readbytes == readlen);
202
203 } else {
David Wagner48995b52012-01-13 13:27:37 +0000204 txt_filename = argv[optind];
David Wagnera6337e62011-09-26 03:26:34 +0000205 txt_fd = open(txt_filename, O_RDONLY);
206 if (txt_fd == -1) {
207 fprintf(stderr, "Can't open \"%s\": %s\n",
208 txt_filename, strerror(errno));
209 return EXIT_FAILURE;
210 }
211 /* ... and check it */
212 ret = fstat(txt_fd, &txt_file_stat);
213 if (ret == -1) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000214 fprintf(stderr, "Can't stat() on \"%s\": %s\n",
215 txt_filename, strerror(errno));
David Wagnera6337e62011-09-26 03:26:34 +0000216 return EXIT_FAILURE;
217 }
218
219 filesize = txt_file_stat.st_size;
David Wagner6ee39f82012-01-13 13:27:38 +0000220
221 filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
222 MAP_PRIVATE, txt_fd, 0);
223 if (filebuf == MAP_FAILED) {
Dirk Behme1ebff632012-04-05 23:15:07 +0000224 fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
David Wagner6ee39f82012-01-13 13:27:38 +0000225 sizeof(*envptr) * filesize,
226 strerror(errno));
227 fprintf(stderr, "Falling back to read()\n");
228
229 filebuf = malloc(sizeof(*envptr) * filesize);
230 ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
231 if (ret != sizeof(*envptr) * filesize) {
Dirk Behme1ebff632012-04-05 23:15:07 +0000232 fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
David Wagner6ee39f82012-01-13 13:27:38 +0000233 sizeof(*envptr) * filesize,
234 strerror(errno));
235
236 return EXIT_FAILURE;
237 }
David Wagnera6337e62011-09-26 03:26:34 +0000238 }
239 ret = close(txt_fd);
240 }
David Wagner8a1b8fc2012-01-13 13:27:34 +0000241 /* The +1 is for the additionnal ending \0. See below. */
242 if (filesize + 1 > envsize) {
243 fprintf(stderr, "The input file is larger than the environment partition size\n");
David Wagnera6337e62011-09-26 03:26:34 +0000244 return EXIT_FAILURE;
245 }
246
247 /* Replace newlines separating variables with \0 */
248 for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
249 if (filebuf[fp] == '\n') {
David Wagnerdbee61d2011-12-20 14:59:29 +0000250 if (ep == 0) {
David Wagnera6337e62011-09-26 03:26:34 +0000251 /*
David Wagnerdbee61d2011-12-20 14:59:29 +0000252 * Newlines at the beginning of the file ?
253 * Ignore them.
David Wagnera6337e62011-09-26 03:26:34 +0000254 */
255 continue;
256 } else if (filebuf[fp-1] == '\\') {
257 /*
258 * Embedded newline in a variable.
259 *
David Wagnerdbee61d2011-12-20 14:59:29 +0000260 * The backslash was added to the envptr; rewind
261 * and replace it with a newline
David Wagnera6337e62011-09-26 03:26:34 +0000262 */
263 ep--;
264 envptr[ep++] = '\n';
265 } else {
266 /* End of a variable */
267 envptr[ep++] = '\0';
268 }
David Wagnera6337e62011-09-26 03:26:34 +0000269 } else {
270 envptr[ep++] = filebuf[fp];
271 }
272 }
273 /*
274 * Make sure there is a final '\0'
275 * And do it again on the next byte to mark the end of the environment.
276 */
277 if (envptr[ep-1] != '\0') {
278 envptr[ep++] = '\0';
279 /*
280 * The text file doesn't have an ending newline. We need to
281 * check the env size again to make sure we have room for two \0
282 */
283 if (ep >= envsize) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000284 fprintf(stderr, "The environment file is too large for the target environment storage\n");
David Wagnera6337e62011-09-26 03:26:34 +0000285 return EXIT_FAILURE;
286 }
287 envptr[ep] = '\0';
288 } else {
289 envptr[ep] = '\0';
290 }
291
292 /* Computes the CRC and put it at the beginning of the data */
293 crc = crc32(0, envptr, envsize);
294 targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
295
David Wagnerd8d26592012-01-13 13:27:40 +0000296 memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
297 if (redundant)
298 dataptr[sizeof(targetendian_crc)] = 1;
David Wagnera6337e62011-09-26 03:26:34 +0000299
David Wagner48995b52012-01-13 13:27:37 +0000300 if (!bin_filename || strcmp(bin_filename, "-") == 0) {
301 bin_fd = STDOUT_FILENO;
302 } else {
Vladimir Yakovlev8b6a4952012-07-07 10:05:06 +0000303 bin_fd = creat(bin_filename, FILE_PERM);
David Wagner48995b52012-01-13 13:27:37 +0000304 if (bin_fd == -1) {
305 fprintf(stderr, "Can't open output file \"%s\": %s\n",
306 bin_filename, strerror(errno));
307 return EXIT_FAILURE;
308 }
David Wagnera6337e62011-09-26 03:26:34 +0000309 }
310
311 if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
312 sizeof(*dataptr) * datasize) {
313 fprintf(stderr, "write() failed: %s\n", strerror(errno));
314 return EXIT_FAILURE;
315 }
316
317 ret = close(bin_fd);
318
319 return ret;
320}