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