loader.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 Core {
  15. class System;
  16. }
  17. namespace FileSys {
  18. class NACP;
  19. } // namespace FileSys
  20. namespace Kernel {
  21. struct AddressMapping;
  22. class Process;
  23. } // namespace Kernel
  24. namespace Loader {
  25. /// File types supported by CTR
  26. enum class FileType {
  27. Error,
  28. Unknown,
  29. ELF,
  30. NSO,
  31. NRO,
  32. NCA,
  33. NSP,
  34. XCI,
  35. NAX,
  36. KIP,
  37. DeconstructedRomDirectory,
  38. };
  39. /**
  40. * Identifies the type of a bootable file based on the magic value in its header.
  41. * @param file open file
  42. * @return FileType of file
  43. */
  44. FileType IdentifyFile(FileSys::VirtualFile file);
  45. /**
  46. * Guess the type of a bootable file from its name
  47. * @param name String name 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 GuessFromFilename(const std::string& name);
  52. /**
  53. * Convert a FileType into a string which can be displayed to the user.
  54. */
  55. std::string GetFileTypeString(FileType type);
  56. /// Return type for functions in Loader namespace
  57. enum class ResultStatus : u16 {
  58. Success,
  59. ErrorAlreadyLoaded,
  60. ErrorNotImplemented,
  61. ErrorNotInitialized,
  62. ErrorBadNPDMHeader,
  63. ErrorBadACIDHeader,
  64. ErrorBadACIHeader,
  65. ErrorBadFileAccessControl,
  66. ErrorBadFileAccessHeader,
  67. ErrorBadKernelCapabilityDescriptors,
  68. ErrorBadPFSHeader,
  69. ErrorIncorrectPFSFileSize,
  70. ErrorBadNCAHeader,
  71. ErrorMissingProductionKeyFile,
  72. ErrorMissingHeaderKey,
  73. ErrorIncorrectHeaderKey,
  74. ErrorNCA2,
  75. ErrorNCA0,
  76. ErrorMissingTitlekey,
  77. ErrorMissingTitlekek,
  78. ErrorInvalidRightsID,
  79. ErrorMissingKeyAreaKey,
  80. ErrorIncorrectKeyAreaKey,
  81. ErrorIncorrectTitlekeyOrTitlekek,
  82. ErrorXCIMissingProgramNCA,
  83. ErrorNCANotProgram,
  84. ErrorNoExeFS,
  85. ErrorBadXCIHeader,
  86. ErrorXCIMissingPartition,
  87. ErrorNullFile,
  88. ErrorMissingNPDM,
  89. Error32BitISA,
  90. ErrorUnableToParseKernelMetadata,
  91. ErrorNoRomFS,
  92. ErrorIncorrectELFFileSize,
  93. ErrorLoadingNRO,
  94. ErrorLoadingNSO,
  95. ErrorNoIcon,
  96. ErrorNoControl,
  97. ErrorBadNAXHeader,
  98. ErrorIncorrectNAXFileSize,
  99. ErrorNAXKeyHMACFailed,
  100. ErrorNAXValidationHMACFailed,
  101. ErrorNAXKeyDerivationFailed,
  102. ErrorNAXInconvertibleToNCA,
  103. ErrorBadNAXFilePath,
  104. ErrorMissingSDSeed,
  105. ErrorMissingSDKEKSource,
  106. ErrorMissingAESKEKGenerationSource,
  107. ErrorMissingAESKeyGenerationSource,
  108. ErrorMissingSDSaveKeySource,
  109. ErrorMissingSDNCAKeySource,
  110. ErrorNSPMissingProgramNCA,
  111. ErrorBadBKTRHeader,
  112. ErrorBKTRSubsectionNotAfterRelocation,
  113. ErrorBKTRSubsectionNotAtEnd,
  114. ErrorBadRelocationBlock,
  115. ErrorBadSubsectionBlock,
  116. ErrorBadRelocationBuckets,
  117. ErrorBadSubsectionBuckets,
  118. ErrorMissingBKTRBaseRomFS,
  119. ErrorNoPackedUpdate,
  120. ErrorBadKIPHeader,
  121. ErrorBLZDecompressionFailed,
  122. ErrorBadINIHeader,
  123. ErrorINITooManyKIPs,
  124. };
  125. std::ostream& operator<<(std::ostream& os, ResultStatus status);
  126. /// Interface for loading an application
  127. class AppLoader : NonCopyable {
  128. public:
  129. struct LoadParameters {
  130. s32 main_thread_priority;
  131. u64 main_thread_stack_size;
  132. };
  133. using LoadResult = std::pair<ResultStatus, std::optional<LoadParameters>>;
  134. explicit AppLoader(FileSys::VirtualFile file);
  135. virtual ~AppLoader();
  136. /**
  137. * Returns the type of this file
  138. * @return FileType corresponding to the loaded file
  139. */
  140. virtual FileType GetFileType() const = 0;
  141. /**
  142. * Load the application and return the created Process instance
  143. * @param process The newly created process.
  144. * @param system The system that this process is being loaded under.
  145. * @return The status result of the operation.
  146. */
  147. virtual LoadResult Load(Kernel::Process& process, Core::System& system) = 0;
  148. /**
  149. * Get the code (typically .code section) of the application
  150. * @param buffer Reference to buffer to store data
  151. * @return ResultStatus result of function
  152. */
  153. virtual ResultStatus ReadCode(std::vector<u8>& buffer) {
  154. return ResultStatus::ErrorNotImplemented;
  155. }
  156. /**
  157. * Get the icon (typically icon section) of the application
  158. * @param buffer Reference to buffer to store data
  159. * @return ResultStatus result of function
  160. */
  161. virtual ResultStatus ReadIcon(std::vector<u8>& buffer) {
  162. return ResultStatus::ErrorNotImplemented;
  163. }
  164. /**
  165. * Get the banner (typically banner section) of the application
  166. * In the context of NX, this is the animation that displays in the bottom right of the screen
  167. * when a game boots. Stored in GIF format.
  168. * @param buffer Reference to buffer to store data
  169. * @return ResultStatus result of function
  170. */
  171. virtual ResultStatus ReadBanner(std::vector<u8>& buffer) {
  172. return ResultStatus::ErrorNotImplemented;
  173. }
  174. /**
  175. * Get the logo (typically logo section) of the application
  176. * In the context of NX, this is the static image that displays in the top left of the screen
  177. * when a game boots. Stored in JPEG format.
  178. * @param buffer Reference to buffer to store data
  179. * @return ResultStatus result of function
  180. */
  181. virtual ResultStatus ReadLogo(std::vector<u8>& buffer) {
  182. return ResultStatus::ErrorNotImplemented;
  183. }
  184. /**
  185. * Get the program id of the application
  186. * @param out_program_id Reference to store program id into
  187. * @return ResultStatus result of function
  188. */
  189. virtual ResultStatus ReadProgramId(u64& out_program_id) {
  190. return ResultStatus::ErrorNotImplemented;
  191. }
  192. /**
  193. * Get the RomFS of the application
  194. * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
  195. * @param file The directory containing the RomFS
  196. * @return ResultStatus result of function
  197. */
  198. virtual ResultStatus ReadRomFS(FileSys::VirtualFile& file) {
  199. return ResultStatus::ErrorNotImplemented;
  200. }
  201. /**
  202. * Get the raw update of the application, should it come packed with one
  203. * @param file The raw update NCA file (Program-type
  204. * @return ResultStatus result of function
  205. */
  206. virtual ResultStatus ReadUpdateRaw(FileSys::VirtualFile& file) {
  207. return ResultStatus::ErrorNotImplemented;
  208. }
  209. /**
  210. * Get whether or not updates can be applied to the RomFS.
  211. * By default, this is true, however for formats where it cannot be guaranteed that the RomFS is
  212. * the base game it should be set to false.
  213. * @return bool whether or not updatable.
  214. */
  215. virtual bool IsRomFSUpdatable() const {
  216. return true;
  217. }
  218. /**
  219. * Gets the difference between the start of the IVFC header and the start of level 6 (RomFS)
  220. * data. Needed for bktr patching.
  221. * @return IVFC offset for romfs.
  222. */
  223. virtual u64 ReadRomFSIVFCOffset() const {
  224. return 0;
  225. }
  226. /**
  227. * Get the title of the application
  228. * @param title Reference to store the application title into
  229. * @return ResultStatus result of function
  230. */
  231. virtual ResultStatus ReadTitle(std::string& title) {
  232. return ResultStatus::ErrorNotImplemented;
  233. }
  234. /**
  235. * Get the control data (CNMT) of the application
  236. * @param control Reference to store the application control data into
  237. * @return ResultStatus result of function
  238. */
  239. virtual ResultStatus ReadControlData(FileSys::NACP& control) {
  240. return ResultStatus::ErrorNotImplemented;
  241. }
  242. /**
  243. * Get the RomFS of the manual of the application
  244. * @param file The raw manual RomFS of the game
  245. * @return ResultStatus result of function
  246. */
  247. virtual ResultStatus ReadManualRomFS(FileSys::VirtualFile& file) {
  248. return ResultStatus::ErrorNotImplemented;
  249. }
  250. using Modules = std::map<VAddr, std::string>;
  251. virtual ResultStatus ReadNSOModules(Modules& modules) {
  252. return ResultStatus::ErrorNotImplemented;
  253. }
  254. protected:
  255. FileSys::VirtualFile file;
  256. bool is_loaded = false;
  257. };
  258. /**
  259. * Identifies a bootable file and return a suitable loader
  260. *
  261. * @param system The system context.
  262. * @param file The bootable file.
  263. * @param program_index Specifies the index within the container of the program to launch.
  264. *
  265. * @return the best loader for this file.
  266. */
  267. std::unique_ptr<AppLoader> GetLoader(Core::System& system, FileSys::VirtualFile file,
  268. std::size_t program_index = 0);
  269. } // namespace Loader