2018-01-13 16:22:39 -05:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2017-09-24 11:08:31 -04:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-03-22 13:04:41 -04:00
|
|
|
#include <array>
|
2018-10-14 21:41:58 -04:00
|
|
|
#include <optional>
|
2019-03-22 13:04:41 -04:00
|
|
|
#include <type_traits>
|
2017-09-24 11:08:31 -04:00
|
|
|
#include "common/common_types.h"
|
2019-03-19 20:45:55 -04:00
|
|
|
#include "common/swap.h"
|
2018-09-29 22:16:09 -04:00
|
|
|
#include "core/file_sys/patch_manager.h"
|
2017-09-24 11:08:31 -04:00
|
|
|
#include "core/loader/loader.h"
|
|
|
|
|
2020-09-16 08:19:25 -04:00
|
|
|
namespace Core {
|
|
|
|
class System;
|
|
|
|
}
|
|
|
|
|
2018-12-02 22:24:43 -05:00
|
|
|
namespace Kernel {
|
2021-04-24 01:04:28 -04:00
|
|
|
class KProcess;
|
2018-12-02 22:24:43 -05:00
|
|
|
}
|
|
|
|
|
2017-09-24 11:08:31 -04:00
|
|
|
namespace Loader {
|
|
|
|
|
2019-03-22 13:04:41 -04:00
|
|
|
struct NSOSegmentHeader {
|
|
|
|
u32_le offset;
|
|
|
|
u32_le location;
|
|
|
|
u32_le size;
|
|
|
|
union {
|
|
|
|
u32_le alignment;
|
|
|
|
u32_le bss_size;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NSOSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect size.");
|
|
|
|
|
|
|
|
struct NSOHeader {
|
|
|
|
using SHA256Hash = std::array<u8, 0x20>;
|
|
|
|
|
|
|
|
struct RODataRelativeExtent {
|
|
|
|
u32_le data_offset;
|
|
|
|
u32_le size;
|
|
|
|
};
|
|
|
|
|
|
|
|
u32_le magic;
|
|
|
|
u32_le version;
|
|
|
|
u32 reserved;
|
|
|
|
u32_le flags;
|
|
|
|
std::array<NSOSegmentHeader, 3> segments; // Text, RoData, Data (in that order)
|
|
|
|
std::array<u8, 0x20> build_id;
|
|
|
|
std::array<u32_le, 3> segments_compressed_size;
|
|
|
|
std::array<u8, 0x1C> padding;
|
|
|
|
RODataRelativeExtent api_info_extent;
|
|
|
|
RODataRelativeExtent dynstr_extent;
|
|
|
|
RODataRelativeExtent dynsyn_extent;
|
|
|
|
std::array<SHA256Hash, 3> segment_hashes;
|
|
|
|
|
|
|
|
bool IsSegmentCompressed(size_t segment_num) const;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NSOHeader) == 0x100, "NSOHeader has incorrect size.");
|
|
|
|
static_assert(std::is_trivially_copyable_v<NSOHeader>, "NSOHeader must be trivially copyable.");
|
|
|
|
|
2020-10-13 08:10:50 -04:00
|
|
|
constexpr u32 NSO_ARGUMENT_DATA_ALLOCATION_SIZE = 0x9000;
|
2018-10-05 13:52:07 -04:00
|
|
|
|
2018-09-30 14:04:48 -04:00
|
|
|
struct NSOArgumentHeader {
|
|
|
|
u32_le allocated_size;
|
|
|
|
u32_le actual_size;
|
|
|
|
INSERT_PADDING_BYTES(0x18);
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NSOArgumentHeader) == 0x20, "NSOArgumentHeader has incorrect size.");
|
|
|
|
|
2017-09-24 11:08:31 -04:00
|
|
|
/// Loads an NSO file
|
2019-03-19 20:45:55 -04:00
|
|
|
class AppLoader_NSO final : public AppLoader {
|
2017-09-24 11:08:31 -04:00
|
|
|
public:
|
2021-04-27 12:05:34 -04:00
|
|
|
explicit AppLoader_NSO(FileSys::VirtualFile file_);
|
2017-09-24 11:08:31 -04:00
|
|
|
|
|
|
|
/**
|
2021-04-27 12:05:34 -04:00
|
|
|
* Identifies whether or not the given file is a form of NSO file.
|
|
|
|
*
|
|
|
|
* @param in_file The file to be identified.
|
|
|
|
*
|
|
|
|
* @return FileType::NSO if found, or FileType::Error if some other type of file.
|
2017-09-24 11:08:31 -04:00
|
|
|
*/
|
2021-04-27 12:05:34 -04:00
|
|
|
static FileType IdentifyType(const FileSys::VirtualFile& in_file);
|
2017-09-24 11:08:31 -04:00
|
|
|
|
2018-12-05 17:42:41 -05:00
|
|
|
FileType GetFileType() const override {
|
2018-07-18 21:07:11 -04:00
|
|
|
return IdentifyType(file);
|
2017-09-24 11:08:31 -04:00
|
|
|
}
|
|
|
|
|
2021-04-24 01:04:28 -04:00
|
|
|
static std::optional<VAddr> LoadModule(Kernel::KProcess& process, Core::System& system,
|
2021-04-27 12:05:34 -04:00
|
|
|
const FileSys::VfsFile& nso_file, VAddr load_base,
|
2020-09-16 08:19:25 -04:00
|
|
|
bool should_pass_arguments, bool load_into_process,
|
2018-10-14 21:41:58 -04:00
|
|
|
std::optional<FileSys::PatchManager> pm = {});
|
2018-01-20 14:20:04 -05:00
|
|
|
|
2021-04-24 01:04:28 -04:00
|
|
|
LoadResult Load(Kernel::KProcess& process, Core::System& system) override;
|
2019-05-26 11:40:41 -04:00
|
|
|
|
2021-04-27 12:05:34 -04:00
|
|
|
ResultStatus ReadNSOModules(Modules& out_modules) override;
|
2019-05-26 11:40:41 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
Modules modules;
|
2017-09-24 11:08:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Loader
|