decompile flash

This commit is contained in:
sceptillion 2017-12-11 19:05:47 -08:00
parent b297f50310
commit ae6d7f251e
8 changed files with 129 additions and 227 deletions

View File

@ -10188,7 +10188,7 @@ sub_8011CD8:
adds r0, r6, 0
adds r1, r5, 0
adds r2, r4, 0
bl sub_800CFC0
bl WriteFlashData
adds r1, r0, 0
b _08011D28
.align 2, 0
@ -10241,7 +10241,7 @@ sub_8011D40:
adds r0, r6, 0
adds r1, r5, 0
adds r2, r4, 0
bl sub_800CF5C
bl ReadFlashData
adds r1, r0, 0
b _08011D86
.align 2, 0
@ -10866,7 +10866,7 @@ sub_8012240:
movs r0, 0x1F
adds r1, r5, 0
adds r2, r4, 0
bl sub_800CFC0
bl WriteFlashData
adds r4, r0, 0
bl sub_8011854
adds r0, r5, 0
@ -290829,8 +290829,8 @@ sub_809C478:
bne _0809C492
ldr r0, _0809C4AC
bl sub_8011988
bl sub_800CF0C
bl sub_800CF3C
bl InitFlash
bl ClearFlashData
_0809C492:
bl sub_8035758
add r2, sp, 0x4

View File

