cfg.h 12 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. #pragma once
  5. #include <array>
  6. #include <string>
  7. #include "common/common_types.h"
  8. union ResultCode;
  9. namespace Service {
  10. class Interface;
  11. namespace CFG {
  12. enum SystemModel {
  13. NINTENDO_3DS = 0,
  14. NINTENDO_3DS_XL = 1,
  15. NEW_NINTENDO_3DS = 2,
  16. NINTENDO_2DS = 3,
  17. NEW_NINTENDO_3DS_XL = 4
  18. };
  19. enum SystemLanguage {
  20. LANGUAGE_JP = 0,
  21. LANGUAGE_EN = 1,
  22. LANGUAGE_FR = 2,
  23. LANGUAGE_DE = 3,
  24. LANGUAGE_IT = 4,
  25. LANGUAGE_ES = 5,
  26. LANGUAGE_ZH = 6,
  27. LANGUAGE_KO = 7,
  28. LANGUAGE_NL = 8,
  29. LANGUAGE_PT = 9,
  30. LANGUAGE_RU = 10,
  31. LANGUAGE_TW = 11
  32. };
  33. enum SoundOutputMode { SOUND_MONO = 0, SOUND_STEREO = 1, SOUND_SURROUND = 2 };
  34. /// Block header in the config savedata file
  35. struct SaveConfigBlockEntry {
  36. u32 block_id; ///< The id of the current block
  37. u32 offset_or_data; ///< This is the absolute offset to the block data if the size is greater
  38. /// than 4 bytes, otherwise it contains the data itself
  39. u16 size; ///< The size of the block
  40. u16 flags; ///< The flags of the block, possibly used for access control
  41. };
  42. static constexpr u16 C(const char code[2]) {
  43. return code[0] | (code[1] << 8);
  44. }
  45. static const std::array<u16, 187> country_codes = {{
  46. 0, C("JP"), 0, 0, 0, 0, 0, 0, // 0-7
  47. C("AI"), C("AG"), C("AR"), C("AW"), C("BS"), C("BB"), C("BZ"), C("BO"), // 8-15
  48. C("BR"), C("VG"), C("CA"), C("KY"), C("CL"), C("CO"), C("CR"), C("DM"), // 16-23
  49. C("DO"), C("EC"), C("SV"), C("GF"), C("GD"), C("GP"), C("GT"), C("GY"), // 24-31
  50. C("HT"), C("HN"), C("JM"), C("MQ"), C("MX"), C("MS"), C("AN"), C("NI"), // 32-39
  51. C("PA"), C("PY"), C("PE"), C("KN"), C("LC"), C("VC"), C("SR"), C("TT"), // 40-47
  52. C("TC"), C("US"), C("UY"), C("VI"), C("VE"), 0, 0, 0, // 48-55
  53. 0, 0, 0, 0, 0, 0, 0, 0, // 56-63
  54. C("AL"), C("AU"), C("AT"), C("BE"), C("BA"), C("BW"), C("BG"), C("HR"), // 64-71
  55. C("CY"), C("CZ"), C("DK"), C("EE"), C("FI"), C("FR"), C("DE"), C("GR"), // 72-79
  56. C("HU"), C("IS"), C("IE"), C("IT"), C("LV"), C("LS"), C("LI"), C("LT"), // 80-87
  57. C("LU"), C("MK"), C("MT"), C("ME"), C("MZ"), C("NA"), C("NL"), C("NZ"), // 88-95
  58. C("NO"), C("PL"), C("PT"), C("RO"), C("RU"), C("RS"), C("SK"), C("SI"), // 96-103
  59. C("ZA"), C("ES"), C("SZ"), C("SE"), C("CH"), C("TR"), C("GB"), C("ZM"), // 104-111
  60. C("ZW"), C("AZ"), C("MR"), C("ML"), C("NE"), C("TD"), C("SD"), C("ER"), // 112-119
  61. C("DJ"), C("SO"), C("AD"), C("GI"), C("GG"), C("IM"), C("JE"), C("MC"), // 120-127
  62. C("TW"), 0, 0, 0, 0, 0, 0, 0, // 128-135
  63. C("KR"), 0, 0, 0, 0, 0, 0, 0, // 136-143
  64. C("HK"), C("MO"), 0, 0, 0, 0, 0, 0, // 144-151
  65. C("ID"), C("SG"), C("TH"), C("PH"), C("MY"), 0, 0, 0, // 152-159
  66. C("CN"), 0, 0, 0, 0, 0, 0, 0, // 160-167
  67. C("AE"), C("IN"), C("EG"), C("OM"), C("QA"), C("KW"), C("SA"), C("SY"), // 168-175
  68. C("BH"), C("JO"), 0, 0, 0, 0, 0, 0, // 176-183
  69. C("SM"), C("VA"), C("BM"), // 184-186
  70. }};
  71. /**
  72. * CFG::GetCountryCodeString service function
  73. * Inputs:
  74. * 1 : Country Code ID
  75. * Outputs:
  76. * 1 : Result of function, 0 on success, otherwise error code
  77. * 2 : Country's 2-char string
  78. */
  79. void GetCountryCodeString(Service::Interface* self);
  80. /**
  81. * CFG::GetCountryCodeID service function
  82. * Inputs:
  83. * 1 : Country Code 2-char string
  84. * Outputs:
  85. * 1 : Result of function, 0 on success, otherwise error code
  86. * 2 : Country Code ID
  87. */
  88. void GetCountryCodeID(Service::Interface* self);
  89. /**
  90. * CFG::SecureInfoGetRegion service function
  91. * Inputs:
  92. * 1 : None
  93. * Outputs:
  94. * 0 : Result Header code
  95. * 1 : Result of function, 0 on success, otherwise error code
  96. * 2 : Region value loaded from SecureInfo offset 0x100
  97. */
  98. void SecureInfoGetRegion(Service::Interface* self);
  99. /**
  100. * CFG::GenHashConsoleUnique service function
  101. * Inputs:
  102. * 1 : 20 bit application ID salt
  103. * Outputs:
  104. * 0 : Result Header code
  105. * 1 : Result of function, 0 on success, otherwise error code
  106. * 2 : Hash/"ID" lower word
  107. * 3 : Hash/"ID" upper word
  108. */
  109. void GenHashConsoleUnique(Service::Interface* self);
  110. /**
  111. * CFG::GetRegionCanadaUSA service function
  112. * Inputs:
  113. * 1 : None
  114. * Outputs:
  115. * 0 : Result Header code
  116. * 1 : Result of function, 0 on success, otherwise error code
  117. * 2 : 1 if the system is a Canada or USA model, 0 otherwise
  118. */
  119. void GetRegionCanadaUSA(Service::Interface* self);
  120. /**
  121. * CFG::GetSystemModel service function
  122. * Inputs:
  123. * 0 : 0x00050000
  124. * Outputs:
  125. * 1 : Result of function, 0 on success, otherwise error code
  126. * 2 : Model of the console
  127. */
  128. void GetSystemModel(Service::Interface* self);
  129. /**
  130. * CFG::GetModelNintendo2DS service function
  131. * Inputs:
  132. * 0 : 0x00060000
  133. * Outputs:
  134. * 1 : Result of function, 0 on success, otherwise error code
  135. * 2 : 0 if the system is a Nintendo 2DS, 1 otherwise
  136. */
  137. void GetModelNintendo2DS(Service::Interface* self);
  138. /**
  139. * CFG::GetConfigInfoBlk2 service function
  140. * Inputs:
  141. * 0 : 0x00010082
  142. * 1 : Size
  143. * 2 : Block ID
  144. * 3 : Descriptor for the output buffer
  145. * 4 : Output buffer pointer
  146. * Outputs:
  147. * 1 : Result of function, 0 on success, otherwise error code
  148. */
  149. void GetConfigInfoBlk2(Service::Interface* self);
  150. /**
  151. * CFG::GetConfigInfoBlk8 service function
  152. * Inputs:
  153. * 0 : 0x04010082 / 0x08010082
  154. * 1 : Size
  155. * 2 : Block ID
  156. * 3 : Descriptor for the output buffer
  157. * 4 : Output buffer pointer
  158. * Outputs:
  159. * 1 : Result of function, 0 on success, otherwise error code
  160. */
  161. void GetConfigInfoBlk8(Service::Interface* self);
  162. /**
  163. * CFG::SetConfigInfoBlk4 service function
  164. * Inputs:
  165. * 0 : 0x04020082 / 0x08020082
  166. * 1 : Block ID
  167. * 2 : Size
  168. * 3 : Descriptor for the output buffer
  169. * 4 : Output buffer pointer
  170. * Outputs:
  171. * 1 : Result of function, 0 on success, otherwise error code
  172. * Note:
  173. * The parameters order is different from GetConfigInfoBlk2/8's,
  174. * where Block ID and Size are switched.
  175. */
  176. void SetConfigInfoBlk4(Service::Interface* self);
  177. /**
  178. * CFG::UpdateConfigNANDSavegame service function
  179. * Inputs:
  180. * 0 : 0x04030000 / 0x08030000
  181. * Outputs:
  182. * 1 : Result of function, 0 on success, otherwise error code
  183. */
  184. void UpdateConfigNANDSavegame(Service::Interface* self);
  185. /**
  186. * CFG::FormatConfig service function
  187. * Inputs:
  188. * 0 : 0x08060000
  189. * Outputs:
  190. * 1 : Result of function, 0 on success, otherwise error code
  191. */
  192. void FormatConfig(Service::Interface* self);
  193. /**
  194. * Reads a block with the specified id and flag from the Config savegame buffer
  195. * and writes the output to output. The input size must match exactly the size of the requested
  196. * block.
  197. *
  198. * @param block_id The id of the block we want to read
  199. * @param size The size of the block we want to read
  200. * @param flag The requested block must have this flag set
  201. * @param output A pointer where we will write the read data
  202. * @returns ResultCode indicating the result of the operation, 0 on success
  203. */
  204. ResultCode GetConfigInfoBlock(u32 block_id, u32 size, u32 flag, void* output);
  205. /**
  206. * Reads data from input and writes to a block with the specified id and flag
  207. * in the Config savegame buffer. The input size must match exactly the size of the target block.
  208. *
  209. * @param block_id The id of the block we want to write
  210. * @param size The size of the block we want to write
  211. * @param flag The target block must have this flag set
  212. * @param input A pointer where we will read data and write to Config savegame buffer
  213. * @returns ResultCode indicating the result of the operation, 0 on success
  214. */
  215. ResultCode SetConfigInfoBlock(u32 block_id, u32 size, u32 flag, const void* input);
  216. /**
  217. * Creates a block with the specified id and writes the input data to the cfg savegame buffer in
  218. * memory. The config savegame file in the filesystem is not updated.
  219. *
  220. * @param block_id The id of the block we want to create
  221. * @param size The size of the block we want to create
  222. * @param flags The flags of the new block
  223. * @param data A pointer containing the data we will write to the new block
  224. * @returns ResultCode indicating the result of the operation, 0 on success
  225. */
  226. ResultCode CreateConfigInfoBlk(u32 block_id, u16 size, u16 flags, const void* data);
  227. /**
  228. * Deletes the config savegame file from the filesystem, the buffer in memory is not affected
  229. * @returns ResultCode indicating the result of the operation, 0 on success
  230. */
  231. ResultCode DeleteConfigNANDSaveFile();
  232. /**
  233. * Writes the config savegame memory buffer to the config savegame file in the filesystem
  234. * @returns ResultCode indicating the result of the operation, 0 on success
  235. */
  236. ResultCode UpdateConfigNANDSavegame();
  237. /**
  238. * Re-creates the config savegame file in memory and the filesystem with the default blocks
  239. * @returns ResultCode indicating the result of the operation, 0 on success
  240. */
  241. ResultCode FormatConfig();
  242. /**
  243. * Open the config savegame file and load it to the memory buffer
  244. * @returns ResultCode indicating the result of the operation, 0 on success
  245. */
  246. ResultCode LoadConfigNANDSaveFile();
  247. /// Initialize the config service
  248. void Init();
  249. /// Shutdown the config service
  250. void Shutdown();
  251. /**
  252. * Set the region code preferred by the game so that CFG will adjust to it when the region setting
  253. * is auto.
  254. * @param region_code the preferred region code to set
  255. */
  256. void SetPreferredRegionCode(u32 region_code);
  257. // Utilities for frontend to set config data.
  258. // Note: before calling these functions, LoadConfigNANDSaveFile should be called,
  259. // and UpdateConfigNANDSavegame should be called after making changes to config data.
  260. /**
  261. * Sets the username in config savegame.
  262. * @param name the username to set. The maximum size is 10 in char16_t.
  263. */
  264. void SetUsername(const std::u16string& name);
  265. /**
  266. * Gets the username from config savegame.
  267. * @returns the username
  268. */
  269. std::u16string GetUsername();
  270. /**
  271. * Sets the profile birthday in config savegame.
  272. * @param month the month of birthday.
  273. * @param day the day of the birthday.
  274. */
  275. void SetBirthday(u8 month, u8 day);
  276. /**
  277. * Gets the profile birthday from the config savegame.
  278. * @returns a tuple of (month, day) of birthday
  279. */
  280. std::tuple<u8, u8> GetBirthday();
  281. /**
  282. * Sets the system language in config savegame.
  283. * @param language the system language to set.
  284. */
  285. void SetSystemLanguage(SystemLanguage language);
  286. /**
  287. * Gets the system language from config savegame.
  288. * @returns the system language
  289. */
  290. SystemLanguage GetSystemLanguage();
  291. /**
  292. * Sets the sound output mode in config savegame.
  293. * @param mode the sound output mode to set
  294. */
  295. void SetSoundOutputMode(SoundOutputMode mode);
  296. /**
  297. * Gets the sound output mode from config savegame.
  298. * @returns the sound output mode
  299. */
  300. SoundOutputMode GetSoundOutputMode();
  301. /**
  302. * Generates a new random console unique id.
  303. * @param random_number a random generated 16bit number stored at 0x90002, used for generating the
  304. * console_id
  305. * @param console_id the randomly created console id
  306. */
  307. void GenerateConsoleUniqueId(u32& random_number, u64& console_id);
  308. /**
  309. * Sets the random_number and the console unique id in the config savegame.
  310. * @param random_number the random_number to set
  311. * @param console_id the console id to set
  312. */
  313. ResultCode SetConsoleUniqueId(u32 random_number, u64 console_id);
  314. /**
  315. * Gets the console unique id from config savegame.
  316. * @returns the console unique id
  317. */
  318. u64 GetConsoleUniqueId();
  319. } // namespace CFG
  320. } // namespace Service