📄 protectp.c
字号:
/*
* $Log: V:/Flite/archives/TrueFFS5/Src/PROTECTP.C_V $
*
* Rev 1.20 May 01 2002 19:04:26 oris
* Bug fix - no support for DiskOnChip Millennium Plus 16MB
*
* Rev 1.19 Apr 30 2002 17:30:02 oris
* Bug fix - Bad second DPS unit offset in protectionSet routine.
*
* Rev 1.18 Apr 15 2002 07:38:44 oris
* Added static qualifier for private functions (findChecksum and makeDPS).
* Removed readDPS and writeDPS routine prototypes (no longer exist).
* Added setStickyBit routine for DiskOnChip Plus 128Mbit.
*
* Rev 1.17 Jan 28 2002 21:26:24 oris
* Removed the use of back-slashes in macro definitions.
*
* Rev 1.16 Jan 17 2002 23:04:54 oris
* Replaced docsysp include directive with docsys.
* Changed the use of vol (macro *pVol) to *flash.
* Add support for DiskOnChip Millennium Plus 16MB :
* - Copy extra area of IPL independently of the EDC to copy Strong arm mark.
* - DPS 0 and 1 location where changed - affects protectionSet routine.
* Bug fix - Wrong usage of findChecksum, caused the use of the second copy of the DPS instead of the first.
*
* Rev 1.15 Sep 24 2001 18:24:18 oris
* removed ifdef and forced using flRead8bitRegPlus instead of reading with flRead16bitRegPlus.
*
* Rev 1.14 Sep 15 2001 23:47:56 oris
* Remove all 8-bit access to uneven addresses.
*
* Rev 1.13 Jul 16 2001 17:41:54 oris
* Ignore write protection of the DPSs.
*
* Rev 1.12 Jul 13 2001 01:09:26 oris
* Bug fix for protection boundaries when using Millennium Plus devices that can not access a single byte.
* Added send default key before trying a protection violation command.
* Bug fix - bad IPL second copy offset.
*
* Rev 1.11 May 16 2001 21:21:42 oris
* Removed warnings.
*
* Rev 1.10 May 09 2001 00:35:48 oris
* Bug fix - Lock asserted was reported opposite of the real state.
* Bug fix - Make sure to return "key inserted" if the partition is not read\write protected.
* This is to enable a partition that does not span over all of the media floors to return "key inserted".
*
* Rev 1.9 May 06 2001 22:42:18 oris
* Bug fix - insert key does not try to insert key to a floor that is not read\write protected.
* Bug fix - protection type does not return key inserted is one of the floors key is not inserted.
* Bug fix - set protection no longer clears the IPL.
* redundant was misspelled.
*
* Rev 1.8 May 01 2001 14:24:56 oris
* Bug fix - CHANGEABLE_PRTOECTION was never reported.
*
* Rev 1.7 Apr 18 2001 17:19:02 oris
* Bug fix - bad status code returned by protection set routine du to calling changaInterleave while in access error.
*
* Rev 1.6 Apr 18 2001 09:29:32 oris
* Bug fix - remove key routine always return bad status code.
*
* Rev 1.5 Apr 16 2001 13:58:28 oris
* Removed warrnings.
*
* Rev 1.4 Apr 12 2001 06:52:32 oris
* Changed protectionBounries and protectionSet routine to be floor specific.
*
* Rev 1.3 Apr 10 2001 23:56:30 oris
* Bug fix - protectionBounries routine - floor did not change.
* Bug fix - protectionSet routine - floors with no protected areas were not updated.
* Bug fix - protectionBounries routine - bad paranthesis in MAX calculation.
*
* Rev 1.2 Apr 09 2001 19:04:24 oris
* Removed warrnings.
*
*/
/*******************************************************************
*
* DESCRIPTION: MTD protection mechanism routines for the MDOC32
*
* AUTHOR: arie tamam
*
* HISTORY: created november 14, 2000
*
*******************************************************************/
/** include files **/
#include "mdocplus.h"
#include "protectp.h"
#include "docsys.h"
/** local definitions **/
/* default settings */
/** external functions **/
/** external data **/
/** internal functions **/
static byte findChecksum(byte * buffer, word size);
static void makeDPS(CardAddress addressLow, CardAddress addressHigh,
byte FAR1* key , word flag, byte* buffer,
byte flashType);
#define MINUS_FLOORSIZE(arg) ((arg > NFDC21thisVars->floorSize) ? arg - NFDC21thisVars->floorSize : 0)
/** public data **/
/** private data **/
/** public functions **/
#ifdef HW_PROTECTION
/**********/
/* Macros */
/**********/
/* check if key is correct */
#define isArea0Protected(flash) (((flRead8bitRegPlus(flash,NdataProtect0Status) & PROTECT_STAT_KEY_OK_MASK) != PROTECT_STAT_KEY_OK_MASK) ? TRUE : FALSE)
#define isArea1Protected(flash) (((flRead8bitRegPlus(flash,NdataProtect1Status) & PROTECT_STAT_KEY_OK_MASK) != PROTECT_STAT_KEY_OK_MASK) ? TRUE : FALSE)
/*----------------------------------------------------------------------*/
/* s e t S t i c k y B i t */
/* */
/* Set the sticky bit to prevent the insertion of the protection key. */
/* */
/* Parameters: */
/* flash : Pointer identifying drive. */
/* */
/* Returns: */
/* flOK on success, none zero otherwise. */
/*----------------------------------------------------------------------*/
FLStatus setStickyBit(FLFlash * flash)
{
volatile Reg8bitType val;
register int i;
/* Raise the sticky bit, while keeping the other bits of the register */
for(i=0;i<flash->noOfFloors;i++)
{
/* Remove last bit */
val = flRead8bitRegPlus(flash, NoutputControl) |
OUT_CNTRL_STICKY_BIT_ENABLE;
flWrite8bitRegPlus(flash, NoutputControl, val);
}
return flOK;
}
/*
** protectBoundries
*
*
* PARAMETERS:
* flash : Pointer identifying drive
* area : indicated which protection area to work on. 0 or 1.
* AddressLow : address of lower boundary of protected area
* AddressHigh : address of upper boundary of protected area
*
* DESCRIPTION: Gets protection boundaries from registers
*
* NOTE : protection areas are assumed to be consequtive although they
* may skip DPS , OTP and header units.
*
* RETURNS:
* flOK on success
*
*/
FLStatus protectionBoundries(FLFlash * flash, byte area,CardAddress* addressLow,
CardAddress* addressHigh, byte floorNo)
{
/* Check mode of ASIC and set to NORMAL.*/
FLStatus status = chkASICmode(flash);
int i = 10;
int j;
if(status != flOK)
return status;
if(flash->mediaType == MDOCP_16_TYPE)
i--;
j = i+8;
setFloor(flash,floorNo);
switch (area)
{
case 0: /* data protect structure 0 */
/* read the data protect 0 addresses */
*addressLow = ((dword)flRead8bitRegPlus(flash,NdataProtect0LowAddr) << i)| /* ADDR_1 */
((dword)flRead8bitRegPlus(flash,NdataProtect0LowAddr+1) << j); /* ADDR_2 */
*addressHigh = ((dword)flRead8bitRegPlus(flash,NdataProtect0UpAddr) << i)| /* ADDR_1 */
((dword)flRead8bitRegPlus(flash,NdataProtect0UpAddr+1) << j); /* ADDR_2 */
break;
case 1: /* data protect structure 1 */
/* read the data protect 1 addresses */
*addressLow = ((dword)flRead8bitRegPlus(flash,NdataProtect1LowAddr) << i)| /* ADDR_1 */
((dword)flRead8bitRegPlus(flash,NdataProtect1LowAddr+1) << j); /* ADDR_2 */
*addressHigh = ((dword)flRead8bitRegPlus(flash,NdataProtect1UpAddr) << i)| /* ADDR_1 */
((dword)flRead8bitRegPlus(flash,NdataProtect1UpAddr+1) << j); /* ADDR_2 */
break;
default: /* No such protection area */
return flGeneralFailure;
}
return(flOK);
}
/*
** tryKey
*
*
* PARAMETERS:
* flash : Pointer identifying drive
* area : indicated which protection area to work on. 0 or 1.
* Key : an 8 byte long array containing the protection password.
* unsigned char * is an 8 bytes unsigned char array
*
* DESCRIPTION: Sends protection key
*
* RETURNS:
* flOK on success otherwise flWrongKey
*
*/
FLStatus tryKey(FLFlash * flash, byte area, unsigned char FAR1* key)
{
int i;
switch (area)
{
case 0: /* data protect structure 0 */
for(i=0; i<PROTECTION_KEY_LENGTH; i++) /* Send key */
flWrite8bitRegPlus(flash,NdataProtect0Key, key[i]);
/* check if key is valid */
if (isArea0Protected(flash) == TRUE)
{
return flWrongKey;
}
else
{
return flOK;
}
case 1: /* data protect structure 0 */
for(i=0; i<PROTECTION_KEY_LENGTH; i++) /* Send key */
flWrite8bitRegPlus(flash,NdataProtect1Key, key[i]);
/* check if key is valid */
if (isArea1Protected(flash) == TRUE)
{
return flWrongKey;
}
else
{
return flOK;
}
default: /* No such protection area */
return flGeneralFailure;
}
}
/*
** protectKeyInsert
*
*
* PARAMETERS:
* flash : Pointer identifying drive
* area : indicated which protection area to work on. 0 or 1.
* Key : an 8 byte long array containing the protection password.
* unsigned char * is an 8 bytes unsigned char array
*
* DESCRIPTION: Sends protection key only to protected areas.
*
* NOTE : If key is already inserted the given key will not be sent.
* NOTE : The key will be sent to all the devices floors even if a key
* did not fit one of them.
* NOTE : This 2 notes above allow inserting diffrent key to
* diffrent floors in the case of power failure while formmating
* the device.
*
* RETURNS:
* flOK on success otherwise flWrongKey
*
*/
FLStatus protectionKeyInsert(FLFlash * flash, byte area, unsigned char FAR1* key)
{
byte floor;
FLStatus status;
FLStatus tmpStatus;
/* Check mode of ASIC and set to NORMAL.*/
status = chkASICmode(flash);
if(status != flOK)
return status;
/* Send key to all floors */
for (floor = 0;floor<flash->noOfFloors;floor++)
{
setFloor(flash,floor);
switch (area)
{
case 0: /* data protect structure 0 */
/* check if key is already inserted */
if ((isArea0Protected(flash) == FALSE) || /* Key is in */
((flRead8bitRegPlus(flash,NdataProtect0Status) & /* Or not protected */
(PROTECT_STAT_WP_MASK | PROTECT_STAT_RP_MASK)) == 0))
continue;
break;
case 1: /* data protect structure 1 */
/* check if key is already inserted */
if ((isArea1Protected(flash) == FALSE) || /* Key is in */
((flRead8bitRegPlus(flash,NdataProtect1Status) & /* Or not protected */
(PROTECT_STAT_WP_MASK | PROTECT_STAT_RP_MASK)) == 0))
continue;
break;
default: /* No such protection area */
return flGeneralFailure;
}
tmpStatus = tryKey(flash,area,key);
if (tmpStatus == flOK)
continue;
/* Try default key */
tmpStatus = tryKey(flash,area,(byte *)DEFAULT_KEY);
if (tmpStatus != flOK)
status = tmpStatus;
}
return(status);
}
/*
** protectKeyRemove
*
*
* PARAMETERS:
* flash : Pointer identifying drive
* area : indicated which protection area to work on. 0 or 1.
*
* DESCRIPTION: Removes protection key
*
* RETURNS:
* Return flOK
*
*/
FLStatus protectionKeyRemove(FLFlash * flash, byte area)
{
byte tmpKey[8];
byte floor;
FLStatus status;
for (floor = 0;floor < flash->noOfFloors;floor++)
{
setFloor(flash,floor);
status = tryKey(flash,area,tmpKey);
if (status == flOK) /* Unfortunatly the key was fine */
{
tmpKey[0]++;
status = tryKey(flash,area,tmpKey);
}
}
return flOK;
}
/*
** protectType
*
*
* PARAMETERS:
* flash : Pointer identifying drive.
* area : indicated which protection area to work on. 0 or 1.
* flag : returns any combination of
* LOCK_ENABLED - The LOCK signal is enabled.
* LOCK_ASSERTED - The LOCK signal input pin is asserted.
* KEY_INSERTED - The key has been correctly written
* READ_PROTECTED - The area is protected against read operations
* WRITE_PROTECTED - The area is protected against write operations
*
* DESCRIPTION: Gets protection type
*
* NOTE: The type is checked for all floors. The attributes are ored
* giving the harshest protection attributes.
*
* RETURNS:
* flOK on success
*/
FLStatus protectionType(FLFlash * flash, byte area, word* flag)
{
volatile Reg8bitType protectData;
byte floor;
FLBoolean curFlag; /* Indicated if the floor has r/w protection */
CardAddress addressLow,addressHigh;
FLStatus status;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -