363 lines
9.4 KiB
C
363 lines
9.4 KiB
C
/*
|
|
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
|
|
*
|
|
* 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.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
* MA 02110-1301, USA.
|
|
*/
|
|
|
|
/************************************************************************/
|
|
/* */
|
|
/* PROJECT : exFAT & FAT12/16/32 File System */
|
|
/* FILE : fatent.c */
|
|
/* PURPOSE : sdFAT FAT entry manager */
|
|
/* */
|
|
/*----------------------------------------------------------------------*/
|
|
/* NOTES */
|
|
/* */
|
|
/* */
|
|
/************************************************************************/
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
#include "sdfat.h"
|
|
#include "core.h"
|
|
|
|
/*----------------------------------------------------------------------*/
|
|
/* Global Variable Definitions */
|
|
/*----------------------------------------------------------------------*/
|
|
/* All buffer structures are protected w/ fsi->v_sem */
|
|
|
|
/*----------------------------------------------------------------------*/
|
|
/* Static functions */
|
|
/*----------------------------------------------------------------------*/
|
|
|
|
/*======================================================================*/
|
|
/* FAT Read/Write Functions */
|
|
/*======================================================================*/
|
|
/* in : sb, loc
|
|
* out: content
|
|
* returns 0 on success, -1 on error
|
|
*/
|
|
static s32 exfat_ent_get(struct super_block *sb, u32 loc, u32 *content)
|
|
{
|
|
u32 sec, off, _content;
|
|
u8 *fat_sector;
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
|
|
/* fsi->vol_type == EXFAT */
|
|
sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
|
|
off = (loc << 2) & (u32)(sb->s_blocksize - 1);
|
|
|
|
fat_sector = fcache_getblk(sb, sec);
|
|
if (!fat_sector)
|
|
return -EIO;
|
|
|
|
// _content = get_unaligned_le32(&fat_sector[off]);
|
|
_content = le32_to_cpu(*(__le32*)(&fat_sector[off]));
|
|
if (_content >= CLUSTER_32(0xFFFFFFF8)) {
|
|
//return 0xFFFFFFFF to simplify code
|
|
*content = CLUS_EOF;
|
|
return 0;
|
|
}
|
|
|
|
*content = CLUSTER_32(_content);
|
|
return 0;
|
|
}
|
|
|
|
static s32 exfat_ent_set(struct super_block *sb, u32 loc, u32 content)
|
|
{
|
|
u32 sec, off;
|
|
u8 *fat_sector;
|
|
// u8 *fat_entry;
|
|
__le32 *fat_entry;
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
|
|
sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
|
|
off = (loc << 2) & (u32)(sb->s_blocksize - 1);
|
|
|
|
fat_sector = fcache_getblk(sb, sec);
|
|
if (!fat_sector)
|
|
return -EIO;
|
|
|
|
// fat_entry = &(fat_sector[off]);
|
|
// put_unaligned_le32(content, fat_entry);
|
|
fat_entry = (__le32 *)&(fat_sector[off]);
|
|
*fat_entry = cpu_to_le32(content);
|
|
|
|
return fcache_modify(sb, sec);
|
|
}
|
|
|
|
static s32 fat32_ent_get(struct super_block *sb, u32 loc, u32 *content)
|
|
{
|
|
u32 sec, off, _content;
|
|
u8 *fat_sector;
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
|
|
sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
|
|
off = (loc << 2) & (u32)(sb->s_blocksize - 1);
|
|
|
|
fat_sector = fcache_getblk(sb, sec);
|
|
if (!fat_sector)
|
|
return -EIO;
|
|
|
|
// _content = get_unaligned_le32(&fat_sector[off]);
|
|
_content = le32_to_cpu(*(__le32*)(&fat_sector[off]));
|
|
_content &= 0x0FFFFFFF;
|
|
|
|
if (_content >= CLUSTER_32(0x0FFFFFF8)) {
|
|
//return 0xFFFFFFFF to simplify code
|
|
*content = CLUS_EOF;
|
|
return 0;
|
|
}
|
|
|
|
*content = CLUSTER_32(_content);
|
|
return 0;
|
|
}
|
|
|
|
static s32 fat32_ent_set(struct super_block *sb, u32 loc, u32 content)
|
|
{
|
|
u32 sec, off;
|
|
u8 *fat_sector;
|
|
// u8 *fat_entry;
|
|
__le32 *fat_entry;
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
|
|
content &= 0x0FFFFFFF;
|
|
|
|
sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
|
|
off = (loc << 2) & (u32)(sb->s_blocksize - 1);
|
|
|
|
fat_sector = fcache_getblk(sb, sec);
|
|
if (!fat_sector)
|
|
return -EIO;
|
|
|
|
// fat_entry = &(fat_sector[off]);
|
|
// content |= get_unaligned_le32(fat_entry) & 0xF0000000;
|
|
// put_unaligned_le32(content, fat_entry);
|
|
fat_entry = (__le32 *)&(fat_sector[off]);
|
|
content |= (le32_to_cpu(*fat_entry) & 0xF0000000);
|
|
*fat_entry = cpu_to_le32(content);
|
|
|
|
return fcache_modify(sb, sec);
|
|
}
|
|
|
|
static s32 fat16_ent_get(struct super_block *sb, u32 loc, u32 *content)
|
|
{
|
|
u32 sec, off, _content;
|
|
u8 *fat_sector;
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
|
|
sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-1));
|
|
off = (loc << 1) & (u32)(sb->s_blocksize - 1);
|
|
|
|
fat_sector = fcache_getblk(sb, sec);
|
|
if(!fat_sector)
|
|
return -EIO;
|
|
|
|
//_content = get_unaligned_le16(&fat_sector[off]);
|
|
_content = (u32)le16_to_cpu(*(__le16*)(&fat_sector[off]));
|
|
_content &= 0x0000FFFF;
|
|
|
|
if (_content >= CLUSTER_16(0xFFF8)) {
|
|
// return 0x0FFFFFFF to simplify code
|
|
*content = CLUS_EOF;
|
|
return 0;
|
|
}
|
|
|
|
*content = CLUSTER_32(_content);
|
|
return 0;
|
|
}
|
|
|
|
static s32 fat16_ent_set(struct super_block *sb, u32 loc, u32 content)
|
|
{
|
|
u32 sec, off;
|
|
u8 *fat_sector;
|
|
//u8 *fat_entry;
|
|
__le16 *fat_entry;
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
|
|
content &= 0x0000FFFF;
|
|
|
|
sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-1));
|
|
off = (loc << 1) & (u32)(sb->s_blocksize - 1);
|
|
|
|
fat_sector = fcache_getblk(sb, sec);
|
|
if (!fat_sector)
|
|
return -EIO;
|
|
|
|
//fat_entry = &(fat_sector[off]);
|
|
//put_unaligned_le16(content, fat_entry);
|
|
fat_entry = (__le16*)&(fat_sector[off]);
|
|
*fat_entry = cpu_to_le16(content);
|
|
|
|
return fcache_modify(sb, sec);
|
|
}
|
|
|
|
static s32 fat12_ent_get(struct super_block *sb, u32 loc, u32 *content)
|
|
{
|
|
u32 sec, off, _content;
|
|
u8 *fat_sector;
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
|
|
sec = fsi->FAT1_start_sector + ((loc + (loc >> 1)) >> sb->s_blocksize_bits);
|
|
off = (loc + (loc >> 1)) & (u32)(sb->s_blocksize - 1);
|
|
|
|
fat_sector = fcache_getblk(sb, sec);
|
|
if (!fat_sector)
|
|
return -EIO;
|
|
|
|
if (off == (u32)(sb->s_blocksize - 1)) {
|
|
_content = (u32) fat_sector[off];
|
|
|
|
fat_sector = fcache_getblk(sb, ++sec);
|
|
if (!fat_sector)
|
|
return -EIO;
|
|
|
|
_content |= (u32) fat_sector[0] << 8;
|
|
} else {
|
|
_content = get_unaligned_le16(&fat_sector[off]);
|
|
}
|
|
|
|
if (loc & 1) _content >>= 4;
|
|
|
|
_content &= 0x00000FFF;
|
|
|
|
if (_content >= CLUSTER_16(0x0FF8)) {
|
|
/* return 0xFFFFFFFF to simplify code */
|
|
*content = CLUS_EOF;
|
|
return 0;
|
|
}
|
|
|
|
*content = CLUSTER_32(_content);
|
|
return 0;
|
|
}
|
|
|
|
static s32 fat12_ent_set(struct super_block *sb, u32 loc, u32 content)
|
|
{
|
|
u32 sec, off;
|
|
u8 *fat_sector, *fat_entry;
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
|
|
content &= 0x00000FFF;
|
|
|
|
sec = fsi->FAT1_start_sector + ((loc + (loc >> 1)) >> sb->s_blocksize_bits);
|
|
off = (loc + (loc >> 1)) & (u32)(sb->s_blocksize - 1);
|
|
|
|
fat_sector = fcache_getblk(sb, sec);
|
|
if (!fat_sector)
|
|
return -EIO;
|
|
|
|
if (loc & 1) { /* odd */
|
|
|
|
content <<= 4;
|
|
|
|
if (off == (u32)(sb->s_blocksize-1)) {
|
|
fat_sector[off] = (u8)(content | (fat_sector[off] & 0x0F));
|
|
if (fcache_modify(sb, sec))
|
|
return -EIO;
|
|
|
|
fat_sector = fcache_getblk(sb, ++sec);
|
|
if (!fat_sector)
|
|
return -EIO;
|
|
|
|
fat_sector[0] = (u8)(content >> 8);
|
|
} else {
|
|
fat_entry = &(fat_sector[off]);
|
|
content |= 0x000F &
|
|
get_unaligned_le16(fat_entry);
|
|
|
|
put_unaligned_le16(content, fat_entry);
|
|
}
|
|
} else { /* even */
|
|
fat_sector[off] = (u8)(content);
|
|
|
|
if (off == (u32)(sb->s_blocksize-1)) {
|
|
fat_sector[off] = (u8)(content);
|
|
if (fcache_modify(sb, sec))
|
|
return -EIO;
|
|
|
|
fat_sector = fcache_getblk(sb, ++sec);
|
|
fat_sector[0] = (u8)((fat_sector[0] & 0xF0) | (content >> 8));
|
|
} else {
|
|
fat_entry = &(fat_sector[off]);
|
|
content |= 0xF000 &
|
|
get_unaligned_le16(fat_entry);
|
|
|
|
put_unaligned_le16(content, fat_entry);
|
|
}
|
|
}
|
|
return fcache_modify(sb, sec);
|
|
}
|
|
|
|
|
|
static FATENT_OPS_T fat12_ent_ops = {
|
|
fat12_ent_get,
|
|
fat12_ent_set
|
|
};
|
|
|
|
static FATENT_OPS_T fat16_ent_ops = {
|
|
fat16_ent_get,
|
|
fat16_ent_set
|
|
};
|
|
|
|
static FATENT_OPS_T fat32_ent_ops = {
|
|
fat32_ent_get,
|
|
fat32_ent_set
|
|
};
|
|
|
|
static FATENT_OPS_T exfat_ent_ops = {
|
|
exfat_ent_get,
|
|
exfat_ent_set
|
|
};
|
|
|
|
s32 fat_ent_ops_init(struct super_block *sb)
|
|
{
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
|
|
switch (fsi->vol_type) {
|
|
case EXFAT:
|
|
fsi->fatent_ops = &exfat_ent_ops;
|
|
break;
|
|
case FAT32:
|
|
fsi->fatent_ops = &fat32_ent_ops;
|
|
break;
|
|
case FAT16:
|
|
fsi->fatent_ops = &fat16_ent_ops;
|
|
break;
|
|
case FAT12:
|
|
fsi->fatent_ops = &fat12_ent_ops;
|
|
break;
|
|
default:
|
|
fsi->fatent_ops = NULL;
|
|
EMSG("Unknown volume type : %d", (int)fsi->vol_type);
|
|
return -ENOTSUPP;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
s32 fat_ent_get(struct super_block *sb, u32 loc, u32 *content)
|
|
{
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
return fsi->fatent_ops->ent_get(sb, loc, content);
|
|
}
|
|
|
|
s32 fat_ent_set(struct super_block *sb, u32 loc, u32 content)
|
|
{
|
|
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
|
|
return fsi->fatent_ops->ent_set(sb, loc, content);
|
|
}
|
|
|
|
/* end of fatent.c */
|