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