cfg_u.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/log.h"
  6. #include "common/string_util.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. _dbg_assert_(Service_CFG, country_codes[0] == 0);
  76. for (size_t 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::GetSystemModel service function
  115. * Inputs:
  116. * 0 : 0x00050000
  117. * Outputs:
  118. * 1 : Result of function, 0 on success, otherwise error code
  119. * 2 : Model of the console
  120. */
  121. static void GetSystemModel(Service::Interface* self) {
  122. u32* cmd_buffer = Kernel::GetCommandBuffer();
  123. u32 data;
  124. // TODO(Subv): Find out the correct error codes
  125. cmd_buffer[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8,
  126. reinterpret_cast<u8*>(&data)).raw;
  127. cmd_buffer[2] = data & 0xFF;
  128. }
  129. /**
  130. * CFG_User::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. static void GetModelNintendo2DS(Service::Interface* self) {
  138. u32* cmd_buffer = Kernel::GetCommandBuffer();
  139. u32 data;
  140. // TODO(Subv): Find out the correct error codes
  141. cmd_buffer[1] = Service::CFG::GetConfigInfoBlock(0x000F0004, 4, 0x8,
  142. reinterpret_cast<u8*>(&data)).raw;
  143. u8 model = data & 0xFF;
  144. if (model == Service::CFG::NINTENDO_2DS)
  145. cmd_buffer[2] = 0;
  146. else
  147. cmd_buffer[2] = 1;
  148. }
  149. const Interface::FunctionInfo FunctionTable[] = {
  150. {0x00010082, GetConfigInfoBlk2, "GetConfigInfoBlk2"},
  151. {0x00020000, nullptr, "SecureInfoGetRegion"},
  152. {0x00030040, nullptr, "GenHashConsoleUnique"},
  153. {0x00040000, nullptr, "GetRegionCanadaUSA"},
  154. {0x00050000, GetSystemModel, "GetSystemModel"},
  155. {0x00060000, GetModelNintendo2DS, "GetModelNintendo2DS"},
  156. {0x00070040, nullptr, "WriteToFirstByteCfgSavegame"},
  157. {0x00080080, nullptr, "GoThroughTable"},
  158. {0x00090040, GetCountryCodeString, "GetCountryCodeString"},
  159. {0x000A0040, GetCountryCodeID, "GetCountryCodeID"},
  160. };
  161. ////////////////////////////////////////////////////////////////////////////////////////////////////
  162. // Interface class
  163. Interface::Interface() {
  164. Register(FunctionTable);
  165. }
  166. } // namespace