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