state.c
11.8 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "os.h"
#include "os_bb.h"
#include "bcp.h"
#include "bbint.h"
#include "state.h"
#define TMP_FILE "state.tmp"
#define NAME_LEN BB_STATENAME_LEN
#define DONT_SIGN_BIT 0x80000000
#define MAGIC_STASH_ADDR ((void*)PHYS_TO_K1(USB1_BDT_SRAM+256))
#define STATE_STASH_ADDR ((void*)(MAGIC_STASH_ADDR+4))
#define BIND_STASH_ADDR ((void*)(STATE_STASH_ADDR+sizeof(OSBbStateVector)))
#define NAME_STASH_ADDR ((void*)(BIND_STASH_ADDR+sizeof(s32)*4))
#define TID_STASH_ADDR ((void*)(NAME_STASH_ADDR+NAME_LEN))
#define STATE_DIRTY_STASH_ADDR ((void*)(TID_STASH_ADDR+4))
#define BLOCK_ROUND(x) (((x) + (BB_FL_BLOCK_SIZE-1))&~(BB_FL_BLOCK_SIZE-1))
static void
_wcopy(u32 *src, u32 *dst, int bytes)
{
int i;
int num_words = (bytes+3)>>2;
for (i = 0; i < num_words; ++i) {
*dst++ = *src++;
}
}
static void
__osBbUnstashState(char *name, BbTicketId* tid, OSBbStateVector *sv, s32 binding[MAXCONTROLLERS], u32* stateDirty)
{
u32 dummy;
if (IO_READ(MAGIC_STASH_ADDR) == STASH_MAGIC) {
_wcopy(STATE_STASH_ADDR, (u32*)sv, sizeof(OSBbStateVector));
_wcopy(BIND_STASH_ADDR, (u32*)binding, 4*sizeof(s32));
_wcopy(NAME_STASH_ADDR, (u32*)name, NAME_LEN);
_wcopy(TID_STASH_ADDR, &dummy, sizeof(BbTicketId));
*tid = dummy;
_wcopy(STATE_DIRTY_STASH_ADDR, stateDirty, sizeof(u32));
} else {
bzero(sv, sizeof(OSBbStateVector));
binding[0] = binding[1] = binding[2] = binding[3] = -1;
name[0] = 0;
tid[0] = 0;
stateDirty[0] = 0;
}
}
static void
__osBbGameUnstashState(char *name, OSBbStateVector *sv, s32 binding[MAXCONTROLLERS],
u32* stateDirty)
{
int i;
/* Called from game, __osBbStashMagic setup by viewer, version
maybe different */
if (STASH_OKAY(__osBbStashMagic))
{
sv->eepromSize = __osBbEepromSize;
sv->eepromAddress = (u32) __osBbEepromAddress;
sv->flashSize = __osBbFlashSize;
sv->flashAddress = (u32) __osBbFlashAddress;
sv->sramSize = __osBbSramSize;
sv->sramAddress = (u32) __osBbSramAddress;
sv->pakSize = __osBbPakSize;
for (i = 0; i < MAXCONTROLLERS; i++)
sv->pakAddress[i] = (u32) __osBbPakAddress[i];
for (i = 0; i < MAXCONTROLLERS; i++)
binding[i] = __osBbPakBindings[i];
memcpy(name, __osBbStateName, NAME_LEN);
*stateDirty = __osBbStateDirty;
} else {
bzero(sv, sizeof(OSBbStateVector));
binding[0] = binding[1] = binding[2] = binding[3] = -1;
name[0] = 0;
stateDirty[0] = 0;
}
}
static void
__osBbStashState(const char *name, BbTicketId tid, OSBbStateVector *sv, s32 binding[MAXCONTROLLERS], u32 stateDirty)
{
u32 dummy = tid;
IO_WRITE(MAGIC_STASH_ADDR, STASH_MAGIC);
_wcopy((u32*)sv, STATE_STASH_ADDR, sizeof(OSBbStateVector));
_wcopy((u32*)binding, BIND_STASH_ADDR, 4*sizeof(s32));
_wcopy((u32*)name, NAME_STASH_ADDR, NAME_LEN);
_wcopy((u32*)&dummy, TID_STASH_ADDR, sizeof(BbTicketId));
_wcopy((u32*)&stateDirty, STATE_DIRTY_STASH_ADDR, sizeof(u32));
}
static void
__osBbClearState(void)
{
IO_WRITE(MAGIC_STASH_ADDR, 0);
}
static void
__osBbGetStateFilename(const char *name, char *state_name)
{
int i;
for(i = 0; i < 8 && name[i] != '.' && name[i]; i++) {
state_name[i] = name[i];
}
state_name[i] = '.';
state_name[i+1] = 's';
state_name[i+2] = 't';
state_name[i+3] = 'a';
state_name[i+4] = '\0';
}
static s32
__osBbSaveState(const char* state_name,
OSBbStateVector sv,
s32 binding[MAXCONTROLLERS], u32 stateDirty)
{
int i, fd;
u32 state_size = 0, off = 0;
s32 rv = 0;
u32 block = 0;
if ((state_name == NULL) || !state_name[0]) return -1;
osBbFDelete(TMP_FILE);
state_size = BLOCK_ROUND(sv.eepromSize) + BLOCK_ROUND(sv.flashSize) + BLOCK_ROUND(sv.sramSize);
if (state_size) {
if ((fd = osBbFCreate(TMP_FILE, 1, state_size)) >= 0) {
if (sv.eepromSize) {
osBbFWrite(fd, off, (void*)sv.eepromAddress, BLOCK_ROUND(sv.eepromSize));
off += BLOCK_ROUND(sv.eepromSize);
block = sv.eepromAddress;
}
if (sv.flashSize) {
osBbFWrite(fd, off, (void*)sv.flashAddress, BLOCK_ROUND(sv.flashSize));
off += BLOCK_ROUND(sv.flashSize);
block = sv.flashAddress;
}
if (sv.sramSize) {
osBbFWrite(fd, off, (void*)sv.sramAddress, BLOCK_ROUND(sv.sramSize));
off += BLOCK_ROUND(sv.sramSize);
block = sv.sramAddress;
}
osBbFClose(fd);
osBbFRename(TMP_FILE, state_name);
if (!stateDirty)
osBbSignFile(state_name, (u8*)block);
}
}
osBbFDelete(TMP_FILE);
for (i = 0; i < MAXCONTROLLERS; ++i) {
if (binding[i] != -1 && sv.pakAddress[i]) {
if ((fd = osBbFCreate(TMP_FILE, 1, BLOCK_ROUND(sv.pakSize))) >= 0) {
char pak_name[NAME_LEN];
u32 sign;
osBbFWrite(fd, 0, (void*)sv.pakAddress[i], BLOCK_ROUND(sv.pakSize));
osBbFClose(fd);
sign = binding[i] & DONT_SIGN_BIT;
binding[i] &= ~DONT_SIGN_BIT;
sprintf(pak_name, "%ld.pak", binding[i]);
osBbFRename(TMP_FILE, pak_name);
if (!sign) {
osBbSignFile(pak_name, (u8*)sv.pakAddress[i]);
}
}
}
}
return rv;
}
s32
osBbSaveState(char stateName[] /* OUTPUT, 8.3 filename */, BbTicketId* tid)
{
char state_name[NAME_LEN];
OSBbStateVector sv;
s32 binding[MAXCONTROLLERS];
u32 stateDirty = 0;
int rv, i;
stateName[0]=0;
*tid=0;
/* retrieve name from stash, retrieve state, write to file */
__osBbUnstashState(state_name, tid, &sv, binding, &stateDirty);
if (!state_name[0]) return -1;
for(i=0; state_name[i] != '\0' && i < NAME_LEN; i++){
stateName[i] = state_name[i];
}
rv = __osBbSaveState(state_name, sv, binding, stateDirty);
/* clear to avoid multiple saves */
__osBbClearState();
return rv;
}
/* Commits the state to the BBCard from within a Game.
*
* NOTE: The file system will overwrite PI_FLASH_CTRL_REG, which is
* also used for DMA to/from the card. To ensure that FS accesses
* by a game does not break DMA, it is important to save and restore
* this register every time the file system is accessed.
*
* NOTE: osBbAuxDataInit should be called before using this function.
*/
s32
osBbGameCommitState()
{
char state_name[NAME_LEN];
OSBbStateVector sv;
s32 rv, binding[MAXCONTROLLERS];
u32 stateDirty = 0;
u32 old = IO_READ(PI_FLASH_CTRL_REG); // Save reg
/* retrieve name from stash, retrieve state, write to file */
__osBbGameUnstashState(state_name, &sv, binding, &stateDirty);
rv = __osBbSaveState(state_name, sv, binding, stateDirty);
IO_WRITE(PI_FLASH_CTRL_REG, old); // Restore reg
return rv;
}
/* Initialize the game state parameters */
void osBbInitState(const char* name)
{
int i;
__osBbStashMagic = STASH_MAGIC;
/* Initialize parameters used by the game */
memcpy(__osBbStateName, name, NAME_LEN);
/* No other state information */
__osBbEepromSize = 0;
__osBbEepromAddress = NULL;
__osBbFlashSize = 0;
__osBbFlashAddress = NULL;
__osBbSramSize = 0;
__osBbSramAddress = NULL;
__osBbPakSize = 0;
for (i = 0; i < MAXCONTROLLERS; i++)
__osBbPakAddress[i] = NULL;
for (i = 0; i < MAXCONTROLLERS; i++)
__osBbPakBindings[i] = -1;
__osBbStateDirty = 0;
/* Clear to indicate no state info for the viewer */
__osBbClearState();
}
s32 osBbLoadState(const char *name, BbTicketId tid, OSBbStateVector *sv, s32 binding[MAXCONTROLLERS])
{
s32 rv = 0;
char state_name[NAME_LEN];
char pak_name[NAME_LEN];
int i, fd;
u32 off = 0;
u8 *block;
u32 stateDirty = 0;
bzero(state_name, NAME_LEN);
__osBbGetStateFilename(name, state_name);
__osBbStashMagic = STASH_MAGIC;
memcpy(__osBbStateName, state_name, NAME_LEN);
__osBbEepromSize = sv->eepromSize;
__osBbEepromAddress = (void*)sv->eepromAddress;
__osBbFlashSize = sv->flashSize;
__osBbFlashAddress = (void*)sv->flashAddress;
__osBbSramSize = sv->sramSize;
__osBbSramAddress = (void*)sv->sramAddress;
if ((block = __osBbEepromAddress) ||
(block = __osBbFlashAddress) ||
(block = __osBbSramAddress) ) {
if ((fd = osBbFOpen(state_name, "r")) >= 0) {
if (!osBbVerifyFile(state_name, block))
stateDirty = 1;
if (sv->eepromSize) {
osBbFRead(fd, off, (void*)sv->eepromAddress, BLOCK_ROUND(sv->eepromSize));
off += BLOCK_ROUND(sv->eepromSize);
}
if (sv->flashSize) {
osBbFRead(fd, off, (void*)sv->flashAddress, BLOCK_ROUND(sv->flashSize));
off += BLOCK_ROUND(sv->flashSize);
}
if (sv->sramSize) {
osBbFRead(fd, off, (void*)sv->sramAddress, BLOCK_ROUND(sv->sramSize));
off += BLOCK_ROUND(sv->sramSize);
}
osBbFClose(fd);
} else {
if (sv->eepromSize) {
bzero((void*)sv->eepromAddress, sv->eepromSize);
}
if (sv->flashSize) {
bzero((void*)sv->flashAddress, sv->flashSize);
}
if (sv->sramSize) {
bzero((void*)sv->sramAddress, sv->sramSize);
}
}
}
__osBbStateDirty = stateDirty;
__osBbPakSize = sv->pakSize;
for (i = 0; i < MAXCONTROLLERS; ++i) {
__osBbPakBindings[i] = binding[i];
if (binding[i] != -1 && sv->pakAddress[i]) {
__osBbPakAddress[i] = (void *)sv->pakAddress[i];
sprintf(pak_name, "%ld.pak", binding[i]);
if ((fd = osBbFOpen(pak_name, "r")) >= 0) {
if (!osBbVerifyFile(pak_name, (u8*)sv->pakAddress[i])) {
binding[i] |= DONT_SIGN_BIT;
}
osBbFRead(fd, 0, (void*)sv->pakAddress[i], BLOCK_ROUND(sv->pakSize));
} else {
/* XXX cheesy hack to try to initialize a cp */
OSPfs pfs;
extern u32 __osGetId(OSPfs *);
bzero (&pfs, sizeof(pfs));
pfs.channel = i;
bzero((void*)sv->pakAddress[i], sv->pakSize);
__osGetId(&pfs);
}
} else {
__osBbPakAddress[i] = 0;
}
}
__osBbStashState(state_name, tid, sv, binding, stateDirty);
return rv;
}
s32 osBbGetLaunchMetaData(OSBbLaunchMetaData *md, u16 *blockList, s32 listSize)
{
int i;
u32 *p = (u32 *)md;
OSPiHandle* handler;
s32 rv = 0;
u16 lastBlock[2];
lastBlock[0] = blockList[listSize-1];
lastBlock[1] = 0;
if ((rv = osBbAtbSetup(PI_DOM1_ADDR2, lastBlock, 2)) < 0)
goto out;
handler = osCartRomInit();
IO_WRITE(PI_AES_CTRL_REG, 0);
IO_WRITE(PI_ERROR_REG, 0);
IO_WRITE(PI_FLASH_CTRL_REG, PI_FLASH_CTRL_RDPH |
(0xf << PI_FLASH_CTRL_ADPH_SHIFT) |
PI_FLASH_CTRL_WRDY |
PI_FLASH_CTRL_ECC |
0x3ff);
for (i = BB_FL_BLOCK_SIZE-sizeof(OSBbLaunchMetaData); i < BB_FL_BLOCK_SIZE; i += 4) {
osEPiReadIo(handler, i, p++);
}
if ((md->magic & 0xFFFFFF00) != BB_METADATA_MAGIC)
rv = -1;
out:
if (rv < 0) {
bzero(md, sizeof(OSBbLaunchMetaData));
md->memSize = 0x400000;
md->tvType = OS_TV_NTSC;
md->romBase = PHYS_TO_K1(PI_DOM1_ADDR2);
}
return rv;
}