forked from isage/VitaShell
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpbp.c
More file actions
executable file
·319 lines (240 loc) · 9.56 KB
/
pbp.c
File metadata and controls
executable file
·319 lines (240 loc) · 9.56 KB
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
/*
VitaShell
Copyright (C) 2015-2018, TheFloW
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <psp2/vshbridge.h>
#include "sha256.h"
#include "main.h"
#include "pbp.h"
#include "sfo.h"
int read_content_id_data_psp(SceUID pbp_fd, char* content_id) {
DataPspHeader data_psp_header;
// read data.psp header
int read_sz = sceIoRead(pbp_fd, &data_psp_header, sizeof(DataPspHeader));
if(read_sz < sizeof(DataPspHeader)) return 0;
// copy the content id from data.psp header to this content_id buffer
strncpy(content_id, data_psp_header.content_id, 0x30);
return (strlen(content_id) == 36);
}
int read_content_id_npumdimg(SceUID pbp_fd, char* content_id) {
NpUmdImgHeader npumdimg_header;
// read npumd header
int read_sz = sceIoRead(pbp_fd, &npumdimg_header, sizeof(NpUmdImgHeader));
if(read_sz < sizeof(NpUmdImgHeader)) return 0;
// copy the content id from npumdimg_header to this content_id buffer
strncpy(content_id, npumdimg_header.content_id, 0x30);
return (strlen(content_id) == 36);
}
int is_psx_signed(SceUID pbp_fd, int data_psar_offset, int pbp_type) { // check psx eboot is signed.
char pgd_magic[0x4]; // this is to filter out psx2psp eboots, and other such tools.
// which could never work from the vita's LiveSpace.
// seek to iso header
if(pbp_type == PBP_TYPE_PSISOIMG)
sceIoLseek(pbp_fd, data_psar_offset+0x400, SCE_SEEK_SET);
else
sceIoLseek(pbp_fd, data_psar_offset+0x200, SCE_SEEK_SET);
// read magic
int read_len = sceIoRead(pbp_fd, pgd_magic, sizeof(pgd_magic));
if(read_len < sizeof(pgd_magic))
return PBP_TYPE_UNKNOWN;
// if is not PGD, then it is not a signed PSX PBP.
if(memcmp(pgd_magic, "\0PGD", sizeof(pgd_magic)) == 0)
return pbp_type;
else
return PBP_TYPE_UNKNOWN;
}
int determine_pbp_type(SceUID pbp_fd, PbpHeader* pbp_header) {
char data_psar_magic[0x8];
int read_sz = sceIoRead(pbp_fd, pbp_header, sizeof(PbpHeader));
if(read_sz < sizeof(PbpHeader)) return 0;
// seek to data.psar
sceIoLseek(pbp_fd, pbp_header->data_psar_ptr, SCE_SEEK_SET);
// read magic value to determine pbp type
read_sz = sceIoRead(pbp_fd, data_psar_magic, sizeof(data_psar_magic));
if(read_sz < sizeof(data_psar_magic)) return 0;
if(memcmp(data_psar_magic, "NPUMDIMG", 0x8) == 0) { // psp
return PBP_TYPE_NPUMDIMG;
}
else if(memcmp(data_psar_magic, "PSISOIMG", 0x8) == 0) { // ps1 single disc
return is_psx_signed(pbp_fd, pbp_header->data_psar_ptr, PBP_TYPE_PSISOIMG);
}
else if(memcmp(data_psar_magic, "PSTITLEI", 0x8) == 0) { // ps1 multi disc
return is_psx_signed(pbp_fd, pbp_header->data_psar_ptr, PBP_TYPE_PSTITLEIMG);
}
else{ // update package, homebrew, etc,
return PBP_TYPE_UNKNOWN;
}
if(read_sz < sizeof(PbpHeader)) return PBP_TYPE_UNKNOWN;
}
int read_data_psar_header(SceUID pbp_fd, char* content_id) {
PbpHeader pbp_header;
int pbp_type = determine_pbp_type(pbp_fd, &pbp_header);
if(pbp_type == PBP_TYPE_NPUMDIMG) {
// seek to start of npumdimg
sceIoLseek(pbp_fd, pbp_header.data_psar_ptr, SCE_SEEK_SET);
// read content_id from npumdimg
return read_content_id_npumdimg(pbp_fd, content_id);
}
else if(pbp_type == PBP_TYPE_PSISOIMG || pbp_type == PBP_TYPE_PSTITLEIMG) {
// seek to start of data.psp
sceIoLseek(pbp_fd, pbp_header.data_psp_ptr, SCE_SEEK_SET);
// read content_id from data.psp
return read_content_id_data_psp(pbp_fd, content_id);
}
else {
return 0;
}
}
void get_sce_discinfo_sig(char* sce_discinfo, char* disc_id) {
memset(sce_discinfo, 0x00, 0x100);
SceUID discinfo_fd = sceIoOpen("vs0:app/NPXS10028/__sce_discinfo", SCE_O_RDONLY, 0);
if(discinfo_fd < 0)
return;
int read_size = 0;
do{
read_size = sceIoRead(discinfo_fd, sce_discinfo, 0x100);
if(strncmp(sce_discinfo, disc_id, 0x9) == 0) {
break;
}
} while(read_size >= 0x100);
sceIoClose(discinfo_fd);
}
int read_sfo(SceUID pbp_fd, void** param_sfo_buffer){
PbpHeader pbp_header;
// read pbp header
int read_sz = sceIoRead(pbp_fd, &pbp_header, sizeof(PbpHeader));
if(read_sz < sizeof(PbpHeader)) return 0;
// get sfo size
int param_sfo_size = pbp_header.icon0_png_ptr - pbp_header.param_sfo_ptr;
if(param_sfo_size <= 0) return 0;
// allocate a buffer for the param.sfo file
*param_sfo_buffer = malloc(param_sfo_size);
if(*param_sfo_buffer == NULL) return 0;
// seek to the start of param.sfo
sceIoLseek(pbp_fd, pbp_header.param_sfo_ptr, SCE_SEEK_SET);
// read the param.sfo file
read_sz = sceIoRead(pbp_fd, *param_sfo_buffer, param_sfo_size);
if(read_sz < param_sfo_size) {
free(*param_sfo_buffer);
*param_sfo_buffer = NULL;
return 0;
}
return param_sfo_size;
}
int hash_pbp(SceUID pbp_fd, unsigned char* out_hash) {
unsigned char wbuf[0x7c0];
// seek to the start of the eboot.pbp
sceIoLseek(pbp_fd, 0x00, SCE_SEEK_SET);
// inital read
int read_sz = sceIoRead(pbp_fd, wbuf, sizeof(wbuf));
if(read_sz < sizeof(PbpHeader)) return read_sz;
// calculate data hash size
size_t hash_sz = (((PbpHeader*)wbuf)->data_psar_ptr + 0x1C0000);
// initalize hash
SHA256_CTX ctx;
sha256_init(&ctx);
// first hash
sha256_update(&ctx, wbuf, read_sz);
size_t total_hashed = read_sz;
do {
read_sz = sceIoRead(pbp_fd, wbuf, sizeof(wbuf));
if((total_hashed + read_sz) > hash_sz)
read_sz = (hash_sz - total_hashed); // calculate remaining
sha256_update(&ctx, wbuf, read_sz);
total_hashed += read_sz;
if(read_sz < sizeof(wbuf)) // treat EOF as complete
total_hashed = hash_sz;
} while(total_hashed < hash_sz);
sha256_final(&ctx, out_hash);
return 1;
}
int get_pbp_sfo(const char* pbp_file, void** param_sfo_buffer) {
PbpHeader pbp_header;
if(param_sfo_buffer == NULL) return 0;
*param_sfo_buffer = NULL;
int res = 0;
if(pbp_file != NULL) {
SceUID pbp_fd = sceIoOpen(pbp_file, SCE_O_RDONLY, 0);
if(pbp_fd < 0) return 0;
// read param.sfo from pbp
res = read_sfo(pbp_fd, param_sfo_buffer);
sceIoClose(pbp_fd);
}
return res;
}
int get_pbp_type(const char* pbp_file) {
int res = 0;
PbpHeader pbp_header;
if(pbp_file != NULL) {
SceUID pbp_fd = sceIoOpen(pbp_file, SCE_O_RDONLY, 0);
if(pbp_fd < 0)
return 0;
res = determine_pbp_type(pbp_fd, &pbp_header);
sceIoClose(pbp_fd);
}
return res;
}
int get_pbp_content_id(const char* pbp_file, char* content_id) {
int res = 0;
if(pbp_file != NULL && content_id != NULL) {
SceUID pbp_fd = sceIoOpen(pbp_file, SCE_O_RDONLY, 0);
if(pbp_fd < 0)
return 0;
res = read_data_psar_header(pbp_fd, content_id);
// check the content id is valid
if(res) {
int content_id_len = strnlen(content_id, 0x30);
if(content_id_len != 0x24) res = 0;
}
sceIoClose(pbp_fd);
}
return res;
}
int gen_sce_ebootpbp(const char* psp_game_folder, char* disc_id) {
int res = 0;
unsigned char pbp_hash[0x20];
char sce_ebootpbp[0x200];
char sce_discinfo[0x100];
int sw_version = 0;
PbpHeader pbp_header;
if(psp_game_folder != NULL) {
char ebootpbp_path[MAX_PATH_LENGTH];
char sce_ebootpbp_path[MAX_PATH_LENGTH];
snprintf(ebootpbp_path, MAX_PATH_LENGTH, "%s/EBOOT.PBP", psp_game_folder);
snprintf(sce_ebootpbp_path, MAX_PATH_LENGTH, "%s/__sce_ebootpbp", psp_game_folder);
memset(pbp_hash, 0x00, sizeof(pbp_hash));
memset(sce_ebootpbp, 0x00, sizeof(pbp_hash));
SceUID pbp_fd = sceIoOpen(ebootpbp_path, SCE_O_RDONLY, 0);
if(pbp_fd < 0)
return res;
int pbp_type = determine_pbp_type(pbp_fd, &pbp_header); // determine pbp header
if(pbp_type == PBP_TYPE_PSISOIMG || pbp_type == PBP_TYPE_NPUMDIMG)
res = hash_pbp(pbp_fd, pbp_hash); // hash eboot.pbp
if(pbp_type == PBP_TYPE_PSTITLEIMG)
get_sce_discinfo_sig(sce_discinfo, disc_id); // read sce_discinfo
sceIoClose(pbp_fd);
if(pbp_type == PBP_TYPE_UNKNOWN)
return res;
// actually generate the __sce_ebootpbp
if(pbp_type == PBP_TYPE_NPUMDIMG)
res = _vshNpDrmEbootSigGenPsp(ebootpbp_path, pbp_hash, sce_ebootpbp, &sw_version);
else if(pbp_type == PBP_TYPE_PSISOIMG)
res = _vshNpDrmEbootSigGenPs1(ebootpbp_path, pbp_hash, sce_ebootpbp, &sw_version);
else if(pbp_type == PBP_TYPE_PSTITLEIMG)
res = _vshNpDrmEbootSigGenMultiDisc(ebootpbp_path, sce_discinfo, sce_ebootpbp, &sw_version);
if(res >= 0) { // write __sce_ebootpbp
res = WriteFile(sce_ebootpbp_path, sce_ebootpbp, 0x200);
}
}
return res;
}