[PATCH firmware-utils 2/2] otrx: allow validating TRX from stdin
From: Rafał Miłecki This makes "otrx check" work with stdin to allow e.g. dd if=firmware.chk skip=1 bs=58 | otrx check - Signed-off-by: Rafał Miłecki --- src/otrx.c | 50 +++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/otrx.c b/src/otrx.c index aa81c96..fb8849f 100644 --- a/src/otrx.c +++ b/src/otrx.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #if !defined(__BYTE_ORDER) @@ -130,6 +131,44 @@ uint32_t otrx_crc32(uint32_t crc, uint8_t *buf, size_t len) { return crc; } +/** + * Helpers + **/ + +static FILE *otrx_open(const char *pathname, const char *mode) { + if (strcmp(pathname, "-")) + return fopen(pathname, mode); + + if (isatty(fileno(stdin))) { + fprintf(stderr, "Reading from TTY stdin is unsupported\n"); + return NULL; + } + + return stdin; +} + +static int otrx_skip(FILE *fp, size_t length) +{ + if (fseek(fp, trx_offset, SEEK_CUR)) { + uint8_t buf[1024]; + size_t bytes; + + do { + bytes = fread(buf, 1, otrx_min(sizeof(buf), length), fp); + if (bytes <= 0) + return -EIO; + length -= bytes; + } while (length); + } + + return 0; +} + +static void otrx_close(FILE *fp) { + if (fp != stdin) + fclose(fp); +} + /** * Check **/ @@ -164,14 +203,19 @@ static int otrx_check(int argc, char **argv) { optind = 3; otrx_check_parse_options(argc, argv); - trx = fopen(trx_path, "r"); + trx = otrx_open(trx_path, "r"); if (!trx) { fprintf(stderr, "Couldn't open %s\n", trx_path); err = -EACCES; goto out; } - fseek(trx, trx_offset, SEEK_SET); + if (otrx_skip(trx, trx_offset)) { + fprintf(stderr, "Couldn't skip first %zd B\n", trx_offset); + err = -EIO; + goto err_close; + } + bytes = fread(, 1, sizeof(hdr), trx); if (bytes != sizeof(hdr)) { fprintf(stderr, "Couldn't read %s header\n", trx_path); @@ -215,7 +259,7 @@ static int otrx_check(int argc, char **argv) { printf("Found a valid TRX version %d\n", le32_to_cpu(hdr.version)); err_close: - fclose(trx); + otrx_close(trx); out: return err; } -- 2.31.1 ___ openwrt-devel mailing list openwrt-devel@lists.openwrt.org https://lists.openwrt.org/mailman/listinfo/openwrt-devel
[PATCH firmware-utils 1/2] otrx: avoid unneeded fseek() when calculating CRC32
From: Rafał Miłecki Just use already read data. Signed-off-by: Rafał Miłecki --- src/otrx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/otrx.c b/src/otrx.c index b5cd70d..aa81c96 100644 --- a/src/otrx.c +++ b/src/otrx.c @@ -193,8 +193,8 @@ static int otrx_check(int argc, char **argv) { } crc32 = 0x; - fseek(trx, trx_offset + TRX_FLAGS_OFFSET, SEEK_SET); - length -= TRX_FLAGS_OFFSET; + crc32 = otrx_crc32(crc32, (uint8_t *) + TRX_FLAGS_OFFSET, sizeof(hdr) - TRX_FLAGS_OFFSET); + length -= sizeof(hdr); while ((bytes = fread(buf, 1, otrx_min(sizeof(buf), length), trx)) > 0) { crc32 = otrx_crc32(crc32, buf, bytes); length -= bytes; -- 2.31.1 ___ openwrt-devel mailing list openwrt-devel@lists.openwrt.org https://lists.openwrt.org/mailman/listinfo/openwrt-devel
Re: [PATCH 0/1] build: scripts/config - update to kconfig-v5.14
On Thu, Nov 25, 2021 at 4:54 AM Florian Eckert wrote: > > Hello Eneas, > > > I've been looking at this too and wanted to update. > Since I wasn't there from the beginning, I don't know what all OpenWrt > changes. > Hence my suggestion: > Can't we put the changes OpenWrt makes to the source code into a patch > directory > and then patch that with 'quilt' like we do with all the other packages? > That would make the task easier for others too update this too. I don't think we should keep the patches along with the main sources, but it may be beneficial to create an official repository under the openwrt infrastructure. What I had done was to create a fork of 'linux', and applied the openwrt patches on top of that. You can see my current version, which resulted in the patch I just sent, here: https://github.com/cotequeiroz/linux/commits/openwrt-5.14/scripts/kconfig I don't think anyone can review the openwrt patch without looking at the commits I applied. Getting all of those commits together took some effort when I first did it. I was in the same situation as you are, but I really wanted the much better dependency view that the new kernel had. That was my motivation then. I documented the changes the best I could. It resulted in this branch: https://github.com/cotequeiroz/linux/tree/openwrt-b2c55d50f8 Then fast-forwarding them was also time-consuming, because of the high number of commits to adapt, especially the many changes to the Makefile. I remember doing it in two ways: starting from openwrt version of kconfig and applying the linux updates ("linux-on-top-of-openwrt" branch, stale after the review); and rebasing the openwrt changes on top of kconfig-5.6, which ended up being the final version (openwrt branch--I should have added a version to it), to see how close I would get both versions. I added the link to the branch I used in the README. At least now the Makefile was less subject to change, and our modifications have become straightforward and clean. There have been some syntax adjustments (notably the removal of '---help---'), but when we moved from 5.4 to 5.10, our kernel patches had to be adapted as well. This is why I think it is beneficial to update this regularly, keeping it fresh. Cheers, Eneas ___ openwrt-devel mailing list openwrt-devel@lists.openwrt.org https://lists.openwrt.org/mailman/listinfo/openwrt-devel
Re: [PATCH 0/1] build: scripts/config - update to kconfig-v5.14
Hello Eneas, On 2021-11-24 22:25, Eneas U de Queiroz wrote: I'm updating the Kconfig programs in scripts/config, from Linux 5.6 to 5.14. The changes are not crytical to openwrt, but regular updates ease the transition to an eventual newer version with more desirable features. I've been looking at this too and wanted to update. Since I wasn't there from the beginning, I don't know what all OpenWrt changes. Hence my suggestion: Can't we put the changes OpenWrt makes to the source code into a patch directory and then patch that with 'quilt' like we do with all the other packages? That would make the task easier for others too update this too. I am ready to help you there Best regards Florian ___ openwrt-devel mailing list openwrt-devel@lists.openwrt.org https://lists.openwrt.org/mailman/listinfo/openwrt-devel