forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbgdl.cpp
More file actions
339 lines (292 loc) · 9.85 KB
/
Copy pathbgdl.cpp
File metadata and controls
339 lines (292 loc) · 9.85 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "bgdl.hpp"
#include "download.hpp"
#include "file.hpp"
#include "log.hpp"
#include <taihen.h>
#include <vitasdk.h>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <sys/syslimits.h>
// RE by @dots_tb with help from:
// @CelesteBlue123 @SilicaDevs @possvkey and the NPS Team
namespace
{
// offsets for DC0 structure
// 0x70 URL
// 0x870 icon path
// 0x970 title
// 0xCAA license path (the bgldl will copy and paste it to bgdlid folder)
typedef struct ipmi_download_param
{
int type[2];
char unk_0x08[0x68];
char url[0x800]; // size is 2048 ?
char icon_path[0x100];
char title[0x33A]; // size is 0x33A ?
char license_path[0x100];
char unk_0xDAA[0x16];
} ipmi_download_param;
typedef struct sce_ipmi_download_param
{
union
{
struct
{
uint32_t* ptr_to_dc0_ptr;
uint32_t* ptr_to_2e0_ptr;
uint32_t unk_1; // 2
uint32_t unk_2; //-1
uint32_t unk_3; // 0
ipmi_download_param* addr_DC0;
uint32_t sizeDC0;
} init;
struct
{
int32_t* result;
uint32_t unk_2; // 0
uint32_t unk_3; // 0
uint32_t unk_4; // 1
uint32_t unk_5; // 0
uint32_t unk_6;
uint32_t unk_7; // 0x00000A0A
} state;
};
void* addr_2E0;
uint32_t size2E0;
uint32_t unk_4;
uint32_t* pBgdlId; // points to -1
uint32_t unk_5; // 4
int32_t* result; // points to 1
uint32_t unk_4_2;
uint32_t shell_func_8;
} sce_ipmi_download_param;
typedef struct ipmi_download_param_state
{
int32_t* result;
uint32_t unk_2; // 0
uint32_t unk_3; // 0
uint32_t unk_4; // 1
uint32_t unk_5; // 0
uint32_t unk_6;
uint32_t unk_7; // 0x00000A0A
} ipmi_download_param_state;
typedef struct shellsvc_init_struct
{
uint32_t unk_0;
char name[0x10];
void* unk_ptr;
uint32_t unk_1; // 1
uint32_t size1; // 0x1E00
uint32_t size2; // 0x1E00
uint32_t unk_2; // 1
uint32_t unk_3; // 0x0F00
uint32_t unk_4; // 0x0F00
uint32_t unk_5; // 1
char padding[0x84];
uint32_t unk_7; // 2
uint32_t unk_8; // -1
void* unk_ptr_2;
char padding2[0x88]; // 0x14c
} shellsvc_init_struct;
typedef struct scedownload_class_header
{
uint32_t unk0;
uint32_t unk1;
uint32_t unk2;
uint32_t** func_table;
uint32_t unk3;
uint32_t* bufC4;
uint32_t* buf10000;
} scedownload_class_header;
typedef int (*SceDownloadInit)(
uint32_t** ipmi_sce_download_ptr,
void* ipmi_sce_download_ptr_deref,
int unk_1,
shellsvc_init_struct* bufc8,
int unk_2);
typedef int (*SceDownloadChangeState)(
uint32_t** ipmi_sce_download_ptr,
int cmd,
void* ptr_to_dc0_ptr,
int unk_1,
sce_ipmi_download_param r5);
typedef struct scedownload_class
{
shellsvc_init_struct init_header;
scedownload_class_header* class_header;
SceDownloadInit init;
SceDownloadChangeState change_state;
} scedownload_class;
// sceIpmiCreateDownloadTask?
int (*SceIpmi_4E255C31)(const char* name, int unk);
// sceIpmiInitDownloadTask?
int (*SceIpmi_B282B430)(
uint32_t*** func_table,
const char* name,
scedownload_class_header* class_header,
uint32_t* buf10000);
void init_download_class(scedownload_class* sceDownloadObj)
{
memset(sceDownloadObj, 0, sizeof(scedownload_class));
strncpy(sceDownloadObj->init_header.name, "SceDownload", 0x10);
sceDownloadObj->init_header.unk_1 = 1;
sceDownloadObj->init_header.size1 = 0x1E00;
sceDownloadObj->init_header.size2 = 0x1E00;
sceDownloadObj->init_header.unk_2 = 1;
sceDownloadObj->init_header.unk_3 = 0x0F00;
sceDownloadObj->init_header.unk_4 = 0x0F00;
sceDownloadObj->init_header.unk_5 = 1;
sceDownloadObj->init_header.unk_7 = 2;
sceDownloadObj->init_header.unk_8 = -1;
int res = SceIpmi_4E255C31(
(char*)&(sceDownloadObj->init_header.name), 0x1E00);
if (res != 0xc4)
throw formatEx<std::runtime_error>(
"SceIpmi_4E255C31 failed: {:#08x}", static_cast<uint32_t>(res));
sceDownloadObj->class_header = (scedownload_class_header*)new char[0x18]();
sceDownloadObj->class_header->bufC4 = (uint32_t*)new char[res]();
sceDownloadObj->class_header->buf10000 = (uint32_t*)new char[0x1000]();
res = SceIpmi_B282B430(
&(sceDownloadObj->class_header->func_table),
(char*)&(sceDownloadObj->init_header.name),
sceDownloadObj->class_header,
sceDownloadObj->class_header->buf10000);
if (res != 0)
throw formatEx<std::runtime_error>(
"SceIpmi_B282B430 init failed: {:#08x}",
static_cast<uint32_t>(res));
sceDownloadObj->init =
(SceDownloadInit)(*(sceDownloadObj->class_header->func_table))[1];
sceDownloadObj->change_state = (SceDownloadChangeState)(*(
sceDownloadObj->class_header->func_table))[5];
res = sceDownloadObj->init(
sceDownloadObj->class_header->func_table,
*(sceDownloadObj->class_header->func_table),
0x14,
&(sceDownloadObj->init_header),
2);
if (res != 0)
throw formatEx<std::runtime_error>(
"SceDownload init failed: {:#08x}", static_cast<uint32_t>(res));
}
void scedownload_start_with_rif(
scedownload_class* sceDownloadObj,
const char* title,
const char* url,
const char* rif,
int type)
{
int32_t result = 0;
int32_t bgdlid = 1;
sce_ipmi_download_param params;
memset(¶ms, 0, sizeof(params));
params.init.ptr_to_dc0_ptr = (uint32_t*)¶ms.init.addr_DC0;
params.init.ptr_to_2e0_ptr = (uint32_t*)¶ms.addr_2E0;
params.init.unk_1 = 2;
params.init.unk_2 = -1;
params.init.unk_3 = 0;
params.init.sizeDC0 = 0xDC0;
std::vector<uint8_t> buf_dc0(params.init.sizeDC0);
params.init.addr_DC0 = (ipmi_download_param*)buf_dc0.data();
params.size2E0 = 0x2E0;
std::vector<uint8_t> buf_2e0(params.size2E0);
params.addr_2E0 = buf_2e0.data();
params.pBgdlId = (uint32_t*)&bgdlid; // points to -1
params.unk_5 = 4;
params.result = &result;
params.shell_func_8 = (*(sceDownloadObj->class_header->func_table))[8];
strcpy((char*)params.init.addr_DC0->url, url);
strcpy((char*)params.init.addr_DC0->license_path, rif);
strcpy((char*)params.init.addr_DC0->title, title);
strcpy((char*)params.init.addr_DC0->icon_path, "ux0:bgdl/icon0.png");
params.init.addr_DC0->type[0] = params.init.addr_DC0->type[1] = type;
int res = sceDownloadObj->change_state(
sceDownloadObj->class_header->func_table,
0x12340012,
params.init.ptr_to_dc0_ptr,
1,
params);
if (res < 0)
throw formatEx<std::runtime_error>(
"SceDownload change_state failed: {:#08x}",
static_cast<uint32_t>(res));
if (result < 0)
throw formatEx<std::runtime_error>(
"SceDownload change_state result failed: {:#08x}",
static_cast<uint32_t>(result));
if (bgdlid < 0)
throw formatEx<std::runtime_error>(
"SceDownload change_state bgdlid failed: {:#08x}",
static_cast<uint32_t>(res));
if (reinterpret_cast<intptr_t>(params.init.addr_DC0) < 0)
throw formatEx<std::runtime_error>(
"SceDownload change_state DC0 failed: {:#08x}",
reinterpret_cast<uintptr_t>(params.init.addr_DC0));
result = 0;
memset(¶ms, 0, sizeof(params));
params.state.result = &result;
params.state.unk_4 = 1;
params.state.unk_7 = 0x00000A0A;
res = sceDownloadObj->change_state(
sceDownloadObj->class_header->func_table, 0x12340007, 0, 0, params);
if (res < 0)
throw formatEx<std::runtime_error>(
"SceDownload second change_state failed: {:#08x}",
static_cast<uint32_t>(res));
if (result < 0)
throw formatEx<std::runtime_error>(
"SceDownload second change_state result failed: {:#08x}",
static_cast<uint32_t>(result));
buf_dc0.clear();
buf_2e0.clear();
}
std::unique_ptr<scedownload_class> new_scedownload()
{
char lib_path[] = "vs0:sys/external/libshellsvc.suprx";
sceKernelLoadStartModule(lib_path, 0, NULL, 0, NULL, NULL);
taiGetModuleExportFunc(
"SceShellSvc",
0xF4E34EDB,
0x4E255C31,
(uintptr_t*)&SceIpmi_4E255C31);
taiGetModuleExportFunc(
"SceShellSvc",
0xF4E34EDB,
0xB282B430,
(uintptr_t*)&SceIpmi_B282B430);
auto example_class = std::make_unique<scedownload_class>();
init_download_class(example_class.get());
return example_class;
}
}
void pkgi_start_bgdl(
const int type,
const std::string& title,
const std::string& url,
const std::vector<uint8_t>& rif)
{
if (pkgi_list_dir_contents("ux0:bgdl/t").size() >= 32)
throw std::runtime_error(
"There are too many pending installation on your device, "
"install them from LiveArea's notifications or delete them to "
"be able to download more.");
static auto example_class = new_scedownload();
std::string license_path = "ux0:bgdl/temp.dat";
int rif_size = rif.size();
if(rif_size >= PKGI_PSM_RIF_SIZE) {
if (type == BgdlTypePsp)
rif_size = PKGI_PSP_RIF_SIZE;
else if(type == BgdlTypePsm)
rif_size = PKGI_PSM_RIF_SIZE;
else
rif_size = PKGI_RIF_SIZE;
}
pkgi_save(license_path, rif.data(), rif_size);
scedownload_start_with_rif(
example_class.get(),
title.c_str(),
url.c_str(),
(rif_size > 0) ? license_path.c_str() : "",
type);
}