8 #include "From/ARM/ARM.h"
9 #include "From/m68k/m68k.h"
10 #include "From/PPC/PPC.h"
13 * infileName file name to convert or disambler
14 * outputfileName file name to save to
15 * BaseAddress the address we should emulate
16 * cpuid the cpu we choice not vaild for pe loader
17 * type the loading mode Auto, PE, bin
18 * mode disambler mode : 0 the arch cpu.
19 * translate mode : 1 intel
23 CPU_INT
LoadPFileImage( char *infileName
, char *outputfileName
,
24 CPU_UNINT BaseAddress
, char *cpuid
,
25 CPU_UNINT type
, CPU_INT mode
)
30 CPU_UNINT cpu_pos
= 0;
33 //fopen("testms.exe","RB");
36 /* Open file for read */
38 if (!(infp
= fopen(infileName
, "rb")))
40 printf("Can not open file %s\n",infileName
);
44 /* Open file for write */
45 if (!(outfp
= fopen(outputfileName
,"wb")))
47 printf("Can not open file %s\n",outputfileName
);
51 /* Load the binary file to a memory buffer */
52 fseek(infp
,0,SEEK_END
);
55 printf("error can not seek in the read file");
61 /* get the memory size buffer */
62 cpu_size
= ftell(infp
);
65 printf("error can not get file size of the read file");
71 /* Load the binary file to a memory buffer */
72 fseek(infp
,0,SEEK_SET
);
75 printf("error can not seek in the read file");
83 printf("error file size is Zero lenght of the read file");
89 /* alloc memory now */
91 if (!(cpu_buffer
= (unsigned char *) malloc(cpu_size
+1)))
93 printf("error can not alloc %uld size for memory buffer",cpu_size
);
98 ZeroMemory(cpu_buffer
,cpu_size
);
100 /* read from the file now in one sweep */
101 fread((void *)cpu_buffer
,1,cpu_size
,infp
);
104 printf("error can not read file ");
113 if ( PEFileStart(cpu_buffer
, 0, BaseAddress
, cpu_size
, outfp
, mode
) !=0)
122 ConvertToIntelProcess(outfp
,cpuid
);
132 if (stricmp(cpuid
,"m68000"))
134 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68000,outfp
,mode
);
138 ConvertToIntelProcess(outfp
,cpuid
);
143 else if (stricmp(cpuid
,"m68010"))
145 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68010,outfp
,mode
);
149 ConvertToIntelProcess(outfp
,cpuid
);
155 else if (stricmp(cpuid
,"m68020"))
157 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68020,outfp
,mode
);
161 ConvertToIntelProcess(outfp
,cpuid
);
167 else if (stricmp(cpuid
,"m68030"))
169 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68030,outfp
,mode
);
173 ConvertToIntelProcess(outfp
,cpuid
);
179 else if (stricmp(cpuid
,"m68040"))
181 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68040,outfp
,mode
);
185 ConvertToIntelProcess(outfp
,cpuid
);
191 else if (stricmp(cpuid
,"ppc"))
193 ret
= PPCBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,0,outfp
,mode
);
197 ConvertToIntelProcess(outfp
,cpuid
);
203 else if (stricmp(cpuid
,"arm4"))
205 ret
= ARMBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,4,outfp
,mode
);
209 ConvertToIntelProcess(outfp
,cpuid
);
220 ret
= PEFileStart(cpu_buffer
, 0, BaseAddress
, cpu_size
, outfp
, mode
);
224 ConvertToIntelProcess(outfp
,cpuid
);
234 CPU_INT
PEFileStart( CPU_BYTE
*memory
, CPU_UNINT pos
,
235 CPU_UNINT base
, CPU_UNINT size
,
236 FILE *outfp
, CPU_INT mode
)
238 PIMAGE_DOS_HEADER DosHeader
;
239 PIMAGE_NT_HEADERS NtHeader
;
240 PIMAGE_SECTION_HEADER SectionHeader
;
241 INT NumberOfSections
;
242 INT NumberOfSectionsCount
=0;
244 DosHeader
= (PIMAGE_DOS_HEADER
)memory
;
245 if ( (DosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
) ||
248 printf("No MZ file \n");
252 NtHeader
= (PIMAGE_NT_HEADERS
) (((ULONG
)memory
) + ((ULONG
)DosHeader
->e_lfanew
));
253 if (NtHeader
->Signature
!= IMAGE_NT_SIGNATURE
)
255 printf("No PE header found \n");
258 if (!(NtHeader
->FileHeader
.Characteristics
& IMAGE_FILE_EXECUTABLE_IMAGE
))
260 printf("No execute image found \n");
264 switch(NtHeader
->OptionalHeader
.Subsystem
)
266 case IMAGE_SUBSYSTEM_EFI_APPLICATION
:
267 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_EFI_APPLICATION\n");
268 printf("This exe file is desgin run in EFI bios as applactions\n");
270 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
271 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER\n");
272 printf("This exe file is desgin run in EFI bios as service driver\n");
274 case IMAGE_SUBSYSTEM_EFI_ROM
:
275 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_EFI_ROM\n");
276 printf("This exe file is EFI ROM\n");
278 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
279 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER\n");
280 printf("This exe file is desgin run in EFI bios as driver\n");
282 case IMAGE_SUBSYSTEM_NATIVE
:
283 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_NATIVE\n");
284 printf("This exe file does not need any subsystem\n");
286 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS
:
287 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_NATIVE_WINDOWS\n");
288 printf("This exe file is desgin run on Windows 9x as driver \n");
290 case IMAGE_SUBSYSTEM_OS2_CUI
:
291 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_OS2_CUI\n");
292 printf("This exe file is desgin run on OS2 as CUI\n");
294 case IMAGE_SUBSYSTEM_POSIX_CUI
:
295 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_POSIX_CUI\n");
296 printf("This exe file is desgin run on POSIX as CUI\n");
298 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
:
299 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_WINDOWS_CE_GUI\n");
300 printf("This exe file is desgin run on Windows CE as GUI\n");
302 case IMAGE_SUBSYSTEM_WINDOWS_CUI
:
303 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_WINDOWS_CUI\n");
304 printf("This exe file is desgin run on Windows as CUI\n");
306 case IMAGE_SUBSYSTEM_WINDOWS_GUI
:
307 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_WINDOWS_GUI\n");
308 printf("This exe file is desgin run on Windows as GUI\n");
310 case IMAGE_SUBSYSTEM_XBOX
:
311 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_XBOX\n");
312 printf("This exe file is desgin run on X-Box\n");
315 fprintf(outfp
,"; OS type : Unknown\n");
316 printf("Unknown OS : SubID : %d\n",NtHeader
->OptionalHeader
.Subsystem
);
321 SectionHeader->Name == ".tls$"
322 SectionHeader->Name == ".tls"
323 SectionHeader->Name == ".text" // Executable code
324 SectionHeader->Name == ".sxdata"
325 SectionHeader->Name == ".sdata"
326 SectionHeader->Name == ".sbss"
327 SectionHeader->Name == ".rsrc" // rc data
328 SectionHeader->Name == ".reloc"
329 SectionHeader->Name == ".rdata" // read only initialized data
330 SectionHeader->Name == ".pdata"
331 SectionHeader->Name == ".idlsym"
332 SectionHeader->Name == ".idata" // Import tables
333 SectionHeader->Name == ".edata" // Export tables
334 SectionHeader->Name == ".drective"
335 SectionHeader->Name == ".debug$T"
336 SectionHeader->Name == ".debug$S"
337 SectionHeader->Name == ".debug$P"
338 SectionHeader->Name == ".debug$F"
339 SectionHeader->Name == ".data" //data segment
340 SectionHeader->Name == ".cormeta"
341 SectionHeader->Name == ".bss" // bss segment
344 SectionHeader->Name == ".textbss" // bss segment
347 //*base = NtHeader->OptionalHeader.AddressOfEntryPoint;
349 SectionHeader
= IMAGE_FIRST_SECTION(NtHeader
);
350 NumberOfSections
= NtHeader
->FileHeader
.NumberOfSections
;
352 for (NumberOfSectionsCount
= 0; NumberOfSectionsCount
< NumberOfSections
; NumberOfSectionsCount
++, SectionHeader
++)
354 if (strnicmp((PCHAR
) SectionHeader
->Name
,".rsrc",5)==0)
356 /* FIXME add a rc bin to text scanner */
359 else if (strnicmp((PCHAR
) SectionHeader
->Name
,".textbss",8)==0)
361 /* FIXME add a bss to text scanner */
365 else if (strnicmp((PCHAR
) SectionHeader
->Name
,".text\0",6)==0)
368 FIXME we should output gas syntax
375 fprintf(outfp
,".SECTION .text\n");
376 switch (NtHeader
->FileHeader
.Machine
)
378 case IMAGE_FILE_MACHINE_ALPHA
:
379 printf("CPU ALPHA Detected no CPUBrain implement for it\n");
380 fprintf(outfp
,"; CPU found Alpha\n");
383 case IMAGE_FILE_MACHINE_ALPHA64
:
384 printf("CPU ALPHA64/AXP64 Detected no CPUBrain implement for it\n");
385 fprintf(outfp
,"; CPU found Alpha64/AXP64\n");
388 case IMAGE_FILE_MACHINE_AM33
:
389 printf("CPU AM33 Detected no CPUBrain implement for it\n");
390 fprintf(outfp
,"; CPU found AM33\n");
393 case IMAGE_FILE_MACHINE_AMD64
:
394 printf("CPU AMD64 Detected no CPUBrain implement for it\n");
395 fprintf(outfp
,"; CPU found AMD64\n");
398 case IMAGE_FILE_MACHINE_ARM
:
399 printf("CPU ARM Detected no CPUBrain implement for it\n");
400 fprintf(outfp
,"; CPU found ARM\n");
403 case IMAGE_FILE_MACHINE_CEE
:
404 printf("CPU CEE Detected no CPUBrain implement for it\n");
405 fprintf(outfp
,"; CPU found CEE\n");
408 case IMAGE_FILE_MACHINE_CEF
:
409 printf("CPU CEF Detected no CPUBrain implement for it\n");
410 fprintf(outfp
,"; CPU found CEF\n");
413 case IMAGE_FILE_MACHINE_EBC
:
414 printf("CPU EBC Detected no CPUBrain implement for it\n");
415 fprintf(outfp
,"; CPU found EBC\n");
418 case IMAGE_FILE_MACHINE_I386
:
419 printf("CPU I386 Detected no CPUBrain implement for it\n");
420 fprintf(outfp
,"; CPU found I386\n");
423 case IMAGE_FILE_MACHINE_IA64
:
424 printf("CPU IA64 Detected no CPUBrain implement for it\n");
425 fprintf(outfp
,"; CPU found IA64\n");
428 case IMAGE_FILE_MACHINE_M32R
:
429 printf("CPU M32R Detected no CPUBrain implement for it\n");
430 fprintf(outfp
,"; CPU found M32R\n");
433 case IMAGE_FILE_MACHINE_MIPS16
:
434 printf("CPU MIPS16 Detected no CPUBrain implement for it\n");
435 fprintf(outfp
,"; CPU found MIPS16\n");
438 case IMAGE_FILE_MACHINE_MIPSFPU
:
439 printf("CPU MIPSFPU Detected no CPUBrain implement for it\n");
440 fprintf(outfp
,"; CPU found MIPSFPU\n");
443 case IMAGE_FILE_MACHINE_MIPSFPU16
:
444 printf("CPU MIPSFPU16 Detected no CPUBrain implement for it\n");
445 fprintf(outfp
,"; CPU found MIPSFPU16\n");
448 case IMAGE_FILE_MACHINE_POWERPC
:
449 printf("CPU POWERPC Detected partily CPUBrain implement for it\n");
450 fprintf(outfp
,"; CPU found POWERPC\n");
451 //PPCBrain(memory, pos, cpu_size, base, 0, outfp);
452 return PPCBrain(memory
+SectionHeader
->PointerToRawData
, 0, SectionHeader
->SizeOfRawData
, 0, 0, outfp
,mode
);
456 case IMAGE_FILE_MACHINE_POWERPCFP
:
457 printf("CPU POWERPCFP Detected no CPUBrain implement for it\n");
458 fprintf(outfp
,"; CPU found POWERPCFP\n");
461 case IMAGE_FILE_MACHINE_R10000
:
462 printf("CPU R10000 Detected no CPUBrain implement for it\n");
463 fprintf(outfp
,"; CPU found R10000\n");
466 case IMAGE_FILE_MACHINE_R3000
:
467 printf("CPU R3000 Detected no CPUBrain implement for it\n");
468 fprintf(outfp
,"; CPU found R3000\n");
471 case IMAGE_FILE_MACHINE_R4000
:
472 printf("CPU R4000 Detected no CPUBrain implement for it\n");
473 fprintf(outfp
,"; CPU found R4000\n");
476 case IMAGE_FILE_MACHINE_SH3
:
477 printf("CPU SH3 Detected no CPUBrain implement for it\n");
478 fprintf(outfp
,"; CPU found SH3\n");
481 case IMAGE_FILE_MACHINE_SH3DSP
:
482 printf("CPU SH3DSP Detected no CPUBrain implement for it\n");
483 fprintf(outfp
,"; CPU found SH3DSP\n");
486 case IMAGE_FILE_MACHINE_SH3E
:
487 printf("CPU SH3E Detected no CPUBrain implement for it\n");
488 fprintf(outfp
,"; CPU found SH3E\n");
491 case IMAGE_FILE_MACHINE_SH4
:
492 printf("CPU SH4 Detected no CPUBrain implement for it\n");
493 fprintf(outfp
,"; CPU found SH4\n");
496 case IMAGE_FILE_MACHINE_SH5
:
497 printf("CPU SH5 Detected no CPUBrain implement for it\n");
498 fprintf(outfp
,"; CPU found SH5\n");
501 case IMAGE_FILE_MACHINE_THUMB
:
502 printf("CPU THUMB Detected no CPUBrain implement for it\n");
503 fprintf(outfp
,"; CPU found THUMB\n");
506 case IMAGE_FILE_MACHINE_TRICORE
:
507 printf("CPU TRICORE Detected no CPUBrain implement for it\n");
508 fprintf(outfp
,"; CPU found TRICORE\n");
511 case IMAGE_FILE_MACHINE_WCEMIPSV2
:
512 printf("CPU WCEMIPSV2 Detected no CPUBrain implement for it\n");
513 fprintf(outfp
,"; CPU found WCEMIPSV2\n");
517 printf("Unknown Machine : %d",NtHeader
->FileHeader
.Machine
);
521 /* End if .text statment */