NTSTATUS
FileObjectContext::Create()
{
    TRACE_FUNCTION_ENTRY(LEVEL_VERBOSE);
    NTSTATUS status;

    UNICODE_STRING* fileObjectName = WdfFileObjectGetFileName(_FileObject);
    if (CSTR_EQUAL == CompareStringOrdinal(fileObjectName->Buffer, fileObjectName->Length / sizeof(WCHAR), L"\\" FILE_NAMESPACE_NCI_SIMULATOR, int(wcslen(L"\\" FILE_NAMESPACE_NCI_SIMULATOR)), /*IgnoreCase*/ TRUE))
    {
        _Type = Type::NciSim;
    }
    else
    {
        status = STATUS_OBJECT_NAME_NOT_FOUND;
        TRACE_LINE(LEVEL_ERROR, "Unknown file namespace: %S. %!STATUS!", fileObjectName->Buffer, status);
        return status;
    }

    status = _DeviceContext->ClientConnected(this);
    if (!NT_SUCCESS(status))
    {
        TRACE_LINE(LEVEL_ERROR, "DeviceContext::ClientConnected failed. %!STATUS!", status);
        return status;
    }

    TRACE_FUNCTION_SUCCESS(LEVEL_VERBOSE);
    return STATUS_SUCCESS;
}
Exemple #2
0
/*++
Routine Description:

The framework calls a driver's EvtDeviceFileCreate callback
when the framework receives an IRP_MJ_CREATE request.
The system sends this request when a user application opens the
device to perform an I/O operation, such as reading or writing a file.
This callback is called synchronously, in the context of the thread
that created the IRP_MJ_CREATE request.

Arguments:

Device - Handle to a framework device object.
FileObject - Pointer to fileobject that represents the open handle.
CreateParams - copy of the create IO_STACK_LOCATION

Return Value:

NT status code
--*/
VOID PSDrv_EvtDeviceFileCreate(IN WDFDEVICE Device, IN WDFREQUEST Request, IN WDFFILEOBJECT FileObject)
{
    NTSTATUS                    status = STATUS_UNSUCCESSFUL;
    PUNICODE_STRING             fileName;
    PFILE_CONTEXT               pFileContext;
    PDEVICE_CONTEXT             pDevContext;
    WDFUSBPIPE                  pipe;

    PSDrv_DbgPrint(3, ("PSDrv_EvtDeviceFileCreate - begins\n"));

    PAGED_CODE();

    // initialize variables
    pDevContext = GetDeviceContext(Device);
    pFileContext = GetFileContext(FileObject);
    fileName = WdfFileObjectGetFileName(FileObject);

    if (0 == fileName->Length)
	{
        // opening a device as opposed to pipe.
        status = STATUS_SUCCESS;
    }
    else {
        pipe = GetPipeFromName(pDevContext, fileName);

		// Does the pipe exists?
        if (pipe != NULL)
		{
            pFileContext->Pipe = pipe;

            WdfUsbTargetPipeSetNoMaximumPacketSizeCheck(pipe);

			// Set the default timeout
			pFileContext->nTimeOut = PSUSBDRV_DEFAULT_TIMEOUT;

            status = STATUS_SUCCESS;
        }
		else
		{
            status = STATUS_INVALID_DEVICE_REQUEST;
        }
    }

    WdfRequestComplete(Request, status);

    PSDrv_DbgPrint(3, ("PSDrv_EvtDeviceFileCreate - ends\n"));

    return;
}
Exemple #3
0
static VOID UsbChief_EvtDeviceFileCreate(IN WDFDEVICE Device, IN WDFREQUEST Request,
				  IN WDFFILEOBJECT FileObject)
{
	NTSTATUS status = STATUS_UNSUCCESSFUL;
	PUNICODE_STRING fileName;
	PFILE_CONTEXT pFileContext;
	PDEVICE_CONTEXT pDevContext;
	WDFUSBPIPE  pipe;

	PAGED_CODE();

	UsbChief_DbgPrint(DEBUG_RW|DEBUG_IOCTL, ("called\n"));

	pDevContext = GetDeviceContext(Device);
	pFileContext = GetFileContext(FileObject);

	fileName = WdfFileObjectGetFileName(FileObject);

	if (!fileName->Length) {
		status = STATUS_SUCCESS;
	} else {
		pipe = UsbChief_GetPipeFromName(pDevContext, fileName);

		if (pipe != NULL) {
			pFileContext->Pipe = pipe;

			WdfUsbTargetPipeSetNoMaximumPacketSizeCheck(pipe);

			status = STATUS_SUCCESS;
		} else {
			status = STATUS_INVALID_DEVICE_REQUEST;
		}
	}

	WdfRequestComplete(Request, status);
}
Exemple #4
0
VOID
NonPnpEvtDeviceFileCreate (
    IN WDFDEVICE            Device,
    IN WDFREQUEST Request,
    IN WDFFILEOBJECT        FileObject
    )
