8 #include "From/ARM/ARM.h"
9 #include "From/m68k/m68k.h"
10 #include "From/PPC/PPC.h"
12 static CPU_INT machine_type
= 0;
13 //static CPU_INT ToMachine_type = IMAGE_FILE_MACHINE_I386;
14 static CPU_INT ToMachine_type
= IMAGE_FILE_MACHINE_POWERPC
;
16 * infileName file name to convert or disambler
17 * outputfileName file name to save to
18 * BaseAddress the address we should emulate
19 * cpuid the cpu we choice not vaild for pe loader
20 * type the loading mode Auto, PE, bin
21 * mode disambler mode : 0 the arch cpu.
22 * translate mode : 1 intel
23 * translate mode : 2 ppc
27 static void SetCPU(CPU_INT FromCpu
, CPU_INT mode
)
29 machine_type
= FromCpu
;
33 ToMachine_type
= machine_type
;
37 ToMachine_type
= IMAGE_FILE_MACHINE_I386
;
41 ToMachine_type
= IMAGE_FILE_MACHINE_POWERPC
;
45 printf("Not supported mode\n");
51 static void Convert(FILE *outfp
, CPU_INT FromCpu
, CPU_INT mode
)
53 SetCPU(machine_type
,mode
);
55 ConvertProcess(outfp
, machine_type
, ToMachine_type
);
60 CPU_INT
LoadPFileImage( char *infileName
, char *outputfileName
,
61 CPU_UNINT BaseAddress
, char *cpuid
,
62 CPU_UNINT type
, CPU_INT mode
)
67 CPU_UNINT cpu_pos
= 0;
70 //fopen("testms.exe","RB");
73 /* Open file for read */
75 if (!(infp
= fopen(infileName
, "rb")))
77 printf("Can not open file %s\n",infileName
);
81 /* Open file for write */
82 if (!(outfp
= fopen(outputfileName
,"wb")))
84 printf("Can not open file %s\n",outputfileName
);
88 /* Load the binary file to a memory buffer */
89 fseek(infp
,0,SEEK_END
);
92 printf("error can not seek in the read file");
98 /* get the memory size buffer */
99 cpu_size
= ftell(infp
);
102 printf("error can not get file size of the read file");
108 /* Load the binary file to a memory buffer */
109 fseek(infp
,0,SEEK_SET
);
112 printf("error can not seek in the read file");
120 printf("error file size is Zero lenght of the read file");
126 /* alloc memory now */
128 if (!(cpu_buffer
= (unsigned char *) malloc(cpu_size
+1)))
130 printf("error can not alloc %uld size for memory buffer",cpu_size
);
135 ZeroMemory(cpu_buffer
,cpu_size
);
137 /* read from the file now in one sweep */
138 fread((void *)cpu_buffer
,1,cpu_size
,infp
);
141 printf("error can not read file ");
150 if ( PEFileStart(cpu_buffer
, 0, BaseAddress
, cpu_size
, outfp
, mode
) !=0)
158 Convert(outfp
,machine_type
,mode
);
170 if (stricmp(cpuid
,"m68000"))
172 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68000,outfp
);
175 Convert(outfp
,machine_type
,mode
);
179 else if (stricmp(cpuid
,"m68010"))
181 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68010,outfp
);
184 Convert(outfp
,machine_type
,mode
);
189 else if (stricmp(cpuid
,"m68020"))
191 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68020,outfp
);
194 Convert(outfp
,machine_type
,mode
);
199 else if (stricmp(cpuid
,"m68030"))
201 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68030,outfp
);
204 Convert(outfp
,machine_type
,mode
);
209 else if (stricmp(cpuid
,"m68040"))
211 ret
= M68KBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,68040,outfp
);
214 Convert(outfp
,machine_type
,mode
);
219 else if (stricmp(cpuid
,"ppc"))
221 ret
= PPCBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,0,outfp
);
224 Convert(outfp
,machine_type
,mode
);
229 else if (stricmp(cpuid
,"arm4"))
231 ret
= ARMBrain(cpu_buffer
,cpu_pos
,cpu_size
,BaseAddress
,4,outfp
);
234 Convert(outfp
,machine_type
,mode
);
244 ret
= PEFileStart(cpu_buffer
, 0, BaseAddress
, cpu_size
, outfp
, mode
);
247 Convert(outfp
,machine_type
,mode
);
256 #define MAXSECTIONNUMBER 16
258 CPU_INT
PEFileStart( CPU_BYTE
*memory
, CPU_UNINT pos
,
259 CPU_UNINT base
, CPU_UNINT size
,
260 FILE *outfp
, CPU_INT mode
)
262 PIMAGE_DOS_HEADER DosHeader
;
263 PIMAGE_NT_HEADERS NtHeader
;
264 IMAGE_SECTION_HEADER SectionHeader
[MAXSECTIONNUMBER
] = {NULL
};
265 PIMAGE_SECTION_HEADER pSectionHeader
;
266 PIMAGE_EXPORT_DIRECTORY ExportEntry
;
267 INT NumberOfSections
;
268 INT NumberOfSectionsCount
=0;
271 DosHeader
= (PIMAGE_DOS_HEADER
)memory
;
272 if ( (DosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
) ||
275 printf("No MZ file \n");
279 NtHeader
= (PIMAGE_NT_HEADERS
) (((ULONG
)memory
) + ((ULONG
)DosHeader
->e_lfanew
));
280 if (NtHeader
->Signature
!= IMAGE_NT_SIGNATURE
)
282 printf("No PE header found \n");
285 if (!(NtHeader
->FileHeader
.Characteristics
& IMAGE_FILE_EXECUTABLE_IMAGE
))
287 printf("No execute image found \n");
291 switch(NtHeader
->OptionalHeader
.Subsystem
)
293 case IMAGE_SUBSYSTEM_EFI_APPLICATION
:
294 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_EFI_APPLICATION\n");
295 printf("This exe file is desgin run in EFI bios as applactions\n");
297 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
298 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER\n");
299 printf("This exe file is desgin run in EFI bios as service driver\n");
301 case IMAGE_SUBSYSTEM_EFI_ROM
:
302 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_EFI_ROM\n");
303 printf("This exe file is EFI ROM\n");
305 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
306 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER\n");
307 printf("This exe file is desgin run in EFI bios as driver\n");
309 case IMAGE_SUBSYSTEM_NATIVE
:
310 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_NATIVE\n");
311 printf("This exe file does not need any subsystem\n");
313 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS
:
314 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_NATIVE_WINDOWS\n");
315 printf("This exe file is desgin run on Windows 9x as driver \n");
317 case IMAGE_SUBSYSTEM_OS2_CUI
:
318 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_OS2_CUI\n");
319 printf("This exe file is desgin run on OS2 as CUI\n");
321 case IMAGE_SUBSYSTEM_POSIX_CUI
:
322 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_POSIX_CUI\n");
323 printf("This exe file is desgin run on POSIX as CUI\n");
325 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
:
326 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_WINDOWS_CE_GUI\n");
327 printf("This exe file is desgin run on Windows CE as GUI\n");
329 case IMAGE_SUBSYSTEM_WINDOWS_CUI
:
330 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_WINDOWS_CUI\n");
331 printf("This exe file is desgin run on Windows as CUI\n");
333 case IMAGE_SUBSYSTEM_WINDOWS_GUI
:
334 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_WINDOWS_GUI\n");
335 printf("This exe file is desgin run on Windows as GUI\n");
337 case IMAGE_SUBSYSTEM_XBOX
:
338 fprintf(outfp
,"; OS type : IMAGE_SUBSYSTEM_XBOX\n");
339 printf("This exe file is desgin run on X-Box\n");
342 fprintf(outfp
,"; OS type : Unknown\n");
343 printf("Unknown OS : SubID : %d\n",NtHeader
->OptionalHeader
.Subsystem
);
348 printf("Number of object : %d\n",NtHeader
->FileHeader
.NumberOfSections
);
349 printf("Base Address : %8x\n\n",NtHeader
->OptionalHeader
.ImageBase
);
351 pSectionHeader
= IMAGE_FIRST_SECTION(NtHeader
);
353 NumberOfSections
= NtHeader
->FileHeader
.NumberOfSections
;
355 for (i
= 0; i
< NumberOfSections
; i
++)
357 SectionHeader
[i
] = *pSectionHeader
++;
358 printf("Found Sector : %s \n ",SectionHeader
[i
].Name
);
359 printf("RVA: %08lX ",SectionHeader
[i
].VirtualAddress
);
360 printf("Offset: %08lX ",SectionHeader
[i
].PointerToRawData
);
361 printf("Size: %08lX ",SectionHeader
[i
].SizeOfRawData
);
362 printf("Flags: %08lX \n\n",SectionHeader
[i
].Characteristics
);
365 /* Get export data */
366 if (NtHeader
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].Size
!= 0)
368 for (i
= 0; i
< NumberOfSections
; i
++)
370 if ( SectionHeader
[i
].VirtualAddress
<= (ULONG
) NtHeader
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
&&
371 SectionHeader
[i
].VirtualAddress
+ SectionHeader
[i
].SizeOfRawData
> (ULONG
)NtHeader
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
)
373 ExportEntry
= (PIMAGE_NT_HEADERS
) (((ULONG
)memory
) +
374 (ULONG
)(NtHeader
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
-
375 SectionHeader
[i
].VirtualAddress
+
376 SectionHeader
[i
].PointerToRawData
));
384 for (i
=0;i
< NumberOfSections
; i
++)
386 if (strnicmp((PCHAR
) SectionHeader
[i
].Name
,".text\0",6)==0)
388 switch (NtHeader
->FileHeader
.Machine
)
390 case IMAGE_FILE_MACHINE_ALPHA
:
391 printf("CPU ALPHA Detected no CPUBrain implement for it\n");
392 fprintf(outfp
,"; CPU found Alpha\n");
393 machine_type
= IMAGE_FILE_MACHINE_ALPHA
;
396 case IMAGE_FILE_MACHINE_ALPHA64
:
397 printf("CPU ALPHA64/AXP64 Detected no CPUBrain implement for it\n");
398 fprintf(outfp
,"; CPU found Alpha64/AXP64\n");
399 machine_type
= IMAGE_FILE_MACHINE_ALPHA64
;
402 case IMAGE_FILE_MACHINE_AM33
:
403 printf("CPU AM33 Detected no CPUBrain implement for it\n");
404 fprintf(outfp
,"; CPU found AM33\n");
405 machine_type
= IMAGE_FILE_MACHINE_AM33
;
408 case IMAGE_FILE_MACHINE_AMD64
:
409 printf("CPU AMD64 Detected no CPUBrain implement for it\n");
410 fprintf(outfp
,"; CPU found AMD64\n");
411 machine_type
= IMAGE_FILE_MACHINE_AMD64
;
414 case IMAGE_FILE_MACHINE_ARM
:
415 printf("CPU ARM Detected no CPUBrain implement for it\n");
416 fprintf(outfp
,"; CPU found ARM\n");
417 machine_type
= IMAGE_FILE_MACHINE_ARM
;
420 case IMAGE_FILE_MACHINE_CEE
:
421 printf("CPU CEE Detected no CPUBrain implement for it\n");
422 fprintf(outfp
,"; CPU found CEE\n");
423 machine_type
= IMAGE_FILE_MACHINE_CEE
;
426 case IMAGE_FILE_MACHINE_CEF
:
427 printf("CPU CEF Detected no CPUBrain implement for it\n");
428 fprintf(outfp
,"; CPU found CEF\n");
429 machine_type
= IMAGE_FILE_MACHINE_CEF
;
432 case IMAGE_FILE_MACHINE_EBC
:
433 printf("CPU EBC Detected no CPUBrain implement for it\n");
434 fprintf(outfp
,"; CPU found EBC\n");
435 machine_type
= IMAGE_FILE_MACHINE_EBC
;
438 case IMAGE_FILE_MACHINE_I386
:
439 printf("CPU I386 Detected no CPUBrain implement for it\n");
440 fprintf(outfp
,"; CPU found I386\n");
441 machine_type
= IMAGE_FILE_MACHINE_I386
;
444 case IMAGE_FILE_MACHINE_IA64
:
445 printf("CPU IA64 Detected no CPUBrain implement for it\n");
446 fprintf(outfp
,"; CPU found IA64\n");
447 machine_type
= IMAGE_FILE_MACHINE_IA64
;
450 case IMAGE_FILE_MACHINE_M32R
:
451 printf("CPU M32R Detected no CPUBrain implement for it\n");
452 fprintf(outfp
,"; CPU found M32R\n");
453 machine_type
= IMAGE_FILE_MACHINE_M32R
;
456 case IMAGE_FILE_MACHINE_MIPS16
:
457 printf("CPU MIPS16 Detected no CPUBrain implement for it\n");
458 fprintf(outfp
,"; CPU found MIPS16\n");
459 machine_type
= IMAGE_FILE_MACHINE_MIPS16
;
462 case IMAGE_FILE_MACHINE_MIPSFPU
:
463 printf("CPU MIPSFPU Detected no CPUBrain implement for it\n");
464 fprintf(outfp
,"; CPU found MIPSFPU\n");
465 machine_type
= IMAGE_FILE_MACHINE_MIPSFPU
;
468 case IMAGE_FILE_MACHINE_MIPSFPU16
:
469 printf("CPU MIPSFPU16 Detected no CPUBrain implement for it\n");
470 fprintf(outfp
,"; CPU found MIPSFPU16\n");
471 machine_type
= IMAGE_FILE_MACHINE_MIPSFPU16
;
474 case IMAGE_FILE_MACHINE_POWERPC
:
475 printf("CPU POWERPC Detected partily CPUBrain implement for it\n");
476 fprintf(outfp
,"; CPU found POWERPC\n");
477 //PPCBrain(memory, pos, cpu_size, base, 0, outfp);
478 machine_type
= IMAGE_FILE_MACHINE_POWERPC
;
479 PPCBrain(memory
+SectionHeader
[i
].PointerToRawData
, 0, SectionHeader
[i
].SizeOfRawData
, NtHeader
->OptionalHeader
.ImageBase
, 0, outfp
);
483 case IMAGE_FILE_MACHINE_POWERPCFP
:
484 printf("CPU POWERPCFP Detected no CPUBrain implement for it\n");
485 fprintf(outfp
,"; CPU found POWERPCFP\n");
486 machine_type
= IMAGE_FILE_MACHINE_POWERPCFP
;
489 case IMAGE_FILE_MACHINE_R10000
:
490 printf("CPU R10000 Detected no CPUBrain implement for it\n");
491 fprintf(outfp
,"; CPU found R10000\n");
492 machine_type
= IMAGE_FILE_MACHINE_R10000
;
495 case IMAGE_FILE_MACHINE_R3000
:
496 printf("CPU R3000 Detected no CPUBrain implement for it\n");
497 fprintf(outfp
,"; CPU found R3000\n");
498 machine_type
= IMAGE_FILE_MACHINE_R3000
;
501 case IMAGE_FILE_MACHINE_R4000
:
502 printf("CPU R4000 Detected no CPUBrain implement for it\n");
503 fprintf(outfp
,"; CPU found R4000\n");
504 machine_type
= IMAGE_FILE_MACHINE_R4000
;
507 case IMAGE_FILE_MACHINE_SH3
:
508 printf("CPU SH3 Detected no CPUBrain implement for it\n");
509 fprintf(outfp
,"; CPU found SH3\n");
510 machine_type
= IMAGE_FILE_MACHINE_SH3
;
513 case IMAGE_FILE_MACHINE_SH3DSP
:
514 printf("CPU SH3DSP Detected no CPUBrain implement for it\n");
515 fprintf(outfp
,"; CPU found SH3DSP\n");
516 machine_type
= IMAGE_FILE_MACHINE_SH3DSP
;
519 case IMAGE_FILE_MACHINE_SH3E
:
520 printf("CPU SH3E Detected no CPUBrain implement for it\n");
521 fprintf(outfp
,"; CPU found SH3E\n");
522 machine_type
= IMAGE_FILE_MACHINE_SH3E
;
525 case IMAGE_FILE_MACHINE_SH4
:
526 printf("CPU SH4 Detected no CPUBrain implement for it\n");
527 fprintf(outfp
,"; CPU found SH4\n");
528 machine_type
= IMAGE_FILE_MACHINE_SH4
;
531 case IMAGE_FILE_MACHINE_SH5
:
532 printf("CPU SH5 Detected no CPUBrain implement for it\n");
533 fprintf(outfp
,"; CPU found SH5\n");
534 machine_type
= IMAGE_FILE_MACHINE_SH5
;
537 case IMAGE_FILE_MACHINE_THUMB
:
538 printf("CPU THUMB Detected no CPUBrain implement for it\n");
539 fprintf(outfp
,"; CPU found THUMB\n");
540 machine_type
= IMAGE_FILE_MACHINE_THUMB
;
543 case IMAGE_FILE_MACHINE_TRICORE
:
544 printf("CPU TRICORE Detected no CPUBrain implement for it\n");
545 fprintf(outfp
,"; CPU found TRICORE\n");
546 machine_type
= IMAGE_FILE_MACHINE_TRICORE
;
549 case IMAGE_FILE_MACHINE_WCEMIPSV2
:
550 printf("CPU WCEMIPSV2 Detected no CPUBrain implement for it\n");
551 fprintf(outfp
,"; CPU found WCEMIPSV2\n");
552 machine_type
= IMAGE_FILE_MACHINE_WCEMIPSV2
;
556 printf("Unknown Machine : %d",NtHeader
->FileHeader
.Machine
);
558 } /* end case switch*/
559 } /* end if text sector */