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-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
|
|
|
|
2002-08-08 20:32:09 +00:00
|
|
|
extern int ata_init(char*);
|
|
|
|
extern void ata_read_sectors(int, int, char*);
|
|
|
|
|
2002-05-03 11:59:53 +00:00
|
|
|
void dbg_dump_sector(int sec);
|
2002-10-30 16:15:03 +00:00
|
|
|
void dbg_dump_buffer(unsigned char *buf, int len);
|
2002-05-03 15:36:52 +00:00
|
|
|
void dbg_console(void);
|
2002-04-26 16:44:58 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-30 16:15:03 +00:00
|
|
|
dbg_dump_buffer(buf, 512);
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
void dbg_dump_buffer(unsigned char *buf, int len)
|
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-08-08 20:32:09 +00:00
|
|
|
DEBUGF("%03x: ", i*16);
|
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
|
|
|
|
|
|
|
|
int dbg_mkfile(char* name, int num)
|
2002-10-15 14:36:52 +00:00
|
|
|
{
|
2002-10-30 16:15:03 +00:00
|
|
|
char text[8192];
|
2002-10-20 22:50:58 +00:00
|
|
|
int i;
|
2002-10-30 16:15:03 +00:00
|
|
|
int fd;
|
|
|
|
int x=0;
|
|
|
|
|
|
|
|
fd = open(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 ) {
|
|
|
|
int len = num > sizeof text ? sizeof text : num;
|
|
|
|
|
|
|
|
for (i=0; i<len/CHUNKSIZE; i++ )
|
|
|
|
sprintf(text+i*CHUNKSIZE,"%07x,",x++);
|
2002-10-22 15:06:08 +00:00
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
if (write(fd, text, len) < 0) {
|
2002-10-20 22:50:58 +00:00
|
|
|
DEBUGF("Failed writing data\n");
|
2002-10-30 16:15:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
num -= len;
|
|
|
|
DEBUGF("wrote %d bytes\n",len);
|
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-10-15 14:36:52 +00:00
|
|
|
close(fd);
|
2002-10-30 16:15:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dbg_chkfile(char* name)
|
|
|
|
{
|
|
|
|
char text[8192];
|
|
|
|
int i;
|
|
|
|
int x=0;
|
|
|
|
int block=0;
|
|
|
|
int fd = open(name,O_RDONLY);
|
|
|
|
if (fd<0) {
|
|
|
|
DEBUGF("Failed opening file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
int rc = read(fd, text, sizeof text);
|
|
|
|
DEBUGF("read %d bytes\n",rc);
|
|
|
|
if (rc < 0) {
|
|
|
|
DEBUGF("Failed writing data\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char tmp[CHUNKSIZE+1];
|
|
|
|
if (!rc)
|
|
|
|
break;
|
|
|
|
for (i=0; i<rc/CHUNKSIZE; i++ ) {
|
|
|
|
sprintf(tmp,"%07x,",x++);
|
|
|
|
if (strncmp(text+i*CHUNKSIZE,tmp,CHUNKSIZE)) {
|
|
|
|
DEBUGF("Mismatch in byte %d (%.4s != %.4s)\n",
|
|
|
|
block*sizeof(text)+i*CHUNKSIZE, tmp,
|
|
|
|
text+i*CHUNKSIZE);
|
|
|
|
dbg_dump_buffer(text+i*CHUNKSIZE - 0x20, 0x40);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block++;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
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;
|
|
|
|
unsigned char buf[size+1];
|
|
|
|
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-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-10-20 22:50:58 +00:00
|
|
|
void dbg_head(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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2002-04-27 01:17:49 +00:00
|
|
|
char current_directory[256] = "\\";
|
2002-04-26 16:44:58 +00:00
|
|
|
int last_secnum = 0;
|
|
|
|
|
|
|
|
void dbg_prompt(void)
|
|
|
|
{
|
2002-05-07 16:01:53 +00:00
|
|
|
DEBUGF("C:%s> ", current_directory);
|
2002-04-26 16:44:58 +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-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 ) {
|
|
|
|
DEBUGF("secnum: %d\n", atoi(arg1));
|
|
|
|
dbg_dump_sector(atoi(arg1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "type"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
|
|
|
dbg_type(arg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "head"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
|
|
|
dbg_head(arg1);
|
|
|
|
}
|
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)
|
2002-10-30 16:15:03 +00:00
|
|
|
return dbg_mkfile(arg1,atoi(arg2));
|
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"))
|
|
|
|
{
|
|
|
|
if (arg1)
|
|
|
|
return dbg_chkfile(arg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-23 14:34:53 +00:00
|
|
|
if(ata_init("disk.img")) {
|
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++ ) {
|
|
|
|
if ( pinfo[i].type == PARTITION_TYPE_FAT32 ) {
|
|
|
|
DEBUGF("*** Mounting at block %ld\n",pinfo[i].start);
|
|
|
|
rc = fat_mount(pinfo[i].start);
|
|
|
|
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 ) {
|
|
|
|
if(fat_mount(0)) {
|
|
|
|
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();
|
|
|
|
|
2002-10-30 16:15:03 +00:00
|
|
|
return rc;
|
2002-05-03 11:59:53 +00:00
|
|
|
}
|
|
|
|
|