2002-05-03 11:59:53 +00:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* __________ __ ___.
|
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 by Bj<EFBFBD>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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2002-05-30 19:41:35 +00:00
|
|
|
|
#include <stdio.h>
|
2002-05-03 11:59:53 +00:00
|
|
|
|
#include "ata.h"
|
|
|
|
|
#include "debug.h"
|
2004-12-29 22:10:24 +00:00
|
|
|
|
#include "fat.h"
|
2007-06-30 02:08:27 +00:00
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
|
#include "hotswap.h"
|
2007-07-20 17:06:55 +00:00
|
|
|
|
#include "dir.h" /* for release_dirs() */
|
|
|
|
|
#include "file.h" /* for release_files() */
|
2004-12-29 22:10:24 +00:00
|
|
|
|
#endif
|
2002-05-03 11:59:53 +00:00
|
|
|
|
#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
|
|
|
|
|
*/
|
|
|
|
|
|
2005-01-23 23:29:35 +00:00
|
|
|
|
#define BYTES2INT32(array,pos) \
|
2005-02-27 20:44:26 +00:00
|
|
|
|
((long)array[pos] | ((long)array[pos+1] << 8 ) | \
|
2005-01-23 23:29:35 +00:00
|
|
|
|
((long)array[pos+2] << 16 ) | ((long)array[pos+3] << 24 ))
|
2002-05-03 11:59:53 +00:00
|
|
|
|
|
2004-12-29 22:10:24 +00:00
|
|
|
|
static struct partinfo part[8]; /* space for 4 partitions on 2 drives */
|
2005-01-28 21:32:16 +00:00
|
|
|
|
static int vol_drive[NUM_VOLUMES]; /* mounted to which drive (-1 if none) */
|
2002-05-03 11:59:53 +00:00
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
|
struct partinfo* disk_init(IF_MV_NONVOID(int drive))
|
2002-05-03 11:59:53 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
unsigned char sector[512];
|
2004-12-28 22:16:07 +00:00
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
|
/* For each drive, start at a different position, in order not to destroy
|
|
|
|
|
the first entry of drive 0.
|
|
|
|
|
That one is needed to calculate config sector position. */
|
|
|
|
|
struct partinfo* pinfo = &part[drive*4];
|
|
|
|
|
if ((size_t)drive >= sizeof(part)/sizeof(*part)/4)
|
|
|
|
|
return NULL; /* out of space in table */
|
|
|
|
|
#else
|
|
|
|
|
struct partinfo* pinfo = part;
|
|
|
|
|
#endif
|
2002-05-03 11:59:53 +00:00
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
|
ata_read_sectors(IF_MV2(drive,) 0,1,§or);
|
2002-05-03 11:59:53 +00:00
|
|
|
|
|
|
|
|
|
/* check that the boot sector is initialized */
|
|
|
|
|
if ( (sector[510] != 0x55) ||
|
|
|
|
|
(sector[511] != 0xaa)) {
|
|
|
|
|
DEBUGF("Bad boot sector signature\n");
|
2002-05-30 19:41:35 +00:00
|
|
|
|
return NULL;
|
2002-05-03 11:59:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* parse partitions */
|
|
|
|
|
for ( i=0; i<4; i++ ) {
|
|
|
|
|
unsigned char* ptr = sector + 0x1be + 16*i;
|
2004-12-28 22:16:07 +00:00
|
|
|
|
pinfo[i].type = ptr[4];
|
|
|
|
|
pinfo[i].start = BYTES2INT32(ptr, 8);
|
|
|
|
|
pinfo[i].size = BYTES2INT32(ptr, 12);
|
2002-05-03 11:59:53 +00:00
|
|
|
|
|
2005-01-23 23:29:35 +00:00
|
|
|
|
DEBUGF("Part%d: Type %02x, start: %08lx size: %08lx\n",
|
2004-12-28 22:16:07 +00:00
|
|
|
|
i,pinfo[i].type,pinfo[i].start,pinfo[i].size);
|
2002-05-03 11:59:53 +00:00
|
|
|
|
|
|
|
|
|
/* extended? */
|
2004-12-28 22:16:07 +00:00
|
|
|
|
if ( pinfo[i].type == 5 ) {
|
2002-05-03 11:59:53 +00:00
|
|
|
|
/* not handled yet */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
|
return pinfo;
|
2002-05-03 11:59:53 +00:00
|
|
|
|
}
|
2002-10-10 12:01:58 +00:00
|
|
|
|
|
|
|
|
|
struct partinfo* disk_partinfo(int partition)
|
|
|
|
|
{
|
|
|
|
|
return &part[partition];
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-29 22:10:24 +00:00
|
|
|
|
int disk_mount_all(void)
|
|
|
|
|
{
|
2005-01-28 21:32:16 +00:00
|
|
|
|
int mounted;
|
|
|
|
|
int i;
|
2005-05-16 15:21:09 +00:00
|
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
|
#ifdef HAVE_MMC
|
|
|
|
|
card_enable_monitoring(false);
|
2005-05-16 15:21:09 +00:00
|
|
|
|
#endif
|
2005-01-28 21:32:16 +00:00
|
|
|
|
|
|
|
|
|
fat_init(); /* reset all mounted partitions */
|
|
|
|
|
for (i=0; i<NUM_VOLUMES; i++)
|
|
|
|
|
vol_drive[i] = -1; /* mark all as unassigned */
|
|
|
|
|
|
|
|
|
|
mounted = disk_mount(0);
|
2007-06-30 02:08:27 +00:00
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
|
if (card_detect())
|
2004-12-29 22:10:24 +00:00
|
|
|
|
{
|
2005-01-28 21:32:16 +00:00
|
|
|
|
mounted += disk_mount(1); /* try 2nd "drive", too */
|
2004-12-29 22:10:24 +00:00
|
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
|
#ifdef HAVE_MMC
|
|
|
|
|
card_enable_monitoring(true);
|
2005-05-16 15:21:09 +00:00
|
|
|
|
#endif
|
2004-12-29 22:10:24 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2005-01-28 21:32:16 +00:00
|
|
|
|
return mounted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int get_free_volume(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i=0; i<NUM_VOLUMES; i++)
|
|
|
|
|
{
|
|
|
|
|
if (vol_drive[i] == -1) /* unassigned? */
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1; /* none found */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int disk_mount(int drive)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int mounted = 0; /* reset partition-on-drive flag */
|
|
|
|
|
int volume = get_free_volume();
|
|
|
|
|
struct partinfo* pinfo = disk_init(IF_MV(drive));
|
|
|
|
|
|
|
|
|
|
if (pinfo == NULL)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2006-08-31 19:19:35 +00:00
|
|
|
|
#ifndef ELIO_TPJ1022
|
|
|
|
|
/* The Elio's hard drive has no partition table and probing for partitions causes
|
|
|
|
|
Rockbox to crash - so we temporarily disable the probing until we fix the
|
|
|
|
|
real problem. */
|
2005-01-28 21:32:16 +00:00
|
|
|
|
for (i=0; volume != -1 && i<4; i++)
|
2004-12-29 22:10:24 +00:00
|
|
|
|
{
|
2007-05-23 17:51:18 +00:00
|
|
|
|
#ifdef MAX_LOG_SECTOR_SIZE
|
2006-12-04 21:37:22 +00:00
|
|
|
|
int j;
|
|
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
|
for (j = 1; j <= (MAX_LOG_SECTOR_SIZE/SECTOR_SIZE); j <<= 1)
|
2004-12-29 22:10:24 +00:00
|
|
|
|
{
|
2006-12-04 21:37:22 +00:00
|
|
|
|
if (!fat_mount(IF_MV2(volume,) IF_MV2(drive,) pinfo[i].start * j))
|
|
|
|
|
{
|
|
|
|
|
pinfo[i].start *= j;
|
|
|
|
|
pinfo[i].size *= j;
|
|
|
|
|
mounted++;
|
|
|
|
|
vol_drive[volume] = drive; /* remember the drive for this volume */
|
|
|
|
|
volume = get_free_volume(); /* prepare next entry */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2004-12-29 22:10:24 +00:00
|
|
|
|
}
|
2006-12-04 21:37:22 +00:00
|
|
|
|
#else
|
|
|
|
|
if (!fat_mount(IF_MV2(volume,) IF_MV2(drive,) pinfo[i].start))
|
2006-12-03 18:12:19 +00:00
|
|
|
|
{
|
|
|
|
|
mounted++;
|
|
|
|
|
vol_drive[volume] = drive; /* remember the drive for this volume */
|
|
|
|
|
volume = get_free_volume(); /* prepare next entry */
|
|
|
|
|
}
|
2006-12-04 21:37:22 +00:00
|
|
|
|
#endif
|
2005-01-28 21:32:16 +00:00
|
|
|
|
}
|
2006-08-31 19:19:35 +00:00
|
|
|
|
#endif
|
2005-01-28 21:32:16 +00:00
|
|
|
|
|
|
|
|
|
if (mounted == 0 && volume != -1) /* none of the 4 entries worked? */
|
|
|
|
|
{ /* try "superfloppy" mode */
|
|
|
|
|
DEBUGF("No partition found, trying to mount sector 0.\n");
|
|
|
|
|
if (!fat_mount(IF_MV2(volume,) IF_MV2(drive,) 0))
|
2004-12-29 22:10:24 +00:00
|
|
|
|
{
|
2005-01-28 21:32:16 +00:00
|
|
|
|
mounted = 1;
|
|
|
|
|
vol_drive[volume] = drive; /* remember the drive for this volume */
|
2004-12-29 22:10:24 +00:00
|
|
|
|
}
|
2005-01-28 21:32:16 +00:00
|
|
|
|
}
|
|
|
|
|
return mounted;
|
|
|
|
|
}
|
2004-12-29 22:10:24 +00:00
|
|
|
|
|
2005-01-28 21:32:16 +00:00
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
|
int disk_unmount(int drive)
|
|
|
|
|
{
|
|
|
|
|
int unmounted = 0;
|
|
|
|
|
int i;
|
|
|
|
|
for (i=0; i<NUM_VOLUMES; i++)
|
|
|
|
|
{
|
|
|
|
|
if (vol_drive[i] == drive)
|
|
|
|
|
{ /* force releasing resources */
|
|
|
|
|
vol_drive[i] = -1; /* mark unused */
|
|
|
|
|
unmounted++;
|
|
|
|
|
release_files(i);
|
|
|
|
|
release_dirs(i);
|
|
|
|
|
fat_unmount(i, false);
|
2004-12-29 22:10:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-28 21:32:16 +00:00
|
|
|
|
return unmounted;
|
2004-12-29 22:10:24 +00:00
|
|
|
|
}
|
2005-01-28 21:32:16 +00:00
|
|
|
|
#endif /* #ifdef HAVE_HOTSWAP */
|