From 9f4187d790eb5c4271afd5f167fe3b2608c162b2 Mon Sep 17 00:00:00 2001 From: JianjunJiang <8192542@qq.com> Date: Wed, 4 Sep 2024 11:09:55 +0800 Subject: [PATCH] [crc]add crc32 check for loader --- loader.c | 71 ++++++++++++++++++++++++++++++++++++++++---------------- loader.h | 5 ++-- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/loader.c b/loader.c index 3cea4e0..af8df22 100644 --- a/loader.c +++ b/loader.c @@ -1,5 +1,25 @@ #include +uint32_t rkloader_read_le32(void * addr) +{ + uint8_t * p = (uint8_t *)addr; + + return (uint32_t)((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0] << 0)); +} + +char * loader_wide2str(char * str, uint8_t * wide, int len) +{ + int i; + + for(i = 0; i < len; i++) + { + char c = wide[i * 2 + 0] & 0xff; + str[i] = (c && isprint(c)) ? c : '\0'; + } + str[i] = '\0'; + return str; +} + struct rkloader_ctx_t * rkloader_ctx_alloc(const char * filename) { struct rkloader_ctx_t * ctx = calloc(1, sizeof(struct rkloader_ctx_t)); @@ -25,41 +45,52 @@ struct rkloader_ctx_t * rkloader_ctx_alloc(const char * filename) free(ctx); return NULL; } + ctx->nentry = ctx->header->code471_num + ctx->header->code472_num + ctx->header->loader_num; + if(ctx->nentry <= 0) + { + if(ctx->buffer) + free(ctx->buffer); + if(ctx) + free(ctx); + return NULL; + } + for(int i = 0; i < ctx->nentry; i++) { ctx->entry[i] = (struct rkloader_entry_t *)(ctx->buffer + sizeof(struct rkloader_header_t) + sizeof(struct rkloader_entry_t) * i); } - return ctx; -} - -void rkloader_ctx_free(struct rkloader_ctx_t * ctx) -{ - if(ctx) + struct rkloader_entry_t * e = ctx->entry[ctx->nentry - 1]; + uint32_t len = rkloader_read_le32(&e->data_offset) + rkloader_read_le32(&e->data_size); + if(ctx->length != len + 4) { if(ctx->buffer) free(ctx->buffer); - free(ctx); + if(ctx) + free(ctx); + return NULL; } -} -uint32_t rkloader_read_le32(void * addr) -{ - uint8_t * p = (uint8_t *)addr; + uint32_t crc32 = 0x0; + if(crc32_sum(crc32, (const uint8_t *)ctx->buffer, len) != rkloader_read_le32((char *)ctx->buffer + len)) + { + if(ctx->buffer) + free(ctx->buffer); + if(ctx) + free(ctx); + return NULL; + } - return (uint32_t)((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0] << 0)); + return ctx; } -char * loader_wide2str(char * str, uint8_t * wide, int len) +void rkloader_ctx_free(struct rkloader_ctx_t * ctx) { - int i; - - for(i = 0; i < len; i++) + if(ctx) { - char c = wide[i * 2 + 0] & 0xff; - str[i] = (c && isprint(c)) ? c : '\0'; + if(ctx->buffer) + free(ctx->buffer); + free(ctx); } - str[i] = '\0'; - return str; } diff --git a/loader.h b/loader.h index 6521adc..0bbca73 100644 --- a/loader.h +++ b/loader.h @@ -6,6 +6,7 @@ extern "C" { #endif #include +#include #include enum rkloader_entry_type_t { @@ -61,10 +62,10 @@ struct rkloader_ctx_t { int nentry; }; -struct rkloader_ctx_t * rkloader_ctx_alloc(const char * filename); -void rkloader_ctx_free(struct rkloader_ctx_t * ctx); uint32_t rkloader_read_le32(void * addr); char * loader_wide2str(char * str, uint8_t * wide, int len); +struct rkloader_ctx_t * rkloader_ctx_alloc(const char * filename); +void rkloader_ctx_free(struct rkloader_ctx_t * ctx); #ifdef __cplusplus }