From fa3d72ab3e5ba8b3e8dccdb1d3bd9b976dbc28e2 Mon Sep 17 00:00:00 2001 From: Subv Date: Thu, 18 Dec 2014 23:35:24 -0500 Subject: [PATCH 01/20] CFG: Implemented the GetConfigInfoBlk2 function. Added a "config" file to the CFG process service (CFG:U), and added a few default blocks to it. Implemented GetSystemModel and GetModelNintendo2DS --- src/core/file_sys/archive_backend.h | 5 + src/core/file_sys/archive_systemsavedata.cpp | 5 +- src/core/file_sys/archive_systemsavedata.h | 2 +- src/core/hle/service/cfg_u.cpp | 191 ++++++++++++++++++- src/core/hle/service/fs/archive.cpp | 9 - 5 files changed, 197 insertions(+), 15 deletions(-) diff --git a/src/core/file_sys/archive_backend.h b/src/core/file_sys/archive_backend.h index eb1fdaa1f0..e2979be179 100644 --- a/src/core/file_sys/archive_backend.h +++ b/src/core/file_sys/archive_backend.h @@ -45,6 +45,11 @@ public: { } + Path(const char* path): + type(Char), string(path) + { + } + Path(LowPathType type, u32 size, u32 pointer): type(type) { diff --git a/src/core/file_sys/archive_systemsavedata.cpp b/src/core/file_sys/archive_systemsavedata.cpp index 5da1ec9468..392c3cd392 100644 --- a/src/core/file_sys/archive_systemsavedata.cpp +++ b/src/core/file_sys/archive_systemsavedata.cpp @@ -16,8 +16,9 @@ namespace FileSys { -Archive_SystemSaveData::Archive_SystemSaveData(const std::string& mount_point) - : DiskArchive(mount_point) { +Archive_SystemSaveData::Archive_SystemSaveData(const std::string& mount_point, u64 save_id) + : DiskArchive(Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), + static_cast(save_id & 0xFFFFFFFF), static_cast((save_id >> 31) & 0xFFFFFFFF))) { LOG_INFO(Service_FS, "Directory %s set as SystemSaveData.", this->mount_point.c_str()); } diff --git a/src/core/file_sys/archive_systemsavedata.h b/src/core/file_sys/archive_systemsavedata.h index c3ebb7c994..443e270919 100644 --- a/src/core/file_sys/archive_systemsavedata.h +++ b/src/core/file_sys/archive_systemsavedata.h @@ -19,7 +19,7 @@ namespace FileSys { /// specifically nand:/data//sysdata// class Archive_SystemSaveData final : public DiskArchive { public: - Archive_SystemSaveData(const std::string& mount_point); + Archive_SystemSaveData(const std::string& mount_point, u64 save_id); /** * Initialize the archive. diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index 6fcd1d7f36..e6c9ce1fa2 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -2,7 +2,9 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "common/file_util.h" #include "common/log.h" +#include "core/file_sys/archive_systemsavedata.h" #include "core/hle/hle.h" #include "core/hle/service/cfg_u.h" @@ -11,6 +13,19 @@ namespace CFG_U { +static std::unique_ptr cfg_system_save_data; +static const u64 CFG_SAVE_ID = 0x00010017; +static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; +static const char CONSOLE_USERNAME[0x1C] = "THIS IS CITRAAAAAAAAAAAAAA"; + +enum SystemModel { + NINTENDO_3DS, + NINTENDO_3DS_XL, + NEW_NINTENDO_3DS, + NINTENDO_2DS, + NEW_NINTENDO_3DS_XL +}; + // TODO(Link Mauve): use a constexpr once MSVC starts supporting it. #define C(code) ((code)[0] | ((code)[1] << 8)) @@ -99,13 +114,137 @@ static void GetCountryCodeID(Service::Interface* self) { cmd_buffer[2] = country_code_id; } +/// Block header in the config savedata file +struct SaveConfigBlockEntry { + u32 block_id; + u32 offset_or_data; + u16 size; + u16 flags; +}; + +/// The header of the config savedata file, +/// contains information about the blocks in the file +struct SaveFileConfig { + u16 total_entries; + u16 data_entries_offset; + SaveConfigBlockEntry block_entries[1479]; +}; + +/* Reads a block with the specified id and flag from the Config savegame file + * and writes the output to output. + * The input size must match exactly the size of the requested block + * TODO(Subv): This should actually be in some file common to the CFG process + * @param block_id The id of the block we want to read + * @param size The size of the block we want to read + * @param flag The requested block must have this flag set + * @param output A pointer where we will write the read data + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { + FileSys::Mode mode; + mode.hex = 0; + mode.read_flag = 1; + FileSys::Path path("config"); + auto file = cfg_system_save_data->OpenFile(path, mode); + _dbg_assert_msg_(Service_CFG, file != nullptr, "Could not open the CFG service config file"); + SaveFileConfig config; + size_t read = file->Read(0, sizeof(SaveFileConfig), reinterpret_cast(&config)); + + if (read != sizeof(SaveFileConfig)) { + LOG_CRITICAL(Service_CFG, "The config savefile is corrupted"); + return ResultCode(-1); // TODO(Subv): Find the correct error code + } + + auto itr = std::find_if(std::begin(config.block_entries), std::end(config.block_entries), + [&](SaveConfigBlockEntry const& entry) { + return entry.block_id == block_id && entry.size == size && (entry.flags & flag); + }); + + if (itr == std::end(config.block_entries)) { + LOG_TRACE(Service_CFG, "Config block %u with size %u and flags %u not found", block_id, size, flag); + return ResultCode(-1); // TODO(Subv): Find the correct error code + } + + // The data is located in the block header itself if the size is less than 4 bytes + if (itr->size <= 4) { + memcpy(output, &itr->offset_or_data, itr->size); + } else { + size_t data_read = file->Read(itr->offset_or_data, itr->size, output); + if (data_read != itr->size) { + LOG_CRITICAL(Service_CFG, "The config savefile is corrupted"); + return ResultCode(-1); // TODO(Subv): Find the correct error code + } + } + + return RESULT_SUCCESS; +} + +/** + * CFG_User::GetConfigInfoBlk2 service function + * Inputs: + * 1 : Size + * 2 : Block ID + * 3 : Descriptor for the output buffer + * 4 : Output buffer pointer + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + */ +static void GetConfigInfoBlk2(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + u32 size = cmd_buffer[1]; + u32 block_id = cmd_buffer[2]; + u8* data_pointer = Memory::GetPointer(cmd_buffer[4]); + + if (data_pointer == nullptr) { + cmd_buffer[1] = -1; // TODO(Subv): Find the right error code + return; + } + + cmd_buffer[1] = GetConfigInfoBlock(block_id, size, 0x2, data_pointer).raw; +} + +/** + * CFG_User::GetSystemModel service function + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + * 2 : Model of the console + */ +static void GetSystemModel(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + u32 data; + + cmd_buffer[1] = GetConfigInfoBlock(0x000F0004, 4, 0x8, + reinterpret_cast(&data)).raw; // TODO(Subv): Find out the correct error codes + cmd_buffer[2] = data & 0xFF; +} + +/** + * CFG_User::GetModelNintendo2DS service function + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + * 2 : 0 if the system is a Nintendo 2DS, 1 otherwise + */ +static void GetModelNintendo2DS(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + u32 data; + + cmd_buffer[1] = GetConfigInfoBlock(0x000F0004, 4, 0x8, + reinterpret_cast(&data)).raw; // TODO(Subv): Find out the correct error codes + + u8 model = data & 0xFF; + if (model == NINTENDO_2DS) + cmd_buffer[2] = 0; + else + cmd_buffer[2] = 1; +} + const Interface::FunctionInfo FunctionTable[] = { - {0x00010082, nullptr, "GetConfigInfoBlk2"}, + {0x00010082, GetConfigInfoBlk2, "GetConfigInfoBlk2"}, {0x00020000, nullptr, "SecureInfoGetRegion"}, {0x00030000, nullptr, "GenHashConsoleUnique"}, {0x00040000, nullptr, "GetRegionCanadaUSA"}, - {0x00050000, nullptr, "GetSystemModel"}, - {0x00060000, nullptr, "GetModelNintendo2DS"}, + {0x00050000, GetSystemModel, "GetSystemModel"}, + {0x00060000, GetModelNintendo2DS, "GetModelNintendo2DS"}, {0x00070040, nullptr, "unknown"}, {0x00080080, nullptr, "unknown"}, {0x00090040, GetCountryCodeString, "GetCountryCodeString"}, @@ -116,6 +255,52 @@ const Interface::FunctionInfo FunctionTable[] = { Interface::Interface() { Register(FunctionTable, ARRAY_SIZE(FunctionTable)); + // TODO(Subv): In the future we should use the FS service to query this archive, + // currently it is not possible because you can only have one open archive of the same type at any time + std::string syssavedata_directory = FileUtil::GetUserPath(D_SYSSAVEDATA_IDX); + cfg_system_save_data = std::make_unique(syssavedata_directory, + CFG_SAVE_ID); + if (!cfg_system_save_data->Initialize()) { + LOG_CRITICAL(Service_CFG, "Could not initialize SystemSaveData archive for the CFG:U service"); + return; + } + + // Try to open the file in read-only mode to check its existence + FileSys::Mode mode; + mode.hex = 0; + mode.read_flag = 1; + FileSys::Path path("config"); + auto file = cfg_system_save_data->OpenFile(path, mode); + + // Don't do anything if the file already exists + if (file != nullptr) + return; + + mode.create_flag = 1; + mode.write_flag = 1; + mode.read_flag = 0; + // Re-open the file in write-create mode + file = cfg_system_save_data->OpenFile(path, mode); + + // Setup the default config file data header + SaveFileConfig config = { 3, 0, {} }; + u32 offset = sizeof(SaveFileConfig); + // Console-unique ID + config.block_entries[0] = { 0x00090001, offset, 0x8, 0xE }; + offset += 0x8; + // Username + config.block_entries[1] = { 0x000A0000, offset, 0x1C, 0xE }; + offset += 0x1C; + // System Model (Nintendo 3DS XL) + config.block_entries[2] = { 0x000F0004, NINTENDO_3DS_XL, 0x4, 0x8 }; + + // Write the config file data header to the config file + file->Write(0, sizeof(SaveFileConfig), 1, reinterpret_cast(&config)); + // Write the data itself + file->Write(config.block_entries[0].offset_or_data, 0x8, 1, + reinterpret_cast(&CONSOLE_UNIQUE_ID)); + file->Write(config.block_entries[1].offset_or_data, 0x1C, 1, + reinterpret_cast(CONSOLE_USERNAME)); } Interface::~Interface() { diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp index e2a59a0693..98db02f151 100644 --- a/src/core/hle/service/fs/archive.cpp +++ b/src/core/hle/service/fs/archive.cpp @@ -428,15 +428,6 @@ void ArchiveInit() { CreateArchive(std::move(sdmc_archive), ArchiveIdCode::SDMC); else LOG_ERROR(Service_FS, "Can't instantiate SDMC archive with path %s", sdmc_directory.c_str()); - - std::string systemsavedata_directory = FileUtil::GetUserPath(D_SYSSAVEDATA_IDX); - auto systemsavedata_archive = Common::make_unique(systemsavedata_directory); - if (systemsavedata_archive->Initialize()) { - CreateArchive(std::move(systemsavedata_archive), ArchiveIdCode::SystemSaveData); - } else { - LOG_ERROR(Service_FS, "Can't instantiate SystemSaveData archive with path %s", - systemsavedata_directory.c_str()); - } } /// Shutdown archives From 462740278dfcf84b712f4cd615dd69cafa4373ee Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 19 Dec 2014 02:04:54 -0500 Subject: [PATCH 02/20] CFG:U: Add some data to the 0x00050005 config block. Seems to allow some games to boot further, thanks @Normmatt for sharing this information --- src/core/hle/service/cfg_u.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index e6c9ce1fa2..771575e298 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -16,7 +16,13 @@ namespace CFG_U { static std::unique_ptr cfg_system_save_data; static const u64 CFG_SAVE_ID = 0x00010017; static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; -static const char CONSOLE_USERNAME[0x1C] = "THIS IS CITRAAAAAAAAAAAAAA"; + +/// TODO(Subv): Find out what this actually is +/// Thanks Normmatt for providing this information +static const u8 STEREO_CAMERA_SETTINGS[32] = { + 0x00, 0x00, 0x78, 0x42, 0x00, 0x80, 0x90, 0x43, 0x9A, 0x99, 0x99, 0x42, 0xEC, 0x51, 0x38, 0x42, + 0x00, 0x00, 0x20, 0x41, 0x00, 0x00, 0xA0, 0x40, 0xEC, 0x51, 0x5E, 0x42, 0x5C, 0x8F, 0xAC, 0x41 +}; enum SystemModel { NINTENDO_3DS, @@ -288,9 +294,9 @@ Interface::Interface() { // Console-unique ID config.block_entries[0] = { 0x00090001, offset, 0x8, 0xE }; offset += 0x8; - // Username - config.block_entries[1] = { 0x000A0000, offset, 0x1C, 0xE }; - offset += 0x1C; + // Stereo Camera Settings? + config.block_entries[1] = { 0x00050005, offset, 0x20, 0xE }; + offset += 0x20; // System Model (Nintendo 3DS XL) config.block_entries[2] = { 0x000F0004, NINTENDO_3DS_XL, 0x4, 0x8 }; @@ -299,8 +305,7 @@ Interface::Interface() { // Write the data itself file->Write(config.block_entries[0].offset_or_data, 0x8, 1, reinterpret_cast(&CONSOLE_UNIQUE_ID)); - file->Write(config.block_entries[1].offset_or_data, 0x1C, 1, - reinterpret_cast(CONSOLE_USERNAME)); + file->Write(config.block_entries[1].offset_or_data, 0x20, 1, STEREO_CAMERA_SETTINGS); } Interface::~Interface() { From 4cd21b43c1e28933898c4a2e829555efe22ff12e Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 19 Dec 2014 15:30:25 -0500 Subject: [PATCH 03/20] CFG: Refactored how the config file works. It is now kept in memory as per 3dbrew, all updates happen on memory, then they can be saved using UpdateConfigNANDSavegame. --- src/core/file_sys/archive_systemsavedata.cpp | 2 +- src/core/hle/service/cfg_u.cpp | 187 +++++++++++++------ 2 files changed, 130 insertions(+), 59 deletions(-) diff --git a/src/core/file_sys/archive_systemsavedata.cpp b/src/core/file_sys/archive_systemsavedata.cpp index 392c3cd392..b942864b29 100644 --- a/src/core/file_sys/archive_systemsavedata.cpp +++ b/src/core/file_sys/archive_systemsavedata.cpp @@ -18,7 +18,7 @@ namespace FileSys { Archive_SystemSaveData::Archive_SystemSaveData(const std::string& mount_point, u64 save_id) : DiskArchive(Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), - static_cast(save_id & 0xFFFFFFFF), static_cast((save_id >> 31) & 0xFFFFFFFF))) { + static_cast(save_id & 0xFFFFFFFF), static_cast((save_id >> 32) & 0xFFFFFFFF))) { LOG_INFO(Service_FS, "Directory %s set as SystemSaveData.", this->mount_point.c_str()); } diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index 771575e298..827399cf9c 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -13,17 +13,6 @@ namespace CFG_U { -static std::unique_ptr cfg_system_save_data; -static const u64 CFG_SAVE_ID = 0x00010017; -static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; - -/// TODO(Subv): Find out what this actually is -/// Thanks Normmatt for providing this information -static const u8 STEREO_CAMERA_SETTINGS[32] = { - 0x00, 0x00, 0x78, 0x42, 0x00, 0x80, 0x90, 0x43, 0x9A, 0x99, 0x99, 0x42, 0xEC, 0x51, 0x38, 0x42, - 0x00, 0x00, 0x20, 0x41, 0x00, 0x00, 0xA0, 0x40, 0xEC, 0x51, 0x5E, 0x42, 0x5C, 0x8F, 0xAC, 0x41 -}; - enum SystemModel { NINTENDO_3DS, NINTENDO_3DS_XL, @@ -32,6 +21,20 @@ enum SystemModel { NEW_NINTENDO_3DS_XL }; +static std::unique_ptr cfg_system_save_data; +static const u64 CFG_SAVE_ID = 0x00010017; +static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; +static const u32 CONSOLE_MODEL = NINTENDO_3DS_XL; +static const u32 CONFIG_SAVEFILE_SIZE = 0x8000; +static std::array cfg_config_file_buffer = { }; + +/// TODO(Subv): Find out what this actually is +/// Thanks Normmatt for providing this information +static const u8 STEREO_CAMERA_SETTINGS[32] = { + 0x00, 0x00, 0x78, 0x42, 0x00, 0x80, 0x90, 0x43, 0x9A, 0x99, 0x99, 0x42, 0xEC, 0x51, 0x38, 0x42, + 0x00, 0x00, 0x20, 0x41, 0x00, 0x00, 0xA0, 0x40, 0xEC, 0x51, 0x5E, 0x42, 0x5C, 0x8F, 0xAC, 0x41 +}; + // TODO(Link Mauve): use a constexpr once MSVC starts supporting it. #define C(code) ((code)[0] | ((code)[1] << 8)) @@ -134,9 +137,11 @@ struct SaveFileConfig { u16 total_entries; u16 data_entries_offset; SaveConfigBlockEntry block_entries[1479]; + u32 unknown; }; -/* Reads a block with the specified id and flag from the Config savegame file +/** + * Reads a block with the specified id and flag from the Config savegame buffer * and writes the output to output. * The input size must match exactly the size of the requested block * TODO(Subv): This should actually be in some file common to the CFG process @@ -147,41 +152,128 @@ struct SaveFileConfig { * @returns ResultCode indicating the result of the operation, 0 on success */ ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { - FileSys::Mode mode; - mode.hex = 0; - mode.read_flag = 1; - FileSys::Path path("config"); - auto file = cfg_system_save_data->OpenFile(path, mode); - _dbg_assert_msg_(Service_CFG, file != nullptr, "Could not open the CFG service config file"); - SaveFileConfig config; - size_t read = file->Read(0, sizeof(SaveFileConfig), reinterpret_cast(&config)); + // Read the header + SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); - if (read != sizeof(SaveFileConfig)) { - LOG_CRITICAL(Service_CFG, "The config savefile is corrupted"); - return ResultCode(-1); // TODO(Subv): Find the correct error code - } - - auto itr = std::find_if(std::begin(config.block_entries), std::end(config.block_entries), + auto itr = std::find_if(std::begin(config->block_entries), std::end(config->block_entries), [&](SaveConfigBlockEntry const& entry) { return entry.block_id == block_id && entry.size == size && (entry.flags & flag); }); - if (itr == std::end(config.block_entries)) { - LOG_TRACE(Service_CFG, "Config block %u with size %u and flags %u not found", block_id, size, flag); + if (itr == std::end(config->block_entries)) { + LOG_ERROR(Service_CFG, "Config block %u with size %u and flags %u not found", block_id, size, flag); return ResultCode(-1); // TODO(Subv): Find the correct error code } // The data is located in the block header itself if the size is less than 4 bytes - if (itr->size <= 4) { + if (itr->size <= 4) memcpy(output, &itr->offset_or_data, itr->size); - } else { - size_t data_read = file->Read(itr->offset_or_data, itr->size, output); - if (data_read != itr->size) { - LOG_CRITICAL(Service_CFG, "The config savefile is corrupted"); - return ResultCode(-1); // TODO(Subv): Find the correct error code + else + memcpy(output, &cfg_config_file_buffer[config->data_entries_offset + itr->offset_or_data], itr->size); + + return RESULT_SUCCESS; +} + +/** + * Creates a block with the specified id and writes the input data to the cfg savegame buffer in memory. + * The config savegame file in the filesystem is not updated. + * TODO(Subv): This should actually be in some file common to the CFG process + * @param block_id The id of the block we want to create + * @param size The size of the block we want to create + * @param flag The flags of the new block + * @param data A pointer containing the data we will write to the new block + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, u8 const* data) { + SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); + // Insert the block header with offset 0 for now + config->block_entries[config->total_entries] = { block_id, 0, size, flags }; + if (size > 4) { + s32 total_entries = config->total_entries - 1; + u32 offset = 0; + // Perform a search to locate the next offset for the new data + while (total_entries >= 0) { + // Ignore the blocks that don't have a separate data offset + if (config->block_entries[total_entries].size <= 4) { + --total_entries; + continue; + } + + offset = config->block_entries[total_entries].offset_or_data + + config->block_entries[total_entries].size; + break; } + + config->block_entries[config->total_entries].offset_or_data = offset; + + // Write the data at the new offset + memcpy(&cfg_config_file_buffer[config->data_entries_offset + offset], data, size); + } else { + // The offset_or_data field in the header contains the data itself if it's 4 bytes or less + memcpy(&config->block_entries[config->total_entries].offset_or_data, data, size); } + ++config->total_entries; + return RESULT_SUCCESS; +} + +/** + * Deletes the config savegame file from the filesystem, the buffer in memory is not affected + * TODO(Subv): This should actually be in some file common to the CFG process + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode DeleteConfigNANDSaveFile() { + FileSys::Path path("config"); + if (cfg_system_save_data->DeleteFile(path)) + return RESULT_SUCCESS; + return ResultCode(-1); // TODO(Subv): Find the right error code +} + +/** + * Writes the config savegame memory buffer to the config savegame file in the filesystem + * TODO(Subv): This should actually be in some file common to the CFG process + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode UpdateConfigNANDSavegame() { + FileSys::Mode mode; + mode.hex = 0; + mode.write_flag = 1; + mode.create_flag = 1; + FileSys::Path path("config"); + auto file = cfg_system_save_data->OpenFile(path, mode); + _dbg_assert_msg_(Service_CFG, file != nullptr, "could not open file"); + file->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data()); + return RESULT_SUCCESS; +} + +/** + * Re-creates the config savegame file in memory and the filesystem with the default blocks + * TODO(Subv): This should actually be in some file common to the CFG process + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode FormatConfig() { + ResultCode res = DeleteConfigNANDSaveFile(); + if (!res.IsSuccess()) + return res; + // Delete the old data + std::fill(cfg_config_file_buffer.begin(), cfg_config_file_buffer.end(), 0); + // Create the header + SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); + config->data_entries_offset = 0x455C; + // Insert the default blocks + res = CreateConfigInfoBlk(0x00050005, 0x20, 0xE, STEREO_CAMERA_SETTINGS); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x00090001, 0x8, 0xE, reinterpret_cast(&CONSOLE_UNIQUE_ID)); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x000F0004, 0x4, 0x8, reinterpret_cast(&CONSOLE_MODEL)); + if (!res.IsSuccess()) + return res; + // Save the buffer to the file + res = UpdateConfigNANDSavegame(); + if (!res.IsSuccess()) + return res; return RESULT_SUCCESS; } @@ -271,6 +363,8 @@ Interface::Interface() { return; } + // TODO(Subv): All this code should be moved to cfg:i, + // it's only here because we do not currently emulate the lower level code that uses that service // Try to open the file in read-only mode to check its existence FileSys::Mode mode; mode.hex = 0; @@ -282,30 +376,7 @@ Interface::Interface() { if (file != nullptr) return; - mode.create_flag = 1; - mode.write_flag = 1; - mode.read_flag = 0; - // Re-open the file in write-create mode - file = cfg_system_save_data->OpenFile(path, mode); - - // Setup the default config file data header - SaveFileConfig config = { 3, 0, {} }; - u32 offset = sizeof(SaveFileConfig); - // Console-unique ID - config.block_entries[0] = { 0x00090001, offset, 0x8, 0xE }; - offset += 0x8; - // Stereo Camera Settings? - config.block_entries[1] = { 0x00050005, offset, 0x20, 0xE }; - offset += 0x20; - // System Model (Nintendo 3DS XL) - config.block_entries[2] = { 0x000F0004, NINTENDO_3DS_XL, 0x4, 0x8 }; - - // Write the config file data header to the config file - file->Write(0, sizeof(SaveFileConfig), 1, reinterpret_cast(&config)); - // Write the data itself - file->Write(config.block_entries[0].offset_or_data, 0x8, 1, - reinterpret_cast(&CONSOLE_UNIQUE_ID)); - file->Write(config.block_entries[1].offset_or_data, 0x20, 1, STEREO_CAMERA_SETTINGS); + FormatConfig(); } Interface::~Interface() { From b49bdb6ba7700007240c207afb69c75f8fbd0f62 Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 19 Dec 2014 17:01:23 -0500 Subject: [PATCH 04/20] CFGU: Added block 0x000A0002 to the default savegame file That's the language id block, we're using LANGUAGE_EN for now. This block allows some games to boot further --- src/core/hle/service/cfg_u.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index 827399cf9c..5530454378 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -21,10 +21,25 @@ enum SystemModel { NEW_NINTENDO_3DS_XL }; +enum SystemLanguage { + LANGUAGE_JP, + LANGUAGE_EN, + LANGUAGE_FR, + LANGUAGE_DE, + LANGUAGE_IT, + LANGUAGE_ES, + LANGUAGE_ZH, + LANGUAGE_KO, + LANGUAGE_NL, + LANGUAGE_PT, + LANGUAGE_RU +}; + static std::unique_ptr cfg_system_save_data; static const u64 CFG_SAVE_ID = 0x00010017; static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; static const u32 CONSOLE_MODEL = NINTENDO_3DS_XL; +static const u8 CONSOLE_LANGUAGE = LANGUAGE_EN; static const u32 CONFIG_SAVEFILE_SIZE = 0x8000; static std::array cfg_config_file_buffer = { }; @@ -268,6 +283,9 @@ ResultCode FormatConfig() { if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x000F0004, 0x4, 0x8, reinterpret_cast(&CONSOLE_MODEL)); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x000A0002, 0x1, 0xA, &CONSOLE_LANGUAGE); if (!res.IsSuccess()) return res; // Save the buffer to the file From 95ca6ae1e1c96de395f9d75b953b8ebb7823e9fa Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 19 Dec 2014 18:39:57 -0500 Subject: [PATCH 05/20] CFG: Load the Config savedata file if it already exists. --- src/core/hle/service/cfg_u.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index 5530454378..ee97cf3e5d 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -390,10 +390,11 @@ Interface::Interface() { FileSys::Path path("config"); auto file = cfg_system_save_data->OpenFile(path, mode); - // Don't do anything if the file already exists - if (file != nullptr) + // Load the config if it already exists + if (file != nullptr) { + file->Read(0, CONFIG_SAVEFILE_SIZE, cfg_config_file_buffer.data()); return; - + } FormatConfig(); } From b3d1c8ba6a57946658a2b3823a1920bc3a22a982 Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 19 Dec 2014 19:57:42 -0500 Subject: [PATCH 06/20] CFGU: Use an absolute offset in the config savefile blocks --- src/core/hle/service/cfg_u.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index ee97cf3e5d..33f63a7592 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -184,7 +184,7 @@ ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { if (itr->size <= 4) memcpy(output, &itr->offset_or_data, itr->size); else - memcpy(output, &cfg_config_file_buffer[config->data_entries_offset + itr->offset_or_data], itr->size); + memcpy(output, &cfg_config_file_buffer[itr->offset_or_data], itr->size); return RESULT_SUCCESS; } @@ -219,6 +219,8 @@ ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, u8 const* data break; } + offset += config->data_entries_offset; + config->block_entries[config->total_entries].offset_or_data = offset; // Write the data at the new offset From 8b0ee935267c23e0d213266cfdb4535b91a8a5c8 Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 19 Dec 2014 20:10:05 -0500 Subject: [PATCH 07/20] CFG: Implemented block 0x00070001 in the config savefile --- src/core/hle/service/cfg_u.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index 33f63a7592..ef8e8c3c94 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -40,6 +40,8 @@ static const u64 CFG_SAVE_ID = 0x00010017; static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; static const u32 CONSOLE_MODEL = NINTENDO_3DS_XL; static const u8 CONSOLE_LANGUAGE = LANGUAGE_EN; +/// TODO(Subv): Find out what this actually is +static const u8 SOUND_OUTPUT_MODE = 2; static const u32 CONFIG_SAVEFILE_SIZE = 0x8000; static std::array cfg_config_file_buffer = { }; @@ -288,6 +290,9 @@ ResultCode FormatConfig() { if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x000A0002, 0x1, 0xA, &CONSOLE_LANGUAGE); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x00070001, 0x1, 0xE, &SOUND_OUTPUT_MODE); if (!res.IsSuccess()) return res; // Save the buffer to the file From 9029efd873758a1cd668e8394d089d4486bc9c43 Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 19 Dec 2014 21:51:31 -0500 Subject: [PATCH 08/20] CFG:U: Implemented some more blocks --- src/core/hle/service/cfg_u.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index ef8e8c3c94..0e56f1245b 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -4,6 +4,7 @@ #include "common/file_util.h" #include "common/log.h" +#include "common/string_util.h" #include "core/file_sys/archive_systemsavedata.h" #include "core/hle/hle.h" #include "core/hle/service/cfg_u.h" @@ -35,11 +36,21 @@ enum SystemLanguage { LANGUAGE_RU }; +struct UsernameBlock { + char16_t username[10]; ///< Exactly 20 bytes long, padded with zeros at the end if necessary + u32 zero; + u32 ng_word; +}; +static_assert(sizeof(UsernameBlock) == 0x1C, "Size of UsernameBlock must be 0x1C"); + static std::unique_ptr cfg_system_save_data; static const u64 CFG_SAVE_ID = 0x00010017; static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; static const u32 CONSOLE_MODEL = NINTENDO_3DS_XL; static const u8 CONSOLE_LANGUAGE = LANGUAGE_EN; +static const char CONSOLE_USERNAME[0x14] = "CITRA"; +/// This will be initialized in the Interface constructor, and will be used when creating the block +static UsernameBlock CONSOLE_USERNAME_BLOCK; /// TODO(Subv): Find out what this actually is static const u8 SOUND_OUTPUT_MODE = 2; static const u32 CONFIG_SAVEFILE_SIZE = 0x8000; @@ -55,6 +66,9 @@ static const u8 STEREO_CAMERA_SETTINGS[32] = { // TODO(Link Mauve): use a constexpr once MSVC starts supporting it. #define C(code) ((code)[0] | ((code)[1] << 8)) +/// TODO(Subv): Find what the other bytes are +static const std::array COUNTRY_INFO = { 0, 0, 0, C("US") }; + static const std::array country_codes = { 0, C("JP"), 0, 0, 0, 0, 0, 0, // 0-7 C("AI"), C("AG"), C("AR"), C("AW"), C("BS"), C("BB"), C("BZ"), C("BO"), // 8-15 @@ -207,7 +221,7 @@ ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, u8 const* data config->block_entries[config->total_entries] = { block_id, 0, size, flags }; if (size > 4) { s32 total_entries = config->total_entries - 1; - u32 offset = 0; + u32 offset = config->data_entries_offset; // Perform a search to locate the next offset for the new data while (total_entries >= 0) { // Ignore the blocks that don't have a separate data offset @@ -221,12 +235,10 @@ ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, u8 const* data break; } - offset += config->data_entries_offset; - config->block_entries[config->total_entries].offset_or_data = offset; // Write the data at the new offset - memcpy(&cfg_config_file_buffer[config->data_entries_offset + offset], data, size); + memcpy(&cfg_config_file_buffer[offset], data, size); } else { // The offset_or_data field in the header contains the data itself if it's 4 bytes or less memcpy(&config->block_entries[config->total_entries].offset_or_data, data, size); @@ -293,6 +305,12 @@ ResultCode FormatConfig() { if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x00070001, 0x1, 0xE, &SOUND_OUTPUT_MODE); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x000B0000, 0x4, 0xE, COUNTRY_INFO.data()); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x000A0000, 0x1C, 0xE, reinterpret_cast(&CONSOLE_USERNAME_BLOCK)); if (!res.IsSuccess()) return res; // Save the buffer to the file @@ -402,6 +420,14 @@ Interface::Interface() { file->Read(0, CONFIG_SAVEFILE_SIZE, cfg_config_file_buffer.data()); return; } + + // Initialize the Username block + // TODO(Subv): Do this somewhere else, or in another way + CONSOLE_USERNAME_BLOCK.ng_word = 0; + CONSOLE_USERNAME_BLOCK.zero = 0; + std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + + Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14), + std::end(CONSOLE_USERNAME_BLOCK.username), 0); FormatConfig(); } From a7cc7972de3401ed2ccc5002272695f098b4955f Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 20 Dec 2014 16:47:47 -0500 Subject: [PATCH 09/20] CFG_U: Use Common::make_unique instead of the std version --- src/core/hle/service/cfg_u.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index 0e56f1245b..5d21fcae8e 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -4,6 +4,7 @@ #include "common/file_util.h" #include "common/log.h" +#include "common/make_unique.h" #include "common/string_util.h" #include "core/file_sys/archive_systemsavedata.h" #include "core/hle/hle.h" @@ -399,7 +400,7 @@ Interface::Interface() { // TODO(Subv): In the future we should use the FS service to query this archive, // currently it is not possible because you can only have one open archive of the same type at any time std::string syssavedata_directory = FileUtil::GetUserPath(D_SYSSAVEDATA_IDX); - cfg_system_save_data = std::make_unique(syssavedata_directory, + cfg_system_save_data = Common::make_unique(syssavedata_directory, CFG_SAVE_ID); if (!cfg_system_save_data->Initialize()) { LOG_CRITICAL(Service_CFG, "Could not initialize SystemSaveData archive for the CFG:U service"); From a1b9b80a55121320fa543fa40fcde0addb205d24 Mon Sep 17 00:00:00 2001 From: Subv Date: Sat, 20 Dec 2014 23:33:33 -0500 Subject: [PATCH 10/20] Style: Addressed some comments --- src/core/file_sys/archive_systemsavedata.cpp | 9 +++++++-- src/core/hle/service/cfg_u.cpp | 9 +++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/core/file_sys/archive_systemsavedata.cpp b/src/core/file_sys/archive_systemsavedata.cpp index b942864b29..0da32d510c 100644 --- a/src/core/file_sys/archive_systemsavedata.cpp +++ b/src/core/file_sys/archive_systemsavedata.cpp @@ -16,9 +16,14 @@ namespace FileSys { +static std::string GetSystemSaveDataPath(const std::string& mount_point, u64 save_id) { + u32 save_high = static_cast((save_id >> 32) & 0xFFFFFFFF); + u32 save_low = static_cast(save_id & 0xFFFFFFFF); + return Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), save_low, save_high); +} + Archive_SystemSaveData::Archive_SystemSaveData(const std::string& mount_point, u64 save_id) - : DiskArchive(Common::StringFromFormat("%s%08X/%08X/", mount_point.c_str(), - static_cast(save_id & 0xFFFFFFFF), static_cast((save_id >> 32) & 0xFFFFFFFF))) { + : DiskArchive(GetSystemSaveDataPath(mount_point, save_id)) { LOG_INFO(Service_FS, "Directory %s set as SystemSaveData.", this->mount_point.c_str()); } diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index 5d21fcae8e..ca70e48b60 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -59,9 +59,9 @@ static std::array cfg_config_file_buffer = { }; /// TODO(Subv): Find out what this actually is /// Thanks Normmatt for providing this information -static const u8 STEREO_CAMERA_SETTINGS[32] = { - 0x00, 0x00, 0x78, 0x42, 0x00, 0x80, 0x90, 0x43, 0x9A, 0x99, 0x99, 0x42, 0xEC, 0x51, 0x38, 0x42, - 0x00, 0x00, 0x20, 0x41, 0x00, 0x00, 0xA0, 0x40, 0xEC, 0x51, 0x5E, 0x42, 0x5C, 0x8F, 0xAC, 0x41 +static const std::array STEREO_CAMERA_SETTINGS = { + 62.0f, 289.0f, 76.80000305175781f, 46.08000183105469f, + 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f }; // TODO(Link Mauve): use a constexpr once MSVC starts supporting it. @@ -293,7 +293,8 @@ ResultCode FormatConfig() { SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); config->data_entries_offset = 0x455C; // Insert the default blocks - res = CreateConfigInfoBlk(0x00050005, 0x20, 0xE, STEREO_CAMERA_SETTINGS); + res = CreateConfigInfoBlk(0x00050005, 0x20, 0xE, + reinterpret_cast(STEREO_CAMERA_SETTINGS.data())); if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x00090001, 0x8, 0xE, reinterpret_cast(&CONSOLE_UNIQUE_ID)); From 718a1207549f3701995dfa4e36683321035e0f23 Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 09:29:52 -0500 Subject: [PATCH 11/20] CFGU: Addressed some comments. --- src/core/hle/service/cfg_u.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index ca70e48b60..9701e6aed1 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -57,7 +57,7 @@ static const u8 SOUND_OUTPUT_MODE = 2; static const u32 CONFIG_SAVEFILE_SIZE = 0x8000; static std::array cfg_config_file_buffer = { }; -/// TODO(Subv): Find out what this actually is +/// TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games /// Thanks Normmatt for providing this information static const std::array STEREO_CAMERA_SETTINGS = { 62.0f, 289.0f, 76.80000305175781f, 46.08000183105469f, @@ -67,8 +67,9 @@ static const std::array STEREO_CAMERA_SETTINGS = { // TODO(Link Mauve): use a constexpr once MSVC starts supporting it. #define C(code) ((code)[0] | ((code)[1] << 8)) +static const u8 UNITED_STATES_COUNTRY_ID = 49; /// TODO(Subv): Find what the other bytes are -static const std::array COUNTRY_INFO = { 0, 0, 0, C("US") }; +static const std::array COUNTRY_INFO = { 0, 0, 0, UNITED_STATES_COUNTRY_ID }; static const std::array country_codes = { 0, C("JP"), 0, 0, 0, 0, 0, 0, // 0-7 @@ -224,16 +225,16 @@ ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, u8 const* data s32 total_entries = config->total_entries - 1; u32 offset = config->data_entries_offset; // Perform a search to locate the next offset for the new data + // use the offset and size of the previous block to determine the new position while (total_entries >= 0) { // Ignore the blocks that don't have a separate data offset - if (config->block_entries[total_entries].size <= 4) { - --total_entries; - continue; + if (config->block_entries[total_entries].size > 4) { + offset = config->block_entries[total_entries].offset_or_data + + config->block_entries[total_entries].size; + break; } - offset = config->block_entries[total_entries].offset_or_data + - config->block_entries[total_entries].size; - break; + --total_entries; } config->block_entries[config->total_entries].offset_or_data = offset; @@ -424,11 +425,12 @@ Interface::Interface() { } // Initialize the Username block - // TODO(Subv): Do this somewhere else, or in another way + // TODO(Subv): Initialize this directly in the variable when MSVC supports char16_t string literals CONSOLE_USERNAME_BLOCK.ng_word = 0; CONSOLE_USERNAME_BLOCK.zero = 0; - std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + - Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14), + // Copy string to buffer and pad with zeros at the end + auto itr = Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14); + std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + itr, std::end(CONSOLE_USERNAME_BLOCK.username), 0); FormatConfig(); } From cdd78fa01da30a9de117ca8163f572f0dc8ffc8f Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 12:03:41 -0500 Subject: [PATCH 12/20] CFGU: Addressed some issues. --- src/core/hle/service/cfg_u.cpp | 98 +++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 43 deletions(-) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index 9701e6aed1..1a128a0aa4 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -16,25 +16,25 @@ namespace CFG_U { enum SystemModel { - NINTENDO_3DS, - NINTENDO_3DS_XL, - NEW_NINTENDO_3DS, - NINTENDO_2DS, - NEW_NINTENDO_3DS_XL + NINTENDO_3DS = 0, + NINTENDO_3DS_XL = 1, + NEW_NINTENDO_3DS = 2, + NINTENDO_2DS = 3, + NEW_NINTENDO_3DS_XL = 4 }; enum SystemLanguage { - LANGUAGE_JP, - LANGUAGE_EN, - LANGUAGE_FR, - LANGUAGE_DE, - LANGUAGE_IT, - LANGUAGE_ES, - LANGUAGE_ZH, - LANGUAGE_KO, - LANGUAGE_NL, - LANGUAGE_PT, - LANGUAGE_RU + LANGUAGE_JP = 0, + LANGUAGE_EN = 1, + LANGUAGE_FR = 2, + LANGUAGE_DE = 3, + LANGUAGE_IT = 4, + LANGUAGE_ES = 5, + LANGUAGE_ZH = 6, + LANGUAGE_KO = 7, + LANGUAGE_NL = 8, + LANGUAGE_PT = 9, + LANGUAGE_RU = 10 }; struct UsernameBlock { @@ -57,8 +57,11 @@ static const u8 SOUND_OUTPUT_MODE = 2; static const u32 CONFIG_SAVEFILE_SIZE = 0x8000; static std::array cfg_config_file_buffer = { }; -/// TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games -/// Thanks Normmatt for providing this information +/** + * TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games, + * for example Nintendo Zone + * Thanks Normmatt for providing this information + */ static const std::array STEREO_CAMERA_SETTINGS = { 62.0f, 289.0f, 76.80000305175781f, 46.08000183105469f, 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f @@ -158,19 +161,24 @@ static void GetCountryCodeID(Service::Interface* self) { /// Block header in the config savedata file struct SaveConfigBlockEntry { - u32 block_id; - u32 offset_or_data; - u16 size; - u16 flags; + u32 block_id; ///< The id of the current block + u32 offset_or_data; ///< This is the absolute offset to the block data if the size is greater than 4 bytes, otherwise it contains the data itself + u16 size; ///< The size of the block + u16 flags; ///< The flags of the block, possibly used for access control }; -/// The header of the config savedata file, -/// contains information about the blocks in the file +/// The maximum number of block entries that can exist in the config file +static const u32 CONFIG_FILE_MAX_BLOCK_ENTRIES = 1479; + +/** + * The header of the config savedata file, + * contains information about the blocks in the file + */ struct SaveFileConfig { - u16 total_entries; - u16 data_entries_offset; - SaveConfigBlockEntry block_entries[1479]; - u32 unknown; + u16 total_entries; ///< The total number of set entries in the config file + u16 data_entries_offset; ///< The offset where the data for the blocks start, this is hardcoded to 0x455C as per hardware + SaveConfigBlockEntry block_entries[CONFIG_FILE_MAX_BLOCK_ENTRIES]; ///< The block headers, the maximum possible value is 1479 as per hardware + u32 unknown; ///< This field is unknown, possibly padding, 0 has been observed in hardware }; /** @@ -189,7 +197,7 @@ ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); auto itr = std::find_if(std::begin(config->block_entries), std::end(config->block_entries), - [&](SaveConfigBlockEntry const& entry) { + [&](const SaveConfigBlockEntry& entry) { return entry.block_id == block_id && entry.size == size && (entry.flags & flag); }); @@ -217,8 +225,11 @@ ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { * @param data A pointer containing the data we will write to the new block * @returns ResultCode indicating the result of the operation, 0 on success */ -ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, u8 const* data) { +ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, const u8* data) { SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); + if (config->total_entries >= CONFIG_FILE_MAX_BLOCK_ENTRIES) + return ResultCode(-1); // TODO(Subv): Find the right error code + // Insert the block header with offset 0 for now config->block_entries[config->total_entries] = { block_id, 0, size, flags }; if (size > 4) { @@ -268,13 +279,12 @@ ResultCode DeleteConfigNANDSaveFile() { * @returns ResultCode indicating the result of the operation, 0 on success */ ResultCode UpdateConfigNANDSavegame() { - FileSys::Mode mode; - mode.hex = 0; + FileSys::Mode mode = {}; mode.write_flag = 1; mode.create_flag = 1; FileSys::Path path("config"); auto file = cfg_system_save_data->OpenFile(path, mode); - _dbg_assert_msg_(Service_CFG, file != nullptr, "could not open file"); + _assert_msg_(Service_CFG, file != nullptr, "could not open file"); file->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data()); return RESULT_SUCCESS; } @@ -292,16 +302,17 @@ ResultCode FormatConfig() { std::fill(cfg_config_file_buffer.begin(), cfg_config_file_buffer.end(), 0); // Create the header SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); + // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value config->data_entries_offset = 0x455C; // Insert the default blocks res = CreateConfigInfoBlk(0x00050005, 0x20, 0xE, - reinterpret_cast(STEREO_CAMERA_SETTINGS.data())); + reinterpret_cast(STEREO_CAMERA_SETTINGS.data())); if (!res.IsSuccess()) return res; - res = CreateConfigInfoBlk(0x00090001, 0x8, 0xE, reinterpret_cast(&CONSOLE_UNIQUE_ID)); + res = CreateConfigInfoBlk(0x00090001, 0x8, 0xE, reinterpret_cast(&CONSOLE_UNIQUE_ID)); if (!res.IsSuccess()) return res; - res = CreateConfigInfoBlk(0x000F0004, 0x4, 0x8, reinterpret_cast(&CONSOLE_MODEL)); + res = CreateConfigInfoBlk(0x000F0004, 0x4, 0x8, reinterpret_cast(&CONSOLE_MODEL)); if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x000A0002, 0x1, 0xA, &CONSOLE_LANGUAGE); @@ -313,7 +324,7 @@ ResultCode FormatConfig() { res = CreateConfigInfoBlk(0x000B0000, 0x4, 0xE, COUNTRY_INFO.data()); if (!res.IsSuccess()) return res; - res = CreateConfigInfoBlk(0x000A0000, 0x1C, 0xE, reinterpret_cast(&CONSOLE_USERNAME_BLOCK)); + res = CreateConfigInfoBlk(0x000A0000, 0x1C, 0xE, reinterpret_cast(&CONSOLE_USERNAME_BLOCK)); if (!res.IsSuccess()) return res; // Save the buffer to the file @@ -357,8 +368,9 @@ static void GetSystemModel(Service::Interface* self) { u32* cmd_buffer = Kernel::GetCommandBuffer(); u32 data; + // TODO(Subv): Find out the correct error codes cmd_buffer[1] = GetConfigInfoBlock(0x000F0004, 4, 0x8, - reinterpret_cast(&data)).raw; // TODO(Subv): Find out the correct error codes + reinterpret_cast(&data)).raw; cmd_buffer[2] = data & 0xFF; } @@ -372,8 +384,9 @@ static void GetModelNintendo2DS(Service::Interface* self) { u32* cmd_buffer = Kernel::GetCommandBuffer(); u32 data; + // TODO(Subv): Find out the correct error codes cmd_buffer[1] = GetConfigInfoBlock(0x000F0004, 4, 0x8, - reinterpret_cast(&data)).raw; // TODO(Subv): Find out the correct error codes + reinterpret_cast(&data)).raw; u8 model = data & 0xFF; if (model == NINTENDO_2DS) @@ -412,8 +425,7 @@ Interface::Interface() { // TODO(Subv): All this code should be moved to cfg:i, // it's only here because we do not currently emulate the lower level code that uses that service // Try to open the file in read-only mode to check its existence - FileSys::Mode mode; - mode.hex = 0; + FileSys::Mode mode = {}; mode.read_flag = 1; FileSys::Path path("config"); auto file = cfg_system_save_data->OpenFile(path, mode); @@ -429,8 +441,8 @@ Interface::Interface() { CONSOLE_USERNAME_BLOCK.ng_word = 0; CONSOLE_USERNAME_BLOCK.zero = 0; // Copy string to buffer and pad with zeros at the end - auto itr = Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14); - std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + itr, + auto size = Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14); + std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + size, std::end(CONSOLE_USERNAME_BLOCK.username), 0); FormatConfig(); } From 9e45240e23a3eb43e24c4a6ffe9caf1953a6cad5 Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 15:57:06 -0500 Subject: [PATCH 13/20] CFGU: Some changes --- src/core/hle/service/cfg_u.cpp | 45 +++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp index 1a128a0aa4..b97181cbd0 100644 --- a/src/core/hle/service/cfg_u.cpp +++ b/src/core/hle/service/cfg_u.cpp @@ -44,16 +44,32 @@ struct UsernameBlock { }; static_assert(sizeof(UsernameBlock) == 0x1C, "Size of UsernameBlock must be 0x1C"); +struct ConsoleModelInfo { + u8 model; ///< The console model (3DS, 2DS, etc) + u8 unknown[3]; ///< Unknown data +}; +static_assert(sizeof(ConsoleModelInfo) == 4, "ConsoleModelInfo must be exactly 4 bytes"); + +struct ConsoleCountryInfo { + u8 unknown[3]; ///< Unknown data + u8 country_code; ///< The country code of the console +}; +static_assert(sizeof(ConsoleCountryInfo) == 4, "ConsoleCountryInfo must be exactly 4 bytes"); + static std::unique_ptr cfg_system_save_data; static const u64 CFG_SAVE_ID = 0x00010017; static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; -static const u32 CONSOLE_MODEL = NINTENDO_3DS_XL; +static const ConsoleModelInfo CONSOLE_MODEL = { NINTENDO_3DS_XL, 0, 0, 0 }; static const u8 CONSOLE_LANGUAGE = LANGUAGE_EN; static const char CONSOLE_USERNAME[0x14] = "CITRA"; /// This will be initialized in the Interface constructor, and will be used when creating the block static UsernameBlock CONSOLE_USERNAME_BLOCK; /// TODO(Subv): Find out what this actually is static const u8 SOUND_OUTPUT_MODE = 2; +static const u8 UNITED_STATES_COUNTRY_ID = 49; +/// TODO(Subv): Find what the other bytes are +static const ConsoleCountryInfo COUNTRY_INFO = { 0, 0, 0, UNITED_STATES_COUNTRY_ID }; + static const u32 CONFIG_SAVEFILE_SIZE = 0x8000; static std::array cfg_config_file_buffer = { }; @@ -67,13 +83,14 @@ static const std::array STEREO_CAMERA_SETTINGS = { 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f }; +static_assert(sizeof(STEREO_CAMERA_SETTINGS) == 0x20, "STEREO_CAMERA_SETTINGS must be exactly 0x20 bytes"); +static_assert(sizeof(CONSOLE_UNIQUE_ID) == 8, "CONSOLE_UNIQUE_ID must be exactly 8 bytes"); +static_assert(sizeof(CONSOLE_LANGUAGE) == 1, "CONSOLE_LANGUAGE must be exactly 1 byte"); +static_assert(sizeof(SOUND_OUTPUT_MODE) == 1, "SOUND_OUTPUT_MODE must be exactly 1 byte"); + // TODO(Link Mauve): use a constexpr once MSVC starts supporting it. #define C(code) ((code)[0] | ((code)[1] << 8)) -static const u8 UNITED_STATES_COUNTRY_ID = 49; -/// TODO(Subv): Find what the other bytes are -static const std::array COUNTRY_INFO = { 0, 0, 0, UNITED_STATES_COUNTRY_ID }; - static const std::array country_codes = { 0, C("JP"), 0, 0, 0, 0, 0, 0, // 0-7 C("AI"), C("AG"), C("AR"), C("AW"), C("BS"), C("BB"), C("BZ"), C("BO"), // 8-15 @@ -305,26 +322,30 @@ ResultCode FormatConfig() { // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value config->data_entries_offset = 0x455C; // Insert the default blocks - res = CreateConfigInfoBlk(0x00050005, 0x20, 0xE, + res = CreateConfigInfoBlk(0x00050005, sizeof(STEREO_CAMERA_SETTINGS), 0xE, reinterpret_cast(STEREO_CAMERA_SETTINGS.data())); if (!res.IsSuccess()) return res; - res = CreateConfigInfoBlk(0x00090001, 0x8, 0xE, reinterpret_cast(&CONSOLE_UNIQUE_ID)); + res = CreateConfigInfoBlk(0x00090001, sizeof(CONSOLE_UNIQUE_ID), 0xE, + reinterpret_cast(&CONSOLE_UNIQUE_ID)); if (!res.IsSuccess()) return res; - res = CreateConfigInfoBlk(0x000F0004, 0x4, 0x8, reinterpret_cast(&CONSOLE_MODEL)); + res = CreateConfigInfoBlk(0x000F0004, sizeof(CONSOLE_MODEL), 0x8, + reinterpret_cast(&CONSOLE_MODEL)); if (!res.IsSuccess()) return res; - res = CreateConfigInfoBlk(0x000A0002, 0x1, 0xA, &CONSOLE_LANGUAGE); + res = CreateConfigInfoBlk(0x000A0002, sizeof(CONSOLE_LANGUAGE), 0xA, &CONSOLE_LANGUAGE); if (!res.IsSuccess()) return res; - res = CreateConfigInfoBlk(0x00070001, 0x1, 0xE, &SOUND_OUTPUT_MODE); + res = CreateConfigInfoBlk(0x00070001, sizeof(SOUND_OUTPUT_MODE), 0xE, &SOUND_OUTPUT_MODE); if (!res.IsSuccess()) return res; - res = CreateConfigInfoBlk(0x000B0000, 0x4, 0xE, COUNTRY_INFO.data()); + res = CreateConfigInfoBlk(0x000B0000, sizeof(COUNTRY_INFO), 0xE, + reinterpret_cast(&COUNTRY_INFO)); if (!res.IsSuccess()) return res; - res = CreateConfigInfoBlk(0x000A0000, 0x1C, 0xE, reinterpret_cast(&CONSOLE_USERNAME_BLOCK)); + res = CreateConfigInfoBlk(0x000A0000, sizeof(CONSOLE_USERNAME_BLOCK), 0xE, + reinterpret_cast(&CONSOLE_USERNAME_BLOCK)); if (!res.IsSuccess()) return res; // Save the buffer to the file From 6115f013a9df6faf66c9799ab25ecb106182b8c2 Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 16:36:18 -0500 Subject: [PATCH 14/20] CFG: Create a new subfolder cfg inside service to handle cfg Moved most of the shared CFG code there, implemented a few CFG:I functions --- src/core/CMakeLists.txt | 10 +- src/core/file_sys/disk_archive.h | 1 + src/core/hle/hle.cpp | 3 + src/core/hle/service/cfg/cfg.cpp | 204 ++++++++++ src/core/hle/service/cfg/cfg.h | 144 +++++++ src/core/hle/service/{ => cfg}/cfg_i.cpp | 72 +++- src/core/hle/service/{ => cfg}/cfg_i.h | 0 src/core/hle/service/cfg/cfg_u.cpp | 194 ++++++++++ src/core/hle/service/{ => cfg}/cfg_u.h | 0 src/core/hle/service/cfg_u.cpp | 474 ----------------------- src/core/hle/service/service.cpp | 4 +- 11 files changed, 617 insertions(+), 489 deletions(-) create mode 100644 src/core/hle/service/cfg/cfg.cpp create mode 100644 src/core/hle/service/cfg/cfg.h rename src/core/hle/service/{ => cfg}/cfg_i.cpp (50%) rename src/core/hle/service/{ => cfg}/cfg_i.h (100%) create mode 100644 src/core/hle/service/cfg/cfg_u.cpp rename src/core/hle/service/{ => cfg}/cfg_u.h (100%) delete mode 100644 src/core/hle/service/cfg_u.cpp diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 3381524e3e..c00fc34931 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -37,8 +37,9 @@ set(SRCS hle/service/apt_u.cpp hle/service/boss_u.cpp hle/service/cecd_u.cpp - hle/service/cfg_i.cpp - hle/service/cfg_u.cpp + hle/service/cfg/cfg.cpp + hle/service/cfg/cfg_i.cpp + hle/service/cfg/cfg_u.cpp hle/service/csnd_snd.cpp hle/service/dsp_dsp.cpp hle/service/err_f.cpp @@ -122,8 +123,9 @@ set(HEADERS hle/service/apt_u.h hle/service/boss_u.h hle/service/cecd_u.h - hle/service/cfg_i.h - hle/service/cfg_u.h + hle/service/cfg/cfg.h + hle/service/cfg/cfg_i.h + hle/service/cfg/cfg_u.h hle/service/csnd_snd.h hle/service/dsp_dsp.h hle/service/err_f.h diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h index c1784e8705..6c9b689e08 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h @@ -5,6 +5,7 @@ #pragma once #include "common/common_types.h" +#include "common/file_util.h" #include "core/file_sys/archive_backend.h" #include "core/loader/loader.h" diff --git a/src/core/hle/hle.cpp b/src/core/hle/hle.cpp index 98f3bb9226..2d314a4cf1 100644 --- a/src/core/hle/hle.cpp +++ b/src/core/hle/hle.cpp @@ -9,6 +9,7 @@ #include "core/hle/kernel/thread.h" #include "core/hle/service/service.h" #include "core/hle/service/fs/archive.h" +#include "core/hle/service/cfg/cfg.h" //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -58,6 +59,7 @@ void RegisterAllModules() { void Init() { Service::Init(); Service::FS::ArchiveInit(); + Service::CFG::CFGInit(); RegisterAllModules(); @@ -65,6 +67,7 @@ void Init() { } void Shutdown() { + Service::CFG::CFGShutdown(); Service::FS::ArchiveShutdown(); Service::Shutdown(); diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp new file mode 100644 index 0000000000..d41e152855 --- /dev/null +++ b/src/core/hle/service/cfg/cfg.cpp @@ -0,0 +1,204 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/log.h" +#include "common/make_unique.h" +#include "core/file_sys/archive_systemsavedata.h" +#include "core/hle/service/cfg/cfg.h" + +namespace Service { +namespace CFG { + +const u64 CFG_SAVE_ID = 0x00010017; +const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; +const ConsoleModelInfo CONSOLE_MODEL = { NINTENDO_3DS_XL, 0, 0, 0 }; +const u8 CONSOLE_LANGUAGE = LANGUAGE_EN; +const char CONSOLE_USERNAME[0x14] = "CITRA"; +/// This will be initialized in CFGInit, and will be used when creating the block +UsernameBlock CONSOLE_USERNAME_BLOCK; +/// TODO(Subv): Find out what this actually is +const u8 SOUND_OUTPUT_MODE = 2; +const u8 UNITED_STATES_COUNTRY_ID = 49; +/// TODO(Subv): Find what the other bytes are +const ConsoleCountryInfo COUNTRY_INFO = { 0, 0, 0, UNITED_STATES_COUNTRY_ID }; + +/** + * TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games, + * for example Nintendo Zone + * Thanks Normmatt for providing this information + */ +const std::array STEREO_CAMERA_SETTINGS = { + 62.0f, 289.0f, 76.80000305175781f, 46.08000183105469f, + 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f +}; + +const u32 CONFIG_SAVEFILE_SIZE = 0x8000; +std::array cfg_config_file_buffer = {}; + +static std::unique_ptr cfg_system_save_data; + +ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { + // Read the header + SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); + + auto itr = std::find_if(std::begin(config->block_entries), std::end(config->block_entries), + [&](const SaveConfigBlockEntry& entry) { + return entry.block_id == block_id && entry.size == size && (entry.flags & flag); + }); + + if (itr == std::end(config->block_entries)) { + LOG_ERROR(Service_CFG, "Config block %u with size %u and flags %u not found", block_id, size, flag); + return ResultCode(-1); // TODO(Subv): Find the correct error code + } + + // The data is located in the block header itself if the size is less than 4 bytes + if (itr->size <= 4) + memcpy(output, &itr->offset_or_data, itr->size); + else + memcpy(output, &cfg_config_file_buffer[itr->offset_or_data], itr->size); + + return RESULT_SUCCESS; +} + +ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, const u8* data) { + SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); + if (config->total_entries >= CONFIG_FILE_MAX_BLOCK_ENTRIES) + return ResultCode(-1); // TODO(Subv): Find the right error code + + // Insert the block header with offset 0 for now + config->block_entries[config->total_entries] = { block_id, 0, size, flags }; + if (size > 4) { + s32 total_entries = config->total_entries - 1; + u32 offset = config->data_entries_offset; + // Perform a search to locate the next offset for the new data + // use the offset and size of the previous block to determine the new position + while (total_entries >= 0) { + // Ignore the blocks that don't have a separate data offset + if (config->block_entries[total_entries].size > 4) { + offset = config->block_entries[total_entries].offset_or_data + + config->block_entries[total_entries].size; + break; + } + + --total_entries; + } + + config->block_entries[config->total_entries].offset_or_data = offset; + + // Write the data at the new offset + memcpy(&cfg_config_file_buffer[offset], data, size); + } + else { + // The offset_or_data field in the header contains the data itself if it's 4 bytes or less + memcpy(&config->block_entries[config->total_entries].offset_or_data, data, size); + } + + ++config->total_entries; + return RESULT_SUCCESS; +} + +ResultCode DeleteConfigNANDSaveFile() { + FileSys::Path path("config"); + if (cfg_system_save_data->DeleteFile(path)) + return RESULT_SUCCESS; + return ResultCode(-1); // TODO(Subv): Find the right error code +} + +ResultCode UpdateConfigNANDSavegame() { + FileSys::Mode mode = {}; + mode.write_flag = 1; + mode.create_flag = 1; + FileSys::Path path("config"); + auto file = cfg_system_save_data->OpenFile(path, mode); + _assert_msg_(Service_CFG, file != nullptr, "could not open file"); + file->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data()); + return RESULT_SUCCESS; +} + +ResultCode FormatConfig() { + ResultCode res = DeleteConfigNANDSaveFile(); + if (!res.IsSuccess()) + return res; + // Delete the old data + std::fill(cfg_config_file_buffer.begin(), cfg_config_file_buffer.end(), 0); + // Create the header + SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); + // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value + config->data_entries_offset = 0x455C; + // Insert the default blocks + res = CreateConfigInfoBlk(0x00050005, sizeof(STEREO_CAMERA_SETTINGS), 0xE, + reinterpret_cast(STEREO_CAMERA_SETTINGS.data())); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x00090001, sizeof(CONSOLE_UNIQUE_ID), 0xE, + reinterpret_cast(&CONSOLE_UNIQUE_ID)); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x000F0004, sizeof(CONSOLE_MODEL), 0x8, + reinterpret_cast(&CONSOLE_MODEL)); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x000A0002, sizeof(CONSOLE_LANGUAGE), 0xA, &CONSOLE_LANGUAGE); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x00070001, sizeof(SOUND_OUTPUT_MODE), 0xE, &SOUND_OUTPUT_MODE); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x000B0000, sizeof(COUNTRY_INFO), 0xE, + reinterpret_cast(&COUNTRY_INFO)); + if (!res.IsSuccess()) + return res; + res = CreateConfigInfoBlk(0x000A0000, sizeof(CONSOLE_USERNAME_BLOCK), 0xE, + reinterpret_cast(&CONSOLE_USERNAME_BLOCK)); + if (!res.IsSuccess()) + return res; + // Save the buffer to the file + res = UpdateConfigNANDSavegame(); + if (!res.IsSuccess()) + return res; + return RESULT_SUCCESS; +} + +void CFGInit() { + // TODO(Subv): In the future we should use the FS service to query this archive, + // currently it is not possible because you can only have one open archive of the same type at any time + std::string syssavedata_directory = FileUtil::GetUserPath(D_SYSSAVEDATA_IDX); + cfg_system_save_data = Common::make_unique(syssavedata_directory, + CFG_SAVE_ID); + if (!cfg_system_save_data->Initialize()) { + LOG_CRITICAL(Service_CFG, "Could not initialize SystemSaveData archive for the CFG:U service"); + return; + } + + // TODO(Subv): All this code should be moved to cfg:i, + // it's only here because we do not currently emulate the lower level code that uses that service + // Try to open the file in read-only mode to check its existence + FileSys::Mode mode = {}; + mode.read_flag = 1; + FileSys::Path path("config"); + auto file = cfg_system_save_data->OpenFile(path, mode); + + // Load the config if it already exists + if (file != nullptr) { + file->Read(0, CONFIG_SAVEFILE_SIZE, cfg_config_file_buffer.data()); + return; + } + + // Initialize the Username block + // TODO(Subv): Initialize this directly in the variable when MSVC supports char16_t string literals + CONSOLE_USERNAME_BLOCK.ng_word = 0; + CONSOLE_USERNAME_BLOCK.zero = 0; + // Copy string to buffer and pad with zeros at the end + auto size = Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14); + std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + size, + std::end(CONSOLE_USERNAME_BLOCK.username), 0); + FormatConfig(); +} + +void CFGShutdown() { + +} + +} // namespace CFG +} // namespace Service diff --git a/src/core/hle/service/cfg/cfg.h b/src/core/hle/service/cfg/cfg.h new file mode 100644 index 0000000000..38db5a5ec9 --- /dev/null +++ b/src/core/hle/service/cfg/cfg.h @@ -0,0 +1,144 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include "core/hle/result.h" + +namespace Service { +namespace CFG { + +enum SystemModel { + NINTENDO_3DS = 0, + NINTENDO_3DS_XL = 1, + NEW_NINTENDO_3DS = 2, + NINTENDO_2DS = 3, + NEW_NINTENDO_3DS_XL = 4 +}; + +enum SystemLanguage { + LANGUAGE_JP = 0, + LANGUAGE_EN = 1, + LANGUAGE_FR = 2, + LANGUAGE_DE = 3, + LANGUAGE_IT = 4, + LANGUAGE_ES = 5, + LANGUAGE_ZH = 6, + LANGUAGE_KO = 7, + LANGUAGE_NL = 8, + LANGUAGE_PT = 9, + LANGUAGE_RU = 10 +}; + +/// Block header in the config savedata file +struct SaveConfigBlockEntry { + u32 block_id; ///< The id of the current block + u32 offset_or_data; ///< This is the absolute offset to the block data if the size is greater than 4 bytes, otherwise it contains the data itself + u16 size; ///< The size of the block + u16 flags; ///< The flags of the block, possibly used for access control +}; + +/// The maximum number of block entries that can exist in the config file +static const u32 CONFIG_FILE_MAX_BLOCK_ENTRIES = 1479; + +/** +* The header of the config savedata file, +* contains information about the blocks in the file +*/ +struct SaveFileConfig { + u16 total_entries; ///< The total number of set entries in the config file + u16 data_entries_offset; ///< The offset where the data for the blocks start, this is hardcoded to 0x455C as per hardware + SaveConfigBlockEntry block_entries[CONFIG_FILE_MAX_BLOCK_ENTRIES]; ///< The block headers, the maximum possible value is 1479 as per hardware + u32 unknown; ///< This field is unknown, possibly padding, 0 has been observed in hardware +}; +static_assert(sizeof(SaveFileConfig) == 0x455C, "The SaveFileConfig header must be exactly 0x455C bytes"); + +struct UsernameBlock { + char16_t username[10]; ///< Exactly 20 bytes long, padded with zeros at the end if necessary + u32 zero; + u32 ng_word; +}; +static_assert(sizeof(UsernameBlock) == 0x1C, "Size of UsernameBlock must be 0x1C"); + +struct ConsoleModelInfo { + u8 model; ///< The console model (3DS, 2DS, etc) + u8 unknown[3]; ///< Unknown data +}; +static_assert(sizeof(ConsoleModelInfo) == 4, "ConsoleModelInfo must be exactly 4 bytes"); + +struct ConsoleCountryInfo { + u8 unknown[3]; ///< Unknown data + u8 country_code; ///< The country code of the console +}; +static_assert(sizeof(ConsoleCountryInfo) == 4, "ConsoleCountryInfo must be exactly 4 bytes"); + +extern const u64 CFG_SAVE_ID; +extern const u64 CONSOLE_UNIQUE_ID; +extern const ConsoleModelInfo CONSOLE_MODEL; +extern const u8 CONSOLE_LANGUAGE; +extern const char CONSOLE_USERNAME[0x14]; +/// This will be initialized in the Interface constructor, and will be used when creating the block +extern UsernameBlock CONSOLE_USERNAME_BLOCK; +/// TODO(Subv): Find out what this actually is +extern const u8 SOUND_OUTPUT_MODE; +extern const u8 UNITED_STATES_COUNTRY_ID; +/// TODO(Subv): Find what the other bytes are +extern const ConsoleCountryInfo COUNTRY_INFO; +extern const std::array STEREO_CAMERA_SETTINGS; + +static_assert(sizeof(STEREO_CAMERA_SETTINGS) == 0x20, "STEREO_CAMERA_SETTINGS must be exactly 0x20 bytes"); +static_assert(sizeof(CONSOLE_UNIQUE_ID) == 8, "CONSOLE_UNIQUE_ID must be exactly 8 bytes"); +static_assert(sizeof(CONSOLE_LANGUAGE) == 1, "CONSOLE_LANGUAGE must be exactly 1 byte"); +static_assert(sizeof(SOUND_OUTPUT_MODE) == 1, "SOUND_OUTPUT_MODE must be exactly 1 byte"); + +/** + * Reads a block with the specified id and flag from the Config savegame buffer + * and writes the output to output. + * The input size must match exactly the size of the requested block + * @param block_id The id of the block we want to read + * @param size The size of the block we want to read + * @param flag The requested block must have this flag set + * @param output A pointer where we will write the read data + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output); + +/** + * Creates a block with the specified id and writes the input data to the cfg savegame buffer in memory. + * The config savegame file in the filesystem is not updated. + * @param block_id The id of the block we want to create + * @param size The size of the block we want to create + * @param flag The flags of the new block + * @param data A pointer containing the data we will write to the new block + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, const u8* data); + +/** + * Deletes the config savegame file from the filesystem, the buffer in memory is not affected + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode DeleteConfigNANDSaveFile(); + +/** + * Writes the config savegame memory buffer to the config savegame file in the filesystem + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode UpdateConfigNANDSavegame(); + +/** + * Re-creates the config savegame file in memory and the filesystem with the default blocks + * @returns ResultCode indicating the result of the operation, 0 on success + */ +ResultCode FormatConfig(); + +/// Initialize the config service +void CFGInit(); + +/// Shutdown the config service +void CFGShutdown(); + +} // namespace CFG +} // namespace Service diff --git a/src/core/hle/service/cfg_i.cpp b/src/core/hle/service/cfg/cfg_i.cpp similarity index 50% rename from src/core/hle/service/cfg_i.cpp rename to src/core/hle/service/cfg/cfg_i.cpp index e886b7c1f7..b3f28f8e56 100644 --- a/src/core/hle/service/cfg_i.cpp +++ b/src/core/hle/service/cfg/cfg_i.cpp @@ -1,32 +1,86 @@ // Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version +// Licensed under GPLv2 // Refer to the license.txt file included. #include "common/log.h" #include "core/hle/hle.h" -#include "core/hle/service/cfg_i.h" +#include "core/hle/service/cfg/cfg.h" +#include "core/hle/service/cfg/cfg_i.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Namespace CFG_I namespace CFG_I { + +/** + * CFG_I::GetConfigInfoBlk8 service function + * This function is called by two command headers, + * there appears to be no difference between them according to 3dbrew + * Inputs: + * 0 : 0x04010082 / 0x08010082 + * 1 : Size + * 2 : Block ID + * 3 : Descriptor for the output buffer + * 4 : Output buffer pointer + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + */ +static void GetConfigInfoBlk8(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + u32 size = cmd_buffer[1]; + u32 block_id = cmd_buffer[2]; + u8* data_pointer = Memory::GetPointer(cmd_buffer[4]); + + if (data_pointer == nullptr) { + cmd_buffer[1] = -1; // TODO(Subv): Find the right error code + return; + } + + cmd_buffer[1] = Service::CFG::GetConfigInfoBlock(block_id, size, 0x8, data_pointer).raw; +} + +/** + * CFG_I::UpdateConfigNANDSavegame service function + * This function is called by two command headers, + * there appears to be no difference between them according to 3dbrew + * Inputs: + * 0 : 0x04030000 / 0x08030000 + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + */ +static void UpdateConfigNANDSavegame(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + cmd_buffer[1] = Service::CFG::UpdateConfigNANDSavegame().raw; +} + +/** + * CFG_I::FormatConfig service function + * Inputs: + * 0 : 0x08060000 + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + */ +static void FormatConfig(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + cmd_buffer[1] = Service::CFG::FormatConfig().raw; +} const Interface::FunctionInfo FunctionTable[] = { - {0x04010082, nullptr, "GetConfigInfoBlk8"}, - {0x04020082, nullptr, "GetConfigInfoBlk4"}, - {0x04030000, nullptr, "UpdateConfigNANDSavegame"}, + {0x04010082, GetConfigInfoBlk8, "GetConfigInfoBlk8"}, + {0x04020082, nullptr, "SetConfigInfoBlk4"}, + {0x04030000, UpdateConfigNANDSavegame, "UpdateConfigNANDSavegame"}, {0x04040042, nullptr, "GetLocalFriendCodeSeedData"}, {0x04050000, nullptr, "GetLocalFriendCodeSeed"}, {0x04060000, nullptr, "SecureInfoGetRegion"}, {0x04070000, nullptr, "SecureInfoGetByte101"}, {0x04080042, nullptr, "SecureInfoGetSerialNo"}, {0x04090000, nullptr, "UpdateConfigBlk00040003"}, - {0x08010082, nullptr, "GetConfigInfoBlk8"}, - {0x08020082, nullptr, "GetConfigInfoBlk4"}, - {0x08030000, nullptr, "UpdateConfigNANDSavegame"}, + {0x08010082, GetConfigInfoBlk8, "GetConfigInfoBlk8"}, + {0x08020082, nullptr, "SetConfigInfoBlk4"}, + {0x08030000, UpdateConfigNANDSavegame, "UpdateConfigNANDSavegame"}, {0x080400C2, nullptr, "CreateConfigInfoBlk"}, {0x08050000, nullptr, "DeleteConfigNANDSavefile"}, - {0x08060000, nullptr, "FormatConfig"}, + {0x08060000, FormatConfig, "FormatConfig"}, {0x08070000, nullptr, "Unknown"}, {0x08080000, nullptr, "UpdateConfigBlk1"}, {0x08090000, nullptr, "UpdateConfigBlk2"}, diff --git a/src/core/hle/service/cfg_i.h b/src/core/hle/service/cfg/cfg_i.h similarity index 100% rename from src/core/hle/service/cfg_i.h rename to src/core/hle/service/cfg/cfg_i.h diff --git a/src/core/hle/service/cfg/cfg_u.cpp b/src/core/hle/service/cfg/cfg_u.cpp new file mode 100644 index 0000000000..439fcdbb97 --- /dev/null +++ b/src/core/hle/service/cfg/cfg_u.cpp @@ -0,0 +1,194 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/file_util.h" +#include "common/log.h" +#include "common/string_util.h" +#include "core/file_sys/archive_systemsavedata.h" +#include "core/hle/hle.h" +#include "core/hle/service/cfg/cfg.h" +#include "core/hle/service/cfg/cfg_u.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Namespace CFG_U + +namespace CFG_U { + +// TODO(Link Mauve): use a constexpr once MSVC starts supporting it. +#define C(code) ((code)[0] | ((code)[1] << 8)) + +static const std::array country_codes = { + 0, C("JP"), 0, 0, 0, 0, 0, 0, // 0-7 + C("AI"), C("AG"), C("AR"), C("AW"), C("BS"), C("BB"), C("BZ"), C("BO"), // 8-15 + C("BR"), C("VG"), C("CA"), C("KY"), C("CL"), C("CO"), C("CR"), C("DM"), // 16-23 + C("DO"), C("EC"), C("SV"), C("GF"), C("GD"), C("GP"), C("GT"), C("GY"), // 24-31 + C("HT"), C("HN"), C("JM"), C("MQ"), C("MX"), C("MS"), C("AN"), C("NI"), // 32-39 + C("PA"), C("PY"), C("PE"), C("KN"), C("LC"), C("VC"), C("SR"), C("TT"), // 40-47 + C("TC"), C("US"), C("UY"), C("VI"), C("VE"), 0, 0, 0, // 48-55 + 0, 0, 0, 0, 0, 0, 0, 0, // 56-63 + C("AL"), C("AU"), C("AT"), C("BE"), C("BA"), C("BW"), C("BG"), C("HR"), // 64-71 + C("CY"), C("CZ"), C("DK"), C("EE"), C("FI"), C("FR"), C("DE"), C("GR"), // 72-79 + C("HU"), C("IS"), C("IE"), C("IT"), C("LV"), C("LS"), C("LI"), C("LT"), // 80-87 + C("LU"), C("MK"), C("MT"), C("ME"), C("MZ"), C("NA"), C("NL"), C("NZ"), // 88-95 + C("NO"), C("PL"), C("PT"), C("RO"), C("RU"), C("RS"), C("SK"), C("SI"), // 96-103 + C("ZA"), C("ES"), C("SZ"), C("SE"), C("CH"), C("TR"), C("GB"), C("ZM"), // 104-111 + C("ZW"), C("AZ"), C("MR"), C("ML"), C("NE"), C("TD"), C("SD"), C("ER"), // 112-119 + C("DJ"), C("SO"), C("AD"), C("GI"), C("GG"), C("IM"), C("JE"), C("MC"), // 120-127 + C("TW"), 0, 0, 0, 0, 0, 0, 0, // 128-135 + C("KR"), 0, 0, 0, 0, 0, 0, 0, // 136-143 + C("HK"), C("MO"), 0, 0, 0, 0, 0, 0, // 144-151 + C("ID"), C("SG"), C("TH"), C("PH"), C("MY"), 0, 0, 0, // 152-159 + C("CN"), 0, 0, 0, 0, 0, 0, 0, // 160-167 + C("AE"), C("IN"), C("EG"), C("OM"), C("QA"), C("KW"), C("SA"), C("SY"), // 168-175 + C("BH"), C("JO"), 0, 0, 0, 0, 0, 0, // 176-183 + C("SM"), C("VA"), C("BM") // 184-186 +}; + +#undef C + +/** + * CFG_User::GetCountryCodeString service function + * Inputs: + * 1 : Country Code ID + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + * 2 : Country's 2-char string + */ +static void GetCountryCodeString(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + u32 country_code_id = cmd_buffer[1]; + + if (country_code_id >= country_codes.size() || 0 == country_codes[country_code_id]) { + LOG_ERROR(Service_CFG, "requested country code id=%d is invalid", country_code_id); + cmd_buffer[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw; + return; + } + + cmd_buffer[1] = 0; + cmd_buffer[2] = country_codes[country_code_id]; +} + +/** + * CFG_User::GetCountryCodeID service function + * Inputs: + * 1 : Country Code 2-char string + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + * 2 : Country Code ID + */ +static void GetCountryCodeID(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + u16 country_code = cmd_buffer[1]; + u16 country_code_id = 0; + + // The following algorithm will fail if the first country code isn't 0. + _dbg_assert_(Service_CFG, country_codes[0] == 0); + + for (size_t id = 0; id < country_codes.size(); ++id) { + if (country_codes[id] == country_code) { + country_code_id = id; + break; + } + } + + if (0 == country_code_id) { + LOG_ERROR(Service_CFG, "requested country code name=%c%c is invalid", country_code & 0xff, country_code >> 8); + cmd_buffer[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw; + cmd_buffer[2] = 0xFFFF; + return; + } + + cmd_buffer[1] = 0; + cmd_buffer[2] = country_code_id; +} + +/** + * CFG_User::GetConfigInfoBlk2 service function + * Inputs: + * 0 : 0x00010082 + * 1 : Size + * 2 : Block ID + * 3 : Descriptor for the output buffer + * 4 : Output buffer pointer + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + */ +static void GetConfigInfoBlk2(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + u32 size = cmd_buffer[1]; + u32 block_id = cmd_buffer[2]; + u8* data_pointer = Memory::GetPointer(cmd_buffer[4]); + + if (data_pointer == nullptr) { + cmd_buffer[1] = -1; // TODO(Subv): Find the right error code + return; + } + + cmd_buffer[1] = Service::CFG::GetConfigInfoBlock(block_id, size, 0x2, data_pointer).raw; +} + +/** + * CFG_User::GetSystemModel service function + * Inputs: + * 0 : 0x00050000 + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + * 2 : Model of the console + */ +static void GetSystemModel(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + u32 data; + + // TODO(Subv): Find out the correct error codes + cmd_buffer[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8, + reinterpret_cast(&data)).raw; + cmd_buffer[2] = data & 0xFF; +} + +/** + * CFG_User::GetModelNintendo2DS service function + * Inputs: + * 0 : 0x00060000 + * Outputs: + * 1 : Result of function, 0 on success, otherwise error code + * 2 : 0 if the system is a Nintendo 2DS, 1 otherwise + */ +static void GetModelNintendo2DS(Service::Interface* self) { + u32* cmd_buffer = Kernel::GetCommandBuffer(); + u32 data; + + // TODO(Subv): Find out the correct error codes + cmd_buffer[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8, + reinterpret_cast(&data)).raw; + + u8 model = data & 0xFF; + if (model == Service::CFG::NINTENDO_2DS) + cmd_buffer[2] = 0; + else + cmd_buffer[2] = 1; +} + +const Interface::FunctionInfo FunctionTable[] = { + {0x00010082, GetConfigInfoBlk2, "GetConfigInfoBlk2"}, + {0x00020000, nullptr, "SecureInfoGetRegion"}, + {0x00030000, nullptr, "GenHashConsoleUnique"}, + {0x00040000, nullptr, "GetRegionCanadaUSA"}, + {0x00050000, GetSystemModel, "GetSystemModel"}, + {0x00060000, GetModelNintendo2DS, "GetModelNintendo2DS"}, + {0x00070040, nullptr, "unknown"}, + {0x00080080, nullptr, "unknown"}, + {0x00090040, GetCountryCodeString, "GetCountryCodeString"}, + {0x000A0040, GetCountryCodeID, "GetCountryCodeID"}, +}; +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Interface class + +Interface::Interface() { + Register(FunctionTable, ARRAY_SIZE(FunctionTable)); +} + +Interface::~Interface() { +} + +} // namespace diff --git a/src/core/hle/service/cfg_u.h b/src/core/hle/service/cfg/cfg_u.h similarity index 100% rename from src/core/hle/service/cfg_u.h rename to src/core/hle/service/cfg/cfg_u.h diff --git a/src/core/hle/service/cfg_u.cpp b/src/core/hle/service/cfg_u.cpp deleted file mode 100644 index b97181cbd0..0000000000 --- a/src/core/hle/service/cfg_u.cpp +++ /dev/null @@ -1,474 +0,0 @@ -// Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "common/file_util.h" -#include "common/log.h" -#include "common/make_unique.h" -#include "common/string_util.h" -#include "core/file_sys/archive_systemsavedata.h" -#include "core/hle/hle.h" -#include "core/hle/service/cfg_u.h" - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// Namespace CFG_U - -namespace CFG_U { - -enum SystemModel { - NINTENDO_3DS = 0, - NINTENDO_3DS_XL = 1, - NEW_NINTENDO_3DS = 2, - NINTENDO_2DS = 3, - NEW_NINTENDO_3DS_XL = 4 -}; - -enum SystemLanguage { - LANGUAGE_JP = 0, - LANGUAGE_EN = 1, - LANGUAGE_FR = 2, - LANGUAGE_DE = 3, - LANGUAGE_IT = 4, - LANGUAGE_ES = 5, - LANGUAGE_ZH = 6, - LANGUAGE_KO = 7, - LANGUAGE_NL = 8, - LANGUAGE_PT = 9, - LANGUAGE_RU = 10 -}; - -struct UsernameBlock { - char16_t username[10]; ///< Exactly 20 bytes long, padded with zeros at the end if necessary - u32 zero; - u32 ng_word; -}; -static_assert(sizeof(UsernameBlock) == 0x1C, "Size of UsernameBlock must be 0x1C"); - -struct ConsoleModelInfo { - u8 model; ///< The console model (3DS, 2DS, etc) - u8 unknown[3]; ///< Unknown data -}; -static_assert(sizeof(ConsoleModelInfo) == 4, "ConsoleModelInfo must be exactly 4 bytes"); - -struct ConsoleCountryInfo { - u8 unknown[3]; ///< Unknown data - u8 country_code; ///< The country code of the console -}; -static_assert(sizeof(ConsoleCountryInfo) == 4, "ConsoleCountryInfo must be exactly 4 bytes"); - -static std::unique_ptr cfg_system_save_data; -static const u64 CFG_SAVE_ID = 0x00010017; -static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; -static const ConsoleModelInfo CONSOLE_MODEL = { NINTENDO_3DS_XL, 0, 0, 0 }; -static const u8 CONSOLE_LANGUAGE = LANGUAGE_EN; -static const char CONSOLE_USERNAME[0x14] = "CITRA"; -/// This will be initialized in the Interface constructor, and will be used when creating the block -static UsernameBlock CONSOLE_USERNAME_BLOCK; -/// TODO(Subv): Find out what this actually is -static const u8 SOUND_OUTPUT_MODE = 2; -static const u8 UNITED_STATES_COUNTRY_ID = 49; -/// TODO(Subv): Find what the other bytes are -static const ConsoleCountryInfo COUNTRY_INFO = { 0, 0, 0, UNITED_STATES_COUNTRY_ID }; - -static const u32 CONFIG_SAVEFILE_SIZE = 0x8000; -static std::array cfg_config_file_buffer = { }; - -/** - * TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games, - * for example Nintendo Zone - * Thanks Normmatt for providing this information - */ -static const std::array STEREO_CAMERA_SETTINGS = { - 62.0f, 289.0f, 76.80000305175781f, 46.08000183105469f, - 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f -}; - -static_assert(sizeof(STEREO_CAMERA_SETTINGS) == 0x20, "STEREO_CAMERA_SETTINGS must be exactly 0x20 bytes"); -static_assert(sizeof(CONSOLE_UNIQUE_ID) == 8, "CONSOLE_UNIQUE_ID must be exactly 8 bytes"); -static_assert(sizeof(CONSOLE_LANGUAGE) == 1, "CONSOLE_LANGUAGE must be exactly 1 byte"); -static_assert(sizeof(SOUND_OUTPUT_MODE) == 1, "SOUND_OUTPUT_MODE must be exactly 1 byte"); - -// TODO(Link Mauve): use a constexpr once MSVC starts supporting it. -#define C(code) ((code)[0] | ((code)[1] << 8)) - -static const std::array country_codes = { - 0, C("JP"), 0, 0, 0, 0, 0, 0, // 0-7 - C("AI"), C("AG"), C("AR"), C("AW"), C("BS"), C("BB"), C("BZ"), C("BO"), // 8-15 - C("BR"), C("VG"), C("CA"), C("KY"), C("CL"), C("CO"), C("CR"), C("DM"), // 16-23 - C("DO"), C("EC"), C("SV"), C("GF"), C("GD"), C("GP"), C("GT"), C("GY"), // 24-31 - C("HT"), C("HN"), C("JM"), C("MQ"), C("MX"), C("MS"), C("AN"), C("NI"), // 32-39 - C("PA"), C("PY"), C("PE"), C("KN"), C("LC"), C("VC"), C("SR"), C("TT"), // 40-47 - C("TC"), C("US"), C("UY"), C("VI"), C("VE"), 0, 0, 0, // 48-55 - 0, 0, 0, 0, 0, 0, 0, 0, // 56-63 - C("AL"), C("AU"), C("AT"), C("BE"), C("BA"), C("BW"), C("BG"), C("HR"), // 64-71 - C("CY"), C("CZ"), C("DK"), C("EE"), C("FI"), C("FR"), C("DE"), C("GR"), // 72-79 - C("HU"), C("IS"), C("IE"), C("IT"), C("LV"), C("LS"), C("LI"), C("LT"), // 80-87 - C("LU"), C("MK"), C("MT"), C("ME"), C("MZ"), C("NA"), C("NL"), C("NZ"), // 88-95 - C("NO"), C("PL"), C("PT"), C("RO"), C("RU"), C("RS"), C("SK"), C("SI"), // 96-103 - C("ZA"), C("ES"), C("SZ"), C("SE"), C("CH"), C("TR"), C("GB"), C("ZM"), // 104-111 - C("ZW"), C("AZ"), C("MR"), C("ML"), C("NE"), C("TD"), C("SD"), C("ER"), // 112-119 - C("DJ"), C("SO"), C("AD"), C("GI"), C("GG"), C("IM"), C("JE"), C("MC"), // 120-127 - C("TW"), 0, 0, 0, 0, 0, 0, 0, // 128-135 - C("KR"), 0, 0, 0, 0, 0, 0, 0, // 136-143 - C("HK"), C("MO"), 0, 0, 0, 0, 0, 0, // 144-151 - C("ID"), C("SG"), C("TH"), C("PH"), C("MY"), 0, 0, 0, // 152-159 - C("CN"), 0, 0, 0, 0, 0, 0, 0, // 160-167 - C("AE"), C("IN"), C("EG"), C("OM"), C("QA"), C("KW"), C("SA"), C("SY"), // 168-175 - C("BH"), C("JO"), 0, 0, 0, 0, 0, 0, // 176-183 - C("SM"), C("VA"), C("BM") // 184-186 -}; - -#undef C - -/** - * CFG_User::GetCountryCodeString service function - * Inputs: - * 1 : Country Code ID - * Outputs: - * 1 : Result of function, 0 on success, otherwise error code - * 2 : Country's 2-char string - */ -static void GetCountryCodeString(Service::Interface* self) { - u32* cmd_buffer = Kernel::GetCommandBuffer(); - u32 country_code_id = cmd_buffer[1]; - - if (country_code_id >= country_codes.size() || 0 == country_codes[country_code_id]) { - LOG_ERROR(Service_CFG, "requested country code id=%d is invalid", country_code_id); - cmd_buffer[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw; - return; - } - - cmd_buffer[1] = 0; - cmd_buffer[2] = country_codes[country_code_id]; -} - -/** - * CFG_User::GetCountryCodeID service function - * Inputs: - * 1 : Country Code 2-char string - * Outputs: - * 1 : Result of function, 0 on success, otherwise error code - * 2 : Country Code ID - */ -static void GetCountryCodeID(Service::Interface* self) { - u32* cmd_buffer = Kernel::GetCommandBuffer(); - u16 country_code = cmd_buffer[1]; - u16 country_code_id = 0; - - // The following algorithm will fail if the first country code isn't 0. - _dbg_assert_(Service_CFG, country_codes[0] == 0); - - for (size_t id = 0; id < country_codes.size(); ++id) { - if (country_codes[id] == country_code) { - country_code_id = id; - break; - } - } - - if (0 == country_code_id) { - LOG_ERROR(Service_CFG, "requested country code name=%c%c is invalid", country_code & 0xff, country_code >> 8); - cmd_buffer[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw; - cmd_buffer[2] = 0xFFFF; - return; - } - - cmd_buffer[1] = 0; - cmd_buffer[2] = country_code_id; -} - -/// Block header in the config savedata file -struct SaveConfigBlockEntry { - u32 block_id; ///< The id of the current block - u32 offset_or_data; ///< This is the absolute offset to the block data if the size is greater than 4 bytes, otherwise it contains the data itself - u16 size; ///< The size of the block - u16 flags; ///< The flags of the block, possibly used for access control -}; - -/// The maximum number of block entries that can exist in the config file -static const u32 CONFIG_FILE_MAX_BLOCK_ENTRIES = 1479; - -/** - * The header of the config savedata file, - * contains information about the blocks in the file - */ -struct SaveFileConfig { - u16 total_entries; ///< The total number of set entries in the config file - u16 data_entries_offset; ///< The offset where the data for the blocks start, this is hardcoded to 0x455C as per hardware - SaveConfigBlockEntry block_entries[CONFIG_FILE_MAX_BLOCK_ENTRIES]; ///< The block headers, the maximum possible value is 1479 as per hardware - u32 unknown; ///< This field is unknown, possibly padding, 0 has been observed in hardware -}; - -/** - * Reads a block with the specified id and flag from the Config savegame buffer - * and writes the output to output. - * The input size must match exactly the size of the requested block - * TODO(Subv): This should actually be in some file common to the CFG process - * @param block_id The id of the block we want to read - * @param size The size of the block we want to read - * @param flag The requested block must have this flag set - * @param output A pointer where we will write the read data - * @returns ResultCode indicating the result of the operation, 0 on success - */ -ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { - // Read the header - SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); - - auto itr = std::find_if(std::begin(config->block_entries), std::end(config->block_entries), - [&](const SaveConfigBlockEntry& entry) { - return entry.block_id == block_id && entry.size == size && (entry.flags & flag); - }); - - if (itr == std::end(config->block_entries)) { - LOG_ERROR(Service_CFG, "Config block %u with size %u and flags %u not found", block_id, size, flag); - return ResultCode(-1); // TODO(Subv): Find the correct error code - } - - // The data is located in the block header itself if the size is less than 4 bytes - if (itr->size <= 4) - memcpy(output, &itr->offset_or_data, itr->size); - else - memcpy(output, &cfg_config_file_buffer[itr->offset_or_data], itr->size); - - return RESULT_SUCCESS; -} - -/** - * Creates a block with the specified id and writes the input data to the cfg savegame buffer in memory. - * The config savegame file in the filesystem is not updated. - * TODO(Subv): This should actually be in some file common to the CFG process - * @param block_id The id of the block we want to create - * @param size The size of the block we want to create - * @param flag The flags of the new block - * @param data A pointer containing the data we will write to the new block - * @returns ResultCode indicating the result of the operation, 0 on success - */ -ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, const u8* data) { - SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); - if (config->total_entries >= CONFIG_FILE_MAX_BLOCK_ENTRIES) - return ResultCode(-1); // TODO(Subv): Find the right error code - - // Insert the block header with offset 0 for now - config->block_entries[config->total_entries] = { block_id, 0, size, flags }; - if (size > 4) { - s32 total_entries = config->total_entries - 1; - u32 offset = config->data_entries_offset; - // Perform a search to locate the next offset for the new data - // use the offset and size of the previous block to determine the new position - while (total_entries >= 0) { - // Ignore the blocks that don't have a separate data offset - if (config->block_entries[total_entries].size > 4) { - offset = config->block_entries[total_entries].offset_or_data + - config->block_entries[total_entries].size; - break; - } - - --total_entries; - } - - config->block_entries[config->total_entries].offset_or_data = offset; - - // Write the data at the new offset - memcpy(&cfg_config_file_buffer[offset], data, size); - } else { - // The offset_or_data field in the header contains the data itself if it's 4 bytes or less - memcpy(&config->block_entries[config->total_entries].offset_or_data, data, size); - } - - ++config->total_entries; - return RESULT_SUCCESS; -} - -/** - * Deletes the config savegame file from the filesystem, the buffer in memory is not affected - * TODO(Subv): This should actually be in some file common to the CFG process - * @returns ResultCode indicating the result of the operation, 0 on success - */ -ResultCode DeleteConfigNANDSaveFile() { - FileSys::Path path("config"); - if (cfg_system_save_data->DeleteFile(path)) - return RESULT_SUCCESS; - return ResultCode(-1); // TODO(Subv): Find the right error code -} - -/** - * Writes the config savegame memory buffer to the config savegame file in the filesystem - * TODO(Subv): This should actually be in some file common to the CFG process - * @returns ResultCode indicating the result of the operation, 0 on success - */ -ResultCode UpdateConfigNANDSavegame() { - FileSys::Mode mode = {}; - mode.write_flag = 1; - mode.create_flag = 1; - FileSys::Path path("config"); - auto file = cfg_system_save_data->OpenFile(path, mode); - _assert_msg_(Service_CFG, file != nullptr, "could not open file"); - file->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data()); - return RESULT_SUCCESS; -} - -/** - * Re-creates the config savegame file in memory and the filesystem with the default blocks - * TODO(Subv): This should actually be in some file common to the CFG process - * @returns ResultCode indicating the result of the operation, 0 on success - */ -ResultCode FormatConfig() { - ResultCode res = DeleteConfigNANDSaveFile(); - if (!res.IsSuccess()) - return res; - // Delete the old data - std::fill(cfg_config_file_buffer.begin(), cfg_config_file_buffer.end(), 0); - // Create the header - SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); - // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value - config->data_entries_offset = 0x455C; - // Insert the default blocks - res = CreateConfigInfoBlk(0x00050005, sizeof(STEREO_CAMERA_SETTINGS), 0xE, - reinterpret_cast(STEREO_CAMERA_SETTINGS.data())); - if (!res.IsSuccess()) - return res; - res = CreateConfigInfoBlk(0x00090001, sizeof(CONSOLE_UNIQUE_ID), 0xE, - reinterpret_cast(&CONSOLE_UNIQUE_ID)); - if (!res.IsSuccess()) - return res; - res = CreateConfigInfoBlk(0x000F0004, sizeof(CONSOLE_MODEL), 0x8, - reinterpret_cast(&CONSOLE_MODEL)); - if (!res.IsSuccess()) - return res; - res = CreateConfigInfoBlk(0x000A0002, sizeof(CONSOLE_LANGUAGE), 0xA, &CONSOLE_LANGUAGE); - if (!res.IsSuccess()) - return res; - res = CreateConfigInfoBlk(0x00070001, sizeof(SOUND_OUTPUT_MODE), 0xE, &SOUND_OUTPUT_MODE); - if (!res.IsSuccess()) - return res; - res = CreateConfigInfoBlk(0x000B0000, sizeof(COUNTRY_INFO), 0xE, - reinterpret_cast(&COUNTRY_INFO)); - if (!res.IsSuccess()) - return res; - res = CreateConfigInfoBlk(0x000A0000, sizeof(CONSOLE_USERNAME_BLOCK), 0xE, - reinterpret_cast(&CONSOLE_USERNAME_BLOCK)); - if (!res.IsSuccess()) - return res; - // Save the buffer to the file - res = UpdateConfigNANDSavegame(); - if (!res.IsSuccess()) - return res; - return RESULT_SUCCESS; -} - -/** - * CFG_User::GetConfigInfoBlk2 service function - * Inputs: - * 1 : Size - * 2 : Block ID - * 3 : Descriptor for the output buffer - * 4 : Output buffer pointer - * Outputs: - * 1 : Result of function, 0 on success, otherwise error code - */ -static void GetConfigInfoBlk2(Service::Interface* self) { - u32* cmd_buffer = Kernel::GetCommandBuffer(); - u32 size = cmd_buffer[1]; - u32 block_id = cmd_buffer[2]; - u8* data_pointer = Memory::GetPointer(cmd_buffer[4]); - - if (data_pointer == nullptr) { - cmd_buffer[1] = -1; // TODO(Subv): Find the right error code - return; - } - - cmd_buffer[1] = GetConfigInfoBlock(block_id, size, 0x2, data_pointer).raw; -} - -/** - * CFG_User::GetSystemModel service function - * Outputs: - * 1 : Result of function, 0 on success, otherwise error code - * 2 : Model of the console - */ -static void GetSystemModel(Service::Interface* self) { - u32* cmd_buffer = Kernel::GetCommandBuffer(); - u32 data; - - // TODO(Subv): Find out the correct error codes - cmd_buffer[1] = GetConfigInfoBlock(0x000F0004, 4, 0x8, - reinterpret_cast(&data)).raw; - cmd_buffer[2] = data & 0xFF; -} - -/** - * CFG_User::GetModelNintendo2DS service function - * Outputs: - * 1 : Result of function, 0 on success, otherwise error code - * 2 : 0 if the system is a Nintendo 2DS, 1 otherwise - */ -static void GetModelNintendo2DS(Service::Interface* self) { - u32* cmd_buffer = Kernel::GetCommandBuffer(); - u32 data; - - // TODO(Subv): Find out the correct error codes - cmd_buffer[1] = GetConfigInfoBlock(0x000F0004, 4, 0x8, - reinterpret_cast(&data)).raw; - - u8 model = data & 0xFF; - if (model == NINTENDO_2DS) - cmd_buffer[2] = 0; - else - cmd_buffer[2] = 1; -} - -const Interface::FunctionInfo FunctionTable[] = { - {0x00010082, GetConfigInfoBlk2, "GetConfigInfoBlk2"}, - {0x00020000, nullptr, "SecureInfoGetRegion"}, - {0x00030000, nullptr, "GenHashConsoleUnique"}, - {0x00040000, nullptr, "GetRegionCanadaUSA"}, - {0x00050000, GetSystemModel, "GetSystemModel"}, - {0x00060000, GetModelNintendo2DS, "GetModelNintendo2DS"}, - {0x00070040, nullptr, "unknown"}, - {0x00080080, nullptr, "unknown"}, - {0x00090040, GetCountryCodeString, "GetCountryCodeString"}, - {0x000A0040, GetCountryCodeID, "GetCountryCodeID"}, -}; -//////////////////////////////////////////////////////////////////////////////////////////////////// -// Interface class - -Interface::Interface() { - Register(FunctionTable, ARRAY_SIZE(FunctionTable)); - // TODO(Subv): In the future we should use the FS service to query this archive, - // currently it is not possible because you can only have one open archive of the same type at any time - std::string syssavedata_directory = FileUtil::GetUserPath(D_SYSSAVEDATA_IDX); - cfg_system_save_data = Common::make_unique(syssavedata_directory, - CFG_SAVE_ID); - if (!cfg_system_save_data->Initialize()) { - LOG_CRITICAL(Service_CFG, "Could not initialize SystemSaveData archive for the CFG:U service"); - return; - } - - // TODO(Subv): All this code should be moved to cfg:i, - // it's only here because we do not currently emulate the lower level code that uses that service - // Try to open the file in read-only mode to check its existence - FileSys::Mode mode = {}; - mode.read_flag = 1; - FileSys::Path path("config"); - auto file = cfg_system_save_data->OpenFile(path, mode); - - // Load the config if it already exists - if (file != nullptr) { - file->Read(0, CONFIG_SAVEFILE_SIZE, cfg_config_file_buffer.data()); - return; - } - - // Initialize the Username block - // TODO(Subv): Initialize this directly in the variable when MSVC supports char16_t string literals - CONSOLE_USERNAME_BLOCK.ng_word = 0; - CONSOLE_USERNAME_BLOCK.zero = 0; - // Copy string to buffer and pad with zeros at the end - auto size = Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14); - std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + size, - std::end(CONSOLE_USERNAME_BLOCK.username), 0); - FormatConfig(); -} - -Interface::~Interface() { -} - -} // namespace diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index ac3f908f89..664f914d6b 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -12,8 +12,8 @@ #include "core/hle/service/apt_u.h" #include "core/hle/service/boss_u.h" #include "core/hle/service/cecd_u.h" -#include "core/hle/service/cfg_i.h" -#include "core/hle/service/cfg_u.h" +#include "core/hle/service/cfg/cfg_i.h" +#include "core/hle/service/cfg/cfg_u.h" #include "core/hle/service/csnd_snd.h" #include "core/hle/service/dsp_dsp.h" #include "core/hle/service/err_f.h" From 3e94b9054c542d3d76e6db622f361a03de788410 Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 16:45:13 -0500 Subject: [PATCH 15/20] CFG: Corrected the licenses in cfg_i.cpp and cfg_u.cpp --- src/core/hle/service/cfg/cfg_i.cpp | 2 +- src/core/hle/service/cfg/cfg_u.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/hle/service/cfg/cfg_i.cpp b/src/core/hle/service/cfg/cfg_i.cpp index b3f28f8e56..3254cc10e2 100644 --- a/src/core/hle/service/cfg/cfg_i.cpp +++ b/src/core/hle/service/cfg/cfg_i.cpp @@ -1,5 +1,5 @@ // Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 +// Licensed under GPLv2 or any later version // Refer to the license.txt file included. #include "common/log.h" diff --git a/src/core/hle/service/cfg/cfg_u.cpp b/src/core/hle/service/cfg/cfg_u.cpp index 439fcdbb97..59934ea46b 100644 --- a/src/core/hle/service/cfg/cfg_u.cpp +++ b/src/core/hle/service/cfg/cfg_u.cpp @@ -1,5 +1,5 @@ // Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 +// Licensed under GPLv2 or any later version // Refer to the license.txt file included. #include "common/file_util.h" From b3cee192892e9ee770c42f203e5a14bca17d4ba0 Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 17:26:51 -0500 Subject: [PATCH 16/20] CFG: Changed the CreateConfigInfoBlk search loop --- src/core/hle/service/cfg/cfg.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp index d41e152855..2697f8036f 100644 --- a/src/core/hle/service/cfg/cfg.cpp +++ b/src/core/hle/service/cfg/cfg.cpp @@ -69,19 +69,16 @@ ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, const u8* data // Insert the block header with offset 0 for now config->block_entries[config->total_entries] = { block_id, 0, size, flags }; if (size > 4) { - s32 total_entries = config->total_entries - 1; u32 offset = config->data_entries_offset; // Perform a search to locate the next offset for the new data // use the offset and size of the previous block to determine the new position - while (total_entries >= 0) { + for (int i = config->total_entries - 1; i >= 0; --i) { // Ignore the blocks that don't have a separate data offset - if (config->block_entries[total_entries].size > 4) { - offset = config->block_entries[total_entries].offset_or_data + - config->block_entries[total_entries].size; + if (config->block_entries[i].size > 4) { + offset = config->block_entries[i].offset_or_data + + config->block_entries[i].size; break; } - - --total_entries; } config->block_entries[config->total_entries].offset_or_data = offset; From 6f304d3b00b4c877dd04b7b953302c147020f2e8 Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 17:41:35 -0500 Subject: [PATCH 17/20] CFG: Some indentation --- src/core/hle/service/cfg/cfg.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp index 2697f8036f..2d17496e43 100644 --- a/src/core/hle/service/cfg/cfg.cpp +++ b/src/core/hle/service/cfg/cfg.cpp @@ -43,8 +43,9 @@ ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); auto itr = std::find_if(std::begin(config->block_entries), std::end(config->block_entries), - [&](const SaveConfigBlockEntry& entry) { - return entry.block_id == block_id && entry.size == size && (entry.flags & flag); + [&](const SaveConfigBlockEntry& entry) { + return entry.block_id == block_id && entry.size == size && + (entry.flags & flag); }); if (itr == std::end(config->block_entries)) { @@ -76,7 +77,7 @@ ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, const u8* data // Ignore the blocks that don't have a separate data offset if (config->block_entries[i].size > 4) { offset = config->block_entries[i].offset_or_data + - config->block_entries[i].size; + config->block_entries[i].size; break; } } @@ -125,15 +126,15 @@ ResultCode FormatConfig() { config->data_entries_offset = 0x455C; // Insert the default blocks res = CreateConfigInfoBlk(0x00050005, sizeof(STEREO_CAMERA_SETTINGS), 0xE, - reinterpret_cast(STEREO_CAMERA_SETTINGS.data())); + reinterpret_cast(STEREO_CAMERA_SETTINGS.data())); if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x00090001, sizeof(CONSOLE_UNIQUE_ID), 0xE, - reinterpret_cast(&CONSOLE_UNIQUE_ID)); + reinterpret_cast(&CONSOLE_UNIQUE_ID)); if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x000F0004, sizeof(CONSOLE_MODEL), 0x8, - reinterpret_cast(&CONSOLE_MODEL)); + reinterpret_cast(&CONSOLE_MODEL)); if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x000A0002, sizeof(CONSOLE_LANGUAGE), 0xA, &CONSOLE_LANGUAGE); @@ -143,11 +144,11 @@ ResultCode FormatConfig() { if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x000B0000, sizeof(COUNTRY_INFO), 0xE, - reinterpret_cast(&COUNTRY_INFO)); + reinterpret_cast(&COUNTRY_INFO)); if (!res.IsSuccess()) return res; res = CreateConfigInfoBlk(0x000A0000, sizeof(CONSOLE_USERNAME_BLOCK), 0xE, - reinterpret_cast(&CONSOLE_USERNAME_BLOCK)); + reinterpret_cast(&CONSOLE_USERNAME_BLOCK)); if (!res.IsSuccess()) return res; // Save the buffer to the file @@ -160,9 +161,10 @@ ResultCode FormatConfig() { void CFGInit() { // TODO(Subv): In the future we should use the FS service to query this archive, // currently it is not possible because you can only have one open archive of the same type at any time + using Common::make_unique; std::string syssavedata_directory = FileUtil::GetUserPath(D_SYSSAVEDATA_IDX); - cfg_system_save_data = Common::make_unique(syssavedata_directory, - CFG_SAVE_ID); + cfg_system_save_data = make_unique( + syssavedata_directory, CFG_SAVE_ID); if (!cfg_system_save_data->Initialize()) { LOG_CRITICAL(Service_CFG, "Could not initialize SystemSaveData archive for the CFG:U service"); return; @@ -189,7 +191,7 @@ void CFGInit() { // Copy string to buffer and pad with zeros at the end auto size = Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14); std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + size, - std::end(CONSOLE_USERNAME_BLOCK.username), 0); + std::end(CONSOLE_USERNAME_BLOCK.username), 0); FormatConfig(); } From f080e3ccfa579879969803b4871a1afb6c3f937c Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 17:54:14 -0500 Subject: [PATCH 18/20] CFGU: Indentation --- src/core/hle/service/cfg/cfg.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp index 2d17496e43..034a2c0d62 100644 --- a/src/core/hle/service/cfg/cfg.cpp +++ b/src/core/hle/service/cfg/cfg.cpp @@ -43,10 +43,9 @@ ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); auto itr = std::find_if(std::begin(config->block_entries), std::end(config->block_entries), - [&](const SaveConfigBlockEntry& entry) { - return entry.block_id == block_id && entry.size == size && - (entry.flags & flag); - }); + [&](const SaveConfigBlockEntry& entry) { + return entry.block_id == block_id && entry.size == size && (entry.flags & flag); + }); if (itr == std::end(config->block_entries)) { LOG_ERROR(Service_CFG, "Config block %u with size %u and flags %u not found", block_id, size, flag); From 304735fb5236eea7b5f2594058a4de99ab26ec4a Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 18:02:27 -0500 Subject: [PATCH 19/20] CFG: More style changes --- src/core/hle/service/cfg/cfg.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp index 034a2c0d62..abc2480f3e 100644 --- a/src/core/hle/service/cfg/cfg.cpp +++ b/src/core/hle/service/cfg/cfg.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/log.h" #include "common/make_unique.h" #include "core/file_sys/archive_systemsavedata.h" @@ -33,8 +34,8 @@ const std::array STEREO_CAMERA_SETTINGS = { 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f }; -const u32 CONFIG_SAVEFILE_SIZE = 0x8000; -std::array cfg_config_file_buffer = {}; +static const u32 CONFIG_SAVEFILE_SIZE = 0x8000; +static std::array cfg_config_file_buffer; static std::unique_ptr cfg_system_save_data; @@ -118,7 +119,7 @@ ResultCode FormatConfig() { if (!res.IsSuccess()) return res; // Delete the old data - std::fill(cfg_config_file_buffer.begin(), cfg_config_file_buffer.end(), 0); + cfg_config_file_buffer.fill(0); // Create the header SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value @@ -160,9 +161,8 @@ ResultCode FormatConfig() { void CFGInit() { // TODO(Subv): In the future we should use the FS service to query this archive, // currently it is not possible because you can only have one open archive of the same type at any time - using Common::make_unique; std::string syssavedata_directory = FileUtil::GetUserPath(D_SYSSAVEDATA_IDX); - cfg_system_save_data = make_unique( + cfg_system_save_data = Common::make_unique( syssavedata_directory, CFG_SAVE_ID); if (!cfg_system_save_data->Initialize()) { LOG_CRITICAL(Service_CFG, "Could not initialize SystemSaveData archive for the CFG:U service"); From 2030f9d946dca2b45f343deea207ece15843341d Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 21 Dec 2014 18:25:49 -0500 Subject: [PATCH 20/20] CFG: Fixed some warnings and errors in Clang --- src/core/hle/service/cfg/cfg.cpp | 6 +++--- src/core/hle/service/cfg/cfg.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp index abc2480f3e..161aa8531e 100644 --- a/src/core/hle/service/cfg/cfg.cpp +++ b/src/core/hle/service/cfg/cfg.cpp @@ -13,7 +13,7 @@ namespace CFG { const u64 CFG_SAVE_ID = 0x00010017; const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE; -const ConsoleModelInfo CONSOLE_MODEL = { NINTENDO_3DS_XL, 0, 0, 0 }; +const ConsoleModelInfo CONSOLE_MODEL = { NINTENDO_3DS_XL, { 0, 0, 0 } }; const u8 CONSOLE_LANGUAGE = LANGUAGE_EN; const char CONSOLE_USERNAME[0x14] = "CITRA"; /// This will be initialized in CFGInit, and will be used when creating the block @@ -22,7 +22,7 @@ UsernameBlock CONSOLE_USERNAME_BLOCK; const u8 SOUND_OUTPUT_MODE = 2; const u8 UNITED_STATES_COUNTRY_ID = 49; /// TODO(Subv): Find what the other bytes are -const ConsoleCountryInfo COUNTRY_INFO = { 0, 0, 0, UNITED_STATES_COUNTRY_ID }; +const ConsoleCountryInfo COUNTRY_INFO = { { 0, 0, 0 }, UNITED_STATES_COUNTRY_ID }; /** * TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games, @@ -62,7 +62,7 @@ ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) { return RESULT_SUCCESS; } -ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, const u8* data) { +ResultCode CreateConfigInfoBlk(u32 block_id, u16 size, u16 flags, const u8* data) { SaveFileConfig* config = reinterpret_cast(cfg_config_file_buffer.data()); if (config->total_entries >= CONFIG_FILE_MAX_BLOCK_ENTRIES) return ResultCode(-1); // TODO(Subv): Find the right error code diff --git a/src/core/hle/service/cfg/cfg.h b/src/core/hle/service/cfg/cfg.h index 38db5a5ec9..c74527ca47 100644 --- a/src/core/hle/service/cfg/cfg.h +++ b/src/core/hle/service/cfg/cfg.h @@ -114,7 +114,7 @@ ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output); * @param data A pointer containing the data we will write to the new block * @returns ResultCode indicating the result of the operation, 0 on success */ -ResultCode CreateConfigInfoBlk(u32 block_id, u32 size, u32 flags, const u8* data); +ResultCode CreateConfigInfoBlk(u32 block_id, u16 size, u16 flags, const u8* data); /** * Deletes the config savegame file from the filesystem, the buffer in memory is not affected