loader.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <initializer_list>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include <boost/optional.hpp>
  12. #include "common/common_types.h"
  13. #include "common/file_util.h"
  14. #include "core/hle/kernel/kernel.h"
  15. namespace Kernel {
  16. struct AddressMapping;
  17. class Process;
  18. } // namespace Kernel
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. // Loader namespace
  21. namespace Loader {
  22. /// File types supported by CTR
  23. enum class FileType {
  24. Error,
  25. Unknown,
  26. ELF,
  27. NSO,
  28. NRO,
  29. DeconstructedRomDirectory,
  30. };
  31. /**
  32. * Identifies the type of a bootable file based on the magic value in its header.
  33. * @param file open file
  34. * @param filepath Path of the file that we are opening.
  35. * @return FileType of file
  36. */
  37. FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath);
  38. /**
  39. * Identifies the type of a bootable file based on the magic value in its header.
  40. * @param file_name path to file
  41. * @return FileType of file. Note: this will return FileType::Unknown if it is unable to determine
  42. * a filetype, and will never return FileType::Error.
  43. */
  44. FileType IdentifyFile(const std::string& file_name);
  45. /**
  46. * Guess the type of a bootable file from its extension
  47. * @param extension String extension of bootable file
  48. * @return FileType of file. Note: this will return FileType::Unknown if it is unable to determine
  49. * a filetype, and will never return FileType::Error.
  50. */
  51. FileType GuessFromExtension(const std::string& extension);
  52. /**
  53. * Convert a FileType into a string which can be displayed to the user.
  54. */
  55. const char* GetFileTypeString(FileType type);
  56. /// Return type for functions in Loader namespace
  57. enum class ResultStatus {
  58. Success,
  59. Error,
  60. ErrorInvalidFormat,
  61. ErrorNotImplemented,
  62. ErrorNotLoaded,
  63. ErrorNotUsed,
  64. ErrorAlreadyLoaded,
  65. ErrorMemoryAllocationFailed,
  66. ErrorEncrypted,
  67. };
  68. /// Interface for loading an application
  69. class AppLoader : NonCopyable {
  70. public:
  71. AppLoader(FileUtil::IOFile&& file) : file(std::move(file)) {}
  72. virtual ~AppLoader() {}
  73. /**
  74. * Returns the type of this file
  75. * @return FileType corresponding to the loaded file
  76. */
  77. virtual FileType GetFileType() = 0;
  78. /**
  79. * Load the application and return the created Process instance
  80. * @param process The newly created process.
  81. * @return The status result of the operation.
  82. */
  83. virtual ResultStatus Load(Kernel::SharedPtr<Kernel::Process>& process) = 0;
  84. /**
  85. * Loads the system mode that this application needs.
  86. * This function defaults to 2 (96MB allocated to the application) if it can't read the
  87. * information.
  88. * @returns A pair with the optional system mode, and and the status.
  89. */
  90. virtual std::pair<boost::optional<u32>, ResultStatus> LoadKernelSystemMode() {
  91. // 96MB allocated to the application.
  92. return std::make_pair(2, ResultStatus::Success);
  93. }
  94. /**
  95. * Get the code (typically .code section) of the application
  96. * @param buffer Reference to buffer to store data
  97. * @return ResultStatus result of function
  98. */
  99. virtual ResultStatus ReadCode(std::vector<u8>& buffer) {
  100. return ResultStatus::ErrorNotImplemented;
  101. }
  102. /**
  103. * Get the icon (typically icon section) of the application
  104. * @param buffer Reference to buffer to store data
  105. * @return ResultStatus result of function
  106. */
  107. virtual ResultStatus ReadIcon(std::vector<u8>& buffer) {
  108. return ResultStatus::ErrorNotImplemented;
  109. }
  110. /**
  111. * Get the banner (typically banner section) of the application
  112. * @param buffer Reference to buffer to store data
  113. * @return ResultStatus result of function
  114. */
  115. virtual ResultStatus ReadBanner(std::vector<u8>& buffer) {
  116. return ResultStatus::ErrorNotImplemented;
  117. }
  118. /**
  119. * Get the logo (typically logo section) of the application
  120. * @param buffer Reference to buffer to store data
  121. * @return ResultStatus result of function
  122. */
  123. virtual ResultStatus ReadLogo(std::vector<u8>& buffer) {
  124. return ResultStatus::ErrorNotImplemented;
  125. }
  126. /**
  127. * Get the program id of the application
  128. * @param out_program_id Reference to store program id into
  129. * @return ResultStatus result of function
  130. */
  131. virtual ResultStatus ReadProgramId(u64& out_program_id) {
  132. return ResultStatus::ErrorNotImplemented;
  133. }
  134. /**
  135. * Get the RomFS of the application
  136. * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
  137. * @param romfs_file The file containing the RomFS
  138. * @param offset The offset the romfs begins on
  139. * @param size The size of the romfs
  140. * @return ResultStatus result of function
  141. */
  142. virtual ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
  143. u64& size) {
  144. return ResultStatus::ErrorNotImplemented;
  145. }
  146. /**
  147. * Get the update RomFS of the application
  148. * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
  149. * @param romfs_file The file containing the RomFS
  150. * @param offset The offset the romfs begins on
  151. * @param size The size of the romfs
  152. * @return ResultStatus result of function
  153. */
  154. virtual ResultStatus ReadUpdateRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset,
  155. u64& size) {
  156. return ResultStatus::ErrorNotImplemented;
  157. }
  158. /**
  159. * Get the title of the application
  160. * @param title Reference to store the application title into
  161. * @return ResultStatus result of function
  162. */
  163. virtual ResultStatus ReadTitle(std::string& title) {
  164. return ResultStatus::ErrorNotImplemented;
  165. }
  166. protected:
  167. FileUtil::IOFile file;
  168. bool is_loaded = false;
  169. };
  170. /**
  171. * Common address mappings found in most games, used for binary formats that don't have this
  172. * information.
  173. */
  174. extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings;
  175. /**
  176. * Identifies a bootable file and return a suitable loader
  177. * @param filename String filename of bootable file
  178. * @return best loader for this file
  179. */
  180. std::unique_ptr<AppLoader> GetLoader(const std::string& filename);
  181. } // namespace Loader