2008-08-24 21:28:42 +00:00
|
|
|
#include "../AppConfig.h"
|
2008-01-15 20:27:44 +00:00
|
|
|
#include "Iop_Ioman.h"
|
|
|
|
#include "StdStream.h"
|
2014-08-07 02:34:03 -04:00
|
|
|
#include "../Log.h"
|
2008-01-15 20:27:44 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace Iop;
|
|
|
|
|
2014-08-07 02:34:03 -04:00
|
|
|
#define LOG_NAME "iop_ioman"
|
|
|
|
|
2008-01-15 20:27:44 +00:00
|
|
|
#define PREF_IOP_FILEIO_STDLOGGING ("iop.fileio.stdlogging")
|
|
|
|
|
2012-05-26 18:37:09 +00:00
|
|
|
CIoman::CIoman(uint8* ram)
|
|
|
|
: m_ram(ram)
|
|
|
|
, m_nextFileHandle(3)
|
2008-01-15 20:27:44 +00:00
|
|
|
{
|
2008-08-24 21:28:42 +00:00
|
|
|
CAppConfig::GetInstance().RegisterPreferenceBoolean(PREF_IOP_FILEIO_STDLOGGING, false);
|
2008-01-15 20:27:44 +00:00
|
|
|
|
|
|
|
//Insert standard files if requested.
|
2014-08-02 21:18:07 -04:00
|
|
|
if(CAppConfig::GetInstance().GetPreferenceBoolean(PREF_IOP_FILEIO_STDLOGGING)
|
|
|
|
#ifdef DEBUGGER_INCLUDED
|
|
|
|
|| true
|
|
|
|
#endif
|
|
|
|
)
|
2008-01-15 20:27:44 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-08-02 21:18:07 -04:00
|
|
|
auto stdoutPath = CAppConfig::GetBasePath() / "ps2_stdout.txt";
|
|
|
|
auto stderrPath = CAppConfig::GetBasePath() / "ps2_stderr.txt";
|
|
|
|
|
|
|
|
m_files[FID_STDOUT] = new Framework::CStdStream(fopen(stdoutPath.string().c_str(), "ab"));
|
|
|
|
m_files[FID_STDERR] = new Framework::CStdStream(fopen(stderrPath.string().c_str(), "ab"));
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
//Humm, some error occured when opening these files...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CIoman::~CIoman()
|
|
|
|
{
|
2012-05-26 18:37:09 +00:00
|
|
|
for(auto fileIterator(std::begin(m_files));
|
|
|
|
std::end(m_files) != fileIterator; fileIterator++)
|
|
|
|
{
|
|
|
|
delete fileIterator->second;
|
|
|
|
}
|
2008-11-04 03:47:36 +00:00
|
|
|
m_devices.clear();
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CIoman::GetId() const
|
|
|
|
{
|
2012-05-26 18:37:09 +00:00
|
|
|
return "ioman";
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
2012-05-26 18:37:09 +00:00
|
|
|
std::string CIoman::GetFunctionName(unsigned int functionId) const
|
2008-11-28 23:46:52 +00:00
|
|
|
{
|
2012-11-10 22:53:43 +00:00
|
|
|
switch(functionId)
|
|
|
|
{
|
|
|
|
case 4:
|
|
|
|
return "open";
|
|
|
|
break;
|
2016-01-09 18:47:54 -05:00
|
|
|
case 5:
|
|
|
|
return "close";
|
|
|
|
break;
|
2012-11-10 22:53:43 +00:00
|
|
|
case 6:
|
|
|
|
return "read";
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
return "seek";
|
|
|
|
break;
|
2016-07-09 21:20:52 -04:00
|
|
|
case 16:
|
|
|
|
return "getstat";
|
|
|
|
break;
|
2012-11-10 22:53:43 +00:00
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
break;
|
|
|
|
}
|
2008-11-28 23:46:52 +00:00
|
|
|
}
|
|
|
|
|
2008-11-04 03:47:36 +00:00
|
|
|
void CIoman::RegisterDevice(const char* name, const DevicePtr& device)
|
2008-01-15 20:27:44 +00:00
|
|
|
{
|
2012-05-26 18:37:09 +00:00
|
|
|
m_devices[name] = device;
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 CIoman::Open(uint32 flags, const char* path)
|
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "Open(flags = 0x%0.8X, path = '%s');\r\n", flags, path);
|
|
|
|
|
2012-05-26 18:37:09 +00:00
|
|
|
uint32 handle = 0xFFFFFFFF;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string fullPath(path);
|
|
|
|
std::string::size_type position = fullPath.find(":");
|
|
|
|
if(position == std::string::npos)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid path.");
|
|
|
|
}
|
|
|
|
std::string deviceName(fullPath.begin(), fullPath.begin() + position);
|
|
|
|
std::string devicePath(fullPath.begin() + position + 1, fullPath.end());
|
|
|
|
DeviceMapType::iterator device(m_devices.find(deviceName));
|
|
|
|
if(device == m_devices.end())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Device not found.");
|
|
|
|
}
|
|
|
|
Framework::CStream* stream = device->second->GetFile(flags, devicePath.c_str());
|
|
|
|
if(stream == NULL)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("File not found.");
|
|
|
|
}
|
|
|
|
handle = m_nextFileHandle++;
|
|
|
|
m_files[handle] = stream;
|
|
|
|
}
|
|
|
|
catch(const std::exception& except)
|
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "%s: Error occured while trying to open file : %s\r\n", __FUNCTION__, except.what());
|
2012-05-26 18:37:09 +00:00
|
|
|
}
|
|
|
|
return handle;
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 CIoman::Close(uint32 handle)
|
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "Close(handle = %d);\r\n", handle);
|
|
|
|
|
2012-05-26 18:37:09 +00:00
|
|
|
uint32 result = 0xFFFFFFFF;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto file(m_files.find(handle));
|
|
|
|
if(file == std::end(m_files))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid file handle.");
|
|
|
|
}
|
|
|
|
delete file->second;
|
|
|
|
m_files.erase(file);
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
catch(const std::exception& except)
|
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "%s: Error occured while trying to close file : %s\r\n", __FUNCTION__, except.what());
|
2012-05-26 18:37:09 +00:00
|
|
|
}
|
|
|
|
return result;
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 CIoman::Read(uint32 handle, uint32 size, void* buffer)
|
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "Read(handle = %d, size = 0x%X, buffer = ptr);\r\n", handle, size);
|
|
|
|
|
2012-05-26 18:37:09 +00:00
|
|
|
uint32 result = 0xFFFFFFFF;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Framework::CStream* stream = GetFileStream(handle);
|
|
|
|
result = static_cast<uint32>(stream->Read(buffer, size));
|
|
|
|
}
|
|
|
|
catch(const std::exception& except)
|
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "%s: Error occured while trying to read file : %s\r\n", __FUNCTION__, except.what());
|
2012-05-26 18:37:09 +00:00
|
|
|
}
|
|
|
|
return result;
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 21:18:07 -04:00
|
|
|
uint32 CIoman::Write(uint32 handle, uint32 size, const void* buffer)
|
2008-01-15 20:27:44 +00:00
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "Write(handle = %d, size = 0x%X, buffer = ptr);\r\n", handle, size);
|
|
|
|
|
2012-05-26 18:37:09 +00:00
|
|
|
uint32 result = 0xFFFFFFFF;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Framework::CStream* stream = GetFileStream(handle);
|
|
|
|
result = static_cast<uint32>(stream->Write(buffer, size));
|
2014-08-02 21:18:07 -04:00
|
|
|
if((handle == FID_STDOUT) || (handle == FID_STDERR))
|
|
|
|
{
|
|
|
|
//Force flusing stdout and stderr
|
|
|
|
stream->Flush();
|
|
|
|
}
|
2012-05-26 18:37:09 +00:00
|
|
|
}
|
|
|
|
catch(const std::exception& except)
|
|
|
|
{
|
2014-08-02 21:18:07 -04:00
|
|
|
if((handle != FID_STDOUT) && (handle != FID_STDERR))
|
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "%s: Error occured while trying to write file : %s\r\n", __FUNCTION__, except.what());
|
2014-08-02 21:18:07 -04:00
|
|
|
}
|
2012-05-26 18:37:09 +00:00
|
|
|
}
|
|
|
|
return result;
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 CIoman::Seek(uint32 handle, uint32 position, uint32 whence)
|
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "Seek(handle = %d, position = 0x%X, whence = %d);\r\n",
|
|
|
|
handle, position, whence);
|
|
|
|
|
2012-05-26 18:37:09 +00:00
|
|
|
uint32 result = 0xFFFFFFFF;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Framework::CStream* stream = GetFileStream(handle);
|
|
|
|
switch(whence)
|
|
|
|
{
|
2014-11-08 21:44:56 -05:00
|
|
|
case SEEK_DIR_SET:
|
2012-05-26 18:37:09 +00:00
|
|
|
whence = Framework::STREAM_SEEK_SET;
|
|
|
|
break;
|
2014-11-08 21:44:56 -05:00
|
|
|
case SEEK_DIR_CUR:
|
2012-05-26 18:37:09 +00:00
|
|
|
whence = Framework::STREAM_SEEK_CUR;
|
|
|
|
break;
|
2014-11-08 21:44:56 -05:00
|
|
|
case SEEK_DIR_END:
|
2012-05-26 18:37:09 +00:00
|
|
|
whence = Framework::STREAM_SEEK_END;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->Seek(position, static_cast<Framework::STREAM_SEEK_DIRECTION>(whence));
|
|
|
|
result = static_cast<uint32>(stream->Tell());
|
|
|
|
}
|
|
|
|
catch(const std::exception& except)
|
|
|
|
{
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "%s: Error occured while trying to seek file : %s\r\n", __FUNCTION__, except.what());
|
2012-05-26 18:37:09 +00:00
|
|
|
}
|
|
|
|
return result;
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 17:42:02 -05:00
|
|
|
uint32 CIoman::GetStat(const char* path, STAT* stat)
|
|
|
|
{
|
|
|
|
CLog::GetInstance().Print(LOG_NAME, "GetStat(path = '%s', stat = ptr);\r\n", path);
|
|
|
|
|
|
|
|
uint32 fd = Open(Ioman::CDevice::OPEN_FLAG_RDONLY, path);
|
|
|
|
if(static_cast<int32>(fd) < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint32 size = Seek(fd, 0, SEEK_DIR_END);
|
|
|
|
Close(fd);
|
|
|
|
memset(stat, 0, sizeof(STAT));
|
|
|
|
stat->mode = 0777 | (2 << 12); //File mode + File type (2)
|
|
|
|
stat->loSize = size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-29 01:08:09 +00:00
|
|
|
uint32 CIoman::AddDrv(uint32 drvPtr)
|
|
|
|
{
|
2015-06-28 00:43:38 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "AddDrv(drvPtr = 0x%0.8X);\r\n",
|
|
|
|
drvPtr);
|
2012-05-29 01:08:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-28 00:43:38 -04:00
|
|
|
uint32 CIoman::DelDrv(uint32 drvPtr)
|
2008-01-15 20:27:44 +00:00
|
|
|
{
|
2015-06-28 00:43:38 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "DelDrv(drvPtr = 0x%0.8X);\r\n",
|
|
|
|
drvPtr);
|
2012-05-26 18:37:09 +00:00
|
|
|
return -1;
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
2012-05-26 18:37:09 +00:00
|
|
|
Framework::CStream* CIoman::GetFileStream(uint32 handle)
|
2008-01-15 20:27:44 +00:00
|
|
|
{
|
2012-05-26 18:37:09 +00:00
|
|
|
auto file(m_files.find(handle));
|
|
|
|
if(file == std::end(m_files))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid file handle.");
|
|
|
|
}
|
|
|
|
return file->second;
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 00:07:32 -04:00
|
|
|
void CIoman::SetFileStream(uint32 handle, Framework::CStream* stream)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto prevStreamIterator = m_files.find(handle);
|
|
|
|
if(prevStreamIterator != std::end(m_files))
|
|
|
|
{
|
|
|
|
delete prevStreamIterator->second;
|
|
|
|
m_files.erase(prevStreamIterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_files[handle] = stream;
|
|
|
|
}
|
|
|
|
|
2008-01-15 20:27:44 +00:00
|
|
|
//IOP Invoke
|
|
|
|
void CIoman::Invoke(CMIPS& context, unsigned int functionId)
|
|
|
|
{
|
2012-05-26 18:37:09 +00:00
|
|
|
switch(functionId)
|
|
|
|
{
|
|
|
|
case 4:
|
|
|
|
context.m_State.nGPR[CMIPS::V0].nD0 = static_cast<int32>(Open(
|
|
|
|
context.m_State.nGPR[CMIPS::A1].nV[0],
|
|
|
|
reinterpret_cast<char*>(&m_ram[context.m_State.nGPR[CMIPS::A0].nV[0]])
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
context.m_State.nGPR[CMIPS::V0].nD0 = static_cast<int32>(Close(
|
|
|
|
context.m_State.nGPR[CMIPS::A0].nV[0]
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
context.m_State.nGPR[CMIPS::V0].nD0 = static_cast<int32>(Read(
|
|
|
|
context.m_State.nGPR[CMIPS::A0].nV[0],
|
|
|
|
context.m_State.nGPR[CMIPS::A2].nV[0],
|
|
|
|
&m_ram[context.m_State.nGPR[CMIPS::A1].nV[0]]
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
context.m_State.nGPR[CMIPS::V0].nD0 = static_cast<int32>(Seek(
|
|
|
|
context.m_State.nGPR[CMIPS::A0].nV[0],
|
|
|
|
context.m_State.nGPR[CMIPS::A1].nV[0],
|
|
|
|
context.m_State.nGPR[CMIPS::A2].nV[0]));
|
|
|
|
break;
|
2016-07-09 21:20:52 -04:00
|
|
|
case 16:
|
|
|
|
context.m_State.nGPR[CMIPS::V0].nD0 = static_cast<int32>(GetStat(
|
|
|
|
reinterpret_cast<char*>(&m_ram[context.m_State.nGPR[CMIPS::A0].nV[0]]),
|
|
|
|
reinterpret_cast<STAT*>(&m_ram[context.m_State.nGPR[CMIPS::A1].nV[0]])
|
|
|
|
));
|
|
|
|
break;
|
2012-05-29 01:08:09 +00:00
|
|
|
case 20:
|
|
|
|
context.m_State.nGPR[CMIPS::V0].nD0 = static_cast<int32>(AddDrv(
|
|
|
|
context.m_State.nGPR[CMIPS::A0].nV0
|
|
|
|
));
|
|
|
|
break;
|
2012-05-26 18:37:09 +00:00
|
|
|
case 21:
|
|
|
|
context.m_State.nGPR[CMIPS::V0].nD0 = static_cast<int32>(DelDrv(
|
2015-06-28 00:43:38 -04:00
|
|
|
context.m_State.nGPR[CMIPS::A0].nV0
|
2012-05-26 18:37:09 +00:00
|
|
|
));
|
|
|
|
break;
|
|
|
|
default:
|
2014-08-07 02:34:03 -04:00
|
|
|
CLog::GetInstance().Print(LOG_NAME, "%s(%0.8X): Unknown function (%d) called.\r\n", __FUNCTION__, context.m_State.nPC, functionId);
|
2012-05-26 18:37:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-01-15 20:27:44 +00:00
|
|
|
}
|
|
|
|
|
2008-10-20 04:14:13 +00:00
|
|
|
//--------------------------------------------------
|
|
|
|
// CFile
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
2012-05-26 18:37:09 +00:00
|
|
|
CIoman::CFile::CFile(uint32 handle, CIoman& ioman)
|
|
|
|
: m_handle(handle)
|
|
|
|
, m_ioman(ioman)
|
2008-10-20 04:14:13 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CIoman::CFile::~CFile()
|
|
|
|
{
|
2012-05-26 18:37:09 +00:00
|
|
|
m_ioman.Close(m_handle);
|
2008-10-20 04:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CIoman::CFile::operator uint32()
|
|
|
|
{
|
2012-05-26 18:37:09 +00:00
|
|
|
return m_handle;
|
2008-10-20 04:14:13 +00:00
|
|
|
}
|