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> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 17 | #include <u-boot/crc.h> |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 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 | |
Andre Przywara | 849f9be | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 69 | #define CHUNK_SIZE 4096 |
| 70 | |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 71 | int main(int argc, char **argv) |
| 72 | { |
| 73 | uint32_t crc, targetendian_crc; |
Andre Przywara | 849f9be | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 74 | const char *bin_filename = NULL; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 75 | int txt_fd, bin_fd; |
| 76 | unsigned char *dataptr, *envptr; |
| 77 | unsigned char *filebuf = NULL; |
| 78 | unsigned int filesize = 0, envsize = 0, datasize = 0; |
| 79 | int bigendian = 0; |
| 80 | int redundant = 0; |
| 81 | unsigned char padbyte = 0xff; |
Andre Przywara | 849f9be | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 82 | int readbytes = 0; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 83 | |
| 84 | int option; |
| 85 | int ret = EXIT_SUCCESS; |
| 86 | |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 87 | int fp, ep; |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 88 | const char *prg; |
| 89 | |
| 90 | prg = basename(argv[0]); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 91 | |
Horst Kronstorfer | 5e0c63e | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 92 | /* Turn off getopt()'s internal error message */ |
| 93 | opterr = 0; |
| 94 | |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 95 | /* Parse the cmdline */ |
Horst Kronstorfer | 1895420 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 96 | while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) { |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 97 | switch (option) { |
| 98 | case 's': |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame] | 99 | datasize = xstrtol(optarg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 100 | break; |
| 101 | case 'o': |
| 102 | bin_filename = strdup(optarg); |
| 103 | if (!bin_filename) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 104 | fprintf(stderr, "Can't strdup() the output filename\n"); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 105 | return EXIT_FAILURE; |
| 106 | } |
| 107 | break; |
| 108 | case 'r': |
| 109 | redundant = 1; |
| 110 | break; |
| 111 | case 'b': |
| 112 | bigendian = 1; |
| 113 | break; |
| 114 | case 'p': |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame] | 115 | padbyte = xstrtol(optarg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 116 | break; |
| 117 | case 'h': |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 118 | usage(prg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 119 | return EXIT_SUCCESS; |
Horst Kronstorfer | 1895420 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 120 | case 'V': |
| 121 | printf("%s version %s\n", prg, PLAIN_VERSION); |
| 122 | return EXIT_SUCCESS; |
Horst Kronstorfer | 5e0c63e | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 123 | case ':': |
| 124 | fprintf(stderr, "Missing argument for option -%c\n", |
Horst Kronstorfer | 72ebafb | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 125 | optopt); |
Wolfgang Denk | e758a5c | 2012-03-01 21:19:40 +0000 | [diff] [blame] | 126 | usage(prg); |
Horst Kronstorfer | 5e0c63e | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 127 | return EXIT_FAILURE; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 128 | default: |
Horst Kronstorfer | 72ebafb | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 129 | fprintf(stderr, "Wrong option -%c\n", optopt); |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 130 | usage(prg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 131 | return EXIT_FAILURE; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* Check datasize and allocate the data */ |
| 136 | if (datasize == 0) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 137 | fprintf(stderr, "Please specify the size of the environment partition.\n"); |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 138 | usage(prg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 139 | return EXIT_FAILURE; |
| 140 | } |
| 141 | |
| 142 | dataptr = malloc(datasize * sizeof(*dataptr)); |
| 143 | if (!dataptr) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 144 | fprintf(stderr, "Can't alloc %d bytes for dataptr.\n", |
| 145 | datasize); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 146 | return EXIT_FAILURE; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * envptr points to the beginning of the actual environment (after the |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 151 | * crc and possible `redundant' byte |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 152 | */ |
| 153 | envsize = datasize - (CRC_SIZE + redundant); |
| 154 | envptr = dataptr + CRC_SIZE + redundant; |
| 155 | |
| 156 | /* Pad the environment with the padding byte */ |
| 157 | memset(envptr, padbyte, envsize); |
| 158 | |
| 159 | /* Open the input file ... */ |
David Wagner | 48995b5 | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 160 | if (optind >= argc || strcmp(argv[optind], "-") == 0) { |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 161 | txt_fd = STDIN_FILENO; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 162 | } else { |
Andre Przywara | 849f9be | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 163 | txt_fd = open(argv[optind], O_RDONLY); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 164 | if (txt_fd == -1) { |
| 165 | fprintf(stderr, "Can't open \"%s\": %s\n", |
Andre Przywara | 849f9be | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 166 | argv[optind], strerror(errno)); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 167 | return EXIT_FAILURE; |
| 168 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 169 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 170 | |
Andre Przywara | 849f9be | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 171 | do { |
| 172 | filebuf = realloc(filebuf, filesize + CHUNK_SIZE); |
| 173 | if (!filebuf) { |
| 174 | fprintf(stderr, "Can't realloc memory for the input file buffer\n"); |
| 175 | return EXIT_FAILURE; |
| 176 | } |
| 177 | readbytes = read(txt_fd, filebuf + filesize, CHUNK_SIZE); |
| 178 | if (readbytes < 0) { |
| 179 | fprintf(stderr, "Error while reading: %s\n", |
| 180 | strerror(errno)); |
| 181 | return EXIT_FAILURE; |
| 182 | } |
| 183 | filesize += readbytes; |
| 184 | } while (readbytes > 0); |
| 185 | |
| 186 | if (txt_fd != STDIN_FILENO) |
| 187 | ret = close(txt_fd); |
| 188 | |
Brian McFarland | 80ee019 | 2015-03-12 11:52:49 -0400 | [diff] [blame] | 189 | /* Parse a byte at time until reaching the file OR until the environment fills |
| 190 | * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */ |
| 191 | for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) { |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 192 | if (filebuf[fp] == '\n') { |
Dominik Muth | e72be89 | 2014-08-28 12:25:27 +0200 | [diff] [blame] | 193 | if (fp == 0 || filebuf[fp-1] == '\n') { |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 194 | /* |
Dominik Muth | e72be89 | 2014-08-28 12:25:27 +0200 | [diff] [blame] | 195 | * Skip empty lines. |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 196 | */ |
| 197 | continue; |
| 198 | } else if (filebuf[fp-1] == '\\') { |
| 199 | /* |
| 200 | * Embedded newline in a variable. |
| 201 | * |
David Wagner | dbee61d | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 202 | * The backslash was added to the envptr; rewind |
| 203 | * and replace it with a newline |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 204 | */ |
| 205 | ep--; |
| 206 | envptr[ep++] = '\n'; |
| 207 | } else { |
| 208 | /* End of a variable */ |
| 209 | envptr[ep++] = '\0'; |
| 210 | } |
Dominik Muth | e72be89 | 2014-08-28 12:25:27 +0200 | [diff] [blame] | 211 | } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') { |
| 212 | /* Comment, skip the line. */ |
| 213 | while (++fp < filesize && filebuf[fp] != '\n') |
| 214 | continue; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 215 | } else { |
| 216 | envptr[ep++] = filebuf[fp]; |
| 217 | } |
| 218 | } |
Brian McFarland | 80ee019 | 2015-03-12 11:52:49 -0400 | [diff] [blame] | 219 | /* If there are more bytes in the file still, it means the env filled up |
| 220 | * before parsing the whole file. Eat comments & whitespace here to see if |
| 221 | * there was anything meaning full left in the file, and if so, throw a error |
| 222 | * and exit. */ |
| 223 | for( ; fp < filesize; fp++ ) |
| 224 | { |
| 225 | if (filebuf[fp] == '\n') { |
| 226 | if (fp == 0 || filebuf[fp-1] == '\n') { |
| 227 | /* Ignore blank lines */ |
| 228 | continue; |
| 229 | } |
| 230 | } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') { |
| 231 | while (++fp < filesize && filebuf[fp] != '\n') |
| 232 | continue; |
| 233 | } else { |
| 234 | fprintf(stderr, "The environment file is too large for the target environment storage\n"); |
| 235 | return EXIT_FAILURE; |
| 236 | } |
| 237 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 238 | /* |
| 239 | * Make sure there is a final '\0' |
| 240 | * And do it again on the next byte to mark the end of the environment. |
| 241 | */ |
| 242 | if (envptr[ep-1] != '\0') { |
| 243 | envptr[ep++] = '\0'; |
| 244 | /* |
| 245 | * The text file doesn't have an ending newline. We need to |
| 246 | * check the env size again to make sure we have room for two \0 |
| 247 | */ |
| 248 | if (ep >= envsize) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 249 | 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] | 250 | return EXIT_FAILURE; |
| 251 | } |
| 252 | envptr[ep] = '\0'; |
| 253 | } else { |
| 254 | envptr[ep] = '\0'; |
| 255 | } |
| 256 | |
| 257 | /* Computes the CRC and put it at the beginning of the data */ |
| 258 | crc = crc32(0, envptr, envsize); |
| 259 | targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc); |
| 260 | |
David Wagner | d8d2659 | 2012-01-13 13:27:40 +0000 | [diff] [blame] | 261 | memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc)); |
| 262 | if (redundant) |
| 263 | dataptr[sizeof(targetendian_crc)] = 1; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 264 | |
David Wagner | 48995b5 | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 265 | if (!bin_filename || strcmp(bin_filename, "-") == 0) { |
| 266 | bin_fd = STDOUT_FILENO; |
| 267 | } else { |
Mike Frysinger | 4f7136e | 2012-07-18 16:59:45 +0000 | [diff] [blame] | 268 | bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | |
| 269 | S_IWGRP); |
David Wagner | 48995b5 | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 270 | if (bin_fd == -1) { |
| 271 | fprintf(stderr, "Can't open output file \"%s\": %s\n", |
| 272 | bin_filename, strerror(errno)); |
| 273 | return EXIT_FAILURE; |
| 274 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) != |
| 278 | sizeof(*dataptr) * datasize) { |
| 279 | fprintf(stderr, "write() failed: %s\n", strerror(errno)); |
| 280 | return EXIT_FAILURE; |
| 281 | } |
| 282 | |
| 283 | ret = close(bin_fd); |
| 284 | |
| 285 | return ret; |
| 286 | } |