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