From b8e5b47c80d1780ddb22508fa7a8bb2c5870cec9 Mon Sep 17 00:00:00 2001
From: Vicki Pfau <vi@endrift.com>
Date: Tue, 24 Nov 2020 01:38:41 -0800
Subject: [PATCH] GBA: Fix loading multiboot ELF files (fixes #1949)

---
 CHANGES        | 1 +
 src/gba/core.c | 7 ++++++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/CHANGES b/CHANGES
index a989b93fb..9236918f0 100644
--- a/CHANGES
+++ b/CHANGES
@@ -63,6 +63,7 @@ Other fixes:
  - FFmpeg: Fix some small memory leaks
  - FFmpeg: Fix encoding of time base
  - GB Video: Fix SGB video logs
+ - GBA: Fix loading multiboot ELF files (fixes mgba.io/i/1949)
  - mGUI: Don't attempt to preload files larger than can fit in RAM
  - Qt: Force OpenGL paint engine creation thread (fixes mgba.io/i/1642)
  - Qt: Fix static compilation in MinGW (fixes mgba.io/i/1769)
diff --git a/src/gba/core.c b/src/gba/core.c
index 6e5a5bd30..1df7286ff 100644
--- a/src/gba/core.c
+++ b/src/gba/core.c
@@ -487,9 +487,14 @@ static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
 #ifdef USE_ELF
 	struct ELF* elf = ELFOpen(vf);
 	if (elf) {
-		GBALoadNull(core->board);
+		if (ELFEntry(elf) == BASE_CART0) {
+			GBALoadNull(core->board);
+		}
 		bool success = mCoreLoadELF(core, elf);
 		ELFClose(elf);
+		if (success) {
+			vf->close(vf);
+		}
 		return success;
 	}
 #endif