📄 haleep.c
字号:
/* ============================================================================ Project Name : jayaCard Module Name : proto/hal/simu/haleep.c Version : $Id: haleep.c,v 1.20 2004/04/23 20:29:16 dgil Exp $ Description: EEPROM read/write of byte/buffer from/into simulated EEPROM Simulation of the EEPROM area(s) with: EEPROM direct/inverse cell OTP management MANUFACTURING DATA area 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 072102 dgil wrote it from scratch ============================================================================*/#include "precomp.h"/* ========================================================================= gEEPROM - array to store the EEPROM bytes inside the simulator ========================================================================= */jbyte gEEPROM[SIMU_EEPROM_SIZE];/* ========================================================================= __hal_eeprom_read_byte() Read a single byte from the EEPROM !this is an unsecure version! BIOS will handle the security around this primitive ========================================================================= */jbyte __hal_eeprom_read_byte(jword src){ jbyte b; __hal_eeprom_read(&b,src,sizeof(b)); return b;}/* ========================================================================= __hal_eeprom_read_word() Read a word from the EEPROM - endian independent !this is an unsecure version! BIOS will handle the security around this primitive ========================================================================= */jword __hal_eeprom_read_word(jword src){ return (((jword)__hal_eeprom_read_byte(src))<<8) + ((jword)__hal_eeprom_read_byte(src+1));}/* ========================================================================= __hal_eeprom_read() Read a buffer from the EEPROM !this is an unsecure version! BIOS will handle the security around this primitive ========================================================================= */void __hal_eeprom_read(jbyte xdata* dst, jword src,jbyte len){ if (len==0) { LOG("EEPERR","__hal_eeprom_read() - len == 0 !"); HAL_HALT(); return; } while (len>0) { if ((src>=BASE_MANUFACTURING_DATA) && (src<=END_MANUFACTURING_DATA)) { /* read Manufacturing Data */ *dst = gEEPROM[src]; } else if ((src>=BASE_OTP) && (src<=END_OTP)) { /* read OTP */ *dst = gEEPROM[src]; } else if ((src>=ADDR_CHIP_UUID) && (src<=(ADDR_CHIP_UUID+7))) { /* read chipUUID */ *dst = gEEPROM[src]; } else if ((src>=ADDR_RANDOM) && (src<(ADDR_RANDOM+SIZE_RANDOM))) { /* read random area */ #if defined(JAYACFG_EEPROM_INVERSED) *dst = ~gEEPROM[src]; #else *dst = gEEPROM[src]; #endif } else if ((src>=BASE_EEPROM) && (src<=END_EEPROM)) { /* read EEPROM */ #if defined(JAYACFG_EEPROM_INVERSED) *dst = ~gEEPROM[src]; #else *dst = gEEPROM[src]; #endif } else { /* outside EEPROM ! */ LOG1("EEPERR","__hal_eeprom_read() - src %.4X is outside the EEPROM area !",src); HAL_HALT(); return; } dst++; src++; len--; }}/* ========================================================================= __hal_eeprom_write_byte() Write a single byte to the EEPROM. Returns EEPROM_HAL_OK if the write was successful, EEPROM_HAL_ERROR otherwise. Note that this primitive *can* write to write-once and OTP areas !this is an unsecure version! BIOS will handle the security around this primitive ========================================================================= */jbyte __hal_eeprom_write_byte(jword dest,jbyte val){ return __hal_eeprom_write(dest,&val,sizeof(val));}/* ========================================================================= __hal_eeprom_write_word() Write a word to the EEPROM. Returns EEPROM_HAL_OK if the write was successful, EEPROM_HAL_ERROR otherwise. Note that this primitive *can* write to write-once and OTP areas !this is an unsecure version! BIOS will handle the security around this primitive ========================================================================= */jbyte __hal_eeprom_write_word(jword dest,jword val){ if (__hal_eeprom_write_byte(dest,HIBYTE(val))==JSEC_FAIL) return EEPROM_HAL_ERROR; return __hal_eeprom_write_byte(dest+1,LOBYTE(val));}/* ========================================================================= __hal_eeprom_write() Write a buffer to the EEPROM. Returns EEPROM_HAL_OK if the write was successful, EEPROM_HAL_ERROR otherwise. Note that this primitive *can* write to write-once and OTP areas !this is an unsecure version! BIOS will handle the security around this primitive simulator note: this function never failed in the simulator. ========================================================================= */jbyte __hal_eeprom_write(jword dst, jbyte xdata* src,jbyte len){ while (len>0) { if ((dst>=BASE_MANUFACTURING_DATA) && (dst<=END_MANUFACTURING_DATA)) { /* write Manufacturing Data !! Note: we can change the MSK and the MSK attempt counter */ if ((dst>=ADDR_MSK) && (dst<=ADDR_MSK_ATTEMPT)) { LOG2("MSK","MSK[%d] <- 0x%.2X",dst-ADDR_MSK,*src); gEEPROM[dst] = *src; } else { LOG1("EEPERR","*** __hal_eeprom_write(): dst 0x%X is inside MANUFACTURING DATA area / can't write !\n",dst); return EEPROM_HAL_OK; } } else if ((dst>=BASE_OTP) && (dst<=END_OTP)) { /* write OTP - BIT write once */ gEEPROM[dst] |= *src; } else if ((dst>=ADDR_RANDOM) && (dst<(ADDR_RANDOM+SIZE_RANDOM))) { /* write Random Area */ #if defined(JAYACFG_EEPROM_INVERSED) gEEPROM[dst] = ~(*src); #else gEEPROM[dst] = *src; #endif } else if ((dst>=ADDR_CHIP_UUID) && (dst<=(ADDR_CHIP_UUID+7))) { /* can't write chipUUID */ LOG1("EEPERR","*** __hal_eeprom_write(): dst 0x%X is inside chip UUID area / can't write !\n",dst); return EEPROM_HAL_OK; } else if ((dst>=BASE_EEPROM) && (dst<=END_EEPROM)) { /* write EEPROM */ #if defined(JAYACFG_EEPROM_INVERSED) gEEPROM[dst] = ~(*src); #else gEEPROM[dst] = *src; #endif } else { /* outside EEPROM ! */ LOG1("EEPERR","*** __hal_eeprom_write(): dst 0x%X is outside the EEPROM area !\n",dst); return EEPROM_HAL_OK; } src++; dst++; len--; } return EEPROM_HAL_OK;}/* ========================================================================= That's all folks ! ========================================================================= */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -