module.cpp 22 KB

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