📄 out.c
字号:
#include "sysinc.h"
#include "command.h"
#include "term.h"
#include "tftp.h"
#include "channel.h"
#include "sys_layer.h"
#include "out.h"
#include "flash.h"
#include "zipmem.h"
#include <stdio.h>
#define BOOTRAM_LEN 0x400 /* 引导空间长度 */
#define DEBUG_INFO_INDEX 3 /* debug info section index */
#define DEBUG_ABBR_INDEX 2 /* debug abbrev section index */
unsigned char out_stat = STAT_BEGIN; /* 接收开始 */
struct out_info OutInfo; /* */
struct section_info * current_section_info;
extern void dwarf2_init( char * info_buffer, unsigned int info_buffer_size,
char * abbrev_buf, unsigned int abbrev_size );
extern void dwarf2_build_symtabs( void );
void init_debug( void )
{
static char flag = 0;
char * info_ptr,* abbrev_ptr; /* 压缩后的输出地址 */
int info_len, abbrev_len;
if( flag )
return;
flag = 1;
/* 分配解压需要的空间 */
info_ptr = ( char *)malloc( *( unsigned int *)( CFG_FLASH_BASE + DEBUG_INFO_UNZIPLEN ) );
if( info_ptr == 0 )
{
printf( "no memory malloc %s : %d", __FILE__, __LINE__ );
exit( 1 );
}
info_len = unzipmem(( char *)(*( unsigned int *)( CFG_FLASH_BASE + DEBUG_INFO_ADDR )),
(*( unsigned int *)( CFG_FLASH_BASE + DEBUG_INFO_LEN )), info_ptr );
/* 分配解压需要的空间 */
abbrev_ptr = ( char *)malloc(*( unsigned int *)( CFG_FLASH_BASE + DEBUG_ABBR_UNZIPLEN ) ); /* 分配abbrev节大小 */
if( info_ptr == 0 )
{
printf( "no memory malloc %s : %d", __FILE__, __LINE__ );
exit( 1 );
}
abbrev_len = unzipmem(( char *)(*( unsigned int *)( CFG_FLASH_BASE + DEBUG_ABBR_ADDR )),
(*( unsigned int *)( CFG_FLASH_BASE + DEBUG_ABBR_LEN )), abbrev_ptr );
dwarf2_init( info_ptr, info_len, abbrev_ptr, abbrev_len );
dwarf2_build_symtabs();
free( abbrev_ptr );
free( info_ptr );
}
void OutInit( void )
{
static int flag = 0;
if( ! flag )
{
memset(& OutInfo, 0, sizeof( struct out_info ));
flag = 1;
}
}
void FreeOut( void )
{
struct section_info * psection = OutInfo.psection;
while( psection && psection <= & OutInfo.psection[OutInfo.OutHead.num_sections - 1] )
{
if( psection->prawdata )
free( psection->prawdata );
psection ++;
}
if( OutInfo.psection )
free( OutInfo.psection );
memset(& OutInfo, 0, sizeof( struct out_info ));
}
/* 回调函数 ,由tftp中调用,构造out信息 */
int CreateOut ( unsigned int block, unsigned char * src, unsigned int len )
{
/* len 肯定等于TFTP_BLOCK_SIZE,除非已经是最后一块 */
unsigned int offset = block * TFTP_BLOCK_SIZE + TftpBlockWrapOffset;
unsigned int already_deal = 0;
unsigned int copyed = 0;
static unsigned int left = 0;
while( already_deal != len )
{
switch( out_stat )
{
case STAT_BEGIN:
/* 释放原来的空间 */
/* 拷贝out文件头部 */
memcpy(( unsigned char *)& OutInfo.OutHead, src, SIZE_OF_OUTHEAD );
already_deal += SIZE_OF_OUTHEAD;
if( OutInfo.OutHead.sizes_optheader != 0 ) /* 如果存在文件可选头部 */
{
memcpy(( unsigned char *)& OutInfo.OptHead, src, SIZE_OF_OPTHEAD );
already_deal += SIZE_OF_OPTHEAD;
}
/* 为节点分配空间 */
OutInfo.psection = ( struct section_info *)malloc( sizeof( struct section_info ) *
OutInfo.OutHead.num_sections );
memset( OutInfo.psection, 0, sizeof( struct section_info ) * OutInfo.OutHead.num_sections );
out_stat = STAT_RECV_SECTION;
OutInfo.section_recved = 0;
current_section_info = OutInfo.psection;
break;
case STAT_RECV_SECTION:
/* 接收section */
if( current_section_info->head_bytes_recved < SIZE_OF_SECTIONHEAD )
{
copyed = (( len - already_deal ) > ( SIZE_OF_SECTIONHEAD - current_section_info->head_bytes_recved )) ?
( SIZE_OF_SECTIONHEAD - current_section_info->head_bytes_recved ): ( len - already_deal );
memcpy(( char *)(( char *)& current_section_info->sectionhead +
current_section_info->head_bytes_recved ), src + already_deal, copyed );
current_section_info->head_bytes_recved += copyed;
already_deal += copyed;
}
else
{
/* 全部接收完毕 */
if( current_section_info->sectionhead.pointer_rawdata )
{
if( current_section_info->sectionhead.virtual_addr == CFG_FLASH_BASE )
{
/* 是启动扇区 */
current_section_info->prawdata = ( unsigned char *)malloc( BOOTRAM_LEN );
}
else
current_section_info->prawdata =
( unsigned char *)malloc( current_section_info->sectionhead.sizes );
}
OutInfo.section_recved ++;
if( OutInfo.section_recved != OutInfo.OutHead.num_sections )
current_section_info ++;
else
{
out_stat = STAT_SEARCH_RAWPOINTER;
current_section_info = OutInfo.psection; /* 指向最开始位置 */
}
}
break;
case STAT_SEARCH_RAWPOINTER:
if( current_section_info->sectionhead.pointer_rawdata != offset + already_deal &&
current_section_info->prawdata )
/* 查找原始数据开始位置 */
already_deal ++;
else
out_stat = STAT_RECV_RAWDATA;
break;
case STAT_RECV_RAWDATA:
/* 当前section原始数据还有多少没有收到 */
left = current_section_info->sectionhead.sizes - current_section_info->raw_bytes_recved ;
if( left != 0 && current_section_info->prawdata )
{
/* 表示需要拷贝的数据长度 */
copyed = (( len - already_deal ) > left ) ? left : ( len - already_deal );
memcpy( current_section_info->prawdata +
current_section_info->raw_bytes_recved, src + already_deal, copyed );
current_section_info->raw_bytes_recved += copyed;
already_deal += copyed;
}
else
{
current_section_info ++;
if( current_section_info > & OutInfo.psection[OutInfo.OutHead.num_sections - 1] )
{
out_stat = STAT_END;
}
else
{
out_stat = STAT_SEARCH_RAWPOINTER;
}
}
break;
default:
already_deal += len - already_deal;
break;
}
}
return 1;
}
typedef void (* go )( void );
/* 装载程序到FLASH */
int do_load( struct cmd_tbl_s * cmdtp, int flag, int argc, char * argv[] )
{
int len, i, idx_text, idx_bss, idx_far;
unsigned int low_addr = CFG_FLASH_BASE + CFG_FLASH_LEN, high_addr = 0;
#ifdef OS_WINDOWS
char * ptr_info,* ptr_abbrev;
unsigned int old_info_len, old_abbrev_len;
#endif
char * zip_out_ptr; /* 压缩后的输出地址 */
if( argc != 3 ) /* 参数数目检查 */
{
if( cmdtp->usage )
channel_printf( cmdtp->usage );
return 1;
}
out_stat = STAT_BEGIN;
/* 目标代码格式初始化 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -