diff --git a/src/CxbxLoader/CxbxEmulator.cpp b/src/CxbxLoader/CxbxEmulator.cpp new file mode 100644 index 000000000..b14e02448 --- /dev/null +++ b/src/CxbxLoader/CxbxEmulator.cpp @@ -0,0 +1,75 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com +// ****************************************************************** +// * +// * .,-::::: .,:: .::::::::. .,:: .: +// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;; +// * [[[ '[[,,[[' [[[__[[\. '[[,,[[' +// * $$$ Y$$$P $$""""Y$$ Y$$$P +// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo, +// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm, +// * +// * This file is part of the Cxbx project. +// * +// * Cxbx is free software; you can redistribute it +// * and/or modify it under the terms of the GNU General Public +// * License as published by the Free Software Foundation; either +// * version 2 of the license, or (at your option) any later version. +// * +// * This program 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 General Public License for more details. +// * +// * You should have recieved a copy of the GNU General Public License +// * along with this program; see the file COPYING. +// * If not, write to the Free Software Foundation, Inc., +// * 59 Temple Place - Suite 330, Bostom, MA 02111-1307, USA. +// * +// * (c) 2017-2019 Patrick van Logchem +// * +// * All rights reserved +// * +// ****************************************************************** + +// CxbxEmulator.cpp : Defines the exported functions for the DLL application. + +#include "stdafx.h" +#include "Cxbx.h" + +DWORD WINAPI Emulate() +{ + FUNC_EXPORTS + + /*! verify Cxbx-Loader.exe is loaded to base address 0x00010000 */ + if ((UINT_PTR)GetModuleHandle(nullptr) != CXBX_BASE_ADDR) + { + /*! CXBX_BASE_ADDR is defined as 0x00010000, which is the base address of + the Cxbx-Loader.exe host executable. + Set in Cxbx-Loader.exe Project options, Linker, Advanced, Base Address */ + MessageBox(NULL, "Cxbx-Loader.exe was not loaded to base address 0x00010000 (which is a requirement for Xbox emulation)", "Cxbx-Reloaded", MB_OK); + return 1; + } + + /* Initialize Cxbx File Paths */ + CxbxInitFilePaths(); + + /*! initialize shared memory */ + if (!EmuShared::Init()) { + MessageBox(NULL, "Could not map shared memory!", "Cxbx-Reloaded", MB_OK); + return 1; + } + + LPSTR CommandLine = GetCommandLine(); + int argc; + PCHAR *argv = CommandLineToArgvA(CommandLine, &argc); + + CxbxKrnlMain(argc, argv); + + LocalFree(argv); + + /*! cleanup shared memory */ + EmuShared::Cleanup(); + + return 0; +} diff --git a/src/CxbxLoader/loader.cpp b/src/CxbxLoader/loader.cpp index d20cf8105..93b470681 100644 --- a/src/CxbxLoader/loader.cpp +++ b/src/CxbxLoader/loader.cpp @@ -87,6 +87,32 @@ unsigned char virtual_memory_placeholder[VM_PLACEHOLDER_SIZE] = { 0 }; // = { OP // /SUBSYSTEM:CONSOLE // /ENTRY:"rawMain" + +void OutputMessage(const char *msg) +{ + if (!msg) { + return; + } + + OutputDebugStringA(msg); // Send message to debugger output too + + HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD nNumberOfCharsToWrite = 0; + while (msg[nNumberOfCharsToWrite]) nNumberOfCharsToWrite++; // poor-man's strlen() + + // Detect output-redirection to a file + DWORD ConsoleMode; + if (!GetConsoleMode(hConsoleOutput, &ConsoleMode)) { + // Note : assume the output file accepts ANSI encoded characters + DWORD NumberOfBytesWritten; + WriteFile(hConsoleOutput, (const void *)msg, nNumberOfCharsToWrite, &NumberOfBytesWritten, /*lpOverlapped=*/NULL); + } else { + // Write message to console output + DWORD NumberOfCharsWritten; + WriteConsoleA(hConsoleOutput, (const void *)msg, nNumberOfCharsToWrite, &NumberOfCharsWritten, /*lpReserved=*/NULL); + } +} + DWORD CALLBACK rawMain() { (void)virtual_memory_placeholder; // prevent optimization removing this data @@ -99,7 +125,7 @@ DWORD CALLBACK rawMain() } if (!bIsWow64Process) { - OutputDebugString("Not running as a WOW64 process!"); + OutputMessage("Not running as a WOW64 process!\n"); return ERROR_BAD_ENVIRONMENT; } @@ -108,18 +134,18 @@ DWORD CALLBACK rawMain() GetSystemInfo(&SystemInfo); if (SystemInfo.dwAllocationGranularity != BLOCK_SIZE) { - OutputDebugString("Unsupported system allocation granularity!"); + OutputMessage("Unsupported system allocation granularity!\n"); return ERROR_BAD_ENVIRONMENT; } if (SystemInfo.dwPageSize != PAGE_SIZE) { - OutputDebugString("Unsupported system page size!"); + OutputMessage("Unsupported system page size!\n"); return ERROR_BAD_ENVIRONMENT; } if (SystemInfo.lpMaximumApplicationAddress < (void*)0xFFFEFFFF) { // Note : If this fails, the large-address-aware linker flag must be restored - OutputDebugString("Maximum applocation address too low!"); + OutputMessage("Maximum application address too low!\n"); return ERROR_BAD_ENVIRONMENT; } @@ -138,16 +164,17 @@ DWORD CALLBACK rawMain() if (!ReserveAddressRanges(system)) { // If we get here, emulation lacks important address ranges; Don't launch - OutputDebugString("Required address range couldn't be reserved!"); + OutputMessage("Required address range couldn't be reserved!\n"); return ERROR_NOT_ENOUGH_MEMORY; } // Only after the required memory ranges are reserved, load our emulation DLL HMODULE hEmulationDLL = LoadLibraryA("CxbxEmulator.dll"); if (!hEmulationDLL) { + OutputMessage("Error loading CxbxEmulator.dll\n"); LPTSTR Error = GetLastErrorString(); if (Error) { - OutputDebugString(Error); + OutputMessage(Error); FreeLastErrorString(Error); } @@ -158,7 +185,7 @@ DWORD CALLBACK rawMain() typedef void (WINAPI *Emulate_t)(); Emulate_t pfnEmulate = (Emulate_t)GetProcAddress(hEmulationDLL, "Emulate"); if (!pfnEmulate) { - OutputDebugString("Entrypoint not found!"); + OutputMessage("Entrypoint not found!\n"); return ERROR_RESOURCE_NOT_FOUND; } diff --git a/src/common/VerifyAddressRanges.cpp b/src/common/VerifyAddressRanges.cpp index 05d390fce..90485931f 100644 --- a/src/common/VerifyAddressRanges.cpp +++ b/src/common/VerifyAddressRanges.cpp @@ -43,6 +43,7 @@ bool VerifyAddressRange(int index) if (BaseAddress == 0) { // The zero page (the entire first 64 KB block) can't be verified + // so to avoid verification failures, we just skip it, knowing it'll be alright BaseAddress += BLOCK_SIZE; Size -= BLOCK_SIZE; } @@ -62,6 +63,9 @@ bool VerifyAddressRange(int index) // Allowed deviations if (XboxAddressRanges[index].Start == 0) { AllocationBase = (PVOID)0x10000; + // TODO : Either update below to current section layout, or reduce the number of sections + // (by merging them, preferrably into a single section if possible). + // (Note, that merging sections would make the loader smaller, so that's preferrable.) switch (BaseAddress) { case 0x10000: // Image Header RegionSize = PAGE_SIZE;