module.cpp 21 KB

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