xref: /openbmc/u-boot/tools/mkenvimage.c (revision 03efcb05)
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  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 /* We want the GNU version of basename() */
13 #define _GNU_SOURCE
14 
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <libgen.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/mman.h>
26 
27 #include "compiler.h"
28 #include <u-boot/crc.h>
29 #include <version.h>
30 
31 #define CRC_SIZE sizeof(uint32_t)
32 
33 static void usage(const char *exec_name)
34 {
35 	fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
36 	       "\n"
37 	       "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"
38 	       "\n"
39 	       "\tThe input file is in format:\n"
40 	       "\t\tkey1=value1\n"
41 	       "\t\tkey2=value2\n"
42 	       "\t\t...\n"
43 	       "\t-r : the environment has multiple copies in flash\n"
44 	       "\t-b : the target is big endian (default is little endian)\n"
45 	       "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
46 	       "\t-V : print version information and exit\n"
47 	       "\n"
48 	       "If the input file is \"-\", data is read from standard input\n",
49 	       exec_name);
50 }
51 
52 long int xstrtol(const char *s)
53 {
54 	long int tmp;
55 
56 	errno = 0;
57 	tmp = strtol(s, NULL, 0);
58 	if (!errno)
59 		return tmp;
60 
61 	if (errno == ERANGE)
62 		fprintf(stderr, "Bad integer format: %s\n",  s);
63 	else
64 		fprintf(stderr, "Error while parsing %s: %s\n", s,
65 				strerror(errno));
66 
67 	exit(EXIT_FAILURE);
68 }
69 
70 int main(int argc, char **argv)
71 {
72 	uint32_t crc, targetendian_crc;
73 	const char *txt_filename = NULL, *bin_filename = NULL;
74 	int txt_fd, bin_fd;
75 	unsigned char *dataptr, *envptr;
76 	unsigned char *filebuf = NULL;
77 	unsigned int filesize = 0, envsize = 0, datasize = 0;
78 	int bigendian = 0;
79 	int redundant = 0;
80 	unsigned char padbyte = 0xff;
81 
82 	int option;
83 	int ret = EXIT_SUCCESS;
84 
85 	struct stat txt_file_stat;
86 
87 	int fp, ep;
88 	const char *prg;
89 
90 	prg = basename(argv[0]);
91 
92 	/* Turn off getopt()'s internal error message */
93 	opterr = 0;
94 
95 	/* Parse the cmdline */
96 	while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
97 		switch (option) {
98 		case 's':
99 			datasize = xstrtol(optarg);
100 			break;
101 		case 'o':
102 			bin_filename = strdup(optarg);
103 			if (!bin_filename) {
104 				fprintf(stderr, "Can't strdup() the output filename\n");
105 				return EXIT_FAILURE;
106 			}
107 			break;
108 		case 'r':
109 			redundant = 1;
110 			break;
111 		case 'b':
112 			bigendian = 1;
113 			break;
114 		case 'p':
115 			padbyte = xstrtol(optarg);
116 			break;
117 		case 'h':
118 			usage(prg);
119 			return EXIT_SUCCESS;
120 		case 'V':
121 			printf("%s version %s\n", prg, PLAIN_VERSION);
122 			return EXIT_SUCCESS;
123 		case ':':
124 			fprintf(stderr, "Missing argument for option -%c\n",
125 				optopt);
126 			usage(prg);
127 			return EXIT_FAILURE;
128 		default:
129 			fprintf(stderr, "Wrong option -%c\n", optopt);
130 			usage(prg);
131 			return EXIT_FAILURE;
132 		}
133 	}
134 
135 	/* Check datasize and allocate the data */
136 	if (datasize == 0) {
137 		fprintf(stderr, "Please specify the size of the environment partition.\n");
138 		usage(prg);
139 		return EXIT_FAILURE;
140 	}
141 
142 	dataptr = malloc(datasize * sizeof(*dataptr));
143 	if (!dataptr) {
144 		fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
145 				datasize);
146 		return EXIT_FAILURE;
147 	}
148 
149 	/*
150 	 * envptr points to the beginning of the actual environment (after the
151 	 * crc and possible `redundant' byte
152 	 */
153 	envsize = datasize - (CRC_SIZE + redundant);
154 	envptr = dataptr + CRC_SIZE + redundant;
155 
156 	/* Pad the environment with the padding byte */
157 	memset(envptr, padbyte, envsize);
158 
159 	/* Open the input file ... */
160 	if (optind >= argc || strcmp(argv[optind], "-") == 0) {
161 		int readbytes = 0;
162 		int readlen = sizeof(*envptr) * 4096;
163 		txt_fd = STDIN_FILENO;
164 
165 		do {
166 			filebuf = realloc(filebuf, readlen);
167 			if (!filebuf) {
168 				fprintf(stderr, "Can't realloc memory for the input file buffer\n");
169 				return EXIT_FAILURE;
170 			}
171 			readbytes = read(txt_fd, filebuf + filesize, readlen);
172 			if (errno) {
173 				fprintf(stderr, "Error while reading stdin: %s\n",
174 						strerror(errno));
175 				return EXIT_FAILURE;
176 			}
177 			filesize += readbytes;
178 		} while (readbytes == readlen);
179 
180 	} else {
181 		txt_filename = argv[optind];
182 		txt_fd = open(txt_filename, O_RDONLY);
183 		if (txt_fd == -1) {
184 			fprintf(stderr, "Can't open \"%s\": %s\n",
185 					txt_filename, strerror(errno));
186 			return EXIT_FAILURE;
187 		}
188 		/* ... and check it */
189 		ret = fstat(txt_fd, &txt_file_stat);
190 		if (ret == -1) {
191 			fprintf(stderr, "Can't stat() on \"%s\": %s\n",
192 					txt_filename, strerror(errno));
193 			return EXIT_FAILURE;
194 		}
195 
196 		filesize = txt_file_stat.st_size;
197 
198 		filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
199 			       MAP_PRIVATE, txt_fd, 0);
200 		if (filebuf == MAP_FAILED) {
201 			fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
202 					sizeof(*envptr) * filesize,
203 					strerror(errno));
204 			fprintf(stderr, "Falling back to read()\n");
205 
206 			filebuf = malloc(sizeof(*envptr) * filesize);
207 			ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
208 			if (ret != sizeof(*envptr) * filesize) {
209 				fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
210 					sizeof(*envptr) * filesize,
211 					strerror(errno));
212 
213 				return EXIT_FAILURE;
214 			}
215 		}
216 		ret = close(txt_fd);
217 	}
218 	/* The +1 is for the additionnal ending \0. See below. */
219 	if (filesize + 1 > envsize) {
220 		fprintf(stderr, "The input file is larger than the environment partition size\n");
221 		return EXIT_FAILURE;
222 	}
223 
224 	/* Replace newlines separating variables with \0 */
225 	for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
226 		if (filebuf[fp] == '\n') {
227 			if (ep == 0) {
228 				/*
229 				 * Newlines at the beginning of the file ?
230 				 * Ignore them.
231 				 */
232 				continue;
233 			} else if (filebuf[fp-1] == '\\') {
234 				/*
235 				 * Embedded newline in a variable.
236 				 *
237 				 * The backslash was added to the envptr; rewind
238 				 * and replace it with a newline
239 				 */
240 				ep--;
241 				envptr[ep++] = '\n';
242 			} else {
243 				/* End of a variable */
244 				envptr[ep++] = '\0';
245 			}
246 		} else {
247 			envptr[ep++] = filebuf[fp];
248 		}
249 	}
250 	/*
251 	 * Make sure there is a final '\0'
252 	 * And do it again on the next byte to mark the end of the environment.
253 	 */
254 	if (envptr[ep-1] != '\0') {
255 		envptr[ep++] = '\0';
256 		/*
257 		 * The text file doesn't have an ending newline.  We need to
258 		 * check the env size again to make sure we have room for two \0
259 		 */
260 		if (ep >= envsize) {
261 			fprintf(stderr, "The environment file is too large for the target environment storage\n");
262 			return EXIT_FAILURE;
263 		}
264 		envptr[ep] = '\0';
265 	} else {
266 		envptr[ep] = '\0';
267 	}
268 
269 	/* Computes the CRC and put it at the beginning of the data */
270 	crc = crc32(0, envptr, envsize);
271 	targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
272 
273 	memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
274 	if (redundant)
275 		dataptr[sizeof(targetendian_crc)] = 1;
276 
277 	if (!bin_filename || strcmp(bin_filename, "-") == 0) {
278 		bin_fd = STDOUT_FILENO;
279 	} else {
280 		bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
281 					     S_IWGRP);
282 		if (bin_fd == -1) {
283 			fprintf(stderr, "Can't open output file \"%s\": %s\n",
284 					bin_filename, strerror(errno));
285 			return EXIT_FAILURE;
286 		}
287 	}
288 
289 	if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
290 			sizeof(*dataptr) * datasize) {
291 		fprintf(stderr, "write() failed: %s\n", strerror(errno));
292 		return EXIT_FAILURE;
293 	}
294 
295 	ret = close(bin_fd);
296 
297 	return ret;
298 }
299