module.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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);
  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. {30101, nullptr, "Unknown"},
  124. {30102, nullptr, "Unknown2"},
  125. {30200, nullptr, "RegisterBackgroundDeliveryTask"},
  126. {30201, nullptr, "UnregisterBackgroundDeliveryTask"},
  127. {30202, nullptr, "BlockDeliveryTask"},
  128. {30203, nullptr, "UnblockDeliveryTask"},
  129. {30210, nullptr, "SetDeliveryTaskTimer"},
  130. {30300, nullptr, "RegisterSystemApplicationDeliveryTasks"},
  131. {90100, nullptr, "EnumerateBackgroundDeliveryTask"},
  132. {90200, nullptr, "GetDeliveryList"},
  133. {90201, &IBcatService::ClearDeliveryCacheStorage, "ClearDeliveryCacheStorage"},
  134. {90202, nullptr, "ClearDeliveryTaskSubscriptionStatus"},
  135. {90300, nullptr, "GetPushNotificationLog"},
  136. };
  137. // clang-format on
  138. RegisterHandlers(functions);
  139. }
  140. private:
  141. enum class SyncType {
  142. Normal,
  143. Directory,
  144. Count,
  145. };
  146. std::shared_ptr<IDeliveryCacheProgressService> CreateProgressService(SyncType type) {
  147. auto& backend{progress.at(static_cast<std::size_t>(type))};
  148. return std::make_shared<IDeliveryCacheProgressService>(backend.GetEvent(),
  149. backend.GetImpl());
  150. }
  151. void RequestSyncDeliveryCache(Kernel::HLERequestContext& ctx) {
  152. LOG_DEBUG(Service_BCAT, "called");
  153. backend.Synchronize({system.CurrentProcess()->GetTitleID(),
  154. GetCurrentBuildID(system.GetCurrentProcessBuildID())},
  155. progress.at(static_cast<std::size_t>(SyncType::Normal)));
  156. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  157. rb.Push(RESULT_SUCCESS);
  158. rb.PushIpcInterface(CreateProgressService(SyncType::Normal));
  159. }
  160. void RequestSyncDeliveryCacheWithDirectoryName(Kernel::HLERequestContext& ctx) {
  161. IPC::RequestParser rp{ctx};
  162. const auto name_raw = rp.PopRaw<DirectoryName>();
  163. const auto name =
  164. Common::StringFromFixedZeroTerminatedBuffer(name_raw.data(), name_raw.size());
  165. LOG_DEBUG(Service_BCAT, "called, name={}", name);
  166. backend.SynchronizeDirectory({system.CurrentProcess()->GetTitleID(),
  167. GetCurrentBuildID(system.GetCurrentProcessBuildID())},
  168. name,
  169. progress.at(static_cast<std::size_t>(SyncType::Directory)));
  170. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  171. rb.Push(RESULT_SUCCESS);
  172. rb.PushIpcInterface(CreateProgressService(SyncType::Directory));
  173. }
  174. void SetPassphrase(Kernel::HLERequestContext& ctx) {
  175. IPC::RequestParser rp{ctx};
  176. const auto title_id = rp.PopRaw<u64>();
  177. const auto passphrase_raw = ctx.ReadBuffer();
  178. LOG_DEBUG(Service_BCAT, "called, title_id={:016X}, passphrase={}", title_id,
  179. Common::HexToString(passphrase_raw));
  180. if (title_id == 0) {
  181. LOG_ERROR(Service_BCAT, "Invalid title ID!");
  182. IPC::ResponseBuilder rb{ctx, 2};
  183. rb.Push(ERROR_INVALID_ARGUMENT);
  184. }
  185. if (passphrase_raw.size() > 0x40) {
  186. LOG_ERROR(Service_BCAT, "Passphrase too large!");
  187. IPC::ResponseBuilder rb{ctx, 2};
  188. rb.Push(ERROR_INVALID_ARGUMENT);
  189. return;
  190. }
  191. Passphrase passphrase{};
  192. std::memcpy(passphrase.data(), passphrase_raw.data(),
  193. std::min(passphrase.size(), passphrase_raw.size()));
  194. backend.SetPassphrase(title_id, passphrase);
  195. IPC::ResponseBuilder rb{ctx, 2};
  196. rb.Push(RESULT_SUCCESS);
  197. }
  198. void ClearDeliveryCacheStorage(Kernel::HLERequestContext& ctx) {
  199. IPC::RequestParser rp{ctx};
  200. const auto title_id = rp.PopRaw<u64>();
  201. LOG_DEBUG(Service_BCAT, "called, title_id={:016X}", title_id);
  202. if (title_id == 0) {
  203. LOG_ERROR(Service_BCAT, "Invalid title ID!");
  204. IPC::ResponseBuilder rb{ctx, 2};
  205. rb.Push(ERROR_INVALID_ARGUMENT);
  206. return;
  207. }
  208. if (!backend.Clear(title_id)) {
  209. LOG_ERROR(Service_BCAT, "Could not clear the directory successfully!");
  210. IPC::ResponseBuilder rb{ctx, 2};
  211. rb.Push(ERROR_FAILED_CLEAR_CACHE);
  212. return;
  213. }
  214. IPC::ResponseBuilder rb{ctx, 2};
  215. rb.Push(RESULT_SUCCESS);
  216. }
  217. Core::System& system;
  218. Backend& backend;
  219. std::array<ProgressServiceBackend, static_cast<std::size_t>(SyncType::Count)> progress;
  220. };
  221. void Module::Interface::CreateBcatService(Kernel::HLERequestContext& ctx) {
  222. LOG_DEBUG(Service_BCAT, "called");
  223. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  224. rb.Push(RESULT_SUCCESS);
  225. rb.PushIpcInterface<IBcatService>(system, *backend);
  226. }
  227. class IDeliveryCacheFileService final : public ServiceFramework<IDeliveryCacheFileService> {
  228. public:
  229. IDeliveryCacheFileService(FileSys::VirtualDir root_)
  230. : ServiceFramework{"IDeliveryCacheFileService"}, root(std::move(root_)) {
  231. // clang-format off
  232. static const FunctionInfo functions[] = {
  233. {0, &IDeliveryCacheFileService::Open, "Open"},
  234. {1, &IDeliveryCacheFileService::Read, "Read"},
  235. {2, &IDeliveryCacheFileService::GetSize, "GetSize"},
  236. {3, &IDeliveryCacheFileService::GetDigest, "GetDigest"},
  237. };
  238. // clang-format on
  239. RegisterHandlers(functions);
  240. }
  241. private:
  242. void Open(Kernel::HLERequestContext& ctx) {
  243. IPC::RequestParser rp{ctx};
  244. const auto dir_name_raw = rp.PopRaw<DirectoryName>();
  245. const auto file_name_raw = rp.PopRaw<FileName>();
  246. const auto dir_name =
  247. Common::StringFromFixedZeroTerminatedBuffer(dir_name_raw.data(), dir_name_raw.size());
  248. const auto file_name =
  249. Common::StringFromFixedZeroTerminatedBuffer(file_name_raw.data(), file_name_raw.size());
  250. LOG_DEBUG(Service_BCAT, "called, dir_name={}, file_name={}", dir_name, file_name);
  251. if (!VerifyNameValidDir(ctx, dir_name_raw) || !VerifyNameValidFile(ctx, file_name_raw))
  252. return;
  253. if (current_file != nullptr) {
  254. LOG_ERROR(Service_BCAT, "A file has already been opened on this interface!");
  255. IPC::ResponseBuilder rb{ctx, 2};
  256. rb.Push(ERROR_ENTITY_ALREADY_OPEN);
  257. return;
  258. }
  259. const auto dir = root->GetSubdirectory(dir_name);
  260. if (dir == nullptr) {
  261. LOG_ERROR(Service_BCAT, "The directory of name={} couldn't be opened!", dir_name);
  262. IPC::ResponseBuilder rb{ctx, 2};
  263. rb.Push(ERROR_FAILED_OPEN_ENTITY);
  264. return;
  265. }
  266. current_file = dir->GetFile(file_name);
  267. if (current_file == nullptr) {
  268. LOG_ERROR(Service_BCAT, "The file of name={} couldn't be opened!", file_name);
  269. IPC::ResponseBuilder rb{ctx, 2};
  270. rb.Push(ERROR_FAILED_OPEN_ENTITY);
  271. return;
  272. }
  273. IPC::ResponseBuilder rb{ctx, 2};
  274. rb.Push(RESULT_SUCCESS);
  275. }
  276. void Read(Kernel::HLERequestContext& ctx) {
  277. IPC::RequestParser rp{ctx};
  278. const auto offset{rp.PopRaw<u64>()};
  279. auto size = ctx.GetWriteBufferSize();
  280. LOG_DEBUG(Service_BCAT, "called, offset={:016X}, size={:016X}", offset, size);
  281. if (current_file == nullptr) {
  282. LOG_ERROR(Service_BCAT, "There is no file currently open!");
  283. IPC::ResponseBuilder rb{ctx, 2};
  284. rb.Push(ERROR_NO_OPEN_ENTITY);
  285. }
  286. size = std::min<u64>(current_file->GetSize() - offset, size);
  287. const auto buffer = current_file->ReadBytes(size, offset);
  288. ctx.WriteBuffer(buffer);
  289. IPC::ResponseBuilder rb{ctx, 4};
  290. rb.Push(RESULT_SUCCESS);
  291. rb.Push<u64>(buffer.size());
  292. }
  293. void GetSize(Kernel::HLERequestContext& ctx) {
  294. LOG_DEBUG(Service_BCAT, "called");
  295. if (current_file == nullptr) {
  296. LOG_ERROR(Service_BCAT, "There is no file currently open!");
  297. IPC::ResponseBuilder rb{ctx, 2};
  298. rb.Push(ERROR_NO_OPEN_ENTITY);
  299. }
  300. IPC::ResponseBuilder rb{ctx, 4};
  301. rb.Push(RESULT_SUCCESS);
  302. rb.Push<u64>(current_file->GetSize());
  303. }
  304. void GetDigest(Kernel::HLERequestContext& ctx) {
  305. LOG_DEBUG(Service_BCAT, "called");
  306. if (current_file == nullptr) {
  307. LOG_ERROR(Service_BCAT, "There is no file currently open!");
  308. IPC::ResponseBuilder rb{ctx, 2};
  309. rb.Push(ERROR_NO_OPEN_ENTITY);
  310. }
  311. IPC::ResponseBuilder rb{ctx, 6};
  312. rb.Push(RESULT_SUCCESS);
  313. rb.PushRaw(DigestFile(current_file));
  314. }
  315. FileSys::VirtualDir root;
  316. FileSys::VirtualFile current_file;
  317. };
  318. class IDeliveryCacheDirectoryService final
  319. : public ServiceFramework<IDeliveryCacheDirectoryService> {
  320. public:
  321. IDeliveryCacheDirectoryService(FileSys::VirtualDir root_)
  322. : ServiceFramework{"IDeliveryCacheDirectoryService"}, root(std::move(root_)) {
  323. // clang-format off
  324. static const FunctionInfo functions[] = {
  325. {0, &IDeliveryCacheDirectoryService::Open, "Open"},
  326. {1, &IDeliveryCacheDirectoryService::Read, "Read"},
  327. {2, &IDeliveryCacheDirectoryService::GetCount, "GetCount"},
  328. };
  329. // clang-format on
  330. RegisterHandlers(functions);
  331. }
  332. private:
  333. void Open(Kernel::HLERequestContext& ctx) {
  334. IPC::RequestParser rp{ctx};
  335. const auto name_raw = rp.PopRaw<DirectoryName>();
  336. const auto name =
  337. Common::StringFromFixedZeroTerminatedBuffer(name_raw.data(), name_raw.size());
  338. LOG_DEBUG(Service_BCAT, "called, name={}", name);
  339. if (!VerifyNameValidDir(ctx, name_raw))
  340. return;
  341. if (current_dir != nullptr) {
  342. LOG_ERROR(Service_BCAT, "A file has already been opened on this interface!");
  343. IPC::ResponseBuilder rb{ctx, 2};
  344. rb.Push(ERROR_ENTITY_ALREADY_OPEN);
  345. return;
  346. }
  347. current_dir = root->GetSubdirectory(name);
  348. if (current_dir == nullptr) {
  349. LOG_ERROR(Service_BCAT, "Failed to open the directory name={}!", name);
  350. IPC::ResponseBuilder rb{ctx, 2};
  351. rb.Push(ERROR_FAILED_OPEN_ENTITY);
  352. return;
  353. }
  354. IPC::ResponseBuilder rb{ctx, 2};
  355. rb.Push(RESULT_SUCCESS);
  356. }
  357. void Read(Kernel::HLERequestContext& ctx) {
  358. auto write_size = ctx.GetWriteBufferSize() / sizeof(DeliveryCacheDirectoryEntry);
  359. LOG_DEBUG(Service_BCAT, "called, write_size={:016X}", write_size);
  360. if (current_dir == nullptr) {
  361. LOG_ERROR(Service_BCAT, "There is no open directory!");
  362. IPC::ResponseBuilder rb{ctx, 2};
  363. rb.Push(ERROR_NO_OPEN_ENTITY);
  364. return;
  365. }
  366. const auto files = current_dir->GetFiles();
  367. write_size = std::min<u64>(write_size, files.size());
  368. std::vector<DeliveryCacheDirectoryEntry> entries(write_size);
  369. std::transform(
  370. files.begin(), files.begin() + static_cast<s64>(write_size), entries.begin(),
  371. [](const auto& file) {
  372. FileName name{};
  373. std::memcpy(name.data(), file->GetName().data(),
  374. std::min(file->GetName().size(), name.size()));
  375. return DeliveryCacheDirectoryEntry{name, file->GetSize(), DigestFile(file)};
  376. });
  377. ctx.WriteBuffer(entries);
  378. IPC::ResponseBuilder rb{ctx, 3};
  379. rb.Push(RESULT_SUCCESS);
  380. rb.Push(static_cast<u32>(write_size * sizeof(DeliveryCacheDirectoryEntry)));
  381. }
  382. void GetCount(Kernel::HLERequestContext& ctx) {
  383. LOG_DEBUG(Service_BCAT, "called");
  384. if (current_dir == nullptr) {
  385. LOG_ERROR(Service_BCAT, "There is no open directory!");
  386. IPC::ResponseBuilder rb{ctx, 2};
  387. rb.Push(ERROR_NO_OPEN_ENTITY);
  388. return;
  389. }
  390. const auto files = current_dir->GetFiles();
  391. IPC::ResponseBuilder rb{ctx, 3};
  392. rb.Push(RESULT_SUCCESS);
  393. rb.Push(static_cast<u32>(files.size()));
  394. }
  395. FileSys::VirtualDir root;
  396. FileSys::VirtualDir current_dir;
  397. };
  398. class IDeliveryCacheStorageService final : public ServiceFramework<IDeliveryCacheStorageService> {
  399. public:
  400. IDeliveryCacheStorageService(FileSys::VirtualDir root_)
  401. : ServiceFramework{"IDeliveryCacheStorageService"}, root(std::move(root_)) {
  402. // clang-format off
  403. static const FunctionInfo functions[] = {
  404. {0, &IDeliveryCacheStorageService::CreateFileService, "CreateFileService"},
  405. {1, &IDeliveryCacheStorageService::CreateDirectoryService, "CreateDirectoryService"},
  406. {10, &IDeliveryCacheStorageService::EnumerateDeliveryCacheDirectory, "EnumerateDeliveryCacheDirectory"},
  407. };
  408. // clang-format on
  409. RegisterHandlers(functions);
  410. for (const auto& subdir : root->GetSubdirectories()) {
  411. DirectoryName name{};
  412. std::memcpy(name.data(), subdir->GetName().data(),
  413. std::min(sizeof(DirectoryName) - 1, subdir->GetName().size()));
  414. entries.push_back(name);
  415. }
  416. }
  417. private:
  418. void CreateFileService(Kernel::HLERequestContext& ctx) {
  419. LOG_DEBUG(Service_BCAT, "called");
  420. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  421. rb.Push(RESULT_SUCCESS);
  422. rb.PushIpcInterface<IDeliveryCacheFileService>(root);
  423. }
  424. void CreateDirectoryService(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<IDeliveryCacheDirectoryService>(root);
  429. }
  430. void EnumerateDeliveryCacheDirectory(Kernel::HLERequestContext& ctx) {
  431. auto size = ctx.GetWriteBufferSize() / sizeof(DirectoryName);
  432. LOG_DEBUG(Service_BCAT, "called, size={:016X}", size);
  433. size = std::min<u64>(size, entries.size() - next_read_index);
  434. ctx.WriteBuffer(entries.data() + next_read_index, size * sizeof(DirectoryName));
  435. next_read_index += size;
  436. IPC::ResponseBuilder rb{ctx, 3};
  437. rb.Push(RESULT_SUCCESS);
  438. rb.Push(static_cast<u32>(size));
  439. }
  440. FileSys::VirtualDir root;
  441. std::vector<DirectoryName> entries;
  442. u64 next_read_index = 0;
  443. };
  444. void Module::Interface::CreateDeliveryCacheStorageService(Kernel::HLERequestContext& ctx) {
  445. LOG_DEBUG(Service_BCAT, "called");
  446. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  447. rb.Push(RESULT_SUCCESS);
  448. rb.PushIpcInterface<IDeliveryCacheStorageService>(
  449. fsc.GetBCATDirectory(system.CurrentProcess()->GetTitleID()));
  450. }
  451. void Module::Interface::CreateDeliveryCacheStorageServiceWithApplicationId(
  452. Kernel::HLERequestContext& ctx) {
  453. IPC::RequestParser rp{ctx};
  454. const auto title_id = rp.PopRaw<u64>();
  455. LOG_DEBUG(Service_BCAT, "called, title_id={:016X}", title_id);
  456. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  457. rb.Push(RESULT_SUCCESS);
  458. rb.PushIpcInterface<IDeliveryCacheStorageService>(fsc.GetBCATDirectory(title_id));
  459. }
  460. std::unique_ptr<Backend> CreateBackendFromSettings([[maybe_unused]] Core::System& system,
  461. DirectoryGetter getter) {
  462. #ifdef YUZU_ENABLE_BOXCAT
  463. if (Settings::values.bcat_backend == "boxcat") {
  464. return std::make_unique<Boxcat>(system.GetAppletManager(), std::move(getter));
  465. }
  466. #endif
  467. return std::make_unique<NullBackend>(std::move(getter));
  468. }
  469. Module::Interface::Interface(Core::System& system_, std::shared_ptr<Module> module_,
  470. FileSystem::FileSystemController& fsc_, const char* name)
  471. : ServiceFramework(name), fsc{fsc_}, module{std::move(module_)},
  472. backend{CreateBackendFromSettings(system_,
  473. [&fsc_](u64 tid) { return fsc_.GetBCATDirectory(tid); })},
  474. system{system_} {}
  475. Module::Interface::~Interface() = default;
  476. void InstallInterfaces(Core::System& system) {
  477. auto module = std::make_shared<Module>();
  478. std::make_shared<BCAT>(system, module, system.GetFileSystemController(), "bcat:a")
  479. ->InstallAsService(system.ServiceManager());
  480. std::make_shared<BCAT>(system, module, system.GetFileSystemController(), "bcat:m")
  481. ->InstallAsService(system.ServiceManager());
  482. std::make_shared<BCAT>(system, module, system.GetFileSystemController(), "bcat:u")
  483. ->InstallAsService(system.ServiceManager());
  484. std::make_shared<BCAT>(system, module, system.GetFileSystemController(), "bcat:s")
  485. ->InstallAsService(system.ServiceManager());
  486. }
  487. } // namespace Service::BCAT