mtd: detect image format when writing
[librecmc/librecmc.git] / package / system / mtd / src / trx.c
index 089d1ace46398376591b0dfebbefd196598acffd..00c4d6c0b50b7006f5ac503b98db2320000ae63b 100644 (file)
@@ -26,6 +26,7 @@
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
+#include <endian.h>
 #include <string.h>
 #include <errno.h>
 
@@ -111,10 +112,12 @@ trx_check(int imagefd, const char *mtd, char *buf, int *len)
        if (strcmp(mtd, "firmware") != 0)
                return 1;
 
-       *len = read(imagefd, buf, 32);
        if (*len < 32) {
-               fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
-               return 0;
+               *len += read(imagefd, buf + *len, 32 - *len);
+               if (*len < 32) {
+                       fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
+                       return 0;
+               }
        }
 
        if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {