2009-10-01 01:10:58 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <ogc/lwp_watchdog.h>
|
|
|
|
#include <ogc/mutex.h>
|
|
|
|
#include <ogc/system.h>
|
|
|
|
#include <ogc/usbstorage.h>
|
|
|
|
#include <sdcard/wiisd_io.h>
|
2009-12-19 15:05:31 +01:00
|
|
|
#include <locale.h>
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-02-22 22:29:47 +01:00
|
|
|
#include "usbloader/usbstorage2.h"
|
2010-02-25 13:08:03 +01:00
|
|
|
#include "usbloader/sdhc.h"
|
2009-12-19 15:05:31 +01:00
|
|
|
#include "usbloader/wbfs.h"
|
|
|
|
#include "libntfs/ntfs.h"
|
2010-09-19 17:12:16 +02:00
|
|
|
#include "libfat/fat.h"
|
2009-12-11 00:05:32 +01:00
|
|
|
#include "gecko.h"
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
//these are the only stable and speed is good
|
2010-05-30 08:49:23 +02:00
|
|
|
#define CACHE 32
|
2009-10-01 01:10:58 +02:00
|
|
|
#define SECTORS 64
|
2009-12-19 15:05:31 +01:00
|
|
|
#define SECTORS_SD 32
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-11-15 20:52:58 +01:00
|
|
|
#define MOUNT_NONE 0
|
|
|
|
#define MOUNT_SD 1
|
|
|
|
#define MOUNT_SDHC 2
|
|
|
|
|
2010-02-15 03:45:18 +01:00
|
|
|
#define DEBUG_FAT
|
|
|
|
|
2009-12-19 15:05:31 +01:00
|
|
|
/* Disc interfaces */
|
|
|
|
extern const DISC_INTERFACE __io_sdhc;
|
|
|
|
|
|
|
|
void _FAT_mem_init();
|
2009-11-15 20:52:58 +01:00
|
|
|
extern sec_t _FAT_startSector;
|
|
|
|
|
2009-12-19 15:05:31 +01:00
|
|
|
extern s32 wbfsDev;
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int fat_sd_mount = MOUNT_NONE;
|
2009-11-15 20:52:58 +01:00
|
|
|
sec_t fat_sd_sec = 0; // u32
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int fat_usb_mount = 0;
|
2009-11-15 20:52:58 +01:00
|
|
|
sec_t fat_usb_sec = 0;
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int fat_wbfs_mount = 0;
|
2009-11-15 20:52:58 +01:00
|
|
|
sec_t fat_wbfs_sec = 0;
|
2009-10-13 23:19:23 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int fs_ntfs_mount = 0;
|
2009-12-19 15:05:31 +01:00
|
|
|
sec_t fs_ntfs_sec = 0;
|
2009-12-11 00:05:32 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int USBDevice_Init()
|
|
|
|
{
|
2010-02-15 03:45:18 +01:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf("USBDevice_Init()");
|
2010-02-15 03:45:18 +01:00
|
|
|
#endif
|
2010-09-19 01:16:05 +02:00
|
|
|
//closing all open Files write back the cache and then shutdown em!
|
2010-09-24 02:48:03 +02:00
|
|
|
fatUnmount("USB:/");
|
2009-10-01 01:10:58 +02:00
|
|
|
//right now mounts first FAT-partition
|
2009-11-15 20:52:58 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
//try first mount with cIOS
|
2010-09-24 02:48:03 +02:00
|
|
|
// if (!fatMount("USB", &__io_wiiums, 0, CACHE, SECTORS)) {
|
|
|
|
// //try now mount with libogc
|
|
|
|
if (!fatMount("USB", &__io_usbstorage2, 0, CACHE, SECTORS))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-02-15 03:45:18 +01:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf(":-1\n");
|
2010-02-15 03:45:18 +01:00
|
|
|
#endif
|
2010-09-19 01:16:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
// }
|
2010-02-25 13:08:03 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
fat_usb_mount = 1;
|
|
|
|
fat_usb_sec = _FAT_startSector;
|
2010-02-15 03:45:18 +01:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf(":0\n");
|
2010-02-15 03:45:18 +01:00
|
|
|
#endif
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void USBDevice_deInit()
|
|
|
|
{
|
2010-02-15 03:45:18 +01:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf("USBDevice_deInit(): ");
|
2010-02-15 03:45:18 +01:00
|
|
|
#endif
|
2009-10-01 01:10:58 +02:00
|
|
|
//closing all open Files write back the cache and then shutdown em!
|
2010-09-24 02:48:03 +02:00
|
|
|
fatUnmount("USB:/");
|
2009-11-15 20:52:58 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
fat_usb_mount = 0;
|
|
|
|
fat_usb_sec = 0;
|
2010-09-19 12:53:24 +02:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf("ok\n");
|
2010-09-19 12:53:24 +02:00
|
|
|
#endif
|
2009-11-15 20:52:58 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int WBFSDevice_Init(u32 sector)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-11-15 20:52:58 +01:00
|
|
|
//closing all open Files write back the cache and then shutdown em!
|
2010-09-24 02:48:03 +02:00
|
|
|
fatUnmount("WBFS:/");
|
2009-11-15 20:52:58 +01:00
|
|
|
//right now mounts first FAT-partition
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
//try first mount with cIOS
|
2010-09-24 02:48:03 +02:00
|
|
|
// if (!fatMount("WBFS", &__io_wiiums, 0, CACHE, SECTORS)) {
|
2010-09-19 01:16:05 +02:00
|
|
|
//try now mount with libogc
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!fatMount("WBFS", &__io_usbstorage2, 0, CACHE, SECTORS))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
// }
|
2010-09-19 01:16:05 +02:00
|
|
|
|
|
|
|
fat_wbfs_mount = 1;
|
|
|
|
fat_wbfs_sec = _FAT_startSector;
|
2010-09-24 02:48:03 +02:00
|
|
|
if (sector && fat_wbfs_sec != sector)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
// This is an error situation...actually, but is ignored in Config loader also
|
|
|
|
// Should ask Oggzee about it...
|
|
|
|
}
|
|
|
|
return 0;
|
2009-11-15 20:52:58 +01:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void WBFSDevice_deInit()
|
|
|
|
{
|
2009-11-15 20:52:58 +01:00
|
|
|
//closing all open Files write back the cache and then shutdown em!
|
2010-09-24 02:48:03 +02:00
|
|
|
fatUnmount("WBFS:/");
|
2009-11-15 20:52:58 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
fat_wbfs_mount = 0;
|
|
|
|
fat_wbfs_sec = 0;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int isInserted(const char *path)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!strncmp(path, "USB:", 4)) return 1;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-10-13 23:19:23 +02:00
|
|
|
return __io_sdhc.isInserted() || __io_wiisd.isInserted();
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
2009-11-15 20:52:58 +01:00
|
|
|
|
2010-09-19 15:21:36 +02:00
|
|
|
static u8 sdIsInited = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
int SDCard_Init()
|
|
|
|
{
|
2010-02-15 03:45:18 +01:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf("SDCard_Init()");
|
2010-02-15 03:45:18 +01:00
|
|
|
#endif
|
2009-10-01 01:10:58 +02:00
|
|
|
//closing all open Files write back the cache and then shutdown em!
|
2010-09-24 02:48:03 +02:00
|
|
|
fatUnmount("SD:/");
|
2009-10-01 01:10:58 +02:00
|
|
|
//right now mounts first FAT-partition
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fatMount("SD", &__io_wiisd, 0, CACHE, SECTORS))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
fat_sd_mount = MOUNT_SD;
|
|
|
|
fat_sd_sec = _FAT_startSector;
|
2010-09-24 02:48:03 +02:00
|
|
|
sdIsInited = 1;
|
2010-02-15 03:45:18 +01:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf(":1\n");
|
2010-02-15 03:45:18 +01:00
|
|
|
#endif
|
2010-09-19 01:16:05 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
else if (fatMount("SD", &__io_sdhc, 0, CACHE, SDHC_SECTOR_SIZE))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
fat_sd_mount = MOUNT_SDHC;
|
|
|
|
fat_sd_sec = _FAT_startSector;
|
2010-09-24 02:48:03 +02:00
|
|
|
sdIsInited = 1;
|
2010-02-15 03:45:18 +01:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf(":1\n");
|
2010-02-15 03:45:18 +01:00
|
|
|
#endif
|
2010-09-19 01:16:05 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2010-02-15 03:45:18 +01:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf(":-1\n");
|
2010-02-15 03:45:18 +01:00
|
|
|
#endif
|
2010-09-19 01:16:05 +02:00
|
|
|
return -1;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void SDCard_deInit()
|
|
|
|
{
|
2010-02-15 03:45:18 +01:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf("SDCard_deInit( %d ): ", sdIsInited);
|
2010-02-15 03:45:18 +01:00
|
|
|
#endif
|
2009-10-01 01:10:58 +02:00
|
|
|
//closing all open Files write back the cache and then shutdown em!
|
2010-09-24 02:48:03 +02:00
|
|
|
if (sdIsInited)
|
2010-09-19 15:21:36 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
fatUnmount("SD:/");
|
2009-11-15 22:30:44 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
fat_sd_mount = MOUNT_NONE;
|
|
|
|
fat_sd_sec = 0;
|
|
|
|
sdIsInited = 0;
|
2010-09-19 15:21:36 +02:00
|
|
|
}
|
2010-09-19 12:53:24 +02:00
|
|
|
#ifdef DEBUG_FAT
|
2010-09-24 02:48:03 +02:00
|
|
|
gprintf("ok\n");
|
2010-09-19 12:53:24 +02:00
|
|
|
#endif
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
2009-12-19 15:05:31 +01:00
|
|
|
|
|
|
|
void ntfsInit();
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 MountNTFS(u32 sector)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
s32 ret;
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (fs_ntfs_mount) return 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
//printf("mounting NTFS\n");
|
|
|
|
//Wpad_WaitButtons();
|
|
|
|
_FAT_mem_init();
|
|
|
|
|
|
|
|
ntfsInit(); // Call ntfs init here, to prevent locale resets
|
|
|
|
|
|
|
|
// ntfsInit resets locale settings
|
|
|
|
// which breaks unicode in console
|
|
|
|
// so we change it back to C-UTF-8
|
2010-09-24 02:48:03 +02:00
|
|
|
setlocale(LC_CTYPE, "C-UTF-8");
|
|
|
|
setlocale(LC_MESSAGES, "C-UTF-8");
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (wbfsDev == WBFS_DEVICE_USB)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
/* Initialize WBFS interface */
|
2010-09-24 02:48:03 +02:00
|
|
|
// if (!__io_wiiums.startup()) {
|
2010-09-19 01:16:05 +02:00
|
|
|
ret = __io_usbstorage2.startup();
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!ret)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
// }
|
2010-09-19 01:16:05 +02:00
|
|
|
/* Mount device */
|
2010-09-24 02:48:03 +02:00
|
|
|
// if (!ntfsMount("NTFS", &__io_wiiums, sector, CACHE, SECTORS, NTFS_SHOW_HIDDEN_FILES | NTFS_RECOVER)) {
|
|
|
|
ret = ntfsMount("NTFS", &__io_usbstorage2, sector, CACHE, SECTORS, NTFS_SHOW_HIDDEN_FILES | NTFS_RECOVER);
|
|
|
|
if (!ret)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
return -2;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
// }
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
else if (wbfsDev == WBFS_DEVICE_SDHC)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (sdhc_mode_sd == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
ret = ntfsMount("NTFS", &__io_sdhc, 0, CACHE, SECTORS, NTFS_SHOW_HIDDEN_FILES | NTFS_RECOVER);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
ret = ntfsMount("NTFS", &__io_sdhc, 0, CACHE, SECTORS_SD, NTFS_SHOW_HIDDEN_FILES | NTFS_RECOVER);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!ret)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs_ntfs_mount = 1;
|
|
|
|
fs_ntfs_sec = sector; //_FAT_startSector;
|
|
|
|
|
|
|
|
return 0;
|
2009-12-19 15:05:31 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 UnmountNTFS(void)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
/* Unmount device */
|
2010-09-24 02:48:03 +02:00
|
|
|
ntfsUnmount("NTFS:/", true);
|
2009-12-19 15:05:31 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
fs_ntfs_mount = 0;
|
|
|
|
fs_ntfs_sec = 0;
|
2009-12-19 15:05:31 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2009-12-19 15:05:31 +01:00
|
|
|
}
|
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
void _FAT_mem_init()
|
|
|
|
{
|
2009-12-19 15:05:31 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
void* _FAT_mem_allocate(size_t size)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
return malloc(size);
|
2009-12-19 15:05:31 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
void* _FAT_mem_align(size_t size)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
return memalign(32, size);
|
2009-12-19 15:05:31 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
void _FAT_mem_free(void *mem)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
free(mem);
|
2009-12-19 15:05:31 +01:00
|
|
|
}
|