?? iomgr.cpp
字號:
//
//The WriteFile's implementation.
//The routine does the following:
// 1. Create a DRCB object,and initialize it;
// 2. Commit the write transaction by calling DeviceWrite routine;
// 3. According to the result,set appropriate return value(s).
//
static BOOL WriteFile(__COMMON_OBJECT* lpThis,
__COMMON_OBJECT* lpFileObj,
DWORD dwWriteSize,
LPVOID lpBuffer,
DWORD* lpWrittenSize)
{
BOOL bResult = FALSE;
__DRCB* lpDrcb = NULL;
__DEVICE_OBJECT* lpDevObject = NULL;
__DRIVER_OBJECT* lpDrvObject = NULL;
DWORD dwWriteBlockSize = 0L;
DWORD dwTotalSize = 0L;
if((NULL == lpThis) ||
(NULL == lpFileObj) ||
(0 == dwWriteSize) ||
(NULL == lpBuffer)) //Parameters check.
return bResult;
lpDevObject = (__DEVICE_OBJECT*)lpFileObj;
lpDrvObject = lpDevObject->lpDriverObject;
lpDrcb = (__DRCB*)ObjectManager.CreateObject(&ObjectManager,
NULL,
OBJECT_TYPE_DRCB);
if(NULL == lpDrcb) //Failed to create DRCB object.
goto __TERMINAL;
if(!lpDrcb->Initialize((__COMMON_OBJECT*)lpDrcb)) //Failed to initialize.
goto __TERMINAL;
//
//The following code gets the write block size of this device object.
//
lpDrcb->dwRequestMode = DRCB_REQUEST_MODE_IOCTRL;
lpDrcb->dwCtrlCommand = IOCONTROL_GET_WRITE_BLOCK_SIZE;
lpDrcb->dwStatus = DRCB_STATUS_INITIALIZED;
lpDrcb->dwOutputLen = sizeof(DWORD);
lpDrcb->lpOutputBuffer = (LPVOID)&dwWriteBlockSize;
lpDrcb->WaitForCompletion = WaitForCompletion;
lpDrcb->OnCancel = OnCancel;
lpDrcb->OnCompletion = OnCompletion;
lpDrvObject->DeviceCtrl((__COMMON_OBJECT*)lpDrvObject,
(__COMMON_OBJECT*)lpDevObject,
lpDrcb); //Get the write block size.
if(DRCB_STATUS_SUCCESS != lpDrcb->dwStatus) //Failed to get the write block size.
goto __TERMINAL;
//
//Now,the variable dwWriteBlockSize countains the write block size.
//The following code,we commit the write transaction to device driver object
//by calling DeviceWrite.
//
dwTotalSize = dwWriteSize;
lpDrcb->dwRequestMode = DRCB_REQUEST_MODE_WRITE;
lpDrcb->dwStatus = DRCB_STATUS_INITIALIZED;
while(TRUE)
{
lpDrcb->dwInputLen =
dwTotalSize > dwWriteBlockSize ? dwWriteBlockSize : dwTotalSize;
lpDrcb->lpInputBuffer = lpBuffer;
lpDrvObject->DeviceWrite((__COMMON_OBJECT*)lpDrvObject,
(__COMMON_OBJECT*)lpDevObject,
lpDrcb); //Commit the write transaction.
if(DRCB_STATUS_SUCCESS != lpDrcb->dwStatus) //Failed to write.
{
goto __TERMINAL;
}
if(dwTotalSize <= dwWriteBlockSize) //This indicates the write transaction is
//over.
{
*lpWrittenSize = dwWriteSize;
bResult = TRUE;
goto __TERMINAL;
}
dwTotalSize -= dwWriteBlockSize;
lpBuffer = (LPVOID)((DWORD)lpBuffer + dwWriteBlockSize); //Adjust the buffer.
}
__TERMINAL:
if(lpDrcb != NULL) //Destroy the DRCB object.
{
ObjectManager.DestroyObject(&ObjectManager,
(__COMMON_OBJECT*)lpDrcb);
}
return bResult;
}
//
//The implementation of CloseFile.
//This routine does the following:
// 1. Check the validation of the parameters;
// 2. Check the device type of the target device object;
// 3. If the device to closed is a normal file,then destroy the object;
// 4. If the target deivce is not a normal file,then reduce the
// reference counter,and return.
//
static VOID CloseFile(__COMMON_OBJECT* lpThis,
__COMMON_OBJECT* lpFileObj)
{
__IO_MANAGER* lpIoManager = NULL;
__DEVICE_OBJECT* lpDevObject = NULL;
if((NULL == lpThis) || (NULL == lpFileObj)) //Parameter check.
return;
lpIoManager = (__IO_MANAGER*)lpThis;
lpDevObject = (__DEVICE_OBJECT*)lpFileObj;
/*if(lpDevObject->dwDevType != DEVICE_TYPE_FILE) //Not a normal file.
{
//ENTER_CRITICAL_SECTION();
//lpDevObject->dwRefCounter --;
//LEAVE_CRITICAL_SECTION();
return;
}*/
//
//If control flow reaches here,it indicates that the device to be closed is
//a normal file,then,we will destroy this file object by calling DestroyDevice
//routine of IOManager object.
//
//lpIoManager->DestroyDevice(lpThis,lpFileObj); //Destroy the file object.
}
//
//The implementation of IOControl.
//This routine does the following:
// 1.
//
static BOOL IOControl(__COMMON_OBJECT* lpThis,
__COMMON_OBJECT* lpFileObj,
DWORD dwCommand,
LPVOID lpParameter,
DWORD dwOutputBufLen,
LPVOID lpOutputBuffer,
DWORD* lpdwOutputFilled)
{
BOOL bResult = FALSE;
if((NULL == lpThis) ||
(NULL == lpFileObj)) //Parameters check.
return bResult;
return bResult;
}
//
//The implementation of SetFilePointer.
//This routine does the following:
// 1.
//
static BOOL SetFilePointer(__COMMON_OBJECT* lpThis,
__COMMON_OBJECT* lpFileObj,
DWORD dwWhereBegin,
INT nOffset)
{
BOOL bResult = FALSE;
if((NULL == lpThis) || (NULL == lpFileObj)) //Parameters check.
return bResult;
return bResult;
}
//
//The implementation of FlushFile.
//This routine does the following:
// 1.
//
static BOOL FlushFile(__COMMON_OBJECT* lpThis,
__COMMON_OBJECT* lpFileObj)
{
BOOL bResult = FALSE;
if((NULL == lpThis) || (NULL == lpFileObj)) //Parameters check.
return bResult;
return bResult;
}
//
//The implementation of CreateDevice,this routine is called by device
//driver(s) to create device object.Generally,this routine is called in
//DriverEntry of device driver(s).
//This routine does the following:
// 1. Creates a device object by calling ObjectManager's interface;
// 2. Initializes the device object;
// 3. Allocates a block of memory as device object's extension;
// 4. Inserts the device object into device object's list.
//
static __DEVICE_OBJECT* CreateDevice(__COMMON_OBJECT* lpThis,
LPSTR lpszDevName,
DWORD dwAttribute,
DWORD dwBlockSize,
DWORD dwMaxReadSize,
DWORD dwMaxWriteSize,
LPVOID lpDevExtension,
__DRIVER_OBJECT* lpDrvObject)
{
__DEVICE_OBJECT* lpDevObject = NULL;
__IO_MANAGER* lpIoManager = (__IO_MANAGER*)lpThis;
DWORD dwFlags = 0L;
//Check the parameters.
if((NULL == lpThis) || (NULL == lpszDevName) || (NULL == lpDrvObject))
{
BUG();
return NULL;
}
if(StrLen(lpszDevName) > MAX_DEV_NAME_LEN) //The device's name is too long.
{
return NULL;
}
lpDevObject = (__DEVICE_OBJECT*)ObjectManager.CreateObject(
&ObjectManager,
NULL,
OBJECT_TYPE_DEVICE);
if(NULL == lpDevObject) //Failed to create device object.
{
return NULL;
}
if(!lpDevObject->Initialize((__COMMON_OBJECT*)lpDevObject)) //Failed to initialize the object.
{
ObjectManager.DestroyObject(&ObjectManager,
(__COMMON_OBJECT*)lpDevObject);
return NULL;
}
//Initialize the device object's members.
lpDevObject->lpDevExtension = lpDevExtension;
lpDevObject->lpDriverObject = lpDrvObject;
lpDevObject->dwAttribute = dwAttribute;
lpDevObject->dwBlockSize = dwBlockSize;
lpDevObject->dwMaxWriteSize = dwMaxWriteSize;
lpDevObject->dwMaxReadSize = dwMaxReadSize;
StrCpy(lpszDevName,(LPSTR)&lpDevObject->DevName[0]);
//
//The following code add the device object into device object's list.
//
__ENTER_CRITICAL_SECTION(NULL,dwFlags);
if(NULL == lpIoManager->lpDeviceRoot) //This is the first object.
{
lpIoManager->lpDeviceRoot = lpDevObject;
}
else //This is not the first object.
{
lpDevObject->lpNext = lpIoManager->lpDeviceRoot;
lpDevObject->lpPrev = NULL;
lpIoManager->lpDeviceRoot->lpPrev = lpDevObject;
lpIoManager->lpDeviceRoot = lpDevObject;
}
__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
return lpDevObject;
}
//
//The implementation of DestroyDevice.
//
static VOID DestroyDevice(__COMMON_OBJECT* lpThis,
__DEVICE_OBJECT* lpDeviceObject)
{
__IO_MANAGER* lpIoManager = (__IO_MANAGER*)lpThis;
DWORD dwFlags = 0L;
if((NULL == lpThis) || (NULL == lpDeviceObject)) //Parameters check.
{
BUG();
return;
}
//
//The following code deletes the device object from system list.
//
__ENTER_CRITICAL_SECTION(NULL,dwFlags);
if(NULL == lpDeviceObject->lpPrev) //This is the first object.
{
if(NULL == lpDeviceObject->lpNext) //This is the last object.
{
lpIoManager->lpDeviceRoot = NULL;
}
else //This is not the last object.
{
lpDeviceObject->lpNext->lpPrev = NULL;
lpIoManager->lpDeviceRoot = lpDeviceObject->lpNext;
}
}
else //This is not the first object.
{
if(NULL == lpDeviceObject->lpNext) //This is the last object.
{
lpDeviceObject->lpPrev->lpNext = NULL;
}
else //This is not the last object.
{
lpDeviceObject->lpPrev->lpNext = lpDeviceObject->lpNext;
lpDeviceObject->lpNext->lpPrev = lpDeviceObject->lpPrev;
}
}
__LEAVE_CRITICAL_SECTION(NULL,dwFlags);
//Destroy the device object.
ObjectManager.DestroyObject(&ObjectManager,
(__COMMON_OBJECT*)lpDeviceObject);
return;
}
//
//The implementation of ReserveResource.
//This routine does the following:
// 1.
//
static BOOL ReserveResource(__COMMON_OBJECT* lpThis,
__RESOURCE_DESCRIPTOR*
lpResDesc)
{
BOOL bResult = FALSE;
if((NULL == lpThis) || (NULL == lpResDesc)) //Parameters check.
return bResult;
return bResult;
}
//
//The implementation of LoadDriver.
//This routine is used by OS loader to load device drivers.
//
static BOOL LoadDriver(__DRIVER_ENTRY DrvEntry)
{
__DRIVER_OBJECT* lpDrvObject = NULL;
if(NULL == DrvEntry) //Invalid parameter.
{
BUG();
return FALSE;
}
lpDrvObject = (__DRIVER_OBJECT*)ObjectManager.CreateObject(
&ObjectManager,
NULL,
OBJECT_TYPE_DRIVER);
if(NULL == lpDrvObject) //Can not create driver object.
{
return FALSE;
}
if(!lpDrvObject->Initialize((__COMMON_OBJECT*)lpDrvObject)) //Initialize failed.
{
return FALSE;
}
//Call the driver entry.
if(DrvEntry(lpDrvObject))
{
return TRUE;
}
//Failed to call DrvEntry routine,so release the driver object.
ObjectManager.DestroyObject(
&ObjectManager,
(__COMMON_OBJECT*)lpDrvObject);
return FALSE;
}
/*************************************************************************
**************************************************************************
**************************************************************************
**************************************************************************
*************************************************************************/
//
//The following code defines one of the global objects in Hello China - IOManager.
//This object is a global object,and only one in the whole system life-cycle.
//
__IO_MANAGER IOManager = {
NULL, //lpDeviceRoot.
NULL, //lpDriverRoot.
NULL, //lpResDescriptor.
IOManagerInitialize, //Initialize.
CreateFile, //CreateFile.
ReadFile, //ReadFile.
WriteFile, //WriteFile.
CloseFile, //CloseFile.
IOControl, //IOControl.
SetFilePointer, //SetFilePointer.
FlushFile, //FileFlush.
CreateDevice, //CreateDevice.
DestroyDevice, //DestroyDevice.
ReserveResource, //ReserveResource.
LoadDriver
};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -