├── LICENSE ├── README.md ├── lsblk.c └── lsblk.png /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 | # lsblk for windows 2 | 3 | Similar to Linux *lsblk(1)* but for Windows. 4 | 5 | The latest version supports printing drive letters, mount points and filesystem types. 6 | 7 | ![lsblk screenshot](lsblk.png) 8 | 9 | Download under [releases](https://github.com/tenox7/lsblk/releases) 10 | 11 | Explanation of header columns: 12 | 13 | ``` 14 | ST - Status (1=healthy, 0=unhealthy) 15 | TR - Trim/Unmap/Discard capability support 16 | RM - Removable media (1=yes, 0=no) 17 | RO - Read only (1=yes, 0=no) 18 | ``` 19 | 20 | Flags: 21 | 22 | ``` 23 | -n - do not list, query and print volumes, drive letters and mount points 24 | -d - debug output 25 | ``` 26 | -------------------------------------------------------------------------------- /lsblk.c: -------------------------------------------------------------------------------- 1 | // 2 | // lsblk for Windows 3 | // 4 | // Copyright (c) 2016-2018 by Antoni Sawicki 5 | // Copyright (c) 2021 by Google LLC 6 | // 7 | 8 | #define VERSION L"1.2" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #pragma comment(lib, "ntdll.lib") 20 | #pragma comment(lib, "shlwapi.lib") 21 | 22 | DWORD debug = 0; 23 | float MB = 1 << 20; 24 | float GB = 1 << 30; 25 | WCHAR* ft[] = { L"False", L"True" }; 26 | WCHAR* bus[] = { L"UNKNOWN", L"SCSI", L"ATAPI", L"ATA", L"1394", L"SSA", L"FC", L"USB", L"RAID", L"ISCSI", L"SAS", L"SATA", L"SD", L"MMC", L"VIRTUAL", L"VHD", L"MAX", L"NVME" }; 27 | WCHAR* layout[] = { L"MBR", L"GPT", L"RAW" }; 28 | char* MBRTypes[] = { "Unused", "FAT12", "XENIX root", "XENIX /usr", "FAT16 < 32 MiB", "Extended", "FAT16", "NTFS/exFAT", "AIX boot, OS/2, Commodore DOS", "AIX data, Coherent, QNX", "Coherent swap, OPUS, OS/2 Boot Manager", "FAT32", "FAT32 (LBA)", "Unknown", "FAT16 (LBA)", "Extended (LBA)", "OPUS", "Hidden FAT12", "Compaq diagnostics, recovery partition", "Unknown", "Hidden FAT16 < 32 MiB, AST-DOS", "Unknown", "Hidden FAT16", "Hidden NTFS", "AST-Windows swap", "Willowtech Photon coS", "Unknown", "Hidden FAT32", "Hidden FAT32 (LBA)", "Unknown", "Hidden FAT16 (LBA)", "Unknown", "Willowsoft Overture File System", "Oxygen FSo2", "Oxygen Extended ", "SpeedStor reserved", "NEC-DOS", "Unknown", "SpeedStor reserved", "Hidden NTFS WinRE", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "SpeedStor reserved", "Unknown", "SpeedStor reserved", "SpeedStor reserved", "Unknown", "SpeedStor reserved", "Unknown", "Theos", "Plan 9", "Unknown", "Unknown", "Partition Magic", "Hidden NetWare", "Unknown", "Unknown", "VENIX 80286", "PReP Boot", "Secure File System", "PTS-DOS", "Unknown", "Priam, EUMEL/Elan", "EUMEL/Elan", "EUMEL/Elan", "EUMEL/Elan", "Unknown", "ALFS/THIN lightweight filesystem for DOS", "Unknown", "Unknown", "QNX 4", "QNX 4", "QNX 4, Oberon", "Ontrack DM, R/O, FAT", "Ontrack DM, R/W, FAT", "CP/M, Microport UNIX", "Ontrack DM 6", "Ontrack DM 6", "EZ-Drive", "Golden Bow VFeature", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Priam EDISK", "Unknown", "Unknown", "Unknown", "Unknown", "SpeedStor", "Unknown", "GNU Hurd, System V, 386/ix", "NetWare 286", "NetWare", "NetWare 386", "NetWare", "NetWare", "NetWare NSS", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "DiskSecure Multi-Boot", "Unknown", "UNIX 7th Edition", "Unknown", "Unknown", "IBM PC/IX", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Old MINIX", "MINIX, Old Linux", "Linux swap, Solaris", "Linux", "Hidden by OS/2, APM hibernation", "Linux extended", "NT Stripe Set", "NT Stripe Set", "Linux Plaintext", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Linux LVM", "Unknown", "Unknown", "Unknown", "Unknown", "Amoeba, Hidden Linux", "Amoeba bad blocks", "Unknown", "Unknown", "Unknown", "Unknown", "Mylex EISA SCSI", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "BSD/OS", "Hibernation", "HP Volume Expansion", "Unknown", "HP Volume Expansion", "HP Volume Expansion", "FreeBSD", "OpenBSD", "NeXTStep", "Apple UFS", "NetBSD", "Olivetti DOS FAT12", "Apple Boot", "Unknown", "Unknown", "Unknown", "Apple HFS", "BootStar", "HP Volume Expansion", "Unknown", "HP Volume Expansion", "HP Volume Expansion", "Unknown", "HP Volume Expansion", "BSDi", "BSDi swap", "Unknown", "Unknown", "PTS BootWizard", "Unknown", "Unknown", "Solaris boot", "Solaris", "Novell DOS, DR-DOS secured", "DR-DOS secured FAT12", "DR-DOS reserved", "DR-DOS reserved", "DR-DOS secured FAT16 < 32 MiB", "Unknown", "DR-DOS secured FAT16", "Syrinx", "DR-DOS reserved", "DR-DOS reserved", "DR-DOS reserved", "DR-DOS secured FAT32", "DR-DOS secured FAT32 (LBA)", "DR-DOS reserved", "DR-DOS secured FAT16 (LBA)", "DR-DOS secured extended (LBA)", "Multiuser DOS secured FAT12", "Multiuser DOS secured FAT12", "Unknown", "Unknown", "Multiuser DOS secured FAT16 < 32 MiB", "Multiuser DOS secured extended", "Multiuser DOS secured FAT16", "Unknown", "CP/M", "Unknown", "Filesystem-less data", "CP/M, CCP/M, CTOS", "Unknown", "Unknown", "Dell partition", "BootIt EMBRM", "Unknown", "SpeedStor", "DOS read/only", "SpeedStor", "SpeedStor", "Tandy DOS", "SpeedStor", "Unknown", "Unknown", "Unknown", "Unknown", "BeOS", "Unknown", "Spryt*x", "Guid Partition Table", "EFI system partition", "Linux boot", "SpeedStor", "DOS 3.3 secondary, Unisys DOS", "SpeedStor", "SpeedStor", "Prologue", "SpeedStor", "Unknown", "Unknown", "Unknown", "Unknown", "VMWare VMFS", "VMWare VMKCORE", "Linux RAID, FreeDOS", "SpeedStor, LANStep, PS/2 IML", "Xenix bad block" }; 29 | 30 | #define DIRECTORY_QUERY (0x0001) 31 | #define DIRECTORY_TRAVERSE (0x0002) 32 | typedef struct _OBJECT_DIRECTORY_INFORMATION { 33 | UNICODE_STRING Name; 34 | UNICODE_STRING TypeName; 35 | } OBJECT_DIRECTORY_INFORMATION, * POBJECT_DIRECTORY_INFORMATION; 36 | 37 | typedef struct _VOLINFO { 38 | WCHAR DiskName[256]; 39 | LONGLONG Start; 40 | LONGLONG Length; 41 | WCHAR MntPaths[1024]; 42 | WCHAR FsType[MAX_PATH + 1]; 43 | } VOLINFO, * PVOLINFO; 44 | 45 | #define GUID_FORMAT L" {%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX} " 46 | #define GUID_ARG(guid) (guid).Data1, (guid).Data2, (guid).Data3, (guid).Data4[0], (guid).Data4[1], (guid).Data4[2], (guid).Data4[3], (guid).Data4[4], (guid).Data4[5], (guid).Data4[6], (guid).Data4[7] 47 | 48 | VOID ErrPt(BOOL, WCHAR*, ...); 49 | VOID ListDisks(PVOLINFO*, DWORD); 50 | VOID QueryDisk(WCHAR*, PVOLINFO*, DWORD); 51 | DWORD ListVolumes(PVOLINFO*); 52 | NTSTATUS QueryVolume(WCHAR*, PVOLINFO); 53 | VOID DumpVolumes(PVOLINFO*, DWORD); 54 | 55 | VOID ErrPt(BOOL exit, WCHAR* msg, ...) { 56 | va_list ap; 57 | WCHAR buff[1024] = { 0 }; 58 | DWORD err; 59 | 60 | wprintf(L"Error: "); 61 | va_start(ap, msg); 62 | vwprintf(msg, ap); 63 | va_end(ap); 64 | putchar(L'\n'); 65 | err = GetLastError(); 66 | if (err) { 67 | FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buff, ARRAYSIZE(buff), NULL); 68 | wprintf(L"[0x%08X] %s\n", err, buff); 69 | } 70 | 71 | if (exit) 72 | ExitProcess(1); 73 | } 74 | 75 | VOID SizePt(float size) { 76 | if (size < GB) 77 | wprintf(L"%5.0fM", size / MB); 78 | else 79 | wprintf(L"%5.0fG", size / GB); 80 | } 81 | 82 | VOID ListDisks(PVOLINFO* Mounts, DWORD mnts) { 83 | HANDLE hDir; 84 | OBJECT_ATTRIBUTES attr = { 0 }; 85 | UNICODE_STRING root = { 0 }; 86 | POBJECT_DIRECTORY_INFORMATION DirInfo; 87 | DWORD nDirInfo = 2048; 88 | ULONG i = 0, index = 0, bytes = 0, istart = 0, first = 0; 89 | NTSTATUS status; 90 | 91 | RtlInitUnicodeString(&root, L"\\GLOBAL??"); 92 | InitializeObjectAttributes(&attr, &root, 0, NULL, NULL); 93 | 94 | status = NtOpenDirectoryObject(&hDir, DIRECTORY_QUERY | DIRECTORY_TRAVERSE, &attr); 95 | if (status != 0) 96 | ErrPt(TRUE, L"NtOpenDirectoryObject NTSTATUS=%08X", status); 97 | 98 | if (debug) wprintf(L"Disks:\n"); 99 | DirInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(OBJECT_DIRECTORY_INFORMATION) * nDirInfo); 100 | if (DirInfo == NULL) 101 | ErrPt(TRUE, L"Unable to allocate memory"); 102 | first = TRUE; 103 | istart = 0; 104 | do { 105 | moremem: 106 | status = NtQueryDirectoryObject(hDir, DirInfo, sizeof(OBJECT_DIRECTORY_INFORMATION) * nDirInfo, FALSE, first, &index, &bytes); 107 | if (status == 0x00000105) { 108 | if (debug) wprintf(L"\nNtQueryDirectoryObject needs more memory n=%d\n", nDirInfo); 109 | nDirInfo = nDirInfo * 2; 110 | DirInfo = (POBJECT_DIRECTORY_INFORMATION)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, DirInfo, sizeof(OBJECT_DIRECTORY_INFORMATION) * nDirInfo); 111 | if (DirInfo == NULL) 112 | ErrPt(TRUE, L"Unable to allocate memory for NtQueryDirectoryObject"); 113 | goto moremem; 114 | } 115 | if (status < 0) 116 | break; 117 | 118 | for (i = 0; i < index - istart; i++) { 119 | if (wcsncmp(DirInfo[i].Name.Buffer, L"PhysicalDrive", 13) == 0) { 120 | if (debug) wprintf(L"\\\\.\\%s\n", DirInfo[i].Name.Buffer); 121 | QueryDisk(DirInfo[i].Name.Buffer, Mounts, mnts); 122 | } 123 | } 124 | 125 | istart = index; 126 | first = FALSE; 127 | } while (TRUE); 128 | 129 | NtClose(hDir); 130 | HeapFree(GetProcessHeap(), 0, DirInfo); 131 | } 132 | 133 | VOID QueryDisk(WCHAR* name, PVOLINFO* Mounts, DWORD mnts) { 134 | HANDLE hDisk; 135 | GET_LENGTH_INFORMATION DiskLengthInfo; 136 | GET_DISK_ATTRIBUTES DiskAttributes; 137 | PDRIVE_LAYOUT_INFORMATION_EX DiskLayout; 138 | WCHAR nDiskLayout = 64; 139 | SCSI_ADDRESS DiskAddress; 140 | STORAGE_PROPERTY_QUERY desc_q = { StorageDeviceProperty, PropertyStandardQuery }; 141 | STORAGE_DESCRIPTOR_HEADER desc_h = { 0 }; 142 | PSTORAGE_DEVICE_DESCRIPTOR desc_d; 143 | STORAGE_PROPERTY_QUERY trim_q = { StorageDeviceTrimProperty, PropertyStandardQuery }; 144 | DEVICE_TRIM_DESCRIPTOR trim_d = { 0 }; 145 | OBJECT_ATTRIBUTES attr = { 0 }; 146 | UNICODE_STRING diskname = { 0 }; 147 | WCHAR buff[1024] = { 0 }; 148 | PWCHAR mnt = NULL; 149 | PWCHAR fst = NULL; 150 | IO_STATUS_BLOCK iosb; 151 | NTSTATUS status; 152 | int i, n, p; 153 | 154 | swprintf(buff, ARRAYSIZE(buff), L"\\??\\%s", name); 155 | RtlInitUnicodeString(&diskname, buff); 156 | InitializeObjectAttributes(&attr, &diskname, 0, NULL, NULL); 157 | 158 | // NAME 159 | wprintf(L"%-15s", name); 160 | 161 | status = NtOpenFile(&hDisk, GENERIC_READ | SYNCHRONIZE, &attr, &iosb, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT); 162 | if (status != 0) { 163 | ErrPt(FALSE, L"Unable to open %s, NTSTATUS=0x%08X\n\n", diskname.Buffer, status); 164 | return; 165 | } 166 | 167 | // HCTL 168 | status = NtDeviceIoControlFile(hDisk, NULL, NULL, NULL, &iosb, IOCTL_SCSI_GET_ADDRESS, NULL, 0, &DiskAddress, sizeof(DiskAddress)); 169 | if (status == 0) 170 | wprintf(L" %d:%d:%d:%d ", DiskAddress.PortNumber, DiskAddress.PathId, DiskAddress.TargetId, DiskAddress.Lun); 171 | else 172 | wprintf(L" -:-:-: -"); 173 | 174 | // SIZE 175 | status = NtDeviceIoControlFile(hDisk, NULL, NULL, NULL, &iosb, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &DiskLengthInfo, sizeof(DiskLengthInfo)); 176 | if (status == 0) 177 | SizePt((float)DiskLengthInfo.Length.QuadPart); 178 | else 179 | wprintf(L" "); 180 | 181 | // ST 182 | status = NtDeviceIoControlFile(hDisk, NULL, NULL, NULL, &iosb, IOCTL_DISK_GET_DISK_ATTRIBUTES, NULL, 0, &DiskAttributes, sizeof(DiskAttributes)); 183 | if (status == 0) 184 | wprintf(L" %d ", (DiskAttributes.Attributes) ? 0 : 1); 185 | else 186 | wprintf(L" "); 187 | 188 | // TR 189 | status = NtDeviceIoControlFile(hDisk, NULL, NULL, NULL, &iosb, IOCTL_STORAGE_QUERY_PROPERTY, &trim_q, sizeof(trim_q), &trim_d, sizeof(trim_d)); 190 | if (status == 0) 191 | wprintf(L" %d ", (trim_d.Version == sizeof(DEVICE_TRIM_DESCRIPTOR) && trim_d.TrimEnabled == 1) ? 1 : 0); 192 | else 193 | wprintf(L" 0 "); 194 | 195 | // RM RO TYPE DESCRIPTION 196 | status = NtDeviceIoControlFile(hDisk, NULL, NULL, NULL, &iosb, IOCTL_STORAGE_QUERY_PROPERTY, &desc_q, sizeof(desc_q), &desc_h, sizeof(desc_h)); 197 | if (status != 0) { 198 | NtClose(hDisk); 199 | wprintf(L"\n"); 200 | if (debug) ErrPt(FALSE, L"IOCTL_STORAGE_QUERY_PROPERTY NTSTATUS=%08X", status); 201 | return; 202 | } 203 | 204 | desc_d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, desc_h.Size); 205 | if (desc_d == NULL) { 206 | NtClose(hDisk); 207 | wprintf(L"\n"); 208 | if (debug) ErrPt(FALSE, L"Unable to allocate memory"); 209 | return; 210 | } 211 | 212 | status = NtDeviceIoControlFile(hDisk, NULL, NULL, NULL, &iosb, IOCTL_STORAGE_QUERY_PROPERTY, &desc_q, sizeof(desc_q), desc_d, desc_h.Size); 213 | if (status != 0 || desc_d->Version != sizeof(STORAGE_DEVICE_DESCRIPTOR)) { 214 | NtClose(hDisk); 215 | wprintf(L"\n"); 216 | return; 217 | } 218 | swprintf(buff, sizeof(buff) / sizeof(WCHAR), L"%S %S", 219 | (desc_d->VendorIdOffset) ? (char*)desc_d + desc_d->VendorIdOffset : "", 220 | (desc_d->ProductIdOffset) ? (char*)desc_d + desc_d->ProductIdOffset : "" 221 | ); 222 | StrTrimW(buff, L" "); 223 | wprintf( 224 | L" %d " 225 | L" %d " 226 | L" %-5s " 227 | L"%s\n", 228 | desc_d->RemovableMedia, 229 | (NtDeviceIoControlFile(hDisk, NULL, NULL, NULL, &iosb, IOCTL_DISK_IS_WRITABLE, NULL, 0, NULL, 0) < 0) ? 1 : 0, 230 | (desc_d->BusType < ARRAYSIZE(bus)) ? bus[desc_d->BusType] : bus[0], 231 | buff 232 | ); 233 | HeapFree(GetProcessHeap(), 0, desc_d); 234 | 235 | // Partitions 236 | DiskLayout = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DRIVE_LAYOUT_INFORMATION_EX) * nDiskLayout); 237 | moremem: 238 | status = NtDeviceIoControlFile(hDisk, NULL, NULL, NULL, &iosb, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0, DiskLayout, sizeof(DRIVE_LAYOUT_INFORMATION_EX) * nDiskLayout); 239 | if (status == 0xC0000023) { 240 | if (debug) wprintf(L"\nIOCTL_DISK_GET_DRIVE_LAYOUT_EX needs more memory n=%d\n", nDiskLayout); 241 | nDiskLayout = nDiskLayout * 2; 242 | DiskLayout = (PDRIVE_LAYOUT_INFORMATION_EX)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, DiskLayout, sizeof(DRIVE_LAYOUT_INFORMATION_EX) * nDiskLayout); 243 | if (DiskLayout == NULL) 244 | ErrPt(TRUE, L"Unable to allocate memory for DiskLayout"); 245 | goto moremem; 246 | } 247 | if (status != 0) { 248 | NtClose(hDisk); 249 | HeapFree(GetProcessHeap(), 0, DiskLayout); 250 | wprintf(L"\n"); 251 | if (debug) ErrPt(FALSE, L"IOCTL_DISK_GET_DRIVE_LAYOUT_EX NTSTATUS=%08X", status); 252 | return; 253 | } 254 | 255 | for (n = 0; n < DiskLayout->PartitionCount; n++) { 256 | if (!DiskLayout->PartitionEntry[n].PartitionNumber) 257 | continue; 258 | 259 | // Mount Points 260 | for (p = 0; p < mnts; p++) { 261 | if (debug > 1) wprintf(L"\n %d: %s==%s %lld==%lld %lld=%lld\n", p, (*Mounts)[p].DiskName, name, (*Mounts)[p].Start, DiskLayout->PartitionEntry[n].StartingOffset.QuadPart, (*Mounts)[p].Length, DiskLayout->PartitionEntry[n].PartitionLength.QuadPart); 262 | mnt = NULL; 263 | if (wcscmp((*Mounts)[p].DiskName, name) == 0 && 264 | (*Mounts)[p].Start == DiskLayout->PartitionEntry[n].StartingOffset.QuadPart && 265 | (*Mounts)[p].Length == DiskLayout->PartitionEntry[n].PartitionLength.QuadPart 266 | ) { 267 | //wprintf(L"%s", (*Mounts)[p].MntPaths); 268 | mnt = (*Mounts)[p].MntPaths; 269 | fst = (*Mounts)[p].FsType; 270 | break; 271 | } 272 | } 273 | 274 | wprintf(L" L Partition %d ", DiskLayout->PartitionEntry[n].PartitionNumber); 275 | SizePt((float)DiskLayout->PartitionEntry[n].PartitionLength.QuadPart); 276 | wprintf(L" %-4s ", layout[DiskLayout->PartitionEntry[n].PartitionStyle]); 277 | 278 | 279 | if (DiskLayout->PartitionEntry[n].PartitionStyle == PARTITION_STYLE_MBR) { 280 | wprintf(L"%s %s %S", 281 | IsContainerPartition(DiskLayout->PartitionEntry[n].Mbr.PartitionType&PARTITION_EXTENDED) ? L"Primary" : L"Extended", 282 | (DiskLayout->PartitionEntry[n].Mbr.BootIndicator) ? L"(Active)" : L"", 283 | MBRTypes[DiskLayout->PartitionEntry[n].Mbr.PartitionType] 284 | ); 285 | 286 | if (fst) 287 | wprintf(L" %s", fst); 288 | 289 | if (mnt) 290 | wprintf(L" %s", mnt); 291 | } 292 | else if (DiskLayout->PartitionEntry[n].PartitionStyle == PARTITION_STYLE_GPT) { 293 | wprintf(L"%s", DiskLayout->PartitionEntry[n].Gpt.Name); 294 | 295 | //if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_BASIC_DATA_GUID)) wprintf(L" [Basic Data]"); 296 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_BSP_GUID)) wprintf(L" [BSP]"); 297 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_CLUSTER_GUID)) wprintf(L" [Cluster]"); 298 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_DPP_GUID)) wprintf(L" [DPP]"); 299 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_LDM_DATA_GUID)) wprintf(L" [LDM Data]"); 300 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_LDM_METADATA_GUID)) wprintf(L" [LDM Meta]"); 301 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_LEGACY_BL_GUID)) wprintf(L" [Legacy Boot]"); 302 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_LEGACY_BL_GUID_BACKUP)) wprintf(L" [Legacy Boot Backup]"); 303 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_MAIN_OS_GUID)) wprintf(L" [OS Main]"); 304 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_MSFT_RECOVERY_GUID)) wprintf(L" [Recovery]"); 305 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_MSFT_RESERVED_GUID)) wprintf(L" [Reserved]"); 306 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_MSFT_SNAPSHOT_GUID)) wprintf(L" [Snapshot]"); 307 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_OS_DATA_GUID)) wprintf(L" [OS Data]"); 308 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_PATCH_GUID)) wprintf(L" [Patch]"); 309 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_PRE_INSTALLED_GUID)) wprintf(L" [Preinstall]"); 310 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_SPACES_GUID)) wprintf(L" [Spaces]"); 311 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_SPACES_DATA_GUID)) wprintf(L" [Spaces Data]"); 312 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_SYSTEM_GUID)) wprintf(L" [System]"); 313 | if (IsEqualPartitionType(DiskLayout->PartitionEntry[n].Gpt.PartitionType, PARTITION_WINDOWS_SYSTEM_GUID)) wprintf(L" [Windows System]"); 314 | 315 | if ((DiskLayout->PartitionEntry[n].Gpt.Attributes & GPT_ATTRIBUTE_PLATFORM_REQUIRED) == GPT_ATTRIBUTE_PLATFORM_REQUIRED) wprintf(L" [Required]"); 316 | if ((DiskLayout->PartitionEntry[n].Gpt.Attributes & GPT_BASIC_DATA_ATTRIBUTE_HIDDEN) == GPT_BASIC_DATA_ATTRIBUTE_HIDDEN) wprintf(L" [Hidden]"); 317 | if ((DiskLayout->PartitionEntry[n].Gpt.Attributes & GPT_BASIC_DATA_ATTRIBUTE_SHADOW_COPY) == GPT_BASIC_DATA_ATTRIBUTE_SHADOW_COPY) wprintf(L" [Shadow_Copy]"); 318 | if ((DiskLayout->PartitionEntry[n].Gpt.Attributes & GPT_BASIC_DATA_ATTRIBUTE_READ_ONLY) == GPT_BASIC_DATA_ATTRIBUTE_READ_ONLY) wprintf(L" [Readonly]"); 319 | 320 | if(debug) wprintf(GUID_FORMAT, GUID_ARG(DiskLayout->PartitionEntry[n].Gpt.PartitionType)); 321 | 322 | if (fst) 323 | wprintf(L" %s", fst); 324 | 325 | if (mnt) 326 | wprintf(L" %s", mnt); 327 | } 328 | else { 329 | wprintf(L"RAW"); 330 | } 331 | 332 | 333 | wprintf(L"\n"); 334 | } 335 | 336 | NtClose(hDisk); 337 | HeapFree(GetProcessHeap(), 0, DiskLayout); 338 | } 339 | 340 | DWORD ListVolumes(PVOLINFO* Mounts) { 341 | WCHAR VolName[1024] = { 0 }; 342 | HANDLE find; 343 | DWORD n = 1; 344 | 345 | find = FindFirstVolumeW(VolName, ARRAYSIZE(VolName)); 346 | if (find == INVALID_HANDLE_VALUE) { 347 | ErrPt(FALSE, L"Unable to list volumes\n"); 348 | return 0; 349 | } 350 | 351 | *Mounts = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VOLINFO)); 352 | if (*Mounts == NULL) { 353 | ErrPt(FALSE, L"Unable to allocate memory\n"); 354 | return 0; 355 | } 356 | 357 | if (debug) wprintf(L"Volumes:\n"); 358 | 359 | do { 360 | if (QueryVolume(VolName, &(*Mounts)[n - 1]) != 0) 361 | continue; 362 | 363 | if (debug) wprintf(L"n=%d d=%s f=%s p=%s\n", n, (*Mounts)[n - 1].DiskName, (*Mounts)[n - 1].FsType, (*Mounts)[n - 1].MntPaths); 364 | 365 | n++; 366 | *Mounts = (PVOLINFO)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *Mounts, n * sizeof(VOLINFO)); 367 | if (*Mounts == NULL) { 368 | ErrPt(FALSE, L"Unable to reallocate memory\n"); 369 | return n - 1; 370 | } 371 | } while (FindNextVolumeW(find, VolName, ARRAYSIZE(VolName))); 372 | 373 | return n - 1; 374 | } 375 | 376 | NTSTATUS QueryVolume(WCHAR* name, PVOLINFO Mounts) { 377 | WCHAR buff[1024] = { 0 }; 378 | DWORD len, n; 379 | PWCHAR next; 380 | NTSTATUS status; 381 | OBJECT_ATTRIBUTES attr = { 0 }; 382 | UNICODE_STRING volname = { 0 }; 383 | IO_STATUS_BLOCK iosb; 384 | HANDLE hVol; 385 | VOLUME_DISK_EXTENTS volext; 386 | 387 | if (debug) wprintf(L"\n* %s\n", name); 388 | 389 | // Filesystem Type 390 | GetVolumeInformationW(name, NULL, 0, NULL, NULL, NULL, Mounts->FsType, ARRAYSIZE(Mounts->FsType)); 391 | 392 | // Extents 393 | swprintf(buff, ARRAYSIZE(buff), L"\\GLOBAL??%s", name + 3); 394 | buff[wcslen(buff) - 1] = L'\0'; 395 | if (debug) wprintf(L" %s\n", buff); 396 | RtlInitUnicodeString(&volname, buff); 397 | InitializeObjectAttributes(&attr, &volname, 0, NULL, NULL); 398 | 399 | status = NtOpenFile(&hVol, SYNCHRONIZE, &attr, &iosb, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT); 400 | if (status != 0) { 401 | if (debug) ErrPt(FALSE, L"Unable to open %s, NTSTATUS=0x%08X\n\n", volname.Buffer, status); 402 | return 1; 403 | } 404 | 405 | // TODO: add support for dynamic disks 406 | status = NtDeviceIoControlFile(hVol, NULL, NULL, NULL, &iosb, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, &volext, sizeof(VOLUME_DISK_EXTENTS)); 407 | if (status != 0) { 408 | if (debug) ErrPt(FALSE, L"Unable to get volume extents, NTSTATUS=0x%08X\n\n", status); 409 | NtClose(hVol); 410 | return 1; 411 | } 412 | NtClose(hVol); 413 | 414 | if (volext.NumberOfDiskExtents < 1) 415 | return 1; 416 | 417 | if (debug) wprintf(L" Extent: disk=%d start=%llu len=%llu \n\n", 418 | volext.Extents[0].DiskNumber, 419 | volext.Extents[0].StartingOffset.QuadPart, 420 | volext.Extents[0].ExtentLength.QuadPart 421 | ); 422 | 423 | swprintf(Mounts->DiskName, ARRAYSIZE(Mounts->DiskName), L"PhysicalDrive%d", volext.Extents[0].DiskNumber); 424 | Mounts->Start = volext.Extents[0].StartingOffset.QuadPart; 425 | Mounts->Length = volext.Extents[0].ExtentLength.QuadPart; 426 | 427 | // Mount points 428 | if (!GetVolumePathNamesForVolumeNameW(name, buff, ARRAYSIZE(buff), &len)) 429 | return 0; 430 | 431 | if (len < 2) 432 | return 0; 433 | 434 | if (debug) wprintf(L"Mounts: \n"); 435 | 436 | for (next = buff; next[0] != L'\0'; next += wcslen(next) + 1) { 437 | if (debug) wprintf(L" - %s ", next); 438 | swprintf(Mounts->MntPaths, ARRAYSIZE(Mounts->MntPaths), L"%s%s ", Mounts->MntPaths, next); 439 | } 440 | if (debug) wprintf(L"\n"); 441 | 442 | return 0; 443 | } 444 | 445 | VOID DumpVolumes(PVOLINFO* Mounts, DWORD mnts) { 446 | DWORD n; 447 | wprintf(L"\nMounts:\n\n"); 448 | for (n = 0; n < mnts; n++) { 449 | wprintf(L"%d: disk=%s start=%llu len=%llu fs=%s paths=%s\n", 450 | n, 451 | (*Mounts)[n].DiskName, 452 | (*Mounts)[n].Start, 453 | (*Mounts)[n].Length, 454 | (*Mounts)[n].FsType, 455 | (*Mounts)[n].MntPaths 456 | ); 457 | } 458 | return; 459 | } 460 | 461 | 462 | 463 | int wmain(int argc, WCHAR** argv) { 464 | PVOLINFO Vols; 465 | DWORD nvol = 0; 466 | BOOL getvols = TRUE; 467 | int n; 468 | 469 | for (n = 1; n < argc; n++) { 470 | if (wcscmp(argv[n], L"-d") == 0) 471 | debug = 1; 472 | if (wcscmp(argv[n], L"-n") == 0) 473 | getvols = FALSE; 474 | } 475 | 476 | if (getvols) 477 | nvol = ListVolumes(&Vols); 478 | if (debug) DumpVolumes(&Vols, nvol); 479 | 480 | wprintf(L"lsblk for Windows v%s, Copyright (c) 2021 Google LLC\n\n" 481 | L"NAME HCTL SIZE ST TR RM RO TYPE DESCRIPTION\n", VERSION); 482 | 483 | ListDisks(&Vols, nvol); 484 | return 0; 485 | } 486 | -------------------------------------------------------------------------------- /lsblk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tenox7/lsblk/de244ce3d8d07b421308e912168c3e8e65f53dee/lsblk.png --------------------------------------------------------------------------------