mtd: seama: update MD5 using header in the first block buffer
[openwrt.git] / package / system / mtd / src / seama.c
index b0c8bf3..b51177a 100644 (file)
@@ -51,9 +51,8 @@ ssize_t pread(int fd, void *buf, size_t count, off_t offset);
 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
 
 int
-seama_fix_md5(char *buf, size_t len)
+seama_fix_md5(struct seama_entity_header *shdr, char *buf, size_t len)
 {
-       struct seama_hdr *shdr;
        char *data;
        size_t msize;
        size_t isize;
@@ -61,15 +60,9 @@ seama_fix_md5(char *buf, size_t len)
        unsigned char digest[16];
        int i;
 
-       if (len < sizeof(struct seama_hdr))
+       if (len < sizeof(struct seama_entity_header))
                return -1;
 
-       shdr = (struct seama_hdr *) buf;
-       if (shdr->magic != htonl(SEAMA_MAGIC)) {
-               fprintf(stderr, "no SEAMA header found\n");
-               return -1;
-       }
-
        isize = ntohl(shdr->size);
        msize = ntohs(shdr->metasize);
        if (isize == 0) {
@@ -77,17 +70,17 @@ seama_fix_md5(char *buf, size_t len)
                return -1;
        }
 
-       len -= sizeof(struct seama_hdr) + sizeof(digest) + msize;
+       len -= sizeof(struct seama_entity_header) + msize;
        if (isize > len)
                isize = len;
 
-       data = buf + sizeof(struct seama_hdr) + sizeof(digest) + msize;
+       data = buf + sizeof(struct seama_entity_header) + msize;
 
        MD5_Init(&ctx);
        MD5_Update(&ctx, data, isize);
        MD5_Final(digest, &ctx);
 
-       if (!memcmp(digest, &buf[sizeof(struct seama_hdr)], sizeof(digest))) {
+       if (!memcmp(digest, shdr->md5, sizeof(digest))) {
                if (quiet < 2)
                        fprintf(stderr, "the header is fixed already\n");
                return -1;
@@ -105,8 +98,7 @@ seama_fix_md5(char *buf, size_t len)
        shdr->size = htonl(isize);
 
        /* update the checksum in the image */
-       for (i = 0; i < sizeof(digest); i++)
-               buf[sizeof(struct seama_hdr) + i] = digest[i];
+       memcpy(shdr->md5, digest, sizeof(digest));
 
        return 0;
 }
@@ -115,9 +107,11 @@ int
 mtd_fixseama(const char *mtd, size_t offset)
 {
        int fd;
+       char *first_block;
        char *buf;
        ssize_t res;
        size_t block_offset;
+       struct seama_entity_header *shdr;
 
        if (quiet < 2)
                fprintf(stderr, "Trying to fix SEAMA header in %s at 0x%x...\n",
@@ -138,6 +132,24 @@ mtd_fixseama(const char *mtd, size_t offset)
                exit(1);
        }
 
+       first_block = malloc(erasesize);
+       if (!first_block) {
+               perror("malloc");
+               exit(1);
+       }
+
+       res = pread(fd, first_block, erasesize, block_offset);
+       if (res != erasesize) {
+               perror("pread");
+               exit(1);
+       }
+
+       shdr = (struct seama_entity_header *)first_block;
+       if (shdr->magic != htonl(SEAMA_MAGIC)) {
+               fprintf(stderr, "No SEAMA header found\n");
+               return -1;
+       }
+
        buf = malloc(mtdsize);
        if (!buf) {
                perror("malloc");
@@ -150,7 +162,7 @@ mtd_fixseama(const char *mtd, size_t offset)
                exit(1);
        }
 
-       if (seama_fix_md5(buf, mtdsize))
+       if (seama_fix_md5(shdr, buf, mtdsize))
                goto out;
 
        if (mtd_erase_block(fd, block_offset)) {
@@ -162,7 +174,7 @@ mtd_fixseama(const char *mtd, size_t offset)
        if (quiet < 2)
                fprintf(stderr, "Rewriting block at 0x%x\n", block_offset);
 
-       if (pwrite(fd, buf, erasesize, block_offset) != erasesize) {
+       if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) {
                fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
                exit(1);
        }