From 4ed92df134bb72a6d4c9717e6269f7c91bdf9ce7 Mon Sep 17 00:00:00 2001 From: Robz8 Date: Thu, 4 Oct 2018 18:52:49 -0600 Subject: [PATCH] dldiLoadFromBin function now works --- arm9/source/driveOperations.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/arm9/source/driveOperations.cpp b/arm9/source/driveOperations.cpp index 8e5de25..434fe3a 100644 --- a/arm9/source/driveOperations.cpp +++ b/arm9/source/driveOperations.cpp @@ -33,15 +33,16 @@ bool bothSDandFlashcard(void) { } } -DLDI_INTERFACE* dldiLoadFromBin (void* dldiAddr, u16 size) { +DLDI_INTERFACE* dldiLoadFromBin (const u8 dldiAddr[]) { DLDI_INTERFACE* device; + size_t dldiSize; + // Read in the DLDI header if ((device = (DLDI_INTERFACE*)malloc (sizeof(DLDI_INTERFACE))) == NULL) { return NULL; } - // Load entire DLDI - memcpy(device, dldiAddr, size); + memcpy(device, dldiAddr, sizeof(DLDI_INTERFACE)); // Check that it is a valid DLDI if (!dldiIsValid (device)) { @@ -49,6 +50,23 @@ DLDI_INTERFACE* dldiLoadFromBin (void* dldiAddr, u16 size) { return NULL; } + // Calculate actual size of DLDI + // Although the file may only go to the dldiEnd, the BSS section can extend past that + if (device->dldiEnd > device->bssEnd) { + dldiSize = (char*)device->dldiEnd - (char*)device->dldiStart; + } else { + dldiSize = (char*)device->bssEnd - (char*)device->dldiStart; + } + dldiSize = (dldiSize + 0x03) & ~0x03; // Round up to nearest integer multiple + + // Load entire DLDI + free (device); + if ((device = (DLDI_INTERFACE*)malloc (dldiSize)) == NULL) { + return NULL; + } + + memcpy(device, dldiAddr, dldiSize); + dldiFixDriverAddresses (device); if (device->ioInterface.features & FEATURE_SLOT_GBA) { @@ -109,10 +127,10 @@ void flashcardMount(void) { // Read a DLDI driver specific to the cart if (!memcmp(gamename, "R4DSULTRA", 9)) { - io_dldi_data = dldiLoadFromBin((void*)r4idsn_sd_bin, r4idsn_sd_bin_size); + io_dldi_data = dldiLoadFromBin(r4idsn_sd_bin); fatMountSimple("fat", &io_dldi_data->ioInterface); } else if (!memcmp(gameid, "YCEP", 4) || !memcmp(gameid, "AHZH", 4)) { - io_dldi_data = dldiLoadFromBin((void*)ak2_sd_bin, ak2_sd_bin_size); + io_dldi_data = dldiLoadFromBin(ak2_sd_bin); fatMountSimple("fat", &io_dldi_data->ioInterface); } }