xref: /openbmc/u-boot/tools/mkenvimage.c (revision 8875833a)
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 <stdint.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <compiler.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 
41 #include <u-boot/crc.h>
42 #include <version.h>
43 
44 #define CRC_SIZE sizeof(uint32_t)
45 
46 static void usage(const char *exec_name)
47 {
48 	fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] "
49 	       "-s <environment partition size> -o <output> <input file>\n"
50 	       "\n"
51 	       "This tool takes a key=value input file (same as would a "
52 	       "`printenv' show) and generates the corresponding environment "
53 	       "image, ready to be flashed.\n"
54 	       "\n"
55 	       "\tThe input file is in format:\n"
56 	       "\t\tkey1=value1\n"
57 	       "\t\tkey2=value2\n"
58 	       "\t\t...\n"
59 	       "\t-r : the environment has multiple copies in flash\n"
60 	       "\t-b : the target is big endian (default is little endian)\n"
61 	       "\t-p <byte> : fill the image with <byte> bytes instead of "
62 	       "0xff bytes\n"
63 	       "\t-V : print version information and exit\n"
64 	       "\n"
65 	       "If the input file is \"-\", data is read from standard input\n",
66 	       exec_name);
67 }
68 
69 int main(int argc, char **argv)
70 {
71 	uint32_t crc, targetendian_crc;
72 	const char *txt_filename = NULL, *bin_filename = NULL;
73 	int txt_fd, bin_fd;
74 	unsigned char *dataptr, *envptr;
75 	unsigned char *filebuf = NULL;
76 	unsigned int filesize = 0, envsize = 0, datasize = 0;
77 	int bigendian = 0;
78 	int redundant = 0;
79 	unsigned char padbyte = 0xff;
80 
81 	int option;
82 	int ret = EXIT_SUCCESS;
83 
84 	struct stat txt_file_stat;
85 
86 	int fp, ep;
87 	const char *prg;
88 
89 	prg = basename(argv[0]);
90 
91 	/* Turn off getopt()'s internal error message */
92 	opterr = 0;
93 
94 	/* Parse the cmdline */
95 	while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
96 		switch (option) {
97 		case 's':
98 			datasize = strtol(optarg, NULL, 0);
99 			break;
100 		case 'o':
101 			bin_filename = strdup(optarg);
102 			if (!bin_filename) {
103 				fprintf(stderr, "Can't strdup() the output "
104 						"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 = strtol(optarg, NULL, 0);
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(argv[0]);
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,
138 			"Please specify the size of the environment "
139 			"partition.\n");
140 		usage(prg);
141 		return EXIT_FAILURE;
142 	}
143 
144 	dataptr = malloc(datasize * sizeof(*dataptr));
145 	if (!dataptr) {
146 		fprintf(stderr, "Can't alloc dataptr.\n");
147 		return EXIT_FAILURE;
148 	}
149 
150 	/*
151 	 * envptr points to the beginning of the actual environment (after the
152 	 * crc and possible `redundant' bit
153 	 */
154 	envsize = datasize - (CRC_SIZE + redundant);
155 	envptr = dataptr + CRC_SIZE + redundant;
156 
157 	/* Pad the environment with the padding byte */
158 	memset(envptr, padbyte, envsize);
159 
160 	/* Open the input file ... */
161 	if (optind >= argc) {
162 		fprintf(stderr, "Please specify an input filename\n");
163 		return EXIT_FAILURE;
164 	}
165 
166 	txt_filename = argv[optind];
167 	if (strcmp(txt_filename, "-") == 0) {
168 		int readbytes = 0;
169 		int readlen = sizeof(*envptr) * 2048;
170 		txt_fd = STDIN_FILENO;
171 
172 		do {
173 			filebuf = realloc(filebuf, readlen);
174 			readbytes = read(txt_fd, filebuf + filesize, readlen);
175 			filesize += readbytes;
176 		} while (readbytes == readlen);
177 
178 	} else {
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\": "
189 					"%s\n", txt_filename, strerror(errno));
190 			return EXIT_FAILURE;
191 		}
192 
193 		filesize = txt_file_stat.st_size;
194 		/* Read the raw input file and transform it */
195 		filebuf = malloc(sizeof(*envptr) * filesize);
196 		ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
197 		if (ret != sizeof(*envptr) * filesize) {
198 			fprintf(stderr, "Can't read the whole input file\n");
199 			return EXIT_FAILURE;
200 		}
201 		ret = close(txt_fd);
202 	}
203 	/*
204 	 * The right test to do is "=>" (not ">") because of the additional
205 	 * ending \0. See below.
206 	 */
207 	if (filesize >= envsize) {
208 		fprintf(stderr, "The input file is larger than the "
209 				"environment partition size\n");
210 		return EXIT_FAILURE;
211 	}
212 
213 	/* Replace newlines separating variables with \0 */
214 	for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
215 		if (filebuf[fp] == '\n') {
216 			if (ep == 0) {
217 				/*
218 				 * Newlines at the beginning of the file ?
219 				 * Ignore them.
220 				 */
221 				continue;
222 			} else if (filebuf[fp-1] == '\\') {
223 				/*
224 				 * Embedded newline in a variable.
225 				 *
226 				 * The backslash was added to the envptr; rewind
227 				 * and replace it with a newline
228 				 */
229 				ep--;
230 				envptr[ep++] = '\n';
231 			} else {
232 				/* End of a variable */
233 				envptr[ep++] = '\0';
234 			}
235 		} else if (filebuf[fp] == '#') {
236 			if (fp != 0 && filebuf[fp-1] == '\n') {
237 				/* This line is a comment, let's skip it */
238 				while (fp < txt_file_stat.st_size && fp++ &&
239 				       filebuf[fp] != '\n');
240 			} else {
241 				envptr[ep++] = filebuf[fp];
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 "
259 					"the target environment storage\n");
260 			return EXIT_FAILURE;
261 		}
262 		envptr[ep] = '\0';
263 	} else {
264 		envptr[ep] = '\0';
265 	}
266 
267 	/* Computes the CRC and put it at the beginning of the data */
268 	crc = crc32(0, envptr, envsize);
269 	targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
270 
271 	memcpy(dataptr, &targetendian_crc, sizeof(uint32_t));
272 
273 	bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
274 	if (bin_fd == -1) {
275 		fprintf(stderr, "Can't open output file \"%s\": %s\n",
276 				bin_filename, strerror(errno));
277 		return EXIT_FAILURE;
278 	}
279 
280 	if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
281 			sizeof(*dataptr) * datasize) {
282 		fprintf(stderr, "write() failed: %s\n", strerror(errno));
283 		return EXIT_FAILURE;
284 	}
285 
286 	ret = close(bin_fd);
287 
288 	return ret;
289 }
290