mirror of
https://github.com/LNH-team/pico-loader.git
synced 2026-06-02 17:26:48 +02:00
Initial commit
This commit is contained in:
64
arm9/source/patches/platform/ak2/AK2LoaderPlatform.h
Normal file
64
arm9/source/patches/platform/ak2/AK2LoaderPlatform.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
#include "common.h"
|
||||
#include "../acekard-common/IoRPGLoaderPlatform.h"
|
||||
#include "ak2ReadSdAsm.h"
|
||||
#include "ak2SdReadSectorAsm.h"
|
||||
#include "ak2WriteSdAsm.h"
|
||||
|
||||
/// @brief Implementation of LoaderPlatform for the Acekard 2 flashcard
|
||||
class AK2LoaderPlatform : public IoRPGLoaderPlatform
|
||||
{
|
||||
private:
|
||||
enum
|
||||
{
|
||||
IORPG_CMD_SDIO_BYTE = 0xD5
|
||||
};
|
||||
|
||||
public:
|
||||
AK2LoaderPlatform() : IoRPGLoaderPlatform(IORPG_CMD_SDIO_BYTE) { }
|
||||
|
||||
const SdReadPatchCode* CreateSdReadPatchCode(
|
||||
PatchCodeCollection& patchCodeCollection, PatchHeap& patchHeap) const override
|
||||
{
|
||||
return patchCodeCollection.GetOrAddSharedPatchCode([&]
|
||||
{
|
||||
auto* waitForStatePatchCode = patchCodeCollection.GetOrAddSharedPatchCode([&]
|
||||
{
|
||||
return new ioRPGSDWaitForStatePatchCode(patchHeap);
|
||||
});
|
||||
return new AK2ReadSdPatchCode(patchHeap,
|
||||
patchCodeCollection.GetOrAddSharedPatchCode([&]
|
||||
{
|
||||
return new ioRPGSendSDIOCommandPatchCode(patchHeap);
|
||||
}),
|
||||
patchCodeCollection.GetOrAddSharedPatchCode([&]
|
||||
{
|
||||
return new AK2SDReadSectorPatchCode(patchHeap, waitForStatePatchCode);
|
||||
}),
|
||||
waitForStatePatchCode);
|
||||
});
|
||||
}
|
||||
|
||||
const SdWritePatchCode* CreateSdWritePatchCode(
|
||||
PatchCodeCollection& patchCodeCollection, PatchHeap& patchHeap) const override
|
||||
{
|
||||
return patchCodeCollection.GetOrAddSharedPatchCode([&]
|
||||
{
|
||||
return new AK2WriteSdPatchCode(patchHeap,
|
||||
patchCodeCollection.GetOrAddSharedPatchCode([&]
|
||||
{
|
||||
return new ioRPGSendSDIOCommandPatchCode(patchHeap);
|
||||
}),
|
||||
patchCodeCollection.GetOrAddSharedPatchCode([&]
|
||||
{
|
||||
return new ioRPGSDWaitForStatePatchCode(patchHeap);
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
void PatchSdscShift(void) const override
|
||||
{
|
||||
ak2_readSd_sdsc_shift = THUMB_MOVS_REG(THUMB_R4, THUMB_R0);
|
||||
ak2_writeSd_sdsc_shift = THUMB_MOVS_REG(THUMB_R7, THUMB_R0);
|
||||
}
|
||||
};
|
||||
36
arm9/source/patches/platform/ak2/ak2ReadSdAsm.h
Normal file
36
arm9/source/patches/platform/ak2/ak2ReadSdAsm.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include "sections.h"
|
||||
#include "thumbInstructions.h"
|
||||
#include "ak2SdReadSectorAsm.h"
|
||||
#include "../SdReadPatchCode.h"
|
||||
#include "../acekard-common/iorpgSendSdioCommandAsm.h"
|
||||
#include "../acekard-common/iorpgSdWaitForStateAsm.h"
|
||||
|
||||
DEFINE_SECTION_SYMBOLS(ak2_readsd);
|
||||
|
||||
extern "C" void ak2_readSd(u32 srcSector, void* dst, u32 sectorCount);
|
||||
|
||||
extern u32 ak2_readSd_sendSdioCommand_address;
|
||||
extern u32 ak2_readSd_sdReadSector_address;
|
||||
extern u32 ak2_readSd_sdWaitForState_address;
|
||||
extern u16 ak2_readSd_sdsc_shift;
|
||||
|
||||
class AK2ReadSdPatchCode : public SdReadPatchCode
|
||||
{
|
||||
public:
|
||||
explicit AK2ReadSdPatchCode(PatchHeap& patchHeap,
|
||||
const ioRPGSendSDIOCommandPatchCode* iorpgSendSdioCommandPatchCode,
|
||||
const AK2SDReadSectorPatchCode* ak2SdReadSectorPatchCode,
|
||||
const ioRPGSDWaitForStatePatchCode* iorpgSdWaitForStatePatchCode)
|
||||
: SdReadPatchCode(SECTION_START(ak2_readsd), SECTION_SIZE(ak2_readsd), patchHeap)
|
||||
{
|
||||
ak2_readSd_sendSdioCommand_address = (u32)iorpgSendSdioCommandPatchCode->GetSendSdioCommandFunction();
|
||||
ak2_readSd_sdReadSector_address = (u32)ak2SdReadSectorPatchCode->GetSDReadSectorFunction();
|
||||
ak2_readSd_sdWaitForState_address = (u32)iorpgSdWaitForStatePatchCode->GetSDWaitForStateFunction();
|
||||
}
|
||||
|
||||
const SdReadFunc GetSdReadFunction() const override
|
||||
{
|
||||
return (const SdReadFunc)GetAddressAtTarget((void*)ak2_readSd);
|
||||
}
|
||||
};
|
||||
69
arm9/source/patches/platform/ak2/ak2ReadSdAsm.s
Normal file
69
arm9/source/patches/platform/ak2/ak2ReadSdAsm.s
Normal file
@@ -0,0 +1,69 @@
|
||||
.cpu arm7tdmi
|
||||
.syntax unified
|
||||
.section "ak2_readsd", "ax"
|
||||
.thumb
|
||||
|
||||
// r0 = src sector
|
||||
// r1 = dst
|
||||
// r2 = sector count
|
||||
.global ak2_readSd
|
||||
.type ak2_readSd, %function
|
||||
ak2_readSd:
|
||||
push {r4,lr}
|
||||
|
||||
ldr r4, =0x040001A0
|
||||
movs r3, #0x80
|
||||
strb r3, [r4,#1]
|
||||
|
||||
.global ak2_readSd_sdsc_shift
|
||||
ak2_readSd_sdsc_shift:
|
||||
lsls r4, r0, #9
|
||||
|
||||
push {r0-r1}
|
||||
|
||||
ldr r0, =0x120004D5
|
||||
movs r1, r4
|
||||
ldr r3, ak2_readSd_sendSdioCommand_address
|
||||
bl blx_r3
|
||||
|
||||
// Wait for SD state
|
||||
movs r0, #4
|
||||
movs r1, #7
|
||||
ldr r3, ak2_readSd_sdWaitForState_address
|
||||
bl blx_r3
|
||||
|
||||
pop {r0-r1}
|
||||
|
||||
// Read sectors. Parameters identical to readSd
|
||||
ldr r3, ak2_readSd_sdReadSector_address
|
||||
bl blx_r3
|
||||
|
||||
// Send CMD12 == STOP_TRANSMISSION
|
||||
push {r0-r1}
|
||||
ldr r0, =0x0C0001D5
|
||||
movs r1, #0
|
||||
ldr r3, ak2_readSd_sendSdioCommand_address
|
||||
bl blx_r3
|
||||
|
||||
pop {r0-r1,r4,pc}
|
||||
|
||||
blx_r3:
|
||||
bx r3
|
||||
|
||||
.balign 4
|
||||
|
||||
.global ak2_readSd_sendSdioCommand_address
|
||||
ak2_readSd_sendSdioCommand_address:
|
||||
.word 0
|
||||
|
||||
.global ak2_readSd_sdReadSector_address
|
||||
ak2_readSd_sdReadSector_address:
|
||||
.word 0
|
||||
|
||||
.global ak2_readSd_sdWaitForState_address
|
||||
ak2_readSd_sdWaitForState_address:
|
||||
.word 0
|
||||
|
||||
.pool
|
||||
|
||||
.end
|
||||
26
arm9/source/patches/platform/ak2/ak2SdReadSectorAsm.h
Normal file
26
arm9/source/patches/platform/ak2/ak2SdReadSectorAsm.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
#include "sections.h"
|
||||
#include "thumbInstructions.h"
|
||||
#include "../acekard-common/iorpgSdWaitForStateAsm.h"
|
||||
|
||||
DEFINE_SECTION_SYMBOLS(ak2_sdreadsector);
|
||||
|
||||
extern "C" void ak2_sdReadSector(u32 srcSector, void* dst, u32 sectorCount);
|
||||
|
||||
extern u32 ak2_sdReadSector_sdWaitForState_address;
|
||||
|
||||
class AK2SDReadSectorPatchCode : public PatchCode
|
||||
{
|
||||
public:
|
||||
explicit AK2SDReadSectorPatchCode(PatchHeap& patchHeap,
|
||||
const ioRPGSDWaitForStatePatchCode* iorpgSdWaitForStatePatchCode)
|
||||
: PatchCode(SECTION_START(ak2_sdreadsector), SECTION_SIZE(ak2_sdreadsector), patchHeap)
|
||||
{
|
||||
ak2_sdReadSector_sdWaitForState_address = (u32)iorpgSdWaitForStatePatchCode->GetSDWaitForStateFunction();
|
||||
}
|
||||
|
||||
const void* GetSDReadSectorFunction() const
|
||||
{
|
||||
return GetAddressAtTarget((void*)ak2_sdReadSector);
|
||||
}
|
||||
};
|
||||
67
arm9/source/patches/platform/ak2/ak2SdReadSectorAsm.s
Normal file
67
arm9/source/patches/platform/ak2/ak2SdReadSectorAsm.s
Normal file
@@ -0,0 +1,67 @@
|
||||
.cpu arm7tdmi
|
||||
.syntax unified
|
||||
.section "ak2_sdreadsector", "ax"
|
||||
.thumb
|
||||
|
||||
// r0 = src sector
|
||||
// r1 = dst
|
||||
// r2 = sector count
|
||||
.global ak2_sdReadSector
|
||||
.type ak2_sdReadSector, %function
|
||||
ak2_sdReadSector:
|
||||
push {r4-r5,lr}
|
||||
|
||||
ldr r4, =0x040001A0
|
||||
ldr r5, =0x04100010
|
||||
|
||||
sector_loop:
|
||||
// NOW we can start reading things
|
||||
movs r3, #0xB7
|
||||
str r3, [r4,#0x8]
|
||||
// The full CMD is B7 00 00 00 00 13 00 00
|
||||
// If we use 0x1300 and str, then it goes into the expected place
|
||||
movs r3, #0x13
|
||||
lsls r3, r3, #8
|
||||
str r3, [r4,#0xC]
|
||||
|
||||
ldr r3, =0xA1406004
|
||||
str r3, [r4,#4]
|
||||
|
||||
ak2_sdReadSector_read_loop:
|
||||
ldrb r3, [r4,#6]
|
||||
lsrs r3, r3, #8 // check if data is ready
|
||||
bcc ak2_sdReadSector_read_loop_check_transfer_end // if not skip reading
|
||||
|
||||
ldr r3, [r5]
|
||||
stmia r1!, {r3}
|
||||
|
||||
ak2_sdReadSector_read_loop_check_transfer_end:
|
||||
ldrb r3, [r4,#7]
|
||||
lsrs r3, r3, #8 // check if transfer is done
|
||||
bcs ak2_sdReadSector_read_loop
|
||||
|
||||
// Wait for SD state
|
||||
push {r0-r1}
|
||||
movs r0, #4
|
||||
movs r1, #7
|
||||
ldr r3, ak2_sdReadSector_sdWaitForState_address
|
||||
bl blx_r3
|
||||
pop {r0-r1}
|
||||
|
||||
subs r2, #1
|
||||
bne sector_loop
|
||||
|
||||
pop {r4-r5,pc}
|
||||
|
||||
blx_r3:
|
||||
bx r3
|
||||
|
||||
.balign 4
|
||||
|
||||
.global ak2_sdReadSector_sdWaitForState_address
|
||||
ak2_sdReadSector_sdWaitForState_address:
|
||||
.word 0
|
||||
|
||||
.pool
|
||||
|
||||
.end
|
||||
32
arm9/source/patches/platform/ak2/ak2WriteSdAsm.h
Normal file
32
arm9/source/patches/platform/ak2/ak2WriteSdAsm.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "sections.h"
|
||||
#include "thumbInstructions.h"
|
||||
#include "../SdWritePatchCode.h"
|
||||
#include "../acekard-common/iorpgSendSdioCommandAsm.h"
|
||||
#include "../acekard-common/iorpgSdWaitForStateAsm.h"
|
||||
|
||||
DEFINE_SECTION_SYMBOLS(ak2_writesd);
|
||||
|
||||
extern "C" void ak2_writeSd(u32 srcSector, void* dst, u32 sectorCount);
|
||||
|
||||
extern u32 ak2_writeSd_sendSdioCommand_address;
|
||||
extern u32 ak2_writeSd_sdWaitForState_address;
|
||||
extern u16 ak2_writeSd_sdsc_shift;
|
||||
|
||||
class AK2WriteSdPatchCode : public SdWritePatchCode
|
||||
{
|
||||
public:
|
||||
explicit AK2WriteSdPatchCode(PatchHeap& patchHeap,
|
||||
const ioRPGSendSDIOCommandPatchCode* iorpgSendSdioCommandPatchCode,
|
||||
const ioRPGSDWaitForStatePatchCode* iorpgSdWaitForStatePatchCode)
|
||||
: SdWritePatchCode(SECTION_START(ak2_writesd), SECTION_SIZE(ak2_writesd), patchHeap)
|
||||
{
|
||||
ak2_writeSd_sendSdioCommand_address = (u32)iorpgSendSdioCommandPatchCode->GetSendSdioCommandFunction();
|
||||
ak2_writeSd_sdWaitForState_address = (u32)iorpgSdWaitForStatePatchCode->GetSDWaitForStateFunction();
|
||||
}
|
||||
|
||||
const SdWriteFunc GetSdWriteFunction() const override
|
||||
{
|
||||
return (const SdWriteFunc)GetAddressAtTarget((void*)ak2_writeSd);
|
||||
}
|
||||
};
|
||||
81
arm9/source/patches/platform/ak2/ak2WriteSdAsm.s
Normal file
81
arm9/source/patches/platform/ak2/ak2WriteSdAsm.s
Normal file
@@ -0,0 +1,81 @@
|
||||
.cpu arm7tdmi
|
||||
.syntax unified
|
||||
.section "ak2_writesd", "ax"
|
||||
.thumb
|
||||
|
||||
// r0 = dst sector
|
||||
// r1 = src
|
||||
// r2 = sector count
|
||||
.global ak2_writeSd
|
||||
.type ak2_writeSd, %function
|
||||
ak2_writeSd:
|
||||
push {r4-r7,lr}
|
||||
|
||||
ldr r4, =0x040001A0
|
||||
movs r3, #0x80
|
||||
strb r3, [r4,#1]
|
||||
|
||||
sector_loop:
|
||||
.global ak2_writeSd_sdsc_shift
|
||||
ak2_writeSd_sdsc_shift:
|
||||
lsls r7, r0, #9
|
||||
|
||||
push {r0-r1}
|
||||
// SDIO cmd
|
||||
ldr r0, =0x180005D5
|
||||
// param
|
||||
movs r1, r7
|
||||
ldr r6, ak2_writeSd_sendSdioCommand_address
|
||||
bl blx_r6
|
||||
pop {r0-r1}
|
||||
|
||||
// NOW we can start writing things
|
||||
movs r7, #1
|
||||
lsls r7, r7, #9
|
||||
ak2_writeSd_block_write_loop:
|
||||
// start copy to MCCMD1
|
||||
ldmia r1!, {r5, r6}
|
||||
str r5, [r4, #0x8]
|
||||
str r6, [r4, #0xc]
|
||||
|
||||
ldr r3, =0xA0406000
|
||||
str r3, [r4,#4]
|
||||
|
||||
ak2_writeSd_block_write_wait_busy:
|
||||
ldr r3, [r4,#4]
|
||||
cmp r3, #0
|
||||
blt ak2_writeSd_block_write_wait_busy
|
||||
|
||||
subs r7, r7, #8
|
||||
bne ak2_writeSd_block_write_loop
|
||||
|
||||
push {r0-r1}
|
||||
// Wait for SD state
|
||||
movs r0, #4
|
||||
movs r1, #0
|
||||
ldr r6, ak2_writeSd_sdWaitForState_address
|
||||
bl blx_r6
|
||||
pop {r0-r1}
|
||||
|
||||
adds r0, #1
|
||||
subs r2, #1
|
||||
bne sector_loop
|
||||
|
||||
pop {r4-r7,pc}
|
||||
|
||||
blx_r6:
|
||||
bx r6
|
||||
|
||||
.balign 4
|
||||
|
||||
.global ak2_writeSd_sendSdioCommand_address
|
||||
ak2_writeSd_sendSdioCommand_address:
|
||||
.word 0
|
||||
|
||||
.global ak2_writeSd_sdWaitForState_address
|
||||
ak2_writeSd_sdWaitForState_address:
|
||||
.word 0
|
||||
|
||||
.pool
|
||||
|
||||
.end
|
||||
Reference in New Issue
Block a user