2002-04-26 16:44:58 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#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);
|
|
|
|
|
void dbg_dump_buffer(unsigned char *buf);
|
2002-05-03 15:36:52 +00:00
|
|
|
|
void dbg_console(void);
|
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-04-26 16:44:58 +00:00
|
|
|
|
dbg_dump_buffer(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dbg_dump_buffer(unsigned char *buf)
|
|
|
|
|
{
|
|
|
|
|
int i, j;
|
|
|
|
|
unsigned char c;
|
|
|
|
|
unsigned char ascii[33];
|
|
|
|
|
|
2002-04-27 01:17:49 +00:00
|
|
|
|
for(i = 0;i < 512/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-15 14:36:52 +00:00
|
|
|
|
void dbg_mkfile(char* name)
|
|
|
|
|
{
|
|
|
|
|
char* text = "Detta <20>r en dummy-text\n";
|
|
|
|
|
int fd = open(name,O_WRONLY);
|
|
|
|
|
if (fd<0) {
|
|
|
|
|
DEBUGF("Failed creating file\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (write(fd, text, strlen(text)) < 0)
|
|
|
|
|
DEBUGF("Failed writing data\n");
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
|
void dbg_type(char* name)
|
2002-04-27 19:39:08 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned char buf[SECTOR_SIZE*5];
|
2002-05-07 16:01:53 +00:00
|
|
|
|
int i,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-05-07 16:01:53 +00:00
|
|
|
|
for (i=0;i<5;i++) {
|
2002-05-08 12:13:47 +00:00
|
|
|
|
rc = read(fd, buf, SECTOR_SIZE*2/3);
|
|
|
|
|
if( rc > 0 )
|
2002-04-27 19:39:08 +00:00
|
|
|
|
{
|
2002-05-08 12:13:47 +00:00
|
|
|
|
buf[rc]=0;
|
|
|
|
|
printf("%d: %s\n", i, buf);
|
|
|
|
|
}
|
|
|
|
|
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-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);
|
|
|
|
|
|
|
|
|
|
rc = lseek(fd,512,SEEK_SET);
|
|
|
|
|
if ( rc >= 0 ) {
|
|
|
|
|
rc = read(fd, buf, SECTOR_SIZE);
|
|
|
|
|
if( rc > 0 )
|
|
|
|
|
{
|
|
|
|
|
buf[rc]=0;
|
|
|
|
|
printf("%d: %s\n", strlen(buf), buf);
|
|
|
|
|
}
|
|
|
|
|
else if ( rc == 0 ) {
|
|
|
|
|
DEBUGF("EOF\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DEBUGF("Failed reading file: %d\n",rc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
perror("lseek");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = lseek(fd,-100,SEEK_CUR);
|
|
|
|
|
if ( rc >= 0 ) {
|
|
|
|
|
rc = read(fd, buf, SECTOR_SIZE);
|
|
|
|
|
if( rc > 0 )
|
|
|
|
|
{
|
|
|
|
|
buf[rc]=0;
|
|
|
|
|
printf("%d: %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);
|
|
|
|
|
}
|
|
|
|
|
|
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-05-03 15:36:52 +00:00
|
|
|
|
void dbg_console(void)
|
2002-04-26 16:44:58 +00:00
|
|
|
|
{
|
|
|
|
|
char cmd[32] = "";
|
|
|
|
|
char last_cmd[32] = "";
|
|
|
|
|
int quit = 0;
|
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
|
|
while(!quit)
|
|
|
|
|
{
|
|
|
|
|
dbg_prompt();
|
|
|
|
|
if(fgets(cmd, sizeof(cmd) - 1, stdin))
|
|
|
|
|
{
|
|
|
|
|
if(strlen(cmd) == 1) /* empty command? */
|
|
|
|
|
{
|
|
|
|
|
strcpy(cmd, last_cmd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get the first token */
|
|
|
|
|
s = strtok(cmd, " \n");
|
|
|
|
|
if(s)
|
|
|
|
|
{
|
|
|
|
|
if(!strcasecmp(s, "dir"))
|
|
|
|
|
{
|
2002-05-03 15:36:52 +00:00
|
|
|
|
s = strtok(NULL, " \n");
|
|
|
|
|
if (!s)
|
|
|
|
|
s = "/";
|
|
|
|
|
dbg_dir(s);
|
2002-04-26 16:44:58 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!strcasecmp(s, "ds"))
|
|
|
|
|
{
|
|
|
|
|
/* Remember the command */
|
|
|
|
|
strcpy(last_cmd, s);
|
|
|
|
|
|
|
|
|
|
if((s = strtok(NULL, " \n")))
|
|
|
|
|
{
|
|
|
|
|
last_secnum = atoi(s);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
last_secnum++;
|
|
|
|
|
}
|
2002-05-07 16:01:53 +00:00
|
|
|
|
DEBUGF("secnum: %d\n", last_secnum);
|
2002-04-26 16:44:58 +00:00
|
|
|
|
dbg_dump_sector(last_secnum);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2002-04-27 19:39:08 +00:00
|
|
|
|
|
|
|
|
|
if(!strcasecmp(s, "type"))
|
|
|
|
|
{
|
2002-05-07 16:01:53 +00:00
|
|
|
|
s = strtok(NULL, " \n");
|
|
|
|
|
if (!s)
|
|
|
|
|
continue;
|
|
|
|
|
dbg_type(s);
|
2002-04-27 19:39:08 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2002-04-26 16:44:58 +00:00
|
|
|
|
|
|
|
|
|
if(!strcasecmp(s, "exit") ||
|
|
|
|
|
!strcasecmp(s, "x"))
|
|
|
|
|
{
|
|
|
|
|
quit = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-05-07 16:01:53 +00:00
|
|
|
|
else
|
|
|
|
|
quit = 1;
|
2002-04-26 16:44:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
|
|
if(ata_init(argv[1])) {
|
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
|
|
|
|
if ( argc > 2 ) {
|
|
|
|
|
dbg_dump_sector(atoi(argv[2]));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( i==4 ) {
|
|
|
|
|
if(fat_mount(0)) {
|
|
|
|
|
DEBUGF("No FAT32 partition!");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2002-05-03 11:59:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-08 15:16:02 +00:00
|
|
|
|
//dbg_console();
|
2002-08-08 20:32:09 +00:00
|
|
|
|
//dbg_tail("/fat.h");
|
2002-10-15 14:36:52 +00:00
|
|
|
|
//dbg_dir("/");
|
|
|
|
|
dbg_mkfile("/apa.txt");
|
2002-08-08 20:32:09 +00:00
|
|
|
|
dbg_dir("/");
|
2002-05-08 12:13:47 +00:00
|
|
|
|
|
2002-05-03 11:59:53 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|