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 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 28 | /* We want the GNU version of basename() */ |
| 29 | #define _GNU_SOURCE |
| 30 | |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <stdio.h> |
David Wagner | d1acdae | 2012-01-13 13:27:35 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 35 | #include <stdint.h> |
| 36 | #include <string.h> |
| 37 | #include <unistd.h> |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
| 39 | #include <sys/stat.h> |
| 40 | |
David Wagner | d1acdae | 2012-01-13 13:27:35 +0000 | [diff] [blame] | 41 | #include "compiler.h" |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 42 | #include <u-boot/crc.h> |
Horst Kronstorfer | 1895420 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 43 | #include <version.h> |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 44 | |
| 45 | #define CRC_SIZE sizeof(uint32_t) |
| 46 | |
| 47 | static void usage(const char *exec_name) |
| 48 | { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 49 | 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] | 50 | "\n" |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 51 | "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] | 52 | "\n" |
| 53 | "\tThe input file is in format:\n" |
| 54 | "\t\tkey1=value1\n" |
| 55 | "\t\tkey2=value2\n" |
| 56 | "\t\t...\n" |
| 57 | "\t-r : the environment has multiple copies in flash\n" |
| 58 | "\t-b : the target is big endian (default is little endian)\n" |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 59 | "\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] | 60 | "\t-V : print version information and exit\n" |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 61 | "\n" |
| 62 | "If the input file is \"-\", data is read from standard input\n", |
| 63 | exec_name); |
| 64 | } |
| 65 | |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame^] | 66 | long int xstrtol(const char *s) |
| 67 | { |
| 68 | long int tmp; |
| 69 | |
| 70 | errno = 0; |
| 71 | tmp = strtol(s, NULL, 0); |
| 72 | if (!errno) |
| 73 | return tmp; |
| 74 | |
| 75 | if (errno == ERANGE) |
| 76 | fprintf(stderr, "Bad integer format: %s\n", s); |
| 77 | else |
| 78 | fprintf(stderr, "Error while parsing %s: %s\n", s, |
| 79 | strerror(errno)); |
| 80 | |
| 81 | exit(EXIT_FAILURE); |
| 82 | } |
| 83 | |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 84 | int main(int argc, char **argv) |
| 85 | { |
| 86 | uint32_t crc, targetendian_crc; |
| 87 | const char *txt_filename = NULL, *bin_filename = NULL; |
| 88 | int txt_fd, bin_fd; |
| 89 | unsigned char *dataptr, *envptr; |
| 90 | unsigned char *filebuf = NULL; |
| 91 | unsigned int filesize = 0, envsize = 0, datasize = 0; |
| 92 | int bigendian = 0; |
| 93 | int redundant = 0; |
| 94 | unsigned char padbyte = 0xff; |
| 95 | |
| 96 | int option; |
| 97 | int ret = EXIT_SUCCESS; |
| 98 | |
| 99 | struct stat txt_file_stat; |
| 100 | |
| 101 | int fp, ep; |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 102 | const char *prg; |
| 103 | |
| 104 | prg = basename(argv[0]); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 105 | |
Horst Kronstorfer | 5e0c63e | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 106 | /* Turn off getopt()'s internal error message */ |
| 107 | opterr = 0; |
| 108 | |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 109 | /* Parse the cmdline */ |
Horst Kronstorfer | 1895420 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 110 | while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) { |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 111 | switch (option) { |
| 112 | case 's': |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame^] | 113 | datasize = xstrtol(optarg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 114 | break; |
| 115 | case 'o': |
| 116 | bin_filename = strdup(optarg); |
| 117 | if (!bin_filename) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 118 | fprintf(stderr, "Can't strdup() the output filename\n"); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 119 | return EXIT_FAILURE; |
| 120 | } |
| 121 | break; |
| 122 | case 'r': |
| 123 | redundant = 1; |
| 124 | break; |
| 125 | case 'b': |
| 126 | bigendian = 1; |
| 127 | break; |
| 128 | case 'p': |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame^] | 129 | padbyte = xstrtol(optarg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 130 | break; |
| 131 | case 'h': |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 132 | usage(prg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 133 | return EXIT_SUCCESS; |
Horst Kronstorfer | 1895420 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 134 | case 'V': |
| 135 | printf("%s version %s\n", prg, PLAIN_VERSION); |
| 136 | return EXIT_SUCCESS; |
Horst Kronstorfer | 5e0c63e | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 137 | case ':': |
| 138 | fprintf(stderr, "Missing argument for option -%c\n", |
Horst Kronstorfer | 72ebafb | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 139 | optopt); |
Wolfgang Denk | e758a5c | 2012-03-01 21:19:40 +0000 | [diff] [blame] | 140 | usage(prg); |
Horst Kronstorfer | 5e0c63e | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 141 | return EXIT_FAILURE; |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 142 | default: |
Horst Kronstorfer | 72ebafb | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 143 | fprintf(stderr, "Wrong option -%c\n", optopt); |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 144 | usage(prg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 145 | return EXIT_FAILURE; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* Check datasize and allocate the data */ |
| 150 | if (datasize == 0) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 151 | fprintf(stderr, "Please specify the size of the environment partition.\n"); |
Horst Kronstorfer | af44f4b | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 152 | usage(prg); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 153 | return EXIT_FAILURE; |
| 154 | } |
| 155 | |
| 156 | dataptr = malloc(datasize * sizeof(*dataptr)); |
| 157 | if (!dataptr) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 158 | fprintf(stderr, "Can't alloc %d bytes for dataptr.\n", |
| 159 | datasize); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 160 | return EXIT_FAILURE; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * envptr points to the beginning of the actual environment (after the |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 165 | * crc and possible `redundant' byte |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 166 | */ |
| 167 | envsize = datasize - (CRC_SIZE + redundant); |
| 168 | envptr = dataptr + CRC_SIZE + redundant; |
| 169 | |
| 170 | /* Pad the environment with the padding byte */ |
| 171 | memset(envptr, padbyte, envsize); |
| 172 | |
| 173 | /* Open the input file ... */ |
| 174 | if (optind >= argc) { |
| 175 | fprintf(stderr, "Please specify an input filename\n"); |
| 176 | return EXIT_FAILURE; |
| 177 | } |
| 178 | |
| 179 | txt_filename = argv[optind]; |
| 180 | if (strcmp(txt_filename, "-") == 0) { |
| 181 | int readbytes = 0; |
| 182 | int readlen = sizeof(*envptr) * 2048; |
| 183 | txt_fd = STDIN_FILENO; |
| 184 | |
| 185 | do { |
| 186 | filebuf = realloc(filebuf, readlen); |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame^] | 187 | if (!filebuf) { |
| 188 | fprintf(stderr, "Can't realloc memory for the input file buffer\n"); |
| 189 | return EXIT_FAILURE; |
| 190 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 191 | readbytes = read(txt_fd, filebuf + filesize, readlen); |
David Wagner | 3d0f9bd | 2012-01-13 13:27:36 +0000 | [diff] [blame^] | 192 | if (errno) { |
| 193 | fprintf(stderr, "Error while reading stdin: %s\n", |
| 194 | strerror(errno)); |
| 195 | return EXIT_FAILURE; |
| 196 | } |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 197 | filesize += readbytes; |
| 198 | } while (readbytes == readlen); |
| 199 | |
| 200 | } else { |
| 201 | txt_fd = open(txt_filename, O_RDONLY); |
| 202 | if (txt_fd == -1) { |
| 203 | fprintf(stderr, "Can't open \"%s\": %s\n", |
| 204 | txt_filename, strerror(errno)); |
| 205 | return EXIT_FAILURE; |
| 206 | } |
| 207 | /* ... and check it */ |
| 208 | ret = fstat(txt_fd, &txt_file_stat); |
| 209 | if (ret == -1) { |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 210 | fprintf(stderr, "Can't stat() on \"%s\": %s\n", |
| 211 | txt_filename, strerror(errno)); |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 212 | return EXIT_FAILURE; |
| 213 | } |
| 214 | |
| 215 | filesize = txt_file_stat.st_size; |
| 216 | /* Read the raw input file and transform it */ |
| 217 | filebuf = malloc(sizeof(*envptr) * filesize); |
| 218 | ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize); |
| 219 | if (ret != sizeof(*envptr) * filesize) { |
| 220 | fprintf(stderr, "Can't read the whole input file\n"); |
| 221 | return EXIT_FAILURE; |
| 222 | } |
| 223 | ret = close(txt_fd); |
| 224 | } |
David Wagner | 8a1b8fc | 2012-01-13 13:27:34 +0000 | [diff] [blame] | 225 | /* The +1 is for the additionnal ending \0. See below. */ |
| 226 | if (filesize + 1 > envsize) { |
| 227 | 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] | 228 | return EXIT_FAILURE; |
| 229 | } |
| 230 | |
| 231 | /* Replace newlines separating variables with \0 */ |
| 232 | for (fp = 0, ep = 0 ; fp < filesize ; fp++) { |
| 233 | if (filebuf[fp] == '\n') { |
David Wagner | dbee61d | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 234 | if (ep == 0) { |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 235 | /* |
David Wagner | dbee61d | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 236 | * Newlines at the beginning of the file ? |
| 237 | * Ignore them. |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 238 | */ |
| 239 | continue; |
| 240 | } else if (filebuf[fp-1] == '\\') { |
| 241 | /* |
| 242 | * Embedded newline in a variable. |
| 243 | * |
David Wagner | dbee61d | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 244 | * The backslash was added to the envptr; rewind |
| 245 | * and replace it with a newline |
David Wagner | a6337e6 | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 246 | */ |
| 247 | ep--; |
| 248 | envptr[ep++] = '\n'; |
| 249 | } else { |
| 250 | /* End of a variable */ |
| 251 | envptr[ep++] = '\0'; |
| 252 | } |
| 253 | } else if (filebuf[fp] == '#') { |
| 254 | if (fp != 0 && filebuf[fp-1] == '\n') { |
| 255 | /* This line is a comment, let's skip it */ |
| 256 | while (fp < txt_file_stat.st_size && fp++ && |
| 257 | filebuf[fp] != '\n'); |
| 258 | } else { |
| 259 | envptr[ep++] = filebuf[fp]; |
| 260 | } |
| 261 | } else { |
| 262 | envptr[ep++] = filebuf[fp]; |
| 263 | } |
| 264 | } |
| 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 | |
| 288 | memcpy(dataptr, &targetendian_crc, sizeof(uint32_t)); |
| 289 | |
| 290 | bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 291 | if (bin_fd == -1) { |
| 292 | fprintf(stderr, "Can't open output file \"%s\": %s\n", |
| 293 | bin_filename, strerror(errno)); |
| 294 | return EXIT_FAILURE; |
| 295 | } |
| 296 | |
| 297 | if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) != |
| 298 | sizeof(*dataptr) * datasize) { |
| 299 | fprintf(stderr, "write() failed: %s\n", strerror(errno)); |
| 300 | return EXIT_FAILURE; |
| 301 | } |
| 302 | |
| 303 | ret = close(bin_fd); |
| 304 | |
| 305 | return ret; |
| 306 | } |