Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'hle-fs-cleanup'
  • Loading branch information
jordan-woyak committed Mar 5, 2013
2 parents 642eab9 + 5d47fd1 commit b7db96e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 112 deletions.
4 changes: 1 addition & 3 deletions Source/Core/Core/Src/Boot/Boot_WiiWAD.cpp
Expand Up @@ -90,9 +90,7 @@ bool CBoot::Boot_WiiWAD(const char* _pFilename)
u64 titleID = ContentLoader.GetTitleID();
// create data directory
File::CreateFullPath(Common::GetTitleDataPath(titleID));

if (titleID == TITLEID_SYSMENU)
HLE_IPC_CreateVirtualFATFilesystem();

// setup wii mem
if (!SetupWiiMemory(ContentLoader.GetCountry()))
return false;
Expand Down
173 changes: 68 additions & 105 deletions Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_FileIO.cpp
Expand Up @@ -33,9 +33,6 @@ std::string HLE_IPC_BuildFilename(std::string path_wii, int _size)
{
std::string path_full = File::GetUserPath(D_WIIROOT_IDX);

if ((path_wii.length() > 0) && (path_wii[1] == '0'))
path_full += std::string("/title"); // this looks and feel like a hack...

// Replaces chars that FAT32 can't support with strings defined in /sys/replace
for (auto i = replacements.begin(); i != replacements.end(); ++i)
{
Expand All @@ -48,39 +45,6 @@ std::string HLE_IPC_BuildFilename(std::string path_wii, int _size)
return path_full;
}

void HLE_IPC_CreateVirtualFATFilesystem()
{
const int cdbSize = 0x01400000;
const std::string cdbPath = Common::GetTitleDataPath(TITLEID_SYSMENU) + "cdb.vff";
if ((int)File::GetSize(cdbPath) < cdbSize)
{
// cdb.vff is a virtual Fat filesystem created on first launch of sysmenu
// we create it here as it is faster ~3 minutes for me when sysmenu does it ~1 second created here
const u8 cdbHDR[0x20] = {'V', 'F', 'F', 0x20, 0xfe, 0xff, 1, 0, 1, 0x40, 0, 0, 0, 0x20};
const u8 cdbFAT[4] = {0xf0, 0xff, 0xff, 0xff};

File::IOFile cdbFile(cdbPath, "wb");
if (cdbFile)
{
cdbFile.WriteBytes(cdbHDR, 0x20);
cdbFile.WriteBytes(cdbFAT, 0x4);
cdbFile.Seek(0x14020, SEEK_SET);
cdbFile.WriteBytes(cdbFAT, 0x4);
// 20 MiB file
cdbFile.Seek(cdbSize - 1, SEEK_SET);
// write the final 0 to 0 file from the second FAT to 20 MiB
cdbFile.WriteBytes(cdbHDR + 14, 1);
if (!cdbFile.IsGood())
{
cdbFile.Close();
File::Delete(cdbPath);
}
cdbFile.Flush();
cdbFile.Close();
}
}
}

CWII_IPC_HLE_Device_FileIO::CWII_IPC_HLE_Device_FileIO(u32 _DeviceID, const std::string& _rDeviceName)
: IWII_IPC_HLE_Device(_DeviceID, _rDeviceName, false) // not a real hardware
, m_Mode(0)
Expand Down Expand Up @@ -139,82 +103,81 @@ bool CWII_IPC_HLE_Device_FileIO::Open(u32 _CommandAddress, u32 _Mode)
return true;
}

File::IOFile CWII_IPC_HLE_Device_FileIO::OpenFile()
// Opens file if needed.
// Clears any error state.
// Seeks to proper position position.
void CWII_IPC_HLE_Device_FileIO::PrepareFile()
{
const char* open_mode = "";

switch (m_Mode)
if (!m_file.IsOpen())
{
case ISFS_OPEN_READ:
open_mode = "rb";
break;

case ISFS_OPEN_WRITE:
case ISFS_OPEN_RW:
open_mode = "r+b";
break;

default:
PanicAlertT("FileIO: Unknown open mode : 0x%02x", m_Mode);
break;
const char* open_mode = "";

switch (m_Mode)
{
case ISFS_OPEN_READ:
open_mode = "rb";
break;

case ISFS_OPEN_WRITE:
case ISFS_OPEN_RW:
open_mode = "r+b";
break;

default:
PanicAlertT("FileIO: Unknown open mode : 0x%02x", m_Mode);
break;
}

m_file.Open(m_filepath, open_mode);
}

return File::IOFile(m_filepath, open_mode);
m_file.Clear();
m_file.Seek(m_SeekPos, SEEK_SET);
}

