Skip to content

Commit

Permalink
Replace dldiFixDriverAddresses in dldiLoadFromBin
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Nov 11, 2024
1 parent 7e56d4f commit b4abe2e
Showing 1 changed file with 131 additions and 7 deletions.
138 changes: 131 additions & 7 deletions arm9/source/driveOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,142 @@ void sdUnmount(void) {
sdMounted = false;
}

typedef signed int addr_t;
typedef unsigned char data_t;

#define FIX_ALL 0x01
#define FIX_GLUE 0x02
#define FIX_GOT 0x04
#define FIX_BSS 0x08

enum DldiOffsets {
DO_magicString = 0x00, // "\xED\xA5\x8D\xBF Chishm"
DO_magicToken = 0x00, // 0xBF8DA5ED
DO_magicShortString = 0x04, // " Chishm"
DO_version = 0x0C,
DO_driverSize = 0x0D,
DO_fixSections = 0x0E,
DO_allocatedSpace = 0x0F,

DO_friendlyName = 0x10,

DO_text_start = 0x40, // Data start
DO_data_end = 0x44, // Data end
DO_glue_start = 0x48, // Interworking glue start -- Needs address fixing
DO_glue_end = 0x4C, // Interworking glue end
DO_got_start = 0x50, // GOT start -- Needs address fixing
DO_got_end = 0x54, // GOT end
DO_bss_start = 0x58, // bss start -- Needs setting to zero
DO_bss_end = 0x5C, // bss end

// IO_INTERFACE data
DO_ioType = 0x60,
DO_features = 0x64,
DO_startup = 0x68,
DO_isInserted = 0x6C,
DO_readSectors = 0x70,
DO_writeSectors = 0x74,
DO_clearStatus = 0x78,
DO_shutdown = 0x7C,
DO_code = 0x80
};

static addr_t readAddr (data_t *mem, addr_t offset) {
return ((addr_t*)mem)[offset/sizeof(addr_t)];
}

static void writeAddr (data_t *mem, addr_t offset, addr_t value) {
((addr_t*)mem)[offset/sizeof(addr_t)] = value;
}

TWL_CODE static void dldiRelocateBinary (data_t *binData, size_t dldiFileSize)
{
addr_t memOffset; // Offset of DLDI after the file is loaded into memory
addr_t relocationOffset; // Value added to all offsets within the patch to fix it properly
addr_t ddmemOffset; // Original offset used in the DLDI file
addr_t ddmemStart; // Start of range that offsets can be in the DLDI file
addr_t ddmemEnd; // End of range that offsets can be in the DLDI file
addr_t ddmemSize; // Size of range that offsets can be in the DLDI file

addr_t addrIter;

data_t *pDH = binData;
data_t *pAH = (data_t*)(io_dldi_data);

// size_t dldiFileSize = 1 << pDH[DO_driverSize];

memOffset = readAddr (pAH, DO_text_start);
if (memOffset == 0) {
memOffset = readAddr (pAH, DO_startup) - DO_code;
}
ddmemOffset = readAddr (pDH, DO_text_start);
relocationOffset = memOffset - ddmemOffset;

ddmemStart = readAddr (pDH, DO_text_start);
ddmemSize = (1 << pDH[DO_driverSize]);
ddmemEnd = ddmemStart + ddmemSize;

// Remember how much space is actually reserved
pDH[DO_allocatedSpace] = pAH[DO_allocatedSpace];
// Copy the DLDI patch into the application
tonccpy (pAH, pDH, dldiFileSize);

// Fix the section pointers in the header
writeAddr (pAH, DO_text_start, readAddr (pAH, DO_text_start) + relocationOffset);
writeAddr (pAH, DO_data_end, readAddr (pAH, DO_data_end) + relocationOffset);
writeAddr (pAH, DO_glue_start, readAddr (pAH, DO_glue_start) + relocationOffset);
writeAddr (pAH, DO_glue_end, readAddr (pAH, DO_glue_end) + relocationOffset);
writeAddr (pAH, DO_got_start, readAddr (pAH, DO_got_start) + relocationOffset);
writeAddr (pAH, DO_got_end, readAddr (pAH, DO_got_end) + relocationOffset);
writeAddr (pAH, DO_bss_start, readAddr (pAH, DO_bss_start) + relocationOffset);
writeAddr (pAH, DO_bss_end, readAddr (pAH, DO_bss_end) + relocationOffset);
// Fix the function pointers in the header
writeAddr (pAH, DO_startup, readAddr (pAH, DO_startup) + relocationOffset);
writeAddr (pAH, DO_isInserted, readAddr (pAH, DO_isInserted) + relocationOffset);
writeAddr (pAH, DO_readSectors, readAddr (pAH, DO_readSectors) + relocationOffset);
writeAddr (pAH, DO_writeSectors, readAddr (pAH, DO_writeSectors) + relocationOffset);
writeAddr (pAH, DO_clearStatus, readAddr (pAH, DO_clearStatus) + relocationOffset);
writeAddr (pAH, DO_shutdown, readAddr (pAH, DO_shutdown) + relocationOffset);

if (pDH[DO_fixSections] & FIX_ALL) {
// Search through and fix pointers within the data section of the file
for (addrIter = (readAddr(pDH, DO_text_start) - ddmemStart); addrIter < (readAddr(pDH, DO_data_end) - ddmemStart); addrIter++) {
if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
}
}
}

if (pDH[DO_fixSections] & FIX_GLUE) {
// Search through and fix pointers within the glue section of the file
for (addrIter = (readAddr(pDH, DO_glue_start) - ddmemStart); addrIter < (readAddr(pDH, DO_glue_end) - ddmemStart); addrIter++) {
if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
}
}
}

if (pDH[DO_fixSections] & FIX_GOT) {
// Search through and fix pointers within the Global Offset Table section of the file
for (addrIter = (readAddr(pDH, DO_got_start) - ddmemStart); addrIter < (readAddr(pDH, DO_got_end) - ddmemStart); addrIter++) {
if ((ddmemStart <= readAddr(pAH, addrIter)) && (readAddr(pAH, addrIter) < ddmemEnd)) {
writeAddr (pAH, addrIter, readAddr(pAH, addrIter) + relocationOffset);
}
}
}

if (pDH[DO_fixSections] & FIX_BSS) {
// Initialise the BSS to 0
toncset (&pAH[readAddr(pDH, DO_bss_start) - ddmemStart] , 0, readAddr(pDH, DO_bss_end) - readAddr(pDH, DO_bss_start));
}
}

TWL_CODE void dldiLoadFromBin (const u8 dldiAddr[]) {
// Check that it is a valid DLDI
if (!dldiIsValid ((DLDI_INTERFACE*)dldiAddr)) {
return;
}

extern DLDI_INTERFACE _io_dldi_stub;
DLDI_INTERFACE* device = (DLDI_INTERFACE*)dldiAddr;
size_t dldiSize;

Expand All @@ -217,12 +346,7 @@ TWL_CODE void dldiLoadFromBin (const u8 dldiAddr[]) {
}
dldiSize = (dldiSize + 0x03) & ~0x03; // Round up to nearest integer multiple

// Clear unused space
toncset(&_io_dldi_stub, 0, 0x4000);

tonccpy(&_io_dldi_stub, device, dldiSize);

dldiFixDriverAddresses (&_io_dldi_stub);
dldiRelocateBinary ((data_t*)dldiAddr, dldiSize);

if (device->ioInterface.features & FEATURE_SLOT_GBA) {
sysSetCartOwner(BUS_OWNER_ARM9);
Expand Down

0 comments on commit b4abe2e

Please sign in to comment.