2010-02-25 13:08:03 +01:00
|
|
|
#include "wbfs_wbfs.h"
|
2011-06-22 19:57:37 +02:00
|
|
|
#include "Controls/DeviceHandler.hpp"
|
2010-02-25 13:08:03 +01:00
|
|
|
#include "prompts/ProgressWindow.h"
|
2010-09-19 22:25:12 +02:00
|
|
|
#include "settings/CSettings.h"
|
2010-12-31 00:49:22 +01:00
|
|
|
#include "usbloader/wbfs.h"
|
2011-06-22 19:57:37 +02:00
|
|
|
#include "usbloader/usbstorage2.h"
|
2010-02-25 13:08:03 +01:00
|
|
|
#include "wbfs_rw.h"
|
|
|
|
|
2011-02-04 16:25:27 +01:00
|
|
|
#define MAX_WBFS_SECTORSIZE 4096
|
|
|
|
|
2011-06-22 19:57:37 +02:00
|
|
|
extern u32 hdd_sector_size[2];
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
s32 Wbfs_Wbfs::Open()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
wbfs_t *part = NULL;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
2011-02-04 16:25:27 +01:00
|
|
|
PartInfo.wbfs_sector_size = MAX_WBFS_SECTORSIZE;
|
2011-06-22 19:57:37 +02:00
|
|
|
PartInfo.hdd_sector_size = hdd_sector_size[usbport];
|
2011-01-28 22:19:43 +01:00
|
|
|
PartInfo.partition_lba = lba;
|
|
|
|
PartInfo.partition_num_sec = size;
|
2011-06-22 19:57:37 +02:00
|
|
|
PartInfo.handle = (usbport == 0) ? DeviceHandler::GetUSB0Interface() : DeviceHandler::GetUSB1Interface();
|
2011-01-16 14:12:07 +01:00
|
|
|
|
2011-02-04 16:25:27 +01:00
|
|
|
u8 * buffer = (u8 *) malloc(MAX_WBFS_SECTORSIZE);
|
|
|
|
memset(buffer, 0, MAX_WBFS_SECTORSIZE);
|
2011-01-16 14:12:07 +01:00
|
|
|
|
2011-01-28 22:19:43 +01:00
|
|
|
if(readCallback(&PartInfo, lba, 1, buffer) < 0)
|
|
|
|
{
|
|
|
|
free(buffer);
|
2011-01-16 14:12:07 +01:00
|
|
|
return -1;
|
2011-01-28 22:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wbfs_head_t head;
|
|
|
|
memcpy(&head, buffer, sizeof(wbfs_head_t));
|
|
|
|
free(buffer);
|
2011-01-16 14:12:07 +01:00
|
|
|
|
2011-01-28 22:19:43 +01:00
|
|
|
if (head.magic != wbfs_htonl(WBFS_MAGIC))
|
2011-01-16 14:12:07 +01:00
|
|
|
return -1;
|
|
|
|
|
2011-02-04 16:25:27 +01:00
|
|
|
/* Set correct sector values for wbfs read/write */
|
2011-01-28 22:19:43 +01:00
|
|
|
PartInfo.wbfs_sector_size = 1 << head.hd_sec_sz_s;
|
2011-02-04 16:25:27 +01:00
|
|
|
PartInfo.partition_num_sec = head.n_hd_sec;
|
2011-01-28 22:19:43 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
/* Open partition */
|
2011-02-04 16:25:27 +01:00
|
|
|
part = wbfs_open_partition(readCallback, writeCallback, &PartInfo,
|
|
|
|
PartInfo.wbfs_sector_size, PartInfo.partition_num_sec,
|
|
|
|
lba, 0);
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!part) return -1;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
/* Close current hard disk */
|
|
|
|
Close();
|
|
|
|
hdd = part;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return 0;
|
2010-02-25 13:08:03 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 18:02:10 +01:00
|
|
|
void Wbfs_Wbfs::Close()
|
|
|
|
{
|
|
|
|
if (hdd)
|
|
|
|
{
|
|
|
|
wbfs_close(hdd);
|
|
|
|
hdd = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
wbfs_disc_t* Wbfs_Wbfs::OpenDisc(u8 *discid)
|
2010-02-25 13:08:03 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
/* No device open */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!hdd) return NULL;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
/* Open disc */
|
2010-09-24 02:48:03 +02:00
|
|
|
return wbfs_open_disc(hdd, discid);
|
2010-02-25 13:08:03 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
void Wbfs_Wbfs::CloseDisc(wbfs_disc_t *disc)
|
2010-02-25 13:08:03 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
/* No device open */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!hdd || !disc) return;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
/* Close disc */
|
2010-09-24 02:48:03 +02:00
|
|
|
wbfs_close_disc(disc);
|
2010-02-25 13:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s32 Wbfs_Wbfs::Format()
|
|
|
|
{
|
2011-01-28 22:19:43 +01:00
|
|
|
WBFS_PartInfo HDD_Inf;
|
2011-06-22 19:57:37 +02:00
|
|
|
HDD_Inf.wbfs_sector_size = hdd_sector_size[usbport];
|
|
|
|
HDD_Inf.hdd_sector_size = hdd_sector_size[usbport];
|
2011-01-28 22:19:43 +01:00
|
|
|
HDD_Inf.partition_lba = lba;
|
|
|
|
HDD_Inf.partition_num_sec = size;
|
2011-06-22 19:57:37 +02:00
|
|
|
HDD_Inf.handle = (usbport == 0) ? DeviceHandler::GetUSB0Interface() : DeviceHandler::GetUSB1Interface();
|
2011-01-28 22:19:43 +01:00
|
|
|
|
2011-02-04 16:25:27 +01:00
|
|
|
//! If size is over 500GB in sectors and sector size is 512
|
|
|
|
//! set 2048 as hdd sector size
|
2011-06-22 19:57:37 +02:00
|
|
|
if(size > 1048576000 && hdd_sector_size[usbport] == 512)
|
2011-02-04 16:25:27 +01:00
|
|
|
{
|
|
|
|
HDD_Inf.wbfs_sector_size = 2048;
|
2011-06-22 19:57:37 +02:00
|
|
|
HDD_Inf.partition_num_sec = size/(2048/hdd_sector_size[usbport]);
|
2011-02-04 16:25:27 +01:00
|
|
|
}
|
|
|
|
|
2010-02-25 13:08:03 +01:00
|
|
|
wbfs_t *partition = NULL;
|
|
|
|
|
|
|
|
/* Reset partition */
|
2011-02-04 16:25:27 +01:00
|
|
|
partition = wbfs_open_partition(readCallback, writeCallback, &HDD_Inf, HDD_Inf.wbfs_sector_size, HDD_Inf.partition_num_sec, lba, 1);
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!partition) return -1;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
/* Free memory */
|
2010-09-24 02:48:03 +02:00
|
|
|
wbfs_close(partition);
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 Wbfs_Wbfs::GetCount(u32 *count)
|
2010-02-25 13:08:03 +01:00
|
|
|
{
|
|
|
|
/* No device open */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!hdd) return -1;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
/* Get list length */
|
2010-09-24 02:48:03 +02:00
|
|
|
*count = wbfs_count_discs(hdd);
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 Wbfs_Wbfs::GetHeaders(struct discHdr *outbuf, u32 cnt, u32 len)
|
2010-02-25 13:08:03 +01:00
|
|
|
{
|
|
|
|
u32 idx, size;
|
|
|
|
s32 ret;
|
|
|
|
|
|
|
|
/* No device open */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!hdd) return -1;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
for (idx = 0; idx < cnt; idx++)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
u8 *ptr = ((u8 *) outbuf) + (idx * len);
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
/* Get header */
|
2010-09-24 02:48:03 +02:00
|
|
|
ret = wbfs_get_disc_info(hdd, idx, ptr, len, &size);
|
|
|
|
if (ret < 0) return ret;
|
2010-02-25 13:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 Wbfs_Wbfs::AddGame()
|
|
|
|
{
|
|
|
|
s32 ret;
|
|
|
|
|
|
|
|
/* No device open */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!hdd) return -1;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
2010-12-26 18:02:14 +01:00
|
|
|
partition_selector_t part_sel = (partition_selector_t) Settings.InstallPartitions;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-12-26 18:02:14 +01:00
|
|
|
/* Add game to device */
|
2011-01-14 16:39:42 +01:00
|
|
|
ret = wbfs_add_disc(hdd, __ReadDVD, NULL, ShowProgress, part_sel, 0);
|
2010-09-24 02:48:03 +02:00
|
|
|
if (ret < 0) return ret;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 Wbfs_Wbfs::RemoveGame(u8 *discid)
|
2010-02-25 13:08:03 +01:00
|
|
|
{
|
|
|
|
s32 ret;
|
|
|
|
|
|
|
|
/* No device open */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!hdd) return -1;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
/* Remove game from USB device */
|
2010-09-24 02:48:03 +02:00
|
|
|
ret = wbfs_rm_disc(hdd, discid);
|
|
|
|
if (ret < 0) return ret;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 Wbfs_Wbfs::DiskSpace(f32 *used, f32 *free)
|
2010-02-25 13:08:03 +01:00
|
|
|
{
|
|
|
|
f32 ssize;
|
|
|
|
u32 cnt;
|
|
|
|
|
|
|
|
/* No device open */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!hdd) return -1;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
/* Count used blocks */
|
2010-09-24 02:48:03 +02:00
|
|
|
cnt = wbfs_count_usedblocks(hdd);
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
/* Sector size in GB */
|
|
|
|
ssize = hdd->wbfs_sec_sz / GB_SIZE;
|
|
|
|
|
|
|
|
/* Copy values */
|
|
|
|
*free = ssize * cnt;
|
2010-09-24 02:48:03 +02:00
|
|
|
*used = ssize * (hdd->n_wbfs_sec - cnt);
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 Wbfs_Wbfs::RenameGame(u8 *discid, const void *newname)
|
2010-02-25 13:08:03 +01:00
|
|
|
{
|
|
|
|
s32 ret;
|
|
|
|
|
|
|
|
/* No USB device open */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!hdd) return -1;
|
|
|
|
ret = wbfs_ren_disc(hdd, discid, (u8*) newname);
|
|
|
|
if (ret < 0) return ret;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 Wbfs_Wbfs::ReIDGame(u8 *discid, const void *newID)
|
2010-02-25 13:08:03 +01:00
|
|
|
{
|
|
|
|
s32 ret;
|
|
|
|
|
|
|
|
/* No USB device open */
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!hdd) return -1;
|
|
|
|
ret = wbfs_rID_disc(hdd, discid, (u8*) newID);
|
|
|
|
if (ret < 0) return ret;
|
2010-02-25 13:08:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-16 14:12:07 +01:00
|
|
|
u64 Wbfs_Wbfs::EstimateGameSize()
|
2010-02-25 13:08:03 +01:00
|
|
|
{
|
2010-12-26 18:02:14 +01:00
|
|
|
partition_selector_t part_sel = (partition_selector_t) Settings.InstallPartitions;
|
2010-09-24 02:48:03 +02:00
|
|
|
return wbfs_estimate_disc(hdd, __ReadDVD, NULL, part_sel);
|
2010-02-25 13:08:03 +01:00
|
|
|
}
|
2011-01-07 23:42:03 +01:00
|
|
|
|
|
|
|
s32 Wbfs_Wbfs::GetFragList(u8 *id)
|
|
|
|
{
|
|
|
|
//! Doesn't have to be called ".iso" just something different than .wbfs but with a dot.
|
|
|
|
//! So that the code doesn't fail.
|
2011-06-22 19:57:37 +02:00
|
|
|
return get_frag_list_for_file((char *) ".iso", id, GetFSType(), lba, hdd_sector_size[usbport]);
|
2011-01-07 23:42:03 +01:00
|
|
|
}
|