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