module.cpp 22 KB

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