#include #include #include #include #include #include "fat.h" #include "debug.h" #include "disk.h" #include "dir.h" #include "file.h" extern int ata_init(char*); extern void ata_read_sectors(int, int, char*); void dbg_dump_sector(int sec); void dbg_dump_buffer(unsigned char *buf, int len, int offset); void dbg_console(void); void panicf( char *fmt, ...) { va_list ap; va_start( ap, fmt ); fprintf(stderr,"***PANIC*** "); vfprintf(stderr, fmt, ap ); va_end( ap ); exit(1); } void dbg_dump_sector(int sec) { unsigned char buf[512]; ata_read_sectors(sec,1,buf); DEBUGF("---< Sector %d >-----------------------------------------\n", sec); dbg_dump_buffer(buf, 512, 0); } void dbg_dump_buffer(unsigned char *buf, int len, int offset) { int i, j; unsigned char c; unsigned char ascii[33]; for(i = 0;i < len/16;i++) { DEBUGF("%03x: ", i*16 + offset); for(j = 0;j < 16;j++) { c = buf[i*16+j]; DEBUGF("%02x ", c); if(c < 32 || c > 127) { ascii[j] = '.'; } else { ascii[j] = c; } } ascii[j] = 0; DEBUGF("%s\n", ascii); } } void dbg_dir(char* currdir) { DIR* dir; struct dirent* entry; dir = opendir(currdir); if (dir) { while ( (entry = readdir(dir)) ) { DEBUGF("%15s (%d bytes) %x\n", entry->d_name, entry->size, entry->startcluster); } closedir(dir); } else { DEBUGF( "Could not open dir %s\n", currdir); } } #define CHUNKSIZE 8 #define BUFSIZE 8192 int dbg_mkfile(char* name, int num) { char text[BUFSIZE+1]; int i; int fd; int x=0; bool stop = false; fd = creat(name,O_WRONLY); if (fd<0) { DEBUGF("Failed creating file\n"); return -1; } num *= 1024; while ( num ) { int rc; int len = num > BUFSIZE ? BUFSIZE : num; for (i=0; i 0 ) { buf[size] = 0; printf("%d: %.*s\n", rc, rc, buf); } else if ( rc == 0 ) { DEBUGF("EOF\n"); break; } else { DEBUGF("Failed reading file: %d\n",rc); break; } } close(fd); } 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); if (close(fd) < 0) return -1; 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"); return close(fd); } int dbg_test(char* name) { int x=0; int j; int fd; char text[BUFSIZE+1]; for (j=0; j<5; j++) { int num = 40960; fd = open(name,O_WRONLY|O_CREAT|O_APPEND); if (fd<0) { DEBUGF("Failed opening file\n"); return -1; } while ( num ) { int rc, i; int len = num > BUFSIZE ? BUFSIZE : num; for (i=0; i= 0 ) { rc = read(fd, buf, SECTOR_SIZE); if( rc > 0 ) { buf[rc]=0; printf("%d:\n%s\n", strlen(buf), buf); } else if ( rc == 0 ) { DEBUGF("EOF\n"); } else { DEBUGF("Failed reading file: %d\n",rc); } } else { perror("lseek"); } close(fd); } int dbg_head(char* name) { unsigned char buf[SECTOR_SIZE*5]; int fd,rc; fd = open(name,O_RDONLY); if (fd<0) return -1; DEBUGF("Got file descriptor %d\n",fd); rc = read(fd, buf, SECTOR_SIZE*3); if( rc > 0 ) { buf[rc]=0; printf("%d:\n%s\n", strlen(buf), buf); } else if ( rc == 0 ) { DEBUGF("EOF\n"); } else { DEBUGF("Failed reading file: %d\n",rc); } return close(fd); } int dbg_trunc(char* name, int size) { int fd,rc; #if 1 fd = open(name,O_RDWR); if (fd<0) return -1; 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 return close(fd); } int dbg_mkdir(char* name) { int fd; fd = mkdir(name, 0); if (fd<0) { DEBUGF("Failed creating directory\n"); return -1; } return 0; } int dbg_cmd(int argc, char *argv[]) { 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 \n" " ds - display sector\n" " type \n" " head \n" " tail \n" " mkfile \n" " chkfile \n" " del \n" " rmdir \n" " dump \n" " mkdir \n" " trunc \n" " wrtest \n" " append \n" " test \n" " ren \n" ); return -1; } if (!strcasecmp(cmd, "dir")) { if ( arg1 ) dbg_dir(arg1); else dbg_dir("/"); } if (!strcasecmp(cmd, "ds")) { if ( arg1 ) { DEBUGF("secnum: %d\n", strtol(arg1, NULL, 0)); dbg_dump_sector(strtol(arg1, NULL, 0)); } } if (!strcasecmp(cmd, "type")) { if (arg1) dbg_type(arg1); } if (!strcasecmp(cmd, "head")) { if (arg1) return dbg_head(arg1); } if (!strcasecmp(cmd, "tail")) { if (arg1) dbg_tail(arg1); } if (!strcasecmp(cmd, "mkfile")) { if (arg1) { if (arg2) return dbg_mkfile(arg1,strtol(arg2, NULL, 0)); else return dbg_mkfile(arg1,1); } } if (!strcasecmp(cmd, "chkfile")) { if (arg1) { if (arg2) return dbg_chkfile(arg1, strtol(arg2, NULL, 0)); else return dbg_chkfile(arg1, 0); } } if (!strcasecmp(cmd, "mkdir")) { if (arg1) { return dbg_mkdir(arg1); } } if (!strcasecmp(cmd, "del")) { if (arg1) return remove(arg1); } if (!strcasecmp(cmd, "rmdir")) { if (arg1) return rmdir(arg1); } if (!strcasecmp(cmd, "dump")) { if (arg1) { if (arg2) return dbg_dump(arg1, strtol(arg2, NULL, 0)); 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); } if (!strcasecmp(cmd, "test")) { if (arg1) return dbg_test(arg1); } if (!strcasecmp(cmd, "trunc")) { if (arg1 && arg2) return dbg_trunc(arg1, strtol(arg2, NULL, 0)); } if (!strcasecmp(cmd, "ren")) { if (arg1 && arg2) return rename(arg1, arg2); } return 0; } extern void ata_exit(void); int main(int argc, char *argv[]) { int rc,i; struct partinfo* pinfo; srand(clock()); if(ata_init("disk.img")) { DEBUGF("*** Warning! The disk is uninitialized\n"); return -1; } pinfo = disk_init(); if (!pinfo) { DEBUGF("*** Failed reading partitions\n"); return -1; } for ( i=0; i<4; i++ ) { if ( pinfo[i].type == PARTITION_TYPE_FAT32 #ifdef HAVE_FAT16SUPPORT || pinfo[i].type == PARTITION_TYPE_FAT16 #endif ) { DEBUGF("*** Mounting at block %ld\n",pinfo[i].start); rc = fat_mount(IF_MV2(0,) IF_MV2(0,) pinfo[i].start); if(rc) { DEBUGF("mount: %d",rc); return -1; } break; } } if ( i==4 ) { if(fat_mount(IF_MV2(0,) IF_MV2(0,) 0)) { DEBUGF("No FAT32 partition!"); return -1; } } rc = dbg_cmd(argc, argv); ata_exit(); return rc; }