123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <string.h>
- #include <unistd.h>
- #include <libgen.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include "compiler.h"
- #include <u-boot/crc.h>
- #include <version.h>
- #define CRC_SIZE sizeof(uint32_t)
- static void usage(const char *exec_name)
- {
- fprintf(stderr, "%s [-h] [-r] [-b] [-p <byte>] -s <environment partition size> -o <output> <input file>\n"
- "\n"
- "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"
- "\n"
- "\tThe input file is in format:\n"
- "\t\tkey1=value1\n"
- "\t\tkey2=value2\n"
- "\t\t...\n"
- "\tEmpty lines are skipped, and lines with a # in the first\n"
- "\tcolumn are treated as comments (also skipped).\n"
- "\t-r : the environment has multiple copies in flash\n"
- "\t-b : the target is big endian (default is little endian)\n"
- "\t-p <byte> : fill the image with <byte> bytes instead of 0xff bytes\n"
- "\t-V : print version information and exit\n"
- "\n"
- "If the input file is \"-\", data is read from standard input\n",
- exec_name);
- }
- long int xstrtol(const char *s)
- {
- long int tmp;
- errno = 0;
- tmp = strtol(s, NULL, 0);
- if (!errno)
- return tmp;
- if (errno == ERANGE)
- fprintf(stderr, "Bad integer format: %s\n", s);
- else
- fprintf(stderr, "Error while parsing %s: %s\n", s,
- strerror(errno));
- exit(EXIT_FAILURE);
- }
- int main(int argc, char **argv)
- {
- uint32_t crc, targetendian_crc;
- const char *txt_filename = NULL, *bin_filename = NULL;
- int txt_fd, bin_fd;
- unsigned char *dataptr, *envptr;
- unsigned char *filebuf = NULL;
- unsigned int filesize = 0, envsize = 0, datasize = 0;
- int bigendian = 0;
- int redundant = 0;
- unsigned char padbyte = 0xff;
- int option;
- int ret = EXIT_SUCCESS;
- struct stat txt_file_stat;
- int fp, ep;
- const char *prg;
- prg = basename(argv[0]);
-
- opterr = 0;
-
- while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
- switch (option) {
- case 's':
- datasize = xstrtol(optarg);
- break;
- case 'o':
- bin_filename = strdup(optarg);
- if (!bin_filename) {
- fprintf(stderr, "Can't strdup() the output filename\n");
- return EXIT_FAILURE;
- }
- break;
- case 'r':
- redundant = 1;
- break;
- case 'b':
- bigendian = 1;
- break;
- case 'p':
- padbyte = xstrtol(optarg);
- break;
- case 'h':
- usage(prg);
- return EXIT_SUCCESS;
- case 'V':
- printf("%s version %s\n", prg, PLAIN_VERSION);
- return EXIT_SUCCESS;
- case ':':
- fprintf(stderr, "Missing argument for option -%c\n",
- optopt);
- usage(prg);
- return EXIT_FAILURE;
- default:
- fprintf(stderr, "Wrong option -%c\n", optopt);
- usage(prg);
- return EXIT_FAILURE;
- }
- }
-
- if (datasize == 0) {
- fprintf(stderr, "Please specify the size of the environment partition.\n");
- usage(prg);
- return EXIT_FAILURE;
- }
- dataptr = malloc(datasize * sizeof(*dataptr));
- if (!dataptr) {
- fprintf(stderr, "Can't alloc %d bytes for dataptr.\n",
- datasize);
- return EXIT_FAILURE;
- }
-
- envsize = datasize - (CRC_SIZE + redundant);
- envptr = dataptr + CRC_SIZE + redundant;
-
- memset(envptr, padbyte, envsize);
-
- if (optind >= argc || strcmp(argv[optind], "-") == 0) {
- int readbytes = 0;
- int readlen = sizeof(*envptr) * 4096;
- txt_fd = STDIN_FILENO;
- do {
- filebuf = realloc(filebuf, readlen);
- if (!filebuf) {
- fprintf(stderr, "Can't realloc memory for the input file buffer\n");
- return EXIT_FAILURE;
- }
- readbytes = read(txt_fd, filebuf + filesize, readlen);
- if (errno) {
- fprintf(stderr, "Error while reading stdin: %s\n",
- strerror(errno));
- return EXIT_FAILURE;
- }
- filesize += readbytes;
- } while (readbytes == readlen);
- } else {
- txt_filename = argv[optind];
- txt_fd = open(txt_filename, O_RDONLY);
- if (txt_fd == -1) {
- fprintf(stderr, "Can't open \"%s\": %s\n",
- txt_filename, strerror(errno));
- return EXIT_FAILURE;
- }
-
- ret = fstat(txt_fd, &txt_file_stat);
- if (ret == -1) {
- fprintf(stderr, "Can't stat() on \"%s\": %s\n",
- txt_filename, strerror(errno));
- return EXIT_FAILURE;
- }
- filesize = txt_file_stat.st_size;
- filebuf = mmap(NULL, sizeof(*envptr) * filesize, PROT_READ,
- MAP_PRIVATE, txt_fd, 0);
- if (filebuf == MAP_FAILED) {
- fprintf(stderr, "mmap (%zu bytes) failed: %s\n",
- sizeof(*envptr) * filesize,
- strerror(errno));
- fprintf(stderr, "Falling back to read()\n");
- filebuf = malloc(sizeof(*envptr) * filesize);
- ret = read(txt_fd, filebuf, sizeof(*envptr) * filesize);
- if (ret != sizeof(*envptr) * filesize) {
- fprintf(stderr, "Can't read the whole input file (%zu bytes): %s\n",
- sizeof(*envptr) * filesize,
- strerror(errno));
- return EXIT_FAILURE;
- }
- }
- ret = close(txt_fd);
- }
-
- for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) {
- if (filebuf[fp] == '\n') {
- if (fp == 0 || filebuf[fp-1] == '\n') {
-
- continue;
- } else if (filebuf[fp-1] == '\\') {
-
- ep--;
- envptr[ep++] = '\n';
- } else {
-
- envptr[ep++] = '\0';
- }
- } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
-
- while (++fp < filesize && filebuf[fp] != '\n')
- continue;
- } else {
- envptr[ep++] = filebuf[fp];
- }
- }
-
- for( ; fp < filesize; fp++ )
- {
- if (filebuf[fp] == '\n') {
- if (fp == 0 || filebuf[fp-1] == '\n') {
-
- continue;
- }
- } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
- while (++fp < filesize && filebuf[fp] != '\n')
- continue;
- } else {
- fprintf(stderr, "The environment file is too large for the target environment storage\n");
- return EXIT_FAILURE;
- }
- }
-
- if (envptr[ep-1] != '\0') {
- envptr[ep++] = '\0';
-
- if (ep >= envsize) {
- fprintf(stderr, "The environment file is too large for the target environment storage\n");
- return EXIT_FAILURE;
- }
- envptr[ep] = '\0';
- } else {
- envptr[ep] = '\0';
- }
-
- crc = crc32(0, envptr, envsize);
- targetendian_crc = bigendian ? cpu_to_be32(crc) : cpu_to_le32(crc);
- memcpy(dataptr, &targetendian_crc, sizeof(targetendian_crc));
- if (redundant)
- dataptr[sizeof(targetendian_crc)] = 1;
- if (!bin_filename || strcmp(bin_filename, "-") == 0) {
- bin_fd = STDOUT_FILENO;
- } else {
- bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
- S_IWGRP);
- if (bin_fd == -1) {
- fprintf(stderr, "Can't open output file \"%s\": %s\n",
- bin_filename, strerror(errno));
- return EXIT_FAILURE;
- }
- }
- if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=
- sizeof(*dataptr) * datasize) {
- fprintf(stderr, "write() failed: %s\n", strerror(errno));
- return EXIT_FAILURE;
- }
- ret = close(bin_fd);
- return ret;
- }
|