From ef137eef5b1debec8f2ec80f706e8a50c68d4193 Mon Sep 17 00:00:00 2001 From: fincs Date: Mon, 1 Jan 2024 02:22:23 +0100 Subject: [PATCH] Automatically choose load address of DSi device list --- source/elf.cpp | 11 ++++++++++- source/elf.h | 1 + source/ndscreate.cpp | 10 +++++----- source/ndstool.cpp | 1 + source/ndstool.h | 1 + 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/source/elf.cpp b/source/elf.cpp index c9c980e..f552546 100644 --- a/source/elf.cpp +++ b/source/elf.cpp @@ -137,12 +137,13 @@ void ElfReadHdr(FILE *fp, Elf32_Ehdr *hdr, Elf32_Phdr **phdr) { * unsigned int *size, a pointer to place the data size at. * unsigned int *wram_address,a pointer to map DSi exclusive ARM7 WRAM at. * bool *has_overlays, a pointer to place the "has overlays" flag at. + * bool is_arm9, true if this is the arm9 binary. * bool is_twl, true if we want to copy TWL sections. */ int CopyFromElf(char *elfFilename, unsigned int *entry, unsigned int *ram_address, unsigned int *size, unsigned int *wram_address, bool *has_overlays, - bool is_twl) + bool is_arm9, bool is_twl) { FILE *in; Elf32_Ehdr header; @@ -192,6 +193,14 @@ int CopyFromElf(char *elfFilename, unsigned int *entry, if(wram_address && !*wram_address && p_headers[i].p_vaddr >= 0x03000000 && p_headers[i].p_vaddr < 0x037F8000) *wram_address = p_headers[i].p_vaddr; + /* Automatically choose load address of DSi device list (right after all ARM7 WRAM segments). */ + if(!is_arm9 && !is_twl) { + unsigned int end_address = (p_headers[i].p_vaddr + p_headers[i].p_memsz + 3) &~ 3; + if (end_address > deviceListRamAddress && end_address + 0x400 <= 0x0380F000) { + deviceListRamAddress = end_address; + } + } + /* Skip BSS segments. */ if(!p_headers[i].p_filesz) continue; diff --git a/source/elf.h b/source/elf.h index 6c76d3a..d17cf96 100644 --- a/source/elf.h +++ b/source/elf.h @@ -158,6 +158,7 @@ int CopyFromElf(char *elfFilename, unsigned int *size, unsigned int *wram_address, bool *has_overlays, + bool is_arm9, bool is_twl); void CopyOverlaysFromElf(const char* elfFilename, bool is_arm9); void ElfReadHdr(FILE *fp, Elf32_Ehdr *hdr, Elf32_Phdr **phdr); diff --git a/source/ndscreate.cpp b/source/ndscreate.cpp index 990e1db..1be2857 100644 --- a/source/ndscreate.cpp +++ b/source/ndscreate.cpp @@ -444,7 +444,7 @@ void Create() unsigned int size = 0; bool has_overlays = false; if (is_arm9_elf) - CopyFromElf(arm9filename, &entry_address, &ram_address, &size, NULL, &has_overlays, false); + CopyFromElf(arm9filename, &entry_address, &ram_address, &size, NULL, &has_overlays, true, false); else CopyFromBin(arm9filename, 0, &size); header.arm9_entry_address = entry_address; @@ -499,7 +499,7 @@ void Create() bool has_overlays = false; if (is_arm7_elf) - CopyFromElf(arm7filename, &entry_address, &ram_address, &size, NULL, &has_overlays, false); + CopyFromElf(arm7filename, &entry_address, &ram_address, &size, NULL, &has_overlays, false, false); else CopyFromBin(arm7filename, &size); @@ -652,7 +652,7 @@ void Create() unsigned int ram_address = 0; unsigned int size = 0; - CopyFromElf(arm9filename, NULL, &ram_address, &size, NULL, NULL, true); + CopyFromElf(arm9filename, NULL, &ram_address, &size, NULL, NULL, true, true); if (!size) { sections--; @@ -675,7 +675,7 @@ void Create() unsigned int ram_address = 0; unsigned int size = 0; - CopyFromElf(arm7filename, NULL, &ram_address, &size, &mbkArm7WramMapAddress, NULL, true); + CopyFromElf(arm7filename, NULL, &ram_address, &size, &mbkArm7WramMapAddress, NULL, false, true); if (!size) { sections--; @@ -771,7 +771,7 @@ void Create() header.access_control = accessControl; header.scfg_ext_mask = scfgExtMask; header.appflags = appFlags; - header.device_list_ram_address = 0x02FFDC00; + header.device_list_ram_address = deviceListRamAddress; header.offset_0x20C = 0x00010000; header.tid_low = header.gamecode[3] | (header.gamecode[2]<<8) | (header.gamecode[1]<<16) | (header.gamecode[0]<<24); header.tid_high = titleidHigh; diff --git a/source/ndstool.cpp b/source/ndstool.cpp index 18665f3..c167e8c 100644 --- a/source/ndstool.cpp +++ b/source/ndstool.cpp @@ -57,6 +57,7 @@ unsigned int scfgExtMask = 0x80040407; // enable access to everything unsigned int accessControl = 0x00000138; unsigned int mbkArm7WramMapAddress = 0; unsigned int appFlags = 0x01; +unsigned int deviceListRamAddress = 0x03800000; /* diff --git a/source/ndstool.h b/source/ndstool.h index 593e76d..09c0dce 100644 --- a/source/ndstool.h +++ b/source/ndstool.h @@ -64,6 +64,7 @@ extern unsigned int scfgExtMask; extern unsigned int accessControl; extern unsigned int appFlags; extern unsigned int mbkArm7WramMapAddress; +extern unsigned int deviceListRamAddress; extern char *title; extern char *makercode; extern char *gamecode;