summaryrefslogtreecommitdiff
path: root/firmware/test/fat/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/test/fat/main.c')
-rw-r--r--firmware/test/fat/main.c724
1 files changed, 0 insertions, 724 deletions
diff --git a/firmware/test/fat/main.c b/firmware/test/fat/main.c
deleted file mode 100644
index e838682b0b..0000000000
--- a/firmware/test/fat/main.c
+++ /dev/null
@@ -1,724 +0,0 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdarg.h>
5#include <time.h>
6#include "fat.h"
7#include "debug.h"
8#include "disk.h"
9#include "dir.h"
10#include "file.h"
11#include "ata.h"
12#include "storage.h"
13
14void dbg_dump_sector(int sec);
15void dbg_dump_buffer(unsigned char *buf, int len, int offset);
16void dbg_console(void);
17
18void mutex_init(struct mutex* l) {}
19void mutex_lock(struct mutex* l) {}
20void mutex_unlock(struct mutex* l) {}
21
22void panicf( char *fmt, ...)
23{
24 va_list ap;
25 va_start( ap, fmt );
26 fprintf(stderr,"***PANIC*** ");
27 vfprintf(stderr, fmt, ap );
28 va_end( ap );
29 exit(1);
30}
31
32void debugf(const char *fmt, ...)
33{
34 va_list ap;
35 va_start( ap, fmt );
36 fprintf(stderr,"DEBUGF: ");
37 vfprintf( stderr, fmt, ap );
38 va_end( ap );
39}
40
41void ldebugf(const char* file, int line, const char *fmt, ...)
42{
43 va_list ap;
44 va_start( ap, fmt );
45 fprintf( stderr, "%s:%d ", file, line );
46 vfprintf( stderr, fmt, ap );
47 va_end( ap );
48}
49
50void dbg_dump_sector(int sec)
51{
52 unsigned char buf[SECTOR_SIZE];
53
54 storage_read_sectors(sec,1,buf);
55 DEBUGF("---< Sector %d >-----------------------------------------\n", sec);
56 dbg_dump_buffer(buf, SECTOR_SIZE, 0);
57}
58
59void dbg_dump_buffer(unsigned char *buf, int len, int offset)
60{
61 int i, j;
62 unsigned char c;
63 unsigned char ascii[33];
64
65 for(i = 0;i < len/16;i++)
66 {
67 DEBUGF("%03x: ", i*16 + offset);
68 for(j = 0;j < 16;j++)
69 {
70 c = buf[i*16+j];
71
72 DEBUGF("%02x ", c);
73 if(c < 32 || c > 127)
74 {
75 ascii[j] = '.';
76 }
77 else
78 {
79 ascii[j] = c;
80 }
81 }
82
83 ascii[j] = 0;
84 DEBUGF("%s\n", ascii);
85 }
86}
87
88void dbg_dir(char* currdir)
89{
90 DIR* dir;
91 struct dirent* entry;
92
93 dir = opendir(currdir);
94 if (dir)
95 {
96 while ( (entry = readdir(dir)) ) {
97 DEBUGF("%15s %lx\n", entry->d_name, entry->startcluster);
98 }
99 closedir(dir);
100 }
101 else
102 {
103 DEBUGF( "Could not open dir %s\n", currdir);
104 }
105}
106
107#define CHUNKSIZE 8
108#define BUFSIZE 8192
109
110int dbg_mkfile(char* name, int num)
111{
112 char text[BUFSIZE+1];
113 int i;
114 int fd;
115 int x=0;
116 bool stop = false;
117
118 fd = creat(name,O_WRONLY);
119 if (fd<0) {
120 DEBUGF("Failed creating file\n");
121 return -1;
122 }
123 num *= 1024;
124 while ( num ) {
125 int rc;
126 int len = num > BUFSIZE ? BUFSIZE : num;
127
128 for (i=0; i<len/CHUNKSIZE; i++ )
129 sprintf(text+i*CHUNKSIZE,"%c%06x,",name[1],x++);
130
131 rc = write(fd, text, len);
132 if ( rc < 0 ) {
133 DEBUGF("Failed writing data\n");
134 return -1;
135 }
136 else
137 if ( rc == 0 ) {
138 DEBUGF("No space left\n");
139 return -2;
140 }
141 else
142 DEBUGF("wrote %d bytes\n",rc);
143
144 num -= len;
145
146 if ( !num ) {
147 if ( stop )
148 break;
149
150 /* add a random number of chunks to test byte-copy code */
151 num = ((int) rand() % SECTOR_SIZE) & ~7;
152 LDEBUGF("Adding random size %d\n",num);
153 stop = true;
154 }
155 }
156
157 return close(fd);
158}
159
160
161int dbg_chkfile(char* name, int size)
162{
163 char text[81920];
164 int i;
165 int x=0;
166 int pos = 0;
167 int block=0;
168 int fd = open(name,O_RDONLY);
169 if (fd<0) {
170 DEBUGF("Failed opening file\n");
171 return -1;
172 }
173
174 size = lseek(fd, 0, SEEK_END);
175 DEBUGF("File is %d bytes\n", size);
176 /* random start position */
177 if ( size )
178 pos = ((int)rand() % size) & ~7;
179 lseek(fd, pos, SEEK_SET);
180 x = pos / CHUNKSIZE;
181
182 LDEBUGF("Check base is %x (%d)\n",x,pos);
183
184 while (1) {
185 int rc = read(fd, text, sizeof text);
186 DEBUGF("read %d bytes\n",rc);
187 if (rc < 0) {
188 panicf("Failed reading data\n");
189 }
190 else {
191 char tmp[CHUNKSIZE+1];
192 if (!rc)
193 break;
194 for (i=0; i<rc/CHUNKSIZE; i++ ) {
195 sprintf(tmp,"%c%06x,",name[1],x++);
196 if (strncmp(text+i*CHUNKSIZE,tmp,CHUNKSIZE)) {
197 int idx = pos + block*sizeof(text) + i*CHUNKSIZE;
198 DEBUGF("Mismatch in byte 0x%x (byte 0x%x of sector 0x%x)."
199 "\nExpected %.8s found %.8s\n",
200 idx, idx % SECTOR_SIZE, idx / SECTOR_SIZE,
201 tmp,
202 text+i*CHUNKSIZE);
203 DEBUGF("i=%x, idx=%x\n",i,idx);
204 dbg_dump_buffer(text+i*CHUNKSIZE - 0x20, 0x40, idx - 0x20);
205 return -1;
206 }
207 }
208 }
209 block++;
210 }
211
212 return close(fd);
213}
214
215int dbg_wrtest(char* name)
216{
217 char text[81920];
218 int i;
219 int x=0;
220 int pos = 0;
221 int block=0;
222 int size, fd, rc;
223 char tmp[CHUNKSIZE+1];
224
225 fd = open(name,O_RDWR);
226 if (fd<0) {
227 DEBUGF("Failed opening file\n");
228 return -1;
229 }
230
231 size = lseek(fd, 0, SEEK_END);
232 DEBUGF("File is %d bytes\n", size);
233 /* random start position */
234 if ( size )
235 pos = ((int)rand() % size) & ~7;
236 rc = lseek(fd, pos, SEEK_SET);
237 if ( rc < 0 )
238 panicf("Failed seeking\n");
239 x = pos / CHUNKSIZE;
240 LDEBUGF("Check base is %x (%d)\n",x,pos);
241
242 sprintf(tmp,"%c%06x,",name[1],x++);
243 rc = write(fd, tmp, 8);
244 if ( rc < 0 )
245 panicf("Failed writing data\n");
246
247 if ( size )
248 pos = ((int)rand() % size) & ~7;
249 rc = lseek(fd, pos, SEEK_SET);
250 if ( rc < 0 )
251 panicf("Failed seeking\n");
252 x = pos / CHUNKSIZE;
253 LDEBUGF("Check base 2 is %x (%d)\n",x,pos);
254
255 while (1) {
256 rc = read(fd, text, sizeof text);
257 DEBUGF("read %d bytes\n",rc);
258 if (rc < 0) {
259 panicf("Failed reading data\n");
260 }
261 else {
262 if (!rc)
263 break;
264 for (i=0; i<rc/CHUNKSIZE; i++ ) {
265 sprintf(tmp,"%c%06x,",name[1],x++);
266 if (strncmp(text+i*CHUNKSIZE,tmp,CHUNKSIZE)) {
267 int idx = pos + block*sizeof(text) + i*CHUNKSIZE;
268 DEBUGF("Mismatch in byte 0x%x (byte 0x%x of sector 0x%x)."
269 "\nExpected %.8s found %.8s\n",
270 idx, idx % SECTOR_SIZE, idx / SECTOR_SIZE,
271 tmp,
272 text+i*CHUNKSIZE);
273 DEBUGF("i=%x, idx=%x\n",i,idx);
274 dbg_dump_buffer(text+i*CHUNKSIZE - 0x20, 0x40, idx - 0x20);
275 return -1;
276 }
277 }
278 }
279 block++;
280 }
281
282 return close(fd);
283}
284
285void dbg_type(char* name)
286{
287 const int size = SECTOR_SIZE*5;
288 unsigned char buf[SECTOR_SIZE*5+1];
289 int fd,rc;
290
291 fd = open(name,O_RDONLY);
292 if (fd<0)
293 return;
294 DEBUGF("Got file descriptor %d\n",fd);
295
296 while ( 1 ) {
297 rc = read(fd, buf, size);
298 if( rc > 0 )
299 {
300 buf[size] = 0;
301 printf("%d: %.*s\n", rc, rc, buf);
302 }
303 else if ( rc == 0 ) {
304 DEBUGF("EOF\n");
305 break;
306 }
307 else
308 {
309 DEBUGF("Failed reading file: %d\n",rc);
310 break;
311 }
312 }
313 close(fd);
314}
315
316int dbg_append(char* name)
317{
318 int x=0;
319 int size, fd, rc;
320 char tmp[CHUNKSIZE+1];
321
322 fd = open(name,O_RDONLY);
323 if (fd<0) {
324 DEBUGF("Failed opening file\n");
325 return -1;
326 }
327
328 size = lseek(fd, 0, SEEK_END);
329 DEBUGF("File is %d bytes\n", size);
330 x = size / CHUNKSIZE;
331 LDEBUGF("Check base is %x (%d)\n",x,size);
332
333 if (close(fd) < 0)
334 return -1;
335
336 fd = open(name,O_RDWR|O_APPEND);
337 if (fd<0) {
338 DEBUGF("Failed opening file\n");
339 return -1;
340 }
341
342 sprintf(tmp,"%c%06x,",name[1],x++);
343 rc = write(fd, tmp, 8);
344 if ( rc < 0 )
345 panicf("Failed writing data\n");
346
347 return close(fd);
348}
349
350int dbg_test(char* name)
351{
352 int x=0;
353 int j;
354 int fd;
355 char text[BUFSIZE+1];
356
357 for (j=0; j<5; j++) {
358 int num = 40960;
359
360 fd = open(name,O_WRONLY|O_CREAT|O_APPEND, 0666);
361 if (fd<0) {
362 DEBUGF("Failed opening file\n");
363 return -1;
364 }
365
366 while ( num ) {
367 int rc, i;
368 int len = num > BUFSIZE ? BUFSIZE : num;
369
370 for (i=0; i<len/CHUNKSIZE; i++ )
371 sprintf(text+i*CHUNKSIZE,"%c%06x,",name[1],x++);
372
373 rc = write(fd, text, len);
374 if ( rc < 0 ) {
375 DEBUGF("Failed writing data\n");
376 return -1;
377 }
378 else
379 if ( rc == 0 ) {
380 DEBUGF("No space left\n");
381 return -2;
382 }
383 else
384 DEBUGF("wrote %d bytes\n",rc);
385
386 num -= len;
387 }
388
389 if (close(fd) < 0)
390 return -1;
391 }
392
393 return 0;
394}
395
396int dbg_dump(char* name, int offset)
397{
398 char buf[SECTOR_SIZE];
399
400 int rc;
401 int fd = open(name,O_RDONLY);
402 if (fd<0) {
403 DEBUGF("Failed opening file\n");
404 return -1;
405 }
406 lseek(fd, offset, SEEK_SET);
407 rc = read(fd, buf, sizeof buf);
408
409 if ( rc < 0 )
410 panicf("Error reading data\n");
411
412 if (close(fd) < 0)
413 return -1;
414
415 dbg_dump_buffer(buf, rc, offset);
416
417 return 0;
418}
419
420void dbg_tail(char* name)
421{
422 unsigned char buf[SECTOR_SIZE*5];
423 int fd,rc;
424
425 fd = open(name,O_RDONLY);
426 if (fd<0)
427 return;
428 DEBUGF("Got file descriptor %d\n",fd);
429
430 rc = lseek(fd,-SECTOR_SIZE,SEEK_END);
431 if ( rc >= 0 ) {
432 rc = read(fd, buf, SECTOR_SIZE);
433 if( rc > 0 )
434 {
435 buf[rc]=0;
436 printf("%d:\n%s\n", (int)strlen(buf), buf);
437 }
438 else if ( rc == 0 ) {
439 DEBUGF("EOF\n");
440 }
441 else
442 {
443 DEBUGF("Failed reading file: %d\n",rc);
444 }
445 }
446 else {
447 perror("lseek");
448 }
449
450 close(fd);
451}
452
453int dbg_head(char* name)
454{
455 unsigned char buf[SECTOR_SIZE*5];
456 int fd,rc;
457
458 fd = open(name,O_RDONLY);
459 if (fd<0)
460 return -1;
461 DEBUGF("Got file descriptor %d\n",fd);
462
463 rc = read(fd, buf, SECTOR_SIZE*3);
464 if( rc > 0 )
465 {
466 buf[rc]=0;
467 printf("%d:\n%s\n", (int)strlen(buf), buf);
468 }
469 else if ( rc == 0 ) {
470 DEBUGF("EOF\n");
471 }
472 else
473 {
474 DEBUGF("Failed reading file: %d\n",rc);
475 }
476
477 return close(fd);
478}
479
480int dbg_trunc(char* name, int size)
481{
482 int fd,rc;
483
484#if 1
485 fd = open(name,O_RDWR);
486 if (fd<0)
487 return -1;
488
489 rc = ftruncate(fd, size);
490 if (rc<0) {
491 DEBUGF("ftruncate(%d) failed\n", size);
492 return -2;
493 }
494
495#else
496 fd = open(name,O_RDWR|O_TRUNC);
497 if (fd<0)
498 return -1;
499
500 rc = lseek(fd, size, SEEK_SET);
501 if (fd<0)
502 return -2;
503#endif
504
505 return close(fd);
506}
507
508int dbg_mkdir(char* name)
509{
510 int fd;
511
512 fd = mkdir(name);
513 if (fd<0) {
514 DEBUGF("Failed creating directory\n");
515 return -1;
516 }
517 return 0;
518}
519
520int dbg_cmd(int argc, char *argv[])
521{
522 char* cmd = NULL;
523 char* arg1 = NULL;
524 char* arg2 = NULL;
525
526 if (argc > 1) {
527 cmd = argv[1];
528 if ( argc > 2 ) {
529 arg1 = argv[2];
530 if ( argc > 3 ) {
531 arg2 = argv[3];
532 }
533 }
534 }
535 else {
536 DEBUGF("usage: fat command [options]\n"
537 "commands:\n"
538 " dir <dir>\n"
539 " ds <sector> - display sector\n"
540 " type <file>\n"
541 " head <file>\n"
542 " tail <file>\n"
543 " mkfile <file> <size (KB)>\n"
544 " chkfile <file>\n"
545 " del <file>\n"
546 " rmdir <dir>\n"
547 " dump <file> <offset>\n"
548 " mkdir <dir>\n"
549 " trunc <file> <size>\n"
550 " wrtest <file>\n"
551 " append <file>\n"
552 " test <file>\n"
553 " ren <file> <newname>\n"
554 );
555 return -1;
556 }
557
558 if (!strcasecmp(cmd, "dir"))
559 {
560 if ( arg1 )
561 dbg_dir(arg1);
562 else
563 dbg_dir("/");
564 }
565
566 if (!strcasecmp(cmd, "ds"))
567 {
568 if ( arg1 ) {
569 DEBUGF("secnum: %ld\n", strtol(arg1, NULL, 0));
570 dbg_dump_sector(strtol(arg1, NULL, 0));
571 }
572 }
573
574 if (!strcasecmp(cmd, "type"))
575 {
576 if (arg1)
577 dbg_type(arg1);
578 }
579
580 if (!strcasecmp(cmd, "head"))
581 {
582 if (arg1)
583 return dbg_head(arg1);
584 }
585
586 if (!strcasecmp(cmd, "tail"))
587 {
588 if (arg1)
589 dbg_tail(arg1);
590 }
591
592 if (!strcasecmp(cmd, "mkfile"))
593 {
594 if (arg1) {
595 if (arg2)
596 return dbg_mkfile(arg1,strtol(arg2, NULL, 0));
597 else
598 return dbg_mkfile(arg1,1);
599 }
600 }
601
602 if (!strcasecmp(cmd, "chkfile"))
603 {
604 if (arg1) {
605 if (arg2)
606 return dbg_chkfile(arg1, strtol(arg2, NULL, 0));
607 else
608 return dbg_chkfile(arg1, 0);
609 }
610 }
611
612 if (!strcasecmp(cmd, "mkdir"))
613 {
614 if (arg1) {
615 return dbg_mkdir(arg1);
616 }
617 }
618
619 if (!strcasecmp(cmd, "del"))
620 {
621 if (arg1)
622 return remove(arg1);
623 }
624
625 if (!strcasecmp(cmd, "rmdir"))
626 {
627 if (arg1)
628 return rmdir(arg1);
629 }
630
631 if (!strcasecmp(cmd, "dump"))
632 {
633 if (arg1) {
634 if (arg2)
635 return dbg_dump(arg1, strtol(arg2, NULL, 0));
636 else
637 return dbg_dump(arg1, 0);
638 }
639 }
640
641 if (!strcasecmp(cmd, "wrtest"))
642 {
643 if (arg1)
644 return dbg_wrtest(arg1);
645 }
646
647 if (!strcasecmp(cmd, "append"))
648 {
649 if (arg1)
650 return dbg_append(arg1);
651 }
652
653 if (!strcasecmp(cmd, "test"))
654 {
655 if (arg1)
656 return dbg_test(arg1);
657 }
658
659 if (!strcasecmp(cmd, "trunc"))
660 {
661 if (arg1 && arg2)
662 return dbg_trunc(arg1, strtol(arg2, NULL, 0));
663 }
664
665 if (!strcasecmp(cmd, "ren"))
666 {
667 if (arg1 && arg2)
668 return rename(arg1, arg2);
669 }
670
671 return 0;
672}
673
674extern void ata_exit(void);
675
676int main(int argc, char *argv[])
677{
678 int rc,i;
679 struct partinfo* pinfo;
680
681 srand(clock());
682
683 if(ata_init()) {
684 DEBUGF("*** Warning! The disk is uninitialized\n");
685 return -1;
686 }
687 pinfo = disk_init();
688 if (!pinfo) {
689 DEBUGF("*** Failed reading partitions\n");
690 return -1;
691 }
692
693 for ( i=0; i<4; i++ ) {
694 if ( pinfo[i].type == PARTITION_TYPE_FAT32
695#ifdef HAVE_FAT16SUPPORT
696 || pinfo[i].type == PARTITION_TYPE_FAT16
697#endif
698 ) {
699 DEBUGF("*** Mounting at block %ld\n",pinfo[i].start);
700 rc = fat_mount(IF_MV(0,) IF_MD(0,) pinfo[i].start);
701 if(rc) {
702 DEBUGF("mount: %d",rc);
703 return -1;
704 }
705 break;
706 }
707 }
708 if ( i==4 ) {
709 if(fat_mount(IF_MV(0,) IF_MD(0,) 0)) {
710 DEBUGF("No FAT32 partition!");
711 return -1;
712 }
713 }
714
715 rc = dbg_cmd(argc, argv);
716
717 ata_exit();
718
719 if (rc)
720 DEBUGF("Return code: %d\n", rc);
721
722 return rc;
723}
724