├── LICENSE ├── README.md ├── build.cmd ├── disk.ico ├── disktrim.c └── disktrim.rc /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DiskTrim for Windows 2 | ==================== 3 | Utility for Microsoft Windows that allows to send TRIM / UNMAP / DISCARD commands directly to an SSD drive. The operation is performed on the whole drive, sectors zero to the end. It securely erases contents of an entire SSD drive, and tests whether TRIM actually worked. You can also think of it as equivalent of Linux `blkdiscard(8)` utility for Windows. 4 | 5 | # WARNING: 6 | This utility is very dangerous and will irreversibly destroy all your data. 7 | Once the operation is performed the SSD drive is unrecoverable in any way. 8 | Authors of this software application take absolutely no 9 | responsibility for use of this program and its consequences. 10 | 11 | ## Legal 12 | Copyright (c) 2016 by Antoni Sawicki and Tomasz Nowak 13 | 14 | License: Apache 2.0 15 | -------------------------------------------------------------------------------- /build.cmd: -------------------------------------------------------------------------------- 1 | set INCLUDE= 2 | set LIB= 3 | set LIBPATH= 4 | call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64 5 | echo on 6 | del disktrim-x64.exe disktrim.res 7 | rc /D _UNICODE /D UNICODE disktrim.rc 8 | cl /Fedisktrim-x64.exe disktrim.c disktrim.res 9 | 10 | set INCLUDE= 11 | set LIB= 12 | set LIBPATH= 13 | call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64_x86 14 | echo on 15 | del disktrim-x86.exe disktrim.res 16 | rc /D _UNICODE /D UNICODE disktrim.rc 17 | cl /Fedisktrim-x86.exe disktrim.c disktrim.res 18 | 19 | set INCLUDE= 20 | set LIB= 21 | set LIBPATH= 22 | call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64_arm 23 | echo on 24 | del disktrim-arm.exe disktrim.res 25 | rc /D _UNICODE /D UNICODE disktrim.rc 26 | cl /D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE /Fedisktrim-arm.exe disktrim.c disktrim.res 27 | 28 | pause 29 | -------------------------------------------------------------------------------- /disk.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tenox7/disktrim/b2e365709bb19b2ea3c573aad88ed9b1a43f3d0c/disk.ico -------------------------------------------------------------------------------- /disktrim.c: -------------------------------------------------------------------------------- 1 | // 2 | // DiskTrim 2.0 by Antoni Sawicki and Tomasz Nowak 3 | // Requires Windows 2012 R2 / Windows 8.1 or above 4 | // 5 | // DiskTrim -- a small command line utility for Windows that allows to 6 | // send ATA TRIM and SCSI UNMAP commands directly to an SSD drive. The 7 | // operation is performed arbitrarily on a full sector range from zero 8 | // to the end. It securely erases contents of an entire SSD drive, and 9 | // tests whether TRIM actually worked. 10 | // 11 | // If you just want to test if your SSD supports TRIM without deleting 12 | // it's entire contents, you can simply create and mount a small .VHDX 13 | // file on top and run DiskTrim on the VHDX instead of physical disk. 14 | // 15 | // WARNING: 16 | // This utility is particularly dangerous and if used incorrectly - it 17 | // will permanently destroy contents of your SSD drive, and delete all 18 | // your data. Authors of this software application take absolutely no 19 | // responsibility for use of this program and its consequences. 20 | // 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | //#define SAFE 28 | 29 | #pragma pack(1) 30 | 31 | typedef struct _CDB_10 { 32 | UCHAR OperationCode; // 0x42 - SCSIOP_UNMAP 33 | UCHAR Anchor : 1; 34 | UCHAR Reserved1 : 7; 35 | UCHAR Reserved2[4]; 36 | UCHAR GroupNumber : 5; 37 | UCHAR Reserved3 : 3; 38 | UCHAR AllocationLength[2]; 39 | UCHAR Control; 40 | } CDB_10, * PCDB_10; 41 | 42 | typedef struct _CDB_16 { 43 | UCHAR OperationCode; 44 | UCHAR ServiceAction : 5; 45 | UCHAR Reserved1 : 3; 46 | UCHAR LBA[8]; 47 | UCHAR AllocationLength[4]; 48 | UCHAR PMI : 1; 49 | UCHAR Reserved2 : 7; 50 | UCHAR Control; 51 | } CDB_16, * PCDB_16; 52 | 53 | 54 | typedef struct _UNMAP_BLOCK_DESCRIPTOR { 55 | ULONG64 StartingLba; 56 | ULONG LbaCount; 57 | UCHAR Reserved[4]; 58 | } UNMAP_BLOCK_DESCRIPTOR, * PUNMAP_BLOCK_DESCRIPTOR; 59 | 60 | typedef struct _UNMAP_LIST_HEADER { 61 | USHORT DataLength; 62 | USHORT BlockDescrDataLength; 63 | UCHAR Reserved[4]; 64 | UNMAP_BLOCK_DESCRIPTOR Descriptors[0]; 65 | } UNMAP_LIST_HEADER, * PUNMAP_LIST_HEADER; 66 | 67 | 68 | typedef struct _READ_CAPACITY10 { 69 | ULONG LBA; 70 | ULONG BlockLength; 71 | } READ_CAPACITY10, * PREAD_CAPACITY10; 72 | 73 | typedef struct _READ_CAPACITY16 { 74 | ULONG64 LBA; 75 | ULONG BlockLength; 76 | UCHAR ProtEn : 1; 77 | UCHAR PType : 3; 78 | UCHAR Reserved1 : 4; 79 | UCHAR LBPerPBExp : 4; 80 | UCHAR PIExp : 4; 81 | UCHAR MSB : 6; 82 | UCHAR TPRZ : 1; 83 | UCHAR TPE : 1; 84 | UCHAR LSB; 85 | UCHAR Reserved2[16]; 86 | 87 | } READ_CAPACITY16, * PREAD_CAPACITY16; 88 | 89 | #pragma pack() 90 | 91 | 92 | typedef struct _SCSI_PASS_THROUGH { 93 | USHORT Length; 94 | UCHAR ScsiStatus; 95 | UCHAR PathId; 96 | UCHAR TargetId; 97 | UCHAR Lun; 98 | UCHAR CdbLength; 99 | UCHAR SenseInfoLength; 100 | UCHAR DataIn; 101 | ULONG DataTransferLength; 102 | ULONG TimeOutValue; 103 | ULONG_PTR DataBufferOffset; 104 | ULONG SenseInfoOffset; 105 | UCHAR Cdb[16]; 106 | } SCSI_PASS_THROUGH, * PSCSI_PASS_THROUGH; 107 | 108 | #define REVERSE_BYTES_SHORT( x ) ( ((x & 0xFF) << 8) | ((x & 0xFF00) >> 8)) 109 | #define REVERSE_BYTES_LONG( x ) ( ((x & 0xFF) << 24) | ((x & 0xFF00) << 8) | ((x & 0xFF0000) >> 8) | ((x & 0xFF000000) >> 24)) 110 | #define REVERSE_BYTES_LONG64( x ) ( ((x & 0xFF) << 56) | ((x & 0xFF00) << 40) | ((x & 0xFF0000) << 24) | ((x & 0xFF000000) << 8) | ((x & 0xFF00000000) >> 8) | ((x & 0xFF0000000000) >> 24) | ((x & 0xFF000000000000) >> 40) | ((x & 0xFF00000000000000) >> 56) ) 111 | 112 | #define SRB_FLAGS_DATA_IN 0x00000040 113 | #define SRB_FLAGS_DATA_OUT 0x00000080 114 | 115 | #define IOCTL_SCSI_BASE 0x00000004 116 | #define IOCTL_SCSI_PASS_THROUGH CTL_CODE(IOCTL_SCSI_BASE, 0x0401, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) 117 | 118 | #define SENSE_INFO_LENGTH 128 119 | 120 | #define TEST_PATTERN L"====[Test*Pattern]====" 121 | 122 | #define WIDEN2(x) L ## x 123 | #define WIDEN(x) WIDEN2(x) 124 | #define __WDATE__ WIDEN(__DATE__) 125 | #define __WTIME__ WIDEN(__TIME__) 126 | 127 | #define USAGE L"Usage: disktrim [-y] \n\n"\ 128 | L"Disk# number can be obtained from:\n"\ 129 | L"- Disk Management (diskmgmt.msc)\n"\ 130 | L"- diskpart (list disk)\n"\ 131 | L"- wmic diskdrive get index,caption,size\n"\ 132 | L"- get-disk\n"\ 133 | L"- get-physicaldisk | ft deviceid,friendlyname\n\n"\ 134 | L"Long form \\\\.\\PhysicalDriveXX is also allowed\n\n" 135 | 136 | void error(int exit, WCHAR* msg, ...) { 137 | va_list valist; 138 | WCHAR vaBuff[1024] = { L'\0' }; 139 | WCHAR errBuff[1024] = { L'\0' }; 140 | DWORD err; 141 | 142 | err = GetLastError(); 143 | 144 | va_start(valist, msg); 145 | vswprintf(vaBuff, ARRAYSIZE(vaBuff), msg, valist); 146 | va_end(valist); 147 | 148 | wprintf(L"%s: %s\n", (exit) ? L"ERROR" : L"WARNING", vaBuff); 149 | 150 | if (err) { 151 | FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), errBuff, ARRAYSIZE(errBuff), NULL); 152 | wprintf(L"[0x%08X] %s\n\n", err, errBuff); 153 | } 154 | else { 155 | putchar(L'\n'); 156 | } 157 | 158 | FlushFileBuffers(GetStdHandle(STD_OUTPUT_HANDLE)); 159 | 160 | if (exit) 161 | ExitProcess(1); 162 | } 163 | 164 | int wmain(int argc, WCHAR* argv[]) { 165 | HANDLE hDisk; 166 | WCHAR DevName[64] = { '\0' }; 167 | OVERLAPPED Ovr = { 0 }; 168 | WCHAR TestBuff[512] = { '\0' }; 169 | WCHAR* DiskNo = NULL; 170 | DWORD y = 0; 171 | wint_t p; 172 | PSCSI_PASS_THROUGH ScsiPass; 173 | GET_LENGTH_INFORMATION DiskLengthInfo; 174 | STORAGE_PROPERTY_QUERY trim_q = { StorageDeviceTrimProperty, PropertyStandardQuery }; 175 | DEVICE_TRIM_DESCRIPTOR trim_d = { 0 }; 176 | STORAGE_PROPERTY_QUERY desc_q = { StorageDeviceProperty, PropertyStandardQuery }; 177 | STORAGE_DESCRIPTOR_HEADER desc_h = { 0 }; 178 | PSTORAGE_DEVICE_DESCRIPTOR desc_d; 179 | PVOID Buffer; 180 | ULONG BufLen; 181 | ULONG TransferSize; 182 | PCDB_10 pCDB; 183 | PCDB_16 pCDB16; 184 | PUNMAP_LIST_HEADER pUnmapHdr; 185 | ULONG BytesRet; 186 | PUCHAR pSenseCode; 187 | PREAD_CAPACITY16 pReadCapacity; 188 | ULONG DiskBlockSize; 189 | ULONG64 DiskLbaCount; 190 | ULONG UnmapEntryCount; 191 | ULONG i; 192 | ULONG64 LBAStart, LBACount; 193 | 194 | 195 | wprintf(L"DiskTrim v2.2 by Antoni Sawicki & Tomasz Nowak, Build %s %s\n\n", __WDATE__, __WTIME__); 196 | 197 | if (argc == 3) { 198 | if (wcscmp(argv[1], L"-y") == 0) { 199 | DiskNo = argv[2]; 200 | y = 1; 201 | } 202 | else { 203 | error(1, L"argc=3 argv[1]=%s argv[2]=%s\n\n%s\n", argv[1], argv[2], USAGE); 204 | } 205 | } 206 | else if (argc == 2) { 207 | DiskNo = argv[1]; 208 | } 209 | else { 210 | error(1, L"Wrong number of parameters [argc=%d]\n\n%s\n", argc, USAGE); 211 | } 212 | 213 | if (DiskNo == NULL) 214 | error(1, L"DiskNo is empty\n"); 215 | 216 | if (wcsnicmp(DiskNo, L"\\\\.\\PhysicalDrive", 17) == 0) 217 | wcsncpy(DevName, DiskNo, ARRAYSIZE(DevName)); 218 | else if (iswdigit(*DiskNo)) 219 | swprintf(DevName, ARRAYSIZE(DevName), L"\\\\.\\PhysicalDrive%s", DiskNo); 220 | else 221 | error(1, USAGE, argv[0]); 222 | 223 | if ((hDisk = CreateFileW(DevName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)) == INVALID_HANDLE_VALUE) 224 | error(1, L"Cannot open %s", DevName); 225 | 226 | if (!DeviceIoControl(hDisk, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &DiskLengthInfo, sizeof(GET_LENGTH_INFORMATION), &BytesRet, NULL)) 227 | error(1, L"Error on DeviceIoControl IOCTL_DISK_GET_LENGTH_INFO [%d] ", BytesRet); 228 | 229 | if (!DeviceIoControl(hDisk, IOCTL_STORAGE_QUERY_PROPERTY, &trim_q, sizeof(trim_q), &trim_d, sizeof(trim_d), &BytesRet, NULL)) 230 | error(1, L"Error on DeviceIoControl IOCTL_STORAGE_QUERY_PROPERTY Trim Property [%d] ", BytesRet); 231 | 232 | if (!DeviceIoControl(hDisk, IOCTL_STORAGE_QUERY_PROPERTY, &desc_q, sizeof(desc_q), &desc_h, sizeof(desc_h), &BytesRet, NULL)) 233 | error(1, L"Error on DeviceIoControl IOCTL_STORAGE_QUERY_PROPERTY Device Property [%d] ", BytesRet); 234 | 235 | desc_d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, desc_h.Size); 236 | 237 | if (!DeviceIoControl(hDisk, IOCTL_STORAGE_QUERY_PROPERTY, &desc_q, sizeof(desc_q), desc_d, desc_h.Size, &BytesRet, NULL)) 238 | error(1, L"Error on DeviceIoControl IOCTL_STORAGE_QUERY_PROPERTY [%d] ", BytesRet); 239 | 240 | wprintf(L"Disk: %s\nSize: %.1f GB \n", DiskNo, (float)DiskLengthInfo.Length.QuadPart / 1024.0 / 1024.0 / 1024.0); 241 | 242 | if (desc_d->Version == sizeof(STORAGE_DEVICE_DESCRIPTOR)) 243 | wprintf(L"Type: %S %S\n", 244 | (desc_d->VendorIdOffset) ? (char*)desc_d + desc_d->VendorIdOffset : "n/a", 245 | (desc_d + desc_d->ProductIdOffset) ? (char*)desc_d + desc_d->ProductIdOffset : "n/a" 246 | ); 247 | 248 | if (trim_d.Version == sizeof(DEVICE_TRIM_DESCRIPTOR) && trim_d.TrimEnabled == 1) 249 | wprintf(L"Trim: Supported\n"); 250 | else 251 | wprintf(L"Trim: Not Supported\n"); 252 | 253 | 254 | if (!y) { 255 | wprintf(L"\n" 256 | L"WARNING: Contents of your drive an all data will be permanently erased! \n" 257 | L"There is no possibility of data recovery even with 3rd party companies.\n\n" 258 | L"Do you want to erase this disk (y/N) ? "); 259 | p = getwchar(); 260 | if (p == L'y') 261 | wprintf(L"All right...\n"); 262 | else 263 | error(1, L"\rAborting...\n"); 264 | } 265 | 266 | // 267 | // Query disk size 268 | // 269 | wprintf(L"Querying drive parameters...\n"); 270 | 271 | TransferSize = 36; 272 | 273 | BufLen = sizeof(SCSI_PASS_THROUGH) + SENSE_INFO_LENGTH + TransferSize; 274 | 275 | Buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, BufLen); 276 | 277 | (PVOID)ScsiPass = Buffer; 278 | 279 | ScsiPass->Length = sizeof(SCSI_PASS_THROUGH); 280 | ScsiPass->TargetId = 1; 281 | ScsiPass->PathId = 0; 282 | ScsiPass->Lun = 0; 283 | ScsiPass->CdbLength = 16; 284 | ScsiPass->SenseInfoLength = SENSE_INFO_LENGTH; 285 | ScsiPass->SenseInfoOffset = sizeof(SCSI_PASS_THROUGH); 286 | ScsiPass->DataIn = SRB_FLAGS_DATA_IN; 287 | ScsiPass->TimeOutValue = 5000; 288 | ScsiPass->DataTransferLength = TransferSize; 289 | ScsiPass->DataBufferOffset = ScsiPass->SenseInfoOffset + ScsiPass->SenseInfoLength; 290 | 291 | pSenseCode = (PUCHAR)Buffer + ScsiPass->SenseInfoOffset; 292 | 293 | (PVOID)pCDB16 = ScsiPass->Cdb; 294 | pCDB16->OperationCode = 0x9E; 295 | pCDB16->ServiceAction = 0x10; 296 | 297 | (PVOID)pReadCapacity = (PUCHAR)Buffer + ScsiPass->DataBufferOffset; 298 | 299 | 300 | if (!DeviceIoControl(hDisk, IOCTL_SCSI_PASS_THROUGH, Buffer, BufLen, Buffer, BufLen, &BytesRet, NULL)) 301 | error(1, L"Error on DeviceIoControl IOCTL_SCSI_PASS_THROUGH"); 302 | 303 | DiskLbaCount = REVERSE_BYTES_LONG64(pReadCapacity->LBA); 304 | DiskBlockSize = REVERSE_BYTES_LONG(pReadCapacity->BlockLength); 305 | 306 | wprintf(L"%s LBA: %I64u, Block: %lu, Size: %.1f GB\n", DevName, DiskLbaCount, DiskBlockSize, (float)(((float)DiskLbaCount * (float)DiskBlockSize) / 1024.0 / 1024.0 / 1024.0)); 307 | 308 | HeapFree(GetProcessHeap(), 0, Buffer); 309 | 310 | // There is no going back after this... 311 | #ifdef SAFE 312 | return 0; 313 | #endif 314 | 315 | // 316 | // Uninitialize disk so it doesn't have any partitions in order for pass through to work 317 | // 318 | wprintf(L"Deleting disk partitions...\n"); 319 | if (!DeviceIoControl(hDisk, IOCTL_DISK_DELETE_DRIVE_LAYOUT, NULL, 0, NULL, 0, &BytesRet, NULL)) 320 | error(1, L"Error on DeviceIoControl IOCTL_DISK_DELETE_DRIVE_LAYOUT [%d] ", BytesRet); 321 | 322 | // 323 | // Write test pattern 324 | // 325 | wprintf(L"Writing test pattern...\n"); 326 | ZeroMemory(&Ovr, sizeof(Ovr)); 327 | Ovr.Offset = 0x00; 328 | Ovr.OffsetHigh = 0; 329 | 330 | ZeroMemory(TestBuff, sizeof(TestBuff)); 331 | swprintf(TestBuff, ARRAYSIZE(TestBuff), TEST_PATTERN); 332 | 333 | if (!WriteFile(hDisk, TestBuff, sizeof(TestBuff), NULL, &Ovr)) 334 | error(1, L"Error writing test pattern to disk"); 335 | 336 | ZeroMemory(TestBuff, sizeof(TestBuff)); 337 | 338 | if (!ReadFile(hDisk, TestBuff, sizeof(TestBuff), NULL, &Ovr)) 339 | error(1, L"Error reading disk"); 340 | 341 | wprintf(L"Buffer before TRIM: \"%s\"\n", TestBuff); 342 | 343 | if (wcscmp(TestBuff, TEST_PATTERN) != 0) 344 | error(1, L"Unable to write test pattern to disk"); 345 | 346 | // 347 | // UNMAP 348 | // 349 | wprintf(L"Performing UNMAP on the LBA range...\n"); 350 | 351 | UnmapEntryCount = (DiskLbaCount >> 32) + 1; 352 | 353 | TransferSize = sizeof(UNMAP_LIST_HEADER) + (UnmapEntryCount * sizeof(UNMAP_BLOCK_DESCRIPTOR)); 354 | 355 | BufLen = sizeof(SCSI_PASS_THROUGH) + SENSE_INFO_LENGTH + TransferSize; 356 | 357 | Buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, BufLen); 358 | 359 | (PVOID)ScsiPass = Buffer; 360 | 361 | ScsiPass->Length = sizeof(SCSI_PASS_THROUGH); 362 | ScsiPass->TargetId = 1; 363 | ScsiPass->PathId = 0; 364 | ScsiPass->Lun = 0; 365 | ScsiPass->CdbLength = 10; 366 | ScsiPass->SenseInfoLength = SENSE_INFO_LENGTH; 367 | ScsiPass->SenseInfoOffset = sizeof(SCSI_PASS_THROUGH); 368 | ScsiPass->DataIn = SRB_FLAGS_DATA_OUT; 369 | ScsiPass->TimeOutValue = 5000; 370 | ScsiPass->DataTransferLength = TransferSize; 371 | ScsiPass->DataBufferOffset = sizeof(SCSI_PASS_THROUGH) + ScsiPass->SenseInfoLength; 372 | 373 | pSenseCode = (PUCHAR)Buffer + ScsiPass->SenseInfoOffset; 374 | 375 | (PVOID)pCDB = ScsiPass->Cdb; 376 | pCDB->OperationCode = 0x42; 377 | pCDB->Anchor = 0; 378 | pCDB->GroupNumber = 0; 379 | pCDB->AllocationLength[0] = (UCHAR)(TransferSize >> 8); 380 | pCDB->AllocationLength[1] = (UCHAR)TransferSize; 381 | 382 | (PVOID)pUnmapHdr = (PUCHAR)ScsiPass + ScsiPass->DataBufferOffset; 383 | 384 | pUnmapHdr->DataLength = REVERSE_BYTES_SHORT(TransferSize - 2); 385 | pUnmapHdr->BlockDescrDataLength = REVERSE_BYTES_SHORT(TransferSize - sizeof(UNMAP_LIST_HEADER)); 386 | 387 | 388 | LBAStart = 0; 389 | LBACount = DiskLbaCount + 1; 390 | 391 | for (i = 0; i < UnmapEntryCount; i++) { 392 | pUnmapHdr->Descriptors[i].StartingLba = REVERSE_BYTES_LONG64(LBAStart); 393 | pUnmapHdr->Descriptors[i].LbaCount = REVERSE_BYTES_LONG((ULONG)((LBACount < 0xFFFFFFFF) ? (LBACount & 0xFFFFFFFF) : 0xFFFFFFFF)); 394 | 395 | if (LBACount > 0xFFFFFFFF) { 396 | LBACount -= 0xFFFFFFFF; 397 | LBAStart += 0xFFFFFFFF; 398 | } 399 | } 400 | 401 | 402 | if (!DeviceIoControl(hDisk, IOCTL_SCSI_PASS_THROUGH, Buffer, BufLen, Buffer, BufLen, &BytesRet, NULL)) 403 | error(1, L"Error performing DeviceIoControl IOCTL_SCSI_PASS_THROUGH"); 404 | 405 | ZeroMemory(TestBuff, sizeof(TestBuff)); 406 | 407 | wprintf(L"Reading test pattern...\n"); 408 | if (!ReadFile(hDisk, TestBuff, sizeof(TestBuff), NULL, &Ovr)) 409 | error(1, L"Error reading disk"); 410 | 411 | wprintf(L"Buffer after TRIM : \"%s\" [if empty, TRIM worked]\n", TestBuff); 412 | 413 | if (wcscmp(TestBuff, TEST_PATTERN) == 0) 414 | error(1, L"TRIM didn't seem to work\n"); 415 | 416 | wprintf(L"Looks like TRIM worked!\n"); 417 | 418 | FlushFileBuffers(GetStdHandle(STD_OUTPUT_HANDLE)); 419 | 420 | return 0; 421 | } 422 | -------------------------------------------------------------------------------- /disktrim.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tenox7/disktrim/b2e365709bb19b2ea3c573aad88ed9b1a43f3d0c/disktrim.rc --------------------------------------------------------------------------------