-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathUtility.cpp
More file actions
310 lines (251 loc) · 7.38 KB
/
Utility.cpp
File metadata and controls
310 lines (251 loc) · 7.38 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
//
// Created by cpasjuste on 14/01/16.
//
#include <string.h>
#include <stdio.h>
#include "Utility.h"
#include "SuperUserLib3DS/libsu.h"
#include "libmd5-rfc/md5.h"
#define BUFSIZE 131072
static FS_Archive sdmcArchive;
extern void _gfxInit();
extern u8 isNew3DS;
extern "C" {
void patchServiceAccess();
void svchax_init();
}
static const u32 titleTypes[7] = {
0x00040138, // System Firmware
0x00040130, // System Modules
0x00040030, // Applets
0x00040010, // System Applications
0x0004001B, // System Data Archives
0x0004009B, // System Data Archives (Shared Archives)
0x000400DB, // System Data Archives
};
static const u64 titleHomeMenu[4] = {
0x0004003000008F02, // USA Home Menu
0x0004003000009802, // EUR Home Menu
0x0004003000008202, // JPN Home Menu
0x000400300000A902, // KOR Home Menu
};
static const u64 titleBrowser[8] = {
0x0004003020009402, // USA New3DS Internet Browser
0x0004003020009D02, // EUR New3DS Internet Browser
0x0004003020008802, // JPN New3DS Internet Browser
0x000400302000AE02, // KOR New3DS Internet Browser
0x0004003000009402, // USA Internet Browser
0x0004003000009D02, // EUR Internet Browser
0x0004003000008802, // JPN Internet Browser
0x000400300000AE02, // KOR Internet Browser
};
u32 Utility::getTitlePriority(u64 id) {
// nfirm last
if( id == 0x0004013800000002LL
|| id == 0x0004013820000002LL) {
return 0;
}
// downgrade browser and homemenu last
for (u32 i = 0; i < 8; i++) {
if (id == titleBrowser[i]) {
return 2;
}
}
for (u32 i = 0; i < 4; i++) {
if (id == titleHomeMenu[i]) {
return 1;
}
}
u32 type = (u32) (id >> 32);
for (u32 i = 0; i < 7; i++) {
if (type == titleTypes[i]) {
return i+3;
}
}
return 0;
}
bool Utility::sortTitles(const TitleInfo &a, const TitleInfo &b) {
bool aSafe = (a.titleID & 0xFF) == 0x03;
bool bSafe = (b.titleID & 0xFF) == 0x03;
if (aSafe != bSafe) {
return aSafe;
}
return getTitlePriority(a.titleID) > getTitlePriority(b.titleID);
}
int Utility::cmp(std::vector<TitleInfo> &titles, u64 &titleID, u16 version) {
for (auto it : titles) {
if (it.titleID == titleID) {
return (version - it.version);
}
}
return 1;
}
int Utility::version(std::vector<TitleInfo> &titles, u64 &titleID) {
for (auto it : titles) {
if (it.titleID == titleID) {
return it.version;
}
}
return 0;
}
std::vector<TitleInfo> Utility::getTitles() {
std::vector<TitleInfo> titles;
u32 count = 0;
if (AM_GetTitleCount(MEDIATYPE_NAND, &count)) {
return titles;
}
u64 titlesId[count];
if (AM_GetTitleIdList(MEDIATYPE_NAND, count, titlesId)) {
return titles;
}
AM_TitleEntry titleList[count];
if (AM_ListTitles(MEDIATYPE_NAND, count, titlesId, titleList)) {
return titles;
}
for (int i = 0; i < count; i++) {
TitleInfo title("NA", titleList[i].titleID, titleList[i].version);
titles.push_back(title);
}
std::sort(titles.begin(), titles.end(), sortTitles);
return titles;
}
TitleInfo Utility::getTitleInfo(const std::string &path) {
TitleInfo title;
AM_TitleEntry amTitle;
Handle fileHandle = openFileHandle(path);
if (AM_GetCiaFileInfo(MEDIATYPE_NAND, &amTitle, fileHandle)) {
return title;
}
closeFileHandle(fileHandle);
title.path = path;
title.titleID = amTitle.titleID;
title.version = amTitle.version;
return title;
}
bool Utility::deleteTitle(u64 titleID) {
if (titleID >> 32 & 0xFFFF) {
if (AM_DeleteTitle(MEDIATYPE_NAND, titleID)) {
return false;
}
} else if (AM_DeleteAppTitle(MEDIATYPE_NAND, titleID)) {
return false;
}
return true;
}
bool Utility::installTitle(std::string path) {
Handle fileHandle, ciaHandle = 0;
u64 size, bytesToRead, i = 0;
char *ciaBuffer;
u32 bytes;
fileHandle = openFileHandle(path);
FSFILE_GetSize(fileHandle, &size);
if (AM_StartCiaInstall(MEDIATYPE_NAND, &ciaHandle)) {
return false;
}
ciaBuffer = (char *) malloc(BUFSIZE);
memset(ciaBuffer, 0, BUFSIZE);
while (i < size) {
bytesToRead = i + BUFSIZE > size ? size - i : BUFSIZE;
if (FSFILE_Read(fileHandle, &bytes, i, ciaBuffer, bytesToRead)) {
AM_CancelCIAInstall(&ciaHandle);
FSFILE_Close(fileHandle);
free(ciaBuffer);
return false;
}
if (FSFILE_Write(ciaHandle, &bytes, i, ciaBuffer, bytesToRead, FS_WRITE_FLUSH)) {
AM_CancelCIAInstall(&ciaHandle);
FSFILE_Close(fileHandle);
free(ciaBuffer);
return false;
}
i += bytesToRead;
}
if (AM_FinishCiaInstall(MEDIATYPE_NAND, &ciaHandle)) {
AM_CancelCIAInstall(&ciaHandle);
FSFILE_Close(fileHandle);
free(ciaBuffer);
return false;
}
FSFILE_Close(fileHandle);
free(ciaBuffer);
return 0;
}
Utility::SysInfo *Utility::getSysInfo() {
SysInfo *sysInfo = (SysInfo *) malloc(sizeof(struct SysInfo));
Result ret = CFGU_GetSystemModel(&sysInfo->model);
if (R_FAILED(ret)) return NULL;
ret = CFGU_SecureInfoGetRegion(&sysInfo->region);
if (R_FAILED(ret)) return NULL;
return sysInfo;
}
int Utility::getAMu() {
Handle amHandle = 0;
// verify am:u access
srvGetServiceHandleDirect(&amHandle, "am:u");
if (amHandle) {
svcCloseHandle(amHandle);
return 0;
}
// try to get arm11
if(osGetKernelVersion() > SYSTEM_VERSION(2,50,9)) {
svchax_init();
aptInit();
APT_CheckNew3DS(&isNew3DS);
patchServiceAccess();
} else {
gfxExit();
if (suInit() != 0) {
_gfxInit();
return 1;
}
_gfxInit();
}
srvGetServiceHandleDirect(&amHandle, "am:u");
if (amHandle) {
svcCloseHandle(amHandle);
return 0;
}
return 1;
}
int Utility::checkMD5(const char *file, const char *md5) {
md5_state_t state;
md5_byte_t digest[16];
int bytes;
FILE *fp = fopen(file, "rb");
if (fp == NULL) {
return 1;
}
unsigned char *data = (unsigned char *) malloc(1024 * 32);
md5_init(&state);
while ((bytes = fread(data, 1, 1024 * 32, fp)) != 0) {
md5_append(&state, (const md5_byte_t *) data, bytes);
}
md5_finish(&state, digest);
free(data);
fclose(fp);
char rmd5[32];
for (int i = 0; i < 16; i++) {
sprintf(rmd5 + 2 * i, "%02x", digest[i]);
}
return strcmp(md5, rmd5);
}
Handle Utility::openFileHandle(const std::string &path) {
char tmp[128];
Handle fileHandle = 0;
strncpy(tmp, path.c_str(), 128); // fsMakePath doesn't like std::string ?!
FS_Path filePath = fsMakePath(PATH_ASCII, tmp);
if (FSUSER_OpenFile(&fileHandle, sdmcArchive, filePath, FS_OPEN_READ & 3, 0)) {
FSUSER_OpenFile(&fileHandle, sdmcArchive, filePath, FS_OPEN_READ, 0);
}
return fileHandle;
}
void Utility::closeFileHandle(const Handle &handle) {
FSFILE_Close(handle);
}
void Utility::sdmcArchiveInit() {
sdmcArchive = (FS_Archive) {0x00000009, (FS_Path) {PATH_EMPTY, 1, (u8 *) ""}};
FSUSER_OpenArchive(&sdmcArchive);
}
void Utility::sdmcArchiveExit() {
FSUSER_CloseArchive(&sdmcArchive);
}