aboutsummaryrefslogtreecommitdiffstats
path: root/package/system
diff options
context:
space:
mode:
authorJohn Crispin <blogic@openwrt.org>2016-03-03 20:24:38 +0000
committerJohn Crispin <blogic@openwrt.org>2016-03-03 20:24:38 +0000
commit44eafcfd2679a74a7fb1a22c41827547006214c5 (patch)
tree127d9daddad0f56954592ec0b2cb94168fa4d021 /package/system
parent769cd2ffaa873fc629c6db9dac523bb148977f2e (diff)
downloadmaster-187ad058-44eafcfd2679a74a7fb1a22c41827547006214c5.tar.gz
master-187ad058-44eafcfd2679a74a7fb1a22c41827547006214c5.tar.bz2
master-187ad058-44eafcfd2679a74a7fb1a22c41827547006214c5.zip
mtd: fix reading of image magic bytes in smaller chunks
The image_check currently fails when it cannot read all magic bytes in a single chunk. But this can happen when the data are read from a pipe. This currently breaks the openmesh.sh upgrade script with musl because it uses dd with a blocksize of 1 to copy the image file to the mtd process. The read can simply be repeated until enough bytes are read for the magic byte check. It only stops when either an error was returned or 0 bytes were read. Signed-off-by: Sven Eckelmann <sven.eckelmann@open-mesh.com> git-svn-id: svn://svn.openwrt.org/openwrt/trunk@48891 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package/system')
-rw-r--r--package/system/mtd/src/mtd.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/package/system/mtd/src/mtd.c b/package/system/mtd/src/mtd.c
index eda001e1d8..dae0514a0b 100644
--- a/package/system/mtd/src/mtd.c
+++ b/package/system/mtd/src/mtd.c
@@ -179,14 +179,21 @@ image_check(int imagefd, const char *mtd)
{
uint32_t magic;
int ret = 1;
+ int bufread;
+
+ while (buflen < sizeof(magic)) {
+ bufread = read(imagefd, buf + buflen, sizeof(magic) - buflen);
+ if (bufread < 1)
+ break;
+
+ buflen += bufread;
+ }
if (buflen < sizeof(magic)) {
- buflen += read(imagefd, buf + buflen, sizeof(magic) - buflen);
- if (buflen < sizeof(magic)) {
- fprintf(stdout, "Could not get image magic\n");
- return 0;
- }
+ fprintf(stdout, "Could not get image magic\n");
+ return 0;
}
+
magic = ((uint32_t *)buf)[0];
if (be32_to_cpu(magic) == TRX_MAGIC)