diff options
Diffstat (limited to 'mkubootenv.c')
-rw-r--r-- | mkubootenv.c | 298 |
1 files changed, 196 insertions, 102 deletions
diff --git a/mkubootenv.c b/mkubootenv.c index 156e12d..4294b76 100644 --- a/mkubootenv.c +++ b/mkubootenv.c @@ -57,6 +57,7 @@ #define TRAILER_SIZE 2 #define err(fmt, args...) fprintf(stderr, "%s: Error: " fmt, CMD_NAME, ##args) +#define warn(fmt, args...) fprintf(stderr, "%s: Warning: " fmt, CMD_NAME, ##args) #ifdef DEBUG # define dbg(fmt, args...) fprintf(stdout, fmt, ##args) #else @@ -65,165 +66,258 @@ /* file "object" */ struct file { - char *name; + const char *name; int fd; uint8_t *ptr; - struct stat sbuf; + size_t size; }; static void usage_and_exit(int status) __attribute__((noreturn)); -static void usage_and_exit(int status) +static inline void uboot_env_init_file(struct file *f) { - printf("usage: mkenv [-t <type>] [-s <size>] <source file> <target image file>\n" - " -s <size> set size of the target image file to <size> bytes. If <size> is\n" - " bigger than the source file, the target image gets padded with null\n" - " bytes. If <size> is smaller than the source file, an error is emitted.\n" - " -r reverse operation: get source file from target image file\n"); - exit(status); + memset(f, 0, sizeof(struct file)); + f->ptr = MAP_FAILED; } -int main(int argc, char **argv) +static int uboot_env_prepare_source(struct file *s) { - int i; - int status = EXIT_FAILURE; - struct file s, t; /* source and target file */ - ssize_t img_size = -1; - ssize_t min_img_size; - bool reverse = false; - uint8_t *p, *q; - uint32_t *crc; - - if (argc < 2) - usage_and_exit(EXIT_FAILURE); - - /* parse commandline options */ - for (i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) { - switch (argv[i][1]) { - case 's': - img_size = strtol(argv[++i], NULL, 10); - break; - case 'r': - reverse = true; - break; - case 'h': - status = EXIT_SUCCESS; - /* fall through */ - default: - usage_and_exit(status); - break; - } - } - - /* we expect two filenames */ - if (i + 2 > argc) - usage_and_exit(EXIT_FAILURE); - - s.name = argv[i]; - t.name = argv[++i]; + struct stat sbuf; - s.fd = open(s.name, O_RDONLY); - if (s.fd < 0) { - err("Can't open source file '%s': %s\n", s.name, + s->fd = open(s->name, O_RDONLY); + if (s->fd < 0) { + err("Can't open source file '%s': %s\n", s->name, strerror(errno)); - exit(EXIT_FAILURE); + return EXIT_FAILURE; } - if (fstat(s.fd, &s.sbuf) < 0) { - err("Can't stat source file '%s': %s\n", s.name, + if (fstat(s->fd, &sbuf) < 0) { + err("Can't stat source file '%s': %s\n", s->name, strerror(errno)); - goto err_out_close_s; + close(s->fd); + return -1; } - s.ptr = mmap(NULL, s.sbuf.st_size, PROT_READ, MAP_SHARED, s.fd, 0); - if (s.ptr == MAP_FAILED) { - err("Can't mmap source image file '%s': %s\n", s.name, + s->ptr = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, s->fd, 0); + if (s->ptr == MAP_FAILED) { + err("Can't mmap source image file '%s': %s\n", s->name, strerror(errno)); - goto err_out_close_s; + close(s->fd); + return -1; } - /* TODO: Check source file format: - * var=value\n - */ + s->size = sbuf.st_size; - /* - * check whether the size hasn't been set or whether the source file + - * CRC + trailer fits into the specified size. - */ - min_img_size = CRC32_SIZE + s.sbuf.st_size + TRAILER_SIZE; - if (img_size < 0) - img_size = min_img_size; - else if (img_size < min_img_size) { - err("Specified size (%zd) is too small for the source file to " - "fit into. Must be at least %zd bytes.\n", - img_size, min_img_size); - goto err_out_munmap_s; - } + return 0; +} - t.fd = open(t.name, O_RDWR|O_CREAT|O_TRUNC, 0666); - if (t.fd < 0) { - err("Can't open target image file '%s': %s\n", t.name, +static int uboot_env_prepare_target(struct file *t) +{ + t->fd = open(t->name, O_RDWR|O_CREAT|O_TRUNC, 0666); + if (t->fd < 0) { + err("Can't open target image file '%s': %s\n", t->name, strerror(errno)); - goto err_out_munmap_s; + return -1; } /* * seek to the end of the target file to write a byte, so we have the * whole target image size mapped for writing */ - if (lseek(t.fd, img_size - 1, SEEK_SET) < 0) { - err("Can't seek in target image file '%s': %s\n", t.name, + if (lseek(t->fd, t->size - 1, SEEK_SET) < 0) { + err("Can't seek in target image file '%s': %s\n", t->name, strerror(errno)); - goto err_out_close_t; + close(t->fd); + return -1; } - if (write(t.fd, "", 1) < 0) { - err("Can't write to target image file '%s': %s\n", t.name, + if (write(t->fd, "", 1) < 0) { + err("Can't write to target image file '%s': %s\n", t->name, strerror(errno)); - goto err_out_close_t; + close(t->fd); + return -1; } - t.ptr = mmap(NULL, img_size, PROT_READ|PROT_WRITE, MAP_SHARED, t.fd, 0); - if (t.ptr == MAP_FAILED) { - err("Can't mmap target image file '%s': %s\n", t.name, + t->ptr = mmap(NULL, t->size, PROT_READ|PROT_WRITE, MAP_SHARED, t->fd, 0); + if (t->ptr == MAP_FAILED) { + err("Can't mmap target image file '%s': %s\n", t->name, strerror(errno)); - goto err_out_close_t; + close(t->fd); + return -1; } - dbg("source file: %s\n", s.name); - dbg("target image file: %s\n", t.name); - dbg("size: %zd\n", img_size); + return 0; +} + +static void uboot_env_cleanup_file(struct file *f) +{ + if (f->ptr != MAP_FAILED) + munmap(f->ptr, f->size); + if (f->fd > 0) + close(f->fd); +} + +static void uboot_env_to_img(struct file *s, struct file *t) +{ + uint8_t *p, *q, *end; + uint32_t *crc; + + dbg("source file (env): %s\n", s->name); + dbg("target image file (bin): %s\n", t->name); + dbg("target size: %zd\n", t->size); /* CRC32 placeholder, will be filled later */ dbg("writing crc dummy...\n"); - for (p = t.ptr; p < t.ptr + CRC32_SIZE; p++) + end = t->ptr + CRC32_SIZE; + for (p = t->ptr; p < end; p++) *p = 0; /* copy the source file, replacing \n by \0 */ dbg("writing data...\n"); - for (q = s.ptr; q < s.ptr + s.sbuf.st_size; q++, p++) + end = s->ptr + s->size; + for (q = s->ptr; q < end; q++, p++) *p = (*q == '\n') ? '\0' : *q; /* trailer */ dbg("writing trailer...\n"); - for (q = s.ptr + s.sbuf.st_size; q < s.ptr + img_size; q++) + end = s->ptr + t->size; + for (q = s->ptr + s->size; q < end; q++) *p = 0; /* now for the real CRC32 */ dbg("calculating crc...\n"); - crc = (uint32_t *) t.ptr; - *crc = crc32(0, t.ptr + CRC32_SIZE, img_size - CRC32_SIZE); + crc = (uint32_t *) t->ptr; + *crc = crc32(0, t->ptr + CRC32_SIZE, t->size - CRC32_SIZE); dbg("crc: %08x\n", *crc); +} + +static void uboot_img_to_env(struct file *s, struct file *t) +{ + uint8_t *p, *q, *end; + uint32_t *crc; + + dbg("source file (bin): %s\n", s->name); + dbg("target image file (env): %s\n", t->name); + dbg("target size: %zd\n", t->size); + + /* TODO: check CRC */ + + p = t->ptr; + end = s->ptr + CRC32_SIZE + t->size; + for (q = s->ptr + CRC32_SIZE; q < end; p++, q++) + *p = (*q == '\0') ? '\n' : *q; +} + +static void usage_and_exit(int status) +{ + printf("usage: mkenv [-t <type>] [-s <size>] <source file> <target file>\n" + " -s <size> set size of the target image file to <size> bytes. If <size> is\n" + " bigger than the source file, the target image gets padded with null\n" + " bytes. If <size> is smaller than the source file, an error is emitted.\n" + " -r reverse operation: get plaintext env file (target) from binary image\n" + " file (source)\n"); + exit(status); +} + +int main(int argc, char **argv) +{ + int i; + int status = EXIT_FAILURE; + ssize_t img_size = -1; + bool reverse = false; + struct file s, t; /* source and target file */ + + if (argc < 2) + usage_and_exit(EXIT_FAILURE); + + /* parse commandline options */ + for (i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) { + switch (argv[i][1]) { + case 's': + img_size = strtol(argv[++i], NULL, 10); + if (img_size <= 0) { + err("Invalid target image size: %zd. Must be greater than 0.\n", img_size); + exit(EXIT_FAILURE); + } + break; + case 'r': + reverse = true; + break; + case 'h': + status = EXIT_SUCCESS; + /* fall through */ + default: + usage_and_exit(status); + break; + } + } + + /* we expect two filenames */ + if (i + 2 > argc) + usage_and_exit(EXIT_FAILURE); + + if (reverse && img_size >= 0) + warn("Image size specified in reverse mode will be ignored\n"); + + uboot_env_init_file(&s); + uboot_env_init_file(&t); + s.name = argv[i]; + t.name = argv[i+1]; + + if (uboot_env_prepare_source(&s)) + exit(EXIT_FAILURE); + + if (!reverse) { + ssize_t min_img_size = CRC32_SIZE + s.size + TRAILER_SIZE; + + /* TODO: Check source file format: + * var=value\n + */ + + /* + * check whether the size hasn't been set or whether the source file + + * CRC + trailer fits into the specified size. + */ + if (img_size < 0) + img_size = min_img_size; + else if (img_size < min_img_size) { + err("Specified size (%zd) is too small for the source file to " + "fit into. Must be at least %zd bytes.\n", + img_size, min_img_size); + goto cleanup_source; + } + + t.size = img_size; + if (uboot_env_prepare_target(&t)) + goto cleanup_source; + + uboot_env_to_img(&s, &t); + } else { + uint8_t *p; + uint8_t *end; + bool found_data_end = false; + + /* get the length of the data part */ + end = s.ptr + CRC32_SIZE + s.size; + for (p = s.ptr + CRC32_SIZE; (p < end - 1) && (!found_data_end); p++) { + /* two null bytes mark the end of the data section */ + if (*p == '\0' && *(p + 1) == '\0') + found_data_end = true; + } + + /* calculate the plain text file size */ + t.size = p - (s.ptr + CRC32_SIZE); + if (uboot_env_prepare_target(&t)) + goto cleanup_source; + + uboot_img_to_env(&s, &t); + } status = EXIT_SUCCESS; - munmap(t.ptr, img_size); -err_out_close_t: - close(t.fd); -err_out_munmap_s: - munmap(s.ptr, s.sbuf.st_size); -err_out_close_s: - close(s.fd); + uboot_env_cleanup_file(&t); +cleanup_source: + uboot_env_cleanup_file(&s); exit(status); } |