romfs_factory.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <memory>
  4. #include "common/assert.h"
  5. #include "common/common_types.h"
  6. #include "common/logging/log.h"
  7. #include "core/file_sys/common_funcs.h"
  8. #include "core/file_sys/content_archive.h"
  9. #include "core/file_sys/nca_metadata.h"
  10. #include "core/file_sys/patch_manager.h"
  11. #include "core/file_sys/registered_cache.h"
  12. #include "core/file_sys/romfs_factory.h"
  13. #include "core/hle/kernel/k_process.h"
  14. #include "core/hle/service/filesystem/filesystem.h"
  15. #include "core/loader/loader.h"
  16. namespace FileSys {
  17. RomFSFactory::RomFSFactory(Loader::AppLoader& app_loader, ContentProvider& provider,
  18. Service::FileSystem::FileSystemController& controller)
  19. : content_provider{provider}, filesystem_controller{controller} {
  20. // Load the RomFS from the app
  21. if (app_loader.ReadRomFS(file) != Loader::ResultStatus::Success) {
  22. LOG_ERROR(Service_FS, "Unable to read RomFS!");
  23. }
  24. updatable = app_loader.IsRomFSUpdatable();
  25. ivfc_offset = app_loader.ReadRomFSIVFCOffset();
  26. }
  27. RomFSFactory::~RomFSFactory() = default;
  28. void RomFSFactory::SetPackedUpdate(VirtualFile update_raw_file) {
  29. update_raw = std::move(update_raw_file);
  30. }
  31. ResultVal<VirtualFile> RomFSFactory::OpenCurrentProcess(u64 current_process_title_id) const {
  32. if (!updatable) {
  33. return file;
  34. }
  35. const PatchManager patch_manager{current_process_title_id, filesystem_controller,
  36. content_provider};
  37. return patch_manager.PatchRomFS(file, ivfc_offset, ContentRecordType::Program, update_raw);
  38. }
  39. ResultVal<VirtualFile> RomFSFactory::OpenPatchedRomFS(u64 title_id, ContentRecordType type) const {
  40. auto nca = content_provider.GetEntry(title_id, type);
  41. if (nca == nullptr) {
  42. // TODO: Find the right error code to use here
  43. return ResultUnknown;
  44. }
  45. const PatchManager patch_manager{title_id, filesystem_controller, content_provider};
  46. return patch_manager.PatchRomFS(nca->GetRomFS(), nca->GetBaseIVFCOffset(), type);
  47. }
  48. ResultVal<VirtualFile> RomFSFactory::OpenPatchedRomFSWithProgramIndex(
  49. u64 title_id, u8 program_index, ContentRecordType type) const {
  50. const auto res_title_id = GetBaseTitleIDWithProgramIndex(title_id, program_index);
  51. return OpenPatchedRomFS(res_title_id, type);
  52. }
  53. ResultVal<VirtualFile> RomFSFactory::Open(u64 title_id, StorageId storage,
  54. ContentRecordType type) const {
  55. const std::shared_ptr<NCA> res = GetEntry(title_id, storage, type);
  56. if (res == nullptr) {
  57. // TODO(DarkLordZach): Find the right error code to use here
  58. return ResultUnknown;
  59. }
  60. const auto romfs = res->GetRomFS();
  61. if (romfs == nullptr) {
  62. // TODO(DarkLordZach): Find the right error code to use here
  63. return ResultUnknown;
  64. }
  65. return romfs;
  66. }
  67. std::shared_ptr<NCA> RomFSFactory::GetEntry(u64 title_id, StorageId storage,
  68. ContentRecordType type) const {
  69. switch (storage) {
  70. case StorageId::None:
  71. return content_provider.GetEntry(title_id, type);
  72. case StorageId::NandSystem:
  73. return filesystem_controller.GetSystemNANDContents()->GetEntry(title_id, type);
  74. case StorageId::NandUser:
  75. return filesystem_controller.GetUserNANDContents()->GetEntry(title_id, type);
  76. case StorageId::SdCard:
  77. return filesystem_controller.GetSDMCContents()->GetEntry(title_id, type);
  78. case StorageId::Host:
  79. case StorageId::GameCard:
  80. default:
  81. UNIMPLEMENTED_MSG("Unimplemented storage_id={:02X}", static_cast<u8>(storage));
  82. return nullptr;
  83. }
  84. }
  85. } // namespace FileSys