blob: b05f83415f0f798b218255bc31bbc159c29737a0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
David Wagnera6337e62011-09-26 03:26:34 +00002/*
3 * (C) Copyright 2011 Free Electrons
4 * David Wagner <david.wagner@free-electrons.com>
5 *
6 * Inspired from envcrc.c:
7 * (C) Copyright 2001
8 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
David Wagnera6337e62011-09-26 03:26:34 +00009 */
10
11#include <errno.h>
12#include <fcntl.h>
13#include <stdio.h>
David Wagnerd1acdae2012-01-13 13:27:35 +000014#include <stdlib.h>
David Wagnera6337e62011-09-26 03:26:34 +000015#include <stdint.h>
16#include <string.h>
Simon Glass3db71102019-11-14 12:57:16 -070017#include <u-boot/crc.h>
David Wagnera6337e62011-09-26 03:26:34 +000018#include <unistd.h>
Andreas Bießmann558cd992012-06-28 08:01:58 +020019#include <libgen.h>
David Wagnera6337e62011-09-26 03:26:34 +000020#include <sys/types.h>
21#include <sys/stat.h>
David Wagner6ee39f82012-01-13 13:27:38 +000022#include <sys/mman.h>
David Wagnera6337e62011-09-26 03:26:34 +000023
David Wagnerd1acdae2012-01-13 13:27:35 +000024#include "compiler.h"
David Wagnera6337e62011-09-26 03:26:34 +000025#include <u-boot/crc.h>
Horst Kronstorfer18954202011-12-05 00:55:26 +000026#include <version.h>
David Wagnera6337e62011-09-26 03:26:34 +000027
28#define CRC_SIZE sizeof(uint32_t)
29
30static void usage(const char *exec_name)
31{
David Wagner8a1b8fc2012-01-13 13:27:34 +000032 fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
David Wagnera6337e62011-09-26 03:26:34 +000033 "\n"
David Wagner8a1b8fc2012-01-13 13:27:34 +000034 "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"
David Wagnera6337e62011-09-26 03:26:34 +000035 "\n"
36 "\tThe input file is in format:\n"
37 "\t\tkey1=value1\n"
38 "\t\tkey2=value2\n"
39 "\t\t...\n"
Dominik Muthe72be892014-08-28 12:25:27 +020040 "\tEmpty lines are skipped, and lines with a # in the first\n"
41 "\tcolumn are treated as comments (also skipped).\n"
David Wagnera6337e62011-09-26 03:26:34 +000042 "\t-r : the environment has multiple copies in flash\n"
43 "\t-b : the target is big endian (default is little endian)\n"
David Wagner8a1b8fc2012-01-13 13:27:34 +000044 "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
Horst Kronstorfer18954202011-12-05 00:55:26 +000045 "\t-V : print version information and exit\n"
David Wagnera6337e62011-09-26 03:26:34 +000046 "\n"
47 "If the input file is \"-\", data is read from standard input\n",
48 exec_name);
49}
50
David Wagner3d0f9bd2012-01-13 13:27:36 +000051long int xstrtol(const char *s)
52{
53 long int tmp;
54
55 errno = 0;
56 tmp = strtol(s, NULL, 0);
57 if (!errno)
58 return tmp;
59
60 if (errno == ERANGE)
61 fprintf(stderr, "Bad integer format: %s\n", s);
62 else
63 fprintf(stderr, "Error while parsing %s: %s\n", s,
64 strerror(errno));
65
66 exit(EXIT_FAILURE);
67}
68
Andre Przywara849f9be2019-06-30 02:45:01 +010069#define CHUNK_SIZE 4096
70
David Wagnera6337e62011-09-26 03:26:34 +000071int main(int argc, char **argv)
72{
73 uint32_t crc, targetendian_crc;
Andre Przywara849f9be2019-06-30 02:45:01 +010074 const char *bin_filename = NULL;
David Wagnera6337e62011-09-26 03:26:34 +000075 int txt_fd, bin_fd;
76 unsigned char *dataptr, *envptr;
77 unsigned char *filebuf = NULL;
78 unsigned int filesize = 0, envsize = 0, datasize = 0;
79 int bigendian = 0;
80 int redundant = 0;
81 unsigned char padbyte = 0xff;
Andre Przywara849f9be2019-06-30 02:45:01 +010082 int readbytes = 0;
David Wagnera6337e62011-09-26 03:26:34 +000083
84 int option;
85 int ret = EXIT_SUCCESS;
86
David Wagnera6337e62011-09-26 03:26:34 +000087 int fp, ep;
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +000088 const char *prg;
89
90 prg = basename(argv[0]);
David Wagnera6337e62011-09-26 03:26:34 +000091
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +000092 /* Turn off getopt()'s internal error message */
93 opterr = 0;
94
David Wagnera6337e62011-09-26 03:26:34 +000095 /* Parse the cmdline */
Horst Kronstorfer18954202011-12-05 00:55:26 +000096 while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
David Wagnera6337e62011-09-26 03:26:34 +000097 switch (option) {
98 case 's':
David Wagner3d0f9bd2012-01-13 13:27:36 +000099 datasize = xstrtol(optarg);
David Wagnera6337e62011-09-26 03:26:34 +0000100 break;
101 case 'o':
102 bin_filename = strdup(optarg);
103 if (!bin_filename) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000104 fprintf(stderr, "Can't strdup() the output filename\n");
David Wagnera6337e62011-09-26 03:26:34 +0000105 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':
David Wagner3d0f9bd2012-01-13 13:27:36 +0000115 padbyte = xstrtol(optarg);
David Wagnera6337e62011-09-26 03:26:34 +0000116 break;
117 case 'h':
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000118 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000119 return EXIT_SUCCESS;
Horst Kronstorfer18954202011-12-05 00:55:26 +0000120 case 'V':
121 printf("%s version %s\n", prg, PLAIN_VERSION);
122 return EXIT_SUCCESS;
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000123 case ':':
124 fprintf(stderr, "Missing argument for option -%c\n",
Horst Kronstorfer72ebafb2011-12-24 00:41:58 +0000125 optopt);
Wolfgang Denke758a5c2012-03-01 21:19:40 +0000126 usage(prg);
Horst Kronstorfer5e0c63e2011-12-05 00:55:24 +0000127 return EXIT_FAILURE;
David Wagnera6337e62011-09-26 03:26:34 +0000128 default:
Horst Kronstorfer72ebafb2011-12-24 00:41:58 +0000129 fprintf(stderr, "Wrong option -%c\n", optopt);
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000130 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000131 return EXIT_FAILURE;
132 }
133 }
134
135 /* Check datasize and allocate the data */
136 if (datasize == 0) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000137 fprintf(stderr, "Please specify the size of the environment partition.\n");
Horst Kronstorferaf44f4b2011-12-21 10:39:39 +0000138 usage(prg);
David Wagnera6337e62011-09-26 03:26:34 +0000139 return EXIT_FAILURE;
140 }
141
142 dataptr = malloc(datasize * sizeof(*dataptr));
143 if (!dataptr) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000144 fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
145 datasize);
David Wagnera6337e62011-09-26 03:26:34 +0000146 return EXIT_FAILURE;
147 }
148
149 /*
150 * envptr points to the beginning of the actual environment (after the
David Wagner8a1b8fc2012-01-13 13:27:34 +0000151 * crc and possible `redundant' byte
David Wagnera6337e62011-09-26 03:26:34 +0000152 */
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 ... */
David Wagner48995b52012-01-13 13:27:37 +0000160 if (optind >= argc || strcmp(argv[optind], "-") == 0) {
David Wagnera6337e62011-09-26 03:26:34 +0000161 txt_fd = STDIN_FILENO;
David Wagnera6337e62011-09-26 03:26:34 +0000162 } else {
Andre Przywara849f9be2019-06-30 02:45:01 +0100163 txt_fd = open(argv[optind], O_RDONLY);
David Wagnera6337e62011-09-26 03:26:34 +0000164 if (txt_fd == -1) {
165 fprintf(stderr, "Can't open \"%s\": %s\n",
Andre Przywara849f9be2019-06-30 02:45:01 +0100166 argv[optind], strerror(errno));
David Wagnera6337e62011-09-26 03:26:34 +0000167 return EXIT_FAILURE;
168 }
David Wagnera6337e62011-09-26 03:26:34 +0000169 }
David Wagnera6337e62011-09-26 03:26:34 +0000170
Andre Przywara849f9be2019-06-30 02:45:01 +0100171 do {
172 filebuf = realloc(filebuf, filesize + CHUNK_SIZE);
173 if (!filebuf) {
174 fprintf(stderr, "Can't realloc memory for the input file buffer\n");
175 return EXIT_FAILURE;
176 }
177 readbytes = read(txt_fd, filebuf + filesize, CHUNK_SIZE);
178 if (readbytes < 0) {
179 fprintf(stderr, "Error while reading: %s\n",
180 strerror(errno));
181 return EXIT_FAILURE;
182 }
183 filesize += readbytes;
184 } while (readbytes > 0);
185
186 if (txt_fd != STDIN_FILENO)
187 ret = close(txt_fd);
188
Brian McFarland80ee0192015-03-12 11:52:49 -0400189 /* Parse a byte at time until reaching the file OR until the environment fills
190 * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */
191 for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) {
David Wagnera6337e62011-09-26 03:26:34 +0000192 if (filebuf[fp] == '\n') {
Dominik Muthe72be892014-08-28 12:25:27 +0200193 if (fp == 0 || filebuf[fp-1] == '\n') {
David Wagnera6337e62011-09-26 03:26:34 +0000194 /*
Dominik Muthe72be892014-08-28 12:25:27 +0200195 * Skip empty lines.
David Wagnera6337e62011-09-26 03:26:34 +0000196 */
197 continue;
198 } else if (filebuf[fp-1] == '\\') {
199 /*
200 * Embedded newline in a variable.
201 *
David Wagnerdbee61d2011-12-20 14:59:29 +0000202 * The backslash was added to the envptr; rewind
203 * and replace it with a newline
David Wagnera6337e62011-09-26 03:26:34 +0000204 */
205 ep--;
206 envptr[ep++] = '\n';
207 } else {
208 /* End of a variable */
209 envptr[ep++] = '\0';
210 }
Dominik Muthe72be892014-08-28 12:25:27 +0200211 } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
212 /* Comment, skip the line. */
213 while (++fp < filesize && filebuf[fp] != '\n')
214 continue;
David Wagnera6337e62011-09-26 03:26:34 +0000215 } else {
216 envptr[ep++] = filebuf[fp];
217 }
218 }
Brian McFarland80ee0192015-03-12 11:52:49 -0400219 /* If there are more bytes in the file still, it means the env filled up
220 * before parsing the whole file. Eat comments & whitespace here to see if
221 * there was anything meaning full left in the file, and if so, throw a error
222 * and exit. */
223 for( ; fp < filesize; fp++ )
224 {
225 if (filebuf[fp] == '\n') {
226 if (fp == 0 || filebuf[fp-1] == '\n') {
227 /* Ignore blank lines */
228 continue;
229 }
230 } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
231 while (++fp < filesize && filebuf[fp] != '\n')
232 continue;
233 } else {
234 fprintf(stderr, "The environment file is too large for the target environment storage\n");
235 return EXIT_FAILURE;
236 }
237 }
David Wagnera6337e62011-09-26 03:26:34 +0000238 /*
239 * Make sure there is a final '\0'
240 * And do it again on the next byte to mark the end of the environment.
241 */
242 if (envptr[ep-1] != '\0') {
243 envptr[ep++] = '\0';
244 /*
245 * The text file doesn't have an ending newline. We need to
246 * check the env size again to make sure we have room for two \0
247 */
248 if (ep >= envsize) {
David Wagner8a1b8fc2012-01-13 13:27:34 +0000249 fprintf(stderr, "The environment file is too large for the target environment storage\n");
David Wagnera6337e62011-09-26 03:26:34 +0000250 return EXIT_FAILURE;
251 }
252 envptr[ep] = '\0';
253 } else {
254 envptr[ep] = '\0';
255 }
256
257 /* Computes the CRC and put it at the beginning of the data */
258 crc = crc32(0, envptr, envsize);
259 targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
260
David Wagnerd8d26592012-01-13 13:27:40 +0000261 memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
262 if (redundant)
263 dataptr[sizeof(targetendian_crc)] = 1;
David Wagnera6337e62011-09-26 03:26:34 +0000264
David Wagner48995b52012-01-13 13:27:37 +0000265 if (!bin_filename || strcmp(bin_filename, "-") == 0) {
266 bin_fd = STDOUT_FILENO;
267 } else {
Mike Frysinger4f7136e2012-07-18 16:59:45 +0000268 bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
269 S_IWGRP);
David Wagner48995b52012-01-13 13:27:37 +0000270 if (bin_fd == -1) {
271 fprintf(stderr, "Can't open output file \"%s\": %s\n",
272 bin_filename, strerror(errno));
273 return EXIT_FAILURE;
274 }
David Wagnera6337e62011-09-26 03:26:34 +0000275 }
276
277 if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
278 sizeof(*dataptr) * datasize) {
279 fprintf(stderr, "write() failed: %s\n", strerror(errno));
280 return EXIT_FAILURE;
281 }
282
283 ret = close(bin_fd);
284
285 return ret;
286}