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" |
| 40 | "\t-r : the environment has multiple copies in flash\n" |
| 41 | "\t-b : the target is big endian (default is little endian)\n" |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 42 | "\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] | 43 | "\t-V : print version information and exit\n" |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 44 | "\n" |
| 45 | "If the input file is \"-\", data is read from standard input\n", |
| 46 | exec_name); |
| 47 | } |
| 48 | |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame] | 49 | long int xstrtol(const char *s) |
| 50 | { |
| 51 | long int tmp; |
| 52 | |
| 53 | errno = 0; |
| 54 | tmp = strtol(s, NULL, 0); |
| 55 | if (!errno) |
| 56 | return tmp; |
| 57 | |
| 58 | if (errno == ERANGE) |
| 59 | fprintf(stderr, "Bad integer format: %s\n", s); |
| 60 | else |
| 61 | fprintf(stderr, "Error while parsing %s: %s\n", s, |
| 62 | strerror(errno)); |
| 63 | |
| 64 | exit(EXIT_FAILURE); |
| 65 | } |
| 66 | |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 67 | int main(int argc, char **argv) |
| 68 | { |
| 69 | uint32_t crc, targetendian_crc; |
| 70 | const char *txt_filename = NULL, *bin_filename = NULL; |
| 71 | int txt_fd, bin_fd; |
| 72 | unsigned char *dataptr, *envptr; |
| 73 | unsigned char *filebuf = NULL; |
| 74 | unsigned int filesize = 0, envsize = 0, datasize = 0; |
| 75 | int bigendian = 0; |
| 76 | int redundant = 0; |
| 77 | unsigned char padbyte = 0xff; |
| 78 | |
| 79 | int option; |
| 80 | int ret = EXIT_SUCCESS; |
| 81 | |
| 82 | struct stat txt_file_stat; |
| 83 | |
| 84 | int fp, ep; |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 85 | const char *prg; |
| 86 | |
| 87 | prg = basename(argv[0]); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 88 | |
Horst Kronstorfer | 5e0c63e | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 89 | /* Turn off getopt()'s internal error message */ |
| 90 | opterr = 0; |
| 91 | |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 92 | /* Parse the cmdline */ |
Horst Kronstorfer | 1895420 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 93 | while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) { |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 94 | switch (option) { |
| 95 | case 's': |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame] | 96 | datasize = xstrtol(optarg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 97 | break; |
| 98 | case 'o': |
| 99 | bin_filename = strdup(optarg); |
| 100 | if (!bin_filename) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 101 | fprintf(stderr, "Can't strdup() the output filename\n"); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 102 | return EXIT_FAILURE; |
| 103 | } |
| 104 | break; |
| 105 | case 'r': |
| 106 | redundant = 1; |
| 107 | break; |
| 108 | case 'b': |
| 109 | bigendian = 1; |
| 110 | break; |
| 111 | case 'p': |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame] | 112 | padbyte = xstrtol(optarg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 113 | break; |
| 114 | case 'h': |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 115 | usage(prg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 116 | return EXIT_SUCCESS; |
Horst Kronstorfer | 1895420 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 117 | case 'V': |
| 118 | printf("%s version %s\n", prg, PLAIN_VERSION); |
| 119 | return EXIT_SUCCESS; |
Horst Kronstorfer | 5e0c63e | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 120 | case ':': |
| 121 | fprintf(stderr, "Missing argument for option -%c\n", |
Horst Kronstorfer | 72ebafb | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 122 | optopt); |
Wolfgang Denk | e758a5c | 2012-03-01 21:19:40 +0000 | [diff] [blame] | 123 | usage(prg); |
Horst Kronstorfer | 5e0c63e | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 124 | return EXIT_FAILURE; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 125 | default: |
Horst Kronstorfer | 72ebafb | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 126 | fprintf(stderr, "Wrong option -%c\n", optopt); |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 127 | usage(prg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 128 | return EXIT_FAILURE; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* Check datasize and allocate the data */ |
| 133 | if (datasize == 0) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 134 | fprintf(stderr, "Please specify the size of the environment partition.\n"); |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 135 | usage(prg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 136 | return EXIT_FAILURE; |
| 137 | } |
| 138 | |
| 139 | dataptr = malloc(datasize * sizeof(*dataptr)); |
| 140 | if (!dataptr) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 141 | fprintf(stderr, "Can't alloc %d bytes for dataptr.\n", |
| 142 | datasize); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 143 | return EXIT_FAILURE; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * envptr points to the beginning of the actual environment (after the |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 148 | * crc and possible `redundant' byte |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 149 | */ |
| 150 | envsize = datasize - (CRC_SIZE + redundant); |
| 151 | envptr = dataptr + CRC_SIZE + redundant; |
| 152 | |
| 153 | /* Pad the environment with the padding byte */ |
| 154 | memset(envptr, padbyte, envsize); |
| 155 | |
| 156 | /* Open the input file ... */ |
David Wagner | 48995b5 | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 157 | if (optind >= argc || strcmp(argv[optind], "-") == 0) { |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 158 | int readbytes = 0; |
David Wagner | 48995b5 | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 159 | int readlen = sizeof(*envptr) * 4096; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 160 | txt_fd = STDIN_FILENO; |
| 161 | |
| 162 | do { |
| 163 | filebuf = realloc(filebuf, readlen); |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame] | 164 | if (!filebuf) { |
| 165 | fprintf(stderr, "Can't realloc memory for the input file buffer\n"); |
| 166 | return EXIT_FAILURE; |
| 167 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 168 | readbytes = read(txt_fd, filebuf + filesize, readlen); |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame] | 169 | if (errno) { |
| 170 | fprintf(stderr, "Error while reading stdin: %s\n", |
| 171 | strerror(errno)); |
| 172 | return EXIT_FAILURE; |
| 173 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 174 | filesize += readbytes; |
| 175 | } while (readbytes == readlen); |
| 176 | |
| 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 | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 215 | /* The +1 is for the additionnal ending \0. See below. */ |
| 216 | if (filesize + 1 > envsize) { |
| 217 | 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] | 218 | return EXIT_FAILURE; |
| 219 | } |
| 220 | |
| 221 | /* Replace newlines separating variables with \0 */ |
| 222 | for (fp = 0, ep = 0 ; fp < filesize ; fp++) { |
| 223 | if (filebuf[fp] == '\n') { |
David Wagner | dbee61d | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 224 | if (ep == 0) { |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 225 | /* |
David Wagner | dbee61d | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 226 | * Newlines at the beginning of the file ? |
| 227 | * Ignore them. |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 228 | */ |
| 229 | continue; |
| 230 | } else if (filebuf[fp-1] == '\\') { |
| 231 | /* |
| 232 | * Embedded newline in a variable. |
| 233 | * |
David Wagner | dbee61d | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 234 | * The backslash was added to the envptr; rewind |
| 235 | * and replace it with a newline |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 236 | */ |
| 237 | ep--; |
| 238 | envptr[ep++] = '\n'; |
| 239 | } else { |
| 240 | /* End of a variable */ |
| 241 | envptr[ep++] = '\0'; |
| 242 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 243 | } else { |
| 244 | envptr[ep++] = filebuf[fp]; |
| 245 | } |
| 246 | } |
| 247 | /* |
| 248 | * Make sure there is a final '\0' |
| 249 | * And do it again on the next byte to mark the end of the environment. |
| 250 | */ |
| 251 | if (envptr[ep-1] != '\0') { |
| 252 | envptr[ep++] = '\0'; |
| 253 | /* |
| 254 | * The text file doesn't have an ending newline. We need to |
| 255 | * check the env size again to make sure we have room for two \0 |
| 256 | */ |
| 257 | if (ep >= envsize) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 258 | 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] | 259 | return EXIT_FAILURE; |
| 260 | } |
| 261 | envptr[ep] = '\0'; |
| 262 | } else { |
| 263 | envptr[ep] = '\0'; |
| 264 | } |
| 265 | |
| 266 | /* Computes the CRC and put it at the beginning of the data */ |
| 267 | crc = crc32(0, envptr, envsize); |
| 268 | targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc); |
| 269 | |
David Wagner | d8d2659 | 2012-01-13 13:27:40 +0000 | [diff] [blame] | 270 | memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc)); |
| 271 | if (redundant) |
| 272 | dataptr[sizeof(targetendian_crc)] = 1; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 273 | |
David Wagner | 48995b5 | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 274 | if (!bin_filename || strcmp(bin_filename, "-") == 0) { |
| 275 | bin_fd = STDOUT_FILENO; |
| 276 | } else { |
Mike Frysinger | 4f7136e | 2012-07-18 16:59:45 +0000 | [diff] [blame] | 277 | bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | |
| 278 | S_IWGRP); |
David Wagner | 48995b5 | 2012-01-13 13:27:37 +0000 | [diff] [blame] | 279 | if (bin_fd == -1) { |
| 280 | fprintf(stderr, "Can't open output file \"%s\": %s\n", |
| 281 | bin_filename, strerror(errno)); |
| 282 | return EXIT_FAILURE; |
| 283 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) != |
| 287 | sizeof(*dataptr) * datasize) { |
| 288 | fprintf(stderr, "write() failed: %s\n", strerror(errno)); |
| 289 | return EXIT_FAILURE; |
| 290 | } |
| 291 | |
| 292 | ret = close(bin_fd); |
| 293 | |
| 294 | return ret; |
| 295 | } |