acc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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_timing.h"
  14. #include "core/file_sys/control_metadata.h"
  15. #include "core/file_sys/patch_manager.h"
  16. #include "core/hle/ipc_helpers.h"
  17. #include "core/hle/kernel/kernel.h"
  18. #include "core/hle/kernel/process.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/errors.h"
  25. #include "core/hle/service/acc/profile_manager.h"
  26. #include "core/hle/service/glue/arp.h"
  27. #include "core/hle/service/glue/manager.h"
  28. #include "core/hle/service/sm/sm.h"
  29. #include "core/loader/loader.h"
  30. namespace Service::Account {
  31. constexpr ResultCode ERR_INVALID_BUFFER_SIZE{ErrorModule::Account, 30};
  32. constexpr ResultCode ERR_FAILED_SAVE_DATA{ErrorModule::Account, 100};
  33. static std::string GetImagePath(Common::UUID uuid) {
  34. return FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
  35. "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
  36. }
  37. static constexpr u32 SanitizeJPEGSize(std::size_t size) {
  38. constexpr std::size_t max_jpeg_image_size = 0x20000;
  39. return static_cast<u32>(std::min(size, max_jpeg_image_size));
  40. }
  41. class IProfileCommon : public ServiceFramework<IProfileCommon> {
  42. public:
  43. explicit IProfileCommon(const char* name, bool editor_commands, Common::UUID user_id,
  44. ProfileManager& profile_manager)
  45. : ServiceFramework(name), profile_manager(profile_manager), user_id(user_id) {
  46. static const FunctionInfo functions[] = {
  47. {0, &IProfileCommon::Get, "Get"},
  48. {1, &IProfileCommon::GetBase, "GetBase"},
  49. {10, &IProfileCommon::GetImageSize, "GetImageSize"},
  50. {11, &IProfileCommon::LoadImage, "LoadImage"},
  51. };
  52. RegisterHandlers(functions);
  53. if (editor_commands) {
  54. static const FunctionInfo editor_functions[] = {
  55. {100, &IProfileCommon::Store, "Store"},
  56. {101, &IProfileCommon::StoreWithImage, "StoreWithImage"},
  57. };
  58. RegisterHandlers(editor_functions);
  59. }
  60. }
  61. protected:
  62. void Get(Kernel::HLERequestContext& ctx) {
  63. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  64. ProfileBase profile_base{};
  65. ProfileData data{};
  66. if (profile_manager.GetProfileBaseAndData(user_id, profile_base, data)) {
  67. std::array<u8, sizeof(ProfileData)> raw_data;
  68. std::memcpy(raw_data.data(), &data, sizeof(ProfileData));
  69. ctx.WriteBuffer(raw_data);
  70. IPC::ResponseBuilder rb{ctx, 16};
  71. rb.Push(RESULT_SUCCESS);
  72. rb.PushRaw(profile_base);
  73. } else {
  74. LOG_ERROR(Service_ACC, "Failed to get profile base and data for user={}",
  75. user_id.Format());
  76. IPC::ResponseBuilder rb{ctx, 2};
  77. rb.Push(RESULT_UNKNOWN); // TODO(ogniK): Get actual error code
  78. }
  79. }
  80. void GetBase(Kernel::HLERequestContext& ctx) {
  81. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  82. ProfileBase profile_base{};
  83. if (profile_manager.GetProfileBase(user_id, profile_base)) {
  84. IPC::ResponseBuilder rb{ctx, 16};
  85. rb.Push(RESULT_SUCCESS);
  86. rb.PushRaw(profile_base);
  87. } else {
  88. LOG_ERROR(Service_ACC, "Failed to get profile base for user={}", user_id.Format());
  89. IPC::ResponseBuilder rb{ctx, 2};
  90. rb.Push(RESULT_UNKNOWN); // TODO(ogniK): Get actual error code
  91. }
  92. }
  93. void LoadImage(Kernel::HLERequestContext& ctx) {
  94. LOG_DEBUG(Service_ACC, "called");
  95. IPC::ResponseBuilder rb{ctx, 3};
  96. rb.Push(RESULT_SUCCESS);
  97. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  98. if (!image.IsOpen()) {
  99. LOG_WARNING(Service_ACC,
  100. "Failed to load user provided image! Falling back to built-in backup...");
  101. ctx.WriteBuffer(Core::Constants::ACCOUNT_BACKUP_JPEG);
  102. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  103. return;
  104. }
  105. const u32 size = SanitizeJPEGSize(image.GetSize());
  106. std::vector<u8> buffer(size);
  107. image.ReadBytes(buffer.data(), buffer.size());
  108. ctx.WriteBuffer(buffer.data(), buffer.size());
  109. rb.Push<u32>(size);
  110. }
  111. void GetImageSize(Kernel::HLERequestContext& ctx) {
  112. LOG_DEBUG(Service_ACC, "called");
  113. IPC::ResponseBuilder rb{ctx, 3};
  114. rb.Push(RESULT_SUCCESS);
  115. const FileUtil::IOFile image(GetImagePath(user_id), "rb");
  116. if (!image.IsOpen()) {
  117. LOG_WARNING(Service_ACC,
  118. "Failed to load user provided image! Falling back to built-in backup...");
  119. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  120. } else {
  121. rb.Push(SanitizeJPEGSize(image.GetSize()));
  122. }
  123. }
  124. void Store(Kernel::HLERequestContext& ctx) {
  125. IPC::RequestParser rp{ctx};
  126. const auto base = rp.PopRaw<ProfileBase>();
  127. const auto user_data = ctx.ReadBuffer();
  128. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid={}",
  129. Common::StringFromFixedZeroTerminatedBuffer(
  130. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  131. base.timestamp, base.user_uuid.Format());
  132. if (user_data.size() < sizeof(ProfileData)) {
  133. LOG_ERROR(Service_ACC, "ProfileData buffer too small!");
  134. IPC::ResponseBuilder rb{ctx, 2};
  135. rb.Push(ERR_INVALID_BUFFER_SIZE);
  136. return;
  137. }
  138. ProfileData data;
  139. std::memcpy(&data, user_data.data(), sizeof(ProfileData));
  140. if (!profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  141. LOG_ERROR(Service_ACC, "Failed to update profile data and base!");
  142. IPC::ResponseBuilder rb{ctx, 2};
  143. rb.Push(ERR_FAILED_SAVE_DATA);
  144. return;
  145. }
  146. IPC::ResponseBuilder rb{ctx, 2};
  147. rb.Push(RESULT_SUCCESS);
  148. }
  149. void StoreWithImage(Kernel::HLERequestContext& ctx) {
  150. IPC::RequestParser rp{ctx};
  151. const auto base = rp.PopRaw<ProfileBase>();
  152. const auto user_data = ctx.ReadBuffer();
  153. const auto image_data = ctx.ReadBuffer(1);
  154. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid={}",
  155. Common::StringFromFixedZeroTerminatedBuffer(
  156. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  157. base.timestamp, base.user_uuid.Format());
  158. if (user_data.size() < sizeof(ProfileData)) {
  159. LOG_ERROR(Service_ACC, "ProfileData buffer too small!");
  160. IPC::ResponseBuilder rb{ctx, 2};
  161. rb.Push(ERR_INVALID_BUFFER_SIZE);
  162. return;
  163. }
  164. ProfileData data;
  165. std::memcpy(&data, user_data.data(), sizeof(ProfileData));
  166. FileUtil::IOFile image(GetImagePath(user_id), "wb");
  167. if (!image.IsOpen() || !image.Resize(image_data.size()) ||
  168. image.WriteBytes(image_data.data(), image_data.size()) != image_data.size() ||
  169. !profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  170. LOG_ERROR(Service_ACC, "Failed to update profile data, base, and image!");
  171. IPC::ResponseBuilder rb{ctx, 2};
  172. rb.Push(ERR_FAILED_SAVE_DATA);
  173. return;
  174. }
  175. IPC::ResponseBuilder rb{ctx, 2};
  176. rb.Push(RESULT_SUCCESS);
  177. }
  178. ProfileManager& profile_manager;
  179. Common::UUID user_id; ///< The user id this profile refers to.
  180. };
  181. class IProfile final : public IProfileCommon {
  182. public:
  183. IProfile(Common::UUID user_id, ProfileManager& profile_manager)
  184. : IProfileCommon("IProfile", false, user_id, profile_manager) {}
  185. };
  186. class IProfileEditor final : public IProfileCommon {
  187. public:
  188. IProfileEditor(Common::UUID user_id, ProfileManager& profile_manager)
  189. : IProfileCommon("IProfileEditor", true, user_id, profile_manager) {}
  190. };
  191. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  192. public:
  193. IManagerForApplication() : ServiceFramework("IManagerForApplication") {
  194. // clang-format off
  195. static const FunctionInfo functions[] = {
  196. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  197. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  198. {2, nullptr, "EnsureIdTokenCacheAsync"},
  199. {3, nullptr, "LoadIdTokenCache"},
  200. {130, nullptr, "GetNintendoAccountUserResourceCacheForApplication"},
  201. {150, nullptr, "CreateAuthorizationRequest"},
  202. {160, nullptr, "StoreOpenContext"},
  203. {170, nullptr, "LoadNetworkServiceLicenseKindAsync"},
  204. };
  205. // clang-format on
  206. RegisterHandlers(functions);
  207. }
  208. private:
  209. void CheckAvailability(Kernel::HLERequestContext& ctx) {
  210. LOG_WARNING(Service_ACC, "(STUBBED) called");
  211. IPC::ResponseBuilder rb{ctx, 3};
  212. rb.Push(RESULT_SUCCESS);
  213. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  214. }
  215. void GetAccountId(Kernel::HLERequestContext& ctx) {
  216. LOG_WARNING(Service_ACC, "(STUBBED) called");
  217. // Should return a nintendo account ID
  218. IPC::ResponseBuilder rb{ctx, 4};
  219. rb.Push(RESULT_SUCCESS);
  220. rb.PushRaw<u64>(1);
  221. }
  222. };
  223. void Module::Interface::GetUserCount(Kernel::HLERequestContext& ctx) {
  224. LOG_DEBUG(Service_ACC, "called");
  225. IPC::ResponseBuilder rb{ctx, 3};
  226. rb.Push(RESULT_SUCCESS);
  227. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  228. }
  229. void Module::Interface::GetUserExistence(Kernel::HLERequestContext& ctx) {
  230. IPC::RequestParser rp{ctx};
  231. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  232. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  233. IPC::ResponseBuilder rb{ctx, 3};
  234. rb.Push(RESULT_SUCCESS);
  235. rb.Push(profile_manager->UserExists(user_id));
  236. }
  237. void Module::Interface::ListAllUsers(Kernel::HLERequestContext& ctx) {
  238. LOG_DEBUG(Service_ACC, "called");
  239. ctx.WriteBuffer(profile_manager->GetAllUsers());
  240. IPC::ResponseBuilder rb{ctx, 2};
  241. rb.Push(RESULT_SUCCESS);
  242. }
  243. void Module::Interface::ListOpenUsers(Kernel::HLERequestContext& ctx) {
  244. LOG_DEBUG(Service_ACC, "called");
  245. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  246. IPC::ResponseBuilder rb{ctx, 2};
  247. rb.Push(RESULT_SUCCESS);
  248. }
  249. void Module::Interface::GetLastOpenedUser(Kernel::HLERequestContext& ctx) {
  250. LOG_DEBUG(Service_ACC, "called");
  251. IPC::ResponseBuilder rb{ctx, 6};
  252. rb.Push(RESULT_SUCCESS);
  253. rb.PushRaw<Common::UUID>(profile_manager->GetLastOpenedUser());
  254. }
  255. void Module::Interface::GetProfile(Kernel::HLERequestContext& ctx) {
  256. IPC::RequestParser rp{ctx};
  257. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  258. LOG_DEBUG(Service_ACC, "called user_id={}", user_id.Format());
  259. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  260. rb.Push(RESULT_SUCCESS);
  261. rb.PushIpcInterface<IProfile>(user_id, *profile_manager);
  262. }
  263. void Module::Interface::IsUserRegistrationRequestPermitted(Kernel::HLERequestContext& ctx) {
  264. LOG_WARNING(Service_ACC, "(STUBBED) called");
  265. IPC::ResponseBuilder rb{ctx, 3};
  266. rb.Push(RESULT_SUCCESS);
  267. rb.Push(profile_manager->CanSystemRegisterUser());
  268. }
  269. void Module::Interface::InitializeApplicationInfo(Kernel::HLERequestContext& ctx) {
  270. IPC::RequestParser rp{ctx};
  271. auto pid = rp.Pop<u64>();
  272. LOG_DEBUG(Service_ACC, "called, process_id={}", pid);
  273. IPC::ResponseBuilder rb{ctx, 2};
  274. rb.Push(InitializeApplicationInfoBase(pid));
  275. }
  276. void Module::Interface::InitializeApplicationInfoRestricted(Kernel::HLERequestContext& ctx) {
  277. IPC::RequestParser rp{ctx};
  278. auto pid = rp.Pop<u64>();
  279. LOG_WARNING(Service_ACC, "(Partial implementation) called, process_id={}", pid);
  280. // TODO(ogniK): We require checking if the user actually owns the title and what not. As of
  281. // currently, we assume the user owns the title. InitializeApplicationInfoBase SHOULD be called
  282. // first then we do extra checks if the game is a digital copy.
  283. IPC::ResponseBuilder rb{ctx, 2};
  284. rb.Push(InitializeApplicationInfoBase(pid));
  285. }
  286. ResultCode Module::Interface::InitializeApplicationInfoBase(u64 process_id) {
  287. if (application_info) {
  288. LOG_ERROR(Service_ACC, "Application already initialized");
  289. return ERR_ACCOUNTINFO_ALREADY_INITIALIZED;
  290. }
  291. const auto& list = system.Kernel().GetProcessList();
  292. const auto iter = std::find_if(list.begin(), list.end(), [&process_id](const auto& process) {
  293. return process->GetProcessID() == process_id;
  294. });
  295. if (iter == list.end()) {
  296. LOG_ERROR(Service_ACC, "Failed to find process ID");
  297. application_info.application_type = ApplicationType::Unknown;
  298. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  299. }
  300. const auto launch_property = system.GetARPManager().GetLaunchProperty((*iter)->GetTitleID());
  301. if (launch_property.Failed()) {
  302. LOG_ERROR(Service_ACC, "Failed to get launch property");
  303. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  304. }
  305. switch (launch_property->base_game_storage_id) {
  306. case FileSys::StorageId::GameCard:
  307. application_info.application_type = ApplicationType::GameCard;
  308. break;
  309. case FileSys::StorageId::Host:
  310. case FileSys::StorageId::NandUser:
  311. case FileSys::StorageId::SdCard:
  312. application_info.application_type = ApplicationType::Digital;
  313. break;
  314. default:
  315. LOG_ERROR(Service_ACC, "Invalid game storage ID");
  316. return ERR_ACCOUNTINFO_BAD_APPLICATION;
  317. }
  318. LOG_WARNING(Service_ACC, "ApplicationInfo init required");
  319. // TODO(ogniK): Actual initalization here
  320. return RESULT_SUCCESS;
  321. }
  322. void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestContext& ctx) {
  323. LOG_DEBUG(Service_ACC, "called");
  324. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  325. rb.Push(RESULT_SUCCESS);
  326. rb.PushIpcInterface<IManagerForApplication>();
  327. }
  328. void Module::Interface::IsUserAccountSwitchLocked(Kernel::HLERequestContext& ctx) {
  329. LOG_DEBUG(Service_ACC, "called");
  330. FileSys::NACP nacp;
  331. const auto res = system.GetAppLoader().ReadControlData(nacp);
  332. bool is_locked = false;
  333. if (res != Loader::ResultStatus::Success) {
  334. FileSys::PatchManager pm{system.CurrentProcess()->GetTitleID()};
  335. auto nacp_unique = pm.GetControlMetadata().first;
  336. if (nacp_unique != nullptr) {
  337. is_locked = nacp_unique->GetUserAccountSwitchLock();
  338. } else {
  339. LOG_ERROR(Service_ACC, "nacp_unique is null!");
  340. }
  341. } else {
  342. is_locked = nacp.GetUserAccountSwitchLock();
  343. }
  344. IPC::ResponseBuilder rb{ctx, 3};
  345. rb.Push(RESULT_SUCCESS);
  346. rb.Push(is_locked);
  347. }
  348. void Module::Interface::GetProfileEditor(Kernel::HLERequestContext& ctx) {
  349. IPC::RequestParser rp{ctx};
  350. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  351. LOG_DEBUG(Service_ACC, "called, user_id={}", user_id.Format());
  352. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  353. rb.Push(RESULT_SUCCESS);
  354. rb.PushIpcInterface<IProfileEditor>(user_id, *profile_manager);
  355. }
  356. void Module::Interface::TrySelectUserWithoutInteraction(Kernel::HLERequestContext& ctx) {
  357. LOG_DEBUG(Service_ACC, "called");
  358. // A u8 is passed into this function which we can safely ignore. It's to determine if we have
  359. // access to use the network or not by the looks of it
  360. IPC::ResponseBuilder rb{ctx, 6};
  361. if (profile_manager->GetUserCount() != 1) {
  362. rb.Push(RESULT_SUCCESS);
  363. rb.PushRaw<u128>(Common::INVALID_UUID);
  364. return;
  365. }
  366. const auto user_list = profile_manager->GetAllUsers();
  367. if (std::all_of(user_list.begin(), user_list.end(),
  368. [](const auto& user) { return user.uuid == Common::INVALID_UUID; })) {
  369. rb.Push(RESULT_UNKNOWN); // TODO(ogniK): Find the correct error code
  370. rb.PushRaw<u128>(Common::INVALID_UUID);
  371. return;
  372. }
  373. // Select the first user we have
  374. rb.Push(RESULT_SUCCESS);
  375. rb.PushRaw<u128>(profile_manager->GetUser(0)->uuid);
  376. }
  377. Module::Interface::Interface(std::shared_ptr<Module> module,
  378. std::shared_ptr<ProfileManager> profile_manager, Core::System& system,
  379. const char* name)
  380. : ServiceFramework(name), module(std::move(module)),
  381. profile_manager(std::move(profile_manager)), system(system) {}
  382. Module::Interface::~Interface() = default;
  383. void InstallInterfaces(Core::System& system) {
  384. auto module = std::make_shared<Module>();
  385. auto profile_manager = std::make_shared<ProfileManager>();
  386. std::make_shared<ACC_AA>(module, profile_manager, system)
  387. ->InstallAsService(system.ServiceManager());
  388. std::make_shared<ACC_SU>(module, profile_manager, system)
  389. ->InstallAsService(system.ServiceManager());
  390. std::make_shared<ACC_U0>(module, profile_manager, system)
  391. ->InstallAsService(system.ServiceManager());
  392. std::make_shared<ACC_U1>(module, profile_manager, system)
  393. ->InstallAsService(system.ServiceManager());
  394. }
  395. } // namespace Service::Account