CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Dialog/SavedataParam.h
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <memory>
21
#include <mutex>
22
#include <set>
23
#include <unordered_map>
24
#include <cstdint>
25
26
#include "Common/CommonTypes.h"
27
#include "Core/ELF/ParamSFO.h"
28
#include "Core/MemMap.h"
29
#include "Core/HLE/sceRtc.h"
30
#include "Core/Dialog/PSPDialog.h"
31
32
#undef st_ctime
33
#undef st_atime
34
#undef st_mtime
35
36
class PPGeImage;
37
struct PSPFileInfo;
38
typedef u32_le SceSize_le;
39
40
enum SceUtilitySavedataType
41
{
42
SCE_UTILITY_SAVEDATA_TYPE_AUTOLOAD = 0,
43
SCE_UTILITY_SAVEDATA_TYPE_AUTOSAVE = 1,
44
SCE_UTILITY_SAVEDATA_TYPE_LOAD = 2,
45
SCE_UTILITY_SAVEDATA_TYPE_SAVE = 3,
46
SCE_UTILITY_SAVEDATA_TYPE_LISTLOAD = 4,
47
SCE_UTILITY_SAVEDATA_TYPE_LISTSAVE = 5,
48
SCE_UTILITY_SAVEDATA_TYPE_LISTDELETE = 6,
49
SCE_UTILITY_SAVEDATA_TYPE_LISTALLDELETE = 7,// When run on a PSP, displays a list of all saves on the PSP. Weird. (Not really, it's to let you free up space)
50
SCE_UTILITY_SAVEDATA_TYPE_SIZES = 8,
51
SCE_UTILITY_SAVEDATA_TYPE_AUTODELETE = 9,
52
SCE_UTILITY_SAVEDATA_TYPE_DELETE = 10,
53
SCE_UTILITY_SAVEDATA_TYPE_LIST = 11,
54
SCE_UTILITY_SAVEDATA_TYPE_FILES = 12,
55
SCE_UTILITY_SAVEDATA_TYPE_MAKEDATASECURE = 13,
56
SCE_UTILITY_SAVEDATA_TYPE_MAKEDATA = 14,
57
SCE_UTILITY_SAVEDATA_TYPE_READDATASECURE = 15,
58
SCE_UTILITY_SAVEDATA_TYPE_READDATA = 16,
59
SCE_UTILITY_SAVEDATA_TYPE_WRITEDATASECURE = 17,
60
SCE_UTILITY_SAVEDATA_TYPE_WRITEDATA = 18,
61
SCE_UTILITY_SAVEDATA_TYPE_ERASESECURE = 19,
62
SCE_UTILITY_SAVEDATA_TYPE_ERASE = 20,
63
SCE_UTILITY_SAVEDATA_TYPE_DELETEDATA = 21,
64
SCE_UTILITY_SAVEDATA_TYPE_GETSIZE = 22,
65
};
66
67
static const char *const utilitySavedataTypeNames[] = {
68
"AUTOLOAD",
69
"AUTOSAVE",
70
"LOAD",
71
"SAVE",
72
"LISTLOAD",
73
"LISTSAVE",
74
"LISTDELETE",
75
"LISTALLDELETE",
76
"SIZES",
77
"AUTODELETE",
78
"DELETE",
79
"LIST",
80
"FILES",
81
"MAKEDATASECURE",
82
"MAKEDATA",
83
"READDATASECURE",
84
"READDATA",
85
"WRITEDATASECURE",
86
"WRITEDATA",
87
"ERASESECURE",
88
"ERASE",
89
"DELETEDATA",
90
"GETSIZE",
91
};
92
93
enum SceUtilitySavedataFocus
94
{
95
SCE_UTILITY_SAVEDATA_FOCUS_NAME = 0, // specified by saveName[]
96
SCE_UTILITY_SAVEDATA_FOCUS_FIRSTLIST = 1, // first listed (on screen or of all?)
97
SCE_UTILITY_SAVEDATA_FOCUS_LASTLIST = 2, // last listed (on screen or of all?)
98
SCE_UTILITY_SAVEDATA_FOCUS_LATEST = 3, // latest by modification date (first if none)
99
SCE_UTILITY_SAVEDATA_FOCUS_OLDEST = 4, // oldest by modification date (first if none)
100
SCE_UTILITY_SAVEDATA_FOCUS_FIRSTDATA = 5, // first non-empty (first if none)
101
SCE_UTILITY_SAVEDATA_FOCUS_LASTDATA = 6, // last non-empty (first if none)
102
SCE_UTILITY_SAVEDATA_FOCUS_FIRSTEMPTY = 7, // first empty (what if no empty?)
103
SCE_UTILITY_SAVEDATA_FOCUS_LASTEMPTY = 8, // last empty (what if no empty?)
104
};
105
106
#if COMMON_LITTLE_ENDIAN
107
typedef SceUtilitySavedataType SceUtilitySavedataType_le;
108
typedef SceUtilitySavedataFocus SceUtilitySavedataFocus_le;
109
#else
110
typedef swap_struct_t<SceUtilitySavedataType, swap_32_t<SceUtilitySavedataType> > SceUtilitySavedataType_le;
111
typedef swap_struct_t<SceUtilitySavedataFocus, swap_32_t<SceUtilitySavedataFocus> > SceUtilitySavedataFocus_le;
112
#endif
113
114
typedef char SceUtilitySavedataSaveName[20];
115
116
// title, savedataTitle, detail: parts of the unencrypted SFO
117
// data, it contains what the VSH and standard load screen shows
118
struct PspUtilitySavedataSFOParam
119
{
120
char title[0x80];
121
char savedataTitle[0x80];
122
char detail[0x400];
123
unsigned char parentalLevel;
124
unsigned char unknown[3];
125
};
126
127
struct PspUtilitySavedataFileData {
128
PSPPointer<u8> buf;
129
SceSize_le bufSize; // Size of the buffer pointed to by buf
130
SceSize_le size; // Actual file size to write / was read
131
s32_le unknown;
132
};
133
134
struct PspUtilitySavedataSizeEntry {
135
u64_le size;
136
char name[16];
137
};
138
139
struct PspUtilitySavedataSizeInfo {
140
s32_le numSecureEntries;
141
s32_le numNormalEntries;
142
PSPPointer<PspUtilitySavedataSizeEntry> secureEntries;
143
PSPPointer<PspUtilitySavedataSizeEntry> normalEntries;
144
s32_le sectorSize;
145
s32_le freeSectors;
146
s32_le freeKB;
147
char freeString[8];
148
s32_le neededKB;
149
char neededString[8];
150
s32_le overwriteKB;
151
char overwriteString[8];
152
};
153
154
struct SceUtilitySavedataIdListEntry
155
{
156
s32_le st_mode;
157
ScePspDateTime st_ctime;
158
ScePspDateTime st_atime;
159
ScePspDateTime st_mtime;
160
SceUtilitySavedataSaveName name;
161
};
162
163
struct SceUtilitySavedataIdListInfo
164
{
165
s32_le maxCount;
166
s32_le resultCount;
167
PSPPointer<SceUtilitySavedataIdListEntry> entries;
168
};
169
170
struct SceUtilitySavedataFileListEntry
171
{
172
s32_le st_mode;
173
u32_le st_unk0;
174
u64_le st_size;
175
ScePspDateTime st_ctime;
176
ScePspDateTime st_atime;
177
ScePspDateTime st_mtime;
178
char name[16];
179
};
180
181
struct SceUtilitySavedataFileListInfo
182
{
183
u32_le maxSecureEntries;
184
u32_le maxNormalEntries;
185
u32_le maxSystemEntries;
186
u32_le resultNumSecureEntries;
187
u32_le resultNumNormalEntries;
188
u32_le resultNumSystemEntries;
189
PSPPointer<SceUtilitySavedataFileListEntry> secureEntries;
190
PSPPointer<SceUtilitySavedataFileListEntry> normalEntries;
191
PSPPointer<SceUtilitySavedataFileListEntry> systemEntries;
192
};
193
194
struct SceUtilitySavedataMsFreeInfo
195
{
196
s32_le clusterSize;
197
s32_le freeClusters;
198
s32_le freeSpaceKB;
199
char freeSpaceStr[8];
200
};
201
202
struct SceUtilitySavedataUsedDataInfo
203
{
204
s32_le usedClusters;
205
s32_le usedSpaceKB;
206
char usedSpaceStr[8];
207
s32_le usedSpace32KB;
208
char usedSpace32Str[8];
209
};
210
211
struct SceUtilitySavedataMsDataInfo
212
{
213
char gameName[13];
214
char pad[3];
215
SceUtilitySavedataSaveName saveName;
216
SceUtilitySavedataUsedDataInfo info;
217
};
218
219
// Structure to hold the parameters for the sceUtilitySavedataInitStart function.
220
struct SceUtilitySavedataParam
221
{
222
pspUtilityDialogCommon common;
223
224
SceUtilitySavedataType_le mode; // 0 to load, 1 to save
225
s32_le bind;
226
227
s32_le overwriteMode; // use 0x10 ?
228
229
/** gameName: name used from the game for saves, equal for all saves */
230
char gameName[13];
231
char unused[3];
232
/** saveName: name of the particular save, normally a number */
233
SceUtilitySavedataSaveName saveName;
234
PSPPointer<SceUtilitySavedataSaveName> saveNameList;
235
/** fileName: name of the data file of the game for example DATA.BIN */
236
char fileName[13];
237
char unused2[3];
238
239
/** pointer to a buffer that will contain data file unencrypted data */
240
PSPPointer<u8> dataBuf;
241
/** size of allocated space to dataBuf */
242
SceSize_le dataBufSize;
243
SceSize_le dataSize; // Size of the actual save data
244
245
PspUtilitySavedataSFOParam sfoParam;
246
247
PspUtilitySavedataFileData icon0FileData;
248
PspUtilitySavedataFileData icon1FileData;
249
PspUtilitySavedataFileData pic1FileData;
250
PspUtilitySavedataFileData snd0FileData;
251
252
PSPPointer<PspUtilitySavedataFileData> newData;
253
SceUtilitySavedataFocus_le focus;
254
s32_le abortStatus;
255
256
// Function SCE_UTILITY_SAVEDATA_TYPE_SIZES
257
PSPPointer<SceUtilitySavedataMsFreeInfo> msFree;
258
PSPPointer<SceUtilitySavedataMsDataInfo> msData;
259
PSPPointer<SceUtilitySavedataUsedDataInfo> utilityData;
260
261
u8 key[16];
262
263
u32_le secureVersion;
264
s32_le multiStatus;
265
266
// Function 11 LIST
267
PSPPointer<SceUtilitySavedataIdListInfo> idList;
268
269
// Function 12 FILES
270
PSPPointer<SceUtilitySavedataFileListInfo> fileList;
271
272
// Function 22 GETSIZES
273
PSPPointer<PspUtilitySavedataSizeInfo> sizeInfo;
274
};
275
276
// Non native, this one we can reorganize as we like
277
struct SaveFileInfo
278
{
279
s64 size = 0;
280
std::string saveName;
281
std::string saveDir;
282
int idx = 0;
283
284
char title[128]{};
285
char saveTitle[128]{};
286
char saveDetail[1024]{};
287
288
bool broken = false;
289
290
tm modif_time{};
291
292
PPGeImage *texture = nullptr;
293
294
void DoState(PointerWrap &p);
295
};
296
297
struct SaveSFOFileListEntry {
298
char filename[13];
299
u8 hash[16];
300
u8 pad[3];
301
};
302
303
class SavedataParam
304
{
305
public:
306
SavedataParam();
307
308
static void Init();
309
std::string GetSaveFilePath(const SceUtilitySavedataParam *param, int saveId = -1) const;
310
std::string GetSaveFilePath(const SceUtilitySavedataParam *param, const std::string &saveDir) const;
311
std::string GetSaveDirName(const SceUtilitySavedataParam *param, int saveId = -1) const;
312
std::string GetSaveDir(const SceUtilitySavedataParam *param, int saveId = -1) const;
313
std::string GetSaveDir(const SceUtilitySavedataParam *param, const std::string &saveDirName) const;
314
bool Delete(SceUtilitySavedataParam* param, int saveId = -1);
315
int DeleteData(SceUtilitySavedataParam* param);
316
int Save(SceUtilitySavedataParam* param, const std::string &saveDirName, bool secureMode = true);
317
int Load(SceUtilitySavedataParam* param, const std::string &saveDirName, int saveId = -1, bool secureMode = true);
318
int GetSizes(SceUtilitySavedataParam* param);
319
bool GetList(SceUtilitySavedataParam* param);
320
int GetFilesList(SceUtilitySavedataParam* param, u32 requestAddr);
321
bool GetSize(SceUtilitySavedataParam* param);
322
int GetSaveCryptMode(const SceUtilitySavedataParam *param, const std::string &saveDirName);
323
bool IsInSaveDataList(const std::string &saveName, int count);
324
325
std::string GetGameName(const SceUtilitySavedataParam *param) const;
326
std::string GetSaveName(const SceUtilitySavedataParam *param) const;
327
std::string GetFileName(const SceUtilitySavedataParam *param) const;
328
std::string GetKey(const SceUtilitySavedataParam *param) const;
329
bool HasKey(const SceUtilitySavedataParam *param) const;
330
331
static std::string GetSpaceText(u64 size, bool roundUp);
332
333
void SetIgnoreTextures(bool state) {
334
ignoreTextures_ = state;
335
}
336
int SetPspParam(SceUtilitySavedataParam* param);
337
SceUtilitySavedataParam *GetPspParam();
338
const SceUtilitySavedataParam *GetPspParam() const;
339
340
int GetFilenameCount();
341
const SaveFileInfo& GetFileInfo(int idx);
342
std::string GetFilename(int idx) const;
343
std::string GetSaveDir(int idx) const;
344
345
int GetSelectedSave();
346
void SetSelectedSave(int idx);
347
348
int GetFirstListSave();
349
int GetLastListSave();
350
int GetLatestSave();
351
int GetOldestSave();
352
int GetFirstDataSave();
353
int GetLastDataSave();
354
int GetFirstEmptySave();
355
int GetLastEmptySave();
356
int GetSaveNameIndex(const SceUtilitySavedataParam *param);
357
358
bool WouldHaveMultiSaveName(const SceUtilitySavedataParam *param);
359
360
void ClearSFOCache();
361
362
void DoState(PointerWrap &p);
363
364
private:
365
void Clear();
366
void SetFileInfo(int idx, PSPFileInfo &info, const std::string &saveName, const std::string &saveDir = "");
367
void SetFileInfo(SaveFileInfo &saveInfo, PSPFileInfo &info, const std::string &saveName, const std::string &saveDir = "");
368
void ClearFileInfo(SaveFileInfo &saveInfo, const std::string &saveName);
369
PSPFileInfo GetSaveInfo(const std::string &saveDir);
370
371
int LoadSaveData(SceUtilitySavedataParam *param, const std::string &saveDirName, const std::string& dirPath, bool secureMode);
372
u32 LoadCryptedSave(SceUtilitySavedataParam *param, u8 *data, const u8 *saveData, int &saveSize, int prevCryptMode, const u8 *expectedHash, bool &saveDone);
373
u32 LoadNotCryptedSave(SceUtilitySavedataParam *param, u8 *data, u8 *saveData, int &saveSize);
374
bool LoadSFO(SceUtilitySavedataParam *param, const std::string& dirPath);
375
void LoadFile(const std::string& dirPath, const std::string& filename, PspUtilitySavedataFileData *fileData);
376
377
int DecryptData(unsigned int mode, unsigned char *data, int *dataLen, int *alignedLen, unsigned char *cryptkey, const u8 *expectedHash);
378
int EncryptData(unsigned int mode, unsigned char *data, int *dataLen, int *alignedLen, unsigned char *hash, unsigned char *cryptkey);
379
int UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, int encryptmode);
380
381
// data must be zero-padded from len to alignedLen (which should be the next multiply of 16)!
382
int BuildHash(uint8_t *output, const uint8_t *data, unsigned int len, unsigned int alignedLen, int mode, const uint8_t *cryptkey);
383
int DetermineCryptMode(const SceUtilitySavedataParam *param) const;
384
385
std::vector<SaveSFOFileListEntry> GetSFOEntries(const std::string &dirPath);
386
std::set<std::string> GetSecureFileNames(const std::string &dirPath);
387
bool GetExpectedHash(const std::string &dirPath, const std::string &filename, u8 hash[16]);
388
389
std::shared_ptr<ParamSFOData> LoadCachedSFO(const std::string &path, bool orCreate = false);
390
391
SceUtilitySavedataParam* pspParam = nullptr;
392
int selectedSave = 0;
393
SaveFileInfo *saveDataList = nullptr;
394
SaveFileInfo *noSaveIcon = nullptr;
395
int saveDataListCount = 0;
396
int saveNameListDataCount = 0;
397
bool ignoreTextures_ = false;
398
399
// Cleared before returning to PSP, no need to save state.
400
std::mutex cacheLock_;
401
std::unordered_map<std::string, std::shared_ptr<ParamSFOData>> sfoCache_;
402
};
403
404