Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
David Wagner | 65a7f2d | 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 | 65a7f2d | 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 | 3f923cb | 2012-01-13 13:27:35 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 15 | #include <stdint.h> |
| 16 | #include <string.h> |
| 17 | #include <unistd.h> |
Andreas Bießmann | 03ddd4d | 2012-06-28 08:01:58 +0200 | [diff] [blame] | 18 | #include <libgen.h> |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
David Wagner | 957ec69 | 2012-01-13 13:27:38 +0000 | [diff] [blame] | 21 | #include <sys/mman.h> |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 22 | |
David Wagner | 3f923cb | 2012-01-13 13:27:35 +0000 | [diff] [blame] | 23 | #include "compiler.h" |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 24 | #include <u-boot/crc.h> |
Horst Kronstorfer | fa8df72 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 25 | #include <version.h> |
David Wagner | 65a7f2d | 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 | 7ddfb60 | 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 | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 32 | "\n" |
David Wagner | 7ddfb60 | 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 | 65a7f2d | 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 | 21f0dbe | 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 | 65a7f2d | 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 | 7ddfb60 | 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 | fa8df72 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 44 | "\t-V : print version information and exit\n" |
David Wagner | 65a7f2d | 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 | 111d950 | 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 | |
Andre Przywara | 871820d | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 68 | #define CHUNK_SIZE 4096 |
| 69 | |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 70 | int main(int argc, char **argv) |
| 71 | { |
| 72 | uint32_t crc, targetendian_crc; |
Andre Przywara | 871820d | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 73 | const char *bin_filename = NULL; |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 74 | int txt_fd, bin_fd; |
| 75 | unsigned char *dataptr, *envptr; |
| 76 | unsigned char *filebuf = NULL; |
| 77 | unsigned int filesize = 0, envsize = 0, datasize = 0; |
| 78 | int bigendian = 0; |
| 79 | int redundant = 0; |
| 80 | unsigned char padbyte = 0xff; |
Andre Przywara | 871820d | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 81 | int readbytes = 0; |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 82 | |
| 83 | int option; |
| 84 | int ret = EXIT_SUCCESS; |
| 85 | |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 86 | int fp, ep; |
Horst Kronstorfer | a0c8290 | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 87 | const char *prg; |
| 88 | |
| 89 | prg = basename(argv[0]); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 90 | |
Horst Kronstorfer | ab6d249 | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 91 | /* Turn off getopt()'s internal error message */ |
| 92 | opterr = 0; |
| 93 | |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 94 | /* Parse the cmdline */ |
Horst Kronstorfer | fa8df72 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 95 | while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) { |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 96 | switch (option) { |
| 97 | case 's': |
David Wagner | 111d950 | 2012-01-13 13:27:36 +0000 | [diff] [blame] | 98 | datasize = xstrtol(optarg); |
David Wagner | 65a7f2d | 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 | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 103 | fprintf(stderr, "Can't strdup() the output filename\n"); |
David Wagner | 65a7f2d | 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 | 111d950 | 2012-01-13 13:27:36 +0000 | [diff] [blame] | 114 | padbyte = xstrtol(optarg); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 115 | break; |
| 116 | case 'h': |
Horst Kronstorfer | a0c8290 | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 117 | usage(prg); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 118 | return EXIT_SUCCESS; |
Horst Kronstorfer | fa8df72 | 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 | ab6d249 | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 122 | case ':': |
| 123 | fprintf(stderr, "Missing argument for option -%c\n", |
Horst Kronstorfer | 405bfbc | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 124 | optopt); |
Wolfgang Denk | a23b7de | 2012-03-01 21:19:40 +0000 | [diff] [blame] | 125 | usage(prg); |
Horst Kronstorfer | ab6d249 | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 126 | return EXIT_FAILURE; |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 127 | default: |
Horst Kronstorfer | 405bfbc | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 128 | fprintf(stderr, "Wrong option -%c\n", optopt); |
Horst Kronstorfer | a0c8290 | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 129 | usage(prg); |
David Wagner | 65a7f2d | 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 | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 136 | fprintf(stderr, "Please specify the size of the environment partition.\n"); |
Horst Kronstorfer | a0c8290 | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 137 | usage(prg); |
David Wagner | 65a7f2d | 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 | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 143 | fprintf(stderr, "Can't alloc %d bytes for dataptr.\n", |
| 144 | datasize); |
David Wagner | 65a7f2d | 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 | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 150 | * crc and possible `redundant' byte |
David Wagner | 65a7f2d | 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 | bc3177a | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 159 | if (optind >= argc || strcmp(argv[optind], "-") == 0) { |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 160 | txt_fd = STDIN_FILENO; |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 161 | } else { |
Andre Przywara | 871820d | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 162 | txt_fd = open(argv[optind], O_RDONLY); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 163 | if (txt_fd == -1) { |
| 164 | fprintf(stderr, "Can't open \"%s\": %s\n", |
Andre Przywara | 871820d | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 165 | argv[optind], strerror(errno)); |
| 166 | return EXIT_FAILURE; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | do { |
| 171 | filebuf = realloc(filebuf, filesize + CHUNK_SIZE); |
| 172 | if (!filebuf) { |
| 173 | fprintf(stderr, "Can't realloc memory for the input file buffer\n"); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 174 | return EXIT_FAILURE; |
| 175 | } |
Andre Przywara | 871820d | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 176 | readbytes = read(txt_fd, filebuf + filesize, CHUNK_SIZE); |
| 177 | if (readbytes < 0) { |
| 178 | fprintf(stderr, "Error while reading: %s\n", |
| 179 | strerror(errno)); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 180 | return EXIT_FAILURE; |
| 181 | } |
Andre Przywara | 871820d | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 182 | filesize += readbytes; |
| 183 | } while (readbytes > 0); |
David Wagner | 957ec69 | 2012-01-13 13:27:38 +0000 | [diff] [blame] | 184 | |
Andre Przywara | 871820d | 2019-06-30 02:45:01 +0100 | [diff] [blame] | 185 | if (txt_fd != STDIN_FILENO) |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 186 | ret = close(txt_fd); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 187 | |
Brian McFarland | 7cf7e13 | 2015-03-12 11:52:49 -0400 | [diff] [blame] | 188 | /* Parse a byte at time until reaching the file OR until the environment fills |
| 189 | * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */ |
| 190 | for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) { |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 191 | if (filebuf[fp] == '\n') { |
Dominik Muth | 21f0dbe | 2014-08-28 12:25:27 +0200 | [diff] [blame] | 192 | if (fp == 0 || filebuf[fp-1] == '\n') { |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 193 | /* |
Dominik Muth | 21f0dbe | 2014-08-28 12:25:27 +0200 | [diff] [blame] | 194 | * Skip empty lines. |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 195 | */ |
| 196 | continue; |
| 197 | } else if (filebuf[fp-1] == '\\') { |
| 198 | /* |
| 199 | * Embedded newline in a variable. |
| 200 | * |
David Wagner | dafad3c | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 201 | * The backslash was added to the envptr; rewind |
| 202 | * and replace it with a newline |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 203 | */ |
| 204 | ep--; |
| 205 | envptr[ep++] = '\n'; |
| 206 | } else { |
| 207 | /* End of a variable */ |
| 208 | envptr[ep++] = '\0'; |
| 209 | } |
Dominik Muth | 21f0dbe | 2014-08-28 12:25:27 +0200 | [diff] [blame] | 210 | } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') { |
| 211 | /* Comment, skip the line. */ |
| 212 | while (++fp < filesize && filebuf[fp] != '\n') |
| 213 | continue; |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 214 | } else { |
| 215 | envptr[ep++] = filebuf[fp]; |
| 216 | } |
| 217 | } |
Brian McFarland | 7cf7e13 | 2015-03-12 11:52:49 -0400 | [diff] [blame] | 218 | /* If there are more bytes in the file still, it means the env filled up |
| 219 | * before parsing the whole file. Eat comments & whitespace here to see if |
| 220 | * there was anything meaning full left in the file, and if so, throw a error |
| 221 | * and exit. */ |
| 222 | for( ; fp < filesize; fp++ ) |
| 223 | { |
| 224 | if (filebuf[fp] == '\n') { |
| 225 | if (fp == 0 || filebuf[fp-1] == '\n') { |
| 226 | /* Ignore blank lines */ |
| 227 | continue; |
| 228 | } |
| 229 | } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') { |
| 230 | while (++fp < filesize && filebuf[fp] != '\n') |
| 231 | continue; |
| 232 | } else { |
| 233 | fprintf(stderr, "The environment file is too large for the target environment storage\n"); |
| 234 | return EXIT_FAILURE; |
| 235 | } |
| 236 | } |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 237 | /* |
| 238 | * Make sure there is a final '\0' |
| 239 | * And do it again on the next byte to mark the end of the environment. |
| 240 | */ |
| 241 | if (envptr[ep-1] != '\0') { |
| 242 | envptr[ep++] = '\0'; |
| 243 | /* |
| 244 | * The text file doesn't have an ending newline. We need to |
| 245 | * check the env size again to make sure we have room for two \0 |
| 246 | */ |
| 247 | if (ep >= envsize) { |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 248 | 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] | 249 | return EXIT_FAILURE; |
| 250 | } |
| 251 | envptr[ep] = '\0'; |
| 252 | } else { |
| 253 | envptr[ep] = '\0'; |
| 254 | } |
| 255 | |
| 256 | /* Computes the CRC and put it at the beginning of the data */ |
| 257 | crc = crc32(0, envptr, envsize); |
| 258 | targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc); |
| 259 | |
David Wagner | 99cae31 | 2012-01-13 13:27:40 +0000 | [diff] [blame] | 260 | memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc)); |
| 261 | if (redundant) |
| 262 | dataptr[sizeof(targetendian_crc)] = 1; |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 263 | |
David Wagner | bc3177a | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 264 | if (!bin_filename || strcmp(bin_filename, "-") == 0) { |
| 265 | bin_fd = STDOUT_FILENO; |
| 266 | } else { |
Mike Frysinger | 92e144f | 2012-07-18 16:59:45 +0000 | [diff] [blame] | 267 | bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | |
| 268 | S_IWGRP); |
David Wagner | bc3177a | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 269 | if (bin_fd == -1) { |
| 270 | fprintf(stderr, "Can't open output file \"%s\": %s\n", |
| 271 | bin_filename, strerror(errno)); |
| 272 | return EXIT_FAILURE; |
| 273 | } |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) != |
| 277 | sizeof(*dataptr) * datasize) { |
| 278 | fprintf(stderr, "write() failed: %s\n", strerror(errno)); |
| 279 | return EXIT_FAILURE; |
| 280 | } |
| 281 | |
| 282 | ret = close(bin_fd); |
| 283 | |
| 284 | return ret; |
| 285 | } |