⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cas_demo.c

📁 EMU_FTAbox_ref441328_code_source ali m3327
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*
*  ALi 
*
*  File: cas_demo.c
*
*  Description: 
*             
*  History:
*      Date        		Author         	Version   	Comment
*      ====        	      ======         	=======   	=======
*  		
*
****************************************************************************/


//#include <stdio.h>
//#include <stdlib.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>

//#include "cas_api.h"
#include "cas_key_db.h" /* you can change key database by yourself */

void* memcpy(void *dest,const void *src, unsigned int len)
{
	return ali_memcpy (dest,src, len);
}

int  memcmp(const void *buf1,const void *buf2,unsigned int  len)
{
	return ali_memcmp (buf1,buf2,len);
}

void* memset(void *dest, int c,unsigned int  len)
{
	return ali_memset (dest,c,len);
}

void *cas_malloc(unsigned long size)
{
	char str[32];
	
	sprintf(str, "malloc %d\n", size);

	if (cas_get_debug_level() >= 1)
	{	
		cas_print(str);
	}
	
	/* fix me */
	return malloc(size);
}

void cas_free(void *ptr)
{
	/* fix me */
	free(ptr);
}

void cas_sleep(unsigned long msec)
{
	/* fix me */
	osal_delay(msec);
}

void cas_print(char* str)
{
	/* fix me */
	libc_printf( str );
	
}

void cas_print_buf(unsigned char* buf, int len)
{
	int	i;
	char str[16];

	for ( i = 0; i < len; i++ ) {
		sprintf(str, "%02X ", buf[i] );
		cas_print( str );
	}
	cas_print( "\r\n" );
}



int cas_get_init_key_size(void)
{
	return (sizeof( newpatch_cas_key_db ) / sizeof( cas_key_db ));
}

char *cas_get_provider_name(unsigned long provider, unsigned short ca_id)
{
	int i;

	for(i=0; i< sizeof(newpatch_provider_name_db)/sizeof(*newpatch_provider_name_db); i++)
	{
		if ((newpatch_provider_name_db[i].provider == provider) && (newpatch_provider_name_db[i].ca_id == ca_id))
			return (char *)newpatch_provider_name_db[i].name;
	}

	for(i=0; i< sizeof(newpatch_provider_name_db)/sizeof(*newpatch_provider_name_db); i++)
	{
		if ((provider_name_db[i].provider == provider) && (newpatch_provider_name_db[i].ca_id == (ca_id&0xff00)))
			return (char *)newpatch_provider_name_db[i].name;
	}

	return "unknown";
}

unsigned char dummy_flash_memory[0x10000];
void cas_write_flash(unsigned char *key_db, unsigned long key_db_size)
{
    /* fix me */
	/* write key database to flash memory */
	/* normal key_db_size is 0x10000 */
	if(key_db_size<0xf000)
		api_cas_mem_write(key_db,key_db_size); //add by zkc	
	else
		api_cas_mem_write(key_db,0xf000);
	MEMCPY(dummy_flash_memory, key_db, key_db_size);
}

void cas_read_flash(unsigned char *key_db, unsigned long key_db_size)
{
    /* fix me */
	/* read key database from flash memory */
	/* normal key_db_size is 0x10000 */
	if(key_db_size<0xf000)
		{
		api_cas_mem_read(key_db,key_db_size);//add by zkc
		MEMCPY(key_db, dummy_flash_memory, key_db_size);
		}
	else
	{
		api_cas_mem_read(key_db,0xf000);//add by zkc
		MEMCPY(key_db, dummy_flash_memory, 0xf000);
	}
}

/* you can display some msg, if this function is called */
void cas_send_msg(unsigned long msg, unsigned long param1, unsigned long param2, unsigned long param3)
{
	char str[128];
	
	switch(msg)
	{
		case CAS_SC_INSERTED:
			sprintf(str, "smart card[%d] cas_id %x inserted\n", param1, param2);
			cas_print(str);

			if (param2 == 0)
			{
				sprintf(str, "invalid smart card[%d] inserted\n", param1);
				cas_print(str);
			}

			break;

		case CAS_SC_EXTRACTED:
			sprintf(str, "smart card [%d] extracted\n", param1);
			cas_print(str);
			
			break;

		case CAS_SC_INIT_OK:
			sprintf(str, "smart card [%d] cas_id %x initialized\n", param1, param2);
			cas_print(str);
			break;

		case CAS_SC_NO_ACCESS:
			cas_print("no access with this smart card\n");
			break;

		case CAS_SC_RESTART:
            /* fix me */
			cas_print("you should restart ecm/emm\n");
			break;

		default:
			break;
	}
}

int sc_is_inserted(int sc_id)
{
    /* fix me */
	return 1;
}

