forked from yifanlu/UVLoader
/
uvloader.c
320 lines (292 loc) · 8.56 KB
/
uvloader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* uvloader.c - Userland Vita Loader entry point
* Copyright 2012 Yifan Lu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cleanup.h"
#include "config.h"
#include "load.h"
#include "resolve.h"
#include "scefuncs.h"
#include "utils.h"
#include "uvloader.h"
#include "debugnet.h"
// make sure code is PIE
#ifndef __PIE__
#error "Must compile with -fPIE"
#endif
static uvl_context_t *g_context;
static debug_log_func g_debug_log;
static void uvl_init_from_context (uvl_context_t *ctx);
/********************************************//**
* \brief Starting point from exploit
*
* Call this from your exploit to run UVLoader.
* It will first cache all loaded modules and
* attempt to resolve its own NIDs which
* should only depend on sceLibKernel.
* \returns Zero on success, otherwise error
***********************************************/
int START_SECTION
uvl_start (uvl_context_t *ctx) ///< Pass in context information
{
uvl_init_from_context (ctx);
uvl_scefuncs_resolve_loader (ctx->libkernel_anchor);
uvl_scefuncs_resolve_appmgruser();
vita_init_log ();
LOG ("UVLoader %u.%u.%u started.", UVL_VER_MAJOR, UVL_VER_MINOR, UVL_VER_REV);
return uvl_start_load ();
}
/********************************************//**
* \brief Sets up hooks from context buffer
***********************************************/
static void
uvl_init_from_context (uvl_context_t *ctx) ///< Context buffer
{
ctx->psvUnlockMem ();
g_context = ctx;
g_debug_log = &uvl_debug_log_psm;
ctx->psvLockMem ();
}
/********************************************//**
* \brief From context buffer
***********************************************/
void *
uvl_alloc_code_mem (unsigned int *p_len)
{
return g_context->psvCodeAllocMem (p_len);
}
/********************************************//**
* \brief From context buffer
***********************************************/
void
uvl_unlock_mem (void)
{
g_context->psvUnlockMem ();
}
/********************************************//**
* \brief From context buffer
***********************************************/
void
uvl_lock_mem (void)
{
g_context->psvLockMem ();
}
/********************************************//**
* \brief From context buffer
***********************************************/
void
uvl_flush_icache (void *addr, unsigned int len)
{
g_context->psvFlushIcache (addr, len);
}
/********************************************//**
* \brief From context buffer
***********************************************/
int
uvl_debug_log_psm (const char *line)
{
if (g_context->logline)
return g_context->logline (line);
else
return -1;
}
void
uvl_set_debug_log_func(debug_log_func func)
{
uvl_unlock_mem();
g_debug_log = func;
uvl_lock_mem();
}
int
uvl_debug_log (const char *line)
{
return g_debug_log(line);
}
/********************************************//**
* \brief printf proxy
*
* Used for exporting debug logging
***********************************************/
static int
printf (const char *format, ...)
{
char buffer[MAX_LOG_LENGTH];
va_list arg;
va_start (arg, format);
sceClibVsnprintf (buffer, MAX_LOG_LENGTH, format, arg);
va_end (arg);
g_debug_log(buffer);
return 0;
}
/********************************************//**
* \brief Add custom NID entries for UVL
***********************************************/
void
uvl_add_uvl_exports (void)
{
resolve_entry_t entry;
entry.nid = UVL_EXIT_NID;
entry.type = RESOLVE_TYPE_FUNCTION;
entry.value.func_ptr = uvl_exit;
uvl_resolve_table_add (&entry);
entry.nid = UVL_CODE_ALLOC_NID;
entry.type = RESOLVE_TYPE_FUNCTION;
entry.value.func_ptr = uvl_alloc_code_mem;
uvl_resolve_table_add (&entry);
entry.nid = UVL_CODE_UNLOCK_NID;
entry.type = RESOLVE_TYPE_FUNCTION;
entry.value.func_ptr = uvl_unlock_mem;
uvl_resolve_table_add (&entry);
entry.nid = UVL_CODE_LOCK_NID;
entry.type = RESOLVE_TYPE_FUNCTION;
entry.value.func_ptr = uvl_lock_mem;
uvl_resolve_table_add (&entry);
entry.nid = UVL_CODE_FLUSH_NID;
entry.type = RESOLVE_TYPE_FUNCTION;
entry.value.func_ptr = uvl_flush_icache;
uvl_resolve_table_add (&entry);
entry.nid = UVL_DEBUG_LOG_NID;
entry.type = RESOLVE_TYPE_FUNCTION;
entry.value.func_ptr = uvl_debug_log;
uvl_resolve_table_add (&entry);
entry.nid = UVL_PRINTF_NID;
entry.type = RESOLVE_TYPE_FUNCTION;
entry.value.func_ptr = printf;
uvl_resolve_table_add (&entry);
entry.nid = UVL_LOAD_NID;
entry.type = RESOLVE_TYPE_FUNCTION;
entry.value.func_ptr = uvl_load;
uvl_resolve_table_add (&entry);
entry.nid = UVL_LOG_WRITE_NID;
entry.type = RESOLVE_TYPE_FUNCTION;
entry.value.func_ptr = uvl_log_write;
uvl_resolve_table_add(&entry);
}
/********************************************//**
* \brief Main function. Builds resolves and load.
*
* \returns Zero on success, otherwise error
***********************************************/
int
uvl_start_load ()
{
IF_DEBUG LOG ("Initializing resolve table.");
if (uvl_resolve_table_initialize () < 0)
{
LOG ("Failed to initialize resolve table.");
return -1;
}
IF_DEBUG LOG ("Filling resolve table.");
if (uvl_resolve_add_all_modules (RESOLVE_MOD_IMPS | RESOLVE_MOD_EXPS | RESOLVE_IMPS_SVC_ONLY | RESOLVE_RELOAD_MOD) < 0)
{
LOG ("Cannot cache all loaded entries.");
return -1;
}
IF_DEBUG LOG ("Adding UVL imports.");
uvl_add_uvl_exports ();
if (g_context->use_debugnet)
{
IF_DEBUG LOG("Initializing DebugNet logging...");
if (debugNetSetup() < 0)
{
LOG("Failed to set up DebugNet.");
}
}
uvl_pre_clean();
IF_DEBUG LOG ("Loading homebrew.");
if (uvl_load (UVL_HOMEBREW_PATH) < 0)
{
return -1;
}
IF_DEBUG LOG ("Cleaning up.");
uvl_resolve_table_destroy ();
return 0;
}
/********************************************//**
* \brief Loads a executable at path
*
* Starts a new thread with the homebrew
* loaded. Waits until the homebrew exits and
* returns.
* \returns Thread ID on success, otherwise error
***********************************************/
int
uvl_load (const char *path)
{
char data_blob[LOADED_INFO_SIZE];
uvl_loaded_t *loaded;
int (*start)(int, void *);
int ret_value;
PsvUID tid;
int i;
loaded = (uvl_loaded_t *)data_blob;
IF_DEBUG LOG ("Loading homebrew");
if (uvl_load_exe (path, (void**)&start, loaded) < 0)
{
LOG ("Cannot load homebrew.");
return -1;
}
IF_DEBUG LOG ("Starting homebrew: entry at 0x%08X", start);
tid = sceKernelCreateThread ("homebrew", start, 0, 0x00040000, 0, 0x00070000, NULL);
if (tid < 0)
{
LOG ("Cannot create thread.");
return -1;
}
loaded->tid = tid;
IF_DEBUG LOG ("Starting new thread.");
if (sceKernelStartThread (tid, 0, NULL) < 0)
{
LOG ("Cannot start thread.");
return -1;
}
IF_DEBUG LOG ("Homebrew running...");
if (sceKernelWaitThreadEnd (tid, &ret_value, NULL) < 0)
{
LOG ("Failed to wait for thread to exit.");
}
IF_DEBUG LOG ("Homebrew exited with value 0x%08X", ret_value);
// free segments
for (i = 0; i < loaded->numsegs; i++)
{
sceKernelFreeMemBlock (loaded->segs[i]);
}
return 0;
}
/********************************************//**
* \brief Exiting point for loaded application
*
* This is hooked by the loaded application to
* terminate the process early. The call is
* not needed in normal operations.
* For this reason, you should not add any
* cleanup code here.
* \returns Zero on success, otherwise error
***********************************************/
void
uvl_exit (int status)
{
sceKernelExitDeleteThread (0);
}
/********************************************//**
* \brief Write to UVL log
*
* \returns Zero on success, otherwise error
***********************************************/
int
uvl_log_write(const void* buffer, unsigned int size)
{
return debugNetSend(buffer, size);
}