| 1 |
---------------------------------------------------------------------- |
| 2 |
Patch name: patch.example-vdisk-dll |
| 3 |
Author: Volker Ruppert |
| 4 |
Date: 11 Mar 2013 |
| 5 |
Status: Demo |
| 6 |
|
| 7 |
Detailed description: |
| 8 |
This patch shows how to write the source for the VDISK.DLL required for the |
| 9 |
disk image mode 'dll'. This demo can be compiled with MSVC nmake and it can |
| 10 |
be used to access 'flat' type images. Some disk images formats have licenses |
| 11 |
incompatible with the Bochs one and writing an external DLL could be a way |
| 12 |
to fix this. The DLL needs to implement these functions: |
| 13 |
|
| 14 |
int vdisk_open(const char *path, int flags) |
| 15 |
BOOL vdisk_read(int vunit, LONGLONG blk, void *buf) |
| 16 |
BOOL vdisk_write(int vunit, LONGLONG blk, const void *buf) |
| 17 |
void vdisk_close(int vunit) |
| 18 |
LONGLONG vdisk_get_size(int vunit) |
| 19 |
|
| 20 |
The function vdisk_open() returns the 'vunit' value to be used in the other |
| 21 |
functions or -1 on failure. The demo is limited to 8 flat disk images and |
| 22 |
does not handle the flags value. The 'blk' value is the sector address and |
| 23 |
the returned size is the sector count. |
| 24 |
|
| 25 |
Apply patch to what version: |
| 26 |
svn revision 11650 |
| 27 |
---------------------------------------------------------------------- |
| 28 |
diff -urN ../bochs/misc/vdisk/Makefile ./misc/vdisk/Makefile |
| 29 |
--- ../bochs/misc/vdisk/Makefile 1970-01-01 01:00:00.000000000 +0100 |
| 30 |
+++ ./misc/vdisk/Makefile 2013-03-11 17:58:03.526467800 +0100 |
| 31 |
@@ -0,0 +1,2 @@ |
| 32 |
+all: |
| 33 |
+ cl /nologo /Ox /LD /Tc vdisk.c /link |
| 34 |
diff -urN ../bochs/misc/vdisk/vdisk.c ./misc/vdisk/vdisk.c |
| 35 |
--- ../bochs/misc/vdisk/vdisk.c 1970-01-01 01:00:00.000000000 +0100 |
| 36 |
+++ ./misc/vdisk/vdisk.c 2013-03-09 12:12:19.687570500 +0100 |
| 37 |
@@ -0,0 +1,123 @@ |
| 38 |
+// Copyright (C) 2013 Volker Ruppert |
| 39 |
+// |
| 40 |
+// This library is free software; you can redistribute it and/or |
| 41 |
+// modify it under the terms of the GNU Lesser General Public |
| 42 |
+// License as published by the Free Software Foundation; either |
| 43 |
+// version 2 of the License, or (at your option) any later version. |
| 44 |
+// |
| 45 |
+// This library is distributed in the hope that it will be useful, |
| 46 |
+// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 47 |
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 48 |
+// Lesser General Public License for more details. |
| 49 |
+// |
| 50 |
+// You should have received a copy of the GNU Lesser General Public |
| 51 |
+// License along with this library; if not, write to the Free Software |
| 52 |
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 53 |
+ |
| 54 |
+// VDISK.DLL (demo for disk image mode 'dll') |
| 55 |
+ |
| 56 |
+#include <windows.h> |
| 57 |
+ |
| 58 |
+#define VUNIT_MAX 8 |
| 59 |
+ |
| 60 |
+HANDLE hFile[VUNIT_MAX]; |
| 61 |
+BOOL vunit_open[VUNIT_MAX]; |
| 62 |
+UINT vunit_count; |
| 63 |
+ |
| 64 |
+__declspec(dllexport) BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved) |
| 65 |
+{ |
| 66 |
+ int i; |
| 67 |
+ |
| 68 |
+ switch (fdwReason) { |
| 69 |
+ case DLL_PROCESS_ATTACH: |
| 70 |
+ vunit_count = 0; |
| 71 |
+ for (i = 0; i < VUNIT_MAX; i++) |
| 72 |
+ vunit_open[i] = FALSE; |
| 73 |
+ break; |
| 74 |
+ case DLL_PROCESS_DETACH: |
| 75 |
+ break; |
| 76 |
+ case DLL_THREAD_ATTACH: |
| 77 |
+ break; |
| 78 |
+ case DLL_THREAD_DETACH: |
| 79 |
+ break; |
| 80 |
+ } |
| 81 |
+ return TRUE; |
| 82 |
+} |
| 83 |
+ |
| 84 |
+__declspec(dllexport) int vdisk_open(const char *path, int flags) |
| 85 |
+{ |
| 86 |
+ if (vunit_count == VUNIT_MAX) |
| 87 |
+ return -1; |
| 88 |
+ |
| 89 |
+ hFile[vunit_count] = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 90 |
+ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 91 |
+ if (hFile[vunit_count] != INVALID_HANDLE_VALUE) { |
| 92 |
+ vunit_open[vunit_count] = TRUE; |
| 93 |
+ return vunit_count++; |
| 94 |
+ } else { |
| 95 |
+ return -1; |
| 96 |
+ } |
| 97 |
+} |
| 98 |
+ |
| 99 |
+__declspec(dllexport) BOOL vdisk_read(int vunit, LONGLONG blk, void *buf) |
| 100 |
+{ |
| 101 |
+ LONG offlo, offhi; |
| 102 |
+ DWORD len2; |
| 103 |
+ |
| 104 |
+ if ((vunit < 0) || (vunit >= VUNIT_MAX)) |
| 105 |
+ return FALSE; |
| 106 |
+ |
| 107 |
+ if (vunit_open[vunit]) { |
| 108 |
+ offlo = (LONG)(blk << 9); |
| 109 |
+ offhi = (LONG)(blk >> 23); |
| 110 |
+ if (SetFilePointer(hFile[vunit], offlo, &offhi, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
| 111 |
+ return FALSE; |
| 112 |
+ } |
| 113 |
+ return ReadFile(hFile[vunit], buf, 512, &len2, NULL); |
| 114 |
+ } |
| 115 |
+ return FALSE; |
| 116 |
+} |
| 117 |
+ |
| 118 |
+__declspec(dllexport) BOOL vdisk_write(int vunit, LONGLONG blk, const void *buf) |
| 119 |
+{ |
| 120 |
+ LONG offlo, offhi; |
| 121 |
+ DWORD len2; |
| 122 |
+ |
| 123 |
+ if ((vunit < 0) || (vunit >= VUNIT_MAX)) |
| 124 |
+ return FALSE; |
| 125 |
+ |
| 126 |
+ if (vunit_open[vunit]) { |
| 127 |
+ offlo = (LONG)(blk << 9); |
| 128 |
+ offhi = (LONG)(blk >> 23); |
| 129 |
+ if (SetFilePointer(hFile[vunit], offlo, &offhi, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
| 130 |
+ return FALSE; |
| 131 |
+ } |
| 132 |
+ return WriteFile(hFile[vunit], buf, 512, &len2, NULL); |
| 133 |
+ } |
| 134 |
+ return FALSE; |
| 135 |
+} |
| 136 |
+ |
| 137 |
+__declspec(dllexport) void vdisk_close(int vunit) |
| 138 |
+{ |
| 139 |
+ if ((vunit < 0) || (vunit >= VUNIT_MAX)) |
| 140 |
+ return; |
| 141 |
+ |
| 142 |
+ if (vunit_open[vunit]) { |
| 143 |
+ CloseHandle(hFile[vunit]); |
| 144 |
+ vunit_open[vunit] = FALSE; |
| 145 |
+ } |
| 146 |
+} |
| 147 |
+ |
| 148 |
+__declspec(dllexport) LONGLONG vdisk_get_size(int vunit) |
| 149 |
+{ |
| 150 |
+ LARGE_INTEGER fSize; |
| 151 |
+ |
| 152 |
+ if ((vunit < 0) || (vunit >= VUNIT_MAX)) |
| 153 |
+ return 0; |
| 154 |
+ |
| 155 |
+ if (vunit_open[vunit]) { |
| 156 |
+ GetFileSizeEx(hFile[vunit], &fSize); |
| 157 |
+ return (LONGLONG)(fSize.QuadPart >> 9); |
| 158 |
+ } |
| 159 |
+ return 0; |
| 160 |
+} |