2002-04-26 16:44:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-10-20 22:50:58 +00:00
|
|
|
#include <stdarg.h>
|
2002-11-11 15:47:19 +00:00
|
|
|
#include <time.h>
|
2002-04-26 16:44:58 +00:00
|
|
|
#include "fat.h"
|
|
|
|
#include "debug.h"
|
2002-05-03 11:59:53 +00:00
|
|
|
#include "disk.h"
|
2002-05-03 15:36:52 +00:00
|
|
|
#include "dir.h"
|
2002-05-07 16:01:53 +00:00
|
|
|
#include "file.h"
|
2002-05-03 11:59:53 +00:00
|
|
|
|
|
|
|
void dbg_dump_sector(int sec);
|
2002-10-31 16:09:28 +00:00
|
|
|
void dbg_dump_buffer(unsigned char *buf, int len, int offset);
|
2002-05-03 15:36:52 +00:00
|
|
|
void dbg_console(void);
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2005-04-18 14:16:35 +00:00
|
|
|
void mutex_init(void* l) {}
|
|
|
|
void mutex_lock(void* l) {}
|
|
|
|
void mutex_unlock(void* l) {}
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
void panicf( char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start( ap, fmt );
|
2002-10-30 16:15:03 +00:00
|
|
|
fprintf(stderr,"***PANIC*** ");
|
|
|
|
vfprintf(stderr, fmt, ap );
|
2002-10-20 22:50:58 +00:00
|
|
|
va_end( ap );
|
2002-10-30 16:15:03 +00:00
|
|
|
exit(1);
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
2005-04-18 14:16:35 +00:00
|
|
|
void debugf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start( ap, fmt );
|
|
|
|
fprintf(stderr,"DEBUGF: ");
|
|
|
|
vfprintf( stderr, fmt, ap );
|
|
|
|
va_end( ap );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ldebugf(const char* file, int line, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start( ap, fmt );
|
|
|
|
fprintf( stderr, "%s:%d ", file, line );
|
|
|
|
vfprintf( stderr, fmt, ap );
|
|
|
|
va_end( ap );
|
|
|
|
}
|
|
|
|
|
2002-04-26 16:44:58 +00:00
|
|
|
void dbg_dump_sector(int sec)
|
|
|
|
{
|
|
|
|
unsigned char buf[512];
|
|
|
|
|
|
|
|
ata_read_sectors(sec,1,buf);
|
2002-05-07 16:01:53 +00:00
|
|
|
DEBUGF("---< Sector %d >-----------------------------------------\n", sec);
|
2002-10-31 16:09:28 +00:00
|
|
|
dbg_dump_buffer(buf, 512, 0);
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
|
2002-10-31 16:09:28 +00:00
|
|
|
void dbg_dump_buffer(unsigned char *buf, int len, int offset)
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
unsigned char c;
|
|
|
|
unsigned char ascii[33];
|
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
for(i = 0;i < len/16;i++)
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2002-10-31 16:09:28 +00:00
|
|
|
DEBUGF("%03x: ", i*16 + offset);
|
2002-04-27 01:17:49 +00:00
|
|
|
for(j = 0;j < 16;j++)
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2002-04-27 01:17:49 +00:00
|
|
|
c = buf[i*16+j];
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
DEBUGF("%02x ", c);
|
2002-04-26 16:44:58 +00:00
|
|
|
if(c < 32 || c > 127)
|
|
|
|
{
|
|
|
|
ascii[j] = '.';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ascii[j] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ascii[j] = 0;
|
2002-05-07 16:01:53 +00:00
|
|
|
DEBUGF("%s\n", ascii);
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:36:52 +00:00
|
|
|
void dbg_dir(char* currdir)
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2002-05-03 15:36:52 +00:00
|
|
|
DIR* dir;
|
|
|
|
struct dirent* entry;
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-05-03 15:36:52 +00:00
|
|
|
dir = opendir(currdir);
|
|
|
|
if (dir)
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2002-05-07 16:01:53 +00:00
|
|
|
while ( (entry = readdir(dir)) ) {
|
2002-10-15 14:36:52 +00:00
|
|
|
DEBUGF("%15s (%d bytes) %x\n",
|
|
|
|
entry->d_name, entry->size, entry->startcluster);
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
2002-08-08 20:32:09 +00:00
|
|
|
closedir(dir);
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-07 16:01:53 +00:00
|
|
|
DEBUGF( "Could not open dir %s\n", currdir);
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
#define CHUNKSIZE 8
|
2002-11-11 15:47:19 +00:00
|
|
|
#define BUFSIZE 8192
|
2002-10-30 16:15:03 +00:00
|
|
|
|
|
|
|
int dbg_mkfile(char* name, int num)
|
2002-10-15 14:36:52 +00:00
|
|
|
{
|
2002-11-11 15:47:19 +00:00
|
|
|
char text[BUFSIZE+1];
|
2002-10-20 22:50:58 +00:00
|
|
|
int i;
|
2002-10-30 16:15:03 +00:00
|
|
|
int fd;
|
|
|
|
int x=0;
|
2002-11-11 15:47:19 +00:00
|
|
|
bool stop = false;
|
2002-10-30 16:15:03 +00:00
|
|
|
|
2002-11-12 09:04:53 +00:00
|
|
|
fd = creat(name,O_WRONLY);
|
2002-10-15 14:36:52 +00:00
|
|
|
if (fd<0) {
|
|
|
|
DEBUGF("Failed creating file\n");
|
2002-10-30 16:15:03 +00:00
|
|
|
return -1;
|
2002-10-15 14:36:52 +00:00
|
|
|
}
|
2002-10-30 16:15:03 +00:00
|
|
|
num *= 1024;
|
|
|
|
while ( num ) {
|
2002-10-31 19:06:14 +00:00
|
|
|
int rc;
|
2002-11-11 15:47:19 +00:00
|
|
|
int len = num > BUFSIZE ? BUFSIZE : num;
|
2002-10-30 16:15:03 +00:00
|
|
|
|
|
|
|
for (i=0; i<len/CHUNKSIZE; i++ )
|
2002-10-31 16:09:28 +00:00
|
|
|
sprintf(text+i*CHUNKSIZE,"%c%06x,",name[1],x++);
|
2002-10-22 15:06:08 +00:00
|
|
|
|
2002-10-31 19:06:14 +00:00
|
|
|
rc = write(fd, text, len);
|
|
|
|
if ( rc < 0 ) {
|
2002-10-20 22:50:58 +00:00
|
|
|
DEBUGF("Failed writing data\n");
|
2002-10-30 16:15:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-10-31 19:06:14 +00:00
|
|
|
else
|
|
|
|
if ( rc == 0 ) {
|
|
|
|
DEBUGF("No space left\n");
|
2002-11-11 15:47:19 +00:00
|
|
|
return -2;
|
2002-10-31 19:06:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
DEBUGF("wrote %d bytes\n",rc);
|
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
num -= len;
|
2002-11-11 15:47:19 +00:00
|
|
|
|
|
|
|
if ( !num ) {
|
|
|
|
if ( stop )
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* add a random number of chunks to test byte-copy code */
|
|
|
|
num = ((int) rand() % SECTOR_SIZE) & ~7;
|
|
|
|
LDEBUGF("Adding random size %d\n",num);
|
|
|
|
stop = true;
|
|
|
|
}
|
2002-10-30 16:15:03 +00:00
|
|
|
}
|
2002-11-11 15:47:19 +00:00
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
return close(fd);
|
2002-10-30 16:15:03 +00:00
|
|
|
}
|
|
|
|
|
2002-11-11 15:47:19 +00:00
|
|
|
|
2002-10-31 20:40:15 +00:00
|
|
|
int dbg_chkfile(char* name, int size)
|
2002-10-30 16:15:03 +00:00
|
|
|
{
|
2002-10-31 20:40:15 +00:00
|
|
|
char text[81920];
|
2002-10-30 16:15:03 +00:00
|
|
|
int i;
|
|
|
|
int x=0;
|
2002-11-11 15:47:19 +00:00
|
|
|
int pos = 0;
|
2002-10-30 16:15:03 +00:00
|
|
|
int block=0;
|
|
|
|
int fd = open(name,O_RDONLY);
|
|
|
|
if (fd<0) {
|
|
|
|
DEBUGF("Failed opening file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-10-31 20:40:15 +00:00
|
|
|
|
2002-11-11 15:47:19 +00:00
|
|
|
size = lseek(fd, 0, SEEK_END);
|
|
|
|
DEBUGF("File is %d bytes\n", size);
|
|
|
|
/* random start position */
|
|
|
|
if ( size )
|
|
|
|
pos = ((int)rand() % size) & ~7;
|
|
|
|
lseek(fd, pos, SEEK_SET);
|
|
|
|
x = pos / CHUNKSIZE;
|
|
|
|
|
|
|
|
LDEBUGF("Check base is %x (%d)\n",x,pos);
|
2002-10-31 20:40:15 +00:00
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
while (1) {
|
|
|
|
int rc = read(fd, text, sizeof text);
|
|
|
|
DEBUGF("read %d bytes\n",rc);
|
|
|
|
if (rc < 0) {
|
2002-11-11 15:47:19 +00:00
|
|
|
panicf("Failed reading data\n");
|
2002-10-30 16:15:03 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
char tmp[CHUNKSIZE+1];
|
|
|
|
if (!rc)
|
|
|
|
break;
|
|
|
|
for (i=0; i<rc/CHUNKSIZE; i++ ) {
|
2002-10-31 16:09:28 +00:00
|
|
|
sprintf(tmp,"%c%06x,",name[1],x++);
|
2002-10-30 16:15:03 +00:00
|
|
|
if (strncmp(text+i*CHUNKSIZE,tmp,CHUNKSIZE)) {
|
2002-11-11 15:47:19 +00:00
|
|
|
int idx = pos + block*sizeof(text) + i*CHUNKSIZE;
|
|
|
|
DEBUGF("Mismatch in byte 0x%x (byte 0x%x of sector 0x%x)."
|
|
|
|
"\nExpected %.8s found %.8s\n",
|
|
|
|
idx, idx % SECTOR_SIZE, idx / SECTOR_SIZE,
|
2002-10-31 16:09:28 +00:00
|
|
|
tmp,
|
2002-10-30 16:15:03 +00:00
|
|
|
text+i*CHUNKSIZE);
|
2002-11-11 15:47:19 +00:00
|
|
|
DEBUGF("i=%x, idx=%x\n",i,idx);
|
|
|
|
dbg_dump_buffer(text+i*CHUNKSIZE - 0x20, 0x40, idx - 0x20);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block++;
|
|
|
|
}
|
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
return close(fd);
|
2002-11-11 15:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dbg_wrtest(char* name)
|
|
|
|
{
|
|
|
|
char text[81920];
|
|
|
|
int i;
|
|
|
|
int x=0;
|
|
|
|
int pos = 0;
|
|
|
|
int block=0;
|
|
|
|
int size, fd, rc;
|
|
|
|
char tmp[CHUNKSIZE+1];
|
|
|
|
|
|
|
|
fd = open(name,O_RDWR);
|
|
|
|
if (fd<0) {
|
|
|
|
DEBUGF("Failed opening file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = lseek(fd, 0, SEEK_END);
|
|
|
|
DEBUGF("File is %d bytes\n", size);
|
|
|
|
/* random start position */
|
|
|
|
if ( size )
|
|
|
|
pos = ((int)rand() % size) & ~7;
|
|
|
|
rc = lseek(fd, pos, SEEK_SET);
|
|
|
|
if ( rc < 0 )
|
|
|
|
panicf("Failed seeking\n");
|
|
|
|
x = pos / CHUNKSIZE;
|
|
|
|
LDEBUGF("Check base is %x (%d)\n",x,pos);
|
|
|
|
|
|
|
|
sprintf(tmp,"%c%06x,",name[1],x++);
|
|
|
|
rc = write(fd, tmp, 8);
|
|
|
|
if ( rc < 0 )
|
|
|
|
panicf("Failed writing data\n");
|
|
|
|
|
|
|
|
if ( size )
|
|
|
|
pos = ((int)rand() % size) & ~7;
|
|
|
|
rc = lseek(fd, pos, SEEK_SET);
|
|
|
|
if ( rc < 0 )
|
|
|
|
panicf("Failed seeking\n");
|
|
|
|
x = pos / CHUNKSIZE;
|
|
|
|
LDEBUGF("Check base 2 is %x (%d)\n",x,pos);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
rc = read(fd, text, sizeof text);
|
|
|
|
DEBUGF("read %d bytes\n",rc);
|
|
|
|
if (rc < 0) {
|
|
|
|
panicf("Failed reading data\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!rc)
|
|
|
|
break;
|
|
|
|
for (i=0; i<rc/CHUNKSIZE; i++ ) {
|
|
|
|
sprintf(tmp,"%c%06x,",name[1],x++);
|
|
|
|
if (strncmp(text+i*CHUNKSIZE,tmp,CHUNKSIZE)) {
|
|
|
|
int idx = pos + block*sizeof(text) + i*CHUNKSIZE;
|
|
|
|
DEBUGF("Mismatch in byte 0x%x (byte 0x%x of sector 0x%x)."
|
|
|
|
"\nExpected %.8s found %.8s\n",
|
|
|
|
idx, idx % SECTOR_SIZE, idx / SECTOR_SIZE,
|
|
|
|
tmp,
|
|
|
|
text+i*CHUNKSIZE);
|
|
|
|
DEBUGF("i=%x, idx=%x\n",i,idx);
|
|
|
|
dbg_dump_buffer(text+i*CHUNKSIZE - 0x20, 0x40, idx - 0x20);
|
2002-10-30 16:15:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block++;
|
|
|
|
}
|
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
return close(fd);
|
2002-10-15 14:36:52 +00:00
|
|
|
}
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
void dbg_type(char* name)
|
2002-04-27 19:39:08 +00:00
|
|
|
{
|
2002-10-30 16:15:03 +00:00
|
|
|
const int size = SECTOR_SIZE*5;
|
2004-10-01 19:19:09 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE*5+1];
|
2002-10-30 16:15:03 +00:00
|
|
|
int fd,rc;
|
2002-04-27 19:39:08 +00:00
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
fd = open(name,O_RDONLY);
|
|
|
|
if (fd<0)
|
|
|
|
return;
|
|
|
|
DEBUGF("Got file descriptor %d\n",fd);
|
2002-04-27 19:39:08 +00:00
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
while ( 1 ) {
|
|
|
|
rc = read(fd, buf, size);
|
2002-05-08 12:13:47 +00:00
|
|
|
if( rc > 0 )
|
2002-04-27 19:39:08 +00:00
|
|
|
{
|
2002-10-30 16:15:03 +00:00
|
|
|
buf[size] = 0;
|
|
|
|
printf("%d: %.*s\n", rc, rc, buf);
|
2002-05-08 12:13:47 +00:00
|
|
|
}
|
|
|
|
else if ( rc == 0 ) {
|
|
|
|
DEBUGF("EOF\n");
|
|
|
|
break;
|
2002-04-27 19:39:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-08 12:13:47 +00:00
|
|
|
DEBUGF("Failed reading file: %d\n",rc);
|
2002-10-30 16:15:03 +00:00
|
|
|
break;
|
2002-04-27 19:39:08 +00:00
|
|
|
}
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
2002-04-27 19:39:08 +00:00
|
|
|
}
|
|
|
|
|
2002-11-11 15:47:19 +00:00
|
|
|
int dbg_append(char* name)
|
|
|
|
{
|
|
|
|
int x=0;
|
|
|
|
int size, fd, rc;
|
|
|
|
char tmp[CHUNKSIZE+1];
|
|
|
|
|
|
|
|
fd = open(name,O_RDONLY);
|
|
|
|
if (fd<0) {
|
|
|
|
DEBUGF("Failed opening file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = lseek(fd, 0, SEEK_END);
|
|
|
|
DEBUGF("File is %d bytes\n", size);
|
|
|
|
x = size / CHUNKSIZE;
|
|
|
|
LDEBUGF("Check base is %x (%d)\n",x,size);
|
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
if (close(fd) < 0)
|
|
|
|
return -1;
|
2002-11-11 15:47:19 +00:00
|
|
|
|
|
|
|
fd = open(name,O_RDWR|O_APPEND);
|
|
|
|
if (fd<0) {
|
|
|
|
DEBUGF("Failed opening file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(tmp,"%c%06x,",name[1],x++);
|
|
|
|
rc = write(fd, tmp, 8);
|
|
|
|
if ( rc < 0 )
|
|
|
|
panicf("Failed writing data\n");
|
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
return close(fd);
|
|
|
|
}
|
2002-11-11 15:47:19 +00:00
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
int dbg_test(char* name)
|
|
|
|
{
|
|
|
|
int x=0;
|
2002-12-03 14:00:33 +00:00
|
|
|
int j;
|
|
|
|
int fd;
|
|
|
|
char text[BUFSIZE+1];
|
2002-11-14 15:32:34 +00:00
|
|
|
|
2002-12-03 14:00:33 +00:00
|
|
|
for (j=0; j<5; j++) {
|
|
|
|
int num = 40960;
|
2002-11-14 15:32:34 +00:00
|
|
|
|
2002-12-03 14:00:33 +00:00
|
|
|
fd = open(name,O_WRONLY|O_CREAT|O_APPEND);
|
|
|
|
if (fd<0) {
|
|
|
|
DEBUGF("Failed opening file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-11-14 15:32:34 +00:00
|
|
|
|
2002-12-03 14:00:33 +00:00
|
|
|
while ( num ) {
|
|
|
|
int rc, i;
|
|
|
|
int len = num > BUFSIZE ? BUFSIZE : num;
|
2002-11-14 15:32:34 +00:00
|
|
|
|
2002-12-03 14:00:33 +00:00
|
|
|
for (i=0; i<len/CHUNKSIZE; i++ )
|
|
|
|
sprintf(text+i*CHUNKSIZE,"%c%06x,",name[1],x++);
|
2002-11-14 15:32:34 +00:00
|
|
|
|
2002-12-03 14:00:33 +00:00
|
|
|
rc = write(fd, text, len);
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
DEBUGF("Failed writing data\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( rc == 0 ) {
|
|
|
|
DEBUGF("No space left\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
DEBUGF("wrote %d bytes\n",rc);
|
2002-11-14 15:32:34 +00:00
|
|
|
|
2002-12-03 14:00:33 +00:00
|
|
|
num -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close(fd) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2002-11-11 15:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dbg_dump(char* name, int offset)
|
|
|
|
{
|
|
|
|
char buf[SECTOR_SIZE];
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
int fd = open(name,O_RDONLY);
|
|
|
|
if (fd<0) {
|
|
|
|
DEBUGF("Failed opening file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lseek(fd, offset, SEEK_SET);
|
|
|
|
rc = read(fd, buf, sizeof buf);
|
|
|
|
|
|
|
|
if ( rc < 0 )
|
|
|
|
panicf("Error reading data\n");
|
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
if (close(fd) < 0)
|
|
|
|
return -1;
|
2002-11-11 15:47:19 +00:00
|
|
|
|
|
|
|
dbg_dump_buffer(buf, rc, offset);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-08 15:16:02 +00:00
|
|
|
void dbg_tail(char* name)
|
|
|
|
{
|
|
|
|
unsigned char buf[SECTOR_SIZE*5];
|
|
|
|
int fd,rc;
|
|
|
|
|
|
|
|
fd = open(name,O_RDONLY);
|
|
|
|
if (fd<0)
|
|
|
|
return;
|
|
|
|
DEBUGF("Got file descriptor %d\n",fd);
|
|
|
|
|
2002-10-22 15:06:08 +00:00
|
|
|
rc = lseek(fd,-512,SEEK_END);
|
2002-05-08 15:16:02 +00:00
|
|
|
if ( rc >= 0 ) {
|
|
|
|
rc = read(fd, buf, SECTOR_SIZE);
|
|
|
|
if( rc > 0 )
|
|
|
|
{
|
|
|
|
buf[rc]=0;
|
2002-10-23 14:34:53 +00:00
|
|
|
printf("%d:\n%s\n", strlen(buf), buf);
|
2002-05-08 15:16:02 +00:00
|
|
|
}
|
|
|
|
else if ( rc == 0 ) {
|
|
|
|
DEBUGF("EOF\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGF("Failed reading file: %d\n",rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
perror("lseek");
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2002-11-11 15:47:19 +00:00
|
|
|
int dbg_head(char* name)
|
2002-10-20 22:50:58 +00:00
|
|
|
{
|
|
|
|
unsigned char buf[SECTOR_SIZE*5];
|
|
|
|
int fd,rc;
|
|
|
|
|
|
|
|
fd = open(name,O_RDONLY);
|
|
|
|
if (fd<0)
|
2002-11-11 15:47:19 +00:00
|
|
|
return -1;
|
2002-10-20 22:50:58 +00:00
|
|
|
DEBUGF("Got file descriptor %d\n",fd);
|
|
|
|
|
2002-10-23 14:34:53 +00:00
|
|
|
rc = read(fd, buf, SECTOR_SIZE*3);
|
2002-10-20 22:50:58 +00:00
|
|
|
if( rc > 0 )
|
|
|
|
{
|
|
|
|
buf[rc]=0;
|
2002-10-23 14:34:53 +00:00
|
|
|
printf("%d:\n%s\n", strlen(buf), buf);
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
else if ( rc == 0 ) {
|
|
|
|
DEBUGF("EOF\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGF("Failed reading file: %d\n",rc);
|
|
|
|
}
|
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
return close(fd);
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
2002-11-11 15:47:19 +00:00
|
|
|
int dbg_trunc(char* name, int size)
|
2002-11-01 15:26:06 +00:00
|
|
|
{
|
2002-11-11 15:47:19 +00:00
|
|
|
int fd,rc;
|
2002-11-01 15:26:06 +00:00
|
|
|
|
2002-11-11 15:47:19 +00:00
|
|
|
#if 1
|
|
|
|
fd = open(name,O_RDWR);
|
|
|
|
if (fd<0)
|
|
|
|
return -1;
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-11-11 15:47:19 +00:00
|
|
|
rc = ftruncate(fd, size);
|
|
|
|
if (rc<0) {
|
|
|
|
DEBUGF("ftruncate(%d) failed\n", size);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
fd = open(name,O_RDWR|O_TRUNC);
|
|
|
|
if (fd<0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rc = lseek(fd, size, SEEK_SET);
|
|
|
|
if (fd<0)
|
|
|
|
return -2;
|
|
|
|
#endif
|
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
return close(fd);
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
int dbg_mkdir(char* name)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2004-04-15 08:38:13 +00:00
|
|
|
fd = mkdir(name, 0);
|
2004-01-15 14:30:59 +00:00
|
|
|
if (fd<0) {
|
|
|
|
DEBUGF("Failed creating directory\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-15 08:38:13 +00:00
|
|
|
return 0;
|
2004-01-15 14:30:59 +00:00
|
|
|
}
|
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
int dbg_cmd(int argc, char *argv[])
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2002-10-23 14:34:53 +00:00
|
|
|
char* cmd = NULL;
|
|
|
|
char* arg1 = NULL;
|
|
|
|
char* arg2 = NULL;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
cmd = argv[1];
|
|
|
|
if ( argc > 2 ) {
|
|
|
|
arg1 = argv[2];
|
|
|
|
if ( argc > 3 ) {
|
|
|
|
arg2 = argv[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUGF("usage: fat command [options]\n"
|
|
|
|
"commands:\n"
|
|
|
|
" dir <dir>\n"
|
|
|
|
" ds <sector> - display sector\n"
|
|
|
|
" type <file>\n"
|
|
|
|
" head <file>\n"
|
|
|
|
" tail <file>\n"
|
|
|
|
" mkfile <file> <size (KB)>\n"
|
2002-10-30 16:15:03 +00:00
|
|
|
" chkfile <file>\n"
|
2002-11-01 15:26:06 +00:00
|
|
|
" del <file>\n"
|
2004-04-16 08:13:27 +00:00
|
|
|
" rmdir <dir>\n"
|
2002-11-11 15:47:19 +00:00
|
|
|
" dump <file> <offset>\n"
|
|
|
|
" mkdir <dir>\n"
|
|
|
|
" trunc <file> <size>\n"
|
|
|
|
" wrtest <file>\n"
|
|
|
|
" append <file>\n"
|
2002-11-14 15:32:34 +00:00
|
|
|
" test <file>\n"
|
2002-12-03 14:00:33 +00:00
|
|
|
" ren <file> <newname>\n"
|
2002-10-23 14:34:53 +00:00
|
|
|
);
|
2002-10-30 16:15:03 +00:00
|
|
|
return -1;
|
2002-10-23 14:34:53 +00:00
|
|
|
}
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-10-23 14:34:53 +00:00
|
|
|
if (!strcasecmp(cmd, "dir"))
|
2002-04-26 16:44:58 +00:00
|
|
|
{
|
2002-10-23 14:34:53 +00:00
|
|
|
if ( arg1 )
|
|
|
|
dbg_dir(arg1);
|
|
|
|
else
|
|
|
|
dbg_dir("/");
|
|
|
|
}
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-10-23 14:34:53 +00:00
|
|
|
if (!strcasecmp(cmd, "ds"))
|
|
|
|
{
|
|
|
|
if ( arg1 ) {
|
2004-04-16 08:13:27 +00:00
|
|
|
DEBUGF("secnum: %d\n", strtol(arg1, NULL, 0));
|
|
|
|
dbg_dump_sector(strtol(arg1, NULL, 0));
|
2002-10-23 14:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "type"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
|
|
|
dbg_type(arg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "head"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
2002-11-11 15:47:19 +00:00
|
|
|
return dbg_head(arg1);
|
2002-10-23 14:34:53 +00:00
|
|
|
}
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-10-23 14:34:53 +00:00
|
|
|
if (!strcasecmp(cmd, "tail"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
|
|
|
dbg_tail(arg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "mkfile"))
|
|
|
|
{
|
|
|
|
if (arg1) {
|
|
|
|
if (arg2)
|
2004-04-16 08:13:27 +00:00
|
|
|
return dbg_mkfile(arg1,strtol(arg2, NULL, 0));
|
2002-10-23 14:34:53 +00:00
|
|
|
else
|
2002-10-30 16:15:03 +00:00
|
|
|
return dbg_mkfile(arg1,1);
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-30 16:15:03 +00:00
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "chkfile"))
|
|
|
|
{
|
2002-10-31 20:40:15 +00:00
|
|
|
if (arg1) {
|
|
|
|
if (arg2)
|
2004-04-16 08:13:27 +00:00
|
|
|
return dbg_chkfile(arg1, strtol(arg2, NULL, 0));
|
2002-10-31 20:40:15 +00:00
|
|
|
else
|
|
|
|
return dbg_chkfile(arg1, 0);
|
|
|
|
}
|
2002-10-30 16:15:03 +00:00
|
|
|
}
|
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
if (!strcasecmp(cmd, "mkdir"))
|
|
|
|
{
|
|
|
|
if (arg1) {
|
|
|
|
return dbg_mkdir(arg1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-01 15:26:06 +00:00
|
|
|
if (!strcasecmp(cmd, "del"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
2002-11-11 15:47:19 +00:00
|
|
|
return remove(arg1);
|
|
|
|
}
|
|
|
|
|
2004-04-16 08:13:27 +00:00
|
|
|
if (!strcasecmp(cmd, "rmdir"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
|
|
|
return rmdir(arg1);
|
|
|
|
}
|
|
|
|
|
2002-11-11 15:47:19 +00:00
|
|
|
if (!strcasecmp(cmd, "dump"))
|
|
|
|
{
|
|
|
|
if (arg1) {
|
|
|
|
if (arg2)
|
2004-04-16 08:13:27 +00:00
|
|
|
return dbg_dump(arg1, strtol(arg2, NULL, 0));
|
2002-11-11 15:47:19 +00:00
|
|
|
else
|
|
|
|
return dbg_dump(arg1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "wrtest"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
|
|
|
return dbg_wrtest(arg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "append"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
|
|
|
return dbg_append(arg1);
|
|
|
|
}
|
|
|
|
|
2002-11-14 15:32:34 +00:00
|
|
|
if (!strcasecmp(cmd, "test"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
|
|
|
return dbg_test(arg1);
|
|
|
|
}
|
|
|
|
|
2002-11-11 15:47:19 +00:00
|
|
|
if (!strcasecmp(cmd, "trunc"))
|
|
|
|
{
|
|
|
|
if (arg1 && arg2)
|
2004-04-16 08:13:27 +00:00
|
|
|
return dbg_trunc(arg1, strtol(arg2, NULL, 0));
|
2002-11-01 15:26:06 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 14:00:33 +00:00
|
|
|
if (!strcasecmp(cmd, "ren"))
|
|
|
|
{
|
|
|
|
if (arg1 && arg2)
|
|
|
|
return rename(arg1, arg2);
|
|
|
|
}
|
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
return 0;
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
2002-05-03 11:59:53 +00:00
|
|
|
|
2002-10-22 15:06:08 +00:00
|
|
|
extern void ata_exit(void);
|
|
|
|
|
2002-05-03 11:59:53 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2002-08-08 20:32:09 +00:00
|
|
|
int rc,i;
|
|
|
|
struct partinfo* pinfo;
|
2002-11-11 15:47:19 +00:00
|
|
|
|
2004-10-01 19:19:09 +00:00
|
|
|
srand(clock());
|
2002-08-08 20:32:09 +00:00
|
|
|
|
2005-04-18 14:16:35 +00:00
|
|
|
if(ata_init()) {
|
2002-05-03 11:59:53 +00:00
|
|
|
DEBUGF("*** Warning! The disk is uninitialized\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-08-08 20:32:09 +00:00
|
|
|
pinfo = disk_init();
|
|
|
|
if (!pinfo) {
|
2002-05-03 11:59:53 +00:00
|
|
|
DEBUGF("*** Failed reading partitions\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-08-08 20:32:09 +00:00
|
|
|
for ( i=0; i<4; i++ ) {
|
2004-10-01 19:19:09 +00:00
|
|
|
if ( pinfo[i].type == PARTITION_TYPE_FAT32
|
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
|
|
|
|| pinfo[i].type == PARTITION_TYPE_FAT16
|
|
|
|
#endif
|
|
|
|
) {
|
2002-08-08 20:32:09 +00:00
|
|
|
DEBUGF("*** Mounting at block %ld\n",pinfo[i].start);
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = fat_mount(IF_MV2(0,) IF_MV2(0,) pinfo[i].start);
|
2002-08-08 20:32:09 +00:00
|
|
|
if(rc) {
|
|
|
|
DEBUGF("mount: %d",rc);
|
2002-10-30 16:15:03 +00:00
|
|
|
return -1;
|
2002-08-08 20:32:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( i==4 ) {
|
2004-12-28 22:16:07 +00:00
|
|
|
if(fat_mount(IF_MV2(0,) IF_MV2(0,) 0)) {
|
2002-08-08 20:32:09 +00:00
|
|
|
DEBUGF("No FAT32 partition!");
|
2002-10-30 16:15:03 +00:00
|
|
|
return -1;
|
2002-08-08 20:32:09 +00:00
|
|
|
}
|
2002-05-03 11:59:53 +00:00
|
|
|
}
|
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
rc = dbg_cmd(argc, argv);
|
2002-05-08 12:13:47 +00:00
|
|
|
|
2002-10-22 15:06:08 +00:00
|
|
|
ata_exit();
|
|
|
|
|
2005-04-18 14:32:21 +00:00
|
|
|
if (rc)
|
|
|
|
DEBUGF("Return code: %d\n", rc);
|
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
return rc;
|
2002-05-03 11:59:53 +00:00
|
|
|
}
|
|
|
|
|