loader.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2018 yuzu emulator team
  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/file_sys/vfs.h"
  15. #include "core/hle/kernel/object.h"
  16. namespace Kernel {
  17. struct AddressMapping;
  18. class Process;
  19. } // namespace Kernel
  20. namespace Loader {
  21. /// File types supported by CTR
  22. enum class FileType {
  23. Error,
  24. Unknown,
  25. ELF,
  26. NSO,
  27. NRO,
  28. NCA,
  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. * @return FileType of file
  35. */
  36. FileType IdentifyFile(FileSys::VirtualFile file);
  37. /**
  38. * Identifies the type of a bootable file based on the magic value in its header.
  39. * @param file_name path to file
  40. * @return FileType of file. Note: this will return FileType::Unknown if it is unable to determine
  41. * a filetype, and will never return FileType::Error.
  42. */
  43. FileType IdentifyFile(const std::string& file_name);
  44. /**
  45. * Guess the type of a bootable file from its name
  46. * @param name String name of bootable file
  47. * @return FileType of file. Note: this will return FileType::Unknown if it is unable to determine
  48. * a filetype, and will never return FileType::Error.
  49. */
  50. FileType GuessFromFilename(const std::string& name);
  51. /**
  52. * Convert a FileType into a string which can be displayed to the user.
  53. */
  54. const char* GetFileTypeString(FileType type);
  55. /// Return type for functions in Loader namespace
  56. enum class ResultStatus {
  57. Success,
  58. Error,
  59. ErrorInvalidFormat,
  60. ErrorNotImplemented,
  61. ErrorNotLoaded,
  62. ErrorNotUsed,
  63. ErrorAlreadyLoaded,
  64. ErrorMemoryAllocationFailed,
  65. ErrorEncrypted,
  66. ErrorUnsupportedArch,
  67. };
  68. /// Interface for loading an application
  69. class AppLoader : NonCopyable {
  70. public:
  71. explicit AppLoader(FileSys::VirtualFile 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 dir The directory containing the RomFS
  138. * @return ResultStatus result of function
  139. */
  140. virtual ResultStatus ReadRomFS(FileSys::VirtualFile& dir) {
  141. return ResultStatus::ErrorNotImplemented;
  142. }
  143. /**
  144. * Get the update RomFS of the application
  145. * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
  146. * @param file The file containing the RomFS
  147. * @return ResultStatus result of function
  148. */
  149. virtual ResultStatus ReadUpdateRomFS(FileSys::VirtualFile& file) {
  150. return ResultStatus::ErrorNotImplemented;
  151. }
  152. /**
  153. * Get the title of the application
  154. * @param title Reference to store the application title into
  155. * @return ResultStatus result of function
  156. */
  157. virtual ResultStatus ReadTitle(std::string& title) {
  158. return ResultStatus::ErrorNotImplemented;
  159. }
  160. protected:
  161. FileSys::VirtualFile file;
  162. bool is_loaded = false;
  163. };
  164. /**
  165. * Common address mappings found in most games, used for binary formats that don't have this
  166. * information.
  167. */
  168. extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings;
  169. /**
  170. * Identifies a bootable file and return a suitable loader
  171. * @param file The bootable file
  172. * @return the best loader for this file
  173. */
  174. std::unique_ptr<AppLoader> GetLoader(FileSys::VirtualFile file);
  175. } // namespace Loader