PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
loader.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# (c) 2020 Francisco Javier Trujillo Mata <fjtrujy@gmail.com>
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include <stdio.h>
12#include <string.h>
13#include <stdlib.h>
14#include <kernel.h>
15#include <loadfile.h>
16#include <iopcontrol.h>
17#include <sifrpc.h>
18#include <errno.h>
19#include <ps2sdkapi.h>
20
21#ifdef LOADER_ENABLE_DEBUG_COLORS
22#define SET_GS_BGCOLOUR(colour) {*((volatile unsigned long int *)0x120000E0) = colour;}
23#else
24#define SET_GS_BGCOLOUR(colour)
25#endif
26
27// Color status helper in BGR format
28#define WHITE_BG 0xFFFFFF // start main
29#define CYAN_BG 0xFFFF00 // proper argc count
30#define RED_BG 0x0000FF // wrong argc count
31#define GREEN_BG 0x00FF00 // before SifLoadELF
32#define BLUE_BG 0xFF0000 // after SifLoadELF
33#define YELLOW_BG 0x00FFFF // good SifLoadELF return
34#define MAGENTA_BG 0xFF00FF // wrong SifLoadELF return
35#define ORANGE_BG 0x00A5FF // after reset IOP
36#define BROWN_BG 0x2A2AA5 // before FlushCache
37#define PURPBLE_BG 0x800080 // before ExecPS2
38
39
40//--------------------------------------------------------------
41// Redefinition of init/deinit libc:
42//--------------------------------------------------------------
43// DON'T REMOVE is for reducing binary size.
44// These funtios are defined as weak in /libc/src/init.c
45//--------------------------------------------------------------
46 void _libcglue_init() {}
47 void _libcglue_deinit() {}
48 void _libcglue_args_parse(int argc, char **argv) {}
49
50 DISABLE_PATCHED_FUNCTIONS();
51 DISABLE_EXTRA_TIMERS_FUNCTIONS();
52 PS2_DISABLE_AUTOSTART_PTHREAD();
53
54//--------------------------------------------------------------
55//Start of function code:
56//--------------------------------------------------------------
57// Clear user memory
58// PS2Link (C) 2003 Tord Lindstrom (pukko@home.se)
59// (C) 2003 adresd (adresd_ps2dev@yahoo.com)
60//--------------------------------------------------------------
61static void wipeUserMem(void)
62{
63 int i;
64 for (i = 0x100000; i < GetMemorySize(); i += 64) {
65 asm volatile(
66 "\tsq $0, 0(%0) \n"
67 "\tsq $0, 16(%0) \n"
68 "\tsq $0, 32(%0) \n"
69 "\tsq $0, 48(%0) \n" ::"r"(i));
70 }
71}
72
73//--------------------------------------------------------------
74//End of func: void wipeUserMem(void)
75//--------------------------------------------------------------
76// *** MAIN ***
77//
78//--------------------------------------------------------------
79int main(int argc, char *argv[])
80{
81 static t_ExecData elfdata;
82 int ret, i;
83
84 elfdata.epc = 0;
85
86 SET_GS_BGCOLOUR(WHITE_BG);
87 // arg[0] partition if exists, otherwise is ""
88 // arg[1]=path to ELF
89 if (argc < 2) {
90 SET_GS_BGCOLOUR(RED_BG);
91 return -EINVAL;
92 }
93
94 char *new_argv[argc - 1];
95 int fullPath_length = 1 + strlen(argv[0]) + strlen(argv[1]);
96 char fullPath[fullPath_length];
97 strcpy(fullPath, argv[0]);
98 strcat(fullPath, argv[1]);
99 // final new_argv[0] is partition + path to elf
100 new_argv[0] = fullPath;
101 for (i = 2; i < argc; i++) {
102 new_argv[i - 1] = argv[i];
103 }
104
105 SET_GS_BGCOLOUR(CYAN_BG);
106
107 // Initialize
108 sceSifInitRpc(0);
109 wipeUserMem();
110
111 //Writeback data cache before loading ELF.
112 FlushCache(0);
113 SET_GS_BGCOLOUR(GREEN_BG);
115 ret = SifLoadElf(argv[1], &elfdata);
117 SET_GS_BGCOLOUR(BLUE_BG);
118 if (ret == 0 && elfdata.epc != 0) {
119 SET_GS_BGCOLOUR(YELLOW_BG);
120
121 // Let's reset IOP because ELF was already loaded in memory
122 while(!SifIopReset(NULL, 0)){};
123 while (!SifIopSync()) {};
124
125 SET_GS_BGCOLOUR(ORANGE_BG);
126
127 sceSifInitRpc(0);
128 // Load modules.
130 SifLoadModule("rom0:SIO2MAN", 0, NULL);
131 SifLoadModule("rom0:MCMAN", 0, NULL);
132 SifLoadModule("rom0:MCSERV", 0, NULL);
134 sceSifExitRpc();
135
136 SET_GS_BGCOLOUR(BROWN_BG);
137
138 FlushCache(0);
139 FlushCache(2);
140
141 SET_GS_BGCOLOUR(PURPBLE_BG);
142
143 return ExecPS2((void *)elfdata.epc, (void *)elfdata.gp, argc-1, new_argv);
144 } else {
145 SET_GS_BGCOLOUR(MAGENTA_BG);
146 sceSifExitRpc();
147 return -ENOENT;
148 }
149}
150
151//--------------------------------------------------------------
152//End of func: int main(int argc, char *argv[])
153//--------------------------------------------------------------
154//End of file: loader.c
155//--------------------------------------------------------------
#define ENOENT
Definition errno.h:23
#define EINVAL
Definition errno.h:63
int SifLoadModule(const char *path, int arg_len, const char *args)
void SifLoadFileExit(void)
int SifLoadElf(const char *path, t_ExecData *data)
int SifLoadFileInit(void)
int SifIopReset(const char *arg, int mode)
int SifIopSync(void)