Skip to content

Commit 626c73d

Browse files
authored
refactor (#195)
* refactor * Update sd.c * Update sd.h
1 parent 3fb3cc7 commit 626c73d

2 files changed

Lines changed: 32 additions & 49 deletions

File tree

include/sd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct {
2929

3030
int loadPrivLibs(void);
3131
int generateSealedKey(uint8_t data[ENC_SEALEDKEY_LEN]);
32-
int decryptSealedKey(uint8_t enc_key[ENC_SEALEDKEY_LEN], uint8_t dec_key[DEC_SEALEDKEY_LEN]);
32+
int decryptSealedKey(const uint8_t enc_key[ENC_SEALEDKEY_LEN], uint8_t dec_key[DEC_SEALEDKEY_LEN]);
3333
int decryptSealedKeyAtPath(const char *keyPath, uint8_t decryptedSealedKey[DEC_SEALEDKEY_LEN]);
3434
int createSave(const char *folder, const char *saveName, int blocks);
3535
int mountSave(const char *folder, const char *saveName, const char *mountPath);
@@ -40,4 +40,4 @@ uint16_t getMaxKeySet(void);
4040
}
4141
#endif
4242

43-
#endif // SD_H
43+
#endif // SD_H

source/sd.c

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,7 @@ int (*sceFsInitMountSaveDataOpt)(MountSaveDataOpt *opt);
1818
int (*sceFsMountSaveData)(MountSaveDataOpt *opt, const char *volumePath, const char *mountPath, uint8_t decryptedSealedKey[DEC_SEALEDKEY_LEN]);
1919
int (*sceFsInitUmountSaveDataOpt)(UmountSaveDataOpt *opt);
2020
int (*sceFsUmountSaveData)(UmountSaveDataOpt *opt, const char *mountPath, int handle, bool ignoreErrors);
21-
void (*statfs)();
22-
23-
24-
static int sys_open(const char *path, int flags, int mode) {
25-
int result;
26-
int err;
27-
28-
asm volatile(
29-
".intel_syntax;"
30-
"mov rax, 5;" // System call number for open: 5
31-
"syscall;" // Invoke syscall
32-
: "=a" (result), // Output operand: result
33-
"=@ccc" (err) // Output operand: err (clobbers condition codes)
34-
);
35-
36-
UNUSED(path);
37-
UNUSED(flags);
38-
UNUSED(mode);
39-
40-
return result;
41-
}
21+
void (*statfs)(void);
4222

4323
// must be loaded upon setup
4424
int loadPrivLibs(void) {
@@ -57,7 +37,7 @@ int loadPrivLibs(void) {
5737
}
5838
else {
5939
LOG("Failed to load libSceFsInternalForVsh.sprx");
60-
return -2;
40+
return -1;
6141
}
6242

6343
kernel_sys = sceKernelLoadStartModule("/system/common/lib/libkernel_sys.sprx", 0, NULL, 0, NULL, NULL);
@@ -66,7 +46,7 @@ int loadPrivLibs(void) {
6646
}
6747
else {
6848
LOG("Failed to load libkernel_sys.sprx");
69-
return -4;
49+
return -2;
7050
}
7151

7252
return 0;
@@ -78,10 +58,10 @@ int generateSealedKey(uint8_t data[ENC_SEALEDKEY_LEN]) {
7858
int fd;
7959

8060
UNUSED(dummy);
81-
8261
memset(sealedKey, 0, sizeof(sealedKey));
8362

84-
if ((fd = open("/dev/sbl_srv", O_RDWR)) == -1) {
63+
fd = open("/dev/sbl_srv", O_RDWR);
64+
if (fd == -1) {
8565
LOG("sbl_srv open fail!");
8666
return -1;
8767
}
@@ -97,16 +77,16 @@ int generateSealedKey(uint8_t data[ENC_SEALEDKEY_LEN]) {
9777
return 0;
9878
}
9979

100-
int decryptSealedKey(uint8_t enc_key[ENC_SEALEDKEY_LEN], uint8_t dec_key[DEC_SEALEDKEY_LEN]) {
80+
int decryptSealedKey(const uint8_t enc_key[ENC_SEALEDKEY_LEN], uint8_t dec_key[DEC_SEALEDKEY_LEN]) {
10181
uint8_t dummy[0x10];
10282
uint8_t data[ENC_SEALEDKEY_LEN + DEC_SEALEDKEY_LEN];
10383
int fd;
10484

105-
memset(data, 0, sizeof(data));
106-
10785
UNUSED(dummy);
86+
memset(data, 0, sizeof(data));
10887

109-
if ((fd = open("/dev/sbl_srv", O_RDWR)) == -1) {
88+
fd = open("/dev/sbl_srv", O_RDWR);
89+
if (fd == -1) {
11090
LOG("sbl_srv open fail!");
11191
return -1;
11292
}
@@ -119,16 +99,17 @@ int decryptSealedKey(uint8_t enc_key[ENC_SEALEDKEY_LEN], uint8_t dec_key[DEC_SEA
11999
}
120100

121101
memcpy(dec_key, &data[ENC_SEALEDKEY_LEN], DEC_SEALEDKEY_LEN);
122-
123102
close(fd);
103+
124104
return 0;
125105
}
126106

127107
int decryptSealedKeyAtPath(const char *keyPath, uint8_t decryptedSealedKey[DEC_SEALEDKEY_LEN]) {
128108
uint8_t sealedKey[ENC_SEALEDKEY_LEN];
129109
int fd;
130110

131-
if ((fd = sys_open(keyPath, O_RDONLY, 0)) == -1) {
111+
fd = open(keyPath, O_RDONLY);
112+
if (fd == -1) {
132113
return -1;
133114
}
134115

@@ -153,7 +134,7 @@ int createSave(const char *volumePath, const char *volumeKeyPath, int blocks) {
153134
CreatePfsSaveDataOpt opt;
154135

155136
memset(&opt, 0, sizeof(CreatePfsSaveDataOpt));
156-
137+
157138
// generate a key
158139
if (generateSealedKey(sealedKey) != 0) {
159140
return -1;
@@ -164,30 +145,30 @@ int createSave(const char *volumePath, const char *volumeKeyPath, int blocks) {
164145
return -2;
165146
}
166147

167-
fd = sys_open(volumeKeyPath, O_CREAT | O_TRUNC | O_WRONLY, 0777);
148+
fd = sceKernelOpen(volumeKeyPath, O_CREAT | O_TRUNC | O_WRONLY, 0777);
168149
if (fd == -1) {
169150
return -3;
170151
}
171152

172153
// write sealed key
173-
if (write(fd, sealedKey, sizeof(sealedKey)) != sizeof(sealedKey)) {
174-
close(fd);
154+
if (sceKernelWrite(fd, sealedKey, sizeof(sealedKey)) != sizeof(sealedKey)) {
155+
sceKernelClose(fd);
175156
return -4;
176157
}
177-
close(fd);
158+
sceKernelClose(fd);
178159

179-
fd = sys_open(volumePath, O_CREAT | O_TRUNC | O_WRONLY, 0777);
160+
fd = sceKernelOpen(volumePath, O_CREAT | O_TRUNC | O_WRONLY, 0777);
180161
if (fd == -1) {
181162
return -5;
182163
}
183164

184165
volumeSize = blocks << 15;
185166

186167
if (sceFsUfsAllocateSaveData(fd, volumeSize, 0 << 7, 0) < 0) {
187-
close(fd);
168+
sceKernelClose(fd);
188169
return -6;
189170
}
190-
close(fd);
171+
sceKernelClose(fd);
191172

192173
if (sceFsInitCreatePfsSaveDataOpt(&opt) < 0) {
193174
return -7;
@@ -198,29 +179,30 @@ int createSave(const char *volumePath, const char *volumeKeyPath, int blocks) {
198179
}
199180

200181
// finalize
201-
fd = sys_open(volumePath, O_RDONLY, 0);
202-
fsync(fd);
203-
close(fd);
182+
fd = sceKernelOpen(volumePath, O_RDONLY, 0);
183+
sceKernelFsync(fd);
184+
sceKernelClose(fd);
204185

205186
return 0;
206187
}
207188

208189
int mountSave(const char *volumePath, const char *volumeKeyPath, const char *mountPath) {
209-
char bid[] = "system";
210190
int ret;
211191
uint8_t decryptedSealedKey[DEC_SEALEDKEY_LEN];
212192
MountSaveDataOpt opt;
213193

214194
memset(&opt, 0, sizeof(MountSaveDataOpt));
215195

216-
if ((ret = decryptSealedKeyAtPath(volumeKeyPath, decryptedSealedKey)) < 0) {
196+
ret = decryptSealedKeyAtPath(volumeKeyPath, decryptedSealedKey);
197+
if (ret < 0) {
217198
return ret;
218199
}
219200

220201
sceFsInitMountSaveDataOpt(&opt);
221-
opt.budgetid = bid;
202+
opt.budgetid = "system";
222203

223-
if ((ret = sceFsMountSaveData(&opt, volumePath, mountPath, decryptedSealedKey)) < 0) {
204+
ret = sceFsMountSaveData(&opt, volumePath, mountPath, decryptedSealedKey);
205+
if (ret < 0) {
224206
return ret;
225207
}
226208

@@ -229,6 +211,7 @@ int mountSave(const char *volumePath, const char *volumeKeyPath, const char *mou
229211

230212
int umountSave(const char *mountPath, int handle, bool ignoreErrors) {
231213
UmountSaveDataOpt opt;
214+
232215
memset(&opt, 0, sizeof(UmountSaveDataOpt));
233216
sceFsInitUmountSaveDataOpt(&opt);
234217
return sceFsUmountSaveData(&opt, mountPath, handle, ignoreErrors);
@@ -248,4 +231,4 @@ uint16_t getMaxKeySet(void) {
248231

249232
maxKeyset = (sampleSealedKey[9] << 8) + sampleSealedKey[8];
250233
return maxKeyset;
251-
}
234+
}

0 commit comments

Comments
 (0)