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