1
0
Fork 0

service/cecd: Corrected behaviour, started checking and updating file headers

This commit is contained in:
NarcolepticK 2018-08-17 02:32:48 -04:00
parent d88ad61da4
commit 71204e525f
4 changed files with 375 additions and 195 deletions

View File

@ -2,6 +2,8 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <chrono>
#include <ctime>
#include "common/file_util.h" #include "common/file_util.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/string_util.h" #include "common/string_util.h"
@ -26,7 +28,7 @@ using CecDataPathType = Module::CecDataPathType;
using CecOpenMode = Module::CecOpenMode; using CecOpenMode = Module::CecOpenMode;
using CecSystemInfoType = Module::CecSystemInfoType; using CecSystemInfoType = Module::CecSystemInfoType;
void Module::Interface::OpenRawFile(Kernel::HLERequestContext& ctx) { void Module::Interface::Open(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x01, 3, 2); IPC::RequestParser rp(ctx, 0x01, 3, 2);
const u32 ncch_program_id = rp.Pop<u32>(); const u32 ncch_program_id = rp.Pop<u32>();
const CecDataPathType path_type = rp.PopEnum<CecDataPathType>(); const CecDataPathType path_type = rp.PopEnum<CecDataPathType>();
@ -55,11 +57,11 @@ void Module::Interface::OpenRawFile(Kernel::HLERequestContext& ctx) {
auto dir_result = auto dir_result =
Service::FS::OpenDirectoryFromArchive(cecd->cecd_system_save_data_archive, path); Service::FS::OpenDirectoryFromArchive(cecd->cecd_system_save_data_archive, path);
if (dir_result.Failed()) { if (dir_result.Failed()) {
if (open_mode.make_dir) { if (open_mode.create) {
Service::FS::CreateDirectoryFromArchive(cecd->cecd_system_save_data_archive, path); Service::FS::CreateDirectoryFromArchive(cecd->cecd_system_save_data_archive, path);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
} else { } else {
LOG_ERROR(Service_CECD, "Failed to open directory: {}", path.AsString()); LOG_DEBUG(Service_CECD, "Failed to open directory: {}", path.AsString());
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC,
ErrorSummary::NotFound, ErrorLevel::Status)); ErrorSummary::NotFound, ErrorLevel::Status));
} }
@ -75,7 +77,7 @@ void Module::Interface::OpenRawFile(Kernel::HLERequestContext& ctx) {
auto file_result = auto file_result =
Service::FS::OpenFileFromArchive(cecd->cecd_system_save_data_archive, path, mode); Service::FS::OpenFileFromArchive(cecd->cecd_system_save_data_archive, path, mode);
if (file_result.Failed()) { if (file_result.Failed()) {
LOG_ERROR(Service_CECD, "Failed to open file: {}", path.AsString()); LOG_DEBUG(Service_CECD, "Failed to open file: {}", path.AsString());
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound, rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
ErrorLevel::Status)); ErrorLevel::Status));
rb.Push<u32>(0); /// No file size rb.Push<u32>(0); /// No file size
@ -99,7 +101,7 @@ void Module::Interface::OpenRawFile(Kernel::HLERequestContext& ctx) {
ncch_program_id, static_cast<u32>(path_type), open_mode.raw, path.AsString()); ncch_program_id, static_cast<u32>(path_type), open_mode.raw, path.AsString());
} }
void Module::Interface::ReadRawFile(Kernel::HLERequestContext& ctx) { void Module::Interface::ReadFile(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x02, 1, 2); IPC::RequestParser rp(ctx, 0x02, 1, 2);
const u32 write_buffer_size = rp.Pop<u32>(); const u32 write_buffer_size = rp.Pop<u32>();
auto& write_buffer = rp.PopMappedBuffer(); auto& write_buffer = rp.PopMappedBuffer();
@ -142,9 +144,42 @@ void Module::Interface::ReadMessage(Kernel::HLERequestContext& ctx) {
auto& message_id_buffer = rp.PopMappedBuffer(); auto& message_id_buffer = rp.PopMappedBuffer();
auto& write_buffer = rp.PopMappedBuffer(); auto& write_buffer = rp.PopMappedBuffer();
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);
IPC::RequestBuilder rb = rp.MakeBuilder(2, 4); IPC::RequestBuilder rb = rp.MakeBuilder(2, 4);
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(RESULT_SUCCESS);
rb.Push<u32>(0); /// Read size rb.Push<u32>(bytes_read);
} else {
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
ErrorLevel::Status));
rb.Push<u32>(0); /// zero bytes read
}
rb.PushMappedBuffer(message_id_buffer); rb.PushMappedBuffer(message_id_buffer);
rb.PushMappedBuffer(write_buffer); rb.PushMappedBuffer(write_buffer);
@ -162,9 +197,43 @@ void Module::Interface::ReadMessageWithHMAC(Kernel::HLERequestContext& ctx) {
auto& hmac_key_buffer = rp.PopMappedBuffer(); auto& hmac_key_buffer = rp.PopMappedBuffer();
auto& write_buffer = rp.PopMappedBuffer(); auto& write_buffer = rp.PopMappedBuffer();
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);
IPC::RequestBuilder rb = rp.MakeBuilder(2, 6); IPC::RequestBuilder rb = rp.MakeBuilder(2, 6);
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(RESULT_SUCCESS);
rb.Push<u32>(0); /// Read size rb.Push<u32>(bytes_read);
} else {
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
ErrorLevel::Status));
rb.Push<u32>(0); /// zero bytes read
}
rb.PushMappedBuffer(message_id_buffer); rb.PushMappedBuffer(message_id_buffer);
rb.PushMappedBuffer(hmac_key_buffer); rb.PushMappedBuffer(hmac_key_buffer);
rb.PushMappedBuffer(write_buffer); rb.PushMappedBuffer(write_buffer);
@ -173,7 +242,7 @@ void Module::Interface::ReadMessageWithHMAC(Kernel::HLERequestContext& ctx) {
ncch_program_id, is_outbox); ncch_program_id, is_outbox);
} }
void Module::Interface::WriteRawFile(Kernel::HLERequestContext& ctx) { void Module::Interface::WriteFile(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x05, 1, 2); IPC::RequestParser rp(ctx, 0x05, 1, 2);
const u32 read_buffer_size = rp.Pop<u32>(); const u32 read_buffer_size = rp.Pop<u32>();
auto& read_buffer = rp.PopMappedBuffer(); auto& read_buffer = rp.PopMappedBuffer();
@ -193,6 +262,11 @@ void Module::Interface::WriteRawFile(Kernel::HLERequestContext& ctx) {
std::vector<u8> buffer(read_buffer_size); std::vector<u8> buffer(read_buffer_size);
read_buffer.Read(buffer.data(), 0, read_buffer_size); read_buffer.Read(buffer.data(), 0, read_buffer_size);
if (session_data->open_mode.check) {
cecd->CheckAndUpdateFile(session_data->data_path_type, session_data->ncch_program_id,
buffer);
}
const u32 bytes_written = const u32 bytes_written =
session_data->file->backend->Write(0, read_buffer_size, true, buffer.data()).Unwrap(); session_data->file->backend->Write(0, read_buffer_size, true, buffer.data()).Unwrap();
session_data->file->backend->Close(); session_data->file->backend->Close();
@ -213,8 +287,43 @@ void Module::Interface::WriteMessage(Kernel::HLERequestContext& ctx) {
auto& read_buffer = rp.PopMappedBuffer(); auto& read_buffer = rp.PopMappedBuffer();
auto& message_id_buffer = rp.PopMappedBuffer(); auto& message_id_buffer = rp.PopMappedBuffer();
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);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 4); IPC::RequestBuilder rb = rp.MakeBuilder(1, 4);
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); rb.Push(RESULT_SUCCESS);
} else {
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
ErrorLevel::Status));
}
rb.PushMappedBuffer(read_buffer); rb.PushMappedBuffer(read_buffer);
rb.PushMappedBuffer(message_id_buffer); rb.PushMappedBuffer(message_id_buffer);
@ -232,8 +341,43 @@ void Module::Interface::WriteMessageWithHMAC(Kernel::HLERequestContext& ctx) {
auto& hmac_key_buffer = rp.PopMappedBuffer(); auto& hmac_key_buffer = rp.PopMappedBuffer();
auto& message_id_buffer = rp.PopMappedBuffer(); auto& message_id_buffer = rp.PopMappedBuffer();
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);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 6); IPC::RequestBuilder rb = rp.MakeBuilder(1, 6);
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); rb.Push(RESULT_SUCCESS);
} else {
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
ErrorLevel::Status));
}
rb.PushMappedBuffer(read_buffer); rb.PushMappedBuffer(read_buffer);
rb.PushMappedBuffer(hmac_key_buffer); rb.PushMappedBuffer(hmac_key_buffer);
rb.PushMappedBuffer(message_id_buffer); rb.PushMappedBuffer(message_id_buffer);
@ -250,6 +394,11 @@ void Module::Interface::Delete(Kernel::HLERequestContext& ctx) {
const u32 message_id_size = rp.Pop<u32>(); const u32 message_id_size = rp.Pop<u32>();
auto& message_id_buffer = rp.PopMappedBuffer(); auto& message_id_buffer = rp.PopMappedBuffer();
if (is_outbox) {
} else { /// otherwise inbox
}
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
rb.PushMappedBuffer(message_id_buffer); rb.PushMappedBuffer(message_id_buffer);
@ -259,7 +408,7 @@ void Module::Interface::Delete(Kernel::HLERequestContext& ctx) {
ncch_program_id, static_cast<u32>(path_type), is_outbox); ncch_program_id, static_cast<u32>(path_type), is_outbox);
} }
void Module::Interface::Cecd_0x000900C2(Kernel::HLERequestContext& ctx) { void Module::Interface::Cecd_0x000900C2(Kernel::HLERequestContext& ctx) { /// DeleteFile?
IPC::RequestParser rp(ctx, 0x09, 3, 2); IPC::RequestParser rp(ctx, 0x09, 3, 2);
const u32 ncch_program_id = rp.Pop<u32>(); const u32 ncch_program_id = rp.Pop<u32>();
const u32 size = rp.Pop<u32>(); const u32 size = rp.Pop<u32>();
@ -308,7 +457,7 @@ void Module::Interface::GetSystemInfo(Kernel::HLERequestContext& ctx) {
rb.PushMappedBuffer(dest_buffer); rb.PushMappedBuffer(dest_buffer);
LOG_DEBUG(Service_CECD, LOG_DEBUG(Service_CECD,
"(STUBBED) called, dest_buffer_size={:#010x}, info_type={:#010x}, " "called, dest_buffer_size={:#010x}, info_type={:#010x}, "
"param_buffer_size={:#010x}", "param_buffer_size={:#010x}",
dest_buffer_size, static_cast<u32>(info_type), param_buffer_size); dest_buffer_size, static_cast<u32>(info_type), param_buffer_size);
} }
@ -393,26 +542,19 @@ void Module::Interface::OpenAndWrite(Kernel::HLERequestContext& ctx) {
if (file_result.Succeeded()) { if (file_result.Succeeded()) {
auto file = file_result.Unwrap(); auto file = file_result.Unwrap();
std::vector<u8> buffer(buffer_size); std::vector<u8> buffer(buffer_size);
read_buffer.Read(buffer.data(), 0, buffer_size); read_buffer.Read(buffer.data(), 0, buffer_size);
if (open_mode.check) {
cecd->CheckAndUpdateFile(path_type, ncch_program_id, buffer);
}
const u32 bytes_written = const u32 bytes_written =
file->backend->Write(0, buffer_size, true, buffer.data()).Unwrap(); file->backend->Write(0, buffer_size, true, buffer.data()).Unwrap();
file->backend->Close(); file->backend->Close();
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
} else { } else {
LOG_ERROR(Service_CECD, "Failed to open file: {}", path.AsString()); LOG_DEBUG(Service_CECD, "Failed to open file: {}", path.AsString());
/// We need to check if the MBox /CEC/<id> directory even exists...
/// If it doesn't, we create and populate it.
const FileSys::Path mbox_path(
cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_MBOX_DIR,
ncch_program_id)
.data());
auto mbox_result = Service::FS::OpenDirectoryFromArchive(
cecd->cecd_system_save_data_archive, mbox_path);
if (mbox_result.Failed())
cecd->CreateAndPopulateMBoxDirectory(ncch_program_id);
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound, rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
ErrorLevel::Status)); ErrorLevel::Status));
@ -438,8 +580,6 @@ void Module::Interface::OpenAndRead(Kernel::HLERequestContext& ctx) {
FileSys::Path path(cecd->GetCecDataPathTypeAsString(path_type, ncch_program_id).data()); FileSys::Path path(cecd->GetCecDataPathTypeAsString(path_type, ncch_program_id).data());
FileSys::Mode mode; FileSys::Mode mode;
mode.read_flag.Assign(1); mode.read_flag.Assign(1);
mode.create_flag.Assign(1);
mode.write_flag.Assign(1);
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2); IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
switch (path_type) { switch (path_type) {
@ -449,7 +589,7 @@ void Module::Interface::OpenAndRead(Kernel::HLERequestContext& ctx) {
case CecDataPathType::CEC_PATH_OUTBOX_DIR: case CecDataPathType::CEC_PATH_OUTBOX_DIR:
rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC, rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::CEC,
ErrorSummary::NotFound, ErrorLevel::Status)); ErrorSummary::NotFound, ErrorLevel::Status));
rb.Push<u32>(0); /// No bytes read rb.Push<u32>(0); /// No entries read
break; break;
default: /// If not directory, then it is a file default: /// If not directory, then it is a file
auto file_result = auto file_result =
@ -465,21 +605,8 @@ void Module::Interface::OpenAndRead(Kernel::HLERequestContext& ctx) {
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
rb.Push<u32>(bytes_read); rb.Push<u32>(bytes_read);
} else { } else {
LOG_ERROR(Service_CECD, "Failed to open file: {}", path.AsString()); LOG_DEBUG(Service_CECD, "Failed to open file: {}", path.AsString());
/// We need to check if the MBox /CEC/<id> directory even exists...
/// If it doesn't, we create and populate it.
const FileSys::Path mbox_path(
cecd->GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_MBOX_DIR,
ncch_program_id)
.data());
auto mbox_result = Service::FS::OpenDirectoryFromArchive(
cecd->cecd_system_save_data_archive, mbox_path);
if (mbox_result.Failed())
cecd->CreateAndPopulateMBoxDirectory(ncch_program_id);
/// Since the directories/files didn't exist before, we still push a failure
/// A second attempt will then be called, and now everything exists.
rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound, rb.Push(ResultCode(ErrorDescription::NoData, ErrorModule::CEC, ErrorSummary::NotFound,
ErrorLevel::Status)); ErrorLevel::Status));
rb.Push<u32>(0); /// No bytes read rb.Push<u32>(0); /// No bytes read
@ -555,128 +682,161 @@ std::string Module::GetCecDataPathTypeAsString(const CecDataPathType type, const
} }
} }
void Module::CreateAndPopulateMBoxDirectory(const u32 ncch_program_id) { void Module::CheckAndUpdateFile(const CecDataPathType path_type, const u32 ncch_program_id,
const FileSys::Path root_dir_path( std::vector<u8>& file_buffer) {
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_ROOT_DIR, ncch_program_id).data()); switch (path_type) {
const FileSys::Path mbox_path( case CecDataPathType::CEC_PATH_MBOX_LIST:
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_MBOX_DIR, ncch_program_id).data()); break;
const FileSys::Path inbox_path( case CecDataPathType::CEC_PATH_MBOX_INFO: {
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_INBOX_DIR, ncch_program_id).data()); CecMBoxInfoHeader mbox_info_header = {};
const FileSys::Path outbox_path( std::memcpy(&mbox_info_header, file_buffer.data(), sizeof(CecMBoxInfoHeader));
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_OUTBOX_DIR, ncch_program_id).data());
/// Just in case the root dir /CEC doesn't exist, we try to create it first if (file_buffer.size() != sizeof(CecMBoxInfoHeader)) { /// 0x60
Service::FS::CreateDirectoryFromArchive(cecd_system_save_data_archive, root_dir_path); LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader size is incorrect");
Service::FS::CreateDirectoryFromArchive(cecd_system_save_data_archive, mbox_path); }
Service::FS::CreateDirectoryFromArchive(cecd_system_save_data_archive, inbox_path);
Service::FS::CreateDirectoryFromArchive(cecd_system_save_data_archive, outbox_path);
/// Now that the directories have been created, we can create the required files if (mbox_info_header.magic != 0x6363) { /// 'cc'
FileSys::Mode mode; LOG_DEBUG(Service_CECD, "CecMBoxInfoHeader magic number is incorrect");
mode.write_flag.Assign(1); }
mode.create_flag.Assign(1);
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");
}
break;
}
case CecDataPathType::CEC_PATH_INBOX_INFO: {
CecInOutBoxInfoHeader inbox_info_header = {};
std::memcpy(&inbox_info_header, file_buffer.data(), sizeof(CecInOutBoxInfoHeader));
if (inbox_info_header.magic != 0x6262) { /// 'bb'
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader magic number is incorrect");
inbox_info_header.magic = 0x6262;
}
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);
}
if (inbox_info_header.max_box_size > 0x100000) {
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max box size is incorrect");
}
if (inbox_info_header.max_message_num > 99) {
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max box size is incorrect");
}
if (inbox_info_header.max_message_size >= 0x019000) {
LOG_DEBUG(Service_CECD, "CecInBoxInfoHeader max message size is incorrect");
}
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;
}
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));
if (outbox_info_header.magic != 0x6262) { /// 'bb'
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader magic number is incorrect");
outbox_info_header.magic = 0x6262;
}
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);
}
if (outbox_info_header.max_box_size > 0x100000) {
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max box size is incorrect");
}
if (outbox_info_header.max_message_num > 99) {
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max box size is incorrect");
}
if (outbox_info_header.max_message_size >= 0x019000) {
LOG_DEBUG(Service_CECD, "CecOutBoxInfoHeader max message size is incorrect");
}
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;
}
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, /// MBoxInfo____ resides in the MBox /CEC/<id> directory,
/// The Magic number is 0x6363 'cc', and has size 0x60 /// The Magic number is 0x6363 'cc', and has size 0x60
FileSys::Path mbox_info_path(
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_MBOX_INFO, ncch_program_id).data());
auto mbox_info_result =
Service::FS::OpenFileFromArchive(cecd_system_save_data_archive, mbox_info_path, mode);
constexpr u32 mbox_info_size = 0x60; constexpr u32 mbox_info_size = 0x60;
auto mbox_info = mbox_info_result.Unwrap(); auto mbox_info = mbox_info_result.Unwrap();
std::vector<u8> mbox_info_buffer(mbox_info_size); std::vector<u8> mbox_info_buffer(mbox_info_size);
u32_le le_ncch_program_id = ncch_program_id;
std::memset(&mbox_info_buffer[0], 0, mbox_info_size); CecMBoxInfoHeader mbox_info_header = {};
mbox_info_buffer[0] = 0x63; std::memset(&mbox_info_header, 0, sizeof(CecMBoxInfoHeader));
mbox_info_buffer[1] = 0x63;
mbox_info->backend->Write(0, mbox_info_size, true, mbox_info_buffer.data()); auto now = std::chrono::system_clock::now();
mbox_info->backend->Close(); 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?
/// BoxInfo_____ resides in both the InBox and OutBox directories, /// 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 /// 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. /// entries. Each entry is a copy of the message header.
FileSys::Path inbox_info_path(
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_INBOX_INFO, ncch_program_id).data());
auto inbox_info_result =
Service::FS::OpenFileFromArchive(cecd_system_save_data_archive, inbox_info_path, mode);
constexpr u32 inbox_info_size = 0x20;
auto inbox_info = inbox_info_result.Unwrap();
std::vector<u8> inbox_info_buffer(inbox_info_size);
CecInOutBoxInfoHeader inbox_info_header = {};
std::memset(&inbox_info_header, 0, sizeof(CecInOutBoxInfoHeader));
inbox_info_header.magic = 0x6262;
inbox_info_header.box_info_size = 0x20;
inbox_info_header.max_box_size = 0xC3000;
inbox_info_header.box_size = 0x00;
inbox_info_header.max_message_num = 0x0A;
inbox_info_header.message_num = 0x00;
inbox_info_header.max_batch_size = 0x0A;
inbox_info_header.max_message_size = 0x019000;
inbox_info->backend->Write(0, inbox_info_size, true, (u8*)&inbox_info_header);
inbox_info->backend->Close();
/// BoxInfo_____ resides in both the InBox and OutBox directories, /// 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 /// 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. /// entries. Each entry is a copy of the message header.
FileSys::Path outbox_info_path(
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_OUTBOX_INFO, ncch_program_id).data());
auto outbox_info_result =
Service::FS::OpenFileFromArchive(cecd_system_save_data_archive, outbox_info_path, mode);
constexpr u32 outbox_info_size = 0x20;
auto outbox_info = outbox_info_result.Unwrap();
std::vector<u8> outbox_info_buffer(outbox_info_size);
CecInOutBoxInfoHeader outbox_info_header = {};
std::memset(&outbox_info_header, 0, sizeof(CecInOutBoxInfoHeader));
outbox_info_header.magic = 0x6262;
outbox_info_header.box_info_size = 0x20;
outbox_info_header.max_box_size = 0xC000;
outbox_info_header.box_size = 0x00;
outbox_info_header.max_message_num = 0x01;
outbox_info_header.message_num = 0x00;
outbox_info_header.max_batch_size = 0x01;
outbox_info_header.max_message_size = 0x019000;
outbox_info->backend->Write(0, outbox_info_size, true, (u8*)&outbox_info_header);
outbox_info->backend->Close();
/// OBIndex_____ resides in the OutBox directory, /// OBIndex_____ resides in the OutBox directory,
/// The magic number is 0x6767 'gg', and the total size is 0x10 /// The magic number is 0x6767 'gg', and the total size is 0x10
FileSys::Path outbox_index_path(
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_OUTBOX_INDEX, ncch_program_id).data());
auto outbox_index_result =
Service::FS::OpenFileFromArchive(cecd_system_save_data_archive, outbox_index_path, mode);
constexpr u32 outbox_index_size = 0x10;
auto outbox_index = outbox_index_result.Unwrap();
std::vector<u8> outbox_index_buffer(outbox_index_size);
std::memset(&outbox_index_buffer[0], 0, outbox_index_size);
outbox_index_buffer[0] = 0x67;
outbox_index_buffer[1] = 0x67;
outbox_index->backend->Write(0, outbox_index_size, true, outbox_index_buffer.data());
outbox_index->backend->Close();
/// MBoxData.001 resides in the MBox /CEC/<id> directory and contains the icon of the app /// MBoxData.001 resides in the MBox /CEC/<id> directory and contains the icon of the app
FileSys::Path mbox_icon_path(
GetCecDataPathTypeAsString(CecDataPathType::CEC_MBOX_ICON, ncch_program_id).data());
auto mbox_icon_result =
Service::FS::OpenFileFromArchive(cecd_system_save_data_archive, mbox_icon_path, mode);
std::vector<u8> mbox_icon_buffer; std::vector<u8> mbox_icon_buffer;
Core::System::GetInstance().GetAppLoader().ReadIcon(mbox_icon_buffer); Core::System::GetInstance().GetAppLoader().ReadIcon(mbox_icon_buffer);
@ -688,12 +848,6 @@ void Module::CreateAndPopulateMBoxDirectory(const u32 ncch_program_id) {
/// MBoxData.010 resides in the MBox /CEC/<id> directory and contains the title of the app /// MBoxData.010 resides in the MBox /CEC/<id> directory and contains the title of the app
/// in null-terminated UTF-16 string. /// in null-terminated UTF-16 string.
FileSys::Path mbox_title_path(
GetCecDataPathTypeAsString(CecDataPathType::CEC_MBOX_TITLE, ncch_program_id).data());
auto mbox_title_result =
Service::FS::OpenFileFromArchive(cecd_system_save_data_archive, mbox_title_path, mode);
std::string title_name; std::string title_name;
Core::System::GetInstance().GetAppLoader().ReadTitle(title_name); Core::System::GetInstance().GetAppLoader().ReadTitle(title_name);
@ -702,20 +856,9 @@ void Module::CreateAndPopulateMBoxDirectory(const u32 ncch_program_id) {
const u32 mbox_title_size = program_name.size(); const u32 mbox_title_size = program_name.size();
auto mbox_title = mbox_title_result.Unwrap(); auto mbox_title = mbox_title_result.Unwrap();
std::vector<u8> mbox_title_buffer(mbox_title_size); std::vector<u8> mbox_title_buffer(mbox_title_size);
std::memcpy(mbox_title_buffer.data(), program_name.data(), mbox_title_size); std::memcpy(mbox_title_buffer.data(), program_name.data(), mbox_title_size);
mbox_title->backend->Write(0, mbox_title_size, true, mbox_title_buffer.data());
mbox_title->backend->Close();
/// MBoxData.050 resides in the MBox /CEC/<id> directory and contains the program id of the app /// MBoxData.050 resides in the MBox /CEC/<id> directory and contains the program id of the app
FileSys::Path mbox_program_id_path(
GetCecDataPathTypeAsString(CecDataPathType::CEC_MBOX_PROGRAM_ID, ncch_program_id).data());
auto mbox_program_id_result =
Service::FS::OpenFileFromArchive(cecd_system_save_data_archive, mbox_program_id_path, mode);
auto mbox_program_id = mbox_program_id_result.Unwrap();
std::vector<u8> program_id_buffer(8); std::vector<u8> program_id_buffer(8);
u64_le le_program_id = Kernel::g_current_process->codeset->program_id; u64_le le_program_id = Kernel::g_current_process->codeset->program_id;
std::memcpy(program_id_buffer.data(), &le_program_id, sizeof(u64)); std::memcpy(program_id_buffer.data(), &le_program_id, sizeof(u64));
@ -723,6 +866,7 @@ void Module::CreateAndPopulateMBoxDirectory(const u32 ncch_program_id) {
mbox_program_id->backend->Write(0, sizeof(u64), true, program_id_buffer.data()); mbox_program_id->backend->Write(0, sizeof(u64), true, program_id_buffer.data());
mbox_program_id->backend->Close(); mbox_program_id->backend->Close();
} }
*/
Module::SessionData::SessionData() {} Module::SessionData::SessionData() {}
@ -783,12 +927,15 @@ Module::Module() {
eventlog_buffer[2] = 0x12; eventlog_buffer[2] = 0x12;
std::memset(&eventlog_buffer[0x1000], 0xDD, eventlog_size - 0x1000); std::memset(&eventlog_buffer[0x1000], 0xDD, eventlog_size - 0x1000);
eventlog->backend->Write(0, eventlog_size, true, eventlog_buffer.data()).Unwrap(); eventlog->backend->Write(0, eventlog_size, true, eventlog_buffer.data());
eventlog->backend->Close(); eventlog->backend->Close();
/*
/// MBoxList____ resides within the root CEC/ directory. /// MBoxList____ resides within the root CEC/ directory.
/// Initially created, at offset 0x0, are bytes 0x68 0x68 0x00 0x00 0x01, with 0x6868 'hh', /// Initially created, at offset 0x0, are bytes 0x68 0x68 0x00 0x00 0x01, with
/// being the magic number. The rest of the file is filled with zeroes, until the end of 0x6868 'hh',
/// being the magic number. The rest of the file is filled with zeroes, until the
end of
/// file at offset 0x18b /// file at offset 0x18b
FileSys::Path mboxlist_path( FileSys::Path mboxlist_path(
GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_MBOX_LIST, 0).data()); GetCecDataPathTypeAsString(CecDataPathType::CEC_PATH_MBOX_LIST, 0).data());
@ -806,8 +953,9 @@ Module::Module() {
/// mboxlist_buffer[2-3] are already zeroed /// mboxlist_buffer[2-3] are already zeroed
mboxlist_buffer[4] = 0x01; mboxlist_buffer[4] = 0x01;
mboxlist->backend->Write(0, mboxlist_size, true, mboxlist_buffer.data()).Unwrap(); mboxlist->backend->Write(0, mboxlist_size, true, mboxlist_buffer.data());
mboxlist->backend->Close(); mboxlist->backend->Close();
*/
} }
ASSERT_MSG(archive_result.Succeeded(), "Could not open the CECD SystemSaveData archive!"); ASSERT_MSG(archive_result.Succeeded(), "Could not open the CECD SystemSaveData archive!");

