From 7cb30ba83e0805cba80ee6ccca8fe9c1bdc61b39 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Wed, 1 Nov 2017 16:57:39 -0700 Subject: [PATCH] GBA Savedata: Fix crash when resizing flash --- CHANGES | 1 + src/gba/savedata.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 8c3cd8a64..7e56d7387 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,7 @@ Bugfixes: - GBA Cheats: Fix PARv3 slide codes (fixes mgba.io/i/919) - GBA Video: OBJWIN can change blend params after OBJ is drawn (fixes mgba.io/i/921) - GBA DMA: Fix invalid DMA reads (fixes mgba.io/i/142) + - GBA Savedata: Fix crash when resizing flash Misc: - GBA Timer: Use global cycles for timers - GBA: Extend oddly-sized ROMs to full address space (fixes mgba.io/i/722) diff --git a/src/gba/savedata.c b/src/gba/savedata.c index 3008b9428..c8c6c8be7 100644 --- a/src/gba/savedata.c +++ b/src/gba/savedata.c @@ -576,9 +576,15 @@ void _flashSwitchBank(struct GBASavedata* savedata, int bank) { if (bank > 0 && savedata->type == SAVEDATA_FLASH512) { mLOG(GBA_SAVE, INFO, "Updating flash chip from 512kb to 1Mb"); savedata->type = SAVEDATA_FLASH1M; - if (savedata->vf && savedata->vf->size(savedata->vf) == SIZE_CART_FLASH512) { - savedata->vf->truncate(savedata->vf, SIZE_CART_FLASH1M); - memset(&savedata->data[SIZE_CART_FLASH512], 0xFF, SIZE_CART_FLASH512); + if (savedata->vf) { + savedata->vf->unmap(savedata->vf, savedata->data, SIZE_CART_FLASH512); + if (savedata->vf->size(savedata->vf) == SIZE_CART_FLASH512) { + savedata->vf->truncate(savedata->vf, SIZE_CART_FLASH1M); + savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MAP_WRITE); + memset(&savedata->data[SIZE_CART_FLASH512], 0xFF, SIZE_CART_FLASH512); + } else { + savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MAP_WRITE); + } } } }