2011-02-02 19:30:15 +01:00
|
|
|
#include <vector>
|
2010-09-17 17:15:21 +02:00
|
|
|
#include <ogcsys.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2010-12-31 00:49:22 +01:00
|
|
|
#include "Controls/DeviceHandler.hpp"
|
2010-09-17 17:15:21 +02:00
|
|
|
#include "usbloader/usbstorage2.h"
|
|
|
|
#include "wbfs.h"
|
|
|
|
#include "usbloader/wbfs/wbfs_base.h"
|
|
|
|
#include "usbloader/wbfs/wbfs_wbfs.h"
|
|
|
|
#include "usbloader/wbfs/wbfs_fat.h"
|
|
|
|
#include "usbloader/wbfs/wbfs_ntfs.h"
|
2010-12-09 21:57:35 +01:00
|
|
|
#include "usbloader/wbfs/wbfs_ext.h"
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-18 13:46:25 +02:00
|
|
|
#include "usbloader/GameList.h"
|
2010-10-27 21:50:48 +02:00
|
|
|
#include "menu/menus.h"
|
2010-09-17 17:15:21 +02:00
|
|
|
#include "gecko.h"
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
#define VALID(x) (x >= 0 && x < (int) WbfsList.size() && WbfsList[x] != NULL)
|
|
|
|
|
|
|
|
static std::vector<Wbfs *> WbfsList;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
wbfs_disc_t* WBFS_OpenDisc(u8 *discid)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-03 22:46:54 +01:00
|
|
|
if(!discid) return NULL;
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
int part = gameList.GetPartitionNumber(discid);
|
2011-02-05 22:06:52 +01:00
|
|
|
if(!VALID(part))
|
|
|
|
return NULL;
|
2011-02-02 19:30:15 +01:00
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part);
|
|
|
|
|
|
|
|
return WbfsList[part]->OpenDisc(discid);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
void WBFS_CloseDisc(wbfs_disc_t *disc)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
if(!disc) return;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2011-02-04 16:25:27 +01:00
|
|
|
struct discHdr * header = (struct discHdr *) disc->header;
|
|
|
|
int part_num = gameList.GetPartitionNumber(header->id);
|
|
|
|
if(!VALID(part_num))
|
|
|
|
return;
|
2011-02-03 22:46:54 +01:00
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
|
|
|
|
2011-02-04 16:25:27 +01:00
|
|
|
WbfsList[part_num]->CloseDisc(disc);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 WBFS_Init(u32 device)
|
2010-09-18 13:46:25 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
return Wbfs::Init(device);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
s32 WBFS_OpenAll()
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
PartitionHandle * usbHandle = DeviceHandler::Instance()->GetUSBHandle();
|
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
for(int i = 0; i < usbHandle->GetPartitionTotalCount(); ++i)
|
2011-02-02 19:30:15 +01:00
|
|
|
{
|
|
|
|
if(WBFS_OpenPart(i) == 0)
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-31 00:49:22 +01:00
|
|
|
s32 WBFS_OpenPart(int part_num)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-12-31 00:49:22 +01:00
|
|
|
PartitionHandle * usbHandle = DeviceHandler::Instance()->GetUSBHandle();
|
2011-02-05 22:06:52 +01:00
|
|
|
if(part_num < 0 || part_num >= usbHandle->GetPartitionTotalCount())
|
2010-12-31 00:49:22 +01:00
|
|
|
return -1;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2011-02-06 10:34:06 +01:00
|
|
|
//! No need to switch ports on other partitions than WBFS
|
|
|
|
//! the open() function does not actually read from drive there.
|
|
|
|
if(strncmp(usbHandle->GetFSName(part_num), "WBFS", 4) == 0)
|
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
2011-02-05 22:06:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// close
|
2011-02-02 19:30:15 +01:00
|
|
|
WBFS_Close(part_num);
|
|
|
|
|
2011-02-03 22:46:54 +01:00
|
|
|
if(part_num >= (int) WbfsList.size())
|
2011-02-02 19:30:15 +01:00
|
|
|
WbfsList.resize(part_num+1);
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2011-02-05 11:36:55 +01:00
|
|
|
gprintf("\tWBFS_OpenPart: filesystem: %s, start sector %u, sector count: %u\n", usbHandle->GetFSName(part_num), usbHandle->GetLBAStart(part_num), usbHandle->GetSecCount(part_num));
|
2010-12-31 00:49:22 +01:00
|
|
|
|
|
|
|
if (strncmp(usbHandle->GetFSName(part_num), "FAT", 3) == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-05 22:06:52 +01:00
|
|
|
WbfsList[part_num] = new Wbfs_Fat(usbHandle->GetLBAStart(part_num), usbHandle->GetSecCount(part_num), part_num);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-12-31 00:49:22 +01:00
|
|
|
else if (strncmp(usbHandle->GetFSName(part_num), "NTFS", 4) == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-05 22:06:52 +01:00
|
|
|
WbfsList[part_num] = new Wbfs_Ntfs(usbHandle->GetLBAStart(part_num), usbHandle->GetSecCount(part_num), part_num);
|
2010-12-09 21:57:35 +01:00
|
|
|
}
|
2010-12-31 00:49:22 +01:00
|
|
|
else if (strncmp(usbHandle->GetFSName(part_num), "LINUX", 5) == 0)
|
2010-12-09 21:57:35 +01:00
|
|
|
{
|
2011-02-05 22:06:52 +01:00
|
|
|
WbfsList[part_num] = new Wbfs_Ext(usbHandle->GetLBAStart(part_num), usbHandle->GetSecCount(part_num), part_num);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-12-31 00:49:22 +01:00
|
|
|
else if (strncmp(usbHandle->GetFSName(part_num), "WBFS", 4) == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-05 22:06:52 +01:00
|
|
|
WbfsList[part_num] = new Wbfs_Wbfs(usbHandle->GetLBAStart(part_num), usbHandle->GetSecCount(part_num), part_num);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2011-01-08 13:35:41 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
if (WbfsList[part_num]->Open() != 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
delete WbfsList[part_num];
|
|
|
|
WbfsList[part_num] = NULL;
|
2010-09-19 01:16:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
bool WBFS_Close(int part_num)
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
if(!VALID(part_num))
|
|
|
|
return false;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2011-02-06 10:34:06 +01:00
|
|
|
//! No need to switch ports on other partitions than WBFS
|
|
|
|
//! the close() function does not actually write to drive there.
|
|
|
|
if(WbfsList[part_num]->GetFSType() == PART_FS_WBFS)
|
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
2011-02-05 22:06:52 +01:00
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
delete WbfsList[part_num];
|
|
|
|
WbfsList[part_num] = NULL;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
gameList.RemovePartition(part_num);
|
|
|
|
|
|
|
|
return true;
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
void WBFS_CloseAll()
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2011-02-04 16:25:27 +01:00
|
|
|
gameList.clear();
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
for(u32 i = 0; i < WbfsList.size(); ++i)
|
|
|
|
WBFS_Close(i);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 WBFS_Format(u32 lba, u32 size)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
Wbfs_Wbfs Part(WBFS_MIN_DEVICE, lba, size);
|
2011-01-09 11:45:29 +01:00
|
|
|
|
|
|
|
return Part.Format();
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
s32 WBFS_GetCount(int part_num, u32 *count)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
if(!VALID(part_num))
|
|
|
|
return -1;
|
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
|
|
|
int ret = WbfsList[part_num]->GetCount(count);
|
|
|
|
|
|
|
|
return ret;
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
s32 WBFS_GetHeaders(int part_num, struct discHdr *outbuf, u32 cnt, u32 len)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
if(!VALID(part_num))
|
|
|
|
return -1;
|
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[part_num]->GetHeaders(outbuf, cnt, len);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 WBFS_CheckGame(u8 *discid)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
int part_num = gameList.GetPartitionNumber(discid);
|
|
|
|
if(!VALID(part_num))
|
2011-02-04 16:25:27 +01:00
|
|
|
return 0;
|
2011-02-02 19:30:15 +01:00
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[part_num]->CheckGame(discid);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 WBFS_AddGame(void)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
if(!VALID(Settings.partition))
|
|
|
|
return -1;
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(Settings.partition);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[Settings.partition]->AddGame();
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 WBFS_RemoveGame(u8 *discid)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
int part_num = gameList.GetPartitionNumber(discid);
|
|
|
|
if(!VALID(part_num))
|
|
|
|
return -1;
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[part_num]->RemoveGame(discid);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 WBFS_GameSize(u8 *discid, f32 *size)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
int part_num = gameList.GetPartitionNumber(discid);
|
|
|
|
if(!VALID(part_num))
|
|
|
|
return -1;
|
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[part_num]->GameSize(discid, size);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 WBFS_DiskSpace(f32 *used, f32 *free)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
if(!VALID(Settings.partition))
|
|
|
|
return -1;
|
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(Settings.partition);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[Settings.partition]->DiskSpace(used, free);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 WBFS_RenameGame(u8 *discid, const void *newname)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
int part_num = gameList.GetPartitionNumber(discid);
|
|
|
|
if(!VALID(part_num))
|
|
|
|
return -1;
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[part_num]->RenameGame(discid, newname);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 WBFS_ReIDGame(u8 *discid, const void *newID)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
int part_num = gameList.GetPartitionNumber(discid);
|
|
|
|
if(!VALID(part_num))
|
|
|
|
return -1;
|
2010-09-18 13:46:25 +02:00
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[part_num]->ReIDGame(discid, newID);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2011-01-16 14:12:07 +01:00
|
|
|
u64 WBFS_EstimeGameSize(void)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
if(!VALID(Settings.partition))
|
|
|
|
return 0;
|
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(Settings.partition);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[Settings.partition]->EstimateGameSize();
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int WBFS_GetFragList(u8 *id)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2011-02-02 19:30:15 +01:00
|
|
|
int part_num = gameList.GetPartitionNumber(id);
|
|
|
|
if(!VALID(part_num))
|
|
|
|
return -1;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2011-02-05 22:06:52 +01:00
|
|
|
DeviceHandler::SetUSBPortFromPartition(part_num);
|
|
|
|
|
2011-02-02 19:30:15 +01:00
|
|
|
return WbfsList[part_num]->GetFragList(id);
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-10-27 21:50:48 +02:00
|
|
|
int MountWBFS(bool ShowGUI)
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-10-27 21:50:48 +02:00
|
|
|
if(ShowGUI)
|
2010-12-31 14:13:14 +01:00
|
|
|
return WBFS_Init(WBFS_DEVICE_USB);
|
2010-10-27 21:50:48 +02:00
|
|
|
|
2010-09-17 17:15:21 +02:00
|
|
|
int ret = -1;
|
2010-09-24 02:48:03 +02:00
|
|
|
time_t currTime = time(0);
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
while (time(0) - currTime < 30)
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
ret = WBFS_Init(WBFS_DEVICE_USB);
|
|
|
|
printf("%i...", int(time(0) - currTime));
|
|
|
|
if (ret < 0)
|
|
|
|
sleep(1);
|
2010-12-31 14:13:14 +01:00
|
|
|
else
|
2010-12-31 00:49:22 +01:00
|
|
|
break;
|
2010-12-31 14:13:14 +01:00
|
|
|
|
2010-12-31 00:49:22 +01:00
|
|
|
DeviceHandler::Instance()->UnMountAllUSB();
|
|
|
|
DeviceHandler::Instance()->MountAllUSB();
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
printf("\n");
|
2010-09-17 17:15:21 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|