ctr_firmware/trunk/firmware/build/libraries/fatfs/common/src/drfile.c
(no author) fab593a113 (shirait)
firmwareビルド環境の設定、fatfsライブラリ追加。

git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_firmware@253 b871894f-2f95-9b40-918c-086798483c85
2009-02-04 12:47:09 +00:00

331 lines
10 KiB
C

/*---------------------------------------------------------------------------*
Project: TWL - for RTFS
File: drfile.c
Copyright 2006-2008 Nintendo. All rights reserved.
These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law. They may
not be disclosed to third parties or copied or duplicated in any form,
in whole or in part, without the prior written consent of Nintendo.
$Date:: #$
$Rev$
$Author$
*---------------------------------------------------------------------------*/
#include <brom.h>
#include <rtfs_target_os.h>
#include <rtfs.h>
#include <rtfsconf.h>
#if (RTFS_DEBUG_PRINT_ON == 1)
// #define PRINTDEBUG OS_TPrintf
#else
// #define PRINTDEBUG( ...) ((void)0)
#endif
// #define PRINTDEBUG OS_TPrintf
#define PRINTDEBUG( ...) ((void)0)
/*---------------------------------------------------------------------------*
global変数
*---------------------------------------------------------------------------*/
PCFD fileDescList[26] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
u32 file_capacity;
u32 file_adjusted_capacity;
u16 file_heads;
u16 file_secptrack;
u16 file_cylinders;
/*---------------------------------------------------------------------------*
extern変数
*---------------------------------------------------------------------------*/
extern int rtfs_first_stat_flag[26];
/*---------------------------------------------------------------------------*
static関数
*---------------------------------------------------------------------------*/
static void file_get_CHS_params( u32 file_sector_num);
/*---------------------------------------------------------------------------*
Name: fileRtfsIo
Description: 上位層からのセクタリード/ライト要求を受ける
Arguments: driveno : ドライブ番号
block : 開始ブロック番号
buffer :
count : ブロック数
reading : リード要求時にTRUE
Returns: TRUE/FALSE
*---------------------------------------------------------------------------*/
BOOL fileRtfsIo( int driveno, dword block, void* buffer, word count, BOOLEAN reading)
{
int result=0;
int i, n_driveno;
unsigned long dmy;
n_driveno = (driveno & (~RTFS_FILL_OPERATION_FLAG));
po_ulseek( fileDescList[n_driveno], block*512, &dmy, PSEEK_SET);
if( reading) {
PRINTDEBUG( "DEVCTL_IO_READ ... block:%x, count:%x -> buf:%x\n", block, count, buffer);
result = po_read( fileDescList[n_driveno], buffer, 512*count);
}else{
if( driveno & RTFS_FILL_OPERATION_FLAG) {
PRINTDEBUG( "DEVCTL_IO_FILL ... block:%x, count:%x <- buf:%x\n", block, count, buffer);
for( i=0; i<count; i++) {
result = po_write( fileDescList[n_driveno], buffer, 512);
}
}else{
PRINTDEBUG( "DEVCTL_IO_WRITE ... block:%x, count:%x <- buf:%x\n", block, count, buffer);
result = po_write( fileDescList[n_driveno], buffer, 512*count);
}
}
if( result < 0) {
return FALSE;
}else{
return TRUE;
}
}
/*---------------------------------------------------------------------------*
Name: fileRtfsCtrl
Description: 上位層からのコントロール要求を受ける
Arguments: driveno : ドライブ番号
opcode : 要求の種類
pargs :
Returns:
*---------------------------------------------------------------------------*/
int fileRtfsCtrl( int driveno, int opcode, void* pargs)
{
DDRIVE *pdr;
DEV_GEOMETRY gc;
int heads, secptrack;
unsigned long file_size, dmy;
pdr = pc_drno_to_drive_struct( driveno);
switch( opcode) {
case DEVCTL_GET_GEOMETRY:
PRINTDEBUG( "%s : DEVCTL_GET_GEOMETRY\n", __FUNCTION__);
rtfs_memset( &gc, (byte)0, sizeof(gc));
po_ulseek( fileDescList[driveno], 0, &dmy, PSEEK_SET);
po_ulseek( fileDescList[driveno], 0, &file_size, PSEEK_END);
po_ulseek( fileDescList[driveno], 0, &dmy, PSEEK_SET);
file_capacity = file_size / 512; //セクタ単位にする
file_get_CHS_params( file_capacity);
PRINTDEBUG( "capacity : 0x%x\n", file_adjusted_capacity);
PRINTDEBUG( "heads : 0x%x\n", file_heads);
PRINTDEBUG( "cylinders : 0x%x\n", file_cylinders);
PRINTDEBUG( "secptrack : 0x%x\n", file_secptrack);
PRINTDEBUG( "file size : 0x%x\n", file_size);
PRINTDEBUG( "file capacity : 0x%x\n", file_capacity);
PRINTDEBUG( "file adj capacity : 0x%x\n", file_adjusted_capacity);
gc.dev_geometry_lbas = file_adjusted_capacity;
gc.dev_geometry_heads = file_heads;
gc.dev_geometry_cylinders = file_cylinders;
gc.dev_geometry_secptrack = file_secptrack;
/**/
gc.fmt_parms_valid = FALSE;
OSAPI_CPUCOPY8( &gc, pargs, sizeof(gc));
return( 0);
case DEVCTL_FORMAT:
{
/* u32 filebuf[512/4];
miCpuFill8( filebuf, 0, 512);
po_write( fileDescList[driveno], filebuf, 512);*/
PRINTDEBUG( "%s : DEVCTL_FORMAT\n", __FUNCTION__);
return( 0);
}
case DEVCTL_REPORT_REMOVE:
pdr->drive_flags &= ~DRIVE_FLAGS_INSERTED;
return( 0);
case DEVCTL_CHECKSTATUS:
PRINTDEBUG( "%s : DEVCTL_CHECKSTATUS\n", __FUNCTION__);
if (!(pdr->drive_flags & DRIVE_FLAGS_REMOVABLE)) { //リムーバブルでない場合
return(DEVTEST_NOCHANGE); //fix
}else{ //リムーバブルの場合
if( rtfs_first_stat_flag[driveno]) {
rtfs_first_stat_flag[driveno] = 0;
PRINTDEBUG( "CHANGED!\n");
pdr->drive_flags |= DRIVE_FLAGS_INSERTED;
return(DEVTEST_CHANGED);
}
pdr->drive_flags |= DRIVE_FLAGS_INSERTED;
return( DEVTEST_NOCHANGE);
}
case DEVCTL_WARMSTART:
PRINTDEBUG( "%s : DEVCTL_WARMSTART\n", __FUNCTION__);
pdr->drive_flags |= (DRIVE_FLAGS_VALID | DRIVE_FLAGS_REMOVABLE);
return( 0);
case DEVCTL_POWER_RESTORE:
PRINTDEBUG( "%s : DEVCTL_POWER_RESTORE\n", __FUNCTION__);
break;
case DEVCTL_POWER_LOSS:
PRINTDEBUG( "%s : DEVCTL_POWER_LOSS\n", __FUNCTION__);
break;
default:
PRINTDEBUG( "%s : DEVCTL_unknown\n", __FUNCTION__);
break;
}
return( 0);
}
/*SD File System Specification(仕様書)に基づいた値を出す*/
static void file_get_CHS_params( u32 file_sector_num)
{
int mbytes;
// mbytes = (sdmc_current_spec.card_capacity / (1024 * 1024)) * 512;
mbytes = (file_sector_num >> 11);
while( 1) {
#if 0
if( file_sector_num < 512) { // < 256k
file_heads = 1;
file_secptrack = 4;
break;
}
if( file_sector_num < 2048) { // < 1MB
file_heads = 1;
file_secptrack = 8;
break;
}
if( mbytes <= 8) { // <= 8MB
file_heads = 2;
file_secptrack = 16;
break;
}
#else
if( mbytes <= 2) {
file_heads = 2;
file_secptrack = 16;
break;
}
#endif
if( mbytes <= 16) {
file_heads = 2;
file_secptrack = 32;
break;
}
if( mbytes <= 32) {
file_heads = 4;
file_secptrack = 32;
break;
}
if( mbytes <= 128) {
file_heads = 8;
file_secptrack = 32;
break;
}
if( mbytes <= 256) {
file_heads = 16;
file_secptrack = 32;
break;
}
if( mbytes <= 504) {
file_heads = 16;
file_secptrack = 63;
break;
}
if( mbytes <= 1008) {
file_heads = 32;
file_secptrack = 63;
break;
}
if( mbytes <= 2016) {
file_heads = 64;
file_secptrack = 63;
break;
}
if( mbytes <= 2048) {
file_heads = 128;
file_secptrack = 63;
break;
}
if( mbytes <= 4032) {
file_heads = 128;
file_secptrack = 63;
break;
}
if( mbytes <= 32768) {
file_heads = 255;
file_secptrack = 63;
break;
}
}
/*シリンダ数を計算*/
file_cylinders = (file_sector_num / (file_heads * file_secptrack));
/*memory_capacityを再計算してadjusted_memory_capacityに格納*/
file_adjusted_capacity = file_cylinders * (file_heads * file_secptrack);
}
/*---------------------------------------------------------------------------*
Name: fileRtfsAttach
Description: fileドライバをドライブに割り当てる
Arguments: driveno : ドライブ番号
Returns:
*---------------------------------------------------------------------------*/
BOOL fileRtfsAttach( PCFD fileDesc, int driveno)
{
BOOLEAN result;
DDRIVE pdr;
pdr.dev_table_drive_io = fileRtfsIo;
pdr.dev_table_perform_device_ioctl = fileRtfsCtrl;
pdr.register_file_address = (dword) 0; /* Not used */
pdr.interrupt_number = 0; /* Not used */
pdr.drive_flags = DRIVE_FLAGS_VALID;
pdr.partition_number = 0; /* Not used */
pdr.pcmcia_slot_number = 0; /* Not used */
pdr.controller_number = 0;
pdr.logical_unit_number = 0;
fileDescList[driveno] = fileDesc; /* file descripter セット */
result = rtfs_attach( driveno, &pdr, "FILE"); //構造体がFSライブラリ側にコピーされる
/**/
if( !result) {
PRINTDEBUG( "fsEnableDevice failured\n");
}
return( result);
}