Read drive number from the command line.

Added some sanity checks.

svn path=/trunk/; revision=3290
This commit is contained in:
Eric Kohl 2002-07-22 17:53:12 +00:00
parent 5faa3537c3
commit c1652a2ee6
2 changed files with 58 additions and 10 deletions

View file

@ -1,4 +1,4 @@
# $Id: makefile,v 1.2 2002/06/07 22:58:18 ekohl Exp $ # $Id: makefile,v 1.3 2002/07/22 17:53:12 ekohl Exp $
PATH_TO_TOP = ../../.. PATH_TO_TOP = ../../..
@ -12,7 +12,7 @@ TARGET_NAME = partinfo
#TARGET_CFLAGS = -fnative_struct #TARGET_CFLAGS = -fnative_struct
TARGET_SDKLIBS = kernel32.a TARGET_SDKLIBS = ntdll.a kernel32.a
TARGET_OBJECTS = $(TARGET_NAME).o TARGET_OBJECTS = $(TARGET_NAME).o

View file

@ -54,6 +54,13 @@ void HexDump(char *buffer, ULONG size)
} }
#endif #endif
void Usage(void)
{
puts("Usage: partinfo <drive number>");
}
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
HANDLE hDisk; HANDLE hDisk;
@ -62,8 +69,53 @@ int main (int argc, char *argv[])
char *Buffer; char *Buffer;
DRIVE_LAYOUT_INFORMATION *LayoutBuffer; DRIVE_LAYOUT_INFORMATION *LayoutBuffer;
DISK_GEOMETRY DiskGeometry; DISK_GEOMETRY DiskGeometry;
ULONG ulDrive;
CHAR DriveName[40];
SYSTEM_DEVICE_INFORMATION DeviceInfo;
NTSTATUS Status;
hDisk = CreateFile("\\\\.\\PHYSICALDRIVE0", if (argc != 2)
{
Usage();
return(0);
}
ulDrive = strtoul(argv[1], NULL, 10);
if (errno != 0)
{
printf("Error: Malformed drive number\n", errno);
return(0);
}
/* Check drive number */
Status = NtQuerySystemInformation(SystemDeviceInformation,
&DeviceInfo,
sizeof(SYSTEM_DEVICE_INFORMATION),
&i);
if (!NT_SUCCESS(Status))
{
printf("NtQuerySystemInformation() failed (Status %lx)\n", Status);
return(0);
}
if (DeviceInfo.NumberOfDisks == 0)
{
printf("No disk drive installed!\n");
return(0);
}
if (ulDrive >= DeviceInfo.NumberOfDisks)
{
printf("Invalid disk drive number! Valid drive numbers [0-%lu]\n",
DeviceInfo.NumberOfDisks-1);
return(0);
}
/* Build full drive name */
sprintf(DriveName, "\\\\.\\PHYSICALDRIVE%lu", ulDrive);
/* Open drive */
hDisk = CreateFile(DriveName,
GENERIC_READ, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, NULL,
@ -76,8 +128,7 @@ int main (int argc, char *argv[])
return 0; return 0;
} }
/* Get drive geometry */
/* get drive geometry */
if (!DeviceIoControl(hDisk, if (!DeviceIoControl(hDisk,
IOCTL_DISK_GET_DRIVE_GEOMETRY, IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL, NULL,
@ -97,6 +148,7 @@ int main (int argc, char *argv[])
#ifdef DUMP_DATA #ifdef DUMP_DATA
HexDump((char*)&DiskGeometry, dwRead); HexDump((char*)&DiskGeometry, dwRead);
#endif #endif
printf("Drive number: %lu\n", ulDrive);
printf("Cylinders: %I64u\nMediaType: %lx\nTracksPerCylinder: %lu\n" printf("Cylinders: %I64u\nMediaType: %lx\nTracksPerCylinder: %lu\n"
"SectorsPerTrack: %lu\nBytesPerSector: %lu\n\n", "SectorsPerTrack: %lu\nBytesPerSector: %lu\n\n",
DiskGeometry.Cylinders.QuadPart, DiskGeometry.Cylinders.QuadPart,
@ -125,7 +177,7 @@ int main (int argc, char *argv[])
NULL)) NULL))
{ {
CloseHandle(hDisk); CloseHandle(hDisk);
printf("DeviceIoControl failed! Error: %u\n", printf("DeviceIoControl(IOCTL_DISK_GET_DRIVE_LAYOUT) failed! Error: %u\n",
GetLastError()); GetLastError());
free(Buffer); free(Buffer);
return 0; return 0;
@ -154,10 +206,6 @@ int main (int argc, char *argv[])
LayoutBuffer->PartitionEntry[i].PartitionLength.QuadPart); LayoutBuffer->PartitionEntry[i].PartitionLength.QuadPart);
} }
#ifdef DUMP_SIZE_INFO
printf("\nsizeof(PARTITION_INFORMATION): %lu\n", sizeof(PARTITION_INFORMATION));
#endif
free(Buffer); free(Buffer);
return 0; return 0;