mirror of
				https://github.com/PabloMK7/citra.git
				synced 2025-10-31 05:40:04 +00:00 
			
		
		
		
	Merge pull request #2993 from shinyquagsire23/am-and-archive-ncch-rework
AM Service and NCCH Archive Rework
This commit is contained in:
		
						commit
						dcb48840c3
					
				
					 9 changed files with 739 additions and 172 deletions
				
			
		|  | @ -14,7 +14,7 @@ | |||
| #include "core/file_sys/errors.h" | ||||
| #include "core/file_sys/ivfc_archive.h" | ||||
| #include "core/file_sys/ncch_container.h" | ||||
| #include "core/file_sys/title_metadata.h" | ||||
| #include "core/hle/service/am/am.h" | ||||
| #include "core/hle/service/fs/archive.h" | ||||
| #include "core/loader/loader.h" | ||||
| 
 | ||||
|  | @ -23,42 +23,72 @@ | |||
| 
 | ||||
| namespace FileSys { | ||||
| 
 | ||||
| static std::string GetNCCHContainerPath(const std::string& nand_directory) { | ||||
|     return Common::StringFromFormat("%s%s/title/", nand_directory.c_str(), SYSTEM_ID); | ||||
| } | ||||
| enum class NCCHFilePathType : u32 { | ||||
|     RomFS = 0, | ||||
|     Code = 1, | ||||
|     ExeFS = 2, | ||||
| }; | ||||
| 
 | ||||
| static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) { | ||||
|     u32 content_id = 0; | ||||
| struct NCCHArchivePath { | ||||
|     u64_le tid; | ||||
|     u32_le media_type; | ||||
|     u32_le unknown; | ||||
| }; | ||||
| static_assert(sizeof(NCCHArchivePath) == 0x10, "NCCHArchivePath has wrong size!"); | ||||
| 
 | ||||
|     // TODO(shinyquagsire23): Title database should be doing this path lookup
 | ||||
|     std::string content_path = | ||||
|         Common::StringFromFormat("%s%08x/%08x/content/", mount_point.c_str(), high, low); | ||||
|     std::string tmd_path = content_path + "00000000.tmd"; | ||||
|     TitleMetadata tmd(tmd_path); | ||||
|     if (tmd.Load() == Loader::ResultStatus::Success) { | ||||
|         content_id = tmd.GetBootContentID(); | ||||
| struct NCCHFilePath { | ||||
|     u32_le open_type; | ||||
|     u32_le content_index; | ||||
|     u32_le filepath_type; | ||||
|     std::array<char, 8> exefs_filepath; | ||||
| }; | ||||
| static_assert(sizeof(NCCHFilePath) == 0x14, "NCCHFilePath has wrong size!"); | ||||
| 
 | ||||
| ResultVal<std::unique_ptr<FileBackend>> NCCHArchive::OpenFile(const Path& path, | ||||
|                                                               const Mode& mode) const { | ||||
|     if (path.GetType() != LowPathType::Binary) { | ||||
|         LOG_ERROR(Service_FS, "Path need to be Binary"); | ||||
|         return ERROR_INVALID_PATH; | ||||
|     } | ||||
| 
 | ||||
|     return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id); | ||||
| } | ||||
|     std::vector<u8> binary = path.AsBinary(); | ||||
|     if (binary.size() != sizeof(NCCHFilePath)) { | ||||
|         LOG_ERROR(Service_FS, "Wrong path size %zu", binary.size()); | ||||
|         return ERROR_INVALID_PATH; | ||||
|     } | ||||
| 
 | ||||
| ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory) | ||||
|     : mount_point(GetNCCHContainerPath(nand_directory)) {} | ||||
|     NCCHFilePath openfile_path; | ||||
|     std::memcpy(&openfile_path, binary.data(), sizeof(NCCHFilePath)); | ||||
| 
 | ||||
| ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& path) { | ||||
|     auto vec = path.AsBinary(); | ||||
|     const u32* data = reinterpret_cast<u32*>(vec.data()); | ||||
|     u32 high = data[1]; | ||||
|     u32 low = data[0]; | ||||
|     std::string file_path = GetNCCHPath(mount_point, high, low); | ||||
| 
 | ||||
|     std::shared_ptr<FileUtil::IOFile> romfs_file; | ||||
|     u64 romfs_offset = 0; | ||||
|     u64 romfs_size = 0; | ||||
|     std::string file_path = | ||||
|         Service::AM::GetTitleContentPath(media_type, title_id, openfile_path.content_index); | ||||
|     auto ncch_container = NCCHContainer(file_path); | ||||
| 
 | ||||
|     if (ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size) != | ||||
|         Loader::ResultStatus::Success) { | ||||
|     Loader::ResultStatus result; | ||||
|     std::unique_ptr<FileBackend> file; | ||||
| 
 | ||||
|     // NCCH RomFS
 | ||||
|     NCCHFilePathType filepath_type = static_cast<NCCHFilePathType>(openfile_path.filepath_type); | ||||
|     if (filepath_type == NCCHFilePathType::RomFS) { | ||||
|         std::shared_ptr<FileUtil::IOFile> romfs_file; | ||||
|         u64 romfs_offset = 0; | ||||
|         u64 romfs_size = 0; | ||||
| 
 | ||||
|         result = ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size); | ||||
|         file = std::make_unique<IVFCFile>(romfs_file, romfs_offset, romfs_size); | ||||
|     } else if (filepath_type == NCCHFilePathType::Code || | ||||
|                filepath_type == NCCHFilePathType::ExeFS) { | ||||
|         std::vector<u8> buffer; | ||||
| 
 | ||||
|         // Load NCCH .code or icon/banner/logo
 | ||||
|         result = ncch_container.LoadSectionExeFS(openfile_path.exefs_filepath.data(), buffer); | ||||
|         file = std::make_unique<NCCHFile>(buffer); | ||||
|     } else { | ||||
|         LOG_ERROR(Service_FS, "Unknown NCCH archive type %u!", openfile_path.filepath_type); | ||||
|         result = Loader::ResultStatus::Error; | ||||
|     } | ||||
| 
 | ||||
|     if (result != Loader::ResultStatus::Success) { | ||||
|         // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list).
 | ||||
|         constexpr u32 shared_data_archive = 0x0004009B; | ||||
|         constexpr u32 system_data_archive = 0x000400DB; | ||||
|  | @ -68,32 +98,149 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& | |||
|         constexpr u32 region_manifest = 0x00010402; | ||||
|         constexpr u32 ng_word_list = 0x00010302; | ||||
| 
 | ||||
|         u32 high = static_cast<u32>(title_id >> 32); | ||||
|         u32 low = static_cast<u32>(title_id & 0xFFFFFFFF); | ||||
| 
 | ||||
|         LOG_DEBUG(Service_FS, "Full Path: %s. Category: 0x%X. Path: 0x%X.", path.DebugStr().c_str(), | ||||
|                   high, low); | ||||
| 
 | ||||
|         std::string archive_name; | ||||
|         if (high == shared_data_archive) { | ||||
|             if (low == mii_data) { | ||||
|                 LOG_ERROR(Service_FS, "Failed to get a handle for shared data archive: Mii data. "); | ||||
|                 Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles, | ||||
|                                                       "Mii data"); | ||||
|             } else if (low == region_manifest) { | ||||
|                 LOG_ERROR(Service_FS, | ||||
|                           "Failed to get a handle for shared data archive: region manifest."); | ||||
|                 Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles, | ||||
|                                                       "Region manifest"); | ||||
|             } | ||||
|             if (low == mii_data) | ||||
|                 archive_name = "Mii Data"; | ||||
|             else if (low == region_manifest) | ||||
|                 archive_name = "Region manifest"; | ||||
|         } else if (high == system_data_archive) { | ||||
|             if (low == ng_word_list) { | ||||
|                 LOG_ERROR(Service_FS, | ||||
|                           "Failed to get a handle for system data archive: NG bad word list."); | ||||
|                 Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles, | ||||
|                                                       "NG bad word list"); | ||||
|             } | ||||
|             if (low == ng_word_list) | ||||
|                 archive_name = "NG bad word list"; | ||||
|         } | ||||
| 
 | ||||
