blob: c1915795ea2167236617bce586ed31f159024f55 [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>
David Wagnera6337e62011-09-26 03:26:34 +000038#include <sys/types.h>
39#include <sys/stat.h>
40
David Wagnerd1acdae2012-01-13 13:27:35 +000041#include "compiler.h"
David Wagnera6337e62011-09-26 03:26:34 +000042#include <u-boot/crc.h>
Horst Kronstorfer18954202011-12-05 00:55:26 +000043#include <version.h>
David Wagnera6337e62011-09-26 03:26:34 +000044
45#define CRC_SIZE sizeof(uint32_t)
46
47static void usage(const char *exec_name)
48{
David Wagner8a1b8fc2012-01-13 13:27:34 +000049 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
David Wagnera6337e62011-09-26 03:26:34 +000050 "\n"
David Wagner8a1b8fc2012-01-13 13:27:34 +000051 "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 +000052 "\n"
53 "\tThe input file is in format:\n"
54 "\t\tkey1=value1\n"
55 "\t\tkey2=value2\n"
56 "\t\t...\n"
57 "\t-r : the environment has multiple copies in flash\n"
58 "\t-b : the target is big endian (default is little endian)\n"
David Wagner8a1b8fc2012-01-13 13:27:34 +000059 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
Horst Kronstorfer18954202011-12-05 00:55:26 +000060 "\t-V : print version information and exit\n"
David Wagnera6337e62011-09-26 03:26:34 +000061 "\n"
62 "If the input file is \"-\", data is read from standard input\n",
63 exec_name);
64}
65
66int main(int argc, char **argv)
67{
68 uint32_t crc, targetendian_crc;
69 const char *txt_filename = NULL, *bin_filename = NULL;
70 int txt_fd, bin_fd;
71 unsigned char *dataptr, *envptr;
72 unsigned char *filebuf = NULL;
73 unsigned int filesize = 0, envsize = 0, datasize = 0;
74 int bigendian = 0;
75 int redundant = 0;
76 unsigned char padbyte = 0xff;
77
78 int option;
79 int ret = EXIT_SUCCESS;
80
81 struct stat txt_file_stat;
82
83 int fp, ep;
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +000084 const char *prg;
85
86 prg = basename(argv[0]);
David Wagnera6337e62011-09-26 03:26:34 +000087
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +000088 /* Turn off getopt()'s internal error message */
89 opterr = 0;
90
David Wagnera6337e62011-09-26 03:26:34 +000091 /* Parse the cmdline */
Horst Kronstorfer18954202011-12-05 00:55:26 +000092 while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
David Wagnera6337e62011-09-26 03:26:34 +000093 switch (option) {
94 case 's':
95 datasize = strtol(optarg, NULL, 0);
96 break;
97 case 'o':
98 bin_filename = strdup(optarg);
99 if (!bin_filename) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000100 fprintf(stderr, "Can't strdup() the output filename\n");
David Wagnera6337e62011-09-26 03:26:34 +0000101 return EXIT_FAILURE;
102 }
103 break;
104 case 'r':
105 redundant = 1;
106 break;
107 case 'b':
108 bigendian = 1;
109 break;
110 case 'p':
111 padbyte = strtol(optarg, NULL, 0);
112 break;
113 case 'h':
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000114 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000115 return EXIT_SUCCESS;
Horst Kronstorfer18954202011-12-05 00:55:26 +0000116 case 'V':
117 printf("%s version %s\n", prg, PLAIN_VERSION);
118 return EXIT_SUCCESS;
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000119 case ':':
120 fprintf(stderr, "Missing argument for option -%c\n",
Horst Kronstorfer72ebafb2011-12-24 00:41:58 +0000121 optopt);
Wolfgang Denke758a5c2012-03-01 21:19:40 +0000122 usage(prg);
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000123 return EXIT_FAILURE;
David Wagnera6337e62011-09-26 03:26:34 +0000124 default:
Horst Kronstorfer72ebafb2011-12-24 00:41:58 +0000125 fprintf(stderr, "Wrong option -%c\n", optopt);
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000126 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000127 return EXIT_FAILURE;
128 }
129 }
130
131 /* Check datasize and allocate the data */
132 if (datasize == 0) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000133 fprintf(stderr, "Please specify the size of the environment partition.\n");
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000134 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000135 return EXIT_FAILURE;
136 }
137
138 dataptr = malloc(datasize * sizeof(*dataptr));
139 if (!dataptr) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000140 fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
141 datasize);
David Wagnera6337e62011-09-26 03:26:34 +0000142 return EXIT_FAILURE;
143 }
144
145 /*
146 * envptr points to the beginning of the actual environment (after the
David Wagner8a1b8fc2012-01-13 13:27:34 +0000147 * crc and possible `redundant' byte
David Wagnera6337e62011-09-26 03:26:34 +0000148 */
149 envsize = datasize - (CRC_SIZE + redundant);
150 envptr = dataptr + CRC_SIZE + redundant;
151
152 /* Pad the environment with the padding byte */
153 memset(envptr, padbyte, envsize);
154
155 /* Open the input file ... */
156 if (optind >= argc) {
157 fprintf(stderr, "Please specify an input filename\n");
158 return EXIT_FAILURE;
159 }
160
161 txt_filename = argv[optind];
162 if (strcmp(txt_filename, "-") == 0) {
163 int readbytes = 0;
164 int readlen = sizeof(*envptr) * 2048;
165 txt_fd = STDIN_FILENO;
166
167 do {
168 filebuf = realloc(filebuf, readlen);
169 readbytes = read(txt_fd, filebuf + filesize, readlen);
170 filesize += readbytes;
171 } while (readbytes == readlen);
172
173 } else {
174 txt_fd = open(txt_filename, O_RDONLY);
175 if (txt_fd == -1) {
176 fprintf(stderr, "Can't open \"%s\": %s\n",
177 txt_filename, strerror(errno));
178 return EXIT_FAILURE;
179 }
180 /* ... and check it */
181 ret = fstat(txt_fd, &txt_file_stat);
182 if (ret == -1) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000183 fprintf(stderr, "Can't stat() on \"%s\": %s\n",
184 txt_filename, strerror(errno));
David Wagnera6337e62011-09-26 03:26:34 +0000185 return EXIT_FAILURE;
186 }
187
188 filesize = txt_file_stat.st_size;
189 /* Read the raw input file and transform it */
190 filebuf = malloc(sizeof(*envptr) * filesize);
191 ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
192 if (ret != sizeof(*envptr) * filesize) {
193 fprintf(stderr, "Can't read the whole input file\n");
194 return EXIT_FAILURE;
195 }
196 ret = close(txt_fd);
197 }
David Wagner8a1b8fc2012-01-13 13:27:34 +0000198 /* The +1 is for the additionnal ending \0. See below. */
199 if (filesize + 1 > envsize) {
200 fprintf(stderr, "The input file is larger than the environment partition size\n");
David Wagnera6337e62011-09-26 03:26:34 +0000201 return EXIT_FAILURE;
202 }
203
204 /* Replace newlines separating variables with \0 */
205 for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
206 if (filebuf[fp] == '\n') {
David Wagnerdbee61d2011-12-20 14:59:29 +0000207 if (ep == 0) {
David Wagnera6337e62011-09-26 03:26:34 +0000208 /*
David Wagnerdbee61d2011-12-20 14:59:29 +0000209 * Newlines at the beginning of the file ?
210 * Ignore them.
David Wagnera6337e62011-09-26 03:26:34 +0000211 */
212 continue;
213 } else if (filebuf[fp-1] == '\\') {
214 /*
215 * Embedded newline in a variable.
216 *
David Wagnerdbee61d2011-12-20 14:59:29 +0000217 * The backslash was added to the envptr; rewind
218 * and replace it with a newline
David Wagnera6337e62011-09-26 03:26:34 +0000219 */
220 ep--;
221 envptr[ep++] = '\n';
222 } else {
223 /* End of a variable */
224 envptr[ep++] = '\0';
225 }
226 } else if (filebuf[fp] == '#') {
227 if (fp != 0 && filebuf[fp-1] == '\n') {
228 /* This line is a comment, let's skip it */
229 while (fp < txt_file_stat.st_size && fp++ &&
230 filebuf[fp] != '\n');
231 } else {
232 envptr[ep++] = filebuf[fp];
233 }
234 } else {
235 envptr[ep++] = filebuf[fp];
236 }
237 }
238 /*
239 * Make sure there is a final '\0'
240 * And do it again on the next byte to mark the end of the environment.
241 */
242 if (envptr[ep-1] != '\0') {
243 envptr[ep++] = '\0';
244 /*
245 * The text file doesn't have an ending newline. We need to
246 * check the env size again to make sure we have room for two \0
247 */
248 if (ep >= envsize) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000249 fprintf(stderr, "The environment file is too large for the target environment storage\n");
David Wagnera6337e62011-09-26 03:26:34 +0000250 return EXIT_FAILURE;
251 }
252 envptr[ep] = '\0';
253 } else {
254 envptr[ep] = '\0';
255 }
256
257 /* Computes the CRC and put it at the beginning of the data */
258 crc = crc32(0, envptr, envsize);
259 targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
260
261 memcpy(dataptr, &targetendian_crc, sizeof(uint32_t));
262
263 bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
264 if (bin_fd == -1) {
265 fprintf(stderr, "Can't open output file \"%s\": %s\n",
266 bin_filename, strerror(errno));
267 return EXIT_FAILURE;
268 }
269
270 if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
271 sizeof(*dataptr) * datasize) {
272 fprintf(stderr, "write() failed: %s\n", strerror(errno));
273 return EXIT_FAILURE;
274 }
275
276 ret = close(bin_fd);
277
278 return ret;
279}