Bochs/bochs/patches/patch.example-vdisk-dll

161 lines
4.9 KiB
Plaintext

----------------------------------------------------------------------
Patch name: patch.example-vdisk-dll
Author: Volker Ruppert
Date: 11 Mar 2013
Status: Demo
Detailed description:
This patch shows how to write the source for the VDISK.DLL required for the
disk image mode 'dll'. This demo can be compiled with MSVC nmake and it can
be used to access 'flat' type images. Some disk images formats have licenses
incompatible with the Bochs one and writing an external DLL could be a way
to fix this. The DLL needs to implement these functions:
int vdisk_open(const char *path, int flags)
BOOL vdisk_read(int vunit, LONGLONG blk, void *buf)
BOOL vdisk_write(int vunit, LONGLONG blk, const void *buf)
void vdisk_close(int vunit)
LONGLONG vdisk_get_size(int vunit)
The function vdisk_open() returns the 'vunit' value to be used in the other
functions or -1 on failure. The demo is limited to 8 flat disk images and
does not handle the flags value. The 'blk' value is the sector address and
the returned size is the sector count.
Apply patch to what version:
svn revision 11650
----------------------------------------------------------------------
diff -urN ../bochs/misc/vdisk/Makefile ./misc/vdisk/Makefile
--- ../bochs/misc/vdisk/Makefile 1970-01-01 01:00:00.000000000 +0100
+++ ./misc/vdisk/Makefile 2013-03-11 17:58:03.526467800 +0100
@@ -0,0 +1,2 @@
+all:
+ cl /nologo /Ox /LD /Tc vdisk.c /link
diff -urN ../bochs/misc/vdisk/vdisk.c ./misc/vdisk/vdisk.c
--- ../bochs/misc/vdisk/vdisk.c 1970-01-01 01:00:00.000000000 +0100
+++ ./misc/vdisk/vdisk.c 2013-03-09 12:12:19.687570500 +0100
@@ -0,0 +1,123 @@
+// Copyright (C) 2013 Volker Ruppert
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+// VDISK.DLL (demo for disk image mode 'dll')
+
+#include <windows.h>
+
+#define VUNIT_MAX 8
+
+HANDLE hFile[VUNIT_MAX];
+BOOL vunit_open[VUNIT_MAX];
+UINT vunit_count;
+
+__declspec(dllexport) BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
+{
+ int i;
+
+ switch (fdwReason) {
+ case DLL_PROCESS_ATTACH:
+ vunit_count = 0;
+ for (i = 0; i < VUNIT_MAX; i++)
+ vunit_open[i] = FALSE;
+ break;
+ case DLL_PROCESS_DETACH:
+ break;
+ case DLL_THREAD_ATTACH:
+ break;
+ case DLL_THREAD_DETACH:
+ break;
+ }
+ return TRUE;
+}
+
+__declspec(dllexport) int vdisk_open(const char *path, int flags)
+{
+ if (vunit_count == VUNIT_MAX)
+ return -1;
+
+ hFile[vunit_count] = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL,
+ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (hFile[vunit_count] != INVALID_HANDLE_VALUE) {
+ vunit_open[vunit_count] = TRUE;
+ return vunit_count++;
+ } else {
+ return -1;
+ }
+}
+
+__declspec(dllexport) BOOL vdisk_read(int vunit, LONGLONG blk, void *buf)
+{
+ LONG offlo, offhi;
+ DWORD len2;
+
+ if ((vunit < 0) || (vunit >= VUNIT_MAX))
+ return FALSE;
+
+ if (vunit_open[vunit]) {
+ offlo = (LONG)(blk << 9);
+ offhi = (LONG)(blk >> 23);
+ if (SetFilePointer(hFile[vunit], offlo, &offhi, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
+ return FALSE;
+ }
+ return ReadFile(hFile[vunit], buf, 512, &len2, NULL);
+ }
+ return FALSE;
+}
+
+__declspec(dllexport) BOOL vdisk_write(int vunit, LONGLONG blk, const void *buf)
+{
+ LONG offlo, offhi;
+ DWORD len2;
+
+ if ((vunit < 0) || (vunit >= VUNIT_MAX))
+ return FALSE;
+
+ if (vunit_open[vunit]) {
+ offlo = (LONG)(blk << 9);
+ offhi = (LONG)(blk >> 23);
+ if (SetFilePointer(hFile[vunit], offlo, &offhi, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
+ return FALSE;
+ }
+ return WriteFile(hFile[vunit], buf, 512, &len2, NULL);
+ }
+ return FALSE;
+}
+
+__declspec(dllexport) void vdisk_close(int vunit)
+{
+ if ((vunit < 0) || (vunit >= VUNIT_MAX))
+ return;
+
+ if (vunit_open[vunit]) {
+ CloseHandle(hFile[vunit]);
+ vunit_open[vunit] = FALSE;
+ }
+}
+
+__declspec(dllexport) LONGLONG vdisk_get_size(int vunit)
+{
+ LARGE_INTEGER fSize;
+
+ if ((vunit < 0) || (vunit >= VUNIT_MAX))
+ return 0;
+
+ if (vunit_open[vunit]) {
+ GetFileSizeEx(hFile[vunit], &fSize);
+ return (LONGLONG)(fSize.QuadPart >> 9);
+ }
+ return 0;
+}