Files
2017-10-13 14:02:55 +03:00

179 lines
5.8 KiB
C

/*
************************************************************************************************************************
* Boot rom
* Seucre Boot
*
* Copyright(C), 2006-2013, AllWinners Microelectronic Co., Ltd.
* All Rights Reserved
*
* File Name : Base.h
*
* Author : glhuang
*
* Version : 0.0.1
*
* Date : 2013.09.05
*
* Description :
*
* Others : None at present.
*
*
* History :
*
* <Author> <time> <version> <description>
*
* glhuang 2013.09.05 0.0.1 build the file
*
************************************************************************************************************************
*/
#include "common.h"
#include "private_toc.h"
#include <private_uboot.h>
//#pragma arm section code="check_magic"
/********************************************************************************
*函数名称: check_magic
*函数原型: __s32 check_magic( __u32 *mem_base, const char *magic )
*函数功能: 使用“算术和”来校验内存中的一段数据
*入口参数: mem_base Boot文件在内存中的起始地址
* magic Boot的magic
*返 回 值: CHECK_IS_CORRECT 校验正确
* CHECK_IS_WRONG 校验错误
*备 注:
********************************************************************************/
__s32 check_magic( __u32 *mem_base, const char *magic )
{
struct spare_boot_head_t *bfh;
bfh = (struct spare_boot_head_t *)mem_base;
if(!(strncmp((const char *)bfh->boot_head.magic, magic, 8)))
{
return 0;
}
return -1;
}
//#pragma arm section
//#pragma arm section code="check_sum"
/********************************************************************************
*函数名称: check_sum
*函数原型: __s32 check_sum( __u32 *mem_base, __u32 size, const char *magic )
*函数功能: 使用“算术和”来校验内存中的一段数据
*入口参数: mem_base 待校验的数据在内存中的起始地址(必须是4字节对齐的)
* size 待校验的数据的个数(以字节为单位,必须是4字节对齐的)
*返 回 值: CHECK_IS_CORRECT 校验正确
* CHECK_IS_WRONG 校验错误
*备 注:
********************************************************************************/
__s32 check_sum( __u32 *mem_base, __u32 size )
{
__u32 *buf;
__u32 count;
__u32 src_sum;
__u32 sum;
struct spare_boot_head_t *bfh;
bfh = (struct spare_boot_head_t *)mem_base;
/* 生成校验和 */
src_sum = bfh->boot_head.check_sum; // 从Boot_file_head中的“check_sum”字段取出校验和
bfh->boot_head.check_sum = STAMP_VALUE; // 将STAMP_VALUE写入Boot_file_head中的“check_sum”字段
count = size >> 2; // 以 字(4bytes)为单位计数
sum = 0;
buf = (__u32 *)mem_base;
do
{
sum += *buf++; // 依次累加,求得校验和
sum += *buf++; // 依次累加,求得校验和
sum += *buf++; // 依次累加,求得校验和
sum += *buf++; // 依次累加,求得校验和
}while( ( count -= 4 ) > (4-1) );
while( count-- > 0 )
sum += *buf++;
bfh->boot_head.check_sum = src_sum; // 恢复Boot_file_head中的“check_sum”字段的值
printf("sum=%x\n", sum);
printf("src_sum=%x\n", src_sum);
if( sum == src_sum )
return 0; // 校验成功
else
return -1; // 校验失败
}
//#pragma arm section
//#pragma arm section code="check_file"
/********************************************************************************
*函数名称: check_file
*函数原型: __s32 check_file( __u32 *mem_base, __u32 size, const char *magic )
*函数功能: 使用“算术和”来校验内存中的一段数据
*入口参数: mem_base 待校验的数据在内存中的起始地址(必须是4字节对齐的)
* size 待校验的数据的个数(以字节为单位,必须是4字节对齐的)
* magic magic number, 待校验文件的标识码
*返 回 值: CHECK_IS_CORRECT 校验正确
* CHECK_IS_WRONG 校验错误
*备 注:
********************************************************************************/
__s32 check_file( __u32 *mem_base, __u32 size, const char *magic )
{
if( check_magic( mem_base, magic ) == 0
&&check_sum( mem_base, size ) == 0 )
return 0;
else
return -1;
}
int verify_addsum( void *mem_base, __u32 size )
{
__u32 *buf;
__u32 count;
__u32 src_sum;
__u32 sum;
struct spare_boot_head_t *bfh;
bfh = (struct spare_boot_head_t *)mem_base;
/* 生成校验和 */
src_sum = bfh->boot_head.check_sum; // 从Boot_file_head中的“check_sum”字段取出校验和
bfh->boot_head.check_sum = STAMP_VALUE; // 将STAMP_VALUE写入Boot_file_head中的“check_sum”字段
count = size >> 2; // 以 字(4bytes)为单位计数
sum = 0;
buf = (__u32 *)mem_base;
do
{
sum += *buf++; // 依次累加,求得校验和
sum += *buf++; // 依次累加,求得校验和
sum += *buf++; // 依次累加,求得校验和
sum += *buf++; // 依次累加,求得校验和
}while( ( count -= 4 ) > (4-1) );
while( count-- > 0 )
sum += *buf++;
bfh->boot_head.check_sum = src_sum; // 恢复Boot_file_head中的“check_sum”字段的值
printf("sum=%x\n", sum);
printf("src_sum=%x\n", src_sum);
if( sum == src_sum )
return 0; // 校验成功
else
return -1; // 校验失败
}