Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.2 KiB

  1. /*
  2. *
  3. * maioctl.c -- Disk-querying IOCTLs for MigApp.
  4. *
  5. */
  6. #include "pch.h"
  7. #include "migappp.h"
  8. #ifdef UNICODE
  9. #error "UNICODE not supported for maioctl.c"
  10. #endif
  11. ////////////////////////////////////////////////////////////////////////////
  12. #define VWIN32_DIOC_DOS_IOCTL 1
  13. typedef struct _DEVIOCTL_REGISTERS {
  14. DWORD reg_EBX;
  15. DWORD reg_EDX;
  16. DWORD reg_ECX;
  17. DWORD reg_EAX;
  18. DWORD reg_EDI;
  19. DWORD reg_ESI;
  20. DWORD reg_Flags;
  21. } DEVIOCTL_REGISTERS, *PDEVIOCTL_REGISTERS;
  22. typedef struct _MID {
  23. WORD midInfoLevel;
  24. DWORD midSerialNum;
  25. BYTE midVolLabel[11];
  26. BYTE midFileSysType[8];
  27. } MID, *PMID;
  28. typedef struct _DEVPARAMS {
  29. BYTE dpSpecFunc;
  30. BYTE dpDevType;
  31. WORD dpDevAttr;
  32. WORD dpCylinders;
  33. BYTE dpMediaType;
  34. BYTE dpBiosParameterBlock[25];
  35. } DEVPARAMS, *PDEVPARAMS;
  36. BOOL DoIOCTL(PDEVIOCTL_REGISTERS preg)
  37. {
  38. HANDLE hDevice;
  39. BOOL fResult;
  40. DWORD cb;
  41. preg->reg_Flags = 0x8000; /* assume error (carry flag set) */
  42. hDevice = CreateFile("\\\\.\\vwin32",
  43. GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  44. (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING,
  45. FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
  46. if (hDevice == (HANDLE) INVALID_HANDLE_VALUE) {
  47. return FALSE;
  48. } else {
  49. fResult = DeviceIoControl(hDevice, VWIN32_DIOC_DOS_IOCTL,
  50. preg, sizeof(*preg), preg, sizeof(*preg), &cb, 0);
  51. if (!fResult)
  52. return FALSE;
  53. }
  54. CloseHandle(hDevice);
  55. return TRUE;
  56. }
  57. BOOL
  58. IsDriveRemoteOrSubstituted(
  59. UINT nDrive, // 'A'==1, etc.
  60. BOOL *fRemote,
  61. BOOL *fSubstituted)
  62. {
  63. DEVIOCTL_REGISTERS reg;
  64. MID mid;
  65. reg.reg_EAX = 0x4409; /* "Check if block device remote" */
  66. reg.reg_EBX = nDrive; /* zero-based drive ID */
  67. reg.reg_ECX = 0; /* Believe this is no-care */
  68. reg.reg_EDX = (DWORD) ∣ /* Believe this is no-care */
  69. if (!DoIOCTL(&reg)) {
  70. return FALSE;
  71. }
  72. if (reg.reg_Flags & 0x8000) /* error if carry flag set */ {
  73. return FALSE;
  74. }
  75. // Check bit 15 for SUBST-ness
  76. *fSubstituted = (0 != (reg.reg_EDX & 0x8000));
  77. // Check bit 12 for REMOTE-ness
  78. *fRemote = (0 != (reg.reg_EDX & 0x1000));
  79. return TRUE;
  80. }
  81. BOOL GetMediaID(
  82. PMID pmid,
  83. UINT nDrive) // 'A'==1, etc.
  84. {
  85. DEVIOCTL_REGISTERS reg;
  86. reg.reg_EAX = 0x440D; /* IOCTL for block devices */
  87. reg.reg_EBX = nDrive; /* zero-based drive ID */
  88. reg.reg_ECX = 0x0866; /* Get Media ID command */
  89. reg.reg_EDX = (DWORD) pmid; /* receives media ID info */
  90. if (!DoIOCTL(&reg)) {
  91. return FALSE;
  92. }
  93. if (reg.reg_Flags & 0x8000) /* error if carry flag set */ {
  94. return FALSE;
  95. }
  96. return TRUE;
  97. }
  98. BOOL
  99. IsFloppyDrive(
  100. UINT nDrive) // 'A'==1, etc.
  101. {
  102. DEVIOCTL_REGISTERS reg;
  103. DEVPARAMS devparams;
  104. DWORD driveType;
  105. TCHAR szDriveRoot[] = TEXT("A:\\");
  106. *szDriveRoot += (TCHAR)(nDrive - 1);
  107. driveType = GetDriveType(szDriveRoot);
  108. if (driveType != DRIVE_REMOVABLE) {
  109. return FALSE;
  110. }
  111. reg.reg_EAX = 0x440d; /* Generic IOctl */
  112. reg.reg_EBX = nDrive; /* zero-based drive ID */
  113. reg.reg_ECX = 0x0860; /* device category (must be 08h) and "Get Device Parameters" */
  114. reg.reg_EDX = (DWORD)&devparams; /* offset of Device Parameters Structure */
  115. devparams.dpSpecFunc = 0; /* request default information */
  116. if (!DoIOCTL(&reg) || (reg.reg_Flags & 0x8000)) {
  117. return FALSE;
  118. }
  119. switch (devparams.dpDevType) {
  120. case (0x00): // 320/360KB
  121. case (0x01): // 1.2MB
  122. case (0x02): // 720KB
  123. case (0x03): // 8-inch, single-density
  124. case (0x04): // 8-inch, single-density
  125. return TRUE;
  126. case (0x05): // Hard disk
  127. case (0x06): // Tape drive
  128. return FALSE;
  129. case (0x07): // 1.44MB
  130. return TRUE;
  131. case (0x08): // Read/Write optical
  132. return FALSE;
  133. case (0x09): // 2.88MB
  134. return TRUE;
  135. default: // other
  136. return FALSE;
  137. }
  138. }