2002-03-28 15:09:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
|
|
|
*
|
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-03-28 15:09:10 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2002-05-29 11:56:42 +00:00
|
|
|
#include <stdbool.h>
|
2002-03-28 15:09:10 +00:00
|
|
|
#include "fat.h"
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "storage.h"
|
2002-05-02 14:05:51 +00:00
|
|
|
#include "debug.h"
|
2002-10-20 22:50:58 +00:00
|
|
|
#include "panic.h"
|
2002-05-09 22:57:54 +00:00
|
|
|
#include "system.h"
|
2002-11-22 23:51:46 +00:00
|
|
|
#include "timefuncs.h"
|
2005-01-04 23:01:25 +00:00
|
|
|
#include "kernel.h"
|
2005-12-06 13:27:15 +00:00
|
|
|
#include "rbunicode.h"
|
2010-03-22 10:35:25 +00:00
|
|
|
/*#define LOGF_ENABLE*/
|
2006-07-31 22:59:45 +00:00
|
|
|
#include "logf.h"
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-26 16:44:58 +00:00
|
|
|
#define BYTES2INT16(array,pos) \
|
|
|
|
(array[pos] | (array[pos+1] << 8 ))
|
2006-07-31 22:59:45 +00:00
|
|
|
#define BYTES2INT32(array,pos) \
|
|
|
|
((long)array[pos] | ((long)array[pos+1] << 8 ) | \
|
|
|
|
((long)array[pos+2] << 16 ) | ((long)array[pos+3] << 24 ))
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
#define FATTYPE_FAT12 0
|
|
|
|
#define FATTYPE_FAT16 1
|
|
|
|
#define FATTYPE_FAT32 2
|
|
|
|
|
|
|
|
/* BPB offsets; generic */
|
|
|
|
#define BS_JMPBOOT 0
|
|
|
|
#define BS_OEMNAME 3
|
|
|
|
#define BPB_BYTSPERSEC 11
|
|
|
|
#define BPB_SECPERCLUS 13
|
|
|
|
#define BPB_RSVDSECCNT 14
|
|
|
|
#define BPB_NUMFATS 16
|
|
|
|
#define BPB_ROOTENTCNT 17
|
|
|
|
#define BPB_TOTSEC16 19
|
|
|
|
#define BPB_MEDIA 21
|
|
|
|
#define BPB_FATSZ16 22
|
|
|
|
#define BPB_SECPERTRK 24
|
|
|
|
#define BPB_NUMHEADS 26
|
|
|
|
#define BPB_HIDDSEC 28
|
|
|
|
#define BPB_TOTSEC32 32
|
|
|
|
|
|
|
|
/* fat12/16 */
|
|
|
|
#define BS_DRVNUM 36
|
|
|
|
#define BS_RESERVED1 37
|
|
|
|
#define BS_BOOTSIG 38
|
|
|
|
#define BS_VOLID 39
|
|
|
|
#define BS_VOLLAB 43
|
|
|
|
#define BS_FILSYSTYPE 54
|
|
|
|
|
|
|
|
/* fat32 */
|
|
|
|
#define BPB_FATSZ32 36
|
|
|
|
#define BPB_EXTFLAGS 40
|
|
|
|
#define BPB_FSVER 42
|
|
|
|
#define BPB_ROOTCLUS 44
|
|
|
|
#define BPB_FSINFO 48
|
|
|
|
#define BPB_BKBOOTSEC 50
|
|
|
|
#define BS_32_DRVNUM 64
|
|
|
|
#define BS_32_BOOTSIG 66
|
|
|
|
#define BS_32_VOLID 67
|
|
|
|
#define BS_32_VOLLAB 71
|
|
|
|
#define BS_32_FILSYSTYPE 82
|
|
|
|
|
|
|
|
#define BPB_LAST_WORD 510
|
|
|
|
|
|
|
|
|
|
|
|
/* attributes */
|
|
|
|
#define FAT_ATTR_LONG_NAME (FAT_ATTR_READ_ONLY | FAT_ATTR_HIDDEN | \
|
|
|
|
FAT_ATTR_SYSTEM | FAT_ATTR_VOLUME_ID)
|
|
|
|
#define FAT_ATTR_LONG_NAME_MASK (FAT_ATTR_READ_ONLY | FAT_ATTR_HIDDEN | \
|
|
|
|
FAT_ATTR_SYSTEM | FAT_ATTR_VOLUME_ID | \
|
|
|
|
FAT_ATTR_DIRECTORY | FAT_ATTR_ARCHIVE )
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
/* NTRES flags */
|
|
|
|
#define FAT_NTRES_LC_NAME 0x08
|
|
|
|
#define FAT_NTRES_LC_EXT 0x10
|
2002-04-27 01:25:22 +00:00
|
|
|
|
|
|
|
#define FATDIR_NAME 0
|
|
|
|
#define FATDIR_ATTR 11
|
|
|
|
#define FATDIR_NTRES 12
|
|
|
|
#define FATDIR_CRTTIMETENTH 13
|
|
|
|
#define FATDIR_CRTTIME 14
|
|
|
|
#define FATDIR_CRTDATE 16
|
|
|
|
#define FATDIR_LSTACCDATE 18
|
|
|
|
#define FATDIR_FSTCLUSHI 20
|
|
|
|
#define FATDIR_WRTTIME 22
|
|
|
|
#define FATDIR_WRTDATE 24
|
|
|
|
#define FATDIR_FSTCLUSLO 26
|
|
|
|
#define FATDIR_FILESIZE 28
|
|
|
|
|
2002-11-15 16:41:02 +00:00
|
|
|
#define FATLONG_ORDER 0
|
|
|
|
#define FATLONG_TYPE 12
|
|
|
|
#define FATLONG_CHKSUM 13
|
2010-03-22 10:35:25 +00:00
|
|
|
#define FATLONG_LAST_LONG_ENTRY 0x40
|
|
|
|
#define FATLONG_NAME_BYTES_PER_ENTRY 26
|
|
|
|
/* at most 20 LFN entries, keep coherent with fat_dir->longname size ! */
|
|
|
|
#define FATLONG_MAX_ORDER 20
|
|
|
|
|
|
|
|
#define FATLONG_NAME_CHUNKS 3
|
|
|
|
static unsigned char FATLONG_NAME_POS[FATLONG_NAME_CHUNKS] = {1, 14, 28};
|
|
|
|
static unsigned char FATLONG_NAME_SIZE[FATLONG_NAME_CHUNKS] = {10, 12, 4};
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2006-12-04 21:37:22 +00:00
|
|
|
#define CLUSTERS_PER_FAT_SECTOR (SECTOR_SIZE / 4)
|
|
|
|
#define CLUSTERS_PER_FAT16_SECTOR (SECTOR_SIZE / 2)
|
|
|
|
#define DIR_ENTRIES_PER_SECTOR (SECTOR_SIZE / DIR_ENTRY_SIZE)
|
2002-10-20 22:50:58 +00:00
|
|
|
#define DIR_ENTRY_SIZE 32
|
2002-11-15 16:41:02 +00:00
|
|
|
#define NAME_BYTES_PER_ENTRY 13
|
2002-10-20 22:50:58 +00:00
|
|
|
#define FAT_BAD_MARK 0x0ffffff7
|
|
|
|
#define FAT_EOF_MARK 0x0ffffff8
|
2006-07-31 22:59:45 +00:00
|
|
|
#define FAT_LONGNAME_PAD_BYTE 0xff
|
|
|
|
#define FAT_LONGNAME_PAD_UCS 0xffff
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
struct fsinfo {
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long freecount; /* last known free cluster count */
|
|
|
|
unsigned long nextfree; /* first cluster to start looking for free
|
2002-11-15 11:20:33 +00:00
|
|
|
clusters, or 0xffffffff for no hint */
|
2002-03-28 15:09:10 +00:00
|
|
|
};
|
2002-04-27 01:25:22 +00:00
|
|
|
/* fsinfo offsets */
|
|
|
|
#define FSINFO_FREECOUNT 488
|
|
|
|
#define FSINFO_NEXTFREE 492
|
|
|
|
|
2006-12-04 21:37:22 +00:00
|
|
|
/* Note: This struct doesn't hold the raw values after mounting if
|
|
|
|
* bpb_bytspersec isn't 512. All sector counts are normalized to 512 byte
|
|
|
|
* physical sectors. */
|
2002-05-30 19:47:56 +00:00
|
|
|
struct bpb
|
|
|
|
{
|
2004-10-14 07:52:08 +00:00
|
|
|
int bpb_bytspersec; /* Bytes per sector, typically 512 */
|
2002-11-15 11:20:33 +00:00
|
|
|
unsigned int bpb_secperclus; /* Sectors per cluster */
|
2002-05-30 19:47:56 +00:00
|
|
|
int bpb_rsvdseccnt; /* Number of reserved sectors */
|
|
|
|
int bpb_numfats; /* Number of FAT structures, typically 2 */
|
|
|
|
int bpb_totsec16; /* Number of sectors on the volume (old 16-bit) */
|
|
|
|
int bpb_media; /* Media type (typically 0xf0 or 0xf8) */
|
|
|
|
int bpb_fatsz16; /* Number of used sectors per FAT structure */
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long bpb_totsec32; /* Number of sectors on the volume
|
2002-05-30 19:47:56 +00:00
|
|
|
(new 32-bit) */
|
2005-01-24 14:26:24 +00:00
|
|
|
unsigned int last_word; /* 0xAA55 */
|
2002-05-30 19:47:56 +00:00
|
|
|
|
|
|
|
/**** FAT32 specific *****/
|
2005-01-23 23:20:40 +00:00
|
|
|
long bpb_fatsz32;
|
|
|
|
long bpb_rootclus;
|
|
|
|
long bpb_fsinfo;
|
2002-05-30 19:47:56 +00:00
|
|
|
|
|
|
|
/* variables for internal use */
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long fatsize;
|
|
|
|
unsigned long totalsectors;
|
|
|
|
unsigned long rootdirsector;
|
|
|
|
unsigned long firstdatasector;
|
|
|
|
unsigned long startsector;
|
|
|
|
unsigned long dataclusters;
|
2002-10-20 22:50:58 +00:00
|
|
|
struct fsinfo fsinfo;
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-10-14 07:52:08 +00:00
|
|
|
int bpb_rootentcnt; /* Number of dir entries in the root */
|
2004-09-23 21:43:43 +00:00
|
|
|
/* internals for FAT16 support */
|
|
|
|
bool is_fat16; /* true if we mounted a FAT16 partition, false if FAT32 */
|
2005-01-03 07:59:49 +00:00
|
|
|
unsigned int rootdiroffset; /* sector offset of root dir relative to start
|
|
|
|
* of first pseudo cluster */
|
2004-09-23 21:43:43 +00:00
|
|
|
#endif /* #ifdef HAVE_FAT16SUPPORT */
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2004-12-28 22:16:07 +00:00
|
|
|
int drive; /* on which physical device is this located */
|
2009-07-17 22:28:49 +00:00
|
|
|
#endif
|
2004-12-28 22:16:07 +00:00
|
|
|
bool mounted; /* flag if this volume is mounted */
|
|
|
|
#endif
|
2002-05-30 19:47:56 +00:00
|
|
|
};
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
static struct bpb fat_bpbs[NUM_VOLUMES]; /* mounted partition info */
|
2008-03-12 10:03:52 +00:00
|
|
|
static bool initialized = false;
|
2002-05-30 19:47:56 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb));
|
2005-01-05 00:42:24 +00:00
|
|
|
static int flush_fat(IF_MV_NONVOID(struct bpb* fat_bpb));
|
2004-12-28 22:16:07 +00:00
|
|
|
static int bpb_is_sane(IF_MV_NONVOID(struct bpb* fat_bpb));
|
2007-10-28 23:44:49 +00:00
|
|
|
static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,)
|
|
|
|
long secnum, bool dirty);
|
2005-04-23 13:15:25 +00:00
|
|
|
static void create_dos_name(const unsigned char *name, unsigned char *newname);
|
2005-02-16 20:45:56 +00:00
|
|
|
static void randomize_dos_name(unsigned char *name);
|
2007-10-28 23:44:49 +00:00
|
|
|
static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,)
|
|
|
|
unsigned long start);
|
|
|
|
static int transfer(IF_MV2(struct bpb* fat_bpb,) unsigned long start,
|
|
|
|
long count, char* buf, bool write );
|
2002-04-27 01:25:22 +00:00
|
|
|
|
2002-05-09 21:27:49 +00:00
|
|
|
#define FAT_CACHE_SIZE 0x20
|
|
|
|
#define FAT_CACHE_MASK (FAT_CACHE_SIZE-1)
|
|
|
|
|
|
|
|
struct fat_cache_entry
|
|
|
|
{
|
2005-02-25 09:11:29 +00:00
|
|
|
long secnum;
|
2002-05-29 11:56:42 +00:00
|
|
|
bool inuse;
|
|
|
|
bool dirty;
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_vol ; /* shared cache for all volumes */
|
|
|
|
#endif
|
2002-05-09 21:27:49 +00:00
|
|
|
};
|
|
|
|
|
2006-12-04 21:37:22 +00:00
|
|
|
static char fat_cache_sectors[FAT_CACHE_SIZE][SECTOR_SIZE];
|
2002-05-29 11:56:42 +00:00
|
|
|
static struct fat_cache_entry fat_cache[FAT_CACHE_SIZE];
|
2008-04-06 04:34:57 +00:00
|
|
|
static struct mutex cache_mutex SHAREDBSS_ATTR;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2008-10-31 21:25:04 +00:00
|
|
|
#if defined(HAVE_HOTSWAP) && !(CONFIG_STORAGE & STORAGE_MMC) /* A better condition ?? */
|
2008-03-12 10:03:52 +00:00
|
|
|
void fat_lock(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&cache_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fat_unlock(void)
|
|
|
|
{
|
|
|
|
mutex_unlock(&cache_mutex);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
static long cluster2sec(IF_MV2(struct bpb* fat_bpb,) long cluster)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
2005-01-03 07:59:49 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
|
|
|
/* negative clusters (FAT16 root dir) don't get the 2 offset */
|
|
|
|
int zerocluster = cluster < 0 ? 0 : 2;
|
|
|
|
#else
|
2005-01-23 23:20:40 +00:00
|
|
|
const long zerocluster = 2;
|
2004-12-28 22:16:07 +00:00
|
|
|
#endif
|
2005-02-03 22:58:50 +00:00
|
|
|
|
|
|
|
if (cluster > (long)(fat_bpb->dataclusters + 1))
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2005-01-23 23:20:40 +00:00
|
|
|
DEBUGF( "cluster2sec() - Bad cluster number (%ld)\n", cluster);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-03 07:59:49 +00:00
|
|
|
return (cluster - zerocluster) * fat_bpb->bpb_secperclus
|
|
|
|
+ fat_bpb->firstdatasector;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
void fat_size(IF_MV2(int volume,) unsigned long* size, unsigned long* free)
|
2002-11-12 11:32:26 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
const int volume = 0;
|
|
|
|
#endif
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[volume];
|
2002-11-12 11:32:26 +00:00
|
|
|
if (size)
|
2009-11-09 22:53:54 +00:00
|
|
|
*size = fat_bpb->dataclusters * (fat_bpb->bpb_secperclus * SECTOR_SIZE / 1024);
|
2002-11-12 11:32:26 +00:00
|
|
|
if (free)
|
2009-11-09 22:53:54 +00:00
|
|
|
*free = fat_bpb->fsinfo.freecount * (fat_bpb->bpb_secperclus * SECTOR_SIZE / 1024);
|
2002-11-12 11:32:26 +00:00
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
void fat_init(void)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-11-15 11:20:33 +00:00
|
|
|
unsigned int i;
|
2005-01-04 23:01:25 +00:00
|
|
|
|
2008-03-12 10:03:52 +00:00
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
initialized = true;
|
|
|
|
mutex_init(&cache_mutex);
|
|
|
|
}
|
2005-01-04 23:01:25 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
/* Disable this because it is dangerous due to the assumption that
|
|
|
|
* mutex_unlock won't yield */
|
|
|
|
mutex_set_preempt(&cache_mutex, false);
|
|
|
|
#endif
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
/* mark the FAT cache as unused */
|
2002-05-09 21:27:49 +00:00
|
|
|
for(i = 0;i < FAT_CACHE_SIZE;i++)
|
|
|
|
{
|
|
|
|
fat_cache[i].secnum = 8; /* We use a "safe" sector just in case */
|
2002-05-29 11:56:42 +00:00
|
|
|
fat_cache[i].inuse = false;
|
|
|
|
fat_cache[i].dirty = false;
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
fat_cache[i].fat_vol = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
/* mark the possible volumes as not mounted */
|
|
|
|
for (i=0; i<NUM_VOLUMES;i++)
|
|
|
|
{
|
|
|
|
fat_bpbs[i].mounted = false;
|
2002-05-09 21:27:49 +00:00
|
|
|
}
|
2004-12-28 22:16:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
int fat_mount(IF_MV2(int volume,) IF_MD2(int drive,) long startsector)
|
2004-12-28 22:16:07 +00:00
|
|
|
{
|
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
const int volume = 0;
|
|
|
|
#endif
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[volume];
|
2006-12-04 21:37:22 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE];
|
2004-12-28 22:16:07 +00:00
|
|
|
int rc;
|
2006-12-04 21:37:22 +00:00
|
|
|
int secmult;
|
2005-01-23 23:20:40 +00:00
|
|
|
long datasec;
|
2005-01-03 07:59:49 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
|
|
|
int rootdirsectors;
|
|
|
|
#endif
|
2002-03-28 15:09:10 +00:00
|
|
|
|
|
|
|
/* Read the sector */
|
2010-04-03 22:02:09 +00:00
|
|
|
rc = storage_read_sectors(IF_MD2(drive,) startsector,1,buf);
|
2003-02-26 03:02:49 +00:00
|
|
|
if(rc)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2003-02-26 03:02:49 +00:00
|
|
|
DEBUGF( "fat_mount() - Couldn't read BPB (error code %d)\n", rc);
|
|
|
|
return rc * 10 - 1;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
memset(fat_bpb, 0, sizeof(struct bpb));
|
2005-01-03 07:59:49 +00:00
|
|
|
fat_bpb->startsector = startsector;
|
2009-07-17 22:28:49 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->drive = drive;
|
|
|
|
#endif
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->bpb_bytspersec = BYTES2INT16(buf,BPB_BYTSPERSEC);
|
2006-12-04 21:37:22 +00:00
|
|
|
secmult = fat_bpb->bpb_bytspersec / SECTOR_SIZE;
|
|
|
|
/* Sanity check is performed later */
|
|
|
|
|
|
|
|
fat_bpb->bpb_secperclus = secmult * buf[BPB_SECPERCLUS];
|
|
|
|
fat_bpb->bpb_rsvdseccnt = secmult * BYTES2INT16(buf,BPB_RSVDSECCNT);
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->bpb_numfats = buf[BPB_NUMFATS];
|
|
|
|
fat_bpb->bpb_media = buf[BPB_MEDIA];
|
2006-12-04 21:37:22 +00:00
|
|
|
fat_bpb->bpb_fatsz16 = secmult * BYTES2INT16(buf,BPB_FATSZ16);
|
|
|
|
fat_bpb->bpb_fatsz32 = secmult * BYTES2INT32(buf,BPB_FATSZ32);
|
|
|
|
fat_bpb->bpb_totsec16 = secmult * BYTES2INT16(buf,BPB_TOTSEC16);
|
|
|
|
fat_bpb->bpb_totsec32 = secmult * BYTES2INT32(buf,BPB_TOTSEC32);
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->last_word = BYTES2INT16(buf,BPB_LAST_WORD);
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
/* calculate a few commonly used values */
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->bpb_fatsz16 != 0)
|
|
|
|
fat_bpb->fatsize = fat_bpb->bpb_fatsz16;
|
2002-04-27 01:25:22 +00:00
|
|
|
else
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fatsize = fat_bpb->bpb_fatsz32;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->bpb_totsec16 != 0)
|
|
|
|
fat_bpb->totalsectors = fat_bpb->bpb_totsec16;
|
2002-03-28 15:09:10 +00:00
|
|
|
else
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->totalsectors = fat_bpb->bpb_totsec32;
|
2004-09-23 21:43:43 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->bpb_rootentcnt = BYTES2INT16(buf,BPB_ROOTENTCNT);
|
2007-07-09 22:58:08 +00:00
|
|
|
if (!fat_bpb->bpb_bytspersec)
|
|
|
|
return -2;
|
2006-12-04 21:37:22 +00:00
|
|
|
rootdirsectors = secmult * ((fat_bpb->bpb_rootentcnt * DIR_ENTRY_SIZE
|
|
|
|
+ fat_bpb->bpb_bytspersec - 1) / fat_bpb->bpb_bytspersec);
|
2004-09-23 21:43:43 +00:00
|
|
|
#endif /* #ifdef HAVE_FAT16SUPPORT */
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->firstdatasector = fat_bpb->bpb_rsvdseccnt
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2005-01-03 07:59:49 +00:00
|
|
|
+ rootdirsectors
|
2004-09-23 21:43:43 +00:00
|
|
|
#endif
|
2004-12-28 22:16:07 +00:00
|
|
|
+ fat_bpb->bpb_numfats * fat_bpb->fatsize;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
/* Determine FAT type */
|
2004-12-28 22:16:07 +00:00
|
|
|
datasec = fat_bpb->totalsectors - fat_bpb->firstdatasector;
|
2007-07-09 21:23:26 +00:00
|
|
|
if (fat_bpb->bpb_secperclus)
|
|
|
|
fat_bpb->dataclusters = datasec / fat_bpb->bpb_secperclus;
|
|
|
|
else
|
|
|
|
return -2;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
#ifdef TEST_FAT
|
2002-04-26 16:44:58 +00:00
|
|
|
/*
|
2002-04-27 01:25:22 +00:00
|
|
|
we are sometimes testing with "illegally small" fat32 images,
|
|
|
|
so we don't use the proper fat32 test case for test code
|
2002-04-26 16:44:58 +00:00
|
|
|
*/
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( fat_bpb->bpb_fatsz16 )
|
2002-04-27 01:25:22 +00:00
|
|
|
#else
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( fat_bpb->dataclusters < 65525 )
|
2002-04-27 01:25:22 +00:00
|
|
|
#endif
|
2004-09-23 21:43:43 +00:00
|
|
|
{ /* FAT16 */
|
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->is_fat16 = true;
|
|
|
|
if (fat_bpb->dataclusters < 4085)
|
2004-09-23 21:43:43 +00:00
|
|
|
{ /* FAT12 */
|
|
|
|
DEBUGF("This is FAT12. Go away!\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
#else /* #ifdef HAVE_FAT16SUPPORT */
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("This is not FAT32. Go away!\n");
|
2002-10-20 22:50:58 +00:00
|
|
|
return -2;
|
2004-09-23 21:43:43 +00:00
|
|
|
#endif /* #ifndef HAVE_FAT16SUPPORT */
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->is_fat16)
|
2004-09-23 21:43:43 +00:00
|
|
|
{ /* FAT16 specific part of BPB */
|
|
|
|
int dirclusters;
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->rootdirsector = fat_bpb->bpb_rsvdseccnt
|
|
|
|
+ fat_bpb->bpb_numfats * fat_bpb->bpb_fatsz16;
|
2005-01-03 07:59:49 +00:00
|
|
|
dirclusters = ((rootdirsectors + fat_bpb->bpb_secperclus - 1)
|
|
|
|
/ fat_bpb->bpb_secperclus); /* rounded up, to full clusters */
|
2004-09-23 21:43:43 +00:00
|
|
|
/* I assign negative pseudo cluster numbers for the root directory,
|
|
|
|
their range is counted upward until -1. */
|
2007-10-28 23:44:49 +00:00
|
|
|
fat_bpb->bpb_rootclus = 0 - dirclusters; /* backwards, before the data*/
|
2005-01-03 07:59:49 +00:00
|
|
|
fat_bpb->rootdiroffset = dirclusters * fat_bpb->bpb_secperclus
|
|
|
|
- rootdirsectors;
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* #ifdef HAVE_FAT16SUPPORT */
|
|
|
|
{ /* FAT32 specific part of BPB */
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->bpb_rootclus = BYTES2INT32(buf,BPB_ROOTCLUS);
|
2006-12-04 21:37:22 +00:00
|
|
|
fat_bpb->bpb_fsinfo = secmult * BYTES2INT16(buf,BPB_FSINFO);
|
2007-10-28 23:44:49 +00:00
|
|
|
fat_bpb->rootdirsector = cluster2sec(IF_MV2(fat_bpb,)
|
|
|
|
fat_bpb->bpb_rootclus);
|
2002-04-27 01:25:22 +00:00
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = bpb_is_sane(IF_MV(fat_bpb));
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
2002-04-27 01:25:22 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "fat_mount() - BPB is not sane\n");
|
2003-02-26 03:02:49 +00:00
|
|
|
return rc * 10 - 3;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->is_fat16)
|
2002-10-20 22:50:58 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fsinfo.freecount = 0xffffffff; /* force recalc below */
|
|
|
|
fat_bpb->fsinfo.nextfree = 0xffffffff;
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* #ifdef HAVE_FAT16SUPPORT */
|
|
|
|
{
|
|
|
|
/* Read the fsinfo sector */
|
2010-04-03 22:02:09 +00:00
|
|
|
rc = storage_read_sectors(IF_MD2(drive,)
|
2006-12-04 21:37:22 +00:00
|
|
|
startsector + fat_bpb->bpb_fsinfo, 1, buf);
|
2004-09-23 21:43:43 +00:00
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
DEBUGF( "fat_mount() - Couldn't read FSInfo (error code %d)\n", rc);
|
|
|
|
return rc * 10 - 4;
|
|
|
|
}
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fsinfo.freecount = BYTES2INT32(buf, FSINFO_FREECOUNT);
|
|
|
|
fat_bpb->fsinfo.nextfree = BYTES2INT32(buf, FSINFO_NEXTFREE);
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2002-11-15 11:20:33 +00:00
|
|
|
|
|
|
|
/* calculate freecount if unset */
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( fat_bpb->fsinfo.freecount == 0xffffffff )
|
2002-11-15 11:20:33 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_recalc_free(IF_MV(volume));
|
2002-11-15 11:20:33 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("Freecount: %ld\n",fat_bpb->fsinfo.freecount);
|
|
|
|
LDEBUGF("Nextfree: 0x%lx\n",fat_bpb->fsinfo.nextfree);
|
|
|
|
LDEBUGF("Cluster count: 0x%lx\n",fat_bpb->dataclusters);
|
2004-12-28 22:16:07 +00:00
|
|
|
LDEBUGF("Sectors per cluster: %d\n",fat_bpb->bpb_secperclus);
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("FAT sectors: 0x%lx\n",fat_bpb->fatsize);
|
2004-12-28 22:16:07 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
fat_bpb->mounted = true;
|
|
|
|
#endif
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-28 21:32:16 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2005-01-05 00:09:04 +00:00
|
|
|
int fat_unmount(int volume, bool flush)
|
|
|
|
{
|
2005-01-05 00:42:24 +00:00
|
|
|
int rc;
|
2009-11-26 16:57:30 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2005-01-05 00:09:04 +00:00
|
|
|
struct bpb* fat_bpb = &fat_bpbs[volume];
|
2009-11-26 16:57:30 +00:00
|
|
|
#else
|
|
|
|
(void)volume;
|
|
|
|
#endif
|
2005-01-05 00:42:24 +00:00
|
|
|
|
2005-01-05 00:09:04 +00:00
|
|
|
if(flush)
|
|
|
|
{
|
2009-11-26 16:57:30 +00:00
|
|
|
rc = flush_fat(IF_MV(fat_bpb)); /* the clean way, while still alive */
|
2005-01-05 00:09:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* volume is not accessible any more, e.g. MMC removed */
|
|
|
|
int i;
|
|
|
|
mutex_lock(&cache_mutex);
|
|
|
|
for(i = 0;i < FAT_CACHE_SIZE;i++)
|
|
|
|
{
|
|
|
|
struct fat_cache_entry *fce = &fat_cache[i];
|
2009-11-26 16:57:30 +00:00
|
|
|
if(fce->inuse
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
&& fce->fat_vol == fat_bpb
|
|
|
|
#endif
|
|
|
|
)
|
2005-01-05 00:09:04 +00:00
|
|
|
{
|
|
|
|
fce->inuse = false; /* discard all from that volume */
|
|
|
|
fce->dirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_unlock(&cache_mutex);
|
2005-01-05 00:42:24 +00:00
|
|
|
rc = 0;
|
2005-01-05 00:09:04 +00:00
|
|
|
}
|
2009-11-26 16:57:30 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2005-01-05 00:09:04 +00:00
|
|
|
fat_bpb->mounted = false;
|
2009-11-26 16:57:30 +00:00
|
|
|
#endif
|
2005-01-05 00:42:24 +00:00
|
|
|
return rc;
|
2005-01-05 00:09:04 +00:00
|
|
|
}
|
2005-01-28 21:32:16 +00:00
|
|
|
#endif /* #ifdef HAVE_HOTSWAP */
|
2005-01-05 00:09:04 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
void fat_recalc_free(IF_MV_NONVOID(int volume))
|
2002-12-09 15:39:32 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
const int volume = 0;
|
|
|
|
#endif
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[volume];
|
2005-01-23 23:20:40 +00:00
|
|
|
long free = 0;
|
|
|
|
unsigned long i;
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->is_fat16)
|
2004-09-23 21:43:43 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
for (i = 0; i<fat_bpb->fatsize; i++) {
|
2004-09-23 21:43:43 +00:00
|
|
|
unsigned int j;
|
2005-01-04 23:01:25 +00:00
|
|
|
unsigned short* fat = cache_fat_sector(IF_MV2(fat_bpb,) i, false);
|
2004-09-23 21:43:43 +00:00
|
|
|
for (j = 0; j < CLUSTERS_PER_FAT16_SECTOR; j++) {
|
|
|
|
unsigned int c = i * CLUSTERS_PER_FAT16_SECTOR + j;
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */
|
2004-09-23 21:43:43 +00:00
|
|
|
break;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2005-10-06 19:27:43 +00:00
|
|
|
if (letoh16(fat[j]) == 0x0000) {
|
2004-09-23 21:43:43 +00:00
|
|
|
free++;
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( fat_bpb->fsinfo.nextfree == 0xffffffff )
|
|
|
|
fat_bpb->fsinfo.nextfree = c;
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* #ifdef HAVE_FAT16SUPPORT */
|
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
for (i = 0; i<fat_bpb->fatsize; i++) {
|
2004-09-23 21:43:43 +00:00
|
|
|
unsigned int j;
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long* fat = cache_fat_sector(IF_MV2(fat_bpb,) i, false);
|
2004-09-23 21:43:43 +00:00
|
|
|
for (j = 0; j < CLUSTERS_PER_FAT_SECTOR; j++) {
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long c = i * CLUSTERS_PER_FAT_SECTOR + j;
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */
|
2004-09-23 21:43:43 +00:00
|
|
|
break;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2005-10-06 19:27:43 +00:00
|
|
|
if (!(letoh32(fat[j]) & 0x0fffffff)) {
|
2004-09-23 21:43:43 +00:00
|
|
|
free++;
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( fat_bpb->fsinfo.nextfree == 0xffffffff )
|
|
|
|
fat_bpb->fsinfo.nextfree = c;
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
2002-12-09 15:39:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fsinfo.freecount = free;
|
|
|
|
update_fsinfo(IF_MV(fat_bpb));
|
2002-12-09 15:39:32 +00:00
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
static int bpb_is_sane(IF_MV_NONVOID(struct bpb* fat_bpb))
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2006-12-04 21:37:22 +00:00
|
|
|
if(fat_bpb->bpb_bytspersec % SECTOR_SIZE)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2006-12-03 18:12:19 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Error: sector size is not sane (%d)\n",
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->bpb_bytspersec);
|
2002-04-27 19:37:41 +00:00
|
|
|
return -1;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2007-10-28 23:44:49 +00:00
|
|
|
if((long)fat_bpb->bpb_secperclus * (long)fat_bpb->bpb_bytspersec
|
|
|
|
> 128L*1024L)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-10-31 19:05:25 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Error: cluster size is larger than 128K "
|
2002-05-08 08:26:38 +00:00
|
|
|
"(%d * %d = %d)\n",
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->bpb_bytspersec, fat_bpb->bpb_secperclus,
|
|
|
|
fat_bpb->bpb_bytspersec * fat_bpb->bpb_secperclus);
|
2002-10-31 19:05:25 +00:00
|
|
|
return -2;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2004-12-28 22:16:07 +00:00
|
|
|
if(fat_bpb->bpb_numfats != 2)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-08 08:26:38 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Warning: NumFATS is not 2 (%d)\n",
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->bpb_numfats);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2004-12-28 22:16:07 +00:00
|
|
|
if(fat_bpb->bpb_media != 0xf0 && fat_bpb->bpb_media < 0xf8)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Warning: Non-standard "
|
2002-03-28 15:09:10 +00:00
|
|
|
"media type (0x%02x)\n",
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->bpb_media);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2004-12-28 22:16:07 +00:00
|
|
|
if(fat_bpb->last_word != 0xaa55)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Error: Last word is not "
|
2004-12-28 22:16:07 +00:00
|
|
|
"0xaa55 (0x%04x)\n", fat_bpb->last_word);
|
2002-10-31 19:05:25 +00:00
|
|
|
return -3;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->fsinfo.freecount >
|
|
|
|
(fat_bpb->totalsectors - fat_bpb->firstdatasector)/
|
|
|
|
fat_bpb->bpb_secperclus)
|
2002-10-20 22:50:58 +00:00
|
|
|
{
|
|
|
|
DEBUGF( "bpb_is_sane() - Error: FSInfo.Freecount > disk size "
|
2005-01-23 23:20:40 +00:00
|
|
|
"(0x%04lx)\n", fat_bpb->fsinfo.freecount);
|
2002-10-31 19:05:25 +00:00
|
|
|
return -4;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
static void flush_fat_sector(struct fat_cache_entry *fce,
|
|
|
|
unsigned char *sectorbuf)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
int rc;
|
2005-02-25 09:11:29 +00:00
|
|
|
long secnum;
|
2004-12-28 22:16:07 +00:00
|
|
|
|
|
|
|
/* With multivolume, use only the FAT info from the cached sector! */
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2006-12-04 21:37:22 +00:00
|
|
|
secnum = fce->secnum + fce->fat_vol->startsector;
|
2004-12-28 22:16:07 +00:00
|
|
|
#else
|
2006-12-04 21:37:22 +00:00
|
|
|
secnum = fce->secnum + fat_bpbs[0].startsector;
|
2004-12-28 22:16:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Write to the first FAT */
|
2010-04-03 22:02:09 +00:00
|
|
|
rc = storage_write_sectors(IF_MD2(fce->fat_vol->drive,)
|
2006-12-04 21:37:22 +00:00
|
|
|
secnum, 1,
|
2004-12-28 22:16:07 +00:00
|
|
|
sectorbuf);
|
|
|
|
if(rc < 0)
|
|
|
|
{
|
2005-02-25 18:50:16 +00:00
|
|
|
panicf("flush_fat_sector() - Could not write sector %ld"
|
2004-12-28 22:16:07 +00:00
|
|
|
" (error %d)\n",
|
|
|
|
secnum, rc);
|
|
|
|
}
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
if(fce->fat_vol->bpb_numfats > 1)
|
|
|
|
#else
|
|
|
|
if(fat_bpbs[0].bpb_numfats > 1)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Write to the second FAT */
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2006-12-04 21:37:22 +00:00
|
|
|
secnum += fce->fat_vol->fatsize;
|
2004-12-28 22:16:07 +00:00
|
|
|
#else
|
2006-12-04 21:37:22 +00:00
|
|
|
secnum += fat_bpbs[0].fatsize;
|
2004-12-28 22:16:07 +00:00
|
|
|
#endif
|
2010-04-03 22:02:09 +00:00
|
|
|
rc = storage_write_sectors(IF_MD2(fce->fat_vol->drive,)
|
2006-12-04 21:37:22 +00:00
|
|
|
secnum, 1, sectorbuf);
|
2004-12-28 22:16:07 +00:00
|
|
|
if(rc < 0)
|
|
|
|
{
|
2005-02-25 18:50:16 +00:00
|
|
|
panicf("flush_fat_sector() - Could not write sector %ld"
|
2004-12-28 22:16:07 +00:00
|
|
|
" (error %d)\n",
|
|
|
|
secnum, rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fce->dirty = false;
|
|
|
|
}
|
|
|
|
|
2007-10-28 23:44:49 +00:00
|
|
|
/* Note: The returned pointer is only safely valid until the next
|
2005-01-04 23:01:25 +00:00
|
|
|
task switch! (Any subsequent ata read/write may yield.) */
|
|
|
|
static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,)
|
2005-01-23 23:20:40 +00:00
|
|
|
long fatsector, bool dirty)
|
2004-12-28 22:16:07 +00:00
|
|
|
{
|
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2005-01-23 23:20:40 +00:00
|
|
|
long secnum = fatsector + fat_bpb->bpb_rsvdseccnt;
|
2002-05-09 21:27:49 +00:00
|
|
|
int cache_index = secnum & FAT_CACHE_MASK;
|
2002-11-09 09:23:43 +00:00
|
|
|
struct fat_cache_entry *fce = &fat_cache[cache_index];
|
|
|
|
unsigned char *sectorbuf = &fat_cache_sectors[cache_index][0];
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2002-05-09 21:27:49 +00:00
|
|
|
|
2005-01-04 23:01:25 +00:00
|
|
|
mutex_lock(&cache_mutex); /* make changes atomic */
|
|
|
|
|
2002-05-09 21:27:49 +00:00
|
|
|
/* Delete the cache entry if it isn't the sector we want */
|
2004-12-28 22:16:07 +00:00
|
|
|
if(fce->inuse && (fce->secnum != secnum
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
|| fce->fat_vol != fat_bpb
|
|
|
|
#endif
|
|
|
|
))
|
2002-05-09 21:27:49 +00:00
|
|
|
{
|
|
|
|
/* Write back if it is dirty */
|
2002-11-09 09:23:43 +00:00
|
|
|
if(fce->dirty)
|
2002-05-09 21:27:49 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
flush_fat_sector(fce, sectorbuf);
|
2002-05-09 21:27:49 +00:00
|
|
|
}
|
2002-11-09 09:23:43 +00:00
|
|
|
fce->inuse = false;
|
2002-05-09 21:27:49 +00:00
|
|
|
}
|
2002-11-09 09:23:43 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
/* Load the sector if it is not cached */
|
2002-11-09 09:23:43 +00:00
|
|
|
if(!fce->inuse)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2010-04-03 22:02:09 +00:00
|
|
|
rc = storage_read_sectors(IF_MD2(fat_bpb->drive,)
|
2006-12-04 21:37:22 +00:00
|
|
|
secnum + fat_bpb->startsector,1,
|
|
|
|
sectorbuf);
|
2003-02-26 03:02:49 +00:00
|
|
|
if(rc < 0)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2005-01-23 23:20:40 +00:00
|
|
|
DEBUGF( "cache_fat_sector() - Could not read sector %ld"
|
2003-02-26 03:02:49 +00:00
|
|
|
" (error %d)\n", secnum, rc);
|
2005-01-04 23:01:25 +00:00
|
|
|
mutex_unlock(&cache_mutex);
|
2002-03-28 15:09:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-11-09 09:23:43 +00:00
|
|
|
fce->inuse = true;
|
|
|
|
fce->secnum = secnum;
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
fce->fat_vol = fat_bpb;
|
|
|
|
#endif
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2005-01-04 23:01:25 +00:00
|
|
|
if (dirty)
|
|
|
|
fce->dirty = true; /* dirt remains, sticky until flushed */
|
|
|
|
mutex_unlock(&cache_mutex);
|
2002-11-09 09:23:43 +00:00
|
|
|
return sectorbuf;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2007-10-28 23:44:49 +00:00
|
|
|
static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,)
|
|
|
|
unsigned long startcluster)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long sector;
|
|
|
|
unsigned long offset;
|
|
|
|
unsigned long i;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->is_fat16)
|
2004-09-23 21:43:43 +00:00
|
|
|
{
|
|
|
|
sector = startcluster / CLUSTERS_PER_FAT16_SECTOR;
|
|
|
|
offset = startcluster % CLUSTERS_PER_FAT16_SECTOR;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
for (i = 0; i<fat_bpb->fatsize; i++) {
|
2004-09-23 21:43:43 +00:00
|
|
|
unsigned int j;
|
2004-12-28 22:16:07 +00:00
|
|
|
unsigned int nr = (i + sector) % fat_bpb->fatsize;
|
2005-01-04 23:01:25 +00:00
|
|
|
unsigned short* fat = cache_fat_sector(IF_MV2(fat_bpb,) nr, false);
|
2004-09-23 21:43:43 +00:00
|
|
|
if ( !fat )
|
|
|
|
break;
|
|
|
|
for (j = 0; j < CLUSTERS_PER_FAT16_SECTOR; j++) {
|
|
|
|
int k = (j + offset) % CLUSTERS_PER_FAT16_SECTOR;
|
2005-10-06 19:27:43 +00:00
|
|
|
if (letoh16(fat[k]) == 0x0000) {
|
2004-09-23 21:43:43 +00:00
|
|
|
unsigned int c = nr * CLUSTERS_PER_FAT16_SECTOR + k;
|
|
|
|
/* Ignore the reserved clusters 0 & 1, and also
|
|
|
|
cluster numbers out of bounds */
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( c < 2 || c > fat_bpb->dataclusters+1 )
|
2004-09-23 21:43:43 +00:00
|
|
|
continue;
|
|
|
|
LDEBUGF("find_free_cluster(%x) == %x\n",startcluster,c);
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fsinfo.nextfree = c;
|
2004-09-23 21:43:43 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* #ifdef HAVE_FAT16SUPPORT */
|
|
|
|
{
|
|
|
|
sector = startcluster / CLUSTERS_PER_FAT_SECTOR;
|
|
|
|
offset = startcluster % CLUSTERS_PER_FAT_SECTOR;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
for (i = 0; i<fat_bpb->fatsize; i++) {
|
2004-09-23 21:43:43 +00:00
|
|
|
unsigned int j;
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long nr = (i + sector) % fat_bpb->fatsize;
|
|
|
|
unsigned long* fat = cache_fat_sector(IF_MV2(fat_bpb,) nr, false);
|
2004-09-23 21:43:43 +00:00
|
|
|
if ( !fat )
|
|
|
|
break;
|
|
|
|
for (j = 0; j < CLUSTERS_PER_FAT_SECTOR; j++) {
|
|
|
|
int k = (j + offset) % CLUSTERS_PER_FAT_SECTOR;
|
2005-10-06 19:27:43 +00:00
|
|
|
if (!(letoh32(fat[k]) & 0x0fffffff)) {
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long c = nr * CLUSTERS_PER_FAT_SECTOR + k;
|
2004-09-23 21:43:43 +00:00
|
|
|
/* Ignore the reserved clusters 0 & 1, and also
|
|
|
|
cluster numbers out of bounds */
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( c < 2 || c > fat_bpb->dataclusters+1 )
|
2004-09-23 21:43:43 +00:00
|
|
|
continue;
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("find_free_cluster(%lx) == %lx\n",startcluster,c);
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fsinfo.nextfree = c;
|
2004-09-23 21:43:43 +00:00
|
|
|
return c;
|
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2004-09-23 21:43:43 +00:00
|
|
|
offset = 0;
|
2002-10-31 19:05:25 +00:00
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2004-09-23 21:43:43 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("find_free_cluster(%lx) == 0\n",startcluster);
|
2002-10-20 22:50:58 +00:00
|
|
|
return 0; /* 0 is an illegal cluster number */
|
|
|
|
}
|
|
|
|
|
2007-10-28 23:44:49 +00:00
|
|
|
static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry,
|
|
|
|
unsigned long val)
|
2002-10-20 22:50:58 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->is_fat16)
|
2004-09-23 21:43:43 +00:00
|
|
|
{
|
|
|
|
int sector = entry / CLUSTERS_PER_FAT16_SECTOR;
|
|
|
|
int offset = entry % CLUSTERS_PER_FAT16_SECTOR;
|
|
|
|
unsigned short* sec;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
val &= 0xFFFF;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
LDEBUGF("update_fat_entry(%x,%x)\n",entry,val);
|
2002-10-22 15:06:08 +00:00
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
if (entry==val)
|
2005-02-28 10:52:28 +00:00
|
|
|
panicf("Creating FAT loop: %lx,%lx\n",entry,val);
|
2002-10-22 15:06:08 +00:00
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
if ( entry < 2 )
|
2005-02-28 10:52:28 +00:00
|
|
|
panicf("Updating reserved FAT entry %ld.\n",entry);
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2005-01-04 23:01:25 +00:00
|
|
|
sec = cache_fat_sector(IF_MV2(fat_bpb,) sector, true);
|
2004-09-23 21:43:43 +00:00
|
|
|
if (!sec)
|
|
|
|
{
|
2005-01-03 07:59:49 +00:00
|
|
|
DEBUGF( "update_fat_entry() - Could not cache sector %d\n", sector);
|
2004-09-23 21:43:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( val ) {
|
2005-10-06 21:23:59 +00:00
|
|
|
if (letoh16(sec[offset]) == 0x0000 && fat_bpb->fsinfo.freecount > 0)
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fsinfo.freecount--;
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-10-06 21:23:59 +00:00
|
|
|
if (letoh16(sec[offset]))
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fsinfo.freecount++;
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
|
|
|
|
2007-10-28 23:44:49 +00:00
|
|
|
LDEBUGF("update_fat_entry: %d free clusters\n",
|
|
|
|
fat_bpb->fsinfo.freecount);
|
2004-09-23 21:43:43 +00:00
|
|
|
|
2005-10-06 19:27:43 +00:00
|
|
|
sec[offset] = htole16(val);
|
2002-10-30 16:16:47 +00:00
|
|
|
}
|
2004-09-23 21:43:43 +00:00
|
|
|
else
|
|
|
|
#endif /* #ifdef HAVE_FAT16SUPPORT */
|
|
|
|
{
|
2005-01-23 23:20:40 +00:00
|
|
|
long sector = entry / CLUSTERS_PER_FAT_SECTOR;
|
2004-09-23 21:43:43 +00:00
|
|
|
int offset = entry % CLUSTERS_PER_FAT_SECTOR;
|
2005-02-25 18:50:16 +00:00
|
|
|
unsigned long* sec;
|
2002-04-27 01:25:22 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("update_fat_entry(%lx,%lx)\n",entry,val);
|
2002-11-01 15:26:06 +00:00
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
if (entry==val)
|
2005-02-25 18:50:16 +00:00
|
|
|
panicf("Creating FAT loop: %lx,%lx\n",entry,val);
|
2004-09-23 21:43:43 +00:00
|
|
|
|
|
|
|
if ( entry < 2 )
|
2005-02-25 18:50:16 +00:00
|
|
|
panicf("Updating reserved FAT entry %ld.\n",entry);
|
2004-09-23 21:43:43 +00:00
|
|
|
|
2005-01-04 23:01:25 +00:00
|
|
|
sec = cache_fat_sector(IF_MV2(fat_bpb,) sector, true);
|
2004-09-23 21:43:43 +00:00
|
|
|
if (!sec)
|
|
|
|
{
|
2007-10-28 23:44:49 +00:00
|
|
|
DEBUGF("update_fat_entry() - Could not cache sector %ld\n", sector);
|
2004-09-23 21:43:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( val ) {
|
2005-10-06 21:23:59 +00:00
|
|
|
if (!(letoh32(sec[offset]) & 0x0fffffff) &&
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fsinfo.freecount > 0)
|
|
|
|
fat_bpb->fsinfo.freecount--;
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-10-06 21:23:59 +00:00
|
|
|
if (letoh32(sec[offset]) & 0x0fffffff)
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_bpb->fsinfo.freecount++;
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
|
|
|
|
2007-10-28 23:44:49 +00:00
|
|
|
LDEBUGF("update_fat_entry: %ld free clusters\n",
|
|
|
|
fat_bpb->fsinfo.freecount);
|
2004-09-23 21:43:43 +00:00
|
|
|
|
|
|
|
/* don't change top 4 bits */
|
2005-10-06 19:27:43 +00:00
|
|
|
sec[offset] &= htole32(0xf0000000);
|
|
|
|
sec[offset] |= htole32(val & 0x0fffffff);
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
static long read_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2006-12-04 21:37:22 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
|
|
|
if (fat_bpb->is_fat16)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2004-09-23 21:43:43 +00:00
|
|
|
int sector = entry / CLUSTERS_PER_FAT16_SECTOR;
|
|
|
|
int offset = entry % CLUSTERS_PER_FAT16_SECTOR;
|
|
|
|
unsigned short* sec;
|
|
|
|
|
2005-01-04 23:01:25 +00:00
|
|
|
sec = cache_fat_sector(IF_MV2(fat_bpb,) sector, false);
|
2004-09-23 21:43:43 +00:00
|
|
|
if (!sec)
|
|
|
|
{
|
|
|
|
DEBUGF( "read_fat_entry() - Could not cache sector %d\n", sector);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-06 19:27:43 +00:00
|
|
|
return letoh16(sec[offset]);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2004-09-23 21:43:43 +00:00
|
|
|
else
|
|
|
|
#endif /* #ifdef HAVE_FAT16SUPPORT */
|
|
|
|
{
|
2005-01-23 23:20:40 +00:00
|
|
|
long sector = entry / CLUSTERS_PER_FAT_SECTOR;
|
2004-09-23 21:43:43 +00:00
|
|
|
int offset = entry % CLUSTERS_PER_FAT_SECTOR;
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long* sec;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2005-01-04 23:01:25 +00:00
|
|
|
sec = cache_fat_sector(IF_MV2(fat_bpb,) sector, false);
|
2004-09-23 21:43:43 +00:00
|
|
|
if (!sec)
|
|
|
|
{
|
2005-01-23 23:20:40 +00:00
|
|
|
DEBUGF( "read_fat_entry() - Could not cache sector %ld\n", sector);
|
2004-09-23 21:43:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-06 19:27:43 +00:00
|
|
|
return letoh32(sec[offset]) & 0x0fffffff;
|
2004-09-23 21:43:43 +00:00
|
|
|
}
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
static long get_next_cluster(IF_MV2(struct bpb* fat_bpb,) long cluster)
|
2002-04-27 01:25:22 +00:00
|
|
|
{
|
2005-01-23 23:20:40 +00:00
|
|
|
long next_cluster;
|
|
|
|
long eof_mark = FAT_EOF_MARK;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:35:10 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->is_fat16)
|
2004-09-23 21:43:43 +00:00
|
|
|
{
|
|
|
|
eof_mark &= 0xFFFF; /* only 16 bit */
|
|
|
|
if (cluster < 0) /* FAT16 root dir */
|
|
|
|
return cluster + 1; /* don't use the FAT */
|
|
|
|
}
|
|
|
|
#endif
|
2004-12-28 22:16:07 +00:00
|
|
|
next_cluster = read_fat_entry(IF_MV2(fat_bpb,) cluster);
|
2002-05-09 22:57:54 +00:00
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
/* is this last cluster in chain? */
|
2004-09-23 21:43:43 +00:00
|
|
|
if ( next_cluster >= eof_mark )
|
2002-04-27 01:25:22 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return next_cluster;
|
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb))
|
2002-12-09 15:39:32 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2006-12-04 21:37:22 +00:00
|
|
|
unsigned char fsinfo[SECTOR_SIZE];
|
2005-02-25 09:11:29 +00:00
|
|
|
unsigned long* intptr;
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->is_fat16)
|
2004-09-23 21:43:43 +00:00
|
|
|
return 0; /* FAT16 has no FsInfo */
|
|
|
|
#endif /* #ifdef HAVE_FAT16SUPPORT */
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2002-12-09 15:39:32 +00:00
|
|
|
/* update fsinfo */
|
2010-04-03 22:02:09 +00:00
|
|
|
rc = storage_read_sectors(IF_MD2(fat_bpb->drive,)
|
2006-12-04 21:37:22 +00:00
|
|
|
fat_bpb->startsector + fat_bpb->bpb_fsinfo, 1,fsinfo);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
2002-12-09 15:39:32 +00:00
|
|
|
{
|
2003-02-26 03:02:49 +00:00
|
|
|
DEBUGF( "flush_fat() - Couldn't read FSInfo (error code %d)\n", rc);
|
|
|
|
return rc * 10 - 1;
|
2002-12-09 15:39:32 +00:00
|
|
|
}
|
2005-02-25 09:11:29 +00:00
|
|
|
intptr = (long*)&(fsinfo[FSINFO_FREECOUNT]);
|
2005-10-06 19:27:43 +00:00
|
|
|
*intptr = htole32(fat_bpb->fsinfo.freecount);
|
2002-12-09 15:39:32 +00:00
|
|
|
|
2005-02-25 09:11:29 +00:00
|
|
|
intptr = (long*)&(fsinfo[FSINFO_NEXTFREE]);
|
2005-10-06 19:27:43 +00:00
|
|
|
*intptr = htole32(fat_bpb->fsinfo.nextfree);
|
2002-12-09 15:39:32 +00:00
|
|
|
|
2010-04-03 22:02:09 +00:00
|
|
|
rc = storage_write_sectors(IF_MD2(fat_bpb->drive,)
|
2006-12-04 21:37:22 +00:00
|
|
|
fat_bpb->startsector + fat_bpb->bpb_fsinfo,1,fsinfo);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
2002-12-09 15:39:32 +00:00
|
|
|
{
|
2003-02-26 03:02:49 +00:00
|
|
|
DEBUGF( "flush_fat() - Couldn't write FSInfo (error code %d)\n", rc);
|
|
|
|
return rc * 10 - 2;
|
2002-12-09 15:39:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
static int flush_fat(IF_MV_NONVOID(struct bpb* fat_bpb))
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2002-03-28 15:09:10 +00:00
|
|
|
unsigned char *sec;
|
2002-10-20 22:50:58 +00:00
|
|
|
LDEBUGF("flush_fat()\n");
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2005-01-05 00:09:04 +00:00
|
|
|
mutex_lock(&cache_mutex);
|
2002-05-09 21:27:49 +00:00
|
|
|
for(i = 0;i < FAT_CACHE_SIZE;i++)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
struct fat_cache_entry *fce = &fat_cache[i];
|
2005-01-05 00:09:04 +00:00
|
|
|
if(fce->inuse
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
&& fce->fat_vol == fat_bpb
|
|
|
|
#endif
|
|
|
|
&& fce->dirty)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-10-20 22:50:58 +00:00
|
|
|
sec = fat_cache_sectors[i];
|
2004-12-28 22:16:07 +00:00
|
|
|
flush_fat_sector(fce, sec);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
2005-01-05 00:09:04 +00:00
|
|
|
mutex_unlock(&cache_mutex);
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = update_fsinfo(IF_MV(fat_bpb));
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 3;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-22 23:51:46 +00:00
|
|
|
static void fat_time(unsigned short* date,
|
|
|
|
unsigned short* time,
|
|
|
|
unsigned short* tenth )
|
|
|
|
{
|
2007-03-16 23:47:03 +00:00
|
|
|
#if CONFIG_RTC
|
2002-11-22 23:51:46 +00:00
|
|
|
struct tm* tm = get_time();
|
|
|
|
|
|
|
|
if (date)
|
|
|
|
*date = ((tm->tm_year - 80) << 9) |
|
|
|
|
((tm->tm_mon + 1) << 5) |
|
|
|
|
tm->tm_mday;
|
|
|
|
|
|
|
|
if (time)
|
|
|
|
*time = (tm->tm_hour << 11) |
|
|
|
|
(tm->tm_min << 5) |
|
|
|
|
(tm->tm_sec >> 1);
|
|
|
|
|
|
|
|
if (tenth)
|
|
|
|
*tenth = (tm->tm_sec & 1) * 100;
|
2007-03-03 00:11:20 +00:00
|
|
|
#else
|
2003-11-03 00:59:04 +00:00
|
|
|
/* non-RTC version returns an increment from the supplied time, or a
|
|
|
|
* fixed standard time/date if no time given as input */
|
2009-08-16 17:18:45 +00:00
|
|
|
|
|
|
|
/* Macros to convert a 2-digit string to a decimal constant.
|
|
|
|
(YEAR), MONTH and DAY are set by the date command, which outputs
|
|
|
|
DAY as 00..31 and MONTH as 01..12. The leading zero would lead to
|
|
|
|
misinterpretation as an octal constant. */
|
|
|
|
#define S100(x) 1 ## x
|
|
|
|
#define C2DIG2DEC(x) (S100(x)-100)
|
|
|
|
/* The actual build date, as FAT date constant */
|
|
|
|
#define BUILD_DATE_FAT (((YEAR - 1980) << 9) \
|
|
|
|
| (C2DIG2DEC(MONTH) << 5) \
|
|
|
|
| C2DIG2DEC(DAY))
|
|
|
|
|
|
|
|
bool date_forced = false;
|
2003-11-03 00:59:04 +00:00
|
|
|
bool next_day = false;
|
2009-08-16 17:18:45 +00:00
|
|
|
unsigned time2 = 0; /* double time, for CRTTIME with 1s precision */
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2009-08-16 17:18:45 +00:00
|
|
|
if (date && *date < BUILD_DATE_FAT)
|
|
|
|
{
|
|
|
|
*date = BUILD_DATE_FAT;
|
|
|
|
date_forced = true;
|
|
|
|
}
|
|
|
|
|
2003-11-03 00:59:04 +00:00
|
|
|
if (time)
|
|
|
|
{
|
2009-08-16 17:18:45 +00:00
|
|
|
time2 = *time << 1;
|
|
|
|
if (time2 == 0 || date_forced)
|
2003-11-03 00:59:04 +00:00
|
|
|
{
|
2009-08-16 17:18:45 +00:00
|
|
|
time2 = (11 < 6) | 11; /* set to 00:11:11 */
|
2003-11-03 00:59:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-16 17:18:45 +00:00
|
|
|
unsigned mins = (time2 >> 6) & 0x3f;
|
|
|
|
unsigned hours = (time2 >> 12) & 0x1f;
|
|
|
|
|
|
|
|
mins = 11 * ((mins/11) + 1); /* advance to next multiple of 11 */
|
|
|
|
if (mins > 59)
|
2003-11-03 00:59:04 +00:00
|
|
|
{
|
2009-08-16 17:18:45 +00:00
|
|
|
mins = 11; /* 00 would be a bad marker */
|
|
|
|
if (++hours > 23)
|
|
|
|
{
|
|
|
|
hours = 0;
|
|
|
|
next_day = true;
|
|
|
|
}
|
2003-11-03 00:59:04 +00:00
|
|
|
}
|
2009-08-16 17:18:45 +00:00
|
|
|
time2 = (hours << 12) | (mins << 6) | mins; /* secs = mins */
|
2003-11-03 00:59:04 +00:00
|
|
|
}
|
2009-08-16 17:18:45 +00:00
|
|
|
*time = time2 >> 1;
|
2003-11-03 00:59:04 +00:00
|
|
|
}
|
2009-08-16 17:18:45 +00:00
|
|
|
|
|
|
|
if (tenth)
|
|
|
|
*tenth = (time2 & 1) * 100;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2009-08-16 17:18:45 +00:00
|
|
|
if (date && next_day)
|
2003-11-03 00:59:04 +00:00
|
|
|
{
|
2009-08-16 17:18:45 +00:00
|
|
|
static const unsigned char daysinmonth[] =
|
|
|
|
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
|
|
unsigned day = *date & 0x1f;
|
|
|
|
unsigned month = (*date >> 5) & 0x0f;
|
|
|
|
unsigned year = (*date >> 9) & 0x7f;
|
|
|
|
|
|
|
|
/* simplification: ignore leap years */
|
|
|
|
if (++day > daysinmonth[month-1])
|
2003-11-03 00:59:04 +00:00
|
|
|
{
|
2009-08-16 17:18:45 +00:00
|
|
|
day = 1;
|
|
|
|
if (++month > 12)
|
2003-11-03 00:59:04 +00:00
|
|
|
{
|
2009-08-16 17:18:45 +00:00
|
|
|
month = 1;
|
|
|
|
year++;
|
2003-11-03 00:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-16 17:18:45 +00:00
|
|
|
*date = (year << 9) | (month << 5) | day;
|
2003-11-03 00:59:04 +00:00
|
|
|
}
|
2009-08-16 17:18:45 +00:00
|
|
|
|
2007-03-03 00:11:20 +00:00
|
|
|
#endif /* CONFIG_RTC */
|
2002-11-22 23:51:46 +00:00
|
|
|
}
|
2007-03-03 00:11:20 +00:00
|
|
|
|
2002-11-15 16:41:02 +00:00
|
|
|
static int write_long_name(struct fat_file* file,
|
|
|
|
unsigned int firstentry,
|
|
|
|
unsigned int numentries,
|
2004-08-17 01:45:48 +00:00
|
|
|
const unsigned char* name,
|
|
|
|
const unsigned char* shortname,
|
2004-01-15 14:30:59 +00:00
|
|
|
bool is_directory)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2006-12-04 21:37:22 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE];
|
2002-11-15 16:41:02 +00:00
|
|
|
unsigned char* entry;
|
|
|
|
unsigned int idx = firstentry % DIR_ENTRIES_PER_SECTOR;
|
|
|
|
unsigned int sector = firstentry / DIR_ENTRIES_PER_SECTOR;
|
|
|
|
unsigned char chksum = 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
unsigned int i, j=0;
|
|
|
|
unsigned int nameidx=0, namelen = utf8length(name);
|
2002-11-15 16:41:02 +00:00
|
|
|
int rc;
|
2005-12-06 13:27:15 +00:00
|
|
|
unsigned short name_utf16[namelen + 1];
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("write_long_name(file:%lx, first:%d, num:%d, name:%s)\n",
|
2002-11-15 16:41:02 +00:00
|
|
|
file->firstcluster, firstentry, numentries, name);
|
|
|
|
|
|
|
|
rc = fat_seek(file, sector);
|
|
|
|
if (rc<0)
|
2003-02-26 03:02:49 +00:00
|
|
|
return rc * 10 - 1;
|
2002-11-15 16:41:02 +00:00
|
|
|
|
|
|
|
rc = fat_readwrite(file, 1, buf, false);
|
|
|
|
if (rc<1)
|
2003-02-26 03:02:49 +00:00
|
|
|
return rc * 10 - 2;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-11-15 16:41:02 +00:00
|
|
|
/* calculate shortname checksum */
|
|
|
|
for (i=11; i>0; i--)
|
|
|
|
chksum = ((chksum & 1) ? 0x80 : 0) + (chksum >> 1) + shortname[j++];
|
|
|
|
|
|
|
|
/* calc position of last name segment */
|
2002-11-18 11:58:43 +00:00
|
|
|
if ( namelen > NAME_BYTES_PER_ENTRY )
|
|
|
|
for (nameidx=0;
|
|
|
|
nameidx < (namelen - NAME_BYTES_PER_ENTRY);
|
|
|
|
nameidx += NAME_BYTES_PER_ENTRY);
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* we need to convert the name first */
|
|
|
|
/* since it is written in reverse order */
|
|
|
|
for (i = 0; i <= namelen; i++)
|
|
|
|
name = utf8decode(name, &name_utf16[i]);
|
|
|
|
|
2002-11-15 16:41:02 +00:00
|
|
|
for (i=0; i < numentries; i++) {
|
|
|
|
/* new sector? */
|
|
|
|
if ( idx >= DIR_ENTRIES_PER_SECTOR ) {
|
|
|
|
/* update current sector */
|
|
|
|
rc = fat_seek(file, sector);
|
|
|
|
if (rc<0)
|
2003-02-26 03:02:49 +00:00
|
|
|
return rc * 10 - 3;
|
2002-11-15 16:41:02 +00:00
|
|
|
|
|
|
|
rc = fat_readwrite(file, 1, buf, true);
|
|
|
|
if (rc<1)
|
2003-02-26 03:02:49 +00:00
|
|
|
return rc * 10 - 4;
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2002-11-18 11:58:43 +00:00
|
|
|
/* read next sector */
|
2002-11-15 16:41:02 +00:00
|
|
|
rc = fat_readwrite(file, 1, buf, false);
|
2002-11-18 11:58:43 +00:00
|
|
|
if (rc<0) {
|
|
|
|
LDEBUGF("Failed writing new sector: %d\n",rc);
|
2003-02-26 03:02:49 +00:00
|
|
|
return rc * 10 - 5;
|
2002-11-18 11:58:43 +00:00
|
|
|
}
|
|
|
|
if (rc==0)
|
|
|
|
/* end of dir */
|
|
|
|
memset(buf, 0, sizeof buf);
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-11-15 16:41:02 +00:00
|
|
|
sector++;
|
|
|
|
idx = 0;
|
|
|
|
}
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-11-15 16:41:02 +00:00
|
|
|
entry = buf + idx * DIR_ENTRY_SIZE;
|
|
|
|
|
|
|
|
/* verify this entry is free */
|
|
|
|
if (entry[0] && entry[0] != 0xe5 )
|
|
|
|
panicf("Dir entry %d in sector %x is not free! "
|
|
|
|
"%02x %02x %02x %02x",
|
|
|
|
idx, sector,
|
|
|
|
entry[0], entry[1], entry[2], entry[3]);
|
|
|
|
|
|
|
|
memset(entry, 0, DIR_ENTRY_SIZE);
|
|
|
|
if ( i+1 < numentries ) {
|
|
|
|
/* longname entry */
|
2005-12-06 13:27:15 +00:00
|
|
|
unsigned int k, l = nameidx;
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2002-11-15 16:41:02 +00:00
|
|
|
entry[FATLONG_ORDER] = numentries-i-1;
|
2002-11-18 11:58:43 +00:00
|
|
|
if (i==0) {
|
|
|
|
/* mark this as last long entry */
|
2010-03-22 10:35:25 +00:00
|
|
|
entry[FATLONG_ORDER] |= FATLONG_LAST_LONG_ENTRY;
|
2002-11-18 11:58:43 +00:00
|
|
|
|
|
|
|
/* pad name with 0xffff */
|
2006-07-31 22:59:45 +00:00
|
|
|
for (k=1; k<11; k++) entry[k] = FAT_LONGNAME_PAD_BYTE;
|
|
|
|
for (k=14; k<26; k++) entry[k] = FAT_LONGNAME_PAD_BYTE;
|
|
|
|
for (k=28; k<32; k++) entry[k] = FAT_LONGNAME_PAD_BYTE;
|
2002-11-18 11:58:43 +00:00
|
|
|
};
|
|
|
|
/* set name */
|
|
|
|
for (k=0; k<5 && l <= namelen; k++) {
|
2005-12-06 13:27:15 +00:00
|
|
|
entry[k*2 + 1] = (unsigned char)(name_utf16[l] & 0xff);
|
|
|
|
entry[k*2 + 2] = (unsigned char)(name_utf16[l++] >> 8);
|
2002-11-18 11:58:43 +00:00
|
|
|
}
|
|
|
|
for (k=0; k<6 && l <= namelen; k++) {
|
2005-12-06 13:27:15 +00:00
|
|
|
entry[k*2 + 14] = (unsigned char)(name_utf16[l] & 0xff);
|
|
|
|
entry[k*2 + 15] = (unsigned char)(name_utf16[l++] >> 8);
|
2002-11-18 11:58:43 +00:00
|
|
|
}
|
|
|
|
for (k=0; k<2 && l <= namelen; k++) {
|
2005-12-06 13:27:15 +00:00
|
|
|
entry[k*2 + 28] = (unsigned char)(name_utf16[l] & 0xff);
|
|
|
|
entry[k*2 + 29] = (unsigned char)(name_utf16[l++] >> 8);
|
2002-11-18 11:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entry[FATDIR_ATTR] = FAT_ATTR_LONG_NAME;
|
|
|
|
entry[FATDIR_FSTCLUSLO] = 0;
|
|
|
|
entry[FATLONG_TYPE] = 0;
|
2002-11-15 16:41:02 +00:00
|
|
|
entry[FATLONG_CHKSUM] = chksum;
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("Longname entry %d: %s\n", idx, name+nameidx);
|
2002-11-15 16:41:02 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* shortname entry */
|
2002-11-22 23:51:46 +00:00
|
|
|
unsigned short date=0, time=0, tenth=0;
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("Shortname entry: %s\n", shortname);
|
2009-07-14 13:57:45 +00:00
|
|
|
memcpy(entry + FATDIR_NAME, shortname, 11);
|
2004-01-15 14:30:59 +00:00
|
|
|
entry[FATDIR_ATTR] = is_directory?FAT_ATTR_DIRECTORY:0;
|
2002-11-15 16:41:02 +00:00
|
|
|
entry[FATDIR_NTRES] = 0;
|
2002-11-22 23:51:46 +00:00
|
|
|
|
|
|
|
fat_time(&date, &time, &tenth);
|
|
|
|
entry[FATDIR_CRTTIMETENTH] = tenth;
|
2005-10-06 19:27:43 +00:00
|
|
|
*(unsigned short*)(entry + FATDIR_CRTTIME) = htole16(time);
|
|
|
|
*(unsigned short*)(entry + FATDIR_WRTTIME) = htole16(time);
|
|
|
|
*(unsigned short*)(entry + FATDIR_CRTDATE) = htole16(date);
|
|
|
|
*(unsigned short*)(entry + FATDIR_WRTDATE) = htole16(date);
|
|
|
|
*(unsigned short*)(entry + FATDIR_LSTACCDATE) = htole16(date);
|
2002-11-15 16:41:02 +00:00
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
nameidx -= NAME_BYTES_PER_ENTRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update last sector */
|
|
|
|
rc = fat_seek(file, sector);
|
|
|
|
if (rc<0)
|
2003-02-26 03:02:49 +00:00
|
|
|
return rc * 10 - 6;
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2002-11-15 16:41:02 +00:00
|
|
|
rc = fat_readwrite(file, 1, buf, true);
|
|
|
|
if (rc<1)
|
2003-02-26 03:02:49 +00:00
|
|
|
return rc * 10 - 7;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-07-03 17:03:33 +00:00
|
|
|
|
2005-09-07 01:35:15 +00:00
|
|
|
static int fat_checkname(const unsigned char* newname)
|
|
|
|
{
|
2007-07-03 17:03:33 +00:00
|
|
|
static const char invalid_chars[] = "\"*/:<>?\\|";
|
2007-07-03 09:53:16 +00:00
|
|
|
int len = strlen(newname);
|
2005-09-07 01:35:15 +00:00
|
|
|
/* More sanity checks are probably needed */
|
2007-07-03 17:03:33 +00:00
|
|
|
if (len > 255 || newname[len - 1] == '.')
|
2007-07-03 09:53:16 +00:00
|
|
|
{
|
2005-09-07 01:35:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-07-03 09:53:16 +00:00
|
|
|
while (*newname)
|
|
|
|
{
|
2007-07-03 17:03:33 +00:00
|
|
|
if (*newname < ' ' || strchr(invalid_chars, *newname) != NULL)
|
2007-07-03 09:53:16 +00:00
|
|
|
return -1;
|
|
|
|
newname++;
|
|
|
|
}
|
2008-02-07 22:15:13 +00:00
|
|
|
/* check trailing space(s) */
|
|
|
|
if(*(--newname) == ' ')
|
|
|
|
return -1;
|
|
|
|
|
2005-09-07 01:35:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
static int add_dir_entry(struct fat_dir* dir,
|
2002-11-15 16:41:02 +00:00
|
|
|
struct fat_file* file,
|
2004-08-17 01:45:48 +00:00
|
|
|
const char* name,
|
2004-01-15 14:30:59 +00:00
|
|
|
bool is_directory,
|
|
|
|
bool dotdir)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[dir->file.volume];
|
|
|
|
#else
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2006-12-04 21:37:22 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE];
|
2005-04-22 22:26:13 +00:00
|
|
|
unsigned char shortname[12];
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2002-11-15 11:20:33 +00:00
|
|
|
unsigned int sector;
|
2002-10-20 22:50:58 +00:00
|
|
|
bool done = false;
|
2002-11-15 16:41:02 +00:00
|
|
|
int entries_needed, entries_found = 0;
|
2002-11-18 11:58:43 +00:00
|
|
|
int firstentry;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF( "add_dir_entry(%s,%lx)\n",
|
2002-11-15 16:41:02 +00:00
|
|
|
name, file->firstcluster);
|
|
|
|
|
2005-09-07 01:35:15 +00:00
|
|
|
/* Don't check dotdirs name for validity */
|
|
|
|
if (dotdir == false) {
|
|
|
|
rc = fat_checkname(name);
|
|
|
|
if (rc < 0) {
|
|
|
|
/* filename is invalid */
|
|
|
|
return rc * 10 - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
file->volume = dir->file.volume; /* inherit the volume, to make sure */
|
|
|
|
#endif
|
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
/* The "." and ".." directory entries must not be long names */
|
|
|
|
if(dotdir) {
|
|
|
|
int i;
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(shortname, name, 12);
|
2005-04-22 22:26:13 +00:00
|
|
|
for(i = strlen(shortname); i < 12; i++)
|
2004-01-15 14:30:59 +00:00
|
|
|
shortname[i] = ' ';
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
entries_needed = 1;
|
|
|
|
} else {
|
2005-04-23 13:15:25 +00:00
|
|
|
create_dos_name(name, shortname);
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
/* one dir entry needed for every 13 bytes of filename,
|
|
|
|
plus one entry for the short name */
|
2005-12-06 13:27:15 +00:00
|
|
|
entries_needed = (utf8length(name) + (NAME_BYTES_PER_ENTRY-1))
|
2005-04-22 22:26:13 +00:00
|
|
|
/ NAME_BYTES_PER_ENTRY + 1;
|
2004-01-15 14:30:59 +00:00
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
restart:
|
|
|
|
firstentry = -1;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_seek(&dir->file, 0);
|
|
|
|
if (rc < 0)
|
2005-09-07 01:35:15 +00:00
|
|
|
return rc * 10 - 2;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
/* step 1: search for free entries and check for duplicate shortname */
|
|
|
|
for (sector = 0; !done; sector++)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-11-15 16:41:02 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
rc = fat_readwrite(&dir->file, 1, buf, false);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0) {
|
2002-11-13 23:16:32 +00:00
|
|
|
DEBUGF( "add_dir_entry() - Couldn't read dir"
|
2003-02-26 03:02:49 +00:00
|
|
|
" (error code %d)\n", rc);
|
2005-09-07 01:35:15 +00:00
|
|
|
return rc * 10 - 3;
|
2005-04-22 22:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == 0) { /* current end of dir reached */
|
|
|
|
LDEBUGF("End of dir on cluster boundary\n");
|
|
|
|
break;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2002-11-15 16:41:02 +00:00
|
|
|
/* look for free slots */
|
2005-04-22 22:26:13 +00:00
|
|
|
for (i = 0; i < DIR_ENTRIES_PER_SECTOR; i++)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2005-04-22 22:26:13 +00:00
|
|
|
switch (buf[i * DIR_ENTRY_SIZE]) {
|
|
|
|
case 0:
|
|
|
|
entries_found += DIR_ENTRIES_PER_SECTOR - i;
|
|
|
|
LDEBUGF("Found end of dir %d\n",
|
2002-11-15 16:41:02 +00:00
|
|
|
sector * DIR_ENTRIES_PER_SECTOR + i);
|
2005-04-22 22:26:13 +00:00
|
|
|
i = DIR_ENTRIES_PER_SECTOR - 1;
|
2002-11-18 11:58:43 +00:00
|
|
|
done = true;
|
2002-11-15 16:41:02 +00:00
|
|
|
break;
|
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
case 0xe5:
|
2002-11-15 16:41:02 +00:00
|
|
|
entries_found++;
|
|
|
|
LDEBUGF("Found free entry %d (%d/%d)\n",
|
|
|
|
sector * DIR_ENTRIES_PER_SECTOR + i,
|
|
|
|
entries_found, entries_needed);
|
|
|
|
break;
|
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
default:
|
2002-11-15 16:41:02 +00:00
|
|
|
entries_found = 0;
|
2002-11-18 11:58:43 +00:00
|
|
|
|
|
|
|
/* check that our intended shortname doesn't already exist */
|
2006-04-08 12:17:51 +00:00
|
|
|
if (!strncmp(shortname, buf + i * DIR_ENTRY_SIZE, 11)) {
|
2005-04-22 22:26:13 +00:00
|
|
|
/* shortname exists already, make a new one */
|
2005-02-16 20:45:56 +00:00
|
|
|
randomize_dos_name(shortname);
|
2002-11-18 11:58:43 +00:00
|
|
|
LDEBUGF("Duplicate shortname, changing to %s\n",
|
|
|
|
shortname);
|
|
|
|
|
|
|
|
/* name has changed, we need to restart search */
|
|
|
|
goto restart;
|
|
|
|
}
|
2002-11-15 16:41:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-04-22 22:26:13 +00:00
|
|
|
if (firstentry < 0 && (entries_found >= entries_needed))
|
|
|
|
firstentry = sector * DIR_ENTRIES_PER_SECTOR + i + 1
|
|
|
|
- entries_found;
|
|
|
|
}
|
|
|
|
}
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
/* step 2: extend the dir if necessary */
|
|
|
|
if (firstentry < 0)
|
|
|
|
{
|
|
|
|
LDEBUGF("Adding new sector(s) to dir\n");
|
|
|
|
rc = fat_seek(&dir->file, sector);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 4;
|
|
|
|
memset(buf, 0, sizeof buf);
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
/* we must clear whole clusters */
|
|
|
|
for (; (entries_found < entries_needed) ||
|
|
|
|
(dir->file.sectornum < (int)fat_bpb->bpb_secperclus); sector++)
|
|
|
|
{
|
|
|
|
if (sector >= (65536/DIR_ENTRIES_PER_SECTOR))
|
|
|
|
return -5; /* dir too large -- FAT specification */
|
|
|
|
|
|
|
|
rc = fat_readwrite(&dir->file, 1, buf, true);
|
|
|
|
if (rc < 1) /* No more room or something went wrong */
|
|
|
|
return rc * 10 - 6;
|
|
|
|
|
|
|
|
entries_found += DIR_ENTRIES_PER_SECTOR;
|
2002-11-18 11:58:43 +00:00
|
|
|
}
|
2005-04-22 22:26:13 +00:00
|
|
|
|
|
|
|
firstentry = sector * DIR_ENTRIES_PER_SECTOR - entries_found;
|
2002-11-18 11:58:43 +00:00
|
|
|
}
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
/* step 3: add entry */
|
2002-11-18 11:58:43 +00:00
|
|
|
sector = firstentry / DIR_ENTRIES_PER_SECTOR;
|
|
|
|
LDEBUGF("Adding longname to entry %d in sector %d\n",
|
|
|
|
firstentry, sector);
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
rc = write_long_name(&dir->file, firstentry,
|
2004-01-15 14:30:59 +00:00
|
|
|
entries_needed, name, shortname, is_directory);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
2005-04-22 22:26:13 +00:00
|
|
|
return rc * 10 - 7;
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2002-11-18 11:58:43 +00:00
|
|
|
/* remember where the shortname dir entry is located */
|
|
|
|
file->direntry = firstentry + entries_needed - 1;
|
|
|
|
file->direntries = entries_needed;
|
|
|
|
file->dircluster = dir->file.firstcluster;
|
|
|
|
LDEBUGF("Added new dir entry %d, using %d slots.\n",
|
|
|
|
file->direntry, file->direntries);
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-25 15:44:55 +00:00
|
|
|
static unsigned char char2dos(unsigned char c, int* randomize)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2007-07-03 17:03:33 +00:00
|
|
|
static const char invalid_chars[] = "\"*+,./:;<=>?[\\]|";
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2007-07-03 17:03:33 +00:00
|
|
|
if (c <= 0x20)
|
|
|
|
c = 0; /* Illegal char, remove */
|
|
|
|
else if (strchr(invalid_chars, c) != NULL)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2007-07-03 17:03:33 +00:00
|
|
|
/* Illegal char, replace */
|
|
|
|
c = '_';
|
|
|
|
*randomize = 1; /* as per FAT spec */
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2007-07-03 09:53:16 +00:00
|
|
|
else
|
|
|
|
c = toupper(c);
|
2007-07-03 17:03:33 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2005-04-23 13:15:25 +00:00
|
|
|
static void create_dos_name(const unsigned char *name, unsigned char *newname)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2005-02-16 20:45:56 +00:00
|
|
|
int i;
|
2007-10-28 23:44:49 +00:00
|
|
|
unsigned char *ext;
|
2007-02-24 21:06:28 +00:00
|
|
|
int randomize = 0;
|
2005-02-16 20:45:56 +00:00
|
|
|
|
|
|
|
/* Find extension part */
|
|
|
|
ext = strrchr(name, '.');
|
|
|
|
if (ext == name) /* handle .dotnames */
|
|
|
|
ext = NULL;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2007-02-24 21:06:28 +00:00
|
|
|
/* needs to randomize? */
|
|
|
|
if((ext && (strlen(ext) > 4)) ||
|
|
|
|
((ext ? (unsigned int)(ext-name) : strlen(name)) > 8) )
|
|
|
|
randomize = 1;
|
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
/* Name part */
|
2005-02-16 20:45:56 +00:00
|
|
|
for (i = 0; *name && (!ext || name < ext) && (i < 8); name++)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2007-02-24 21:06:28 +00:00
|
|
|
unsigned char c = char2dos(*name, &randomize);
|
2002-11-18 11:58:43 +00:00
|
|
|
if (c)
|
2005-04-22 22:26:13 +00:00
|
|
|
newname[i++] = c;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 20:45:56 +00:00
|
|
|
/* Pad both name and extension */
|
|
|
|
while (i < 11)
|
|
|
|
newname[i++] = ' ';
|
2002-11-15 16:41:02 +00:00
|
|
|
|
2005-04-22 22:26:13 +00:00
|
|
|
if (newname[0] == 0xe5) /* Special kanji character */
|
|
|
|
newname[0] = 0x05;
|
|
|
|
|
2005-02-16 20:45:56 +00:00
|
|
|
if (ext)
|
|
|
|
{ /* Extension part */
|
|
|
|
ext++;
|
|
|
|
for (i = 8; *ext && (i < 11); ext++)
|
|
|
|
{
|
2007-02-24 21:06:28 +00:00
|
|
|
unsigned char c = char2dos(*ext, &randomize);
|
2005-02-16 20:45:56 +00:00
|
|
|
if (c)
|
2005-04-22 22:26:13 +00:00
|
|
|
newname[i++] = c;
|
2005-02-16 20:45:56 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-24 21:06:28 +00:00
|
|
|
|
|
|
|
if(randomize)
|
|
|
|
randomize_dos_name(newname);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 20:45:56 +00:00
|
|
|
static void randomize_dos_name(unsigned char *name)
|
|
|
|
{
|
2007-02-24 21:06:28 +00:00
|
|
|
unsigned char* tilde = NULL; /* ~ location */
|
|
|
|
unsigned char* lastpt = NULL; /* last point of filename */
|
|
|
|
unsigned char* nameptr = name; /* working copy of name pointer */
|
|
|
|
unsigned char num[9]; /* holds number as string */
|
|
|
|
int i = 0;
|
|
|
|
int cnt = 1;
|
|
|
|
int numlen;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
while(i++ < 8)
|
|
|
|
{
|
|
|
|
/* hunt for ~ and where to put it */
|
|
|
|
if((!tilde) && (*nameptr == '~'))
|
|
|
|
tilde = nameptr;
|
|
|
|
if((!lastpt) && ((*nameptr == ' ' || *nameptr == '~')))
|
|
|
|
lastpt = nameptr;
|
|
|
|
nameptr++;
|
|
|
|
}
|
|
|
|
if(tilde)
|
|
|
|
{
|
|
|
|
/* extract current count and increment */
|
|
|
|
memcpy(num,tilde+1,7-(unsigned int)(tilde-name));
|
|
|
|
num[7-(unsigned int)(tilde-name)] = 0;
|
|
|
|
cnt = atoi(num) + 1;
|
|
|
|
}
|
2007-02-24 21:40:00 +00:00
|
|
|
cnt %= 10000000; /* protection */
|
2007-02-24 21:06:28 +00:00
|
|
|
snprintf(num, 9, "~%d", cnt); /* allow room for trailing zero */
|
|
|
|
numlen = strlen(num); /* required space */
|
|
|
|
offset = (unsigned int)(lastpt ? lastpt - name : 8); /* prev startpoint */
|
|
|
|
if(offset > (8-numlen)) offset = 8-numlen; /* correct for new numlen */
|
2005-02-16 20:45:56 +00:00
|
|
|
|
2007-02-24 21:06:28 +00:00
|
|
|
memcpy(&name[offset], num, numlen);
|
2005-02-16 20:45:56 +00:00
|
|
|
|
2007-02-24 21:06:28 +00:00
|
|
|
/* in special case of counter overflow: pad with spaces */
|
|
|
|
for(offset = offset+numlen; offset < 8; offset++)
|
|
|
|
name[offset] = ' ';
|
2005-02-16 20:45:56 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
static int update_short_entry( struct fat_file* file, long size, int attr )
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2006-12-04 21:37:22 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE];
|
2002-11-18 11:58:43 +00:00
|
|
|
int sector = file->direntry / DIR_ENTRIES_PER_SECTOR;
|
|
|
|
unsigned char* entry =
|
|
|
|
buf + DIR_ENTRY_SIZE * (file->direntry % DIR_ENTRIES_PER_SECTOR);
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long* sizeptr;
|
2002-10-20 22:50:58 +00:00
|
|
|
unsigned short* clusptr;
|
2002-11-18 11:58:43 +00:00
|
|
|
struct fat_file dir;
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("update_file_size(cluster:%lx entry:%d size:%ld)\n",
|
2002-11-18 11:58:43 +00:00
|
|
|
file->firstcluster, file->direntry, size);
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-11-18 11:58:43 +00:00
|
|
|
/* create a temporary file handle for the dir holding this file */
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = fat_open(IF_MV2(file->volume,) file->dircluster, &dir, NULL);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 1;
|
2002-11-01 15:26:06 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_seek( &dir, sector );
|
|
|
|
if (rc<0)
|
|
|
|
return rc * 10 - 2;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_readwrite(&dir, 1, buf, false);
|
|
|
|
if (rc < 1)
|
|
|
|
return rc * 10 - 3;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-11-18 11:58:43 +00:00
|
|
|
if (!entry[0] || entry[0] == 0xe5)
|
|
|
|
panicf("Updating size on empty dir entry %d\n", file->direntry);
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2003-01-27 09:32:17 +00:00
|
|
|
entry[FATDIR_ATTR] = attr & 0xFF;
|
|
|
|
|
2002-11-18 11:58:43 +00:00
|
|
|
clusptr = (short*)(entry + FATDIR_FSTCLUSHI);
|
2005-10-06 19:27:43 +00:00
|
|
|
*clusptr = htole16(file->firstcluster >> 16);
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2002-11-18 11:58:43 +00:00
|
|
|
clusptr = (short*)(entry + FATDIR_FSTCLUSLO);
|
2005-10-06 19:27:43 +00:00
|
|
|
*clusptr = htole16(file->firstcluster & 0xffff);
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
sizeptr = (long*)(entry + FATDIR_FILESIZE);
|
2005-10-06 19:27:43 +00:00
|
|
|
*sizeptr = htole32(size);
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-11-22 23:55:08 +00:00
|
|
|
{
|
2007-03-16 23:47:03 +00:00
|
|
|
#if CONFIG_RTC
|
2003-11-03 00:59:04 +00:00
|
|
|
unsigned short time = 0;
|
|
|
|
unsigned short date = 0;
|
|
|
|
#else
|
|
|
|
/* get old time to increment from */
|
2007-10-28 23:44:49 +00:00
|
|
|
unsigned short time = htole16(*(unsigned short*)(entry+FATDIR_WRTTIME));
|
|
|
|
unsigned short date = htole16(*(unsigned short*)(entry+FATDIR_WRTDATE));
|
2003-11-03 00:59:04 +00:00
|
|
|
#endif
|
2002-11-22 23:55:08 +00:00
|
|
|
fat_time(&date, &time, NULL);
|
2005-10-06 19:27:43 +00:00
|
|
|
*(unsigned short*)(entry + FATDIR_WRTTIME) = htole16(time);
|
|
|
|
*(unsigned short*)(entry + FATDIR_WRTDATE) = htole16(date);
|
|
|
|
*(unsigned short*)(entry + FATDIR_LSTACCDATE) = htole16(date);
|
2002-11-22 23:55:08 +00:00
|
|
|
}
|
2002-11-22 23:51:46 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_seek( &dir, sector );
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 4;
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_readwrite(&dir, 1, buf, true);
|
|
|
|
if (rc < 1)
|
|
|
|
return rc * 10 - 5;
|
2002-11-13 23:16:32 +00:00
|
|
|
|
|
|
|
return 0;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2004-08-17 01:45:48 +00:00
|
|
|
static int parse_direntry(struct fat_direntry *de, const unsigned char *buf)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-07 18:56:32 +00:00
|
|
|
int i=0,j=0;
|
2005-04-22 22:26:13 +00:00
|
|
|
unsigned char c;
|
|
|
|
bool lowercase;
|
|
|
|
|
2002-04-26 16:44:58 +00:00
|
|
|
memset(de, 0, sizeof(struct fat_direntry));
|
2002-03-28 15:09:10 +00:00
|
|
|
de->attr = buf[FATDIR_ATTR];
|
|
|
|
de->crttimetenth = buf[FATDIR_CRTTIMETENTH];
|
2002-04-26 16:44:58 +00:00
|
|
|
de->crtdate = BYTES2INT16(buf,FATDIR_CRTDATE);
|
|
|
|
de->crttime = BYTES2INT16(buf,FATDIR_CRTTIME);
|
|
|
|
de->wrtdate = BYTES2INT16(buf,FATDIR_WRTDATE);
|
|
|
|
de->wrttime = BYTES2INT16(buf,FATDIR_WRTTIME);
|
|
|
|
de->filesize = BYTES2INT32(buf,FATDIR_FILESIZE);
|
2005-02-27 20:44:26 +00:00
|
|
|
de->firstcluster = ((long)(unsigned)BYTES2INT16(buf,FATDIR_FSTCLUSLO)) |
|
|
|
|
((long)(unsigned)BYTES2INT16(buf,FATDIR_FSTCLUSHI) << 16);
|
|
|
|
/* The double cast is to prevent a sign-extension to be done on CalmRISC16.
|
|
|
|
(the result of the shift is always considered signed) */
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2002-05-07 18:56:32 +00:00
|
|
|
/* fix the name */
|
2005-04-22 22:26:13 +00:00
|
|
|
lowercase = (buf[FATDIR_NTRES] & FAT_NTRES_LC_NAME);
|
|
|
|
c = buf[FATDIR_NAME];
|
|
|
|
if (c == 0x05) /* special kanji char */
|
|
|
|
c = 0xe5;
|
|
|
|
i = 0;
|
|
|
|
while (c != ' ') {
|
|
|
|
de->name[j++] = lowercase ? tolower(c) : c;
|
|
|
|
if (++i >= 8)
|
|
|
|
break;
|
|
|
|
c = buf[FATDIR_NAME+i];
|
|
|
|
}
|
|
|
|
if (buf[FATDIR_NAME+8] != ' ') {
|
|
|
|
lowercase = (buf[FATDIR_NTRES] & FAT_NTRES_LC_EXT);
|
2002-05-07 18:56:32 +00:00
|
|
|
de->name[j++] = '.';
|
2005-04-22 22:26:13 +00:00
|
|
|
for (i = 8; (i < 11) && ((c = buf[FATDIR_NAME+i]) != ' '); i++)
|
|
|
|
de->name[j++] = lowercase ? tolower(c) : c;
|
2002-05-07 18:56:32 +00:00
|
|
|
}
|
2002-04-26 16:44:58 +00:00
|
|
|
return 1;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2005-01-03 07:59:49 +00:00
|
|
|
int fat_open(IF_MV2(int volume,)
|
2005-01-23 23:20:40 +00:00
|
|
|
long startcluster,
|
2002-10-20 22:50:58 +00:00
|
|
|
struct fat_file *file,
|
2004-08-17 01:45:48 +00:00
|
|
|
const struct fat_dir* dir)
|
2002-04-27 19:37:41 +00:00
|
|
|
{
|
2010-03-15 12:31:24 +00:00
|
|
|
/* Remember where the file's dir entry is located
|
2010-03-16 00:54:08 +00:00
|
|
|
* Do it before assigning other fields so that fat_open
|
2010-03-15 12:31:24 +00:00
|
|
|
* can be called with file == &dir->file (see fat_opendir) */
|
|
|
|
if ( dir ) {
|
|
|
|
file->direntry = dir->entry - 1;
|
|
|
|
file->direntries = dir->entrycount;
|
|
|
|
file->dircluster = dir->file.firstcluster;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
file->firstcluster = startcluster;
|
2002-10-22 15:06:08 +00:00
|
|
|
file->lastcluster = startcluster;
|
2002-10-31 16:09:28 +00:00
|
|
|
file->lastsector = 0;
|
2003-06-03 18:04:22 +00:00
|
|
|
file->clusternum = 0;
|
2002-05-03 15:35:51 +00:00
|
|
|
file->sectornum = 0;
|
2002-11-13 23:16:32 +00:00
|
|
|
file->eof = false;
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
file->volume = volume;
|
|
|
|
/* fixme: remove error check when done */
|
|
|
|
if (volume >= NUM_VOLUMES || !fat_bpbs[volume].mounted)
|
|
|
|
{
|
|
|
|
LDEBUGF("fat_open() illegal volume %d\n", volume);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("fat_open(%lx), entry %d\n",startcluster,file->direntry);
|
2002-10-20 22:50:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-17 01:45:48 +00:00
|
|
|
int fat_create_file(const char* name,
|
2002-10-20 22:50:58 +00:00
|
|
|
struct fat_file* file,
|
|
|
|
struct fat_dir* dir)
|
|
|
|
{
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("fat_create_file(\"%s\",%lx,%lx)\n",name,(long)file,(long)dir);
|
2004-01-15 14:30:59 +00:00
|
|
|
rc = add_dir_entry(dir, file, name, false, false);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (!rc) {
|
2002-10-20 22:50:58 +00:00
|
|
|
file->firstcluster = 0;
|
2002-10-22 15:06:08 +00:00
|
|
|
file->lastcluster = 0;
|
|
|
|
file->lastsector = 0;
|
2003-06-03 18:04:22 +00:00
|
|
|
file->clusternum = 0;
|
2002-10-20 22:50:58 +00:00
|
|
|
file->sectornum = 0;
|
2002-11-13 23:16:32 +00:00
|
|
|
file->eof = false;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
return rc;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
2004-08-17 01:45:48 +00:00
|
|
|
int fat_create_dir(const char* name,
|
2004-01-15 14:30:59 +00:00
|
|
|
struct fat_dir* newdir,
|
|
|
|
struct fat_dir* dir)
|
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[dir->file.volume];
|
|
|
|
#else
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2006-12-04 21:37:22 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE];
|
2004-01-15 19:05:49 +00:00
|
|
|
int i;
|
2005-02-25 09:11:29 +00:00
|
|
|
long sector;
|
2004-01-15 14:30:59 +00:00
|
|
|
int rc;
|
|
|
|
struct fat_file dummyfile;
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("fat_create_dir(\"%s\",%lx,%lx)\n",name,(long)newdir,(long)dir);
|
2004-01-15 14:30:59 +00:00
|
|
|
|
2004-01-26 10:14:46 +00:00
|
|
|
memset(newdir, 0, sizeof(struct fat_dir));
|
|
|
|
memset(&dummyfile, 0, sizeof(struct fat_file));
|
2004-01-15 14:30:59 +00:00
|
|
|
|
|
|
|
/* First, add the entry in the parent directory */
|
|
|
|
rc = add_dir_entry(dir, &newdir->file, name, true, false);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 1;
|
|
|
|
|
2004-01-15 19:05:49 +00:00
|
|
|
/* Allocate a new cluster for the directory */
|
2007-10-28 23:44:49 +00:00
|
|
|
newdir->file.firstcluster = find_free_cluster(IF_MV2(fat_bpb,)
|
|
|
|
fat_bpb->fsinfo.nextfree);
|
2004-01-15 19:05:49 +00:00
|
|
|
if(newdir->file.firstcluster == 0)
|
|
|
|
return -1;
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
update_fat_entry(IF_MV2(fat_bpb,) newdir->file.firstcluster, FAT_EOF_MARK);
|
2004-01-15 19:05:49 +00:00
|
|
|
|
|
|
|
/* Clear the entire cluster */
|
|
|
|
memset(buf, 0, sizeof buf);
|
2004-12-28 22:16:07 +00:00
|
|
|
sector = cluster2sec(IF_MV2(fat_bpb,) newdir->file.firstcluster);
|
|
|
|
for(i = 0;i < (int)fat_bpb->bpb_secperclus;i++) {
|
|
|
|
rc = transfer(IF_MV2(fat_bpb,) sector + i, 1, buf, true );
|
2004-01-15 19:05:49 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 2;
|
|
|
|
}
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-01-15 19:05:49 +00:00
|
|
|
/* Then add the "." entry */
|
2004-01-15 14:30:59 +00:00
|
|
|
rc = add_dir_entry(newdir, &dummyfile, ".", true, true);
|
|
|
|
if (rc < 0)
|
2004-01-15 19:05:49 +00:00
|
|
|
return rc * 10 - 3;
|
2004-01-15 14:30:59 +00:00
|
|
|
dummyfile.firstcluster = newdir->file.firstcluster;
|
|
|
|
update_short_entry(&dummyfile, 0, FAT_ATTR_DIRECTORY);
|
|
|
|
|
|
|
|
/* and the ".." entry */
|
|
|
|
rc = add_dir_entry(newdir, &dummyfile, "..", true, true);
|
|
|
|
if (rc < 0)
|
2004-01-15 19:05:49 +00:00
|
|
|
return rc * 10 - 4;
|
2004-01-15 14:30:59 +00:00
|
|
|
|
|
|
|
/* The root cluster is cluster 0 in the ".." entry */
|
2004-12-28 22:16:07 +00:00
|
|
|
if(dir->file.firstcluster == fat_bpb->bpb_rootclus)
|
2004-01-15 14:30:59 +00:00
|
|
|
dummyfile.firstcluster = 0;
|
|
|
|
else
|
|
|
|
dummyfile.firstcluster = dir->file.firstcluster;
|
|
|
|
update_short_entry(&dummyfile, 0, FAT_ATTR_DIRECTORY);
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
/* Set the firstcluster field in the direntry */
|
|
|
|
update_short_entry(&newdir->file, 0, FAT_ATTR_DIRECTORY);
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = flush_fat(IF_MV(fat_bpb));
|
2004-01-15 14:30:59 +00:00
|
|
|
if (rc < 0)
|
2004-01-15 19:05:49 +00:00
|
|
|
return rc * 10 - 5;
|
2004-01-15 14:30:59 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2004-08-17 01:45:48 +00:00
|
|
|
int fat_truncate(const struct fat_file *file)
|
2002-10-20 22:50:58 +00:00
|
|
|
{
|
2002-10-31 19:05:25 +00:00
|
|
|
/* truncate trailing clusters */
|
2005-01-23 23:20:40 +00:00
|
|
|
long next;
|
|
|
|
long last = file->lastcluster;
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[file->volume];
|
|
|
|
#endif
|
2002-11-11 14:40:18 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("fat_truncate(%lx, %lx)\n", file->firstcluster, last);
|
2002-11-11 14:40:18 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
for ( last = get_next_cluster(IF_MV2(fat_bpb,) last); last; last = next ) {
|
|
|
|
next = get_next_cluster(IF_MV2(fat_bpb,) last);
|
|
|
|
update_fat_entry(IF_MV2(fat_bpb,) last,0);
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2002-11-14 22:17:41 +00:00
|
|
|
if (file->lastcluster)
|
2004-12-28 22:16:07 +00:00
|
|
|
update_fat_entry(IF_MV2(fat_bpb,) file->lastcluster,FAT_EOF_MARK);
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-11-11 10:21:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
int fat_closewrite(struct fat_file *file, long size, int attr)
|
2002-11-11 10:21:51 +00:00
|
|
|
{
|
2003-10-26 11:16:15 +00:00
|
|
|
int rc;
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[file->volume];
|
|
|
|
#endif
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("fat_closewrite(size=%ld)\n",size);
|
2002-10-31 19:05:25 +00:00
|
|
|
|
|
|
|
if (!size) {
|
2002-10-31 16:09:28 +00:00
|
|
|
/* empty file */
|
2002-10-31 19:05:25 +00:00
|
|
|
if ( file->firstcluster ) {
|
2004-12-28 22:16:07 +00:00
|
|
|
update_fat_entry(IF_MV2(fat_bpb,) file->firstcluster, 0);
|
2002-10-31 19:05:25 +00:00
|
|
|
file->firstcluster = 0;
|
|
|
|
}
|
2002-10-31 16:09:28 +00:00
|
|
|
}
|
2002-10-31 19:05:25 +00:00
|
|
|
|
2003-10-28 22:23:33 +00:00
|
|
|
if (file->dircluster) {
|
2003-10-26 11:16:15 +00:00
|
|
|
rc = update_short_entry(file, size, attr);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 1;
|
2003-10-28 22:23:33 +00:00
|
|
|
}
|
2002-11-13 23:16:32 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
flush_fat(IF_MV(fat_bpb));
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-10-31 16:09:28 +00:00
|
|
|
#ifdef TEST_FAT
|
2002-11-11 10:21:51 +00:00
|
|
|
if ( file->firstcluster ) {
|
2002-10-31 16:09:28 +00:00
|
|
|
/* debug */
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[file->volume];
|
|
|
|
#else
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2005-01-23 23:20:40 +00:00
|
|
|
long count = 0;
|
|
|
|
long len;
|
|
|
|
long next;
|
2002-10-31 16:09:28 +00:00
|
|
|
for ( next = file->firstcluster; next;
|
2005-03-07 14:38:30 +00:00
|
|
|
next = get_next_cluster(IF_MV2(fat_bpb,) next) ) {
|
|
|
|
LDEBUGF("cluster %ld: %lx\n", count, next);
|
|
|
|
count++;
|
|
|
|
}
|
2006-12-04 21:37:22 +00:00
|
|
|
len = count * fat_bpb->bpb_secperclus * SECTOR_SIZE;
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("File is %ld clusters (chainlen=%ld, size=%ld)\n",
|
2002-10-31 16:09:28 +00:00
|
|
|
count, len, size );
|
2006-12-04 21:37:22 +00:00
|
|
|
if ( len > size + fat_bpb->bpb_secperclus * SECTOR_SIZE)
|
2002-10-31 16:09:28 +00:00
|
|
|
panicf("Cluster chain is too long\n");
|
2002-10-31 19:05:25 +00:00
|
|
|
if ( len < size )
|
|
|
|
panicf("Cluster chain is too short\n");
|
2002-10-31 16:09:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-10-22 15:06:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
static int free_direntries(struct fat_file* file)
|
2002-11-19 12:48:50 +00:00
|
|
|
{
|
2006-12-04 21:37:22 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE];
|
2002-11-19 12:48:50 +00:00
|
|
|
struct fat_file dir;
|
2004-12-28 22:16:07 +00:00
|
|
|
int numentries = file->direntries;
|
|
|
|
unsigned int entry = file->direntry - numentries + 1;
|
2002-11-19 12:48:50 +00:00
|
|
|
unsigned int sector = entry / DIR_ENTRIES_PER_SECTOR;
|
|
|
|
int i;
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
|
|
|
/* create a temporary file handle for the dir holding this file */
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = fat_open(IF_MV2(file->volume,) file->dircluster, &dir, NULL);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 1;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_seek( &dir, sector );
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 2;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_readwrite(&dir, 1, buf, false);
|
|
|
|
if (rc < 1)
|
|
|
|
return rc * 10 - 3;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
|
|
|
for (i=0; i < numentries; i++) {
|
|
|
|
LDEBUGF("Clearing dir entry %d (%d/%d)\n",
|
|
|
|
entry, i+1, numentries);
|
|
|
|
buf[(entry % DIR_ENTRIES_PER_SECTOR) * DIR_ENTRY_SIZE] = 0xe5;
|
|
|
|
entry++;
|
|
|
|
|
|
|
|
if ( (entry % DIR_ENTRIES_PER_SECTOR) == 0 ) {
|
|
|
|
/* flush this sector */
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_seek(&dir, sector);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 4;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_readwrite(&dir, 1, buf, true);
|
|
|
|
if (rc < 1)
|
|
|
|
return rc * 10 - 5;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
|
|
|
if ( i+1 < numentries ) {
|
|
|
|
/* read next sector */
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_readwrite(&dir, 1, buf, false);
|
|
|
|
if (rc < 1)
|
|
|
|
return rc * 10 - 6;
|
2002-11-19 12:48:50 +00:00
|
|
|
}
|
|
|
|
sector++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( entry % DIR_ENTRIES_PER_SECTOR ) {
|
|
|
|
/* flush this sector */
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_seek(&dir, sector);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 7;
|
2007-02-25 15:44:55 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = fat_readwrite(&dir, 1, buf, true);
|
|
|
|
if (rc < 1)
|
|
|
|
return rc * 10 - 8;
|
2002-11-19 12:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-22 15:06:08 +00:00
|
|
|
int fat_remove(struct fat_file* file)
|
|
|
|
{
|
2005-01-23 23:20:40 +00:00
|
|
|
long next, last = file->firstcluster;
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[file->volume];
|
|
|
|
#endif
|
2002-10-22 15:06:08 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("fat_remove(%lx)\n",last);
|
2002-10-22 15:06:08 +00:00
|
|
|
|
2002-11-01 15:26:06 +00:00
|
|
|
while ( last ) {
|
2004-12-28 22:16:07 +00:00
|
|
|
next = get_next_cluster(IF_MV2(fat_bpb,) last);
|
|
|
|
update_fat_entry(IF_MV2(fat_bpb,) last,0);
|
2002-10-22 15:06:08 +00:00
|
|
|
last = next;
|
|
|
|
}
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2003-02-26 03:02:49 +00:00
|
|
|
if ( file->dircluster ) {
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = free_direntries(file);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 1;
|
|
|
|
}
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
file->firstcluster = 0;
|
|
|
|
file->dircluster = 0;
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = flush_fat(IF_MV(fat_bpb));
|
2004-04-16 08:58:29 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 2;
|
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
int fat_rename(struct fat_file* file,
|
2004-08-22 11:28:24 +00:00
|
|
|
struct fat_dir* dir,
|
|
|
|
const unsigned char* newname,
|
2005-01-23 23:20:40 +00:00
|
|
|
long size,
|
2004-08-22 11:28:24 +00:00
|
|
|
int attr)
|
2002-11-19 12:48:50 +00:00
|
|
|
{
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2004-08-22 11:28:24 +00:00
|
|
|
struct fat_dir olddir;
|
2002-11-19 12:48:50 +00:00
|
|
|
struct fat_file newfile = *file;
|
2007-07-05 18:30:55 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE];
|
|
|
|
unsigned char* entry = NULL;
|
|
|
|
unsigned short* clusptr = NULL;
|
|
|
|
unsigned int parentcluster;
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[file->volume];
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2005-01-04 23:20:02 +00:00
|
|
|
if (file->volume != dir->file.volume) {
|
|
|
|
DEBUGF("No rename across volumes!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-07-05 18:30:55 +00:00
|
|
|
#else
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
2004-12-28 22:16:07 +00:00
|
|
|
#endif
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
if ( !file->dircluster ) {
|
2002-11-19 12:58:45 +00:00
|
|
|
DEBUGF("File has no dir cluster!\n");
|
2005-01-04 23:20:02 +00:00
|
|
|
return -2;
|
2002-11-19 12:48:50 +00:00
|
|
|
}
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
/* create a temporary file handle */
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = fat_opendir(IF_MV2(file->volume,) &olddir, file->dircluster, NULL);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
2007-07-05 18:30:55 +00:00
|
|
|
return rc * 10 - 1;
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
/* create new name */
|
2004-08-22 11:28:24 +00:00
|
|
|
rc = add_dir_entry(dir, &newfile, newname, false, false);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
2007-07-05 18:30:55 +00:00
|
|
|
return rc * 10 - 2;
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
/* write size and cluster link */
|
2003-02-26 03:02:49 +00:00
|
|
|
rc = update_short_entry(&newfile, size, attr);
|
|
|
|
if (rc < 0)
|
2007-07-05 18:30:55 +00:00
|
|
|
return rc * 10 - 3;
|
2002-11-18 11:58:43 +00:00
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
/* remove old name */
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = free_direntries(file);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
2007-07-05 18:30:55 +00:00
|
|
|
return rc * 10 - 4;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = flush_fat(IF_MV(fat_bpb));
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0)
|
2007-07-05 18:30:55 +00:00
|
|
|
return rc * 10 - 5;
|
|
|
|
|
|
|
|
/* if renaming a directory, update the .. entry to make sure
|
|
|
|
it points to its parent directory (we don't check if it was a move) */
|
|
|
|
if(FAT_ATTR_DIRECTORY == attr) {
|
|
|
|
/* open the dir that was renamed, we re-use the olddir struct */
|
|
|
|
rc = fat_opendir(IF_MV2(file->volume,) &olddir, newfile.firstcluster,
|
|
|
|
NULL);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 6;
|
|
|
|
|
|
|
|
/* get the first sector of the dir */
|
|
|
|
rc = fat_seek(&olddir.file, 0);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 7;
|
|
|
|
|
|
|
|
rc = fat_readwrite(&olddir.file, 1, buf, false);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 8;
|
|
|
|
|
|
|
|
/* parent cluster is 0 if parent dir is the root - FAT spec (p.29) */
|
|
|
|
if(dir->file.firstcluster == fat_bpb->bpb_rootclus)
|
|
|
|
parentcluster = 0;
|
|
|
|
else
|
|
|
|
parentcluster = dir->file.firstcluster;
|
|
|
|
|
|
|
|
entry = buf + DIR_ENTRY_SIZE;
|
|
|
|
if(strncmp(".. ", entry, 11))
|
|
|
|
{
|
|
|
|
/* .. entry must be second entry according to FAT spec (p.29) */
|
|
|
|
DEBUGF("Second dir entry is not double-dot!\n");
|
|
|
|
return rc * 10 - 9;
|
|
|
|
}
|
|
|
|
clusptr = (short*)(entry + FATDIR_FSTCLUSHI);
|
|
|
|
*clusptr = htole16(parentcluster >> 16);
|
|
|
|
|
|
|
|
clusptr = (short*)(entry + FATDIR_FSTCLUSLO);
|
|
|
|
*clusptr = htole16(parentcluster & 0xffff);
|
|
|
|
|
|
|
|
/* write back this sector */
|
|
|
|
rc = fat_seek(&olddir.file, 0);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 7;
|
|
|
|
|
|
|
|
rc = fat_readwrite(&olddir.file, 1, buf, true);
|
|
|
|
if (rc < 1)
|
|
|
|
return rc * 10 - 8;
|
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-04-27 19:37:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-02-26 21:18:05 +00:00
|
|
|
static long next_write_cluster(struct fat_file* file,
|
2005-01-23 23:20:40 +00:00
|
|
|
long oldcluster,
|
|
|
|
long* newsector)
|
2002-10-31 16:09:28 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[file->volume];
|
|
|
|
#else
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2005-01-23 23:20:40 +00:00
|
|
|
long cluster = 0;
|
|
|
|
long sector;
|
2002-10-31 16:09:28 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("next_write_cluster(%lx,%lx)\n",file->firstcluster, oldcluster);
|
2002-10-31 16:09:28 +00:00
|
|
|
|
2002-10-31 19:05:25 +00:00
|
|
|
if (oldcluster)
|
2004-12-28 22:16:07 +00:00
|
|
|
cluster = get_next_cluster(IF_MV2(fat_bpb,) oldcluster);
|
2002-10-31 19:05:25 +00:00
|
|
|
|
2002-10-31 16:09:28 +00:00
|
|
|
if (!cluster) {
|
2004-09-23 21:43:43 +00:00
|
|
|
if (oldcluster > 0)
|
2004-12-28 22:16:07 +00:00
|
|
|
cluster = find_free_cluster(IF_MV2(fat_bpb,) oldcluster+1);
|
2004-09-23 21:43:43 +00:00
|
|
|
else if (oldcluster == 0)
|
2007-10-28 23:44:49 +00:00
|
|
|
cluster = find_free_cluster(IF_MV2(fat_bpb,)
|
|
|
|
fat_bpb->fsinfo.nextfree);
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
|
|
|
else /* negative, pseudo-cluster of the root dir */
|
|
|
|
return 0; /* impossible to append something to the root */
|
|
|
|
#endif
|
2002-10-31 16:09:28 +00:00
|
|
|
|
|
|
|
if (cluster) {
|
|
|
|
if (oldcluster)
|
2007-10-28 23:44:49 +00:00
|
|
|
update_fat_entry(IF_MV2(fat_bpb,) oldcluster, cluster);
|
2002-10-31 16:09:28 +00:00
|
|
|
else
|
|
|
|
file->firstcluster = cluster;
|
2004-12-28 22:16:07 +00:00
|
|
|
update_fat_entry(IF_MV2(fat_bpb,) cluster, FAT_EOF_MARK);
|
2002-10-31 16:09:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-10-31 19:05:25 +00:00
|
|
|
#ifdef TEST_FAT
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->fsinfo.freecount>0)
|
2002-10-31 19:05:25 +00:00
|
|
|
panicf("There is free space, but find_free_cluster() "
|
|
|
|
"didn't find it!\n");
|
|
|
|
#endif
|
|
|
|
DEBUGF("next_write_cluster(): Disk full!\n");
|
2002-10-31 16:09:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-12-28 22:16:07 +00:00
|
|
|
sector = cluster2sec(IF_MV2(fat_bpb,) cluster);
|
2002-10-31 16:09:28 +00:00
|
|
|
if (sector<0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*newsector = sector;
|
|
|
|
return cluster;
|
|
|
|
}
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
static int transfer(IF_MV2(struct bpb* fat_bpb,)
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long start, long count, char* buf, bool write )
|
2002-10-31 19:05:25 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2002-10-31 19:05:25 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("transfer(s=%lx, c=%lx, %s)\n",
|
2004-12-28 22:16:07 +00:00
|
|
|
start+ fat_bpb->startsector, count, write?"write":"read");
|
2002-11-12 20:02:23 +00:00
|
|
|
if (write) {
|
2005-01-23 23:20:40 +00:00
|
|
|
unsigned long firstallowed;
|
2004-09-23 21:43:43 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
2004-12-28 22:16:07 +00:00
|
|
|
if (fat_bpb->is_fat16)
|
|
|
|
firstallowed = fat_bpb->rootdirsector;
|
2004-09-23 21:43:43 +00:00
|
|
|
else
|
|
|
|
#endif
|
2004-12-28 22:16:07 +00:00
|
|
|
firstallowed = fat_bpb->firstdatasector;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2006-12-04 21:37:22 +00:00
|
|
|
if (start < firstallowed)
|
2005-02-25 18:50:16 +00:00
|
|
|
panicf("Write %ld before data\n", firstallowed - start);
|
2006-12-04 21:37:22 +00:00
|
|
|
if (start + count > fat_bpb->totalsectors)
|
2005-02-25 18:50:16 +00:00
|
|
|
panicf("Write %ld after data\n",
|
2004-12-28 22:16:07 +00:00
|
|
|
start + count - fat_bpb->totalsectors);
|
2010-04-03 22:02:09 +00:00
|
|
|
rc = storage_write_sectors(IF_MD2(fat_bpb->drive,)
|
2004-12-28 22:16:07 +00:00
|
|
|
start + fat_bpb->startsector, count, buf);
|
2002-11-12 20:02:23 +00:00
|
|
|
}
|
2002-10-31 19:05:25 +00:00
|
|
|
else
|
2010-04-03 22:02:09 +00:00
|
|
|
rc = storage_read_sectors(IF_MD2(fat_bpb->drive,)
|
2004-12-28 22:16:07 +00:00
|
|
|
start + fat_bpb->startsector, count, buf);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0) {
|
2005-01-23 23:20:40 +00:00
|
|
|
DEBUGF( "transfer() - Couldn't %s sector %lx"
|
2002-10-31 19:05:25 +00:00
|
|
|
" (error code %d)\n",
|
2003-02-26 03:02:49 +00:00
|
|
|
write ? "write":"read", start, rc);
|
2003-04-24 18:33:32 +00:00
|
|
|
return rc;
|
2002-10-31 19:05:25 +00:00
|
|
|
}
|
2003-04-24 18:33:32 +00:00
|
|
|
return 0;
|
2002-10-31 19:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-26 21:18:05 +00:00
|
|
|
long fat_readwrite( struct fat_file *file, long sectorcount,
|
2002-10-31 16:09:28 +00:00
|
|
|
void* buf, bool write )
|
2002-10-30 16:16:47 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[file->volume];
|
|
|
|
#else
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2005-01-23 23:20:40 +00:00
|
|
|
long cluster = file->lastcluster;
|
|
|
|
long sector = file->lastsector;
|
|
|
|
long clusternum = file->clusternum;
|
|
|
|
long numsec = file->sectornum;
|
2002-11-13 23:16:32 +00:00
|
|
|
bool eof = file->eof;
|
2005-01-23 23:20:40 +00:00
|
|
|
long first=0, last=0;
|
|
|
|
long i;
|
2003-04-24 18:33:32 +00:00
|
|
|
int rc;
|
2002-10-30 16:16:47 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF( "fat_readwrite(file:%lx,count:0x%lx,buf:%lx,%s)\n",
|
|
|
|
file->firstcluster,sectorcount,(long)buf,write?"write":"read");
|
|
|
|
LDEBUGF( "fat_readwrite: sec=%lx numsec=%ld eof=%d\n",
|
2002-11-13 23:16:32 +00:00
|
|
|
sector,numsec, eof?1:0);
|
|
|
|
|
|
|
|
if ( eof && !write)
|
2002-10-30 16:16:47 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* find sequential sectors and write them all at once */
|
2002-10-31 19:05:25 +00:00
|
|
|
for (i=0; (i < sectorcount) && (sector > -1); i++ ) {
|
2002-10-30 16:16:47 +00:00
|
|
|
numsec++;
|
2005-01-23 23:20:40 +00:00
|
|
|
if ( numsec > (long)fat_bpb->bpb_secperclus || !cluster ) {
|
|
|
|
long oldcluster = cluster;
|
2007-10-10 23:26:17 +00:00
|
|
|
long oldsector = sector;
|
|
|
|
long oldnumsec = numsec;
|
2002-10-31 16:09:28 +00:00
|
|
|
if (write)
|
|
|
|
cluster = next_write_cluster(file, cluster, §or);
|
|
|
|
else {
|
2004-12-28 22:16:07 +00:00
|
|
|
cluster = get_next_cluster(IF_MV2(fat_bpb,) cluster);
|
|
|
|
sector = cluster2sec(IF_MV2(fat_bpb,) cluster);
|
2002-10-31 16:09:28 +00:00
|
|
|
}
|
2003-06-03 18:04:22 +00:00
|
|
|
|
|
|
|
clusternum++;
|
|
|
|
numsec=1;
|
|
|
|
|
2002-10-31 19:05:25 +00:00
|
|
|
if (!cluster) {
|
2002-11-13 23:16:32 +00:00
|
|
|
eof = true;
|
2002-10-31 19:05:25 +00:00
|
|
|
if ( write ) {
|
2007-10-28 23:44:49 +00:00
|
|
|
/* remember last cluster, in case
|
2002-11-13 23:16:32 +00:00
|
|
|
we want to append to the file */
|
2007-10-10 23:26:17 +00:00
|
|
|
sector = oldsector;
|
2002-10-31 19:05:25 +00:00
|
|
|
cluster = oldcluster;
|
2007-10-10 23:26:17 +00:00
|
|
|
numsec = oldnumsec;
|
2003-06-03 18:04:22 +00:00
|
|
|
clusternum--;
|
2003-06-19 12:03:36 +00:00
|
|
|
i = -1; /* Error code */
|
|
|
|
break;
|
2002-10-31 19:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-13 23:16:32 +00:00
|
|
|
else
|
|
|
|
eof = false;
|
2002-10-31 16:09:28 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (sector)
|
|
|
|
sector++;
|
|
|
|
else {
|
2002-10-31 19:05:25 +00:00
|
|
|
/* look up first sector of file */
|
2004-12-28 22:16:07 +00:00
|
|
|
sector = cluster2sec(IF_MV2(fat_bpb,) file->firstcluster);
|
2002-10-31 16:09:28 +00:00
|
|
|
numsec=1;
|
2005-01-03 07:59:49 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
|
|
|
if (file->firstcluster < 0)
|
|
|
|
{ /* FAT16 root dir */
|
|
|
|
sector += fat_bpb->rootdiroffset;
|
|
|
|
numsec += fat_bpb->rootdiroffset;
|
|
|
|
}
|
|
|
|
#endif
|
2002-10-30 16:16:47 +00:00
|
|
|
}
|
2002-10-31 16:09:28 +00:00
|
|
|
}
|
2002-10-30 16:16:47 +00:00
|
|
|
|
2002-10-31 16:09:28 +00:00
|
|
|
if (!first)
|
|
|
|
first = sector;
|
2002-10-30 16:16:47 +00:00
|
|
|
|
2002-10-31 16:09:28 +00:00
|
|
|
if ( ((sector != first) && (sector != last+1)) || /* not sequential */
|
2006-12-04 21:37:22 +00:00
|
|
|
(last-first+1 == 256) ) { /* max 256 sectors per ata request */
|
2005-01-23 23:20:40 +00:00
|
|
|
long count = last - first + 1;
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = transfer(IF_MV2(fat_bpb,) first, count, buf, write );
|
2003-04-24 18:33:32 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 1;
|
2002-10-31 19:05:25 +00:00
|
|
|
|
2006-12-04 21:37:22 +00:00
|
|
|
buf = (char *)buf + count * SECTOR_SIZE;
|
2002-10-31 19:05:25 +00:00
|
|
|
first = sector;
|
|
|
|
}
|
2002-11-04 13:52:47 +00:00
|
|
|
|
2002-11-13 23:16:32 +00:00
|
|
|
if ((i == sectorcount-1) && /* last sector requested */
|
|
|
|
(!eof))
|
|
|
|
{
|
2005-01-23 23:20:40 +00:00
|
|
|
long count = sector - first + 1;
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = transfer(IF_MV2(fat_bpb,) first, count, buf, write );
|
2003-04-24 18:33:32 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 2;
|
2002-10-30 16:16:47 +00:00
|
|
|
}
|
2002-10-31 19:05:25 +00:00
|
|
|
|
2002-10-31 16:09:28 +00:00
|
|
|
last = sector;
|
2002-10-30 16:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file->lastcluster = cluster;
|
|
|
|
file->lastsector = sector;
|
2003-06-03 18:04:22 +00:00
|
|
|
file->clusternum = clusternum;
|
2002-10-30 16:16:47 +00:00
|
|
|
file->sectornum = numsec;
|
2002-11-13 23:16:32 +00:00
|
|
|
file->eof = eof;
|
2002-10-30 16:16:47 +00:00
|
|
|
|
2002-10-31 19:05:25 +00:00
|
|
|
/* if eof, don't report last block as read/written */
|
2002-11-13 23:16:32 +00:00
|
|
|
if (eof)
|
2002-10-31 19:05:25 +00:00
|
|
|
i--;
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
DEBUGF("Sectors written: %ld\n", i);
|
2002-10-31 19:05:25 +00:00
|
|
|
return i;
|
2002-10-30 16:16:47 +00:00
|
|
|
}
|
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
int fat_seek(struct fat_file *file, unsigned long seeksector )
|
2002-04-27 20:03:05 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[file->volume];
|
|
|
|
#else
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2005-01-23 23:20:40 +00:00
|
|
|
long clusternum=0, numclusters=0, sectornum=0, sector=0;
|
|
|
|
long cluster = file->firstcluster;
|
|
|
|
long i;
|
2002-04-27 20:03:05 +00:00
|
|
|
|
2005-01-03 07:59:49 +00:00
|
|
|
#ifdef HAVE_FAT16SUPPORT
|
|
|
|
if (cluster < 0) /* FAT16 root dir */
|
|
|
|
seeksector += fat_bpb->rootdiroffset;
|
|
|
|
#endif
|
|
|
|
|
2002-11-13 23:16:32 +00:00
|
|
|
file->eof = false;
|
2002-11-04 14:59:46 +00:00
|
|
|
if (seeksector) {
|
|
|
|
/* we need to find the sector BEFORE the requested, since
|
|
|
|
the file struct stores the last accessed sector */
|
|
|
|
seeksector--;
|
2004-12-28 22:16:07 +00:00
|
|
|
numclusters = clusternum = seeksector / fat_bpb->bpb_secperclus;
|
|
|
|
sectornum = seeksector % fat_bpb->bpb_secperclus;
|
2002-11-13 23:16:32 +00:00
|
|
|
|
2003-06-03 18:04:22 +00:00
|
|
|
if (file->clusternum && clusternum >= file->clusternum)
|
|
|
|
{
|
|
|
|
cluster = file->lastcluster;
|
|
|
|
numclusters -= file->clusternum;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<numclusters; i++) {
|
2004-12-28 22:16:07 +00:00
|
|
|
cluster = get_next_cluster(IF_MV2(fat_bpb,) cluster);
|
2002-11-04 14:59:46 +00:00
|
|
|
if (!cluster) {
|
2002-11-15 11:20:33 +00:00
|
|
|
DEBUGF("Seeking beyond the end of the file! "
|
2005-01-23 23:20:40 +00:00
|
|
|
"(sector %ld, cluster %ld)\n", seeksector, i);
|
2002-11-13 23:16:32 +00:00
|
|
|
return -1;
|
2002-11-04 14:59:46 +00:00
|
|
|
}
|
2002-04-27 20:03:05 +00:00
|
|
|
}
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
sector = cluster2sec(IF_MV2(fat_bpb,) cluster) + sectornum;
|
2002-04-27 20:03:05 +00:00
|
|
|
}
|
2002-11-13 23:16:32 +00:00
|
|
|
else {
|
2002-11-05 14:46:27 +00:00
|
|
|
sectornum = -1;
|
2002-11-13 23:16:32 +00:00
|
|
|
}
|
2002-10-31 20:41:36 +00:00
|
|
|
|
2005-01-23 23:20:40 +00:00
|
|
|
LDEBUGF("fat_seek(%lx, %lx) == %lx, %lx, %lx\n",
|
2002-11-13 23:16:32 +00:00
|
|
|
file->firstcluster, seeksector, cluster, sector, sectornum);
|
2002-10-31 20:41:36 +00:00
|
|
|
|
2002-10-22 15:06:08 +00:00
|
|
|
file->lastcluster = cluster;
|
|
|
|
file->lastsector = sector;
|
2003-06-03 18:04:22 +00:00
|
|
|
file->clusternum = clusternum;
|
2002-10-31 20:41:36 +00:00
|
|
|
file->sectornum = sectornum + 1;
|
2002-04-27 20:03:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-04-27 19:37:41 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
int fat_opendir(IF_MV2(int volume,)
|
2005-01-23 23:20:40 +00:00
|
|
|
struct fat_dir *dir, unsigned long startcluster,
|
2004-08-17 01:45:48 +00:00
|
|
|
const struct fat_dir *parent_dir)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[volume];
|
|
|
|
/* fixme: remove error check when done */
|
|
|
|
if (volume >= NUM_VOLUMES || !fat_bpbs[volume].mounted)
|
|
|
|
{
|
|
|
|
LDEBUGF("fat_open() illegal volume %d\n", volume);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[0];
|
|
|
|
#endif
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-11-13 23:16:32 +00:00
|
|
|
if (startcluster == 0)
|
2005-01-03 07:59:49 +00:00
|
|
|
startcluster = fat_bpb->bpb_rootclus;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
rc = fat_open(IF_MV2(volume,) startcluster, &dir->file, parent_dir);
|
2003-02-26 03:02:49 +00:00
|
|
|
if(rc)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-11-13 23:16:32 +00:00
|
|
|
DEBUGF( "fat_opendir() - Couldn't open dir"
|
2003-02-26 03:02:49 +00:00
|
|
|
" (error code %d)\n", rc);
|
|
|
|
return rc * 10 - 1;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2010-03-15 12:31:24 +00:00
|
|
|
|
|
|
|
/* assign them after fat_open call so that fat_opendir can be called with the same
|
|
|
|
* fat_dir as parent and result */
|
|
|
|
dir->entry = 0;
|
|
|
|
dir->sector = 0;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-10-20 22:50:58 +00:00
|
|
|
bool done = false;
|
2010-03-22 10:35:25 +00:00
|
|
|
int i, j;
|
2003-02-26 03:02:49 +00:00
|
|
|
int rc;
|
2010-03-22 10:35:25 +00:00
|
|
|
int order;
|
2002-03-28 15:09:10 +00:00
|
|
|
unsigned char firstbyte;
|
2006-07-31 22:59:45 +00:00
|
|
|
/* Long file names are stored in special entries. Each entry holds
|
2010-03-22 10:35:25 +00:00
|
|
|
up to 13 characters. Names can be max 255 chars (not bytes!) long */
|
|
|
|
/* The number of long entries in the long name can be retrieve from the first
|
|
|
|
* long entry because there are stored in reverse order and have an ordinal */
|
|
|
|
int nb_longs = 0;
|
|
|
|
/* The long entries are expected to be in order, so remember the last ordinal */
|
|
|
|
int last_long_ord = 0;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-11-18 11:58:43 +00:00
|
|
|
dir->entrycount = 0;
|
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
while(!done)
|
|
|
|
{
|
2007-01-29 23:35:21 +00:00
|
|
|
if ( !(dir->entry % DIR_ENTRIES_PER_SECTOR) || !dir->sector )
|
2002-11-15 11:20:33 +00:00
|
|
|
{
|
2010-03-22 10:35:25 +00:00
|
|
|
rc = fat_readwrite(&dir->file, 1, dir->sectorcache, false);
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc == 0) {
|
2002-11-15 11:20:33 +00:00
|
|
|
/* eof */
|
|
|
|
entry->name[0] = 0;
|
|
|
|
break;
|
|
|
|
}
|
2003-02-26 03:02:49 +00:00
|
|
|
if (rc < 0) {
|
2002-11-15 11:20:33 +00:00
|
|
|
DEBUGF( "fat_getnext() - Couldn't read dir"
|
2003-02-26 03:02:49 +00:00
|
|
|
" (error code %d)\n", rc);
|
|
|
|
return rc * 10 - 1;
|
2002-11-15 11:20:33 +00:00
|
|
|
}
|
|
|
|
dir->sector = dir->file.lastsector;
|
|
|
|
}
|
|
|
|
|
2007-01-29 23:35:21 +00:00
|
|
|
for (i = dir->entry % DIR_ENTRIES_PER_SECTOR;
|
2010-03-22 10:35:25 +00:00
|
|
|
i < DIR_ENTRIES_PER_SECTOR; i++) {
|
2002-11-15 11:20:33 +00:00
|
|
|
unsigned int entrypos = i * DIR_ENTRY_SIZE;
|
|
|
|
|
2010-03-22 10:35:25 +00:00
|
|
|
firstbyte = dir->sectorcache[entrypos];
|
2002-11-15 11:20:33 +00:00
|
|
|
dir->entry++;
|
2002-04-27 22:32:37 +00:00
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
if (firstbyte == 0xe5) {
|
2002-04-27 22:32:37 +00:00
|
|
|
/* free entry */
|
2002-11-18 11:58:43 +00:00
|
|
|
dir->entrycount = 0;
|
2002-03-28 15:09:10 +00:00
|
|
|
continue;
|
2002-07-15 23:39:06 +00:00
|
|
|
}
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
if (firstbyte == 0) {
|
2002-04-27 22:32:37 +00:00
|
|
|
/* last entry */
|
2002-05-03 15:35:51 +00:00
|
|
|
entry->name[0] = 0;
|
2002-11-18 11:58:43 +00:00
|
|
|
dir->entrycount = 0;
|
2002-05-03 15:35:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-11-18 11:58:43 +00:00
|
|
|
dir->entrycount++;
|
|
|
|
|
2010-03-22 10:35:25 +00:00
|
|
|
/* LFN entry? */
|
|
|
|
if ( ( dir->sectorcache[entrypos + FATDIR_ATTR] &
|
2002-04-27 22:32:37 +00:00
|
|
|
FAT_ATTR_LONG_NAME_MASK ) == FAT_ATTR_LONG_NAME ) {
|
2010-03-22 10:35:25 +00:00
|
|
|
/* extract ordinal */
|
|
|
|
order = dir->sectorcache[entrypos + FATLONG_ORDER] & ~FATLONG_LAST_LONG_ENTRY;
|
|
|
|
/* is this entry the first long entry ? (first in order but containing last part) */
|
|
|
|
if (dir->sectorcache[entrypos + FATLONG_ORDER] & FATLONG_LAST_LONG_ENTRY) {
|
|
|
|
/* check that order is not too big ! (and non-zero) */
|
|
|
|
if(order <= 0 || order > FATLONG_MAX_ORDER)
|
|
|
|
continue; /* ignore the whole LFN, will trigger lots of warnings */
|
|
|
|
nb_longs = order;
|
|
|
|
last_long_ord = order;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* check orphan entry */
|
|
|
|
if (nb_longs == 0) {
|
|
|
|
logf("fat warning: orphan LFN entry");
|
|
|
|
/* ignore */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check order */
|
|
|
|
if (order != (last_long_ord - 1)) {
|
|
|
|
logf("fat warning: wrong LFN ordinal");
|
|
|
|
/* ignore the whole LFN, will trigger lots of warnings */
|
|
|
|
nb_longs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_long_ord = order;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy part, reuse [order] for another purpose :) */
|
|
|
|
order = (order - 1) * FATLONG_NAME_BYTES_PER_ENTRY;
|
|
|
|
for(j = 0; j < FATLONG_NAME_CHUNKS; j++) {
|
|
|
|
memcpy(dir->longname + order,
|
|
|
|
dir->sectorcache + entrypos + FATLONG_NAME_POS[j],
|
|
|
|
FATLONG_NAME_SIZE[j]);
|
|
|
|
order += FATLONG_NAME_SIZE[j];
|
|
|
|
}
|
2002-04-27 22:32:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-03-22 10:35:25 +00:00
|
|
|
if ( parse_direntry(entry, dir->sectorcache + entrypos) ) {
|
2002-04-27 22:32:37 +00:00
|
|
|
|
2002-06-19 17:31:35 +00:00
|
|
|
/* don't return volume id entry */
|
2006-05-13 21:47:27 +00:00
|
|
|
if ( (entry->attr &
|
|
|
|
(FAT_ATTR_VOLUME_ID|FAT_ATTR_DIRECTORY))
|
|
|
|
== FAT_ATTR_VOLUME_ID)
|
2002-06-19 17:31:35 +00:00
|
|
|
continue;
|
|
|
|
|
2002-04-27 22:32:37 +00:00
|
|
|
/* replace shortname with longname? */
|
2010-03-22 10:35:25 +00:00
|
|
|
/* check that the long name is complete */
|
|
|
|
if (nb_longs != 0 && last_long_ord == 1) {
|
|
|
|
/* hold a copy of the shortname in case the long one is too long */
|
2006-07-31 22:59:45 +00:00
|
|
|
unsigned char shortname[13]; /* 8+3+dot+\0 */
|
|
|
|
int longname_utf8len = 0;
|
2010-03-22 10:35:25 +00:00
|
|
|
/* One character at a time, add 1 for trailing \0, 4 is the maximum size
|
|
|
|
* of a UTF8 encoded character in rockbox */
|
|
|
|
unsigned char longname_utf8segm[4 + 1];
|
|
|
|
unsigned short ucs;
|
|
|
|
int segm_utf8len;
|
|
|
|
/* Temporarily store short name */
|
2007-10-28 23:44:49 +00:00
|
|
|
strcpy(shortname, entry->name);
|
2006-07-31 22:59:45 +00:00
|
|
|
entry->name[0] = 0;
|
2007-10-28 23:44:49 +00:00
|
|
|
|
2010-03-22 10:35:25 +00:00
|
|
|
/* Convert the FAT name to a utf8-encoded one.
|
|
|
|
* The name is not necessary NUL-terminated ! */
|
|
|
|
for (j = 0; j < nb_longs * FATLONG_NAME_BYTES_PER_ENTRY; j += 2) {
|
|
|
|
ucs = dir->longname[j] | (dir->longname[j + 1] << 8);
|
|
|
|
if(ucs == 0 || ucs == FAT_LONGNAME_PAD_UCS)
|
2006-07-31 22:59:45 +00:00
|
|
|
break;
|
2010-03-22 10:35:25 +00:00
|
|
|
/* utf8encode will return a pointer after the converted
|
|
|
|
* string, subtract the pointer to the start to get the length of it */
|
|
|
|
segm_utf8len = utf8encode(ucs, longname_utf8segm) - longname_utf8segm;
|
|
|
|
|
|
|
|
/* warn the trailing zero ! (FAT_FILENAME_BYTES includes it) */
|
|
|
|
if (longname_utf8len + segm_utf8len >= FAT_FILENAME_BYTES) {
|
|
|
|
/* force use of short name */
|
|
|
|
longname_utf8len = FAT_FILENAME_BYTES + 1;
|
|
|
|
break; /* fallback later */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
longname_utf8segm[segm_utf8len] = 0;
|
|
|
|
strcat(entry->name + longname_utf8len, longname_utf8segm);
|
|
|
|
longname_utf8len += segm_utf8len;
|
|
|
|
}
|
2002-04-27 22:32:37 +00:00
|
|
|
}
|
2006-07-31 22:59:45 +00:00
|
|
|
|
|
|
|
/* Does the utf8-encoded name fit into the entry? */
|
2010-03-22 10:35:25 +00:00
|
|
|
/* warn the trailing zero ! (FAT_FILENAME_BYTES includes it) */
|
2006-07-31 22:59:45 +00:00
|
|
|
if (longname_utf8len >= FAT_FILENAME_BYTES) {
|
2007-10-28 23:44:49 +00:00
|
|
|
/* Take the short DOS name. Need to utf8-encode it
|
|
|
|
since it may contain chars from the upper half of
|
|
|
|
the OEM code page which wouldn't be a valid utf8.
|
|
|
|
Beware: this file will be shown with strange
|
|
|
|
glyphs in file browser since unicode 0x80 to 0x9F
|
|
|
|
are control characters. */
|
2006-07-31 22:59:45 +00:00
|
|
|
logf("SN-DOS: %s", shortname);
|
|
|
|
unsigned char *utf8;
|
2007-10-28 23:44:49 +00:00
|
|
|
utf8 = iso_decode(shortname, entry->name, -1,
|
|
|
|
strlen(shortname));
|
2010-03-22 10:35:25 +00:00
|
|
|
*utf8 = 0;
|
2006-07-31 22:59:45 +00:00
|
|
|
logf("SN: %s", entry->name);
|
2007-01-29 23:35:21 +00:00
|
|
|
} else {
|
2010-03-22 10:35:25 +00:00
|
|
|
logf("LN: %s", entry->name);
|
|
|
|
logf("LNLen: %d", longname_utf8len);
|
2006-07-31 22:59:45 +00:00
|
|
|
}
|
2002-04-27 22:32:37 +00:00
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
done = true;
|
2002-11-15 11:20:33 +00:00
|
|
|
i++;
|
2002-04-27 22:32:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2004-04-06 00:17:02 +00:00
|
|
|
|
2005-02-26 21:18:05 +00:00
|
|
|
unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume))
|
2004-12-28 22:16:07 +00:00
|
|
|
{
|
|
|
|
#ifndef HAVE_MULTIVOLUME
|
|
|
|
const int volume = 0;
|
|
|
|
#endif
|
|
|
|
struct bpb* fat_bpb = &fat_bpbs[volume];
|
2006-12-04 21:37:22 +00:00
|
|
|
return fat_bpb->bpb_secperclus * SECTOR_SIZE;
|
2004-12-28 22:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
bool fat_ismounted(int volume)
|
2004-04-06 00:17:02 +00:00
|
|
|
{
|
2004-12-28 22:16:07 +00:00
|
|
|
return (volume<NUM_VOLUMES && fat_bpbs[volume].mounted);
|
2004-04-06 00:17:02 +00:00
|
|
|
}
|
2004-12-28 22:16:07 +00:00
|
|
|
#endif
|