152 lines
5.0 KiB
C
152 lines
5.0 KiB
C
|
/*
|
|||
|
************************************************************************************************************************
|
|||
|
* eGON
|
|||
|
* the Embedded GO-ON Bootloader System
|
|||
|
*
|
|||
|
* Copyright(C), 2006-2008, SoftWinners Microelectronic Co., Ltd.
|
|||
|
* All Rights Reserved
|
|||
|
*
|
|||
|
* File Name : check.c
|
|||
|
*
|
|||
|
* Author : Gary.Wang
|
|||
|
*
|
|||
|
* Version : 1.1.0
|
|||
|
*
|
|||
|
* Date : 2007.10.12
|
|||
|
*
|
|||
|
* Description : This file provides a function to check Boot0 and Boot1.
|
|||
|
*
|
|||
|
* Others : None at present.
|
|||
|
*
|
|||
|
*
|
|||
|
* History :
|
|||
|
*
|
|||
|
* <Author> <time> <version> <description>
|
|||
|
*
|
|||
|
* Gary.Wang 2007.10.12 1.1.0 build the file
|
|||
|
*
|
|||
|
************************************************************************************************************************
|
|||
|
*/
|
|||
|
#include "check.h"
|
|||
|
#include "spare_head.h"
|
|||
|
|
|||
|
//#pragma arm section code="check_magic"
|
|||
|
/********************************************************************************
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: check_magic
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD>: __s32 check_magic( __u32 *mem_base, const char *magic )
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ʹ<EFBFBD>á<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>: mem_base Boot<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
|
|||
|
* magic Boot<EFBFBD><EFBFBD>magic
|
|||
|
*<EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: CHECK_IS_CORRECT У<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ
|
|||
|
* CHECK_IS_WRONG У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*<EFBFBD><EFBFBD> ע:
|
|||
|
********************************************************************************/
|
|||
|
__s32 check_magic( __u32 *mem_base, const char *magic )
|
|||
|
{
|
|||
|
struct spare_boot_ctrl_head *bfh;
|
|||
|
unsigned char *p;
|
|||
|
|
|||
|
bfh = (struct spare_boot_ctrl_head *)mem_base;
|
|||
|
p = bfh->magic;
|
|||
|
if(strcmp((const char *)p, magic))
|
|||
|
{
|
|||
|
return CHECK_IS_WRONG;
|
|||
|
}
|
|||
|
return CHECK_IS_CORRECT;
|
|||
|
}
|
|||
|
|
|||
|
/********************************************************************************
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: check_sum
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD>: __s32 check_sum( __u32 *mem_base, __u32 size, const char *magic )
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ʹ<EFBFBD>á<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>: mem_base <EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD>ֽڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD>
|
|||
|
* size <EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>Ϊ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD>ֽڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD>
|
|||
|
*<EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: CHECK_IS_CORRECT У<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ
|
|||
|
* CHECK_IS_WRONG У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*<EFBFBD><EFBFBD> ע:
|
|||
|
********************************************************************************/
|
|||
|
__s32 check_sum( __u32 *mem_base, __u32 size )
|
|||
|
{
|
|||
|
__u32 *buf;
|
|||
|
__u32 count;
|
|||
|
__u32 src_sum;
|
|||
|
__u32 sum;
|
|||
|
struct spare_boot_ctrl_head *bfh;
|
|||
|
|
|||
|
bfh = (struct spare_boot_ctrl_head *)mem_base;
|
|||
|
/* <20><><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD> */
|
|||
|
src_sum = bfh->check_sum; // <20><>Boot_file_head<61>еġ<D0B5>check_sum<75><6D><EFBFBD>ֶ<EFBFBD>ȡ<EFBFBD><C8A1>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
|
|||
|
bfh->check_sum = STAMP_VALUE; // <20><>STAMP_VALUEд<45><D0B4>Boot_file_head<61>еġ<D0B5>check_sum<75><6D><EFBFBD>ֶ<EFBFBD>
|
|||
|
|
|||
|
count = size >> 2; // <20><> <20>֣<EFBFBD>4bytes<65><73>Ϊ<EFBFBD><CEAA>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
|
|||
|
sum = 0;
|
|||
|
buf = (__u32 *)mem_base;
|
|||
|
|
|||
|
do
|
|||
|
{
|
|||
|
sum += *buf++; // <20><><EFBFBD><EFBFBD><EFBFBD>ۼӣ<DBBC><D3A3><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
|
|||
|
sum += *buf++; // <20><><EFBFBD><EFBFBD><EFBFBD>ۼӣ<DBBC><D3A3><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
|
|||
|
sum += *buf++; // <20><><EFBFBD><EFBFBD><EFBFBD>ۼӣ<DBBC><D3A3><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
|
|||
|
sum += *buf++; // <20><><EFBFBD><EFBFBD><EFBFBD>ۼӣ<DBBC><D3A3><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
|
|||
|
}while( ( count -= 4 ) > (4-1) );
|
|||
|
|
|||
|
while( count-- > 0 )
|
|||
|
sum += *buf++;
|
|||
|
|
|||
|
bfh->check_sum = src_sum; // <20>ָ<EFBFBD>Boot_file_head<61>еġ<D0B5>check_sum<75><6D><EFBFBD>ֶε<D6B6>ֵ
|
|||
|
if( sum == src_sum )
|
|||
|
return CHECK_IS_CORRECT; // У<><D0A3><EFBFBD>ɹ<EFBFBD>
|
|||
|
else
|
|||
|
return CHECK_IS_WRONG; // У<><D0A3>ʧ<EFBFBD><CAA7>
|
|||
|
}
|
|||
|
|
|||
|
/********************************************************************************
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: check_file
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD>: __s32 check_file( __u32 *mem_base, __u32 size, const char *magic )
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ʹ<EFBFBD>á<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>: mem_base <EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD>ֽڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD>
|
|||
|
* size <EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>Ϊ<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD>ֽڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD>
|
|||
|
* magic magic number, <EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>ı<EFBFBD>ʶ<EFBFBD><EFBFBD>
|
|||
|
*<EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: CHECK_IS_CORRECT У<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ
|
|||
|
* CHECK_IS_WRONG У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*<EFBFBD><EFBFBD> ע:
|
|||
|
********************************************************************************/
|
|||
|
__s32 check_file( __u32 *mem_base, __u32 size, const char *magic )
|
|||
|
{
|
|||
|
if( check_magic( mem_base, magic ) == CHECK_IS_CORRECT
|
|||
|
&&check_sum( mem_base, size ) == CHECK_IS_CORRECT )
|
|||
|
return CHECK_IS_CORRECT;
|
|||
|
else
|
|||
|
return CHECK_IS_WRONG;
|
|||
|
}
|
|||
|
|
|||
|
__s32 gen_check_sum( void *boot_buf )
|
|||
|
{
|
|||
|
struct spare_boot_head_t *head_p;
|
|||
|
__u32 length;
|
|||
|
__u32 *buf;
|
|||
|
__u32 loop;
|
|||
|
__u32 i;
|
|||
|
__u32 sum;
|
|||
|
|
|||
|
head_p = (struct spare_boot_head_t *)boot_buf;
|
|||
|
length = head_p->boot_head.length;
|
|||
|
|
|||
|
if( ( length & 0x3 ) != 0 ) // must 4-byte-aligned
|
|||
|
return -1;
|
|||
|
buf = (__u32 *)boot_buf;
|
|||
|
head_p->boot_head.check_sum = STAMP_VALUE; // fill stamp
|
|||
|
loop = length >> 2;
|
|||
|
/* <20><><EFBFBD>㵱ǰ<E3B5B1>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ݵġ<DDB5>У<EFBFBD><D0A3><EFBFBD>͡<EFBFBD>*/
|
|||
|
for( i = 0, sum = 0; i < loop; i++ )
|
|||
|
sum += buf[i];
|
|||
|
/* write back check sum */
|
|||
|
head_p->boot_head.check_sum = sum;
|
|||
|
|
|||
|
return 0;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
|