module.cpp 22 KB

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