blob: 55212687e17d0162790002c1c3e5fffbfccb45ca [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
49static void usage(const char *exec_name)
50{
David Wagner8a1b8fc2012-01-13 13:27:34 +000051 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
David Wagnera6337e62011-09-26 03:26:34 +000052 "\n"
David Wagner8a1b8fc2012-01-13 13:27:34 +000053 "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 +000054 "\n"
55 "\tThe input file is in format:\n"
56 "\t\tkey1=value1\n"
57 "\t\tkey2=value2\n"
58 "\t\t...\n"
59 "\t-r : the environment has multiple copies in flash\n"
60 "\t-b : the target is big endian (default is little endian)\n"
David Wagner8a1b8fc2012-01-13 13:27:34 +000061 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
Horst Kronstorfer18954202011-12-05 00:55:26 +000062 "\t-V : print version information and exit\n"
David Wagnera6337e62011-09-26 03:26:34 +000063 "\n"
64 "If the input file is \"-\", data is read from standard input\n",
65 exec_name);
66}
67
David Wagner3d0f9bd2012-01-13 13:27:36 +000068long int xstrtol(const char *s)
69{
70 long int tmp;
71
72 errno = 0;
73 tmp = strtol(s, NULL, 0);
74 if (!errno)
75 return tmp;
76
77 if (errno == ERANGE)
78 fprintf(stderr, "Bad integer format: %s\n", s);
79 else
80 fprintf(stderr, "Error while parsing %s: %s\n", s,
81 strerror(errno));
82
83 exit(EXIT_FAILURE);
84}
85
David Wagnera6337e62011-09-26 03:26:34 +000086int main(int argc, char **argv)
87{
88 uint32_t crc, targetendian_crc;
89 const char *txt_filename = NULL, *bin_filename = NULL;
90 int txt_fd, bin_fd;
91 unsigned char *dataptr, *envptr;
92 unsigned char *filebuf = NULL;
93 unsigned int filesize = 0, envsize = 0, datasize = 0;
94 int bigendian = 0;
95 int redundant = 0;
96 unsigned char padbyte = 0xff;
97
98 int option;
99 int ret = EXIT_SUCCESS;
100
101 struct stat txt_file_stat;
102
103 int fp, ep;
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000104 const char *prg;
105
106 prg = basename(argv[0]);
David Wagnera6337e62011-09-26 03:26:34 +0000107
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000108 /* Turn off getopt()'s internal error message */
109 opterr = 0;
110
David Wagnera6337e62011-09-26 03:26:34 +0000111 /* Parse the cmdline */
Horst Kronstorfer18954202011-12-05 00:55:26 +0000112 while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
David Wagnera6337e62011-09-26 03:26:34 +0000113 switch (option) {
114 case 's':
David Wagner3d0f9bd2012-01-13 13:27:36 +0000115 datasize = xstrtol(optarg);
David Wagnera6337e62011-09-26 03:26:34 +0000116 break;
117 case 'o':
118 bin_filename = strdup(optarg);
119 if (!bin_filename) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000120 fprintf(stderr, "Can't strdup() the output filename\n");
David Wagnera6337e62011-09-26 03:26:34 +0000121 return EXIT_FAILURE;
122 }
123 break;
124 case 'r':
125 redundant = 1;
126 break;
127 case 'b':
128 bigendian = 1;
129 break;
130 case 'p':
David Wagner3d0f9bd2012-01-13 13:27:36 +0000131 padbyte = xstrtol(optarg);
David Wagnera6337e62011-09-26 03:26:34 +0000132 break;
133 case 'h':
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000134 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000135 return EXIT_SUCCESS;
Horst Kronstorfer18954202011-12-05 00:55:26 +0000136 case 'V':
137 printf("%s version %s\n", prg, PLAIN_VERSION);
138 return EXIT_SUCCESS;
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000139 case ':':
140 fprintf(stderr, "Missing argument for option -%c\n",
Horst Kronstorfer72ebafb2011-12-24 00:41:58 +0000141 optopt);
Wolfgang Denke758a5c2012-03-01 21:19:40 +0000142 usage(prg);
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000143 return EXIT_FAILURE;
David Wagnera6337e62011-09-26 03:26:34 +0000144 default:
Horst Kronstorfer72ebafb2011-12-24 00:41:58 +0000145 fprintf(stderr, "Wrong option -%c\n", optopt);
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000146 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000147 return EXIT_FAILURE;
148 }
149 }
150
151 /* Check datasize and allocate the data */
152 if (datasize == 0) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000153 fprintf(stderr, "Please specify the size of the environment partition.\n");
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000154 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000155 return EXIT_FAILURE;
156 }
157
158 dataptr = malloc(datasize * sizeof(*dataptr));
159 if (!dataptr) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000160 fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
161 datasize);
David Wagnera6337e62011-09-26 03:26:34 +0000162 return EXIT_FAILURE;
163 }
164
165 /*
166 * envptr points to the beginning of the actual environment (after the
David Wagner8a1b8fc2012-01-13 13:27:34 +0000167 * crc and possible `redundant' byte
David Wagnera6337e62011-09-26 03:26:34 +0000168 */
169 envsize = datasize - (CRC_SIZE + redundant);
170 envptr = dataptr + CRC_SIZE + redundant;
171
172 /* Pad the environment with the padding byte */
173 memset(envptr, padbyte, envsize);
174
175 /* Open the input file ... */
David Wagner48995b52012-01-13 13:27:37 +0000176 if (optind >= argc || strcmp(argv[optind], "-") == 0) {
David Wagnera6337e62011-09-26 03:26:34 +0000177 int readbytes = 0;
David Wagner48995b52012-01-13 13:27:37 +0000178 int readlen = sizeof(*envptr) * 4096;
David Wagnera6337e62011-09-26 03:26:34 +0000179 txt_fd = STDIN_FILENO;
180
181 do {
182 filebuf = realloc(filebuf, readlen);
David Wagner3d0f9bd2012-01-13 13:27:36 +0000183 if (!filebuf) {
184 fprintf(stderr, "Can't realloc memory for the input file buffer\n");
185 return EXIT_FAILURE;
186 }
David Wagnera6337e62011-09-26 03:26:34 +0000187 readbytes = read(txt_fd, filebuf + filesize, readlen);
David Wagner3d0f9bd2012-01-13 13:27:36 +0000188 if (errno) {
189 fprintf(stderr, "Error while reading stdin: %s\n",
190 strerror(errno));
191 return EXIT_FAILURE;
192 }
David Wagnera6337e62011-09-26 03:26:34 +0000193 filesize += readbytes;
194 } while (readbytes == readlen);
195
196 } else {
David Wagner48995b52012-01-13 13:27:37 +0000197 txt_filename = argv[optind];
David Wagnera6337e62011-09-26 03:26:34 +0000198 txt_fd = open(txt_filename, O_RDONLY);
199 if (txt_fd == -1) {
200 fprintf(stderr, "Can't open \"%s\": %s\n",
201 txt_filename, strerror(errno));
202 return EXIT_FAILURE;
203 }
204 /* ... and check it */
205 ret = fstat(txt_fd, &txt_file_stat);
206 if (ret == -1) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000207 fprintf(stderr, "Can't stat() on \"%s\": %s\n",
208 txt_filename, strerror(errno));
David Wagnera6337e62011-09-26 03:26:34 +0000209 return EXIT_FAILURE;
210 }
211
212 filesize = txt_file_stat.st_size;
David Wagner6ee39f82012-01-13 13:27:38 +0000213
214 filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
215 MAP_PRIVATE, txt_fd, 0);
216 if (filebuf == MAP_FAILED) {
Dirk Behme1ebff632012-04-05 23:15:07 +0000217 fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
David Wagner6ee39f82012-01-13 13:27:38 +0000218 sizeof(*envptr) * filesize,
219 strerror(errno));
220 fprintf(stderr, "Falling back to read()\n");
221
222 filebuf = malloc(sizeof(*envptr) * filesize);
223 ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
224 if (ret != sizeof(*envptr) * filesize) {
Dirk Behme1ebff632012-04-05 23:15:07 +0000225 fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
David Wagner6ee39f82012-01-13 13:27:38 +0000226 sizeof(*envptr) * filesize,
227 strerror(errno));
228
229 return EXIT_FAILURE;
230 }
David Wagnera6337e62011-09-26 03:26:34 +0000231 }
232 ret = close(txt_fd);
233 }
David Wagner8a1b8fc2012-01-13 13:27:34 +0000234 /* The +1 is for the additionnal ending \0. See below. */
235 if (filesize + 1 > envsize) {
236 fprintf(stderr, "The input file is larger than the environment partition size\n");
David Wagnera6337e62011-09-26 03:26:34 +0000237 return EXIT_FAILURE;
238 }
239
240 /* Replace newlines separating variables with \0 */
241 for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
242 if (filebuf[fp] == '\n') {
David Wagnerdbee61d2011-12-20 14:59:29 +0000243 if (ep == 0) {
David Wagnera6337e62011-09-26 03:26:34 +0000244 /*
David Wagnerdbee61d2011-12-20 14:59:29 +0000245 * Newlines at the beginning of the file ?
246 * Ignore them.
David Wagnera6337e62011-09-26 03:26:34 +0000247 */
248 continue;
249 } else if (filebuf[fp-1] == '\\') {
250 /*
251 * Embedded newline in a variable.
252 *
David Wagnerdbee61d2011-12-20 14:59:29 +0000253 * The backslash was added to the envptr; rewind
254 * and replace it with a newline
David Wagnera6337e62011-09-26 03:26:34 +0000255 */
256 ep--;
257 envptr[ep++] = '\n';
258 } else {
259 /* End of a variable */
260 envptr[ep++] = '\0';
261 }
David Wagnera6337e62011-09-26 03:26:34 +0000262 } else {
263 envptr[ep++] = filebuf[fp];
264 }
265 }
266 /*
267 * Make sure there is a final '\0'
268 * And do it again on the next byte to mark the end of the environment.
269 */
270 if (envptr[ep-1] != '\0') {
271 envptr[ep++] = '\0';
272 /*
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
275 */
276 if (ep >= envsize) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000277 fprintf(stderr, "The environment file is too large for the target environment storage\n");
David Wagnera6337e62011-09-26 03:26:34 +0000278 return EXIT_FAILURE;
279 }
280 envptr[ep] = '\0';
281 } else {
282 envptr[ep] = '\0';
283 }
284
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);
288
David Wagnerd8d26592012-01-13 13:27:40 +0000289 memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
290 if (redundant)
291 dataptr[sizeof(targetendian_crc)] = 1;
David Wagnera6337e62011-09-26 03:26:34 +0000292
David Wagner48995b52012-01-13 13:27:37 +0000293 if (!bin_filename || strcmp(bin_filename, "-") == 0) {
294 bin_fd = STDOUT_FILENO;
295 } else {
Mike Frysinger4f7136e2012-07-18 16:59:45 +0000296 bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
297 S_IWGRP);
David Wagner48995b52012-01-13 13:27:37 +0000298 if (bin_fd == -1) {
299 fprintf(stderr, "Can't open output file \"%s\": %s\n",
300 bin_filename, strerror(errno));
301 return EXIT_FAILURE;
302 }
David Wagnera6337e62011-09-26 03:26:34 +0000303 }
304
305 if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
306 sizeof(*dataptr) * datasize) {
307 fprintf(stderr, "write() failed: %s\n", strerror(errno));
308 return EXIT_FAILURE;
309 }
310
311 ret = close(bin_fd);
312
313 return ret;
314}