int sc_read(int sc_id, unsigned char *data, int length, int *read_bytes)
{
	int result = 0;

	*read_bytes = 0;
	
    /* fix me */
	/* result = smart_card_read(sc_id, data, length, read_bytes); */
	
	if (result || *read_bytes)
	{
		result = 1;
		if (cas_get_debug_level() >= 2)
		{
			cas_print("sc read:");
			cas_print_buf(data, *read_bytes);
		}
	}
	else
	{
		cas_print("sc read error\n");
	}
	return result;
}

int sc_write(int sc_id, unsigned char *data, int length)
{
	int result = 0;
    
	/* fix me */
	/* result = smart_card_write(sc_id, data, length); */

	if (result)
	{
		if (cas_get_debug_level() >= 2)
		{
			cas_print("sc write:");
			cas_print_buf(data, length);
		}
	}
	else
	{
		cas_print("sc write error\n");
	}

	return result;
}

/* don't change this funtion */
static int is_t14_checksum(unsigned char *buffer, int count)
{
	int result = 0;

	unsigned char checksum = buffer[0];
	int i;

	for (i = 1; i < count - 1; i++)
		checksum ^= buffer[i];
	checksum ^= 0x3F;

	if (checksum == buffer[count - 1])
		result = 1;
	else
	{
		result = 0;
	}

	return result;
}

/* don't change this funtion */
static unsigned char gen_t14_checksum(unsigned char *buffer, int count)
{
	unsigned char checksum = buffer[0];
	int i;

	for (i = 1; i < count - 1; i++)
		checksum ^= buffer[i];
	checksum ^= 0x3F;

	return checksum;
}


#define ISO_WAIT	0x60
#define SW_OK		0x90

int cas_sc_t0(int sc_id, unsigned char *Buf, int LEN, unsigned char *SW1, unsigned char *SW2)
{
	int done, ACK, ack;
	int counter = 0;
	int read_bytes=0;
	int result = 0;

	int INS = Buf[1];
	unsigned char buf[5];

	if ((LEN == 4) && (Buf[0] == 0xFF))
	{
		sc_write(sc_id, Buf, 4);
		result = sc_read(sc_id, buf, 4, &read_bytes);
		if (result == 0)
			goto exit_cas_sc_t0;
		if (MEMCMP(buf, Buf, 4) == 0)
			result = 1;
		else
			result = 0;
		goto exit_cas_sc_t0;
	}
	else if (LEN >= 5)
	{
		if (sc_is_inserted(sc_id))
		{
			sc_write(sc_id, Buf, 5);
		}
		else
		{
			result = 0;
			goto exit_cas_sc_t0;
		}
	}
	ack = -1;
	done = 0;
	if (sc_is_inserted(sc_id))
	{
		result = sc_read(sc_id, buf, 1, &read_bytes);
    	if (result == 0)
    	{
    		goto exit_cas_sc_t0;
    	}
	}
	else
	{
		result = 0;
		goto exit_cas_sc_t0;
	}

	do
	{
		ACK = buf[0];
		if (ACK == ISO_WAIT)
		{
			if (sc_is_inserted(sc_id))
			{
				result = sc_read(sc_id, buf, 1, &read_bytes);
				if (result == 0)
					goto exit_cas_sc_t0;
			}
			else
			{
				result = 0;
				goto exit_cas_sc_t0;
			}

			done = 0;
			continue;
		}
		else if ((ACK & 0xF0) == ISO_WAIT || (ACK & 0xF0) == SW_OK)
		{
			*SW1 = ACK;
			if (sc_is_inserted(sc_id))
			{
				result = sc_read(sc_id, buf, 1, &read_bytes);
    			if (result == 0)
    				goto exit_cas_sc_t0;
			}
			else
			{
				result = 0;
				goto exit_cas_sc_t0;
			}

			*SW2 = buf[0];
			result = 1;
			goto exit_cas_sc_t0;
		}
		else if (ACK == INS)
		{
			ack = ACK;
			if (LEN == 5 && Buf[4] == 0)
			{
				if (sc_is_inserted(sc_id))
				{
					result = sc_read(sc_id, buf, 1, &read_bytes);
    				if (result == 0)
    					goto exit_cas_sc_t0;
				}
				else
				{
					result = 0;
					goto exit_cas_sc_t0;
				}
			}
			else if ((LEN == 5) && (Buf[4] > 0))
			{
				if (sc_is_inserted(sc_id))
				{
					result = sc_read(sc_id, Buf + 5, Buf[4], &read_bytes);
    				if (result == 0)
    					goto exit_cas_sc_t0;
				}
				else
				{
					result = 0;
					goto exit_cas_sc_t0;
				}

				if (sc_is_inserted(sc_id))
				{
					result = sc_read(sc_id, buf, 1, &read_bytes);
    				if (result == 0)
    					goto exit_cas_sc_t0;
				}
				else
				{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -