forked from TheOfficialFloW/modoru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
433 lines (345 loc) · 10.7 KB
/
main.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* main.c -- launcher
*
* Copyright (C) 2019 TheFloW
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <psp2/appmgr.h>
#include <psp2/ctrl.h>
#include <psp2/power.h>
#include <psp2/shellutil.h>
#include <psp2/vshbridge.h>
#include <psp2/io/devctl.h>
#include <psp2/io/fcntl.h>
#include <psp2/io/stat.h>
#include <psp2/io/dirent.h>
#include <psp2/kernel/modulemgr.h>
#include <psp2/kernel/processmgr.h>
#include <taihen.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <malloc.h>
#include "pspdebug.h"
#include "modoru.h"
#define printf psvDebugScreenPrintf
#define APP_PATH "ux0:app/MODORU000/"
#define PUP_PATH APP_PATH "PSP2UPDAT.PUP"
#define CHUNK_SIZE 64 * 1024
#define WHITE 0x00FFFFFF
#define YELLOW 0x0000FFFF
static SceUID modoru_patch_id = -1, modoru_kernel_id = -1, modoru_user_id = -1;
int unload_modoru_drivers(void);
void ErrorExit(int milisecs, char *fmt, ...) {
va_list list;
char msg[256];
va_start(list, fmt);
vsprintf(msg, fmt, list);
va_end(list);
printf(msg);
sceKernelDelayThread(milisecs * 1000);
unload_modoru_drivers();
sceKernelPowerUnlock(0);
sceKernelExitProcess(0);
}
int unload_modoru_drivers(void) {
if (modoru_user_id >= 0)
sceKernelStopUnloadModule(modoru_user_id, 0, NULL, 0, NULL, NULL);
if (modoru_kernel_id >= 0)
taiStopUnloadKernelModule(modoru_kernel_id, 0, NULL, 0, NULL, NULL);
if (modoru_patch_id >= 0)
taiStopUnloadKernelModule(modoru_patch_id, 0, NULL, 0, NULL, NULL);
return 0;
}
int load_modoru_drivers(void) {
modoru_patch_id = taiLoadStartKernelModule(APP_PATH "modoru_patch.skprx", 0, NULL, 0);
if (modoru_patch_id < 0)
return modoru_patch_id;
modoru_kernel_id = taiLoadStartKernelModule(APP_PATH "modoru_kernel.skprx", 0, NULL, 0);
if (modoru_kernel_id < 0)
return modoru_kernel_id;
modoru_user_id = sceKernelLoadStartModule(APP_PATH "modoru_user.suprx", 0, NULL, 0, NULL, NULL);
if (modoru_user_id < 0)
return modoru_user_id;
return 0;
}
// by yifanlu
int extract(const char *pup, const char *psp2swu) {
int inf, outf;
if ((inf = sceIoOpen(pup, SCE_O_RDONLY, 0)) < 0) {
return -1;
}
if ((outf = sceIoOpen(psp2swu, SCE_O_CREAT | SCE_O_WRONLY | SCE_O_TRUNC, 6)) < 0) {
return -1;
}
int ret = -1;
int count;
if (sceIoLseek(inf, 0x18, SCE_SEEK_SET) < 0) {
goto end;
}
if (sceIoRead(inf, &count, 4) < 4) {
goto end;
}
if (sceIoLseek(inf, 0x80, SCE_SEEK_SET) < 0) {
goto end;
}
struct {
uint64_t id;
uint64_t off;
uint64_t len;
uint64_t field_18;
} __attribute__((packed)) file_entry;
for (int i = 0; i < count; i++) {
if (sceIoRead(inf, &file_entry, sizeof(file_entry)) != sizeof(file_entry)) {
goto end;
}
if (file_entry.id == 0x200) {
break;
}
}
if (file_entry.id == 0x200) {
char buffer[1024];
size_t rd;
if (sceIoLseek(inf, file_entry.off, SCE_SEEK_SET) < 0) {
goto end;
}
while (file_entry.len && (rd = sceIoRead(inf, buffer, sizeof(buffer))) > 0) {
if (rd > file_entry.len) {
rd = file_entry.len;
}
sceIoWrite(outf, buffer, rd);
file_entry.len -= rd;
}
if (file_entry.len == 0) {
ret = 0;
}
}
end:
sceIoClose(inf);
sceIoClose(outf);
return ret;
}
int copy(const char *src, const char *dst) {
int res;
SceUID fdsrc = -1, fddst = -1;
void *buf = NULL;
res = fdsrc = sceIoOpen(src, SCE_O_RDONLY, 0);
if (res < 0)
goto err;
res = fddst = sceIoOpen(dst, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
if (res < 0)
goto err;
buf = memalign(4096, CHUNK_SIZE);
if (!buf) {
res = -1;
goto err;
}
do {
res = sceIoRead(fdsrc, buf, CHUNK_SIZE);
if (res > 0)
res = sceIoWrite(fddst, buf, res);
} while (res > 0);
err:
if (buf)
free(buf);
if (fddst >= 0)
sceIoClose(fddst);
if (fdsrc >= 0)
sceIoClose(fdsrc);
return res;
}
int remove_dir(const char *path) {
SceUID dfd = sceIoDopen(path);
if (dfd >= 0) {
int res = 0;
do {
SceIoDirent dir;
memset(&dir, 0, sizeof(SceIoDirent));
res = sceIoDread(dfd, &dir);
if (res > 0) {
char *new_path = malloc(strlen(path) + strlen(dir.d_name) + 2);
snprintf(new_path, 1024, "%s/%s", path, dir.d_name);
remove_dir(new_path);
free(new_path);
}
} while (res > 0);
sceIoDclose(dfd);
return sceIoRmdir(path);
} else {
return sceIoRemove(path);
}
}
void firmware_string(char string[8], unsigned int version) {
char a = (version >> 24) & 0xf;
char b = (version >> 20) & 0xf;
char c = (version >> 16) & 0xf;
char d = (version >> 12) & 0xf;
memset(string, 0, 8);
string[0] = '0' + a;
string[1] = '.';
string[2] = '0' + b;
string[3] = '0' + c;
string[4] = '\0';
if (d) {
string[4] = '0' + d;
string[5] = '\0';
}
}
void wait_confirm(const char *msg) {
printf(msg);
while (1) {
SceCtrlData pad;
sceCtrlPeekBufferPositive(0, &pad, 1);
if (pad.buttons & SCE_CTRL_CROSS) {
break;
} else if (pad.buttons & (SCE_CTRL_RTRIGGER | SCE_CTRL_R1)) {
ErrorExit(5000, "Exiting in 5 seconds.\n");
}
sceKernelDelayThread(10000);
}
sceKernelDelayThread(500 * 1000);
}
int main(int argc, char *argv[]) {
int res;
int bypass = 0;
psvDebugScreenInit();
sceKernelPowerLock(0);
printf("-- modoru v1.0\n");
printf(" by TheFloW\n\n");
if (sceIoDevctl("ux0:", 0x3001, NULL, 0, NULL, 0) == 0x80010030)
ErrorExit(10000, "Enable unsafe homebrew first before using this software.\n");
res = load_modoru_drivers();
if (res < 0)
ErrorExit(10000, "Error 0x%08X loading drivers.\n", res);
SceCtrlData pad;
modoru_ctrl_peek_buffer_positive(0, &pad, 1);
if (pad.buttons & (SCE_CTRL_LTRIGGER | SCE_CTRL_R1)) {
bypass = 1;
}
if (!bypass) {
if (scePowerGetBatteryLifePercent() < 50)
ErrorExit(10000, "Battery has to be at least at 50%%.\n");
res = modoru_detect_plugins();
if (res < 0) {
ErrorExit(10000, "Error 0x%08X detecting plugins.\n", res);
} else if (res != 0) {
ErrorExit(20000, "Disable all your plugins first before using this software.\n"
"If you have already disabled them, but still get this message,\n"
"reboot your device and launch this software again without\n"
"launching any other applications before (e.g. VitaShell\n"
"or Adrenaline).\n");
}
}
char header[0x80];
SceUID fd = sceIoOpen(PUP_PATH, SCE_O_RDONLY, 0);
if (fd < 0)
ErrorExit(10000, "Error 0x%08X opening %s.\n", fd, PUP_PATH);
sceIoRead(fd, header, sizeof(header));
sceIoClose(fd);
if (strncmp(header, "SCEUF", 5) != 0)
ErrorExit(10000, "Error invalid updater file.\n");
SceKernelFwInfo fwinfo;
fwinfo.size = sizeof(SceKernelFwInfo);
_vshSblGetSystemSwVersion(&fwinfo);
unsigned int current_version = (unsigned int)fwinfo.version;
unsigned int factory_version = modoru_get_factory_firmware();
unsigned int target_version = *(unsigned int *)(header + 0x10);
char current_fw[8], factory_fw[8], target_fw[8];
firmware_string(current_fw, current_version);
firmware_string(factory_fw, factory_version);
firmware_string(target_fw, target_version);
printf("Firmware information:\n");
printf(" - Current firmware: ");
psvDebugScreenSetTextColor(YELLOW);
printf("%s", current_fw);
psvDebugScreenSetTextColor(WHITE);
printf("\n");
printf(" - Factory firmware: ");
psvDebugScreenSetTextColor(YELLOW);
printf("%s", factory_fw);
psvDebugScreenSetTextColor(WHITE);
printf("\n");
printf(" - Target firmware: ");
psvDebugScreenSetTextColor(YELLOW);
printf("%s", target_fw);
psvDebugScreenSetTextColor(WHITE);
printf("\n\n");
if (target_version < factory_version)
ErrorExit(10000, "Error you cannot go lower than your factory firmware.");
if (target_version == current_version) {
printf("Do you want to reinstall firmware ");
psvDebugScreenSetTextColor(YELLOW);
printf("%s", current_fw);
psvDebugScreenSetTextColor(WHITE);
printf("?\n");
} else if (target_version < current_version) {
printf("Do you want to downgrade from firmware ");
psvDebugScreenSetTextColor(YELLOW);
printf("%s", current_fw);
psvDebugScreenSetTextColor(WHITE);
printf(" to firmware ");
psvDebugScreenSetTextColor(YELLOW);
printf("%s", target_fw);
psvDebugScreenSetTextColor(WHITE);
printf("?\n");
} else if (target_version > current_version) {
printf("Do you want to update from firmware ");
psvDebugScreenSetTextColor(YELLOW);
printf("%s", current_fw);
psvDebugScreenSetTextColor(WHITE);
printf(" to firmware ");
psvDebugScreenSetTextColor(YELLOW);
printf("%s", target_fw);
psvDebugScreenSetTextColor(WHITE);
printf("?\n");
}
wait_confirm("Press X to confirm, R to exit.\n\n");
printf("This software will make PERMANENT modifications to your Vita.\n"
"If anything goes wrong, there is NO RECOVERY (not even with a\n"
"hardware flasher). The creators provide this tool \"as is\", without\n"
"warranty of any kind, express or implied and cannot be held liable\n"
"for any damage done.\n\n");
if (!bypass) {
printf("Continues in 20 seconds.\n\n");
sceKernelDelayThread(20 * 1000 * 1000);
}
wait_confirm("Press X to accept these terms and start the installation,\n"
" R to not accept and exit.\n\n");
printf("Cleaning ud0:...");
remove_dir("ud0:");
sceIoMkdir("ud0:PSP2UPDATE", 0777);
printf("OK\n");
printf("Copying PSP2UPDAT.PUP to ud0:...");
res = copy(PUP_PATH, "ud0:PSP2UPDATE/PSP2UPDAT.PUP");
if (res < 0)
ErrorExit(10000, "Error 0x%08X copying PSP2UPDAT.PUP.\n", res);
printf("OK\n");
sceKernelDelayThread(500 * 1000);
printf("Extracting psp2swu.self...");
res = extract("ud0:PSP2UPDATE/PSP2UPDAT.PUP", "ud0:PSP2UPDATE/psp2swu.self");
if (res < 0)
ErrorExit(10000, "Error 0x%08X extracting psp2swu.self.\n", res);
printf("OK\n");
sceKernelDelayThread(500 * 1000);
printf("Removing ux0:id.dat...");
res = sceIoRemove("ux0:id.dat");
if (res < 0 && res != 0x80010002)
ErrorExit(10000, "Error 0x%08X deleting ux0:id.dat.\n", res);
printf("OK\n");
sceKernelDelayThread(500 * 1000);
printf("Starting SCE updater...\n");
sceKernelDelayThread(1 * 1000 * 1000);
sceKernelPowerUnlock(0);
res = modoru_patch_updater();
if (res < 0)
ErrorExit(10000, "Error 0x%08X patching updater.\n", res);
res = modoru_launch_updater();
if (res < 0)
goto err;
sceKernelDelayThread(10 * 1000 * 1000);
err:
modoru_release_updater_patches();
ErrorExit(10000, "Error 0x%08X starting SCE updater.\n", res);
return 0;
}