loader.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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(std::unique_ptr<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) const {
  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) const {
  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) const {
  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) const {
  82. return ResultStatus::ErrorNotImplemented;
  83. }
  84. /**
  85. * Get the RomFS of the application
  86. * @param buffer Reference to buffer to store data
  87. * @return ResultStatus result of function
  88. */
  89. virtual ResultStatus ReadRomFS(std::vector<u8>& buffer) const {
  90. return ResultStatus::ErrorNotImplemented;
  91. }
  92. protected:
  93. std::unique_ptr<FileUtil::IOFile> file;
  94. bool is_loaded = false;
  95. };
  96. /**
  97. * Common address mappings found in most games, used for binary formats that don't have this
  98. * information.
  99. */
  100. extern const std::initializer_list<Kernel::AddressMapping> default_address_mappings;
  101. /**
  102. * Identifies and loads a bootable file
  103. * @param filename String filename of bootable file
  104. * @return ResultStatus result of function
  105. */
  106. ResultStatus LoadFile(const std::string& filename);
  107. } // namespace