From c7f7934e8f74be4b98abe83c2f6a2593fd294cf0 Mon Sep 17 00:00:00 2001 From: Björn Stenberg Date: Fri, 3 May 2002 11:59:53 +0000 Subject: Added disk/partition handling git-svn-id: svn://svn.rockbox.org/rockbox/trunk@405 a1c6a512-1295-4272-9138-f99709370657 --- firmware/test/fat/Makefile | 13 ++- firmware/test/fat/debug.c | 191 --------------------------------------- firmware/test/fat/debug.h | 9 -- firmware/test/fat/main.c | 219 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 229 insertions(+), 203 deletions(-) delete mode 100644 firmware/test/fat/debug.c delete mode 100644 firmware/test/fat/debug.h create mode 100644 firmware/test/fat/main.c (limited to 'firmware/test') diff --git a/firmware/test/fat/Makefile b/firmware/test/fat/Makefile index 8b60aba99c..6b253fe16c 100644 --- a/firmware/test/fat/Makefile +++ b/firmware/test/fat/Makefile @@ -1,18 +1,25 @@ +FIRMWARE = ../.. DRIVERS = ../../drivers -CFLAGS = -g -Wall -DTEST_FAT -I$(DRIVERS) -I. +CFLAGS = -g -Wall -DTEST_FAT -I$(DRIVERS) -I$(FIRMWARE)/common -I$(FIRMWARE) -I. -DDEBUG -DCRT_DISPLAY TARGET = fat -$(TARGET): fat.o ata-sim.o debug.o +$(TARGET): fat.o ata-sim.o debug.o main.o disk.o gcc -g -o fat $+ -lfl fat.o: $(DRIVERS)/fat.c $(DRIVERS)/fat.h $(DRIVERS)/ata.h $(CC) $(CFLAGS) -c $< -o $@ +disk.o: $(FIRMWARE)/common/disk.c + $(CC) $(CFLAGS) -c $< -o $@ + +debug.o: $(FIRMWARE)/debug.c + $(CC) $(CFLAGS) -c $< -o $@ + ata-sim.o: ata-sim.c $(DRIVERS)/ata.h -debug.o: debug.c debug.h $(DRIVERS)/ata.h +main.o: main.c $(DRIVERS)/ata.h clean: rm -f *.o $(TARGET) diff --git a/firmware/test/fat/debug.c b/firmware/test/fat/debug.c deleted file mode 100644 index fa7ff8651a..0000000000 --- a/firmware/test/fat/debug.c +++ /dev/null @@ -1,191 +0,0 @@ -#include -#include -#include -#include "fat.h" -#include "ata.h" -#include "debug.h" - -void dbg_dump_sector(int sec) -{ - unsigned char buf[512]; - - ata_read_sectors(sec,1,buf); - printf("---< Sector %d >-----------------------------------------\n", sec); - dbg_dump_buffer(buf); -} - -void dbg_dump_buffer(unsigned char *buf) -{ - int i, j; - unsigned char c; - unsigned char ascii[33]; - - for(i = 0;i < 512/16;i++) - { - for(j = 0;j < 16;j++) - { - c = buf[i*16+j]; - - printf("%02x ", c); - if(c < 32 || c > 127) - { - ascii[j] = '.'; - } - else - { - ascii[j] = c; - } - } - - ascii[j] = 0; - printf("%s\n", ascii); - } -} - -void dbg_print_bpb(struct bpb *bpb) -{ - printf("bpb_oemname = \"%s\"\n", bpb->bs_oemname); - printf("bpb_bytspersec = %d\n", bpb->bpb_bytspersec); - printf("bpb_secperclus = %d\n", bpb->bpb_secperclus); - printf("bpb_rsvdseccnt = %d\n", bpb->bpb_rsvdseccnt); - printf("bpb_numfats = %d\n", bpb->bpb_numfats); - printf("bpb_rootentcnt = %d\n", bpb->bpb_rootentcnt); - printf("bpb_totsec16 = %d\n", bpb->bpb_totsec16); - printf("bpb_media = %02x\n", bpb->bpb_media); - printf("bpb_fatsz16 = %d\n", bpb->bpb_fatsz16); - printf("bpb_secpertrk = %d\n", bpb->bpb_secpertrk); - printf("bpb_numheads = %d\n", bpb->bpb_numheads); - printf("bpb_hiddsec = %u\n", bpb->bpb_hiddsec); - printf("bpb_totsec32 = %u\n", bpb->bpb_totsec32); - - printf("bs_drvnum = %d\n", bpb->bs_drvnum); - printf("bs_bootsig = %02x\n", bpb->bs_bootsig); - if(bpb->bs_bootsig == 0x29) - { - printf("bs_volid = %xl\n", bpb->bs_volid); - printf("bs_vollab = \"%s\"\n", bpb->bs_vollab); - printf("bs_filsystype = \"%s\"\n", bpb->bs_filsystype); - } - - printf("bpb_fatsz32 = %u\n", bpb->bpb_fatsz32); - printf("last_word = %04x\n", bpb->last_word); - - printf("fat_type = FAT32\n"); -} - -void dbg_dir(struct bpb *bpb, int currdir) -{ - struct fat_dirent dent; - struct fat_direntry de; - - if(fat_opendir(bpb, &dent, currdir) >= 0) - { - while(fat_getnext(bpb, &dent, &de) >= 0) - { - printf("%s (%d)\n", de.name,de.firstcluster); - } - } - else - { - fprintf(stderr, "Could not read dir on cluster %d\n", currdir); - } -} - -void dbg_type(struct bpb *bpb, int cluster) -{ - unsigned char buf[SECTOR_SIZE*5]; - struct fat_fileent ent; - int i; - - fat_open(bpb,cluster,&ent); - - for (i=0;i<5;i++) - if(fat_read(bpb, &ent, 1, buf) >= 0) - { - buf[SECTOR_SIZE]=0; - printf("%s\n", buf); - } - else - { - fprintf(stderr, "Could not read file on cluster %d\n", cluster); - } -} - -char current_directory[256] = "\\"; -int last_secnum = 0; - -void dbg_prompt(void) -{ - printf("C:%s> ", current_directory); -} - -void dbg_console(struct bpb* bpb) -{ - 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")) - { - int secnum = 0; - if((s = strtok(NULL, " \n"))) - { - secnum = atoi(s); - } - dbg_dir(bpb, secnum); - continue; - } - - if(!strcasecmp(s, "ds")) - { - /* Remember the command */ - strcpy(last_cmd, s); - - if((s = strtok(NULL, " \n"))) - { - last_secnum = atoi(s); - } - else - { - last_secnum++; - } - printf("secnum: %d\n", last_secnum); - dbg_dump_sector(last_secnum); - continue; - } - - if(!strcasecmp(s, "type")) - { - int cluster = 0; - if((s = strtok(NULL, " \n"))) - { - cluster = atoi(s); - } - dbg_type(bpb,cluster); - continue; - } - - if(!strcasecmp(s, "exit") || - !strcasecmp(s, "x")) - { - quit = 1; - } - } - } - } -} diff --git a/firmware/test/fat/debug.h b/firmware/test/fat/debug.h deleted file mode 100644 index ff786ab2b1..0000000000 --- a/firmware/test/fat/debug.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef DEBUG_H -#define DEBUG_H - -void dbg_dump_sector(int sec); -void dbg_dump_buffer(unsigned char *buf); -void dbg_print_bpb(struct bpb *bpb); -void dbg_console(struct bpb *bpb); - -#endif diff --git a/firmware/test/fat/main.c b/firmware/test/fat/main.c new file mode 100644 index 0000000000..ffe3dd8764 --- /dev/null +++ b/firmware/test/fat/main.c @@ -0,0 +1,219 @@ +#include +#include +#include +#include "fat.h" +#include "ata.h" +#include "debug.h" +#include "disk.h" + +void dbg_dump_sector(int sec); +void dbg_dump_buffer(unsigned char *buf); +void dbg_print_bpb(struct bpb *bpb); +void dbg_console(struct bpb *bpb); + +void dbg_dump_sector(int sec) +{ + unsigned char buf[512]; + + ata_read_sectors(sec,1,buf); + printf("---< Sector %d >-----------------------------------------\n", sec); + dbg_dump_buffer(buf); +} + +void dbg_dump_buffer(unsigned char *buf) +{ + int i, j; + unsigned char c; + unsigned char ascii[33]; + + for(i = 0;i < 512/16;i++) + { + for(j = 0;j < 16;j++) + { + c = buf[i*16+j]; + + printf("%02x ", c); + if(c < 32 || c > 127) + { + ascii[j] = '.'; + } + else + { + ascii[j] = c; + } + } + + ascii[j] = 0; + printf("%s\n", ascii); + } +} + +void dbg_print_bpb(struct bpb *bpb) +{ + printf("bpb_oemname = \"%s\"\n", bpb->bs_oemname); + printf("bpb_bytspersec = %d\n", bpb->bpb_bytspersec); + printf("bpb_secperclus = %d\n", bpb->bpb_secperclus); + printf("bpb_rsvdseccnt = %d\n", bpb->bpb_rsvdseccnt); + printf("bpb_numfats = %d\n", bpb->bpb_numfats); + printf("bpb_rootentcnt = %d\n", bpb->bpb_rootentcnt); + printf("bpb_totsec16 = %d\n", bpb->bpb_totsec16); + printf("bpb_media = %02x\n", bpb->bpb_media); + printf("bpb_fatsz16 = %d\n", bpb->bpb_fatsz16); + printf("bpb_secpertrk = %d\n", bpb->bpb_secpertrk); + printf("bpb_numheads = %d\n", bpb->bpb_numheads); + printf("bpb_hiddsec = %u\n", bpb->bpb_hiddsec); + printf("bpb_totsec32 = %u\n", bpb->bpb_totsec32); + + printf("bs_drvnum = %d\n", bpb->bs_drvnum); + printf("bs_bootsig = %02x\n", bpb->bs_bootsig); + if(bpb->bs_bootsig == 0x29) + { + printf("bs_volid = %xl\n", bpb->bs_volid); + printf("bs_vollab = \"%s\"\n", bpb->bs_vollab); + printf("bs_filsystype = \"%s\"\n", bpb->bs_filsystype); + } + + printf("bpb_fatsz32 = %u\n", bpb->bpb_fatsz32); + printf("last_word = %04x\n", bpb->last_word); + + printf("fat_type = FAT32\n"); +} + +void dbg_dir(struct bpb *bpb, int currdir) +{ + struct fat_dirent dent; + struct fat_direntry de; + + if(fat_opendir(bpb, &dent, currdir) >= 0) + { + while(fat_getnext(bpb, &dent, &de) >= 0) + { + printf("%s (%d)\n", de.name,de.firstcluster); + } + } + else + { + fprintf(stderr, "Could not read dir on cluster %d\n", currdir); + } +} + +void dbg_type(struct bpb *bpb, int cluster) +{ + unsigned char buf[SECTOR_SIZE*5]; + struct fat_fileent ent; + int i; + + fat_open(bpb,cluster,&ent); + + for (i=0;i<5;i++) + if(fat_read(bpb, &ent, 1, buf) >= 0) + { + buf[SECTOR_SIZE]=0; + printf("%s\n", buf); + } + else + { + fprintf(stderr, "Could not read file on cluster %d\n", cluster); + } +} + +char current_directory[256] = "\\"; +int last_secnum = 0; + +void dbg_prompt(void) +{ + printf("C:%s> ", current_directory); +} + +void dbg_console(struct bpb* bpb) +{ + 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")) + { + int secnum = 0; + if((s = strtok(NULL, " \n"))) + { + secnum = atoi(s); + } + dbg_dir(bpb, secnum); + continue; + } + + if(!strcasecmp(s, "ds")) + { + /* Remember the command */ + strcpy(last_cmd, s); + + if((s = strtok(NULL, " \n"))) + { + last_secnum = atoi(s); + } + else + { + last_secnum++; + } + printf("secnum: %d\n", last_secnum); + dbg_dump_sector(last_secnum); + continue; + } + + if(!strcasecmp(s, "type")) + { + int cluster = 0; + if((s = strtok(NULL, " \n"))) + { + cluster = atoi(s); + } + dbg_type(bpb,cluster); + continue; + } + + if(!strcasecmp(s, "exit") || + !strcasecmp(s, "x")) + { + quit = 1; + } + } + } + } +} + +int main(int argc, char *argv[]) +{ + struct bpb bpb; + + if(ata_init()) { + DEBUGF("*** Warning! The disk is uninitialized\n"); + return -1; + } + if (disk_init()) { + DEBUGF("*** Failed reading partitions\n"); + return -1; + } + + if(fat_mount(&bpb,part[0].start)) { + DEBUGF("*** Failed mounting fat\n"); + } + + dbg_console(&bpb); + return 0; +} + -- cgit v1.2.3