rawdisk code added
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 8 Jan 2020 08:19:06 +0000 (10:19 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 8 Jan 2020 08:19:06 +0000 (10:19 +0200)
instimg.dsp
src/main.c
src/rawdisk.c [new file with mode: 0644]
src/rawdisk.h [new file with mode: 0644]

index 65018c2..85eb38f 100644 (file)
@@ -65,6 +65,7 @@ LINK32=link.exe
 # PROP Use_Debug_Libraries 1\r
 # PROP Output_Dir "Debug"\r
 # PROP Intermediate_Dir "Debug"\r
+# PROP Ignore_Export_Lib 0\r
 # PROP Target_Dir ""\r
 # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /YX /FD /GZ /c\r
 # ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /YX /FD /GZ /c\r
@@ -77,7 +78,7 @@ BSC32=bscmake.exe
 # ADD BSC32 /nologo\r
 LINK32=link.exe\r
 # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept\r
-# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept\r
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib  setupapi.lib /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept\r
 \r
 !ENDIF \r
 \r
@@ -94,6 +95,10 @@ SOURCE=.\src\main.c
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\src\rawdisk.c\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\src\widgets.c\r
 # End Source File\r
 # End Group\r
@@ -102,6 +107,10 @@ SOURCE=.\src\widgets.c
 # PROP Default_Filter "h;hpp;hxx;hm;inl"\r
 # Begin Source File\r
 \r
+SOURCE=.\src\rawdisk.h\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\src\widgets.h\r
 # End Source File\r
 # End Group\r
index 43a5b62..68b4c98 100644 (file)
@@ -2,23 +2,28 @@
 #include <stdlib.h>
 #include <windows.h>
 #include "widgets.h"
+#include "rawdisk.h"
 
 static struct wgt_window *win;
 static struct wgt_widget *lb_instto, *bn_inst, *bn_cancel, *cb_devs, *ck_usbonly;
+static struct rawdisk_device rawdev[64];
+static int num_rawdev;
 
 static void onclick(struct wgt_widget *w);
 static void onmodify(struct wgt_widget *w);
 
 int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, char *cmdline, int showcmd)
 {
-       int x, y;
+       int i, x, y;
        MSG msg;
-       static const char *items[] = {
-               "item one",
-               "item two",
-               "item three"
-       };
+       static const char *items[64];
 
+       if((num_rawdev = rawdisk_detect(rawdev, sizeof rawdev / sizeof *rawdev)) == -1) {
+               return 1;
+       }
+       for(i=0; i<num_rawdev; i++) {
+               items[i] = rawdev[i].name;
+       }
 
        if(!(win = wgt_window("256boss USB stick installer", 400, 300))) {
                return 1;
@@ -26,8 +31,8 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, char *cmdline, int show
        lb_instto = wgt_label(win, "Install to device:", 10, 10);
 
        x = wgt_xpos_after(lb_instto, WGT_AUTO);
-       cb_devs = wgt_combo(win, items, sizeof items / sizeof *items, 0,
-                       x, 10, WGT_AUTO, WGT_AUTO, onmodify);
+       cb_devs = wgt_combo(win, items, num_rawdev, 0, x, 10, WGT_AUTO, WGT_AUTO,
+                       onmodify);
 
        y = wgt_ypos_after(cb_devs, 16);
        ck_usbonly = wgt_checkbox(win, "only show USB devices", 1, x, y,
diff --git a/src/rawdisk.c b/src/rawdisk.c
new file mode 100644 (file)
index 0000000..b2576ba
--- /dev/null
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <windows.h>
+#include <setupapi.h>
+#include <devguid.h>
+#include <winioctl.h>
+#include "rawdisk.h"
+
+static GUID guid_iface_disk = {0x53f56307, 0xb6bf, 0x11d0, {0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b}};
+
+int rawdisk_detect(struct rawdisk_device *disks, int max_disks)
+{
+       int devidx, ifidx, count;
+       HDEVINFO devset;
+       SP_DEVINFO_DATA devdata;
+       SP_DEVICE_INTERFACE_DATA devif;
+       SP_DEVICE_INTERFACE_DETAIL_DATA_A *devdetail;
+       DWORD size, regtype;
+       char devname[1024];
+
+       if((devset = SetupDiGetClassDevs(&guid_iface_disk, 0, 0,
+                       DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)) == INVALID_HANDLE_VALUE) {
+               fprintf(stderr, "failed to enumerate devices\n");
+               return -1;
+       }
+
+       count = 0;
+       devidx = 0;
+       for(;;) {
+               memset(&devdata, 0, sizeof devdata);
+               devdata.cbSize = sizeof devdata;
+               if(!SetupDiEnumDeviceInfo(devset, devidx, &devdata)) {
+                       if(GetLastError() == ERROR_NO_MORE_ITEMS) break;
+                       devidx++;
+                       continue;
+               }
+
+               regtype = SPDRP_PHYSICAL_DEVICE_OBJECT_NAME;
+               SetupDiGetDeviceRegistryProperty(devset, &devdata, SPDRP_FRIENDLYNAME,
+                               &regtype, (unsigned char*)devname, sizeof devname, &size);
+
+               count = 0;
+               ifidx = 0;
+               for(;;) {
+                       memset(&devif, 0, sizeof devif);
+                       devif.cbSize = sizeof devif;
+                       if(!SetupDiEnumDeviceInterfaces(devset, &devdata, &guid_iface_disk, ifidx, &devif)) {
+                               if(GetLastError() == ERROR_NO_MORE_ITEMS) break;
+                               ifidx++;
+                               continue;
+                       }
+
+                       SetupDiGetDeviceInterfaceDetail(devset, &devif, 0, 0, &size, 0);
+                       if(!(devdetail = malloc(size))) {
+                               fprintf(stderr, "failed to allocate device interface detail buffer (size: %lu)\n", (unsigned long)size);
+                               return -1;
+                       }
+                       devdetail->cbSize = sizeof *devdetail;
+                       SetupDiGetDeviceInterfaceDetail(devset, &devif, devdetail, size, 0, 0);
+
+
+                       if(count < max_disks) {
+                               disks[count].path = strdup(devdetail->DevicePath);
+                               disks[count].name = strdup(devname);
+                               if(!disks[count].path || !disks[count].name) {
+                                       fprintf(stderr, "failed to allocate device strings\n");
+                                       return -1;
+                               }
+                               count++;
+                       }
+
+                       free(devdetail);
+                       ifidx++;
+               }
+
+               devidx++;
+       }
+
+       return count;
+}
diff --git a/src/rawdisk.h b/src/rawdisk.h
new file mode 100644 (file)
index 0000000..5c843b1
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef RAWDISK_H_
+#define RAWDISK_H_
+
+struct rawdisk_device {
+       char *name;
+       char *path;
+};
+
+int rawdisk_detect(struct rawdisk_device *disks, int max_disks);
+
+#endif /* RAWDISK_H_ */
\ No newline at end of file