bcat_module.cpp 22 KB

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