From 87889506d908ae54b9b40a7e8095fff3583053a6 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Thu, 2 Jan 2020 12:50:11 +0200 Subject: [PATCH] initial commit --- Makefile | 13 +++++++++++ src/main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 Makefile create mode 100644 src/main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a2c4f4b --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +src = $(wildcard src/*.c) +obj = $(src:.c=.o) +bin = rawdisktest + +CFLAGS = -pedantic -Wall -g +LDFLAGS = -lsetupapi -luuid + +$(bin): $(obj) + $(CC) -o $(bin) $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..8f7b239 --- /dev/null +++ b/src/main.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int devidx, ifidx; + HDEVINFO devset; + SP_DEVINFO_DATA devinfo; + SP_DEVICE_INTERFACE_DATA devif; + SP_DEVICE_INTERFACE_DETAIL_DATA_A *devdetail; + DWORD detsz; + + if((devset = SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISK, 0, 0, + DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)) == INVALID_HANDLE_VALUE) { + fprintf(stderr, "failed to enumerate devices\n"); + return 1; + } + + devidx = 0; + for(;;) { + memset(&devinfo, 0, sizeof devinfo); + devinfo.cbSize = sizeof devinfo; + if(!SetupDiEnumDeviceInfo(devset, devidx, &devinfo)) { + if(GetLastError() == ERROR_NO_MORE_ITEMS) { + printf("no such device: %d\n", devidx); + break; + } + devidx++; + continue; + } + + printf("device %d:\n", devidx); + ifidx = 0; + for(;;) { + memset(&devif, 0, sizeof devif); + devif.cbSize = sizeof devif; + if(!SetupDiEnumDeviceInterfaces(devset, &devinfo, &GUID_DEVINTERFACE_DISK, ifidx, &devif)) { + if(GetLastError() == ERROR_NO_MORE_ITEMS) { + printf("no such interface: %d\n", ifidx); + break; + } + ifidx++; + continue; + } + + SetupDiGetDeviceInterfaceDetail(devset, &devif, 0, 0, &detsz, 0); + if(!(devdetail = malloc(detsz))) { + fprintf(stderr, "failed to allocate device interface detail buffer (size: %lu)\n", (unsigned long)detsz); + return 1; + } + devdetail->cbSize = detsz; + SetupDiGetDeviceInterfaceDetail(devset, &devif, devdetail, detsz, 0, 0); + printf("device %d.%d path: %s\n", devidx, ifidx, devdetail->DevicePath); + free(devdetail); + + ifidx++; + } + printf("found %d interfaces\n", ifidx); + devidx++; + } + + printf("found %d devices\n", devidx); + return 0; +} -- 1.7.10.4