loader.h 6.3 KB

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