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