I have created a File system driver to allow or block the external storage devices in windows. For allowing a particular device I need to find out the Serial Number. I am using PSTORAGE_DEVICE_DESCRIPTOR to find out Serial Number but it will not work for all devices(eg: some devices shows the serial number as a special character). Is there any other possible way to find the Serial number in File system driver
The following are the code used for enumerating the serial number
char szBuff [1024];
STORAGE_PROPERTY_QUERY Query;
//STORAGE_DEVICE_DESCRIPTOR Buffer[4];
NTSTATUS Status = STATUS_SUCCESS;
PSTORAGE_DEVICE_DESCRIPTOR Descriptor=NULL;
PIRP NewIrp2=NULL;
PIO_STACK_LOCATION NextIrpStack=NULL;
IO_STATUS_BLOCK IoStatus;
char szSptr[2]={'_','\0'};
SIZE_T szLength = 0;
Query.PropertyId = StorageDeviceProperty;// first set the query properties
Query.QueryType = PropertyStandardQuery;
KeInitializeEvent(&WaitEvent_newIrp, NotificationEvent, TRUE);// initialize the waitable event
__try
{
memset(szBuff,0,1024);
NewIrp2=IoBuildDeviceIoControlRequest(IOCTL_STORAGE_QUERY_PROPERTY, StorageStackDeviceObject,
(PVOID)&Query,sizeof(STORAGE_PROPERTY_QUERY),
szBuff,1024,
FALSE,&WaitEvent_newIrp,&IoStatus);
if(NewIrp2==NULL)
{
return STATUS_SUCCESS;
}
Status = IoCallDriver(StorageStackDeviceObject, NewIrp2);// send this irp to the storage device
if (Status == STATUS_PENDING)
{
KeWaitForSingleObject(&WaitEvent_newIrp, Executive, KernelMode, FALSE, NULL);
Status =IoStatus.Status;
}
}
__finally
{
//if(NT_SUCCESS(Status))
if(szBuff!=NULL)
{
try
{
Descriptor = (PSTORAGE_DEVICE_DESCRIPTOR)szBuff;
Descriptor->Size = sizeof(szBuff);
uBusType = Descriptor->BusType; //Get the bus type.
}
except(EXCEPTION_EXECUTE_HANDLER)
{
/* Error handling code */
}
}
char pszStart[255];
if(Descriptor->SerialNumberOffset!=0)
{
memset(pszStart,0,255);
RtlStringCopyWorkerA(pszStart,255,((char*)(UINT_PTR)Descriptor+(DWORD32)Descriptor->SerialNumberOffset),255);
//pszStart contains the serial number
}
}
Following is the code block used for enumerating the serial number
char szBuff [1024];
STORAGE_PROPERTY_QUERY Query;
//STORAGE_DEVICE_DESCRIPTOR Buffer[4];
NTSTATUS Status = STATUS_SUCCESS;
PSTORAGE_DEVICE_DESCRIPTOR Descriptor=NULL;
PIRP NewIrp2=NULL;
PIO_STACK_LOCATION NextIrpStack=NULL;
IO_STATUS_BLOCK IoStatus;
char szSptr[2]={'_','\0'};
SIZE_T szLength = 0;
Query.PropertyId = StorageDeviceProperty;// first set the query properties
Query.QueryType = PropertyStandardQuery;
KeInitializeEvent(&WaitEvent_newIrp, NotificationEvent, TRUE);// initialize the waitable event
__try
{
memset(szBuff,0,1024);
NewIrp2=IoBuildDeviceIoControlRequest(IOCTL_STORAGE_QUERY_PROPERTY, StorageStackDeviceObject,
(PVOID)&Query,sizeof(STORAGE_PROPERTY_QUERY),
szBuff,1024,
FALSE,&WaitEvent_newIrp,&IoStatus);
if(NewIrp2==NULL)
{
return STATUS_SUCCESS;
}
Status = IoCallDriver(StorageStackDeviceObject, NewIrp2);// send this irp to the storage device
if (Status == STATUS_PENDING)
{
KeWaitForSingleObject(&WaitEvent_newIrp, Executive, KernelMode, FALSE, NULL);
Status =IoStatus.Status;
}
}
__finally
{
//if(NT_SUCCESS(Status))
if(szBuff!=NULL)
{
try
{
Descriptor = (PSTORAGE_DEVICE_DESCRIPTOR)szBuff;
Descriptor->Size = sizeof(szBuff);
uBusType = Descriptor->BusType; //Get the bus type.
}
except(EXCEPTION_EXECUTE_HANDLER)
{
/* Error handling code */
}
}
char pszStart[255];
if(Descriptor->SerialNumberOffset!=0)
{
memset(pszStart,0,255);
RtlStringCopyWorkerA(pszStart,255,((char*)(UINT_PTR)Descriptor+(DWORD32)Descriptor->SerialNumberOffset),255);
//pszStart contains the serial number
}
}