acc.cpp 37 KB

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