cfg_u.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/file_util.h"
  5. #include "common/string_util.h"
  6. #include "core/settings.h"
  7. #include "core/file_sys/archive_systemsavedata.h"
  8. #include "core/hle/hle.h"
  9. #include "core/hle/service/cfg/cfg.h"
  10. #include "core/hle/service/cfg/cfg_u.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // Namespace CFG_U
  13. namespace CFG_U {
  14. // TODO(Link Mauve): use a constexpr once MSVC starts supporting it.
  15. #define C(code) ((code)[0] | ((code)[1] << 8))
  16. static const std::array<u16, 187> country_codes = {
  17. 0, C("JP"), 0, 0, 0, 0, 0, 0, // 0-7
  18. C("AI"), C("AG"), C("AR"), C("AW"), C("BS"), C("BB"), C("BZ"), C("BO"), // 8-15
  19. C("BR"), C("VG"), C("CA"), C("KY"), C("CL"), C("CO"), C("CR"), C("DM"), // 16-23
  20. C("DO"), C("EC"), C("SV"), C("GF"), C("GD"), C("GP"), C("GT"), C("GY"), // 24-31
  21. C("HT"), C("HN"), C("JM"), C("MQ"), C("MX"), C("MS"), C("AN"), C("NI"), // 32-39
  22. C("PA"), C("PY"), C("PE"), C("KN"), C("LC"), C("VC"), C("SR"), C("TT"), // 40-47
  23. C("TC"), C("US"), C("UY"), C("VI"), C("VE"), 0, 0, 0, // 48-55
  24. 0, 0, 0, 0, 0, 0, 0, 0, // 56-63
  25. C("AL"), C("AU"), C("AT"), C("BE"), C("BA"), C("BW"), C("BG"), C("HR"), // 64-71
  26. C("CY"), C("CZ"), C("DK"), C("EE"), C("FI"), C("FR"), C("DE"), C("GR"), // 72-79
  27. C("HU"), C("IS"), C("IE"), C("IT"), C("LV"), C("LS"), C("LI"), C("LT"), // 80-87
  28. C("LU"), C("MK"), C("MT"), C("ME"), C("MZ"), C("NA"), C("NL"), C("NZ"), // 88-95
  29. C("NO"), C("PL"), C("PT"), C("RO"), C("RU"), C("RS"), C("SK"), C("SI"), // 96-103
  30. C("ZA"), C("ES"), C("SZ"), C("SE"), C("CH"), C("TR"), C("GB"), C("ZM"), // 104-111
  31. C("ZW"), C("AZ"), C("MR"), C("ML"), C("NE"), C("TD"), C("SD"), C("ER"), // 112-119
  32. C("DJ"), C("SO"), C("AD"), C("GI"), C("GG"), C("IM"), C("JE"), C("MC"), // 120-127
  33. C("TW"), 0, 0, 0, 0, 0, 0, 0, // 128-135
  34. C("KR"), 0, 0, 0, 0, 0, 0, 0, // 136-143
  35. C("HK"), C("MO"), 0, 0, 0, 0, 0, 0, // 144-151
  36. C("ID"), C("SG"), C("TH"), C("PH"), C("MY"), 0, 0, 0, // 152-159
  37. C("CN"), 0, 0, 0, 0, 0, 0, 0, // 160-167
  38. C("AE"), C("IN"), C("EG"), C("OM"), C("QA"), C("KW"), C("SA"), C("SY"), // 168-175
  39. C("BH"), C("JO"), 0, 0, 0, 0, 0, 0, // 176-183
  40. C("SM"), C("VA"), C("BM") // 184-186
  41. };
  42. #undef C
  43. /**
  44. * CFG_User::GetCountryCodeString service function
  45. * Inputs:
  46. * 1 : Country Code ID
  47. * Outputs:
  48. * 1 : Result of function, 0 on success, otherwise error code
  49. * 2 : Country's 2-char string
  50. */
  51. static void GetCountryCodeString(Service::Interface* self) {
  52. u32* cmd_buffer = Kernel::GetCommandBuffer();
  53. u32 country_code_id = cmd_buffer[1];
  54. if (country_code_id >= country_codes.size() || 0 == country_codes[country_code_id]) {
  55. LOG_ERROR(Service_CFG, "requested country code id=%d is invalid", country_code_id);
  56. cmd_buffer[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw;
  57. return;
  58. }
  59. cmd_buffer[1] = 0;
  60. cmd_buffer[2] = country_codes[country_code_id];
  61. }
  62. /**
  63. * CFG_User::GetCountryCodeID service function
  64. * Inputs:
  65. * 1 : Country Code 2-char string
  66. * Outputs:
  67. * 1 : Result of function, 0 on success, otherwise error code
  68. * 2 : Country Code ID
  69. */
  70. static void GetCountryCodeID(Service::Interface* self) {
  71. u32* cmd_buffer = Kernel::GetCommandBuffer();
  72. u16 country_code = cmd_buffer[1];
  73. u16 country_code_id = 0;
  74. // The following algorithm will fail if the first country code isn't 0.
  75. DEBUG_ASSERT(country_codes[0] == 0);
  76. for (u16 id = 0; id < country_codes.size(); ++id) {
  77. if (country_codes[id] == country_code) {
  78. country_code_id = id;
  79. break;
  80. }
  81. }
  82. if (0 == country_code_id) {
  83. LOG_ERROR(Service_CFG, "requested country code name=%c%c is invalid", country_code & 0xff, country_code >> 8);
  84. cmd_buffer[1] = ResultCode(ErrorDescription::NotFound, ErrorModule::Config, ErrorSummary::WrongArgument, ErrorLevel::Permanent).raw;
  85. cmd_buffer[2] = 0xFFFF;
  86. return;
  87. }
  88. cmd_buffer[1] = 0;
  89. cmd_buffer[2] = country_code_id;
  90. }
  91. /**
  92. * CFG_User::GetConfigInfoBlk2 service function
  93. * Inputs:
  94. * 0 : 0x00010082
  95. * 1 : Size
  96. * 2 : Block ID
  97. * 3 : Descriptor for the output buffer
  98. * 4 : Output buffer pointer
  99. * Outputs:
  100. * 1 : Result of function, 0 on success, otherwise error code
  101. */
  102. static void GetConfigInfoBlk2(Service::Interface* self) {
  103. u32* cmd_buffer = Kernel::GetCommandBuffer();
  104. u32 size = cmd_buffer[1];
  105. u32 block_id = cmd_buffer[2];
  106. u8* data_pointer = Memory::GetPointer(cmd_buffer[4]);
  107. if (data_pointer == nullptr) {
  108. cmd_buffer[1] = -1; // TODO(Subv): Find the right error code
  109. return;
  110. }
  111. cmd_buffer[1] = Service::CFG::GetConfigInfoBlock(block_id, size, 0x2, data_pointer).raw;
  112. }
  113. /**
  114. * CFG_User::SecureInfoGetRegion service function
  115. * Inputs:
  116. * 1 : None
  117. * Outputs:
  118. * 0 : Result Header code
  119. * 1 : Result of function, 0 on success, otherwise error code
  120. * 2 : Region value loaded from SecureInfo offset 0x100
  121. */
  122. static void SecureInfoGetRegion(Service::Interface* self) {
  123. u32* cmd_buffer = Kernel::GetCommandBuffer();
  124. cmd_buffer[1] = RESULT_SUCCESS.raw; // No Error
  125. cmd_buffer[2] = Settings::values.region_value;
  126. }
  127. /**
  128. * CFG_User::GenHashConsoleUnique service function
  129. * Inputs:
  130. * 1 : 20 bit application ID salt
  131. * Outputs:
  132. * 0 : Result Header code
  133. * 1 : Result of function, 0 on success, otherwise error code
  134. * 2 : Hash/"ID" lower word
  135. * 3 : Hash/"ID" upper word
  136. */
  137. static void GenHashConsoleUnique(Service::Interface* self) {
  138. u32* cmd_buffer = Kernel::GetCommandBuffer();
  139. u32 app_id_salt = cmd_buffer[1];
  140. cmd_buffer[1] = RESULT_SUCCESS.raw; // No Error
  141. cmd_buffer[2] = 0x33646D6F ^ (app_id_salt & 0xFFFFF); // 3dmoo hash
  142. cmd_buffer[3] = 0x6F534841 ^ (app_id_salt & 0xFFFFF);
  143. LOG_WARNING(Service_CFG, "(STUBBED) called app_id_salt=0x%08X", app_id_salt);
  144. }
  145. /**
  146. * CFG_User::GetRegionCanadaUSA service function
  147. * Inputs:
  148. * 1 : None
  149. * Outputs:
  150. * 0 : Result Header code
  151. * 1 : Result of function, 0 on success, otherwise error code
  152. * 2 : Output value
  153. */
  154. static void GetRegionCanadaUSA(Service::Interface* self) {
  155. u32* cmd_buffer = Kernel::GetCommandBuffer();
  156. cmd_buffer[1] = RESULT_SUCCESS.raw; // No Error
  157. u8 canada_or_usa = 1;
  158. if (canada_or_usa == Settings::values.region_value) {
  159. cmd_buffer[2] = 1;
  160. } else {
  161. cmd_buffer[2] = 0;
  162. }
  163. }
  164. /**
  165. * CFG_User::GetSystemModel service function
  166. * Inputs:
  167. * 0 : 0x00050000
  168. * Outputs:
  169. * 1 : Result of function, 0 on success, otherwise error code
  170. * 2 : Model of the console
  171. */
  172. static void GetSystemModel(Service::Interface* self) {
  173. u32* cmd_buffer = Kernel::GetCommandBuffer();
  174. u32 data;
  175. // TODO(Subv): Find out the correct error codes
  176. cmd_buffer[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8,
  177. reinterpret_cast<u8*>(&data)).raw;
  178. cmd_buffer[2] = data & 0xFF;
  179. }
  180. /**
  181. * CFG_User::GetModelNintendo2DS service function
  182. * Inputs:
  183. * 0 : 0x00060000
  184. * Outputs:
  185. * 1 : Result of function, 0 on success, otherwise error code
  186. * 2 : 0 if the system is a Nintendo 2DS, 1 otherwise
  187. */
  188. static void GetModelNintendo2DS(Service::Interface* self) {
  189. u32* cmd_buffer = Kernel::GetCommandBuffer();
  190. u32 data;
  191. // TODO(Subv): Find out the correct error codes
  192. cmd_buffer[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8,
  193. reinterpret_cast<u8*>(&data)).raw;
  194. u8 model = data & 0xFF;
  195. if (model == Service::CFG::NINTENDO_2DS)
  196. cmd_buffer[2] = 0;
  197. else
  198. cmd_buffer[2] = 1;
  199. }
  200. const Interface::FunctionInfo FunctionTable[] = {
  201. {0x00010082, GetConfigInfoBlk2, "GetConfigInfoBlk2"},
  202. {0x00020000, SecureInfoGetRegion, "SecureInfoGetRegion"},
  203. {0x00030040, GenHashConsoleUnique, "GenHashConsoleUnique"},
  204. {0x00040000, GetRegionCanadaUSA, "GetRegionCanadaUSA"},
  205. {0x00050000, GetSystemModel, "GetSystemModel"},
  206. {0x00060000, GetModelNintendo2DS, "GetModelNintendo2DS"},
  207. {0x00070040, nullptr, "WriteToFirstByteCfgSavegame"},
  208. {0x00080080, nullptr, "GoThroughTable"},
  209. {0x00090040, GetCountryCodeString, "GetCountryCodeString"},
  210. {0x000A0040, GetCountryCodeID, "GetCountryCodeID"},
  211. };
  212. ////////////////////////////////////////////////////////////////////////////////////////////////////
  213. // Interface class
  214. Interface::Interface() {
  215. Register(FunctionTable);
  216. }
  217. } // namespace