acc.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include "common/common_types.h"
  6. #include "common/fs/file.h"
  7. #include "common/fs/path_util.h"
  8. #include "common/logging/log.h"
  9. #include "common/polyfill_ranges.h"
  10. #include "common/string_util.h"
  11. #include "common/swap.h"
  12. #include "core/constants.h"
  13. #include "core/core.h"
  14. #include "core/core_timing.h"
  15. #include "core/file_sys/control_metadata.h"
  16. #include "core/file_sys/patch_manager.h"
  17. #include "core/hle/ipc_helpers.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/async_context.h"
  24. #include "core/hle/service/acc/errors.h"
  25. #include "core/hle/service/acc/profile_manager.h"
  26. #include "core/hle/service/glue/glue_manager.h"
  27. #include "core/loader/loader.h"
  28. namespace Service::Account {
  29. constexpr Result ERR_INVALID_USER_ID{ErrorModule::Account, 20};
  30. constexpr Result ERR_INVALID_APPLICATION_ID{ErrorModule::Account, 22};
  31. constexpr Result ERR_INVALID_BUFFER{ErrorModule::Account, 30};
  32. constexpr Result ERR_INVALID_BUFFER_SIZE{ErrorModule::Account, 31};
  33. constexpr Result ERR_FAILED_SAVE_DATA{ErrorModule::Account, 100};
  34. // Thumbnails are hard coded to be at least this size
  35. constexpr std::size_t THUMBNAIL_SIZE = 0x24000;
  36. static std::filesystem::path GetImagePath(const Common::UUID& uuid) {
  37. return Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir) /
  38. fmt::format("system/save/8000000000000010/su/avators/{}.jpg", uuid.FormattedString());
  39. }
  40. static constexpr u32 SanitizeJPEGSize(std::size_t size) {
  41. constexpr std::size_t max_jpeg_image_size = 0x20000;
  42. return static_cast<u32>(std::min(size, max_jpeg_image_size));
  43. }
  44. class IManagerForSystemService final : public ServiceFramework<IManagerForSystemService> {
  45. public:
  46. explicit IManagerForSystemService(Core::System& system_, Common::UUID)
  47. : ServiceFramework{system_, "IManagerForSystemService"} {
  48. // clang-format off
  49. static const FunctionInfo functions[] = {
  50. {0, nullptr, "CheckAvailability"},
  51. {1, nullptr, "GetAccountId"},
  52. {2, nullptr, "EnsureIdTokenCacheAsync"},
  53. {3, nullptr, "LoadIdTokenCache"},
  54. {100, nullptr, "SetSystemProgramIdentification"},
  55. {101, nullptr, "RefreshNotificationTokenAsync"}, // 7.0.0+
  56. {110, nullptr, "GetServiceEntryRequirementCache"}, // 4.0.0+
  57. {111, nullptr, "InvalidateServiceEntryRequirementCache"}, // 4.0.0+
  58. {112, nullptr, "InvalidateTokenCache"}, // 4.0.0 - 6.2.0
  59. {113, nullptr, "GetServiceEntryRequirementCacheForOnlinePlay"}, // 6.1.0+
  60. {120, nullptr, "GetNintendoAccountId"},
  61. {121, nullptr, "CalculateNintendoAccountAuthenticationFingerprint"}, // 9.0.0+
  62. {130, nullptr, "GetNintendoAccountUserResourceCache"},
  63. {131, nullptr, "RefreshNintendoAccountUserResourceCacheAsync"},
  64. {132, nullptr, "RefreshNintendoAccountUserResourceCacheAsyncIfSecondsElapsed"},
  65. {133, nullptr, "GetNintendoAccountVerificationUrlCache"}, // 9.0.0+
  66. {134, nullptr, "RefreshNintendoAccountVerificationUrlCache"}, // 9.0.0+
  67. {135, nullptr, "RefreshNintendoAccountVerificationUrlCacheAsyncIfSecondsElapsed"}, // 9.0.0+
  68. {140, nullptr, "GetNetworkServiceLicenseCache"}, // 5.0.0+
  69. {141, nullptr, "RefreshNetworkServiceLicenseCacheAsync"}, // 5.0.0+
  70. {142, nullptr, "RefreshNetworkServiceLicenseCacheAsyncIfSecondsElapsed"}, // 5.0.0+
  71. {150, nullptr, "CreateAuthorizationRequest"},
  72. };
  73. // clang-format on
  74. RegisterHandlers(functions);
  75. }
  76. };
  77. // 3.0.0+
  78. class IFloatingRegistrationRequest final : public ServiceFramework<IFloatingRegistrationRequest> {
  79. public:
  80. explicit IFloatingRegistrationRequest(Core::System& system_, Common::UUID)
  81. : ServiceFramework{system_, "IFloatingRegistrationRequest"} {
  82. // clang-format off
  83. static const FunctionInfo functions[] = {
  84. {0, nullptr, "GetSessionId"},
  85. {12, nullptr, "GetAccountId"},
  86. {13, nullptr, "GetLinkedNintendoAccountId"},
  87. {14, nullptr, "GetNickname"},
  88. {15, nullptr, "GetProfileImage"},
  89. {21, nullptr, "LoadIdTokenCache"},
  90. {100, nullptr, "RegisterUser"}, // [1.0.0-3.0.2] RegisterAsync
  91. {101, nullptr, "RegisterUserWithUid"}, // [1.0.0-3.0.2] RegisterWithUidAsync
  92. {102, nullptr, "RegisterNetworkServiceAccountAsync"}, // 4.0.0+
  93. {103, nullptr, "RegisterNetworkServiceAccountWithUidAsync"}, // 4.0.0+
  94. {110, nullptr, "SetSystemProgramIdentification"},
  95. {111, nullptr, "EnsureIdTokenCacheAsync"},
  96. };
  97. // clang-format on
  98. RegisterHandlers(functions);
  99. }
  100. };
  101. class IAdministrator final : public ServiceFramework<IAdministrator> {
  102. public:
  103. explicit IAdministrator(Core::System& system_, Common::UUID)
  104. : ServiceFramework{system_, "IAdministrator"} {
  105. // clang-format off
  106. static const FunctionInfo functions[] = {
  107. {0, nullptr, "CheckAvailability"},
  108. {1, nullptr, "GetAccountId"},
  109. {2, nullptr, "EnsureIdTokenCacheAsync"},
  110. {3, nullptr, "LoadIdTokenCache"},
  111. {100, nullptr, "SetSystemProgramIdentification"},
  112. {101, nullptr, "RefreshNotificationTokenAsync"}, // 7.0.0+
  113. {110, nullptr, "GetServiceEntryRequirementCache"}, // 4.0.0+
  114. {111, nullptr, "InvalidateServiceEntryRequirementCache"}, // 4.0.0+
  115. {112, nullptr, "InvalidateTokenCache"}, // 4.0.0 - 6.2.0
  116. {113, nullptr, "GetServiceEntryRequirementCacheForOnlinePlay"}, // 6.1.0+
  117. {120, nullptr, "GetNintendoAccountId"},
  118. {121, nullptr, "CalculateNintendoAccountAuthenticationFingerprint"}, // 9.0.0+
  119. {130, nullptr, "GetNintendoAccountUserResourceCache"},
  120. {131, nullptr, "RefreshNintendoAccountUserResourceCacheAsync"},
  121. {132, nullptr, "RefreshNintendoAccountUserResourceCacheAsyncIfSecondsElapsed"},
  122. {133, nullptr, "GetNintendoAccountVerificationUrlCache"}, // 9.0.0+
  123. {134, nullptr, "RefreshNintendoAccountVerificationUrlCacheAsync"}, // 9.0.0+
  124. {135, nullptr, "RefreshNintendoAccountVerificationUrlCacheAsyncIfSecondsElapsed"}, // 9.0.0+
  125. {140, nullptr, "GetNetworkServiceLicenseCache"}, // 5.0.0+
  126. {141, nullptr, "RefreshNetworkServiceLicenseCacheAsync"}, // 5.0.0+
  127. {142, nullptr, "RefreshNetworkServiceLicenseCacheAsyncIfSecondsElapsed"}, // 5.0.0+
  128. {150, nullptr, "CreateAuthorizationRequest"},
  129. {200, nullptr, "IsRegistered"},
  130. {201, nullptr, "RegisterAsync"},
  131. {202, nullptr, "UnregisterAsync"},
  132. {203, nullptr, "DeleteRegistrationInfoLocally"},
  133. {220, nullptr, "SynchronizeProfileAsync"},
  134. {221, nullptr, "UploadProfileAsync"},
  135. {222, nullptr, "SynchronizaProfileAsyncIfSecondsElapsed"},
  136. {250, nullptr, "IsLinkedWithNintendoAccount"},
  137. {251, nullptr, "CreateProcedureToLinkWithNintendoAccount"},
  138. {252, nullptr, "ResumeProcedureToLinkWithNintendoAccount"},
  139. {255, nullptr, "CreateProcedureToUpdateLinkageStateOfNintendoAccount"},
  140. {256, nullptr, "ResumeProcedureToUpdateLinkageStateOfNintendoAccount"},
  141. {260, nullptr, "CreateProcedureToLinkNnidWithNintendoAccount"}, // 3.0.0+
  142. {261, nullptr, "ResumeProcedureToLinkNnidWithNintendoAccount"}, // 3.0.0+
  143. {280, nullptr, "ProxyProcedureToAcquireApplicationAuthorizationForNintendoAccount"},
  144. {290, nullptr, "GetRequestForNintendoAccountUserResourceView"}, // 8.0.0+
  145. {300, nullptr, "TryRecoverNintendoAccountUserStateAsync"}, // 6.0.0+
  146. {400, nullptr, "IsServiceEntryRequirementCacheRefreshRequiredForOnlinePlay"}, // 6.1.0+
  147. {401, nullptr, "RefreshServiceEntryRequirementCacheForOnlinePlayAsync"}, // 6.1.0+
  148. {900, nullptr, "GetAuthenticationInfoForWin"}, // 9.0.0+
  149. {901, nullptr, "ImportAsyncForWin"}, // 9.0.0+
  150. {997, nullptr, "DebugUnlinkNintendoAccountAsync"},
  151. {998, nullptr, "DebugSetAvailabilityErrorDetail"},
  152. };
  153. // clang-format on
  154. RegisterHandlers(functions);
  155. }
  156. };
  157. class IAuthorizationRequest final : public ServiceFramework<IAuthorizationRequest> {
  158. public:
  159. explicit IAuthorizationRequest(Core::System& system_, Common::UUID)
  160. : ServiceFramework{system_, "IAuthorizationRequest"} {
  161. // clang-format off
  162. static const FunctionInfo functions[] = {
  163. {0, nullptr, "GetSessionId"},
  164. {10, nullptr, "InvokeWithoutInteractionAsync"},
  165. {19, nullptr, "IsAuthorized"},
  166. {20, nullptr, "GetAuthorizationCode"},
  167. {21, nullptr, "GetIdToken"},
  168. {22, nullptr, "GetState"},
  169. };
  170. // clang-format on
  171. RegisterHandlers(functions);
  172. }
  173. };
  174. class IOAuthProcedure final : public ServiceFramework<IOAuthProcedure> {
  175. public:
  176. explicit IOAuthProcedure(Core::System& system_, Common::UUID)
  177. : ServiceFramework{system_, "IOAuthProcedure"} {
  178. // clang-format off
  179. static const FunctionInfo functions[] = {
  180. {0, nullptr, "PrepareAsync"},
  181. {1, nullptr, "GetRequest"},
  182. {2, nullptr, "ApplyResponse"},
  183. {3, nullptr, "ApplyResponseAsync"},
  184. {10, nullptr, "Suspend"},
  185. };
  186. // clang-format on
  187. RegisterHandlers(functions);
  188. }
  189. };
  190. // 3.0.0+
  191. class IOAuthProcedureForExternalNsa final : public ServiceFramework<IOAuthProcedureForExternalNsa> {
  192. public:
  193. explicit IOAuthProcedureForExternalNsa(Core::System& system_, Common::UUID)
  194. : ServiceFramework{system_, "IOAuthProcedureForExternalNsa"} {
  195. // clang-format off
  196. static const FunctionInfo functions[] = {
  197. {0, nullptr, "PrepareAsync"},
  198. {1, nullptr, "GetRequest"},
  199. {2, nullptr, "ApplyResponse"},
  200. {3, nullptr, "ApplyResponseAsync"},
  201. {10, nullptr, "Suspend"},
  202. {100, nullptr, "GetAccountId"},
  203. {101, nullptr, "GetLinkedNintendoAccountId"},
  204. {102, nullptr, "GetNickname"},
  205. {103, nullptr, "GetProfileImage"},
  206. };
  207. // clang-format on
  208. RegisterHandlers(functions);
  209. }
  210. };
  211. class IOAuthProcedureForNintendoAccountLinkage final
  212. : public ServiceFramework<IOAuthProcedureForNintendoAccountLinkage> {
  213. public:
  214. explicit IOAuthProcedureForNintendoAccountLinkage(Core::System& system_, Common::UUID)
  215. : ServiceFramework{system_, "IOAuthProcedureForNintendoAccountLinkage"} {
  216. // clang-format off
  217. static const FunctionInfo functions[] = {
  218. {0, nullptr, "PrepareAsync"},
  219. {1, nullptr, "GetRequest"},
  220. {2, nullptr, "ApplyResponse"},
  221. {3, nullptr, "ApplyResponseAsync"},
  222. {10, nullptr, "Suspend"},
  223. {100, nullptr, "GetRequestWithTheme"},
  224. {101, nullptr, "IsNetworkServiceAccountReplaced"},
  225. {199, nullptr, "GetUrlForIntroductionOfExtraMembership"}, // 2.0.0 - 5.1.0
  226. };
  227. // clang-format on
  228. RegisterHandlers(functions);
  229. }
  230. };
  231. class INotifier final : public ServiceFramework<INotifier> {
  232. public:
  233. explicit INotifier(Core::System& system_, Common::UUID)
  234. : ServiceFramework{system_, "INotifier"} {
  235. // clang-format off
  236. static const FunctionInfo functions[] = {
  237. {0, nullptr, "GetSystemEvent"},
  238. };
  239. // clang-format on
  240. RegisterHandlers(functions);
  241. }
  242. };
  243. class IProfileCommon : public ServiceFramework<IProfileCommon> {
  244. public:
  245. explicit IProfileCommon(Core::System& system_, const char* name, bool editor_commands,
  246. Common::UUID user_id_, ProfileManager& profile_manager_)
  247. : ServiceFramework{system_, name}, profile_manager{profile_manager_}, user_id{user_id_} {
  248. static const FunctionInfo functions[] = {
  249. {0, &IProfileCommon::Get, "Get"},
  250. {1, &IProfileCommon::GetBase, "GetBase"},
  251. {10, &IProfileCommon::GetImageSize, "GetImageSize"},
  252. {11, &IProfileCommon::LoadImage, "LoadImage"},
  253. };
  254. RegisterHandlers(functions);
  255. if (editor_commands) {
  256. static const FunctionInfo editor_functions[] = {
  257. {100, &IProfileCommon::Store, "Store"},
  258. {101, &IProfileCommon::StoreWithImage, "StoreWithImage"},
  259. };
  260. RegisterHandlers(editor_functions);
  261. }
  262. }
  263. protected:
  264. void Get(Kernel::HLERequestContext& ctx) {
  265. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  266. ProfileBase profile_base{};
  267. UserData data{};
  268. if (profile_manager.GetProfileBaseAndData(user_id, profile_base, data)) {
  269. ctx.WriteBuffer(data);
  270. IPC::ResponseBuilder rb{ctx, 16};
  271. rb.Push(ResultSuccess);
  272. rb.PushRaw(profile_base);
  273. } else {
  274. LOG_ERROR(Service_ACC, "Failed to get profile base and data for user=0x{}",
  275. user_id.RawString());
  276. IPC::ResponseBuilder rb{ctx, 2};
  277. rb.Push(ResultUnknown); // TODO(ogniK): Get actual error code
  278. }
  279. }
  280. void GetBase(Kernel::HLERequestContext& ctx) {
  281. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  282. ProfileBase profile_base{};
  283. if (profile_manager.GetProfileBase(user_id, profile_base)) {
  284. IPC::ResponseBuilder rb{ctx, 16};
  285. rb.Push(ResultSuccess);
  286. rb.PushRaw(profile_base);
  287. } else {
  288. LOG_ERROR(Service_ACC, "Failed to get profile base for user=0x{}", user_id.RawString());
  289. IPC::ResponseBuilder rb{ctx, 2};
  290. rb.Push(ResultUnknown); // TODO(ogniK): Get actual error code
  291. }
  292. }
  293. void LoadImage(Kernel::HLERequestContext& ctx) {
  294. LOG_DEBUG(Service_ACC, "called");
  295. IPC::ResponseBuilder rb{ctx, 3};
  296. rb.Push(ResultSuccess);
  297. const Common::FS::IOFile image(GetImagePath(user_id), Common::FS::FileAccessMode::Read,
  298. Common::FS::FileType::BinaryFile);
  299. if (!image.IsOpen()) {
  300. LOG_WARNING(Service_ACC,
  301. "Failed to load user provided image! Falling back to built-in backup...");
  302. ctx.WriteBuffer(Core::Constants::ACCOUNT_BACKUP_JPEG);
  303. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  304. return;
  305. }
  306. const u32 size = SanitizeJPEGSize(image.GetSize());
  307. std::vector<u8> buffer(size);
  308. if (image.Read(buffer) != buffer.size()) {
  309. LOG_ERROR(Service_ACC, "Failed to read all the bytes in the user provided image.");
  310. }
  311. ctx.WriteBuffer(buffer);
  312. rb.Push<u32>(size);
  313. }
  314. void GetImageSize(Kernel::HLERequestContext& ctx) {
  315. LOG_DEBUG(Service_ACC, "called");
  316. IPC::ResponseBuilder rb{ctx, 3};
  317. rb.Push(ResultSuccess);
  318. const Common::FS::IOFile image(GetImagePath(user_id), Common::FS::FileAccessMode::Read,
  319. Common::FS::FileType::BinaryFile);
  320. if (!image.IsOpen()) {
  321. LOG_WARNING(Service_ACC,
  322. "Failed to load user provided image! Falling back to built-in backup...");
  323. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  324. } else {
  325. rb.Push(SanitizeJPEGSize(image.GetSize()));
  326. }
  327. }
  328. void Store(Kernel::HLERequestContext& ctx) {
  329. IPC::RequestParser rp{ctx};
  330. const auto base = rp.PopRaw<ProfileBase>();
  331. const auto user_data = ctx.ReadBuffer();
  332. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid=0x{}",
  333. Common::StringFromFixedZeroTerminatedBuffer(
  334. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  335. base.timestamp, base.user_uuid.RawString());
  336. if (user_data.size() < sizeof(UserData)) {
  337. LOG_ERROR(Service_ACC, "UserData buffer too small!");
  338. IPC::ResponseBuilder rb{ctx, 2};
  339. rb.Push(ERR_INVALID_BUFFER);
  340. return;
  341. }
  342. UserData data;
  343. std::memcpy(&data, user_data.data(), sizeof(UserData));
  344. if (!profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  345. LOG_ERROR(Service_ACC, "Failed to update user data and base!");
  346. IPC::ResponseBuilder rb{ctx, 2};
  347. rb.Push(ERR_FAILED_SAVE_DATA);
  348. return;
  349. }
  350. IPC::ResponseBuilder rb{ctx, 2};
  351. rb.Push(ResultSuccess);
  352. }
  353. void StoreWithImage(Kernel::HLERequestContext& ctx) {
  354. IPC::RequestParser rp{ctx};
  355. const auto base = rp.PopRaw<ProfileBase>();
  356. const auto user_data = ctx.ReadBuffer();
  357. const auto image_data = ctx.ReadBuffer(1);
  358. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid=0x{}",
  359. Common::StringFromFixedZeroTerminatedBuffer(
  360. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  361. base.timestamp, base.user_uuid.RawString());
  362. if (user_data.size() < sizeof(UserData)) {
  363. LOG_ERROR(Service_ACC, "UserData buffer too small!");
  364. IPC::ResponseBuilder rb{ctx, 2};
  365. rb.Push(ERR_INVALID_BUFFER);
  366. return;
  367. }
  368. UserData data;
  369. std::memcpy(&data, user_data.data(), sizeof(UserData));
  370. Common::FS::IOFile image(GetImagePath(user_id), Common::FS::FileAccessMode::Write,
  371. Common::FS::FileType::BinaryFile);
  372. if (!image.IsOpen() || !image.SetSize(image_data.size()) ||
  373. image.Write(image_data) != image_data.size() ||
  374. !profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  375. LOG_ERROR(Service_ACC, "Failed to update profile data, base, and image!");
  376. IPC::ResponseBuilder rb{ctx, 2};
  377. rb.Push(ERR_FAILED_SAVE_DATA);
  378. return;
  379. }
  380. IPC::ResponseBuilder rb{ctx, 2};
  381. rb.Push(ResultSuccess);
  382. }
  383. ProfileManager& profile_manager;
  384. Common::UUID user_id{}; ///< The user id this profile refers to.
  385. };
  386. class IProfile final : public IProfileCommon {
  387. public:
  388. explicit IProfile(Core::System& system_, Common::UUID user_id_,
  389. ProfileManager& profile_manager_)
  390. : IProfileCommon{system_, "IProfile", false, user_id_, profile_manager_} {}
  391. };
  392. class IProfileEditor final : public IProfileCommon {
  393. public:
  394. explicit IProfileEditor(Core::System& system_, Common::UUID user_id_,
  395. ProfileManager& profile_manager_)
  396. : IProfileCommon{system_, "IProfileEditor", true, user_id_, profile_manager_} {}
  397. };
  398. class ISessionObject final : public ServiceFramework<ISessionObject> {
  399. public:
  400. explicit ISessionObject(Core::System& system_, Common::UUID)
  401. : ServiceFramework{system_, "ISessionObject"} {
  402. // clang-format off
  403. static const FunctionInfo functions[] = {
  404. {999, nullptr, "Dummy"},
  405. };
  406. // clang-format on
  407. RegisterHandlers(functions);
  408. }
  409. };
  410. class IGuestLoginRequest final : public ServiceFramework<IGuestLoginRequest> {
  411. public:
  412. explicit IGuestLoginRequest(Core::System& system_, Common::UUID)
  413. : ServiceFramework{system_, "IGuestLoginRequest"} {
  414. // clang-format off
  415. static const FunctionInfo functions[] = {
  416. {0, nullptr, "GetSessionId"},
  417. {11, nullptr, "Unknown"}, // 1.0.0 - 2.3.0 (the name is blank on Switchbrew)
  418. {12, nullptr, "GetAccountId"},
  419. {13, nullptr, "GetLinkedNintendoAccountId"},
  420. {14, nullptr, "GetNickname"},
  421. {15, nullptr, "GetProfileImage"},
  422. {21, nullptr, "LoadIdTokenCache"}, // 3.0.0+
  423. };
  424. // clang-format on
  425. RegisterHandlers(functions);
  426. }
  427. };
  428. class EnsureTokenIdCacheAsyncInterface final : public IAsyncContext {
  429. public:
  430. explicit EnsureTokenIdCacheAsyncInterface(Core::System& system_) : IAsyncContext{system_} {
  431. MarkComplete();
  432. }
  433. ~EnsureTokenIdCacheAsyncInterface() = default;
  434. void LoadIdTokenCache(Kernel::HLERequestContext& ctx) {
  435. LOG_WARNING(Service_ACC, "(STUBBED) called");
  436. IPC::ResponseBuilder rb{ctx, 2};
  437. rb.Push(ResultSuccess);
  438. }
  439. protected:
  440. bool IsComplete() const override {
  441. return true;
  442. }
  443. void Cancel() override {}
  444. Result GetResult() const override {
  445. return ResultSuccess;
  446. }
  447. };
  448. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  449. public:
  450. explicit IManagerForApplication(Core::System& system_,
  451. const std::shared_ptr<ProfileManager>& profile_manager_)
  452. : ServiceFramework{system_, "IManagerForApplication"},
  453. ensure_token_id{std::make_shared<EnsureTokenIdCacheAsyncInterface>(system)},
  454. profile_manager{profile_manager_} {
  455. // clang-format off
  456. static const FunctionInfo functions[] = {
  457. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  458. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  459. {2, &IManagerForApplication::EnsureIdTokenCacheAsync, "EnsureIdTokenCacheAsync"},
  460. {3, &IManagerForApplication::LoadIdTokenCache, "LoadIdTokenCache"},
  461. {130, &IManagerForApplication::GetNintendoAccountUserResourceCacheForApplication, "GetNintendoAccountUserResourceCacheForApplication"},
  462. {150, nullptr, "CreateAuthorizationRequest"},
  463. {160, &IManagerForApplication::StoreOpenContext, "StoreOpenContext"},
  464. {170, nullptr, "LoadNetworkServiceLicenseKindAsync"},
  465. };
  466. // clang-format on
  467. RegisterHandlers(functions);
  468. }
  469. private:
  470. void CheckAvailability(Kernel::HLERequestContext& ctx) {
  471. LOG_DEBUG(Service_ACC, "(STUBBED) called");
  472. IPC::ResponseBuilder rb{ctx, 3};
  473. rb.Push(ResultSuccess);
  474. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  475. }
  476. void GetAccountId(Kernel::HLERequestContext& ctx) {
  477. LOG_DEBUG(Service_ACC, "called");
  478. IPC::ResponseBuilder rb{ctx, 4};
  479. rb.Push(ResultSuccess);
  480. rb.PushRaw<u64>(profile_manager->GetLastOpenedUser().Hash());
  481. }
  482. void EnsureIdTokenCacheAsync(Kernel::HLERequestContext& ctx) {
  483. LOG_WARNING(Service_ACC, "(STUBBED) called");
  484. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  485. rb.Push(ResultSuccess);
  486. rb.PushIpcInterface(ensure_token_id);
  487. }
  488. void LoadIdTokenCache(Kernel::HLERequestContext& ctx) {
  489. LOG_WARNING(Service_ACC, "(STUBBED) called");
  490. ensure_token_id->LoadIdTokenCache(ctx);
  491. }
  492. void GetNintendoAccountUserResourceCacheForApplication(Kernel::HLERequestContext& ctx) {
  493. LOG_WARNING(Service_ACC, "(STUBBED) called");
  494. std::vector<u8> nas_user_base_for_application(0x68);
  495. ctx.WriteBuffer(nas_user_base_for_application, 0);
  496. if (ctx.CanWriteBuffer(1)) {
  497. std::vector<u8> unknown_out_buffer(ctx.GetWriteBufferSize(1));
  498. ctx.WriteBuffer(unknown_out_buffer, 1);
  499. }
  500. IPC::ResponseBuilder rb{ctx, 4};
  501. rb.Push(ResultSuccess);
  502. rb.PushRaw<u64>(profile_manager->GetLastOpenedUser().Hash());
  503. }
  504. void StoreOpenContext(Kernel::HLERequestContext& ctx) {
  505. LOG_DEBUG(Service_ACC, "called");
  506. profile_manager->StoreOpenedUsers();
  507. IPC::ResponseBuilder rb{ctx, 2};
  508. rb.Push(ResultSuccess);
  509. }
  510. std::shared_ptr<EnsureTokenIdCacheAsyncInterface> ensure_token_id{};
  511. std::shared_ptr<ProfileManager> profile_manager;
  512. };
  513. // 6.0.0+
  514. class IAsyncNetworkServiceLicenseKindContext final
  515. : public ServiceFramework<IAsyncNetworkServiceLicenseKindContext> {
  516. public:
  517. explicit IAsyncNetworkServiceLicenseKindContext(Core::System& system_, Common::UUID)
  518. : ServiceFramework{system_, "IAsyncNetworkServiceLicenseKindContext"} {
  519. // clang-format off
  520. static const FunctionInfo functions[] = {
  521. {0, nullptr, "GetSystemEvent"},
  522. {1, nullptr, "Cancel"},
  523. {2, nullptr, "HasDone"},
  524. {3, nullptr, "GetResult"},
  525. {4, nullptr, "GetNetworkServiceLicenseKind"},
  526. };
  527. // clang-format on
  528. RegisterHandlers(functions);
  529. }
  530. };
  531. // 8.0.0+
  532. class IOAuthProcedureForUserRegistration final
  533. : public ServiceFramework<IOAuthProcedureForUserRegistration> {
  534. public:
  535. explicit IOAuthProcedureForUserRegistration(Core::System& system_, Common::UUID)
  536. : ServiceFramework{system_, "IOAuthProcedureForUserRegistration"} {
  537. // clang-format off
  538. static const FunctionInfo functions[] = {
  539. {0, nullptr, "PrepareAsync"},
  540. {1, nullptr, "GetRequest"},
  541. {2, nullptr, "ApplyResponse"},
  542. {3, nullptr, "ApplyResponseAsync"},
  543. {10, nullptr, "Suspend"},
  544. {100, nullptr, "GetAccountId"},
  545. {101, nullptr, "GetLinkedNintendoAccountId"},
  546. {102, nullptr, "GetNickname"},
  547. {103, nullptr, "GetProfileImage"},
  548. {110, nullptr, "RegisterUserAsync"},
  549. {111, nullptr, "GetUid"},
  550. };
  551. // clang-format on
  552. RegisterHandlers(functions);
  553. }
  554. };
  555. class DAUTH_O final : public ServiceFramework<DAUTH_O> {
  556. public:
  557. explicit DAUTH_O(Core::System& system_, Common::UUID) : ServiceFramework{system_, "dauth:o"} {
  558. // clang-format off
  559. static const FunctionInfo functions[] = {
  560. {0, nullptr, "EnsureAuthenticationTokenCacheAsync"},
  561. {1, nullptr, "LoadAuthenticationTokenCache"},
  562. {2, nullptr, "InvalidateAuthenticationTokenCache"},
  563. {10, nullptr, "EnsureEdgeTokenCacheAsync"},
  564. {11, nullptr, "LoadEdgeTokenCache"},
  565. {12, nullptr, "InvalidateEdgeTokenCache"},
  566. {20, nullptr, "EnsureApplicationAuthenticationCacheAsync"},
  567. {21, nullptr, "LoadApplicationAuthenticationTokenCache"},
  568. {22, nullptr, "LoadApplicationNetworkServiceClientConfigCache"},
  569. {23, nullptr, "IsApplicationAuthenticationCacheAvailable"},
  570. {24, nullptr, "InvalidateApplicationAuthenticationCache"},
  571. };
  572. // clang-format on
  573. RegisterHandlers(functions);
  574. }
  575. };
  576. // 6.0.0+
  577. class IAsyncResult final : public ServiceFramework<IAsyncResult> {
  578. public:
  579. explicit IAsyncResult(Core::System& system_, Common::UUID)
  580. : ServiceFramework{system_, "IAsyncResult"} {
  581. // clang-format off
  582. static const FunctionInfo functions[] = {
  583. {0, nullptr, "GetResult"},
  584. {1, nullptr, "Cancel"},
  585. {2, nullptr, "IsAvailable"},
  586. {3, nullptr, "GetSystemEvent"},
  587. };
  588. // clang-format on
  589. RegisterHandlers(functions);
  590. }
  591. };
  592. void Module::Interface::GetUserCount(Kernel::HLERequestContext& ctx) {
  593. LOG_DEBUG(Service_ACC, "called");
  594. IPC::ResponseBuilder rb{ctx, 3};
  595. rb.Push(ResultSuccess);
  596. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  597. }
  598. void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
  599. IPC::RequestParser rp{ctx};
  600. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  601. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  602. IPC::ResponseBuilder rb{ctx, 3};
  603. rb.Push(ResultSuccess);
  604. rb.Push(profile_manager->UserExists(user_id));
  605. }
  606. void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
  607. LOG_DEBUG(Service_ACC, "called");
  608. ctx.WriteBuffer(profile_manager->GetAllUsers());
  609. IPC::ResponseBuilder rb{ctx, 2};
  610. rb.Push(ResultSuccess);
  611. }
  612. void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
  613. LOG_DEBUG(Service_ACC, "called");
  614. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  615. IPC::ResponseBuilder rb{ctx, 2};
  616. rb.Push(ResultSuccess);
  617. }
  618. void Module::Interface::GetLastOpenedUser(Kernel::HLERequestContext& ctx) {
  619. LOG_DEBUG(Service_ACC, "called");
  620. IPC::ResponseBuilder rb{ctx, 6};
  621. rb.Push(ResultSuccess);
  622. rb.PushRaw<Common::UUID>(profile_manager->GetLastOpenedUser());
  623. }
  624. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  625. IPC::RequestParser rp{ctx};
  626. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  627. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  628. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  629. rb.Push(ResultSuccess);
  630. rb.PushIpcInterface<IProfile>(system, user_id, *profile_manager);
  631. }
  632. void Module::Interface::IsUserRegistrationRequestPermitted(Kernel::HLERequestContext& ctx) {
  633. LOG_WARNING(Service_ACC, "(STUBBED) called");
  634. IPC::ResponseBuilder rb{ctx, 3};
  635. rb.Push(ResultSuccess);
  636. rb.Push(profile_manager->CanSystemRegisterUser());
  637. }
  638. void Module::Interface::InitializeApplicationInfo(Kernel::HLERequestContext& ctx) {
  639. LOG_DEBUG(Service_ACC, "called");
  640. IPC::ResponseBuilder rb{ctx, 2};
  641. rb.Push(InitializeApplicationInfoBase());
  642. }
  643. void Module::Interface::InitializeApplicationInfoRestricted(Kernel::HLERequestContext& ctx) {
  644. LOG_WARNING(Service_ACC, "(Partial implementation) called");
  645. // TODO(ogniK): We require checking if the user actually owns the title and what not. As of
  646. // currently, we assume the user owns the title. InitializeApplicationInfoBase SHOULD be called
  647. // first then we do extra checks if the game is a digital copy.
  648. IPC::ResponseBuilder rb{ctx, 2};
  649. rb.Push(InitializeApplicationInfoBase());
  650. }
  651. Result Module::Interface::InitializeApplicationInfoBase() {
  652. if (application_info) {
  653. LOG_ERROR(Service_ACC, "Application already initialized");
  654. return ERR_ACCOUNTINFO_ALREADY_INITIALIZED;
  655. }
  656. // TODO(ogniK): This should be changed to reflect the target process for when we have multiple
  657. // processes emulated. As we don't actually have pid support we should assume we're just using
  658. // our own process
  659. const auto launch_property =
  660. system.GetARPManager().GetLaunchProperty(system.GetApplicationProcessProgramID());
  661. if (launch_property.Failed()) {
  662. LOG_ERROR(Service_ACC, "Failed to get launch property");
  663. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  664. }
  665. switch (launch_property->base_game_storage_id) {
  666. case FileSys::StorageId::GameCard:
  667. application_info.application_type = ApplicationType::GameCard;
  668. break;
  669. case FileSys::StorageId::Host:
  670. case FileSys::StorageId::NandUser:
  671. case FileSys::StorageId::SdCard:
  672. case FileSys::StorageId::None: // Yuzu specific, differs from hardware
  673. application_info.application_type = ApplicationType::Digital;
  674. break;
  675. default:
  676. LOG_ERROR(Service_ACC, "Invalid game storage ID! storage_id={}",
  677. launch_property->base_game_storage_id);
  678. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  679. }
  680. LOG_WARNING(Service_ACC, "ApplicationInfo init required");
  681. // TODO(ogniK): Actual initalization here
  682. return ResultSuccess;
  683. }
  684. void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestContext& ctx) {
  685. LOG_DEBUG(Service_ACC, "called");
  686. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  687. rb.Push(ResultSuccess);
  688. rb.PushIpcInterface<IManagerForApplication>(system, profile_manager);
  689. }
  690. void Module::Interface::IsUserAccountSwitchLocked(Kernel::HLERequestContext& ctx) {
  691. LOG_DEBUG(Service_ACC, "called");
  692. FileSys::NACP nacp;
  693. const auto res = system.GetAppLoader().ReadControlData(nacp);
  694. bool is_locked = false;
  695. if (res != Loader::ResultStatus::Success) {
  696. const FileSys::PatchManager pm{system.GetApplicationProcessProgramID(),
  697. system.GetFileSystemController(),
  698. system.GetContentProvider()};
  699. const auto nacp_unique = pm.GetControlMetadata().first;
  700. if (nacp_unique != nullptr) {
  701. is_locked = nacp_unique->GetUserAccountSwitchLock();
  702. } else {
  703. LOG_ERROR(Service_ACC, "nacp_unique is null!");
  704. }
  705. } else {
  706. is_locked = nacp.GetUserAccountSwitchLock();
  707. }
  708. IPC::ResponseBuilder rb{ctx, 3};
  709. rb.Push(ResultSuccess);
  710. rb.Push(is_locked);
  711. }
  712. void Module::Interface::InitializeApplicationInfoV2(Kernel::HLERequestContext& ctx) {
  713. LOG_WARNING(Service_ACC, "(STUBBED) called");
  714. IPC::ResponseBuilder rb{ctx, 2};
  715. rb.Push(ResultSuccess);
  716. }
  717. void Module::Interface::GetProfileEditor(Kernel::HLERequestContext& ctx) {
  718. IPC::RequestParser rp{ctx};
  719. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  720. LOG_DEBUG(Service_ACC, "called, user_id=0x{}", user_id.RawString());
  721. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  722. rb.Push(ResultSuccess);
  723. rb.PushIpcInterface<IProfileEditor>(system, user_id, *profile_manager);
  724. }
  725. void Module::Interface::ListQualifiedUsers(Kernel::HLERequestContext& ctx) {
  726. LOG_DEBUG(Service_ACC, "called");
  727. // All users should be qualified. We don't actually have parental control or anything to do with
  728. // nintendo online currently. We're just going to assume the user running the game has access to
  729. // the game regardless of parental control settings.
  730. ctx.WriteBuffer(profile_manager->GetAllUsers());
  731. IPC::ResponseBuilder rb{ctx, 2};
  732. rb.Push(ResultSuccess);
  733. }
  734. void Module::Interface::ListOpenContextStoredUsers(Kernel::HLERequestContext& ctx) {
  735. LOG_DEBUG(Service_ACC, "called");
  736. ctx.WriteBuffer(profile_manager->GetStoredOpenedUsers());
  737. IPC::ResponseBuilder rb{ctx, 2};
  738. rb.Push(ResultSuccess);
  739. }
  740. void Module::Interface::StoreSaveDataThumbnailApplication(Kernel::HLERequestContext& ctx) {
  741. IPC::RequestParser rp{ctx};
  742. const auto uuid = rp.PopRaw<Common::UUID>();
  743. LOG_WARNING(Service_ACC, "(STUBBED) called, uuid=0x{}", uuid.RawString());
  744. // TODO(ogniK): Check if application ID is zero on acc initialize. As we don't have a reliable
  745. // way of confirming things like the TID, we're going to assume a non zero value for the time
  746. // being.
  747. constexpr u64 tid{1};
  748. StoreSaveDataThumbnail(ctx, uuid, tid);
  749. }
  750. void Module::Interface::StoreSaveDataThumbnailSystem(Kernel::HLERequestContext& ctx) {
  751. IPC::RequestParser rp{ctx};
  752. const auto uuid = rp.PopRaw<Common::UUID>();
  753. const auto tid = rp.Pop<u64_le>();
  754. LOG_WARNING(Service_ACC, "(STUBBED) called, uuid=0x{}, tid={:016X}", uuid.RawString(), tid);
  755. StoreSaveDataThumbnail(ctx, uuid, tid);
  756. }
  757. void Module::Interface::StoreSaveDataThumbnail(Kernel::HLERequestContext& ctx,
  758. const Common::UUID& uuid, const u64 tid) {
  759. IPC::ResponseBuilder rb{ctx, 2};
  760. if (tid == 0) {
  761. LOG_ERROR(Service_ACC, "TitleID is not valid!");
  762. rb.Push(ERR_INVALID_APPLICATION_ID);
  763. return;
  764. }
  765. if (uuid.IsInvalid()) {
  766. LOG_ERROR(Service_ACC, "User ID is not valid!");
  767. rb.Push(ERR_INVALID_USER_ID);
  768. return;
  769. }
  770. const auto thumbnail_size = ctx.GetReadBufferSize();
  771. if (thumbnail_size != THUMBNAIL_SIZE) {
  772. LOG_ERROR(Service_ACC, "Buffer size is empty! size={:X} expecting {:X}", thumbnail_size,
  773. THUMBNAIL_SIZE);
  774. rb.Push(ERR_INVALID_BUFFER_SIZE);
  775. return;
  776. }
  777. // TODO(ogniK): Construct save data thumbnail
  778. rb.Push(ResultSuccess);
  779. }
  780. void Module::Interface::TrySelectUserWithoutInteraction(Kernel::HLERequestContext& ctx) {
  781. LOG_DEBUG(Service_ACC, "called");
  782. // A u8 is passed into this function which we can safely ignore. It's to determine if we have
  783. // access to use the network or not by the looks of it
  784. IPC::ResponseBuilder rb{ctx, 6};
  785. if (profile_manager->GetUserCount() != 1) {
  786. rb.Push(ResultSuccess);
  787. rb.PushRaw(Common::InvalidUUID);
  788. return;
  789. }
  790. const auto user_list = profile_manager->GetAllUsers();
  791. if (std::ranges::all_of(user_list, [](const auto& user) { return user.IsInvalid(); })) {
  792. rb.Push(ResultUnknown); // TODO(ogniK): Find the correct error code
  793. rb.PushRaw(Common::InvalidUUID);
  794. return;
  795. }
  796. // Select the first user we have
  797. rb.Push(ResultSuccess);
  798. rb.PushRaw(profile_manager->GetUser(0)->uuid);
  799. }
  800. Module::Interface::Interface(std::shared_ptr<Module> module_,
  801. std::shared_ptr<ProfileManager> profile_manager_,
  802. Core::System& system_, const char* name)
  803. : ServiceFramework{system_, name}, module{std::move(module_)}, profile_manager{std::move(
  804. profile_manager_)} {}
  805. Module::Interface::~Interface() = default;
  806. void InstallInterfaces(Core::System& system) {
  807. auto module = std::make_shared<Module>();
  808. auto profile_manager = std::make_shared<ProfileManager>();
  809. std::make_shared<ACC_AA>(module, profile_manager, system)
  810. ->InstallAsService(system.ServiceManager());
  811. std::make_shared<ACC_SU>(module, profile_manager, system)
  812. ->InstallAsService(system.ServiceManager());
  813. std::make_shared<ACC_U0>(module, profile_manager, system)
  814. ->InstallAsService(system.ServiceManager());
  815. std::make_shared<ACC_U1>(module, profile_manager, system)
  816. ->InstallAsService(system.ServiceManager());
  817. }
  818. } // namespace Service::Account