Add some screenshots for Sansa c200.
[Rockbox.git] / firmware / common / disk.c
blob563bb05ec14e59e0cb312928a8f7b1e623302c79
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Björn Stenberg
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include "ata.h"
21 #include "debug.h"
22 #include "fat.h"
23 #ifdef HAVE_HOTSWAP
24 #include "hotswap.h"
25 #include "dir.h" /* for release_dirs() */
26 #include "file.h" /* for release_files() */
27 #endif
28 #include "disk.h"
30 /* Partition table entry layout:
31 -----------------------
32 0: 0x80 - active
33 1: starting head
34 2: starting sector
35 3: starting cylinder
36 4: partition type
37 5: end head
38 6: end sector
39 7: end cylinder
40 8-11: starting sector (LBA)
41 12-15: nr of sectors in partition
44 #define BYTES2INT32(array,pos) \
45 ((long)array[pos] | ((long)array[pos+1] << 8 ) | \
46 ((long)array[pos+2] << 16 ) | ((long)array[pos+3] << 24 ))
48 static struct partinfo part[8]; /* space for 4 partitions on 2 drives */
49 static int vol_drive[NUM_VOLUMES]; /* mounted to which drive (-1 if none) */
51 struct partinfo* disk_init(IF_MV_NONVOID(int drive))
53 int i;
54 unsigned char sector[512];
55 #ifdef HAVE_MULTIVOLUME
56 /* For each drive, start at a different position, in order not to destroy
57 the first entry of drive 0.
58 That one is needed to calculate config sector position. */
59 struct partinfo* pinfo = &part[drive*4];
60 if ((size_t)drive >= sizeof(part)/sizeof(*part)/4)
61 return NULL; /* out of space in table */
62 #else
63 struct partinfo* pinfo = part;
64 #endif
66 ata_read_sectors(IF_MV2(drive,) 0,1,&sector);
68 /* check that the boot sector is initialized */
69 if ( (sector[510] != 0x55) ||
70 (sector[511] != 0xaa)) {
71 DEBUGF("Bad boot sector signature\n");
72 return NULL;
75 /* parse partitions */
76 for ( i=0; i<4; i++ ) {
77 unsigned char* ptr = sector + 0x1be + 16*i;
78 pinfo[i].type = ptr[4];
79 pinfo[i].start = BYTES2INT32(ptr, 8);
80 pinfo[i].size = BYTES2INT32(ptr, 12);
82 DEBUGF("Part%d: Type %02x, start: %08lx size: %08lx\n",
83 i,pinfo[i].type,pinfo[i].start,pinfo[i].size);
85 /* extended? */
86 if ( pinfo[i].type == 5 ) {
87 /* not handled yet */
91 return pinfo;
94 struct partinfo* disk_partinfo(int partition)
96 return &part[partition];
99 int disk_mount_all(void)
101 int mounted;
102 int i;
104 #ifdef HAVE_MMC
105 card_enable_monitoring(false);
106 #endif
108 fat_init(); /* reset all mounted partitions */
109 for (i=0; i<NUM_VOLUMES; i++)
110 vol_drive[i] = -1; /* mark all as unassigned */
112 mounted = disk_mount(0);
113 #ifdef HAVE_HOTSWAP
114 if (card_detect())
116 mounted += disk_mount(1); /* try 2nd "drive", too */
118 #ifdef HAVE_MMC
119 card_enable_monitoring(true);
120 #endif
121 #endif
123 return mounted;
126 static int get_free_volume(void)
128 int i;
129 for (i=0; i<NUM_VOLUMES; i++)
131 if (vol_drive[i] == -1) /* unassigned? */
132 return i;
135 return -1; /* none found */
138 int disk_mount(int drive)
140 int i;
141 int mounted = 0; /* reset partition-on-drive flag */
142 int volume = get_free_volume();
143 struct partinfo* pinfo = disk_init(IF_MV(drive));
145 if (pinfo == NULL)
147 return 0;
149 #ifndef ELIO_TPJ1022
150 /* The Elio's hard drive has no partition table and probing for partitions causes
151 Rockbox to crash - so we temporarily disable the probing until we fix the
152 real problem. */
153 for (i=0; volume != -1 && i<4; i++)
155 #ifdef MAX_LOG_SECTOR_SIZE
156 int j;
158 for (j = 1; j <= (MAX_LOG_SECTOR_SIZE/SECTOR_SIZE); j <<= 1)
160 if (!fat_mount(IF_MV2(volume,) IF_MV2(drive,) pinfo[i].start * j))
162 pinfo[i].start *= j;
163 pinfo[i].size *= j;
164 mounted++;
165 vol_drive[volume] = drive; /* remember the drive for this volume */
166 volume = get_free_volume(); /* prepare next entry */
167 break;
170 #else
171 if (!fat_mount(IF_MV2(volume,) IF_MV2(drive,) pinfo[i].start))
173 mounted++;
174 vol_drive[volume] = drive; /* remember the drive for this volume */
175 volume = get_free_volume(); /* prepare next entry */
177 #endif
179 #endif
181 if (mounted == 0 && volume != -1) /* none of the 4 entries worked? */
182 { /* try "superfloppy" mode */
183 DEBUGF("No partition found, trying to mount sector 0.\n");
184 if (!fat_mount(IF_MV2(volume,) IF_MV2(drive,) 0))
186 mounted = 1;
187 vol_drive[volume] = drive; /* remember the drive for this volume */
190 return mounted;
193 #ifdef HAVE_HOTSWAP
194 int disk_unmount(int drive)
196 int unmounted = 0;
197 int i;
198 for (i=0; i<NUM_VOLUMES; i++)
200 if (vol_drive[i] == drive)
201 { /* force releasing resources */
202 vol_drive[i] = -1; /* mark unused */
203 unmounted++;
204 release_files(i);
205 release_dirs(i);
206 fat_unmount(i, false);
210 return unmounted;
212 #endif /* #ifdef HAVE_HOTSWAP */