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/Makefile | 4 +- firmware/common/disk.c | 74 +++++++++++++++ firmware/common/disk.h | 32 +++++++ firmware/drivers/fat.c | 59 ++++-------- firmware/drivers/fat.h | 3 +- firmware/test/fat/Makefile | 13 ++- firmware/test/fat/debug.c | 191 --------------------------------------- firmware/test/fat/debug.h | 9 -- firmware/test/fat/main.c | 219 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 355 insertions(+), 249 deletions(-) create mode 100644 firmware/common/disk.c create mode 100644 firmware/common/disk.h delete mode 100644 firmware/test/fat/debug.c delete mode 100644 firmware/test/fat/debug.h create mode 100644 firmware/test/fat/main.c diff --git a/firmware/Makefile b/firmware/Makefile index 91cb19955e..efa7c59b4e 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -35,10 +35,10 @@ archos.bin : archos.elf $(OC) -O binary archos.elf archos.bin archos.asm: archos.bin - sh2d -sh1 archos.bin > archos.asm + ../tools/sh2d -sh1 archos.bin > archos.asm archos.mod : archos.bin - scramble archos.bin archos.mod + ../tools/scramble archos.bin archos.mod dist: tar czvf dist.tar.gz Makefile main.c start.s app.lds diff --git a/firmware/common/disk.c b/firmware/common/disk.c new file mode 100644 index 0000000000..9572f115e5 --- /dev/null +++ b/firmware/common/disk.c @@ -0,0 +1,74 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 by Björn Stenberg + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include "ata.h" +#include "debug.h" +#include "disk.h" + +/* Partition table entry layout: + ----------------------- + 0: 0x80 - active + 1: starting head + 2: starting sector + 3: starting cylinder + 4: partition type + 5: end head + 6: end sector + 7: end cylinder + 8-11: starting sector (LBA) + 12-15: nr of sectors in partition +*/ + +#define BYTES2INT32(array,pos) \ + (array[pos] | (array[pos+1] << 8 ) | \ + (array[pos+2] << 16 ) | (array[pos+3] << 24 )) + +struct partinfo part[8]; + +int disk_init(void) +{ + int i; + unsigned char sector[512]; + + ata_read_sectors(0,1,§or); + + /* check that the boot sector is initialized */ + if ( (sector[510] != 0x55) || + (sector[511] != 0xaa)) { + DEBUGF("Bad boot sector signature\n"); + return -1; + } + + /* parse partitions */ + for ( i=0; i<4; i++ ) { + unsigned char* ptr = sector + 0x1be + 16*i; + part[i].type = ptr[4]; + part[i].start = BYTES2INT32(ptr, 8); + part[i].size = BYTES2INT32(ptr, 12); + + DEBUGF("Part%d: Type %02x, start: %08x size: %08x\n", + i,part[i].type,part[i].start,part[i].size); + + /* extended? */ + if ( part[i].type == 5 ) { + /* not handled yet */ + } + } + + return 0; +} diff --git a/firmware/common/disk.h b/firmware/common/disk.h new file mode 100644 index 0000000000..1e95c73c6b --- /dev/null +++ b/firmware/common/disk.h @@ -0,0 +1,32 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 by Björn Stenberg + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef _DISK_H_ +#define _DISK_H_ + +struct partinfo { + unsigned long start; /* first sector (LBA) */ + unsigned long size; /* number of sectors */ + unsigned char type; +}; + +extern struct partinfo part[8]; + +int disk_init(void); + +#endif diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c index 6192d4619f..329e149a7a 100644 --- a/firmware/drivers/fat.c +++ b/firmware/drivers/fat.c @@ -133,25 +133,6 @@ static int fat_cache_dirty[256]; static unsigned char lastsector[SECTOR_SIZE]; static unsigned char lastsector2[SECTOR_SIZE]; -#ifdef TEST_FAT - -int main(int argc, char *argv[]) -{ - struct bpb bpb; - - memset(fat_cache, 0, sizeof(fat_cache)); - memset(fat_cache_dirty, 0, sizeof(fat_cache_dirty)); - - if(ata_init()) - DEBUGF("*** Warning! The disk is uninitialized\n"); - else - fat_mount(&bpb); - - dbg_console(&bpb); - return 0; -} -#endif - static int sec2cluster(struct bpb *bpb, unsigned int sec) { if ( sec < bpb->firstdatasector ) @@ -183,7 +164,7 @@ static int first_sector_of_cluster(struct bpb *bpb, unsigned int cluster) return (cluster - 2) * bpb->bpb_secperclus + bpb->firstdatasector; } -int fat_mount(struct bpb *bpb) +int fat_mount(struct bpb *bpb, int startsector) { unsigned char buf[SECTOR_SIZE]; int err; @@ -191,7 +172,7 @@ int fat_mount(struct bpb *bpb) int countofclusters; /* Read the sector */ - err = ata_read_sectors(0,1,buf); + err = ata_read_sectors(startsector,1,buf); if(err) { DEBUGF( "fat_mount() - Couldn't read BPB (error code %i)\n", @@ -200,6 +181,7 @@ int fat_mount(struct bpb *bpb) } memset(bpb, 0, sizeof(struct bpb)); + bpb->startsector = startsector; strncpy(bpb->bs_oemname, &buf[BS_OEMNAME], 8); bpb->bs_oemname[8] = 0; @@ -306,20 +288,6 @@ static int bpb_is_sane(struct bpb *bpb) DEBUGF( "bpb_is_sane() - Warning: RootEntCnt is not 512 (%i)\n", bpb->bpb_rootentcnt); } - if(bpb->bpb_totsec16 < 200) - { - if(bpb->bpb_totsec16 == 0) - { - DEBUGF( "bpb_is_sane() - Error: TotSec16 is 0\n"); - return -1; - } - else - { - DEBUGF( "bpb_is_sane() - Warning: TotSec16 " - "is quite small (%i)\n", - bpb->bpb_totsec16); - } - } if(bpb->bpb_media != 0xf0 && bpb->bpb_media < 0xf8) { DEBUGF( "bpb_is_sane() - Warning: Non-standard " @@ -349,7 +317,7 @@ static void *cache_fat_sector(struct bpb *bpb, int secnum) DEBUGF( "cache_fat_sector() - Out of memory\n"); return NULL; } - if(ata_read_sectors(secnum,1,sec)) + if(ata_read_sectors(secnum+bpb->startsector,1,sec)) { DEBUGF( "cache_fat_sector() - Could" " not read sector %d\n", @@ -447,14 +415,17 @@ static int flush_fat(struct bpb *bpb) { DEBUGF("Flushing FAT sector %d\n", i); sec = fat_cache[i]; - err = ata_write_sectors(i + bpb->bpb_rsvdseccnt,1,sec); + err = ata_write_sectors(i + bpb->bpb_rsvdseccnt + bpb->startsector, + 1,sec); if(err) { DEBUGF( "flush_fat() - Couldn't write" " sector (%d)\n", i + bpb->bpb_rsvdseccnt); return -1; } - err = ata_write_sectors(i + bpb->bpb_rsvdseccnt + fatsz,1,sec); + err = ata_write_sectors(i + bpb->bpb_rsvdseccnt + fatsz + + bpb->startsector, + 1,sec); if(err) { DEBUGF( "flush_fat() - Couldn't write" @@ -559,7 +530,7 @@ static int add_dir_entry(struct bpb *bpb, DEBUGF("Reading sector %d...\n", sec); /* Read the next sector in the current dir */ - err = ata_read_sectors(sec,1,buf); + err = ata_read_sectors(sec + bpb->startsector,1,buf); if(err) { DEBUGF( "add_dir_entry() - Couldn't read dir sector" @@ -624,7 +595,7 @@ static int add_dir_entry(struct bpb *bpb, } } - err = ata_write_sectors(sec,1,buf); + err = ata_write_sectors(sec + bpb->startsector,1,buf); if(err) { DEBUGF( "add_dir_entry() - " @@ -821,7 +792,8 @@ int fat_read(struct bpb *bpb, int err, i; for ( i=0; istartsector, 1, + (char*)buf+(i*SECTOR_SIZE)); if(err) { DEBUGF( "fat_read() - Couldn't read sector %d" " (error code %i)\n", sector,err); @@ -898,7 +870,7 @@ int fat_opendir(struct bpb *bpb, } /* Read the first sector in the current dir */ - err = ata_read_sectors(sec,1,ent->cached_buf); + err = ata_read_sectors(sec + bpb->startsector,1,ent->cached_buf); if(err) { DEBUGF( "fat_getfirst() - Couldn't read dir sector" @@ -1037,7 +1009,8 @@ int fat_getnext(struct bpb *bpb, } /* Read the next sector */ - err = ata_read_sectors(ent->cached_sec,1,ent->cached_buf); + err = ata_read_sectors(ent->cached_sec + bpb->startsector, 1, + ent->cached_buf); if(err) { DEBUGF( "fat_getnext() - Couldn't read dir sector" diff --git a/firmware/drivers/fat.h b/firmware/drivers/fat.h index 7f014a8aa1..faec5384dd 100644 --- a/firmware/drivers/fat.h +++ b/firmware/drivers/fat.h @@ -60,6 +60,7 @@ struct bpb int totalsectors; int rootdirsector; int firstdatasector; + int startsector; }; struct fat_direntry @@ -100,7 +101,7 @@ struct fat_fileent int sectornum; /* sector number in this cluster */ }; -extern int fat_mount(struct bpb *bpb); +extern int fat_mount(struct bpb *bpb, int startsector); #ifdef DISK_WRITE extern int fat_create_file(struct bpb *bpb, 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