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