2014-12-07 21:47:06 +01:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-12-07 21:47:06 +01:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2015-05-06 09:06:12 +02:00
|
|
|
#include "common/logging/log.h"
|
2018-09-29 18:39:31 +02:00
|
|
|
#include "core/core.h"
|
2015-05-04 05:01:16 +02:00
|
|
|
#include "core/hle/kernel/process.h"
|
2015-05-12 22:25:15 +02:00
|
|
|
#include "core/hle/kernel/resource_limit.h"
|
2016-05-18 00:06:33 +02:00
|
|
|
#include "core/hle/service/fs/archive.h"
|
2016-09-21 08:52:38 +02:00
|
|
|
#include "core/loader/3dsx.h"
|
2015-05-13 03:38:29 +02:00
|
|
|
#include "core/memory.h"
|
2014-12-07 21:47:06 +01:00
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
2015-07-09 22:11:09 +02:00
|
|
|
/*
|
2014-12-07 21:47:06 +01:00
|
|
|
* File layout:
|
|
|
|
* - File header
|
|
|
|
* - Code, rodata and data relocation table headers
|
|
|
|
* - Code segment
|
|
|
|
* - Rodata segment
|
|
|
|
* - Loadable (non-BSS) part of the data segment
|
|
|
|
* - Code relocation table
|
|
|
|
* - Rodata relocation table
|
|
|
|
* - Data relocation table
|
|
|
|
*
|
|
|
|
* Memory layout before relocations are applied:
|
|
|
|
* [0..codeSegSize) -> code segment
|
|
|
|
* [codeSegSize..rodataSegSize) -> rodata segment
|
|
|
|
* [rodataSegSize..dataSegSize) -> data segment
|
|
|
|
*
|
|
|
|
* Memory layout after relocations are applied: well, however the loader sets it up :)
|
|
|
|
* The entrypoint is always the start of the code segment.
|
|
|
|
* The BSS section must be cleared manually by the application.
|
|
|
|
*/
|
2015-07-09 22:11:09 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
enum THREEDSX_Error { ERROR_NONE = 0, ERROR_READ = 1, ERROR_FILE = 2, ERROR_ALLOC = 3 };
|
2015-07-09 22:11:09 +02:00
|
|
|
|
2014-12-07 21:47:06 +01:00
|
|
|
static const u32 RELOCBUFSIZE = 512;
|
2015-07-09 22:11:09 +02:00
|
|
|
static const unsigned int NUM_SEGMENTS = 3;
|
2014-12-07 21:47:06 +01:00
|
|
|
|
|
|
|
// File header
|
|
|
|
#pragma pack(1)
|
2016-09-18 02:38:01 +02:00
|
|
|
struct THREEDSX_Header {
|
2014-12-07 21:47:06 +01:00
|
|
|
u32 magic;
|
|
|
|
u16 header_size, reloc_hdr_size;
|
|
|
|
u32 format_ver;
|
|
|
|
u32 flags;
|
|
|
|
|
|
|
|
// Sizes of the code, rodata and data segments +
|
|
|
|
// size of the BSS section (uninitialized latter half of the data segment)
|
|
|
|
u32 code_seg_size, rodata_seg_size, data_seg_size, bss_size;
|
2015-09-21 07:30:06 +02:00
|
|
|
// offset and size of smdh
|
|
|
|
u32 smdh_offset, smdh_size;
|
|
|
|
// offset to filesystem
|
|
|
|
u32 fs_offset;
|
2014-12-07 21:47:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Relocation header: all fields (even extra unknown fields) are guaranteed to be relocation counts.
|
2016-09-18 02:38:01 +02:00
|
|
|
struct THREEDSX_RelocHdr {
|
2014-12-07 21:47:06 +01:00
|
|
|
// # of absolute relocations (that is, fix address to post-relocation memory layout)
|
2015-01-05 21:09:35 +01:00
|
|
|
u32 cross_segment_absolute;
|
2016-09-18 02:38:01 +02:00
|
|
|
// # of cross-segment relative relocations (that is, 32bit signed offsets that need to be
|
|
|
|
// patched)
|
2015-01-05 21:09:35 +01:00
|
|
|
u32 cross_segment_relative;
|
2014-12-07 21:47:06 +01:00
|
|
|
// more?
|
|
|
|
|
|
|
|
// Relocations are written in this order:
|
|
|
|
// - Absolute relocations
|
|
|
|
// - Relative relocations
|
|
|
|
};
|
|
|
|
|
|
|
|
// Relocation entry: from the current pointer, skip X words and patch Y words
|
2016-09-18 02:38:01 +02:00
|
|
|
struct THREEDSX_Reloc {
|
2014-12-07 21:47:06 +01:00
|
|
|
u16 skip, patch;
|
|
|
|
};
|
|
|
|
#pragma pack()
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
struct THREEloadinfo {
|
2014-12-07 21:47:06 +01:00
|
|
|
u8* seg_ptrs[3]; // code, rodata & data
|
|
|
|
u32 seg_addrs[3];
|
|
|
|
u32 seg_sizes[3];
|
|
|
|
};
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
static u32 TranslateAddr(u32 addr, const THREEloadinfo* loadinfo, u32* offsets) {
|
2014-12-07 21:47:06 +01:00
|
|
|
if (addr < offsets[0])
|
|
|
|
return loadinfo->seg_addrs[0] + addr;
|
|
|
|
if (addr < offsets[1])
|
|
|
|
return loadinfo->seg_addrs[1] + addr - offsets[0];
|
|
|
|
return loadinfo->seg_addrs[2] + addr - offsets[1];
|
|
|
|
}
|
|
|
|
|
2015-07-10 03:52:15 +02:00
|
|
|
using Kernel::CodeSet;
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr,
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<CodeSet>* out_codeset) {
|
2015-01-06 20:56:26 +01:00
|
|
|
if (!file.IsOpen())
|
2014-12-07 21:47:06 +01:00
|
|
|
return ERROR_FILE;
|
2015-01-06 20:56:26 +01:00
|
|
|
|
2015-01-06 23:47:43 +01:00
|
|
|
// Reset read pointer in case this file has been read before.
|
|
|
|
file.Seek(0, SEEK_SET);
|
|
|
|
|
2014-12-07 21:47:06 +01:00
|
|
|
THREEDSX_Header hdr;
|
|
|
|
if (file.ReadBytes(&hdr, sizeof(hdr)) != sizeof(hdr))
|
|
|
|
return ERROR_READ;
|
|
|
|
|
|
|
|
THREEloadinfo loadinfo;
|
2016-09-18 02:38:01 +02:00
|
|
|
// loadinfo segments must be a multiple of 0x1000
|
|
|
|
loadinfo.seg_sizes[0] = (hdr.code_seg_size + 0xFFF) & ~0xFFF;
|
|
|
|
loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) & ~0xFFF;
|
|
|
|
loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) & ~0xFFF;
|
|
|
|
u32 offsets[2] = {loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1]};
|
2015-07-09 22:11:09 +02:00
|
|
|
u32 n_reloc_tables = hdr.reloc_hdr_size / sizeof(u32);
|
2016-09-18 02:38:01 +02:00
|
|
|
std::vector<u8> program_image(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] +
|
|
|
|
loadinfo.seg_sizes[2]);
|
2014-12-07 21:47:06 +01:00
|
|
|
|
|
|
|
loadinfo.seg_addrs[0] = base_addr;
|
|
|
|
loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0];
|
|
|
|
loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1];
|
2015-07-09 22:11:09 +02:00
|
|
|
loadinfo.seg_ptrs[0] = program_image.data();
|
2014-12-07 21:47:06 +01:00
|
|
|
loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0];
|
|
|
|
loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1];
|
|
|
|
|
|
|
|
// Skip header for future compatibility
|
|
|
|
file.Seek(hdr.header_size, SEEK_SET);
|
|
|
|
|
|
|
|
// Read the relocation headers
|
2015-07-09 22:11:09 +02:00
|
|
|
std::vector<u32> relocs(n_reloc_tables * NUM_SEGMENTS);
|
|
|
|
for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t size = n_reloc_tables * sizeof(u32);
|
2015-01-07 02:21:50 +01:00
|
|
|
if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size)
|
2014-12-07 21:47:06 +01:00
|
|
|
return ERROR_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the segments
|
|
|
|
if (file.ReadBytes(loadinfo.seg_ptrs[0], hdr.code_seg_size) != hdr.code_seg_size)
|
|
|
|
return ERROR_READ;
|
|
|
|
if (file.ReadBytes(loadinfo.seg_ptrs[1], hdr.rodata_seg_size) != hdr.rodata_seg_size)
|
|
|
|
return ERROR_READ;
|
2016-09-18 02:38:01 +02:00
|
|
|
if (file.ReadBytes(loadinfo.seg_ptrs[2], hdr.data_seg_size - hdr.bss_size) !=
|
|
|
|
hdr.data_seg_size - hdr.bss_size)
|
2014-12-07 21:47:06 +01:00
|
|
|
return ERROR_READ;
|
|
|
|
|
|
|
|
// BSS clear
|
|
|
|
memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
|
|
|
|
|
|
|
|
// Relocate the segments
|
2015-07-09 22:11:09 +02:00
|
|
|
for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
|
2016-09-18 02:38:01 +02:00
|
|
|
for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables;
|
|
|
|
current_segment_reloc_table++) {
|
2015-01-07 02:21:50 +01:00
|
|
|
u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table];
|
2014-12-07 21:47:06 +01:00
|
|
|
if (current_segment_reloc_table >= 2) {
|
|
|
|
// We are not using this table - ignore it because we don't know what it dose
|
2016-09-18 02:38:01 +02:00
|
|
|
file.Seek(n_relocs * sizeof(THREEDSX_Reloc), SEEK_CUR);
|
2014-12-07 21:47:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-09 22:11:09 +02:00
|
|
|
THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
|
2014-12-07 21:47:06 +01:00
|
|
|
|
|
|
|
u32* pos = (u32*)loadinfo.seg_ptrs[current_segment];
|
2015-01-07 02:21:50 +01:00
|
|
|
const u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4);
|
2014-12-07 21:47:06 +01:00
|
|
|
|
|
|
|
while (n_relocs) {
|
|
|
|
u32 remaining = std::min(RELOCBUFSIZE, n_relocs);
|
|
|
|
n_relocs -= remaining;
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
if (file.ReadBytes(reloc_table, remaining * sizeof(THREEDSX_Reloc)) !=
|
|
|
|
remaining * sizeof(THREEDSX_Reloc))
|
2014-12-07 21:47:06 +01:00
|
|
|
return ERROR_READ;
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
for (unsigned current_inprogress = 0;
|
|
|
|
current_inprogress < remaining && pos < end_pos; current_inprogress++) {
|
2015-01-07 02:21:50 +01:00
|
|
|
const auto& table = reloc_table[current_inprogress];
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Loader, "(t={},skip={},patch={})", current_segment_reloc_table,
|
2018-06-29 15:56:12 +02:00
|
|
|
static_cast<u32>(table.skip), static_cast<u32>(table.patch));
|
2015-01-07 02:21:50 +01:00
|
|
|
pos += table.skip;
|
|
|
|
s32 num_patches = table.patch;
|
2014-12-07 21:47:06 +01:00
|
|
|
while (0 < num_patches && pos < end_pos) {
|
2017-01-18 13:30:52 +01:00
|
|
|
u32 in_addr = base_addr + static_cast<u32>(reinterpret_cast<u8*>(pos) -
|
|
|
|
program_image.data());
|
|
|
|
u32 orig_data = *pos;
|
|
|
|
u32 sub_type = orig_data >> (32 - 4);
|
|
|
|
u32 addr = TranslateAddr(orig_data & ~0xF0000000, &loadinfo, offsets);
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(Loader, "Patching {:08X} <-- rel({:08X},{}) ({:08X})", in_addr,
|
2018-06-29 15:56:12 +02:00
|
|
|
addr, current_segment_reloc_table, *pos);
|
2014-12-07 21:47:06 +01:00
|
|
|
switch (current_segment_reloc_table) {
|
2017-01-18 13:30:52 +01:00
|
|
|
case 0: {
|
|
|
|
if (sub_type != 0)
|
|
|
|
return ERROR_READ;
|
|
|
|
*pos = addr;
|
2015-01-07 02:21:50 +01:00
|
|
|
break;
|
2017-01-18 13:30:52 +01:00
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
u32 data = addr - in_addr;
|
|
|
|
switch (sub_type) {
|
|
|
|
case 0: // 32-bit signed offset
|
|
|
|
*pos = data;
|
|
|
|
break;
|
|
|
|
case 1: // 31-bit signed offset
|
|
|
|
*pos = data & ~(1U << 31);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ERROR_READ;
|
|
|
|
}
|
2015-01-07 02:21:50 +01:00
|
|
|
break;
|
2017-01-18 13:30:52 +01:00
|
|
|
}
|
2015-01-07 02:21:50 +01:00
|
|
|
default:
|
2016-09-18 02:38:01 +02:00
|
|
|
break; // this should never happen
|
2014-12-07 21:47:06 +01:00
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
num_patches--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 03:52:15 +02:00
|
|
|
// Create the CodeSet
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<CodeSet> code_set = Core::System::GetInstance().Kernel().CreateCodeSet("", 0);
|
2015-07-10 03:52:15 +02:00
|
|
|
|
2018-08-23 18:23:21 +02:00
|
|
|
code_set->CodeSegment().offset = loadinfo.seg_ptrs[0] - program_image.data();
|
|
|
|
code_set->CodeSegment().addr = loadinfo.seg_addrs[0];
|
|
|
|
code_set->CodeSegment().size = loadinfo.seg_sizes[0];
|
2015-07-10 03:52:15 +02:00
|
|
|
|
2018-08-23 18:23:21 +02:00
|
|
|
code_set->RODataSegment().offset = loadinfo.seg_ptrs[1] - program_image.data();
|
|
|
|
code_set->RODataSegment().addr = loadinfo.seg_addrs[1];
|
|
|
|
code_set->RODataSegment().size = loadinfo.seg_sizes[1];
|
2015-07-10 03:52:15 +02:00
|
|
|
|
2018-08-23 18:23:21 +02:00
|
|
|
code_set->DataSegment().offset = loadinfo.seg_ptrs[2] - program_image.data();
|
|
|
|
code_set->DataSegment().addr = loadinfo.seg_addrs[2];
|
|
|
|
code_set->DataSegment().size = loadinfo.seg_sizes[2];
|
2015-07-10 03:52:15 +02:00
|
|
|
|
2018-08-23 18:23:21 +02:00
|
|
|
code_set->entrypoint = code_set->CodeSegment().addr;
|
Port various minor changes from yuzu PRs (#4725)
* common/thread: Remove unused functions
Many of these functions are carried over from Dolphin (where they aren't
used anymore). Given these have no use (and we really shouldn't be
screwing around with OS-specific thread scheduler handling from the
emulator, these can be removed.
The function for setting the thread name is left, however, since it can
have debugging utility usages.
* input_common/sdl: Use a type alias to shorten declaration of GetPollers
Just makes the definitions a little bit more tidy.
* input_common/sdl: Correct return values within implementations of GetPollers()
In both cases, we weren't actually returning anything, which is
undefined behavior.
* yuzu/debugger/graphics_surface: Fill in missing surface format listings
Fills in the missing surface types that were marked as unknown. The
order corresponds with the TextureFormat enum within
video_core/texture.h.
We also don't need to all of these strings as translatable (only the
first string, as it's an English word).
* yuzu/debugger/graphics_surface: Clean up connection overload deduction
We can utilize qOverload with the signal connections to make the
function deducing a little less ugly.
* yuzu/debugger/graphics_surface: Tidy up SaveSurface
- Use QStringLiteral where applicable.
- Use const where applicable
- Remove unnecessary precondition check (we already assert the pixbuf
being non null)
* yuzu/debugger/graphics_surface: Display error messages for file I/O errors
* core: Add missing override specifiers where applicable
Applies the override specifier where applicable. In the case of
destructors that are defaulted in their definition, they can
simply be removed.
This also removes the unnecessary inclusions being done in audin_u and
audrec_u, given their close proximity.
* kernel/thread: Make parameter of GetWaitObjectIndex() const qualified
The pointed to member is never actually modified, so it can be made
const.
* kernel/thread: Avoid sign conversion within GetCommandBufferAddress()
Previously this was performing a u64 + int sign conversion. When dealing
with addresses, we should generally be keeping the arithmetic in the
same signedness type.
This also gets rid of the static lifetime of the constant, as there's no
need to make a trivial type like this potentially live for the entire
duration of the program.
* kernel/codeset: Make CodeSet's memory data member a regular std::vector
The use of a shared_ptr is an implementation detail of the VMManager
itself when mapping memory. Because of that, we shouldn't require all
users of the CodeSet to have to allocate the shared_ptr ahead of time.
It's intended that CodeSet simply pass in the required direct data, and
that the memory manager takes care of it from that point on.
This means we just do the shared pointer allocation in a single place,
when loading modules, as opposed to in each loader.
* kernel/wait_object: Make ShouldWait() take thread members by pointer-to-const
Given this is intended as a querying function, it doesn't make sense to
allow the implementer to modify the state of the given thread.
2019-05-01 14:28:49 +02:00
|
|
|
code_set->memory = std::move(program_image);
|
2014-12-07 21:47:06 +01:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Loader, "code size: {:#X}", loadinfo.seg_sizes[0]);
|
|
|
|
LOG_DEBUG(Loader, "rodata size: {:#X}", loadinfo.seg_sizes[1]);
|
|
|
|
LOG_DEBUG(Loader, "data size: {:#X} (including {:#X} of bss)", loadinfo.seg_sizes[2],
|
2018-06-29 15:56:12 +02:00
|
|
|
hdr.bss_size);
|
2014-12-07 21:47:06 +01:00
|
|
|
|
2015-07-10 03:52:15 +02:00
|
|
|
*out_codeset = code_set;
|
2014-12-07 21:47:06 +01:00
|
|
|
return ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
2015-01-07 00:10:13 +01:00
|
|
|
FileType AppLoader_THREEDSX::IdentifyType(FileUtil::IOFile& file) {
|
|
|
|
u32 magic;
|
|
|
|
file.Seek(0, SEEK_SET);
|
|
|
|
if (1 != file.ReadArray<u32>(&magic, 1))
|
|
|
|
return FileType::Error;
|
|
|
|
|
|
|
|
if (MakeMagic('3', 'D', 'S', 'X') == magic)
|
|
|
|
return FileType::THREEDSX;
|
|
|
|
|
|
|
|
return FileType::Error;
|
|
|
|
}
|
|
|
|
|
2019-03-23 21:04:19 +01:00
|
|
|
ResultStatus AppLoader_THREEDSX::Load(std::shared_ptr<Kernel::Process>& process) {
|
2015-01-06 20:49:25 +01:00
|
|
|
if (is_loaded)
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
|
2015-07-12 00:16:33 +02:00
|
|
|
if (!file.IsOpen())
|
2015-01-05 21:09:35 +01:00
|
|
|
return ResultStatus::Error;
|
2015-01-06 22:30:40 +01:00
|
|
|
|
2019-03-23 21:04:19 +01:00
|
|
|
std::shared_ptr<CodeSet> codeset;
|
2015-07-12 00:16:33 +02:00
|
|
|
if (Load3DSXFile(file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE)
|
2015-07-10 03:52:15 +02:00
|
|
|
return ResultStatus::Error;
|
|
|
|
codeset->name = filename;
|
|
|
|
|
2018-10-12 21:33:36 +02:00
|
|
|
process = Core::System::GetInstance().Kernel().CreateProcess(std::move(codeset));
|
2017-09-27 01:17:47 +02:00
|
|
|
process->svc_access_mask.set();
|
|
|
|
process->address_mappings = default_address_mappings;
|
2015-05-25 20:34:09 +02:00
|
|
|
|
2015-05-12 22:25:15 +02:00
|
|
|
// Attach the default resource limit (APPLICATION) to the process
|
2018-10-13 22:41:34 +02:00
|
|
|
process->resource_limit = Core::System::GetInstance().Kernel().ResourceLimit().GetForCategory(
|
|
|
|
Kernel::ResourceLimitCategory::APPLICATION);
|
2015-05-04 05:01:16 +02:00
|
|
|
|
2017-09-27 01:17:47 +02:00
|
|
|
process->Run(48, Kernel::DEFAULT_STACK_SIZE);
|
2015-01-06 20:49:25 +01:00
|
|
|
|
2018-09-29 18:39:31 +02:00
|
|
|
Core::System::GetInstance().ArchiveManager().RegisterSelfNCCH(*this);
|
2016-05-18 00:06:33 +02:00
|
|
|
|
2015-01-06 20:49:25 +01:00
|
|
|
is_loaded = true;
|
2015-01-05 21:09:35 +01:00
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
2014-12-07 21:47:06 +01:00
|
|
|
|
2018-07-28 17:30:54 +02:00
|
|
|
ResultStatus AppLoader_THREEDSX::ReadRomFS(std::shared_ptr<FileSys::RomFSReader>& romfs_file) {
|
2015-09-21 07:30:06 +02:00
|
|
|
if (!file.IsOpen())
|
|
|
|
return ResultStatus::Error;
|
|
|
|
|
|
|
|
// Reset read pointer in case this file has been read before.
|
|
|
|
file.Seek(0, SEEK_SET);
|
|
|
|
|
|
|
|
THREEDSX_Header hdr;
|
|
|
|
if (file.ReadBytes(&hdr, sizeof(THREEDSX_Header)) != sizeof(THREEDSX_Header))
|
|
|
|
return ResultStatus::Error;
|
|
|
|
|
|
|
|
if (hdr.header_size != sizeof(THREEDSX_Header))
|
|
|
|
return ResultStatus::Error;
|
|
|
|
|
|
|
|
// Check if the 3DSX has a RomFS...
|
|
|
|
if (hdr.fs_offset != 0) {
|
|
|
|
u32 romfs_offset = hdr.fs_offset;
|
2016-04-25 22:10:03 +02:00
|
|
|
u32 romfs_size = static_cast<u32>(file.GetSize()) - hdr.fs_offset;
|
2015-09-21 07:30:06 +02:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Loader, "RomFS offset: {:#010X}", romfs_offset);
|
|
|
|
LOG_DEBUG(Loader, "RomFS size: {:#010X}", romfs_size);
|
2015-09-21 07:30:06 +02:00
|
|
|
|
|
|
|
// We reopen the file, to allow its position to be independent from file's
|
2018-07-28 17:30:54 +02:00
|
|
|
FileUtil::IOFile romfs_file_inner(filepath, "rb");
|
|
|
|
if (!romfs_file_inner.IsOpen())
|
2015-09-21 07:30:06 +02:00
|
|
|
return ResultStatus::Error;
|
|
|
|
|
2018-07-28 17:30:54 +02:00
|
|
|
romfs_file = std::make_shared<FileSys::RomFSReader>(std::move(romfs_file_inner),
|
|
|
|
romfs_offset, romfs_size);
|
2015-09-21 07:30:06 +02:00
|
|
|
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_DEBUG(Loader, "3DSX has no RomFS");
|
2015-09-21 07:30:06 +02:00
|
|
|
return ResultStatus::ErrorNotUsed;
|
|
|
|
}
|
|
|
|
|
2016-04-13 23:04:05 +02:00
|
|
|
ResultStatus AppLoader_THREEDSX::ReadIcon(std::vector<u8>& buffer) {
|
|
|
|
if (!file.IsOpen())
|
|
|
|
return ResultStatus::Error;
|
|
|
|
|
|
|
|
// Reset read pointer in case this file has been read before.
|
|
|
|
file.Seek(0, SEEK_SET);
|
|
|
|
|
|
|
|
THREEDSX_Header hdr;
|
|
|
|
if (file.ReadBytes(&hdr, sizeof(THREEDSX_Header)) != sizeof(THREEDSX_Header))
|
|
|
|
return ResultStatus::Error;
|
|
|
|
|
|
|
|
if (hdr.header_size != sizeof(THREEDSX_Header))
|
|
|
|
return ResultStatus::Error;
|
|
|
|
|
|
|
|
// Check if the 3DSX has a SMDH...
|
|
|
|
if (hdr.smdh_offset != 0) {
|
|
|
|
file.Seek(hdr.smdh_offset, SEEK_SET);
|
|
|
|
buffer.resize(hdr.smdh_size);
|
|
|
|
|
|
|
|
if (file.ReadBytes(&buffer[0], hdr.smdh_size) != hdr.smdh_size)
|
|
|
|
return ResultStatus::Error;
|
|
|
|
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
return ResultStatus::ErrorNotUsed;
|
|
|
|
}
|
|
|
|
|
2014-12-07 21:47:06 +01:00
|
|
|
} // namespace Loader
|