diff --git a/dist/tools/riotboot_gen_hdr/common.c b/dist/tools/riotboot_gen_hdr/common.c index c3cb771b6a..38c7718aee 100644 --- a/dist/tools/riotboot_gen_hdr/common.c +++ b/dist/tools/riotboot_gen_hdr/common.c @@ -17,7 +17,7 @@ #include #include -int to_file(const char *filename, void *buf, size_t len) +int to_file(const char *filename, const void *buf, size_t len) { int fd; @@ -37,3 +37,24 @@ int to_file(const char *filename, void *buf, size_t len) return fd; } } + +int from_file(const char *filename, void *buf, size_t len) +{ + int fd; + + if (strcmp("-", filename)) { + fd = open(filename, O_RDONLY, 0); + } + else { + fd = STDIN_FILENO; + } + + if (fd > 0) { + ssize_t res = read(fd, buf, len); + close(fd); + return res; + } + else { + return fd; + } +} diff --git a/dist/tools/riotboot_gen_hdr/common.h b/dist/tools/riotboot_gen_hdr/common.h index f3380d8ffb..50e154772c 100644 --- a/dist/tools/riotboot_gen_hdr/common.h +++ b/dist/tools/riotboot_gen_hdr/common.h @@ -17,12 +17,23 @@ /** * @brief Write len bytes of a given buffer into a file * - * @param[out] filename name of the file to be written + * @param[in] filename name of the file to be written * @param[in] buf a pointer to the buffer which needs to be written * @param[in] len the number of bytes from buf to be written * */ -int to_file(const char *filename, void *buf, size_t len); +int to_file(const char *filename, const void *buf, size_t len); + +/** + * @brief Read len bytes from a given file into a buffer + * + * @param[in] filename name of the file to be read + * @param[out] buf a pointer to the buffer to store the content + * @param[out] len the number of bytes to be read + * + * @return Number of bytes read, or negative error + */ +int from_file(const char *filename, const void *buf, size_t len); #ifdef __cplusplus } /* end extern "C" */ diff --git a/dist/tools/riotboot_gen_hdr/genhdr.c b/dist/tools/riotboot_gen_hdr/genhdr.c index 409b09b487..3e1622d6cd 100644 --- a/dist/tools/riotboot_gen_hdr/genhdr.c +++ b/dist/tools/riotboot_gen_hdr/genhdr.c @@ -112,3 +112,51 @@ int genhdr(int argc, char *argv[]) return 0; } + +int updatehdr(int argc, char *argv[]) +{ + if (argc < 3) { + fprintf(stderr, "usage: genhdr update \n"); + return -1; + } + const char *file = argv[1]; + + riotboot_hdr_t hdr = { 0 }; + int res = from_file(file, &hdr, sizeof(hdr)); + if (res < (int)sizeof(hdr)) { + fprintf(stderr, "Can't read header from %s\n", file); + return -EIO; + } + + if (hdr.magic_number != RIOTBOOT_MAGIC) { + fprintf(stderr, "Invalid magic: %x\n", hdr.magic_number); + return -EIO; + } + + hdr.version = atoi(argv[2]); + hdr.chksum = riotboot_hdr_checksum(&hdr); + to_file(file, &hdr, sizeof(hdr)); + + return 0; +} + +int readhdr(const char *file) +{ + riotboot_hdr_t hdr = { 0 }; + int res = from_file(file, &hdr, sizeof(hdr)); + if (res < (int)sizeof(hdr)) { + fprintf(stderr, "Can't read header from %s\n", file); + return -EIO; + } + + if (hdr.magic_number != RIOTBOOT_MAGIC) { + fprintf(stderr, "Invalid magic: %x\n", hdr.magic_number); + return -EIO; + } + + printf("version: %u\n", hdr.version); + printf("address: 0x%x\n", hdr.start_addr); + printf("checksum: %svalid\n", riotboot_hdr_validate(&hdr) ? "in" : ""); + + return 0; +} diff --git a/dist/tools/riotboot_gen_hdr/main.c b/dist/tools/riotboot_gen_hdr/main.c index 6288fa29ed..9bf985d494 100644 --- a/dist/tools/riotboot_gen_hdr/main.c +++ b/dist/tools/riotboot_gen_hdr/main.c @@ -18,10 +18,14 @@ #include int genhdr(int argc, char *argv[]); +int updatehdr(int argc, char *argv[]); +int readhdr(const char *file); int main(int argc, char *argv[]) { - char *usage = "genhdr generate [args]"; + char *usage = "genhdr generate [args]\n\t" + "genhdr update [file] [new_version]\n\t" + "genhdr show [file]"; if (argc < 2) { goto usage; @@ -29,8 +33,14 @@ int main(int argc, char *argv[]) else if (!strcmp(argv[1], "generate")) { return genhdr(argc - 1, &argv[1]); } + else if (!strcmp(argv[1], "update")) { + return updatehdr(argc - 1, &argv[1]); + } + else if (!strcmp(argv[1], "show")) { + return readhdr(argv[2]); + } usage: - fprintf(stderr, "usage: %s\n", usage); + fprintf(stderr, "usage:\t%s\n", usage); return 1; }