acc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // Smallest JPEG https://github.com/mathiasbynens/small/blob/master/jpeg.jpg
  22. // used as a backup should the one on disk not exist
  23. constexpr u32 backup_jpeg_size = 107;
  24. constexpr std::array<u8, backup_jpeg_size> backup_jpeg{{
  25. 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02,
  26. 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04, 0x08, 0x06, 0x06, 0x05,
  27. 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09, 0x08, 0x09, 0x09, 0x0a, 0x0c, 0x0f, 0x0c, 0x0a, 0x0b, 0x0e,
  28. 0x0b, 0x09, 0x09, 0x0d, 0x11, 0x0d, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x10, 0x0a, 0x0c, 0x12, 0x13,
  29. 0x12, 0x10, 0x13, 0x0f, 0x10, 0x10, 0x10, 0xff, 0xc9, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01,
  30. 0x01, 0x01, 0x11, 0x00, 0xff, 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, 0xff, 0xda, 0x00, 0x08,
  31. 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9,
  32. }};
  33. static std::string GetImagePath(Common::UUID uuid) {
  34. return FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
  35. "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
  36. }
  37. static constexpr u32 SanitizeJPEGSize(std::size_t size) {
  38. constexpr std::size_t max_jpeg_image_size = 0x20000;
  39. return static_cast<u32>(std::min(size, max_jpeg_image_size));
  40. }
  41. class IProfile final : public ServiceFramework<IProfile> {
  42. public:
  43. explicit IProfile(Common::UUID user_id, ProfileManager& profile_manager)
  44. : ServiceFramework("IProfile"), profile_manager(profile_manager), user_id(user_id) {
  45. static const FunctionInfo functions[] = {
  46. {0, &IProfile::Get, "Get"},
  47. {1, &IProfile::GetBase, "GetBase"},
  48. {10, &IProfile::GetImageSize, "GetImageSize"},
  49. {11, &IProfile::LoadImage, "LoadImage"},
  50. };
  51. RegisterHandlers(functions);
  52. }
  53. private:
  54. void Get(Kernel::HLERequestContext& ctx) {
  55. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  56. ProfileBase profile_base{};
  57. ProfileData data{};
  58. if (profile_manager.GetProfileBaseAndData(user_id, profile_base, data)) {
  59. std::array<u8, sizeof(ProfileData)> raw_data;
  60. std::memcpy(raw_data.data(), &data, sizeof(ProfileData));
  61. ctx.WriteBuffer(raw_data);
  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 and data for user={}",
  67. user_id.Format());
  68. IPC::ResponseBuilder rb{ctx, 2};
  69. rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code
  70. }
  71. }
  72. void GetBase(Kernel::HLERequestContext& ctx) {
  73. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  74. ProfileBase profile_base{};
  75. if (profile_manager.GetProfileBase(user_id, profile_base)) {
  76. IPC::ResponseBuilder rb{ctx, 16};
  77. rb.Push(RESULT_SUCCESS);
  78. rb.PushRaw(profile_base);
  79. } else {
  80. LOG_ERROR(Service_ACC, "Failed to get profile base for user={}", user_id.Format());
  81. IPC::ResponseBuilder rb{ctx, 2};
  82. rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code
  83. }
  84. }
  85. void LoadImage(Kernel::HLERequestContext& ctx) {
  86. LOG_DEBUG(Service_ACC, "called");
  87. IPC::ResponseBuilder rb{ctx, 3};
  88. rb.Push(RESULT_SUCCESS);
  89. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  90. if (!image.IsOpen()) {
  91. LOG_WARNING(Service_ACC,
  92. "Failed to load user provided image! Falling back to built-in backup...");
  93. ctx.WriteBuffer(backup_jpeg);
  94. rb.Push<u32>(backup_jpeg_size);
  95. return;
  96. }
  97. const u32 size = SanitizeJPEGSize(image.GetSize());
  98. std::vector<u8> buffer(size);
  99. image.ReadBytes(buffer.data(), buffer.size());
  100. ctx.WriteBuffer(buffer.data(), buffer.size());
  101. rb.Push<u32>(size);
  102. }
  103. void GetImageSize(Kernel::HLERequestContext& ctx) {
  104. LOG_DEBUG(Service_ACC, "called");
  105. IPC::ResponseBuilder rb{ctx, 3};
  106. rb.Push(RESULT_SUCCESS);
  107. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  108. if (!image.IsOpen()) {
  109. LOG_WARNING(Service_ACC,
  110. "Failed to load user provided image! Falling back to built-in backup...");
  111. rb.Push<u32>(backup_jpeg_size);
  112. } else {
  113. rb.Push<u32>(SanitizeJPEGSize(image.GetSize()));
  114. }
  115. }
  116. const ProfileManager& profile_manager;
  117. Common::UUID user_id; ///< The user id this profile refers to.
  118. };
  119. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  120. public:
  121. IManagerForApplication() : ServiceFramework("IManagerForApplication") {
  122. // clang-format off
  123. static const FunctionInfo functions[] = {
  124. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  125. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  126. {2, nullptr, "EnsureIdTokenCacheAsync"},
  127. {3, nullptr, "LoadIdTokenCache"},
  128. {130, nullptr, "GetNintendoAccountUserResourceCacheForApplication"},
  129. {150, nullptr, "CreateAuthorizationRequest"},
  130. {160, nullptr, "StoreOpenContext"},
  131. {170, nullptr, "LoadNetworkServiceLicenseKindAsync"},
  132. };
  133. // clang-format on
  134. RegisterHandlers(functions);
  135. }
  136. private:
  137. void CheckAvailability(Kernel::HLERequestContext& ctx) {
  138. LOG_WARNING(Service_ACC, "(STUBBED) called");
  139. IPC::ResponseBuilder rb{ctx, 3};
  140. rb.Push(RESULT_SUCCESS);
  141. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  142. }
  143. void GetAccountId(Kernel::HLERequestContext& ctx) {
  144. LOG_WARNING(Service_ACC, "(STUBBED) called");
  145. // Should return a nintendo account ID
  146. IPC::ResponseBuilder rb{ctx, 4};
  147. rb.Push(RESULT_SUCCESS);
  148. rb.PushRaw<u64>(1);
  149. }
  150. };
  151. void Module::Interface::GetUserCount(Kernel::HLERequestContext& ctx) {
  152. LOG_INFO(Service_ACC, "called");
  153. IPC::ResponseBuilder rb{ctx, 3};
  154. rb.Push(RESULT_SUCCESS);
  155. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  156. }
  157. void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
  158. IPC::RequestParser rp{ctx};
  159. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  160. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  161. IPC::ResponseBuilder rb{ctx, 3};
  162. rb.Push(RESULT_SUCCESS);
  163. rb.Push(profile_manager->UserExists(user_id));
  164. }
  165. void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
  166. LOG_INFO(Service_ACC, "called");
  167. ctx.WriteBuffer(profile_manager->GetAllUsers());
  168. IPC::ResponseBuilder rb{ctx, 2};
  169. rb.Push(RESULT_SUCCESS);
  170. }
  171. void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
  172. LOG_INFO(Service_ACC, "called");
  173. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  174. IPC::ResponseBuilder rb{ctx, 2};
  175. rb.Push(RESULT_SUCCESS);
  176. }
  177. void Module::Interface::GetLastOpenedUser(Kernel::HLERequestContext& ctx) {
  178. LOG_INFO(Service_ACC, "called");
  179. IPC::ResponseBuilder rb{ctx, 6};
  180. rb.Push(RESULT_SUCCESS);
  181. rb.PushRaw<Common::UUID>(profile_manager->GetLastOpenedUser());
  182. }
  183. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  184. IPC::RequestParser rp{ctx};
  185. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  186. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  187. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  188. rb.Push(RESULT_SUCCESS);
  189. rb.PushIpcInterface<IProfile>(user_id, *profile_manager);
  190. }
  191. void Module::Interface::IsUserRegistrationRequestPermitted(Kernel::HLERequestContext& ctx) {
  192. LOG_WARNING(Service_ACC, "(STUBBED) called");
  193. IPC::ResponseBuilder rb{ctx, 3};
  194. rb.Push(RESULT_SUCCESS);
  195. rb.Push(profile_manager->CanSystemRegisterUser());
  196. }
  197. void Module::Interface::InitializeApplicationInfo(Kernel::HLERequestContext& ctx) {
  198. LOG_WARNING(Service_ACC, "(STUBBED) called");
  199. IPC::ResponseBuilder rb{ctx, 2};
  200. rb.Push(RESULT_SUCCESS);
  201. }
  202. void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestContext& ctx) {
  203. LOG_DEBUG(Service_ACC, "called");
  204. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  205. rb.Push(RESULT_SUCCESS);
  206. rb.PushIpcInterface<IManagerForApplication>();
  207. }
  208. void Module::Interface::TrySelectUserWithoutInteraction(Kernel::HLERequestContext& ctx) {
  209. LOG_DEBUG(Service_ACC, "called");
  210. // A u8 is passed into this function which we can safely ignore. It's to determine if we have
  211. // access to use the network or not by the looks of it
  212. IPC::ResponseBuilder rb{ctx, 6};
  213. if (profile_manager->GetUserCount() != 1) {
  214. rb.Push(RESULT_SUCCESS);
  215. rb.PushRaw<u128>(Common::INVALID_UUID);
  216. return;
  217. }
  218. const auto user_list = profile_manager->GetAllUsers();
  219. if (std::all_of(user_list.begin(), user_list.end(),
  220. [](const auto& user) { return user.uuid == Common::INVALID_UUID; })) {
  221. rb.Push(ResultCode(-1)); // TODO(ogniK): Find the correct error code
  222. rb.PushRaw<u128>(Common::INVALID_UUID);
  223. return;
  224. }
  225. // Select the first user we have
  226. rb.Push(RESULT_SUCCESS);
  227. rb.PushRaw<u128>(profile_manager->GetUser(0)->uuid);
  228. }
  229. Module::Interface::Interface(std::shared_ptr<Module> module,
  230. std::shared_ptr<ProfileManager> profile_manager, const char* name)
  231. : ServiceFramework(name), module(std::move(module)),
  232. profile_manager(std::move(profile_manager)) {}
  233. Module::Interface::~Interface() = default;
  234. void InstallInterfaces(SM::ServiceManager& service_manager) {
  235. auto module = std::make_shared<Module>();
  236. auto profile_manager = std::make_shared<ProfileManager>();
  237. std::make_shared<ACC_AA>(module, profile_manager)->InstallAsService(service_manager);
  238. std::make_shared<ACC_SU>(module, profile_manager)->InstallAsService(service_manager);
  239. std::make_shared<ACC_U0>(module, profile_manager)->InstallAsService(service_manager);
  240. std::make_shared<ACC_U1>(module, profile_manager)->InstallAsService(service_manager);
  241. }
  242. } // namespace Service::Account