|         if (!archive_name.empty()) { | ||||
|             LOG_ERROR(Service_FS, "Failed to get a handle for shared data archive: %s. ", | ||||
|                       archive_name.c_str()); | ||||
|             Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles, | ||||
|                                                   archive_name.c_str()); | ||||
|         } | ||||
|         return ERROR_NOT_FOUND; | ||||
|     } | ||||
| 
 | ||||
|     auto archive = std::make_unique<IVFCArchive>(romfs_file, romfs_offset, romfs_size); | ||||
|     return MakeResult<std::unique_ptr<FileBackend>>(std::move(file)); | ||||
| } | ||||
| 
 | ||||
| ResultCode NCCHArchive::DeleteFile(const Path& path) const { | ||||
|     LOG_CRITICAL(Service_FS, "Attempted to delete a file from an NCCH archive (%s).", | ||||
|                  GetName().c_str()); | ||||
|     // TODO(Subv): Verify error code
 | ||||
|     return ResultCode(ErrorDescription::NoData, ErrorModule::FS, ErrorSummary::Canceled, | ||||
|                       ErrorLevel::Status); | ||||
| } | ||||
| 
 | ||||
| ResultCode NCCHArchive::RenameFile(const Path& src_path, const Path& dest_path) const { | ||||
|     LOG_CRITICAL(Service_FS, "Attempted to rename a file within an NCCH archive (%s).", | ||||
|                  GetName().c_str()); | ||||
|     // TODO(wwylele): Use correct error code
 | ||||
|     return ResultCode(-1); | ||||
| } | ||||
| 
 | ||||
| ResultCode NCCHArchive::DeleteDirectory(const Path& path) const { | ||||
|     LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an NCCH archive (%s).", | ||||
|                  GetName().c_str()); | ||||
|     // TODO(wwylele): Use correct error code
 | ||||
|     return ResultCode(-1); | ||||
| } | ||||
| 
 | ||||
| ResultCode NCCHArchive::DeleteDirectoryRecursively(const Path& path) const { | ||||
|     LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an NCCH archive (%s).", | ||||
|                  GetName().c_str()); | ||||
|     // TODO(wwylele): Use correct error code
 | ||||
|     return ResultCode(-1); | ||||
| } | ||||
| 
 | ||||
| ResultCode NCCHArchive::CreateFile(const Path& path, u64 size) const { | ||||
|     LOG_CRITICAL(Service_FS, "Attempted to create a file in an NCCH archive (%s).", | ||||
|                  GetName().c_str()); | ||||
|     // TODO: Verify error code
 | ||||
|     return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::FS, ErrorSummary::NotSupported, | ||||
|                       ErrorLevel::Permanent); | ||||
| } | ||||
| 
 | ||||
| ResultCode NCCHArchive::CreateDirectory(const Path& path) const { | ||||
|     LOG_CRITICAL(Service_FS, "Attempted to create a directory in an NCCH archive (%s).", | ||||
|                  GetName().c_str()); | ||||
|     // TODO(wwylele): Use correct error code
 | ||||
|     return ResultCode(-1); | ||||
| } | ||||
| 
 | ||||
| ResultCode NCCHArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const { | ||||
|     LOG_CRITICAL(Service_FS, "Attempted to rename a file within an NCCH archive (%s).", | ||||
|                  GetName().c_str()); | ||||
|     // TODO(wwylele): Use correct error code
 | ||||
|     return ResultCode(-1); | ||||
| } | ||||
| 
 | ||||
| ResultVal<std::unique_ptr<DirectoryBackend>> NCCHArchive::OpenDirectory(const Path& path) const { | ||||
|     LOG_CRITICAL(Service_FS, "Attempted to open a directory within an NCCH archive (%s).", | ||||
|                  GetName().c_str()); | ||||
|     // TODO(shinyquagsire23): Use correct error code
 | ||||
|     return ResultCode(-1); | ||||
| } | ||||
| 
 | ||||
