acc.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include "common/common_types.h"
  6. #include "common/logging/log.h"
  7. #include "common/swap.h"
  8. #include "core/core_timing.h"
  9. #include "core/hle/ipc_helpers.h"
  10. #include "core/hle/service/acc/acc.h"
  11. #include "core/hle/service/acc/acc_aa.h"
  12. #include "core/hle/service/acc/acc_su.h"
  13. #include "core/hle/service/acc/acc_u0.h"
  14. #include "core/hle/service/acc/acc_u1.h"
  15. #include "core/settings.h"
  16. namespace Service::Account {
  17. // TODO: RE this structure
  18. struct UserData {
  19. INSERT_PADDING_WORDS(1);
  20. u32 icon_id;
  21. u8 bg_color_id;
  22. INSERT_PADDING_BYTES(0x7);
  23. INSERT_PADDING_BYTES(0x10);
  24. INSERT_PADDING_BYTES(0x60);
  25. };
  26. static_assert(sizeof(UserData) == 0x80, "UserData structure has incorrect size");
  27. // TODO(ogniK): Generate a real user id based on username, md5(username) maybe?
  28. static UUID DEFAULT_USER_ID{1ull, 0ull};
  29. class IProfile final : public ServiceFramework<IProfile> {
  30. public:
  31. explicit IProfile(UUID user_id, ProfileManager& profile_manager)
  32. : ServiceFramework("IProfile"), user_id(user_id), profile_manager(profile_manager) {
  33. static const FunctionInfo functions[] = {
  34. {0, &IProfile::Get, "Get"},
  35. {1, &IProfile::GetBase, "GetBase"},
  36. {10, nullptr, "GetImageSize"},
  37. {11, &IProfile::LoadImage, "LoadImage"},
  38. };
  39. RegisterHandlers(functions);
  40. }
  41. private:
  42. void Get(Kernel::HLERequestContext& ctx) {
  43. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  44. ProfileBase profile_base{};
  45. std::array<u8, MAX_DATA> data{};
  46. if (profile_manager.GetProfileBaseAndData(user_id, profile_base, data)) {
  47. ctx.WriteBuffer(data);
  48. IPC::ResponseBuilder rb{ctx, 16};
  49. rb.Push(RESULT_SUCCESS);
  50. rb.PushRaw(profile_base);
  51. } else {
  52. LOG_ERROR(Service_ACC, "Failed to get profile base and data for user={}",
  53. user_id.Format());
  54. IPC::ResponseBuilder rb{ctx, 2};
  55. rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code
  56. }
  57. }
  58. void GetBase(Kernel::HLERequestContext& ctx) {
  59. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  60. ProfileBase profile_base{};
  61. if (profile_manager.GetProfileBase(user_id, profile_base)) {
  62. IPC::ResponseBuilder rb{ctx, 16};
  63. rb.Push(RESULT_SUCCESS);
  64. rb.PushRaw(profile_base);
  65. } else {
  66. LOG_ERROR(Service_ACC, "Failed to get profile base for user={}", user_id.Format());
  67. IPC::ResponseBuilder rb{ctx, 2};
  68. rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code
  69. }
  70. }
  71. void LoadImage(Kernel::HLERequestContext& ctx) {
  72. LOG_WARNING(Service_ACC, "(STUBBED) called");
  73. // smallest jpeg https://github.com/mathiasbynens/small/blob/master/jpeg.jpg
  74. // TODO(mailwl): load actual profile image from disk, width 256px, max size 0x20000
  75. const u32 jpeg_size = 107;
  76. static const std::array<u8, jpeg_size> jpeg{
  77. 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03,
  78. 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04,
  79. 0x08, 0x06, 0x06, 0x05, 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09, 0x08, 0x09, 0x09, 0x0a,
  80. 0x0c, 0x0f, 0x0c, 0x0a, 0x0b, 0x0e, 0x0b, 0x09, 0x09, 0x0d, 0x11, 0x0d, 0x0e, 0x0f,
  81. 0x10, 0x10, 0x11, 0x10, 0x0a, 0x0c, 0x12, 0x13, 0x12, 0x10, 0x13, 0x0f, 0x10, 0x10,
  82. 0x10, 0xff, 0xc9, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x11, 0x00,
  83. 0xff, 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, 0xff, 0xda, 0x00, 0x08, 0x01, 0x01,
  84. 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9,
  85. };
  86. ctx.WriteBuffer(jpeg.data(), jpeg_size);
  87. IPC::ResponseBuilder rb{ctx, 3};
  88. rb.Push(RESULT_SUCCESS);
  89. rb.Push<u32>(jpeg_size);
  90. }
  91. const ProfileManager& profile_manager;
  92. UUID user_id; ///< The user id this profile refers to.
  93. };
  94. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  95. public:
  96. IManagerForApplication() : ServiceFramework("IManagerForApplication") {
  97. static const FunctionInfo functions[] = {
  98. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  99. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  100. {2, nullptr, "EnsureIdTokenCacheAsync"},
  101. {3, nullptr, "LoadIdTokenCache"},
  102. {130, nullptr, "GetNintendoAccountUserResourceCacheForApplication"},
  103. {150, nullptr, "CreateAuthorizationRequest"},
  104. {160, nullptr, "StoreOpenContext"},
  105. };
  106. RegisterHandlers(functions);
  107. }
  108. private:
  109. void CheckAvailability(Kernel::HLERequestContext& ctx) {
  110. LOG_WARNING(Service_ACC, "(STUBBED) called");
  111. IPC::ResponseBuilder rb{ctx, 3};
  112. rb.Push(RESULT_SUCCESS);
  113. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  114. }
  115. void GetAccountId(Kernel::HLERequestContext& ctx) {
  116. LOG_WARNING(Service_ACC, "(STUBBED) called");
  117. // TODO(Subv): Find out what this actually does and implement it. Stub it as an error for
  118. // now since we do not implement NNID. Returning a bogus id here will cause games to send
  119. // invalid IPC requests after ListOpenUsers is called.
  120. IPC::ResponseBuilder rb{ctx, 2};
  121. rb.Push(ResultCode(-1));
  122. }
  123. };
  124. void Module::Interface::GetUserCount(Kernel::HLERequestContext& ctx) {
  125. LOG_INFO(Service_ACC, "called");
  126. IPC::ResponseBuilder rb{ctx, 3};
  127. rb.Push(RESULT_SUCCESS);
  128. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  129. }
  130. void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
  131. IPC::RequestParser rp{ctx};
  132. UUID user_id = rp.PopRaw<UUID>();
  133. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  134. IPC::ResponseBuilder rb{ctx, 3};
  135. rb.Push(RESULT_SUCCESS);
  136. rb.Push(profile_manager->UserExists(user_id));
  137. }
  138. void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
  139. LOG_INFO(Service_ACC, "called");
  140. ctx.WriteBuffer(profile_manager->GetAllUsers());
  141. IPC::ResponseBuilder rb{ctx, 2};
  142. rb.Push(RESULT_SUCCESS);
  143. }
  144. void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
  145. LOG_INFO(Service_ACC, "called");
  146. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  147. IPC::ResponseBuilder rb{ctx, 2};
  148. rb.Push(RESULT_SUCCESS);
  149. }
  150. void Module::Interface::GetLastOpenedUser(Kernel::HLERequestContext& ctx) {
  151. LOG_INFO(Service_ACC, "called");
  152. IPC::ResponseBuilder rb{ctx, 6};
  153. rb.Push(RESULT_SUCCESS);
  154. rb.PushRaw<UUID>(profile_manager->GetLastOpenedUser());
  155. }
  156. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  157. IPC::RequestParser rp{ctx};
  158. UUID user_id = rp.PopRaw<UUID>();
  159. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  160. rb.Push(RESULT_SUCCESS);
  161. rb.PushIpcInterface<IProfile>(user_id, *profile_manager);
  162. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  163. }
  164. void Module::Interface::IsUserRegistrationRequestPermitted(Kernel::HLERequestContext& ctx) {
  165. LOG_WARNING(Service_ACC, "(STUBBED) called");
  166. IPC::ResponseBuilder rb{ctx, 3};
  167. rb.Push(RESULT_SUCCESS);
  168. rb.Push(profile_manager->CanSystemRegisterUser());
  169. }
  170. void Module::Interface::InitializeApplicationInfo(Kernel::HLERequestContext& ctx) {
  171. LOG_WARNING(Service_ACC, "(STUBBED) called");
  172. IPC::ResponseBuilder rb{ctx, 2};
  173. rb.Push(RESULT_SUCCESS);
  174. }
  175. void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestContext& ctx) {
  176. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  177. rb.Push(RESULT_SUCCESS);
  178. rb.PushIpcInterface<IManagerForApplication>();
  179. LOG_DEBUG(Service_ACC, "called");
  180. }
  181. Module::Interface::Interface(std::shared_ptr<Module> module,
  182. std::shared_ptr<ProfileManager> profile_manager, const char* name)
  183. : ServiceFramework(name), module(std::move(module)),
  184. profile_manager(std::move(profile_manager)) {}
  185. void InstallInterfaces(SM::ServiceManager& service_manager) {
  186. auto module = std::make_shared<Module>();
  187. auto profile_manager = std::make_shared<ProfileManager>();
  188. std::make_shared<ACC_AA>(module, profile_manager)->InstallAsService(service_manager);
  189. std::make_shared<ACC_SU>(module, profile_manager)->InstallAsService(service_manager);
  190. std::make_shared<ACC_U0>(module, profile_manager)->InstallAsService(service_manager);
  191. std::make_shared<ACC_U1>(module, profile_manager)->InstallAsService(service_manager);
  192. }
  193. } // namespace Service::Account