AcessNative - Huge changes, cleaning up and getting it to work
[tpg/acess2.git] / AcessNative / ld-acess_src / main.c
1 /*
2  */
3 #include "common.h"
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <string.h>
8
9 // === CODE ===
10 int main(int argc, char *argv[], char **envp)
11 {
12          int    i;
13          int    appArgc;
14         char    **appArgv;
15         char    *appPath = NULL;
16          int    (*appMain)(int, char *[], char **);
17         void    *base;
18         
19          int    syscall_handle = -1;
20         
21         for( i = 1; i < argc; i ++ )
22         {
23                 if(strcmp(argv[i], "--key") == 0) {
24                         syscall_handle = atoi(argv[++i]);
25                         continue ;
26                 }
27                 
28                 if(strcmp(argv[i], "--binary") == 0) {
29                         appPath = argv[++i];
30                         continue ;
31                 }
32                 
33                 if(strcmp(argv[i], "--open") == 0) {
34                         if( acess_open(argv[++i], 6) == -1 ) {  // Read/Write
35                                 fprintf(stderr, "Unable to open '%s'\n", argv[i]);
36                                 exit(1);
37                         }
38                         continue ;
39                 }
40                 
41                 if( argv[i][0] != '-' ) break;
42         }
43
44         if( i >= argc ) {
45                 fprintf(stderr, "Usage: ld-acess <executable> [arguments ...]\n");
46                 return 1;
47         }
48         
49         if( !appPath )
50                 appPath = argv[i];
51
52         appArgc = argc - i;
53         appArgv = &argv[i];
54
55         printf("Exectutable Path: '%s'\n", appPath);
56         printf("Executable argc = %i\n", appArgc);
57
58         base = Binary_Load(appPath, (uintptr_t*)&appMain);
59         printf("base = %p\n", base);
60         if( !base )     return 127;
61         
62         printf("==============================\n");
63         for(i = 0; i < appArgc; i ++)
64                 printf("\"%s\" ", appArgv[i]);
65         printf("\n");
66         __asm__ __volatile__ (
67                 "push %0;\n\t"
68                 "push %1;\n\t"
69                 "push %2;\n\t"
70                 "jmp *%3;\n\t"
71                 : : "r" (envp), "r" (appArgv), "r" (appArgc), "r" (appMain) );
72         return -1;
73 }
74
75 void Warning(const char *Format, ...)
76 {
77         va_list args;
78         printf("Warning: ");
79         va_start(args, Format);
80         vprintf(Format, args);
81         va_end(args);
82         printf("\n");
83 }
84
85 void Notice(const char *Format, ...)
86 {
87         va_list args;
88         printf("Notice: ");
89         va_start(args, Format);
90         vprintf(Format, args);
91         va_end(args);
92         printf("\n");
93 }
94

UCC git Repository :: git.ucc.asn.au