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-09-08 03:09:14 +00:00
|
|
|
#include <cryptopp/base64.h>
|
2018-09-10 08:21:24 +00:00
|
|
|
#include <cryptopp/hmac.h>
|
2018-09-10 23:53:23 +00:00
|
|
|
#include <cryptopp/sha.h>
|
2018-09-28 20:47:27 +00:00
|
|
|
#include "common/common_paths.h"
|
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-10-05 14:59:43 +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-30 17:48:46 +00:00
|
|
|
#include "fmt/format.h"
|
2018-07-01 03:27:57 +00:00
|
|
|
|
2018-09-22 12:23:08 +00:00
|
|
|
namespace Service::CECD {
|
2015-06-11 22:12:16 +00:00
|
|
|
|
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) {
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::RootDir:
|
|
|
|
case CecDataPathType::MboxDir:
|
|
|
|
case CecDataPathType::InboxDir:
|
|
|
|
case CecDataPathType::OutboxDir: {
|
2018-09-28 20:47:27 +00:00
|
|
|
auto dir_result = cecd->cecd_system_save_data_archive->OpenDirectory(path);
|
2018-08-06 06:39:47 +00:00
|
|
|
if (dir_result.Failed()) {
|
2018-08-17 06:32:48 +00:00
|
|
|
if (open_mode.create) {
|
2018-09-28 20:47:27 +00:00
|
|
|
cecd->cecd_system_save_data_archive->CreateDirectory(path);
|
2018-08-06 06:39:47 +00:00
|
|
|
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));
|
|
|
|
}
|
2018-09-06 19:48:00 +00:00
|
|
|
rb.Push<u32>(0); // Zero entries
|
2018-08-06 06:39:47 +00:00
|
|
|
} else {
|
2018-09-06 19:48:00 +00:00
|
|
|
constexpr u32 max_entries = 32; // reasonable value, just over max boxes 24
|
2018-09-28 20:47:27 +00:00
|
|
|
auto directory = std::move(dir_result).Unwrap();
|
2018-08-23 07:13:10 +00:00
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
// Actual reading into vector seems to be required for entry count
|
2018-08-23 07:13:10 +00:00
|
|
|
std::vector<FileSys::Entry> entries(max_entries);
|
2018-09-28 20:47:27 +00:00
|
|
|
const u32 entry_count = directory->Read(max_entries, entries.data());
|
2018-08-23 07:13:10 +00:00
|
|
|
|
2018-08-25 06:31:01 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "Number of entries found: {}", entry_count);
|
2018-08-23 07:13:10 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
2018-09-06 19:48:00 +00:00
|
|
|
rb.Push<u32>(entry_count); // Entry count
|
2018-09-28 20:47:27 +00:00
|
|
|
directory->Close();
|
2018-08-06 06:39:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-09-06 19:48:00 +00:00
|
|
|
default: { // If not directory, then it is a file
|
2018-09-28 20:47:27 +00:00
|
|
|
auto file_result = cecd->cecd_system_save_data_archive->OpenFile(path, mode);
|
2018-08-06 06:39:47 +00:00
|
|
|
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));
|
2018-09-06 19:48:00 +00:00
|
|
|
rb.Push<u32>(0); // No file size
|
2018-08-06 06:39:47 +00:00
|
|
|
} else {
|
2018-09-28 20:47:27 +00:00
|
|
|
session_data->file = std::move(file_result).Unwrap();
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
2018-09-28 20:47:27 +00:00
|
|
|
rb.Push<u32>(session_data->file->GetSize()); // Return file size
|
2018-08-06 06:39:47 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 23:36:33 +00:00
|
|
|
if (path_type == CecDataPathType::MboxProgramId) {
|
2018-08-06 06:39:47 +00:00
|
|
|
std::vector<u8> program_id(8);
|
2018-10-17 19:23:56 +00:00
|
|
|
u64_le le_program_id = cecd->system.Kernel().GetCurrentProcess()->codeset->program_id;
|
2018-08-06 06:39:47 +00:00
|
|
|
std::memcpy(program_id.data(), &le_program_id, sizeof(u64));
|
2018-09-28 20:47:27 +00:00
|
|
|
session_data->file->Write(0, sizeof(u64), true, program_id.data());
|
|
|
|
session_data->file->Close();
|
2018-08-06 06:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
2018-08-19 07:05:41 +00:00
|
|
|
"called, ncch_program_id={:#010x}, path_type={:#04x}, path={}, "
|
|
|
|
"open_mode: raw={:#x}, unknown={}, read={}, write={}, create={}, check={}",
|
|
|
|
ncch_program_id, static_cast<u32>(path_type), path.AsString(), open_mode.raw,
|
|
|
|
open_mode.unknown, open_mode.read, open_mode.write, open_mode.create,
|
|
|
|
open_mode.check);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 06:31:01 +00:00
|
|
|
void Module::Interface::Read(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-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"SessionData: ncch_program_id={:#010x}, data_path_type={:#04x}, "
|
|
|
|
"path={}, open_mode: raw={:#x}, unknown={}, read={}, write={}, create={}, check={}",
|
|
|
|
session_data->ncch_program_id, static_cast<u32>(session_data->data_path_type),
|
|
|
|
session_data->path.AsString(), session_data->open_mode.raw,
|
|
|
|
session_data->open_mode.unknown, session_data->open_mode.read,
|
|
|
|
session_data->open_mode.write, session_data->open_mode.create,
|
|
|
|
session_data->open_mode.check);
|
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) {
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::RootDir:
|
|
|
|
case CecDataPathType::MboxDir:
|
|
|
|
case CecDataPathType::InboxDir:
|
|
|
|
case CecDataPathType::OutboxDir:
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC,
|
|
|
|
ErrorSummary::NotFound, ErrorLevel::Status));
|
2018-09-06 19:48:00 +00:00
|
|
|
rb.Push<u32>(0); // No bytes read
|
2018-08-06 06:39:47 +00:00
|
|
|
break;
|
2018-09-06 19:48:00 +00:00
|
|
|
default: // If not directory, then it is a file
|
2018-08-06 06:39:47 +00:00
|
|
|
std::vector<u8> buffer(write_buffer_size);
|
|
|
|
const u32 bytes_read =
|
2018-09-28 20:47:27 +00:00
|
|
|
session_data->file->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);
|
2018-09-28 20:47:27 +00:00
|
|
|
session_data->file->Close();
|
2018-08-06 06:39:47 +00:00
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(bytes_read);
|
|
|
|
}
|
|
|
|
rb.PushMappedBuffer(write_buffer);
|
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "called, write_buffer_size={:#x}, path={}", write_buffer_size,
|
2018-08-06 06:39:47 +00:00
|
|
|
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);
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
FileSys::Path message_path =
|
2018-09-07 23:36:33 +00:00
|
|
|
cecd->GetCecDataPathTypeAsString(is_outbox ? CecDataPathType::OutboxMsg
|
|
|
|
: CecDataPathType::InboxMsg,
|
2018-09-06 19:48:00 +00:00
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message_result = cecd->cecd_system_save_data_archive->OpenFile(message_path, mode);
|
2018-08-17 06:32:48 +00:00
|
|
|
|
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()) {
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message = std::move(message_result).Unwrap();
|
2018-08-17 06:32:48 +00:00
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
const u32 bytes_read = message->Read(0, buffer_size, buffer.data()).Unwrap();
|
2018-08-17 06:32:48 +00:00
|
|
|
write_buffer.Write(buffer.data(), 0, buffer_size);
|
2018-09-28 20:47:27 +00:00
|
|
|
message->Close();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
CecMessageHeader msg_header;
|
|
|
|
std::memcpy(&msg_header, buffer.data(), sizeof(CecMessageHeader));
|
2018-09-10 23:53:23 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"magic={:#06x}, message_size={:#010x}, header_size={:#010x}, "
|
|
|
|
"body_size={:#010x}, title_id={:#010x}, title_id_2={:#010x}, "
|
|
|
|
"batch_id={:#010x}",
|
|
|
|
msg_header.magic, msg_header.message_size, msg_header.header_size,
|
|
|
|
msg_header.body_size, msg_header.title_id, msg_header.title_id2,
|
|
|
|
msg_header.batch_id);
|
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"unknown_id={:#010x}, version={:#010x}, flag={:#04x}, "
|
|
|
|
"send_method={:#04x}, is_unopen={:#04x}, is_new={:#04x}, "
|
|
|
|
"sender_id={:#018x}, sender_id2={:#018x}, send_count={:#04x}, "
|
|
|
|
"forward_count={:#04x}, user_data={:#06x}, ",
|
|
|
|
msg_header.unknown_id, msg_header.version, msg_header.flag,
|
|
|
|
msg_header.send_method, msg_header.is_unopen, msg_header.is_new,
|
|
|
|
msg_header.sender_id, msg_header.sender_id2, msg_header.send_count,
|
|
|
|
msg_header.forward_count, msg_header.user_data);
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(bytes_read);
|
|
|
|
} else {
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
2018-09-06 19:48:00 +00:00
|
|
|
rb.Push<u32>(0); // zero bytes read
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
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-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(
|
|
|
|
Service_CECD,
|
|
|
|
"called, ncch_program_id={:#010x}, is_outbox={}, message_id_size={:#x}, buffer_size={:#x}",
|
|
|
|
ncch_program_id, is_outbox, message_id_size, buffer_size);
|
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);
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
FileSys::Path message_path =
|
2018-09-07 23:36:33 +00:00
|
|
|
cecd->GetCecDataPathTypeAsString(is_outbox ? CecDataPathType::OutboxMsg
|
|
|
|
: CecDataPathType::InboxMsg,
|
2018-09-06 19:48:00 +00:00
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message_result = cecd->cecd_system_save_data_archive->OpenFile(message_path, mode);
|
2018-08-17 06:32:48 +00:00
|
|
|
|
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()) {
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message = std::move(message_result).Unwrap();
|
2018-08-17 06:32:48 +00:00
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
const u32 bytes_read = message->Read(0, buffer_size, buffer.data()).Unwrap();
|
2018-08-17 06:32:48 +00:00
|
|
|
write_buffer.Write(buffer.data(), 0, buffer_size);
|
2018-09-28 20:47:27 +00:00
|
|
|
message->Close();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
CecMessageHeader msg_header;
|
|
|
|
std::memcpy(&msg_header, buffer.data(), sizeof(CecMessageHeader));
|
2018-09-10 23:53:23 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"magic={:#06x}, message_size={:#010x}, header_size={:#010x}, "
|
|
|
|
"body_size={:#010x}, title_id={:#010x}, title_id_2={:#010x}, "
|
|
|
|
"batch_id={:#010x}",
|
|
|
|
msg_header.magic, msg_header.message_size, msg_header.header_size,
|
|
|
|
msg_header.body_size, msg_header.title_id, msg_header.title_id2,
|
|
|
|
msg_header.batch_id);
|
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"unknown_id={:#010x}, version={:#010x}, flag={:#04x}, "
|
|
|
|
"send_method={:#04x}, is_unopen={:#04x}, is_new={:#04x}, "
|
|
|
|
"sender_id={:#018x}, sender_id2={:#018x}, send_count={:#04x}, "
|
|
|
|
"forward_count={:#04x}, user_data={:#06x}, ",
|
|
|
|
msg_header.unknown_id, msg_header.version, msg_header.flag,
|
|
|
|
msg_header.send_method, msg_header.is_unopen, msg_header.is_new,
|
|
|
|
msg_header.sender_id, msg_header.sender_id2, msg_header.send_count,
|
|
|
|
msg_header.forward_count, msg_header.user_data);
|
|
|
|
|
2018-09-10 23:53:23 +00:00
|
|
|
std::vector<u8> hmac_digest(0x20);
|
|
|
|
std::memcpy(hmac_digest.data(),
|
|
|
|
buffer.data() + msg_header.header_size + msg_header.body_size, 0x20);
|
|
|
|
|
|
|
|
std::vector<u8> message_body(msg_header.body_size);
|
|
|
|
std::memcpy(message_body.data(), buffer.data() + msg_header.header_size,
|
|
|
|
msg_header.body_size);
|
|
|
|
|
|
|
|
using namespace CryptoPP;
|
|
|
|
SecByteBlock key(0x20);
|
|
|
|
hmac_key_buffer.Read(key.data(), 0, key.size());
|
|
|
|
|
|
|
|
HMAC<SHA256> hmac(key, key.size());
|
|
|
|
|
|
|
|
const bool verify_hmac =
|
|
|
|
hmac.VerifyDigest(hmac_digest.data(), message_body.data(), message_body.size());
|
|
|
|
|
|
|
|
if (verify_hmac)
|
|
|
|
LOG_DEBUG(Service_CECD, "Verification succeeded");
|
|
|
|
else
|
|
|
|
LOG_DEBUG(Service_CECD, "Verification failed");
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(bytes_read);
|
|
|
|
} else {
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
2018-09-06 19:48:00 +00:00
|
|
|
rb.Push<u32>(0); // zero bytes read
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
|
|
|
|
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-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(
|
|
|
|
Service_CECD,
|
|
|
|
"called, ncch_program_id={:#010x}, is_outbox={}, message_id_size={:#x}, buffer_size={:#x}",
|
|
|
|
ncch_program_id, is_outbox, message_id_size, buffer_size);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 06:31:01 +00:00
|
|
|
void Module::Interface::Write(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-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"SessionData: ncch_program_id={:#010x}, data_path_type={:#04x}, "
|
|
|
|
"path={}, open_mode: raw={:#x}, unknown={}, read={}, write={}, create={}, check={}",
|
|
|
|
session_data->ncch_program_id, static_cast<u32>(session_data->data_path_type),
|
|
|
|
session_data->path.AsString(), session_data->open_mode.raw,
|
|
|
|
session_data->open_mode.unknown, session_data->open_mode.read,
|
|
|
|
session_data->open_mode.write, session_data->open_mode.create,
|
|
|
|
session_data->open_mode.check);
|
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) {
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::RootDir:
|
|
|
|
case CecDataPathType::MboxDir:
|
|
|
|
case CecDataPathType::InboxDir:
|
|
|
|
case CecDataPathType::OutboxDir:
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC,
|
|
|
|
ErrorSummary::NotFound, ErrorLevel::Status));
|
|
|
|
break;
|
2018-09-06 19:48:00 +00:00
|
|
|
default: // If not directory, then it is a file
|
2018-08-06 06:39:47 +00:00
|
|
|
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-09-28 20:47:27 +00:00
|
|
|
if (session_data->file->GetSize() != read_buffer_size) {
|
|
|
|
session_data->file->SetSize(read_buffer_size);
|
2018-09-10 08:21:24 +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 =
|
2018-09-28 20:47:27 +00:00
|
|
|
session_data->file->Write(0, buffer.size(), true, buffer.data()).Unwrap();
|
|
|
|
session_data->file->Close();
|
2018-08-06 06:39:47 +00:00
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
rb.PushMappedBuffer(read_buffer);
|
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "called, read_buffer_size={:#x}", 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);
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
FileSys::Path message_path =
|
2018-09-07 23:36:33 +00:00
|
|
|
cecd->GetCecDataPathTypeAsString(is_outbox ? CecDataPathType::OutboxMsg
|
|
|
|
: CecDataPathType::InboxMsg,
|
2018-09-06 19:48:00 +00:00
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message_result = cecd->cecd_system_save_data_archive->OpenFile(message_path, mode);
|
2018-08-17 06:32:48 +00:00
|
|
|
|
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()) {
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message = std::move(message_result).Unwrap();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-09-10 23:53:23 +00:00
|
|
|
std::vector<u8> buffer(buffer_size);
|
2018-08-17 06:32:48 +00:00
|
|
|
read_buffer.Read(buffer.data(), 0, buffer_size);
|
2018-09-10 23:53:23 +00:00
|
|
|
|
|
|
|
CecMessageHeader msg_header;
|
2018-09-10 08:21:24 +00:00
|
|
|
std::memcpy(&msg_header, buffer.data(), sizeof(CecMessageHeader));
|
2018-09-10 23:53:23 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"magic={:#06x}, message_size={:#010x}, header_size={:#010x}, "
|
|
|
|
"body_size={:#010x}, title_id={:#010x}, title_id_2={:#010x}, "
|
|
|
|
"batch_id={:#010x}",
|
|
|
|
msg_header.magic, msg_header.message_size, msg_header.header_size,
|
|
|
|
msg_header.body_size, msg_header.title_id, msg_header.title_id2,
|
|
|
|
msg_header.batch_id);
|
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"unknown_id={:#010x}, version={:#010x}, flag={:#04x}, "
|
|
|
|
"send_method={:#04x}, is_unopen={:#04x}, is_new={:#04x}, "
|
|
|
|
"sender_id={:#018x}, sender_id2={:#018x}, send_count={:#04x}, "
|
|
|
|
"forward_count={:#04x}, user_data={:#06x}, ",
|
|
|
|
msg_header.unknown_id, msg_header.version, msg_header.flag,
|
|
|
|
msg_header.send_method, msg_header.is_unopen, msg_header.is_new,
|
|
|
|
msg_header.sender_id, msg_header.sender_id2, msg_header.send_count,
|
|
|
|
msg_header.forward_count, msg_header.user_data);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
const u32 bytes_written = message->Write(0, buffer_size, true, buffer.data()).Unwrap();
|
|
|
|
message->Close();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
|
|
|
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-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(
|
|
|
|
Service_CECD,
|
|
|
|
"called, ncch_program_id={:#010x}, is_outbox={}, message_id_size={:#x}, buffer_size={:#x}",
|
|
|
|
ncch_program_id, is_outbox, message_id_size, buffer_size);
|
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);
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
FileSys::Path message_path =
|
2018-09-07 23:36:33 +00:00
|
|
|
cecd->GetCecDataPathTypeAsString(is_outbox ? CecDataPathType::OutboxMsg
|
|
|
|
: CecDataPathType::InboxMsg,
|
2018-09-06 19:48:00 +00:00
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message_result = cecd->cecd_system_save_data_archive->OpenFile(message_path, mode);
|
2018-08-17 06:32:48 +00:00
|
|
|
|
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()) {
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message = std::move(message_result).Unwrap();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-09-10 23:53:23 +00:00
|
|
|
std::vector<u8> buffer(buffer_size);
|
2018-08-17 06:32:48 +00:00
|
|
|
read_buffer.Read(buffer.data(), 0, buffer_size);
|
2018-09-10 23:53:23 +00:00
|
|
|
|
|
|
|
CecMessageHeader msg_header;
|
2018-09-10 08:21:24 +00:00
|
|
|
std::memcpy(&msg_header, buffer.data(), sizeof(CecMessageHeader));
|
2018-09-10 23:53:23 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"magic={:#06x}, message_size={:#010x}, header_size={:#010x}, "
|
|
|
|
"body_size={:#010x}, title_id={:#010x}, title_id_2={:#010x}, "
|
|
|
|
"batch_id={:#010x}",
|
|
|
|
msg_header.magic, msg_header.message_size, msg_header.header_size,
|
|
|
|
msg_header.body_size, msg_header.title_id, msg_header.title_id2,
|
|
|
|
msg_header.batch_id);
|
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"unknown_id={:#010x}, version={:#010x}, flag={:#04x}, "
|
|
|
|
"send_method={:#04x}, is_unopen={:#04x}, is_new={:#04x}, "
|
|
|
|
"sender_id={:#018x}, sender_id2={:#018x}, send_count={:#04x}, "
|
|
|
|
"forward_count={:#04x}, user_data={:#06x}, ",
|
|
|
|
msg_header.unknown_id, msg_header.version, msg_header.flag,
|
|
|
|
msg_header.send_method, msg_header.is_unopen, msg_header.is_new,
|
|
|
|
msg_header.sender_id, msg_header.sender_id2, msg_header.send_count,
|
|
|
|
msg_header.forward_count, msg_header.user_data);
|
|
|
|
|
2018-09-10 23:53:23 +00:00
|
|
|
const u32 hmac_offset = msg_header.header_size + msg_header.body_size;
|
|
|
|
const u32 hmac_size = 0x20;
|
|
|
|
|
|
|
|
std::vector<u8> hmac_digest(hmac_size);
|
|
|
|
std::vector<u8> message_body(msg_header.body_size);
|
|
|
|
std::memcpy(message_body.data(), buffer.data() + msg_header.header_size,
|
|
|
|
msg_header.body_size);
|
|
|
|
|
|
|
|
using namespace CryptoPP;
|
|
|
|
SecByteBlock key(hmac_size);
|
|
|
|
hmac_key_buffer.Read(key.data(), 0, hmac_size);
|
|
|
|
|
|
|
|
HMAC<SHA256> hmac(key, hmac_size);
|
|
|
|
hmac.CalculateDigest(hmac_digest.data(), message_body.data(), msg_header.body_size);
|
|
|
|
std::memcpy(buffer.data() + hmac_offset, hmac_digest.data(), hmac_size);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
const u32 bytes_written = message->Write(0, buffer_size, true, buffer.data()).Unwrap();
|
|
|
|
message->Close();
|
2018-08-17 06:32:48 +00:00
|
|
|
|
|
|
|
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-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(
|
|
|
|
Service_CECD,
|
|
|
|
"called, ncch_program_id={:#010x}, is_outbox={}, message_id_size={:#x}, buffer_size={:#x}",
|
|
|
|
ncch_program_id, is_outbox, message_id_size, buffer_size);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 00:27:46 +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-23 07:13:10 +00:00
|
|
|
FileSys::Path path(cecd->GetCecDataPathTypeAsString(path_type, ncch_program_id).data());
|
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.write_flag.Assign(1);
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-08-23 07:13:10 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
|
|
|
switch (path_type) {
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::RootDir:
|
|
|
|
case CecDataPathType::MboxDir:
|
|
|
|
case CecDataPathType::InboxDir:
|
|
|
|
case CecDataPathType::OutboxDir:
|
2018-09-28 20:47:27 +00:00
|
|
|
rb.Push(cecd->cecd_system_save_data_archive->DeleteDirectoryRecursively(path));
|
2018-08-23 07:13:10 +00:00
|
|
|
break;
|
2018-09-06 19:48:00 +00:00
|
|
|
default: // If not directory, then it is a file
|
2018-08-23 07:13:10 +00:00
|
|
|
if (message_id_size == 0) {
|
2018-09-28 20:47:27 +00:00
|
|
|
rb.Push(cecd->cecd_system_save_data_archive->DeleteFile(path));
|
2018-08-23 07:13:10 +00:00
|
|
|
} else {
|
|
|
|
std::vector<u8> id_buffer(message_id_size);
|
|
|
|
message_id_buffer.Read(id_buffer.data(), 0, message_id_size);
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
FileSys::Path message_path =
|
2018-09-07 23:36:33 +00:00
|
|
|
cecd->GetCecDataPathTypeAsString(is_outbox ? CecDataPathType::OutboxMsg
|
|
|
|
: CecDataPathType::InboxMsg,
|
2018-09-06 19:48:00 +00:00
|
|
|
ncch_program_id, id_buffer)
|
|
|
|
.data();
|
2018-09-28 20:47:27 +00:00
|
|
|
rb.Push(cecd->cecd_system_save_data_archive->DeleteFile(message_path));
|
2018-08-23 07:13:10 +00:00
|
|
|
}
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.PushMappedBuffer(message_id_buffer);
|
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
2018-08-23 07:13:10 +00:00
|
|
|
"called, ncch_program_id={:#010x}, path_type={:#04x}, path={}, "
|
2018-08-19 07:05:41 +00:00
|
|
|
"is_outbox={}, message_id_size={:#x}",
|
2018-08-23 07:13:10 +00:00
|
|
|
ncch_program_id, static_cast<u32>(path_type), path.AsString(), is_outbox,
|
|
|
|
message_id_size);
|
2018-08-06 06:39:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 06:31:01 +00:00
|
|
|
void Module::Interface::SetData(Kernel::HLERequestContext& ctx) {
|
2018-08-06 06:39:47 +00:00
|
|
|
IPC::RequestParser rp(ctx, 0x09, 3, 2);
|
|
|
|
const u32 ncch_program_id = rp.Pop<u32>();
|
2018-08-19 07:05:41 +00:00
|
|
|
const u32 buffer_size = rp.Pop<u32>();
|
2018-08-06 06:39:47 +00:00
|
|
|
const u32 option = rp.Pop<u32>();
|
2018-09-10 08:21:24 +00:00
|
|
|
auto& read_buffer = rp.PopMappedBuffer();
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
if (option == 2 && buffer_size > 0) { // update obindex?
|
|
|
|
FileSys::Path path(
|
|
|
|
cecd->GetCecDataPathTypeAsString(CecDataPathType::OutboxIndex, ncch_program_id).data());
|
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.write_flag.Assign(1);
|
|
|
|
mode.create_flag.Assign(1);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto file_result = cecd->cecd_system_save_data_archive->OpenFile(path, mode);
|
2018-09-10 08:21:24 +00:00
|
|
|
if (file_result.Succeeded()) {
|
2018-09-28 20:47:27 +00:00
|
|
|
auto file = std::move(file_result).Unwrap();
|
2018-09-10 08:21:24 +00:00
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
read_buffer.Read(buffer.data(), 0, buffer_size);
|
|
|
|
|
|
|
|
cecd->CheckAndUpdateFile(CecDataPathType::OutboxIndex, ncch_program_id, buffer);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
file->Write(0, buffer.size(), true, buffer.data());
|
|
|
|
file->Close();
|
2018-09-10 08:21:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-19 07:05:41 +00:00
|
|
|
|
2018-07-26 04:54:11 +00:00
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
2018-09-10 08:21:24 +00:00
|
|
|
rb.PushMappedBuffer(read_buffer);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "called, ncch_program_id={:#010x}, buffer_size={:#x}, option={:#x}",
|
|
|
|
ncch_program_id, buffer_size, option);
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 06:31:01 +00:00
|
|
|
void Module::Interface::ReadData(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-09-06 19:48:00 +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) {
|
2018-09-06 19:48:00 +00:00
|
|
|
case CecSystemInfoType::EulaVersion: // TODO: Read config Eula version
|
2018-08-06 06:39:47 +00:00
|
|
|
buffer = {0xFF, 0xFF};
|
|
|
|
dest_buffer.Write(buffer.data(), 0, buffer.size());
|
|
|
|
break;
|
|
|
|
case CecSystemInfoType::Eula:
|
2018-09-06 19:48:00 +00:00
|
|
|
buffer = {0x01}; // Eula agreed
|
2018-08-06 06:39:47 +00:00
|
|
|
dest_buffer.Write(buffer.data(), 0, buffer.size());
|
|
|
|
break;
|
|
|
|
case CecSystemInfoType::ParentControl:
|
2018-09-06 19:48:00 +00:00
|
|
|
buffer = {0x00}; // No parent control
|
2018-08-06 06:39:47 +00:00
|
|
|
dest_buffer.Write(buffer.data(), 0, buffer.size());
|
|
|
|
break;
|
|
|
|
default:
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_ERROR(Service_CECD, "Unknown system info type={:#x}", 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-19 07:05:41 +00:00
|
|
|
"called, dest_buffer_size={:#x}, info_type={:#x}, param_buffer_size={:#x}",
|
2018-08-06 06:39:47 +00:00
|
|
|
dest_buffer_size, static_cast<u32>(info_type), param_buffer_size);
|
|
|
|
}
|
|
|
|
|
2018-08-25 06:31:01 +00:00
|
|
|
void Module::Interface::Start(Kernel::HLERequestContext& ctx) {
|
2018-08-06 06:39:47 +00:00
|
|
|
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);
|
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called, command={}", cecd->GetCecCommandAsString(command));
|
2018-08-06 06:39:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 06:31:01 +00:00
|
|
|
void Module::Interface::Stop(Kernel::HLERequestContext& ctx) {
|
2018-08-06 06:39:47 +00:00
|
|
|
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);
|
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_WARNING(Service_CECD, "(STUBBED) called, command={}", cecd->GetCecCommandAsString(command));
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 06:31:01 +00:00
|
|
|
void Module::Interface::GetCecInfoBuffer(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp(ctx, 0x0D, 2, 2);
|
|
|
|
const u32 buffer_size = rp.Pop<u32>();
|
|
|
|
const u32 possible_info_type = rp.Pop<u32>();
|
|
|
|
auto& buffer = rp.PopMappedBuffer();
|
|
|
|
|
|
|
|
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.PushMappedBuffer(buffer);
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_CECD, "called, buffer_size={}, possible_info_type={}", buffer_size,
|
|
|
|
possible_info_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::Interface::GetCecdState(Kernel::HLERequestContext& ctx) {
|
2018-07-01 03:27:57 +00:00
|
|
|
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-09-07 23:36:33 +00:00
|
|
|
rb.PushEnum(CecdState::NdmStatusIdle);
|
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) {
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::RootDir:
|
|
|
|
case CecDataPathType::MboxDir:
|
|
|
|
case CecDataPathType::InboxDir:
|
|
|
|
case CecDataPathType::OutboxDir:
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC,
|
|
|
|
ErrorSummary::NotFound, ErrorLevel::Status));
|
|
|
|
break;
|
2018-09-06 19:48:00 +00:00
|
|
|
default: // If not directory, then it is a file
|
2018-09-28 20:47:27 +00:00
|
|
|
auto file_result = cecd->cecd_system_save_data_archive->OpenFile(path, mode);
|
2018-08-06 06:39:47 +00:00
|
|
|
if (file_result.Succeeded()) {
|
2018-09-28 20:47:27 +00:00
|
|
|
auto file = std::move(file_result).Unwrap();
|
2018-09-10 08:21:24 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
read_buffer.Read(buffer.data(), 0, buffer_size);
|
2018-08-17 06:32:48 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
if (file->GetSize() != buffer_size) {
|
|
|
|
file->SetSize(buffer_size);
|
2018-09-10 08:21:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
if (open_mode.check) {
|
|
|
|
cecd->CheckAndUpdateFile(path_type, ncch_program_id, buffer);
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
const u32 bytes_written = file->Write(0, buffer.size(), true, buffer.data()).Unwrap();
|
|
|
|
file->Close();
|
2018-08-06 06:39:47 +00:00
|
|
|
|
|
|
|
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);
|
2018-07-26 04:54:11 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
2018-08-19 07:05:41 +00:00
|
|
|
"called, ncch_program_id={:#010x}, path_type={:#04x}, path={}, buffer_size={:#x} "
|
|
|
|
"open_mode: raw={:#x}, unknown={}, read={}, write={}, create={}, check={}",
|
|
|
|
ncch_program_id, static_cast<u32>(path_type), path.AsString(), buffer_size,
|
|
|
|
open_mode.raw, open_mode.unknown, open_mode.read, open_mode.write, open_mode.create,
|
|
|
|
open_mode.check);
|
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) {
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::RootDir:
|
|
|
|
case CecDataPathType::MboxDir:
|
|
|
|
case CecDataPathType::InboxDir:
|
|
|
|
case CecDataPathType::OutboxDir:
|
2018-08-06 06:39:47 +00:00
|
|
|
rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC,
|
|
|
|
ErrorSummary::NotFound, ErrorLevel::Status));
|
2018-09-06 19:48:00 +00:00
|
|
|
rb.Push<u32>(0); // No entries read
|
2018-08-06 06:39:47 +00:00
|
|
|
break;
|
2018-09-06 19:48:00 +00:00
|
|
|
default: // If not directory, then it is a file
|
2018-09-28 20:47:27 +00:00
|
|
|
auto file_result = cecd->cecd_system_save_data_archive->OpenFile(path, mode);
|
2018-08-06 06:39:47 +00:00
|
|
|
if (file_result.Succeeded()) {
|
2018-09-28 20:47:27 +00:00
|
|
|
auto file = std::move(file_result).Unwrap();
|
2018-08-06 06:39:47 +00:00
|
|
|
std::vector<u8> buffer(buffer_size);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
const u32 bytes_read = file->Read(0, buffer_size, buffer.data()).Unwrap();
|
2018-08-06 06:39:47 +00:00
|
|
|
write_buffer.Write(buffer.data(), 0, buffer_size);
|
2018-09-28 20:47:27 +00:00
|
|
|
file->Close();
|
2018-08-06 06:39:47 +00:00
|
|
|
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(bytes_read);
|
|
|
|
} else {
|
|
|
|
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
|
|
|
|
ErrorLevel::Status));
|
2018-09-06 19:48:00 +00:00
|
|
|
rb.Push<u32>(0); // No bytes read
|
2018-08-06 06:39:47 +00:00
|
|
|
}
|
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,
|
2018-08-19 07:05:41 +00:00
|
|
|
"called, ncch_program_id={:#010x}, path_type={:#04x}, path={}, buffer_size={:#x} "
|
|
|
|
"open_mode: raw={:#x}, unknown={}, read={}, write={}, create={}, check={}",
|
|
|
|
ncch_program_id, static_cast<u32>(path_type), path.AsString(), buffer_size,
|
|
|
|
open_mode.raw, open_mode.unknown, open_mode.read, open_mode.write, open_mode.create,
|
|
|
|
open_mode.check);
|
2018-08-06 06:39:47 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 03:09:14 +00:00
|
|
|
std::string Module::EncodeBase64(const std::vector<u8>& in) const {
|
|
|
|
using namespace CryptoPP;
|
|
|
|
using Name::EncodingLookupArray;
|
|
|
|
using Name::InsertLineBreaks;
|
|
|
|
using Name::Pad;
|
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
std::string out;
|
2018-09-08 03:09:14 +00:00
|
|
|
Base64Encoder encoder;
|
|
|
|
AlgorithmParameters params =
|
|
|
|
MakeParameters(EncodingLookupArray(), (const byte*)base64_dict.data())(InsertLineBreaks(),
|
|
|
|
false)(Pad(), false);
|
|
|
|
|
|
|
|
encoder.IsolatedInitialize(params);
|
|
|
|
encoder.Attach(new StringSink(out));
|
|
|
|
encoder.Put(in.data(), in.size());
|
|
|
|
encoder.MessageEnd();
|
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
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-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::MboxList:
|
2018-07-29 16:43:13 +00:00
|
|
|
return "/CEC/MBoxList____";
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::MboxInfo:
|
2018-08-30 06:39:53 +00:00
|
|
|
return fmt::format("/CEC/{:08x}/MBoxInfo____", program_id);
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::InboxInfo:
|
2018-08-30 06:39:53 +00:00
|
|
|
return fmt::format("/CEC/{:08x}/InBox___/BoxInfo_____", program_id);
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::OutboxInfo:
|
2018-08-30 06:39:53 +00:00
|
|
|
return fmt::format("/CEC/{:08x}/OutBox__/BoxInfo_____", program_id);
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::OutboxIndex:
|
2018-08-30 06:39:53 +00:00
|
|
|
return fmt::format("/CEC/{:08x}/OutBox__/OBIndex_____", program_id);
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::InboxMsg:
|
2018-09-08 03:09:14 +00:00
|
|
|
return fmt::format("/CEC/{:08x}/InBox___/_{}", program_id, EncodeBase64(msg_id));
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::OutboxMsg:
|
2018-09-08 03:09:14 +00:00
|
|
|
return fmt::format("/CEC/{:08x}/OutBox__/_{}", program_id, EncodeBase64(msg_id));
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::RootDir:
|
2018-07-29 16:43:13 +00:00
|
|
|
return "/CEC";
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::MboxDir:
|
2018-08-30 06:39:53 +00:00
|
|
|
return fmt::format("/CEC/{:08x}", program_id);
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::InboxDir:
|
2018-08-30 06:39:53 +00:00
|
|
|
return fmt::format("/CEC/{:08x}/InBox___", program_id);
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::OutboxDir:
|
2018-08-30 06:39:53 +00:00
|
|
|
return fmt::format("/CEC/{:08x}/OutBox__", program_id);
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::MboxData:
|
|
|
|
case CecDataPathType::MboxIcon:
|
|
|
|
case CecDataPathType::MboxTitle:
|
2018-07-29 16:43:13 +00:00
|
|
|
default:
|
2018-08-30 06:39:53 +00:00
|
|
|
return fmt::format("/CEC/{:08x}/MBoxData.{:03}", program_id, static_cast<u32>(type) - 100);
|
2018-07-29 16:43:13 +00:00
|
|
|
}
|
2018-07-26 04:54:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
std::string Module::GetCecCommandAsString(const CecCommand command) const {
|
|
|
|
switch (command) {
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecCommand::None:
|
|
|
|
return "None";
|
|
|
|
case CecCommand::Start:
|
|
|
|
return "Start";
|
|
|
|
case CecCommand::ResetStart:
|
|
|
|
return "ResetStart";
|
|
|
|
case CecCommand::ReadyScan:
|
|
|
|
return "ReadyScan";
|
|
|
|
case CecCommand::ReadyScanWait:
|
|
|
|
return "ReadyScanWait";
|
|
|
|
case CecCommand::StartScan:
|
|
|
|
return "StartScan";
|
|
|
|
case CecCommand::Rescan:
|
|
|
|
return "Rescan";
|
|
|
|
case CecCommand::NdmResume:
|
|
|
|
return "NdmResume";
|
|
|
|
case CecCommand::NdmSuspend:
|
|
|
|
return "NdmSuspend";
|
|
|
|
case CecCommand::NdmSuspendImmediate:
|
|
|
|
return "NdmSuspendImmediate";
|
|
|
|
case CecCommand::StopWait:
|
|
|
|
return "StopWait";
|
|
|
|
case CecCommand::Stop:
|
|
|
|
return "Stop";
|
|
|
|
case CecCommand::StopForce:
|
|
|
|
return "StopForce";
|
|
|
|
case CecCommand::StopForceWait:
|
|
|
|
return "StopForceWait";
|
|
|
|
case CecCommand::ResetFilter:
|
|
|
|
return "ResetFilter";
|
|
|
|
case CecCommand::DaemonStop:
|
|
|
|
return "DaemonStop";
|
|
|
|
case CecCommand::DaemonStart:
|
|
|
|
return "DaemonStart";
|
|
|
|
case CecCommand::Exit:
|
|
|
|
return "Exit";
|
|
|
|
case CecCommand::OverBoss:
|
|
|
|
return "OverBoss";
|
|
|
|
case CecCommand::OverBossForce:
|
|
|
|
return "OverBossForce";
|
|
|
|
case CecCommand::OverBossForceWait:
|
|
|
|
return "OverBossForceWait";
|
|
|
|
case CecCommand::End:
|
|
|
|
return "End";
|
2018-08-19 07:05:41 +00:00
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-08-23 07:13:10 +00:00
|
|
|
constexpr u32 max_num_boxes = 24;
|
2018-09-06 19:48:00 +00:00
|
|
|
constexpr u32 name_size = 16; // fixed size 16 characters long
|
|
|
|
constexpr u32 valid_name_size = 8; // 8 characters are valid, the rest are null
|
2018-08-19 07:05:41 +00:00
|
|
|
const u32 file_size = file_buffer.size();
|
|
|
|
|
2018-08-17 06:32:48 +00:00
|
|
|
switch (path_type) {
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::MboxList: {
|
2018-08-19 07:05:41 +00:00
|
|
|
CecMBoxListHeader mbox_list_header = {};
|
|
|
|
std::memcpy(&mbox_list_header, file_buffer.data(), sizeof(CecMBoxListHeader));
|
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxList: magic={:#06x}, version={:#06x}, num_boxes={:#06x}",
|
|
|
|
mbox_list_header.magic, mbox_list_header.version, mbox_list_header.num_boxes);
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
if (file_size != sizeof(CecMBoxListHeader)) { // 0x18C
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxListHeader size is incorrect: {}", file_size);
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
if (mbox_list_header.magic != 0x6868) { // 'hh'
|
2018-08-23 07:13:10 +00:00
|
|
|
if (mbox_list_header.magic == 0 || mbox_list_header.magic == 0xFFFF) {
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxListHeader magic number is not set");
|
2018-08-23 07:13:10 +00:00
|
|
|
} else {
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxListHeader magic number is incorrect: {}",
|
|
|
|
mbox_list_header.magic);
|
2018-08-23 07:13:10 +00:00
|
|
|
}
|
|
|
|
std::memset(&mbox_list_header, 0, sizeof(CecMBoxListHeader));
|
2018-08-19 07:05:41 +00:00
|
|
|
mbox_list_header.magic = 0x6868;
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
if (mbox_list_header.version != 0x01) { // Not quite sure if it is a version
|
2018-08-19 07:05:41 +00:00
|
|
|
if (mbox_list_header.version == 0)
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxListHeader version is not set");
|
|
|
|
else
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxListHeader version is incorrect: {}",
|
|
|
|
mbox_list_header.version);
|
|
|
|
mbox_list_header.version = 0x01;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbox_list_header.num_boxes > 24) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxListHeader number of boxes is too large: {}",
|
|
|
|
mbox_list_header.num_boxes);
|
2018-08-23 07:13:10 +00:00
|
|
|
} else {
|
|
|
|
std::vector<u8> name_buffer(name_size);
|
|
|
|
std::memset(name_buffer.data(), 0, name_size);
|
|
|
|
|
|
|
|
if (ncch_program_id != 0) {
|
2018-08-30 06:39:53 +00:00
|
|
|
std::string name = fmt::format("{:08x}", ncch_program_id);
|
2018-08-23 07:13:10 +00:00
|
|
|
std::memcpy(name_buffer.data(), name.data(), name.size());
|
|
|
|
|
|
|
|
bool already_activated = false;
|
|
|
|
for (auto i = 0; i < mbox_list_header.num_boxes; i++) {
|
2018-09-06 19:48:00 +00:00
|
|
|
// Box names start at offset 0xC, are 16 char long, first 8 id, last 8 null
|
|
|
|
if (std::memcmp(name_buffer.data(), &mbox_list_header.box_names[i],
|
2018-08-23 07:13:10 +00:00
|
|
|
valid_name_size) == 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "Title already activated");
|
|
|
|
already_activated = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!already_activated) {
|
2018-09-06 19:48:00 +00:00
|
|
|
if (mbox_list_header.num_boxes < max_num_boxes) { // max boxes
|
2018-08-23 07:13:10 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "Adding title to mboxlist____: {}", name);
|
2018-09-06 19:48:00 +00:00
|
|
|
std::memcpy(&mbox_list_header.box_names[mbox_list_header.num_boxes],
|
|
|
|
name_buffer.data(), name_size);
|
2018-08-23 07:13:10 +00:00
|
|
|
mbox_list_header.num_boxes++;
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 19:48:00 +00:00
|
|
|
} else { // ncch_program_id == 0, remove/update activated boxes
|
2018-08-23 07:13:10 +00:00
|
|
|
/// We need to read the /CEC directory to find out which titles, if any,
|
|
|
|
/// are activated. The num_of_titles = (total_read_count) - 1, to adjust for
|
|
|
|
/// the MBoxList____ file that is present in the directory as well.
|
|
|
|
FileSys::Path root_path(
|
2018-09-07 23:36:33 +00:00
|
|
|
GetCecDataPathTypeAsString(CecDataPathType::RootDir, 0).data());
|
2018-08-23 07:13:10 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto dir_result = cecd_system_save_data_archive->OpenDirectory(root_path);
|
2018-08-23 07:13:10 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto root_dir = std::move(dir_result).Unwrap();
|
2018-08-23 07:13:10 +00:00
|
|
|
std::vector<FileSys::Entry> entries(max_num_boxes + 1); // + 1 mboxlist
|
2018-09-28 20:47:27 +00:00
|
|
|
const u32 entry_count = root_dir->Read(max_num_boxes + 1, entries.data());
|
|
|
|
root_dir->Close();
|
2018-08-23 07:13:10 +00:00
|
|
|
|
2018-08-25 06:31:01 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "Number of entries found in /CEC: {}", entry_count);
|
2018-08-23 07:13:10 +00:00
|
|
|
|
|
|
|
std::string mbox_list_name("MBoxList____");
|
|
|
|
std::string file_name;
|
|
|
|
std::u16string u16_filename;
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
// Loop through entries but don't add mboxlist____ to itself.
|
2018-08-23 07:13:10 +00:00
|
|
|
for (auto i = 0; i < entry_count; i++) {
|
|
|
|
u16_filename = std::u16string(entries[i].filename);
|
|
|
|
file_name = Common::UTF16ToUTF8(u16_filename);
|
|
|
|
|
|
|
|
if (mbox_list_name.compare(file_name) != 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "Adding title to mboxlist____: {}", file_name);
|
2018-09-06 19:48:00 +00:00
|
|
|
std::memcpy(&mbox_list_header.box_names[mbox_list_header.num_boxes++],
|
2018-08-23 07:13:10 +00:00
|
|
|
file_name.data(), valid_name_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-19 07:05:41 +00:00
|
|
|
}
|
|
|
|
std::memcpy(file_buffer.data(), &mbox_list_header, sizeof(CecMBoxListHeader));
|
2018-08-17 06:32:48 +00:00
|
|
|
break;
|
2018-08-19 07:05:41 +00:00
|
|
|
}
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::MboxInfo: {
|
2018-08-17 06:32:48 +00:00
|
|
|
CecMBoxInfoHeader mbox_info_header = {};
|
|
|
|
std::memcpy(&mbox_info_header, file_buffer.data(), sizeof(CecMBoxInfoHeader));
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"CecMBoxInfoHeader: magic={:#06x}, program_id={:#010x}, "
|
|
|
|
"private_id={:#010x}, flag={:#04x}, flag2={:#04x}",
|
|
|
|
mbox_info_header.magic, mbox_info_header.program_id, mbox_info_header.private_id,
|
|
|
|
mbox_info_header.flag, mbox_info_header.flag2);
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
if (file_size != sizeof(CecMBoxInfoHeader)) { // 0x60
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader size is incorrect: {}", file_size);
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
if (mbox_info_header.magic != 0x6363) { // 'cc'
|
2018-08-19 07:05:41 +00:00
|
|
|
if (mbox_info_header.magic == 0)
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader magic number is not set");
|
|
|
|
else
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader magic number is incorrect: {}",
|
|
|
|
mbox_info_header.magic);
|
|
|
|
mbox_info_header.magic = 0x6363;
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
if (mbox_info_header.program_id != ncch_program_id) {
|
|
|
|
if (mbox_info_header.program_id == 0)
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader program id is not set");
|
|
|
|
else
|
|
|
|
LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader program id doesn't match current id: {}",
|
|
|
|
mbox_info_header.program_id);
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
std::memcpy(file_buffer.data(), &mbox_info_header, sizeof(CecMBoxInfoHeader));
|
2018-08-17 06:32:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::InboxInfo: {
|
2018-09-10 08:21:24 +00:00
|
|
|
CecBoxInfoHeader inbox_info_header = {};
|
|
|
|
std::memcpy(&inbox_info_header, file_buffer.data(), sizeof(CecBoxInfoHeader));
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"CecBoxInfoHeader: magic={:#06x}, box_info_size={:#010x}, "
|
|
|
|
"max_box_size={:#010x}, box_size={:#010x}, "
|
|
|
|
"max_message_num={:#010x}, message_num={:#010x}, "
|
|
|
|
"max_batch_size={:#010x}, max_message_size={:#010x}",
|
|
|
|
inbox_info_header.magic, inbox_info_header.box_info_size,
|
|
|
|
inbox_info_header.max_box_size, inbox_info_header.box_size,
|
|
|
|
inbox_info_header.max_message_num, inbox_info_header.message_num,
|
|
|
|
inbox_info_header.max_batch_size, inbox_info_header.max_message_size);
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
if (inbox_info_header.magic != 0x6262) { // 'bb'
|
2018-08-19 07:05:41 +00:00
|
|
|
if (inbox_info_header.magic == 0)
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader magic number is not set");
|
|
|
|
else
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader magic number is incorrect: {}",
|
|
|
|
inbox_info_header.magic);
|
2018-08-17 06:32:48 +00:00
|
|
|
inbox_info_header.magic = 0x6262;
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
if (inbox_info_header.box_info_size != file_size) {
|
|
|
|
if (inbox_info_header.box_info_size == 0)
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader box info size is not set");
|
|
|
|
else
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader box info size is incorrect:",
|
|
|
|
inbox_info_header.box_info_size);
|
2018-09-10 08:21:24 +00:00
|
|
|
inbox_info_header.box_info_size = sizeof(CecBoxInfoHeader);
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
if (inbox_info_header.max_box_size == 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max box size is not set");
|
|
|
|
} else if (inbox_info_header.max_box_size > 0x100000) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max box size is too large: {}",
|
|
|
|
inbox_info_header.max_box_size);
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
if (inbox_info_header.max_message_num == 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max message number is not set");
|
|
|
|
} else if (inbox_info_header.max_message_num > 99) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max message number is too large: {}",
|
|
|
|
inbox_info_header.max_message_num);
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
if (inbox_info_header.max_message_size == 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max message size is not set");
|
|
|
|
} else if (inbox_info_header.max_message_size > 0x019000) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max message size is too large");
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
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-19 07:05:41 +00:00
|
|
|
} else if (inbox_info_header.max_batch_size != inbox_info_header.max_message_num) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max batch size != max message number");
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
std::memcpy(file_buffer.data(), &inbox_info_header, sizeof(CecBoxInfoHeader));
|
2018-08-17 06:32:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::OutboxInfo: {
|
2018-09-10 08:21:24 +00:00
|
|
|
CecBoxInfoHeader outbox_info_header = {};
|
|
|
|
std::memcpy(&outbox_info_header, file_buffer.data(), sizeof(CecBoxInfoHeader));
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_CECD,
|
|
|
|
"CecBoxInfoHeader: magic={:#06x}, box_info_size={:#010x}, "
|
|
|
|
"max_box_size={:#010x}, box_size={:#010x}, "
|
|
|
|
"max_message_num={:#010x}, message_num={:#010x}, "
|
|
|
|
"max_batch_size={:#010x}, max_message_size={:#010x}",
|
|
|
|
outbox_info_header.magic, outbox_info_header.box_info_size,
|
|
|
|
outbox_info_header.max_box_size, outbox_info_header.box_size,
|
|
|
|
outbox_info_header.max_message_num, outbox_info_header.message_num,
|
|
|
|
outbox_info_header.max_batch_size, outbox_info_header.max_message_size);
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
if (outbox_info_header.magic != 0x6262) { // 'bb'
|
2018-08-19 07:05:41 +00:00
|
|
|
if (outbox_info_header.magic == 0)
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader magic number is not set");
|
|
|
|
else
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader magic number is incorrect: {}",
|
|
|
|
outbox_info_header.magic);
|
2018-08-17 06:32:48 +00:00
|
|
|
outbox_info_header.magic = 0x6262;
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
if (outbox_info_header.box_info_size != file_buffer.size()) {
|
2018-08-19 07:05:41 +00:00
|
|
|
if (outbox_info_header.box_info_size == 0)
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader box info size is not set");
|
|
|
|
else
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader box info size is incorrect:",
|
|
|
|
outbox_info_header.box_info_size);
|
2018-09-10 08:21:24 +00:00
|
|
|
outbox_info_header.box_info_size = sizeof(CecBoxInfoHeader);
|
|
|
|
outbox_info_header.message_num = 0;
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
if (outbox_info_header.max_box_size == 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max box size is not set");
|
|
|
|
} else if (outbox_info_header.max_box_size > 0x100000) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max box size is too large");
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 21:57:13 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
if (outbox_info_header.max_message_num == 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max message number is not set");
|
|
|
|
} else if (outbox_info_header.max_message_num > 99) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max message number is too large");
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-19 07:05:41 +00:00
|
|
|
if (outbox_info_header.max_message_size == 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max message size is not set");
|
|
|
|
} else if (outbox_info_header.max_message_size > 0x019000) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max message size is too large");
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
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-19 07:05:41 +00:00
|
|
|
} else if (outbox_info_header.max_batch_size != outbox_info_header.max_message_num) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max batch size != max message number");
|
2018-08-17 06:32:48 +00:00
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-09-10 08:21:24 +00:00
|
|
|
/// We need to read the /CEC/<id>/OutBox directory to find out which messages, if any,
|
|
|
|
/// are present. The num_of_messages = (total_read_count) - 2, to adjust for
|
|
|
|
/// the BoxInfo____ and OBIndex_____files that are present in the directory as well.
|
|
|
|
FileSys::Path outbox_path(
|
|
|
|
GetCecDataPathTypeAsString(CecDataPathType::OutboxDir, ncch_program_id).data());
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto dir_result = cecd_system_save_data_archive->OpenDirectory(outbox_path);
|
2018-09-10 08:21:24 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto outbox_dir = std::move(dir_result).Unwrap();
|
2018-09-10 08:21:24 +00:00
|
|
|
std::vector<FileSys::Entry> entries(outbox_info_header.max_message_num + 2);
|
|
|
|
const u32 entry_count =
|
2018-09-28 20:47:27 +00:00
|
|
|
outbox_dir->Read(outbox_info_header.max_message_num + 2, entries.data());
|
|
|
|
outbox_dir->Close();
|
2018-09-10 08:21:24 +00:00
|
|
|
|
|
|
|
LOG_DEBUG(Service_CECD, "Number of entries found in /OutBox: {}", entry_count);
|
|
|
|
std::array<CecMessageHeader, 8> message_headers;
|
|
|
|
|
|
|
|
std::string boxinfo_name("BoxInfo_____");
|
|
|
|
std::string obindex_name("OBIndex_____");
|
|
|
|
std::string file_name;
|
|
|
|
std::u16string u16_filename;
|
|
|
|
|
|
|
|
for (auto i = 0; i < entry_count; i++) {
|
|
|
|
u16_filename = std::u16string(entries[i].filename);
|
|
|
|
file_name = Common::UTF16ToUTF8(u16_filename);
|
|
|
|
|
|
|
|
if (boxinfo_name.compare(file_name) != 0 && obindex_name.compare(file_name) != 0) {
|
|
|
|
LOG_DEBUG(Service_CECD, "Adding message to BoxInfo_____: {}", file_name);
|
|
|
|
|
|
|
|
FileSys::Path message_path(
|
|
|
|
(GetCecDataPathTypeAsString(CecDataPathType::OutboxDir, ncch_program_id) + "/" +
|
|
|
|
file_name)
|
|
|
|
.data());
|
|
|
|
|
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.read_flag.Assign(1);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message_result = cecd_system_save_data_archive->OpenFile(message_path, mode);
|
2018-09-10 08:21:24 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message = std::move(message_result).Unwrap();
|
|
|
|
const u32 message_size = message->GetSize();
|
2018-09-10 08:21:24 +00:00
|
|
|
std::vector<u8> buffer(message_size);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
message->Read(0, message_size, buffer.data()).Unwrap();
|
|
|
|
message->Close();
|
2018-09-10 08:21:24 +00:00
|
|
|
|
|
|
|
std::memcpy(&message_headers[outbox_info_header.message_num++], buffer.data(),
|
|
|
|
sizeof(CecMessageHeader));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outbox_info_header.message_num > 0) {
|
|
|
|
const u32 message_headers_size =
|
|
|
|
outbox_info_header.message_num * sizeof(CecMessageHeader);
|
|
|
|
|
|
|
|
file_buffer.resize(sizeof(CecBoxInfoHeader) + message_headers_size, 0);
|
|
|
|
outbox_info_header.box_info_size += message_headers_size;
|
|
|
|
|
|
|
|
std::memcpy(file_buffer.data() + sizeof(CecBoxInfoHeader), &message_headers,
|
|
|
|
message_headers_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(file_buffer.data(), &outbox_info_header, sizeof(CecBoxInfoHeader));
|
2018-08-17 06:32:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::OutboxIndex: {
|
2018-08-19 07:05:41 +00:00
|
|
|
CecOBIndexHeader obindex_header = {};
|
|
|
|
std::memcpy(&obindex_header, file_buffer.data(), sizeof(CecOBIndexHeader));
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
if (file_size < sizeof(CecOBIndexHeader)) { // 0x08, minimum size
|
2018-08-19 07:05:41 +00:00
|
|
|
LOG_DEBUG(Service_CECD, "CecOBIndexHeader size is too small: {}", file_size);
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:48:00 +00:00
|
|
|
if (obindex_header.magic != 0x6767) { // 'gg'
|
2018-08-19 07:05:41 +00:00
|
|
|
if (obindex_header.magic == 0)
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOBIndexHeader magic number is not set");
|
|
|
|
else
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOBIndexHeader magic number is incorrect: {}",
|
|
|
|
obindex_header.magic);
|
|
|
|
obindex_header.magic = 0x6767;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obindex_header.message_num == 0) {
|
|
|
|
if (file_size > sizeof(CecOBIndexHeader)) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOBIndexHeader message number is not set");
|
2018-09-06 19:48:00 +00:00
|
|
|
obindex_header.message_num = (file_size % 8) - 1; // 8 byte message id - 1 header
|
2018-08-19 07:05:41 +00:00
|
|
|
}
|
|
|
|
} else if (obindex_header.message_num != (file_size % 8) - 1) {
|
|
|
|
LOG_DEBUG(Service_CECD, "CecOBIndexHeader message number is incorrect: {}",
|
|
|
|
obindex_header.message_num);
|
2018-09-10 08:21:24 +00:00
|
|
|
obindex_header.message_num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We need to read the /CEC/<id>/OutBox directory to find out which messages, if any,
|
|
|
|
/// are present. The num_of_messages = (total_read_count) - 2, to adjust for
|
|
|
|
/// the BoxInfo____ and OBIndex_____files that are present in the directory as well.
|
|
|
|
FileSys::Path outbox_path(
|
|
|
|
GetCecDataPathTypeAsString(CecDataPathType::OutboxDir, ncch_program_id).data());
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto dir_result = cecd_system_save_data_archive->OpenDirectory(outbox_path);
|
2018-09-10 08:21:24 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto outbox_dir = std::move(dir_result).Unwrap();
|
2018-09-10 08:21:24 +00:00
|
|
|
std::vector<FileSys::Entry> entries(8);
|
2018-09-28 20:47:27 +00:00
|
|
|
const u32 entry_count = outbox_dir->Read(8, entries.data());
|
|
|
|
outbox_dir->Close();
|
2018-09-10 08:21:24 +00:00
|
|
|
|
|
|
|
LOG_DEBUG(Service_CECD, "Number of entries found in /OutBox: {}", entry_count);
|
|
|
|
std::array<std::array<u8, 8>, 8> message_ids;
|
|
|
|
|
|
|
|
std::string boxinfo_name("BoxInfo_____");
|
|
|
|
std::string obindex_name("OBIndex_____");
|
|
|
|
std::string file_name;
|
|
|
|
std::u16string u16_filename;
|
|
|
|
|
|
|
|
for (auto i = 0; i < entry_count; i++) {
|
|
|
|
u16_filename = std::u16string(entries[i].filename);
|
|
|
|
file_name = Common::UTF16ToUTF8(u16_filename);
|
|
|
|
|
|
|
|
if (boxinfo_name.compare(file_name) != 0 && obindex_name.compare(file_name) != 0) {
|
|
|
|
FileSys::Path message_path(
|
|
|
|
(GetCecDataPathTypeAsString(CecDataPathType::OutboxDir, ncch_program_id) + "/" +
|
|
|
|
file_name)
|
|
|
|
.data());
|
|
|
|
|
|
|
|
FileSys::Mode mode;
|
|
|
|
mode.read_flag.Assign(1);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message_result = cecd_system_save_data_archive->OpenFile(message_path, mode);
|
2018-09-10 08:21:24 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto message = std::move(message_result).Unwrap();
|
|
|
|
const u32 message_size = message->GetSize();
|
2018-09-10 08:21:24 +00:00
|
|
|
std::vector<u8> buffer(message_size);
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
message->Read(0, message_size, buffer.data()).Unwrap();
|
|
|
|
message->Close();
|
2018-09-10 08:21:24 +00:00
|
|
|
|
|
|
|
// Message id is at offset 0x20, and is 8 bytes
|
|
|
|
std::memcpy(&message_ids[obindex_header.message_num++], buffer.data() + 0x20, 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obindex_header.message_num > 0) {
|
|
|
|
const u32 message_ids_size = obindex_header.message_num * 8;
|
|
|
|
file_buffer.resize(sizeof(CecOBIndexHeader) + message_ids_size);
|
|
|
|
std::memcpy(file_buffer.data() + sizeof(CecOBIndexHeader), &message_ids,
|
|
|
|
message_ids_size);
|
2018-08-19 07:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(file_buffer.data(), &obindex_header, sizeof(CecOBIndexHeader));
|
2018-08-17 06:32:48 +00:00
|
|
|
break;
|
2018-08-19 07:05:41 +00:00
|
|
|
}
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::InboxMsg:
|
2018-08-17 06:32:48 +00:00
|
|
|
break;
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::OutboxMsg:
|
2018-08-17 06:32:48 +00:00
|
|
|
break;
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::RootDir:
|
|
|
|
case CecDataPathType::MboxDir:
|
|
|
|
case CecDataPathType::InboxDir:
|
|
|
|
case CecDataPathType::OutboxDir:
|
2018-08-17 06:32:48 +00:00
|
|
|
break;
|
2018-09-07 23:36:33 +00:00
|
|
|
case CecDataPathType::MboxData:
|
|
|
|
case CecDataPathType::MboxIcon:
|
|
|
|
case CecDataPathType::MboxTitle:
|
2018-08-17 06:32:48 +00:00
|
|
|
default: {}
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 07:41:14 +00:00
|
|
|
|
2018-08-06 06:39:47 +00:00
|
|
|
Module::SessionData::SessionData() {}
|
|
|
|
|
|
|
|
Module::SessionData::~SessionData() {
|
|
|
|
if (file)
|
2018-09-28 20:47:27 +00:00
|
|
|
file->Close();
|
2018-08-06 06:39:47 +00:00
|
|
|
}
|
|
|
|
|
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-10-17 19:23:56 +00:00
|
|
|
Module::Module(Core::System& system) : system(system) {
|
2018-07-01 03:27:57 +00:00
|
|
|
using namespace Kernel;
|
2018-10-11 19:48:16 +00:00
|
|
|
cecinfo_event = system.Kernel().CreateEvent(Kernel::ResetType::OneShot, "CECD::cecinfo_event");
|
|
|
|
change_state_event =
|
|
|
|
system.Kernel().CreateEvent(Kernel::ResetType::OneShot, "CECD::change_state_event");
|
2018-07-29 16:43:13 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir);
|
|
|
|
FileSys::ArchiveFactory_SystemSaveData systemsavedata_factory(nand_directory);
|
|
|
|
|
2018-07-29 16:43:13 +00:00
|
|
|
// Open the SystemSaveData archive 0x00010026
|
|
|
|
FileSys::Path archive_path(cecd_system_savedata_id);
|
2019-02-03 01:14:01 +00:00
|
|
|
auto archive_result = systemsavedata_factory.Open(archive_path, 0);
|
2018-07-29 16:43:13 +00:00
|
|
|
|
|
|
|
// If the archive didn't exist, create the files inside
|
2018-09-28 20:47:27 +00:00
|
|
|
if (archive_result.Code() != FileSys::ERR_NOT_FORMATTED) {
|
|
|
|
ASSERT_MSG(archive_result.Succeeded(), "Could not open the CECD SystemSaveData archive!");
|
|
|
|
cecd_system_save_data_archive = std::move(archive_result).Unwrap();
|
|
|
|
} else {
|
2018-07-29 16:43:13 +00:00
|
|
|
// Format the archive to create the directories
|
2019-02-03 01:14:01 +00:00
|
|
|
systemsavedata_factory.Format(archive_path, FileSys::ArchiveFormatInfo(), 0);
|
2018-07-29 16:43:13 +00:00
|
|
|
|
|
|
|
// Open it again to get a valid archive now that the folder exists
|
2019-02-03 01:14:01 +00:00
|
|
|
cecd_system_save_data_archive = systemsavedata_factory.Open(archive_path, 0).Unwrap();
|
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(
|
2018-09-07 23:36:33 +00:00
|
|
|
GetCecDataPathTypeAsString(CecDataPathType::RootDir, 0).data());
|
2018-09-28 20:47:27 +00:00
|
|
|
cecd_system_save_data_archive->CreateDirectory(root_dir_path);
|
2018-08-12 07:26:45 +00:00
|
|
|
|
|
|
|
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
|
2018-08-31 00:27:46 +00:00
|
|
|
/// at offset 0x30d53. 0xDD means that the cec module hasn't written data to that
|
|
|
|
/// region yet.
|
2018-08-12 07:26:45 +00:00
|
|
|
FileSys::Path eventlog_path("/eventlog.dat");
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto eventlog_result = cecd_system_save_data_archive->OpenFile(eventlog_path, mode);
|
2018-08-12 07:26:45 +00:00
|
|
|
|
|
|
|
constexpr u32 eventlog_size = 0x30d54;
|
2018-09-28 20:47:27 +00:00
|
|
|
auto eventlog = std::move(eventlog_result).Unwrap();
|
2018-08-12 07:26:45 +00:00
|
|
|
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;
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
eventlog->Write(0, eventlog_size, true, eventlog_buffer.data());
|
|
|
|
eventlog->Close();
|
2018-08-12 07:26:45 +00:00
|
|
|
|
2018-08-19 07:05:41 +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(
|
2018-09-07 23:36:33 +00:00
|
|
|
GetCecDataPathTypeAsString(CecDataPathType::MboxList, 0).data());
|
2018-08-19 07:05:41 +00:00
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
auto mboxlist_result = cecd_system_save_data_archive->OpenFile(mboxlist_path, mode);
|
2018-08-19 07:05:41 +00:00
|
|
|
|
|
|
|
constexpr u32 mboxlist_size = 0x18c;
|
2018-09-28 20:47:27 +00:00
|
|
|
auto mboxlist = std::move(mboxlist_result).Unwrap();
|
2018-08-19 07:05:41 +00:00
|
|
|
std::vector<u8> mboxlist_buffer(mboxlist_size);
|
|
|
|
|
|
|
|
std::memset(&mboxlist_buffer[0], 0, mboxlist_size);
|
|
|
|
mboxlist_buffer[0] = 0x68;
|
|
|
|
mboxlist_buffer[1] = 0x68;
|
2018-09-06 19:48:00 +00:00
|
|
|
// mboxlist_buffer[2-3] are already zeroed
|
2018-08-19 07:05:41 +00:00
|
|
|
mboxlist_buffer[4] = 0x01;
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
mboxlist->Write(0, mboxlist_size, true, mboxlist_buffer.data());
|
|
|
|
mboxlist->Close();
|
2018-07-29 16:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:47:27 +00:00
|
|
|
Module::~Module() = default;
|
2015-06-11 22:12:16 +00:00
|
|
|
|
2018-10-05 14:59:43 +00:00
|
|
|
void InstallInterfaces(Core::System& system) {
|
|
|
|
auto& service_manager = system.ServiceManager();
|
2018-10-11 19:48:16 +00:00
|
|
|
auto cecd = std::make_shared<Module>(system);
|
2018-07-01 03:27:57 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-22 12:23:08 +00:00
|
|
|
} // namespace Service::CECD
|