cfg.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include "common/logging/log.h"
  6. #include "common/string_util.h"
  7. #include "common/file_util.h"
  8. #include "core/file_sys/archive_systemsavedata.h"
  9. #include "core/file_sys/file_backend.h"
  10. #include "core/settings.h"
  11. #include "core/hle/service/cfg/cfg.h"
  12. #include "core/hle/service/cfg/cfg_i.h"
  13. #include "core/hle/service/cfg/cfg_s.h"
  14. #include "core/hle/service/cfg/cfg_u.h"
  15. #include "core/hle/service/fs/archive.h"
  16. #include "core/hle/service/service.h"
  17. namespace Service {
  18. namespace CFG {
  19. const u64 CFG_SAVE_ID = 0x00010017;
  20. const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE;
  21. const ConsoleModelInfo CONSOLE_MODEL = { NINTENDO_3DS_XL, { 0, 0, 0 } };
  22. const u8 CONSOLE_LANGUAGE = LANGUAGE_EN;
  23. const char CONSOLE_USERNAME[0x14] = "CITRA";
  24. /// This will be initialized in Init, and will be used when creating the block
  25. UsernameBlock CONSOLE_USERNAME_BLOCK;
  26. /// TODO(Subv): Find out what this actually is
  27. const u8 SOUND_OUTPUT_MODE = 2;
  28. const u8 UNITED_STATES_COUNTRY_ID = 49;
  29. /// TODO(Subv): Find what the other bytes are
  30. const ConsoleCountryInfo COUNTRY_INFO = { { 0, 0, 0 }, UNITED_STATES_COUNTRY_ID };
  31. /**
  32. * TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games,
  33. * for example Nintendo Zone
  34. * Thanks Normmatt for providing this information
  35. */
  36. const std::array<float, 8> STEREO_CAMERA_SETTINGS = {
  37. 62.0f, 289.0f, 76.80000305175781f, 46.08000183105469f,
  38. 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f
  39. };
  40. static const u32 CONFIG_SAVEFILE_SIZE = 0x8000;
  41. static std::array<u8, CONFIG_SAVEFILE_SIZE> cfg_config_file_buffer;
  42. static Service::FS::ArchiveHandle cfg_system_save_data_archive;
  43. static const std::vector<u8> cfg_system_savedata_id = { 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x01, 0x00 };
  44. void GetCountryCodeString(Service::Interface* self) {
  45. u32* cmd_buff = Kernel::GetCommandBuffer();
  46. u32 country_code_id = cmd_buff[1];
  47. if (country_code_id >= country_codes.size() || 0 == country_codes[country_code_id]) {
  48. LOG_ERROR(Service_CFG, "requested country code id=%d is invalid", country_code_id);
  49. cmd_buff[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw;
  50. return;
  51. }
  52. cmd_buff[1] = 0;
  53. cmd_buff[2] = country_codes[country_code_id];
  54. }
  55. void GetCountryCodeID(Service::Interface* self) {
  56. u32* cmd_buff = Kernel::GetCommandBuffer();
  57. u16 country_code = cmd_buff[1];
  58. u16 country_code_id = 0;
  59. // The following algorithm will fail if the first country code isn't 0.
  60. DEBUG_ASSERT(country_codes[0] == 0);
  61. for (u16 id = 0; id < country_codes.size(); ++id) {
  62. if (country_codes[id] == country_code) {
  63. country_code_id = id;
  64. break;
  65. }
  66. }
  67. if (0 == country_code_id) {
  68. LOG_ERROR(Service_CFG, "requested country code name=%c%c is invalid", country_code & 0xff, country_code >> 8);
  69. cmd_buff[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw;
  70. cmd_buff[2] = 0xFFFF;
  71. return;
  72. }
  73. cmd_buff[1] = 0;
  74. cmd_buff[2] = country_code_id;
  75. }
  76. void SecureInfoGetRegion(Service::Interface* self) {
  77. u32* cmd_buff = Kernel::GetCommandBuffer();
  78. cmd_buff[1] = RESULT_SUCCESS.raw;
  79. cmd_buff[2] = Settings::values.region_value;
  80. }
  81. void GenHashConsoleUnique(Service::Interface* self) {
  82. u32* cmd_buff = Kernel::GetCommandBuffer();
  83. u32 app_id_salt = cmd_buff[1];
  84. cmd_buff[1] = RESULT_SUCCESS.raw;
  85. cmd_buff[2] = 0x33646D6F ^ (app_id_salt & 0xFFFFF); // 3dmoo hash
  86. cmd_buff[3] = 0x6F534841 ^ (app_id_salt & 0xFFFFF);
  87. LOG_WARNING(Service_CFG, "(STUBBED) called app_id_salt=0x%X", app_id_salt);
  88. }
  89. void GetRegionCanadaUSA(Service::Interface* self) {
  90. u32* cmd_buff = Kernel::GetCommandBuffer();
  91. cmd_buff[1] = RESULT_SUCCESS.raw;
  92. u8 canada_or_usa = 1;
  93. if (canada_or_usa == Settings::values.region_value) {
  94. cmd_buff[2] = 1;
  95. } else {
  96. cmd_buff[2] = 0;
  97. }
  98. }
  99. void GetSystemModel(Service::Interface* self) {
  100. u32* cmd_buff = Kernel::GetCommandBuffer();
  101. u32 data;
  102. // TODO(Subv): Find out the correct error codes
  103. cmd_buff[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8,
  104. reinterpret_cast<u8*>(&data)).raw;
  105. cmd_buff[2] = data & 0xFF;
  106. }
  107. void GetModelNintendo2DS(Service::Interface* self) {
  108. u32* cmd_buff = Kernel::GetCommandBuffer();
  109. u32 data;
  110. // TODO(Subv): Find out the correct error codes
  111. cmd_buff[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8,
  112. reinterpret_cast<u8*>(&data)).raw;
  113. u8 model = data & 0xFF;
  114. if (model == Service::CFG::NINTENDO_2DS)
  115. cmd_buff[2] = 0;
  116. else
  117. cmd_buff[2] = 1;
  118. }
  119. void GetConfigInfoBlk2(Service::Interface* self) {
  120. u32* cmd_buff = Kernel::GetCommandBuffer();
  121. u32 size = cmd_buff[1];
  122. u32 block_id = cmd_buff[2];
  123. u8* data_pointer = Memory::GetPointer(cmd_buff[4]);
  124. if (data_pointer == nullptr) {
  125. cmd_buff[1] = -1; // TODO(Subv): Find the right error code
  126. return;
  127. }
  128. cmd_buff[1] = Service::CFG::GetConfigInfoBlock(block_id, size, 0x2, data_pointer).raw;
  129. }
  130. void GetConfigInfoBlk8(Service::Interface* self) {
  131. u32* cmd_buff = Kernel::GetCommandBuffer();
  132. u32 size = cmd_buff[1];
  133. u32 block_id = cmd_buff[2];
  134. u8* data_pointer = Memory::GetPointer(cmd_buff[4]);
  135. if (data_pointer == nullptr) {
  136. cmd_buff[1] = -1; // TODO(Subv): Find the right error code
  137. return;
  138. }
  139. cmd_buff[1] = Service::CFG::GetConfigInfoBlock(block_id, size, 0x8, data_pointer).raw;
  140. }
  141. void UpdateConfigNANDSavegame(Service::Interface* self) {
  142. u32* cmd_buff = Kernel::GetCommandBuffer();
  143. cmd_buff[1] = Service::CFG::UpdateConfigNANDSavegame().raw;
  144. }
  145. void FormatConfig(Service::Interface* self) {
  146. u32* cmd_buff = Kernel::GetCommandBuffer();
  147. cmd_buff[1] = Service::CFG::FormatConfig().raw;
  148. }
  149. ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, u8* output) {
  150. // Read the header
  151. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  152. auto itr = std::find_if(std::begin(config->block_entries), std::end(config->block_entries),
  153. [&](const SaveConfigBlockEntry& entry) {
  154. return entry.block_id == block_id && (entry.flags & flag);
  155. });
  156. if (itr == std::end(config->block_entries)) {
  157. LOG_ERROR(Service_CFG, "Config block 0x%X with flags %u and size %u was not found", block_id, flag, size);
  158. return ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent);
  159. }
  160. if (itr->size != size) {
  161. LOG_ERROR(Service_CFG, "Invalid size %u for config block 0x%X with flags %u", size, block_id, flag);
  162. return ResultCode(ErrorDescription::InvalidSize, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent);
  163. }
  164. // The data is located in the block header itself if the size is less than 4 bytes
  165. if (itr->size <= 4)
  166. memcpy(output, &itr->offset_or_data, itr->size);
  167. else
  168. memcpy(output, &cfg_config_file_buffer[itr->offset_or_data], itr->size);
  169. return RESULT_SUCCESS;
  170. }
  171. ResultCode CreateConfigInfoBlk(u32 block_id, u16 size, u16 flags, const u8* data) {
  172. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  173. if (config->total_entries >= CONFIG_FILE_MAX_BLOCK_ENTRIES)
  174. return ResultCode(-1); // TODO(Subv): Find the right error code
  175. // Insert the block header with offset 0 for now
  176. config->block_entries[config->total_entries] = { block_id, 0, size, flags };
  177. if (size > 4) {
  178. u32 offset = config->data_entries_offset;
  179. // Perform a search to locate the next offset for the new data
  180. // use the offset and size of the previous block to determine the new position
  181. for (int i = config->total_entries - 1; i >= 0; --i) {
  182. // Ignore the blocks that don't have a separate data offset
  183. if (config->block_entries[i].size > 4) {
  184. offset = config->block_entries[i].offset_or_data +
  185. config->block_entries[i].size;
  186. break;
  187. }
  188. }
  189. config->block_entries[config->total_entries].offset_or_data = offset;
  190. // Write the data at the new offset
  191. memcpy(&cfg_config_file_buffer[offset], data, size);
  192. }
  193. else {
  194. // The offset_or_data field in the header contains the data itself if it's 4 bytes or less
  195. memcpy(&config->block_entries[config->total_entries].offset_or_data, data, size);
  196. }
  197. ++config->total_entries;
  198. return RESULT_SUCCESS;
  199. }
  200. ResultCode DeleteConfigNANDSaveFile() {
  201. FileSys::Path path("config");
  202. return Service::FS::DeleteFileFromArchive(cfg_system_save_data_archive, path);
  203. }
  204. ResultCode UpdateConfigNANDSavegame() {
  205. FileSys::Mode mode = {};
  206. mode.write_flag = 1;
  207. mode.create_flag = 1;
  208. FileSys::Path path("config");
  209. auto config_result = Service::FS::OpenFileFromArchive(cfg_system_save_data_archive, path, mode);
  210. ASSERT_MSG(config_result.Succeeded(), "could not open file");
  211. auto config = config_result.MoveFrom();
  212. config->backend->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data());
  213. return RESULT_SUCCESS;
  214. }
  215. ResultCode FormatConfig() {
  216. ResultCode res = DeleteConfigNANDSaveFile();
  217. if (!res.IsSuccess())
  218. return res;
  219. // Delete the old data
  220. cfg_config_file_buffer.fill(0);
  221. // Create the header
  222. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  223. // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value
  224. config->data_entries_offset = 0x455C;
  225. // Insert the default blocks
  226. res = CreateConfigInfoBlk(0x00050005, sizeof(STEREO_CAMERA_SETTINGS), 0xE,
  227. reinterpret_cast<const u8*>(STEREO_CAMERA_SETTINGS.data()));
  228. if (!res.IsSuccess())
  229. return res;
  230. res = CreateConfigInfoBlk(0x00090001, sizeof(CONSOLE_UNIQUE_ID), 0xE,
  231. reinterpret_cast<const u8*>(&CONSOLE_UNIQUE_ID));
  232. if (!res.IsSuccess())
  233. return res;
  234. res = CreateConfigInfoBlk(0x000F0004, sizeof(CONSOLE_MODEL), 0x8,
  235. reinterpret_cast<const u8*>(&CONSOLE_MODEL));
  236. if (!res.IsSuccess())
  237. return res;
  238. res = CreateConfigInfoBlk(0x000A0002, sizeof(CONSOLE_LANGUAGE), 0xA, &CONSOLE_LANGUAGE);
  239. if (!res.IsSuccess())
  240. return res;
  241. res = CreateConfigInfoBlk(0x00070001, sizeof(SOUND_OUTPUT_MODE), 0xE, &SOUND_OUTPUT_MODE);
  242. if (!res.IsSuccess())
  243. return res;
  244. res = CreateConfigInfoBlk(0x000B0000, sizeof(COUNTRY_INFO), 0xE,
  245. reinterpret_cast<const u8*>(&COUNTRY_INFO));
  246. if (!res.IsSuccess())
  247. return res;
  248. res = CreateConfigInfoBlk(0x000A0000, sizeof(CONSOLE_USERNAME_BLOCK), 0xE,
  249. reinterpret_cast<const u8*>(&CONSOLE_USERNAME_BLOCK));
  250. if (!res.IsSuccess())
  251. return res;
  252. // Save the buffer to the file
  253. res = UpdateConfigNANDSavegame();
  254. if (!res.IsSuccess())
  255. return res;
  256. return RESULT_SUCCESS;
  257. }
  258. void Init() {
  259. AddService(new CFG_I_Interface);
  260. AddService(new CFG_S_Interface);
  261. AddService(new CFG_U_Interface);
  262. // Open the SystemSaveData archive 0x00010017
  263. FileSys::Path archive_path(cfg_system_savedata_id);
  264. auto archive_result = Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SystemSaveData, archive_path);
  265. // If the archive didn't exist, create the files inside
  266. if (archive_result.Code().description == ErrorDescription::FS_NotFormatted) {
  267. // Format the archive to create the directories
  268. Service::FS::FormatArchive(Service::FS::ArchiveIdCode::SystemSaveData, archive_path);
  269. // Open it again to get a valid archive now that the folder exists
  270. archive_result = Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SystemSaveData, archive_path);
  271. }
  272. ASSERT_MSG(archive_result.Succeeded(), "Could not open the CFG SystemSaveData archive!");
  273. cfg_system_save_data_archive = *archive_result;
  274. FileSys::Path config_path("config");
  275. FileSys::Mode open_mode = {};
  276. open_mode.read_flag = 1;
  277. auto config_result = Service::FS::OpenFileFromArchive(*archive_result, config_path, open_mode);
  278. // Read the file if it already exists
  279. if (config_result.Succeeded()) {
  280. auto config = config_result.MoveFrom();
  281. config->backend->Read(0, CONFIG_SAVEFILE_SIZE, cfg_config_file_buffer.data());
  282. return;
  283. }
  284. // Initialize the Username block
  285. // TODO(Subv): Initialize this directly in the variable when MSVC supports char16_t string literals
  286. memset(&CONSOLE_USERNAME_BLOCK, 0, sizeof(CONSOLE_USERNAME_BLOCK));
  287. CONSOLE_USERNAME_BLOCK.ng_word = 0;
  288. CONSOLE_USERNAME_BLOCK.zero = 0;
  289. // Copy string to buffer and pad with zeros at the end
  290. auto size = Common::UTF8ToUTF16(CONSOLE_USERNAME).copy(CONSOLE_USERNAME_BLOCK.username, 0x14);
  291. std::fill(std::begin(CONSOLE_USERNAME_BLOCK.username) + size,
  292. std::end(CONSOLE_USERNAME_BLOCK.username), 0);
  293. FormatConfig();
  294. }
  295. void Shutdown() {
  296. }
  297. } // namespace CFG
  298. } // namespace Service