/*++

Routine Description:

    The framework calls a driver's EvtDeviceFileCreate callback
    when it receives an IRP_MJ_CREATE request.
    The system sends this request when a user application opens the
    device to perform an I/O operation, such as reading or writing a file.
    This callback is called synchronously, in the context of the thread
    that created the IRP_MJ_CREATE request.

Arguments:

    Device - Handle to a framework device object.
    FileObject - Pointer to fileobject that represents the open handle.
    CreateParams - Parameters of IO_STACK_LOCATION for create

Return Value:

   NT status code

--*/
{
    PUNICODE_STRING             fileName;
    UNICODE_STRING              absFileName, directory;
    OBJECT_ATTRIBUTES           fileAttributes;
    IO_STATUS_BLOCK             ioStatus;
    PCONTROL_DEVICE_EXTENSION   devExt;
    NTSTATUS                    status;
    USHORT                      length = 0;


    UNREFERENCED_PARAMETER( FileObject );

    PAGED_CODE ();

    devExt = ControlGetData(Device);

    //
    // Assume the directory is a temp directory under %windir%
    //
    RtlInitUnicodeString(&directory, L"\\SystemRoot\\temp");

    //
    // Parsed filename has "\" in the begining. The object manager strips
    // of all "\", except one, after the device name.
    //
    fileName = WdfFileObjectGetFileName(FileObject);

    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT, "NonPnpEvtDeviceFileCreate %wZ%wZ",
                   &directory, fileName);

    //
    // Find the total length of the directory + filename
    //
    length = directory.Length + fileName->Length;

    absFileName.Buffer = ExAllocatePoolWithTag(PagedPool, length, POOL_TAG);
    if(absFileName.Buffer == NULL) {
        status = STATUS_INSUFFICIENT_RESOURCES;
        TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT, "ExAllocatePoolWithTag failed");
        goto End;
    }
    absFileName.Length = 0;
    absFileName.MaximumLength =  length;

    status = RtlAppendUnicodeStringToString(&absFileName, &directory);
    if (!NT_SUCCESS(status)) {
        TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
                   "RtlAppendUnicodeStringToString failed with status %!STATUS!",
                   status);
        goto End;
    }

    status = RtlAppendUnicodeStringToString(&absFileName, fileName);
    if (!NT_SUCCESS(status)) {
        TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
                   "RtlAppendUnicodeStringToString failed with status %!STATUS!",
                   status);
        goto End;
    }

    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT, "Absolute Filename %wZ", &absFileName);

    InitializeObjectAttributes( &fileAttributes,
                                &absFileName,
                                OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
                                NULL, // RootDirectory
                                NULL // SecurityDescriptor
                                );

    status = ZwCreateFile (
                    &devExt->FileHandle,
                    SYNCHRONIZE | GENERIC_WRITE | GENERIC_READ,
                    &fileAttributes,
                    &ioStatus,
                    NULL,// alloc size = none
                    FILE_ATTRIBUTE_NORMAL,
                    FILE_SHARE_READ,
                    FILE_OPEN_IF,
                    FILE_SYNCHRONOUS_IO_NONALERT |FILE_NON_DIRECTORY_FILE,
                    NULL,// eabuffer
                    0// ealength
                    );

    if (!NT_SUCCESS(status)) {

        TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
                       "ZwCreateFile failed with status %!STATUS!", status);
        devExt->FileHandle = NULL;
    }

