acc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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/kernel.h"
  18. #include "core/hle/kernel/process.h"
  19. #include "core/hle/service/acc/acc.h"
  20. #include "core/hle/service/acc/acc_aa.h"
  21. #include "core/hle/service/acc/acc_su.h"
  22. #include "core/hle/service/acc/acc_u0.h"
  23. #include "core/hle/service/acc/acc_u1.h"
  24. #include "core/hle/service/acc/errors.h"
  25. #include "core/hle/service/acc/profile_manager.h"
  26. #include "core/hle/service/glue/arp.h"
  27. #include "core/hle/service/glue/manager.h"
  28. #include "core/hle/service/sm/sm.h"
  29. #include "core/loader/loader.h"
  30. namespace Service::Account {
  31. constexpr ResultCode ERR_INVALID_BUFFER_SIZE{ErrorModule::Account, 30};
  32. constexpr ResultCode ERR_FAILED_SAVE_DATA{ErrorModule::Account, 100};
  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 IProfileCommon : public ServiceFramework<IProfileCommon> {
  42. public:
  43. explicit IProfileCommon(const char* name, bool editor_commands, Common::UUID user_id,
  44. ProfileManager& profile_manager)
  45. : ServiceFramework(name), profile_manager(profile_manager), user_id(user_id) {
  46. static const FunctionInfo functions[] = {
  47. {0, &IProfileCommon::Get, "Get"},
  48. {1, &IProfileCommon::GetBase, "GetBase"},
  49. {10, &IProfileCommon::GetImageSize, "GetImageSize"},
  50. {11, &IProfileCommon::LoadImage, "LoadImage"},
  51. };
  52. RegisterHandlers(functions);
  53. if (editor_commands) {
  54. static const FunctionInfo editor_functions[] = {
  55. {100, &IProfileCommon::Store, "Store"},
  56. {101, &IProfileCommon::StoreWithImage, "StoreWithImage"},
  57. };
  58. RegisterHandlers(editor_functions);
  59. }
  60. }
  61. protected:
  62. void Get(Kernel::HLERequestContext& ctx) {
  63. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  64. ProfileBase profile_base{};
  65. ProfileData data{};
  66. if (profile_manager.GetProfileBaseAndData(user_id, profile_base, data)) {
  67. std::array<u8, sizeof(ProfileData)> raw_data;
  68. std::memcpy(raw_data.data(), &data, sizeof(ProfileData));
  69. ctx.WriteBuffer(raw_data);
  70. IPC::ResponseBuilder rb{ctx, 16};
  71. rb.Push(RESULT_SUCCESS);
  72. rb.PushRaw(profile_base);
  73. } else {
  74. LOG_ERROR(Service_ACC, "Failed to get profile base and data for user={}",
  75. user_id.Format());
  76. IPC::ResponseBuilder rb{ctx, 2};
  77. rb.Push(RESULT_UNKNOWN); // TODO(ogniK): Get actual error code
  78. }
  79. }
  80. void GetBase(Kernel::HLERequestContext& ctx) {
  81. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  82. ProfileBase profile_base{};
  83. if (profile_manager.GetProfileBase(user_id, profile_base)) {
  84. IPC::ResponseBuilder rb{ctx, 16};
  85. rb.Push(RESULT_SUCCESS);
  86. rb.PushRaw(profile_base);
  87. } else {
  88. LOG_ERROR(Service_ACC, "Failed to get profile base for user={}", user_id.Format());
  89. IPC::ResponseBuilder rb{ctx, 2};
  90. rb.Push(RESULT_UNKNOWN); // TODO(ogniK): Get actual error code
  91. }
  92. }
  93. void LoadImage(Kernel::HLERequestContext& ctx) {
  94. LOG_DEBUG(Service_ACC, "called");
  95. IPC::ResponseBuilder rb{ctx, 3};
  96. rb.Push(RESULT_SUCCESS);
  97. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  98. if (!image.IsOpen()) {
  99. LOG_WARNING(Service_ACC,
  100. "Failed to load user provided image! Falling back to built-in backup...");
  101. ctx.WriteBuffer(Core::Constants::ACCOUNT_BACKUP_JPEG);
  102. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  103. return;
  104. }
  105. const u32 size = SanitizeJPEGSize(image.GetSize());
  106. std::vector<u8> buffer(size);
  107. image.ReadBytes(buffer.data(), buffer.size());
  108. ctx.WriteBuffer(buffer.data(), buffer.size());
  109. rb.Push<u32>(size);
  110. }
  111. void GetImageSize(Kernel::HLERequestContext& ctx) {
  112. LOG_DEBUG(Service_ACC, "called");
  113. IPC::ResponseBuilder rb{ctx, 3};
  114. rb.Push(RESULT_SUCCESS);
  115. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  116. if (!image.IsOpen()) {
  117. LOG_WARNING(Service_ACC,
  118. "Failed to load user provided image! Falling back to built-in backup...");
  119. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  120. } else {
  121. rb.Push(SanitizeJPEGSize(image.GetSize()));
  122. }
  123. }
  124. void Store(Kernel::HLERequestContext& ctx) {
  125. IPC::RequestParser rp{ctx};
  126. const auto base = rp.PopRaw<ProfileBase>();
  127. const auto user_data = ctx.ReadBuffer();
  128. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid={}",
  129. Common::StringFromFixedZeroTerminatedBuffer(
  130. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  131. base.timestamp, base.user_uuid.Format());
  132. if (user_data.size() < sizeof(ProfileData)) {
  133. LOG_ERROR(Service_ACC, "ProfileData buffer too small!");
  134. IPC::ResponseBuilder rb{ctx, 2};
  135. rb.Push(ERR_INVALID_BUFFER_SIZE);
  136. return;
  137. }
  138. ProfileData data;
  139. std::memcpy(&data, user_data.data(), sizeof(ProfileData));
  140. if (!profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  141. LOG_ERROR(Service_ACC, "Failed to update profile data and base!");
  142. IPC::ResponseBuilder rb{ctx, 2};
  143. rb.Push(ERR_FAILED_SAVE_DATA);
  144. return;
  145. }
  146. IPC::ResponseBuilder rb{ctx, 2};
  147. rb.Push(RESULT_SUCCESS);
  148. }
  149. void StoreWithImage(Kernel::HLERequestContext& ctx) {
  150. IPC::RequestParser rp{ctx};
  151. const auto base = rp.PopRaw<ProfileBase>();
  152. const auto user_data = ctx.ReadBuffer();
  153. const auto image_data = ctx.ReadBuffer(1);
  154. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid={}",
  155. Common::StringFromFixedZeroTerminatedBuffer(
  156. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  157. base.timestamp, base.user_uuid.Format());
  158. if (user_data.size() < sizeof(ProfileData)) {
  159. LOG_ERROR(Service_ACC, "ProfileData buffer too small!");
  160. IPC::ResponseBuilder rb{ctx, 2};
  161. rb.Push(ERR_INVALID_BUFFER_SIZE);
  162. return;
  163. }
  164. ProfileData data;
  165. std::memcpy(&data, user_data.data(), sizeof(ProfileData));
  166. FileUtil::IOFile image(GetImagePath(user_id), "wb");
  167. if (!image.IsOpen() || !image.Resize(image_data.size()) ||
  168. image.WriteBytes(image_data.data(), image_data.size()) != image_data.size() ||
  169. !profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  170. LOG_ERROR(Service_ACC, "Failed to update profile data, base, and image!");
  171. IPC::ResponseBuilder rb{ctx, 2};
  172. rb.Push(ERR_FAILED_SAVE_DATA);
  173. return;
  174. }
  175. IPC::ResponseBuilder rb{ctx, 2};
  176. rb.Push(RESULT_SUCCESS);
  177. }
  178. ProfileManager& profile_manager;
  179. Common::UUID user_id{Common::INVALID_UUID}; ///< The user id this profile refers to.
  180. };
  181. class IProfile final : public IProfileCommon {
  182. public:
  183. IProfile(Common::UUID user_id, ProfileManager& profile_manager)
  184. : IProfileCommon("IProfile", false, user_id, profile_manager) {}
  185. };
  186. class IProfileEditor final : public IProfileCommon {
  187. public:
  188. IProfileEditor(Common::UUID user_id, ProfileManager& profile_manager)
  189. : IProfileCommon("IProfileEditor", true, user_id, profile_manager) {}
  190. };
  191. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  192. public:
  193. explicit IManagerForApplication(Common::UUID user_id)
  194. : ServiceFramework("IManagerForApplication"), user_id(user_id) {
  195. // clang-format off
  196. static const FunctionInfo functions[] = {
  197. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  198. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  199. {2, nullptr, "EnsureIdTokenCacheAsync"},
  200. {3, nullptr, "LoadIdTokenCache"},
  201. {130, nullptr, "GetNintendoAccountUserResourceCacheForApplication"},
  202. {150, nullptr, "CreateAuthorizationRequest"},
  203. {160, nullptr, "StoreOpenContext"},
  204. {170, nullptr, "LoadNetworkServiceLicenseKindAsync"},
  205. };
  206. // clang-format on
  207. RegisterHandlers(functions);
  208. }
  209. private:
  210. void CheckAvailability(Kernel::HLERequestContext& ctx) {
  211. LOG_WARNING(Service_ACC, "(STUBBED) called");
  212. IPC::ResponseBuilder rb{ctx, 3};
  213. rb.Push(RESULT_SUCCESS);
  214. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  215. }
  216. void GetAccountId(Kernel::HLERequestContext& ctx) {
  217. LOG_DEBUG(Service_ACC, "called");
  218. IPC::ResponseBuilder rb{ctx, 4};
  219. rb.Push(RESULT_SUCCESS);
  220. rb.PushRaw<u64>(user_id.GetNintendoID());
  221. }
  222. Common::UUID user_id;
  223. };
  224. void Module::Interface::GetUserCount(Kernel::HLERequestContext& ctx) {
  225. LOG_DEBUG(Service_ACC, "called");
  226. IPC::ResponseBuilder rb{ctx, 3};
  227. rb.Push(RESULT_SUCCESS);
  228. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  229. }
  230. void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
  231. IPC::RequestParser rp{ctx};
  232. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  233. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  234. IPC::ResponseBuilder rb{ctx, 3};
  235. rb.Push(RESULT_SUCCESS);
  236. rb.Push(profile_manager->UserExists(user_id));
  237. }
  238. void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
  239. LOG_DEBUG(Service_ACC, "called");
  240. ctx.WriteBuffer(profile_manager->GetAllUsers());
  241. IPC::ResponseBuilder rb{ctx, 2};
  242. rb.Push(RESULT_SUCCESS);
  243. }
  244. void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
  245. LOG_DEBUG(Service_ACC, "called");
  246. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  247. IPC::ResponseBuilder rb{ctx, 2};
  248. rb.Push(RESULT_SUCCESS);
  249. }
  250. void Module::Interface::GetLastOpenedUser(Kernel::HLERequestContext& ctx) {
  251. LOG_DEBUG(Service_ACC, "called");
  252. IPC::ResponseBuilder rb{ctx, 6};
  253. rb.Push(RESULT_SUCCESS);
  254. rb.PushRaw<Common::UUID>(profile_manager->GetLastOpenedUser());
  255. }
  256. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  257. IPC::RequestParser rp{ctx};
  258. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  259. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  260. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  261. rb.Push(RESULT_SUCCESS);
  262. rb.PushIpcInterface<IProfile>(user_id, *profile_manager);
  263. }
  264. void Module::Interface::IsUserRegistrationRequestPermitted(Kernel::HLERequestContext& ctx) {
  265. LOG_WARNING(Service_ACC, "(STUBBED) called");
  266. IPC::ResponseBuilder rb{ctx, 3};
  267. rb.Push(RESULT_SUCCESS);
  268. rb.Push(profile_manager->CanSystemRegisterUser());
  269. }
  270. void Module::Interface::InitializeApplicationInfo(Kernel::HLERequestContext& ctx) {
  271. IPC::RequestParser rp{ctx};
  272. auto pid = rp.Pop<u64>();
  273. LOG_DEBUG(Service_ACC, "called, process_id={}", pid);
  274. IPC::ResponseBuilder rb{ctx, 2};
  275. rb.Push(InitializeApplicationInfoBase(pid));
  276. }
  277. void Module::Interface::InitializeApplicationInfoRestricted(Kernel::HLERequestContext& ctx) {
  278. IPC::RequestParser rp{ctx};
  279. auto pid = rp.Pop<u64>();
  280. LOG_WARNING(Service_ACC, "(Partial implementation) called, process_id={}", pid);
  281. // TODO(ogniK): We require checking if the user actually owns the title and what not. As of
  282. // currently, we assume the user owns the title. InitializeApplicationInfoBase SHOULD be called
  283. // first then we do extra checks if the game is a digital copy.
  284. IPC::ResponseBuilder rb{ctx, 2};
  285. rb.Push(InitializeApplicationInfoBase(pid));
  286. }
  287. ResultCode Module::Interface::InitializeApplicationInfoBase(u64 process_id) {
  288. if (application_info) {
  289. LOG_ERROR(Service_ACC, "Application already initialized");
  290. return ERR_ACCOUNTINFO_ALREADY_INITIALIZED;
  291. }
  292. const auto& list = system.Kernel().GetProcessList();
  293. const auto iter = std::find_if(list.begin(), list.end(), [&process_id](const auto& process) {
  294. return process->GetProcessID() == process_id;
  295. });
  296. if (iter == list.end()) {
  297. LOG_ERROR(Service_ACC, "Failed to find process ID");
  298. application_info.application_type = ApplicationType::Unknown;
  299. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  300. }
  301. const auto launch_property = system.GetARPManager().GetLaunchProperty((*iter)->GetTitleID());
  302. if (launch_property.Failed()) {
  303. LOG_ERROR(Service_ACC, "Failed to get launch property");
  304. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  305. }
  306. switch (launch_property->base_game_storage_id) {
  307. case FileSys::StorageId::GameCard:
  308. application_info.application_type = ApplicationType::GameCard;
  309. break;
  310. case FileSys::StorageId::Host:
  311. case FileSys::StorageId::NandUser:
  312. case FileSys::StorageId::SdCard:
  313. application_info.application_type = ApplicationType::Digital;
  314. break;
  315. default:
  316. LOG_ERROR(Service_ACC, "Invalid game storage ID");
  317. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  318. }
  319. LOG_WARNING(Service_ACC, "ApplicationInfo init required");
  320. // TODO(ogniK): Actual initalization here
  321. return RESULT_SUCCESS;
  322. }
  323. void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestContext& ctx) {
  324. LOG_DEBUG(Service_ACC, "called");
  325. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  326. rb.Push(RESULT_SUCCESS);
  327. rb.PushIpcInterface<IManagerForApplication>(profile_manager->GetLastOpenedUser());
  328. }
  329. void Module::Interface::IsUserAccountSwitchLocked(Kernel::HLERequestContext& ctx) {
  330. LOG_DEBUG(Service_ACC, "called");
  331. FileSys::NACP nacp;
  332. const auto res = system.GetAppLoader().ReadControlData(nacp);
  333. bool is_locked = false;
  334. if (res != Loader::ResultStatus::Success) {
  335. FileSys::PatchManager pm{system.CurrentProcess()->GetTitleID()};
  336. auto nacp_unique = pm.GetControlMetadata().first;
  337. if (nacp_unique != nullptr) {
  338. is_locked = nacp_unique->GetUserAccountSwitchLock();
  339. } else {
  340. LOG_ERROR(Service_ACC, "nacp_unique is null!");
  341. }
  342. } else {
  343. is_locked = nacp.GetUserAccountSwitchLock();
  344. }
  345. IPC::ResponseBuilder rb{ctx, 3};
  346. rb.Push(RESULT_SUCCESS);
  347. rb.Push(is_locked);
  348. }
  349. void Module::Interface::GetProfileEditor(Kernel::HLERequestContext& ctx) {
  350. IPC::RequestParser rp{ctx};
  351. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  352. LOG_DEBUG(Service_ACC, "called, user_id={}", user_id.Format());
  353. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  354. rb.Push(RESULT_SUCCESS);
  355. rb.PushIpcInterface<IProfileEditor>(user_id, *profile_manager);
  356. }
  357. void Module::Interface::TrySelectUserWithoutInteraction(Kernel::HLERequestContext& ctx) {
  358. LOG_DEBUG(Service_ACC, "called");
  359. // A u8 is passed into this function which we can safely ignore. It's to determine if we have
  360. // access to use the network or not by the looks of it
  361. IPC::ResponseBuilder rb{ctx, 6};
  362. if (profile_manager->GetUserCount() != 1) {
  363. rb.Push(RESULT_SUCCESS);
  364. rb.PushRaw<u128>(Common::INVALID_UUID);
  365. return;
  366. }
  367. const auto user_list = profile_manager->GetAllUsers();
  368. if (std::all_of(user_list.begin(), user_list.end(),
  369. [](const auto& user) { return user.uuid == Common::INVALID_UUID; })) {
  370. rb.Push(RESULT_UNKNOWN); // TODO(ogniK): Find the correct error code
  371. rb.PushRaw<u128>(Common::INVALID_UUID);
  372. return;
  373. }
  374. // Select the first user we have
  375. rb.Push(RESULT_SUCCESS);
  376. rb.PushRaw<u128>(profile_manager->GetUser(0)->uuid);
  377. }
  378. Module::Interface::Interface(std::shared_ptr<Module> module,
  379. std::shared_ptr<ProfileManager> profile_manager, Core::System& system,
  380. const char* name)
  381. : ServiceFramework(name), module(std::move(module)),
  382. profile_manager(std::move(profile_manager)), system(system) {}
  383. Module::Interface::~Interface() = default;
  384. void InstallInterfaces(Core::System& system) {
  385. auto module = std::make_shared<Module>();
  386. auto profile_manager = std::make_shared<ProfileManager>();
  387. std::make_shared<ACC_AA>(module, profile_manager, system)
  388. ->InstallAsService(system.ServiceManager());
  389. std::make_shared<ACC_SU>(module, profile_manager, system)
  390. ->InstallAsService(system.ServiceManager());
  391. std::make_shared<ACC_U0>(module, profile_manager, system)
  392. ->InstallAsService(system.ServiceManager());
  393. std::make_shared<ACC_U1>(module, profile_manager, system)
  394. ->InstallAsService(system.ServiceManager());
  395. }
  396. } // namespace Service::Account