forked from cnsldv/CardUnlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
140 lines (119 loc) · 3.95 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
#include <stdio.h>
#include <psp2/ctrl.h>
#include <psp2/io/fcntl.h>
#include <psp2/power.h>
#include "debugScreen.h"
#define printf(...) psvDebugScreenPrintf(__VA_ARGS__)
#define MOUNT_ID (0xE00)
int vshIoUmount(int id, int a2, int a3, int a4);
int _vshIoMount(int id, const char *path, int permission, void *buf);
int vshIoMount(int id, const char *path, int permission, int a4, int a5, int a6);
unsigned int wait_keys(unsigned int key_mask);
int vshIoMount(int id, const char *path, int permission, int a4, int a5, int a6) {
uint32_t buf[6];
buf[0] = a4;
buf[1] = a5;
buf[2] = a6;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
return _vshIoMount(id, path, permission, buf);
}
int main(int argc, char *argv[])
{
int ret;
SceUID fd;
psvDebugScreenInit();
psvDebugScreenSetFgColor(COLOR_WHITE);
printf("CardUnlock v1.1 by cnsldv (Contributions By Hexxellor)\n");
printf("======================================================\n");
printf("NOTE: This application will attempt to rename the\n");
printf("id.dat file on your memory card to id.bak. This will\n");
printf("allow you to preserve the original in case you want to\n");
printf("retrieve any information from it.\n");
printf("======================================================\n");
printf("\n");
psvDebugScreenSetFgColor(COLOR_RED);
printf("WARNING: Use of this program is at your own risk!\n");
printf(" If a backup file (id.bak) exists already\n");
printf(" it will be removed so backup the file\n");
printf(" if you need to keep it!\n");
psvDebugScreenSetFgColor(COLOR_WHITE);
printf("\n");
printf("Insert card now, select \"No\" when asked to reboot\n");
printf("Once card is inserted press X to continue\n");
wait_keys(SCE_CTRL_CROSS);
printf("Mounting card\n");
ret = vshIoMount(MOUNT_ID, NULL, 0, 0, 0, 0);
if (ret != 0) {
psvDebugScreenSetFgColor(COLOR_RED);
printf("Unable to mount card err=0x%08X\n", ret);
psvDebugScreenSetFgColor(COLOR_WHITE);
printf("Press X to exit\n");
wait_keys(SCE_CTRL_CROSS);
return 0;
}
// Remove the old id.bak file if it exists
printf("Checking for existing id.bak file...\n");
fd = sceIoOpen("xmc0:id.bak", SCE_O_RDONLY, 0);
if (fd >= 0) {
sceIoClose(fd);
printf("Removing old id.bak file\n");
ret = sceIoRemove("xmc0:id.bak");
if (ret != 0) {
psvDebugScreenSetFgColor(COLOR_RED);
psvDebugScreenPrintf("Unable to remove id.bak err=0x%08X\n", ret);
psvDebugScreenSetFgColor(COLOR_WHITE);
}
}
// Rename the id.dat file to id.bak so we can retain the information in it.
// NOTE: Technically if the previous attempt to remove an existing id.bak fails
// proceeding with this is sort of stupid. *shrug* Lots of checking could be
// added. Don't care. :)
printf("Renaming current id.dat to id.bak\n");
fd = sceIoOpen("xmc0:id.dat", SCE_O_RDONLY, 0);
if (fd >= 0) {
sceIoClose(fd);
ret = sceIoRename("xmc0:id.dat", "xmc0:id.bak");
if (ret != 0) {
psvDebugScreenSetFgColor(COLOR_RED);
psvDebugScreenPrintf("Unable to rename id.dat err=0x%08X\n", ret);
psvDebugScreenSetFgColor(COLOR_WHITE);
}
}
printf("Unmounting card\n");
ret = vshIoUmount(MOUNT_ID, 0, 0, 0);
if (ret != 0) {
psvDebugScreenSetFgColor(COLOR_RED);
printf("Unable to unmount card err=0x%08X\n", ret);
psvDebugScreenSetFgColor(COLOR_WHITE);
printf("Press X to exit\n");
wait_keys(SCE_CTRL_CROSS);
return 0;
}
psvDebugScreenSetFgColor(COLOR_GREEN);
printf("Done\n");
psvDebugScreenSetFgColor(COLOR_WHITE);
printf("Press X to reboot or O to exit\n");
if (wait_keys(SCE_CTRL_CROSS | SCE_CTRL_CIRCLE) == SCE_CTRL_CROSS) {
scePowerRequestColdReset();
}
return 0;
}
unsigned int wait_keys(unsigned int key_mask)
{
SceCtrlData pad;
unsigned int k;
do {
sceCtrlReadBufferPositive(0, &pad, 1);
sceKernelDelayThreadCB(200 * 1000);
} while (pad.buttons != 0);
while (1) {
sceCtrlReadBufferPositive(0, &pad, 1);
k = pad.buttons & key_mask;
if (k != 0) {
return k;
}
sceKernelDelayThreadCB(200 * 1000);
}
}