Skip to content

Commit

Permalink
XeUnloadSection
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo720 committed Dec 27, 2024
1 parent 1be3c2f commit ba5b14d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nboxkrnl/ke/thunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ ULONG KernelThunkTable[379] =
(ULONG)FUNC(nullptr), //(ULONG)VARIABLE(&XboxSignatureKey), // 0x0145 (325)
(ULONG)VARIABLE(&XeImageFileName), // 0x0146 (326)
(ULONG)FUNC(&XeLoadSection), // 0x0147 (327)
(ULONG)FUNC(nullptr), //(ULONG)FUNC(&XeUnloadSection), // 0x0148 (328)
(ULONG)FUNC(&XeUnloadSection), // 0x0148 (328)
(ULONG)FUNC(nullptr), //(ULONG)FUNC(&READ_PORT_BUFFER_UCHAR), // 0x0149 (329)
(ULONG)FUNC(nullptr), //(ULONG)FUNC(&READ_PORT_BUFFER_USHORT), // 0x014A (330)
(ULONG)FUNC(nullptr), //(ULONG)FUNC(&READ_PORT_BUFFER_ULONG), // 0x014B (331)
Expand Down
49 changes: 49 additions & 0 deletions nboxkrnl/xe/xe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,52 @@ EXPORTNUM(327) NTSTATUS XBOXAPI XeLoadSection

return STATUS_SUCCESS;
}

// Source: Cxbx-Reloaded
EXPORTNUM(328) NTSTATUS XBOXAPI XeUnloadSection
(
PXBE_SECTION Section
)
{
NTSTATUS Status = STATUS_INVALID_PARAMETER;
RtlEnterCriticalSectionAndRegion(&XepXbeLoaderLock);

// If the section was loaded, process it
if (Section->SectionReferenceCount > 0) {
// Decrement the reference count
Section->SectionReferenceCount -= 1;

// Free the section and the physical memory in use if necessary
if (Section->SectionReferenceCount == 0) {
// REMARK: the following can be tested with Broken Sword - The Sleeping Dragon, RalliSport Challenge, ...
// Test-case: Apex/Racing Evoluzione requires the memory NOT to be zeroed

ULONG BaseAddress = (ULONG)Section->VirtualAddress;
ULONG EndingAddress = (ULONG)Section->VirtualAddress + Section->VirtualSize;

// Decrement the head/tail page reference counters
(*Section->HeadReferenceCount)--;
(*Section->TailReferenceCount)--;

if ((*Section->TailReferenceCount) != 0) {
EndingAddress = ROUND_DOWN_4K(EndingAddress);
}

if ((*Section->HeadReferenceCount) != 0) {
BaseAddress = ROUND_UP_4K(BaseAddress);
}

if (EndingAddress > BaseAddress) {
PVOID BaseAddress2 = (PVOID)BaseAddress;
ULONG RegionSize = EndingAddress - BaseAddress;
NtFreeVirtualMemory(&BaseAddress2, &RegionSize, MEM_DECOMMIT);
}
}

Status = STATUS_SUCCESS;
}

RtlLeaveCriticalSectionAndRegion(&XepXbeLoaderLock);

return Status;
}
5 changes: 5 additions & 0 deletions nboxkrnl/xe/xe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ EXPORTNUM(327) DLLEXPORT NTSTATUS XBOXAPI XeLoadSection
PXBE_SECTION Section
);

EXPORTNUM(328) DLLEXPORT NTSTATUS XBOXAPI XeUnloadSection
(
PXBE_SECTION Section
);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit ba5b14d

Please sign in to comment.