acc.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include "common/common_paths.h"
  7. #include "common/common_types.h"
  8. #include "common/file_util.h"
  9. #include "common/logging/log.h"
  10. #include "common/string_util.h"
  11. #include "common/swap.h"
  12. #include "core/core_timing.h"
  13. #include "core/hle/ipc_helpers.h"
  14. #include "core/hle/service/acc/acc.h"
  15. #include "core/hle/service/acc/acc_aa.h"
  16. #include "core/hle/service/acc/acc_su.h"
  17. #include "core/hle/service/acc/acc_u0.h"
  18. #include "core/hle/service/acc/acc_u1.h"
  19. #include "core/hle/service/acc/profile_manager.h"
  20. namespace Service::Account {
  21. constexpr u32 MAX_JPEG_IMAGE_SIZE = 0x20000;
  22. // TODO: RE this structure
  23. struct UserData {
  24. INSERT_PADDING_WORDS(1);
  25. u32 icon_id;
  26. u8 bg_color_id;
  27. INSERT_PADDING_BYTES(0x7);
  28. INSERT_PADDING_BYTES(0x10);
  29. INSERT_PADDING_BYTES(0x60);
  30. };
  31. static_assert(sizeof(UserData) == 0x80, "UserData structure has incorrect size");
  32. static std::string GetImagePath(UUID uuid) {
  33. return FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
  34. "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
  35. }
  36. class IProfile final : public ServiceFramework<IProfile> {
  37. public:
  38. explicit IProfile(UUID user_id, ProfileManager& profile_manager)
  39. : ServiceFramework("IProfile"), profile_manager(profile_manager), user_id(user_id) {
  40. static const FunctionInfo functions[] = {
  41. {0, &IProfile::Get, "Get"},
  42. {1, &IProfile::GetBase, "GetBase"},
  43. {10, &IProfile::GetImageSize, "GetImageSize"},
  44. {11, &IProfile::LoadImage, "LoadImage"},
  45. };
  46. RegisterHandlers(functions);
  47. }
  48. private:
  49. void Get(Kernel::HLERequestContext& ctx) {
  50. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  51. ProfileBase profile_base{};
  52. std::array<u8, MAX_DATA> data{};
  53. if (profile_manager.GetProfileBaseAndData(user_id, profile_base, data)) {
  54. ctx.WriteBuffer(data);
  55. IPC::ResponseBuilder rb{ctx, 16};
  56. rb.Push(RESULT_SUCCESS);
  57. rb.PushRaw(profile_base);
  58. } else {
  59. LOG_ERROR(Service_ACC, "Failed to get profile base and data for user={}",
  60. user_id.Format());
  61. IPC::ResponseBuilder rb{ctx, 2};
  62. rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code
  63. }
  64. }
  65. void GetBase(Kernel::HLERequestContext& ctx) {
  66. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  67. ProfileBase profile_base{};
  68. if (profile_manager.GetProfileBase(user_id, profile_base)) {
  69. IPC::ResponseBuilder rb{ctx, 16};
  70. rb.Push(RESULT_SUCCESS);
  71. rb.PushRaw(profile_base);
  72. } else {
  73. LOG_ERROR(Service_ACC, "Failed to get profile base for user={}", user_id.Format());
  74. IPC::ResponseBuilder rb{ctx, 2};
  75. rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code
  76. }
  77. }
  78. void LoadImage(Kernel::HLERequestContext& ctx) {
  79. LOG_DEBUG(Service_ACC, "called");
  80. // smallest jpeg https://github.com/mathiasbynens/small/blob/master/jpeg.jpg
  81. // used as a backup should the one on disk not exist
  82. constexpr u32 backup_jpeg_size = 107;
  83. static constexpr std::array<u8, backup_jpeg_size> backup_jpeg{
  84. 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03,
  85. 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04,
  86. 0x08, 0x06, 0x06, 0x05, 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09, 0x08, 0x09, 0x09, 0x0a,
  87. 0x0c, 0x0f, 0x0c, 0x0a, 0x0b, 0x0e, 0x0b, 0x09, 0x09, 0x0d, 0x11, 0x0d, 0x0e, 0x0f,
  88. 0x10, 0x10, 0x11, 0x10, 0x0a, 0x0c, 0x12, 0x13, 0x12, 0x10, 0x13, 0x0f, 0x10, 0x10,
  89. 0x10, 0xff, 0xc9, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x11, 0x00,
  90. 0xff, 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, 0xff, 0xda, 0x00, 0x08, 0x01, 0x01,
  91. 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9,
  92. };
  93. IPC::ResponseBuilder rb{ctx, 3};
  94. rb.Push(RESULT_SUCCESS);
  95. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  96. if (!image.IsOpen()) {
  97. ctx.WriteBuffer(backup_jpeg);
  98. rb.Push<u32>(backup_jpeg_size);
  99. } else {
  100. const auto size = std::min<u32>(image.GetSize(), MAX_JPEG_IMAGE_SIZE);
  101. std::vector<u8> buffer(size);
  102. image.ReadBytes(buffer.data(), buffer.size());
  103. ctx.WriteBuffer(buffer.data(), buffer.size());
  104. rb.Push<u32>(buffer.size());
  105. }
  106. }
  107. void GetImageSize(Kernel::HLERequestContext& ctx) {
  108. LOG_DEBUG(Service_ACC, "called");
  109. constexpr u32 backup_jpeg_size = 107;
  110. IPC::ResponseBuilder rb{ctx, 3};
  111. rb.Push(RESULT_SUCCESS);
  112. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  113. if (!image.IsOpen())
  114. rb.Push<u32>(backup_jpeg_size);
  115. else
  116. rb.Push<u32>(std::min<u32>(image.GetSize(), MAX_JPEG_IMAGE_SIZE));
  117. }
  118. const ProfileManager& profile_manager;
  119. UUID user_id; ///< The user id this profile refers to.
  120. };
  121. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  122. public:
  123. IManagerForApplication() : ServiceFramework("IManagerForApplication") {
  124. static const FunctionInfo functions[] = {
  125. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  126. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  127. {2, nullptr, "EnsureIdTokenCacheAsync"},
  128. {3, nullptr, "LoadIdTokenCache"},
  129. {130, nullptr, "GetNintendoAccountUserResourceCacheForApplication"},
  130. {150, nullptr, "CreateAuthorizationRequest"},
  131. {160, nullptr, "StoreOpenContext"},
  132. };
  133. RegisterHandlers(functions);
  134. }
  135. private:
  136. void CheckAvailability(Kernel::HLERequestContext& ctx) {
  137. LOG_WARNING(Service_ACC, "(STUBBED) called");
  138. IPC::ResponseBuilder rb{ctx, 3};
  139. rb.Push(RESULT_SUCCESS);
  140. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  141. }
  142. void GetAccountId(Kernel::HLERequestContext& ctx) {
  143. LOG_WARNING(Service_ACC, "(STUBBED) called");
  144. // Should return a nintendo account ID
  145. IPC::ResponseBuilder rb{ctx, 4};
  146. rb.Push(RESULT_SUCCESS);
  147. rb.PushRaw<u64>(1);
  148. }
  149. };
  150. void Module::Interface::GetUserCount(Kernel::HLERequestContext& ctx) {
  151. LOG_INFO(Service_ACC, "called");
  152. IPC::ResponseBuilder rb{ctx, 3};
  153. rb.Push(RESULT_SUCCESS);
  154. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  155. }
  156. void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
  157. IPC::RequestParser rp{ctx};
  158. UUID user_id = rp.PopRaw<UUID>();
  159. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  160. IPC::ResponseBuilder rb{ctx, 3};
  161. rb.Push(RESULT_SUCCESS);
  162. rb.Push(profile_manager->UserExists(user_id));
  163. }
  164. void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
  165. LOG_INFO(Service_ACC, "called");
  166. ctx.WriteBuffer(profile_manager->GetAllUsers());
  167. IPC::ResponseBuilder rb{ctx, 2};
  168. rb.Push(RESULT_SUCCESS);
  169. }
  170. void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
  171. LOG_INFO(Service_ACC, "called");
  172. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  173. IPC::ResponseBuilder rb{ctx, 2};
  174. rb.Push(RESULT_SUCCESS);
  175. }
  176. void Module::Interface::GetLastOpenedUser(Kernel::HLERequestContext& ctx) {
  177. LOG_INFO(Service_ACC, "called");
  178. IPC::ResponseBuilder rb{ctx, 6};
  179. rb.Push(RESULT_SUCCESS);
  180. rb.PushRaw<UUID>(profile_manager->GetLastOpenedUser());
  181. }
  182. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  183. IPC::RequestParser rp{ctx};
  184. UUID user_id = rp.PopRaw<UUID>();
  185. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  186. rb.Push(RESULT_SUCCESS);
  187. rb.PushIpcInterface<IProfile>(user_id, *profile_manager);
  188. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  189. }
  190. void Module::Interface::IsUserRegistrationRequestPermitted(Kernel::HLERequestContext& ctx) {
  191. LOG_WARNING(Service_ACC, "(STUBBED) called");
  192. IPC::ResponseBuilder rb{ctx, 3};
  193. rb.Push(RESULT_SUCCESS);
  194. rb.Push(profile_manager->CanSystemRegisterUser());
  195. }
  196. void Module::Interface::InitializeApplicationInfo(Kernel::HLERequestContext& ctx) {
  197. LOG_WARNING(Service_ACC, "(STUBBED) called");
  198. IPC::ResponseBuilder rb{ctx, 2};
  199. rb.Push(RESULT_SUCCESS);
  200. }
  201. void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestContext& ctx) {
  202. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  203. rb.Push(RESULT_SUCCESS);
  204. rb.PushIpcInterface<IManagerForApplication>();
  205. LOG_DEBUG(Service_ACC, "called");
  206. }
  207. Module::Interface::Interface(std::shared_ptr<Module> module,
  208. std::shared_ptr<ProfileManager> profile_manager, const char* name)
  209. : ServiceFramework(name), module(std::move(module)),
  210. profile_manager(std::move(profile_manager)) {}
  211. Module::Interface::~Interface() = default;
  212. void InstallInterfaces(SM::ServiceManager& service_manager) {
  213. auto module = std::make_shared<Module>();
  214. auto profile_manager = std::make_shared<ProfileManager>();
  215. std::make_shared<ACC_AA>(module, profile_manager)->InstallAsService(service_manager);
  216. std::make_shared<ACC_SU>(module, profile_manager)->InstallAsService(service_manager);
  217. std::make_shared<ACC_U0>(module, profile_manager)->InstallAsService(service_manager);
  218. std::make_shared<ACC_U1>(module, profile_manager)->InstallAsService(service_manager);
  219. }
  220. } // namespace Service::Account