mtd: detect image format when writing
[librecmc/librecmc.git] / package / system / mtd / src / trx.c
index 86d62fc19e7ebf5884fc33212eefcdce75f879db..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>
 
@@ -101,6 +102,7 @@ err:
        return -1;
 }
 
+#ifndef target_ar71xx
 int
 trx_check(int imagefd, const char *mtd, char *buf, int *len)
 {
@@ -110,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)) {
@@ -141,6 +145,7 @@ trx_check(int imagefd, const char *mtd, char *buf, int *len)
        close(fd);
        return 1;
 }
+#endif
 
 int
 mtd_fixtrx(const char *mtd, size_t offset)