FS: transform File to ServiceFramework

This commit is contained in:
wwylele 2017-07-14 23:47:09 +03:00
parent cef6b15de4
commit a79a8d17fa
2 changed files with 133 additions and 111 deletions

View File

@ -27,6 +27,7 @@
#include "core/file_sys/errors.h" #include "core/file_sys/errors.h"
#include "core/file_sys/file_backend.h" #include "core/file_sys/file_backend.h"
#include "core/hle/ipc.h" #include "core/hle/ipc.h"
#include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/client_port.h" #include "core/hle/kernel/client_port.h"
#include "core/hle/kernel/client_session.h" #include "core/hle/kernel/client_session.h"
#include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/handle_table.h"
@ -84,26 +85,28 @@ enum class DirectoryCommand : u32 {
}; };
File::File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path) File::File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path)
: path(path), priority(0), backend(std::move(backend)) {} : path(path), priority(0), backend(std::move(backend)), ServiceFramework("", 1) {
static const FunctionInfo functions[] = {
{0x080200C2, &File::Read, "Read"},
{0x08030102, &File::Write, "Write"},
{0x08040000, &File::GetSize, "GetSize"},
{0x08050080, &File::SetSize, "SetSize"},
{0x08080000, &File::Close, "Close"},
{0x08090000, &File::Flush, "Flush"},
{0x080A0040, &File::SetPriority, "SetPriority"},
{0x080B0000, &File::GetPriority, "GetPriority"},
{0x080C0000, &File::OpenLinkFile, "OpenLinkFile"},
};
RegisterHandlers(functions);
}
File::~File() {} void File::Read(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x0802, 3, 2);
void File::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) { u64 offset = rp.Pop<u64>();
using Kernel::ClientSession; u32 length = rp.Pop<u32>();
using Kernel::ServerSession; auto& buffer = rp.PopMappedBuffer();
using Kernel::SharedPtr; LOG_TRACE(Service_FS, "Read %s: offset=0x%" PRIx64 " length=0x%08X", GetName().c_str(), offset,
length);
u32* cmd_buff = Kernel::GetCommandBuffer();
FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
switch (cmd) {
// Read from file...
case FileCommand::Read: {
u64 offset = cmd_buff[1] | ((u64)cmd_buff[2]) << 32;
u32 length = cmd_buff[3];
u32 address = cmd_buff[5];
LOG_TRACE(Service_FS, "Read %s: offset=0x%llx length=%d address=0x%x", GetName().c_str(),
offset, length, address);
if (offset + length > backend->GetSize()) { if (offset + length > backend->GetSize()) {
LOG_ERROR(Service_FS, "Reading from out of bounds offset=0x%" PRIx64 LOG_ERROR(Service_FS, "Reading from out of bounds offset=0x%" PRIx64
@ -111,94 +114,102 @@ void File::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_ses
offset, length, backend->GetSize()); offset, length, backend->GetSize());
} }
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
std::vector<u8> data(length); std::vector<u8> data(length);
ResultVal<size_t> read = backend->Read(offset, data.size(), data.data()); ResultVal<size_t> read = backend->Read(offset, data.size(), data.data());
if (read.Failed()) { if (read.Failed()) {
cmd_buff[1] = read.Code().raw; rb.Push(read.Code());
return; rb.Push<u32>(0);
} else {
buffer.Write(data.data(), 0, *read);
rb.Push(RESULT_SUCCESS);
rb.Push<u32>(*read);
} }
Memory::WriteBlock(address, data.data(), *read); rb.PushMappedBuffer(buffer);
cmd_buff[2] = static_cast<u32>(*read);
break;
} }
// Write to file... void File::Write(Kernel::HLERequestContext& ctx) {
case FileCommand::Write: { IPC::RequestParser rp(ctx, 0x0803, 3, 2);
u64 offset = cmd_buff[1] | ((u64)cmd_buff[2]) << 32; u64 offset = rp.Pop<u64>();
u32 length = cmd_buff[3]; u32 length = rp.Pop<u32>();
u32 flush = cmd_buff[4]; u32 flush = rp.Pop<u32>();
u32 address = cmd_buff[6]; auto& buffer = rp.PopMappedBuffer();
LOG_TRACE(Service_FS, "Write %s: offset=0x%llx length=%d address=0x%x, flush=0x%x", LOG_TRACE(Service_FS, "Write %s: offset=0x%llx length=%d, flush=0x%x", GetName().c_str(),
GetName().c_str(), offset, length, address, flush); offset, length, flush);
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
std::vector<u8> data(length); std::vector<u8> data(length);
Memory::ReadBlock(address, data.data(), data.size()); buffer.Read(data.data(), 0, data.size());
ResultVal<size_t> written = backend->Write(offset, data.size(), flush != 0, data.data()); ResultVal<size_t> written = backend->Write(offset, data.size(), flush != 0, data.data());
if (written.Failed()) { if (written.Failed()) {
cmd_buff[1] = written.Code().raw; rb.Push(written.Code());
return; rb.Push<u32>(0);
} else {
rb.Push(RESULT_SUCCESS);
rb.Push<u32>(*written);
} }
cmd_buff[2] = static_cast<u32>(*written); rb.PushMappedBuffer(buffer);
break;
} }
case FileCommand::GetSize: { void File::GetSize(Kernel::HLERequestContext& ctx) {
LOG_TRACE(Service_FS, "GetSize %s", GetName().c_str()); IPC::RequestParser rp(ctx, 0x0804, 0, 0);
u64 size = backend->GetSize(); IPC::RequestBuilder rb = rp.MakeBuilder(3, 0);
cmd_buff[2] = (u32)size; rb.Push(RESULT_SUCCESS);
cmd_buff[3] = size >> 32; rb.Push(backend->GetSize());
break;
} }
case FileCommand::SetSize: { void File::SetSize(Kernel::HLERequestContext& ctx) {
u64 size = cmd_buff[1] | ((u64)cmd_buff[2] << 32); IPC::RequestParser rp(ctx, 0x0805, 2, 0);
LOG_TRACE(Service_FS, "SetSize %s size=%llu", GetName().c_str(), size); backend->SetSize(rp.Pop<u64>());
backend->SetSize(size); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
break; rb.Push(RESULT_SUCCESS);
} }
case FileCommand::Close: { void File::Close(Kernel::HLERequestContext& ctx) {
LOG_TRACE(Service_FS, "Close %s", GetName().c_str()); IPC::RequestParser rp(ctx, 0x0808, 0, 0);
backend->Close(); backend->Close();
break; IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
} }
case FileCommand::Flush: { void File::Flush(Kernel::HLERequestContext& ctx) {
LOG_TRACE(Service_FS, "Flush"); IPC::RequestParser rp(ctx, 0x0809, 0, 0);
backend->Flush(); backend->Flush();
break; IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
} }
case FileCommand::OpenLinkFile: { void File::SetPriority(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x080A, 1, 0);
priority = rp.Pop<u32>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS);
}
void File::GetPriority(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x080B, 0, 0);
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS);
rb.Push(priority);
}
void File::OpenLinkFile(Kernel::HLERequestContext& ctx) {
LOG_WARNING(Service_FS, "(STUBBED) File command OpenLinkFile %s", GetName().c_str()); LOG_WARNING(Service_FS, "(STUBBED) File command OpenLinkFile %s", GetName().c_str());
using Kernel::ClientSession;
using Kernel::ServerSession;
using Kernel::SharedPtr;
IPC::RequestParser rp(ctx, 0x080C, 0, 0);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
auto sessions = ServerSession::CreateSessionPair(GetName()); auto sessions = ServerSession::CreateSessionPair(GetName());
ClientConnected(std::get<SharedPtr<ServerSession>>(sessions)); ClientConnected(std::get<SharedPtr<ServerSession>>(sessions));
cmd_buff[3] = Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions))
.ValueOr(INVALID_HANDLE); rb.Push(RESULT_SUCCESS);
break; rb.PushObjects(std::get<SharedPtr<ClientSession>>(sessions));
} }
case FileCommand::SetPriority: { File::~File() {}
priority = cmd_buff[1];
LOG_TRACE(Service_FS, "SetPriority %u", priority);
break;
}
case FileCommand::GetPriority: {
cmd_buff[2] = priority;
LOG_TRACE(Service_FS, "GetPriority");
break;
}
// Unknown command...
default:
LOG_ERROR(Service_FS, "Unknown command=0x%08X!", static_cast<u32>(cmd));
ResultCode error = UnimplementedFunction(ErrorModule::FS);
cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that.
return;
}
cmd_buff[1] = RESULT_SUCCESS.raw; // No error
}
Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
const FileSys::Path& path) const FileSys::Path& path)

View File

@ -10,6 +10,7 @@
#include "core/file_sys/archive_backend.h" #include "core/file_sys/archive_backend.h"
#include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/hle_ipc.h"
#include "core/hle/result.h" #include "core/hle/result.h"
#include "core/hle/service/service.h"
namespace FileSys { namespace FileSys {
class DirectoryBackend; class DirectoryBackend;
@ -47,7 +48,9 @@ enum class MediaType : u32 { NAND = 0, SDMC = 1, GameCard = 2 };
typedef u64 ArchiveHandle; typedef u64 ArchiveHandle;
class File final : public Kernel::SessionRequestHandler { // TODO: File is not a real service, but it can still utilize ServiceFramework::RegisterHandlers.
// Consider splitting ServiceFramework interface.
class File final : public ServiceFramework<File> {
public: public:
File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path); File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path);
~File(); ~File();
@ -60,8 +63,16 @@ public:
u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means
std::unique_ptr<FileSys::FileBackend> backend; ///< File backend interface std::unique_ptr<FileSys::FileBackend> backend; ///< File backend interface
protected: private:
void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override; void Read(Kernel::HLERequestContext& ctx);
void Write(Kernel::HLERequestContext& ctx);
void GetSize(Kernel::HLERequestContext& ctx);
void SetSize(Kernel::HLERequestContext& ctx);
void Close(Kernel::HLERequestContext& ctx);
void Flush(Kernel::HLERequestContext& ctx);
void SetPriority(Kernel::HLERequestContext& ctx);
void GetPriority(Kernel::HLERequestContext& ctx);
void OpenLinkFile(Kernel::HLERequestContext& ctx);
}; };
class Directory final : public Kernel::SessionRequestHandler { class Directory final : public Kernel::SessionRequestHandler {