usbloadergx/source/usbloader/wbfs/wbfs_rw.c

169 lines
3.5 KiB
C
Raw Normal View History

#include <ogcsys.h>
#include <malloc.h>
#include <string.h>
#include "usbloader/sdhc.h"
#include "usbloader/usbstorage2.h"
#include "usbloader/wdvd.h"
#include "wbfs_rw.h"
/* Constants */
#define MAX_NB_SECTORS 32
u32 sector_size = 512;
2010-09-24 02:48:03 +02:00
rw_sector_callback_t readCallback = NULL;
rw_sector_callback_t writeCallback = NULL;
2010-09-24 02:48:03 +02:00
void SetSectorSize(u32 size)
{
sector_size = size;
}
2010-09-24 02:48:03 +02:00
s32 __ReadDVD(void *fp, u32 lba, u32 len, void *iobuf)
{
void *buffer = NULL;
u64 offset;
u32 mod, size;
s32 ret;
/* Calculate offset */
2010-09-24 02:48:03 +02:00
offset = ((u64) lba) << 2;
/* Calcualte sizes */
2010-09-24 02:48:03 +02:00
mod = len % 32;
size = len - mod;
/* Read aligned data */
2010-09-24 02:48:03 +02:00
if (size)
{
2010-09-24 02:48:03 +02:00
ret = WDVD_UnencryptedRead(iobuf, size, offset);
if (ret < 0) goto out;
}
/* Read non-aligned data */
2010-09-24 02:48:03 +02:00
if (mod)
{
/* Allocate memory */
2010-09-24 02:48:03 +02:00
buffer = memalign(32, 0x20);
if (!buffer) return -1;
/* Read data */
2010-09-24 02:48:03 +02:00
ret = WDVD_UnencryptedRead(buffer, 0x20, offset + size);
if (ret < 0) goto out;
/* Copy data */
2010-09-24 02:48:03 +02:00
void *ptr = ((u8 *) iobuf) + size;
memcpy(ptr, buffer, mod);
}
/* Success */
ret = 0;
2010-09-24 02:48:03 +02:00
out:
/* Free memory */
2010-09-24 02:48:03 +02:00
if (buffer) free(buffer);
return ret;
}
2010-09-24 02:48:03 +02:00
s32 __ReadUSB(void *fp, u32 lba, u32 count, void *iobuf)
{
u32 cnt = 0;
s32 ret;
/* Do reads */
2010-09-24 02:48:03 +02:00
while (cnt < count)
{
2010-09-24 02:48:03 +02:00
void *ptr = ((u8 *) iobuf) + (cnt * sector_size);
u32 sectors = (count - cnt);
/* Read sectors is too big */
2010-09-24 02:48:03 +02:00
if (sectors > MAX_NB_SECTORS) sectors = MAX_NB_SECTORS;
/* USB read */
2010-09-24 02:48:03 +02:00
ret = USBStorage2_ReadSectors(lba + cnt, sectors, ptr);
if (ret < 0) return ret;
/* Increment counter */
cnt += sectors;
}
return 0;
}
2010-09-24 02:48:03 +02:00
s32 __WriteUSB(void *fp, u32 lba, u32 count, void *iobuf)
{
u32 cnt = 0;
s32 ret;
/* Do writes */
2010-09-24 02:48:03 +02:00
while (cnt < count)
{
2010-09-24 02:48:03 +02:00
void *ptr = ((u8 *) iobuf) + (cnt * sector_size);
u32 sectors = (count - cnt);
/* Write sectors is too big */
2010-09-24 02:48:03 +02:00
if (sectors > MAX_NB_SECTORS) sectors = MAX_NB_SECTORS;
/* USB write */
2010-09-24 02:48:03 +02:00
ret = USBStorage2_WriteSectors(lba + cnt, sectors, ptr);
if (ret < 0) return ret;
/* Increment counter */
cnt += sectors;
}
return 0;
}
2010-09-24 02:48:03 +02:00
s32 __ReadSDHC(void *fp, u32 lba, u32 count, void *iobuf)
{
u32 cnt = 0;
s32 ret;
/* Do reads */
2010-09-24 02:48:03 +02:00
while (cnt < count)
{
2010-09-24 02:48:03 +02:00
void *ptr = ((u8 *) iobuf) + (cnt * sector_size);
u32 sectors = (count - cnt);
/* Read sectors is too big */
2010-09-24 02:48:03 +02:00
if (sectors > MAX_NB_SECTORS) sectors = MAX_NB_SECTORS;
/* SDHC read */
2010-09-24 02:48:03 +02:00
ret = SDHC_ReadSectors(lba + cnt, sectors, ptr);
if (!ret) return -1;
/* Increment counter */
cnt += sectors;
}
return 0;
}
2010-09-24 02:48:03 +02:00
s32 __WriteSDHC(void *fp, u32 lba, u32 count, void *iobuf)
{
u32 cnt = 0;
s32 ret;
/* Do writes */
2010-09-24 02:48:03 +02:00
while (cnt < count)
{
2010-09-24 02:48:03 +02:00
void *ptr = ((u8 *) iobuf) + (cnt * sector_size);
u32 sectors = (count - cnt);
/* Write sectors is too big */
2010-09-24 02:48:03 +02:00
if (sectors > MAX_NB_SECTORS) sectors = MAX_NB_SECTORS;
/* SDHC write */
2010-09-24 02:48:03 +02:00
ret = SDHC_WriteSectors(lba + cnt, sectors, ptr);
if (!ret) return -1;
/* Increment counter */
cnt += sectors;
}
return 0;
}