Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/ELF/ParamSFO.cpp
5656 views
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
#include <cstdio>
19
#include <cstring>
20
21
#include "Common/CommonTypes.h"
22
#include "Common/Log.h"
23
#include "Common/StringUtils.h"
24
#include "Common/Swap.h"
25
#include "Common/File/Path.h"
26
#include "Core/ELF/ParamSFO.h"
27
#include "Core/System.h"
28
29
struct Header
30
{
31
u32_le magic; /* Always PSF */
32
u32_le version; /* Usually 1.1 */
33
u32_le key_table_start; /* Start position of key_table */
34
u32_le data_table_start; /* Start position of data_table */
35
u32_le index_table_entries; /* Number of entries in index_table*/
36
};
37
38
struct IndexTable
39
{
40
u16_le key_table_offset; /* Offset of the param_key from start of key_table */
41
u16_le param_fmt; /* Type of data of param_data in the data_table */
42
u32_le param_len; /* Used Bytes by param_data in the data_table */
43
u32_le param_max_len; /* Total bytes reserved for param_data in the data_table */
44
u32_le data_table_offset; /* Offset of the param_data from start of data_table */
45
};
46
47
void ParamSFOData::SetValue(std::string_view key, unsigned int value, int max_size) {
48
auto [it, inserted] = values.try_emplace(std::string(key)); // The string construction only happens if inserted is true.
49
it->second.type = VT_INT;
50
it->second.i_value = value;
51
it->second.max_size = max_size;
52
}
53
54
void ParamSFOData::SetValue(std::string_view key, std::string_view value, int max_size) {
55
auto [it, inserted] = values.try_emplace(std::string(key));
56
it->second.type = VT_UTF8;
57
it->second.s_value = value;
58
it->second.max_size = max_size;
59
}
60
61
void ParamSFOData::SetValue(std::string_view key, const u8 *value, unsigned int size, int max_size) {
62
auto [it, inserted] = values.try_emplace(std::string(key));
63
it->second.type = VT_UTF8_SPE;
64
it->second.SetData(value, size);
65
it->second.max_size = max_size;
66
}
67
68
int ParamSFOData::GetValueInt(std::string_view key) const {
69
std::map<std::string,ValueData>::const_iterator it = values.find(key);
70
if (it == values.end() || it->second.type != VT_INT)
71
return 0;
72
return it->second.i_value;
73
}
74
75
std::string ParamSFOData::GetValueString(std::string_view key) const {
76
std::map<std::string,ValueData>::const_iterator it = values.find(key);
77
if (it == values.end() || (it->second.type != VT_UTF8))
78
return "";
79
return it->second.s_value;
80
}
81
82
bool ParamSFOData::HasKey(std::string_view key) const {
83
return values.find(key) != values.end();
84
}
85
86
const u8 *ParamSFOData::GetValueData(std::string_view key, unsigned int *size) const {
87
std::map<std::string,ValueData>::const_iterator it = values.find(key);
88
if (it == values.end() || (it->second.type != VT_UTF8_SPE)) {
89
return 0;
90
}
91
if (size) {
92
*size = (unsigned int)it->second.u_value.size();
93
}
94
return it->second.u_value.data();
95
}
96
97
std::vector<std::string> ParamSFOData::GetKeys() const {
98
std::vector<std::string> result;
99
for (const auto &pair : values) {
100
result.push_back(pair.first);
101
}
102
return result;
103
}
104
105
std::string ParamSFOData::GetDiscID() {
106
const std::string discID = GetValueString("DISC_ID");
107
if (discID.empty()) {
108
std::string fakeID = GenerateFakeID(Path());
109
WARN_LOG(Log::Loader, "No DiscID found - generating a fake one: '%s' (from %s)", fakeID.c_str(), PSP_CoreParameter().fileToStart.c_str());
110
ValueData data;
111
data.type = VT_UTF8;
112
data.s_value = fakeID;
113
values["DISC_ID"] = data;
114
return fakeID;
115
}
116
return discID;
117
}
118
119
// I'm so sorry Ced but this is highly endian unsafe :(
120
bool ParamSFOData::ReadSFO(const u8 *paramsfo, size_t size) {
121
if (size < sizeof(Header))
122
return false;
123
const Header *header = (const Header *)paramsfo;
124
if (header->magic != 0x46535000)
125
return false;
126
if (header->version != 0x00000101)
127
WARN_LOG(Log::Loader, "Unexpected SFO header version: %08x", header->version);
128
129
const IndexTable *indexTables = (const IndexTable *)(paramsfo + sizeof(Header));
130
131
if (header->key_table_start > size || header->data_table_start > size) {
132
return false;
133
}
134
135
auto readStringCapped = [paramsfo, size](size_t offset, size_t maxLen) -> std::string {
136
std::string str;
137
while (offset < size) {
138
char c = (char)(paramsfo[offset]);
139
if (c) {
140
str.push_back(c);
141
} else {
142
break;
143
}
144
offset++;
145
if (maxLen != 0 && str.size() == maxLen)
146
break;
147
}
148
return str;
149
};
150
151
for (u32 i = 0; i < header->index_table_entries; i++)
152
{
153
size_t key_offset = header->key_table_start + indexTables[i].key_table_offset;
154
if (key_offset >= size) {
155
return false;
156
}
157
158
size_t data_offset = header->data_table_start + indexTables[i].data_table_offset;
159
if (data_offset >= size) {
160
return false;
161
}
162
163
std::string key = readStringCapped(key_offset, 0);
164
if (key.empty())
165
continue; // Likely ran into a truncated PARAMSFO.
166
167
switch (indexTables[i].param_fmt) {
168
case 0x0404:
169
{
170
if (data_offset + 4 > size)
171
continue;
172
// Unsigned int
173
const u32_le *data = (const u32_le *)(paramsfo + data_offset);
174
SetValue(key, *data, indexTables[i].param_max_len);
175
VERBOSE_LOG(Log::Loader, "%s %08x", key.c_str(), *data);
176
}
177
break;
178
case 0x0004:
179
// Special format UTF-8
180
{
181
if (data_offset + indexTables[i].param_len > size)
182
continue;
183
const u8 *utfdata = (const u8 *)(paramsfo + data_offset);
184
VERBOSE_LOG(Log::Loader, "%s %s", key.c_str(), utfdata);
185
SetValue(key, utfdata, indexTables[i].param_len, indexTables[i].param_max_len);
186
}
187
break;
188
case 0x0204:
189
// Regular UTF-8
190
{
191
// TODO: Likely should use param_len here, but there's gotta be a reason we avoided it before.
192
std::string str = readStringCapped(data_offset, indexTables[i].param_max_len);
193
VERBOSE_LOG(Log::Loader, "%s %s", key.c_str(), str.c_str());
194
SetValue(key, str, indexTables[i].param_max_len);
195
}
196
break;
197
default:
198
break;
199
}
200
}
201
202
return true;
203
}
204
205
int ParamSFOData::GetDataOffset(const u8 *paramsfo, const char *dataName) {
206
const Header *header = (const Header *)paramsfo;
207
if (header->magic != 0x46535000)
208
return -1;
209
if (header->version != 0x00000101)
210
WARN_LOG(Log::Loader, "Unexpected SFO header version: %08x", header->version);
211
212
const IndexTable *indexTables = (const IndexTable *)(paramsfo + sizeof(Header));
213
214
const u8 *key_start = paramsfo + header->key_table_start;
215
int data_start = header->data_table_start;
216
217
for (u32 i = 0; i < header->index_table_entries; i++)
218
{
219
const char *key = (const char *)(key_start + indexTables[i].key_table_offset);
220
if (!strcmp(key, dataName))
221
{
222
return data_start + indexTables[i].data_table_offset;
223
}
224
}
225
226
return -1;
227
}
228
229
void ParamSFOData::WriteSFO(u8 **paramsfo, size_t *size) const {
230
size_t total_size = 0;
231
size_t key_size = 0;
232
size_t data_size = 0;
233
234
Header header;
235
header.magic = 0x46535000;
236
header.version = 0x00000101;
237
header.index_table_entries = 0;
238
239
total_size += sizeof(Header);
240
241
// Get size info
242
for (const auto &[k, v] : values)
243
{
244
key_size += k.size() + 1;
245
data_size += v.max_size;
246
247
header.index_table_entries++;
248
}
249
250
// Padding
251
while ((key_size % 4) != 0) key_size++;
252
253
header.key_table_start = sizeof(Header) + header.index_table_entries * sizeof(IndexTable);
254
header.data_table_start = header.key_table_start + (u32)key_size;
255
256
total_size += sizeof(IndexTable) * header.index_table_entries;
257
total_size += key_size;
258
total_size += data_size;
259
*size = total_size;
260
261
size_t aligned_size = (total_size + 15) & ~15;
262
u8* data = new u8[aligned_size];
263
*paramsfo = data;
264
memset(data, 0, aligned_size);
265
memcpy(data, &header, sizeof(Header));
266
267
// Now fill
268
IndexTable *index_ptr = (IndexTable*)(data + sizeof(Header));
269
u8* key_ptr = data + header.key_table_start;
270
u8* data_ptr = data + header.data_table_start;
271
272
for (const auto &[k, v] : values)
273
{
274
u16 offset = (u16)(key_ptr - (data+header.key_table_start));
275
index_ptr->key_table_offset = offset;
276
offset = (u16)(data_ptr - (data+header.data_table_start));
277
index_ptr->data_table_offset = offset;
278
index_ptr->param_max_len = v.max_size;
279
if (v.type == VT_INT)
280
{
281
index_ptr->param_fmt = 0x0404;
282
index_ptr->param_len = 4;
283
284
*(s32_le *)data_ptr = v.i_value;
285
}
286
else if (v.type == VT_UTF8_SPE)
287
{
288
index_ptr->param_fmt = 0x0004;
289
index_ptr->param_len = (u32)v.u_value.size();
290
291
memset(data_ptr, 0, index_ptr->param_max_len);
292
memcpy(data_ptr, v.u_value.data(), index_ptr->param_len);
293
}
294
else if (v.type == VT_UTF8)
295
{
296
index_ptr->param_fmt = 0x0204;
297
index_ptr->param_len = (u32)v.s_value.size()+1;
298
299
memcpy(data_ptr,v.s_value.c_str(),index_ptr->param_len);
300
data_ptr[index_ptr->param_len] = 0;
301
}
302
303
memcpy(key_ptr,k.c_str(),k.size());
304
key_ptr[k.size()] = 0;
305
306
data_ptr += index_ptr->param_max_len;
307
key_ptr += k.size() + 1;
308
index_ptr++;
309
310
}
311
}
312
313
void ParamSFOData::Clear() {
314
values.clear();
315
}
316
317
std::string ParamSFOData::GenerateFakeID(const Path &filename) const {
318
// Generates fake gameID for homebrew based on it's folder name.
319
// Should probably not be a part of ParamSFO, but it'll be called in same places.
320
// FileToStart here is actually a directory name, not a file, so taking GetFilename on it gets what we want.
321
Path path = PSP_CoreParameter().fileToStart;
322
if (!filename.empty())
323
path = filename;
324
325
std::string file = path.GetFilename();
326
327
int sumOfAllLetters = 0;
328
for (char &c : file) {
329
sumOfAllLetters += c;
330
// Get rid of some garbage characters than can arise when opening content URIs. Well, I've only seen '%', but...
331
if (strchr("%() []", c) != nullptr) {
332
c = 'X';
333
} else {
334
c = toupper(c);
335
}
336
}
337
338
if (file.size() < 4) {
339
file += "HOME";
340
}
341
file = file.substr(0, 4);
342
343
std::string fakeID = file + StringFromFormat("%05d", sumOfAllLetters);
344
return fakeID;
345
}
346
347
GameRegion DetectGameRegionFromID(std::string_view id_full) {
348
// DISC_ID format consists of a 4-letter categorization followed by a 5-digit catalog number.
349
if (id_full.size() == 9 || (id_full.size() == 10 && id_full[4] == '-')) {
350
std::string_view id_letters = id_full.substr(0, 4);
351
std::string_view id_release_type = id_letters.substr(0, 2);
352
353
// Determine the type of release from the first two letters, must be one of the following:
354
// "UC" -> (U)MD, (C)opyrighted (first-party)
355
// "UL" -> (U)MD, (L)icensed (third-party)
356
// "NP" -> PlayStation (N)etwork, (P)roduction environment (digital download)
357
if (id_release_type == "UL" || id_release_type == "UC" || id_release_type == "NP") {
358
// Determine the region from the third letter.
359
// This isn't super accurate but it's all we have.
360
switch (id_letters[2]) {
361
case 'E': return GameRegion::EUROPE; break;
362
case 'U': return GameRegion::USA; break;
363
case 'J': return GameRegion::JAPAN; break;
364
case 'K': return GameRegion::KOREA; break;
365
case 'A': return GameRegion::ASIA; break;
366
default:
367
if (id_letters.substr(0, 3) == "NPH") {
368
return GameRegion::HONGKONG; // All games in this region are PSN.
369
} else if (id_letters == "NPIA") {
370
return GameRegion::INTERNAL;
371
} else {
372
return GameRegion::HOMEBREW;
373
}
374
}
375
/* The fourth letter could be used to determine the type of product. It isn't useful to us.
376
* UMD:
377
* 'S' -> full (S)oftware? (used by most games)
378
* 'M' -> (M)edia? (used by some Japanese and Korean games)
379
* 'B' -> (B)undled
380
* 'D' -> (D)emo
381
* 'P' -> (P)re-production
382
* 'T' -> (T)est
383
* 'X' -> e(X)perimental?
384
* Digital:
385
* 'A' -> first-party application
386
* 'B' -> third-party PSP Remasters
387
* 'E' -> first-party PAL PSOne
388
* 'F' -> third-party PAL PSOne / American PC Engine (TurboGrafx-16 Classics)
389
* 'G' -> first-party PSP / PlayView
390
* 'H' -> third-party PSP / PlayView / Neo Geo
391
* 'I' -> first-party NTSC PSOne
392
* 'J' -> third-party NTSC PSOne / Japanese PC Engine
393
* 'W' -> first-party tool?
394
* 'X' -> first-party Minis
395
* 'Z' -> third-party Minis
396
*/
397
// Misc patterns
398
} else if (id_letters == "UTST") {
399
return GameRegion::TEST;
400
} else if (id_letters == "UMDT") {
401
return GameRegion::DIAGNOSTIC;
402
}
403
}
404
return GameRegion::HOMEBREW;
405
}
406
407
std::string_view GameRegionToString(GameRegion region) {
408
switch (region) {
409
case GameRegion::JAPAN: return "Japan";
410
case GameRegion::USA: return "USA";
411
case GameRegion::EUROPE: return "Europe";
412
case GameRegion::HONGKONG: return "Hong Kong";
413
case GameRegion::ASIA: return "Asia";
414
case GameRegion::KOREA: return "Korea";
415
case GameRegion::HOMEBREW: return "Homebrew";
416
case GameRegion::INTERNAL: return "Internal";
417
case GameRegion::TEST: return "Test disc";
418
case GameRegion::DIAGNOSTIC: return "Diagnostic tool";
419
default: return "unknown region";
420
}
421
}
422
423