From 80eded53dedf234ea49f3142adcc6e8ce6393104 Mon Sep 17 00:00:00 2001 From: PatrickvL Date: Sun, 1 Jan 2017 01:10:53 +0100 Subject: [PATCH] Updated conversion of object attributes so that non-file API's do not try to map filenames. Also used CxbxObjectAttributesToNT in more kernel functions. --- src/CxbxKrnl/EmuFile.cpp | 44 +++++++++++++------------ src/CxbxKrnl/EmuKrnlNt.cpp | 67 ++++++++++++-------------------------- 2 files changed, 45 insertions(+), 66 deletions(-) diff --git a/src/CxbxKrnl/EmuFile.cpp b/src/CxbxKrnl/EmuFile.cpp index be388af77..9c2be97f7 100644 --- a/src/CxbxKrnl/EmuFile.cpp +++ b/src/CxbxKrnl/EmuFile.cpp @@ -317,34 +317,38 @@ NTSTATUS CxbxObjectAttributesToNT( OUT NativeObjectAttributes& nativeObjectAttributes, const std::string aFileAPIName) { - NTSTATUS result = STATUS_SUCCESS; - std::string RelativeXboxPath; - std::wstring RelativeHostPath; - NtDll::HANDLE RootDirectory; - if (ObjectAttributes == NULL) { // When the pointer is nil, make sure we pass nil to Windows too : - nativeObjectAttributes.NtObjAttrPtr = NULL; - return result; + nativeObjectAttributes.NtObjAttrPtr = nullptr; + return STATUS_SUCCESS; } + // Pick up the ObjectName, and let's see what to make of it : + std::string ObjectName = PSTRING_to_string(ObjectAttributes->ObjectName); + std::wstring RelativeHostPath; + NtDll::HANDLE RootDirectory = ObjectAttributes->RootDirectory; + // Is there a filename API given? + if (aFileAPIName.size() > 0) + { + // Then interpret the ObjectName as a filename, and update it to host relative : + NTSTATUS result = _CxbxConvertFilePath(ObjectName, /*OUT*/RelativeHostPath, /*OUT*/&RootDirectory, aFileAPIName); + if (FAILED(result)) + return result; + } + else + // When not called from a file-handling API, just convert the ObjectName to a wide string : + RelativeHostPath = string_to_wstring(ObjectName); + + // Copy the wide string to the unicode string + wcscpy_s(nativeObjectAttributes.wszObjectName, RelativeHostPath.c_str()); + NtDll::RtlInitUnicodeString(&nativeObjectAttributes.NtUnicodeString, nativeObjectAttributes.wszObjectName); + // And initialize the NT ObjectAttributes with that : + InitializeObjectAttributes(&nativeObjectAttributes.NtObjAttr, &nativeObjectAttributes.NtUnicodeString, ObjectAttributes->Attributes, RootDirectory, NULL); // ObjectAttributes are given, so make sure the pointer we're going to pass to Windows is assigned : nativeObjectAttributes.NtObjAttrPtr = &nativeObjectAttributes.NtObjAttr; - RelativeXboxPath = PSTRING_to_string(ObjectAttributes->ObjectName); - result = _CxbxConvertFilePath(RelativeXboxPath, /*OUT*/RelativeHostPath, /*OUT*/&RootDirectory, aFileAPIName); - if (!FAILED(result)) - { - // Copy relative path string to the unicode string - wcscpy_s(nativeObjectAttributes.wszObjectName, RelativeHostPath.c_str()); - NtDll::RtlInitUnicodeString(&nativeObjectAttributes.NtUnicodeString, nativeObjectAttributes.wszObjectName); - - // Initialize the NT ObjectAttributes - InitializeObjectAttributes(&nativeObjectAttributes.NtObjAttr, &nativeObjectAttributes.NtUnicodeString, ObjectAttributes->Attributes, RootDirectory, NULL); - } - - return result; + return STATUS_SUCCESS; } int CxbxDeviceIndexByDevicePath(const char *XboxDevicePath) diff --git a/src/CxbxKrnl/EmuKrnlNt.cpp b/src/CxbxKrnl/EmuKrnlNt.cpp index e503472ba..9b2b59c6c 100644 --- a/src/CxbxKrnl/EmuKrnlNt.cpp +++ b/src/CxbxKrnl/EmuKrnlNt.cpp @@ -232,23 +232,21 @@ XBSYSAPI EXPORTNUM(189) xboxkrnl::NTSTATUS NTAPI xboxkrnl::NtCreateEvent // initialize object attributes NativeObjectAttributes nativeObjectAttributes; - NTSTATUS ret = CxbxObjectAttributesToNT(ObjectAttributes, /*var*/nativeObjectAttributes); + CxbxObjectAttributesToNT(ObjectAttributes, /*var*/nativeObjectAttributes); - if (ret == STATUS_SUCCESS) - { - // TODO : Is this the correct ACCESS_MASK? : - const ACCESS_MASK DesiredAccess = EVENT_ALL_ACCESS; + // TODO : Is this the correct ACCESS_MASK? : + const ACCESS_MASK DesiredAccess = EVENT_ALL_ACCESS; - // redirect to Win2k/XP - ret = NtDll::NtCreateEvent( - /*OUT*/EventHandle, - DesiredAccess, - nativeObjectAttributes.NtObjAttrPtr, - (NtDll::EVENT_TYPE)EventType, - InitialState); - // TODO : Instead of the above, we should consider using the Ke*Event APIs, but - // that would require us to create the event's kernel object with the Ob* api's too! - } + // redirect to Win2k/XP + NTSTATUS ret = NtDll::NtCreateEvent( + /*OUT*/EventHandle, + DesiredAccess, + nativeObjectAttributes.NtObjAttrPtr, + (NtDll::EVENT_TYPE)EventType, + InitialState); + + // TODO : Instead of the above, we should consider using the Ke*Event APIs, but + // that would require us to create the event's kernel object with the Ob* api's too! if (FAILED(ret)) EmuWarning("NtCreateEvent Failed!"); @@ -305,33 +303,9 @@ XBSYSAPI EXPORTNUM(192) xboxkrnl::NTSTATUS NTAPI xboxkrnl::NtCreateMutant LOG_FUNC_ARG(InitialOwner) LOG_FUNC_END; - char *szBuffer = (ObjectAttributes != NULL) ? ObjectAttributes->ObjectName->Buffer : nullptr; - wchar_t wszObjectName[MAX_PATH]; - - NtDll::UNICODE_STRING NtUnicodeString; - NtDll::OBJECT_ATTRIBUTES NtObjAttr; - // initialize object attributes - if (szBuffer != nullptr) - { - mbstowcs(/*Dest=*/wszObjectName, /*Source=*/DrivePrefix.c_str(), /*MaxCount=*/DrivePrefix.length()); - mbstowcs(/*Dest=*/wszObjectName + DrivePrefix.length(), /*Source=*/szBuffer, /*MaxCount=*/MAX_PATH); - - NtDll::RtlInitUnicodeString(&NtUnicodeString, wszObjectName); - - InitializeObjectAttributes(&NtObjAttr, &NtUnicodeString, ObjectAttributes->Attributes, ObjectAttributes->RootDirectory, nullptr); - } - - NtObjAttr.RootDirectory = 0; - - // TODO : Replace above with : - // - // // initialize object attributes - // NativeObjectAttributes nativeObjectAttributes; - // NTSTATUS ret = CxbxObjectAttributesToNT(ObjectAttributes, /*var*/nativeObjectAttributes); - - // if (ret == STATUS_SUCCESS) - // { + NativeObjectAttributes nativeObjectAttributes; + CxbxObjectAttributesToNT(ObjectAttributes, /*var*/nativeObjectAttributes); // TODO : Is this the correct ACCESS_MASK? : const ACCESS_MASK DesiredAccess = MUTANT_ALL_ACCESS; @@ -340,7 +314,7 @@ XBSYSAPI EXPORTNUM(192) xboxkrnl::NTSTATUS NTAPI xboxkrnl::NtCreateMutant NTSTATUS ret = NtDll::NtCreateMutant( /*OUT*/MutantHandle, DesiredAccess, - (szBuffer != 0) ? &NtObjAttr : nullptr, + nativeObjectAttributes.NtObjAttrPtr, InitialOwner); if (FAILED(ret)) @@ -372,13 +346,14 @@ XBSYSAPI EXPORTNUM(193) xboxkrnl::NTSTATUS NTAPI xboxkrnl::NtCreateSemaphore // TODO : Is this the correct ACCESS_MASK? : const ACCESS_MASK DesiredAccess = SEMAPHORE_ALL_ACCESS; - // TODO : Call CxbxObjectAttributesToNT on ObjectAttributes? + NativeObjectAttributes nativeObjectAttributes; + CxbxObjectAttributesToNT(ObjectAttributes, nativeObjectAttributes); // redirect to Win2k/XP NTSTATUS ret = NtDll::NtCreateSemaphore( /*OUT*/SemaphoreHandle, DesiredAccess, - (NtDll::POBJECT_ATTRIBUTES)ObjectAttributes, + (NtDll::POBJECT_ATTRIBUTES)nativeObjectAttributes.NtObjAttrPtr, InitialCount, MaximumCount); @@ -410,7 +385,7 @@ XBSYSAPI EXPORTNUM(194) xboxkrnl::NTSTATUS NTAPI xboxkrnl::NtCreateTimer const ACCESS_MASK DesiredAccess = TIMER_ALL_ACCESS; NativeObjectAttributes nativeObjectAttributes; - CxbxObjectAttributesToNT(ObjectAttributes, nativeObjectAttributes, "NtCreateTimer"); + CxbxObjectAttributesToNT(ObjectAttributes, nativeObjectAttributes); // redirect to Windows NT // TODO : Untested @@ -418,7 +393,7 @@ XBSYSAPI EXPORTNUM(194) xboxkrnl::NTSTATUS NTAPI xboxkrnl::NtCreateTimer ( /*OUT*/TimerHandle, DesiredAccess, - (NtDll::POBJECT_ATTRIBUTES)&nativeObjectAttributes, + (NtDll::POBJECT_ATTRIBUTES)nativeObjectAttributes.NtObjAttrPtr, (NtDll::TIMER_TYPE)TimerType );