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