📄 flash_sys.c
字号:
/***************************************************************************
* File: flash_sys.c - Export stream interface to OS
*
* The content of this file or document is CONFIDENTIAL and PROPRIETARY
* to Jade Technologies Co., Ltd. It is subject to the terms of a
* License Agreement between Licensee and Jade Technologies Co., Ltd.
* restricting among other things, the use, reproduction, distribution
* and transfer. Each of the embodiments, including this information
* and any derivative work shall retain this copyright notice.
*
* Copyright (c) 2005 Jade Technologies Co., Ltd.
* All rights reserved.
****************************************************************************/
#include "flash.h"
//------------------------------------------------------------------------------
// Global Variables in this driver
//------------------------------------------------------------------------------
CRITICAL_SECTION CriticalSection;
CEDEVICE_POWER_STATE PMState = D0;
LPWSTR lpActivePath = NULL;
unsigned short gCrc = 0;
extern unsigned long dwBlockAddr;
extern unsigned long dwBlockStartAddr;
extern unsigned char *CacheBuf;
//------------------------------------------------------------------------------
// Stream Interface Functions
//------------------------------------------------------------------------------
BOOL WINAPI
DllMain(HANDLE hinstDLL,
unsigned long dwReason,
LPVOID lpReserved)
{
return TRUE;
}
unsigned long
FLA_Init(
unsigned long dwContext
)
{
RETAILMSG(1, (_T("FLASH: FLA_Init(*)\r\n")));
InitializeCriticalSection(&CriticalSection);
if (dwContext)
{ // Get the device key from active device registry key
if (lpActivePath = (unsigned short *)LocalAlloc(LPTR, wcslen((LPWSTR)dwContext)*sizeof(WCHAR)+sizeof(WCHAR)))
{
wcscpy(lpActivePath, (LPWSTR)dwContext);
}
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_Init - ActiveKey(copy) = %s (0x%x)\r\n"), lpActivePath, lpActivePath));
}
{
PHYSICAL_ADDRESS blockaddr;
blockaddr.HighPart = 0 ;
blockaddr.LowPart = START_FLASH_ADDR;
dwBlockStartAddr = (unsigned long)MmMapIoSpace(blockaddr, FLASH_OFFSET, FALSE);
RETAILMSG(MSG_SYS, (_T("FLASH: @dwBlockStartAddr: 0x%x\r\n"), dwBlockStartAddr));
blockaddr.HighPart = 0 ;
blockaddr.LowPart = BASE_FLASH_ADDR;
dwBlockAddr = (unsigned long)MmMapIoSpace(blockaddr, USERDEF_SIZE, FALSE);
RETAILMSG(MSG_SYS, (_T("FLASH: @dwBlockAddr: 0x%x\r\n"), dwBlockAddr));
}
// tell the OS that it's a Block Device
{ // old block
GUID uid = { 0xA4E7EDDA, 0xE575, 0x4252,
{0x9D, 0x6B, 0x41, 0x95,0xD4,0x8B,0xB8,0x65 } };
AdvertiseInterface( &uid, L"FLA1:", TRUE);
}
{
CacheBuf = malloc(USERDEF_SIZE);
if (CacheBuf == NULL)
return 0;
}
//FLA_test();
//check the manu
return 1;
}
BOOL
FLA_Deinit(
unsigned long dwContext
)
{
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_Deinit\r\n")));
if (CacheBuf != NULL)
free(CacheBuf);
MmUnmapIoSpace((PVOID)dwBlockAddr, USERDEF_SIZE);
MmUnmapIoSpace((PVOID)dwBlockStartAddr, FLASH_OFFSET);
DeleteCriticalSection(&CriticalSection);
return TRUE;
}
unsigned long
FLA_Open(
unsigned long dwData,
unsigned long dwAccess,
unsigned long dwShareMode
)
{
int i = 0;
unsigned short SavedCrc = 0;
unsigned char sum = 0;
EnterCriticalSection(&CriticalSection);
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_Open\r\n")));
// TODO: Read data from the flash to memory at the very beginning
//RETAILMSG(1, (_T("FLASH: FLA_Open - Read&Cache ~~~~~~~~ +\r\n")));
for (i=0; i<USERDEF_SIZE; i++)
{
FLA_cfi_read_byte(dwBlockAddr+i, CacheBuf+i);
sum += CacheBuf[i];
}
// TODO: Do the checkout
gCrc = CRC_Calculate(CacheBuf, USERDEF_SIZE);
gCrc += sum;
RETAILMSG(1, (_T("FLASH: FLA_Open - CRC: 0x%x\r\n"), gCrc));
FLA_cfi_read_byte(dwBlockStartAddr+CRC_SAVE_OFFSET, (unsigned char *)(&SavedCrc)); // 0xc401f000
FLA_cfi_read_byte(dwBlockStartAddr+CRC_SAVE_OFFSET+1, (unsigned char *)(&SavedCrc)+1); // 0xc401f001
RETAILMSG(1, (_T("FLASH: FLA_Open - CRC(saved): 0x%x\r\n"), SavedCrc));
if (SavedCrc != gCrc)
{
RETAILMSG(1, (_T("FLASH: FLA_Open - CRC mismatched!\r\n")));
memset(CacheBuf, 0, USERDEF_SIZE);
}
//RETAILMSG(1, (_T("FLASH: FLA_Open - Read&Cache ~~~~~~~~ -\r\n")));
LeaveCriticalSection(&CriticalSection);
return 1;
}
BOOL
FLA_Close(
unsigned long dwContext
)
{
EnterCriticalSection(&CriticalSection);
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_Close\r\n")));
LeaveCriticalSection(&CriticalSection);
return TRUE;
}
BOOL
FLA_IOControl(
unsigned long Handle,
unsigned long dwIoControlCode,
unsigned char * pInBuf,
unsigned long nInBufSize,
unsigned char * pOutBuf,
unsigned long nOutBufSize,
unsigned long * pBytesReturned
)
{
PSG_REQ pSG;
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl(%d) entered\r\n"), dwIoControlCode));
//
// Check parameters
//
switch (dwIoControlCode)
{
case DISK_IOCTL_READ:
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - read\r\n")));
if (pInBuf == NULL)
{
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - invalid parameter\r\n")));
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case DISK_IOCTL_WRITE:
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - write\r\n")));
if (pInBuf == NULL)
{
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - invalid parameter\r\n")));
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case DISK_IOCTL_GETINFO:
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - get info\r\n")));
if (pInBuf == NULL)
{
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - invalid parameter\r\n")));
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case DISK_IOCTL_SETINFO:
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - set info\r\n")));
if (pInBuf == NULL)
{
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - invalid parameter\r\n")));
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case DISK_IOCTL_INITIALIZED:
if (pInBuf == NULL)
{
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - invalid parameter\r\n")));
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case IOCTL_DISK_GETNAME:
case DISK_IOCTL_GETNAME:
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - get name\r\n")));
if (pOutBuf == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case IOCTL_DISK_DEVICE_INFO:
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - device info\r\n")));
if(!pInBuf || nInBufSize != sizeof(STORAGEDEVICEINFO))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case DISK_IOCTL_FORMAT_MEDIA:
break;
case IOCTL_POWER_CAPABILITIES:
if (pOutBuf == NULL
|| nOutBufSize < sizeof(POWER_CAPABILITIES)
|| pBytesReturned == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case IOCTL_POWER_QUERY:
if(pOutBuf == NULL
|| nOutBufSize != sizeof(CEDEVICE_POWER_STATE)
|| pBytesReturned == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case IOCTL_POWER_SET:
if(pOutBuf == NULL
|| nOutBufSize != sizeof(CEDEVICE_POWER_STATE)
|| pBytesReturned == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
case IOCTL_POWER_GET:
if(pOutBuf == NULL
|| nOutBufSize != sizeof(CEDEVICE_POWER_STATE)
|| pBytesReturned == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
break;
default:
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
//
// Execute dwIoControlCode
//
switch (dwIoControlCode)
{
case DISK_IOCTL_READ:
case DISK_IOCTL_WRITE:
{
pSG = (PSG_REQ)pInBuf;
EnterCriticalSection(&CriticalSection);
FLA_DoDiskIO(dwIoControlCode, pSG);
LeaveCriticalSection(&CriticalSection);
return TRUE;
}
case DISK_IOCTL_GETINFO:
{
DISK_INFO * pDiskInfo = (DISK_INFO *)pInBuf;
pDiskInfo->di_total_sectors = USERDEF_SIZE/BYTES_PER_SECTOR;
pDiskInfo->di_bytes_per_sect = BYTES_PER_SECTOR;
pDiskInfo->di_flags = DISK_INFO_FLAG_CHS_UNCERTAIN | DISK_INFO_FLAG_PAGEABLE;
return TRUE;
}
case DISK_IOCTL_SETINFO:
return TRUE;
case DISK_IOCTL_INITIALIZED:
return TRUE;
case IOCTL_DISK_GETNAME:
case DISK_IOCTL_GETNAME:
{
BOOL ret = FALSE;
ret = (FLA_GetFolderName(lpActivePath, (LPWSTR)pOutBuf, nOutBufSize,
pBytesReturned));
if (!ret)
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - Get Name failed\r\n")));
return ret;
}
case IOCTL_DISK_DEVICE_INFO: // new ioctl for disk info
{
STORAGEDEVICEINFO * pInfo = (STORAGEDEVICEINFO *) pInBuf;
return FLA_GetDeviceInfo(pInfo);
}
case DISK_IOCTL_FORMAT_MEDIA: // need to implement in the near future
{
int i = 0;
for (i=0; i<USERDEF_SIZE; i+=BYTES_PER_BLOCK)
{
FLA_cfi_erase_block(dwBlockAddr+i);
}
}
SetLastError(ERROR_SUCCESS);
return TRUE;
case IOCTL_POWER_CAPABILITIES:
{
PPOWER_CAPABILITIES ppc = (PPOWER_CAPABILITIES) pOutBuf;
memset(ppc, 0, sizeof(*ppc));
ppc->DeviceDx = 0x9; // support D0, D3
RETAILMSG(MSG_SYS,
(_T("FLASH: FLA_IoControl - Power Capabilities: 0x%x\r\n"), ppc->DeviceDx));
*pBytesReturned = sizeof(*ppc);
return TRUE;
}
case IOCTL_POWER_QUERY:
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - Power Query\r\n")));
return TRUE;
case IOCTL_POWER_SET:
{
EnterCriticalSection(&CriticalSection);
PMState = *(PCEDEVICE_POWER_STATE) pOutBuf;
*(PCEDEVICE_POWER_STATE) pOutBuf = PMState;
*pBytesReturned = sizeof(CEDEVICE_POWER_STATE);
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - Power Set: 0x%x\r\n"), PMState));
if (PMState == D3)
{
int i = 0;
unsigned char sum = 0;
// TODO: Do the checkout
//1 we first calculate the CRC of all the data will write into the flash
//2 when we write the CRC num to somewhere
gCrc = CRC_Calculate(CacheBuf, USERDEF_SIZE);
for(i = 0; i < USERDEF_SIZE; i++)
{
sum += CacheBuf[i];
}
gCrc += sum;
//RETAILMSG(1, (_T("FLASH: FLA_IoControl - CRC: 0x%x\r\n"), gCrc));
FLA_cfi_erase_block(dwBlockStartAddr+CRC_SAVE_OFFSET);
FLA_cfi_write_byte(dwBlockStartAddr+CRC_SAVE_OFFSET, (unsigned char)gCrc); // 0xc401F000
FLA_cfi_write_byte(dwBlockStartAddr+CRC_SAVE_OFFSET+1, (unsigned char)(gCrc>>8)); // 0xc401f001
// TODO: Write back the data cached in memory
//1 erase the whole chip
//2 write all cached data
//RETAILMSG(1, (_T("FLASH: FLA_IoControl - Erase&Write ~~~~~~~~ +\r\n")));
for (i=0; i<USERDEF_SIZE; i+=BYTES_PER_BLOCK)
{
FLA_cfi_erase_block(dwBlockAddr+i);
}
for (i=0; i<USERDEF_SIZE; i++)
{
FLA_cfi_write_byte(dwBlockAddr+i, *(CacheBuf+i));
}
//RETAILMSG(1, (_T("FLASH: FLA_IoControl - Erase&Write ~~~~~~~~ -\r\n")));
}
LeaveCriticalSection(&CriticalSection);
return TRUE;
}
case IOCTL_POWER_GET:
{
EnterCriticalSection(&CriticalSection);
*(PCEDEVICE_POWER_STATE) pOutBuf = PMState;
*pBytesReturned = sizeof(CEDEVICE_POWER_STATE);
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - Power Get: 0x%x\r\n"), PMState));
LeaveCriticalSection(&CriticalSection);
return TRUE;
}
default:
RETAILMSG(MSG_SYS, (_T("FLASH: FLA_IoControl - (default) \r\n")));
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
}
//------------------------------------------------------------------------------
//
// Reserved
//
//------------------------------------------------------------------------------
unsigned long FLA_Read(unsigned long Handle, LPVOID pBuffer, unsigned long dwNumBytes){return 0;}
unsigned long FLA_Write(unsigned long Handle, LPVOID pBuffer, unsigned long dwNumBytes){return 0;}
unsigned long FLA_Seek(unsigned long Handle, long lDistance, unsigned long dwMoveMethod){return 0;}
void FLA_PowerUp(void){}
void FLA_PowerDown(void){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -