]> Git Repo - J-u-boot.git/blame - tools/mkenvimage.c
Blackfin: bfin_mac: drop volatile markings on packet buffers
[J-u-boot.git] / tools / mkenvimage.c
CommitLineData
a6337e6f
DW
1/*
2 * (C) Copyright 2011 Free Electrons
3 * David Wagner <[email protected]>
4 *
5 * Inspired from envcrc.c:
6 * (C) Copyright 2001
7 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), [email protected]
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
af44f4b2
HK
28/* We want the GNU version of basename() */
29#define _GNU_SOURCE
30
a6337e6f
DW
31#include <errno.h>
32#include <fcntl.h>
33#include <stdio.h>
d1acdae9 34#include <stdlib.h>
a6337e6f
DW
35#include <stdint.h>
36#include <string.h>
37#include <unistd.h>
558cd995 38#include <libgen.h>
a6337e6f
DW
39#include <sys/types.h>
40#include <sys/stat.h>
6ee39f80 41#include <sys/mman.h>
a6337e6f 42
d1acdae9 43#include "compiler.h"
a6337e6f 44#include <u-boot/crc.h>
1895420b 45#include <version.h>
a6337e6f
DW
46
47#define CRC_SIZE sizeof(uint32_t)
48
8b6a4952
VY
49#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
a6337e6f
DW
56static void usage(const char *exec_name)
57{
8a1b8fc7 58 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
a6337e6f 59 "\n"
8a1b8fc7 60 "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"
a6337e6f
DW
61 "\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"
8a1b8fc7 68 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
1895420b 69 "\t-V : print version information and exit\n"
a6337e6f
DW
70 "\n"
71 "If the input file is \"-\", data is read from standard input\n",
72 exec_name);
73}
74
3d0f9bd0
DW
75long 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
a6337e6f
DW
93int 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;
af44f4b2
HK
111 const char *prg;
112
113 prg = basename(argv[0]);
a6337e6f 114
5e0c63e2
HK
115 /* Turn off getopt()'s internal error message */
116 opterr = 0;
117
a6337e6f 118 /* Parse the cmdline */
1895420b 119 while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
a6337e6f
DW
120 switch (option) {
121 case 's':
3d0f9bd0 122 datasize = xstrtol(optarg);
a6337e6f
DW
123 break;
124 case 'o':
125 bin_filename = strdup(optarg);
126 if (!bin_filename) {
8a1b8fc7 127 fprintf(stderr, "Can't strdup() the output filename\n");
a6337e6f
DW
128 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':
3d0f9bd0 138 padbyte = xstrtol(optarg);
a6337e6f
DW
139 break;
140 case 'h':
af44f4b2 141 usage(prg);
a6337e6f 142 return EXIT_SUCCESS;
1895420b
HK
143 case 'V':
144 printf("%s version %s\n", prg, PLAIN_VERSION);
145 return EXIT_SUCCESS;
5e0c63e2
HK
146 case ':':
147 fprintf(stderr, "Missing argument for option -%c\n",
72ebafbe 148 optopt);
e758a5c4 149 usage(prg);
5e0c63e2 150 return EXIT_FAILURE;
a6337e6f 151 default:
72ebafbe 152 fprintf(stderr, "Wrong option -%c\n", optopt);
af44f4b2 153 usage(prg);
a6337e6f
DW
154 return EXIT_FAILURE;
155 }
156 }
157
158 /* Check datasize and allocate the data */
159 if (datasize == 0) {
8a1b8fc7 160 fprintf(stderr, "Please specify the size of the environment partition.\n");
af44f4b2 161 usage(prg);
a6337e6f
DW
162 return EXIT_FAILURE;
163 }
164
165 dataptr = malloc(datasize * sizeof(*dataptr));
166 if (!dataptr) {
8a1b8fc7
DW
167 fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
168 datasize);
a6337e6f
DW
169 return EXIT_FAILURE;
170 }
171
172 /*
173 * envptr points to the beginning of the actual environment (after the
8a1b8fc7 174 * crc and possible `redundant' byte
a6337e6f
DW
175 */
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 ... */
48995b5a 183 if (optind >= argc || strcmp(argv[optind], "-") == 0) {
a6337e6f 184 int readbytes = 0;
48995b5a 185 int readlen = sizeof(*envptr) * 4096;
a6337e6f
DW
186 txt_fd = STDIN_FILENO;
187
188 do {
189 filebuf = realloc(filebuf, readlen);
3d0f9bd0
DW
190 if (!filebuf) {
191 fprintf(stderr, "Can't realloc memory for the input file buffer\n");
192 return EXIT_FAILURE;
193 }
a6337e6f 194 readbytes = read(txt_fd, filebuf + filesize, readlen);
3d0f9bd0
DW
195 if (errno) {
196 fprintf(stderr, "Error while reading stdin: %s\n",
197 strerror(errno));
198 return EXIT_FAILURE;
199 }
a6337e6f
DW
200 filesize += readbytes;
201 } while (readbytes == readlen);
202
203 } else {
48995b5a 204 txt_filename = argv[optind];
a6337e6f
DW
205 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) {
8a1b8fc7
DW
214 fprintf(stderr, "Can't stat() on \"%s\": %s\n",
215 txt_filename, strerror(errno));
a6337e6f
DW
216 return EXIT_FAILURE;
217 }
218
219 filesize = txt_file_stat.st_size;
6ee39f80
DW
220
221 filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
222 MAP_PRIVATE, txt_fd, 0);
223 if (filebuf == MAP_FAILED) {
1ebff63f 224 fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
6ee39f80
DW
225 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) {
1ebff63f 232 fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
6ee39f80
DW
233 sizeof(*envptr) * filesize,
234 strerror(errno));
235
236 return EXIT_FAILURE;
237 }
a6337e6f
DW
238 }
239 ret = close(txt_fd);
240 }
8a1b8fc7
DW
241 /* 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");
a6337e6f
DW
244 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') {
dbee61db 250 if (ep == 0) {
a6337e6f 251 /*
dbee61db
DW
252 * Newlines at the beginning of the file ?
253 * Ignore them.
a6337e6f
DW
254 */
255 continue;
256 } else if (filebuf[fp-1] == '\\') {
257 /*
258 * Embedded newline in a variable.
259 *
dbee61db
DW
260 * The backslash was added to the envptr; rewind
261 * and replace it with a newline
a6337e6f
DW
262 */
263 ep--;
264 envptr[ep++] = '\n';
265 } else {
266 /* End of a variable */
267 envptr[ep++] = '\0';
268 }
a6337e6f
DW
269 } 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) {
8a1b8fc7 284 fprintf(stderr, "The environment file is too large for the target environment storage\n");
a6337e6f
DW
285 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
d8d26599
DW
296 memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
297 if (redundant)
298 dataptr[sizeof(targetendian_crc)] = 1;
a6337e6f 299
48995b5a
DW
300 if (!bin_filename || strcmp(bin_filename, "-") == 0) {
301 bin_fd = STDOUT_FILENO;
302 } else {
8b6a4952 303 bin_fd = creat(bin_filename, FILE_PERM);
48995b5a
DW
304 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 }
a6337e6f
DW
309 }
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}
This page took 0.073872 seconds and 4 git commands to generate.