loader.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_types.h"
  11. #include "common/file_util.h"
  12. namespace Kernel {
  13. struct AddressMapping;
  14. }
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. // Loader namespace
  17. namespace Loader {
  18. /// File types supported by CTR
  19. enum class FileType {
  20. Error,
  21. Unknown,
  22. CCI,
  23. CXI,
  24. CIA,
  25. ELF,
  26. THREEDSX, //3DSX
  27. };
  28. /// Return type for functions in Loader namespace
  29. enum class ResultStatus {
  30. Success,
  31. Error,
  32. ErrorInvalidFormat,
  33. ErrorNotImplemented,
  34. ErrorNotLoaded,
  35. ErrorNotUsed,
  36. ErrorAlreadyLoaded,
  37. ErrorMemoryAllocationFailed,
  38. };
  39. static inline u32 MakeMagic(char a, char b, char c, char d) {
  40. return a | b << 8 | c << 16 | d << 24;
  41. }
  42. /// Interface for loading an application
  43. class AppLoader : NonCopyable {
  44. public:
  45. AppLoader(FileUtil::IOFile&& file) : file(std::move(file)) { }
  46. virtual ~AppLoader() { }
  47. /**
  48. * Load the application
  49. * @return ResultStatus result of function
  50. */
  51. virtual ResultStatus Load() = 0;
  52. /**
  53. * Get the code (typically .code section) of the application
  54. * @param buffer Reference to buffer to store data
  55. * @return ResultStatus result of function
  56. */
  57. virtual ResultStatus ReadCode(std::vector<u8>& buffer) {
  58. return ResultStatus::ErrorNotImplemented;
  59. }
  60. /**
  61. * Get the icon (typically icon section) of the application
  62. * @param buffer Reference to buffer to store data
  63. * @return ResultStatus result of function
  64. */
  65. virtual ResultStatus ReadIcon(std::vector<u8>& buffer) {
  66. return ResultStatus::ErrorNotImplemented;
  67. }
  68. /**
  69. * Get the banner (typically banner section) of the application
  70. * @param buffer Reference to buffer to store data
  71. * @return ResultStatus result of function
  72. */
  73. virtual ResultStatus ReadBanner(std::vector<u8>& buffer) {
  74. return ResultStatus::ErrorNotImplemented;
  75. }
  76. /**
  77. * Get the logo (typically logo section) of the application
  78. * @param buffer Reference to buffer to store data
  79. * @return ResultStatus result of function
  80. */
  81. virtual ResultStatus ReadLogo(std::vector<u8>& buffer) {
  82. return ResultStatus::ErrorNotImplemented;
  83. }
  84. /**
  85. * Get the RomFS of the application
  86. * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
  87. * @param romfs_file The file containing the RomFS
  88. * @param offset The offset the romfs begins on
  89. * @param size The size of the romfs
  90. * @return ResultStatus result of function
  91. */
  92. virtual ResultStatus ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, u64& offset, u64& size) {
  93. return ResultStatus::ErrorNotImplemented;
  94. }
  95. protected:
  96. FileUtil::IOFile file;
  97. bool is_loaded = false;
  98. };
  99. /**
  100. * Common address mappings found in most games, used for binary formats that don't have this
  101. * information.
  102. */
  103. extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings;
  104. /**
  105. * Identifies and loads a bootable file
  106. * @param filename String filename of bootable file
  107. * @return ResultStatus result of function
  108. */
  109. ResultStatus LoadFile(const std::string& filename);
  110. } // namespace