acc.cpp 38 KB

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