12 void dbg_dump_sector(int sec
);
13 void dbg_dump_buffer(unsigned char *buf
, int len
, int offset
);
14 void dbg_console(void);
16 void mutex_init(void* l
) {}
17 void mutex_lock(void* l
) {}
18 void mutex_unlock(void* l
) {}
20 void panicf( char *fmt
, ...)
24 fprintf(stderr
,"***PANIC*** ");
25 vfprintf(stderr
, fmt
, ap
);
30 void debugf(const char *fmt
, ...)
34 fprintf(stderr
,"DEBUGF: ");
35 vfprintf( stderr
, fmt
, ap
);
39 void ldebugf(const char* file
, int line
, const char *fmt
, ...)
43 fprintf( stderr
, "%s:%d ", file
, line
);
44 vfprintf( stderr
, fmt
, ap
);
48 void dbg_dump_sector(int sec
)
50 unsigned char buf
[512];
52 ata_read_sectors(sec
,1,buf
);
53 DEBUGF("---< Sector %d >-----------------------------------------\n", sec
);
54 dbg_dump_buffer(buf
, 512, 0);
57 void dbg_dump_buffer(unsigned char *buf
, int len
, int offset
)
61 unsigned char ascii
[33];
63 for(i
= 0;i
< len
/16;i
++)
65 DEBUGF("%03x: ", i
*16 + offset
);
82 DEBUGF("%s\n", ascii
);
86 void dbg_dir(char* currdir
)
91 dir
= opendir(currdir
);
94 while ( (entry
= readdir(dir
)) ) {
95 DEBUGF("%15s (%d bytes) %x\n",
96 entry
->d_name
, entry
->size
, entry
->startcluster
);
102 DEBUGF( "Could not open dir %s\n", currdir
);
109 int dbg_mkfile(char* name
, int num
)
111 char text
[BUFSIZE
+1];
117 fd
= creat(name
,O_WRONLY
);
119 DEBUGF("Failed creating file\n");
125 int len
= num
> BUFSIZE
? BUFSIZE
: num
;
127 for (i
=0; i
<len
/CHUNKSIZE
; i
++ )
128 sprintf(text
+i
*CHUNKSIZE
,"%c%06x,",name
[1],x
++);
130 rc
= write(fd
, text
, len
);
132 DEBUGF("Failed writing data\n");
137 DEBUGF("No space left\n");
141 DEBUGF("wrote %d bytes\n",rc
);
149 /* add a random number of chunks to test byte-copy code */
150 num
= ((int) rand() % SECTOR_SIZE
) & ~7;
151 LDEBUGF("Adding random size %d\n",num
);
160 int dbg_chkfile(char* name
, int size
)
167 int fd
= open(name
,O_RDONLY
);
169 DEBUGF("Failed opening file\n");
173 size
= lseek(fd
, 0, SEEK_END
);
174 DEBUGF("File is %d bytes\n", size
);
175 /* random start position */
177 pos
= ((int)rand() % size
) & ~7;
178 lseek(fd
, pos
, SEEK_SET
);
181 LDEBUGF("Check base is %x (%d)\n",x
,pos
);
184 int rc
= read(fd
, text
, sizeof text
);
185 DEBUGF("read %d bytes\n",rc
);
187 panicf("Failed reading data\n");
190 char tmp
[CHUNKSIZE
+1];
193 for (i
=0; i
<rc
/CHUNKSIZE
; i
++ ) {
194 sprintf(tmp
,"%c%06x,",name
[1],x
++);
195 if (strncmp(text
+i
*CHUNKSIZE
,tmp
,CHUNKSIZE
)) {
196 int idx
= pos
+ block
*sizeof(text
) + i
*CHUNKSIZE
;
197 DEBUGF("Mismatch in byte 0x%x (byte 0x%x of sector 0x%x)."
198 "\nExpected %.8s found %.8s\n",
199 idx
, idx
% SECTOR_SIZE
, idx
/ SECTOR_SIZE
,
202 DEBUGF("i=%x, idx=%x\n",i
,idx
);
203 dbg_dump_buffer(text
+i
*CHUNKSIZE
- 0x20, 0x40, idx
- 0x20);
214 int dbg_wrtest(char* name
)
222 char tmp
[CHUNKSIZE
+1];
224 fd
= open(name
,O_RDWR
);
226 DEBUGF("Failed opening file\n");
230 size
= lseek(fd
, 0, SEEK_END
);
231 DEBUGF("File is %d bytes\n", size
);
232 /* random start position */
234 pos
= ((int)rand() % size
) & ~7;
235 rc
= lseek(fd
, pos
, SEEK_SET
);
237 panicf("Failed seeking\n");
239 LDEBUGF("Check base is %x (%d)\n",x
,pos
);
241 sprintf(tmp
,"%c%06x,",name
[1],x
++);
242 rc
= write(fd
, tmp
, 8);
244 panicf("Failed writing data\n");
247 pos
= ((int)rand() % size
) & ~7;
248 rc
= lseek(fd
, pos
, SEEK_SET
);
250 panicf("Failed seeking\n");
252 LDEBUGF("Check base 2 is %x (%d)\n",x
,pos
);
255 rc
= read(fd
, text
, sizeof text
);
256 DEBUGF("read %d bytes\n",rc
);
258 panicf("Failed reading data\n");
263 for (i
=0; i
<rc
/CHUNKSIZE
; i
++ ) {
264 sprintf(tmp
,"%c%06x,",name
[1],x
++);
265 if (strncmp(text
+i
*CHUNKSIZE
,tmp
,CHUNKSIZE
)) {
266 int idx
= pos
+ block
*sizeof(text
) + i
*CHUNKSIZE
;
267 DEBUGF("Mismatch in byte 0x%x (byte 0x%x of sector 0x%x)."
268 "\nExpected %.8s found %.8s\n",
269 idx
, idx
% SECTOR_SIZE
, idx
/ SECTOR_SIZE
,
272 DEBUGF("i=%x, idx=%x\n",i
,idx
);
273 dbg_dump_buffer(text
+i
*CHUNKSIZE
- 0x20, 0x40, idx
- 0x20);
284 void dbg_type(char* name
)
286 const int size
= SECTOR_SIZE
*5;
287 unsigned char buf
[SECTOR_SIZE
*5+1];
290 fd
= open(name
,O_RDONLY
);
293 DEBUGF("Got file descriptor %d\n",fd
);
296 rc
= read(fd
, buf
, size
);
300 printf("%d: %.*s\n", rc
, rc
, buf
);
302 else if ( rc
== 0 ) {
308 DEBUGF("Failed reading file: %d\n",rc
);
315 int dbg_append(char* name
)
319 char tmp
[CHUNKSIZE
+1];
321 fd
= open(name
,O_RDONLY
);
323 DEBUGF("Failed opening file\n");
327 size
= lseek(fd
, 0, SEEK_END
);
328 DEBUGF("File is %d bytes\n", size
);
329 x
= size
/ CHUNKSIZE
;
330 LDEBUGF("Check base is %x (%d)\n",x
,size
);
335 fd
= open(name
,O_RDWR
|O_APPEND
);
337 DEBUGF("Failed opening file\n");
341 sprintf(tmp
,"%c%06x,",name
[1],x
++);
342 rc
= write(fd
, tmp
, 8);
344 panicf("Failed writing data\n");
349 int dbg_test(char* name
)
354 char text
[BUFSIZE
+1];
356 for (j
=0; j
<5; j
++) {
359 fd
= open(name
,O_WRONLY
|O_CREAT
|O_APPEND
, 0666);
361 DEBUGF("Failed opening file\n");
367 int len
= num
> BUFSIZE
? BUFSIZE
: num
;
369 for (i
=0; i
<len
/CHUNKSIZE
; i
++ )
370 sprintf(text
+i
*CHUNKSIZE
,"%c%06x,",name
[1],x
++);
372 rc
= write(fd
, text
, len
);
374 DEBUGF("Failed writing data\n");
379 DEBUGF("No space left\n");
383 DEBUGF("wrote %d bytes\n",rc
);
395 int dbg_dump(char* name
, int offset
)
397 char buf
[SECTOR_SIZE
];
400 int fd
= open(name
,O_RDONLY
);
402 DEBUGF("Failed opening file\n");
405 lseek(fd
, offset
, SEEK_SET
);
406 rc
= read(fd
, buf
, sizeof buf
);
409 panicf("Error reading data\n");
414 dbg_dump_buffer(buf
, rc
, offset
);
419 void dbg_tail(char* name
)
421 unsigned char buf
[SECTOR_SIZE
*5];
424 fd
= open(name
,O_RDONLY
);
427 DEBUGF("Got file descriptor %d\n",fd
);
429 rc
= lseek(fd
,-512,SEEK_END
);
431 rc
= read(fd
, buf
, SECTOR_SIZE
);
435 printf("%d:\n%s\n", strlen(buf
), buf
);
437 else if ( rc
== 0 ) {
442 DEBUGF("Failed reading file: %d\n",rc
);
452 int dbg_head(char* name
)
454 unsigned char buf
[SECTOR_SIZE
*5];
457 fd
= open(name
,O_RDONLY
);
460 DEBUGF("Got file descriptor %d\n",fd
);
462 rc
= read(fd
, buf
, SECTOR_SIZE
*3);
466 printf("%d:\n%s\n", strlen(buf
), buf
);
468 else if ( rc
== 0 ) {
473 DEBUGF("Failed reading file: %d\n",rc
);
479 int dbg_trunc(char* name
, int size
)
484 fd
= open(name
,O_RDWR
);
488 rc
= ftruncate(fd
, size
);
490 DEBUGF("ftruncate(%d) failed\n", size
);
495 fd
= open(name
,O_RDWR
|O_TRUNC
);
499 rc
= lseek(fd
, size
, SEEK_SET
);
507 int dbg_mkdir(char* name
)
513 DEBUGF("Failed creating directory\n");
519 int dbg_cmd(int argc
, char *argv
[])
535 DEBUGF("usage: fat command [options]\n"
538 " ds <sector> - display sector\n"
542 " mkfile <file> <size (KB)>\n"
546 " dump <file> <offset>\n"
548 " trunc <file> <size>\n"
552 " ren <file> <newname>\n"
557 if (!strcasecmp(cmd
, "dir"))
565 if (!strcasecmp(cmd
, "ds"))
568 DEBUGF("secnum: %d\n", strtol(arg1
, NULL
, 0));
569 dbg_dump_sector(strtol(arg1
, NULL
, 0));
573 if (!strcasecmp(cmd
, "type"))
579 if (!strcasecmp(cmd
, "head"))
582 return dbg_head(arg1
);
585 if (!strcasecmp(cmd
, "tail"))
591 if (!strcasecmp(cmd
, "mkfile"))
595 return dbg_mkfile(arg1
,strtol(arg2
, NULL
, 0));
597 return dbg_mkfile(arg1
,1);
601 if (!strcasecmp(cmd
, "chkfile"))
605 return dbg_chkfile(arg1
, strtol(arg2
, NULL
, 0));
607 return dbg_chkfile(arg1
, 0);
611 if (!strcasecmp(cmd
, "mkdir"))
614 return dbg_mkdir(arg1
);
618 if (!strcasecmp(cmd
, "del"))
624 if (!strcasecmp(cmd
, "rmdir"))
630 if (!strcasecmp(cmd
, "dump"))
634 return dbg_dump(arg1
, strtol(arg2
, NULL
, 0));
636 return dbg_dump(arg1
, 0);
640 if (!strcasecmp(cmd
, "wrtest"))
643 return dbg_wrtest(arg1
);
646 if (!strcasecmp(cmd
, "append"))
649 return dbg_append(arg1
);
652 if (!strcasecmp(cmd
, "test"))
655 return dbg_test(arg1
);
658 if (!strcasecmp(cmd
, "trunc"))
661 return dbg_trunc(arg1
, strtol(arg2
, NULL
, 0));
664 if (!strcasecmp(cmd
, "ren"))
667 return rename(arg1
, arg2
);
673 extern void ata_exit(void);
675 int main(int argc
, char *argv
[])
678 struct partinfo
* pinfo
;
683 DEBUGF("*** Warning! The disk is uninitialized\n");
688 DEBUGF("*** Failed reading partitions\n");
692 for ( i
=0; i
<4; i
++ ) {
693 if ( pinfo
[i
].type
== PARTITION_TYPE_FAT32
694 #ifdef HAVE_FAT16SUPPORT
695 || pinfo
[i
].type
== PARTITION_TYPE_FAT16
698 DEBUGF("*** Mounting at block %ld\n",pinfo
[i
].start
);
699 rc
= fat_mount(IF_MV2(0,) IF_MD2(0,) pinfo
[i
].start
);
701 DEBUGF("mount: %d",rc
);
708 if(fat_mount(IF_MV2(0,) IF_MD2(0,) 0)) {
709 DEBUGF("No FAT32 partition!");
714 rc
= dbg_cmd(argc
, argv
);
719 DEBUGF("Return code: %d\n", rc
);