acc.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. LOG_WARNING(Service_ACC,
  98. "Failed to load user provided image! Falling back to built-in backup...");
  99. ctx.WriteBuffer(backup_jpeg);
  100. rb.Push<u32>(backup_jpeg_size);
  101. return;
  102. }
  103. const auto size = std::min<u32>(image.GetSize(), MAX_JPEG_IMAGE_SIZE);
  104. std::vector<u8> buffer(size);
  105. image.ReadBytes(buffer.data(), buffer.size());
  106. ctx.WriteBuffer(buffer.data(), buffer.size());
  107. rb.Push<u32>(buffer.size());
  108. }
  109. void GetImageSize(Kernel::HLERequestContext& ctx) {
  110. LOG_DEBUG(Service_ACC, "called");
  111. constexpr u32 backup_jpeg_size = 107;
  112. IPC::ResponseBuilder rb{ctx, 3};
  113. rb.Push(RESULT_SUCCESS);
  114. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  115. if (!image.IsOpen()) {
  116. LOG_WARNING(Service_ACC,
  117. "Failed to load user provided image! Falling back to built-in backup...");
  118. rb.Push<u32>(backup_jpeg_size);
  119. } else {
  120. rb.Push<u32>(std::min<u32>(image.GetSize(), MAX_JPEG_IMAGE_SIZE));
  121. }
  122. }
  123. const ProfileManager& profile_manager;
  124. UUID user_id; ///< The user id this profile refers to.
  125. };
  126. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  127. public:
  128. IManagerForApplication() : ServiceFramework("IManagerForApplication") {
  129. static const FunctionInfo functions[] = {
  130. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  131. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  132. {2, nullptr, "EnsureIdTokenCacheAsync"},
  133. {3, nullptr, "LoadIdTokenCache"},
  134. {130, nullptr, "GetNintendoAccountUserResourceCacheForApplication"},
  135. {150, nullptr, "CreateAuthorizationRequest"},
  136. {160, nullptr, "StoreOpenContext"},
  137. };
  138. RegisterHandlers(functions);
  139. }
  140. private:
  141. void CheckAvailability(Kernel::HLERequestContext& ctx) {
  142. LOG_WARNING(Service_ACC, "(STUBBED) called");
  143. IPC::ResponseBuilder rb{ctx, 3};
  144. rb.Push(RESULT_SUCCESS);
  145. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  146. }
  147. void GetAccountId(Kernel::HLERequestContext& ctx) {
  148. LOG_WARNING(Service_ACC, "(STUBBED) called");
  149. // Should return a nintendo account ID
  150. IPC::ResponseBuilder rb{ctx, 4};
  151. rb.Push(RESULT_SUCCESS);
  152. rb.PushRaw<u64>(1);
  153. }
  154. };
  155. void Module::Interface::GetUserCount(Kernel::HLERequestContext& ctx) {
  156. LOG_INFO(Service_ACC, "called");
  157. IPC::ResponseBuilder rb{ctx, 3};
  158. rb.Push(RESULT_SUCCESS);
  159. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  160. }
  161. void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
  162. IPC::RequestParser rp{ctx};
  163. UUID user_id = rp.PopRaw<UUID>();
  164. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  165. IPC::ResponseBuilder rb{ctx, 3};
  166. rb.Push(RESULT_SUCCESS);
  167. rb.Push(profile_manager->UserExists(user_id));
  168. }
  169. void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
  170. LOG_INFO(Service_ACC, "called");
  171. ctx.WriteBuffer(profile_manager->GetAllUsers());
  172. IPC::ResponseBuilder rb{ctx, 2};
  173. rb.Push(RESULT_SUCCESS);
  174. }
  175. void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
  176. LOG_INFO(Service_ACC, "called");
  177. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  178. IPC::ResponseBuilder rb{ctx, 2};
  179. rb.Push(RESULT_SUCCESS);
  180. }
  181. void Module::Interface::GetLastOpenedUser(Kernel::HLERequestContext& ctx) {
  182. LOG_INFO(Service_ACC, "called");
  183. IPC::ResponseBuilder rb{ctx, 6};
  184. rb.Push(RESULT_SUCCESS);
  185. rb.PushRaw<UUID>(profile_manager->GetLastOpenedUser());
  186. }
  187. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  188. IPC::RequestParser rp{ctx};
  189. UUID user_id = rp.PopRaw<UUID>();
  190. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  191. rb.Push(RESULT_SUCCESS);
  192. rb.PushIpcInterface<IProfile>(user_id, *profile_manager);
  193. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  194. }
  195. void Module::Interface::IsUserRegistrationRequestPermitted(Kernel::HLERequestContext& ctx) {
  196. LOG_WARNING(Service_ACC, "(STUBBED) called");
  197. IPC::ResponseBuilder rb{ctx, 3};
  198. rb.Push(RESULT_SUCCESS);
  199. rb.Push(profile_manager->CanSystemRegisterUser());
  200. }
  201. void Module::Interface::InitializeApplicationInfo(Kernel::HLERequestContext& ctx) {
  202. LOG_WARNING(Service_ACC, "(STUBBED) called");
  203. IPC::ResponseBuilder rb{ctx, 2};
  204. rb.Push(RESULT_SUCCESS);
  205. }
  206. void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestContext& ctx) {
  207. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  208. rb.Push(RESULT_SUCCESS);
  209. rb.PushIpcInterface<IManagerForApplication>();
  210. LOG_DEBUG(Service_ACC, "called");
  211. }
  212. Module::Interface::Interface(std::shared_ptr<Module> module,
  213. std::shared_ptr<ProfileManager> profile_manager, const char* name)
  214. : ServiceFramework(name), module(std::move(module)),
  215. profile_manager(std::move(profile_manager)) {}
  216. Module::Interface::~Interface() = default;
  217. void InstallInterfaces(SM::ServiceManager& service_manager) {
  218. auto module = std::make_shared<Module>();
  219. auto profile_manager = std::make_shared<ProfileManager>();
  220. std::make_shared<ACC_AA>(module, profile_manager)->InstallAsService(service_manager);
  221. std::make_shared<ACC_SU>(module, profile_manager)->InstallAsService(service_manager);
  222. std::make_shared<ACC_U0>(module, profile_manager)->InstallAsService(service_manager);
  223. std::make_shared<ACC_U1>(module, profile_manager)->InstallAsService(service_manager);
  224. }
  225. } // namespace Service::Account