bool CWII_IPC_HLE_Device_FileIO::Seek(u32 _CommandAddress)
{
u32 ReturnValue = FS_RESULT_FATAL;
const u32 SeekPosition = Memory::Read_U32(_CommandAddress + 0xC);
const u32 SeekOffset = Memory::Read_U32(_CommandAddress + 0xC);
const u32 Mode = Memory::Read_U32(_CommandAddress + 0x10);

if (auto file = OpenFile())
PrepareFile();
if (m_file)
{
ReturnValue = FS_RESULT_FATAL;

const u64 fileSize = file.GetSize();
INFO_LOG(WII_IPC_FILEIO, "FileIO: Seek Pos: 0x%08x, Mode: %i (%s, Length=0x%08llx)", SeekPosition, Mode, m_Name.c_str(), fileSize);
const u64 fileSize = m_file.GetSize();
INFO_LOG(WII_IPC_FILEIO, "FileIO: Seek Pos: 0x%08x, Mode: %i (%s, Length=0x%08llx)", SeekOffset, Mode, m_Name.c_str(), fileSize);
u64 wantedPos = 0;
switch (Mode)
{
case 0:
{
if (SeekPosition <= fileSize)
{
m_SeekPos = SeekPosition;
ReturnValue = m_SeekPos;
}
break;
}
case 1:
{
u32 wantedPos = SeekPosition+m_SeekPos;
if (wantedPos <= fileSize)
{
m_SeekPos = wantedPos;
ReturnValue = m_SeekPos;
}
break;
}
case 2:
{
u64 wantedPos = fileSize+m_SeekPos;
if (wantedPos <= fileSize)
{
m_SeekPos = wantedPos;
ReturnValue = m_SeekPos;
}
break;
}
default:
{
PanicAlert("CWII_IPC_HLE_Device_FileIO Unsupported seek mode %i", Mode);
ReturnValue = FS_RESULT_FATAL;
break;
}
case 0:
wantedPos = SeekOffset;
break;

case 1:
wantedPos = m_SeekPos + (s32)SeekOffset;
break;

case 2:
wantedPos = fileSize + (s32)SeekOffset;
break;

default:
PanicAlert("CWII_IPC_HLE_Device_FileIO Unsupported seek mode %i", Mode);
ReturnValue = FS_RESULT_FATAL;
break;
}

if (wantedPos <= fileSize)
{
m_SeekPos = wantedPos;
ReturnValue = m_SeekPos;
}
}
else
{
// TODO: This can't be right.
ReturnValue = FS_FILE_NOT_EXIST;
}
Memory::Write_U32(ReturnValue, _CommandAddress + 0x4);
Expand All @@ -227,9 +190,9 @@ bool CWII_IPC_HLE_Device_FileIO::Read(u32 _CommandAddress)
u32 ReturnValue = FS_EACCESS;
const u32 Address = Memory::Read_U32(_CommandAddress + 0xC); // Read to this memory address
const u32 Size = Memory::Read_U32(_CommandAddress + 0x10);


if (auto file = OpenFile())
PrepareFile();
if (m_file)
{
if (m_Mode == ISFS_OPEN_WRITE)
{
Expand All @@ -238,9 +201,8 @@ bool CWII_IPC_HLE_Device_FileIO::Read(u32 _CommandAddress)
else
{
INFO_LOG(WII_IPC_FILEIO, "FileIO: Read 0x%x bytes to 0x%08x from %s", Size, Address, m_Name.c_str());
file.Seek(m_SeekPos, SEEK_SET);
ReturnValue = (u32)fread(Memory::GetPointer(Address), 1, Size, file.GetHandle());
if (ReturnValue != Size && ferror(file.GetHandle()))
ReturnValue = (u32)fread(Memory::GetPointer(Address), 1, Size, m_file.GetHandle());
if (ReturnValue != Size && ferror(m_file.GetHandle()))
{
ReturnValue = FS_EACCESS;
}
Expand All @@ -267,8 +229,8 @@ bool CWII_IPC_HLE_Device_FileIO::Write(u32 _CommandAddress)
const u32 Address = Memory::Read_U32(_CommandAddress + 0xC); // Write data from this memory address
const u32 Size = Memory::Read_U32(_CommandAddress + 0x10);


if (auto file = OpenFile())
PrepareFile();
if (m_file)
{
if (m_Mode == ISFS_OPEN_READ)
{
Expand All @@ -277,8 +239,7 @@ bool CWII_IPC_HLE_Device_FileIO::Write(u32 _CommandAddress)
else
{
INFO_LOG(WII_IPC_FILEIO, "FileIO: Write 0x%04x bytes from 0x%08x to %s", Size, Address, m_Name.c_str());
file.Seek(m_SeekPos, SEEK_SET);
if (file.WriteBytes(Memory::GetPointer(Address), Size))
if (m_file.WriteBytes(Memory::GetPointer(Address), Size))
{
ReturnValue = Size;
m_SeekPos += Size;
Expand Down Expand Up @@ -308,9 +269,10 @@ bool CWII_IPC_HLE_Device_FileIO::IOCtl(u32 _CommandAddress)
{
case ISFS_IOCTL_GETFILESTATS:
{
if (auto file = OpenFile())
PrepareFile();
if (m_file)
{
u32 m_FileLength = (u32)file.GetSize();
u32 m_FileLength = (u32)m_file.GetSize();

const u32 BufferOut = Memory::Read_U32(_CommandAddress + 0x18);
INFO_LOG(WII_IPC_FILEIO, "FileIO: ISFS_IOCTL_GETFILESTATS");
Expand Down Expand Up @@ -345,6 +307,7 @@ void CWII_IPC_HLE_Device_FileIO::DoState(PointerWrap &p)

p.Do(m_Mode);
p.Do(m_SeekPos);


m_file.Close();
m_filepath = HLE_IPC_BuildFilename(m_Name, 64);
}
6 changes: 3 additions & 3 deletions Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_FileIO.h
Expand Up @@ -22,7 +22,6 @@
#include "FileUtil.h"

std::string HLE_IPC_BuildFilename(std::string _pFilename, int _size);
void HLE_IPC_CreateVirtualFATFilesystem();

class CWII_IPC_HLE_Device_FileIO : public IWII_IPC_HLE_Device
{
Expand All @@ -39,9 +38,9 @@ class CWII_IPC_HLE_Device_FileIO : public IWII_IPC_HLE_Device
bool IOCtl(u32 _CommandAddress);
void DoState(PointerWrap &p);

File::IOFile OpenFile();

private:
void PrepareFile();

enum
{
ISFS_OPEN_READ = 1,
Expand Down Expand Up @@ -77,6 +76,7 @@ class CWII_IPC_HLE_Device_FileIO : public IWII_IPC_HLE_Device

u32 m_Mode;
u32 m_SeekPos;
File::IOFile m_file;

std::string m_filepath;
};
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_fs.cpp
Expand Up @@ -377,7 +377,7 @@ s32 CWII_IPC_HLE_Device_fs::ExecuteCommand(u32 _Parameter, u32 _BufferIn, u32 _B
u32 Addr = _BufferOut;
Memory::Write_U32(OwnerID, Addr); Addr += 4;
Memory::Write_U16(GroupID, Addr); Addr += 2;
memcpy(Memory::GetPointer(Addr), Filename.c_str(), Filename.size()); Addr += 64;
memcpy(Memory::GetPointer(Addr), Memory::GetPointer(_BufferIn), 64); Addr += 64;
Memory::Write_U8(OwnerPerm, Addr); Addr += 1;
Memory::Write_U8(GroupPerm, Addr); Addr += 1;
Memory::Write_U8(OtherPerm, Addr); Addr += 1;
Expand Down

0 comments on commit b7db96e

Please sign in to comment.