acc.cpp 36 KB

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