| u64 NCCHArchive::GetFreeBytes() const { | ||||
|     LOG_WARNING(Service_FS, "Attempted to get the free space in an NCCH archive"); | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////
 | ||||
| 
 | ||||
| ResultVal<size_t> NCCHFile::Read(const u64 offset, const size_t length, u8* buffer) const { | ||||
|     LOG_TRACE(Service_FS, "called offset=%" PRIu64 ", length=%zu", offset, length); | ||||
|     size_t length_left = static_cast<size_t>(data_size - offset); | ||||
|     size_t read_length = static_cast<size_t>(std::min(length, length_left)); | ||||
| 
 | ||||
|     size_t available_size = static_cast<size_t>(file_buffer.size() - offset); | ||||
|     size_t copy_size = std::min(length, available_size); | ||||
|     memcpy(buffer, file_buffer.data() + offset, copy_size); | ||||
| 
 | ||||
|     return MakeResult<size_t>(copy_size); | ||||
| } | ||||
| 
 | ||||
| ResultVal<size_t> NCCHFile::Write(const u64 offset, const size_t length, const bool flush, | ||||
|                                   const u8* buffer) const { | ||||
|     LOG_ERROR(Service_FS, "Attempted to write to NCCH file"); | ||||
|     // TODO(shinyquagsire23): Find error code
 | ||||
|     return MakeResult<size_t>(0); | ||||
| } | ||||
| 
 | ||||
| u64 NCCHFile::GetSize() const { | ||||
|     return file_buffer.size(); | ||||
| } | ||||
| 
 | ||||
| bool NCCHFile::SetSize(const u64 size) const { | ||||
|     LOG_ERROR(Service_FS, "Attempted to set the size of an NCCH file"); | ||||
|     return false; | ||||
| } | ||||
| 
 | ||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////
 | ||||
| 
 | ||||
| ArchiveFactory_NCCH::ArchiveFactory_NCCH() {} | ||||
| 
 | ||||
| ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& path) { | ||||
|     if (path.GetType() != LowPathType::Binary) { | ||||
|         LOG_ERROR(Service_FS, "Path need to be Binary"); | ||||
|         return ERROR_INVALID_PATH; | ||||
|     } | ||||
| 
 | ||||
|     std::vector<u8> binary = path.AsBinary(); | ||||
|     if (binary.size() != sizeof(NCCHArchivePath)) { | ||||
|         LOG_ERROR(Service_FS, "Wrong path size %zu", binary.size()); | ||||
|         return ERROR_INVALID_PATH; | ||||
|     } | ||||
| 
 | ||||
|     NCCHArchivePath open_path; | ||||
|     std::memcpy(&open_path, binary.data(), sizeof(NCCHArchivePath)); | ||||
| 
 | ||||
|     auto archive = std::make_unique<NCCHArchive>( | ||||
|         open_path.tid, static_cast<Service::FS::MediaType>(open_path.media_type & 0xFF)); | ||||
|     return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -7,17 +7,71 @@ | |||
| #include <memory> | ||||
| #include <string> | ||||
| #include "core/file_sys/archive_backend.h" | ||||
| #include "core/file_sys/file_backend.h" | ||||
| #include "core/hle/result.h" | ||||
| 
 | ||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////
 | ||||
| // FileSys namespace
 | ||||
| 
 | ||||
| namespace Service { | ||||
| namespace FS { | ||||
| enum class MediaType : u32; | ||||
| } | ||||
| } | ||||
| 
 | ||||
| namespace FileSys { | ||||
| 
 | ||||
| /// Archive backend for NCCH Archives (RomFS, ExeFS)
 | ||||
| class NCCHArchive : public ArchiveBackend { | ||||
| public: | ||||
|     explicit NCCHArchive(u64 title_id, Service::FS::MediaType media_type) | ||||
|         : title_id(title_id), media_type(media_type) {} | ||||
| 
 | ||||
|     std::string GetName() const override { | ||||
|         return "NCCHArchive"; | ||||
|     } | ||||
| 
 | ||||
|     ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path, | ||||
|                                                      const Mode& mode) const override; | ||||
|     ResultCode DeleteFile(const Path& path) const override; | ||||
|     ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override; | ||||
|     ResultCode DeleteDirectory(const Path& path) const override; | ||||
|     ResultCode DeleteDirectoryRecursively(const Path& path) const override; | ||||
|     ResultCode CreateFile(const Path& path, u64 size) const override; | ||||
|     ResultCode CreateDirectory(const Path& path) const override; | ||||
|     ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override; | ||||
|     ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const override; | ||||
|     u64 GetFreeBytes() const override; | ||||
| 
 | ||||
| protected: | ||||
|     u64 title_id; | ||||
|     Service::FS::MediaType media_type; | ||||
| }; | ||||
| 
 | ||||
| // File backend for NCCH files
 | ||||
| class NCCHFile : public FileBackend { | ||||
| public: | ||||
|     NCCHFile(std::vector<u8> buffer) : file_buffer(buffer) {} | ||||
| 
 | ||||
|     ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; | ||||
|     ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) const override; | ||||
|     u64 GetSize() const override; | ||||
|     bool SetSize(u64 size) const override; | ||||
|     bool Close() const override { | ||||
|         return false; | ||||
|     } | ||||
|     void Flush() const override {} | ||||
| 
 | ||||
| private: | ||||
|     std::vector<u8> file_buffer; | ||||
|     u64 data_offset; | ||||
|     u64 data_size; | ||||
| }; | ||||
| 
 | ||||
| /// File system interface to the NCCH archive
 | ||||
| class ArchiveFactory_NCCH final : public ArchiveFactory { | ||||
| public: | ||||
|     explicit ArchiveFactory_NCCH(const std::string& mount_point); | ||||
|     explicit ArchiveFactory_NCCH(); | ||||
| 
 | ||||
|     std::string GetName() const override { | ||||
|         return "NCCH"; | ||||
|  | @ -26,9 +80,6 @@ public: | |||
|     ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override; | ||||
|     ResultCode Format(const Path& path, const FileSys::ArchiveFormatInfo& format_info) override; | ||||
|     ResultVal<ArchiveFormatInfo> GetFormatInfo(const Path& path) const override; | ||||
| 
 | ||||
| private: | ||||
|     std::string mount_point; | ||||
| }; | ||||
| 
 | ||||
| } // namespace FileSys
 | ||||
|  |  | |||
|  | @ -153,6 +153,18 @@ u32 TitleMetadata::GetDLPContentID() const { | |||
|     return tmd_chunks[TMDContentIndex::DLP].id; | ||||
| } | ||||
| 
 | ||||
| u32 TitleMetadata::GetContentIDByIndex(u16 index) const { | ||||
|     return tmd_chunks[index].id; | ||||
| } | ||||
| 
 | ||||
| u16 TitleMetadata::GetContentTypeByIndex(u16 index) const { | ||||
|     return tmd_chunks[index].type; | ||||
| } | ||||
| 
 | ||||
| u64 TitleMetadata::GetContentSizeByIndex(u16 index) const { | ||||
|     return tmd_chunks[index].size; | ||||
| } | ||||
| 
 | ||||
| void TitleMetadata::SetTitleID(u64 title_id) { | ||||
|     tmd_body.title_id = title_id; | ||||
| } | ||||
|  |  | |||
|  | @ -35,6 +35,8 @@ enum TMDContentTypeFlag : u16 { | |||
|     Shared = 1 << 15 | ||||
| }; | ||||
| 
 | ||||
| enum TMDContentIndex { Main = 0, Manual = 1, DLP = 2 }; | ||||
| 
 | ||||
| /**
 | ||||
|  * Helper which implements an interface to read and write Title Metadata (TMD) files. | ||||
|  * If a file path is provided and the file exists, it can be parsed and used, otherwise | ||||
|  | @ -102,6 +104,9 @@ public: | |||
|     u32 GetBootContentID() const; | ||||
|     u32 GetManualContentID() const; | ||||
|     u32 GetDLPContentID() const; | ||||
|     u32 GetContentIDByIndex(u16 index) const; | ||||
|     u16 GetContentTypeByIndex(u16 index) const; | ||||
|     u64 GetContentSizeByIndex(u16 index) const; | ||||
| 
 | ||||
|     void SetTitleID(u64 title_id); | ||||
|     void SetTitleType(u32 type); | ||||
|  | @ -112,8 +117,6 @@ public: | |||
|     void Print() const; | ||||
| 
 | ||||
| private: | ||||
|     enum TMDContentIndex { Main = 0, Manual = 1, DLP = 2 }; | ||||
| 
 | ||||
|     Body tmd_body; | ||||
|     u32_be signature_type; | ||||
|     std::vector<u8> tmd_signature; | ||||
|  |  | |||
|  | @ -4,116 +4,335 @@ | |||
| 
 | ||||
| #include <array> | ||||
| #include <cinttypes> | ||||
| #include "common/common_types.h" | ||||
| #include "common/file_util.h" | ||||
| #include "common/logging/log.h" | ||||
| #include "common/string_util.h" | ||||
| #include "core/file_sys/ncch_container.h" | ||||
| #include "core/file_sys/title_metadata.h" | ||||
| #include "core/hle/ipc.h" | ||||
| #include "core/hle/ipc_helpers.h" | ||||
| #include "core/hle/result.h" | ||||
| #include "core/hle/service/am/am.h" | ||||
| #include "core/hle/service/am/am_app.h" | ||||
| #include "core/hle/service/am/am_net.h" | ||||
| #include "core/hle/service/am/am_sys.h" | ||||
| #include "core/hle/service/am/am_u.h" | ||||
| #include "core/hle/service/fs/archive.h" | ||||
| #include "core/hle/service/service.h" | ||||
| #include "core/loader/loader.h" | ||||
| 
 | ||||
| namespace Service { | ||||
| namespace AM { | ||||
| 
 | ||||
| static std::array<u32, 3> am_content_count = {0, 0, 0}; | ||||
| static std::array<u32, 3> am_titles_count = {0, 0, 0}; | ||||
| static std::array<u32, 3> am_titles_list_count = {0, 0, 0}; | ||||
| static u32 am_ticket_count = 0; | ||||
| static u32 am_ticket_list_count = 0; | ||||
| static bool lists_initialized = false; | ||||
| static std::array<std::vector<u64_le>, 3> am_title_list; | ||||
| 
 | ||||
| struct TitleInfo { | ||||
|     u64_le tid; | ||||
|     u64_le size; | ||||
|     u16_le version; | ||||
|     u16_le unused; | ||||
|     u32_le type; | ||||
| }; | ||||
| 
 | ||||
| static_assert(sizeof(TitleInfo) == 0x18, "Title info structure size is wrong"); | ||||
| 
 | ||||
| struct ContentInfo { | ||||
|     u16_le index; | ||||
|     u16_le type; | ||||
|     u32_le content_id; | ||||
|     u64_le size; | ||||
|     u64_le romfs_size; | ||||
| }; | ||||
| 
 | ||||
| static_assert(sizeof(ContentInfo) == 0x18, "Content info structure size is wrong"); | ||||
| 
 | ||||
| struct TicketInfo { | ||||
|     u64_le title_id; | ||||
|     u64_le ticket_id; | ||||
|     u16_le version; | ||||
|     u16_le unused; | ||||
|     u32_le size; | ||||
| }; | ||||
| 
 | ||||
| static_assert(sizeof(TicketInfo) == 0x18, "Ticket info structure size is wrong"); | ||||
| 
 | ||||
| std::string GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid) { | ||||
|     std::string content_path = GetTitlePath(media_type, tid) + "content/"; | ||||
| 
 | ||||
|     if (media_type == Service::FS::MediaType::GameCard) { | ||||
|         LOG_ERROR(Service_AM, "Invalid request for nonexistent gamecard title metadata!"); | ||||
|         return ""; | ||||
|     } | ||||
| 
 | ||||
|     // The TMD ID is usually held in the title databases, which we don't implement.
 | ||||
|     // For now, just scan for any .tmd files which exist and use the first .tmd
 | ||||
|     // found (there should only really be one unless the directories are meddled with)
 | ||||
|     FileUtil::FSTEntry entries; | ||||
|     FileUtil::ScanDirectoryTree(content_path, entries); | ||||
|     for (const FileUtil::FSTEntry& entry : entries.children) { | ||||
|         std::string filename_filename, filename_extension; | ||||
|         Common::SplitPath(entry.virtualName, nullptr, &filename_filename, &filename_extension); | ||||
| 
 | ||||
|         if (filename_extension == ".tmd") | ||||
|             return content_path + entry.virtualName; | ||||
|     } | ||||
| 
 | ||||
|     // If we can't find an existing .tmd, return a path for one to be created.
 | ||||
|     return content_path + "00000000.tmd"; | ||||
| } | ||||
| 
 | ||||
| std::string GetTitleContentPath(Service::FS::MediaType media_type, u64 tid, u16 index) { | ||||
|     std::string content_path = GetTitlePath(media_type, tid) + "content/"; | ||||
| 
 | ||||
|     if (media_type == Service::FS::MediaType::GameCard) { | ||||
|         // TODO(shinyquagsire23): get current app file if TID matches?
 | ||||
|         LOG_ERROR(Service_AM, "Request for gamecard partition %u content path unimplemented!", | ||||
|                   static_cast<u32>(index)); | ||||
|         return ""; | ||||
|     } | ||||
| 
 | ||||
|     std::string tmd_path = GetTitleMetadataPath(media_type, tid); | ||||
| 
 | ||||
|     u32 content_id = 0; | ||||
|     FileSys::TitleMetadata tmd(tmd_path); | ||||
|     if (tmd.Load() == Loader::ResultStatus::Success) { | ||||
|         content_id = tmd.GetContentIDByIndex(index); | ||||
| 
 | ||||
|         // TODO(shinyquagsire23): how does DLC actually get this folder on hardware?
 | ||||
|         // For now, check if the second (index 1) content has the optional flag set, for most
 | ||||
|         // apps this is usually the manual and not set optional, DLC has it set optional.
 | ||||
|         // All .apps (including index 0) will be in the 00000000/ folder for DLC.
 | ||||
|         if (tmd.GetContentCount() > 1 && | ||||
|             tmd.GetContentTypeByIndex(1) & FileSys::TMDContentTypeFlag::Optional) { | ||||
|             content_path += "00000000/"; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id); | ||||
| } | ||||
| 
 | ||||
| std::string GetTitlePath(Service::FS::MediaType media_type, u64 tid) { | ||||
|     u32 high = static_cast<u32>(tid >> 32); | ||||
|     u32 low = static_cast<u32>(tid & 0xFFFFFFFF); | ||||
| 
 | ||||
|     if (media_type == Service::FS::MediaType::NAND || media_type == Service::FS::MediaType::SDMC) | ||||
|         return Common::StringFromFormat("%s%08x/%08x/", GetMediaTitlePath(media_type).c_str(), high, | ||||
|                                         low); | ||||
| 
 | ||||
|     if (media_type == Service::FS::MediaType::GameCard) { | ||||
|         // TODO(shinyquagsire23): get current app path if TID matches?
 | ||||
|         LOG_ERROR(Service_AM, "Request for gamecard title path unimplemented!"); | ||||
|         return ""; | ||||
|     } | ||||
| 
 | ||||
|     return ""; | ||||
| } | ||||
| 
 | ||||
| std::string GetMediaTitlePath(Service::FS::MediaType media_type) { | ||||
|     if (media_type == Service::FS::MediaType::NAND) | ||||
|         return Common::StringFromFormat("%s%s/title/", FileUtil::GetUserPath(D_NAND_IDX).c_str(), | ||||
|                                         SYSTEM_ID); | ||||
| 
 | ||||
|     if (media_type == Service::FS::MediaType::SDMC) | ||||
|         return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/", | ||||
|                                         FileUtil::GetUserPath(D_SDMC_IDX).c_str(), SYSTEM_ID, | ||||
|                                         SDCARD_ID); | ||||
| 
 | ||||
|     if (media_type == Service::FS::MediaType::GameCard) { | ||||
|         // TODO(shinyquagsire23): get current app parent folder if TID matches?
 | ||||
|         LOG_ERROR(Service_AM, "Request for gamecard parent path unimplemented!"); | ||||
|         return ""; | ||||
|     } | ||||
| 
 | ||||
|     return ""; | ||||
| } | ||||
| 
 | ||||
| void ScanForTitles(Service::FS::MediaType media_type) { | ||||
|     am_title_list[static_cast<u32>(media_type)].clear(); | ||||
| 
 | ||||
|     std::string title_path = GetMediaTitlePath(media_type); | ||||
| 
 | ||||
|     FileUtil::FSTEntry entries; | ||||
|     FileUtil::ScanDirectoryTree(title_path, entries, 1); | ||||
|     for (const FileUtil::FSTEntry& tid_high : entries.children) { | ||||
|         for (const FileUtil::FSTEntry& tid_low : tid_high.children) { | ||||
|             std::string tid_string = tid_high.virtualName + tid_low.virtualName; | ||||
|             u64 tid = std::stoull(tid_string.c_str(), nullptr, 16); | ||||
| 
 | ||||
|             FileSys::NCCHContainer container(GetTitleContentPath(media_type, tid)); | ||||
|             if (container.Load() == Loader::ResultStatus::Success) | ||||
|                 am_title_list[static_cast<u32>(media_type)].push_back(tid); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void ScanForAllTitles() { | ||||
|     ScanForTitles(Service::FS::MediaType::NAND); | ||||
|     ScanForTitles(Service::FS::MediaType::SDMC); | ||||
| } | ||||
| 
 | ||||
| void GetNumPrograms(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1, 1, 0); // 0x00010040
 | ||||
|     u32 media_type = rp.Pop<u8>(); | ||||
| 
 | ||||
|     u32 media_type = cmd_buff[1] & 0xFF; | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     cmd_buff[2] = am_titles_count[media_type]; | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, title_count=0x%08x", media_type, | ||||
|                 am_titles_count[media_type]); | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
|     rb.Push<u32>(am_title_list[media_type].size()); | ||||
| } | ||||
| 
 | ||||
| void FindContentInfos(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1002, 4, 2); // 0x10020104
 | ||||
| 
 | ||||
|     u32 media_type = cmd_buff[1] & 0xFF; | ||||
|     u64 title_id = (static_cast<u64>(cmd_buff[3]) << 32) | cmd_buff[2]; | ||||
|     u32 content_ids_pointer = cmd_buff[6]; | ||||
|     u32 content_info_pointer = cmd_buff[8]; | ||||
|     auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>()); | ||||
|     u64 title_id = rp.Pop<u64>(); | ||||
|     u32 content_count = rp.Pop<u32>(); | ||||
|     VAddr content_requested_in = rp.PopMappedBuffer(); | ||||
|     VAddr content_info_out = rp.PopMappedBuffer(); | ||||
| 
 | ||||
|     am_content_count[media_type] = cmd_buff[4]; | ||||
|     std::vector<u16_le> content_requested(content_count); | ||||
|     Memory::ReadBlock(content_requested_in, content_requested.data(), content_count * sizeof(u16)); | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, title_id=0x%016llx, content_cound=%u, " | ||||
|                             "content_ids_pointer=0x%08x, content_info_pointer=0x%08x", | ||||
|                 media_type, title_id, am_content_count[media_type], content_ids_pointer, | ||||
|                 content_info_pointer); | ||||
|     std::string tmd_path = GetTitleMetadataPath(media_type, title_id); | ||||
| 
 | ||||
|     u32 content_read = 0; | ||||
|     FileSys::TitleMetadata tmd(tmd_path); | ||||
|     if (tmd.Load() == Loader::ResultStatus::Success) { | ||||
|         // Get info for each content index requested
 | ||||
|         for (size_t i = 0; i < content_count; i++) { | ||||
|             std::shared_ptr<FileUtil::IOFile> romfs_file; | ||||
|             u64 romfs_offset = 0; | ||||
|             u64 romfs_size = 0; | ||||
| 
 | ||||
|             FileSys::NCCHContainer ncch_container(GetTitleContentPath(media_type, title_id, i)); | ||||
|             ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size); | ||||
| 
 | ||||
|             ContentInfo content_info = {}; | ||||
|             content_info.index = static_cast<u16>(i); | ||||
|             content_info.type = tmd.GetContentTypeByIndex(content_requested[i]); | ||||
|             content_info.content_id = tmd.GetContentIDByIndex(content_requested[i]); | ||||
|             content_info.size = tmd.GetContentSizeByIndex(content_requested[i]); | ||||
|             content_info.romfs_size = romfs_size; | ||||
| 
 | ||||
|             Memory::WriteBlock(content_info_out, &content_info, sizeof(ContentInfo)); | ||||
|             content_info_out += sizeof(ContentInfo); | ||||
|             content_read++; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
| } | ||||
| 
 | ||||
| void ListContentInfos(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1003, 5, 1); // 0x10030142
 | ||||
|     u32 content_count = rp.Pop<u32>(); | ||||
|     auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>()); | ||||
|     u64 title_id = rp.Pop<u64>(); | ||||
|     u32 start_index = rp.Pop<u32>(); | ||||
|     VAddr content_info_out = rp.PopMappedBuffer(); | ||||
| 
 | ||||
|     u32 media_type = cmd_buff[2] & 0xFF; | ||||
|     u64 title_id = (static_cast<u64>(cmd_buff[4]) << 32) | cmd_buff[3]; | ||||
|     u32 start_index = cmd_buff[5]; | ||||
|     u32 content_info_pointer = cmd_buff[7]; | ||||
|     std::string tmd_path = GetTitleMetadataPath(media_type, title_id); | ||||
| 
 | ||||
|     am_content_count[media_type] = cmd_buff[1]; | ||||
|     u32 copied = 0; | ||||
|     FileSys::TitleMetadata tmd(tmd_path); | ||||
|     if (tmd.Load() == Loader::ResultStatus::Success) { | ||||
|         copied = std::min(content_count, static_cast<u32>(tmd.GetContentCount())); | ||||
|         for (u32 i = start_index; i < copied; i++) { | ||||
|             std::shared_ptr<FileUtil::IOFile> romfs_file; | ||||
|             u64 romfs_offset = 0; | ||||
|             u64 romfs_size = 0; | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     cmd_buff[2] = am_content_count[media_type]; | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, content_count=%u, title_id=0x%016" PRIx64 | ||||
|                             ", start_index=0x%08x, content_info_pointer=0x%08X", | ||||
|                 media_type, am_content_count[media_type], title_id, start_index, | ||||
|                 content_info_pointer); | ||||
|             FileSys::NCCHContainer ncch_container(GetTitleContentPath(media_type, title_id, i)); | ||||
|             ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size); | ||||
| 
 | ||||
|             ContentInfo content_info = {}; | ||||
|             content_info.index = static_cast<u16>(i); | ||||
|             content_info.type = tmd.GetContentTypeByIndex(i); | ||||
|             content_info.content_id = tmd.GetContentIDByIndex(i); | ||||
|             content_info.size = tmd.GetContentSizeByIndex(i); | ||||
|             content_info.romfs_size = romfs_size; | ||||
| 
 | ||||
|             Memory::WriteBlock(content_info_out, &content_info, sizeof(ContentInfo)); | ||||
|             content_info_out += sizeof(ContentInfo); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
|     rb.Push(copied); | ||||
| } | ||||
| 
 | ||||
| void DeleteContents(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1004, 4, 1); // 0x10040102
 | ||||
|     u8 media_type = rp.Pop<u8>(); | ||||
|     u64 title_id = rp.Pop<u64>(); | ||||
|     u32 content_count = rp.Pop<u32>(); | ||||
|     VAddr content_ids_in = rp.PopMappedBuffer(); | ||||
| 
 | ||||
|     u32 media_type = cmd_buff[1] & 0xFF; | ||||
|     u64 title_id = (static_cast<u64>(cmd_buff[3]) << 32) | cmd_buff[2]; | ||||
|     u32 content_ids_pointer = cmd_buff[6]; | ||||
| 
 | ||||
|     am_content_count[media_type] = cmd_buff[4]; | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, title_id=0x%016" PRIx64 | ||||
|                             ", content_count=%u, content_ids_pointer=0x%08x", | ||||
|                 media_type, title_id, am_content_count[media_type], content_ids_pointer); | ||||
|                             ", content_count=%u, content_ids_in=0x%08x", | ||||
|                 media_type, title_id, content_count, content_ids_in); | ||||
| } | ||||
| 
 | ||||
| void GetProgramList(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 2, 2, 1); // 0x00020082
 | ||||
| 
 | ||||
|     u32 media_type = cmd_buff[2] & 0xFF; | ||||
|     u32 title_ids_output_pointer = cmd_buff[4]; | ||||
|     u32 count = rp.Pop<u32>(); | ||||
|     u8 media_type = rp.Pop<u8>(); | ||||
|     VAddr title_ids_output_pointer = rp.PopMappedBuffer(); | ||||
| 
 | ||||
|     am_titles_list_count[media_type] = cmd_buff[1]; | ||||
|     if (!Memory::IsValidVirtualAddress(title_ids_output_pointer) || media_type > 2) { | ||||
|         IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|         rb.Push<u32>(-1); // TODO(shinyquagsire23): Find the right error code
 | ||||
|         rb.Push<u32>(0); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     cmd_buff[2] = am_titles_list_count[media_type]; | ||||
|     LOG_WARNING( | ||||
|         Service_AM, | ||||
|         "(STUBBED) media_type=%u, titles_list_count=0x%08X, title_ids_output_pointer=0x%08X", | ||||
|         media_type, am_titles_list_count[media_type], title_ids_output_pointer); | ||||
|     u32 media_count = static_cast<u32>(am_title_list[media_type].size()); | ||||
|     u32 copied = std::min(media_count, count); | ||||
| 
 | ||||
|     Memory::WriteBlock(title_ids_output_pointer, am_title_list[media_type].data(), | ||||
|                        copied * sizeof(u64)); | ||||
| 
 | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
|     rb.Push(copied); | ||||
| } | ||||
| 
 | ||||
| void GetProgramInfos(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 3, 2, 2); // 0x00030084
 | ||||
| 
 | ||||
|     u32 media_type = cmd_buff[1] & 0xFF; | ||||
|     u32 title_id_list_pointer = cmd_buff[4]; | ||||
|     u32 title_list_pointer = cmd_buff[6]; | ||||
|     auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>()); | ||||
|     u32 title_count = rp.Pop<u32>(); | ||||
|     VAddr title_id_list_pointer = rp.PopMappedBuffer(); | ||||
|     VAddr title_info_out = rp.PopMappedBuffer(); | ||||
| 
 | ||||
|     am_titles_count[media_type] = cmd_buff[2]; | ||||
|     std::vector<u64> title_id_list(title_count); | ||||
|     Memory::ReadBlock(title_id_list_pointer, title_id_list.data(), title_count * sizeof(u64)); | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) media_type=%u, total_titles=0x%08X, " | ||||
|                             "title_id_list_pointer=0x%08X, title_list_pointer=0x%08X", | ||||
|                 media_type, am_titles_count[media_type], title_id_list_pointer, title_list_pointer); | ||||
|     for (u32 i = 0; i < title_count; i++) { | ||||
|         std::string tmd_path = GetTitleMetadataPath(media_type, title_id_list[i]); | ||||
| 
 | ||||
|         TitleInfo title_info = {}; | ||||
|         title_info.tid = title_id_list[i]; | ||||
| 
 | ||||
|         FileSys::TitleMetadata tmd(tmd_path); | ||||
|         if (tmd.Load() == Loader::ResultStatus::Success) { | ||||
|             // TODO(shinyquagsire23): This is the total size of all files this process owns,
 | ||||
|             // including savefiles and other content. This comes close but is off.
 | ||||
|             title_info.size = tmd.GetContentSizeByIndex(FileSys::TMDContentIndex::Main); | ||||
|             title_info.version = tmd.GetTitleVersion(); | ||||
|             title_info.type = tmd.GetTitleType(); | ||||
|         } | ||||
|         Memory::WriteBlock(title_info_out, &title_info, sizeof(TitleInfo)); | ||||
|         title_info_out += sizeof(TitleInfo); | ||||
|     } | ||||
| 
 | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
| } | ||||
| 
 | ||||
| void GetDataTitleInfos(Service::Interface* self) { | ||||
|  | @ -123,60 +342,126 @@ void GetDataTitleInfos(Service::Interface* self) { | |||
| } | ||||
| 
 | ||||
| void ListDataTitleTicketInfos(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1007, 4, 1); // 0x10070102
 | ||||
|     u32 ticket_count = rp.Pop<u32>(); | ||||
|     u64 title_id = rp.Pop<u64>(); | ||||
|     u32 start_index = rp.Pop<u32>(); | ||||
|     VAddr ticket_info_out = rp.PopMappedBuffer(); | ||||
|     VAddr ticket_info_write = ticket_info_out; | ||||
| 
 | ||||
|     u64 title_id = (static_cast<u64>(cmd_buff[3]) << 32) | cmd_buff[2]; | ||||
|     u32 start_index = cmd_buff[4]; | ||||
|     u32 ticket_info_pointer = cmd_buff[6]; | ||||
|     for (u32 i = 0; i < ticket_count; i++) { | ||||
|         TicketInfo ticket_info = {}; | ||||
|         ticket_info.title_id = title_id; | ||||
|         ticket_info.version = 0; // TODO
 | ||||
|         ticket_info.size = 0;    // TODO
 | ||||
| 
 | ||||
|     am_ticket_count = cmd_buff[1]; | ||||
|         Memory::WriteBlock(ticket_info_write, &ticket_info, sizeof(TicketInfo)); | ||||
|         ticket_info_write += sizeof(TicketInfo); | ||||
|     } | ||||
| 
 | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
|     rb.Push(ticket_count); | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     cmd_buff[2] = am_ticket_count; | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) ticket_count=0x%08X, title_id=0x%016" PRIx64 | ||||
|                             ", start_index=0x%08X, ticket_info_pointer=0x%08X", | ||||
|                 am_ticket_count, title_id, start_index, ticket_info_pointer); | ||||
|                             ", start_index=0x%08X, ticket_info_out=0x%08X", | ||||
|                 ticket_count, title_id, start_index, ticket_info_out); | ||||
| } | ||||
| 
 | ||||
| void GetNumContentInfos(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1001, 3, 0); // 0x100100C0
 | ||||
|     auto media_type = static_cast<Service::FS::MediaType>(rp.Pop<u8>()); | ||||
|     u64 title_id = rp.Pop<u64>(); | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     cmd_buff[2] = 1; // Number of content infos plus one
 | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) called"); | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); // No error
 | ||||
| 
 | ||||
|     std::string tmd_path = GetTitleMetadataPath(media_type, title_id); | ||||
| 
 | ||||
|     FileSys::TitleMetadata tmd(tmd_path); | ||||
|     if (tmd.Load() == Loader::ResultStatus::Success) { | ||||
|         rb.Push<u32>(tmd.GetContentCount()); | ||||
|     } else { | ||||
|         rb.Push<u32>(1); // Number of content infos plus one
 | ||||
|         LOG_WARNING(Service_AM, "(STUBBED) called media_type=%u, title_id=0x%016" PRIx64, | ||||
|                     media_type, title_id); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void DeleteTicket(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 7, 2, 0); // 0x00070080
 | ||||
|     u64 title_id = rp.Pop<u64>(); | ||||
| 
 | ||||
|     u64 title_id = (static_cast<u64>(cmd_buff[2]) << 32) | cmd_buff[1]; | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) called title_id=0x%016" PRIx64 "", title_id); | ||||
| } | ||||
| 
 | ||||
| void GetNumTickets(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 8, 0, 0); // 0x00080000
 | ||||
|     u32 ticket_count = 0; | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     cmd_buff[2] = am_ticket_count; | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) called ticket_count=0x%08x", am_ticket_count); | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
|     rb.Push(ticket_count); | ||||
|     LOG_WARNING(Service_AM, "(STUBBED) called ticket_count=0x%08x", ticket_count); | ||||
| } | ||||
| 
 | ||||
| void GetTicketList(Service::Interface* self) { | ||||
|     u32* cmd_buff = Kernel::GetCommandBuffer(); | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 9, 2, 1); // 0x00090082
 | ||||
|     u32 ticket_list_count = rp.Pop<u32>(); | ||||
|     u32 ticket_index = rp.Pop<u32>(); | ||||
|     VAddr ticket_tids_out = rp.PopMappedBuffer(); | ||||
| 
 | ||||
|     u32 num_of_skip = cmd_buff[2]; | ||||
|     u32 ticket_list_pointer = cmd_buff[4]; | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); | ||||
|     rb.Push(ticket_list_count); | ||||
|     LOG_WARNING(Service_AM, | ||||
|                 "(STUBBED) ticket_list_count=0x%08x, ticket_index=0x%08x, ticket_tids_out=0x%08x", | ||||
|                 ticket_list_count, ticket_index, ticket_tids_out); | ||||
| } | ||||
| 
 | ||||
|     am_ticket_list_count = cmd_buff[1]; | ||||
| void QueryAvailableTitleDatabase(Service::Interface* self) { | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x19, 1, 0); // 0x190040
 | ||||
|     u8 media_type = rp.Pop<u8>(); | ||||
| 
 | ||||
|     cmd_buff[1] = RESULT_SUCCESS.raw; | ||||
|     cmd_buff[2] = am_ticket_list_count; | ||||
|     LOG_WARNING( | ||||
|         Service_AM, | ||||
|         "(STUBBED) ticket_list_count=0x%08x, num_of_skip=0x%08x, ticket_list_pointer=0x%08x", | ||||
|         am_ticket_list_count, num_of_skip, ticket_list_pointer); | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); // No error
 | ||||
|     rb.Push(true); | ||||
| 
 | ||||
|     LOG_WARNING(Service_APT, "(STUBBED) media_type=%u", media_type); | ||||
| } | ||||
| 
 | ||||
| void CheckContentRights(Service::Interface* self) { | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x25, 3, 0); // 0x2500C0
 | ||||
|     u64 tid = rp.Pop<u64>(); | ||||
|     u16 content_index = rp.Pop<u16>(); | ||||
| 
 | ||||
|     // TODO(shinyquagsire23): Read tickets for this instead?
 | ||||
|     bool has_rights = | ||||
|         FileUtil::Exists(GetTitleContentPath(Service::FS::MediaType::SDMC, tid, content_index)); | ||||
| 
 | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); // No error
 | ||||
|     rb.Push(has_rights); | ||||
| 
 | ||||
|     LOG_WARNING(Service_APT, "(STUBBED) tid=%016" PRIx64 ", content_index=%u", tid, content_index); | ||||
| } | ||||
| 
 | ||||
| void CheckContentRightsIgnorePlatform(Service::Interface* self) { | ||||
|     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x2D, 3, 0); // 0x2D00C0
 | ||||
|     u64 tid = rp.Pop<u64>(); | ||||
|     u16 content_index = rp.Pop<u16>(); | ||||
| 
 | ||||
|     // TODO(shinyquagsire23): Read tickets for this instead?
 | ||||
|     bool has_rights = | ||||
|         FileUtil::Exists(GetTitleContentPath(Service::FS::MediaType::SDMC, tid, content_index)); | ||||
| 
 | ||||
|     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); | ||||
|     rb.Push(RESULT_SUCCESS); // No error
 | ||||
|     rb.Push(has_rights); | ||||
| 
 | ||||
|     LOG_WARNING(Service_APT, "(STUBBED) tid=%016" PRIx64 ", content_index=%u", tid, content_index); | ||||
| } | ||||
| 
 | ||||
| void Init() { | ||||
|  | @ -184,6 +469,8 @@ void Init() { | |||
|     AddService(new AM_NET_Interface); | ||||
|     AddService(new AM_SYS_Interface); | ||||
|     AddService(new AM_U_Interface); | ||||
| 
 | ||||
|     ScanForAllTitles(); | ||||
| } | ||||
| 
 | ||||
| void Shutdown() {} | ||||
|  |  | |||
|  | @ -4,12 +4,64 @@ | |||
| 
 | ||||
| #pragma once | ||||
| 
 | ||||
| #include <string> | ||||
| #include "common/common_types.h" | ||||
| 
 | ||||
| namespace Service { | ||||
| namespace FS { | ||||
| enum class MediaType : u32; | ||||
| } | ||||
| } | ||||
| 
 | ||||
| namespace Service { | ||||
| 
 | ||||
| class Interface; | ||||
| 
 | ||||
| namespace AM { | ||||
| 
 | ||||
| /**
 | ||||
|  * Get the .tmd path for a title | ||||
|  * @param media_type the media the title exists on | ||||
|  * @param tid the title ID to get | ||||
|  * @returns string path to the .tmd file if it exists, otherwise a path to create one is given. | ||||
|  */ | ||||
| std::string GetTitleMetadataPath(Service::FS::MediaType media_type, u64 tid); | ||||
| 
 | ||||
| /**
 | ||||
|  * Get the .app path for a title's installed content index. | ||||
|  * @param media_type the media the title exists on | ||||
|  * @param tid the title ID to get | ||||
|  * @param index the content index to get | ||||
|  * @returns string path to the .app file | ||||
|  */ | ||||
| std::string GetTitleContentPath(Service::FS::MediaType media_type, u64 tid, u16 index = 0); | ||||
| 
 | ||||
| /**
 | ||||
|  * Get the folder for a title's installed content. | ||||
|  * @param media_type the media the title exists on | ||||
|  * @param tid the title ID to get | ||||
|  * @returns string path to the title folder | ||||
|  */ | ||||
| std::string GetTitlePath(Service::FS::MediaType media_type, u64 tid); | ||||
| 
 | ||||
| /**
 | ||||
|  * Get the title/ folder for a storage medium. | ||||
|  * @param media_type the storage medium to get the path for | ||||
|  * @returns string path to the folder | ||||
|  */ | ||||
| std::string GetMediaTitlePath(Service::FS::MediaType media_type); | ||||
| 
 | ||||
| /**
 | ||||
|  * Scans the for titles in a storage medium for listing. | ||||
|  * @param media_type the storage medium to scan | ||||
|  */ | ||||
| void ScanForTitles(Service::FS::MediaType media_type); | ||||
| 
 | ||||
