2015-06-11 22:12:16 +00:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <ctime>
|
2018-07-29 16:43:13 +00:00
|
|
|
#include "common/file_util.h"
|
2015-06-11 22:12:16 +00:00
|
|
|
#include "common/logging/log.h"
|
2018-07-29 16:43:13 +00:00
|
|
|
#include "common/string_util.h"
|
2018-08-15 05:47:33 +00:00
|
|
|
#include "core/core.h"
|
2018-07-29 16:43:13 +00:00
|
|
|
#include "core/file_sys/archive_systemsavedata.h"
|
2018-08-06 06:39:47 +00:00
|
|
|
#include "core/file_sys/directory_backend.h"
|
2018-07-29 16:43:13 +00:00
|
|
|
#include "core/file_sys/errors.h"
|
|
|
|
#include "core/file_sys/file_backend.h"
|
2018-07-01 03:27:57 +00:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2018-08-06 06:39:47 +00:00
|
|
|
#include "core/hle/kernel/process.h"
|
2017-06-06 08:29:46 +00:00
|
|
|
#include "core/hle/result.h"
|
2016-09-21 06:52:38 +00:00
|
|
|
#include "core/hle/service/cecd/cecd.h"
|
2016-12-15 06:52:40 +00:00
|
|
|
#include "core/hle/service/cecd/cecd_ndm.h"
|
2015-06-11 22:12:16 +00:00
|
|
|
#include "core/hle/service/cecd/cecd_s.h"
|
|
|
|
#include "core/hle/service/cecd/cecd_u.h"
|
2018-08-15 05:47:33 +00:00
|
|
|
#include "core/loader/smdh.h"
|
2018-07-01 03:27:57 +00:00
|
|
|
|
2015-06-11 22:12:16 +00:00
|
|
|
namespace Service {
|
|
|
|
namespace CECD {
|
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
using CecDataPathType = Module::CecDataPathType;
|
|
|
|
using CecOpenMode = Module::CecOpenMode;
|
|
|
|
using CecSystemInfoType = Module::CecSystemInfoType;
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
void Module::Interface::Open(Kernel::HLERequestContext& ctx) {
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x01, 3, 2);
|
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
2018-08-07 17:45:11 +00:00
|
|
|
const CecDataPathType path_type = rp.PopEnum<CecDataPathType>();
|
2018-08-06 06:39:47 +00:00
|
|
|
CecOpenMode open_mode;
|
|
|
|
open_mode.raw = rp.Pop<u32>();
|
2018-07-26 04:54:11 +00:00
|
|
|
rp.PopPID();
|
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
FileSys::Path path(cecd->GetCecDataPathTypeAsString(path_type, ncch_program_id).data());
|
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.read_flag.Assign(1);
|
|
|
|
mode.write_flag.Assign(1);
|
|
|
|
mode.create_flag.Assign(1);
|
|
|
|
|
|
|
|
SessionData* session_data = GetSessionData(ctx.Session());
|
|
|
|
session_data->ncch_program_id = ncch_program_id;
|
|
|
|
session_data->open_mode.raw = open_mode.raw;
|
|
|
|
session_data->data_path_type = path_type;
|
|
|
|
session_data->path = path;
|
|
|
|
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
2018-08-06 06:39:47 +00:00
|
|
|
switch (path_type) {
|
|
|
|
case CecDataPathType::CEC_PATH_ROOT_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_DIR: {
|
|
|
|
auto dir_result =
|
|
|
|
Service::FS::OpenDirectoryFromArchive(cecd->cecd_system_save_data_archive, path);
|
|
|
|
if (dir_result.Failed()) {
|
2018-08-17 06:32:48 +00:00
|
|
|
if (open_mode.create) {
|
2018-08-06 06:39:47 +00:00
|
|
|
Service::FS::CreateDirectoryFromArchive(cecd->cecd_system_save_data_archive, path);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
} else {
|
2018-08-17 06:32:48 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "Failed to open directory: {}", path.AsString());
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC,
|
|
|
|
ErrorSummary::NotFound, ErrorLevel::Status));
|
|
|
|
}
|
|
|
|
rb.Push<u32>(0); /// Zero entries
|
|
|
|
} else {
|
|
|
|
auto directory = dir_result.Unwrap();
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(directory->backend->Read(0, nullptr)); /// Entry count
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: { /// If not directory, then it is a file
|
|
|
|
auto file_result =
|
|
|
|
Service::FS::OpenFileFromArchive(cecd->cecd_system_save_data_archive, path, mode);
|
|
|
|
if (file_result.Failed()) {
|
2018-08-17 06:32:48 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "Failed to open file: {}", path.AsString());
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
|
|
|
rb.Push<u32>(0); /// No file size
|
|
|
|
} else {
|
|
|
|
session_data->file = std::move(file_result.Unwrap());
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(session_data->file->backend->GetSize()); /// Return file size
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path_type == CecDataPathType::CEC_MBOX_PROGRAM_ID) {
|
|
|
|
std::vector<u8> program_id(8);
|
|
|
|
u64_le le_program_id = Kernel::g_current_process->codeset->program_id;
|
|
|
|
std::memcpy(program_id.data(), &le_program_id, sizeof(u64));
|
|
|
|
session_data->file->backend->Write(0, sizeof(u64), true, program_id.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"called, ncch_program_id={:#010x}, path_type={:#04x}, open_mode={:#010x}, path={}",
|
|
|
|
ncch_program_id, static_cast<u32>(path_type), open_mode.raw, path.AsString());
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
void Module::Interface::ReadFile(Kernel::HLERequestContext& ctx) {
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x02, 1, 2);
|
2018-08-06 06:39:47 +00:00
|
|
|
const u32 write_buffer_size = rp.Pop<u32>();
|
|
|
|
auto& write_buffer = rp.PopMappedBuffer();
|
|
|
|
|
|
|
|
SessionData* session_data = GetSessionData(ctx.Session());
|
2018-07-26 04:54:11 +00:00
|
|
|
|
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
|
2018-08-06 06:39:47 +00:00
|
|
|
switch (session_data->data_path_type) {
|
|
|
|
case CecDataPathType::CEC_PATH_ROOT_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_DIR:
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC,
|
|
|
|
ErrorSummary::NotFound, ErrorLevel::Status));
|
|
|
|
rb.Push<u32>(0); /// No bytes read
|
|
|
|
break;
|
|
|
|
default: /// If not directory, then it is a file
|
|
|
|
std::vector<u8> buffer(write_buffer_size);
|
|
|
|
const u32 bytes_read =
|
|
|
|
session_data->file->backend->Read(0, write_buffer_size, buffer.data()).Unwrap();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
write_buffer.Write(buffer.data(), 0, write_buffer_size);
|
|
|
|
session_data->file->backend->Close();
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(bytes_read);
|
|
|
|
}
|
|
|
|
rb.PushMappedBuffer(write_buffer);
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_CECD, "called, write_buffer_size={:#010x}, path={}", write_buffer_size,
|
|
|
|
session_data->path.AsString());
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::ReadMessage(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x03, 4, 4);
|
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
const bool is_outbox = rp.Pop<bool>();
|
2018-07-26 04:54:11 +00:00
|
|
|
const u32 message_id_size = rp.Pop<u32>();
|
|
|
|
const u32 buffer_size = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
auto& message_id_buffer = rp.PopMappedBuffer();
|
2018-07-29 16:43:13 +00:00
|
|
|
auto& write_buffer = rp.PopMappedBuffer();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.read_flag.Assign(1);
|
|
|
|
|
|
|
|
std::vector<u8> id_buffer(message_id_size);
|
|
|
|
message_id_buffer.Read(id_buffer.data(), 0, message_id_size);
|
|
|
|
|
|
|
|
FileSys::Path message_path;
|
|
|
|
if (is_outbox) {
|
|
|
|
message_path = cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_OUTBOX_MSG,
|
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
|
|
|
} else { /// otherwise inbox
|
|
|
|
message_path = cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_INBOX_MSG,
|
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto message_result =
|
|
|
|
Service::FS::OpenFileFromArchive(cecd->cecd_system_save_data_archive, message_path, mode);
|
|
|
|
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 4);
|
2018-08-17 06:32:48 +00:00
|
|
|
if (message_result.Succeeded()) {
|
|
|
|
auto message = message_result.Unwrap();
|
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
|
|
|
|
const u32 bytes_read = message->backend->Read(0, buffer_size, buffer.data()).Unwrap();
|
|
|
|
write_buffer.Write(buffer.data(), 0, buffer_size);
|
|
|
|
message->backend->Close();
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(bytes_read);
|
|
|
|
} else {
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
|
|
|
rb.Push<u32>(0); /// zero bytes read
|
|
|
|
}
|
2018-07-29 16:43:13 +00:00
|
|
|
rb.PushMappedBuffer(message_id_buffer);
|
|
|
|
rb.PushMappedBuffer(write_buffer);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called, ncch_program_id={:#010x}, is_outbox={}",
|
|
|
|
ncch_program_id, is_outbox);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::ReadMessageWithHMAC(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x04, 4, 6);
|
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
const bool is_outbox = rp.Pop<bool>();
|
2018-07-26 04:54:11 +00:00
|
|
|
const u32 message_id_size = rp.Pop<u32>();
|
|
|
|
const u32 buffer_size = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
auto& message_id_buffer = rp.PopMappedBuffer();
|
|
|
|
auto& hmac_key_buffer = rp.PopMappedBuffer();
|
2018-07-29 16:43:13 +00:00
|
|
|
auto& write_buffer = rp.PopMappedBuffer();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.read_flag.Assign(1);
|
|
|
|
|
|
|
|
std::vector<u8> id_buffer(message_id_size);
|
|
|
|
message_id_buffer.Read(id_buffer.data(), 0, message_id_size);
|
|
|
|
|
|
|
|
FileSys::Path message_path;
|
|
|
|
if (is_outbox) {
|
|
|
|
message_path = cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_OUTBOX_MSG,
|
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
|
|
|
} else { /// otherwise inbox
|
|
|
|
message_path = cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_INBOX_MSG,
|
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto message_result =
|
|
|
|
Service::FS::OpenFileFromArchive(cecd->cecd_system_save_data_archive, message_path, mode);
|
|
|
|
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 6);
|
2018-08-17 06:32:48 +00:00
|
|
|
if (message_result.Succeeded()) {
|
|
|
|
auto message = message_result.Unwrap();
|
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
|
|
|
|
const u32 bytes_read = message->backend->Read(0, buffer_size, buffer.data()).Unwrap();
|
|
|
|
write_buffer.Write(buffer.data(), 0, buffer_size);
|
|
|
|
message->backend->Close();
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(bytes_read);
|
|
|
|
} else {
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
|
|
|
rb.Push<u32>(0); /// zero bytes read
|
|
|
|
}
|
|
|
|
|
2018-07-29 16:43:13 +00:00
|
|
|
rb.PushMappedBuffer(message_id_buffer);
|
|
|
|
rb.PushMappedBuffer(hmac_key_buffer);
|
|
|
|
rb.PushMappedBuffer(write_buffer);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called, ncch_program_id={:#010x}, is_outbox={}",
|
|
|
|
ncch_program_id, is_outbox);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
void Module::Interface::WriteFile(Kernel::HLERequestContext& ctx) {
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x05, 1, 2);
|
2018-08-06 06:39:47 +00:00
|
|
|
const u32 read_buffer_size = rp.Pop<u32>();
|
|
|
|
auto& read_buffer = rp.PopMappedBuffer();
|
|
|
|
|
|
|
|
SessionData* session_data = GetSessionData(ctx.Session());
|
2018-07-26 04:54:11 +00:00
|
|
|
|
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
2018-08-06 06:39:47 +00:00
|
|
|
switch (session_data->data_path_type) {
|
|
|
|
case CecDataPathType::CEC_PATH_ROOT_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_DIR:
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC,
|
|
|
|
ErrorSummary::NotFound, ErrorLevel::Status));
|
|
|
|
break;
|
|
|
|
default: /// If not directory, then it is a file
|
|
|
|
std::vector<u8> buffer(read_buffer_size);
|
|
|
|
read_buffer.Read(buffer.data(), 0, read_buffer_size);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (session_data->open_mode.check) {
|
|
|
|
cecd->CheckAndUpdateFile(session_data->data_path_type, session_data->ncch_program_id,
|
|
|
|
buffer);
|
|
|
|
}
|
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
const u32 bytes_written =
|
|
|
|
session_data->file->backend->Write(0, read_buffer_size, true, buffer.data()).Unwrap();
|
|
|
|
session_data->file->backend->Close();
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
rb.PushMappedBuffer(read_buffer);
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_CECD, "called, read_buffer_size={:#010x}", read_buffer_size);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::WriteMessage(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x06, 4, 4);
|
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
const bool is_outbox = rp.Pop<bool>();
|
2018-07-26 04:54:11 +00:00
|
|
|
const u32 message_id_size = rp.Pop<u32>();
|
|
|
|
const u32 buffer_size = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
auto& read_buffer = rp.PopMappedBuffer();
|
2018-07-29 16:43:13 +00:00
|
|
|
auto& message_id_buffer = rp.PopMappedBuffer();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.write_flag.Assign(1);
|
|
|
|
mode.create_flag.Assign(1);
|
|
|
|
|
|
|
|
std::vector<u8> id_buffer(message_id_size);
|
|
|
|
message_id_buffer.Read(id_buffer.data(), 0, message_id_size);
|
|
|
|
|
|
|
|
FileSys::Path message_path;
|
|
|
|
if (is_outbox) {
|
|
|
|
message_path = cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_OUTBOX_MSG,
|
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
|
|
|
} else { /// otherwise inbox
|
|
|
|
message_path = cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_INBOX_MSG,
|
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto message_result =
|
|
|
|
Service::FS::OpenFileFromArchive(cecd->cecd_system_save_data_archive, message_path, mode);
|
|
|
|
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
|
2018-08-17 06:32:48 +00:00
|
|
|
if (message_result.Succeeded()) {
|
|
|
|
auto message = message_result.Unwrap();
|
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
|
|
|
|
read_buffer.Read(buffer.data(), 0, buffer_size);
|
|
|
|
const u32 bytes_written =
|
|
|
|
message->backend->Write(0, buffer_size, true, buffer.data()).Unwrap();
|
|
|
|
message->backend->Close();
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
|
|
|
}
|
|
|
|
|
2018-07-29 16:43:13 +00:00
|
|
|
rb.PushMappedBuffer(read_buffer);
|
|
|
|
rb.PushMappedBuffer(message_id_buffer);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called, ncch_program_id={:#010x}, is_outbox={}",
|
|
|
|
ncch_program_id, is_outbox);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::WriteMessageWithHMAC(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x07, 4, 6);
|
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
const bool is_outbox = rp.Pop<bool>();
|
2018-07-26 04:54:11 +00:00
|
|
|
const u32 message_id_size = rp.Pop<u32>();
|
|
|
|
const u32 buffer_size = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
auto& read_buffer = rp.PopMappedBuffer();
|
|
|
|
auto& hmac_key_buffer = rp.PopMappedBuffer();
|
2018-07-29 16:43:13 +00:00
|
|
|
auto& message_id_buffer = rp.PopMappedBuffer();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.write_flag.Assign(1);
|
|
|
|
mode.create_flag.Assign(1);
|
|
|
|
|
|
|
|
std::vector<u8> id_buffer(message_id_size);
|
|
|
|
message_id_buffer.Read(id_buffer.data(), 0, message_id_size);
|
|
|
|
|
|
|
|
FileSys::Path message_path;
|
|
|
|
if (is_outbox) {
|
|
|
|
message_path = cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_OUTBOX_MSG,
|
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
|
|
|
} else { /// otherwise inbox
|
|
|
|
message_path = cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_INBOX_MSG,
|
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto message_result =
|
|
|
|
Service::FS::OpenFileFromArchive(cecd->cecd_system_save_data_archive, message_path, mode);
|
|
|
|
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 6);
|
2018-08-17 06:32:48 +00:00
|
|
|
if (message_result.Succeeded()) {
|
|
|
|
auto message = message_result.Unwrap();
|
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
|
|
|
|
read_buffer.Read(buffer.data(), 0, buffer_size);
|
|
|
|
const u32 bytes_written =
|
|
|
|
message->backend->Write(0, buffer_size, true, buffer.data()).Unwrap();
|
|
|
|
message->backend->Close();
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
|
|
|
}
|
|
|
|
|
2018-07-29 16:43:13 +00:00
|
|
|
rb.PushMappedBuffer(read_buffer);
|
|
|
|
rb.PushMappedBuffer(hmac_key_buffer);
|
|
|
|
rb.PushMappedBuffer(message_id_buffer);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called, ncch_program_id={:#010x}, is_outbox={}",
|
|
|
|
ncch_program_id, is_outbox);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::Delete(Kernel::HLERequestContext& ctx) {
|
2018-08-06 06:39:47 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x08, 4, 2);
|
2018-07-26 04:54:11 +00:00
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
2018-08-07 17:45:11 +00:00
|
|
|
const CecDataPathType path_type = rp.PopEnum<CecDataPathType>();
|
2018-08-06 06:39:47 +00:00
|
|
|
const bool is_outbox = rp.Pop<bool>();
|
2018-07-26 04:54:11 +00:00
|
|
|
const u32 message_id_size = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
auto& message_id_buffer = rp.PopMappedBuffer();
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (is_outbox) {
|
|
|
|
|
|
|
|
} else { /// otherwise inbox
|
|
|
|
}
|
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.PushMappedBuffer(message_id_buffer);
|
|
|
|
|
|
|
|
LOG_WARNING(Service_CECD,
|
|
|
|
"(STUBBED) called, ncch_program_id={:#010x}, path_type={:#04x}, is_outbox={}",
|
|
|
|
ncch_program_id, static_cast<u32>(path_type), is_outbox);
|
|
|
|
}
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
void Module::Interface::Cecd_0x000900C2(Kernel::HLERequestContext& ctx) { /// DeleteFile?
|
2018-08-06 06:39:47 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x09, 3, 2);
|
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
|
|
|
const u32 size = rp.Pop<u32>();
|
|
|
|
const u32 option = rp.Pop<u32>();
|
|
|
|
auto& message_id_buffer = rp.PopMappedBuffer();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
2018-07-29 16:43:13 +00:00
|
|
|
rb.PushMappedBuffer(message_id_buffer);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
|
|
|
LOG_WARNING(Service_CECD,
|
2018-08-06 06:39:47 +00:00
|
|
|
"(STUBBED) called, ncch_program_id={:#010x}, size={:#010x}, option={:#010x}",
|
|
|
|
ncch_program_id, size, option);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::GetSystemInfo(Kernel::HLERequestContext& ctx) {
|
2018-08-06 06:39:47 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x0A, 3, 4);
|
2018-07-26 04:54:11 +00:00
|
|
|
const u32 dest_buffer_size = rp.Pop<u32>();
|
2018-08-07 17:45:11 +00:00
|
|
|
const CecSystemInfoType info_type = rp.PopEnum<CecSystemInfoType>();
|
2018-07-26 04:54:11 +00:00
|
|
|
const u32 param_buffer_size = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
auto& param_buffer = rp.PopMappedBuffer();
|
2018-07-29 16:43:13 +00:00
|
|
|
auto& dest_buffer = rp.PopMappedBuffer();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
/// TODO: Other CecSystemInfoTypes
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
|
2018-08-06 06:39:47 +00:00
|
|
|
std::vector<u8> buffer;
|
|
|
|
switch (info_type) {
|
|
|
|
case CecSystemInfoType::EulaVersion: /// TODO: Read config Eula version
|
|
|
|
buffer = {0xFF, 0xFF};
|
|
|
|
dest_buffer.Write(buffer.data(), 0, buffer.size());
|
|
|
|
break;
|
|
|
|
case CecSystemInfoType::Eula:
|
|
|
|
buffer = {0x01}; /// Eula agreed
|
|
|
|
dest_buffer.Write(buffer.data(), 0, buffer.size());
|
|
|
|
break;
|
|
|
|
case CecSystemInfoType::ParentControl:
|
|
|
|
buffer = {0x00}; /// No parent control
|
|
|
|
dest_buffer.Write(buffer.data(), 0, buffer.size());
|
|
|
|
break;
|
|
|
|
default:
|
2018-08-12 07:26:45 +00:00
|
|
|
LOG_ERROR(Service_CECD, "Unknown system info type={}", static_cast<u32>(info_type));
|
2018-08-06 06:39:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 04:54:11 +00:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
2018-07-29 16:43:13 +00:00
|
|
|
rb.PushMappedBuffer(param_buffer);
|
|
|
|
rb.PushMappedBuffer(dest_buffer);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
2018-08-17 06:32:48 +00:00
|
|
|
"called, dest_buffer_size={:#010x}, info_type={:#010x}, "
|
2018-08-06 06:39:47 +00:00
|
|
|
"param_buffer_size={:#010x}",
|
|
|
|
dest_buffer_size, static_cast<u32>(info_type), param_buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::RunCommand(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x0B, 1, 0);
|
2018-08-07 17:45:11 +00:00
|
|
|
const CecCommand command = rp.PopEnum<CecCommand>();
|
2018-08-06 06:39:47 +00:00
|
|
|
|
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
|
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called, command={}", static_cast<u32>(command));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::RunCommandAlt(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x0C, 1, 0);
|
2018-08-07 17:45:11 +00:00
|
|
|
const CecCommand command = rp.PopEnum<CecCommand>();
|
2018-08-06 06:39:47 +00:00
|
|
|
|
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
|
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called, command={}", static_cast<u32>(command));
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 03:27:57 +00:00
|
|
|
void Module::Interface::GetCecStateAbbreviated(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x0E, 0, 0);
|
2016-04-08 19:39:52 +00:00
|
|
|
|
2018-07-01 03:27:57 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
2018-07-01 16:38:40 +00:00
|
|
|
rb.PushEnum(CecStateAbbreviated::CEC_STATE_ABBREV_IDLE);
|
2016-04-08 19:39:52 +00:00
|
|
|
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called");
|
2016-04-08 19:39:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 03:27:57 +00:00
|
|
|
void Module::Interface::GetCecInfoEventHandle(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x0F, 0, 0);
|
2016-03-31 09:21:02 +00:00
|
|
|
|
2018-07-01 03:27:57 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.PushCopyObjects(cecd->cecinfo_event);
|
2016-03-31 09:21:02 +00:00
|
|
|
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called");
|
2016-03-31 09:21:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 03:27:57 +00:00
|
|
|
void Module::Interface::GetChangeStateEventHandle(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x10, 0, 0);
|
2015-06-11 22:12:16 +00:00
|
|
|
|
2018-07-01 03:27:57 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.PushCopyObjects(cecd->change_state_event);
|
2016-03-31 09:21:02 +00:00
|
|
|
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called");
|
2016-03-31 09:21:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 04:54:11 +00:00
|
|
|
void Module::Interface::OpenAndWrite(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x11, 4, 4);
|
|
|
|
const u32 buffer_size = rp.Pop<u32>();
|
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
2018-08-07 17:45:11 +00:00
|
|
|
const CecDataPathType path_type = rp.PopEnum<CecDataPathType>();
|
2018-08-06 06:39:47 +00:00
|
|
|
CecOpenMode open_mode;
|
|
|
|
open_mode.raw = rp.Pop<u32>();
|
2018-07-26 04:54:11 +00:00
|
|
|
rp.PopPID();
|
2018-07-29 16:43:13 +00:00
|
|
|
auto& read_buffer = rp.PopMappedBuffer();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-07-29 16:43:13 +00:00
|
|
|
FileSys::Path path(cecd->GetCecDataPathTypeAsString(path_type, ncch_program_id).data());
|
2018-08-06 06:39:47 +00:00
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.write_flag.Assign(1);
|
|
|
|
mode.create_flag.Assign(1);
|
2018-07-29 16:43:13 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
|
|
|
switch (path_type) {
|
|
|
|
case CecDataPathType::CEC_PATH_ROOT_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_DIR:
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC,
|
|
|
|
ErrorSummary::NotFound, ErrorLevel::Status));
|
|
|
|
break;
|
|
|
|
default: /// If not directory, then it is a file
|
|
|
|
auto file_result =
|
|
|
|
Service::FS::OpenFileFromArchive(cecd->cecd_system_save_data_archive, path, mode);
|
|
|
|
if (file_result.Succeeded()) {
|
|
|
|
auto file = file_result.Unwrap();
|
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
read_buffer.Read(buffer.data(), 0, buffer_size);
|
2018-08-17 06:32:48 +00:00
|
|
|
|
|
|
|
if (open_mode.check) {
|
|
|
|
cecd->CheckAndUpdateFile(path_type, ncch_program_id, buffer);
|
|
|
|
}
|
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
const u32 bytes_written =
|
|
|
|
file->backend->Write(0, buffer_size, true, buffer.data()).Unwrap();
|
|
|
|
file->backend->Close();
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
} else {
|
2018-08-17 06:32:48 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "Failed to open file: {}", path.AsString());
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
|
|
|
}
|
2018-07-29 16:43:13 +00:00
|
|
|
}
|
|
|
|
rb.PushMappedBuffer(read_buffer);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"called, ncch_program_id={:#010x}, path_type={:#04x}, open_mode={:#010x}, path={}",
|
|
|
|
ncch_program_id, static_cast<u32>(path_type), open_mode.raw, path.AsString());
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::OpenAndRead(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x12, 4, 4);
|
|
|
|
const u32 buffer_size = rp.Pop<u32>();
|
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
2018-08-07 17:45:11 +00:00
|
|
|
const CecDataPathType path_type = rp.PopEnum<CecDataPathType>();
|
2018-08-06 06:39:47 +00:00
|
|
|
CecOpenMode open_mode;
|
|
|
|
open_mode.raw = rp.Pop<u32>();
|
2018-07-26 04:54:11 +00:00
|
|
|
rp.PopPID();
|
2018-07-29 16:43:13 +00:00
|
|
|
auto& write_buffer = rp.PopMappedBuffer();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-07-29 16:43:13 +00:00
|
|
|
FileSys::Path path(cecd->GetCecDataPathTypeAsString(path_type, ncch_program_id).data());
|
2018-08-06 06:39:47 +00:00
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.read_flag.Assign(1);
|
2018-07-29 16:43:13 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
|
|
|
|
switch (path_type) {
|
|
|
|
case CecDataPathType::CEC_PATH_ROOT_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_DIR:
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC,
|
|
|
|
ErrorSummary::NotFound, ErrorLevel::Status));
|
2018-08-17 06:32:48 +00:00
|
|
|
rb.Push<u32>(0); /// No entries read
|
2018-08-06 06:39:47 +00:00
|
|
|
break;
|
|
|
|
default: /// If not directory, then it is a file
|
|
|
|
auto file_result =
|
|
|
|
Service::FS::OpenFileFromArchive(cecd->cecd_system_save_data_archive, path, mode);
|
|
|
|
if (file_result.Succeeded()) {
|
|
|
|
auto file = file_result.Unwrap();
|
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
|
|
|
|
const u32 bytes_read = file->backend->Read(0, buffer_size, buffer.data()).Unwrap();
|
|
|
|
write_buffer.Write(buffer.data(), 0, buffer_size);
|
|
|
|
file->backend->Close();
|
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(bytes_read);
|
|
|
|
} else {
|
2018-08-17 06:32:48 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "Failed to open file: {}", path.AsString());
|
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
|
|
|
rb.Push<u32>(0); /// No bytes read
|
|
|
|
}
|
2018-07-29 16:43:13 +00:00
|
|
|
}
|
|
|
|
rb.PushMappedBuffer(write_buffer);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"called, ncch_program_id={:#010x}, path_type={:#04x}, open_mode={:#010x}, path={}",
|
|
|
|
ncch_program_id, static_cast<u32>(path_type), open_mode.raw, path.AsString());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Module::EncodeBase64(const std::vector<u8>& in, const std::string& dictionary) const {
|
|
|
|
std::string out;
|
|
|
|
out.reserve((in.size() * 4) / 3);
|
|
|
|
int b;
|
|
|
|
for (int i = 0; i < in.size(); i += 3) {
|
|
|
|
b = (in[i] & 0xFC) >> 2;
|
|
|
|
out += dictionary[b];
|
|
|
|
b = (in[i] & 0x03) << 4;
|
|
|
|
if (i + 1 < in.size()) {
|
|
|
|
b |= (in[i + 1] & 0xF0) >> 4;
|
|
|
|
out += dictionary[b];
|
|
|
|
b = (in[i + 1] & 0x0F) << 2;
|
|
|
|
if (i + 2 < in.size()) {
|
|
|
|
b |= (in[i + 2] & 0xC0) >> 6;
|
|
|
|
out += dictionary[b];
|
|
|
|
b = in[i + 2] & 0x3F;
|
|
|
|
out += dictionary[b];
|
|
|
|
} else {
|
|
|
|
out += dictionary[b];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out += dictionary[b];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
2018-07-29 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Module::GetCecDataPathTypeAsString(const CecDataPathType type, const u32 program_id,
|
2018-08-06 06:39:47 +00:00
|
|
|
const std::vector<u8>& msg_id) const {
|
|
|
|
switch (type) {
|
2018-07-29 16:43:13 +00:00
|
|
|
case CecDataPathType::CEC_PATH_MBOX_LIST:
|
|
|
|
return "/CEC/MBoxList____";
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_INFO:
|
|
|
|
return Common::StringFromFormat("/CEC/%08x/MBoxInfo____", program_id);
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_INFO:
|
|
|
|
return Common::StringFromFormat("/CEC/%08x/InBox___/BoxInfo_____", program_id);
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_INFO:
|
|
|
|
return Common::StringFromFormat("/CEC/%08x/OutBox__/BoxInfo_____", program_id);
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_INDEX:
|
|
|
|
return Common::StringFromFormat("/CEC/%08x/OutBox__/OBIndex_____", program_id);
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_MSG:
|
2018-08-06 06:39:47 +00:00
|
|
|
return Common::StringFromFormat("/CEC/%08x/InBox___/_%08x", program_id,
|
|
|
|
EncodeBase64(msg_id, base64_dict).data());
|
2018-07-29 16:43:13 +00:00
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_MSG:
|
2018-08-06 06:39:47 +00:00
|
|
|
return Common::StringFromFormat("/CEC/%08x/OutBox__/_%08x", program_id,
|
|
|
|
EncodeBase64(msg_id, base64_dict).data());
|
2018-07-29 16:43:13 +00:00
|
|
|
case CecDataPathType::CEC_PATH_ROOT_DIR:
|
|
|
|
return "/CEC";
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_DIR:
|
|
|
|
return Common::StringFromFormat("/CEC/%08x", program_id);
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_DIR:
|
|
|
|
return Common::StringFromFormat("/CEC/%08x/InBox___", program_id);
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_DIR:
|
|
|
|
return Common::StringFromFormat("/CEC/%08x/OutBox__", program_id);
|
|
|
|
case CecDataPathType::CEC_MBOX_DATA:
|
|
|
|
case CecDataPathType::CEC_MBOX_ICON:
|
|
|
|
case CecDataPathType::CEC_MBOX_TITLE:
|
|
|
|
default:
|
|
|
|
return Common::StringFromFormat("/CEC/%08x/MBoxData.%03d", program_id,
|
|
|
|
static_cast<u32>(type) - 100);
|
|
|
|
}
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
void Module::CheckAndUpdateFile(const CecDataPathType path_type, const u32 ncch_program_id,
|
|
|
|
std::vector<u8>& file_buffer) {
|
|
|
|
switch (path_type) {
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_LIST:
|
|
|
|
break;
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_INFO: {
|
|
|
|
CecMBoxInfoHeader mbox_info_header = {};
|
|
|
|
std::memcpy(&mbox_info_header, file_buffer.data(), sizeof(CecMBoxInfoHeader));
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (file_buffer.size() != sizeof(CecMBoxInfoHeader)) { /// 0x60
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader size is incorrect");
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (mbox_info_header.magic != 0x6363) { /// 'cc'
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader magic number is incorrect");
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (mbox_info_header.program_id != static_cast<u32_le>(ncch_program_id)) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader program id does not match current id");
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_INFO: {
|
|
|
|
CecInOutBoxInfoHeader inbox_info_header = {};
|
|
|
|
std::memcpy(&inbox_info_header, file_buffer.data(), sizeof(CecInOutBoxInfoHeader));
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (inbox_info_header.magic != 0x6262) { /// 'bb'
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader magic number is incorrect");
|
|
|
|
inbox_info_header.magic = 0x6262;
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (inbox_info_header.box_info_size != file_buffer.size()) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader box info size is incorrect");
|
|
|
|
inbox_info_header.box_info_size = sizeof(CecInOutBoxInfoHeader);
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (inbox_info_header.max_box_size > 0x100000) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max box size is incorrect");
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (inbox_info_header.max_message_num > 99) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max box size is incorrect");
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (inbox_info_header.max_message_size >= 0x019000) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max message size is incorrect");
|
|
|
|
}
|
2018-08-14 21:57:13 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (inbox_info_header.max_batch_size == 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max batch size is not set");
|
|
|
|
inbox_info_header.max_batch_size = inbox_info_header.max_message_num;
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
std::memcpy(file_buffer.data(), &inbox_info_header, sizeof(CecInOutBoxInfoHeader));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_INFO: {
|
|
|
|
CecInOutBoxInfoHeader outbox_info_header = {};
|
|
|
|
std::memcpy(&outbox_info_header, file_buffer.data(), sizeof(CecInOutBoxInfoHeader));
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (outbox_info_header.magic != 0x6262) { /// 'bb'
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader magic number is incorrect");
|
|
|
|
outbox_info_header.magic = 0x6262;
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (outbox_info_header.box_info_size != file_buffer.size()) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader box info size is incorrect");
|
|
|
|
outbox_info_header.box_info_size = sizeof(CecInOutBoxInfoHeader);
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (outbox_info_header.max_box_size > 0x100000) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max box size is incorrect");
|
|
|
|
}
|
2018-08-14 21:57:13 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (outbox_info_header.max_message_num > 99) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max box size is incorrect");
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (outbox_info_header.max_message_size >= 0x019000) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max message size is incorrect");
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (outbox_info_header.max_batch_size == 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max batch size is not set");
|
|
|
|
outbox_info_header.max_batch_size = outbox_info_header.max_message_num;
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
std::memcpy(file_buffer.data(), &outbox_info_header, sizeof(CecInOutBoxInfoHeader));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_INDEX:
|
|
|
|
break;
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_MSG:
|
|
|
|
break;
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_MSG:
|
|
|
|
break;
|
|
|
|
case CecDataPathType::CEC_PATH_ROOT_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_MBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_INBOX_DIR:
|
|
|
|
case CecDataPathType::CEC_PATH_OUTBOX_DIR:
|
|
|
|
break;
|
|
|
|
case CecDataPathType::CEC_MBOX_DATA:
|
|
|
|
case CecDataPathType::CEC_MBOX_ICON:
|
|
|
|
case CecDataPathType::CEC_MBOX_TITLE:
|
|
|
|
default: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
/// MBoxInfo____ resides in the MBox /CEC/<id> directory,
|
|
|
|
/// The Magic number is 0x6363 'cc', and has size 0x60
|
|
|
|
constexpr u32 mbox_info_size = 0x60;
|
|
|
|
auto mbox_info = mbox_info_result.Unwrap();
|
|
|
|
std::vector<u8> mbox_info_buffer(mbox_info_size);
|
|
|
|
u32_le le_ncch_program_id = ncch_program_id;
|
|
|
|
|
|
|
|
CecMBoxInfoHeader mbox_info_header = {};
|
|
|
|
std::memset(&mbox_info_header, 0, sizeof(CecMBoxInfoHeader));
|
|
|
|
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
|
|
|
|
std::tm* now_tm = std::localtime(&now_time_t);
|
|
|
|
|
|
|
|
mbox_info_header.magic = 0x6363;
|
|
|
|
/// mbox_info_header.padding already zeroed
|
|
|
|
mbox_info_header.title_id = ncch_program_id;
|
|
|
|
/// mbox_info_header.private_id = unknown...
|
|
|
|
mbox_info_header.flag = 0x00; /// unknown
|
|
|
|
mbox_info_header.flag2 = 0x01; /// unknown
|
|
|
|
/// mbox_info_header.padding2 already zeroed
|
|
|
|
/// mbox_info_header.hmac_key = unknown...
|
|
|
|
/// mbox_info_header.padding3 already zeroed
|
|
|
|
mbox_info_header.last_accessed.year = static_cast<u32_le>(1900 + now_tm->tm_year);
|
|
|
|
mbox_info_header.last_accessed.month = static_cast<u8>(1 + now_tm->tm_mon);
|
|
|
|
mbox_info_header.last_accessed.day = static_cast<u8>(now_tm->tm_mday);
|
|
|
|
mbox_info_header.last_accessed.hour = static_cast<u8>(now_tm->tm_hour);
|
|
|
|
mbox_info_header.last_accessed.minute = static_cast<u8>(now_tm->tm_min);
|
|
|
|
mbox_info_header.last_accessed.second = static_cast<u8>(now_tm->tm_sec);
|
|
|
|
/// mbox_info_header.padding4 already zeroed
|
|
|
|
/// mbox_info_header.last_received = zeroed until receive?
|
|
|
|
/// mbox_info_header.padding5 already zeroed
|
|
|
|
/// mbox_info_header.unknown_time, might be sent-time, might be padding?
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
/// BoxInfo_____ resides in both the InBox and OutBox directories,
|
|
|
|
/// The Magic number is 0x6262 'bb', and has size 0x20-byte header, and an array of 0x70-byte
|
|
|
|
/// entries. Each entry is a copy of the message header.
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
/// BoxInfo_____ resides in both the InBox and OutBox directories,
|
|
|
|
/// The Magic number is 0x6262 'bb', and has size 0x20-byte header, and an array of 0x70-byte
|
|
|
|
/// entries. Each entry is a copy of the message header.
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
/// OBIndex_____ resides in the OutBox directory,
|
|
|
|
/// The magic number is 0x6767 'gg', and the total size is 0x10
|
2018-08-14 07:41:14 +00:00
|
|
|
|
|
|
|
/// MBoxData.001 resides in the MBox /CEC/<id> directory and contains the icon of the app
|
2018-08-15 05:47:33 +00:00
|
|
|
std::vector<u8> mbox_icon_buffer;
|
|
|
|
Core::System::GetInstance().GetAppLoader().ReadIcon(mbox_icon_buffer);
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-15 05:47:33 +00:00
|
|
|
const u32 mbox_icon_size = mbox_icon_buffer.size() - 0x24C0; // offset where icon starts
|
|
|
|
auto mbox_icon = mbox_icon_result.Unwrap();
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-15 05:47:33 +00:00
|
|
|
mbox_icon->backend->Write(0, mbox_icon_size, true, &mbox_icon_buffer[0x24C0]);
|
2018-08-14 07:41:14 +00:00
|
|
|
mbox_icon->backend->Close();
|
|
|
|
|
|
|
|
/// MBoxData.010 resides in the MBox /CEC/<id> directory and contains the title of the app
|
|
|
|
/// in null-terminated UTF-16 string.
|
2018-08-15 05:47:33 +00:00
|
|
|
std::string title_name;
|
|
|
|
Core::System::GetInstance().GetAppLoader().ReadTitle(title_name);
|
|
|
|
|
|
|
|
std::u16string program_name = Common::UTF8ToUTF16(title_name + '\0');
|
|
|
|
|
|
|
|
const u32 mbox_title_size = program_name.size();
|
2018-08-14 07:41:14 +00:00
|
|
|
auto mbox_title = mbox_title_result.Unwrap();
|
|
|
|
std::vector<u8> mbox_title_buffer(mbox_title_size);
|
2018-08-15 05:47:33 +00:00
|
|
|
std::memcpy(mbox_title_buffer.data(), program_name.data(), mbox_title_size);
|
2018-08-14 07:41:14 +00:00
|
|
|
|
|
|
|
/// MBoxData.050 resides in the MBox /CEC/<id> directory and contains the program id of the app
|
|
|
|
std::vector<u8> program_id_buffer(8);
|
|
|
|
u64_le le_program_id = Kernel::g_current_process->codeset->program_id;
|
|
|
|
std::memcpy(program_id_buffer.data(), &le_program_id, sizeof(u64));
|
|
|
|
|
|
|
|
mbox_program_id->backend->Write(0, sizeof(u64), true, program_id_buffer.data());
|
|
|
|
mbox_program_id->backend->Close();
|
|
|
|
}
|
2018-08-17 06:32:48 +00:00
|
|
|
*/
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
Module::SessionData::SessionData() {}
|
|
|
|
|
|
|
|
Module::SessionData::~SessionData() {
|
|
|
|
if (file)
|
|
|
|
file->backend->Close();
|
|
|
|
}
|
|
|
|
|
2018-07-01 03:27:57 +00:00
|
|
|
Module::Interface::Interface(std::shared_ptr<Module> cecd, const char* name, u32 max_session)
|
|
|
|
: ServiceFramework(name, max_session), cecd(std::move(cecd)) {}
|
2016-03-31 09:21:02 +00:00
|
|
|
|
2018-07-01 03:27:57 +00:00
|
|
|
Module::Module() {
|
|
|
|
using namespace Kernel;
|
|
|
|
cecinfo_event = Event::Create(Kernel::ResetType::OneShot, "CECD::cecinfo_event");
|
|
|
|
change_state_event = Event::Create(Kernel::ResetType::OneShot, "CECD::change_state_event");
|
2018-07-29 16:43:13 +00:00
|
|
|
|
|
|
|
// Open the SystemSaveData archive 0x00010026
|
|
|
|
FileSys::Path archive_path(cecd_system_savedata_id);
|
|
|
|
auto archive_result =
|
|
|
|
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SystemSaveData, archive_path);
|
|
|
|
|
|
|
|
// If the archive didn't exist, create the files inside
|
|
|
|
if (archive_result.Code() == FileSys::ERR_NOT_FORMATTED) {
|
|
|
|
// Format the archive to create the directories
|
|
|
|
Service::FS::FormatArchive(Service::FS::ArchiveIdCode::SystemSaveData,
|
|
|
|
FileSys::ArchiveFormatInfo(), archive_path);
|
|
|
|
|
|
|
|
// Open it again to get a valid archive now that the folder exists
|
|
|
|
archive_result =
|
|
|
|
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SystemSaveData, archive_path);
|
2018-08-12 07:26:45 +00:00
|
|
|
|
|
|
|
/// Now that the archive is formatted, we need to create the root CEC directory,
|
|
|
|
/// eventlog.dat, and CEC/MBoxList____
|
|
|
|
const FileSys::Path root_dir_path(
|
|
|
|
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_ROOT_DIR, 0).data());
|
|
|
|
Service::FS::CreateDirectoryFromArchive(*archive_result, root_dir_path);
|
|
|
|
|
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.write_flag.Assign(1);
|
|
|
|
mode.create_flag.Assign(1);
|
|
|
|
|
|
|
|
/// eventlog.dat resides in the root of the archive beside the CEC directory
|
|
|
|
/// Initially created, at offset 0x0, are bytes 0x01 0x41 0x12, followed by
|
|
|
|
/// zeroes until offset 0x1000, where it changes to 0xDD until the end of file
|
|
|
|
/// at offset 0x30d53
|
|
|
|
FileSys::Path eventlog_path("/eventlog.dat");
|
|
|
|
|
|
|
|
auto eventlog_result =
|
|
|
|
Service::FS::OpenFileFromArchive(*archive_result, eventlog_path, mode);
|
|
|
|
|
|
|
|
constexpr u32 eventlog_size = 0x30d54;
|
|
|
|
auto eventlog = eventlog_result.Unwrap();
|
|
|
|
std::vector<u8> eventlog_buffer(eventlog_size);
|
|
|
|
|
|
|
|
std::memset(&eventlog_buffer[0], 0, 0x1000);
|
|
|
|
eventlog_buffer[0] = 0x01;
|
|
|
|
eventlog_buffer[1] = 0x41;
|
|
|
|
eventlog_buffer[2] = 0x12;
|
|
|
|
std::memset(&eventlog_buffer[0x1000], 0xDD, eventlog_size - 0x1000);
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
eventlog->backend->Write(0, eventlog_size, true, eventlog_buffer.data());
|
2018-08-12 07:26:45 +00:00
|
|
|
eventlog->backend->Close();
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
/*
|
|
|
|
/// MBoxList____ resides within the root CEC/ directory.
|
|
|
|
/// Initially created, at offset 0x0, are bytes 0x68 0x68 0x00 0x00 0x01, with
|
|
|
|
0x6868 'hh',
|
|
|
|
/// being the magic number. The rest of the file is filled with zeroes, until the
|
|
|
|
end of
|
|
|
|
/// file at offset 0x18b
|
|
|
|
FileSys::Path mboxlist_path(
|
|
|
|
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_MBOX_LIST, 0).data());
|
|
|
|
|
|
|
|
auto mboxlist_result =
|
|
|
|
Service::FS::OpenFileFromArchive(*archive_result, mboxlist_path, mode);
|
|
|
|
|
|
|
|
constexpr u32 mboxlist_size = 0x18c;
|
|
|
|
auto mboxlist = mboxlist_result.Unwrap();
|
|
|
|
std::vector<u8> mboxlist_buffer(mboxlist_size);
|
|
|
|
|
|
|
|
std::memset(&mboxlist_buffer[0], 0, mboxlist_size);
|
|
|
|
mboxlist_buffer[0] = 0x68;
|
|
|
|
mboxlist_buffer[1] = 0x68;
|
|
|
|
/// mboxlist_buffer[2-3] are already zeroed
|
|
|
|
mboxlist_buffer[4] = 0x01;
|
|
|
|
|
|
|
|
mboxlist->backend->Write(0, mboxlist_size, true, mboxlist_buffer.data());
|
|
|
|
mboxlist->backend->Close();
|
|
|
|
*/
|
2018-07-29 16:43:13 +00:00
|
|
|
}
|
|
|
|
ASSERT_MSG(archive_result.Succeeded(), "Could not open the CECD SystemSaveData archive!");
|
|
|
|
|
|
|
|
cecd_system_save_data_archive = *archive_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::~Module() {
|
2018-08-06 06:39:47 +00:00
|
|
|
if (cecd_system_save_data_archive)
|
2018-07-29 16:43:13 +00:00
|
|
|
Service::FS::CloseArchive(cecd_system_save_data_archive);
|
2015-06-11 22:12:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 03:27:57 +00:00
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager) {
|
|
|
|
auto cecd = std::make_shared<Module>();
|
|
|
|
std::make_shared<CECD_NDM>(cecd)->InstallAsService(service_manager);
|
|
|
|
std::make_shared<CECD_S>(cecd)->InstallAsService(service_manager);
|
|
|
|
std::make_shared<CECD_U>(cecd)->InstallAsService(service_manager);
|
2015-06-11 22:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace CECD
|
|
|
|
} // namespace Service
|