@ -1,218 +0,0 @@
.include "constants/gba_constants.inc"
.include "asm/macros.inc"
.syntax unified
.text
thumb_func_start sub_800CF0C
sub_800CF0C:
push {lr}
bl IdentifyFlash
lsls r0, 16
cmp r0, 0
bne _0800CF32
movs r0, 0x3
bl sub_800B6E8
adds r1, r0, 0
movs r0, 0x2
bl SetFlashTimerIntr
lsls r0, 16
cmp r0, 0
bne _0800CF32
ldr r1, _0800CF38
movs r0, 0x1
strb r0, [r1]
_0800CF32:
pop {r0}
bx r0
.align 2, 0
_0800CF38: .4byte gUnknown_203B0B4
thumb_func_end sub_800CF0C
thumb_func_start sub_800CF3C
sub_800CF3C:
push {lr}
ldr r0, _0800CF54
ldrb r0, [r0]
cmp r0, 0
beq _0800CF4E
ldr r0, _0800CF58
ldr r0, [r0]
bl _call_via_r0
_0800CF4E:
pop {r0}
bx r0
.align 2, 0
_0800CF54: .4byte gUnknown_203B0B4
_0800CF58: .4byte EraseFlashChip
thumb_func_end sub_800CF3C
thumb_func_start sub_800CF5C
sub_800CF5C:
push {r4-r7,lr}
mov r7, r8
push {r7}
adds r6, r0, 0
adds r5, r2, 0
ldr r0, _0800CFAC
ldrb r0, [r0]
cmp r0, 0
beq _0800CFB4
adds r4, r1, 0
ldr r0, _0800CFB0
cmp r5, r0
ble _0800CF94
movs r7, 0x80
lsls r7, 5
mov r8, r0
_0800CF7C:
lsls r0, r6, 16
lsrs r0, 16
movs r1, 0
adds r2, r4, 0
adds r3, r7, 0
bl ReadFlash
adds r6, 0x1
adds r4, r7
subs r5, r7
cmp r5, r8
bgt _0800CF7C
_0800CF94:
cmp r5, 0
ble _0800CFA6
lsls r0, r6, 16
lsrs r0, 16
movs r1, 0
adds r2, r4, 0
adds r3, r5, 0
bl ReadFlash
_0800CFA6:
movs r0, 0
b _0800CFB6
.align 2, 0
_0800CFAC: .4byte gUnknown_203B0B4
_0800CFB0: .4byte 0x00000fff
_0800CFB4:
movs r0, 0x3
_0800CFB6:
pop {r3}
mov r8, r3
pop {r4-r7}
pop {r1}
bx r1
thumb_func_end sub_800CF5C
thumb_func_start sub_800CFC0
sub_800CFC0:
push {r4-r6,lr}
ldr r4, _0800CFD8
add sp, r4
adds r6, r0, 0
adds r5, r2, 0
ldr r0, _0800CFDC
ldrb r0, [r0]
cmp r0, 0
beq _0800D03C
movs r2, 0x1
adds r4, r1, 0
b _0800CFEC
.align 2, 0
_0800CFD8: .4byte 0xfffff000
_0800CFDC: .4byte gUnknown_203B0B4
_0800CFE0:
adds r6, 0x1
movs r0, 0x80
lsls r0, 5
adds r4, r0
ldr r0, _0800D034
adds r5, r0
_0800CFEC:
ldr r0, _0800D038
cmp r5, r0
ble _0800D002
adds r0, r6, 0
adds r1, r4, 0
bl sub_800D04C
lsls r0, 24
lsrs r2, r0, 24
cmp r2, 0
bne _0800CFE0
_0800D002:
cmp r2, 0
beq _0800D03C
cmp r5, 0
ble _0800D02C
movs r2, 0x80
lsls r2, 5
mov r0, sp
movs r1, 0xFF
bl MemoryFill8
mov r0, sp
adds r1, r4, 0
adds r2, r5, 0
bl MemoryCopy8
adds r0, r6, 0
mov r1, sp
bl sub_800D04C
lsls r0, 24
lsrs r2, r0, 24
_0800D02C:
cmp r2, 0
beq _0800D03C
movs r0, 0
b _0800D03E
.align 2, 0
_0800D034: .4byte 0xfffff000
_0800D038: .4byte 0x00000fff
_0800D03C:
movs r0, 0x3
_0800D03E:
movs r3, 0x80
lsls r3, 5
add sp, r3
pop {r4-r6}
pop {r1}
bx r1
thumb_func_end sub_800CFC0
thumb_func_start sub_800D04C
sub_800D04C:
push {r4-r7,lr}
adds r5, r1, 0
movs r6, 0x3
ldr r7, _0800D07C
lsls r0, 16
lsrs r4, r0, 16
_0800D058:
ldr r2, [r7]
adds r0, r4, 0
adds r1, r5, 0
bl _call_via_r2
lsls r0, 16
cmp r0, 0
bne _0800D080
adds r0, r4, 0
adds r1, r5, 0
movs r2, 0x80
lsls r2, 5
bl VerifyFlashSectorNBytes
cmp r0, 0
bne _0800D080
movs r0, 0x1
b _0800D08A
.align 2, 0
_0800D07C: .4byte ProgramFlashSector
_0800D080:
adds r0, r6, 0
subs r6, 0x1
cmp r0, 0
bgt _0800D058
movs r0, 0
_0800D08A:
pop {r4-r7}
pop {r1}
bx r1
thumb_func_end sub_800D04C
.align 2, 0 @ Don't pad with nop.

9
include/flash.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef GUARD_FLASH_H
#define GUARD_FLASH_H
void InitFlash(void);
void ClearFlashData(void);
s32 ReadFlashData(s32 sector, u8 *dest, s32 size);
s32 WriteFlashData(s32 sector, u8 *src, s32 size);
#endif // GUARD_FLASH_H

18
include/gba/flash.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef GUARD_GBA_FLASH_H
#define GUARD_GBA_FLASH_H
typedef void (*FlashIntrFunc)(void);
extern u16 (*ProgramFlashSector)(u16, u8 *);
extern u16 (*EraseFlashChip)(void);
extern u16 (*EraseFlashSector)(u16);
u16 IdentifyFlash(void);
u16 SetFlashTimerIntr(u8 timerNum, FlashIntrFunc *func);
void ReadFlash(u16 sectorNum, u32 offset, u8 *dest, u32 size);
u32 VerifyFlashSector(u16 sectorNum, u8 *src);
u32 VerifyFlashSectorNBytes(u16 sectorNum, u8 *src, u32 n);
u32 ProgramFlashSectorAndVerify(u16 sectorNum, u8 *src);
u32 ProgramFlashSectorAndVerifyNBytes(u16 sectorNum, u8 *src, u32 n);
#endif // GUARD_GBA_FLASH_H

