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

📄 fstools.c

📁 一个免费的SMART CARD OS系统。
💻 C
字号:
/* ============================================================================   Project Name : jayaCard   Module Name  : proto/bios/fs/fstools.c   Version : $Id: fstools.c,v 1.30 2004/04/24 22:22:22 dgil Exp $	Description: Tools to manipulate EF/DF headers and bodies    The Original Code is jayaCard code.    The Initial Developer of the Original Code is Gilles Dumortier.	Portions created by the Initial Developer are Copyright (C) 2002-2004 the    Initial Developer. All Rights Reserved.    Contributor(s):    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program; see http://www.gnu.org/licenses/gpl.html   History Rev	Description   040603 dgil	wrote it from scratch   ============================================================================*/#include "precomp.h"#ifdef JAYA_FILESYSTEM/* ============================================================================    __fs_save_EF_context()    save current_EF context to header_file    secure: use global semaphore but transparent for caller   ========================================================================= */void __fs_save_EF_context(){    gGlobalSem--;    header_file_addr = current_EF_addr;    HAL_MEMCPY((jbyte xdata*)&header_file,(jbyte xdata*)&current_EF,sizeof(current_EF));    gGlobalSem--;}/* ============================================================================    __fs_restore_EF_context()    restore current_EF context from header_file    secure: use global semaphore but transparent for caller   ========================================================================= */void __fs_restore_EF_context(){    gGlobalSem--;    current_EF_addr = header_file_addr;    HAL_MEMCPY((jbyte xdata*)&current_EF,(jbyte xdata*)&header_file,sizeof(current_EF));    gGlobalSem--;}/* ============================================================================	__fs_load_DF_header()	Fill the current_DF header with the content from the EEPROM	Some error code in lasterr if any problem.   ========================================================================= */void __fs_load_DF_header(jword link){	LOG1("FSTOOLS","__fs_load_DF_header(): current_DF_addr=0x%.4X",link);	current_DF_addr = link;	HAL_CRC_INIT();	BIOS_INIT_STREAM(&streamSrc,READ_STREAM|CHKCRC_STREAM|SECURE_STREAM,link,SIZE_HEADER_FILE);	if ( BIOS_READ_STREAM((jbyte xdata*)&current_DF,&streamSrc,SIZE_HEADER_FILE) != JSEC_OK) {		if (lasterr == SUCCESS) BIOS_SETERR(ERR_READ_ERROR);		return;	}}/* ============================================================================	__fs_load_EF_header()	Fill the current_EF header with content from the EEPROM	Some error code in lasterr if any problem.   ========================================================================= */void __fs_load_EF_header(jword link){	LOG1("FSTOOLS","__fs_load_EF_header(): current_EF_addr=0x%.4X",link);	current_EF_addr = link;	HAL_CRC_INIT();	BIOS_INIT_STREAM(&streamSrc,READ_STREAM|CHKCRC_STREAM|SECURE_STREAM,link,SIZE_HEADER_FILE);	if (BIOS_READ_STREAM((jbyte xdata*)&current_EF,&streamSrc,SIZE_HEADER_FILE)!=JSEC_OK) {		if (lasterr == SUCCESS) BIOS_SETERR(ERR_READ_ERROR);		return;	}}/* ============================================================================	__fs_move_EF_2_DF_header()	Move EF header to DF header	Some error code in lasterr if any problem.   ========================================================================= */void __fs_move_EF_2_DF_header(void){	/* copy EF->DF header */	gGlobalSem = JSEC_OK;	HAL_MEMCPY((jbyte xdata*)&current_DF,(jbyte xdata*)&current_EF,SIZE_HEADER_FILE);	/* erase EF header */	gGlobalSem--;	HAL_ERASE_XRAM((jbyte xdata*)&current_EF,SIZE_HEADER_FILE);	/* update the addr of the header in EEPROM */	LOG2("FSTOOLS","__fs_move_EF_2_DF_header() current_DF_addr was: 0x%.4X is: 0x%.4X",current_DF_addr,current_EF_addr);	current_DF_addr = current_EF_addr;	current_EF_addr = NO_CURRENT_FILE;	/* check security */	if (gGlobalSem!=JSEC_SEM) {		LOG2("ATTACK","__fs_move_EF_2_DF_header() current_DF_addr was: 0x%.4X is: 0x%.4X under attack !",current_DF_addr,current_EF_addr);		HAL_HALT();	}}/* ============================================================================	__fs_save_EF_header()	Fill the EEPROM with the content from current_EF header	Some error code in lasterr if any problem.   ========================================================================= */void __fs_save_EF_header(void){	LOG1("FSTOOLS","__fs_save_EF_header(): current_EF_addr=0x%.4X",current_EF_addr);	HAL_CRC_INIT();	BIOS_INIT_STREAM(&streamSrc,WRITE_STREAM|CHKCRC_STREAM|SECURE_STREAM,current_EF_addr,SIZE_HEADER_FILE);	if (BIOS_WRITE_STREAM(&streamSrc,(jbyte xdata*)&current_EF,SIZE_HEADER_FILE)!=JSEC_OK) {		if (lasterr == SUCCESS) BIOS_SETERR(ERR_WRITE_ERROR);		return;	}}/* ============================================================================	__fs_save_DF_header()	Fill the EEPROM current_DF_addr with the content from current_DF header	Some error code in lasterr if any problem.   ========================================================================= */void __fs_save_DF_header(void){	LOG1("FSTOOLS","__fs_save_DF_header(): current_DF_addr=0x%.4X",current_DF_addr);	HAL_CRC_INIT();	BIOS_INIT_STREAM(&streamSrc,WRITE_STREAM|CHKCRC_STREAM|SECURE_STREAM,current_DF_addr,SIZE_HEADER_FILE);	if (BIOS_WRITE_STREAM(&streamSrc,(jbyte xdata*)&current_DF,SIZE_HEADER_FILE)!=JSEC_OK) {		if (lasterr == SUCCESS) BIOS_SETERR(ERR_WRITE_ERROR);		return;	}}/* ============================================================================	__fs_header_bodysize()	return the number of bytes of the file selected in header_file   ========================================================================= */jword __fs_header_bodysize(void){	switch (header_file.fdesc&FDESC_TYPE_MASK) {		case FDESC_TYPE_BINARY:			return header_file.u4.sizefile;		case FDESC_TYPE_CYCLIC:		case FDESC_TYPE_LINEAR:			return (header_file.u4.record.nummax*header_file.u4.record.size);		default:			break;	}	return 0;}/* ============================================================================	__fs_save_file_header()	Fill the EEPROM header_file_addr with the content from header_file header	Some error code in lasterr if any problem.   ========================================================================= */void __fs_save_file_header(void){	LOG4("FSTOOLS","__fs_save_file_header(): fid=%.4X current_file_addr=%.4X size=%d body=%.4X",			header_file.fid,			header_file_addr,			__fs_header_bodysize(),			header_file.u3.body_ef		);	HAL_CRC_INIT();	BIOS_INIT_STREAM(&streamSrc,WRITE_STREAM|CHKCRC_STREAM|SECURE_STREAM,header_file_addr,SIZE_HEADER_FILE);	if (BIOS_WRITE_STREAM(&streamSrc,(jbyte xdata*)&header_file,SIZE_HEADER_FILE)!=JSEC_OK) {		if (lasterr == SUCCESS) BIOS_SETERR(ERR_WRITE_ERROR);		return;	}}/* ============================================================================	__fs_current_EF_bodysize()	return the number of bytes of the file selected in current_EF   ========================================================================= */jword __fs_current_EF_bodysize(void){	switch (current_EF.fdesc&FDESC_TYPE_MASK) {		case FDESC_TYPE_BINARY:			return current_EF.u4.sizefile;		case FDESC_TYPE_CYCLIC:		case FDESC_TYPE_LINEAR:			return (current_EF.u4.record.nummax*current_EF.u4.record.size);		default:			break;	}	return 0;}/* ============================================================================	__bios_init_stream_from_current_EF()	initialize a stream using the current_EF   ========================================================================= */void __bios_init_stream_from_current_EF(BIOS_STREAM idata* stream){	LOCAL(jbyte,flags);	LOCAL(jbyte,exflags);	LOCAL(jbyte,lSec);	/* save previous gGlobalSem */	gBiosSem = gGlobalSem;	#ifdef PARANO_FLASH	flags = SECURE_STREAM;	#else	flags = 0;	#endif	exflags = 0;	/* check we can read the file */	gGlobalSem = JSEC_FAIL;    lSec = FS_CHECK_AC(ACC_CHECK_EF|ACC_READ);	gGlobalSem++;	if ((JSEC_OK == lSec) && (gGlobalSem == JSEC_SEM)) flags |= READ_STREAM;	if (gGlobalSem!=JSEC_SEM) {		LOG("ATTACK","__bios_init_stream_from_current_EF() #1 - check security failure !");		HAL_HALT();		return;	}	/* check we can write the file */	gGlobalSem = JSEC_FAIL;    lSec = FS_CHECK_AC(ACC_CHECK_EF|ACC_UPDATE);	gGlobalSem++;	if ((JSEC_OK == lSec) && (gGlobalSem == JSEC_SEM)) flags |= WRITE_STREAM;	if (gGlobalSem!=JSEC_SEM) {		LOG("ATTACK","__bios_init_stream_from_current_EF() #2 - check security failure !");		HAL_HALT();		return;	}    if ((current_EF.fdesc&FDESC_TYPE_MASK) == FDESC_TYPE_BINARY) {        /* check we can erase the file */        gGlobalSem = JSEC_FAIL;        lSec = FS_CHECK_AC(ACC_CHECK_EF|ACC_ERASE);        gGlobalSem++;        if ((JSEC_OK == lSec) && (gGlobalSem == JSEC_SEM)) exflags |= ERASE_STREAM;        LOG1("FSTOOLS","__bios_init_stream_from_current_EF() exflags=%.2X == ERASE(0x04) ? !",exflags);        if (gGlobalSem!=JSEC_SEM) {            LOG("ATTACK","__bios_init_stream_from_current_EF() #3 - check security failure !");            HAL_HALT();            return;        }    }	/* is it a transactional file ? => if so, create a transactional stream :-) */	#ifdef JAYA_TRANSACTION	if ((current_EF.fdesc&FDESC_TRANSACTIONAL) == FDESC_TRANSACTIONAL) {		LOG("FSTOOLS","__bios_init_stream_from_current_EF() - setup a transactional stream :-)");		exflags |= TRANS_STREAM;	}	#endif	/* is it an invalidated file ? */	#ifdef JAYACFG_FILESYSTEM_INVALIDATE	if ((current_EF.fdesc&FDESC_INVALIDATED) == FDESC_INVALIDATED) {		LOG("FSTOOLS","__bios_init_stream_from_current_EF() - this is an invalidated file :-(");		BIOS_SETERR(ERR_FILE_INVALIDATED);	}	#endif	BIOS_INITEX_STREAM(stream,flags,exflags,current_EF.u3.body_ef,__fs_current_EF_bodysize());	LOG5("FSTOOLS","__bios_init_stream_from_current_EF() flags=%.2X exflags=%.2X start=%.4X pos=%.4X size=%d",stream->m_flags,stream->m_flags2,stream->m_start,stream->m_pos,stream->m_size);	/* restore gGlobalSem */	gGlobalSem = gBiosSem;	gGlobalSem++;}/* ============================================================================	__fs_isreserved_fid	return jtrue if the given fid is reserved and can't be used by the user   ========================================================================= */#ifdef JAYACFG_DO_NOT_SUPPORT_RETURNTYPE_JBOOLjbyte __fs_isreserved_fid(jword id)#elsejbool __fs_isreserved_fid(jword id)#endif{	if (id==FID_MF) return jtrue;	if (id==FID_BOGUS) return jtrue;	if (id==FID_RFU) return jtrue;	if (id==FID_CURRENT) return jtrue;	return jfalse;}/* ============================================================================	__fs_check_DF	set lasterr to ERR_NO_DF_SELECTED or ERR_FAULT is the check of the	current DF failed   ========================================================================= */void __fs_check_DF(void){	if (current_DF_addr == NO_CURRENT_FILE) {		LOG("FSTOOLS","__fs_check_DF(): current_DF_addr == NO_CURRENT_FILE !");		BIOS_SETERR(ERR_NO_DF_SELECTED);	} else {        /* DF invalidated ? */        #ifdef JAYACFG_FILESYSTEM_INVALIDATE        if ( (current_DF.fdesc&FDESC_INVALIDATED) == FDESC_INVALIDATED ) {            LOG1("FSTOOLS","__fs_check_DF() fid=0x%.4X INVALIDATED !",current_DF.fid);            BIOS_SETERR(ERR_FILE_INVALIDATED);            return;        }        #endif        /* __x XXX add more checking */    }}/* ============================================================================    __manage_update_once()   ========================================================================= */void __manage_update_once(void){    if (lasterr == SUCCESS) {        if ( (current_EF.fdesc&FDESC_UPDATE_ONCE) == FDESC_UPDATE_ONCE) {            LOG1("FSTOOLS","__manage_update_once() update once fid=%.4X ",current_EF.fid);            /* change the UPDATE and ERASE/APPEND access conditions to never */            current_EF.ac[0] &= 0xF0;            current_EF.ac[1] &= 0xF0;            /* save the new header */            __fs_save_EF_header();        }    }}/* ============================================================================	__fs_erase_file()	Erase the file content with 0x00   ========================================================================= */void __fs_erase_file(void){    HAL_ERASE_EEPROM(header_file.u3.body_ef,__fs_header_bodysize(),0x00);}/* =========================================================================	That's all folks !   ========================================================================= */#endif/* JAYA_FILESYSTEM */

⌨️ 快捷键说明

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