acc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/constants.h"
  13. #include "core/core_timing.h"
  14. #include "core/file_sys/control_metadata.h"
  15. #include "core/file_sys/patch_manager.h"
  16. #include "core/hle/ipc_helpers.h"
  17. #include "core/hle/kernel/process.h"
  18. #include "core/hle/service/acc/acc.h"
  19. #include "core/hle/service/acc/acc_aa.h"
  20. #include "core/hle/service/acc/acc_su.h"
  21. #include "core/hle/service/acc/acc_u0.h"
  22. #include "core/hle/service/acc/acc_u1.h"
  23. #include "core/hle/service/acc/profile_manager.h"
  24. #include "core/loader/loader.h"
  25. namespace Service::Account {
  26. static std::string GetImagePath(Common::UUID uuid) {
  27. return FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
  28. "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
  29. }
  30. static constexpr u32 SanitizeJPEGSize(std::size_t size) {
  31. constexpr std::size_t max_jpeg_image_size = 0x20000;
  32. return static_cast<u32>(std::min(size, max_jpeg_image_size));
  33. }
  34. class IProfile final : public ServiceFramework<IProfile> {
  35. public:
  36. explicit IProfile(Common::UUID user_id, ProfileManager& profile_manager)
  37. : ServiceFramework("IProfile"), profile_manager(profile_manager), user_id(user_id) {
  38. static const FunctionInfo functions[] = {
  39. {0, &IProfile::Get, "Get"},
  40. {1, &IProfile::GetBase, "GetBase"},
  41. {10, &IProfile::GetImageSize, "GetImageSize"},
  42. {11, &IProfile::LoadImage, "LoadImage"},
  43. };
  44. RegisterHandlers(functions);
  45. }
  46. private:
  47. void Get(Kernel::HLERequestContext& ctx) {
  48. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  49. ProfileBase profile_base{};
  50. ProfileData data{};
  51. if (profile_manager.GetProfileBaseAndData(user_id, profile_base, data)) {
  52. std::array<u8, sizeof(ProfileData)> raw_data;
  53. std::memcpy(raw_data.data(), &data, sizeof(ProfileData));
  54. ctx.WriteBuffer(raw_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. IPC::ResponseBuilder rb{ctx, 3};
  81. rb.Push(RESULT_SUCCESS);
  82. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  83. if (!image.IsOpen()) {
  84. LOG_WARNING(Service_ACC,
  85. "Failed to load user provided image! Falling back to built-in backup...");
  86. ctx.WriteBuffer(Core::Constants::ACCOUNT_BACKUP_JPEG);
  87. rb.Push<u32>(Core::Constants::ACCOUNT_BACKUP_JPEG.size());
  88. return;
  89. }
  90. const u32 size = SanitizeJPEGSize(image.GetSize());
  91. std::vector<u8> buffer(size);
  92. image.ReadBytes(buffer.data(), buffer.size());
  93. ctx.WriteBuffer(buffer.data(), buffer.size());
  94. rb.Push<u32>(size);
  95. }
  96. void GetImageSize(Kernel::HLERequestContext& ctx) {
  97. LOG_DEBUG(Service_ACC, "called");
  98. IPC::ResponseBuilder rb{ctx, 3};
  99. rb.Push(RESULT_SUCCESS);
  100. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  101. if (!image.IsOpen()) {
  102. LOG_WARNING(Service_ACC,
  103. "Failed to load user provided image! Falling back to built-in backup...");
  104. rb.Push<u32>(Core::Constants::ACCOUNT_BACKUP_JPEG.size());
  105. } else {
  106. rb.Push<u32>(SanitizeJPEGSize(image.GetSize()));
  107. }
  108. }
  109. const ProfileManager& profile_manager;
  110. Common::UUID user_id; ///< The user id this profile refers to.
  111. };
  112. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  113. public:
  114. IManagerForApplication() : ServiceFramework("IManagerForApplication") {
  115. // clang-format off
  116. static const FunctionInfo functions[] = {
  117. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  118. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  119. {2, nullptr, "EnsureIdTokenCacheAsync"},
  120. {3, nullptr, "LoadIdTokenCache"},
  121. {130, nullptr, "GetNintendoAccountUserResourceCacheForApplication"},
  122. {150, nullptr, "CreateAuthorizationRequest"},
  123. {160, nullptr, "StoreOpenContext"},
  124. {170, nullptr, "LoadNetworkServiceLicenseKindAsync"},
  125. };
  126. // clang-format on
  127. RegisterHandlers(functions);
  128. }
  129. private:
  130. void CheckAvailability(Kernel::HLERequestContext& ctx) {
  131. LOG_WARNING(Service_ACC, "(STUBBED) called");
  132. IPC::ResponseBuilder rb{ctx, 3};
  133. rb.Push(RESULT_SUCCESS);
  134. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  135. }
  136. void GetAccountId(Kernel::HLERequestContext& ctx) {
  137. LOG_WARNING(Service_ACC, "(STUBBED) called");
  138. // Should return a nintendo account ID
  139. IPC::ResponseBuilder rb{ctx, 4};
  140. rb.Push(RESULT_SUCCESS);
  141. rb.PushRaw<u64>(1);
  142. }
  143. };
  144. void Module::Interface::GetUserCount(Kernel::HLERequestContext& ctx) {
  145. LOG_INFO(Service_ACC, "called");
  146. IPC::ResponseBuilder rb{ctx, 3};
  147. rb.Push(RESULT_SUCCESS);
  148. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  149. }
  150. void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
  151. IPC::RequestParser rp{ctx};
  152. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  153. LOG_INFO(Service_ACC, "called user_id={}", user_id.Format());
  154. IPC::ResponseBuilder rb{ctx, 3};
  155. rb.Push(RESULT_SUCCESS);
  156. rb.Push(profile_manager->UserExists(user_id));
  157. }
  158. void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
  159. LOG_INFO(Service_ACC, "called");
  160. ctx.WriteBuffer(profile_manager->GetAllUsers());
  161. IPC::ResponseBuilder rb{ctx, 2};
  162. rb.Push(RESULT_SUCCESS);
  163. }
  164. void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
  165. LOG_INFO(Service_ACC, "called");
  166. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  167. IPC::ResponseBuilder rb{ctx, 2};
  168. rb.Push(RESULT_SUCCESS);
  169. }
  170. void Module::Interface::GetLastOpenedUser(Kernel::HLERequestContext& ctx) {
  171. LOG_INFO(Service_ACC, "called");
  172. IPC::ResponseBuilder rb{ctx, 6};
  173. rb.Push(RESULT_SUCCESS);
  174. rb.PushRaw<Common::UUID>(profile_manager->GetLastOpenedUser());
  175. }
  176. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  177. IPC::RequestParser rp{ctx};
  178. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  179. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  180. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  181. rb.Push(RESULT_SUCCESS);
  182. rb.PushIpcInterface<IProfile>(user_id, *profile_manager);
  183. }
  184. void Module::Interface::IsUserRegistrationRequestPermitted(Kernel::HLERequestContext& ctx) {
  185. LOG_WARNING(Service_ACC, "(STUBBED) called");
  186. IPC::ResponseBuilder rb{ctx, 3};
  187. rb.Push(RESULT_SUCCESS);
  188. rb.Push(profile_manager->CanSystemRegisterUser());
  189. }
  190. void Module::Interface::InitializeApplicationInfoOld(Kernel::HLERequestContext& ctx) {
  191. LOG_WARNING(Service_ACC, "(STUBBED) called");
  192. IPC::ResponseBuilder rb{ctx, 2};
  193. rb.Push(RESULT_SUCCESS);
  194. }
  195. void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestContext& ctx) {
  196. LOG_DEBUG(Service_ACC, "called");
  197. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  198. rb.Push(RESULT_SUCCESS);
  199. rb.PushIpcInterface<IManagerForApplication>();
  200. }
  201. void Module::Interface::IsUserAccountSwitchLocked(Kernel::HLERequestContext& ctx) {
  202. LOG_DEBUG(Service_ACC, "called");
  203. FileSys::NACP nacp;
  204. const auto res = system.GetAppLoader().ReadControlData(nacp);
  205. bool is_locked = false;
  206. if (res != Loader::ResultStatus::Success) {
  207. FileSys::PatchManager pm{system.CurrentProcess()->GetTitleID()};
  208. auto nacp_unique = pm.GetControlMetadata().first;
  209. if (nacp_unique != nullptr) {
  210. is_locked = nacp_unique->GetUserAccountSwitchLock();
  211. }
  212. } else {
  213. is_locked = nacp.GetUserAccountSwitchLock();
  214. }
  215. IPC::ResponseBuilder rb{ctx, 3};
  216. rb.Push(RESULT_SUCCESS);
  217. rb.Push(is_locked);
  218. }
  219. void Module::Interface::TrySelectUserWithoutInteraction(Kernel::HLERequestContext& ctx) {
  220. LOG_DEBUG(Service_ACC, "called");
  221. // A u8 is passed into this function which we can safely ignore. It's to determine if we have
  222. // access to use the network or not by the looks of it
  223. IPC::ResponseBuilder rb{ctx, 6};
  224. if (profile_manager->GetUserCount() != 1) {
  225. rb.Push(RESULT_SUCCESS);
  226. rb.PushRaw<u128>(Common::INVALID_UUID);
  227. return;
  228. }
  229. const auto user_list = profile_manager->GetAllUsers();
  230. if (std::all_of(user_list.begin(), user_list.end(),
  231. [](const auto& user) { return user.uuid == Common::INVALID_UUID; })) {
  232. rb.Push(ResultCode(-1)); // TODO(ogniK): Find the correct error code
  233. rb.PushRaw<u128>(Common::INVALID_UUID);
  234. return;
  235. }
  236. // Select the first user we have
  237. rb.Push(RESULT_SUCCESS);
  238. rb.PushRaw<u128>(profile_manager->GetUser(0)->uuid);
  239. }
  240. Module::Interface::Interface(std::shared_ptr<Module> module,
  241. std::shared_ptr<ProfileManager> profile_manager, Core::System& system,
  242. const char* name)
  243. : ServiceFramework(name), module(std::move(module)),
  244. profile_manager(std::move(profile_manager)), system(system) {}
  245. Module::Interface::~Interface() = default;
  246. void InstallInterfaces(SM::ServiceManager& service_manager) {
  247. auto module = std::make_shared<Module>();
  248. auto profile_manager = std::make_shared<ProfileManager>();
  249. Core::System& system = Core::System::GetInstance();
  250. std::make_shared<ACC_AA>(module, profile_manager, system)->InstallAsService(service_manager);
  251. std::make_shared<ACC_SU>(module, profile_manager, system)->InstallAsService(service_manager);
  252. std::make_shared<ACC_U0>(module, profile_manager, system)->InstallAsService(service_manager);
  253. std::make_shared<ACC_U1>(module, profile_manager, system)->InstallAsService(service_manager);
  254. }
  255. } // namespace Service::Account