acc.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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_,
  450. const std::shared_ptr<ProfileManager>& profile_manager_)
  451. : ServiceFramework{system_, "IManagerForApplication"},
  452. ensure_token_id{std::make_shared<EnsureTokenIdCacheAsyncInterface>(system)},
  453. profile_manager{profile_manager_} {
  454. // clang-format off
  455. static const FunctionInfo functions[] = {
  456. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  457. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  458. {2, &IManagerForApplication::EnsureIdTokenCacheAsync, "EnsureIdTokenCacheAsync"},
  459. {3, &IManagerForApplication::LoadIdTokenCache, "LoadIdTokenCache"},
  460. {130, &IManagerForApplication::GetNintendoAccountUserResourceCacheForApplication, "GetNintendoAccountUserResourceCacheForApplication"},
  461. {150, nullptr, "CreateAuthorizationRequest"},
  462. {160, &IManagerForApplication::StoreOpenContext, "StoreOpenContext"},
  463. {170, nullptr, "LoadNetworkServiceLicenseKindAsync"},
  464. };
  465. // clang-format on
  466. RegisterHandlers(functions);
  467. }
  468. private:
  469. void CheckAvailability(Kernel::HLERequestContext& ctx) {
  470. LOG_DEBUG(Service_ACC, "(STUBBED) called");
  471. IPC::ResponseBuilder rb{ctx, 3};
  472. rb.Push(ResultSuccess);
  473. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  474. }
  475. void GetAccountId(Kernel::HLERequestContext& ctx) {
  476. LOG_DEBUG(Service_ACC, "called");
  477. IPC::ResponseBuilder rb{ctx, 4};
  478. rb.Push(ResultSuccess);
  479. rb.PushRaw<u64>(profile_manager->GetLastOpenedUser().Hash());
  480. }
  481. void EnsureIdTokenCacheAsync(Kernel::HLERequestContext& ctx) {
  482. LOG_WARNING(Service_ACC, "(STUBBED) called");
  483. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  484. rb.Push(ResultSuccess);
  485. rb.PushIpcInterface(ensure_token_id);
  486. }
  487. void LoadIdTokenCache(Kernel::HLERequestContext& ctx) {
  488. LOG_WARNING(Service_ACC, "(STUBBED) called");
  489. ensure_token_id->LoadIdTokenCache(ctx);
  490. }
  491. void GetNintendoAccountUserResourceCacheForApplication(Kernel::HLERequestContext& ctx) {
  492. LOG_WARNING(Service_ACC, "(STUBBED) called");
  493. std::vector<u8> nas_user_base_for_application(0x68);
  494. ctx.WriteBuffer(nas_user_base_for_application, 0);
  495. if (ctx.CanWriteBuffer(1)) {
  496. std::vector<u8> unknown_out_buffer(ctx.GetWriteBufferSize(1));
  497. ctx.WriteBuffer(unknown_out_buffer, 1);
  498. }
  499. IPC::ResponseBuilder rb{ctx, 4};
  500. rb.Push(ResultSuccess);
  501. rb.PushRaw<u64>(profile_manager->GetLastOpenedUser().Hash());
  502. }
  503. void StoreOpenContext(Kernel::HLERequestContext& ctx) {
  504. LOG_DEBUG(Service_ACC, "called");
  505. profile_manager->StoreOpenedUsers();
  506. IPC::ResponseBuilder rb{ctx, 2};
  507. rb.Push(ResultSuccess);
  508. }
  509. std::shared_ptr<EnsureTokenIdCacheAsyncInterface> ensure_token_id{};
  510. std::shared_ptr<ProfileManager> profile_manager;
  511. };
  512. // 6.0.0+
  513. class IAsyncNetworkServiceLicenseKindContext final
  514. : public ServiceFramework<IAsyncNetworkServiceLicenseKindContext> {
  515. public:
  516. explicit IAsyncNetworkServiceLicenseKindContext(Core::System& system_, Common::UUID)
  517. : ServiceFramework{system_, "IAsyncNetworkServiceLicenseKindContext"} {
  518. // clang-format off
  519. static const FunctionInfo functions[] = {
  520. {0, nullptr, "GetSystemEvent"},
  521. {1, nullptr, "Cancel"},
  522. {2, nullptr, "HasDone"},
  523. {3, nullptr, "GetResult"},
  524. {4, nullptr, "GetNetworkServiceLicenseKind"},
  525. };
  526. // clang-format on
  527. RegisterHandlers(functions);
  528. }
  529. };
  530. // 8.0.0+
  531. class IOAuthProcedureForUserRegistration final
  532. : public ServiceFramework<IOAuthProcedureForUserRegistration> {
  533. public:
  534. explicit IOAuthProcedureForUserRegistration(Core::System& system_, Common::UUID)
  535. : ServiceFramework{system_, "IOAuthProcedureForUserRegistration"} {
  536. // clang-format off
  537. static const FunctionInfo functions[] = {
  538. {0, nullptr, "PrepareAsync"},
  539. {1, nullptr, "GetRequest"},
  540. {2, nullptr, "ApplyResponse"},
  541. {3, nullptr, "ApplyResponseAsync"},
  542. {10, nullptr, "Suspend"},
  543. {100, nullptr, "GetAccountId"},
  544. {101, nullptr, "GetLinkedNintendoAccountId"},
  545. {102, nullptr, "GetNickname"},
  546. {103, nullptr, "GetProfileImage"},
  547. {110, nullptr, "RegisterUserAsync"},
  548. {111, nullptr, "GetUid"},
  549. };
  550. // clang-format on
  551. RegisterHandlers(functions);
  552. }
  553. };
  554. class DAUTH_O final : public ServiceFramework<DAUTH_O> {
  555. public:
  556. explicit DAUTH_O(Core::System& system_, Common::UUID) : ServiceFramework{system_, "dauth:o"} {
  557. // clang-format off
  558. static const FunctionInfo functions[] = {
  559. {0, nullptr, "EnsureAuthenticationTokenCacheAsync"},
  560. {1, nullptr, "LoadAuthenticationTokenCache"},
  561. {2, nullptr, "InvalidateAuthenticationTokenCache"},
  562. {10, nullptr, "EnsureEdgeTokenCacheAsync"},
  563. {11, nullptr, "LoadEdgeTokenCache"},
  564. {12, nullptr, "InvalidateEdgeTokenCache"},
  565. {20, nullptr, "EnsureApplicationAuthenticationCacheAsync"},
  566. {21, nullptr, "LoadApplicationAuthenticationTokenCache"},
  567. {22, nullptr, "LoadApplicationNetworkServiceClientConfigCache"},
  568. {23, nullptr, "IsApplicationAuthenticationCacheAvailable"},
  569. {24, nullptr, "InvalidateApplicationAuthenticationCache"},
  570. };
  571. // clang-format on
  572. RegisterHandlers(functions);
  573. }
  574. };
  575. // 6.0.0+
  576. class IAsyncResult final : public ServiceFramework<IAsyncResult> {
  577. public:
  578. explicit IAsyncResult(Core::System& system_, Common::UUID)
  579. : ServiceFramework{system_, "IAsyncResult"} {
  580. // clang-format off
  581. static const FunctionInfo functions[] = {
  582. {0, nullptr, "GetResult"},
  583. {1, nullptr, "Cancel"},
  584. {2, nullptr, "IsAvailable"},
  585. {3, nullptr, "GetSystemEvent"},
  586. };
  587. // clang-format on
  588. RegisterHandlers(functions);
  589. }
  590. };
  591. void Module::Interface::GetUserCount(Kernel::HLERequestContext& ctx) {
  592. LOG_DEBUG(Service_ACC, "called");
  593. IPC::ResponseBuilder rb{ctx, 3};
  594. rb.Push(ResultSuccess);
  595. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  596. }
  597. void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
  598. IPC::RequestParser rp{ctx};
  599. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  600. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  601. IPC::ResponseBuilder rb{ctx, 3};
  602. rb.Push(ResultSuccess);
  603. rb.Push(profile_manager->UserExists(user_id));
  604. }
  605. void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
  606. LOG_DEBUG(Service_ACC, "called");
  607. ctx.WriteBuffer(profile_manager->GetAllUsers());
  608. IPC::ResponseBuilder rb{ctx, 2};
  609. rb.Push(ResultSuccess);
  610. }
  611. void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
  612. LOG_DEBUG(Service_ACC, "called");
  613. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  614. IPC::ResponseBuilder rb{ctx, 2};
  615. rb.Push(ResultSuccess);
  616. }
  617. void Module::Interface::GetLastOpenedUser(Kernel::HLERequestContext& ctx) {
  618. LOG_DEBUG(Service_ACC, "called");
  619. IPC::ResponseBuilder rb{ctx, 6};
  620. rb.Push(ResultSuccess);
  621. rb.PushRaw<Common::UUID>(profile_manager->GetLastOpenedUser());
  622. }
  623. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  624. IPC::RequestParser rp{ctx};
  625. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  626. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  627. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  628. rb.Push(ResultSuccess);
  629. rb.PushIpcInterface<IProfile>(system, user_id, *profile_manager);
  630. }
  631. void Module::Interface::IsUserRegistrationRequestPermitted(Kernel::HLERequestContext& ctx) {
  632. LOG_WARNING(Service_ACC, "(STUBBED) called");
  633. IPC::ResponseBuilder rb{ctx, 3};
  634. rb.Push(ResultSuccess);
  635. rb.Push(profile_manager->CanSystemRegisterUser());
  636. }
  637. void Module::Interface::InitializeApplicationInfo(Kernel::HLERequestContext& ctx) {
  638. LOG_DEBUG(Service_ACC, "called");
  639. IPC::ResponseBuilder rb{ctx, 2};
  640. rb.Push(InitializeApplicationInfoBase());
  641. }
  642. void Module::Interface::InitializeApplicationInfoRestricted(Kernel::HLERequestContext& ctx) {
  643. LOG_WARNING(Service_ACC, "(Partial implementation) called");
  644. // TODO(ogniK): We require checking if the user actually owns the title and what not. As of
  645. // currently, we assume the user owns the title. InitializeApplicationInfoBase SHOULD be called
  646. // first then we do extra checks if the game is a digital copy.
  647. IPC::ResponseBuilder rb{ctx, 2};
  648. rb.Push(InitializeApplicationInfoBase());
  649. }
  650. Result Module::Interface::InitializeApplicationInfoBase() {
  651. if (application_info) {
  652. LOG_ERROR(Service_ACC, "Application already initialized");
  653. return ERR_ACCOUNTINFO_ALREADY_INITIALIZED;
  654. }
  655. // TODO(ogniK): This should be changed to reflect the target process for when we have multiple
  656. // processes emulated. As we don't actually have pid support we should assume we're just using
  657. // our own process
  658. const auto launch_property =
  659. system.GetARPManager().GetLaunchProperty(system.GetCurrentProcessProgramID());
  660. if (launch_property.Failed()) {
  661. LOG_ERROR(Service_ACC, "Failed to get launch property");
  662. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  663. }
  664. switch (launch_property->base_game_storage_id) {
  665. case FileSys::StorageId::GameCard:
  666. application_info.application_type = ApplicationType::GameCard;
  667. break;
  668. case FileSys::StorageId::Host:
  669. case FileSys::StorageId::NandUser:
  670. case FileSys::StorageId::SdCard:
  671. case FileSys::StorageId::None: // Yuzu specific, differs from hardware
  672. application_info.application_type = ApplicationType::Digital;
  673. break;
  674. default:
  675. LOG_ERROR(Service_ACC, "Invalid game storage ID! storage_id={}",
  676. launch_property->base_game_storage_id);
  677. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  678. }
  679. LOG_WARNING(Service_ACC, "ApplicationInfo init required");
  680. // TODO(ogniK): Actual initalization here
  681. return ResultSuccess;
  682. }
  683. void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestContext& ctx) {
  684. LOG_DEBUG(Service_ACC, "called");
  685. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  686. rb.Push(ResultSuccess);
  687. rb.PushIpcInterface<IManagerForApplication>(system, profile_manager);
  688. }
  689. void Module::Interface::IsUserAccountSwitchLocked(Kernel::HLERequestContext& ctx) {
  690. LOG_DEBUG(Service_ACC, "called");
  691. FileSys::NACP nacp;
  692. const auto res = system.GetAppLoader().ReadControlData(nacp);
  693. bool is_locked = false;
  694. if (res != Loader::ResultStatus::Success) {
  695. const FileSys::PatchManager pm{system.GetCurrentProcessProgramID(),
  696. system.GetFileSystemController(),
  697. system.GetContentProvider()};
  698. const auto nacp_unique = pm.GetControlMetadata().first;
  699. if (nacp_unique != nullptr) {
  700. is_locked = nacp_unique->GetUserAccountSwitchLock();
  701. } else {
  702. LOG_ERROR(Service_ACC, "nacp_unique is null!");
  703. }
  704. } else {
  705. is_locked = nacp.GetUserAccountSwitchLock();
  706. }
  707. IPC::ResponseBuilder rb{ctx, 3};
  708. rb.Push(ResultSuccess);
  709. rb.Push(is_locked);
  710. }
  711. void Module::Interface::InitializeApplicationInfoV2(Kernel::HLERequestContext& ctx) {
  712. LOG_WARNING(Service_ACC, "(STUBBED) called");
  713. IPC::ResponseBuilder rb{ctx, 2};
  714. rb.Push(ResultSuccess);
  715. }
  716. void Module::Interface::GetProfileEditor(Kernel::HLERequestContext& ctx) {
  717. IPC::RequestParser rp{ctx};
  718. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  719. LOG_DEBUG(Service_ACC, "called, user_id=0x{}", user_id.RawString());
  720. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  721. rb.Push(ResultSuccess);
  722. rb.PushIpcInterface<IProfileEditor>(system, user_id, *profile_manager);
  723. }
  724. void Module::Interface::ListQualifiedUsers(Kernel::HLERequestContext& ctx) {
  725. LOG_DEBUG(Service_ACC, "called");
  726. // All users should be qualified. We don't actually have parental control or anything to do with
  727. // nintendo online currently. We're just going to assume the user running the game has access to
  728. // the game regardless of parental control settings.
  729. ctx.WriteBuffer(profile_manager->GetAllUsers());
  730. IPC::ResponseBuilder rb{ctx, 2};
  731. rb.Push(ResultSuccess);
  732. }
  733. void Module::Interface::ListOpenContextStoredUsers(Kernel::HLERequestContext& ctx) {
  734. LOG_DEBUG(Service_ACC, "called");
  735. ctx.WriteBuffer(profile_manager->GetStoredOpenedUsers());
  736. IPC::ResponseBuilder rb{ctx, 2};
  737. rb.Push(ResultSuccess);
  738. }
  739. void Module::Interface::StoreSaveDataThumbnailApplication(Kernel::HLERequestContext& ctx) {
  740. IPC::RequestParser rp{ctx};
  741. const auto uuid = rp.PopRaw<Common::UUID>();
  742. LOG_WARNING(Service_ACC, "(STUBBED) called, uuid=0x{}", uuid.RawString());
  743. // TODO(ogniK): Check if application ID is zero on acc initialize. As we don't have a reliable
  744. // way of confirming things like the TID, we're going to assume a non zero value for the time
  745. // being.
  746. constexpr u64 tid{1};
  747. StoreSaveDataThumbnail(ctx, uuid, tid);
  748. }
  749. void Module::Interface::StoreSaveDataThumbnailSystem(Kernel::HLERequestContext& ctx) {
  750. IPC::RequestParser rp{ctx};
  751. const auto uuid = rp.PopRaw<Common::UUID>();
  752. const auto tid = rp.Pop<u64_le>();
  753. LOG_WARNING(Service_ACC, "(STUBBED) called, uuid=0x{}, tid={:016X}", uuid.RawString(), tid);
  754. StoreSaveDataThumbnail(ctx, uuid, tid);
  755. }
  756. void Module::Interface::StoreSaveDataThumbnail(Kernel::HLERequestContext& ctx,
  757. const Common::UUID& uuid, const u64 tid) {
  758. IPC::ResponseBuilder rb{ctx, 2};
  759. if (tid == 0) {
  760. LOG_ERROR(Service_ACC, "TitleID is not valid!");
  761. rb.Push(ERR_INVALID_APPLICATION_ID);
  762. return;
  763. }
  764. if (uuid.IsInvalid()) {
  765. LOG_ERROR(Service_ACC, "User ID is not valid!");
  766. rb.Push(ERR_INVALID_USER_ID);
  767. return;
  768. }
  769. const auto thumbnail_size = ctx.GetReadBufferSize();
  770. if (thumbnail_size != THUMBNAIL_SIZE) {
  771. LOG_ERROR(Service_ACC, "Buffer size is empty! size={:X} expecting {:X}", thumbnail_size,
  772. THUMBNAIL_SIZE);
  773. rb.Push(ERR_INVALID_BUFFER_SIZE);
  774. return;
  775. }
  776. // TODO(ogniK): Construct save data thumbnail
  777. rb.Push(ResultSuccess);
  778. }
  779. void Module::Interface::TrySelectUserWithoutInteraction(Kernel::HLERequestContext& ctx) {
  780. LOG_DEBUG(Service_ACC, "called");
  781. // A u8 is passed into this function which we can safely ignore. It's to determine if we have
  782. // access to use the network or not by the looks of it
  783. IPC::ResponseBuilder rb{ctx, 6};
  784. if (profile_manager->GetUserCount() != 1) {
  785. rb.Push(ResultSuccess);
  786. rb.PushRaw(Common::InvalidUUID);
  787. return;
  788. }
  789. const auto user_list = profile_manager->GetAllUsers();
  790. if (std::ranges::all_of(user_list, [](const auto& user) { return user.IsInvalid(); })) {
  791. rb.Push(ResultUnknown); // TODO(ogniK): Find the correct error code
  792. rb.PushRaw(Common::InvalidUUID);
  793. return;
  794. }
  795. // Select the first user we have
  796. rb.Push(ResultSuccess);
  797. rb.PushRaw(profile_manager->GetUser(0)->uuid);
  798. }
  799. Module::Interface::Interface(std::shared_ptr<Module> module_,
  800. std::shared_ptr<ProfileManager> profile_manager_,
  801. Core::System& system_, const char* name)
  802. : ServiceFramework{system_, name}, module{std::move(module_)}, profile_manager{std::move(
  803. profile_manager_)} {}
  804. Module::Interface::~Interface() = default;
  805. void InstallInterfaces(Core::System& system) {
  806. auto module = std::make_shared<Module>();
  807. auto profile_manager = std::make_shared<ProfileManager>();
  808. std::make_shared<ACC_AA>(module, profile_manager, system)
  809. ->InstallAsService(system.ServiceManager());
  810. std::make_shared<ACC_SU>(module, profile_manager, system)
  811. ->InstallAsService(system.ServiceManager());
  812. std::make_shared<ACC_U0>(module, profile_manager, system)
  813. ->InstallAsService(system.ServiceManager());
  814. std::make_shared<ACC_U1>(module, profile_manager, system)
  815. ->InstallAsService(system.ServiceManager());
  816. }
  817. } // namespace Service::Account