View File

@ -109,7 +109,7 @@ public:
enum class CecSystemInfoType : u32 { EulaVersion = 1, Eula = 2, ParentControl = 3 }; enum class CecSystemInfoType : u32 { EulaVersion = 1, Eula = 2, ParentControl = 3 };
struct CecInOutBoxInfoHeader { struct CecInOutBoxInfoHeader {
u16_le magic; // bb u16_le magic; // 0x6262 'bb'
u16_le padding; u16_le padding;
u32_le box_info_size; u32_le box_info_size;
u32_le max_box_size; u32_le max_box_size;
@ -122,6 +122,36 @@ public:
static_assert(sizeof(CecInOutBoxInfoHeader) == 0x20, static_assert(sizeof(CecInOutBoxInfoHeader) == 0x20,
"CecInOutBoxInfoHeader struct has incorrect size."); "CecInOutBoxInfoHeader struct has incorrect size.");
struct CecMBoxInfoHeader {
u16_le magic; // 0x6363 'cc'
u16_le padding;
u32_le program_id;
u32_le private_id;
u8 flag;
u8 flag2;
u16_le padding2;
u8 hmac_key[32];
u32_le padding3;
/// year, 4 bytes, month 1 byte, day 1 byte, hour 1 byte, minute 1 byte
struct Time {
u32_le year;
u8 month;
u8 day;
u8 hour;
u8 minute;
u8 second;
u8 millisecond;
u8 microsecond;
u8 padding;
} last_accessed;
u32_le padding4;
Time last_received;
u32_le padding5;
Time unknown_time;
};
static_assert(sizeof(CecMBoxInfoHeader) == 0x60,
"CecMBoxInfoHeader struct has incorrect size.");
struct CecMessageHeader { struct CecMessageHeader {
u16_le magic; // `` u16_le magic; // ``
u16_le padding; u16_le padding;
@ -163,8 +193,8 @@ public:
u32 raw; u32 raw;
BitField<1, 1, u32> read; BitField<1, 1, u32> read;
BitField<2, 1, u32> write; BitField<2, 1, u32> write;
BitField<3, 1, u32> make_dir; BitField<3, 1, u32> create;
BitField<4, 1, u32> skip_check; /// or is it check/test? BitField<4, 1, u32> check; /// or is it check/test?
BitField<30, 1, u32> unk_flag; BitField<30, 1, u32> unk_flag;
}; };
@ -199,7 +229,7 @@ public:
protected: protected:
/** /**
* CECD::OpenRawFile service function * CECD::Open service function
* Inputs: * Inputs:
* 0 : Header Code[0x000100C2] * 0 : Header Code[0x000100C2]
* 1 : NCCH Program ID * 1 : NCCH Program ID
@ -211,10 +241,10 @@ public:
* 1 : Result of function, 0 on success, otherwise error code * 1 : Result of function, 0 on success, otherwise error code
* 2 : File size? * 2 : File size?
*/ */
void OpenRawFile(Kernel::HLERequestContext& ctx); void Open(Kernel::HLERequestContext& ctx);
/** /**
* CECD::ReadRawFile service function * CECD::ReadFile service function
* Inputs: * Inputs:
* 0 : Header Code[0x00020042] * 0 : Header Code[0x00020042]
* 1 : Buffer size (unused) * 1 : Buffer size (unused)
@ -226,7 +256,7 @@ public:
* 3 : Descriptor for mapping a write-only buffer in the target process * 3 : Descriptor for mapping a write-only buffer in the target process
* 4 : Buffer address * 4 : Buffer address
*/ */
void ReadRawFile(Kernel::HLERequestContext& ctx); void ReadFile(Kernel::HLERequestContext& ctx);
/** /**
* CECD::ReadMessage service function * CECD::ReadMessage service function
@ -277,7 +307,7 @@ public:
void ReadMessageWithHMAC(Kernel::HLERequestContext& ctx); void ReadMessageWithHMAC(Kernel::HLERequestContext& ctx);
/** /**
* CECD::WriteRawFile service function * CECD::WriteFile service function
* Inputs: * Inputs:
* 0 : Header Code[0x00050042] * 0 : Header Code[0x00050042]
* 1 : Buffer size(unused) * 1 : Buffer size(unused)
@ -288,7 +318,7 @@ public:
* 2 : Descriptor for mapping a read-only buffer in the target process * 2 : Descriptor for mapping a read-only buffer in the target process
* 3 : Buffer address * 3 : Buffer address
*/ */
void WriteRawFile(Kernel::HLERequestContext& ctx); void WriteFile(Kernel::HLERequestContext& ctx);
/** /**
* CECD::WriteMessage service function * CECD::WriteMessage service function
@ -534,7 +564,9 @@ private:
std::string GetCecDataPathTypeAsString(const CecDataPathType type, const u32 program_id, std::string GetCecDataPathTypeAsString(const CecDataPathType type, const u32 program_id,
const std::vector<u8>& msg_id = std::vector<u8>()) const; const std::vector<u8>& msg_id = std::vector<u8>()) const;
void CreateAndPopulateMBoxDirectory(const u32 ncch_program_id); // void CreateAndPopulateMBoxDirectory(const u32 ncch_program_id);
void CheckAndUpdateFile(const CecDataPathType path_type, const u32 ncch_program_id,
std::vector<u8>& file_buffer);
Service::FS::ArchiveHandle cecd_system_save_data_archive; Service::FS::ArchiveHandle cecd_system_save_data_archive;

View File

@ -12,11 +12,11 @@ CECD_S::CECD_S(std::shared_ptr<Module> cecd)
static const FunctionInfo functions[] = { static const FunctionInfo functions[] = {
// cecd:u shared commands // cecd:u shared commands
// clang-format off // clang-format off
{0x000100C2, &CECD_S::OpenRawFile, "OpenRawFile"}, {0x000100C2, &CECD_S::Open, "Open"},
{0x00020042, &CECD_S::ReadRawFile, "ReadRawFile"}, {0x00020042, &CECD_S::ReadFile, "ReadFile"},
{0x00030104, &CECD_S::ReadMessage, "ReadMessage"}, {0x00030104, &CECD_S::ReadMessage, "ReadMessage"},
{0x00040106, &CECD_S::ReadMessageWithHMAC, "ReadMessageWithHMAC"}, {0x00040106, &CECD_S::ReadMessageWithHMAC, "ReadMessageWithHMAC"},
{0x00050042, &CECD_S::WriteRawFile, "WriteRawFile"}, {0x00050042, &CECD_S::WriteFile, "WriteFile"},
{0x00060104, &CECD_S::WriteMessage, "WriteMessage"}, {0x00060104, &CECD_S::WriteMessage, "WriteMessage"},
{0x00070106, &CECD_S::WriteMessageWithHMAC, "WriteMessageWithHMAC"}, {0x00070106, &CECD_S::WriteMessageWithHMAC, "WriteMessageWithHMAC"},
{0x00080102, &CECD_S::Delete, "Delete"}, {0x00080102, &CECD_S::Delete, "Delete"},

View File

@ -12,11 +12,11 @@ CECD_U::CECD_U(std::shared_ptr<Module> cecd)
static const FunctionInfo functions[] = { static const FunctionInfo functions[] = {
// cecd:u shared commands // cecd:u shared commands
// clang-format off // clang-format off
{0x000100C2, &CECD_U::OpenRawFile, "OpenRawFile"}, {0x000100C2, &CECD_U::Open, "Open"},
{0x00020042, &CECD_U::ReadRawFile, "ReadRawFile"}, {0x00020042, &CECD_U::ReadFile, "ReadFile"},
{0x00030104, &CECD_U::ReadMessage, "ReadMessage"}, {0x00030104, &CECD_U::ReadMessage, "ReadMessage"},
{0x00040106, &CECD_U::ReadMessageWithHMAC, "ReadMessageWithHMAC"}, {0x00040106, &CECD_U::ReadMessageWithHMAC, "ReadMessageWithHMAC"},
{0x00050042, &CECD_U::WriteRawFile, "WriteRawFile"}, {0x00050042, &CECD_U::WriteFile, "WriteFile"},
{0x00060104, &CECD_U::WriteMessage, "WriteMessage"}, {0x00060104, &CECD_U::WriteMessage, "WriteMessage"},
{0x00070106, &CECD_U::WriteMessageWithHMAC, "WriteMessageWithHMAC"}, {0x00070106, &CECD_U::WriteMessageWithHMAC, "WriteMessageWithHMAC"},
{0x00080102, &CECD_U::Delete, "Delete"}, {0x00080102, &CECD_U::Delete, "Delete"},