summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/file_sys/archive_romfs.cpp7
-rw-r--r--src/core/file_sys/archive_romfs.h6
-rw-r--r--src/core/file_sys/archive_savedatacheck.cpp11
-rw-r--r--src/core/file_sys/disk_archive.cpp8
-rw-r--r--src/core/file_sys/disk_archive.h8
-rw-r--r--src/core/file_sys/file_backend.h8
-rw-r--r--src/core/file_sys/ivfc_archive.cpp19
-rw-r--r--src/core/file_sys/ivfc_archive.h23
-rw-r--r--src/core/hle/service/fs/archive.cpp2
-rw-r--r--src/core/loader/3dsx.cpp4
-rw-r--r--src/core/loader/3dsx.h2
-rw-r--r--src/core/loader/elf.cpp9
-rw-r--r--src/core/loader/elf.h2
-rw-r--r--src/core/loader/loader.cpp8
-rw-r--r--src/core/loader/loader.h21
-rw-r--r--src/core/loader/ncch.cpp51
-rw-r--r--src/core/loader/ncch.h19
17 files changed, 111 insertions, 97 deletions
diff --git a/src/core/file_sys/archive_romfs.cpp b/src/core/file_sys/archive_romfs.cpp
index c1e45dfeb..696b51a94 100644
--- a/src/core/file_sys/archive_romfs.cpp
+++ b/src/core/file_sys/archive_romfs.cpp
@@ -17,16 +17,15 @@
17 17
18namespace FileSys { 18namespace FileSys {
19 19
20ArchiveFactory_RomFS::ArchiveFactory_RomFS(const Loader::AppLoader& app_loader) 20ArchiveFactory_RomFS::ArchiveFactory_RomFS(Loader::AppLoader& app_loader) {
21 : romfs_data(std::make_shared<std::vector<u8>>()) {
22 // Load the RomFS from the app 21 // Load the RomFS from the app
23 if (Loader::ResultStatus::Success != app_loader.ReadRomFS(*romfs_data)) { 22 if (Loader::ResultStatus::Success != app_loader.ReadRomFS(romfs_file, data_offset, data_size)) {
24 LOG_ERROR(Service_FS, "Unable to read RomFS!"); 23 LOG_ERROR(Service_FS, "Unable to read RomFS!");
25 } 24 }
26} 25}
27 26
28ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_RomFS::Open(const Path& path) { 27ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_RomFS::Open(const Path& path) {
29 auto archive = Common::make_unique<IVFCArchive>(romfs_data); 28 auto archive = Common::make_unique<IVFCArchive>(romfs_file, data_offset, data_size);
30 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); 29 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
31} 30}
32 31
diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h
index c69ff91c3..2bedfa9c6 100644
--- a/src/core/file_sys/archive_romfs.h
+++ b/src/core/file_sys/archive_romfs.h
@@ -22,14 +22,16 @@ namespace FileSys {
22/// File system interface to the RomFS archive 22/// File system interface to the RomFS archive
23class ArchiveFactory_RomFS final : public ArchiveFactory { 23class ArchiveFactory_RomFS final : public ArchiveFactory {
24public: 24public:
25 ArchiveFactory_RomFS(const Loader::AppLoader& app_loader); 25 ArchiveFactory_RomFS(Loader::AppLoader& app_loader);
26 26
27 std::string GetName() const override { return "RomFS"; } 27 std::string GetName() const override { return "RomFS"; }
28 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override; 28 ResultVal<std::unique_ptr<ArchiveBackend>> Open(const Path& path) override;
29 ResultCode Format(const Path& path) override; 29 ResultCode Format(const Path& path) override;
30 30
31private: 31private:
32 std::shared_ptr<std::vector<u8>> romfs_data; 32 std::shared_ptr<FileUtil::IOFile> romfs_file;
33 u64 data_offset;
34 u64 data_size;
33}; 35};
34 36
35} // namespace FileSys 37} // namespace FileSys
diff --git a/src/core/file_sys/archive_savedatacheck.cpp b/src/core/file_sys/archive_savedatacheck.cpp
index dec838cae..ea1dfe2c7 100644
--- a/src/core/file_sys/archive_savedatacheck.cpp
+++ b/src/core/file_sys/archive_savedatacheck.cpp
@@ -37,17 +37,14 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveDataCheck::Open(co
37 auto vec = path.AsBinary(); 37 auto vec = path.AsBinary();
38 const u32* data = reinterpret_cast<u32*>(vec.data()); 38 const u32* data = reinterpret_cast<u32*>(vec.data());
39 std::string file_path = GetSaveDataCheckPath(mount_point, data[1], data[0]); 39 std::string file_path = GetSaveDataCheckPath(mount_point, data[1], data[0]);
40 FileUtil::IOFile file(file_path, "rb"); 40 auto file = std::make_shared<FileUtil::IOFile>(file_path, "rb");
41 41
42 if (!file.IsOpen()) { 42 if (!file->IsOpen()) {
43 return ResultCode(-1); // TODO(Subv): Find the right error code 43 return ResultCode(-1); // TODO(Subv): Find the right error code
44 } 44 }
45 auto size = file.GetSize(); 45 auto size = file->GetSize();
46 auto raw_data = std::make_shared<std::vector<u8>>(size);
47 file.ReadBytes(raw_data->data(), size);
48 file.Close();
49 46
50 auto archive = Common::make_unique<IVFCArchive>(std::move(raw_data)); 47 auto archive = Common::make_unique<IVFCArchive>(file, 0, size);
51 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); 48 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
52} 49}
53 50
diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp
index 85151a311..1096fd34d 100644
--- a/src/core/file_sys/disk_archive.cpp
+++ b/src/core/file_sys/disk_archive.cpp
@@ -105,12 +105,12 @@ bool DiskFile::Open() {
105 return true; 105 return true;
106} 106}
107 107
108size_t DiskFile::Read(const u64 offset, const u32 length, u8* buffer) const { 108size_t DiskFile::Read(const u64 offset, const size_t length, u8* buffer) const {
109 file->Seek(offset, SEEK_SET); 109 file->Seek(offset, SEEK_SET);
110 return file->ReadBytes(buffer, length); 110 return file->ReadBytes(buffer, length);
111} 111}
112 112
113size_t DiskFile::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const { 113size_t DiskFile::Write(const u64 offset, const size_t length, const bool flush, const u8* buffer) const {
114 file->Seek(offset, SEEK_SET); 114 file->Seek(offset, SEEK_SET);
115 size_t written = file->WriteBytes(buffer, length); 115 size_t written = file->WriteBytes(buffer, length);
116 if (flush) 116 if (flush)
@@ -118,8 +118,8 @@ size_t DiskFile::Write(const u64 offset, const u32 length, const u32 flush, cons
118 return written; 118 return written;
119} 119}
120 120
121size_t DiskFile::GetSize() const { 121u64 DiskFile::GetSize() const {
122 return static_cast<size_t>(file->GetSize()); 122 return file->GetSize();
123} 123}
124 124
125bool DiskFile::SetSize(const u64 size) const { 125bool DiskFile::SetSize(const u64 size) const {
diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h
index 5cfcddf6c..c5da07508 100644
--- a/src/core/file_sys/disk_archive.h
+++ b/src/core/file_sys/disk_archive.h
@@ -55,10 +55,10 @@ public:
55 DiskFile(const DiskArchive& archive, const Path& path, const Mode mode); 55 DiskFile(const DiskArchive& archive, const Path& path, const Mode mode);
56 56
57 bool Open() override; 57 bool Open() override;
58 size_t Read(const u64 offset, const u32 length, u8* buffer) const override; 58 size_t Read(u64 offset, size_t length, u8* buffer) const override;
59 size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const override; 59 size_t Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
60 size_t GetSize() const override; 60 u64 GetSize() const override;
61 bool SetSize(const u64 size) const override; 61 bool SetSize(u64 size) const override;
62 bool Close() const override; 62 bool Close() const override;
63 63
64 void Flush() const override { 64 void Flush() const override {
diff --git a/src/core/file_sys/file_backend.h b/src/core/file_sys/file_backend.h
index f5f72c722..df7165df3 100644
--- a/src/core/file_sys/file_backend.h
+++ b/src/core/file_sys/file_backend.h
@@ -31,7 +31,7 @@ public:
31 * @param buffer Buffer to read data into 31 * @param buffer Buffer to read data into
32 * @return Number of bytes read 32 * @return Number of bytes read
33 */ 33 */
34 virtual size_t Read(const u64 offset, const u32 length, u8* buffer) const = 0; 34 virtual size_t Read(u64 offset, size_t length, u8* buffer) const = 0;
35 35
36 /** 36 /**
37 * Write data to the file 37 * Write data to the file
@@ -41,20 +41,20 @@ public:
41 * @param buffer Buffer to read data from 41 * @param buffer Buffer to read data from
42 * @return Number of bytes written 42 * @return Number of bytes written
43 */ 43 */
44 virtual size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const = 0; 44 virtual size_t Write(u64 offset, size_t length, bool flush, const u8* buffer) const = 0;
45 45
46 /** 46 /**
47 * Get the size of the file in bytes 47 * Get the size of the file in bytes
48 * @return Size of the file in bytes 48 * @return Size of the file in bytes
49 */ 49 */
50 virtual size_t GetSize() const = 0; 50 virtual u64 GetSize() const = 0;
51 51
52 /** 52 /**
53 * Set the size of the file in bytes 53 * Set the size of the file in bytes
54 * @param size New size of the file 54 * @param size New size of the file
55 * @return true if successful 55 * @return true if successful
56 */ 56 */
57 virtual bool SetSize(const u64 size) const = 0; 57 virtual bool SetSize(u64 size) const = 0;
58 58
59 /** 59 /**
60 * Close the file 60 * Close the file
diff --git a/src/core/file_sys/ivfc_archive.cpp b/src/core/file_sys/ivfc_archive.cpp
index c88b39bcd..e16aa1491 100644
--- a/src/core/file_sys/ivfc_archive.cpp
+++ b/src/core/file_sys/ivfc_archive.cpp
@@ -16,15 +16,12 @@
16 16
17namespace FileSys { 17namespace FileSys {
18 18
19IVFCArchive::IVFCArchive(std::shared_ptr<const std::vector<u8>> data) : data(data) {
20}
21
22std::string IVFCArchive::GetName() const { 19std::string IVFCArchive::GetName() const {
23 return "IVFC"; 20 return "IVFC";
24} 21}
25 22
26std::unique_ptr<FileBackend> IVFCArchive::OpenFile(const Path& path, const Mode mode) const { 23std::unique_ptr<FileBackend> IVFCArchive::OpenFile(const Path& path, const Mode mode) const {
27 return Common::make_unique<IVFCFile>(data); 24 return Common::make_unique<IVFCFile>(romfs_file, data_offset, data_size);
28} 25}
29 26
30bool IVFCArchive::DeleteFile(const Path& path) const { 27bool IVFCArchive::DeleteFile(const Path& path) const {
@@ -64,19 +61,21 @@ std::unique_ptr<DirectoryBackend> IVFCArchive::OpenDirectory(const Path& path) c
64 61
65//////////////////////////////////////////////////////////////////////////////////////////////////// 62////////////////////////////////////////////////////////////////////////////////////////////////////
66 63
67size_t IVFCFile::Read(const u64 offset, const u32 length, u8* buffer) const { 64size_t IVFCFile::Read(const u64 offset, const size_t length, u8* buffer) const {
68 LOG_TRACE(Service_FS, "called offset=%llu, length=%d", offset, length); 65 LOG_TRACE(Service_FS, "called offset=%llu, length=%d", offset, length);
69 memcpy(buffer, data->data() + offset, length); 66 romfs_file->Seek(data_offset + offset, SEEK_SET);
70 return length; 67 size_t read_length = (size_t)std::min((u64)length, data_size - offset);
68
69 return romfs_file->ReadBytes(buffer, read_length);
71} 70}
72 71
73size_t IVFCFile::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const { 72size_t IVFCFile::Write(const u64 offset, const size_t length, const bool flush, const u8* buffer) const {
74 LOG_ERROR(Service_FS, "Attempted to write to IVFC file"); 73 LOG_ERROR(Service_FS, "Attempted to write to IVFC file");
75 return 0; 74 return 0;
76} 75}
77 76
78size_t IVFCFile::GetSize() const { 77u64 IVFCFile::GetSize() const {
79 return sizeof(u8) * data->size(); 78 return data_size;
80} 79}
81 80
82bool IVFCFile::SetSize(const u64 size) const { 81bool IVFCFile::SetSize(const u64 size) const {
diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h
index 1850b3b17..c15a6c4ae 100644
--- a/src/core/file_sys/ivfc_archive.h
+++ b/src/core/file_sys/ivfc_archive.h
@@ -10,6 +10,7 @@
10#include <vector> 10#include <vector>
11 11
12#include "common/common_types.h" 12#include "common/common_types.h"
13#include "common/file_util.h"
13 14
14#include "core/file_sys/archive_backend.h" 15#include "core/file_sys/archive_backend.h"
15#include "core/file_sys/directory_backend.h" 16#include "core/file_sys/directory_backend.h"
@@ -28,7 +29,8 @@ namespace FileSys {
28 */ 29 */
29class IVFCArchive : public ArchiveBackend { 30class IVFCArchive : public ArchiveBackend {
30public: 31public:
31 IVFCArchive(std::shared_ptr<const std::vector<u8>> data); 32 IVFCArchive(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
33 : romfs_file(file), data_offset(offset), data_size(size) {}
32 34
33 std::string GetName() const override; 35 std::string GetName() const override;
34 36
@@ -42,23 +44,28 @@ public:
42 std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override; 44 std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override;
43 45
44protected: 46protected:
45 std::shared_ptr<const std::vector<u8>> data; 47 std::shared_ptr<FileUtil::IOFile> romfs_file;
48 u64 data_offset;
49 u64 data_size;
46}; 50};
47 51
48class IVFCFile : public FileBackend { 52class IVFCFile : public FileBackend {
49public: 53public:
50 IVFCFile(std::shared_ptr<const std::vector<u8>> data) : data(data) {} 54 IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
55 : romfs_file(file), data_offset(offset), data_size(size) {}
51 56
52 bool Open() override { return true; } 57 bool Open() override { return true; }
53 size_t Read(const u64 offset, const u32 length, u8* buffer) const override; 58 size_t Read(u64 offset, size_t length, u8* buffer) const override;
54 size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const override; 59 size_t Write(u64 offset, size_t length, bool flush, const u8* buffer) const override;
55 size_t GetSize() const override; 60 u64 GetSize() const override;
56 bool SetSize(const u64 size) const override; 61 bool SetSize(u64 size) const override;
57 bool Close() const override { return false; } 62 bool Close() const override { return false; }
58 void Flush() const override { } 63 void Flush() const override { }
59 64
60private: 65private:
61 std::shared_ptr<const std::vector<u8>> data; 66 std::shared_ptr<FileUtil::IOFile> romfs_file;
67 u64 data_offset;
68 u64 data_size;
62}; 69};
63 70
64class IVFCDirectory : public DirectoryBackend { 71class IVFCDirectory : public DirectoryBackend {
diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp
index ba272f05f..6c0df67c3 100644
--- a/src/core/hle/service/fs/archive.cpp
+++ b/src/core/hle/service/fs/archive.cpp
@@ -116,7 +116,7 @@ ResultVal<bool> File::SyncRequest() {
116 u32 address = cmd_buff[6]; 116 u32 address = cmd_buff[6];
117 LOG_TRACE(Service_FS, "Write %s %s: offset=0x%llx length=%d address=0x%x, flush=0x%x", 117 LOG_TRACE(Service_FS, "Write %s %s: offset=0x%llx length=%d address=0x%x, flush=0x%x",
118 GetTypeName().c_str(), GetName().c_str(), offset, length, address, flush); 118 GetTypeName().c_str(), GetName().c_str(), offset, length, address, flush);
119 cmd_buff[2] = static_cast<u32>(backend->Write(offset, length, flush, Memory::GetPointer(address))); 119 cmd_buff[2] = static_cast<u32>(backend->Write(offset, length, flush != 0, Memory::GetPointer(address)));
120 break; 120 break;
121 } 121 }
122 122
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp
index 055661363..d043fa9bd 100644
--- a/src/core/loader/3dsx.cpp
+++ b/src/core/loader/3dsx.cpp
@@ -246,11 +246,11 @@ ResultStatus AppLoader_THREEDSX::Load() {
246 if (is_loaded) 246 if (is_loaded)
247 return ResultStatus::ErrorAlreadyLoaded; 247 return ResultStatus::ErrorAlreadyLoaded;
248 248
249 if (!file->IsOpen()) 249 if (!file.IsOpen())
250 return ResultStatus::Error; 250 return ResultStatus::Error;
251 251
252 SharedPtr<CodeSet> codeset; 252 SharedPtr<CodeSet> codeset;
253 if (Load3DSXFile(*file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE) 253 if (Load3DSXFile(file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE)
254 return ResultStatus::Error; 254 return ResultStatus::Error;
255 codeset->name = filename; 255 codeset->name = filename;
256 256
diff --git a/src/core/loader/3dsx.h b/src/core/loader/3dsx.h
index 096b3ec20..a0aa0c533 100644
--- a/src/core/loader/3dsx.h
+++ b/src/core/loader/3dsx.h
@@ -17,7 +17,7 @@ namespace Loader {
17/// Loads an 3DSX file 17/// Loads an 3DSX file
18class AppLoader_THREEDSX final : public AppLoader { 18class AppLoader_THREEDSX final : public AppLoader {
19public: 19public:
20 AppLoader_THREEDSX(std::unique_ptr<FileUtil::IOFile>&& file, std::string filename) 20 AppLoader_THREEDSX(FileUtil::IOFile&& file, std::string filename)
21 : AppLoader(std::move(file)), filename(std::move(filename)) {} 21 : AppLoader(std::move(file)), filename(std::move(filename)) {}
22 22
23 /** 23 /**
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
index ca3c18a9f..5d7264f12 100644
--- a/src/core/loader/elf.cpp
+++ b/src/core/loader/elf.cpp
@@ -273,7 +273,6 @@ SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
273 LOG_DEBUG(Loader, "%i segments:", header->e_phnum); 273 LOG_DEBUG(Loader, "%i segments:", header->e_phnum);
274 274
275 // First pass : Get the bits into RAM 275 // First pass : Get the bits into RAM
276 u32 segment_addr[32];
277 u32 base_addr = relocate ? vaddr : 0; 276 u32 base_addr = relocate ? vaddr : 0;
278 277
279 u32 total_image_size = 0; 278 u32 total_image_size = 0;
@@ -392,15 +391,15 @@ ResultStatus AppLoader_ELF::Load() {
392 if (is_loaded) 391 if (is_loaded)
393 return ResultStatus::ErrorAlreadyLoaded; 392 return ResultStatus::ErrorAlreadyLoaded;
394 393
395 if (!file->IsOpen()) 394 if (!file.IsOpen())
396 return ResultStatus::Error; 395 return ResultStatus::Error;
397 396
398 // Reset read pointer in case this file has been read before. 397 // Reset read pointer in case this file has been read before.
399 file->Seek(0, SEEK_SET); 398 file.Seek(0, SEEK_SET);
400 399
401 u32 size = static_cast<u32>(file->GetSize()); 400 size_t size = file.GetSize();
402 std::unique_ptr<u8[]> buffer(new u8[size]); 401 std::unique_ptr<u8[]> buffer(new u8[size]);
403 if (file->ReadBytes(&buffer[0], size) != size) 402 if (file.ReadBytes(&buffer[0], size) != size)
404 return ResultStatus::Error; 403 return ResultStatus::Error;
405 404
406 ElfReader elf_reader(&buffer[0]); 405 ElfReader elf_reader(&buffer[0]);
diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h
index 32841606a..c6a5ebe99 100644
--- a/src/core/loader/elf.h
+++ b/src/core/loader/elf.h
@@ -17,7 +17,7 @@ namespace Loader {
17/// Loads an ELF/AXF file 17/// Loads an ELF/AXF file
18class AppLoader_ELF final : public AppLoader { 18class AppLoader_ELF final : public AppLoader {
19public: 19public:
20 AppLoader_ELF(std::unique_ptr<FileUtil::IOFile>&& file, std::string filename) 20 AppLoader_ELF(FileUtil::IOFile&& file, std::string filename)
21 : AppLoader(std::move(file)), filename(std::move(filename)) { } 21 : AppLoader(std::move(file)), filename(std::move(filename)) { }
22 22
23 /** 23 /**
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index 2e450fce4..9ef2f8900 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -90,8 +90,8 @@ static const char* GetFileTypeString(FileType type) {
90} 90}
91 91
92ResultStatus LoadFile(const std::string& filename) { 92ResultStatus LoadFile(const std::string& filename) {
93 std::unique_ptr<FileUtil::IOFile> file(new FileUtil::IOFile(filename, "rb")); 93 FileUtil::IOFile file(filename, "rb");
94 if (!file->IsOpen()) { 94 if (!file.IsOpen()) {
95 LOG_ERROR(Loader, "Failed to load file %s", filename.c_str()); 95 LOG_ERROR(Loader, "Failed to load file %s", filename.c_str());
96 return ResultStatus::Error; 96 return ResultStatus::Error;
97 } 97 }
@@ -99,7 +99,7 @@ ResultStatus LoadFile(const std::string& filename) {
99 std::string filename_filename, filename_extension; 99 std::string filename_filename, filename_extension;
100 Common::SplitPath(filename, nullptr, &filename_filename, &filename_extension); 100 Common::SplitPath(filename, nullptr, &filename_filename, &filename_extension);
101 101
102 FileType type = IdentifyFile(*file); 102 FileType type = IdentifyFile(file);
103 FileType filename_type = GuessFromExtension(filename_extension); 103 FileType filename_type = GuessFromExtension(filename_extension);
104 104
105 if (type != filename_type) { 105 if (type != filename_type) {
@@ -124,7 +124,7 @@ ResultStatus LoadFile(const std::string& filename) {
124 case FileType::CXI: 124 case FileType::CXI:
125 case FileType::CCI: 125 case FileType::CCI:
126 { 126 {
127 AppLoader_NCCH app_loader(std::move(file)); 127 AppLoader_NCCH app_loader(std::move(file), filename);
128 128
129 // Load application and RomFS 129 // Load application and RomFS
130 if (ResultStatus::Success == app_loader.Load()) { 130 if (ResultStatus::Success == app_loader.Load()) {
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 52bbf35b8..a37d3348c 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -52,7 +52,7 @@ static inline u32 MakeMagic(char a, char b, char c, char d) {
52/// Interface for loading an application 52/// Interface for loading an application
53class AppLoader : NonCopyable { 53class AppLoader : NonCopyable {
54public: 54public:
55 AppLoader(std::unique_ptr<FileUtil::IOFile>&& file) : file(std::move(file)) { } 55 AppLoader(FileUtil::IOFile&& file) : file(std::move(file)) { }
56 virtual ~AppLoader() { } 56 virtual ~AppLoader() { }
57 57
58 /** 58 /**
@@ -66,7 +66,7 @@ public:
66 * @param buffer Reference to buffer to store data 66 * @param buffer Reference to buffer to store data
67 * @return ResultStatus result of function 67 * @return ResultStatus result of function
68 */ 68 */
69 virtual ResultStatus ReadCode(std::vector<u8>& buffer) const { 69 virtual ResultStatus ReadCode(std::vector<u8>& buffer) {
70 return ResultStatus::ErrorNotImplemented; 70 return ResultStatus::ErrorNotImplemented;
71 } 71 }
72 72
@@ -75,7 +75,7 @@ public:
75 * @param buffer Reference to buffer to store data 75 * @param buffer Reference to buffer to store data
76 * @return ResultStatus result of function 76 * @return ResultStatus result of function
77 */ 77 */
78 virtual ResultStatus ReadIcon(std::vector<u8>& buffer) const { 78 virtual ResultStatus ReadIcon(std::vector<u8>& buffer) {
79 return ResultStatus::ErrorNotImplemented; 79 return ResultStatus::ErrorNotImplemented;
80 } 80 }
81 81
@@ -84,7 +84,7 @@ public:
84 * @param buffer Reference to buffer to store data 84 * @param buffer Reference to buffer to store data
85 * @return ResultStatus result of function 85 * @return ResultStatus result of function
86 */ 86 */
87 virtual ResultStatus ReadBanner(std::vector<u8>& buffer) const { 87 virtual ResultStatus ReadBanner(std::vector<u8>& buffer) {
88 return ResultStatus::ErrorNotImplemented; 88 return ResultStatus::ErrorNotImplemented;
89 } 89 }
90 90
@@ -93,22 +93,25 @@ public:
93 * @param buffer Reference to buffer to store data 93 * @param buffer Reference to buffer to store data
94 * @return ResultStatus result of function 94 * @return ResultStatus result of function
95 */ 95 */
96 virtual ResultStatus ReadLogo(std::vector<u8>& buffer) const { 96 virtual ResultStatus ReadLogo(std::vector<u8>& buffer) {
97 return ResultStatus::ErrorNotImplemented; 97 return ResultStatus::ErrorNotImplemented;
98 } 98 }
99 99
100 /** 100 /**
101 * Get the RomFS of the application 101 * Get the RomFS of the application
102 * @param buffer Reference to buffer to store data 102 * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
103 * @param romfs_file The file containing the RomFS
104 * @param offset The offset the romfs begins on
105 * @param size The size of the romfs
103 * @return ResultStatus result of function 106 * @return ResultStatus result of function
104 */ 107 */
105 virtual ResultStatus ReadRomFS(std::vector<u8>& buffer) const { 108 virtual ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) {
106 return ResultStatus::ErrorNotImplemented; 109 return ResultStatus::ErrorNotImplemented;
107 } 110 }
108 111
109protected: 112protected:
110 std::unique_ptr<FileUtil::IOFile> file; 113 FileUtil::IOFile file;
111 bool is_loaded = false; 114 bool is_loaded = false;
112}; 115};
113 116
114/** 117/**
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 87603d198..094d74100 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -117,7 +117,7 @@ FileType AppLoader_NCCH::IdentifyType(FileUtil::IOFile& file) {
117 return FileType::Error; 117 return FileType::Error;
118} 118}
119 119
120ResultStatus AppLoader_NCCH::LoadExec() const { 120ResultStatus AppLoader_NCCH::LoadExec() {
121 using Kernel::SharedPtr; 121 using Kernel::SharedPtr;
122 using Kernel::CodeSet; 122 using Kernel::CodeSet;
123 123
@@ -171,8 +171,8 @@ ResultStatus AppLoader_NCCH::LoadExec() const {
171 return ResultStatus::Error; 171 return ResultStatus::Error;
172} 172}
173 173
174ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const { 174ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
175 if (!file->IsOpen()) 175 if (!file.IsOpen())
176 return ResultStatus::Error; 176 return ResultStatus::Error;
177 177
178 LOG_DEBUG(Loader, "%d sections:", kMaxSections); 178 LOG_DEBUG(Loader, "%d sections:", kMaxSections);
@@ -186,7 +186,7 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
186 section.offset, section.size, section.name); 186 section.offset, section.size, section.name);
187 187
188 s64 section_offset = (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); 188 s64 section_offset = (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
189 file->Seek(section_offset, SEEK_SET); 189 file.Seek(section_offset, SEEK_SET);
190 190
191 if (is_compressed) { 191 if (is_compressed) {
192 // Section is compressed, read compressed .code section... 192 // Section is compressed, read compressed .code section...
@@ -197,7 +197,7 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
197 return ResultStatus::ErrorMemoryAllocationFailed; 197 return ResultStatus::ErrorMemoryAllocationFailed;
198 } 198 }
199 199
200 if (file->ReadBytes(&temp_buffer[0], section.size) != section.size) 200 if (file.ReadBytes(&temp_buffer[0], section.size) != section.size)
201 return ResultStatus::Error; 201 return ResultStatus::Error;
202 202
203 // Decompress .code section... 203 // Decompress .code section...
@@ -208,7 +208,7 @@ ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>&
208 } else { 208 } else {
209 // Section is uncompressed... 209 // Section is uncompressed...
210 buffer.resize(section.size); 210 buffer.resize(section.size);
211 if (file->ReadBytes(&buffer[0], section.size) != section.size) 211 if (file.ReadBytes(&buffer[0], section.size) != section.size)
212 return ResultStatus::Error; 212 return ResultStatus::Error;
213 } 213 }
214 return ResultStatus::Success; 214 return ResultStatus::Success;
@@ -221,21 +221,21 @@ ResultStatus AppLoader_NCCH::Load() {
221 if (is_loaded) 221 if (is_loaded)
222 return ResultStatus::ErrorAlreadyLoaded; 222 return ResultStatus::ErrorAlreadyLoaded;
223 223
224 if (!file->IsOpen()) 224 if (!file.IsOpen())
225 return ResultStatus::Error; 225 return ResultStatus::Error;
226 226
227 // Reset read pointer in case this file has been read before. 227 // Reset read pointer in case this file has been read before.
228 file->Seek(0, SEEK_SET); 228 file.Seek(0, SEEK_SET);
229 229
230 if (file->ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) 230 if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
231 return ResultStatus::Error; 231 return ResultStatus::Error;
232 232
233 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... 233 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
234 if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { 234 if (MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) {
235 LOG_WARNING(Loader, "Only loading the first (bootable) NCCH within the NCSD file!"); 235 LOG_WARNING(Loader, "Only loading the first (bootable) NCCH within the NCSD file!");
236 ncch_offset = 0x4000; 236 ncch_offset = 0x4000;
237 file->Seek(ncch_offset, SEEK_SET); 237 file.Seek(ncch_offset, SEEK_SET);
238 file->ReadBytes(&ncch_header, sizeof(NCCH_Header)); 238 file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
239 } 239 }
240 240
241 // Verify we are loading the correct file type... 241 // Verify we are loading the correct file type...
@@ -244,7 +244,7 @@ ResultStatus AppLoader_NCCH::Load() {
244 244
245 // Read ExHeader... 245 // Read ExHeader...
246 246
247 if (file->ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header)) 247 if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header))
248 return ResultStatus::Error; 248 return ResultStatus::Error;
249 249
250 is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; 250 is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
@@ -274,8 +274,8 @@ ResultStatus AppLoader_NCCH::Load() {
274 LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset); 274 LOG_DEBUG(Loader, "ExeFS offset: 0x%08X", exefs_offset);
275 LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size); 275 LOG_DEBUG(Loader, "ExeFS size: 0x%08X", exefs_size);
276 276
277 file->Seek(exefs_offset + ncch_offset, SEEK_SET); 277 file.Seek(exefs_offset + ncch_offset, SEEK_SET);
278 if (file->ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) 278 if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
279 return ResultStatus::Error; 279 return ResultStatus::Error;
280 280
281 is_loaded = true; // Set state to loaded 281 is_loaded = true; // Set state to loaded
@@ -283,24 +283,24 @@ ResultStatus AppLoader_NCCH::Load() {
283 return LoadExec(); // Load the executable into memory for booting 283 return LoadExec(); // Load the executable into memory for booting
284} 284}
285 285
286ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const { 286ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) {
287 return LoadSectionExeFS(".code", buffer); 287 return LoadSectionExeFS(".code", buffer);
288} 288}
289 289
290ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const { 290ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) {
291 return LoadSectionExeFS("icon", buffer); 291 return LoadSectionExeFS("icon", buffer);
292} 292}
293 293
294ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const { 294ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) {
295 return LoadSectionExeFS("banner", buffer); 295 return LoadSectionExeFS("banner", buffer);
296} 296}
297 297
298ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const { 298ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) {
299 return LoadSectionExeFS("logo", buffer); 299 return LoadSectionExeFS("logo", buffer);
300} 300}
301 301
302ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const { 302ResultStatus AppLoader_NCCH::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) {
303 if (!file->IsOpen()) 303 if (!file.IsOpen())
304 return ResultStatus::Error; 304 return ResultStatus::Error;
305 305
306 // Check if the NCCH has a RomFS... 306 // Check if the NCCH has a RomFS...
@@ -311,12 +311,17 @@ ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const {
311 LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset); 311 LOG_DEBUG(Loader, "RomFS offset: 0x%08X", romfs_offset);
312 LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size); 312 LOG_DEBUG(Loader, "RomFS size: 0x%08X", romfs_size);
313 313
314 buffer.resize(romfs_size); 314 if (file.GetSize () < romfs_offset + romfs_size)
315 return ResultStatus::Error;
315 316
316 file->Seek(romfs_offset, SEEK_SET); 317 // We reopen the file, to allow its position to be independent from file's
317 if (file->ReadBytes(&buffer[0], romfs_size) != romfs_size) 318 romfs_file = std::make_shared<FileUtil::IOFile>(filepath, "rb");
319 if (!romfs_file->IsOpen())
318 return ResultStatus::Error; 320 return ResultStatus::Error;
319 321
322 offset = romfs_offset;
323 size = romfs_size;
324
320 return ResultStatus::Success; 325 return ResultStatus::Success;
321 } 326 }
322 LOG_DEBUG(Loader, "NCCH has no RomFS"); 327 LOG_DEBUG(Loader, "NCCH has no RomFS");
diff --git a/src/core/loader/ncch.h b/src/core/loader/ncch.h
index 29e39d2c0..b4374a476 100644
--- a/src/core/loader/ncch.h
+++ b/src/core/loader/ncch.h
@@ -163,7 +163,8 @@ namespace Loader {
163/// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI) 163/// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
164class AppLoader_NCCH final : public AppLoader { 164class AppLoader_NCCH final : public AppLoader {
165public: 165public:
166 AppLoader_NCCH(std::unique_ptr<FileUtil::IOFile>&& file) : AppLoader(std::move(file)) { } 166 AppLoader_NCCH(FileUtil::IOFile&& file, const std::string& filepath)
167 : AppLoader(std::move(file)), filepath(filepath) { }
167 168
168 /** 169 /**
169 * Returns the type of the file 170 * Returns the type of the file
@@ -183,35 +184,35 @@ public:
183 * @param buffer Reference to buffer to store data 184 * @param buffer Reference to buffer to store data
184 * @return ResultStatus result of function 185 * @return ResultStatus result of function
185 */ 186 */
186 ResultStatus ReadCode(std::vector<u8>& buffer) const override; 187 ResultStatus ReadCode(std::vector<u8>& buffer) override;
187 188
188 /** 189 /**
189 * Get the icon (typically icon section) of the application 190 * Get the icon (typically icon section) of the application
190 * @param buffer Reference to buffer to store data 191 * @param buffer Reference to buffer to store data
191 * @return ResultStatus result of function 192 * @return ResultStatus result of function
192 */ 193 */
193 ResultStatus ReadIcon(std::vector<u8>& buffer) const override; 194 ResultStatus ReadIcon(std::vector<u8>& buffer) override;
194 195
195 /** 196 /**
196 * Get the banner (typically banner section) of the application 197 * Get the banner (typically banner section) of the application
197 * @param buffer Reference to buffer to store data 198 * @param buffer Reference to buffer to store data
198 * @return ResultStatus result of function 199 * @return ResultStatus result of function
199 */ 200 */
200 ResultStatus ReadBanner(std::vector<u8>& buffer) const override; 201 ResultStatus ReadBanner(std::vector<u8>& buffer) override;
201 202
202 /** 203 /**
203 * Get the logo (typically logo section) of the application 204 * Get the logo (typically logo section) of the application
204 * @param buffer Reference to buffer to store data 205 * @param buffer Reference to buffer to store data
205 * @return ResultStatus result of function 206 * @return ResultStatus result of function
206 */ 207 */
207 ResultStatus ReadLogo(std::vector<u8>& buffer) const override; 208 ResultStatus ReadLogo(std::vector<u8>& buffer) override;
208 209
209 /** 210 /**
210 * Get the RomFS of the application 211 * Get the RomFS of the application
211 * @param buffer Reference to buffer to store data 212 * @param buffer Reference to buffer to store data
212 * @return ResultStatus result of function 213 * @return ResultStatus result of function
213 */ 214 */
214 ResultStatus ReadRomFS(std::vector<u8>& buffer) const override; 215 ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) override;
215 216
216private: 217private:
217 218
@@ -221,13 +222,13 @@ private:
221 * @param buffer Vector to read data into 222 * @param buffer Vector to read data into
222 * @return ResultStatus result of function 223 * @return ResultStatus result of function
223 */ 224 */
224 ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const; 225 ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer);
225 226
226 /** 227 /**
227 * Loads .code section into memory for booting 228 * Loads .code section into memory for booting
228 * @return ResultStatus result of function 229 * @return ResultStatus result of function
229 */ 230 */
230 ResultStatus LoadExec() const; 231 ResultStatus LoadExec();
231 232
232 bool is_compressed = false; 233 bool is_compressed = false;
233 234
@@ -244,6 +245,8 @@ private:
244 NCCH_Header ncch_header; 245 NCCH_Header ncch_header;
245 ExeFs_Header exefs_header; 246 ExeFs_Header exefs_header;
246 ExHeader_Header exheader_header; 247 ExHeader_Header exheader_header;
248
249 std::string filepath;
247}; 250};
248 251
249} // namespace Loader 252} // namespace Loader