mtd: seama: add md5 to header struct

This allows us to drop some extra offset calculations and simplifies
code a bit.

Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
This commit is contained in:
rmilecki 2016-05-09 12:53:29 +00:00 committed by Rafał Miłecki
parent 8632d89fa0
commit bcccb03200
2 changed files with 11 additions and 12 deletions

View File

@ -53,7 +53,7 @@ ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
int int
seama_fix_md5(char *buf, size_t len) seama_fix_md5(char *buf, size_t len)
{ {
struct seama_hdr *shdr = (struct seama_hdr *) buf; struct seama_entity_header *shdr = (struct seama_entity_header *) buf;
char *data; char *data;
size_t msize; size_t msize;
size_t isize; size_t isize;
@ -61,7 +61,7 @@ seama_fix_md5(char *buf, size_t len)
unsigned char digest[16]; unsigned char digest[16];
int i; int i;
if (len < sizeof(struct seama_hdr)) if (len < sizeof(struct seama_entity_header))
return -1; return -1;
isize = ntohl(shdr->size); isize = ntohl(shdr->size);
@ -71,17 +71,17 @@ seama_fix_md5(char *buf, size_t len)
return -1; return -1;
} }
len -= sizeof(struct seama_hdr) + sizeof(digest) + msize; len -= sizeof(struct seama_entity_header) + msize;
if (isize > len) if (isize > len)
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_Init(&ctx);
MD5_Update(&ctx, data, isize); MD5_Update(&ctx, data, isize);
MD5_Final(digest, &ctx); MD5_Final(digest, &ctx);
if (!memcmp(digest, &buf[sizeof(struct seama_hdr)], sizeof(digest))) { if (!memcmp(digest, shdr->md5, sizeof(digest))) {
if (quiet < 2) if (quiet < 2)
fprintf(stderr, "the header is fixed already\n"); fprintf(stderr, "the header is fixed already\n");
return -1; return -1;
@ -99,8 +99,7 @@ seama_fix_md5(char *buf, size_t len)
shdr->size = htonl(isize); shdr->size = htonl(isize);
/* update the checksum in the image */ /* update the checksum in the image */
for (i = 0; i < sizeof(digest); i++) memcpy(shdr->md5, digest, sizeof(digest));
buf[sizeof(struct seama_hdr) + i] = digest[i];
return 0; return 0;
} }
@ -113,7 +112,7 @@ mtd_fixseama(const char *mtd, size_t offset)
char *buf; char *buf;
ssize_t res; ssize_t res;
size_t block_offset; size_t block_offset;
struct seama_hdr *shdr; struct seama_entity_header *shdr;
if (quiet < 2) if (quiet < 2)
fprintf(stderr, "Trying to fix SEAMA header in %s at 0x%x...\n", fprintf(stderr, "Trying to fix SEAMA header in %s at 0x%x...\n",
@ -146,7 +145,7 @@ mtd_fixseama(const char *mtd, size_t offset)
exit(1); exit(1);
} }
shdr = (struct seama_hdr *)first_block; shdr = (struct seama_entity_header *)first_block;
if (shdr->magic != htonl(SEAMA_MAGIC)) { if (shdr->magic != htonl(SEAMA_MAGIC)) {
fprintf(stderr, "No SEAMA header found\n"); fprintf(stderr, "No SEAMA header found\n");
return -1; return -1;

View File

@ -94,14 +94,14 @@
* | image size | * | image size |
* +-------------+-------------+ * +-------------+-------------+
*/ */
/* seama header */ /* seama header */
typedef struct seama_hdr seamahdr_t; struct seama_entity_header {
struct seama_hdr
{
uint32_t magic; /* should always be SEAMA_MAGIC. */ uint32_t magic; /* should always be SEAMA_MAGIC. */
uint16_t reserved; /* reserved for */ uint16_t reserved; /* reserved for */
uint16_t metasize; /* size of the META data */ uint16_t metasize; /* size of the META data */
uint32_t size; /* size of the image */ uint32_t size; /* size of the image */
uint8_t md5[16];
} __attribute__ ((packed)); } __attribute__ ((packed));