forked from ps2homebrew/Open-PS2-Loader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
OSDHistory.c
293 lines (253 loc) · 10.4 KB
/
OSDHistory.c
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
/* Play history management functions
Note: while the code for updating the data itself was a copy of the original,
the code that reads/writes the history file was written with general know-how,
for simplicity.
The original code used libmc and was also designed to allow the host software (the browser) access the history.
(the browser's boot animation would vary according to the user's play history)
However, OPL does not need any of that, so it can be made simpler. */
#include <errno.h>
#include <kernel.h>
#include <libcdvd.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <sys/unistd.h>
#include <libmc.h>
#include <libmc-common.h>
#include "include/util.h"
#include "include/OSDHistory.h"
/* The OSDs have this weird bug whereby the size of the icon file is hardcoded to 1776 bytes... even though that is way too long!
Unfortunately, using the right size will cause the icon to be deemed as corrupted data by the HDDOSD. */
#define SONY_SYSDATA_ICON_SYS_SIZE 1776
extern unsigned char icon_sys_A[];
extern unsigned char icon_sys_J[];
extern unsigned char icon_sys_C[];
#define DEBUG_PRINTF(args...)
int CreateSystemDataFolder(const char *path, char FolderRegionLetter)
{
char fullpath[64];
int fd, result, size;
void *icon;
sprintf(fullpath, "%s/icon.sys", path);
if ((fd = open(fullpath, O_RDONLY)) < 0) {
mkdir(path, 0777);
if ((fd = open(fullpath, O_CREAT | O_TRUNC | O_WRONLY)) >= 0) {
switch (FolderRegionLetter) {
case 'I':
icon = icon_sys_J;
size = SONY_SYSDATA_ICON_SYS_SIZE;
break;
case 'C':
icon = icon_sys_C;
size = SONY_SYSDATA_ICON_SYS_SIZE;
break;
default: // case 'A':
icon = icon_sys_A;
size = SONY_SYSDATA_ICON_SYS_SIZE;
break;
}
result = write(fd, icon, size) == size ? 0 : -EIO; // Yes, just let it write past the end of the icon. Read the comment above.
close(fd);
} else
result = fd;
} else {
close(fd);
result = 0;
}
return result;
}
int LoadHistoryFile(const char *path, struct HistoryEntry *HistoryEntries)
{
char fullpath[64];
int fd, result;
sprintf(fullpath, "%s/history", path);
if ((fd = open(fullpath, O_RDONLY)) >= 0) {
result = read(fd, HistoryEntries, MAX_HISTORY_ENTRIES * sizeof(struct HistoryEntry)) == (MAX_HISTORY_ENTRIES * sizeof(struct HistoryEntry)) ? 0 : -EIO;
close(fd);
} else
result = fd;
return result;
}
int SaveHistoryFile(const char *path, const struct HistoryEntry *HistoryEntries)
{
char fullpath[64];
int fd, result;
sprintf(fullpath, "%s/history", path);
if ((fd = open(fullpath, O_WRONLY | O_CREAT | O_TRUNC)) >= 0) {
result = write(fd, HistoryEntries, MAX_HISTORY_ENTRIES * sizeof(struct HistoryEntry)) == (MAX_HISTORY_ENTRIES * sizeof(struct HistoryEntry)) ? 0 : -EIO;
close(fd);
} else
result = fd;
return result;
}
int AddOldHistoryFileRecord(const char *path, const struct HistoryEntry *OldHistoryEntry)
{
char fullpath[64];
int fd, result;
sprintf(fullpath, "%s/history.old", path);
if ((fd = open(fullpath, O_WRONLY | O_APPEND)) >= 0) {
lseek(fd, 0, SEEK_END);
result = write(fd, OldHistoryEntry, sizeof(struct HistoryEntry)) == sizeof(struct HistoryEntry) ? 0 : -EIO;
close(fd);
} else
result = fd;
return result;
}
static u16 GetTimestamp(void)
{
// The original obtained the time and date from globals.
// return OSD_HISTORY_SET_DATE(currentYear, currentMonth, currentDate);
sceCdCLOCK time;
sceCdReadClock(&time);
return OSD_HISTORY_SET_DATE(btoi(time.year), btoi(time.month & 0x7F), btoi(time.day));
}
int AddHistoryRecord(const char *name)
{
struct HistoryEntry HistoryEntries[MAX_HISTORY_ENTRIES], *NewEntry, OldHistoryEntry;
int i, value, LeastUsedRecord, LeastUsedRecordLaunchCount, LeastUsedRecordTimestamp, NewLaunchCount, result, mcType, format;
u8 BlankSlotList[MAX_HISTORY_ENTRIES];
int NumBlankSlots, NumSlotsUsed, IsNewRecord;
char SystemRegionLetter;
char path[32];
DEBUG_PRINTF("OSDHistory@%s: starts\n", __func__);
for (i = 0; i < 2; i++) { // increase number of slots for mt (not sure how multi tap works haven’t looked at docs ie mc0 - mc4 ?)
mcGetInfo(i, 0, &mcType, NULL, &format);
mcSync(0, NULL, &result);
DEBUG_PRINTF("\tslot=%d, mctype=%d, format=%d\n", i, mcType, format);
if ((mcType == sceMcTypePS2) && (format == MC_FORMATTED))
break;
}
if ((mcType != sceMcTypePS2) || (format != MC_FORMATTED)) { // don't even waste time if there are no PS2 MC's
DEBUG_PRINTF("\tNo PS2 memory cards detected\n");
return -1;
}
DEBUG_PRINTF("\tAdding history record '%s' for slot %d\n", name, i);
// For simplicity, create the data folder immediately if the history file does not exist (unlike the original).
sprintf(path, "mc%d:/%s", i, GetSystemDataPath());
if ((result = LoadHistoryFile(path, HistoryEntries)) != 0) {
DEBUG_PRINTF("\tcan't load history file.\n");
SystemRegionLetter = GetSystemFolderLetter();
if (SystemRegionLetter == 'R') { // @El_isra: R is the default prefix on OPL and FreeMcBoot code, however, no known ps2 uses this prefix for system folders. So skip creating a folder named like that
DEBUG_PRINTF("\n\tERROR: SystemRegionLetter is R\n\n");
return -2;
}
if ((result = CreateSystemDataFolder(path, SystemRegionLetter)) != 0) {
DEBUG_PRINTF("\tERROR: Can't create system data folder: error=%d, regionletter=%c\n", result, SystemRegionLetter);
return result;
}
memset(HistoryEntries, 0, sizeof(HistoryEntries));
}
LeastUsedRecord = 0;
LeastUsedRecordTimestamp = INT_MAX;
LeastUsedRecordLaunchCount = INT_MAX;
IsNewRecord = 1;
for (i = 0; i < MAX_HISTORY_ENTRIES; i++) {
if (HistoryEntries[i].LaunchCount < LeastUsedRecordLaunchCount) {
LeastUsedRecord = i;
LeastUsedRecordLaunchCount = HistoryEntries[i].LaunchCount;
}
if (LeastUsedRecordLaunchCount == HistoryEntries[i].LaunchCount) {
if (HistoryEntries[i].DateStamp < LeastUsedRecordTimestamp) {
LeastUsedRecordTimestamp = HistoryEntries[i].DateStamp;
LeastUsedRecord = i;
}
}
// In v1.0x, this was strcmp
if (!strncmp(HistoryEntries[i].name, name, sizeof(HistoryEntries[i].name))) {
IsNewRecord = 0;
HistoryEntries[i].DateStamp = GetTimestamp();
if ((HistoryEntries[i].bitmask & 0x3F) != 0x3F) {
NewLaunchCount = HistoryEntries[i].LaunchCount + 1;
if (NewLaunchCount >= 0x80) {
NewLaunchCount = 0x7F;
}
if (NewLaunchCount >= 14) {
if ((NewLaunchCount - 14) % 10 == 0) {
while ((HistoryEntries[i].bitmask >> (value = rand() % 6)) & 1) {
};
HistoryEntries[i].ShiftAmount = value;
HistoryEntries[i].bitmask |= 1 << value;
}
}
HistoryEntries[i].LaunchCount = NewLaunchCount;
} else {
// Was a check against 0x40 in v1.0x
if (HistoryEntries[i].LaunchCount < 0x3F) {
HistoryEntries[i].LaunchCount++;
} else {
HistoryEntries[i].LaunchCount = HistoryEntries[i].bitmask & 0x3F;
HistoryEntries[i].ShiftAmount = 7;
}
}
}
}
/*
i = 0;
do { // Original does this. I guess, it is used to ensure that the next random value is truly random?
rand();
i++;
} while (i < (currentMinute * 60 + currentSecond));
*/
if (IsNewRecord) {
// Count and consolidate a list of blank slots.
NumBlankSlots = 0;
NumSlotsUsed = 0;
for (i = 0; i < MAX_HISTORY_ENTRIES; i++) {
if (HistoryEntries[i].name[0] == '\0') {
BlankSlotList[NumBlankSlots] = i;
NumBlankSlots++;
} else {
// Not present in v1.0x.
if (HistoryEntries[i].ShiftAmount == 0x7) {
NumSlotsUsed++;
}
}
}
if (NumSlotsUsed != MAX_HISTORY_ENTRIES) {
if (NumBlankSlots > 0) {
// Randomly choose an empty slot.
NewEntry = &HistoryEntries[result = BlankSlotList[rand() % NumBlankSlots]];
} else {
// Copy out the victim record
NewEntry = &HistoryEntries[LeastUsedRecord];
memcpy(&OldHistoryEntry, NewEntry, sizeof(OldHistoryEntry));
// Unlike the original, add the old history record here.
AddOldHistoryFileRecord(path, &OldHistoryEntry);
}
// Initialize the new entry.
strncpy(NewEntry->name, name, sizeof(NewEntry->name) - 1);
NewEntry->LaunchCount = 1;
NewEntry->bitmask = 1;
NewEntry->ShiftAmount = 0;
NewEntry->DateStamp = GetTimestamp();
}
}
// Unlike the original, save here.
return SaveHistoryFile(path, HistoryEntries);
}
static void GetBootFilename(const char *bootpath, char *filename)
{ // Extract filename from the full path to the file on the CD/DVD.
int i, length;
filename[0] = '\0';
for (i = length = strlen(bootpath); i > 0; i--) {
if (bootpath[i] == '\\' || bootpath[i] == ':' || bootpath[i] == '/') {
if (length > 2 && bootpath[length - 1] == '1' && bootpath[length - 2] == ';')
length -= 2;
length -= (i + 1);
strncpy(filename, &bootpath[i + 1], length);
filename[length] = '\0';
break;
}
}
if (i == 0) { // The boot path contains only the filename.
strcpy(filename, bootpath);
}
}
int AddHistoryRecordUsingFullPath(const char *path)
{
char filename[17];
GetBootFilename(path, filename);
return AddHistoryRecord(filename);
}