blob: 9c32f4a5725fc8503c42f958f705eb683a3b8bbc [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
28#include <errno.h>
29#include <fcntl.h>
30#include <stdio.h>
31#include <stdint.h>
32#include <string.h>
33#include <unistd.h>
34#include <compiler.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37
38#include <u-boot/crc.h>
39
40#define CRC_SIZE sizeof(uint32_t)
41
42static void usage(const char *exec_name)
43{
44 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] "
45 "-s <environment partition size> -o <output> <input file>\n"
46 "\n"
47 "This tool takes a key=value input file (same as would a "
48 "`printenv' show) and generates the corresponding environment "
49 "image, ready to be flashed.\n"
50 "\n"
51 "\tThe input file is in format:\n"
52 "\t\tkey1=value1\n"
53 "\t\tkey2=value2\n"
54 "\t\t...\n"
55 "\t-r : the environment has multiple copies in flash\n"
56 "\t-b : the target is big endian (default is little endian)\n"
57 "\t-p <byte> : fill the image with <byte> bytes instead of "
58 "0xff bytes\n"
59 "\n"
60 "If the input file is \"-\", data is read from standard input\n",
61 exec_name);
62}
63
64int main(int argc, char **argv)
65{
66 uint32_t crc, targetendian_crc;
67 const char *txt_filename = NULL, *bin_filename = NULL;
68 int txt_fd, bin_fd;
69 unsigned char *dataptr, *envptr;
70 unsigned char *filebuf = NULL;
71 unsigned int filesize = 0, envsize = 0, datasize = 0;
72 int bigendian = 0;
73 int redundant = 0;
74 unsigned char padbyte = 0xff;
75
76 int option;
77 int ret = EXIT_SUCCESS;
78
79 struct stat txt_file_stat;
80
81 int fp, ep;
82
83 /* Parse the cmdline */
84 while ((option = getopt(argc, argv, "s:o:rbp:h")) != -1) {
85 switch (option) {
86 case 's':
87 datasize = strtol(optarg, NULL, 0);
88 break;
89 case 'o':
90 bin_filename = strdup(optarg);
91 if (!bin_filename) {
92 fprintf(stderr, "Can't strdup() the output "
93 "filename\n");
94 return EXIT_FAILURE;
95 }
96 break;
97 case 'r':
98 redundant = 1;
99 break;
100 case 'b':
101 bigendian = 1;
102 break;
103 case 'p':
104 padbyte = strtol(optarg, NULL, 0);
105 break;
106 case 'h':
107 usage(argv[0]);
108 return EXIT_SUCCESS;
109 default:
110 fprintf(stderr, "Wrong option -%c\n", option);
111 usage(argv[0]);
112 return EXIT_FAILURE;
113 }
114 }
115
116 /* Check datasize and allocate the data */
117 if (datasize == 0) {
118 fprintf(stderr,
119 "Please specify the size of the envrionnment "
120 "partition.\n");
121 usage(argv[0]);
122 return EXIT_FAILURE;
123 }
124
125 dataptr = malloc(datasize * sizeof(*dataptr));
126 if (!dataptr) {
127 fprintf(stderr, "Can't alloc dataptr.\n");
128 return EXIT_FAILURE;
129 }
130
131 /*
132 * envptr points to the beginning of the actual environment (after the
133 * crc and possible `redundant' bit
134 */
135 envsize = datasize - (CRC_SIZE + redundant);
136 envptr = dataptr + CRC_SIZE + redundant;
137
138 /* Pad the environment with the padding byte */
139 memset(envptr, padbyte, envsize);
140
141 /* Open the input file ... */
142 if (optind >= argc) {
143 fprintf(stderr, "Please specify an input filename\n");
144 return EXIT_FAILURE;
145 }
146
147 txt_filename = argv[optind];
148 if (strcmp(txt_filename, "-") == 0) {
149 int readbytes = 0;
150 int readlen = sizeof(*envptr) * 2048;
151 txt_fd = STDIN_FILENO;
152
153 do {
154 filebuf = realloc(filebuf, readlen);
155 readbytes = read(txt_fd, filebuf + filesize, readlen);
156 filesize += readbytes;
157 } while (readbytes == readlen);
158
159 } else {
160 txt_fd = open(txt_filename, O_RDONLY);
161 if (txt_fd == -1) {
162 fprintf(stderr, "Can't open \"%s\": %s\n",
163 txt_filename, strerror(errno));
164 return EXIT_FAILURE;
165 }
166 /* ... and check it */
167 ret = fstat(txt_fd, &txt_file_stat);
168 if (ret == -1) {
169 fprintf(stderr, "Can't stat() on \"%s\": "
170 "%s\n", txt_filename, strerror(errno));
171 return EXIT_FAILURE;
172 }
173
174 filesize = txt_file_stat.st_size;
175 /* Read the raw input file and transform it */
176 filebuf = malloc(sizeof(*envptr) * filesize);
177 ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
178 if (ret != sizeof(*envptr) * filesize) {
179 fprintf(stderr, "Can't read the whole input file\n");
180 return EXIT_FAILURE;
181 }
182 ret = close(txt_fd);
183 }
184 /*
185 * The right test to do is "=>" (not ">") because of the additionnal
186 * ending \0. See below.
187 */
188 if (filesize >= envsize) {
189 fprintf(stderr, "The input file is larger than the "
190 "envrionnment partition size\n");
191 return EXIT_FAILURE;
192 }
193
194 /* Replace newlines separating variables with \0 */
195 for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
196 if (filebuf[fp] == '\n') {
197 if (fp == 0) {
198 /*
199 * Newline at the beggining of the file ?
200 * Ignore it.
201 */
202 continue;
203 } else if (filebuf[fp-1] == '\\') {
204 /*
205 * Embedded newline in a variable.
206 *
207 * The backslash was added to the envptr ;
208 * rewind and replace it with a newline
209 */
210 ep--;
211 envptr[ep++] = '\n';
212 } else {
213 /* End of a variable */
214 envptr[ep++] = '\0';
215 }
216 } else if (filebuf[fp] == '#') {
217 if (fp != 0 && filebuf[fp-1] == '\n') {
218 /* This line is a comment, let's skip it */
219 while (fp < txt_file_stat.st_size && fp++ &&
220 filebuf[fp] != '\n');
221 } else {
222 envptr[ep++] = filebuf[fp];
223 }
224 } else {
225 envptr[ep++] = filebuf[fp];
226 }
227 }
228 /*
229 * Make sure there is a final '\0'
230 * And do it again on the next byte to mark the end of the environment.
231 */
232 if (envptr[ep-1] != '\0') {
233 envptr[ep++] = '\0';
234 /*
235 * The text file doesn't have an ending newline. We need to
236 * check the env size again to make sure we have room for two \0
237 */
238 if (ep >= envsize) {
239 fprintf(stderr, "The environment file is too large for "
240 "the target environment storage\n");
241 return EXIT_FAILURE;
242 }
243 envptr[ep] = '\0';
244 } else {
245 envptr[ep] = '\0';
246 }
247
248 /* Computes the CRC and put it at the beginning of the data */
249 crc = crc32(0, envptr, envsize);
250 targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
251
252 memcpy(dataptr, &targetendian_crc, sizeof(uint32_t));
253
254 bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
255 if (bin_fd == -1) {
256 fprintf(stderr, "Can't open output file \"%s\": %s\n",
257 bin_filename, strerror(errno));
258 return EXIT_FAILURE;
259 }
260
261 if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
262 sizeof(*dataptr) * datasize) {
263 fprintf(stderr, "write() failed: %s\n", strerror(errno));
264 return EXIT_FAILURE;
265 }
266
267 ret = close(bin_fd);
268
269 return ret;
270}