mirror of
https://github.com/reactos/reactos.git
synced 2025-08-02 13:16:07 +00:00
backport the start of a pe loader,
backport allot of bug fix. like open file, reading file. My working copy have allot of hacks and is a big mess. This stuff is cleanup from it. svn path=/trunk/; revision=25433
This commit is contained in:
parent
a8dce67086
commit
2aab0c74eb
7 changed files with 445 additions and 325 deletions
|
@ -84,6 +84,8 @@ int main(int argc, char * argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
return LoadPFileImage(infile,outfile,BaseAddress,cpuid,type);
|
return LoadPFileImage(infile,outfile,BaseAddress,cpuid,type);
|
||||||
|
//return LoadPFileImage("e:\\cputointel.exe","e:\\cputointel.asm",0,0,0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
389
rosapps/devutils/cputointel/ImageLoader.c
Normal file
389
rosapps/devutils/cputointel/ImageLoader.c
Normal file
|
@ -0,0 +1,389 @@
|
||||||
|
#include <windows.h>
|
||||||
|
#include <winnt.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "misc.h"
|
||||||
|
#include "ARM/ARM.h"
|
||||||
|
#include "m68k/m68k.h"
|
||||||
|
#include "PPC/PPC.h"
|
||||||
|
|
||||||
|
CPU_INT LoadPFileImage( char *infileName, char *outputfileName,
|
||||||
|
CPU_UNINT BaseAddress, char *cpuid,
|
||||||
|
CPU_UNINT type)
|
||||||
|
{
|
||||||
|
FILE *infp;
|
||||||
|
FILE *outfp;
|
||||||
|
CPU_BYTE *cpu_buffer;
|
||||||
|
CPU_UNINT cpu_pos = 0;
|
||||||
|
CPU_UNINT cpu_size=0;
|
||||||
|
//fopen("testms.exe","RB");
|
||||||
|
|
||||||
|
|
||||||
|
/* Open file for read */
|
||||||
|
|
||||||
|
if (!(infp = fopen(infileName, "rb")))
|
||||||
|
{
|
||||||
|
printf("Can not open file %s\n",infileName);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open file for write */
|
||||||
|
if (!(outfp = fopen(outputfileName,"wb")))
|
||||||
|
{
|
||||||
|
printf("Can not open file %s\n",outputfileName);
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Load the binary file to a memory buffer */
|
||||||
|
fseek(infp,0,SEEK_END);
|
||||||
|
if (ferror(infp))
|
||||||
|
{
|
||||||
|
printf("error can not seek in the read file");
|
||||||
|
fclose(infp);
|
||||||
|
fclose(outfp);
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* get the memory size buffer */
|
||||||
|
cpu_size = ftell(infp);
|
||||||
|
if (ferror(infp))
|
||||||
|
{
|
||||||
|
printf("error can not get file size of the read file");
|
||||||
|
fclose(infp);
|
||||||
|
fclose(outfp);
|
||||||
|
return 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Load the binary file to a memory buffer */
|
||||||
|
fseek(infp,0,SEEK_SET);
|
||||||
|
if (ferror(infp))
|
||||||
|
{
|
||||||
|
printf("error can not seek in the read file");
|
||||||
|
fclose(infp);
|
||||||
|
fclose(outfp);
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cpu_size==0)
|
||||||
|
{
|
||||||
|
printf("error file size is Zero lenght of the read file");
|
||||||
|
fclose(infp);
|
||||||
|
fclose(outfp);
|
||||||
|
return 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* alloc memory now */
|
||||||
|
;
|
||||||
|
if (!(cpu_buffer = (unsigned char *) malloc(cpu_size+1)))
|
||||||
|
{
|
||||||
|
printf("error can not alloc %uld size for memory buffer",cpu_size);
|
||||||
|
fclose(infp);
|
||||||
|
fclose(outfp);
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
ZeroMemory(cpu_buffer,cpu_size);
|
||||||
|
|
||||||
|
/* read from the file now in one sweep */
|
||||||
|
fread((void *)cpu_buffer,1,cpu_size,infp);
|
||||||
|
if (ferror(infp))
|
||||||
|
{
|
||||||
|
printf("error can not read file ");
|
||||||
|
fclose(infp);
|
||||||
|
fclose(outfp);
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
fclose(infp);
|
||||||
|
|
||||||
|
if (type==0)
|
||||||
|
{
|
||||||
|
if ( PEFileStart(cpu_buffer, 0, BaseAddress, cpu_size, outfp) !=0)
|
||||||
|
{
|
||||||
|
type=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type== 1)
|
||||||
|
{
|
||||||
|
if (stricmp(cpuid,"m68000"))
|
||||||
|
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68000,outfp);
|
||||||
|
else if (stricmp(cpuid,"m68010"))
|
||||||
|
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68010,outfp);
|
||||||
|
else if (stricmp(cpuid,"m68020"))
|
||||||
|
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68020,outfp);
|
||||||
|
else if (stricmp(cpuid,"m68030"))
|
||||||
|
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68030,outfp);
|
||||||
|
else if (stricmp(cpuid,"m68040"))
|
||||||
|
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68040,outfp);
|
||||||
|
else if (stricmp(cpuid,"ppc"))
|
||||||
|
return PPCBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,0,outfp);
|
||||||
|
else if (stricmp(cpuid,"arm4"))
|
||||||
|
return ARMBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,4,outfp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type==2)
|
||||||
|
{
|
||||||
|
return PEFileStart(cpu_buffer, 0, BaseAddress, cpu_size, outfp);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
CPU_INT PEFileStart( CPU_BYTE *memory, CPU_UNINT pos,
|
||||||
|
CPU_UNINT base, CPU_UNINT size,
|
||||||
|
FILE *outfp)
|
||||||
|
{
|
||||||
|
//INT sizeofHeader = IMAGE_NT_SIGNATURE;
|
||||||
|
PIMAGE_DOS_HEADER DosHeader;
|
||||||
|
PIMAGE_NT_HEADERS NtHeader;
|
||||||
|
PIMAGE_SECTION_HEADER SectionHeader;
|
||||||
|
|
||||||
|
DosHeader = (PIMAGE_DOS_HEADER)memory;
|
||||||
|
if ( (DosHeader->e_magic != IMAGE_DOS_SIGNATURE) ||
|
||||||
|
(size < 0x3c+2) )
|
||||||
|
{
|
||||||
|
printf("No MZ file \n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
NtHeader = (PIMAGE_NT_HEADERS) (((ULONG)memory) + ((ULONG)DosHeader->e_lfanew));
|
||||||
|
if (NtHeader->Signature != IMAGE_NT_SIGNATURE)
|
||||||
|
{
|
||||||
|
printf("No PE header found \n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(NtHeader->FileHeader.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE))
|
||||||
|
{
|
||||||
|
printf("No execute image found \n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(NtHeader->OptionalHeader.Subsystem)
|
||||||
|
{
|
||||||
|
case IMAGE_SUBSYSTEM_EFI_APPLICATION:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_EFI_APPLICATION");
|
||||||
|
printf("This exe file is desgin run in EFI bios as applactions\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER");
|
||||||
|
printf("This exe file is desgin run in EFI bios as service driver\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_EFI_ROM:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_EFI_ROM");
|
||||||
|
printf("This exe file is EFI ROM\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER");
|
||||||
|
printf("This exe file is desgin run in EFI bios as driver\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_NATIVE:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_NATIVE");
|
||||||
|
printf("This exe file does not need any subsystem\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_NATIVE_WINDOWS");
|
||||||
|
printf("This exe file is desgin run on Windows 9x as driver \n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_OS2_CUI:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_OS2_CUI");
|
||||||
|
printf("This exe file is desgin run on OS2 as CUI\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_POSIX_CUI:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_POSIX_CUI");
|
||||||
|
printf("This exe file is desgin run on POSIX as CUI\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_WINDOWS_CE_GUI");
|
||||||
|
printf("This exe file is desgin run on Windows CE as GUI\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_WINDOWS_CUI:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_WINDOWS_CUI");
|
||||||
|
printf("This exe file is desgin run on Windows as CUI\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_WINDOWS_GUI:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_WINDOWS_GUI");
|
||||||
|
printf("This exe file is desgin run on Windows as GUI\n");
|
||||||
|
break;
|
||||||
|
case IMAGE_SUBSYSTEM_XBOX:
|
||||||
|
fprintf(outfp,"; OS type : IMAGE_SUBSYSTEM_XBOX");
|
||||||
|
printf("This exe file is desgin run on X-Box\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(outfp,"; OS type : Unknown");
|
||||||
|
printf("Unknown OS : SubID : %d\n",NtHeader->OptionalHeader.Subsystem);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
SectionHeader->Name == ".tls$"
|
||||||
|
SectionHeader->Name == ".tls"
|
||||||
|
SectionHeader->Name == ".text" // Executable code
|
||||||
|
SectionHeader->Name == ".sxdata"
|
||||||
|
SectionHeader->Name == ".sdata"
|
||||||
|
SectionHeader->Name == ".sbss"
|
||||||
|
SectionHeader->Name == ".rsrc" // rc data
|
||||||
|
SectionHeader->Name == ".reloc"
|
||||||
|
SectionHeader->Name == ".rdata" // read only initialized data
|
||||||
|
SectionHeader->Name == ".pdata"
|
||||||
|
SectionHeader->Name == ".idlsym"
|
||||||
|
SectionHeader->Name == ".idata" // Import tables
|
||||||
|
SectionHeader->Name == ".edata" // Export tables
|
||||||
|
SectionHeader->Name == ".drective"
|
||||||
|
SectionHeader->Name == ".debug$T"
|
||||||
|
SectionHeader->Name == ".debug$S"
|
||||||
|
SectionHeader->Name == ".debug$P"
|
||||||
|
SectionHeader->Name == ".debug$F"
|
||||||
|
SectionHeader->Name == ".data" //data segment
|
||||||
|
SectionHeader->Name == ".cormeta"
|
||||||
|
SectionHeader->Name == ".bss" // bss segment
|
||||||
|
|
||||||
|
undoc
|
||||||
|
SectionHeader->Name == ".textbss" // bss segment
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//*base = NtHeader->OptionalHeader.AddressOfEntryPoint;
|
||||||
|
|
||||||
|
SectionHeader = IMAGE_FIRST_SECTION(NtHeader);
|
||||||
|
while (SectionHeader != NULL)
|
||||||
|
{
|
||||||
|
if (strcmpi(SectionHeader->Name,".rsrc"))
|
||||||
|
{
|
||||||
|
/* FIXME add a rc bin to text scanner */
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (strcmpi(SectionHeader->Name,".textbss"))
|
||||||
|
{
|
||||||
|
/* FIXME add a bss to text scanner */
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (strcmpi(SectionHeader->Name,".text"))
|
||||||
|
{
|
||||||
|
switch (NtHeader->FileHeader.Machine)
|
||||||
|
{
|
||||||
|
case IMAGE_FILE_MACHINE_ALPHA:
|
||||||
|
printf("CPU ALPHA Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_ALPHA64:
|
||||||
|
printf("CPU ALPHA64/AXP64 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_AM33:
|
||||||
|
printf("CPU AM33 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_AMD64:
|
||||||
|
printf("CPU AMD64 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_ARM:
|
||||||
|
printf("CPU ARM Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_CEE:
|
||||||
|
printf("CPU CEE Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_CEF:
|
||||||
|
printf("CPU CEF Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_EBC:
|
||||||
|
printf("CPU EBC Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_I386:
|
||||||
|
printf("CPU I386 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_IA64:
|
||||||
|
printf("CPU IA64 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_M32R:
|
||||||
|
printf("CPU M32R Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_MIPS16:
|
||||||
|
printf("CPU MIPS16 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_MIPSFPU:
|
||||||
|
printf("CPU MIPSFPU Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_MIPSFPU16:
|
||||||
|
printf("CPU MIPSFPU16 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_POWERPC:
|
||||||
|
printf("CPU POWERPC Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_POWERPCFP:
|
||||||
|
printf("CPU POWERPCFP Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_R10000:
|
||||||
|
printf("CPU R10000 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_R3000:
|
||||||
|
printf("CPU R3000 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_R4000:
|
||||||
|
printf("CPU R4000 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_SH3:
|
||||||
|
printf("CPU SH3 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_SH3DSP:
|
||||||
|
printf("CPU SH3DSP Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_SH3E:
|
||||||
|
printf("CPU SH3E Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_SH4:
|
||||||
|
printf("CPU SH4 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_SH5:
|
||||||
|
printf("CPU SH5 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_THUMB:
|
||||||
|
printf("CPU THUMB Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_TRICORE:
|
||||||
|
printf("CPU TRICORE Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
case IMAGE_FILE_MACHINE_WCEMIPSV2:
|
||||||
|
printf("CPU WCEMIPSV2 Detected no CPUBrain implement for it\n");
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("Unknown Machine : %d",NtHeader->FileHeader.Machine);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* FIXME add couter to next sections */
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -5,17 +5,26 @@
|
||||||
#include "PPC.h"
|
#include "PPC.h"
|
||||||
#include "../misc.h"
|
#include "../misc.h"
|
||||||
|
|
||||||
/* retun
|
/*
|
||||||
* 0 = Ok
|
* DummyBrain is example how you create you own cpu brain to translate from
|
||||||
* 1 = unimplemt
|
* cpu to intel assembler, I have not add DummyBrain to the loader it is not
|
||||||
* 2 = Unkonwn Opcode
|
* need it in our example. When you write you own brain, it must be setup in
|
||||||
* 3 = can not open read file
|
* misc.c function LoadPFileImage and PEFileStart, PEFileStart maybe does not
|
||||||
* 4 = can not open write file
|
* need the brain you have writen so you do not need setup it there then.
|
||||||
* 5 = can not seek to end of read file
|
*
|
||||||
* 6 = can not get the file size of the read file
|
* input param:
|
||||||
* 7 = read file size is Zero
|
* cpu_buffer : the memory buffer with loaded program we whant translate
|
||||||
* 8 = can not alloc memory
|
* cpu_pos : the positions in the cpu_buffer
|
||||||
* 9 = can not read file
|
* cpu_size : the alloced memory size of the cpu_buffer
|
||||||
|
* BaseAddress : the virtual memory address we setup to use.
|
||||||
|
* cpuarch : the sub arch for the brain, example if it exists more one
|
||||||
|
* cpu with same desgin but few other opcode or extend opcode
|
||||||
|
* outfp : the output file pointer
|
||||||
|
*
|
||||||
|
* return value
|
||||||
|
* 0 : Ok
|
||||||
|
* 1 : unimplemt
|
||||||
|
* 2 : Unkonwn Opcode
|
||||||
*/
|
*/
|
||||||
|
|
||||||
CPU_INT PPCBrain( CPU_BYTE *cpu_buffer,
|
CPU_INT PPCBrain( CPU_BYTE *cpu_buffer,
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
<library>user32</library>
|
<library>user32</library>
|
||||||
|
|
||||||
<file>CpuToIntel.c</file>
|
<file>CpuToIntel.c</file>
|
||||||
|
<file>ImageLoader.c</file>
|
||||||
<file>misc.c</file>
|
<file>misc.c</file>
|
||||||
|
|
||||||
<file>ARM/ARMBrain.c</file>
|
<file>ARM/ARMBrain.c</file>
|
||||||
|
|
|
@ -5,17 +5,28 @@
|
||||||
#include "Dummy.h"
|
#include "Dummy.h"
|
||||||
#include "../misc.h"
|
#include "../misc.h"
|
||||||
|
|
||||||
/* retun
|
|
||||||
* 0 = Ok
|
|
||||||
* 1 = unimplemt
|
/*
|
||||||
* 2 = Unkonwn Opcode
|
* DummyBrain is example how you create you own cpu brain to translate from
|
||||||
* 3 = can not open read file
|
* cpu to intel assembler, I have not add DummyBrain to the loader it is not
|
||||||
* 4 = can not open write file
|
* need it in our example. When you write you own brain, it must be setup in
|
||||||
* 5 = can not seek to end of read file
|
* misc.c function LoadPFileImage and PEFileStart, PEFileStart maybe does not
|
||||||
* 6 = can not get the file size of the read file
|
* need the brain you have writen so you do not need setup it there then.
|
||||||
* 7 = read file size is Zero
|
*
|
||||||
* 8 = can not alloc memory
|
* input param:
|
||||||
* 9 = can not read file
|
* cpu_buffer : the memory buffer with loaded program we whant translate
|
||||||
|
* cpu_pos : the positions in the cpu_buffer
|
||||||
|
* cpu_size : the alloced memory size of the cpu_buffer
|
||||||
|
* BaseAddress : the virtual memory address we setup to use.
|
||||||
|
* cpuarch : the sub arch for the brain, example if it exists more one
|
||||||
|
* cpu with same desgin but few other opcode or extend opcode
|
||||||
|
* outfp : the output file pointer
|
||||||
|
*
|
||||||
|
* return value
|
||||||
|
* 0 : Ok
|
||||||
|
* 1 : unimplemt
|
||||||
|
* 2 : Unkonwn Opcode
|
||||||
*/
|
*/
|
||||||
|
|
||||||
CPU_INT DummyBrain( CPU_BYTE *cpu_buffer,
|
CPU_INT DummyBrain( CPU_BYTE *cpu_buffer,
|
||||||
|
@ -30,10 +41,7 @@ CPU_INT DummyBrain( CPU_BYTE *cpu_buffer,
|
||||||
CPU_INT retcode = 0;
|
CPU_INT retcode = 0;
|
||||||
CPU_INT retsize;
|
CPU_INT retsize;
|
||||||
|
|
||||||
|
/* now we start the process */
|
||||||
|
|
||||||
|
|
||||||
/* now we start the process */
|
|
||||||
while (cpu_pos<cpu_size)
|
while (cpu_pos<cpu_size)
|
||||||
{
|
{
|
||||||
cpu_oldpos = cpu_pos;
|
cpu_oldpos = cpu_pos;
|
||||||
|
@ -75,5 +83,5 @@ CPU_INT DummyBrain( CPU_BYTE *cpu_buffer,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return retcode;
|
return retcode;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,303 +29,6 @@
|
||||||
* type 2 : exe/dll/sys
|
* type 2 : exe/dll/sys
|
||||||
*/
|
*/
|
||||||
|
|
||||||
CPU_INT LoadPFileImage( char *infileName, char *outputfileName,
|
|
||||||
CPU_UNINT BaseAddress, char *cpuid,
|
|
||||||
CPU_UNINT type)
|
|
||||||
{
|
|
||||||
FILE *infp;
|
|
||||||
FILE *outfp;
|
|
||||||
CPU_BYTE *cpu_buffer;
|
|
||||||
CPU_UNINT cpu_pos = 0;
|
|
||||||
CPU_UNINT cpu_size=0;
|
|
||||||
|
|
||||||
|
|
||||||
/* Open file for read */
|
|
||||||
if (!(infp = fopen(infileName,"RB")))
|
|
||||||
{
|
|
||||||
printf("Can not open file %s\n",infileName);
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Open file for write */
|
|
||||||
if (!(outfp = fopen(outputfileName,"WB")))
|
|
||||||
{
|
|
||||||
printf("Can not open file %s\n",outputfileName);
|
|
||||||
return 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Load the binary file to a memory buffer */
|
|
||||||
fseek(infp,0,SEEK_END);
|
|
||||||
if (!ferror(infp))
|
|
||||||
{
|
|
||||||
printf("error can not seek in the read file");
|
|
||||||
fclose(infp);
|
|
||||||
fclose(outfp);
|
|
||||||
return 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* get the memory size buffer */
|
|
||||||
cpu_size = ftell(infp);
|
|
||||||
if (!ferror(infp))
|
|
||||||
{
|
|
||||||
printf("error can not get file size of the read file");
|
|
||||||
fclose(infp);
|
|
||||||
fclose(outfp);
|
|
||||||
return 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cpu_size==0)
|
|
||||||
{
|
|
||||||
printf("error file size is Zero lenght of the read file");
|
|
||||||
fclose(infp);
|
|
||||||
fclose(outfp);
|
|
||||||
return 7;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* alloc memory now */
|
|
||||||
if (!(cpu_buffer = (unsigned char *) malloc(cpu_size)))
|
|
||||||
{
|
|
||||||
printf("error can not alloc %uld size for memory buffer",cpu_size);
|
|
||||||
fclose(infp);
|
|
||||||
fclose(outfp);
|
|
||||||
return 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* read from the file now in one sweep */
|
|
||||||
fread(cpu_buffer,1,cpu_size,infp);
|
|
||||||
if (!ferror(infp))
|
|
||||||
{
|
|
||||||
printf("error can not read file ");
|
|
||||||
fclose(infp);
|
|
||||||
fclose(outfp);
|
|
||||||
return 9;
|
|
||||||
}
|
|
||||||
fclose(infp);
|
|
||||||
|
|
||||||
if (type==0)
|
|
||||||
{
|
|
||||||
if ( PEFileStart(cpu_buffer, 0, BaseAddress, cpu_size) !=0)
|
|
||||||
{
|
|
||||||
type=1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type== 1)
|
|
||||||
{
|
|
||||||
if (stricmp(cpuid,"m68000"))
|
|
||||||
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68000,outfp);
|
|
||||||
else if (stricmp(cpuid,"m68010"))
|
|
||||||
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68010,outfp);
|
|
||||||
else if (stricmp(cpuid,"m68020"))
|
|
||||||
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68020,outfp);
|
|
||||||
else if (stricmp(cpuid,"m68030"))
|
|
||||||
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68030,outfp);
|
|
||||||
else if (stricmp(cpuid,"m68040"))
|
|
||||||
return M68KBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,68040,outfp);
|
|
||||||
else if (stricmp(cpuid,"ppc"))
|
|
||||||
return PPCBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,0,outfp);
|
|
||||||
else if (stricmp(cpuid,"arm4"))
|
|
||||||
return ARMBrain(cpu_buffer,cpu_pos,cpu_size,BaseAddress,4,outfp);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type==2)
|
|
||||||
{
|
|
||||||
return PEFileStart(cpu_buffer, 0, BaseAddress, cpu_size);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CPU_INT PEFileStart( CPU_BYTE *memory, CPU_UNINT pos,
|
|
||||||
CPU_UNINT base, CPU_UNINT size)
|
|
||||||
{
|
|
||||||
PIMAGE_DOS_HEADER DosHeader;
|
|
||||||
PIMAGE_NT_HEADERS NtHeader;
|
|
||||||
|
|
||||||
DosHeader = (PIMAGE_DOS_HEADER)memory;
|
|
||||||
if ( (DosHeader->e_magic != IMAGE_DOS_SIGNATURE) ||
|
|
||||||
(size < 0x3c+2) )
|
|
||||||
{
|
|
||||||
printf("No MZ file \n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
NtHeader = (PIMAGE_NT_HEADERS) memory+ DosHeader->e_lfanew;
|
|
||||||
if (NtHeader->Signature != IMAGE_NT_SIGNATURE)
|
|
||||||
{
|
|
||||||
printf("No PE header found \n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(NtHeader->FileHeader.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE))
|
|
||||||
{
|
|
||||||
printf("No execute image found \n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch(NtHeader->OptionalHeader.Subsystem)
|
|
||||||
{
|
|
||||||
case IMAGE_SUBSYSTEM_EFI_APPLICATION:
|
|
||||||
printf("This exe file is desgin run in EFI bios as applactions\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
|
|
||||||
printf("This exe file is desgin run in EFI bios as service driver\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_EFI_ROM:
|
|
||||||
printf("This exe file is EFI ROM\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
|
|
||||||
printf("This exe file is desgin run in EFI bios as driver\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_NATIVE:
|
|
||||||
printf("This exe file does not need any subsystem\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
|
|
||||||
printf("This exe file is desgin run on Windows 9x as driver \n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_OS2_CUI:
|
|
||||||
printf("This exe file is desgin run on OS2 as CUI\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_POSIX_CUI:
|
|
||||||
printf("This exe file is desgin run on POSIX as CUI\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
|
|
||||||
printf("This exe file is desgin run on Windows CE as GUI\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_WINDOWS_CUI:
|
|
||||||
printf("This exe file is desgin run on Windows as CUI\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_WINDOWS_GUI:
|
|
||||||
printf("This exe file is desgin run on Windows as GUI\n");
|
|
||||||
break;
|
|
||||||
case IMAGE_SUBSYSTEM_XBOX:
|
|
||||||
printf("This exe file is desgin run on X-Box\n");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("Unknown OS : SubID : %d\n",NtHeader->OptionalHeader.Subsystem);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
//*base = NtHeader->OptionalHeader.AddressOfEntryPoint;
|
|
||||||
|
|
||||||
|
|
||||||
/* return */
|
|
||||||
switch (NtHeader->FileHeader.Machine)
|
|
||||||
{
|
|
||||||
case IMAGE_FILE_MACHINE_ALPHA:
|
|
||||||
printf("CPU ALPHA Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_ALPHA64:
|
|
||||||
printf("CPU ALPHA64/AXP64 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_AM33:
|
|
||||||
printf("CPU AM33 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_AMD64:
|
|
||||||
printf("CPU AMD64 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_ARM:
|
|
||||||
printf("CPU ARM Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_CEE:
|
|
||||||
printf("CPU CEE Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_CEF:
|
|
||||||
printf("CPU CEF Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_EBC:
|
|
||||||
printf("CPU EBC Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_I386:
|
|
||||||
printf("CPU I386 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_IA64:
|
|
||||||
printf("CPU IA64 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_M32R:
|
|
||||||
printf("CPU M32R Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_MIPS16:
|
|
||||||
printf("CPU MIPS16 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_MIPSFPU:
|
|
||||||
printf("CPU MIPSFPU Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_MIPSFPU16:
|
|
||||||
printf("CPU MIPSFPU16 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_POWERPC:
|
|
||||||
printf("CPU POWERPC Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_POWERPCFP:
|
|
||||||
printf("CPU POWERPCFP Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_R10000:
|
|
||||||
printf("CPU R10000 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_R3000:
|
|
||||||
printf("CPU R3000 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_R4000:
|
|
||||||
printf("CPU R4000 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_SH3:
|
|
||||||
printf("CPU SH3 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_SH3DSP:
|
|
||||||
printf("CPU SH3DSP Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_SH3E:
|
|
||||||
printf("CPU SH3E Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_SH4:
|
|
||||||
printf("CPU SH4 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_SH5:
|
|
||||||
printf("CPU SH5 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_THUMB:
|
|
||||||
printf("CPU THUMB Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_TRICORE:
|
|
||||||
printf("CPU TRICORE Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
case IMAGE_FILE_MACHINE_WCEMIPSV2:
|
|
||||||
printf("CPU WCEMIPSV2 Detected no CPUBrain implement for it\n");
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
default:
|
|
||||||
printf("Unknown Machine : %d",NtHeader->FileHeader.Machine);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Conveting bit array to a int byte */
|
/* Conveting bit array to a int byte */
|
||||||
|
@ -393,3 +96,11 @@ CPU_UNINT GetMaskByte32(CPU_BYTE *bit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// add no carry
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
/* Prototypes for misc stuff */
|
/* Prototypes for misc stuff */
|
||||||
CPU_INT LoadPFileImage(char *infileName, char *outputfileName, CPU_UNINT BaseAddress, char *cpuid, CPU_UNINT type);
|
CPU_INT LoadPFileImage(char *infileName, char *outputfileName, CPU_UNINT BaseAddress, char *cpuid, CPU_UNINT type);
|
||||||
CPU_INT PEFileStart( CPU_BYTE *memory, CPU_UNINT pos, CPU_UNINT base, CPU_UNINT size);
|
CPU_INT PEFileStart( CPU_BYTE *memory, CPU_UNINT pos, CPU_UNINT base, CPU_UNINT size, FILE *outfp);
|
||||||
|
|
||||||
CPU_UNINT ConvertBitToByte(CPU_BYTE *bit);
|
CPU_UNINT ConvertBitToByte(CPU_BYTE *bit);
|
||||||
CPU_UNINT GetMaskByte(CPU_BYTE *bit);
|
CPU_UNINT GetMaskByte(CPU_BYTE *bit);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue