module.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cctype>
  5. #include <mbedtls/md5.h>
  6. #include "backend/boxcat.h"
  7. #include "common/hex_util.h"
  8. #include "common/logging/log.h"
  9. #include "common/settings.h"
  10. #include "common/string_util.h"
  11. #include "core/core.h"
  12. #include "core/file_sys/vfs.h"
  13. #include "core/hle/ipc_helpers.h"
  14. #include "core/hle/kernel/k_process.h"
  15. #include "core/hle/kernel/k_readable_event.h"
  16. #include "core/hle/kernel/k_writable_event.h"
  17. #include "core/hle/service/bcat/backend/backend.h"
  18. #include "core/hle/service/bcat/bcat.h"
  19. #include "core/hle/service/bcat/module.h"
  20. #include "core/hle/service/filesystem/filesystem.h"
  21. namespace Service::BCAT {
  22. constexpr ResultCode ERROR_INVALID_ARGUMENT{ErrorModule::BCAT, 1};
  23. constexpr ResultCode ERROR_FAILED_OPEN_ENTITY{ErrorModule::BCAT, 2};
  24. constexpr ResultCode ERROR_ENTITY_ALREADY_OPEN{ErrorModule::BCAT, 6};
  25. constexpr ResultCode ERROR_NO_OPEN_ENTITY{ErrorModule::BCAT, 7};
  26. // The command to clear the delivery cache just calls fs IFileSystem DeleteFile on all of the files
  27. // and if any of them have a non-zero result it just forwards that result. This is the FS error code
  28. // for permission denied, which is the closest approximation of this scenario.
  29. constexpr ResultCode ERROR_FAILED_CLEAR_CACHE{ErrorModule::FS, 6400};
  30. using BCATDigest = std::array<u8, 0x10>;
  31. namespace {
  32. u64 GetCurrentBuildID(const Core::System::CurrentBuildProcessID& id) {
  33. u64 out{};
  34. std::memcpy(&out, id.data(), sizeof(u64));
  35. return out;
  36. }
  37. // The digest is only used to determine if a file is unique compared to others of the same name.
  38. // Since the algorithm isn't ever checked in game, MD5 is safe.
  39. BCATDigest DigestFile(const FileSys::VirtualFile& file) {
  40. BCATDigest out{};
  41. const auto bytes = file->ReadAllBytes();
  42. mbedtls_md5_ret(bytes.data(), bytes.size(), out.data());
  43. return out;
  44. }
  45. // For a name to be valid it must be non-empty, must have a null terminating character as the final
  46. // char, can only contain numbers, letters, underscores and a hyphen if directory and a period if
  47. // file.
  48. bool VerifyNameValidInternal(Kernel::HLERequestContext& ctx, std::array<char, 0x20> name,
  49. char match_char) {
  50. const auto null_chars = std::count(name.begin(), name.end(), 0);
  51. const auto bad_chars = std::count_if(name.begin(), name.end(), [match_char](char c) {
  52. return !std::isalnum(static_cast<u8>(c)) && c != '_' && c != match_char && c != '\0';
  53. });
  54. if (null_chars == 0x20 || null_chars == 0 || bad_chars != 0 || name[0x1F] != '\0') {
  55. LOG_ERROR(Service_BCAT, "Name passed was invalid!");
  56. IPC::ResponseBuilder rb{ctx, 2};
  57. rb.Push(ERROR_INVALID_ARGUMENT);
  58. return false;
  59. }
  60. return true;
  61. }
  62. bool VerifyNameValidDir(Kernel::HLERequestContext& ctx, DirectoryName name) {
  63. return VerifyNameValidInternal(ctx, name, '-');
  64. }
  65. bool VerifyNameValidFile(Kernel::HLERequestContext& ctx, FileName name) {
  66. return VerifyNameValidInternal(ctx, name, '.');
  67. }
  68. } // Anonymous namespace
  69. struct DeliveryCacheDirectoryEntry {
  70. FileName name;
  71. u64 size;
  72. BCATDigest digest;
  73. };
  74. class IDeliveryCacheProgressService final : public ServiceFramework<IDeliveryCacheProgressService> {
  75. public:
  76. explicit IDeliveryCacheProgressService(Core::System& system_, Kernel::KReadableEvent& event_,
  77. const DeliveryCacheProgressImpl& impl_)
  78. : ServiceFramework{system_, "IDeliveryCacheProgressService"}, event{event_}, impl{impl_} {
  79. // clang-format off
  80. static const FunctionInfo functions[] = {
  81. {0, &IDeliveryCacheProgressService::GetEvent, "GetEvent"},
  82. {1, &IDeliveryCacheProgressService::GetImpl, "GetImpl"},
  83. };
  84. // clang-format on
  85. RegisterHandlers(functions);
  86. }
  87. private:
  88. void GetEvent(Kernel::HLERequestContext& ctx) {
  89. LOG_DEBUG(Service_BCAT, "called");
  90. IPC::ResponseBuilder rb{ctx, 2, 1};
  91. rb.Push(RESULT_SUCCESS);
  92. rb.PushCopyObjects(event);
  93. }
  94. void GetImpl(Kernel::HLERequestContext& ctx) {
  95. LOG_DEBUG(Service_BCAT, "called");
  96. ctx.WriteBuffer(impl);
  97. IPC::ResponseBuilder rb{ctx, 2};
  98. rb.Push(RESULT_SUCCESS);
  99. }
  100. Kernel::KReadableEvent& event;
  101. const DeliveryCacheProgressImpl& impl;
  102. };
  103. class IBcatService final : public ServiceFramework<IBcatService> {
  104. public:
  105. explicit IBcatService(Core::System& system_, Backend& backend_)
  106. : ServiceFramework{system_, "IBcatService"}, backend{backend_},
  107. progress{{
  108. ProgressServiceBackend{system_.Kernel(), "Normal"},
  109. ProgressServiceBackend{system_.Kernel(), "Directory"},
  110. }} {
  111. // clang-format off
  112. static const FunctionInfo functions[] = {
  113. {10100, &IBcatService::RequestSyncDeliveryCache, "RequestSyncDeliveryCache"},
  114. {10101, &IBcatService::RequestSyncDeliveryCacheWithDirectoryName, "RequestSyncDeliveryCacheWithDirectoryName"},
  115. {10200, nullptr, "CancelSyncDeliveryCacheRequest"},
  116. {20100, nullptr, "RequestSyncDeliveryCacheWithApplicationId"},
  117. {20101, nullptr, "RequestSyncDeliveryCacheWithApplicationIdAndDirectoryName"},
  118. {20300, nullptr, "GetDeliveryCacheStorageUpdateNotifier"},
  119. {20301, nullptr, "RequestSuspendDeliveryTask"},
  120. {20400, nullptr, "RegisterSystemApplicationDeliveryTask"},
  121. {20401, nullptr, "UnregisterSystemApplicationDeliveryTask"},
  122. {20410, nullptr, "SetSystemApplicationDeliveryTaskTimer"},
  123. {30100, &IBcatService::SetPassphrase, "SetPassphrase"},
  124. {30101, nullptr, "Unknown"},
  125. {30102, nullptr, "Unknown2"},
  126. {30200, nullptr, "RegisterBackgroundDeliveryTask"},
  127. {30201, nullptr, "UnregisterBackgroundDeliveryTask"},
  128. {30202, nullptr, "BlockDeliveryTask"},
  129. {30203, nullptr, "UnblockDeliveryTask"},
  130. {30210, nullptr, "SetDeliveryTaskTimer"},
  131. {30300, nullptr, "RegisterSystemApplicationDeliveryTasks"},
  132. {90100, nullptr, "EnumerateBackgroundDeliveryTask"},
  133. {90101, nullptr, "Unknown90101"},
  134. {90200, nullptr, "GetDeliveryList"},
  135. {90201, &IBcatService::ClearDeliveryCacheStorage, "ClearDeliveryCacheStorage"},
  136. {90202, nullptr, "ClearDeliveryTaskSubscriptionStatus"},
  137. {90300, nullptr, "GetPushNotificationLog"},
  138. {90301, nullptr, "Unknown90301"},
  139. };
  140. // clang-format on
  141. RegisterHandlers(functions);
  142. }
  143. private:
  144. enum class SyncType {
  145. Normal,
  146. Directory,
  147. Count,
  148. };
  149. std::shared_ptr<IDeliveryCacheProgressService> CreateProgressService(SyncType type) {
  150. auto& progress_backend{GetProgressBackend(type)};
  151. return std::make_shared<IDeliveryCacheProgressService>(system, progress_backend.GetEvent(),
  152. progress_backend.GetImpl());
  153. }
  154. void RequestSyncDeliveryCache(Kernel::HLERequestContext& ctx) {
  155. LOG_DEBUG(Service_BCAT, "called");
  156. backend.Synchronize({system.CurrentProcess()->GetTitleID(),
  157. GetCurrentBuildID(system.GetCurrentProcessBuildID())},
  158. GetProgressBackend(SyncType::Normal));
  159. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  160. rb.Push(RESULT_SUCCESS);
  161. rb.PushIpcInterface(CreateProgressService(SyncType::Normal));
  162. }
  163. void RequestSyncDeliveryCacheWithDirectoryName(Kernel::HLERequestContext& ctx) {
  164. IPC::RequestParser rp{ctx};
  165. const auto name_raw = rp.PopRaw<DirectoryName>();
  166. const auto name =
  167. Common::StringFromFixedZeroTerminatedBuffer(name_raw.data(), name_raw.size());
  168. LOG_DEBUG(Service_BCAT, "called, name={}", name);
  169. backend.SynchronizeDirectory({system.CurrentProcess()->GetTitleID(),
  170. GetCurrentBuildID(system.GetCurrentProcessBuildID())},
  171. name, GetProgressBackend(SyncType::Directory));
  172. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  173. rb.Push(RESULT_SUCCESS);
  174. rb.PushIpcInterface(CreateProgressService(SyncType::Directory));
  175. }
  176. void SetPassphrase(Kernel::HLERequestContext& ctx) {
  177. IPC::RequestParser rp{ctx};
  178. const auto title_id = rp.PopRaw<u64>();
  179. const auto passphrase_raw = ctx.ReadBuffer();
  180. LOG_DEBUG(Service_BCAT, "called, title_id={:016X}, passphrase={}", title_id,
  181. Common::HexToString(passphrase_raw));
  182. if (title_id == 0) {
  183. LOG_ERROR(Service_BCAT, "Invalid title ID!");
  184. IPC::ResponseBuilder rb{ctx, 2};
  185. rb.Push(ERROR_INVALID_ARGUMENT);
  186. }
  187. if (passphrase_raw.size() > 0x40) {
  188. LOG_ERROR(Service_BCAT, "Passphrase too large!");
  189. IPC::ResponseBuilder rb{ctx, 2};
  190. rb.Push(ERROR_INVALID_ARGUMENT);
  191. return;
  192. }
  193. Passphrase passphrase{};
  194. std::memcpy(passphrase.data(), passphrase_raw.data(),
  195. std::min(passphrase.size(), passphrase_raw.size()));
  196. backend.SetPassphrase(title_id, passphrase);
  197. IPC::ResponseBuilder rb{ctx, 2};
  198. rb.Push(RESULT_SUCCESS);
  199. }
  200. void ClearDeliveryCacheStorage(Kernel::HLERequestContext& ctx) {
  201. IPC::RequestParser rp{ctx};
  202. const auto title_id = rp.PopRaw<u64>();
  203. LOG_DEBUG(Service_BCAT, "called, title_id={:016X}", title_id);
  204. if (title_id == 0) {
  205. LOG_ERROR(Service_BCAT, "Invalid title ID!");
  206. IPC::ResponseBuilder rb{ctx, 2};
  207. rb.Push(ERROR_INVALID_ARGUMENT);
  208. return;
  209. }
  210. if (!backend.Clear(title_id)) {
  211. LOG_ERROR(Service_BCAT, "Could not clear the directory successfully!");
  212. IPC::ResponseBuilder rb{ctx, 2};
  213. rb.Push(ERROR_FAILED_CLEAR_CACHE);
  214. return;
  215. }
  216. IPC::ResponseBuilder rb{ctx, 2};
  217. rb.Push(RESULT_SUCCESS);
  218. }
  219. ProgressServiceBackend& GetProgressBackend(SyncType type) {
  220. return progress.at(static_cast<size_t>(type));
  221. }
  222. const ProgressServiceBackend& GetProgressBackend(SyncType type) const {
  223. return progress.at(static_cast<size_t>(type));
  224. }
  225. Backend& backend;
  226. std::array<ProgressServiceBackend, static_cast<size_t>(SyncType::Count)> progress;
  227. };
  228. void Module::Interface::CreateBcatService(Kernel::HLERequestContext& ctx) {
  229. LOG_DEBUG(Service_BCAT, "called");
  230. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  231. rb.Push(RESULT_SUCCESS);
  232. rb.PushIpcInterface<IBcatService>(system, *backend);
  233. }
  234. class IDeliveryCacheFileService final : public ServiceFramework<IDeliveryCacheFileService> {
  235. public:
  236. explicit IDeliveryCacheFileService(Core::System& system_, FileSys::VirtualDir root_)
  237. : ServiceFramework{system_, "IDeliveryCacheFileService"}, root(std::move(root_)) {
  238. // clang-format off
  239. static const FunctionInfo functions[] = {
  240. {0, &IDeliveryCacheFileService::Open, "Open"},
  241. {1, &IDeliveryCacheFileService::Read, "Read"},
  242. {2, &IDeliveryCacheFileService::GetSize, "GetSize"},
  243. {3, &IDeliveryCacheFileService::GetDigest, "GetDigest"},
  244. };
  245. // clang-format on
  246. RegisterHandlers(functions);
  247. }
  248. private:
  249. void Open(Kernel::HLERequestContext& ctx) {
  250. IPC::RequestParser rp{ctx};
  251. const auto dir_name_raw = rp.PopRaw<DirectoryName>();
  252. const auto file_name_raw = rp.PopRaw<FileName>();
  253. const auto dir_name =
  254. Common::StringFromFixedZeroTerminatedBuffer(dir_name_raw.data(), dir_name_raw.size());
  255. const auto file_name =
  256. Common::StringFromFixedZeroTerminatedBuffer(file_name_raw.data(), file_name_raw.size());
  257. LOG_DEBUG(Service_BCAT, "called, dir_name={}, file_name={}", dir_name, file_name);
  258. if (!VerifyNameValidDir(ctx, dir_name_raw) || !VerifyNameValidFile(ctx, file_name_raw))
  259. return;
  260. if (current_file != nullptr) {
  261. LOG_ERROR(Service_BCAT, "A file has already been opened on this interface!");
  262. IPC::ResponseBuilder rb{ctx, 2};
  263. rb.Push(ERROR_ENTITY_ALREADY_OPEN);
  264. return;
  265. }
  266. const auto dir = root->GetSubdirectory(dir_name);
  267. if (dir == nullptr) {
  268. LOG_ERROR(Service_BCAT, "The directory of name={} couldn't be opened!", dir_name);
  269. IPC::ResponseBuilder rb{ctx, 2};
  270. rb.Push(ERROR_FAILED_OPEN_ENTITY);
  271. return;
  272. }
  273. current_file = dir->GetFile(file_name);
  274. if (current_file == nullptr) {
  275. LOG_ERROR(Service_BCAT, "The file of name={} couldn't be opened!", file_name);
  276. IPC::ResponseBuilder rb{ctx, 2};
  277. rb.Push(ERROR_FAILED_OPEN_ENTITY);
  278. return;
  279. }
  280. IPC::ResponseBuilder rb{ctx, 2};
  281. rb.Push(RESULT_SUCCESS);
  282. }
  283. void Read(Kernel::HLERequestContext& ctx) {
  284. IPC::RequestParser rp{ctx};
  285. const auto offset{rp.PopRaw<u64>()};
  286. auto size = ctx.GetWriteBufferSize();
  287. LOG_DEBUG(Service_BCAT, "called, offset={:016X}, size={:016X}", offset, size);
  288. if (current_file == nullptr) {
  289. LOG_ERROR(Service_BCAT, "There is no file currently open!");
  290. IPC::ResponseBuilder rb{ctx, 2};
  291. rb.Push(ERROR_NO_OPEN_ENTITY);
  292. }
  293. size = std::min<u64>(current_file->GetSize() - offset, size);
  294. const auto buffer = current_file->ReadBytes(size, offset);
  295. ctx.WriteBuffer(buffer);
  296. IPC::ResponseBuilder rb{ctx, 4};
  297. rb.Push(RESULT_SUCCESS);
  298. rb.Push<u64>(buffer.size());
  299. }
  300. void GetSize(Kernel::HLERequestContext& ctx) {
  301. LOG_DEBUG(Service_BCAT, "called");
  302. if (current_file == nullptr) {
  303. LOG_ERROR(Service_BCAT, "There is no file currently open!");
  304. IPC::ResponseBuilder rb{ctx, 2};
  305. rb.Push(ERROR_NO_OPEN_ENTITY);
  306. }
  307. IPC::ResponseBuilder rb{ctx, 4};
  308. rb.Push(RESULT_SUCCESS);
  309. rb.Push<u64>(current_file->GetSize());
  310. }
  311. void GetDigest(Kernel::HLERequestContext& ctx) {
  312. LOG_DEBUG(Service_BCAT, "called");
  313. if (current_file == nullptr) {
  314. LOG_ERROR(Service_BCAT, "There is no file currently open!");
  315. IPC::ResponseBuilder rb{ctx, 2};
  316. rb.Push(ERROR_NO_OPEN_ENTITY);
  317. }
  318. IPC::ResponseBuilder rb{ctx, 6};
  319. rb.Push(RESULT_SUCCESS);
  320. rb.PushRaw(DigestFile(current_file));
  321. }
  322. FileSys::VirtualDir root;
  323. FileSys::VirtualFile current_file;
  324. };
  325. class IDeliveryCacheDirectoryService final
  326. : public ServiceFramework<IDeliveryCacheDirectoryService> {
  327. public:
  328. explicit IDeliveryCacheDirectoryService(Core::System& system_, FileSys::VirtualDir root_)
  329. : ServiceFramework{system_, "IDeliveryCacheDirectoryService"}, root(std::move(root_)) {
  330. // clang-format off
  331. static const FunctionInfo functions[] = {
  332. {0, &IDeliveryCacheDirectoryService::Open, "Open"},
  333. {1, &IDeliveryCacheDirectoryService::Read, "Read"},
  334. {2, &IDeliveryCacheDirectoryService::GetCount, "GetCount"},
  335. };
  336. // clang-format on
  337. RegisterHandlers(functions);
  338. }
  339. private:
  340. void Open(Kernel::HLERequestContext& ctx) {
  341. IPC::RequestParser rp{ctx};
  342. const auto name_raw = rp.PopRaw<DirectoryName>();
  343. const auto name =
  344. Common::StringFromFixedZeroTerminatedBuffer(name_raw.data(), name_raw.size());
  345. LOG_DEBUG(Service_BCAT, "called, name={}", name);
  346. if (!VerifyNameValidDir(ctx, name_raw))
  347. return;
  348. if (current_dir != nullptr) {
  349. LOG_ERROR(Service_BCAT, "A file has already been opened on this interface!");
  350. IPC::ResponseBuilder rb{ctx, 2};
  351. rb.Push(ERROR_ENTITY_ALREADY_OPEN);
  352. return;
  353. }
  354. current_dir = root->GetSubdirectory(name);
  355. if (current_dir == nullptr) {
  356. LOG_ERROR(Service_BCAT, "Failed to open the directory name={}!", name);
  357. IPC::ResponseBuilder rb{ctx, 2};
  358. rb.Push(ERROR_FAILED_OPEN_ENTITY);
  359. return;
  360. }
  361. IPC::ResponseBuilder rb{ctx, 2};
  362. rb.Push(RESULT_SUCCESS);
  363. }
  364. void Read(Kernel::HLERequestContext& ctx) {
  365. auto write_size = ctx.GetWriteBufferSize() / sizeof(DeliveryCacheDirectoryEntry);
  366. LOG_DEBUG(Service_BCAT, "called, write_size={:016X}", write_size);
  367. if (current_dir == nullptr) {
  368. LOG_ERROR(Service_BCAT, "There is no open directory!");
  369. IPC::ResponseBuilder rb{ctx, 2};
  370. rb.Push(ERROR_NO_OPEN_ENTITY);
  371. return;
  372. }
  373. const auto files = current_dir->GetFiles();
  374. write_size = std::min<u64>(write_size, files.size());
  375. std::vector<DeliveryCacheDirectoryEntry> entries(write_size);
  376. std::transform(
  377. files.begin(), files.begin() + write_size, entries.begin(), [](const auto& file) {
  378. FileName name{};
  379. std::memcpy(name.data(), file->GetName().data(),
  380. std::min(file->GetName().size(), name.size()));
  381. return DeliveryCacheDirectoryEntry{name, file->GetSize(), DigestFile(file)};
  382. });
  383. ctx.WriteBuffer(entries);
  384. IPC::ResponseBuilder rb{ctx, 3};
  385. rb.Push(RESULT_SUCCESS);
  386. rb.Push(static_cast<u32>(write_size * sizeof(DeliveryCacheDirectoryEntry)));
  387. }
  388. void GetCount(Kernel::HLERequestContext& ctx) {
  389. LOG_DEBUG(Service_BCAT, "called");
  390. if (current_dir == nullptr) {
  391. LOG_ERROR(Service_BCAT, "There is no open directory!");
  392. IPC::ResponseBuilder rb{ctx, 2};
  393. rb.Push(ERROR_NO_OPEN_ENTITY);
  394. return;
  395. }
  396. const auto files = current_dir->GetFiles();
  397. IPC::ResponseBuilder rb{ctx, 3};
  398. rb.Push(RESULT_SUCCESS);
  399. rb.Push(static_cast<u32>(files.size()));
  400. }
  401. FileSys::VirtualDir root;
  402. FileSys::VirtualDir current_dir;
  403. };
  404. class IDeliveryCacheStorageService final : public ServiceFramework<IDeliveryCacheStorageService> {
  405. public:
  406. explicit IDeliveryCacheStorageService(Core::System& system_, FileSys::VirtualDir root_)
  407. : ServiceFramework{system_, "IDeliveryCacheStorageService"}, root(std::move(root_)) {
  408. // clang-format off
  409. static const FunctionInfo functions[] = {
  410. {0, &IDeliveryCacheStorageService::CreateFileService, "CreateFileService"},
  411. {1, &IDeliveryCacheStorageService::CreateDirectoryService, "CreateDirectoryService"},
  412. {10, &IDeliveryCacheStorageService::EnumerateDeliveryCacheDirectory, "EnumerateDeliveryCacheDirectory"},
  413. };
  414. // clang-format on
  415. RegisterHandlers(functions);
  416. for (const auto& subdir : root->GetSubdirectories()) {
  417. DirectoryName name{};
  418. std::memcpy(name.data(), subdir->GetName().data(),
  419. std::min(sizeof(DirectoryName) - 1, subdir->GetName().size()));
  420. entries.push_back(name);
  421. }
  422. }
  423. private:
  424. void CreateFileService(Kernel::HLERequestContext& ctx) {
  425. LOG_DEBUG(Service_BCAT, "called");
  426. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  427. rb.Push(RESULT_SUCCESS);
  428. rb.PushIpcInterface<IDeliveryCacheFileService>(system, root);
  429. }
  430. void CreateDirectoryService(Kernel::HLERequestContext& ctx) {
  431. LOG_DEBUG(Service_BCAT, "called");
  432. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  433. rb.Push(RESULT_SUCCESS);
  434. rb.PushIpcInterface<IDeliveryCacheDirectoryService>(system, root);
  435. }
  436. void EnumerateDeliveryCacheDirectory(Kernel::HLERequestContext& ctx) {
  437. auto size = ctx.GetWriteBufferSize() / sizeof(DirectoryName);
  438. LOG_DEBUG(Service_BCAT, "called, size={:016X}", size);
  439. size = std::min<u64>(size, entries.size() - next_read_index);
  440. ctx.WriteBuffer(entries.data() + next_read_index, size * sizeof(DirectoryName));
  441. next_read_index += size;
  442. IPC::ResponseBuilder rb{ctx, 3};
  443. rb.Push(RESULT_SUCCESS);
  444. rb.Push(static_cast<u32>(size));
  445. }
  446. FileSys::VirtualDir root;
  447. std::vector<DirectoryName> entries;
  448. u64 next_read_index = 0;
  449. };
  450. void Module::Interface::CreateDeliveryCacheStorageService(Kernel::HLERequestContext& ctx) {
  451. LOG_DEBUG(Service_BCAT, "called");
  452. const auto title_id = system.CurrentProcess()->GetTitleID();
  453. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  454. rb.Push(RESULT_SUCCESS);
  455. rb.PushIpcInterface<IDeliveryCacheStorageService>(system, fsc.GetBCATDirectory(title_id));
  456. }
  457. void Module::Interface::CreateDeliveryCacheStorageServiceWithApplicationId(
  458. Kernel::HLERequestContext& ctx) {
  459. IPC::RequestParser rp{ctx};
  460. const auto title_id = rp.PopRaw<u64>();
  461. LOG_DEBUG(Service_BCAT, "called, title_id={:016X}", title_id);
  462. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  463. rb.Push(RESULT_SUCCESS);
  464. rb.PushIpcInterface<IDeliveryCacheStorageService>(system, fsc.GetBCATDirectory(title_id));
  465. }
  466. std::unique_ptr<Backend> CreateBackendFromSettings([[maybe_unused]] Core::System& system,
  467. DirectoryGetter getter) {
  468. #ifdef YUZU_ENABLE_BOXCAT
  469. if (Settings::values.bcat_backend == "boxcat") {
  470. return std::make_unique<Boxcat>(system.GetAppletManager(), std::move(getter));
  471. }
  472. #endif
  473. return std::make_unique<NullBackend>(std::move(getter));
  474. }
  475. Module::Interface::Interface(Core::System& system_, std::shared_ptr<Module> module_,
  476. FileSystem::FileSystemController& fsc_, const char* name)
  477. : ServiceFramework{system_, name}, fsc{fsc_}, module{std::move(module_)},
  478. backend{CreateBackendFromSettings(system_,
  479. [&fsc_](u64 tid) { return fsc_.GetBCATDirectory(tid); })} {}
  480. Module::Interface::~Interface() = default;
  481. void InstallInterfaces(Core::System& system) {
  482. auto module = std::make_shared<Module>();
  483. std::make_shared<BCAT>(system, module, system.GetFileSystemController(), "bcat:a")
  484. ->InstallAsService(system.ServiceManager());
  485. std::make_shared<BCAT>(system, module, system.GetFileSystemController(), "bcat:m")
  486. ->InstallAsService(system.ServiceManager());
  487. std::make_shared<BCAT>(system, module, system.GetFileSystemController(), "bcat:u")
  488. ->InstallAsService(system.ServiceManager());
  489. std::make_shared<BCAT>(system, module, system.GetFileSystemController(), "bcat:s")
  490. ->InstallAsService(system.ServiceManager());
  491. }
  492. } // namespace Service::BCAT