loader.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <string>
  6. #include "common/logging/log.h"
  7. #include "common/string_util.h"
  8. #include "core/hle/kernel/process.h"
  9. #include "core/loader/elf.h"
  10. #include "core/loader/nro.h"
  11. #include "core/loader/nso.h"
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. namespace Loader {
  14. const std::initializer_list<Kernel::AddressMapping> default_address_mappings = {
  15. {0x1FF50000, 0x8000, true}, // part of DSP RAM
  16. {0x1FF70000, 0x8000, true}, // part of DSP RAM
  17. {0x1F000000, 0x600000, false}, // entire VRAM
  18. };
  19. FileType IdentifyFile(FileUtil::IOFile& file) {
  20. FileType type;
  21. #define CHECK_TYPE(loader) \
  22. type = AppLoader_##loader::IdentifyType(file); \
  23. if (FileType::Error != type) \
  24. return type;
  25. CHECK_TYPE(ELF)
  26. CHECK_TYPE(NSO)
  27. CHECK_TYPE(NRO)
  28. #undef CHECK_TYPE
  29. return FileType::Unknown;
  30. }
  31. FileType IdentifyFile(const std::string& file_name) {
  32. FileUtil::IOFile file(file_name, "rb");
  33. if (!file.IsOpen()) {
  34. LOG_ERROR(Loader, "Failed to load file %s", file_name.c_str());
  35. return FileType::Unknown;
  36. }
  37. return IdentifyFile(file);
  38. }
  39. FileType GuessFromExtension(const std::string& extension_) {
  40. std::string extension = Common::ToLower(extension_);
  41. if (extension == ".elf" || extension == ".axf")
  42. return FileType::ELF;
  43. return FileType::Unknown;
  44. }
  45. const char* GetFileTypeString(FileType type) {
  46. switch (type) {
  47. case FileType::ELF:
  48. return "ELF";
  49. case FileType::Error:
  50. case FileType::Unknown:
  51. break;
  52. }
  53. return "unknown";
  54. }
  55. /**
  56. * Get a loader for a file with a specific type
  57. * @param file The file to load
  58. * @param type The type of the file
  59. * @param filename the file name (without path)
  60. * @param filepath the file full path (with name)
  61. * @return std::unique_ptr<AppLoader> a pointer to a loader object; nullptr for unsupported type
  62. */
  63. static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileType type,
  64. const std::string& filename,
  65. const std::string& filepath) {
  66. switch (type) {
  67. // Standard ELF file format.
  68. case FileType::ELF:
  69. return std::make_unique<AppLoader_ELF>(std::move(file), filename);
  70. // NX NSO file format.
  71. case FileType::NSO:
  72. return std::make_unique<AppLoader_NSO>(std::move(file), filepath);
  73. // NX NRO file format.
  74. case FileType::NRO:
  75. return std::make_unique<AppLoader_NRO>(std::move(file), filepath);
  76. default:
  77. return nullptr;
  78. }
  79. }
  80. std::unique_ptr<AppLoader> GetLoader(const std::string& filename) {
  81. FileUtil::IOFile file(filename, "rb");
  82. if (!file.IsOpen()) {
  83. LOG_ERROR(Loader, "Failed to load file %s", filename.c_str());
  84. return nullptr;
  85. }
  86. std::string filename_filename, filename_extension;
  87. Common::SplitPath(filename, nullptr, &filename_filename, &filename_extension);
  88. FileType type = IdentifyFile(file);
  89. FileType filename_type = GuessFromExtension(filename_extension);
  90. if (type != filename_type) {
  91. LOG_WARNING(Loader, "File %s has a different type than its extension.", filename.c_str());
  92. if (FileType::Unknown == type)
  93. type = filename_type;
  94. }
  95. LOG_DEBUG(Loader, "Loading file %s as %s...", filename.c_str(), GetFileTypeString(type));
  96. return GetFileLoader(std::move(file), type, filename_filename, filename);
  97. }
  98. } // namespace Loader