arp.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include "common/logging/log.h"
  6. #include "core/core.h"
  7. #include "core/file_sys/control_metadata.h"
  8. #include "core/hle/ipc_helpers.h"
  9. #include "core/hle/kernel/hle_ipc.h"
  10. #include "core/hle/kernel/kernel.h"
  11. #include "core/hle/kernel/process.h"
  12. #include "core/hle/service/glue/arp.h"
  13. #include "core/hle/service/glue/errors.h"
  14. #include "core/hle/service/glue/manager.h"
  15. #include "core/hle/service/service.h"
  16. namespace Service::Glue {
  17. namespace {
  18. std::optional<u64> GetTitleIDForProcessID(const Core::System& system, u64 process_id) {
  19. const auto& list = system.Kernel().GetProcessList();
  20. const auto iter = std::find_if(list.begin(), list.end(), [&process_id](const auto& process) {
  21. return process->GetProcessID() == process_id;
  22. });
  23. if (iter == list.end()) {
  24. return std::nullopt;
  25. }
  26. return (*iter)->GetTitleID();
  27. }
  28. } // Anonymous namespace
  29. ARP_R::ARP_R(const Core::System& system, const ARPManager& manager)
  30. : ServiceFramework{"arp:r"}, system(system), manager(manager) {
  31. // clang-format off
  32. static const FunctionInfo functions[] = {
  33. {0, &ARP_R::GetApplicationLaunchProperty, "GetApplicationLaunchProperty"},
  34. {1, &ARP_R::GetApplicationLaunchPropertyWithApplicationId, "GetApplicationLaunchPropertyWithApplicationId"},
  35. {2, &ARP_R::GetApplicationControlProperty, "GetApplicationControlProperty"},
  36. {3, &ARP_R::GetApplicationControlPropertyWithApplicationId, "GetApplicationControlPropertyWithApplicationId"},
  37. };
  38. // clang-format on
  39. RegisterHandlers(functions);
  40. }
  41. ARP_R::~ARP_R() = default;
  42. void ARP_R::GetApplicationLaunchProperty(Kernel::HLERequestContext& ctx) {
  43. IPC::RequestParser rp{ctx};
  44. const auto process_id = rp.PopRaw<u64>();
  45. LOG_DEBUG(Service_ARP, "called, process_id={:016X}", process_id);
  46. const auto title_id = GetTitleIDForProcessID(system, process_id);
  47. if (!title_id.has_value()) {
  48. LOG_ERROR(Service_ARP, "Failed to get title ID for process ID!");
  49. IPC::ResponseBuilder rb{ctx, 2};
  50. rb.Push(ERR_NOT_REGISTERED);
  51. return;
  52. }
  53. const auto res = manager.GetLaunchProperty(*title_id);
  54. if (res.Failed()) {
  55. LOG_ERROR(Service_ARP, "Failed to get launch property!");
  56. IPC::ResponseBuilder rb{ctx, 2};
  57. rb.Push(res.Code());
  58. return;
  59. }
  60. IPC::ResponseBuilder rb{ctx, 6};
  61. rb.Push(RESULT_SUCCESS);
  62. rb.PushRaw(*res);
  63. }
  64. void ARP_R::GetApplicationLaunchPropertyWithApplicationId(Kernel::HLERequestContext& ctx) {
  65. IPC::RequestParser rp{ctx};
  66. const auto title_id = rp.PopRaw<u64>();
  67. LOG_DEBUG(Service_ARP, "called, title_id={:016X}", title_id);
  68. const auto res = manager.GetLaunchProperty(title_id);
  69. if (res.Failed()) {
  70. LOG_ERROR(Service_ARP, "Failed to get launch property!");
  71. IPC::ResponseBuilder rb{ctx, 2};
  72. rb.Push(res.Code());
  73. return;
  74. }
  75. IPC::ResponseBuilder rb{ctx, 6};
  76. rb.Push(RESULT_SUCCESS);
  77. rb.PushRaw(*res);
  78. }
  79. void ARP_R::GetApplicationControlProperty(Kernel::HLERequestContext& ctx) {
  80. IPC::RequestParser rp{ctx};
  81. const auto process_id = rp.PopRaw<u64>();
  82. LOG_DEBUG(Service_ARP, "called, process_id={:016X}", process_id);
  83. const auto title_id = GetTitleIDForProcessID(system, process_id);
  84. if (!title_id.has_value()) {
  85. LOG_ERROR(Service_ARP, "Failed to get title ID for process ID!");
  86. IPC::ResponseBuilder rb{ctx, 2};
  87. rb.Push(ERR_NOT_REGISTERED);
  88. return;
  89. }
  90. const auto res = manager.GetControlProperty(*title_id);
  91. if (res.Failed()) {
  92. LOG_ERROR(Service_ARP, "Failed to get control property!");
  93. IPC::ResponseBuilder rb{ctx, 2};
  94. rb.Push(res.Code());
  95. return;
  96. }
  97. ctx.WriteBuffer(*res);
  98. IPC::ResponseBuilder rb{ctx, 2};
  99. rb.Push(RESULT_SUCCESS);
  100. }
  101. void ARP_R::GetApplicationControlPropertyWithApplicationId(Kernel::HLERequestContext& ctx) {
  102. IPC::RequestParser rp{ctx};
  103. const auto title_id = rp.PopRaw<u64>();
  104. LOG_DEBUG(Service_ARP, "called, title_id={:016X}", title_id);
  105. const auto res = manager.GetControlProperty(title_id);
  106. if (res.Failed()) {
  107. LOG_ERROR(Service_ARP, "Failed to get control property!");
  108. IPC::ResponseBuilder rb{ctx, 2};
  109. rb.Push(res.Code());
  110. return;
  111. }
  112. ctx.WriteBuffer(*res);
  113. IPC::ResponseBuilder rb{ctx, 2};
  114. rb.Push(RESULT_SUCCESS);
  115. }
  116. class IRegistrar final : public ServiceFramework<IRegistrar> {
  117. friend class ARP_W;
  118. public:
  119. explicit IRegistrar(
  120. std::function<ResultCode(u64, ApplicationLaunchProperty, std::vector<u8>)> issuer)
  121. : ServiceFramework{"IRegistrar"}, issue_process_id(std::move(issuer)) {
  122. // clang-format off
  123. static const FunctionInfo functions[] = {
  124. {0, &IRegistrar::Issue, "Issue"},
  125. {1, &IRegistrar::SetApplicationLaunchProperty, "SetApplicationLaunchProperty"},
  126. {2, &IRegistrar::SetApplicationControlProperty, "SetApplicationControlProperty"},
  127. };
  128. // clang-format on
  129. RegisterHandlers(functions);
  130. }
  131. private:
  132. void Issue(Kernel::HLERequestContext& ctx) {
  133. IPC::RequestParser rp{ctx};
  134. const auto process_id = rp.PopRaw<u64>();
  135. LOG_DEBUG(Service_ARP, "called, process_id={:016X}", process_id);
  136. if (process_id == 0) {
  137. LOG_ERROR(Service_ARP, "Must have non-zero process ID!");
  138. IPC::ResponseBuilder rb{ctx, 2};
  139. rb.Push(ERR_INVALID_PROCESS_ID);
  140. return;
  141. }
  142. if (issued) {
  143. LOG_ERROR(Service_ARP,
  144. "Attempted to issue registrar, but registrar is already issued!");
  145. IPC::ResponseBuilder rb{ctx, 2};
  146. rb.Push(ERR_INVALID_ACCESS);
  147. return;
  148. }
  149. issue_process_id(process_id, launch, std::move(control));
  150. issued = true;
  151. IPC::ResponseBuilder rb{ctx, 2};
  152. rb.Push(RESULT_SUCCESS);
  153. }
  154. void SetApplicationLaunchProperty(Kernel::HLERequestContext& ctx) {
  155. LOG_DEBUG(Service_ARP, "called");
  156. if (issued) {
  157. LOG_ERROR(
  158. Service_ARP,
  159. "Attempted to set application launch property, but registrar is already issued!");
  160. IPC::ResponseBuilder rb{ctx, 2};
  161. rb.Push(ERR_INVALID_ACCESS);
  162. return;
  163. }
  164. IPC::RequestParser rp{ctx};
  165. launch = rp.PopRaw<ApplicationLaunchProperty>();
  166. IPC::ResponseBuilder rb{ctx, 2};
  167. rb.Push(RESULT_SUCCESS);
  168. }
  169. void SetApplicationControlProperty(Kernel::HLERequestContext& ctx) {
  170. LOG_DEBUG(Service_ARP, "called");
  171. if (issued) {
  172. LOG_ERROR(
  173. Service_ARP,
  174. "Attempted to set application control property, but registrar is already issued!");
  175. IPC::ResponseBuilder rb{ctx, 2};
  176. rb.Push(ERR_INVALID_ACCESS);
  177. return;
  178. }
  179. control = ctx.ReadBuffer();
  180. IPC::ResponseBuilder rb{ctx, 2};
  181. rb.Push(RESULT_SUCCESS);
  182. }
  183. std::function<ResultCode(u64, ApplicationLaunchProperty, std::vector<u8>)> issue_process_id;
  184. bool issued = false;
  185. ApplicationLaunchProperty launch;
  186. std::vector<u8> control;
  187. };
  188. ARP_W::ARP_W(const Core::System& system, ARPManager& manager)
  189. : ServiceFramework{"arp:w"}, system(system), manager(manager) {
  190. // clang-format off
  191. static const FunctionInfo functions[] = {
  192. {0, &ARP_W::AcquireRegistrar, "AcquireRegistrar"},
  193. {1, &ARP_W::DeleteProperties, "DeleteProperties"},
  194. };
  195. // clang-format on
  196. RegisterHandlers(functions);
  197. }
  198. ARP_W::~ARP_W() = default;
  199. void ARP_W::AcquireRegistrar(Kernel::HLERequestContext& ctx) {
  200. LOG_DEBUG(Service_ARP, "called");
  201. registrar = std::make_shared<IRegistrar>(
  202. [this](u64 process_id, ApplicationLaunchProperty launch, std::vector<u8> control) {
  203. const auto res = GetTitleIDForProcessID(system, process_id);
  204. if (!res.has_value()) {
  205. return ERR_NOT_REGISTERED;
  206. }
  207. return manager.Register(*res, launch, std::move(control));
  208. });
  209. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  210. rb.Push(RESULT_SUCCESS);
  211. rb.PushIpcInterface(registrar);
  212. }
  213. void ARP_W::DeleteProperties(Kernel::HLERequestContext& ctx) {
  214. IPC::RequestParser rp{ctx};
  215. const auto process_id = rp.PopRaw<u64>();
  216. LOG_DEBUG(Service_ARP, "called, process_id={:016X}", process_id);
  217. if (process_id == 0) {
  218. LOG_ERROR(Service_ARP, "Must have non-zero process ID!");
  219. IPC::ResponseBuilder rb{ctx, 2};
  220. rb.Push(ERR_INVALID_PROCESS_ID);
  221. return;
  222. }
  223. const auto title_id = GetTitleIDForProcessID(system, process_id);
  224. if (!title_id.has_value()) {
  225. LOG_ERROR(Service_ARP, "No title ID for process ID!");
  226. IPC::ResponseBuilder rb{ctx, 2};
  227. rb.Push(ERR_NOT_REGISTERED);
  228. return;
  229. }
  230. IPC::ResponseBuilder rb{ctx, 2};
  231. rb.Push(manager.Unregister(*title_id));
  232. }
  233. } // namespace Service::Glue