fs.c
10.1 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
#include "ultra64.h"
#include <PR/bcp.h>
#include "os_bb.h"
#include "fs.h"
#define DMA_QUEUE_SIZE 200
#define PRINTF osSyncPrintf
#define MSG_FAULT 0x10
/*
* Thread and stack structures
*/
char bootStack[STACKSIZE] __attribute__ ((aligned (8)));
static OSThread idleThread;
static char idleThreadStack[STACKSIZE] __attribute__ ((aligned (8)));
static OSThread mainThread;
static char mainThreadStack[STACKSIZE] __attribute__ ((aligned (8)));
/*
* Message queues and message buffers used by this app
*/
static OSMesg PiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue PiMessageQ;
static OSMesg SiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue SiMessageQ;
/*
* Local variables and routines
*/
static void idleproc(char *);
static void mainproc(char *);
void
boot(void)
{
osInitialize();
osCreateThread(&idleThread, 1, (void(*)(void *))idleproc, (void *)0,
idleThreadStack+STACKSIZE, 8);
osStartThread(&idleThread);
}
static void
idleproc(char *argv) /* priority 8 */
{
osCreateViManager(OS_PRIORITY_VIMGR);
osViSetMode(&osViModeTable[OS_VI_NTSC_LPN1]);
/*
* Start PI Mgr for access to cartridge - start before the debugger
*/
osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
DMA_QUEUE_SIZE);
osCreateMesgQueue(&SiMessageQ, SiMessages, DMA_QUEUE_SIZE);
osSetEventMesg(OS_EVENT_SI, &SiMessageQ, (OSMesg)DMA_QUEUE_SIZE);
/*
* The main thread's priority must be the same or lower than the original
* idle's thread priority. This allows the idle thread to change its
* priority to 0 before the main thread starts execution.
*/
osCreateThread(&mainThread, 3, (void(*)(void *))mainproc, argv,
mainThreadStack+STACKSIZE/8, (OSPri)7);
osStartThread(&mainThread);
osSetThreadPri(0, OS_PRIORITY_IDLE);
for(;;); /* idle thread */
}
static OSMesgQueue retraceMessageQ;
static OSMesg dummyMessage, retraceMessageBuf;
static u8 block[1024*16];
static OSBbFs fs;
#define NDIR 100
static OSBbDirEnt dir[NDIR];
static OSBbStatBuf sb;
static OSBbStatFs fsb;
static u16 blist[256];
static u8 blocks[16*BB_FL_BLOCK_SIZE];
static u32
dump_file(const char* name, int c) {
int i, j, rv;
u32 off;
u16 sum = 0;
if ((i = osBbFOpen(name, "r")) < 0) {
PRINTF("osBbFOpen %s failed %d\n", name, i);
return 0;
}
if ((rv = osBbFStat(i, &sb, NULL, 0)) < 0) {
PRINTF("osBbFStat failed %d\n", rv);
return 0;
}
PRINTF("type %d size %d\n", sb.type, sb.size);
if ((rv = osBbFStat(i, &sb, blist, sizeof(blist)/sizeof(blist[0]))) < 0) {
PRINTF("osBbFStat failed %d\n", rv);
return 0;
}
PRINTF("blocks: ");
for(j = 0; j < BB_FL_BYTE_TO_BLOCK(sb.size+BB_FL_BLOCK_SIZE-1); j++)
PRINTF("%d ", blist[j]);
PRINTF("\n");
for(off = 0; off < sb.size;) {
if ((rv = osBbFRead(i, off, block, sizeof block)) < 0)
PRINTF("osBbFRead failed %d\n", rv);
off += BB_FL_BLOCK_SIZE;
for(j = 0; j < BB_FL_BLOCK_SIZE; j++) {
sum += block[j];
if (c)
PRINTF("%c", block[j]);
}
}
if ((rv = osBbFClose(i)) < 0) {
PRINTF("osBbFClose failed %d\n", rv);
return 0;
}
return sum;
}
static void
dump_dir(int count) {
int i;
for(i = 0; i < count; i++)
osSyncPrintf("%-12s %d %d\n", dir[i].name, dir[i].type, dir[i].size);
}
#if 0
IDE_WRITE(0x046fffe4, 1); /* backdoor module remove */
IDE_WRITE(0x046fffe4, 0); /* backdoor module insert */
#endif
struct {
char* name;
u16 sum;
} file[] = {
{ "zero" , 0 },
{ "one." , 0xe6b8 } ,
{ "two.x" , 0x9f21 } ,
{ "three.xy" , 0x5c1d } ,
{ "four.xyz" , 0x1e4a } ,
{ "fivefive.xyz" , 0x9a86 } ,
{ "2Mfile.xyz" , 0xaab0 } ,
{ 0, 0 }
};
static void
mainproc(char *argv) {
s32 count, i, j, rv;
u32 sum, sum2;
osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
osViSetEvent(&retraceMessageQ, dummyMessage, 1);
PRINTF("\n=> mainproc...\n");
PRINTF("init\n");
if ((rv = osBbFInit(&fs)) < 0) {
PRINTF("osBbFInit failed %d\n", rv);
goto out;
}
if ((rv = osBbFStatFs(&fsb)) < 0) {
PRINTF("osBbFStatFs failed %d\n", rv);
goto out;
}
if (fsb.files != 7 || fsb.freeFiles != BB_INODE16_ENTRIES-fsb.files ||
fsb.blocks != 143 || fsb.freeBlocks != 3873) {
PRINTF("osBbFFStatFS files %d free files %d blocks %d free blocks %d\n", fsb.files, fsb.freeFiles, fsb.blocks, fsb.freeBlocks);
goto out;
}
if ((count = osBbFReadDir(dir, NDIR)) < 0 || count != 7) {
PRINTF("osBbFReadDir failed %d\n", count);
goto out;
}
//dump_dir(count);
for(i = 0; file[i].name; i++) {
if ((rv = dump_file(file[i].name, 0)) != file[i].sum) {
PRINTF("bad sum %s expected %x computed %x\n", file[i].name, file[i].sum, rv);
goto out;
}
}
if ((i = osBbFCreate("zero", 1, BB_FL_BLOCK_SIZE)) != BBFS_ERR_EXISTS) {
PRINTF("osBbFCreate (exists) failed\n", i);
goto out;
}
if ((i = osBbFCreate("big.c", 1, 1024*16*5000)) != BBFS_ERR_SPACE) {
PRINTF("osBbFCreate too big file failed %d\n", i);
goto out;
}
if ((i = osBbFCreate("junk.c", 1, BB_FL_BLOCK_SIZE)) < 0) {
PRINTF("osBbFCreate failed %d\n", i);
goto out;
}
bzero(block, sizeof block);
strcpy(block, "foobar from hell\n");
if ((rv = osBbFWrite(i, 0, block, sizeof block)) < 0) {
PRINTF("osBbFWrite failed %d\n", rv);
goto out;
}
if ((rv = osBbFWrite(i, 2, block, sizeof block/2)) != BBFS_ERR_INVALID) {
PRINTF("osBbFWrite unaligned length failed %d\n", rv);
goto out;
}
if ((rv = osBbFClose(i)) < 0) {
PRINTF("osBbFClose failed %d\n", rv);
goto out;
}
dump_file("junk.c", 0);
if ((rv = osBbFDelete("junk.c")) < 0) {
PRINTF("osBbFDelete failed %d\n", rv);
goto out;
}
/* make a big file */
sum = 0;
for(i = 0; i < 16; i++) {
for(j = 0; j < BB_FL_BLOCK_SIZE; j++) {
blocks[i*BB_FL_BLOCK_SIZE+j] = (j&1)?j : ((i<<4)|(j>>8));
sum += blocks[i*BB_FL_BLOCK_SIZE+j];
}
}
if ((i = osBbFCreate("big.c", 1, sizeof blocks)) < 0) {
PRINTF("osBbFCreate big.c failed %d\n", i);
goto out;
}
if ((rv = osBbFWrite(i, 0, blocks, sizeof blocks)) < 0) {
PRINTF("osBbFWrite big.c failed %d\n", rv);
goto out;
}
bzero(blocks, sizeof blocks);
if ((rv = osBbFRead(i, 0, blocks, sizeof blocks)) < 0 || rv != sizeof blocks) {
PRINTF("osBbFRead big.c failed %d\n", rv);
goto out;
}
sum2 = 0;
for(j = 0; j < 16; j++) {
int k;
for(k = 0; k < BB_FL_BLOCK_SIZE; k++) {
sum2 += blocks[j*BB_FL_BLOCK_SIZE+k];
}
}
if (sum != sum2) {
PRINTF("osBbFRead big.c sum mismatch read %x expected %x\n", sum2, sum);
goto out;
}
dump_file("big.c", 0);
if ((rv = osBbFRepairBlock(i, 0, blocks, BB_FL_BLOCK_SIZE)) < 0 || rv != BB_FL_BLOCK_SIZE) {
PRINTF("osBbFRepairBlock big.c failed %d\n", rv);
goto out;
}
dump_file("big.c", 0);
bzero(blocks, sizeof blocks);
if ((rv = osBbFRead(i, 0, blocks, sizeof blocks)) < 0 || rv != sizeof blocks) {
PRINTF("osBbFRead2 big.c failed %d\n", rv);
goto out;
}
sum2 = 0;
for(j = 0; j < 16; j++) {
int k;
for(k = 0; k < BB_FL_BLOCK_SIZE; k++) {
sum2 += blocks[j*BB_FL_BLOCK_SIZE+k];
}
}
if (sum != sum2) {
PRINTF("osBbFRead2 big.c sum mismatch read %x expected %x\n", sum2, sum);
goto out;
}
if ((rv = osBbFClose(i)) < 0) {
PRINTF("osBbFClose big.c failed %d\n", rv);
goto out;
}
if ((rv = osBbFStatFs(&fsb)) < 0) {
PRINTF("osBbFStat Fsfailed %d\n", rv);
goto out;
}
PRINTF("files %d free %d blocks %d free %d\n", fsb.files, fsb.freeFiles, fsb.blocks, fsb.freeBlocks);
if ((rv == osBbFRename("big.c", "notbig.c")) < 0) {
PRINTF("osBbFRename failed %d\n", rv);
goto out;
}
if ((rv = osBbFDelete("big.c")) != BBFS_ERR_ENTRY) {
PRINTF("osBbFDelete big.c failed %d\n", rv);
goto out;
}
if ((rv = osBbFDelete("notbig.c")) < 0) {
PRINTF("osBbFDelete notbig.c failed %d\n", rv);
goto out;
}
/* test bad parameters descriptors */
if ((rv = osBbFClose(-1)) != BBFS_ERR_INVALID ||
(rv = osBbFClose(1000)) != BBFS_ERR_INVALID ||
(rv = osBbFClose(400)) != BBFS_ERR_INVALID ||
(rv = osBbFRead(-1, 0, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFRead(1000, 0, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFRead(400, 0, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFRead(0, 0, blocks, 1)) != BBFS_ERR_INVALID ||
(rv = osBbFRead(0, 0, blocks, BB_FL_BLOCK_SIZE*1000)) != BBFS_ERR_INVALID ||
(rv = osBbFRead(0, BB_FL_BLOCK_SIZE*1000, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFRead(0, 1, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFWrite(1000, 0, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFWrite(400, 0, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFWrite(0, 0, blocks, 1)) != BBFS_ERR_INVALID ||
(rv = osBbFWrite(0, 0, blocks, BB_FL_BLOCK_SIZE*1000)) != BBFS_ERR_INVALID ||
(rv = osBbFWrite(0, BB_FL_BLOCK_SIZE*1000, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFWrite(0, 1, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFStat(-1, 0, 0, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFStat(1000, 0, 0, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFStat(400, 0, 0, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFOpen("", "r")) != BBFS_ERR_INVALID ||
(rv = osBbFOpen(".", "r")) != BBFS_ERR_INVALID ||
(rv = osBbFOpen("blahblah", "r")) != BBFS_ERR_ENTRY ||
(rv = osBbFCreate("", 1, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFCreate(".", 1, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFDelete("")) != BBFS_ERR_INVALID ||
(rv = osBbFDelete(".")) != BBFS_ERR_INVALID ||
(rv = osBbFRename("", "foo")) != BBFS_ERR_INVALID ||
(rv = osBbFRename(".", "foo")) != BBFS_ERR_INVALID ||
(rv = osBbFRename("foo", "")) != BBFS_ERR_INVALID ||
(rv = osBbFRename("foo", ".")) != BBFS_ERR_INVALID ||
(rv = osBbFRepairBlock(-1, 0, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFRepairBlock(400, 0, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFRepairBlock(0, BB_FL_BLOCK_SIZE*1000, blocks, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFRepairBlock(0, 0, blocks+1, 0)) != BBFS_ERR_INVALID ||
(rv = osBbFRepairBlock(-1, 0, blocks, BB_FL_BLOCK_SIZE*1000)) != BBFS_ERR_INVALID
) {
PRINTF("osBbFXXX invalid param failed %d\n", rv);
goto out;
}
out:
IO_WRITE(PI_GPIO_REG, 0|(1 << PI_GPIO_ENABLE_SHIFT));
for(;;) ;
}