loader.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <vector>
  10. #include "common/common_funcs.h"
  11. #include "common/common_types.h"
  12. #include "common/file_util.h"
  13. #include "common/swap.h"
  14. namespace Kernel {
  15. struct AddressMapping;
  16. }
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // Loader namespace
  19. namespace Loader {
  20. /// File types supported by CTR
  21. enum class FileType {
  22. Error,
  23. Unknown,
  24. CCI,
  25. CXI,
  26. CIA,
  27. ELF,
  28. THREEDSX, //3DSX
  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. constexpr u32 MakeMagic(char a, char b, char c, char d) {
  67. return a | b << 8 | c << 16 | d << 24;
  68. }
  69. /// SMDH data structure that contains titles, icons etc. See https://www.3dbrew.org/wiki/SMDH
  70. struct SMDH {
  71. u32_le magic;
  72. u16_le version;
  73. INSERT_PADDING_BYTES(2);
  74. struct Title {
  75. std::array<u16, 0x40> short_title;
  76. std::array<u16, 0x80> long_title;
  77. std::array<u16, 0x40> publisher;
  78. };
  79. std::array<Title, 16> titles;
  80. std::array<u8, 16> ratings;
  81. u32_le region_lockout;
  82. u32_le match_maker_id;
  83. u64_le match_maker_bit_id;
  84. u32_le flags;
  85. u16_le eula_version;
  86. INSERT_PADDING_BYTES(2);
  87. float_le banner_animation_frame;
  88. u32_le cec_id;
  89. INSERT_PADDING_BYTES(8);
  90. std::array<u8, 0x480> small_icon;
  91. std::array<u8, 0x1200> large_icon;
  92. /// indicates the language used for each title entry
  93. enum class TitleLanguage {
  94. Japanese = 0,
  95. English = 1,
  96. French = 2,
  97. German = 3,
  98. Italian = 4,
  99. Spanish = 5,
  100. SimplifiedChinese = 6,
  101. Korean= 7,
  102. Dutch = 8,
  103. Portuguese = 9,
  104. Russian = 10,
  105. TraditionalChinese = 11
  106. };
  107. };
  108. static_assert(sizeof(SMDH) == 0x36C0, "SMDH structure size is wrong");
  109. /// Interface for loading an application
  110. class AppLoader : NonCopyable {
  111. public:
  112. AppLoader(FileUtil::IOFile&& file) : file(std::move(file)) { }
  113. virtual ~AppLoader() { }
  114. /**
  115. * Returns the type of this file
  116. * @return FileType corresponding to the loaded file
  117. */
  118. virtual FileType GetFileType() = 0;
  119. /**
  120. * Load the application
  121. * @return ResultStatus result of function
  122. */
  123. virtual ResultStatus Load() = 0;
  124. /**
  125. * Get the code (typically .code section) of the application
  126. * @param buffer Reference to buffer to store data
  127. * @return ResultStatus result of function
  128. */
  129. virtual ResultStatus ReadCode(std::vector<u8>& buffer) {
  130. return ResultStatus::ErrorNotImplemented;
  131. }
  132. /**
  133. * Get the icon (typically icon section) of the application
  134. * @param buffer Reference to buffer to store data
  135. * @return ResultStatus result of function
  136. */
  137. virtual ResultStatus ReadIcon(std::vector<u8>& buffer) {
  138. return ResultStatus::ErrorNotImplemented;
  139. }
  140. /**
  141. * Get the banner (typically banner section) of the application
  142. * @param buffer Reference to buffer to store data
  143. * @return ResultStatus result of function
  144. */
  145. virtual ResultStatus ReadBanner(std::vector<u8>& buffer) {
  146. return ResultStatus::ErrorNotImplemented;
  147. }
  148. /**
  149. * Get the logo (typically logo section) of the application
  150. * @param buffer Reference to buffer to store data
  151. * @return ResultStatus result of function
  152. */
  153. virtual ResultStatus ReadLogo(std::vector<u8>& buffer) {
  154. return ResultStatus::ErrorNotImplemented;
  155. }
  156. /**
  157. * Get the RomFS of the application
  158. * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
  159. * @param romfs_file The file containing the RomFS
  160. * @param offset The offset the romfs begins on
  161. * @param size The size of the romfs
  162. * @return ResultStatus result of function
  163. */
  164. virtual ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) {
  165. return ResultStatus::ErrorNotImplemented;
  166. }
  167. protected:
  168. FileUtil::IOFile file;
  169. bool is_loaded = false;
  170. };
  171. /**
  172. * Common address mappings found in most games, used for binary formats that don't have this
  173. * information.
  174. */
  175. extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings;
  176. /**
  177. * Identifies a bootable file and return a suitable loader
  178. * @param filename String filename of bootable file
  179. * @return best loader for this file
  180. */
  181. std::unique_ptr<AppLoader> GetLoader(const std::string& filename);
  182. } // namespace