acc.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include "common/common_types.h"
  7. #include "common/fs/file.h"
  8. #include "common/fs/path_util.h"
  9. #include "common/logging/log.h"
  10. #include "common/string_util.h"
  11. #include "common/swap.h"
  12. #include "core/constants.h"
  13. #include "core/core.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/kernel/kernel.h"
  19. #include "core/hle/service/acc/acc.h"
  20. #include "core/hle/service/acc/acc_aa.h"
  21. #include "core/hle/service/acc/acc_su.h"
  22. #include "core/hle/service/acc/acc_u0.h"
  23. #include "core/hle/service/acc/acc_u1.h"
  24. #include "core/hle/service/acc/async_context.h"
  25. #include "core/hle/service/acc/errors.h"
  26. #include "core/hle/service/acc/profile_manager.h"
  27. #include "core/hle/service/glue/glue_manager.h"
  28. #include "core/loader/loader.h"
  29. namespace Service::Account {
  30. constexpr ResultCode ERR_INVALID_USER_ID{ErrorModule::Account, 20};
  31. constexpr ResultCode ERR_INVALID_APPLICATION_ID{ErrorModule::Account, 22};
  32. constexpr ResultCode ERR_INVALID_BUFFER{ErrorModule::Account, 30};
  33. constexpr ResultCode ERR_INVALID_BUFFER_SIZE{ErrorModule::Account, 31};
  34. constexpr ResultCode ERR_FAILED_SAVE_DATA{ErrorModule::Account, 100};
  35. // Thumbnails are hard coded to be at least this size
  36. constexpr std::size_t THUMBNAIL_SIZE = 0x24000;
  37. static std::filesystem::path GetImagePath(const Common::NewUUID& uuid) {
  38. return Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir) /
  39. fmt::format("system/save/8000000000000010/su/avators/{}.jpg", uuid.FormattedString());
  40. }
  41. static constexpr u32 SanitizeJPEGSize(std::size_t size) {
  42. constexpr std::size_t max_jpeg_image_size = 0x20000;
  43. return static_cast<u32>(std::min(size, max_jpeg_image_size));
  44. }
  45. class IManagerForSystemService final : public ServiceFramework<IManagerForSystemService> {
  46. public:
  47. explicit IManagerForSystemService(Core::System& system_, Common::NewUUID)
  48. : ServiceFramework{system_, "IManagerForSystemService"} {
  49. // clang-format off
  50. static const FunctionInfo functions[] = {
  51. {0, nullptr, "CheckAvailability"},
  52. {1, nullptr, "GetAccountId"},
  53. {2, nullptr, "EnsureIdTokenCacheAsync"},
  54. {3, nullptr, "LoadIdTokenCache"},
  55. {100, nullptr, "SetSystemProgramIdentification"},
  56. {101, nullptr, "RefreshNotificationTokenAsync"}, // 7.0.0+
  57. {110, nullptr, "GetServiceEntryRequirementCache"}, // 4.0.0+
  58. {111, nullptr, "InvalidateServiceEntryRequirementCache"}, // 4.0.0+
  59. {112, nullptr, "InvalidateTokenCache"}, // 4.0.0 - 6.2.0
  60. {113, nullptr, "GetServiceEntryRequirementCacheForOnlinePlay"}, // 6.1.0+
  61. {120, nullptr, "GetNintendoAccountId"},
  62. {121, nullptr, "CalculateNintendoAccountAuthenticationFingerprint"}, // 9.0.0+
  63. {130, nullptr, "GetNintendoAccountUserResourceCache"},
  64. {131, nullptr, "RefreshNintendoAccountUserResourceCacheAsync"},
  65. {132, nullptr, "RefreshNintendoAccountUserResourceCacheAsyncIfSecondsElapsed"},
  66. {133, nullptr, "GetNintendoAccountVerificationUrlCache"}, // 9.0.0+
  67. {134, nullptr, "RefreshNintendoAccountVerificationUrlCache"}, // 9.0.0+
  68. {135, nullptr, "RefreshNintendoAccountVerificationUrlCacheAsyncIfSecondsElapsed"}, // 9.0.0+
  69. {140, nullptr, "GetNetworkServiceLicenseCache"}, // 5.0.0+
  70. {141, nullptr, "RefreshNetworkServiceLicenseCacheAsync"}, // 5.0.0+
  71. {142, nullptr, "RefreshNetworkServiceLicenseCacheAsyncIfSecondsElapsed"}, // 5.0.0+
  72. {150, nullptr, "CreateAuthorizationRequest"},
  73. };
  74. // clang-format on
  75. RegisterHandlers(functions);
  76. }
  77. };
  78. // 3.0.0+
  79. class IFloatingRegistrationRequest final : public ServiceFramework<IFloatingRegistrationRequest> {
  80. public:
  81. explicit IFloatingRegistrationRequest(Core::System& system_, Common::NewUUID)
  82. : ServiceFramework{system_, "IFloatingRegistrationRequest"} {
  83. // clang-format off
  84. static const FunctionInfo functions[] = {
  85. {0, nullptr, "GetSessionId"},
  86. {12, nullptr, "GetAccountId"},
  87. {13, nullptr, "GetLinkedNintendoAccountId"},
  88. {14, nullptr, "GetNickname"},
  89. {15, nullptr, "GetProfileImage"},
  90. {21, nullptr, "LoadIdTokenCache"},
  91. {100, nullptr, "RegisterUser"}, // [1.0.0-3.0.2] RegisterAsync
  92. {101, nullptr, "RegisterUserWithUid"}, // [1.0.0-3.0.2] RegisterWithUidAsync
  93. {102, nullptr, "RegisterNetworkServiceAccountAsync"}, // 4.0.0+
  94. {103, nullptr, "RegisterNetworkServiceAccountWithUidAsync"}, // 4.0.0+
  95. {110, nullptr, "SetSystemProgramIdentification"},
  96. {111, nullptr, "EnsureIdTokenCacheAsync"},
  97. };
  98. // clang-format on
  99. RegisterHandlers(functions);
  100. }
  101. };
  102. class IAdministrator final : public ServiceFramework<IAdministrator> {
  103. public:
  104. explicit IAdministrator(Core::System& system_, Common::NewUUID)
  105. : ServiceFramework{system_, "IAdministrator"} {
  106. // clang-format off
  107. static const FunctionInfo functions[] = {
  108. {0, nullptr, "CheckAvailability"},
  109. {1, nullptr, "GetAccountId"},
  110. {2, nullptr, "EnsureIdTokenCacheAsync"},
  111. {3, nullptr, "LoadIdTokenCache"},
  112. {100, nullptr, "SetSystemProgramIdentification"},
  113. {101, nullptr, "RefreshNotificationTokenAsync"}, // 7.0.0+
  114. {110, nullptr, "GetServiceEntryRequirementCache"}, // 4.0.0+
  115. {111, nullptr, "InvalidateServiceEntryRequirementCache"}, // 4.0.0+
  116. {112, nullptr, "InvalidateTokenCache"}, // 4.0.0 - 6.2.0
  117. {113, nullptr, "GetServiceEntryRequirementCacheForOnlinePlay"}, // 6.1.0+
  118. {120, nullptr, "GetNintendoAccountId"},
  119. {121, nullptr, "CalculateNintendoAccountAuthenticationFingerprint"}, // 9.0.0+
  120. {130, nullptr, "GetNintendoAccountUserResourceCache"},
  121. {131, nullptr, "RefreshNintendoAccountUserResourceCacheAsync"},
  122. {132, nullptr, "RefreshNintendoAccountUserResourceCacheAsyncIfSecondsElapsed"},
  123. {133, nullptr, "GetNintendoAccountVerificationUrlCache"}, // 9.0.0+
  124. {134, nullptr, "RefreshNintendoAccountVerificationUrlCacheAsync"}, // 9.0.0+
  125. {135, nullptr, "RefreshNintendoAccountVerificationUrlCacheAsyncIfSecondsElapsed"}, // 9.0.0+
  126. {140, nullptr, "GetNetworkServiceLicenseCache"}, // 5.0.0+
  127. {141, nullptr, "RefreshNetworkServiceLicenseCacheAsync"}, // 5.0.0+
  128. {142, nullptr, "RefreshNetworkServiceLicenseCacheAsyncIfSecondsElapsed"}, // 5.0.0+
  129. {150, nullptr, "CreateAuthorizationRequest"},
  130. {200, nullptr, "IsRegistered"},
  131. {201, nullptr, "RegisterAsync"},
  132. {202, nullptr, "UnregisterAsync"},
  133. {203, nullptr, "DeleteRegistrationInfoLocally"},
  134. {220, nullptr, "SynchronizeProfileAsync"},
  135. {221, nullptr, "UploadProfileAsync"},
  136. {222, nullptr, "SynchronizaProfileAsyncIfSecondsElapsed"},
  137. {250, nullptr, "IsLinkedWithNintendoAccount"},
  138. {251, nullptr, "CreateProcedureToLinkWithNintendoAccount"},
  139. {252, nullptr, "ResumeProcedureToLinkWithNintendoAccount"},
  140. {255, nullptr, "CreateProcedureToUpdateLinkageStateOfNintendoAccount"},
  141. {256, nullptr, "ResumeProcedureToUpdateLinkageStateOfNintendoAccount"},
  142. {260, nullptr, "CreateProcedureToLinkNnidWithNintendoAccount"}, // 3.0.0+
  143. {261, nullptr, "ResumeProcedureToLinkNnidWithNintendoAccount"}, // 3.0.0+
  144. {280, nullptr, "ProxyProcedureToAcquireApplicationAuthorizationForNintendoAccount"},
  145. {290, nullptr, "GetRequestForNintendoAccountUserResourceView"}, // 8.0.0+
  146. {300, nullptr, "TryRecoverNintendoAccountUserStateAsync"}, // 6.0.0+
  147. {400, nullptr, "IsServiceEntryRequirementCacheRefreshRequiredForOnlinePlay"}, // 6.1.0+
  148. {401, nullptr, "RefreshServiceEntryRequirementCacheForOnlinePlayAsync"}, // 6.1.0+
  149. {900, nullptr, "GetAuthenticationInfoForWin"}, // 9.0.0+
  150. {901, nullptr, "ImportAsyncForWin"}, // 9.0.0+
  151. {997, nullptr, "DebugUnlinkNintendoAccountAsync"},
  152. {998, nullptr, "DebugSetAvailabilityErrorDetail"},
  153. };
  154. // clang-format on
  155. RegisterHandlers(functions);
  156. }
  157. };
  158. class IAuthorizationRequest final : public ServiceFramework<IAuthorizationRequest> {
  159. public:
  160. explicit IAuthorizationRequest(Core::System& system_, Common::NewUUID)
  161. : ServiceFramework{system_, "IAuthorizationRequest"} {
  162. // clang-format off
  163. static const FunctionInfo functions[] = {
  164. {0, nullptr, "GetSessionId"},
  165. {10, nullptr, "InvokeWithoutInteractionAsync"},
  166. {19, nullptr, "IsAuthorized"},
  167. {20, nullptr, "GetAuthorizationCode"},
  168. {21, nullptr, "GetIdToken"},
  169. {22, nullptr, "GetState"},
  170. };
  171. // clang-format on
  172. RegisterHandlers(functions);
  173. }
  174. };
  175. class IOAuthProcedure final : public ServiceFramework<IOAuthProcedure> {
  176. public:
  177. explicit IOAuthProcedure(Core::System& system_, Common::NewUUID)
  178. : ServiceFramework{system_, "IOAuthProcedure"} {
  179. // clang-format off
  180. static const FunctionInfo functions[] = {
  181. {0, nullptr, "PrepareAsync"},
  182. {1, nullptr, "GetRequest"},
  183. {2, nullptr, "ApplyResponse"},
  184. {3, nullptr, "ApplyResponseAsync"},
  185. {10, nullptr, "Suspend"},
  186. };
  187. // clang-format on
  188. RegisterHandlers(functions);
  189. }
  190. };
  191. // 3.0.0+
  192. class IOAuthProcedureForExternalNsa final : public ServiceFramework<IOAuthProcedureForExternalNsa> {
  193. public:
  194. explicit IOAuthProcedureForExternalNsa(Core::System& system_, Common::NewUUID)
  195. : ServiceFramework{system_, "IOAuthProcedureForExternalNsa"} {
  196. // clang-format off
  197. static const FunctionInfo functions[] = {
  198. {0, nullptr, "PrepareAsync"},
  199. {1, nullptr, "GetRequest"},
  200. {2, nullptr, "ApplyResponse"},
  201. {3, nullptr, "ApplyResponseAsync"},
  202. {10, nullptr, "Suspend"},
  203. {100, nullptr, "GetAccountId"},
  204. {101, nullptr, "GetLinkedNintendoAccountId"},
  205. {102, nullptr, "GetNickname"},
  206. {103, nullptr, "GetProfileImage"},
  207. };
  208. // clang-format on
  209. RegisterHandlers(functions);
  210. }
  211. };
  212. class IOAuthProcedureForNintendoAccountLinkage final
  213. : public ServiceFramework<IOAuthProcedureForNintendoAccountLinkage> {
  214. public:
  215. explicit IOAuthProcedureForNintendoAccountLinkage(Core::System& system_, Common::NewUUID)
  216. : ServiceFramework{system_, "IOAuthProcedureForNintendoAccountLinkage"} {
  217. // clang-format off
  218. static const FunctionInfo functions[] = {
  219. {0, nullptr, "PrepareAsync"},
  220. {1, nullptr, "GetRequest"},
  221. {2, nullptr, "ApplyResponse"},
  222. {3, nullptr, "ApplyResponseAsync"},
  223. {10, nullptr, "Suspend"},
  224. {100, nullptr, "GetRequestWithTheme"},
  225. {101, nullptr, "IsNetworkServiceAccountReplaced"},
  226. {199, nullptr, "GetUrlForIntroductionOfExtraMembership"}, // 2.0.0 - 5.1.0
  227. };
  228. // clang-format on
  229. RegisterHandlers(functions);
  230. }
  231. };
  232. class INotifier final : public ServiceFramework<INotifier> {
  233. public:
  234. explicit INotifier(Core::System& system_, Common::NewUUID)
  235. : ServiceFramework{system_, "INotifier"} {
  236. // clang-format off
  237. static const FunctionInfo functions[] = {
  238. {0, nullptr, "GetSystemEvent"},
  239. };
  240. // clang-format on
  241. RegisterHandlers(functions);
  242. }
  243. };
  244. class IProfileCommon : public ServiceFramework<IProfileCommon> {
  245. public:
  246. explicit IProfileCommon(Core::System& system_, const char* name, bool editor_commands,
  247. Common::NewUUID user_id_, ProfileManager& profile_manager_)
  248. : ServiceFramework{system_, name}, profile_manager{profile_manager_}, user_id{user_id_} {
  249. static const FunctionInfo functions[] = {
  250. {0, &IProfileCommon::Get, "Get"},
  251. {1, &IProfileCommon::GetBase, "GetBase"},
  252. {10, &IProfileCommon::GetImageSize, "GetImageSize"},
  253. {11, &IProfileCommon::LoadImage, "LoadImage"},
  254. };
  255. RegisterHandlers(functions);
  256. if (editor_commands) {
  257. static const FunctionInfo editor_functions[] = {
  258. {100, &IProfileCommon::Store, "Store"},
  259. {101, &IProfileCommon::StoreWithImage, "StoreWithImage"},
  260. };
  261. RegisterHandlers(editor_functions);
  262. }
  263. }
  264. protected:
  265. void Get(Kernel::HLERequestContext& ctx) {
  266. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  267. ProfileBase profile_base{};
  268. ProfileData data{};
  269. if (profile_manager.GetProfileBaseAndData(user_id, profile_base, data)) {
  270. ctx.WriteBuffer(data);
  271. IPC::ResponseBuilder rb{ctx, 16};
  272. rb.Push(ResultSuccess);
  273. rb.PushRaw(profile_base);
  274. } else {
  275. LOG_ERROR(Service_ACC, "Failed to get profile base and data for user=0x{}",
  276. user_id.RawString());
  277. IPC::ResponseBuilder rb{ctx, 2};
  278. rb.Push(ResultUnknown); // TODO(ogniK): Get actual error code
  279. }
  280. }
  281. void GetBase(Kernel::HLERequestContext& ctx) {
  282. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  283. ProfileBase profile_base{};
  284. if (profile_manager.GetProfileBase(user_id, profile_base)) {
  285. IPC::ResponseBuilder rb{ctx, 16};
  286. rb.Push(ResultSuccess);
  287. rb.PushRaw(profile_base);
  288. } else {
  289. LOG_ERROR(Service_ACC, "Failed to get profile base for user=0x{}", user_id.RawString());
  290. IPC::ResponseBuilder rb{ctx, 2};
  291. rb.Push(ResultUnknown); // TODO(ogniK): Get actual error code
  292. }
  293. }
  294. void LoadImage(Kernel::HLERequestContext& ctx) {
  295. LOG_DEBUG(Service_ACC, "called");
  296. IPC::ResponseBuilder rb{ctx, 3};
  297. rb.Push(ResultSuccess);
  298. const Common::FS::IOFile image(GetImagePath(user_id), Common::FS::FileAccessMode::Read,
  299. Common::FS::FileType::BinaryFile);
  300. if (!image.IsOpen()) {
  301. LOG_WARNING(Service_ACC,
  302. "Failed to load user provided image! Falling back to built-in backup...");
  303. ctx.WriteBuffer(Core::Constants::ACCOUNT_BACKUP_JPEG);
  304. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  305. return;
  306. }
  307. const u32 size = SanitizeJPEGSize(image.GetSize());
  308. std::vector<u8> buffer(size);
  309. if (image.Read(buffer) != buffer.size()) {
  310. LOG_ERROR(Service_ACC, "Failed to read all the bytes in the user provided image.");
  311. }
  312. ctx.WriteBuffer(buffer);
  313. rb.Push<u32>(size);
  314. }
  315. void GetImageSize(Kernel::HLERequestContext& ctx) {
  316. LOG_DEBUG(Service_ACC, "called");
  317. IPC::ResponseBuilder rb{ctx, 3};
  318. rb.Push(ResultSuccess);
  319. const Common::FS::IOFile image(GetImagePath(user_id), Common::FS::FileAccessMode::Read,
  320. Common::FS::FileType::BinaryFile);
  321. if (!image.IsOpen()) {
  322. LOG_WARNING(Service_ACC,
  323. "Failed to load user provided image! Falling back to built-in backup...");
  324. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  325. } else {
  326. rb.Push(SanitizeJPEGSize(image.GetSize()));
  327. }
  328. }
  329. void Store(Kernel::HLERequestContext& ctx) {
  330. IPC::RequestParser rp{ctx};
  331. const auto base = rp.PopRaw<ProfileBase>();
  332. const auto user_data = ctx.ReadBuffer();
  333. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid=0x{}",
  334. Common::StringFromFixedZeroTerminatedBuffer(
  335. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  336. base.timestamp, base.user_uuid.RawString());
  337. if (user_data.size() < sizeof(ProfileData)) {
  338. LOG_ERROR(Service_ACC, "ProfileData buffer too small!");
  339. IPC::ResponseBuilder rb{ctx, 2};
  340. rb.Push(ERR_INVALID_BUFFER);
  341. return;
  342. }
  343. ProfileData data;
  344. std::memcpy(&data, user_data.data(), sizeof(ProfileData));
  345. if (!profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  346. LOG_ERROR(Service_ACC, "Failed to update profile data and base!");
  347. IPC::ResponseBuilder rb{ctx, 2};
  348. rb.Push(ERR_FAILED_SAVE_DATA);
  349. return;
  350. }
  351. IPC::ResponseBuilder rb{ctx, 2};
  352. rb.Push(ResultSuccess);
  353. }
  354. void StoreWithImage(Kernel::HLERequestContext& ctx) {
  355. IPC::RequestParser rp{ctx};
  356. const auto base = rp.PopRaw<ProfileBase>();
  357. const auto user_data = ctx.ReadBuffer();
  358. const auto image_data = ctx.ReadBuffer(1);
  359. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid=0x{}",
  360. Common::StringFromFixedZeroTerminatedBuffer(
  361. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  362. base.timestamp, base.user_uuid.RawString());
  363. if (user_data.size() < sizeof(ProfileData)) {
  364. LOG_ERROR(Service_ACC, "ProfileData buffer too small!");
  365. IPC::ResponseBuilder rb{ctx, 2};
  366. rb.Push(ERR_INVALID_BUFFER);
  367. return;
  368. }
  369. ProfileData data;
  370. std::memcpy(&data, user_data.data(), sizeof(ProfileData));
  371. Common::FS::IOFile image(GetImagePath(user_id), Common::FS::FileAccessMode::Write,
  372. Common::FS::FileType::BinaryFile);
  373. if (!image.IsOpen() || !image.SetSize(image_data.size()) ||
  374. image.Write(image_data) != image_data.size() ||
  375. !profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  376. LOG_ERROR(Service_ACC, "Failed to update profile data, base, and image!");
  377. IPC::ResponseBuilder rb{ctx, 2};
  378. rb.Push(ERR_FAILED_SAVE_DATA);
  379. return;
  380. }
  381. IPC::ResponseBuilder rb{ctx, 2};
  382. rb.Push(ResultSuccess);
  383. }
  384. ProfileManager& profile_manager;
  385. Common::NewUUID user_id{}; ///< The user id this profile refers to.
  386. };
  387. class IProfile final : public IProfileCommon {
  388. public:
  389. explicit IProfile(Core::System& system_, Common::NewUUID user_id_,
  390. ProfileManager& profile_manager_)
  391. : IProfileCommon{system_, "IProfile", false, user_id_, profile_manager_} {}
  392. };
  393. class IProfileEditor final : public IProfileCommon {
  394. public:
  395. explicit IProfileEditor(Core::System& system_, Common::NewUUID user_id_,
  396. ProfileManager& profile_manager_)
  397. : IProfileCommon{system_, "IProfileEditor", true, user_id_, profile_manager_} {}
  398. };
  399. class ISessionObject final : public ServiceFramework<ISessionObject> {
  400. public:
  401. explicit ISessionObject(Core::System& system_, Common::NewUUID)
  402. : ServiceFramework{system_, "ISessionObject"} {
  403. // clang-format off
  404. static const FunctionInfo functions[] = {
  405. {999, nullptr, "Dummy"},
  406. };
  407. // clang-format on
  408. RegisterHandlers(functions);
  409. }
  410. };
  411. class IGuestLoginRequest final : public ServiceFramework<IGuestLoginRequest> {
  412. public:
  413. explicit IGuestLoginRequest(Core::System& system_, Common::NewUUID)
  414. : ServiceFramework{system_, "IGuestLoginRequest"} {
  415. // clang-format off
  416. static const FunctionInfo functions[] = {
  417. {0, nullptr, "GetSessionId"},
  418. {11, nullptr, "Unknown"}, // 1.0.0 - 2.3.0 (the name is blank on Switchbrew)
  419. {12, nullptr, "GetAccountId"},
  420. {13, nullptr, "GetLinkedNintendoAccountId"},
  421. {14, nullptr, "GetNickname"},
  422. {15, nullptr, "GetProfileImage"},
  423. {21, nullptr, "LoadIdTokenCache"}, // 3.0.0+
  424. };
  425. // clang-format on
  426. RegisterHandlers(functions);
  427. }
  428. };
  429. class EnsureTokenIdCacheAsyncInterface final : public IAsyncContext {
  430. public:
  431. explicit EnsureTokenIdCacheAsyncInterface(Core::System& system_) : IAsyncContext{system_} {
  432. MarkComplete();
  433. }
  434. ~EnsureTokenIdCacheAsyncInterface() = default;
  435. void LoadIdTokenCache(Kernel::HLERequestContext& ctx) {
  436. LOG_WARNING(Service_ACC, "(STUBBED) called");
  437. IPC::ResponseBuilder rb{ctx, 2};
  438. rb.Push(ResultSuccess);
  439. }
  440. protected:
  441. bool IsComplete() const override {
  442. return true;
  443. }
  444. void Cancel() override {}
  445. ResultCode GetResult() const override {
  446. return ResultSuccess;
  447. }
  448. };
  449. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  450. public:
  451. explicit IManagerForApplication(Core::System& system_, Common::NewUUID user_id_)
  452. : ServiceFramework{system_, "IManagerForApplication"},
  453. ensure_token_id{std::make_shared<EnsureTokenIdCacheAsyncInterface>(system)},
  454. user_id{user_id_} {
  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_WARNING(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>(user_id.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>(user_id.Hash());
  503. }
  504. void StoreOpenContext(Kernel::HLERequestContext& ctx) {
  505. LOG_WARNING(Service_ACC, "(STUBBED) called");
  506. IPC::ResponseBuilder rb{ctx, 2};
  507. rb.Push(ResultSuccess);
  508. }
  509. std::shared_ptr<EnsureTokenIdCacheAsyncInterface> ensure_token_id{};
  510. Common::NewUUID user_id{};
  511. };
  512. // 6.0.0+
  513. class IAsyncNetworkServiceLicenseKindContext final
  514. : public ServiceFramework<IAsyncNetworkServiceLicenseKindContext> {
  515. public:
  516. explicit IAsyncNetworkServiceLicenseKindContext(Core::System& system_, Common::NewUUID)
  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::NewUUID)
  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::NewUUID)
  557. : 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::NewUUID)
  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::NewUUID user_id = rp.PopRaw<Common::NewUUID>();
  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::NewUUID>(profile_manager->GetLastOpenedUser());
  623. }
  624. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  625. IPC::RequestParser rp{ctx};
  626. Common::NewUUID user_id = rp.PopRaw<Common::NewUUID>();
  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. ResultCode 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.GetCurrentProcessProgramID());
  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->GetLastOpenedUser());
  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.GetCurrentProcessProgramID(),
  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::NewUUID user_id = rp.PopRaw<Common::NewUUID>();
  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::LoadOpenContext(Kernel::HLERequestContext& ctx) {
  735. LOG_WARNING(Service_ACC, "(STUBBED) called");
  736. // This is similar to GetBaasAccountManagerForApplication
  737. // This command is used concurrently with ListOpenContextStoredUsers
  738. // TODO: Find the differences between this and GetBaasAccountManagerForApplication
  739. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  740. rb.Push(ResultSuccess);
  741. rb.PushIpcInterface<IManagerForApplication>(system, profile_manager->GetLastOpenedUser());
  742. }
  743. void Module::Interface::ListOpenContextStoredUsers(Kernel::HLERequestContext& ctx) {
  744. LOG_WARNING(Service_ACC, "(STUBBED) called");
  745. // TODO(ogniK): Handle open contexts
  746. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  747. IPC::ResponseBuilder rb{ctx, 2};
  748. rb.Push(ResultSuccess);
  749. }
  750. void Module::Interface::StoreSaveDataThumbnailApplication(Kernel::HLERequestContext& ctx) {
  751. IPC::RequestParser rp{ctx};
  752. const auto uuid = rp.PopRaw<Common::NewUUID>();
  753. LOG_WARNING(Service_ACC, "(STUBBED) called, uuid=0x{}", uuid.RawString());
  754. // TODO(ogniK): Check if application ID is zero on acc initialize. As we don't have a reliable
  755. // way of confirming things like the TID, we're going to assume a non zero value for the time
  756. // being.
  757. constexpr u64 tid{1};
  758. StoreSaveDataThumbnail(ctx, uuid, tid);
  759. }
  760. void Module::Interface::StoreSaveDataThumbnailSystem(Kernel::HLERequestContext& ctx) {
  761. IPC::RequestParser rp{ctx};
  762. const auto uuid = rp.PopRaw<Common::NewUUID>();
  763. const auto tid = rp.Pop<u64_le>();
  764. LOG_WARNING(Service_ACC, "(STUBBED) called, uuid=0x{}, tid={:016X}", uuid.RawString(), tid);
  765. StoreSaveDataThumbnail(ctx, uuid, tid);
  766. }
  767. void Module::Interface::StoreSaveDataThumbnail(Kernel::HLERequestContext& ctx,
  768. const Common::NewUUID& uuid, const u64 tid) {
  769. IPC::ResponseBuilder rb{ctx, 2};
  770. if (tid == 0) {
  771. LOG_ERROR(Service_ACC, "TitleID is not valid!");
  772. rb.Push(ERR_INVALID_APPLICATION_ID);
  773. return;
  774. }
  775. if (uuid.IsInvalid()) {
  776. LOG_ERROR(Service_ACC, "User ID is not valid!");
  777. rb.Push(ERR_INVALID_USER_ID);
  778. return;
  779. }
  780. const auto thumbnail_size = ctx.GetReadBufferSize();
  781. if (thumbnail_size != THUMBNAIL_SIZE) {
  782. LOG_ERROR(Service_ACC, "Buffer size is empty! size={:X} expecting {:X}", thumbnail_size,
  783. THUMBNAIL_SIZE);
  784. rb.Push(ERR_INVALID_BUFFER_SIZE);
  785. return;
  786. }
  787. // TODO(ogniK): Construct save data thumbnail
  788. rb.Push(ResultSuccess);
  789. }
  790. void Module::Interface::TrySelectUserWithoutInteraction(Kernel::HLERequestContext& ctx) {
  791. LOG_DEBUG(Service_ACC, "called");
  792. // A u8 is passed into this function which we can safely ignore. It's to determine if we have
  793. // access to use the network or not by the looks of it
  794. IPC::ResponseBuilder rb{ctx, 6};
  795. if (profile_manager->GetUserCount() != 1) {
  796. rb.Push(ResultSuccess);
  797. rb.PushRaw(Common::InvalidUUID);
  798. return;
  799. }
  800. const auto user_list = profile_manager->GetAllUsers();
  801. if (std::ranges::all_of(user_list, [](const auto& user) { return user.IsInvalid(); })) {
  802. rb.Push(ResultUnknown); // TODO(ogniK): Find the correct error code
  803. rb.PushRaw(Common::InvalidUUID);
  804. return;
  805. }
  806. // Select the first user we have
  807. rb.Push(ResultSuccess);
  808. rb.PushRaw(profile_manager->GetUser(0)->uuid);
  809. }
  810. Module::Interface::Interface(std::shared_ptr<Module> module_,
  811. std::shared_ptr<ProfileManager> profile_manager_,
  812. Core::System& system_, const char* name)
  813. : ServiceFramework{system_, name}, module{std::move(module_)}, profile_manager{std::move(
  814. profile_manager_)} {}
  815. Module::Interface::~Interface() = default;
  816. void InstallInterfaces(Core::System& system) {
  817. auto module = std::make_shared<Module>();
  818. auto profile_manager = std::make_shared<ProfileManager>();
  819. std::make_shared<ACC_AA>(module, profile_manager, system)
  820. ->InstallAsService(system.ServiceManager());
  821. std::make_shared<ACC_SU>(module, profile_manager, system)
  822. ->InstallAsService(system.ServiceManager());
  823. std::make_shared<ACC_U0>(module, profile_manager, system)
  824. ->InstallAsService(system.ServiceManager());
  825. std::make_shared<ACC_U1>(module, profile_manager, system)
  826. ->InstallAsService(system.ServiceManager());
  827. }
  828. } // namespace Service::Account