| /**
 | ||||
|  * Scans all storage mediums for titles for listing. | ||||
|  */ | ||||
| void ScanForAllTitles(); | ||||
| 
 | ||||
| /**
 | ||||
|  * AM::GetNumPrograms service function | ||||
|  * Gets the number of installed titles in the requested media type | ||||
|  | @ -154,6 +206,38 @@ void GetNumTickets(Service::Interface* self); | |||
|  */ | ||||
| void GetTicketList(Service::Interface* self); | ||||
| 
 | ||||
| /**
 | ||||
|  * AM::QueryAvailableTitleDatabase service function | ||||
|  *  Inputs: | ||||
|  *      1 : Media Type | ||||
|  *  Outputs: | ||||
|  *      1 : Result, 0 on success, otherwise error code | ||||
|  *      2 : Boolean, database availability | ||||
|  */ | ||||
| void QueryAvailableTitleDatabase(Service::Interface* self); | ||||
| 
 | ||||
| /**
 | ||||
|  * AM::CheckContentRights service function | ||||
|  *  Inputs: | ||||
|  *      1-2 : Title ID | ||||
|  *      3 : Content Index | ||||
|  *  Outputs: | ||||
|  *      1 : Result, 0 on success, otherwise error code | ||||
|  *      2 : Boolean, whether we have rights to this content | ||||
|  */ | ||||
| void CheckContentRights(Service::Interface* self); | ||||
| 
 | ||||
