loader.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <iosfwd>
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "common/common_types.h"
  12. #include "core/file_sys/control_metadata.h"
  13. #include "core/file_sys/vfs.h"
  14. namespace Kernel {
  15. struct AddressMapping;
  16. class Process;
  17. } // namespace Kernel
  18. namespace Loader {
  19. /// File types supported by CTR
  20. enum class FileType {
  21. Error,
  22. Unknown,
  23. ELF,
  24. NSO,
  25. NRO,
  26. NCA,
  27. NSP,
  28. XCI,
  29. NAX,
  30. DeconstructedRomDirectory,
  31. };
  32. /**
  33. * Identifies the type of a bootable file based on the magic value in its header.
  34. * @param file open file
  35. * @return FileType of file
  36. */
  37. FileType IdentifyFile(FileSys::VirtualFile file);
  38. /**
  39. * Guess the type of a bootable file from its name
  40. * @param name String name of bootable 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 GuessFromFilename(const std::string& name);
  45. /**
  46. * Convert a FileType into a string which can be displayed to the user.
  47. */
  48. std::string GetFileTypeString(FileType type);
  49. /// Return type for functions in Loader namespace
  50. enum class ResultStatus : u16 {
  51. Success,
  52. ErrorAlreadyLoaded,
  53. ErrorNotImplemented,
  54. ErrorNotInitialized,
  55. ErrorBadNPDMHeader,
  56. ErrorBadACIDHeader,
  57. ErrorBadACIHeader,
  58. ErrorBadFileAccessControl,
  59. ErrorBadFileAccessHeader,
  60. ErrorBadPFSHeader,
  61. ErrorIncorrectPFSFileSize,
  62. ErrorBadNCAHeader,
  63. ErrorMissingProductionKeyFile,
  64. ErrorMissingHeaderKey,
  65. ErrorIncorrectHeaderKey,
  66. ErrorNCA2,
  67. ErrorNCA0,
  68. ErrorMissingTitlekey,
  69. ErrorMissingTitlekek,
  70. ErrorInvalidRightsID,
  71. ErrorMissingKeyAreaKey,
  72. ErrorIncorrectKeyAreaKey,
  73. ErrorIncorrectTitlekeyOrTitlekek,
  74. ErrorXCIMissingProgramNCA,
  75. ErrorNCANotProgram,
  76. ErrorNoExeFS,
  77. ErrorBadXCIHeader,
  78. ErrorXCIMissingPartition,
  79. ErrorNullFile,
  80. ErrorMissingNPDM,
  81. Error32BitISA,
  82. ErrorNoRomFS,
  83. ErrorIncorrectELFFileSize,
  84. ErrorLoadingNRO,
  85. ErrorLoadingNSO,
  86. ErrorNoIcon,
  87. ErrorNoControl,
  88. ErrorBadNAXHeader,
  89. ErrorIncorrectNAXFileSize,
  90. ErrorNAXKeyHMACFailed,
  91. ErrorNAXValidationHMACFailed,
  92. ErrorNAXKeyDerivationFailed,
  93. ErrorNAXInconvertibleToNCA,
  94. ErrorBadNAXFilePath,
  95. ErrorMissingSDSeed,
  96. ErrorMissingSDKEKSource,
  97. ErrorMissingAESKEKGenerationSource,
  98. ErrorMissingAESKeyGenerationSource,
  99. ErrorMissingSDSaveKeySource,
  100. ErrorMissingSDNCAKeySource,
  101. ErrorNSPMissingProgramNCA,
  102. ErrorBadBKTRHeader,
  103. ErrorBKTRSubsectionNotAfterRelocation,
  104. ErrorBKTRSubsectionNotAtEnd,
  105. ErrorBadRelocationBlock,
  106. ErrorBadSubsectionBlock,
  107. ErrorBadRelocationBuckets,
  108. ErrorBadSubsectionBuckets,
  109. ErrorMissingBKTRBaseRomFS,
  110. ErrorNoPackedUpdate,
  111. };
  112. std::ostream& operator<<(std::ostream& os, ResultStatus status);
  113. /// Interface for loading an application
  114. class AppLoader : NonCopyable {
  115. public:
  116. explicit AppLoader(FileSys::VirtualFile file);
  117. virtual ~AppLoader();
  118. /**
  119. * Returns the type of this file
  120. * @return FileType corresponding to the loaded file
  121. */
  122. virtual FileType GetFileType() const = 0;
  123. /**
  124. * Load the application and return the created Process instance
  125. * @param process The newly created process.
  126. * @return The status result of the operation.
  127. */
  128. virtual ResultStatus Load(Kernel::Process& process) = 0;
  129. /**
  130. * Loads the system mode that this application needs.
  131. * This function defaults to 2 (96MB allocated to the application) if it can't read the
  132. * information.
  133. * @returns A pair with the optional system mode, and and the status.
  134. */
  135. virtual std::pair<std::optional<u32>, ResultStatus> LoadKernelSystemMode() {
  136. // 96MB allocated to the application.
  137. return std::make_pair(2, ResultStatus::Success);
  138. }
  139. /**
  140. * Get the code (typically .code section) of the application
  141. * @param buffer Reference to buffer to store data
  142. * @return ResultStatus result of function
  143. */
  144. virtual ResultStatus ReadCode(std::vector<u8>& buffer) {
  145. return ResultStatus::ErrorNotImplemented;
  146. }
  147. /**
  148. * Get the icon (typically icon section) of the application
  149. * @param buffer Reference to buffer to store data
  150. * @return ResultStatus result of function
  151. */
  152. virtual ResultStatus ReadIcon(std::vector<u8>& buffer) {
  153. return ResultStatus::ErrorNotImplemented;
  154. }
  155. /**
  156. * Get the banner (typically banner section) of the application
  157. * @param buffer Reference to buffer to store data
  158. * @return ResultStatus result of function
  159. */
  160. virtual ResultStatus ReadBanner(std::vector<u8>& buffer) {
  161. return ResultStatus::ErrorNotImplemented;
  162. }
  163. /**
  164. * Get the logo (typically logo section) of the application
  165. * @param buffer Reference to buffer to store data
  166. * @return ResultStatus result of function
  167. */
  168. virtual ResultStatus ReadLogo(std::vector<u8>& buffer) {
  169. return ResultStatus::ErrorNotImplemented;
  170. }
  171. /**
  172. * Get the program id of the application
  173. * @param out_program_id Reference to store program id into
  174. * @return ResultStatus result of function
  175. */
  176. virtual ResultStatus ReadProgramId(u64& out_program_id) {
  177. return ResultStatus::ErrorNotImplemented;
  178. }
  179. /**
  180. * Get the RomFS of the application
  181. * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
  182. * @param file The directory containing the RomFS
  183. * @return ResultStatus result of function
  184. */
  185. virtual ResultStatus ReadRomFS(FileSys::VirtualFile& file) {
  186. return ResultStatus::ErrorNotImplemented;
  187. }
  188. /**
  189. * Get the raw update of the application, should it come packed with one
  190. * @param file The raw update NCA file (Program-type
  191. * @return ResultStatus result of function
  192. */
  193. virtual ResultStatus ReadUpdateRaw(FileSys::VirtualFile& file) {
  194. return ResultStatus::ErrorNotImplemented;
  195. }
  196. /**
  197. * Get whether or not updates can be applied to the RomFS.
  198. * By default, this is true, however for formats where it cannot be guaranteed that the RomFS is
  199. * the base game it should be set to false.
  200. * @return bool whether or not updatable.
  201. */
  202. virtual bool IsRomFSUpdatable() const {
  203. return true;
  204. }
  205. /**
  206. * Gets the difference between the start of the IVFC header and the start of level 6 (RomFS)
  207. * data. Needed for bktr patching.
  208. * @return IVFC offset for romfs.
  209. */
  210. virtual u64 ReadRomFSIVFCOffset() const {
  211. return 0;
  212. }
  213. /**
  214. * Get the title of the application
  215. * @param title Reference to store the application title into
  216. * @return ResultStatus result of function
  217. */
  218. virtual ResultStatus ReadTitle(std::string& title) {
  219. return ResultStatus::ErrorNotImplemented;
  220. }
  221. /**
  222. * Get the developer of the application
  223. * @param developer Reference to store the application developer into
  224. * @return ResultStatus result of function
  225. */
  226. virtual ResultStatus ReadDeveloper(std::string& developer) {
  227. return ResultStatus::ErrorNotImplemented;
  228. }
  229. protected:
  230. FileSys::VirtualFile file;
  231. bool is_loaded = false;
  232. };
  233. /**
  234. * Identifies a bootable file and return a suitable loader
  235. * @param file The bootable file
  236. * @return the best loader for this file
  237. */
  238. std::unique_ptr<AppLoader> GetLoader(FileSys::VirtualFile file);
  239. } // namespace Loader