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