| /**
 | ||||
|  * AM::CheckContentRightsIgnorePlatform service function | ||||
|  *  Inputs: | ||||
|  *      1-2 : Title ID | ||||
|  *      3 : Content Index | ||||
|  *  Outputs: | ||||
|  *      1 : Result, 0 on success, otherwise error code | ||||
|  *      2 : Boolean, whether we have rights to this content | ||||
|  */ | ||||
| void CheckContentRightsIgnorePlatform(Service::Interface* self); | ||||
| 
 | ||||
| /// Initialize AM service
 | ||||
| void Init(); | ||||
| 
 | ||||
|  |  | |||
|  | @ -33,7 +33,7 @@ const Interface::FunctionInfo FunctionTable[] = { | |||
|     {0x00160000, nullptr, "DeleteAllTemporaryPrograms"}, | ||||
|     {0x00170044, nullptr, "ImportTwlBackupLegacy"}, | ||||
|     {0x00180080, nullptr, "InitializeTitleDatabase"}, | ||||
|     {0x00190040, nullptr, "QueryAvailableTitleDatabase"}, | ||||
|     {0x00190040, QueryAvailableTitleDatabase, "QueryAvailableTitleDatabase"}, | ||||
|     {0x001A00C0, nullptr, "CalcTwlBackupSize"}, | ||||
|     {0x001B0144, nullptr, "ExportTwlBackup"}, | ||||
|     {0x001C0084, nullptr, "ImportTwlBackup"}, | ||||
|  | @ -45,7 +45,7 @@ const Interface::FunctionInfo FunctionTable[] = { | |||
|     {0x00220080, nullptr, "DeleteAllImportContextsFiltered"}, | ||||
|     {0x00230080, nullptr, "GetNumImportTitleContextsFiltered"}, | ||||
|     {0x002400C2, nullptr, "GetImportTitleContextListFiltered"}, | ||||
|     {0x002500C0, nullptr, "CheckContentRights"}, | ||||
|     {0x002500C0, CheckContentRights, "CheckContentRights"}, | ||||
|     {0x00260044, nullptr, "GetTicketLimitInfos"}, | ||||
|     {0x00270044, nullptr, "GetDemoLaunchInfos"}, | ||||
|     {0x00280108, nullptr, "ReadTwlBackupInfoEx"}, | ||||
|  | @ -53,7 +53,7 @@ const Interface::FunctionInfo FunctionTable[] = { | |||
|     {0x002A00C0, nullptr, "GetNumExistingContentInfosSystem"}, | ||||
|     {0x002B0142, nullptr, "ListExistingContentInfosSystem"}, | ||||
|     {0x002C0084, nullptr, "GetProgramInfosIgnorePlatform"}, | ||||
|     {0x002D00C0, nullptr, "CheckContentRightsIgnorePlatform"}, | ||||
|     {0x002D00C0, CheckContentRightsIgnorePlatform, "CheckContentRightsIgnorePlatform"}, | ||||
|     {0x100100C0, GetNumContentInfos, "GetNumContentInfos"}, | ||||
|     {0x10020104, FindContentInfos, "FindContentInfos"}, | ||||
|     {0x10030142, ListContentInfos, "ListContentInfos"}, | ||||
|  |  | |||
|  | @ -559,7 +559,7 @@ void RegisterArchiveTypes() { | |||
|                   sharedextsavedata_factory->GetMountPoint().c_str()); | ||||
| 
 | ||||
|     // Create the NCCH archive, basically a small variation of the RomFS archive
 | ||||
|     auto savedatacheck_factory = std::make_unique<FileSys::ArchiveFactory_NCCH>(nand_directory); | ||||
|     auto savedatacheck_factory = std::make_unique<FileSys::ArchiveFactory_NCCH>(); | ||||
|     RegisterArchiveType(std::move(savedatacheck_factory), ArchiveIdCode::NCCH); | ||||
| 
 | ||||
|     auto systemsavedata_factory = | ||||
|  |  | |||
|  | @ -17,6 +17,7 @@ | |||
| #include "core/file_sys/title_metadata.h" | ||||
| #include "core/hle/kernel/process.h" | ||||
| #include "core/hle/kernel/resource_limit.h" | ||||
| #include "core/hle/service/am/am.h" | ||||
| #include "core/hle/service/cfg/cfg.h" | ||||
| #include "core/hle/service/fs/archive.h" | ||||
| #include "core/loader/ncch.h" | ||||
|  | @ -46,25 +47,6 @@ FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) { | |||
|     return FileType::Error; | ||||
| } | ||||
| 
 | ||||
| static std::string GetUpdateNCCHPath(u64_le program_id) { | ||||
|     u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32); | ||||
|     u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF); | ||||
| 
 | ||||
|     // TODO(shinyquagsire23): Title database should be doing this path lookup
 | ||||
|     std::string content_path = Common::StringFromFormat( | ||||
|         "%sNintendo 3DS/%s/%s/title/%08x/%08x/content/", FileUtil::GetUserPath(D_SDMC_IDX).c_str(), | ||||
|         SYSTEM_ID, SDCARD_ID, high, low); | ||||
|     std::string tmd_path = content_path + "00000000.tmd"; | ||||
| 
 | ||||
|     u32 content_id = 0; | ||||
|     FileSys::TitleMetadata tmd(tmd_path); | ||||
|     if (tmd.Load() == ResultStatus::Success) { | ||||
|         content_id = tmd.GetBootContentID(); | ||||
|     } | ||||
| 
 | ||||
|     return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id); | ||||
| } | ||||
| 
 | ||||
| std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { | ||||
|     if (!is_loaded) { | ||||
|         ResultStatus res = base_ncch.Load(); | ||||
|  | @ -176,7 +158,8 @@ ResultStatus AppLoader_NCCH::Load(Kernel::SharedPtr<Kernel::Process>& process) { | |||
| 
 | ||||
|     LOG_INFO(Loader, "Program ID: %s", program_id.c_str()); | ||||
| 
 | ||||
|     update_ncch.OpenFile(GetUpdateNCCHPath(ncch_program_id)); | ||||
|     update_ncch.OpenFile(Service::AM::GetTitleContentPath(Service::FS::MediaType::SDMC, | ||||
|                                                           ncch_program_id | UPDATE_MASK)); | ||||
|     result = update_ncch.Load(); | ||||
|     if (result == ResultStatus::Success) { | ||||
|         overlay_ncch = &update_ncch; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue