Skip to content

Commit

Permalink
[crc]add crc32 check for loader
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjunjiang committed Sep 4, 2024
1 parent 4c44447 commit 9f4187d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
71 changes: 51 additions & 20 deletions loader.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
#include <loader.h>

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));
Expand All @@ -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;
}
5 changes: 3 additions & 2 deletions loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {
#endif

#include <x.h>
#include <crc32.h>
#include <misc.h>

enum rkloader_entry_type_t {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 9f4187d

Please sign in to comment.