2002-05-03 11:59:53 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-05-05 10:32:46 +00:00
|
|
|
* Copyright (C) 2002 by Björn Stenberg
|
2002-05-03 11:59:53 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-05-03 11:59:53 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "storage.h"
|
2002-05-03 11:59:53 +00:00
|
|
|
#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"
|
2008-04-24 20:08:28 +00:00
|
|
|
#include <string.h>
|
2002-05-03 11:59:53 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
#define BYTES2INT32(array,pos) \
|
|
|
|
((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
|
|
|
|
2008-05-15 22:23:14 +00:00
|
|
|
static const unsigned char fat_partition_types[] = {
|
|
|
|
0x0b, 0x1b, /* FAT32 + hidden variant */
|
|
|
|
0x0c, 0x1c, /* FAT32 (LBA) + hidden variant */
|
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
|
|
|
0x04, 0x14, /* FAT16 <= 32MB + hidden variant */
|
|
|
|
0x06, 0x16, /* FAT16 > 32MB + hidden variant */
|
|
|
|
0x0e, 0x1e, /* FAT16 (LBA) + hidden variant */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
static struct partinfo part[NUM_DRIVES*4]; /* 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
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
#ifdef MAX_LOG_SECTOR_SIZE
|
|
|
|
int disk_sector_multiplier = 1;
|
|
|
|
#endif
|
2008-05-15 22:23:14 +00:00
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
struct partinfo* disk_init(IF_MD_NONVOID(int drive))
|
2002-05-03 11:59:53 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char sector[512];
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2004-12-28 22:16:07 +00:00
|
|
|
/* 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;
|
2008-11-09 06:17:07 +00:00
|
|
|
const int drive = 0;
|
2008-11-02 01:14:46 +00:00
|
|
|
(void)drive;
|
2004-12-28 22:16:07 +00:00
|
|
|
#endif
|
2002-05-03 11:59:53 +00:00
|
|
|
|
2008-11-02 01:14:46 +00:00
|
|
|
storage_read_sectors(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)
|
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
int mounted=0;
|
2005-01-28 21:32:16 +00:00
|
|
|
int i;
|
2005-05-16 15:21:09 +00:00
|
|
|
|
2008-03-12 11:08:41 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-06-30 02:08:27 +00:00
|
|
|
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 */
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifndef HAVE_MULTIDRIVE
|
2005-01-28 21:32:16 +00:00
|
|
|
mounted = disk_mount(0);
|
2009-07-17 22:28:49 +00:00
|
|
|
#else
|
|
|
|
for(i=0;i<NUM_DRIVES;i++)
|
2004-12-29 22:10:24 +00:00
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
if (storage_present(i))
|
|
|
|
#endif
|
|
|
|
mounted += disk_mount(i);
|
2004-12-29 22:10:24 +00:00
|
|
|
}
|
2009-07-17 22:28:49 +00:00
|
|
|
#endif
|
2008-03-12 10:03:52 +00:00
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-06-30 02:08:27 +00:00
|
|
|
card_enable_monitoring(true);
|
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 mounted = 0; /* reset partition-on-drive flag */
|
|
|
|
int volume = get_free_volume();
|
2009-07-17 22:28:49 +00:00
|
|
|
struct partinfo* pinfo = disk_init(IF_MD(drive));
|
2005-01-28 21:32:16 +00:00
|
|
|
|
|
|
|
if (pinfo == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-07 17:24:14 +00:00
|
|
|
#if defined(TOSHIBA_GIGABEAT_S)
|
2008-01-09 07:24:43 +00:00
|
|
|
int i = 1; /* For the Gigabeat S, we mount the second partition */
|
2007-11-27 15:40:29 +00:00
|
|
|
#else
|
2008-01-09 07:24:43 +00:00
|
|
|
int i = 0;
|
2007-11-27 15:40:29 +00:00
|
|
|
#endif
|
2009-07-17 22:28:49 +00:00
|
|
|
for (; volume != -1 && i<4 && mounted<NUM_VOLUMES_PER_DRIVE; i++)
|
2004-12-29 22:10:24 +00:00
|
|
|
{
|
2008-05-15 22:23:14 +00:00
|
|
|
if (memchr(fat_partition_types, pinfo[i].type,
|
|
|
|
sizeof(fat_partition_types)) == NULL)
|
|
|
|
continue; /* not an accepted partition type */
|
|
|
|
|
2007-05-23 17:51:18 +00:00
|
|
|
#ifdef MAX_LOG_SECTOR_SIZE
|
2006-12-04 21:37:22 +00:00
|
|
|
int j;
|
2008-05-15 22:23:14 +00:00
|
|
|
|
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
|
|
|
{
|
2009-07-17 22:28:49 +00:00
|
|
|
if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) pinfo[i].start * j))
|
2006-12-04 21:37:22 +00:00
|
|
|
{
|
|
|
|
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 */
|
2008-02-11 14:26:25 +00:00
|
|
|
if (drive == 0)
|
|
|
|
disk_sector_multiplier = j;
|
2006-12-04 21:37:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-12-29 22:10:24 +00:00
|
|
|
}
|
2006-12-04 21:37:22 +00:00
|
|
|
#else
|
2009-07-17 22:28:49 +00:00
|
|
|
if (!fat_mount(IF_MV2(volume,) IF_MD2(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
|
|
|
}
|
|
|
|
|
|
|
|
if (mounted == 0 && volume != -1) /* none of the 4 entries worked? */
|
|
|
|
{ /* try "superfloppy" mode */
|
|
|
|
DEBUGF("No partition found, trying to mount sector 0.\n");
|
2009-07-17 22:28:49 +00:00
|
|
|
if (!fat_mount(IF_MV2(volume,) IF_MD2(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 */
|