diff options
Diffstat (limited to 'src/core/loader/ncch.cpp')
| -rw-r--r-- | src/core/loader/ncch.cpp | 51 |
1 files changed, 28 insertions, 23 deletions
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 | ||
| 120 | ResultStatus AppLoader_NCCH::LoadExec() const { | 120 | ResultStatus 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 | ||
| 174 | ResultStatus AppLoader_NCCH::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) const { | 174 | ResultStatus 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 | ||
| 286 | ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) const { | 286 | ResultStatus AppLoader_NCCH::ReadCode(std::vector<u8>& buffer) { |
| 287 | return LoadSectionExeFS(".code", buffer); | 287 | return LoadSectionExeFS(".code", buffer); |
| 288 | } | 288 | } |
| 289 | 289 | ||
| 290 | ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) const { | 290 | ResultStatus AppLoader_NCCH::ReadIcon(std::vector<u8>& buffer) { |
| 291 | return LoadSectionExeFS("icon", buffer); | 291 | return LoadSectionExeFS("icon", buffer); |
| 292 | } | 292 | } |
| 293 | 293 | ||
| 294 | ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) const { | 294 | ResultStatus AppLoader_NCCH::ReadBanner(std::vector<u8>& buffer) { |
| 295 | return LoadSectionExeFS("banner", buffer); | 295 | return LoadSectionExeFS("banner", buffer); |
| 296 | } | 296 | } |
| 297 | 297 | ||
| 298 | ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) const { | 298 | ResultStatus AppLoader_NCCH::ReadLogo(std::vector<u8>& buffer) { |
| 299 | return LoadSectionExeFS("logo", buffer); | 299 | return LoadSectionExeFS("logo", buffer); |
| 300 | } | 300 | } |
| 301 | 301 | ||
| 302 | ResultStatus AppLoader_NCCH::ReadRomFS(std::vector<u8>& buffer) const { | 302 | ResultStatus 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"); |