From 6966fdf02578750f31098f23f1f14ebb4f642e2a Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Wed, 27 Aug 2014 00:04:26 -0400
Subject: [PATCH] Loader: Added support for loading raw BIN executables.

- Useful for debugging homebrew

Qt: Updated GUI to support loading .bin files.
---
 src/citra_qt/main.cpp      |  2 +-
 src/core/loader/loader.cpp | 20 ++++++++++++++++++++
 src/core/loader/loader.h   |  1 +
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index a6b87f781c..1bf9bc53c0 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -150,7 +150,7 @@ void GMainWindow::BootGame(std::string filename)
 
 void GMainWindow::OnMenuLoadFile()
 {
-    QString filename = QFileDialog::getOpenFileName(this, tr("Load file"), QString(), tr("3DS executable (*.elf *.axf *.cci *.cxi)"));
+    QString filename = QFileDialog::getOpenFileName(this, tr("Load file"), QString(), tr("3DS executable (*.elf *.axf *.bin *.cci *.cxi)"));
     if (filename.size())
        BootGame(filename.toLatin1().data());
 }
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index 2b42e3c647..365f5a2777 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -9,6 +9,7 @@
 #include "core/loader/elf.h"
 #include "core/loader/ncch.h"
 #include "core/hle/kernel/archive.h"
+#include "core/mem_map.h"
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -39,6 +40,9 @@ FileType IdentifyFile(const std::string &filename) {
     else if (!strcasecmp(extension.c_str(), ".cci")) {
         return FileType::CCI; // TODO(bunnei): Do some filetype checking :p
     }
+    else if (!strcasecmp(extension.c_str(), ".bin")) {
+        return FileType::BIN; // TODO(bunnei): Do some filetype checking :p
+    }
     return FileType::Unknown;
 }
 
@@ -69,6 +73,22 @@ ResultStatus LoadFile(const std::string& filename) {
         break;
     }
 
+    // Raw BIN file format...
+    case FileType::BIN:
+    {
+        INFO_LOG(LOADER, "Loading BIN file %s...", filename.c_str());
+
+        File::IOFile file(filename, "rb");
+
+        if (file.IsOpen()) {
+            file.ReadBytes(Memory::GetPointer(Memory::EXEFS_CODE_VADDR), (size_t)file.GetSize());
+            Kernel::LoadExec(Memory::EXEFS_CODE_VADDR);
+        } else {
+            return ResultStatus::Error;
+        }
+        return ResultStatus::Success;
+    }
+
     // Error occurred durring IdentifyFile...
     case FileType::Error:
 
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 4ba10de527..68f843005b 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -21,6 +21,7 @@ enum class FileType {
     CXI,
     CIA,
     ELF,
+    BIN,
 };
 
 /// Return type for functions in Loader namespace