David Wagner | 65a7f2d | 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 | a0c8290 | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 28 | /* We want the GNU version of basename() */ |
| 29 | #define _GNU_SOURCE |
| 30 | |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdint.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | #include <compiler.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/stat.h> |
| 40 | |
| 41 | #include <u-boot/crc.h> |
Horst Kronstorfer | fa8df72 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 42 | #include <version.h> |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 43 | |
| 44 | #define CRC_SIZE sizeof(uint32_t) |
| 45 | |
| 46 | static void usage(const char *exec_name) |
| 47 | { |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame^] | 48 | 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] | 49 | "\n" |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame^] | 50 | "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] | 51 | "\n" |
| 52 | "\tThe input file is in format:\n" |
| 53 | "\t\tkey1=value1\n" |
| 54 | "\t\tkey2=value2\n" |
| 55 | "\t\t...\n" |
| 56 | "\t-r : the environment has multiple copies in flash\n" |
| 57 | "\t-b : the target is big endian (default is little endian)\n" |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame^] | 58 | "\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] | 59 | "\t-V : print version information and exit\n" |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 60 | "\n" |
| 61 | "If the input file is \"-\", data is read from standard input\n", |
| 62 | exec_name); |
| 63 | } |
| 64 | |
| 65 | int main(int argc, char **argv) |
| 66 | { |
| 67 | uint32_t crc, targetendian_crc; |
| 68 | const char *txt_filename = NULL, *bin_filename = NULL; |
| 69 | int txt_fd, bin_fd; |
| 70 | unsigned char *dataptr, *envptr; |
| 71 | unsigned char *filebuf = NULL; |
| 72 | unsigned int filesize = 0, envsize = 0, datasize = 0; |
| 73 | int bigendian = 0; |
| 74 | int redundant = 0; |
| 75 | unsigned char padbyte = 0xff; |
| 76 | |
| 77 | int option; |
| 78 | int ret = EXIT_SUCCESS; |
| 79 | |
| 80 | struct stat txt_file_stat; |
| 81 | |
| 82 | int fp, ep; |
Horst Kronstorfer | a0c8290 | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 83 | const char *prg; |
| 84 | |
| 85 | prg = basename(argv[0]); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 86 | |
Horst Kronstorfer | ab6d249 | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 87 | /* Turn off getopt()'s internal error message */ |
| 88 | opterr = 0; |
| 89 | |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 90 | /* Parse the cmdline */ |
Horst Kronstorfer | fa8df72 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 91 | while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) { |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 92 | switch (option) { |
| 93 | case 's': |
| 94 | datasize = strtol(optarg, NULL, 0); |
| 95 | break; |
| 96 | case 'o': |
| 97 | bin_filename = strdup(optarg); |
| 98 | if (!bin_filename) { |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame^] | 99 | fprintf(stderr, "Can't strdup() the output filename\n"); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 100 | return EXIT_FAILURE; |
| 101 | } |
| 102 | break; |
| 103 | case 'r': |
| 104 | redundant = 1; |
| 105 | break; |
| 106 | case 'b': |
| 107 | bigendian = 1; |
| 108 | break; |
| 109 | case 'p': |
| 110 | padbyte = strtol(optarg, NULL, 0); |
| 111 | break; |
| 112 | case 'h': |
Horst Kronstorfer | a0c8290 | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 113 | usage(prg); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 114 | return EXIT_SUCCESS; |
Horst Kronstorfer | fa8df72 | 2011-12-05 00:55:26 +0000 | [diff] [blame] | 115 | case 'V': |
| 116 | printf("%s version %s\n", prg, PLAIN_VERSION); |
| 117 | return EXIT_SUCCESS; |
Horst Kronstorfer | ab6d249 | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 118 | case ':': |
| 119 | fprintf(stderr, "Missing argument for option -%c\n", |
Horst Kronstorfer | 405bfbc | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 120 | optopt); |
Wolfgang Denk | a23b7de | 2012-03-01 21:19:40 +0000 | [diff] [blame] | 121 | usage(prg); |
Horst Kronstorfer | ab6d249 | 2011-12-05 00:55:24 +0000 | [diff] [blame] | 122 | return EXIT_FAILURE; |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 123 | default: |
Horst Kronstorfer | 405bfbc | 2011-12-24 00:41:58 +0000 | [diff] [blame] | 124 | fprintf(stderr, "Wrong option -%c\n", optopt); |
Horst Kronstorfer | a0c8290 | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 125 | usage(prg); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 126 | return EXIT_FAILURE; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* Check datasize and allocate the data */ |
| 131 | if (datasize == 0) { |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame^] | 132 | fprintf(stderr, "Please specify the size of the environment partition.\n"); |
Horst Kronstorfer | a0c8290 | 2011-12-21 10:39:39 +0000 | [diff] [blame] | 133 | usage(prg); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 134 | return EXIT_FAILURE; |
| 135 | } |
| 136 | |
| 137 | dataptr = malloc(datasize * sizeof(*dataptr)); |
| 138 | if (!dataptr) { |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame^] | 139 | fprintf(stderr, "Can't alloc %d bytes for dataptr.\n", |
| 140 | datasize); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 141 | return EXIT_FAILURE; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * envptr points to the beginning of the actual environment (after the |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame^] | 146 | * crc and possible `redundant' byte |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 147 | */ |
| 148 | envsize = datasize - (CRC_SIZE + redundant); |
| 149 | envptr = dataptr + CRC_SIZE + redundant; |
| 150 | |
| 151 | /* Pad the environment with the padding byte */ |
| 152 | memset(envptr, padbyte, envsize); |
| 153 | |
| 154 | /* Open the input file ... */ |
| 155 | if (optind >= argc) { |
| 156 | fprintf(stderr, "Please specify an input filename\n"); |
| 157 | return EXIT_FAILURE; |
| 158 | } |
| 159 | |
| 160 | txt_filename = argv[optind]; |
| 161 | if (strcmp(txt_filename, "-") == 0) { |
| 162 | int readbytes = 0; |
| 163 | int readlen = sizeof(*envptr) * 2048; |
| 164 | txt_fd = STDIN_FILENO; |
| 165 | |
| 166 | do { |
| 167 | filebuf = realloc(filebuf, readlen); |
| 168 | readbytes = read(txt_fd, filebuf + filesize, readlen); |
| 169 | filesize += readbytes; |
| 170 | } while (readbytes == readlen); |
| 171 | |
| 172 | } else { |
| 173 | txt_fd = open(txt_filename, O_RDONLY); |
| 174 | if (txt_fd == -1) { |
| 175 | fprintf(stderr, "Can't open \"%s\": %s\n", |
| 176 | txt_filename, strerror(errno)); |
| 177 | return EXIT_FAILURE; |
| 178 | } |
| 179 | /* ... and check it */ |
| 180 | ret = fstat(txt_fd, &txt_file_stat); |
| 181 | if (ret == -1) { |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame^] | 182 | fprintf(stderr, "Can't stat() on \"%s\": %s\n", |
| 183 | txt_filename, strerror(errno)); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 184 | return EXIT_FAILURE; |
| 185 | } |
| 186 | |
| 187 | filesize = txt_file_stat.st_size; |
| 188 | /* Read the raw input file and transform it */ |
| 189 | filebuf = malloc(sizeof(*envptr) * filesize); |
| 190 | ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize); |
| 191 | if (ret != sizeof(*envptr) * filesize) { |
| 192 | fprintf(stderr, "Can't read the whole input file\n"); |
| 193 | return EXIT_FAILURE; |
| 194 | } |
| 195 | ret = close(txt_fd); |
| 196 | } |
David Wagner | 7ddfb60 | 2012-01-13 13:27:34 +0000 | [diff] [blame^] | 197 | /* The +1 is for the additionnal ending \0. See below. */ |
| 198 | if (filesize + 1 > envsize) { |
| 199 | fprintf(stderr, "The input file is larger than the environment partition size\n"); |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 200 | return EXIT_FAILURE; |
| 201 | } |
| 202 | |
| 203 | /* Replace newlines separating variables with \0 */ |
| 204 | for (fp = 0, ep = 0 ; fp < filesize ; fp++) { |
| 205 | if (filebuf[fp] == '\n') { |
David Wagner | dafad3c | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 206 | if (ep == 0) { |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 207 | /* |
David Wagner | dafad3c | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 208 | * Newlines at the beginning of the file ? |
| 209 | * Ignore them. |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 210 | */ |
| 211 | continue; |
| 212 | } else if (filebuf[fp-1] == '\\') { |
| 213 | /* |
| 214 | * Embedded newline in a variable. |
| 215 | * |
David Wagner | dafad3c | 2011-12-20 14:59:29 +0000 | [diff] [blame] | 216 | * The backslash was added to the envptr; rewind |
| 217 | * and replace it with a newline |
David Wagner | 65a7f2d | 2011-09-26 03:26:34 +0000 | [diff] [blame] | 218 | */ |
| 219 | ep--; |
| 220 | envptr[ep++] = '\n'; |
| 221 | } else { |
| 222 | /* End of a variable */ |
| 223 | envptr[ep++] = '\0'; |
| 224 | } |
| 225 | } else if (filebuf[fp] == '#') { |
| 226 | if (fp != 0 && filebuf[fp-1] == '\n') { |
| 227 | /* This line is a comment, let's skip it */ |
| 228 | while (fp < txt_file_stat.st_size && fp++ && |
| 229 | filebuf[fp] != '\n'); |
| 230 | } else { |
| 231 | envptr[ep++] = filebuf[fp]; |
| 232 | } |
| 233 | } else { |
| 234 | envptr[ep++] = filebuf[fp]; |
| 235 | } |
| 236 | } |
| 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 | |
| 260 | memcpy(dataptr, &targetendian_crc, sizeof(uint32_t)); |
| 261 | |
| 262 | bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 263 | if (bin_fd == -1) { |
| 264 | fprintf(stderr, "Can't open output file \"%s\": %s\n", |
| 265 | bin_filename, strerror(errno)); |
| 266 | return EXIT_FAILURE; |
| 267 | } |
| 268 | |
| 269 | if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) != |
| 270 | sizeof(*dataptr) * datasize) { |
| 271 | fprintf(stderr, "write() failed: %s\n", strerror(errno)); |
| 272 | return EXIT_FAILURE; |
| 273 | } |
| 274 | |
| 275 | ret = close(bin_fd); |
| 276 | |
| 277 | return ret; |
| 278 | } |