cfg.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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/file_util.h"
  6. #include "common/logging/log.h"
  7. #include "common/string_util.h"
  8. #include "common/swap.h"
  9. #include "core/file_sys/archive_systemsavedata.h"
  10. #include "core/file_sys/file_backend.h"
  11. #include "core/settings.h"
  12. #include "core/hle/result.h"
  13. #include "core/hle/service/cfg/cfg.h"
  14. #include "core/hle/service/cfg/cfg_i.h"
  15. #include "core/hle/service/cfg/cfg_s.h"
  16. #include "core/hle/service/cfg/cfg_u.h"
  17. #include "core/hle/service/fs/archive.h"
  18. #include "core/hle/service/service.h"
  19. namespace Service {
  20. namespace CFG {
  21. /// The maximum number of block entries that can exist in the config file
  22. static const u32 CONFIG_FILE_MAX_BLOCK_ENTRIES = 1479;
  23. namespace {
  24. /**
  25. * The header of the config savedata file,
  26. * contains information about the blocks in the file
  27. */
  28. struct SaveFileConfig {
  29. u16 total_entries; ///< The total number of set entries in the config file
  30. u16 data_entries_offset; ///< The offset where the data for the blocks start, this is hardcoded to 0x455C as per hardware
  31. SaveConfigBlockEntry block_entries[CONFIG_FILE_MAX_BLOCK_ENTRIES]; ///< The block headers, the maximum possible value is 1479 as per hardware
  32. u32 unknown; ///< This field is unknown, possibly padding, 0 has been observed in hardware
  33. };
  34. static_assert(sizeof(SaveFileConfig) == 0x455C, "SaveFileConfig header must be exactly 0x455C bytes");
  35. enum ConfigBlockID {
  36. StereoCameraSettingsBlockID = 0x00050005,
  37. SoundOutputModeBlockID = 0x00070001,
  38. ConsoleUniqueIDBlockID = 0x00090001,
  39. UsernameBlockID = 0x000A0000,
  40. BirthdayBlockID = 0x000A0001,
  41. LanguageBlockID = 0x000A0002,
  42. CountryInfoBlockID = 0x000B0000,
  43. CountryNameBlockID = 0x000B0001,
  44. StateNameBlockID = 0x000B0002,
  45. EULAVersionBlockID = 0x000D0000,
  46. ConsoleModelBlockID = 0x000F0004,
  47. };
  48. struct UsernameBlock {
  49. char16_t username[10]; ///< Exactly 20 bytes long, padded with zeros at the end if necessary
  50. u32 zero;
  51. u32 ng_word;
  52. };
  53. static_assert(sizeof(UsernameBlock) == 0x1C, "UsernameBlock must be exactly 0x1C bytes");
  54. struct BirthdayBlock {
  55. u8 month; ///< The month of the birthday
  56. u8 day; ///< The day of the birthday
  57. };
  58. static_assert(sizeof(BirthdayBlock) == 2, "BirthdayBlock must be exactly 2 bytes");
  59. struct ConsoleModelInfo {
  60. u8 model; ///< The console model (3DS, 2DS, etc)
  61. u8 unknown[3]; ///< Unknown data
  62. };
  63. static_assert(sizeof(ConsoleModelInfo) == 4, "ConsoleModelInfo must be exactly 4 bytes");
  64. struct ConsoleCountryInfo {
  65. u8 unknown[3]; ///< Unknown data
  66. u8 country_code; ///< The country code of the console
  67. };
  68. static_assert(sizeof(ConsoleCountryInfo) == 4, "ConsoleCountryInfo must be exactly 4 bytes");
  69. }
  70. static const u64 CFG_SAVE_ID = 0x00010017;
  71. static const u64 CONSOLE_UNIQUE_ID = 0xDEADC0DE;
  72. static const ConsoleModelInfo CONSOLE_MODEL = { NINTENDO_3DS_XL, { 0, 0, 0 } };
  73. static const u8 CONSOLE_LANGUAGE = LANGUAGE_EN;
  74. static const UsernameBlock CONSOLE_USERNAME_BLOCK = { u"CITRA", 0, 0 };
  75. static const BirthdayBlock PROFILE_BIRTHDAY = { 3, 25 }; // March 25th, 2014
  76. static const u8 SOUND_OUTPUT_MODE = SOUND_SURROUND;
  77. static const u8 UNITED_STATES_COUNTRY_ID = 49;
  78. /// TODO(Subv): Find what the other bytes are
  79. static const ConsoleCountryInfo COUNTRY_INFO = { { 0, 0, 0 }, UNITED_STATES_COUNTRY_ID };
  80. /**
  81. * TODO(Subv): Find out what this actually is, these values fix some NaN uniforms in some games,
  82. * for example Nintendo Zone
  83. * Thanks Normmatt for providing this information
  84. */
  85. static const std::array<float, 8> STEREO_CAMERA_SETTINGS = {{
  86. 62.0f, 289.0f, 76.80000305175781f, 46.08000183105469f,
  87. 10.0f, 5.0f, 55.58000183105469f, 21.56999969482422f
  88. }};
  89. static_assert(sizeof(STEREO_CAMERA_SETTINGS) == 0x20, "STEREO_CAMERA_SETTINGS must be exactly 0x20 bytes");
  90. static const u32 CONFIG_SAVEFILE_SIZE = 0x8000;
  91. static std::array<u8, CONFIG_SAVEFILE_SIZE> cfg_config_file_buffer;
  92. static Service::FS::ArchiveHandle cfg_system_save_data_archive;
  93. static const std::vector<u8> cfg_system_savedata_id = { 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x01, 0x00 };
  94. void GetCountryCodeString(Service::Interface* self) {
  95. u32* cmd_buff = Kernel::GetCommandBuffer();
  96. u32 country_code_id = cmd_buff[1];
  97. if (country_code_id >= country_codes.size() || 0 == country_codes[country_code_id]) {
  98. LOG_ERROR(Service_CFG, "requested country code id=%d is invalid", country_code_id);
  99. cmd_buff[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw;
  100. return;
  101. }
  102. cmd_buff[1] = 0;
  103. cmd_buff[2] = country_codes[country_code_id];
  104. }
  105. void GetCountryCodeID(Service::Interface* self) {
  106. u32* cmd_buff = Kernel::GetCommandBuffer();
  107. u16 country_code = cmd_buff[1];
  108. u16 country_code_id = 0;
  109. // The following algorithm will fail if the first country code isn't 0.
  110. DEBUG_ASSERT(country_codes[0] == 0);
  111. for (u16 id = 0; id < country_codes.size(); ++id) {
  112. if (country_codes[id] == country_code) {
  113. country_code_id = id;
  114. break;
  115. }
  116. }
  117. if (0 == country_code_id) {
  118. LOG_ERROR(Service_CFG, "requested country code name=%c%c is invalid", country_code & 0xff, country_code >> 8);
  119. cmd_buff[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw;
  120. cmd_buff[2] = 0xFFFF;
  121. return;
  122. }
  123. cmd_buff[1] = 0;
  124. cmd_buff[2] = country_code_id;
  125. }
  126. void SecureInfoGetRegion(Service::Interface* self) {
  127. u32* cmd_buff = Kernel::GetCommandBuffer();
  128. cmd_buff[1] = RESULT_SUCCESS.raw;
  129. cmd_buff[2] = Settings::values.region_value;
  130. }
  131. void GenHashConsoleUnique(Service::Interface* self) {
  132. u32* cmd_buff = Kernel::GetCommandBuffer();
  133. u32 app_id_salt = cmd_buff[1];
  134. cmd_buff[1] = RESULT_SUCCESS.raw;
  135. cmd_buff[2] = 0x33646D6F ^ (app_id_salt & 0xFFFFF); // 3dmoo hash
  136. cmd_buff[3] = 0x6F534841 ^ (app_id_salt & 0xFFFFF);
  137. LOG_WARNING(Service_CFG, "(STUBBED) called app_id_salt=0x%X", app_id_salt);
  138. }
  139. void GetRegionCanadaUSA(Service::Interface* self) {
  140. u32* cmd_buff = Kernel::GetCommandBuffer();
  141. cmd_buff[1] = RESULT_SUCCESS.raw;
  142. u8 canada_or_usa = 1;
  143. if (canada_or_usa == Settings::values.region_value) {
  144. cmd_buff[2] = 1;
  145. } else {
  146. cmd_buff[2] = 0;
  147. }
  148. }
  149. void GetSystemModel(Service::Interface* self) {
  150. u32* cmd_buff = Kernel::GetCommandBuffer();
  151. u32 data;
  152. // TODO(Subv): Find out the correct error codes
  153. cmd_buff[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8,
  154. reinterpret_cast<u8*>(&data)).raw;
  155. cmd_buff[2] = data & 0xFF;
  156. }
  157. void GetModelNintendo2DS(Service::Interface* self) {
  158. u32* cmd_buff = Kernel::GetCommandBuffer();
  159. u32 data;
  160. // TODO(Subv): Find out the correct error codes
  161. cmd_buff[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8,
  162. reinterpret_cast<u8*>(&data)).raw;
  163. u8 model = data & 0xFF;
  164. if (model == Service::CFG::NINTENDO_2DS)
  165. cmd_buff[2] = 0;
  166. else
  167. cmd_buff[2] = 1;
  168. }
  169. void GetConfigInfoBlk2(Service::Interface* self) {
  170. u32* cmd_buff = Kernel::GetCommandBuffer();
  171. u32 size = cmd_buff[1];
  172. u32 block_id = cmd_buff[2];
  173. VAddr data_pointer = cmd_buff[4];
  174. if (!Memory::IsValidVirtualAddress(data_pointer)) {
  175. cmd_buff[1] = -1; // TODO(Subv): Find the right error code
  176. return;
  177. }
  178. std::vector<u8> data(size);
  179. cmd_buff[1] = Service::CFG::GetConfigInfoBlock(block_id, size, 0x2, data.data()).raw;
  180. Memory::WriteBlock(data_pointer, data.data(), data.size());
  181. }
  182. void GetConfigInfoBlk8(Service::Interface* self) {
  183. u32* cmd_buff = Kernel::GetCommandBuffer();
  184. u32 size = cmd_buff[1];
  185. u32 block_id = cmd_buff[2];
  186. VAddr data_pointer = cmd_buff[4];
  187. if (!Memory::IsValidVirtualAddress(data_pointer)) {
  188. cmd_buff[1] = -1; // TODO(Subv): Find the right error code
  189. return;
  190. }
  191. std::vector<u8> data(size);
  192. cmd_buff[1] = Service::CFG::GetConfigInfoBlock(block_id, size, 0x8, data.data()).raw;
  193. Memory::WriteBlock(data_pointer, data.data(), data.size());
  194. }
  195. void SetConfigInfoBlk4(Service::Interface* self) {
  196. u32* cmd_buff = Kernel::GetCommandBuffer();
  197. u32 block_id = cmd_buff[1];
  198. u32 size = cmd_buff[2];
  199. VAddr data_pointer = cmd_buff[4];
  200. if (!Memory::IsValidVirtualAddress(data_pointer)) {
  201. cmd_buff[1] = -1; // TODO(Subv): Find the right error code
  202. return;
  203. }
  204. std::vector<u8> data(size);
  205. Memory::ReadBlock(data_pointer, data.data(), data.size());
  206. cmd_buff[1] = Service::CFG::SetConfigInfoBlock(block_id, size, 0x4, data.data()).raw;
  207. }
  208. void UpdateConfigNANDSavegame(Service::Interface* self) {
  209. u32* cmd_buff = Kernel::GetCommandBuffer();
  210. cmd_buff[1] = Service::CFG::UpdateConfigNANDSavegame().raw;
  211. }
  212. void FormatConfig(Service::Interface* self) {
  213. u32* cmd_buff = Kernel::GetCommandBuffer();
  214. cmd_buff[1] = Service::CFG::FormatConfig().raw;
  215. }
  216. static ResultVal<void*> GetConfigInfoBlockPointer(u32 block_id, u32 size, u32 flag) {
  217. // Read the header
  218. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  219. auto itr = std::find_if(std::begin(config->block_entries), std::end(config->block_entries),
  220. [&](const SaveConfigBlockEntry& entry) {
  221. return entry.block_id == block_id;
  222. });
  223. if (itr == std::end(config->block_entries)) {
  224. LOG_ERROR(Service_CFG, "Config block 0x%X with flags %u and size %u was not found", block_id, flag, size);
  225. return ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent);
  226. }
  227. if ((itr->flags & flag) == 0) {
  228. LOG_ERROR(Service_CFG, "Invalid flag %u for config block 0x%X with size %u", flag, block_id, size);
  229. return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent);
  230. }
  231. if (itr->size != size) {
  232. LOG_ERROR(Service_CFG, "Invalid size %u for config block 0x%X with flags %u", size, block_id, flag);
  233. return ResultCode(ErrorDescription::InvalidSize, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent);
  234. }
  235. void* pointer;
  236. // The data is located in the block header itself if the size is less than 4 bytes
  237. if (itr->size <= 4)
  238. pointer = &itr->offset_or_data;
  239. else
  240. pointer = &cfg_config_file_buffer[itr->offset_or_data];
  241. return MakeResult<void*>(pointer);
  242. }
  243. ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, void* output) {
  244. void* pointer;
  245. CASCADE_RESULT(pointer, GetConfigInfoBlockPointer(block_id, size, flag));
  246. memcpy(output, pointer, size);
  247. return RESULT_SUCCESS;
  248. }
  249. ResultCode SetConfigInfoBlock(u32 block_id, u32 size, u32 flag, const void* input) {
  250. void* pointer;
  251. CASCADE_RESULT(pointer, GetConfigInfoBlockPointer(block_id, size, flag));
  252. memcpy(pointer, input, size);
  253. return RESULT_SUCCESS;
  254. }
  255. ResultCode CreateConfigInfoBlk(u32 block_id, u16 size, u16 flags, const void* data) {
  256. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  257. if (config->total_entries >= CONFIG_FILE_MAX_BLOCK_ENTRIES)
  258. return ResultCode(-1); // TODO(Subv): Find the right error code
  259. // Insert the block header with offset 0 for now
  260. config->block_entries[config->total_entries] = { block_id, 0, size, flags };
  261. if (size > 4) {
  262. u32 offset = config->data_entries_offset;
  263. // Perform a search to locate the next offset for the new data
  264. // use the offset and size of the previous block to determine the new position
  265. for (int i = config->total_entries - 1; i >= 0; --i) {
  266. // Ignore the blocks that don't have a separate data offset
  267. if (config->block_entries[i].size > 4) {
  268. offset = config->block_entries[i].offset_or_data +
  269. config->block_entries[i].size;
  270. break;
  271. }
  272. }
  273. config->block_entries[config->total_entries].offset_or_data = offset;
  274. // Write the data at the new offset
  275. memcpy(&cfg_config_file_buffer[offset], data, size);
  276. }
  277. else {
  278. // The offset_or_data field in the header contains the data itself if it's 4 bytes or less
  279. memcpy(&config->block_entries[config->total_entries].offset_or_data, data, size);
  280. }
  281. ++config->total_entries;
  282. return RESULT_SUCCESS;
  283. }
  284. ResultCode DeleteConfigNANDSaveFile() {
  285. FileSys::Path path("config");
  286. return Service::FS::DeleteFileFromArchive(cfg_system_save_data_archive, path);
  287. }
  288. ResultCode UpdateConfigNANDSavegame() {
  289. FileSys::Mode mode = {};
  290. mode.write_flag.Assign(1);
  291. mode.create_flag.Assign(1);
  292. FileSys::Path path("config");
  293. auto config_result = Service::FS::OpenFileFromArchive(cfg_system_save_data_archive, path, mode);
  294. ASSERT_MSG(config_result.Succeeded(), "could not open file");
  295. auto config = config_result.MoveFrom();
  296. config->backend->Write(0, CONFIG_SAVEFILE_SIZE, 1, cfg_config_file_buffer.data());
  297. return RESULT_SUCCESS;
  298. }
  299. ResultCode FormatConfig() {
  300. ResultCode res = DeleteConfigNANDSaveFile();
  301. // The delete command fails if the file doesn't exist, so we have to check that too
  302. if (!res.IsSuccess() && res.description != ErrorDescription::FS_NotFound)
  303. return res;
  304. // Delete the old data
  305. cfg_config_file_buffer.fill(0);
  306. // Create the header
  307. SaveFileConfig* config = reinterpret_cast<SaveFileConfig*>(cfg_config_file_buffer.data());
  308. // This value is hardcoded, taken from 3dbrew, verified by hardware, it's always the same value
  309. config->data_entries_offset = 0x455C;
  310. // Insert the default blocks
  311. u8 zero_buffer[0xC0] = {};
  312. // 0x00030001 - Unknown
  313. res = CreateConfigInfoBlk(0x00030001, 0x8, 0xE, zero_buffer);
  314. if (!res.IsSuccess()) return res;
  315. res = CreateConfigInfoBlk(StereoCameraSettingsBlockID, sizeof(STEREO_CAMERA_SETTINGS), 0xE, STEREO_CAMERA_SETTINGS.data());
  316. if (!res.IsSuccess()) return res;
  317. res = CreateConfigInfoBlk(SoundOutputModeBlockID, sizeof(SOUND_OUTPUT_MODE), 0xE, &SOUND_OUTPUT_MODE);
  318. if (!res.IsSuccess()) return res;
  319. res = CreateConfigInfoBlk(ConsoleUniqueIDBlockID, sizeof(CONSOLE_UNIQUE_ID), 0xE, &CONSOLE_UNIQUE_ID);
  320. if (!res.IsSuccess()) return res;
  321. res = CreateConfigInfoBlk(UsernameBlockID, sizeof(CONSOLE_USERNAME_BLOCK), 0xE, &CONSOLE_USERNAME_BLOCK);
  322. if (!res.IsSuccess()) return res;
  323. res = CreateConfigInfoBlk(BirthdayBlockID, sizeof(PROFILE_BIRTHDAY), 0xE, &PROFILE_BIRTHDAY);
  324. if (!res.IsSuccess()) return res;
  325. res = CreateConfigInfoBlk(LanguageBlockID, sizeof(CONSOLE_LANGUAGE), 0xE, &CONSOLE_LANGUAGE);
  326. if (!res.IsSuccess()) return res;
  327. res = CreateConfigInfoBlk(CountryInfoBlockID, sizeof(COUNTRY_INFO), 0xE, &COUNTRY_INFO);
  328. if (!res.IsSuccess()) return res;
  329. u16_le country_name_buffer[16][0x40] = {};
  330. std::u16string region_name = Common::UTF8ToUTF16("Gensokyo");
  331. for (size_t i = 0; i < 16; ++i) {
  332. std::copy(region_name.cbegin(), region_name.cend(), country_name_buffer[i]);
  333. }
  334. // 0x000B0001 - Localized names for the profile Country
  335. res = CreateConfigInfoBlk(CountryNameBlockID, sizeof(country_name_buffer), 0xE, country_name_buffer);
  336. if (!res.IsSuccess()) return res;
  337. // 0x000B0002 - Localized names for the profile State/Province
  338. res = CreateConfigInfoBlk(StateNameBlockID, sizeof(country_name_buffer), 0xE, country_name_buffer);
  339. if (!res.IsSuccess()) return res;
  340. // 0x000B0003 - Unknown, related to country/address (zip code?)
  341. res = CreateConfigInfoBlk(0x000B0003, 0x4, 0xE, zero_buffer);
  342. if (!res.IsSuccess()) return res;
  343. // 0x000C0000 - Unknown
  344. res = CreateConfigInfoBlk(0x000C0000, 0xC0, 0xE, zero_buffer);
  345. if (!res.IsSuccess()) return res;
  346. // 0x000C0001 - Unknown
  347. res = CreateConfigInfoBlk(0x000C0001, 0x14, 0xE, zero_buffer);
  348. if (!res.IsSuccess()) return res;
  349. // 0x000D0000 - Accepted EULA version
  350. res = CreateConfigInfoBlk(EULAVersionBlockID, 0x4, 0xE, zero_buffer);
  351. if (!res.IsSuccess()) return res;
  352. res = CreateConfigInfoBlk(ConsoleModelBlockID, sizeof(CONSOLE_MODEL), 0xC, &CONSOLE_MODEL);
  353. if (!res.IsSuccess()) return res;
  354. // 0x00170000 - Unknown
  355. res = CreateConfigInfoBlk(0x00170000, 0x4, 0xE, zero_buffer);
  356. if (!res.IsSuccess()) return res;
  357. // Save the buffer to the file
  358. res = UpdateConfigNANDSavegame();
  359. if (!res.IsSuccess())
  360. return res;
  361. return RESULT_SUCCESS;
  362. }
  363. ResultCode LoadConfigNANDSaveFile() {
  364. // Open the SystemSaveData archive 0x00010017
  365. FileSys::Path archive_path(cfg_system_savedata_id);
  366. auto archive_result = Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SystemSaveData, archive_path);
  367. // If the archive didn't exist, create the files inside
  368. if (archive_result.Code().description == ErrorDescription::FS_NotFormatted) {
  369. // Format the archive to create the directories
  370. Service::FS::FormatArchive(Service::FS::ArchiveIdCode::SystemSaveData, FileSys::ArchiveFormatInfo(), archive_path);
  371. // Open it again to get a valid archive now that the folder exists
  372. archive_result = Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SystemSaveData, archive_path);
  373. }
  374. ASSERT_MSG(archive_result.Succeeded(), "Could not open the CFG SystemSaveData archive!");
  375. cfg_system_save_data_archive = *archive_result;
  376. FileSys::Path config_path("config");
  377. FileSys::Mode open_mode = {};
  378. open_mode.read_flag.Assign(1);
  379. auto config_result = Service::FS::OpenFileFromArchive(*archive_result, config_path, open_mode);
  380. // Read the file if it already exists
  381. if (config_result.Succeeded()) {
  382. auto config = config_result.MoveFrom();
  383. config->backend->Read(0, CONFIG_SAVEFILE_SIZE, cfg_config_file_buffer.data());
  384. return RESULT_SUCCESS;
  385. }
  386. return FormatConfig();
  387. }
  388. void Init() {
  389. AddService(new CFG_I_Interface);
  390. AddService(new CFG_S_Interface);
  391. AddService(new CFG_U_Interface);
  392. LoadConfigNANDSaveFile();
  393. }
  394. void Shutdown() {
  395. }
  396. void SetUsername(const std::u16string& name) {
  397. ASSERT(name.size() <= 10);
  398. UsernameBlock block{};
  399. name.copy(block.username, name.size());
  400. SetConfigInfoBlock(UsernameBlockID, sizeof(block), 4, &block);
  401. }
  402. std::u16string GetUsername() {
  403. UsernameBlock block;
  404. GetConfigInfoBlock(UsernameBlockID, sizeof(block), 8, &block);
  405. // the username string in the block isn't null-terminated,
  406. // so we need to find the end manually.
  407. std::u16string username(block.username, ARRAY_SIZE(block.username));
  408. const size_t pos = username.find(u'\0');
  409. if (pos != std::u16string::npos)
  410. username.erase(pos);
  411. return username;
  412. }
  413. void SetBirthday(u8 month, u8 day) {
  414. BirthdayBlock block = { month, day };
  415. SetConfigInfoBlock(BirthdayBlockID, sizeof(block), 4, &block);
  416. }
  417. std::tuple<u8, u8> GetBirthday() {
  418. BirthdayBlock block;
  419. GetConfigInfoBlock(BirthdayBlockID, sizeof(block), 8, &block);
  420. return std::make_tuple(block.month, block.day);
  421. }
  422. void SetSystemLanguage(SystemLanguage language) {
  423. u8 block = language;
  424. SetConfigInfoBlock(LanguageBlockID, sizeof(block), 4, &block);
  425. }
  426. SystemLanguage GetSystemLanguage() {
  427. u8 block;
  428. GetConfigInfoBlock(LanguageBlockID, sizeof(block), 8, &block);
  429. return static_cast<SystemLanguage>(block);
  430. }
  431. void SetSoundOutputMode(SoundOutputMode mode) {
  432. u8 block = mode;
  433. SetConfigInfoBlock(SoundOutputModeBlockID, sizeof(block), 4, &block);
  434. }
  435. SoundOutputMode GetSoundOutputMode() {
  436. u8 block;
  437. GetConfigInfoBlock(SoundOutputModeBlockID, sizeof(block), 8, &block);
  438. return static_cast<SoundOutputMode>(block);
  439. }
  440. } // namespace CFG
  441. } // namespace Service