none
Why do I attach \Device\Harddisk1 fail? RRS feed

  • Question

  • I try to write a sample driver to attach disk device. If I attach \Device\Harddisk0, driver will return fail and errcode is 1073741788. But if I attach \Device\Harddisk0\Partition1, driver will success. My question is why do I attach \Device\Harddisk0 fail? My code is :
    #include <ntddk.h>
    
    #include "Int3_x64.h"
    
    //
    // Function declarations
    //
    
    DRIVER_INITIALIZE DriverEntry;
    
    DRIVER_DISPATCH MdbDispatchPass;
    
    DRIVER_UNLOAD DriverUnload;
    
    //
    // Define the sections that allow for discarding (i.e. paging) some of
    // the code.
    //
    
    #ifdef ALLOC_PRAGMA
    #pragma alloc_text (INIT, DriverEntry)
    #pragma alloc_text (PAGE, DriverUnload)
    #endif // ALLOC_PRAGMA
    
    PDEVICE_OBJECT pdevice;
    
    PDEVICE_OBJECT pNextDevice;
    
    NTSTATUS 
    DriverEntry(
    		PDRIVER_OBJECT driver
    		, PUNICODE_STRING reg_path
    		)
    {
    	NTSTATUS status;
    	UNICODE_STRING targetdevice;
    	ULONG ulIndex;
    	PDRIVER_DISPATCH *dispatch;
    
    	BreakPoint();
    
    	KDbgPrint("Init driver.");
    
    	//
    	// Create dispatch points
    	//
    	for (ulIndex = 0, dispatch = driver->MajorFunction
    			; ulIndex <= IRP_MJ_MAXIMUM_FUNCTION
    			; ulIndex++, dispatch++
    			) {
    		*dispatch = MdbDispatchPass;
    	}
    
    	driver->DriverUnload = DriverUnload;
    
    	status = IoCreateDevice(
    			driver
    			, 0
    			, NULL
    			, FILE_DEVICE_DISK
    			, FILE_DEVICE_SECURE_OPEN
    			, FALSE
    			, &pdevice
    			);
    	if (!NT_SUCCESS(status)) {
    		KDbgPrint("IoCreateDevice fail %d.", status);
    		goto err_createdev;
    	}
    
            /**
             * here if change to "\\Device\\Harddisk0", attach will fail and errcode is 1073741788
             **/
    	RtlInitUnicodeString(&targetdevice, L"\\Device\\Harddisk0\\Partition1");
    	status = IoAttachDevice(
    			pdevice
    			, &targetdevice
    			, &pNextDevice
    			);
    	if (!NT_SUCCESS(status)) {
    		KDbgPrint("IoAttachDevice fail %d.", status);
    		goto err_attachdev;
    	}
    
    	KDbgPrint("Init driver done.");
    	return STATUS_SUCCESS;
    
    err_attachdev:
    	IoDeleteDevice(pdevice);
    err_createdev:
    	return status;
    }
    
    VOID 
    DriverUnload(
    		PDRIVER_OBJECT driver
    		)
    {
    	KDbgPrint("Unload driver.");
    
    	IoDeleteDevice(pdevice);
    }
    
    NTSTATUS
    MdbDispatchPass(
    		IN PDEVICE_OBJECT pDeviceObject
    		, IN PIRP pIrp
    		)
    {
    	IoSkipCurrentIrpStackLocation(pIrp);
    	return IoCallDriver(pNextDevice, pIrp);
    }


    • Edited by SoapLee Tuesday, September 3, 2013 9:52 AM
    Tuesday, September 3, 2013 9:51 AM

Answers

  • Since you have the comment that you are changing \\Device\\Harddisk0\\Partition1 to \\Device\\Harddisk0 I suspect the problem is that you are trying to load this driver at something other than boot time.  Once the partition manager attaches to the disk, there is no way you can attach to \\Device\\Harddisk0, you need to have your driver be set up as a boot start and then reboot.

    Also, I notice this is not a Plug and Play driver, since you are trying to attach in DriverEntry and you have no AddDevice routine.  Since disk drives are Plug and Play you will mess up the system with this model.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Tuesday, September 3, 2013 1:34 PM

All replies

  • Since you have the comment that you are changing \\Device\\Harddisk0\\Partition1 to \\Device\\Harddisk0 I suspect the problem is that you are trying to load this driver at something other than boot time.  Once the partition manager attaches to the disk, there is no way you can attach to \\Device\\Harddisk0, you need to have your driver be set up as a boot start and then reboot.

    Also, I notice this is not a Plug and Play driver, since you are trying to attach in DriverEntry and you have no AddDevice routine.  Since disk drives are Plug and Play you will mess up the system with this model.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Tuesday, September 3, 2013 1:34 PM
  • thanks. Could I see symbol links such as \Device\Harddisk0\Partition1 and \Device\Harddisk0 in user mode?


    • Edited by SoapLee Wednesday, September 4, 2013 2:34 AM
    Wednesday, September 4, 2013 2:23 AM
  • I remove partition on disk 1. But I try to attach \Device\Harddisk1 fail too.
    Wednesday, September 4, 2013 3:04 AM
  • Those aren't the symbolic links they are the object names.  You can see them in with native API calls, which means you are out of the Win32 subsystem.  But even if you can see them what would you do with them.

    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Wednesday, September 4, 2013 1:37 PM