View File

@ -50,7 +50,7 @@ SECTIONS {
asm/code_800AAA0.o(.text);
src/main.o(.text);
asm/code_800B540.o(.text);
asm/flash.o(.text);
src/flash.o(.text);
asm/code_800D090.o(.text);
asm/m4a.o(.text);
asm/syscall.o(.text);

93
src/flash.c Normal file
View File

@ -0,0 +1,93 @@
#include "global.h"
#include "gba/flash.h"
#include "flash.h"
extern u8 gFlashEnabled;
extern FlashIntrFunc *sub_800B6E8(s32);
extern void MemoryFill8(void *, u8, s32);
extern void MemoryCopy8(void *, void *, s32);
static u8 TryProgramSector(s32, u8 *);
void InitFlash(void)
{
if (!IdentifyFlash() && !SetFlashTimerIntr(2, sub_800B6E8(3)))
gFlashEnabled = TRUE;
}
void ClearFlashData(void)
{
if (gFlashEnabled)
EraseFlashChip();
}
s32 ReadFlashData(s32 sector, u8 *dest, s32 size)
{
if (gFlashEnabled)
{
u8 *_dest = dest;
while (size >= 0x1000)
{
ReadFlash(sector, 0, _dest, 0x1000);
sector++;
_dest += 0x1000;
size -= 0x1000;
}
if (size > 0)
ReadFlash(sector, 0, _dest, size);
return 0;
}
return 3;
}
s32 WriteFlashData(s32 sector, u8 *src, s32 size)
{
u8 buffer[0x1000];
if (gFlashEnabled)
{
u8 flag = TRUE;
u8 *_src = src;
while (size >= 0x1000)
{
flag = TryProgramSector(sector, _src);
if (!flag)
break;
sector++;
_src += 0x1000;
size -= 0x1000;
}
if (flag)
{
if (size > 0)
{
MemoryFill8(buffer, 0xFF, 0x1000);
MemoryCopy8(buffer, _src, size);
flag = TryProgramSector(sector, buffer);
}
if (flag)
return 0;
}
}
return 3;
}
static u8 TryProgramSector(s32 sector, u8 *src)
{
s32 tries = 3;
do
{
if (!ProgramFlashSector(sector, src) && !VerifyFlashSectorNBytes(sector, src, 0x1000))
return TRUE;
} while (tries-- > 0);
return FALSE;
}

View File

@ -1,5 +1,6 @@
#include "global.h"
#include "random.h"
#include "flash.h"
extern char ewram_start[];
@ -26,7 +27,6 @@ extern char unk_code[];
extern void sub_800CDA8(int);
extern void sub_800B540(void);
extern void sub_800CF0C(void);
extern void sub_8004D78(void);
extern void nullsub_9(void);
extern void nullsub_6(void);
@ -88,7 +88,7 @@ void AgbMain(void)
REG_DISPCNT = 32704;
sub_800CDA8(1);
sub_800B540();
sub_800CF0C();
InitFlash();
memcpy(seed, gUnknown_80B9BF1, 6);
SeedRng(seed);
sub_8004D78();

View File

@ -142,7 +142,7 @@ gUnknown_203B0B0: @ 203B0B0
gUnknown_203B0B2: @ 203B0B2
.space 0x2
gUnknown_203B0B4: @ 203B0B4
gFlashEnabled: @ 203B0B4
.space 0x4
gUnknown_203B0B8: @ 203B0B8