End:
    if(absFileName.Buffer != NULL) {
        ExFreePool(absFileName.Buffer);
    }

    WdfRequestComplete(Request, status);

    return;
}
Exemple #5
0
// handles operations that must be performed when an application requests access to a device.
VOID vJoy_EvtDeviceFileCreate(
  __in  WDFDEVICE Device,
  __in  WDFREQUEST Request,
  __in  WDFFILEOBJECT FileObject
)
{
		WDFFILEOBJECT FileObj;
		PCUNICODE_STRING pName;
		UNICODE_STRING TmpUstr;
		NTSTATUS status = STATUS_SUCCESS;
		int  id=0;
		WCHAR RefStr[100];
		PFILEOBJECT_EXTENSION pExtension=NULL;
		PDEVICE_EXTENSION    pDevContext = NULL;
		PRPDO_DEVICE_DATA	pPdoData=NULL;
		size_t len;
		DWORD_PTR ProcessId;

	    PAGED_CODE ();
		TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT, "Entered vJoy_EvtDeviceFileCreate\n");


		// Get file object then get its filename
		FileObj = WdfRequestGetFileObject(Request);
		if (!FileObj)
			goto going_out;
		pName = WdfFileObjectGetFileName(FileObj);
		if (!pName)
			goto going_out;

		TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT, "vJoy_EvtDeviceFileCreate: File name=%wZ\n", pName);

		// Extract id from interface number
		status = RtlStringCchLengthW(VJOY_INTERFACE, 100, &len);
		if (!NT_SUCCESS(status))
			goto going_out;
		status = RtlStringCchCopyNW(RefStr, 100, pName->Buffer+len+1,4);	// Copy the numeric part of the string (000)
		if (!NT_SUCCESS(status))
			goto going_out;
		RtlInitUnicodeString(&TmpUstr, RefStr);								// Convert "000" to UNICODE_STRING
		status = RtlUnicodeStringToInteger(&TmpUstr, 10, &id);				// Convert "000" to integer (0)
		if (!NT_SUCCESS(status))
			goto going_out;

		if (id>0)
		{
			// Verify that this interface has a corresponding device
			TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT, "vJoy_EvtDeviceFileCreate: ID=%d\n", id);
			pPdoData = PdoGetData(Device);
			pDevContext = GetDeviceContext(pPdoData->hParentDevice);
			if (!pDevContext)
				goto going_out;
			if (!pDevContext->positions[id - 1])
				goto going_out;

			// Get the file object context space
			// Test that this interface is not in use
			// and store there the parent (Raw PDO) context
			pExtension = GetFileObjectContext(FileObject);
			if (!pExtension)
				goto going_out;
			pExtension->pParentRawDeviceContext = pPdoData;
			if (pPdoData->UsedInterfacesMask & (1 << (id - 1)))
			{
				WdfRequestComplete(Request, STATUS_ACCESS_DENIED);
				ProcessId = (DWORD_PTR)PsGetCurrentProcessId();
				TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT, "vJoy_EvtDeviceFileCreate: PID=%d Failed (Access Denied)\n", ProcessId);
				return;
			}

			///// This is a successful file creation - Now record the file details

			// vJoy Device ID
			else
				pPdoData->UsedInterfacesMask |= 1 << (id - 1);

			// Put id in file object context space 
			pExtension->id = id; // Update

			// Get the id of the calling process
			ProcessId = (DWORD_PTR)PsGetCurrentProcessId();
			pExtension->CallingProcessId = (DWORD)(ProcessId & 0xFFFFFFFF);
			TraceEvents(TRACE_LEVEL_WARNING, DBG_INIT, "vJoy_EvtDeviceFileCreate: PID=%d\n", pExtension->CallingProcessId);

			// Put the file object in the FDO extension
			pDevContext->DeviceFileObject[id - 1] = FileObject;

			// Activate FFB Queues
			FfbActiveSet(TRUE, id, pDevContext);


			WdfRequestComplete(Request, status);
			return;

		} // if (id>0)

		// Case of General purpose and non device-specific Interface
		else // if (id<1)
		{
			TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT, "vJoy_EvtDeviceFileCreate(2nd case): ID=%d\n", id);
#if 0
			pPdoData = PdoGetData(Device);
			pDevContext = GetDeviceContext(pPdoData->hParentDevice);
			if (!pDevContext)
				goto going_out;

			TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT, "vJoy_EvtDeviceFileCreate(2nd case): Completing Request\n");

