📄 cedriver.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
// Copyright (C) M-Systems Flash Disk Pioneers Ltd. 1995-2005
//***********************************************************************
//*
//* File Name: CEdriver.C
//*
//* Purpose..: FLite driver layer for Windows CE.
//*
//* Required Registry Settings:
//* For built-in devices:
//*
//************************************************************************/
#define CE_DRIVER_LAYER
#include "cedriver.h"
#include "bsp_mdoc_cfg.h"
// Flash base Address for tffslib
//
DWORD flDocBaseAddress = 0xAA700000; // uncached address
#define MAX_REGISTRY_PATH_SIZE 50 // maximum expected number of characters in the registry path string
#include "CEdebug.h"
#define ENABLED_DEBUG_ZONE ZONE_DLLINIT_SET | ZONE_INIT_SET | ZONE_OPEN_SET | ZONE_ERROR_SET | ZONE_SDKERROR_SET
// ZONE_ALL_SET
// ZONE_FORMAT_SET
// ZONE_NONE_SET
// ZONE_ERROR_SET
// ZONE_WARNING_SET
// ZONE_FUNCTION_SET
// ZONE_INIT_SET
// ZONE_IO_SET
// ZONE_CALLBACK_SET
// ZONE_TEMP_SET
// ZONE_MTD_SET
// ZONE_PM_SET
// ZONE_FORMAT_SET
// ZONE_UI_SET
// ZONE_FLITE_SET
// ZONE_DLLINIT_SET
// ZONE_ALL_SET
DBGPARAM dpCurSettings =
{
TEXT("TrueFFS"),
{ // Note: the order of the following must
// match the definitions in debug.h:
TEXT("Errors"), TEXT("Warnings"), TEXT("Initialization"),
TEXT("Open / Close"), TEXT("Disk I/O"), TEXT("TrueFFS flow"),
TEXT("TrueFFS Errors"), TEXT("TrurFFS Warnings"), TEXT("Read / Write"),
TEXT("Details (of IO)"),TEXT("Undefined"), TEXT("Undefined"),
TEXT("DLL Undefined"), TEXT("Undefined"), TEXT("Undefined ")
},
ENABLED_DEBUG_ZONE
};
//----------------------------------------------------------------------------------
// g e t R e g V a l u e
//
// Get the a value from the registry.
//
// Parameters:
// path : The sub key of the registry
// buffer : Pointer to output buffer - registry value written here.
// cBytes : The size of the output buffer
// *pcBytes : Pointer to a variable containing the size of read data.
// fieldname : Pointer to a string containing the requested value name.
//
//
// Returns
// False if operation failed (registry entry was not found)
// True if operation succeed and window was found.
//----------------------------------------------------------------------------------
BOOL getRegValue(LPTSTR path, LPBYTE buffer, DWORD cBytes, DWORD *pcBytes,LPCWSTR fieldname)
{
HKEY DriverKey;
DWORD ValType;
DWORD status;
BOOL retVal = FALSE;
status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, 0, &DriverKey);
if(status == ERROR_SUCCESS) //If the key was found
{
*pcBytes = cBytes;
status = RegQueryValueEx(DriverKey,fieldname, NULL, &ValType, buffer, pcBytes);
if(status == ERROR_SUCCESS)
{
DBGMSG(ZONE_INIT, (TEXT("TrueFFS, getRegValue(): FieldName = %s, length = %d\r\n"), fieldname, *pcBytes));
*pcBytes += sizeof(WCHAR); // account for terminating 0.
retVal = TRUE;
}
RegCloseKey(DriverKey);
}
return retVal;
}
//----------------------------------------------------------------------------------
// g e t D e v i c e R e g i s t r y P a t h
//
// Get the name of the driver registry entry accurding to the active path.
//
// Parameters:
// activePath : Pointer to a string describing the active registry path.
// actualName : Pointer to return buffer. The function will return a
// string with the path of the driver registry entry (if exist)
// nameSize : Size of return buffer.
//
// Returns
// True if operation succeed, False otherwise
//----------------------------------------------------------------------------------
BOOL getDeviceRegistryPath(LPTSTR activePath, LPTSTR actualName, DWORD nameSize)
{
HKEY hActive;
DWORD valLen, valType, status;
status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, activePath, 0, 0, &hActive);
if(status)
{
//Can't open the active registry - return FALSE
DBGMSG(ZONE_INIT, (TEXT("TrueFFS, getDeviceRegistryPath(): RegOpenKeyEx(HLM\\%s) returned %d!!!\r\n"), activePath, status));
return FALSE;
}
valLen = nameSize;
status = RegQueryValueEx(hActive, DEVLOAD_DEVKEY_VALNAME, NULL, &valType, (PUCHAR)actualName, &valLen);
RegCloseKey(hActive);
if(status != ERROR_SUCCESS)
{
//Couldn't read the name of the entry
DBGMSG(ZONE_INIT, (TEXT("TrueFFS, getDeviceRegistryPath(): RegQueryValueEx(%s) returned %d\r\n"), DEVLOAD_DEVKEY_VALNAME, status));
RegCloseKey(hActive);
return FALSE;
}
return TRUE;
}
//----------------------------------------------------------------------------
// Function Name: IsValidDisk
// Purpose......: Verify that pDisk points to a valid allocated disk structure
// Returns......: TRUE for a valid disk, FALSE otherwise
//----------------------------------------------------------------------------
static BOOL IsValidDisk(PDISK pDisk)
{
if (pDisk != NULL &&
pDisk->signature == TFFS_DISK_SIGNATURE)
{
return TRUE;
}
return FALSE;
}
//----------------------------------------------------------------------------
// Purpose..: background folding in secondary thread
// thread created after 1000 IOCTL_DISK_WRITE
// thread terminated after the folding
// Returns......: Nothing
//----------------------------------------------------------------------------
static DWORD WINAPI AutoFolding(LPVOID pvParam)
{
FLStatus status;
IOreq ioreq;
FLSDword sectorWriteSinceFolding;
FLSDword actualNoOfSectors;
PDISK pDisk = (PDISK)pvParam;
// Validate the pDisk arguement
if(IsValidDisk(pDisk) == FALSE)
{
DBGMSG(ZONE_IO | ZONE_ERROR, (TEXT("TrueFFS, AutoFolding(): Recieved pDIsk is invalid. Returning.\r\n")));
return 0;
}
ioreq.irHandle = BSP_MDOC_USERSTORE_PARTITION_HANDLE;
ioreq.irLength = -1;
EnterCriticalSection(&pDisk->cs);
sectorWriteSinceFolding = pDisk->sectorWriteSinceFolding;
status = flDefragmentVolume(&ioreq);
LeaveCriticalSection(&pDisk->cs);
actualNoOfSectors = ioreq.irLength;
ioreq.irLength = -1;
DBGMSG(ZONE_IO, (TEXT("TrueFFS, AutoFolding: START\r\n")));
while(ioreq.irLength != actualNoOfSectors && status == flOK)
{
// flDefragmentVolume succeed to release more sectors
actualNoOfSectors = ioreq.irLength;
ioreq.irLength = -1;
EnterCriticalSection(&pDisk->cs);
status = flDefragmentVolume(&ioreq);
LeaveCriticalSection(&pDisk->cs);
}
DBGMSG(ZONE_IO, (TEXT("TrueFFS, AutoFolding: ExitThread %ld\r\n"),actualNoOfSectors));
EnterCriticalSection(&pDisk->cs);
pDisk->sectorWriteSinceFolding -= sectorWriteSinceFolding;
pDisk->foldingThreadInProcess = FALSE;
LeaveCriticalSection(&pDisk->cs);
return 0;
}
//----------------------------------------------------------------------------
// Function Name: statusTranslate
// Purpose......: Translate FLite FLStatus to Windows CE status
// Returns......: The translated status
// Remark.......: The status codes can be found at winerror.h
//----------------------------------------------------------------------------
static DWORD statusTranslate(FLStatus status)
{
static DWORD statusTranslateTable[] =
{ // FLite-WinCE translation table
NO_ERROR, // flOK = 0
ERROR_BAD_COMMAND, // flBadFunction = 1
ERROR_INVALID_HANDLE, // flFileNotFound = 2
ERROR_INVALID_HANDLE, // flPathNotFound = 3
ERROR_GEN_FAILURE, // flTooManyOpenFiles = 4
ERROR_GEN_FAILURE, // flNoWriteAccess = 5
ERROR_INVALID_HANDLE, // flBadFileHandle = 6
ERROR_GEN_FAILURE, // Skip This entry (7)
ERROR_GEN_FAILURE, // Skip This entry (8)
ERROR_BAD_COMMAND, // flDriveNotAvailable = 9
ERROR_GEN_FAILURE, // flNonFATformat = 10
ERROR_GEN_FAILURE, // flFormatNotSupported = 11
ERROR_GEN_FAILURE, // Skip This entry (12)
ERROR_GEN_FAILURE, // Skip This entry (13)
ERROR_GEN_FAILURE, // Skip This entry (14)
ERROR_GEN_FAILURE, // Skip This entry (15)
ERROR_GEN_FAILURE, // Skip This entry (16)
ERROR_GEN_FAILURE, // Skip This entry (17)
ERROR_GEN_FAILURE, // flNoMoreFiles = 18
ERROR_WRITE_PROTECT, // flWriteProtect = 19
ERROR_INVALID_HANDLE, // flBadDriveHandle = 20
ERROR_BUSY_DRIVE, // flDriveNotReady = 21
ERROR_BAD_COMMAND, // flUnknownCmd = 22
ERROR_GEN_FAILURE, // flBadFormat = 23
ERROR_BAD_LENGTH, // flBadLength = 24
ERROR_CRC, // flDataError = 25
ERROR_UNRECOGNIZED_MEDIA, // flUnknownMedia = 26
ERROR_SECTOR_NOT_FOUND, // flSectorNotFound = 27
ERROR_OUTOFMEMORY, // flOutOfPaper = 28
ERROR_WRITE_FAULT, // flWriteFault = 29
ERROR_READ_FAULT, // flReadFault = 30
ERROR_GEN_FAILURE, // flGeneralFailure = 31
ERROR_GEN_FAILURE, // Skip This entry (32)
ERROR_GEN_FAILURE, // Skip This entry (33)
ERROR_MEDIA_CHANGED, // flDiskChange = 34
ERROR_GEN_FAILURE, // Skip This entry (35)
ERROR_GEN_FAILURE, // Skip This entry (36)
ERROR_GEN_FAILURE, // Skip This entry (37)
ERROR_GEN_FAILURE, // Skip This entry (38)
ERROR_GEN_FAILURE, // Skip This entry (39)
ERROR_GEN_FAILURE, // Skip This entry (40)
ERROR_GEN_FAILURE, // Skip This entry (41)
ERROR_GEN_FAILURE, // Skip This entry (42)
ERROR_GEN_FAILURE, // Skip This entry (43)
ERROR_GEN_FAILURE, // Skip This entry (44)
ERROR_GEN_FAILURE, // Skip This entry (45)
ERROR_GEN_FAILURE, // Skip This entry (46)
ERROR_GEN_FAILURE, // Skip This entry (47)
ERROR_GEN_FAILURE, // Skip This entry (48)
ERROR_GEN_FAILURE, // Skip This entry (49)
ERROR_GEN_FAILURE, // flVppFailure = 50
ERROR_INVALID_PARAMETER, // flBadParameter = 51
ERROR_GEN_FAILURE, // flNoSpaceInVolume = 52
ERROR_GEN_FAILURE, // flInvalidFATchain = 53
ERROR_GEN_FAILURE, // flRootDirectoryFull = 54
ERROR_GEN_FAILURE, // flNotMounted = 55
ERROR_INVALID_HANDLE, // flPathIsRootDirectory = 56
ERROR_INVALID_PARAMETER, // flNotADirectory = 57
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -