diff options
author | Björn Stenberg <bjorn@haxx.se> | 2002-05-03 11:59:53 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2002-05-03 11:59:53 +0000 |
commit | c7f7934e8f74be4b98abe83c2f6a2593fd294cf0 (patch) | |
tree | cdebbde186e8db107472c5a7b0775f050e7f4560 /firmware | |
parent | 86a59ecdf6e6bf3e45938c0ca305b892777b28e0 (diff) | |
download | rockbox-c7f7934e8f74be4b98abe83c2f6a2593fd294cf0.tar.gz rockbox-c7f7934e8f74be4b98abe83c2f6a2593fd294cf0.zip |
Added disk/partition handling
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@405 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware')
-rw-r--r-- | firmware/Makefile | 4 | ||||
-rw-r--r-- | firmware/common/disk.c | 74 | ||||
-rw-r--r-- | firmware/common/disk.h | 32 | ||||
-rw-r--r-- | firmware/drivers/fat.c | 59 | ||||
-rw-r--r-- | firmware/drivers/fat.h | 3 | ||||
-rw-r--r-- | firmware/test/fat/Makefile | 13 | ||||
-rw-r--r-- | firmware/test/fat/debug.h | 9 | ||||
-rw-r--r-- | firmware/test/fat/main.c (renamed from firmware/test/fat/debug.c) | 28 |
8 files changed, 164 insertions, 58 deletions
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 | |||
35 | $(OC) -O binary archos.elf archos.bin | 35 | $(OC) -O binary archos.elf archos.bin |
36 | 36 | ||
37 | archos.asm: archos.bin | 37 | archos.asm: archos.bin |
38 | sh2d -sh1 archos.bin > archos.asm | 38 | ../tools/sh2d -sh1 archos.bin > archos.asm |
39 | 39 | ||
40 | archos.mod : archos.bin | 40 | archos.mod : archos.bin |
41 | scramble archos.bin archos.mod | 41 | ../tools/scramble archos.bin archos.mod |
42 | 42 | ||
43 | dist: | 43 | dist: |
44 | tar czvf dist.tar.gz Makefile main.c start.s app.lds | 44 | 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 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by Björn Stenberg | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #include "ata.h" | ||
20 | #include "debug.h" | ||
21 | #include "disk.h" | ||
22 | |||
23 | /* Partition table entry layout: | ||
24 | ----------------------- | ||
25 | 0: 0x80 - active | ||
26 | 1: starting head | ||
27 | 2: starting sector | ||
28 | 3: starting cylinder | ||
29 | 4: partition type | ||
30 | 5: end head | ||
31 | 6: end sector | ||
32 | 7: end cylinder | ||
33 | 8-11: starting sector (LBA) | ||
34 | 12-15: nr of sectors in partition | ||
35 | */ | ||
36 | |||
37 | #define BYTES2INT32(array,pos) \ | ||
38 | (array[pos] | (array[pos+1] << 8 ) | \ | ||
39 | (array[pos+2] << 16 ) | (array[pos+3] << 24 )) | ||
40 | |||
41 | struct partinfo part[8]; | ||
42 | |||
43 | int disk_init(void) | ||
44 | { | ||
45 | int i; | ||
46 | unsigned char sector[512]; | ||
47 | |||
48 | ata_read_sectors(0,1,§or); | ||
49 | |||
50 | /* check that the boot sector is initialized */ | ||
51 | if ( (sector[510] != 0x55) || | ||
52 | (sector[511] != 0xaa)) { | ||
53 | DEBUGF("Bad boot sector signature\n"); | ||
54 | return -1; | ||
55 | } | ||
56 | |||
57 | /* parse partitions */ | ||
58 | for ( i=0; i<4; i++ ) { | ||
59 | unsigned char* ptr = sector + 0x1be + 16*i; | ||
60 | part[i].type = ptr[4]; | ||
61 | part[i].start = BYTES2INT32(ptr, 8); | ||
62 | part[i].size = BYTES2INT32(ptr, 12); | ||
63 | |||
64 | DEBUGF("Part%d: Type %02x, start: %08x size: %08x\n", | ||
65 | i,part[i].type,part[i].start,part[i].size); | ||
66 | |||
67 | /* extended? */ | ||
68 | if ( part[i].type == 5 ) { | ||
69 | /* not handled yet */ | ||
70 | } | ||
71 | } | ||
72 | |||
73 | return 0; | ||
74 | } | ||
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 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by Björn Stenberg | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #ifndef _DISK_H_ | ||
20 | #define _DISK_H_ | ||
21 | |||
22 | struct partinfo { | ||
23 | unsigned long start; /* first sector (LBA) */ | ||
24 | unsigned long size; /* number of sectors */ | ||
25 | unsigned char type; | ||
26 | }; | ||
27 | |||
28 | extern struct partinfo part[8]; | ||
29 | |||
30 | int disk_init(void); | ||
31 | |||
32 | #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]; | |||
133 | static unsigned char lastsector[SECTOR_SIZE]; | 133 | static unsigned char lastsector[SECTOR_SIZE]; |
134 | static unsigned char lastsector2[SECTOR_SIZE]; | 134 | static unsigned char lastsector2[SECTOR_SIZE]; |
135 | 135 | ||
136 | #ifdef TEST_FAT | ||
137 | |||
138 | int main(int argc, char *argv[]) | ||
139 | { | ||
140 | struct bpb bpb; | ||
141 | |||
142 | memset(fat_cache, 0, sizeof(fat_cache)); | ||
143 | memset(fat_cache_dirty, 0, sizeof(fat_cache_dirty)); | ||
144 | |||
145 | if(ata_init()) | ||
146 | DEBUGF("*** Warning! The disk is uninitialized\n"); | ||
147 | else | ||
148 | fat_mount(&bpb); | ||
149 | |||
150 | dbg_console(&bpb); | ||
151 | return 0; | ||
152 | } | ||
153 | #endif | ||
154 | |||
155 | static int sec2cluster(struct bpb *bpb, unsigned int sec) | 136 | static int sec2cluster(struct bpb *bpb, unsigned int sec) |
156 | { | 137 | { |
157 | if ( sec < bpb->firstdatasector ) | 138 | if ( sec < bpb->firstdatasector ) |
@@ -183,7 +164,7 @@ static int first_sector_of_cluster(struct bpb *bpb, unsigned int cluster) | |||
183 | return (cluster - 2) * bpb->bpb_secperclus + bpb->firstdatasector; | 164 | return (cluster - 2) * bpb->bpb_secperclus + bpb->firstdatasector; |
184 | } | 165 | } |
185 | 166 | ||
186 | int fat_mount(struct bpb *bpb) | 167 | int fat_mount(struct bpb *bpb, int startsector) |
187 | { | 168 | { |
188 | unsigned char buf[SECTOR_SIZE]; | 169 | unsigned char buf[SECTOR_SIZE]; |
189 | int err; | 170 | int err; |
@@ -191,7 +172,7 @@ int fat_mount(struct bpb *bpb) | |||
191 | int countofclusters; | 172 | int countofclusters; |
192 | 173 | ||
193 | /* Read the sector */ | 174 | /* Read the sector */ |
194 | err = ata_read_sectors(0,1,buf); | 175 | err = ata_read_sectors(startsector,1,buf); |
195 | if(err) | 176 | if(err) |
196 | { | 177 | { |
197 | DEBUGF( "fat_mount() - Couldn't read BPB (error code %i)\n", | 178 | DEBUGF( "fat_mount() - Couldn't read BPB (error code %i)\n", |
@@ -200,6 +181,7 @@ int fat_mount(struct bpb *bpb) | |||
200 | } | 181 | } |
201 | 182 | ||
202 | memset(bpb, 0, sizeof(struct bpb)); | 183 | memset(bpb, 0, sizeof(struct bpb)); |
184 | bpb->startsector = startsector; | ||
203 | 185 | ||
204 | strncpy(bpb->bs_oemname, &buf[BS_OEMNAME], 8); | 186 | strncpy(bpb->bs_oemname, &buf[BS_OEMNAME], 8); |
205 | bpb->bs_oemname[8] = 0; | 187 | bpb->bs_oemname[8] = 0; |
@@ -306,20 +288,6 @@ static int bpb_is_sane(struct bpb *bpb) | |||
306 | DEBUGF( "bpb_is_sane() - Warning: RootEntCnt is not 512 (%i)\n", | 288 | DEBUGF( "bpb_is_sane() - Warning: RootEntCnt is not 512 (%i)\n", |
307 | bpb->bpb_rootentcnt); | 289 | bpb->bpb_rootentcnt); |
308 | } | 290 | } |
309 | if(bpb->bpb_totsec16 < 200) | ||
310 | { | ||
311 | if(bpb->bpb_totsec16 == 0) | ||
312 | { | ||
313 | DEBUGF( "bpb_is_sane() - Error: TotSec16 is 0\n"); | ||
314 | return -1; | ||
315 | } | ||
316 | else | ||
317 | { | ||
318 | DEBUGF( "bpb_is_sane() - Warning: TotSec16 " | ||
319 | "is quite small (%i)\n", | ||
320 | bpb->bpb_totsec16); | ||
321 | } | ||
322 | } | ||
323 | if(bpb->bpb_media != 0xf0 && bpb->bpb_media < 0xf8) | 291 | if(bpb->bpb_media != 0xf0 && bpb->bpb_media < 0xf8) |
324 | { | 292 | { |
325 | DEBUGF( "bpb_is_sane() - Warning: Non-standard " | 293 | DEBUGF( "bpb_is_sane() - Warning: Non-standard " |
@@ -349,7 +317,7 @@ static void *cache_fat_sector(struct bpb *bpb, int secnum) | |||
349 | DEBUGF( "cache_fat_sector() - Out of memory\n"); | 317 | DEBUGF( "cache_fat_sector() - Out of memory\n"); |
350 | return NULL; | 318 | return NULL; |
351 | } | 319 | } |
352 | if(ata_read_sectors(secnum,1,sec)) | 320 | if(ata_read_sectors(secnum+bpb->startsector,1,sec)) |
353 | { | 321 | { |
354 | DEBUGF( "cache_fat_sector() - Could" | 322 | DEBUGF( "cache_fat_sector() - Could" |
355 | " not read sector %d\n", | 323 | " not read sector %d\n", |
@@ -447,14 +415,17 @@ static int flush_fat(struct bpb *bpb) | |||
447 | { | 415 | { |
448 | DEBUGF("Flushing FAT sector %d\n", i); | 416 | DEBUGF("Flushing FAT sector %d\n", i); |
449 | sec = fat_cache[i]; | 417 | sec = fat_cache[i]; |
450 | err = ata_write_sectors(i + bpb->bpb_rsvdseccnt,1,sec); | 418 | err = ata_write_sectors(i + bpb->bpb_rsvdseccnt + bpb->startsector, |
419 | 1,sec); | ||
451 | if(err) | 420 | if(err) |
452 | { | 421 | { |
453 | DEBUGF( "flush_fat() - Couldn't write" | 422 | DEBUGF( "flush_fat() - Couldn't write" |
454 | " sector (%d)\n", i + bpb->bpb_rsvdseccnt); | 423 | " sector (%d)\n", i + bpb->bpb_rsvdseccnt); |
455 | return -1; | 424 | return -1; |
456 | } | 425 | } |
457 | err = ata_write_sectors(i + bpb->bpb_rsvdseccnt + fatsz,1,sec); | 426 | err = ata_write_sectors(i + bpb->bpb_rsvdseccnt + fatsz + |
427 | bpb->startsector, | ||
428 | 1,sec); | ||
458 | if(err) | 429 | if(err) |
459 | { | 430 | { |
460 | DEBUGF( "flush_fat() - Couldn't write" | 431 | DEBUGF( "flush_fat() - Couldn't write" |
@@ -559,7 +530,7 @@ static int add_dir_entry(struct bpb *bpb, | |||
559 | 530 | ||
560 | DEBUGF("Reading sector %d...\n", sec); | 531 | DEBUGF("Reading sector %d...\n", sec); |
561 | /* Read the next sector in the current dir */ | 532 | /* Read the next sector in the current dir */ |
562 | err = ata_read_sectors(sec,1,buf); | 533 | err = ata_read_sectors(sec + bpb->startsector,1,buf); |
563 | if(err) | 534 | if(err) |
564 | { | 535 | { |
565 | DEBUGF( "add_dir_entry() - Couldn't read dir sector" | 536 | DEBUGF( "add_dir_entry() - Couldn't read dir sector" |
@@ -624,7 +595,7 @@ static int add_dir_entry(struct bpb *bpb, | |||
624 | } | 595 | } |
625 | } | 596 | } |
626 | 597 | ||
627 | err = ata_write_sectors(sec,1,buf); | 598 | err = ata_write_sectors(sec + bpb->startsector,1,buf); |
628 | if(err) | 599 | if(err) |
629 | { | 600 | { |
630 | DEBUGF( "add_dir_entry() - " | 601 | DEBUGF( "add_dir_entry() - " |
@@ -821,7 +792,8 @@ int fat_read(struct bpb *bpb, | |||
821 | int err, i; | 792 | int err, i; |
822 | 793 | ||
823 | for ( i=0; i<sectorcount; i++ ) { | 794 | for ( i=0; i<sectorcount; i++ ) { |
824 | err = ata_read_sectors(sector,1,(char*)buf+(i*SECTOR_SIZE)); | 795 | err = ata_read_sectors(sector + bpb->startsector, 1, |
796 | (char*)buf+(i*SECTOR_SIZE)); | ||
825 | if(err) { | 797 | if(err) { |
826 | DEBUGF( "fat_read() - Couldn't read sector %d" | 798 | DEBUGF( "fat_read() - Couldn't read sector %d" |
827 | " (error code %i)\n", sector,err); | 799 | " (error code %i)\n", sector,err); |
@@ -898,7 +870,7 @@ int fat_opendir(struct bpb *bpb, | |||
898 | } | 870 | } |
899 | 871 | ||
900 | /* Read the first sector in the current dir */ | 872 | /* Read the first sector in the current dir */ |
901 | err = ata_read_sectors(sec,1,ent->cached_buf); | 873 | err = ata_read_sectors(sec + bpb->startsector,1,ent->cached_buf); |
902 | if(err) | 874 | if(err) |
903 | { | 875 | { |
904 | DEBUGF( "fat_getfirst() - Couldn't read dir sector" | 876 | DEBUGF( "fat_getfirst() - Couldn't read dir sector" |
@@ -1037,7 +1009,8 @@ int fat_getnext(struct bpb *bpb, | |||
1037 | } | 1009 | } |
1038 | 1010 | ||
1039 | /* Read the next sector */ | 1011 | /* Read the next sector */ |
1040 | err = ata_read_sectors(ent->cached_sec,1,ent->cached_buf); | 1012 | err = ata_read_sectors(ent->cached_sec + bpb->startsector, 1, |
1013 | ent->cached_buf); | ||
1041 | if(err) | 1014 | if(err) |
1042 | { | 1015 | { |
1043 | DEBUGF( "fat_getnext() - Couldn't read dir sector" | 1016 | 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 | |||
60 | int totalsectors; | 60 | int totalsectors; |
61 | int rootdirsector; | 61 | int rootdirsector; |
62 | int firstdatasector; | 62 | int firstdatasector; |
63 | int startsector; | ||
63 | }; | 64 | }; |
64 | 65 | ||
65 | struct fat_direntry | 66 | struct fat_direntry |
@@ -100,7 +101,7 @@ struct fat_fileent | |||
100 | int sectornum; /* sector number in this cluster */ | 101 | int sectornum; /* sector number in this cluster */ |
101 | }; | 102 | }; |
102 | 103 | ||
103 | extern int fat_mount(struct bpb *bpb); | 104 | extern int fat_mount(struct bpb *bpb, int startsector); |
104 | 105 | ||
105 | #ifdef DISK_WRITE | 106 | #ifdef DISK_WRITE |
106 | extern int fat_create_file(struct bpb *bpb, | 107 | 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 @@ | |||
1 | FIRMWARE = ../.. | ||
1 | DRIVERS = ../../drivers | 2 | DRIVERS = ../../drivers |
2 | 3 | ||
3 | CFLAGS = -g -Wall -DTEST_FAT -I$(DRIVERS) -I. | 4 | CFLAGS = -g -Wall -DTEST_FAT -I$(DRIVERS) -I$(FIRMWARE)/common -I$(FIRMWARE) -I. -DDEBUG -DCRT_DISPLAY |
4 | 5 | ||
5 | TARGET = fat | 6 | TARGET = fat |
6 | 7 | ||
7 | $(TARGET): fat.o ata-sim.o debug.o | 8 | $(TARGET): fat.o ata-sim.o debug.o main.o disk.o |
8 | gcc -g -o fat $+ -lfl | 9 | gcc -g -o fat $+ -lfl |
9 | 10 | ||
10 | fat.o: $(DRIVERS)/fat.c $(DRIVERS)/fat.h $(DRIVERS)/ata.h | 11 | fat.o: $(DRIVERS)/fat.c $(DRIVERS)/fat.h $(DRIVERS)/ata.h |
11 | $(CC) $(CFLAGS) -c $< -o $@ | 12 | $(CC) $(CFLAGS) -c $< -o $@ |
12 | 13 | ||
14 | disk.o: $(FIRMWARE)/common/disk.c | ||
15 | $(CC) $(CFLAGS) -c $< -o $@ | ||
16 | |||
17 | debug.o: $(FIRMWARE)/debug.c | ||
18 | $(CC) $(CFLAGS) -c $< -o $@ | ||
19 | |||
13 | ata-sim.o: ata-sim.c $(DRIVERS)/ata.h | 20 | ata-sim.o: ata-sim.c $(DRIVERS)/ata.h |
14 | 21 | ||
15 | debug.o: debug.c debug.h $(DRIVERS)/ata.h | 22 | main.o: main.c $(DRIVERS)/ata.h |
16 | 23 | ||
17 | clean: | 24 | clean: |
18 | rm -f *.o $(TARGET) | 25 | rm -f *.o $(TARGET) |
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 @@ | |||
1 | #ifndef DEBUG_H | ||
2 | #define DEBUG_H | ||
3 | |||
4 | void dbg_dump_sector(int sec); | ||
5 | void dbg_dump_buffer(unsigned char *buf); | ||
6 | void dbg_print_bpb(struct bpb *bpb); | ||
7 | void dbg_console(struct bpb *bpb); | ||
8 | |||
9 | #endif | ||
diff --git a/firmware/test/fat/debug.c b/firmware/test/fat/main.c index fa7ff8651a..ffe3dd8764 100644 --- a/firmware/test/fat/debug.c +++ b/firmware/test/fat/main.c | |||
@@ -4,6 +4,12 @@ | |||
4 | #include "fat.h" | 4 | #include "fat.h" |
5 | #include "ata.h" | 5 | #include "ata.h" |
6 | #include "debug.h" | 6 | #include "debug.h" |
7 | #include "disk.h" | ||
8 | |||
9 | void dbg_dump_sector(int sec); | ||
10 | void dbg_dump_buffer(unsigned char *buf); | ||
11 | void dbg_print_bpb(struct bpb *bpb); | ||
12 | void dbg_console(struct bpb *bpb); | ||
7 | 13 | ||
8 | void dbg_dump_sector(int sec) | 14 | void dbg_dump_sector(int sec) |
9 | { | 15 | { |
@@ -189,3 +195,25 @@ void dbg_console(struct bpb* bpb) | |||
189 | } | 195 | } |
190 | } | 196 | } |
191 | } | 197 | } |
198 | |||
199 | int main(int argc, char *argv[]) | ||
200 | { | ||
201 | struct bpb bpb; | ||
202 | |||
203 | if(ata_init()) { | ||
204 | DEBUGF("*** Warning! The disk is uninitialized\n"); | ||
205 | return -1; | ||
206 | } | ||
207 | if (disk_init()) { | ||
208 | DEBUGF("*** Failed reading partitions\n"); | ||
209 | return -1; | ||
210 | } | ||
211 | |||
212 | if(fat_mount(&bpb,part[0].start)) { | ||
213 | DEBUGF("*** Failed mounting fat\n"); | ||
214 | } | ||
215 | |||
216 | dbg_console(&bpb); | ||
217 | return 0; | ||
218 | } | ||
219 | |||