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