1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
25 #include "dir.h" /* for release_dirs() */
26 #include "file.h" /* for release_files() */
30 /* Partition table entry layout:
31 -----------------------
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
))
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 */
63 struct partinfo
* pinfo
= part
;
66 ata_read_sectors(IF_MV2(drive
,) 0,1,§or
);
68 /* check that the boot sector is initialized */
69 if ( (sector
[510] != 0x55) ||
70 (sector
[511] != 0xaa)) {
71 DEBUGF("Bad boot sector signature\n");
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
);
86 if ( pinfo
[i
].type
== 5 ) {
94 struct partinfo
* disk_partinfo(int partition
)
96 return &part
[partition
];
99 int disk_mount_all(void)
105 card_enable_monitoring(false);
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);
116 mounted
+= disk_mount(1); /* try 2nd "drive", too */
119 card_enable_monitoring(true);
126 static int get_free_volume(void)
129 for (i
=0; i
<NUM_VOLUMES
; i
++)
131 if (vol_drive
[i
] == -1) /* unassigned? */
135 return -1; /* none found */
138 int disk_mount(int drive
)
141 int mounted
= 0; /* reset partition-on-drive flag */
142 int volume
= get_free_volume();
143 struct partinfo
* pinfo
= disk_init(IF_MV(drive
));
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
153 for (i
=0; volume
!= -1 && i
<4; i
++)
155 #ifdef MAX_LOG_SECTOR_SIZE
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
))
165 vol_drive
[volume
] = drive
; /* remember the drive for this volume */
166 volume
= get_free_volume(); /* prepare next entry */
171 if (!fat_mount(IF_MV2(volume
,) IF_MV2(drive
,) pinfo
[i
].start
))
174 vol_drive
[volume
] = drive
; /* remember the drive for this volume */
175 volume
= get_free_volume(); /* prepare next entry */
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))
187 vol_drive
[volume
] = drive
; /* remember the drive for this volume */
194 int disk_unmount(int drive
)
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 */
206 fat_unmount(i
, false);
212 #endif /* #ifdef HAVE_HOTSWAP */