mirror of
https://github.com/advancingdragon/Dirtbox.git
synced 2024-05-16 20:00:19 -04:00
First commit.
This commit is contained in:
commit
fe3f60cf00
17
.gitignore
vendored
Normal file
17
.gitignore
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
# Vim temp files
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# Visual Studio temp files
|
||||
*.ncb
|
||||
*.suo
|
||||
*.user
|
||||
|
||||
# Mac stuff
|
||||
.DS_Store
|
||||
|
||||
# folders
|
||||
Debug/
|
||||
Release/
|
||||
|
42
AlignPosfix1.h
Normal file
42
AlignPosfix1.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
// ******************************************************************
|
||||
// *
|
||||
// * .,-::::: .,:: .::::::::. .,:: .:
|
||||
// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;;
|
||||
// * [[[ '[[,,[[' [[[__[[\. '[[,,[['
|
||||
// * $$$ Y$$$P $$""""Y$$ Y$$$P
|
||||
// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo,
|
||||
// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm,
|
||||
// *
|
||||
// * Cxbx->Win32->AlignPosfix1.h
|
||||
// *
|
||||
// * This file is part of the Cxbx project.
|
||||
// *
|
||||
// * Cxbx and Cxbe are free software; you can redistribute them
|
||||
// * and/or modify them 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) 2002-2003 Aaron Robinson <caustik@caustik.com>
|
||||
// *
|
||||
// * All rights reserved
|
||||
// *
|
||||
// ******************************************************************
|
||||
//#ifndef ALIGNPOSFIX1_H
|
||||
//#define ALIGNPOSFIX1_H
|
||||
|
||||
// ******************************************************************
|
||||
// * Turn off 1-byte structure alignment
|
||||
// ******************************************************************
|
||||
#pragma pack()
|
||||
|
||||
//#endif
|
47
AlignPrefix1.h
Normal file
47
AlignPrefix1.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
// ******************************************************************
|
||||
// *
|
||||
// * .,-::::: .,:: .::::::::. .,:: .:
|
||||
// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;;
|
||||
// * [[[ '[[,,[[' [[[__[[\. '[[,,[['
|
||||
// * $$$ Y$$$P $$""""Y$$ Y$$$P
|
||||
// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo,
|
||||
// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm,
|
||||
// *
|
||||
// * Cxbx->Win32->AlignPrefix1.h
|
||||
// *
|
||||
// * This file is part of the Cxbx project.
|
||||
// *
|
||||
// * Cxbx and Cxbe are free software; you can redistribute them
|
||||
// * and/or modify them 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) 2002-2003 Aaron Robinson <caustik@caustik.com>
|
||||
// *
|
||||
// * All rights reserved
|
||||
// *
|
||||
// ******************************************************************
|
||||
//#ifndef ALIGNPREFIX1_H
|
||||
//#define ALIGNPREFIX1_H
|
||||
|
||||
// ******************************************************************
|
||||
// * Turn on 1-byte structure alignment
|
||||
// ******************************************************************
|
||||
#pragma pack(1)
|
||||
|
||||
// ******************************************************************
|
||||
// * Tell the compiler not to complain about this *odd* circumstance
|
||||
// ******************************************************************
|
||||
#pragma warning(disable:4103)
|
||||
|
||||
//#endif
|
16
Dirtbox.cpp
Normal file
16
Dirtbox.cpp
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Dirtbox.cpp : Defines the entry point for the console application.
|
||||
//
|
||||
|
||||
#include "Types.h"
|
||||
#include "Xbe.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Xbe XbeFile("C:\\Projects\\Xbox\\XGraphicsTest\\Release\\XGraphicsTest.xbe");
|
||||
XbeFile.WriteExe("C:\\Projects\\Xbox\\DirtboxKernel\\Debug\\xbe.exe");
|
||||
return 0;
|
||||
}
|
||||
|
20
Dirtbox.sln
Normal file
20
Dirtbox.sln
Normal file
|
@ -0,0 +1,20 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 10.00
|
||||
# Visual Studio 2008
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Dirtbox", "Dirtbox.vcproj", "{68C83624-3B50-4D1E-8984-A1BC88812BC0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
Release|Win32 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{68C83624-3B50-4D1E-8984-A1BC88812BC0}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{68C83624-3B50-4D1E-8984-A1BC88812BC0}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{68C83624-3B50-4D1E-8984-A1BC88812BC0}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{68C83624-3B50-4D1E-8984-A1BC88812BC0}.Release|Win32.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
229
Dirtbox.vcproj
Normal file
229
Dirtbox.vcproj
Normal file
|
@ -0,0 +1,229 @@
|
|||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="9.00"
|
||||
Name="Dirtbox"
|
||||
ProjectGUID="{68C83624-3B50-4D1E-8984-A1BC88812BC0}"
|
||||
RootNamespace="Dirtbox"
|
||||
Keyword="Win32Proj"
|
||||
TargetFrameworkVersion="196613"
|
||||
>
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"
|
||||
/>
|
||||
</Platforms>
|
||||
<ToolFiles>
|
||||
</ToolFiles>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="3"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
LinkIncremental="1"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
BaseAddress="0x10000000"
|
||||
RandomizedBaseAddress="1"
|
||||
FixedBaseAddress="2"
|
||||
DataExecutionPrevention="1"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="2"
|
||||
EnableIntrinsicFunctions="true"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
|
||||
RuntimeLibrary="2"
|
||||
EnableFunctionLevelLinking="true"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
LinkIncremental="1"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
OptimizeReferences="2"
|
||||
EnableCOMDATFolding="2"
|
||||
BaseAddress="0x10000000"
|
||||
RandomizedBaseAddress="1"
|
||||
FixedBaseAddress="2"
|
||||
DataExecutionPrevention="1"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\Dirtbox.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Xbe.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
Filter="h;hpp;hxx;hm;inl;inc;xsd"
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\AlignPosfix1.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\AlignPrefix1.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\MicroExe.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Types.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Xbe.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Resource Files"
|
||||
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
|
||||
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
|
||||
>
|
||||
</Filter>
|
||||
<File
|
||||
RelativePath=".\ReadMe.txt"
|
||||
>
|
||||
</File>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
111
MicroExe.h
Normal file
111
MicroExe.h
Normal file
|
@ -0,0 +1,111 @@
|
|||
#ifndef MICROEXE_H
|
||||
#define MICROEXE_H
|
||||
|
||||
#include "Types.h"
|
||||
|
||||
#include "AlignPrefix1.h"
|
||||
struct MicroExeHeaders
|
||||
{
|
||||
// DOS Header
|
||||
struct DOSHeader
|
||||
{
|
||||
uint16 wMagic; // DOS .EXE magic number
|
||||
uint16 Unused;
|
||||
} m_DOSHeader;
|
||||
|
||||
// PE Header
|
||||
struct Header
|
||||
{
|
||||
uint32 dwMagic; // magic number [should be "PE\0\0"]
|
||||
uint16 wMachine; // machine type
|
||||
uint16 wNumberOfSections; // number of sections
|
||||
uint32 dwTimeDateStamp; // timedate stamp
|
||||
uint32 dwPointerToSymbolTable; // symbol table address
|
||||
uint32 dwNumberOfSymbols; // number of symbols
|
||||
uint16 wSizeOfOptionalHeader; // size of optional header
|
||||
uint16 wCharacteristics; // characteristics
|
||||
} m_Header;
|
||||
|
||||
// Optional Header
|
||||
struct OptionalHeader
|
||||
{
|
||||
uint16 wMagic; // magic number [should be 0x010B]
|
||||
uint08 bMajorLinkerVersion; // linker version [major]
|
||||
uint08 bMinorLinkerVersion; // linker version [minor]
|
||||
uint32 dwSizeOfCode; // size of code
|
||||
uint32 dwSizeOfInitializedData; // size of initialized data
|
||||
uint32 dwSizeOfUninitializedData; // size of uninitialized data
|
||||
uint32 dwAddressOfEntryPoint; // address of entry point
|
||||
uint32 dwBaseOfCode; // address of code base
|
||||
uint32 dwBaseOfData; // address of data base
|
||||
|
||||
// NT Additional Fields
|
||||
uint32 dwImageBase; // address of image base
|
||||
uint32 dwLfanew_SectionAlignment; // section alignment
|
||||
// (DOS Header) file address of new .EXE header
|
||||
uint32 dwFileAlignment; // file alignment
|
||||
uint16 wMajorOperatingSystemVersion; // operating system version [major]
|
||||
uint16 wMinorOperatingSystemVersion; // operating system version [minor]
|
||||
uint16 wMajorImageVersion; // image version [major]
|
||||
uint16 wMinorImageVersion; // image version [minor]
|
||||
uint16 wMajorSubsystemVersion; // subsystem version [major]
|
||||
uint16 wMinorSubsystemVersion; // subsystem version [minor]
|
||||
uint32 dwWin32VersionValue; // win32 version
|
||||
uint32 dwSizeOfImage; // size of image
|
||||
uint32 dwSizeOfHeaders; // size of headers
|
||||
uint32 dwCheckSum; // checksum
|
||||
uint16 wSubsystem; // subsystem
|
||||
uint16 wDllCharacteristics; // dll characteristics
|
||||
uint32 dwSizeOfStackReserve; // size of stack reserve
|
||||
uint32 dwSizeOfStackCommit; // size of stack commit
|
||||
uint32 dwSizeOfHeapReserve; // size of heap reserve
|
||||
uint32 dwSizeOfHeapCommit; // size of heap commit
|
||||
uint32 dwLoaderFlags; // loader flags
|
||||
uint32 dwNumberOfRvaAndSizes; // data directories
|
||||
struct ImageDataDirectory
|
||||
{
|
||||
uint32 dwVirtualAddress;
|
||||
uint32 dwSize;
|
||||
} astDataDirectory[4];
|
||||
} m_OptionalHeader;
|
||||
|
||||
struct SectionHeader
|
||||
{
|
||||
uint08 szName[8];
|
||||
uint32 dwVirtualSize;
|
||||
uint32 dwVirtualAddress;
|
||||
uint32 dwSizeOfRawData;
|
||||
uint32 dwPointerToRawData;
|
||||
uint32 dwPointerToRelocations;
|
||||
uint32 dwPointerToLinenumbers;
|
||||
uint16 wNumberOfRelocations;
|
||||
uint16 wNumberOfLinenumbers;
|
||||
uint32 dwCharacteristics;
|
||||
} m_SectionHeader;
|
||||
|
||||
struct ImageImportDescriptor
|
||||
{
|
||||
uint32 dwOriginalFirstThunk; // address of import lookup table
|
||||
uint32 dwTimeDateStamp; // time date stamp
|
||||
uint32 dwForwarderChain; // forwarder chain, -1 if no forwarders
|
||||
uint32 dwName; // address of DLL name string
|
||||
uint32 dwFirstThunk; // address of import address table
|
||||
} m_ImageImportDescriptor[2];
|
||||
|
||||
uint32 m_ImportAddressTable[2];
|
||||
char m_ImportName[10];
|
||||
uint08 m_Trampoline[6];
|
||||
};
|
||||
#include "AlignPosfix1.h"
|
||||
|
||||
|
||||
const uint16 IMAGE_FILE_MACHINE_I386 = 0x014c; // Intel 386.
|
||||
|
||||
const uint16 IMAGE_SUBSYSTEM_UNKNOWN = 0;
|
||||
const uint16 IMAGE_SUBSYSTEM_NATIVE = 1;
|
||||
const uint16 IMAGE_SUBSYSTEM_WINDOWS_GUI = 2;
|
||||
const uint16 IMAGE_SUBSYSTEM_WINDOWS_CUI = 3;
|
||||
|
||||
const uint32 EXE_ALIGNMENT = 4;
|
||||
|
||||
#endif
|
29
Types.h
Normal file
29
Types.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef TYPES_H
|
||||
#define TYPES_H
|
||||
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
// ******************************************************************
|
||||
// * Caustik's favorite typedefs
|
||||
// ******************************************************************
|
||||
typedef signed int sint;
|
||||
typedef unsigned int uint;
|
||||
typedef char int08;
|
||||
typedef short int16;
|
||||
typedef long int32;
|
||||
typedef unsigned char uint08;
|
||||
typedef unsigned short uint16;
|
||||
typedef unsigned long uint32;
|
||||
typedef signed char sint08;
|
||||
typedef signed short sint16;
|
||||
typedef signed long sint32;
|
||||
|
||||
inline static uint32 RoundUp(uint32 dwValue, uint32 dwMult)
|
||||
{
|
||||
if(dwMult == 0)
|
||||
return dwValue;
|
||||
|
||||
return dwValue - (dwValue-1)%dwMult + (dwMult - 1);
|
||||
}
|
||||
|
||||
#endif
|
544
Xbe.cpp
Normal file
544
Xbe.cpp
Normal file
|
@ -0,0 +1,544 @@
|
|||
// ******************************************************************
|
||||
// *
|
||||
// * .,-::::: .,:: .::::::::. .,:: .:
|
||||
// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;;
|
||||
// * [[[ '[[,,[[' [[[__[[\. '[[,,[['
|
||||
// * $$$ Y$$$P $$""""Y$$ Y$$$P
|
||||
// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo,
|
||||
// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm,
|
||||
// *
|
||||
// * Cxbx->Core->Xbe.cpp
|
||||
// *
|
||||
// * This file is part of the Cxbx project.
|
||||
// *
|
||||
// * Cxbx and Cxbe are free software; you can redistribute them
|
||||
// * and/or modify them 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) 2002-2003 Aaron Robinson <caustik@caustik.com>
|
||||
// *
|
||||
// * All rights reserved
|
||||
// *
|
||||
// ******************************************************************
|
||||
#include "Xbe.h"
|
||||
#include "MicroExe.h"
|
||||
|
||||
#include <memory.h>
|
||||
#include <locale.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <windows.h>
|
||||
|
||||
#define XBE_ERROR(str, ...) do \
|
||||
{ \
|
||||
printf("\nError in Xbe::Xbe: " str "\n", __VA_ARGS__); \
|
||||
if (XbeFile != 0) \
|
||||
fclose(XbeFile); \
|
||||
exit(1); \
|
||||
} while(0)
|
||||
|
||||
#define XBE_WRITE_ERROR(str, ...) do \
|
||||
{ \
|
||||
printf("\nError in Xbe::WriteExe: " str "\n", __VA_ARGS__); \
|
||||
if (ExeBuffer != 0) \
|
||||
delete [] ExeBuffer; \
|
||||
return 1; \
|
||||
} while(0)
|
||||
|
||||
// ******************************************************************
|
||||
// * constructor
|
||||
// ******************************************************************
|
||||
Xbe::Xbe(const char *x_szFilename)
|
||||
{
|
||||
m_HeaderEx = 0;
|
||||
m_HeaderExSize = 0;
|
||||
m_SectionHeader = 0;
|
||||
m_szSectionName = 0;
|
||||
m_LibraryVersion = 0;
|
||||
m_KernelLibraryVersion = 0;
|
||||
m_XAPILibraryVersion = 0;
|
||||
m_TLS = 0;
|
||||
m_bzSection = 0;
|
||||
|
||||
printf("Xbe::Xbe: Opening Xbe file...");
|
||||
|
||||
FILE *XbeFile = fopen(x_szFilename, "rb");
|
||||
if(XbeFile == 0)
|
||||
XBE_ERROR("Could not open Xbe file.");
|
||||
|
||||
printf("OK\n");
|
||||
|
||||
// ******************************************************************
|
||||
// * remember xbe path
|
||||
// ******************************************************************
|
||||
{
|
||||
printf("Xbe::Xbe Storing Xbe Path...");
|
||||
|
||||
strcpy(m_szPath, x_szFilename);
|
||||
int v=0, c=0;
|
||||
while(m_szPath[v] != '\0')
|
||||
{
|
||||
if(m_szPath[v] == '\\')
|
||||
c = v+1;
|
||||
v++;
|
||||
}
|
||||
m_szPath[c] = '\0';
|
||||
}
|
||||
|
||||
printf("OK\n");
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe image header
|
||||
// ******************************************************************
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Image Header...");
|
||||
|
||||
if(fread(&m_Header, sizeof(m_Header), 1, XbeFile) != 1)
|
||||
XBE_ERROR("Unexpected end of file while reading Xbe Image Header");
|
||||
if(m_Header.dwMagic != *(uint32 *)"XBEH")
|
||||
XBE_ERROR("Invalid magic number in Xbe file");
|
||||
|
||||
printf("OK\n");
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe image header extra bytes
|
||||
// ******************************************************************
|
||||
if(m_Header.dwSizeOfHeaders > sizeof(m_Header))
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Image Header Extra Bytes...");
|
||||
|
||||
uint32 m_HeaderExSize = RoundUp(m_Header.dwSizeOfHeaders, PAGE_SIZE) - sizeof(m_Header);
|
||||
|
||||
m_HeaderEx = new char[m_HeaderExSize];
|
||||
|
||||
if(fread(m_HeaderEx, m_HeaderExSize, 1, XbeFile) != 1)
|
||||
XBE_ERROR("Unexpected end of file while reading Xbe Image Header (Ex)");
|
||||
|
||||
printf("OK\n");
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe certificate
|
||||
// ******************************************************************
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Certificate...");
|
||||
|
||||
fseek(XbeFile, m_Header.dwCertificateAddr - m_Header.dwBaseAddr, SEEK_SET);
|
||||
if(fread(&m_Certificate, sizeof(m_Certificate), 1, XbeFile) != 1)
|
||||
XBE_ERROR("Unexpected end of file while reading Xbe Certificate");
|
||||
|
||||
setlocale(LC_ALL, "English");
|
||||
wcstombs(m_szAsciiTitle, m_Certificate.wszTitleName, 40);
|
||||
|
||||
printf("OK\n");
|
||||
|
||||
printf("Xbe::Xbe: Title identified as %s\n", m_szAsciiTitle);
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe section headers
|
||||
// ******************************************************************
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Section Headers...\n");
|
||||
|
||||
fseek(XbeFile, m_Header.dwSectionHeadersAddr - m_Header.dwBaseAddr, SEEK_SET);
|
||||
|
||||
m_SectionHeader = new SectionHeader[m_Header.dwSections];
|
||||
for(uint32 v=0; v<m_Header.dwSections; v++)
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Section Header 0x%.04X...", v);
|
||||
|
||||
if(fread(&m_SectionHeader[v], sizeof(*m_SectionHeader), 1, XbeFile) != 1)
|
||||
XBE_ERROR("Unexpected end of file while reading Xbe Section Header %d (%Xh)", v, v);
|
||||
|
||||
printf("OK\n");
|
||||
}
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe section names
|
||||
// ******************************************************************
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Section Names...\n");
|
||||
|
||||
m_szSectionName = new char[m_Header.dwSections][9];
|
||||
for(uint32 v=0; v<m_Header.dwSections; v++)
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Section Name 0x%.04X...", v);
|
||||
|
||||
uint08 *sn = GetAddr(m_SectionHeader[v].dwSectionNameAddr);
|
||||
|
||||
memset(m_szSectionName[v], 0, 9);
|
||||
|
||||
if(sn != 0)
|
||||
{
|
||||
for(int b=0; b<8; b++)
|
||||
{
|
||||
m_szSectionName[v][b] = sn[b];
|
||||
if(m_szSectionName[v][b] == '\0')
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("OK (%s)\n", m_szSectionName[v]);
|
||||
}
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe library versions
|
||||
// ******************************************************************
|
||||
if(m_Header.dwLibraryVersionsAddr != 0)
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Library Versions...\n");
|
||||
|
||||
fseek(XbeFile, m_Header.dwLibraryVersionsAddr - m_Header.dwBaseAddr, SEEK_SET);
|
||||
|
||||
m_LibraryVersion = new LibraryVersion[m_Header.dwLibraryVersions];
|
||||
for(uint32 v=0; v<m_Header.dwLibraryVersions; v++)
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Library Version 0x%.04X...", v);
|
||||
|
||||
if(fread(&m_LibraryVersion[v], sizeof(*m_LibraryVersion), 1, XbeFile) != 1)
|
||||
XBE_ERROR("Unexpected end of file while reading Xbe Library Version %d (%Xh)", v, v);
|
||||
|
||||
printf("OK\n");
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe kernel library version
|
||||
// ******************************************************************
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Kernel Library Version...");
|
||||
|
||||
if(m_Header.dwKernelLibraryVersionAddr == 0)
|
||||
XBE_ERROR("Could not locate kernel library version");
|
||||
|
||||
fseek(XbeFile, m_Header.dwKernelLibraryVersionAddr - m_Header.dwBaseAddr, SEEK_SET);
|
||||
|
||||
m_KernelLibraryVersion = new LibraryVersion;
|
||||
if(fread(m_KernelLibraryVersion, sizeof(*m_LibraryVersion), 1, XbeFile) != 1)
|
||||
XBE_ERROR("Unexpected end of file while reading Xbe Kernel Version");
|
||||
|
||||
printf("OK\n");
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe xapi library version
|
||||
// ******************************************************************
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Xapi Library Version...");
|
||||
|
||||
if(m_Header.dwXAPILibraryVersionAddr == 0)
|
||||
XBE_ERROR("Could not locate Xapi Library Version", true);
|
||||
|
||||
fseek(XbeFile, m_Header.dwXAPILibraryVersionAddr - m_Header.dwBaseAddr, SEEK_SET);
|
||||
|
||||
m_XAPILibraryVersion = new LibraryVersion;
|
||||
if(fread(m_XAPILibraryVersion, sizeof(*m_LibraryVersion), 1, XbeFile) != 1)
|
||||
XBE_ERROR("Unexpected end of file while reading Xbe Xapi Version", true);
|
||||
|
||||
printf("OK\n");
|
||||
}
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe sections
|
||||
// ******************************************************************
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Sections...\n");
|
||||
|
||||
m_bzSection = new uint08*[m_Header.dwSections];
|
||||
|
||||
memset(m_bzSection, 0, m_Header.dwSections);
|
||||
|
||||
for(uint32 v=0; v<m_Header.dwSections; v++)
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Section 0x%.04X...", v);
|
||||
|
||||
uint32 RawSize = m_SectionHeader[v].dwSizeOfRaw;
|
||||
uint32 RawAddr = m_SectionHeader[v].dwRawAddr;
|
||||
|
||||
m_bzSection[v] = new uint08[RawSize];
|
||||
|
||||
fseek(XbeFile, RawAddr, SEEK_SET);
|
||||
|
||||
if(RawSize == 0)
|
||||
{
|
||||
printf("OK\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if(fread(m_bzSection[v], RawSize, 1, XbeFile) != 1)
|
||||
XBE_ERROR("Unexpected end of file while reading Xbe Section %d (%Xh) (%s)", v, v, m_szSectionName[v]);
|
||||
|
||||
printf("OK\n");
|
||||
}
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * read xbe thread local storage
|
||||
// ******************************************************************
|
||||
if(m_Header.dwTLSAddr != 0)
|
||||
{
|
||||
printf("Xbe::Xbe: Reading Thread Local Storage...");
|
||||
|
||||
void *Addr = GetAddr(m_Header.dwTLSAddr);
|
||||
if(Addr == 0)
|
||||
XBE_ERROR("Could not locate Thread Local Storage");
|
||||
|
||||
m_TLS = new TLS;
|
||||
memcpy(m_TLS, Addr, sizeof(*m_TLS));
|
||||
|
||||
printf("OK\n");
|
||||
}
|
||||
|
||||
fclose(XbeFile);
|
||||
return;
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * deconstructor
|
||||
// ******************************************************************
|
||||
Xbe::~Xbe()
|
||||
{
|
||||
if(m_bzSection != 0)
|
||||
{
|
||||
for(uint32 v=0; v<m_Header.dwSections; v++)
|
||||
delete[] m_bzSection[v];
|
||||
|
||||
delete[] m_bzSection;
|
||||
}
|
||||
|
||||
delete m_XAPILibraryVersion;
|
||||
delete m_KernelLibraryVersion;
|
||||
delete[] m_LibraryVersion;
|
||||
delete m_TLS;
|
||||
delete[] m_szSectionName;
|
||||
delete[] m_SectionHeader;
|
||||
delete[] m_HeaderEx;
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * Writer
|
||||
// ******************************************************************
|
||||
int32 Xbe::WriteExe(const char *x_szFilename)
|
||||
{
|
||||
printf("Xbe::WriteExe Converting Xbe to Exe...");
|
||||
|
||||
// ******************************************************************
|
||||
// * create buffer for "as-loaded" XBE/EXE hybrid
|
||||
// ******************************************************************
|
||||
uint08 *ExeBuffer = new uint08[m_Header.dwSizeOfImage];
|
||||
if (ExeBuffer == 0)
|
||||
XBE_WRITE_ERROR("Cannot allocate buffer for Exe");
|
||||
|
||||
memset(ExeBuffer, 0, m_Header.dwSizeOfImage);
|
||||
|
||||
// ******************************************************************
|
||||
// * write xbe section headers
|
||||
// ******************************************************************
|
||||
memcpy(ExeBuffer + 0, &m_Header, sizeof(Xbe::Header));
|
||||
memcpy(ExeBuffer + m_Header.dwSizeOfImageHeader, m_HeaderEx, m_HeaderExSize);
|
||||
|
||||
// ******************************************************************
|
||||
// * write xbe sections
|
||||
// ******************************************************************
|
||||
for (uint32 v=0; v<m_Header.dwSections; v++)
|
||||
{
|
||||
uint32 offs = m_SectionHeader[v].dwVirtualAddr - m_Header.dwBaseAddr;
|
||||
memcpy(ExeBuffer + offs, m_bzSection[v], m_SectionHeader[v].dwSizeOfRaw);
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * patch digital signature with PE stub
|
||||
// ******************************************************************
|
||||
MicroExeHeaders ExeHeaders;
|
||||
|
||||
ExeHeaders.m_DOSHeader.wMagic = *(uint16 *)"MZ";
|
||||
ExeHeaders.m_DOSHeader.Unused = 0;
|
||||
|
||||
ExeHeaders.m_Header.dwMagic = *(uint32 *)"PE\0\0";
|
||||
ExeHeaders.m_Header.wMachine = IMAGE_FILE_MACHINE_I386;
|
||||
ExeHeaders.m_Header.wNumberOfSections = 1;
|
||||
ExeHeaders.m_Header.dwTimeDateStamp = m_Header.dwTimeDate;
|
||||
ExeHeaders.m_Header.dwPointerToSymbolTable = 0;
|
||||
ExeHeaders.m_Header.dwNumberOfSymbols = 0;
|
||||
ExeHeaders.m_Header.wSizeOfOptionalHeader = sizeof(MicroExeHeaders::OptionalHeader);
|
||||
ExeHeaders.m_Header.wCharacteristics = 0x0103;
|
||||
|
||||
ExeHeaders.m_OptionalHeader.wMagic = 0x010B;
|
||||
ExeHeaders.m_OptionalHeader.bMajorLinkerVersion = 0x06;
|
||||
ExeHeaders.m_OptionalHeader.bMinorLinkerVersion = 0x00;
|
||||
ExeHeaders.m_OptionalHeader.dwSizeOfCode = 0;
|
||||
ExeHeaders.m_OptionalHeader.dwSizeOfInitializedData = 0;
|
||||
ExeHeaders.m_OptionalHeader.dwSizeOfUninitializedData = 0;
|
||||
ExeHeaders.m_OptionalHeader.dwAddressOfEntryPoint = (uint32)&ExeHeaders.m_Trampoline - (uint32)&ExeHeaders;
|
||||
ExeHeaders.m_OptionalHeader.dwBaseOfCode = 0;
|
||||
ExeHeaders.m_OptionalHeader.dwBaseOfData = 0;
|
||||
|
||||
ExeHeaders.m_OptionalHeader.dwImageBase = m_Header.dwBaseAddr;
|
||||
ExeHeaders.m_OptionalHeader.dwLfanew_SectionAlignment = EXE_ALIGNMENT;
|
||||
ExeHeaders.m_OptionalHeader.dwFileAlignment = EXE_ALIGNMENT;
|
||||
ExeHeaders.m_OptionalHeader.wMajorOperatingSystemVersion = 4;
|
||||
ExeHeaders.m_OptionalHeader.wMinorOperatingSystemVersion = 0;
|
||||
ExeHeaders.m_OptionalHeader.wMajorImageVersion = 0;
|
||||
ExeHeaders.m_OptionalHeader.wMinorImageVersion = 0;
|
||||
ExeHeaders.m_OptionalHeader.wMajorSubsystemVersion = 4;
|
||||
ExeHeaders.m_OptionalHeader.wMinorSubsystemVersion = 0;
|
||||
ExeHeaders.m_OptionalHeader.dwWin32VersionValue = 0;
|
||||
ExeHeaders.m_OptionalHeader.dwSizeOfImage = m_Header.dwSizeOfImage; // already aligned at 0x20
|
||||
ExeHeaders.m_OptionalHeader.dwSizeOfHeaders = RoundUp(sizeof(MicroExeHeaders), EXE_ALIGNMENT);
|
||||
ExeHeaders.m_OptionalHeader.dwCheckSum = 0;
|
||||
ExeHeaders.m_OptionalHeader.wSubsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
|
||||
ExeHeaders.m_OptionalHeader.wDllCharacteristics = 0x400;
|
||||
ExeHeaders.m_OptionalHeader.dwSizeOfStackReserve = 0x100000;
|
||||
ExeHeaders.m_OptionalHeader.dwSizeOfStackCommit = 0x1000;
|
||||
ExeHeaders.m_OptionalHeader.dwSizeOfHeapReserve = 0x100000;
|
||||
ExeHeaders.m_OptionalHeader.dwSizeOfHeapCommit = 0x1000;
|
||||
ExeHeaders.m_OptionalHeader.dwLoaderFlags = 0;
|
||||
ExeHeaders.m_OptionalHeader.dwNumberOfRvaAndSizes = 4;
|
||||
|
||||
// ******************************************************************
|
||||
// * the other directories
|
||||
// ******************************************************************
|
||||
for (uint32 v=0; v<4; v++)
|
||||
{
|
||||
ExeHeaders.m_OptionalHeader.astDataDirectory[v].dwVirtualAddress = 0;
|
||||
ExeHeaders.m_OptionalHeader.astDataDirectory[v].dwSize = 0;
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * import directory
|
||||
// ******************************************************************
|
||||
uint32 offs = (uint32)&ExeHeaders.m_ImageImportDescriptor - (uint32)&ExeHeaders;
|
||||
|
||||
ExeHeaders.m_OptionalHeader.astDataDirectory[1].dwVirtualAddress = offs;
|
||||
ExeHeaders.m_OptionalHeader.astDataDirectory[1].dwSize = 2*sizeof(MicroExeHeaders::ImageImportDescriptor);
|
||||
|
||||
// ******************************************************************
|
||||
// * the one and only section header
|
||||
// ******************************************************************
|
||||
uint32 XbeSectionAddress = 0; // the whole image
|
||||
uint32 XbeSectionSize = m_Header.dwSizeOfImage - XbeSectionAddress; // already aligned at 0x20
|
||||
|
||||
strncpy((char *)ExeHeaders.m_SectionHeader.szName, "loldongs", 8);
|
||||
ExeHeaders.m_SectionHeader.dwVirtualSize = XbeSectionSize;
|
||||
ExeHeaders.m_SectionHeader.dwVirtualAddress = XbeSectionAddress;
|
||||
ExeHeaders.m_SectionHeader.dwSizeOfRawData = XbeSectionSize;
|
||||
ExeHeaders.m_SectionHeader.dwPointerToRawData = XbeSectionAddress;
|
||||
ExeHeaders.m_SectionHeader.dwPointerToRelocations = 0;
|
||||
ExeHeaders.m_SectionHeader.dwPointerToLinenumbers = 0;
|
||||
ExeHeaders.m_SectionHeader.wNumberOfRelocations = 0;
|
||||
ExeHeaders.m_SectionHeader.wNumberOfLinenumbers = 0;
|
||||
ExeHeaders.m_SectionHeader.dwCharacteristics = 0x60000020;
|
||||
|
||||
// ******************************************************************
|
||||
// * image import descriptor, only one DLL
|
||||
// ******************************************************************
|
||||
uint32 offsIat = (uint32)&ExeHeaders.m_ImportAddressTable - (uint32)&ExeHeaders;
|
||||
uint32 offsName = (uint32)&ExeHeaders.m_ImportName - (uint32)&ExeHeaders;
|
||||
|
||||
ExeHeaders.m_ImageImportDescriptor[0].dwOriginalFirstThunk = offsIat;
|
||||
ExeHeaders.m_ImageImportDescriptor[0].dwTimeDateStamp = 0;
|
||||
ExeHeaders.m_ImageImportDescriptor[0].dwForwarderChain = 0;
|
||||
ExeHeaders.m_ImageImportDescriptor[0].dwName = offsName;
|
||||
ExeHeaders.m_ImageImportDescriptor[0].dwFirstThunk = offsIat;
|
||||
|
||||
ExeHeaders.m_ImageImportDescriptor[1].dwOriginalFirstThunk = 0;
|
||||
ExeHeaders.m_ImageImportDescriptor[1].dwTimeDateStamp = 0;
|
||||
ExeHeaders.m_ImageImportDescriptor[1].dwForwarderChain = 0;
|
||||
ExeHeaders.m_ImageImportDescriptor[1].dwName = 0;
|
||||
ExeHeaders.m_ImageImportDescriptor[1].dwFirstThunk = 0;
|
||||
|
||||
// ******************************************************************
|
||||
// * import address table, import by ordinal
|
||||
// ******************************************************************
|
||||
ExeHeaders.m_ImportAddressTable[0] = 0x80000001;
|
||||
ExeHeaders.m_ImportAddressTable[1] = 0;
|
||||
|
||||
// ******************************************************************
|
||||
// * imported DLL name
|
||||
// ******************************************************************
|
||||
strncpy(ExeHeaders.m_ImportName, "DirtboxKe", 10);
|
||||
|
||||
// ******************************************************************
|
||||
// * trampoline that calls the Dirtbox loader in the DLL
|
||||
// ******************************************************************
|
||||
ExeHeaders.m_Trampoline[0] = 0xFF; // JMP near absolute indirect
|
||||
ExeHeaders.m_Trampoline[1] = 0x25; // memory
|
||||
*(uint32 *)&ExeHeaders.m_Trampoline[2] = 0x000100EC;
|
||||
|
||||
// ******************************************************************
|
||||
// * replaces the magic and digital signature with PE headers
|
||||
// ******************************************************************
|
||||
memcpy(ExeBuffer + 0, &ExeHeaders, sizeof(MicroExeHeaders));
|
||||
|
||||
// ******************************************************************
|
||||
// * write the created buffer into EXE file
|
||||
// ******************************************************************
|
||||
FILE *ExeFile = fopen(x_szFilename, "wb");
|
||||
if (ExeFile == 0)
|
||||
XBE_WRITE_ERROR("Could not open Exe file");
|
||||
|
||||
fwrite(ExeBuffer, m_Header.dwSizeOfImage, 1, ExeFile);
|
||||
fclose(ExeFile);
|
||||
|
||||
// ******************************************************************
|
||||
// * free buffer for Exe file
|
||||
// ******************************************************************
|
||||
delete [] ExeBuffer;
|
||||
|
||||
printf("OK\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ******************************************************************
|
||||
// * GetAddr
|
||||
// ******************************************************************
|
||||
uint08 *Xbe::GetAddr(uint32 x_dwVirtualAddress)
|
||||
{
|
||||
uint32 offs = x_dwVirtualAddress - m_Header.dwBaseAddr;
|
||||
|
||||
// ******************************************************************
|
||||
// * offset in image header
|
||||
// ******************************************************************
|
||||
if(offs < sizeof(m_Header))
|
||||
return &((uint08*)&m_Header)[offs];
|
||||
|
||||
// ******************************************************************
|
||||
// * offset in image header extra bytes
|
||||
// ******************************************************************
|
||||
if(offs < m_Header.dwSizeOfHeaders)
|
||||
return (uint08*)&m_HeaderEx[offs - sizeof(m_Header)];
|
||||
|
||||
// ******************************************************************
|
||||
// * offset in some random section
|
||||
// ******************************************************************
|
||||
{
|
||||
for(uint32 v=0; v<m_Header.dwSections; v++)
|
||||
{
|
||||
uint32 VirtAddr = m_SectionHeader[v].dwVirtualAddr;
|
||||
uint32 VirtSize = m_SectionHeader[v].dwVirtualSize;
|
||||
|
||||
if( (x_dwVirtualAddress >= VirtAddr) && (x_dwVirtualAddress < (VirtAddr + VirtSize)) )
|
||||
return &m_bzSection[v][x_dwVirtualAddress - VirtAddr];
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
298
Xbe.h
Normal file
298
Xbe.h
Normal file
|
@ -0,0 +1,298 @@
|
|||
// ******************************************************************
|
||||
// *
|
||||
// * .,-::::: .,:: .::::::::. .,:: .:
|
||||
// * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;;
|
||||
// * [[[ '[[,,[[' [[[__[[\. '[[,,[['
|
||||
// * $$$ Y$$$P $$""""Y$$ Y$$$P
|
||||
// * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo,
|
||||
// * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm,
|
||||
// *
|
||||
// * Cxbx->Core->Xbe.h
|
||||
// *
|
||||
// * This file is part of the Cxbx project.
|
||||
// *
|
||||
// * Cxbx and Cxbe are free software; you can redistribute them
|
||||
// * and/or modify them 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) 2002-2003 Aaron Robinson <caustik@caustik.com>
|
||||
// *
|
||||
// * All rights reserved
|
||||
// *
|
||||
// ******************************************************************
|
||||
#ifndef XBE_H
|
||||
#define XBE_H
|
||||
|
||||
#include "Types.h"
|
||||
|
||||
// ******************************************************************
|
||||
// * Xbe (Xbox Executable) file object
|
||||
// ******************************************************************
|
||||
class Xbe
|
||||
{
|
||||
public:
|
||||
// ******************************************************************
|
||||
// * Construct via Xbe file
|
||||
// ******************************************************************
|
||||
Xbe(const char *x_szFilename);
|
||||
|
||||
// ******************************************************************
|
||||
// * Deconstructor
|
||||
// ******************************************************************
|
||||
~Xbe();
|
||||
|
||||
// ******************************************************************
|
||||
// * Writer
|
||||
// ******************************************************************
|
||||
int32 WriteExe(const char *x_szFilename);
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE header
|
||||
// ******************************************************************
|
||||
#include "AlignPrefix1.h"
|
||||
struct Header
|
||||
{
|
||||
uint32 dwMagic; // 0x0000 - magic number [should be "XBEH"]
|
||||
uint08 pbDigitalSignature[256]; // 0x0004 - digital signature
|
||||
uint32 dwBaseAddr; // 0x0104 - base address
|
||||
uint32 dwSizeOfHeaders; // 0x0108 - size of headers
|
||||
uint32 dwSizeOfImage; // 0x010C - size of image
|
||||
uint32 dwSizeOfImageHeader; // 0x0110 - size of image header
|
||||
uint32 dwTimeDate; // 0x0114 - timedate stamp
|
||||
uint32 dwCertificateAddr; // 0x0118 - certificate address
|
||||
uint32 dwSections; // 0x011C - number of sections
|
||||
uint32 dwSectionHeadersAddr; // 0x0120 - section headers address
|
||||
|
||||
struct InitFlags // 0x0124 - initialization flags
|
||||
{
|
||||
uint32 bMountUtilityDrive : 1; // mount utility drive flag
|
||||
uint32 bFormatUtilityDrive : 1; // format utility drive flag
|
||||
uint32 bLimit64MB : 1; // limit development kit run time memory to 64mb flag
|
||||
uint32 bDontSetupHarddisk : 1; // don't setup hard disk flag
|
||||
uint32 Unused : 4; // unused (or unknown)
|
||||
uint32 Unused_b1 : 8; // unused (or unknown)
|
||||
uint32 Unused_b2 : 8; // unused (or unknown)
|
||||
uint32 Unused_b3 : 8; // unused (or unknown)
|
||||
}
|
||||
dwInitFlags;
|
||||
|
||||
uint32 dwEntryAddr; // 0x0128 - entry point address
|
||||
uint32 dwTLSAddr; // 0x012C - thread local storage directory address
|
||||
uint32 dwPeStackCommit; // 0x0130 - size of stack commit
|
||||
uint32 dwPeHeapReserve; // 0x0134 - size of heap reserve
|
||||
uint32 dwPeHeapCommit; // 0x0138 - size of heap commit
|
||||
uint32 dwPeBaseAddr; // 0x013C - original base address
|
||||
uint32 dwPeSizeOfImage; // 0x0140 - size of original image
|
||||
uint32 dwPeChecksum; // 0x0144 - original checksum
|
||||
uint32 dwPeTimeDate; // 0x0148 - original timedate stamp
|
||||
uint32 dwDebugPathnameAddr; // 0x014C - debug pathname address
|
||||
uint32 dwDebugFilenameAddr; // 0x0150 - debug filename address
|
||||
uint32 dwDebugUnicodeFilenameAddr; // 0x0154 - debug unicode filename address
|
||||
uint32 dwKernelImageThunkAddr; // 0x0158 - kernel image thunk address
|
||||
uint32 dwNonKernelImportDirAddr; // 0x015C - non kernel import directory address
|
||||
uint32 dwLibraryVersions; // 0x0160 - number of library versions
|
||||
uint32 dwLibraryVersionsAddr; // 0x0164 - library versions address
|
||||
uint32 dwKernelLibraryVersionAddr; // 0x0168 - kernel library version address
|
||||
uint32 dwXAPILibraryVersionAddr; // 0x016C - xapi library version address
|
||||
uint32 dwLogoBitmapAddr; // 0x0170 - logo bitmap address
|
||||
uint32 dwSizeOfLogoBitmap; // 0x0174 - logo bitmap size
|
||||
}
|
||||
#include "AlignPosfix1.h"
|
||||
m_Header;
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE header extra bytes (used to preserve unknown data)
|
||||
// ******************************************************************
|
||||
char *m_HeaderEx;
|
||||
uint32 m_HeaderExSize;
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE certificate
|
||||
// ******************************************************************
|
||||
#include "AlignPrefix1.h"
|
||||
struct Certificate
|
||||
{
|
||||
uint32 dwSize; // 0x0000 - size of certificate
|
||||
uint32 dwTimeDate; // 0x0004 - timedate stamp
|
||||
uint32 dwTitleId; // 0x0008 - title id
|
||||
wchar_t wszTitleName[40]; // 0x000C - title name (unicode)
|
||||
uint32 dwAlternateTitleId[0x10]; // 0x005C - alternate title ids
|
||||
uint32 dwAllowedMedia; // 0x009C - allowed media types
|
||||
uint32 dwGameRegion; // 0x00A0 - game region
|
||||
uint32 dwGameRatings; // 0x00A4 - game ratings
|
||||
uint32 dwDiskNumber; // 0x00A8 - disk number
|
||||
uint32 dwVersion; // 0x00AC - version
|
||||
uint08 bzLanKey[16]; // 0x00B0 - lan key
|
||||
uint08 bzSignatureKey[16]; // 0x00C0 - signature key
|
||||
uint08 bzTitleAlternateSignatureKey[16][16]; // 0x00D0 - alternate signature keys
|
||||
}
|
||||
#include "AlignPosfix1.h"
|
||||
m_Certificate;
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE section header
|
||||
// ******************************************************************
|
||||
#include "AlignPrefix1.h"
|
||||
struct SectionHeader
|
||||
{
|
||||
struct _Flags
|
||||
{
|
||||
uint32 bWritable : 1; // writable flag
|
||||
uint32 bPreload : 1; // preload flag
|
||||
uint32 bExecutable : 1; // executable flag
|
||||
uint32 bInsertedFile : 1; // inserted file flag
|
||||
uint32 bHeadPageRO : 1; // head page read only flag
|
||||
uint32 bTailPageRO : 1; // tail page read only flag
|
||||
uint32 Unused_a1 : 1; // unused (or unknown)
|
||||
uint32 Unused_a2 : 1; // unused (or unknown)
|
||||
uint32 Unused_b1 : 8; // unused (or unknown)
|
||||
uint32 Unused_b2 : 8; // unused (or unknown)
|
||||
uint32 Unused_b3 : 8; // unused (or unknown)
|
||||
}
|
||||
dwFlags;
|
||||
|
||||
uint32 dwVirtualAddr; // virtual address
|
||||
uint32 dwVirtualSize; // virtual size
|
||||
uint32 dwRawAddr; // file offset to raw data
|
||||
uint32 dwSizeOfRaw; // size of raw data
|
||||
uint32 dwSectionNameAddr; // section name addr
|
||||
uint32 dwSectionRefCount; // section reference count
|
||||
uint32 dwHeadSharedRefCountAddr; // head shared page reference count address
|
||||
uint32 dwTailSharedRefCountAddr; // tail shared page reference count address
|
||||
uint08 bzSectionDigest[20]; // section digest
|
||||
}
|
||||
#include "AlignPosfix1.h"
|
||||
*m_SectionHeader;
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE library versions
|
||||
// ******************************************************************
|
||||
#include "AlignPrefix1.h"
|
||||
struct LibraryVersion
|
||||
{
|
||||
char szName[8]; // library name
|
||||
uint16 wMajorVersion; // major version
|
||||
uint16 wMinorVersion; // minor version
|
||||
uint16 wBuildVersion; // build version
|
||||
|
||||
struct Flags
|
||||
{
|
||||
uint16 QFEVersion : 13; // QFE Version
|
||||
uint16 Approved : 2; // Approved? (0:no, 1:possibly, 2:yes)
|
||||
uint16 bDebugBuild : 1; // Is this a debug build?
|
||||
}
|
||||
dwFlags;
|
||||
}
|
||||
#include "AlignPosfix1.h"
|
||||
*m_LibraryVersion, *m_KernelLibraryVersion, *m_XAPILibraryVersion;
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE Thread Local Storage
|
||||
// ******************************************************************
|
||||
#include "AlignPrefix1.h"
|
||||
struct TLS
|
||||
{
|
||||
uint32 dwDataStartAddr; // raw start address
|
||||
uint32 dwDataEndAddr; // raw end address
|
||||
uint32 dwTLSIndexAddr; // tls index address
|
||||
uint32 dwTLSCallbackAddr; // tls callback address
|
||||
uint32 dwSizeOfZeroFill; // size of zero fill
|
||||
uint32 dwCharacteristics; // characteristics
|
||||
}
|
||||
#include "AlignPosfix1.h"
|
||||
*m_TLS;
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE section names, each 8 bytes max and null terminated
|
||||
// ******************************************************************
|
||||
char (*m_szSectionName)[9];
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE sections
|
||||
// ******************************************************************
|
||||
uint08 **m_bzSection;
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE original path
|
||||
// ******************************************************************
|
||||
char m_szPath[260];
|
||||
|
||||
// ******************************************************************
|
||||
// * XBE ascii title, translated from certificate title
|
||||
// ******************************************************************
|
||||
char m_szAsciiTitle[40];
|
||||
|
||||
// ******************************************************************
|
||||
// * GetTLSData
|
||||
// ******************************************************************
|
||||
uint08 *GetTLSData() { if(m_TLS == 0) return 0; else return GetAddr(m_TLS->dwDataStartAddr); }
|
||||
|
||||
// ******************************************************************
|
||||
// * GetTLSIndex
|
||||
// ******************************************************************
|
||||
uint32 *GetTLSIndex() { if(m_TLS == 0) return 0; else return (uint32*)GetAddr(m_TLS->dwTLSIndexAddr); }
|
||||
|
||||
private:
|
||||
// ******************************************************************
|
||||
// * return a modifiable pointer inside this structure that
|
||||
// * corresponds to a virtual address
|
||||
// ******************************************************************
|
||||
uint08 *GetAddr(uint32 x_dwVirtualAddress);
|
||||
};
|
||||
|
||||
// ******************************************************************
|
||||
// * Page size
|
||||
// ******************************************************************
|
||||
const uint32 PAGE_SIZE = 0x1000;
|
||||
|
||||
// ******************************************************************
|
||||
// * Debug / Retail XOR Keys
|
||||
// ******************************************************************
|
||||
const uint32 XOR_EP_DEBUG = 0x94859D4B; // Entry Point (Debug)
|
||||
const uint32 XOR_EP_RETAIL = 0xA8FC57AB; // Entry Point (Retail)
|
||||
const uint32 XOR_KT_DEBUG = 0xEFB1F152; // Kernel Thunk (Debug)
|
||||
const uint32 XOR_KT_RETAIL = 0x5B6D40B6; // Kernel Thunk (Retail)
|
||||
|
||||
// ******************************************************************
|
||||
// * Game region flags for XBE certificate
|
||||
// ******************************************************************
|
||||
const uint32 XBEIMAGE_GAME_REGION_NA = 0x00000001;
|
||||
const uint32 XBEIMAGE_GAME_REGION_JAPAN = 0x00000002;
|
||||
const uint32 XBEIMAGE_GAME_REGION_RESTOFWORLD = 0x00000004;
|
||||
const uint32 XBEIMAGE_GAME_REGION_MANUFACTURING = 0x80000000;
|
||||
|
||||
// ******************************************************************
|
||||
// * Media type flags for XBE certificate
|
||||
// ******************************************************************
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_HARD_DISK = 0x00000001;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_DVD_X2 = 0x00000002;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_DVD_CD = 0x00000004;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_CD = 0x00000008;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_DVD_5_RO = 0x00000010;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_DVD_9_RO = 0x00000020;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_DVD_5_RW = 0x00000040;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_DVD_9_RW = 0x00000080;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_DONGLE = 0x00000100;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_MEDIA_BOARD = 0x00000200;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_NONSECURE_HARD_DISK = 0x40000000;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_NONSECURE_MODE = 0x80000000;
|
||||
const uint32 XBEIMAGE_MEDIA_TYPE_MEDIA_MASK = 0x00FFFFFF;
|
||||
|
||||
// ******************************************************************
|
||||
// * OpenXDK logo bitmap (used by cxbe by default)
|
||||
// ******************************************************************
|
||||
extern uint08 OpenXDK[];
|
||||
extern uint32 dwSizeOfOpenXDK;
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue