void File::Write(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp(ctx, 0x0803, 4, 2); u64 offset = rp.Pop<u64>(); u32 length = rp.Pop<u32>(); u32 flush = rp.Pop<u32>(); auto& buffer = rp.PopMappedBuffer(); LOG_TRACE(Service_FS, "Write {}: offset=0x{:x} length={}, flush=0x{:x}", GetName(), offset, length, flush); IPC::RequestBuilder rb = rp.MakeBuilder(2, 2); const FileSessionSlot* file = GetSessionData(ctx.Session()); // Subfiles can not be written to if (file->subfile) { rb.Push(FileSys::ERROR_UNSUPPORTED_OPEN_FLAGS); rb.Push<u32>(0); rb.PushMappedBuffer(buffer); return; } std::vector<u8> data(length); buffer.Read(data.data(), 0, data.size()); ResultVal<std::size_t> written = backend->Write(offset, data.size(), flush != 0, data.data()); if (written.Failed()) { rb.Push(written.Code()); rb.Push<u32>(0); } else { rb.Push(RESULT_SUCCESS); rb.Push<u32>(static_cast<u32>(*written)); } rb.PushMappedBuffer(buffer); }
/** * FS_User::OpenFile service function * Inputs: * 1 : Transaction * 2 : Archive handle lower word * 3 : Archive handle upper word * 4 : Low path type * 5 : Low path size * 6 : Open flags * 7 : Attributes * 8 : (LowPathSize << 14) | 2 * 9 : Low path data pointer * Outputs: * 1 : Result of function, 0 on success, otherwise error code * 3 : File handle */ static void OpenFile(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to // 3dmoo's or ctrulib's implementations. Triple check if it's really the case. Handle archive_handle = static_cast<Handle>(cmd_buff[3]); auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]); u32 filename_size = cmd_buff[5]; FileSys::Mode mode; mode.hex = cmd_buff[6]; u32 attributes = cmd_buff[7]; // TODO(Link Mauve): do something with those attributes. u32 filename_ptr = cmd_buff[9]; FileSys::Path file_path(filename_type, filename_size, filename_ptr); DEBUG_LOG(KERNEL, "path=%s, mode=%d attrs=%u", file_path.DebugStr().c_str(), mode.hex, attributes); ResultVal<Handle> handle = Kernel::OpenFileFromArchive(archive_handle, file_path, mode); cmd_buff[1] = handle.Code().raw; if (handle.Succeeded()) { cmd_buff[3] = *handle; } else { ERROR_LOG(KERNEL, "failed to get a handle for file %s", file_path.DebugStr().c_str()); } DEBUG_LOG(KERNEL, "called"); }
/** * FS_User::OpenArchive service function * Inputs: * 1 : Archive ID * 2 : Archive low path type * 3 : Archive low path size * 4 : (LowPathSize << 14) | 2 * 5 : Archive low path * Outputs: * 1 : Result of function, 0 on success, otherwise error code * 2 : Archive handle lower word (unused) * 3 : Archive handle upper word (same as file handle) */ static void OpenArchive(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); auto archive_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[1]); auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[2]); u32 archivename_size = cmd_buff[3]; u32 archivename_ptr = cmd_buff[5]; FileSys::Path archive_path(archivename_type, archivename_size, archivename_ptr); DEBUG_LOG(KERNEL, "archive_path=%s", archive_path.DebugStr().c_str()); if (archive_path.GetType() != FileSys::Empty) { ERROR_LOG(KERNEL, "archive LowPath type other than empty is currently unsupported"); cmd_buff[1] = UnimplementedFunction(ErrorModule::FS).raw; return; } ResultVal<Handle> handle = Kernel::OpenArchive(archive_id); cmd_buff[1] = handle.Code().raw; if (handle.Succeeded()) { // cmd_buff[2] isn't used according to 3dmoo's implementation. cmd_buff[3] = *handle; } else { ERROR_LOG(KERNEL, "failed to get a handle for archive"); } DEBUG_LOG(KERNEL, "called"); }
static inline FrameBufferUpdate* GetFrameBufferInfo(u32 thread_id, u32 screen_index) { _dbg_assert_msg_(GSP, screen_index < 2, "Invalid screen index"); // For each thread there are two FrameBufferUpdate fields u32 offset = 0x200 + (2 * thread_id + screen_index) * sizeof(FrameBufferUpdate); ResultVal<u8*> ptr = Kernel::GetSharedMemoryPointer(g_shared_memory, offset); return reinterpret_cast<FrameBufferUpdate*>(ptr.ValueOr(nullptr)); }
/** * FS_User::OpenFileDirectly service function * Inputs: * 1 : Transaction * 2 : Archive ID * 3 : Archive low path type * 4 : Archive low path size * 5 : File low path type * 6 : File low path size * 7 : Flags * 8 : Attributes * 9 : (ArchiveLowPathSize << 14) | 0x802 * 10 : Archive low path * 11 : (FileLowPathSize << 14) | 2 * 12 : File low path * Outputs: * 1 : Result of function, 0 on success, otherwise error code * 3 : File handle */ static void OpenFileDirectly(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); auto archive_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[2]); auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[3]); u32 archivename_size = cmd_buff[4]; auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[5]); u32 filename_size = cmd_buff[6]; FileSys::Mode mode; mode.hex = cmd_buff[7]; u32 attributes = cmd_buff[8]; // TODO(Link Mauve): do something with those attributes. u32 archivename_ptr = cmd_buff[10]; u32 filename_ptr = cmd_buff[12]; FileSys::Path archive_path(archivename_type, archivename_size, archivename_ptr); FileSys::Path file_path(filename_type, filename_size, filename_ptr); DEBUG_LOG(KERNEL, "archive_path=%s file_path=%s, mode=%u attributes=%d", archive_path.DebugStr().c_str(), file_path.DebugStr().c_str(), mode.hex, attributes); if (archive_path.GetType() != FileSys::Empty) { ERROR_LOG(KERNEL, "archive LowPath type other than empty is currently unsupported"); cmd_buff[1] = UnimplementedFunction(ErrorModule::FS).raw; return; } // TODO(Link Mauve): Check if we should even get a handle for the archive, and don't leak it // TODO(yuriks): Why is there all this duplicate (and seemingly useless) code up here? ResultVal<Handle> archive_handle = Kernel::OpenArchive(archive_id); cmd_buff[1] = archive_handle.Code().raw; if (archive_handle.Failed()) { ERROR_LOG(KERNEL, "failed to get a handle for archive"); return; } // cmd_buff[2] isn't used according to 3dmoo's implementation. cmd_buff[3] = *archive_handle; ResultVal<Handle> handle = Kernel::OpenFileFromArchive(*archive_handle, file_path, mode); cmd_buff[1] = handle.Code().raw; if (handle.Succeeded()) { cmd_buff[3] = *handle; } else { ERROR_LOG(KERNEL, "failed to get a handle for file %s", file_path.DebugStr().c_str()); } DEBUG_LOG(KERNEL, "called"); }
/// Synchronize to an OS service static Result SendSyncRequest(Handle handle) { // TODO(yuriks): ObjectPool::Get tries to check the Object type, which fails since this is a generic base Object, // so we are forced to use GetFast and manually verify the handle. if (!Kernel::g_object_pool.IsValid(handle)) { return InvalidHandle(ErrorModule::Kernel).raw; } Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handle); _assert_msg_(KERNEL, (object != nullptr), "called, but kernel object is nullptr!"); DEBUG_LOG(SVC, "called handle=0x%08X(%s)", handle, object->GetTypeName().c_str()); ResultVal<bool> wait = object->SyncRequest(); if (wait.Succeeded() && *wait) { Kernel::WaitCurrentThread(WAITTYPE_SYNCH); // TODO(bunnei): Is this correct? } return wait.Code().raw; }
/// Wait for a handle to synchronize, timeout after the specified nanoseconds static Result WaitSynchronization1(Handle handle, s64 nano_seconds) { // TODO(bunnei): Do something with nano_seconds, currently ignoring this bool wait_infinite = (nano_seconds == -1); // Used to wait until a thread has terminated if (!Kernel::g_object_pool.IsValid(handle)) { return InvalidHandle(ErrorModule::Kernel).raw; } Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handle); _dbg_assert_(KERNEL, object != nullptr); DEBUG_LOG(SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle, object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds); ResultVal<bool> wait = object->WaitSynchronization(); // Check for next thread to schedule if (wait.Succeeded() && *wait) { HLE::Reschedule(__func__); } return wait.Code().raw; }
static void OpenDirectory(Service::Interface* self) { u32* cmd_buff = Service::GetCommandBuffer(); // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to // 3dmoo's or ctrulib's implementations. Triple check if it's really the case. Handle archive_handle = static_cast<Handle>(cmd_buff[2]); auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[3]); u32 dirname_size = cmd_buff[4]; u32 dirname_ptr = cmd_buff[6]; FileSys::Path dir_path(dirname_type, dirname_size, dirname_ptr); DEBUG_LOG(KERNEL, "type=%d size=%d data=%s", dirname_type, dirname_size, dir_path.DebugStr().c_str()); ResultVal<Handle> handle = Kernel::OpenDirectoryFromArchive(archive_handle, dir_path); cmd_buff[1] = handle.Code().raw; if (handle.Succeeded()) { cmd_buff[3] = *handle; } else { ERROR_LOG(KERNEL, "failed to get a handle for directory"); } DEBUG_LOG(KERNEL, "called"); }
LOG_DEBUG(Service_FS, "archive_id=0x%08X archive_path=%s file_path=%s, mode=%u attributes=%u", static_cast<u32>(archive_id), archive_path.DebugStr().c_str(), file_path.DebugStr().c_str(), mode.hex, attributes); ResultVal<ArchiveHandle> archive_handle = OpenArchive(archive_id, archive_path); if (archive_handle.Failed()) { LOG_ERROR(Service_FS, "Failed to get a handle for archive archive_id=0x%08X archive_path=%s", static_cast<u32>(archive_id), archive_path.DebugStr().c_str()); cmd_buff[1] = archive_handle.Code().raw; cmd_buff[3] = 0; return; } SCOPE_EXIT({ CloseArchive(*archive_handle); }); ResultVal<std::shared_ptr<File>> file_res = OpenFileFromArchive(*archive_handle, file_path, mode); cmd_buff[1] = file_res.Code().raw; if (file_res.Succeeded()) { std::shared_ptr<File> file = *file_res; auto sessions = ServerSession::CreateSessionPair(file->GetName()); file->ClientConnected(std::get<SharedPtr<ServerSession>>(sessions)); cmd_buff[3] = Kernel::g_handle_table.Create(std::get<SharedPtr<ClientSession>>(sessions)).Unwrap(); } else { cmd_buff[3] = 0; LOG_ERROR(Service_FS, "failed to get a handle for file %s mode=%u attributes=%u", file_path.DebugStr().c_str(), mode.hex, attributes); } }
attributes); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); ResultVal<ArchiveHandle> archive_handle = archives.OpenArchive(archive_id, archive_path); if (archive_handle.Failed()) { LOG_ERROR(Service_FS, "Failed to get a handle for archive archive_id=0x{:08X} archive_path={}", static_cast<u32>(archive_id), archive_path.DebugStr()); rb.Push(archive_handle.Code()); rb.PushMoveObjects<Kernel::Object>(nullptr); return; } SCOPE_EXIT({ archives.CloseArchive(*archive_handle); }); ResultVal<std::shared_ptr<File>> file_res = archives.OpenFileFromArchive(*archive_handle, file_path, mode); rb.Push(file_res.Code()); if (file_res.Succeeded()) { std::shared_ptr<File> file = *file_res; rb.PushMoveObjects(file->Connect()); } else { rb.PushMoveObjects<Kernel::Object>(nullptr); LOG_ERROR(Service_FS, "failed to get a handle for file {} mode={} attributes={}", file_path.DebugStr(), mode.hex, attributes); } } void FS_USER::DeleteFile(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp(ctx, 0x804, 5, 2); rp.Skip(1, false); // TransactionId ArchiveHandle archive_handle = rp.PopRaw<ArchiveHandle>();
/// Gets a pointer to the interrupt relay queue for a given thread index static inline InterruptRelayQueue* GetInterruptRelayQueue(u32 thread_id) { ResultVal<u8*> ptr = Kernel::GetSharedMemoryPointer(g_shared_memory, sizeof(InterruptRelayQueue) * thread_id); return reinterpret_cast<InterruptRelayQueue*>(ptr.ValueOr(nullptr)); }