From 0c43e25e4bd81b9f30e3103380d32c876f728edf Mon Sep 17 00:00:00 2001 From: TJ Date: Fri, 8 Apr 2016 12:17:21 +0100 Subject: [PATCH] Add CRC32 calculation for headers and image payloads --- firmware_header_dump.c | 46 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/firmware_header_dump.c b/firmware_header_dump.c index 0594376..5e1f99e 100644 --- a/firmware_header_dump.c +++ b/firmware_header_dump.c @@ -84,7 +84,7 @@ unsigned int crc32(const unsigned char *data, ssize_t len, unsigned int crc) int main(int argc, char **argv) { - unsigned int arg, crc; + unsigned int arg, crc_header, crc_payload; char *filename = NULL; int fd, fd_mode; unsigned char *buffer = NULL; @@ -132,9 +132,12 @@ main(int argc, char **argv) unsigned int offset = 0; unsigned int count = 0; unsigned int next = 1; + void *payload = NULL; while (next) { unsigned long len = 0; + crc_header = crc_payload = 0xffffffff; + if (count) // switch struct type after initial file header header_len = sizeof(IMAGE_TAG); @@ -159,11 +162,28 @@ main(int argc, char **argv) } else { next = *(unsigned char *)(pimage->imageNext); len = atol(pimage->imageLen); + offset += sizeof(IMAGE_TAG); } - if (!count) { - printf("Image Offset: 0x%08x\n", 0x20000); + crc_header = crc32(buffer, header_len - (count ? CRC_LEN : TOKEN_LEN), crc_header); + + // read payload and calculate CRC32 + lseek(fd, offset, SEEK_SET); + if ( (payload = heap_and_reap(NULL, len, 1)) != NULL) { + if ( (qty = read(fd, payload, len)) < len) { + fprintf(stderr, "skipping CRC calculation: only able to read %ld of %ld bytes\n", qty, len); + } else { + crc_payload = crc32(payload, len, crc_payload); + + heap_and_reap(payload, 0, 0); + } + } else { + close(fd); + pr_error_exit(0, "unable to allocate memory (%ld bytes)\n", len); + } + if (!count) { + printf("Image Offset: 0x%08x\n", offset); printf("%04lx Tag Version: %s\n" "%04lx Signature 1: %s\n" "%04lx Signature 2: %s\n" @@ -182,7 +202,9 @@ main(int argc, char **argv) "%04lx Internal Version: %s\n" "%04lx Image Next: %u\n" "%04lx Image Validation Token: 0x%08x\n" - "%04lx Tag Validation Token: 0x%08x\n" + "%04lx Tag Validation Token: 0x%08x\n" + " Calculated Image CRC32: 0x%08x\n" + " Calculated Tag CRC32: 0x%08x\n" "\n", offsetof(struct _FILE_TAG, tagVersion), pfile->tagVersion, offsetof(struct _FILE_TAG, signiture_1), pfile->signiture_1, @@ -202,27 +224,35 @@ main(int argc, char **argv) offsetof(struct _FILE_TAG, internalversion), pfile->internalversion, offsetof(struct _FILE_TAG, imageNext), next, offsetof(struct _FILE_TAG, imageValidationToken), ntohl( *((unsigned int *)(pfile->imageValidationToken)) ), - offsetof(struct _FILE_TAG, tagValidationToken), ntohl( *((unsigned int *)(pfile->tagValidationToken)) ) + offsetof(struct _FILE_TAG, tagValidationToken), ntohl( *((unsigned int *)(pfile->tagValidationToken)) ), + crc_payload, + crc_header ); } else { - printf("Image Offset: 0x%08lx\n", offset + sizeof(IMAGE_TAG)); + printf("Image Offset: 0x%08x\n", offset); printf("%04lx Image Next: %u\n" "%04lx Image Type: %s (%lu)\n" "%04lx Image Signature: %u\n" "%04lx Image Len: %s (0x%08lx)\n" "%04lx Image Validation Token: 0x%08x\n" - "%04lx Tag Validation Token: 0x%08x\n" + "%04lx Tag Validation Token: 0x%08x\n" + " Calculated Image CRC32: 0x%08x\n" + " Calculated Tag CRC32: 0x%08x\n" "\n", offsetof(struct _IMAGE_TAG, imageNext), next, offsetof(struct _IMAGE_TAG, imageType), image_type[atol(pimage->imageType)], atol(pimage->imageType), offsetof(struct _IMAGE_TAG, imageSignature), (unsigned int)*pimage->imageSignature, offsetof(struct _IMAGE_TAG, imageLen), pimage->imageLen, len, offsetof(struct _IMAGE_TAG, imageValidationToken), ntohl( *((unsigned int *)(pimage->imageValidationToken)) ), - offsetof(struct _IMAGE_TAG, tagValidationToken), ntohl( *((unsigned int *)(pimage->tagValidationToken)) ) + offsetof(struct _IMAGE_TAG, tagValidationToken), ntohl( *((unsigned int *)(pimage->tagValidationToken)) ), + crc_payload, + crc_header ); } + + // next seek point will be end of current payload offset += len; heap_and_reap(buffer, 0, 0); -- 2.17.1