#endif // 0
			WdfRequestComplete(Request, status);
			return;
		}; // if (id<1)

	going_out:
		ProcessId = (DWORD_PTR)PsGetCurrentProcessId();
		TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT, "vJoy_EvtDeviceFileCreate: PID=%d Failed (Invalid Handle)\n", ProcessId);
		WdfRequestComplete(Request, STATUS_INVALID_HANDLE);
}
void AndroidUsbDeviceObject::OnEvtDeviceFileCreate(WDFREQUEST request,
                                                   WDFFILEOBJECT wdf_fo) {
  ASSERT_IRQL_PASSIVE();
  ASSERT(IsInterfaceSelected());
  if (!IsInterfaceSelected()) {
    WdfRequestComplete(request, STATUS_INVALID_DEVICE_STATE);
    return;
  }

  PUNICODE_STRING file_name = WdfFileObjectGetFileName(wdf_fo);
  ASSERT(NULL != file_name);
  if (NULL == file_name) {
    WdfRequestComplete(request, STATUS_OBJECT_NAME_INVALID);
    return;
  }

  WDFUSBPIPE wdf_pipe_obj = NULL;
  WDF_USB_PIPE_INFORMATION pipe_info;

  // TODO: Share / access check here?

  // Lets see if this is a device open
  if (0 != file_name->Length) {
    // This is a pipe open. Lets retrieve pipe index from the name
    UCHAR pipe_index = GetPipeIndexFromFileName(file_name);
    if (INVALID_UCHAR == pipe_index) {
      GoogleDbgPrint("\n!!!!! There is no pipe index for file %wZ", file_name);
      WdfRequestComplete(request, STATUS_OBJECT_NAME_INVALID);
      return;
    }

    // Make sure that pipe index doesn't exceed number of pipes
    if (pipe_index >= configured_pipes_num()) {
      WdfRequestComplete(request, STATUS_OBJECT_NAME_NOT_FOUND);
      return;
    }

    // Retrieve the pipe along with the pipe info
    WDF_USB_PIPE_INFORMATION_INIT(&pipe_info);
    wdf_pipe_obj = WdfUsbInterfaceGetConfiguredPipe(wdf_usb_interface(),
                                                    pipe_index,
                                                    &pipe_info);
    if (NULL == wdf_pipe_obj) {
      GoogleDbgPrint("\n!!!!! There is no pipe for index %u for file %wZ",
                     pipe_index, file_name);
      WdfRequestComplete(request, STATUS_OBJECT_NAME_NOT_FOUND);
      return;
    }
  }

  // If we're here this must be either device open or pipe open
  ASSERT((NULL != wdf_pipe_obj) || (0 == file_name->Length));

  // Create our file object extension for this file
  AndroidUsbFileObject* wdf_file_ext = NULL;
  NTSTATUS status;

  if (0 == file_name->Length) {
    // This is a device FO. Create wrapper for device FO
    ASSERT(NULL == wdf_pipe_obj);
    wdf_file_ext = new(NonPagedPool, GANDR_POOL_TAG_DEVICE_FO)
      AndroidUsbDeviceFileObject(this, wdf_fo);
    ASSERT(NULL != wdf_file_ext);
    if (NULL == wdf_file_ext) {
      WdfRequestComplete(request, STATUS_INSUFFICIENT_RESOURCES);
      return;
    }

    // Initialize extension
    status = wdf_file_ext->Initialize();
    if (!NT_SUCCESS(status)) {
      delete wdf_file_ext;
      WdfRequestComplete(request, status);
      return;
    }
  } else {
    // This is a pipe file. Create and initialize appropriate extension for it.
    status =
      CreatePipeFileObjectExt(wdf_fo, wdf_pipe_obj, &pipe_info, &wdf_file_ext);
    ASSERT((NULL != wdf_file_ext) || !NT_SUCCESS(status));
    if (!NT_SUCCESS(status)) {
      WdfRequestComplete(request, status);
      return;
    }
  }
  ASSERT(GetAndroidUsbFileObjectFromHandle(wdf_fo) == wdf_file_ext);
  WdfRequestComplete(request, STATUS_SUCCESS);
}