diff options
author | Rafał Miłecki <zajec5@gmail.com> | 2016-05-09 12:53:36 +0000 |
---|---|---|
committer | Luka Perkov <luka@openwrt.org> | 2016-06-19 19:23:49 +0200 |
commit | ff752836feabc2092ee877e79e98c3568fc1c419 (patch) | |
tree | 0eb1470ff204b24eeacc971b60fba24061d72552 /package/system | |
parent | a3546435e5f9739fedfb18b9b2b78fb43f9bfb90 (diff) | |
download | master-187ad058-ff752836feabc2092ee877e79e98c3568fc1c419.tar.gz master-187ad058-ff752836feabc2092ee877e79e98c3568fc1c419.tar.bz2 master-187ad058-ff752836feabc2092ee877e79e98c3568fc1c419.zip |
mtd: seama: move buf allocation to the MD5 function
This buf is only used in this function now, so lets move it there.
Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@49308 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package/system')
-rw-r--r-- | package/system/mtd/src/seama.c | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/package/system/mtd/src/seama.c b/package/system/mtd/src/seama.c index b51177ac87..dac5da2b85 100644 --- a/package/system/mtd/src/seama.c +++ b/package/system/mtd/src/seama.c @@ -51,18 +51,34 @@ 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(struct seama_entity_header *shdr, char *buf, size_t len) +seama_fix_md5(struct seama_entity_header *shdr, int fd, size_t len, size_t block_offset) { + char *buf; char *data; + ssize_t res; size_t msize; size_t isize; MD5_CTX ctx; unsigned char digest[16]; int i; + int err = 0; if (len < sizeof(struct seama_entity_header)) return -1; + buf = malloc(len); + if (!buf) { + err = -ENOMEM; + goto err_out; + } + + res = pread(fd, buf, len, block_offset); + if (res != len) { + perror("pread"); + err = -EIO; + goto err_free; + } + isize = ntohl(shdr->size); msize = ntohs(shdr->metasize); if (isize == 0) { @@ -100,7 +116,10 @@ seama_fix_md5(struct seama_entity_header *shdr, char *buf, size_t len) /* update the checksum in the image */ memcpy(shdr->md5, digest, sizeof(digest)); - return 0; +err_free: + free(buf); +err_out: + return err; } int @@ -108,7 +127,6 @@ 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; @@ -150,19 +168,7 @@ mtd_fixseama(const char *mtd, size_t offset) return -1; } - buf = malloc(mtdsize); - if (!buf) { - perror("malloc"); - exit(1); - } - - res = pread(fd, buf, mtdsize, block_offset); - if (res != mtdsize) { - perror("pread"); - exit(1); - } - - if (seama_fix_md5(shdr, buf, mtdsize)) + if (seama_fix_md5(shdr, fd, mtdsize, block_offset)) goto out; if (mtd_erase_block(fd, block_offset)) { |