Skip to content

Commit eaec91e

Browse files
committed
Support Kamekfile v3 (WriteRange command) in loader
1 parent 7db2b3b commit eaec91e

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

loader/kamekLoader.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "kamekLoader.h"
22

3-
#define KM_FILE_VERSION 2
3+
#define KM_FILE_VERSION 3
44
#define STRINGIFY_(x) #x
55
#define STRINGIFY(x) STRINGIFY_(x)
66

@@ -28,6 +28,7 @@ struct KBHeader {
2828
#define kCondWrite32 36
2929
#define kCondWrite16 37
3030
#define kCondWrite8 38
31+
#define kWriteRange 39
3132
#define kBranch 64
3233
#define kBranchLink 65
3334

@@ -48,9 +49,9 @@ static inline u32 resolveAddress(u32 text, u32 address) {
4849

4950

5051
#define kCommandHandler(name) \
51-
static inline const u8 *kHandle##name(const u8 *input, u32 text, u32 address)
52+
static inline const u8 *kHandle##name(const u8 *input, u32 text, u32 address, const loaderFunctions *funcs)
5253
#define kDispatchCommand(name) \
53-
case k##name: input = kHandle##name(input, text, address); break
54+
case k##name: input = kHandle##name(input, text, address, funcs); break
5455

5556
kCommandHandler(Addr32) {
5657
u32 target = resolveAddress(text, *(const u32 *)input);
@@ -124,13 +125,22 @@ kCommandHandler(CondWrite8) {
124125
*(u8 *)address = value & 0xFF;
125126
return input + 8;
126127
}
128+
kCommandHandler(WriteRange) {
129+
u32 size = *(const u32 *)input;
130+
// skip 1, 2, or 3 bytes to align to 4
131+
u32 startOfBuffer = (u32)input + 4 + (address & 3);
132+
funcs->memcpy((void *)address, (const void *)(startOfBuffer), (size_t)size);
133+
u32 endOfBuffer = startOfBuffer + size;
134+
// skip 1, 2, or 3 bytes to align to 4
135+
return (const u8 *)((endOfBuffer + 3) & ~3);
136+
}
127137
kCommandHandler(Branch) {
128138
*(u32 *)address = 0x48000000;
129-
return kHandleRel24(input, text, address);
139+
return kHandleRel24(input, text, address, funcs);
130140
}
131141
kCommandHandler(BranchLink) {
132142
*(u32 *)address = 0x48000001;
133-
return kHandleRel24(input, text, address);
143+
return kHandleRel24(input, text, address, funcs);
134144
}
135145

136146

@@ -207,6 +217,7 @@ void loadKamekBinary(const loaderFunctions *funcs, const void *binary, u32 binar
207217
kDispatchCommand(CondWrite32);
208218
kDispatchCommand(CondWrite16);
209219
kDispatchCommand(CondWrite8);
220+
kDispatchCommand(WriteRange);
210221
kDispatchCommand(Branch);
211222
kDispatchCommand(BranchLink);
212223
default:

loader/kamekLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef bool (*DVDFastOpen_t) (int entrynum, DVDHandle *handle);
1717
typedef int (*DVDReadPrio_t) (DVDHandle *handle, void *buffer, int length, int offset, int unk);
1818
typedef bool (*DVDClose_t) (DVDHandle *handle);
1919
typedef int (*sprintf_t) (char *str, const char *format, ...);
20+
typedef void *(*memcpy_t) (void *dest, const void *src, size_t count);
2021
typedef void *(*KamekAlloc_t) (u32 size, bool isForCode, const loaderFunctions *funcs);
2122
typedef void (*KamekFree_t) (void *buffer, bool isForCode, const loaderFunctions *funcs);
2223

@@ -29,6 +30,7 @@ struct loaderFunctions {
2930
DVDReadPrio_t DVDReadPrio;
3031
DVDClose_t DVDClose;
3132
sprintf_t sprintf;
33+
memcpy_t memcpy;
3234
KamekAlloc_t kamekAlloc;
3335
KamekFree_t kamekFree;
3436
};

loader/nsmbw.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

88
typedef void *(*EGG_Heap_alloc_t) (u32 size, s32 align, void *heap);
99
typedef void (*EGG_Heap_free_t) (void *buffer, void *heap);
10-
typedef void *(*memcpy_t) (void *dest, const void *src, size_t count);
1110
typedef void (*flush_cache_t) (void *buffer, size_t size);
1211

1312
struct loaderFunctionsEx {
1413
loaderFunctions base;
1514
EGG_Heap_alloc_t EGG_Heap_alloc;
1615
EGG_Heap_free_t EGG_Heap_free;
17-
memcpy_t memcpy;
1816
flush_cache_t __flush_cache;
1917
void **mHeap_g_gameHeaps;
2018
void **mHeap_g_archiveHeap;
@@ -50,11 +48,11 @@ const loaderFunctionsEx functions_p = {
5048
(DVDReadPrio_t) 0x801CAC60,
5149
(DVDClose_t) 0x801CAB40,
5250
(sprintf_t) 0x802E1ACC,
51+
(memcpy_t) 0x80004364,
5352
allocAdapter,
5453
freeAdapter},
5554
(EGG_Heap_alloc_t) 0x802B8E00,
5655
(EGG_Heap_free_t) 0x802B90B0,
57-
(memcpy_t) 0x80004364,
5856
(flush_cache_t) 0x80004330,
5957
(void **) 0x80377F48,
6058
(void **) 0x8042A72C,
@@ -69,11 +67,11 @@ const loaderFunctionsEx functions_e = {
6967
(DVDReadPrio_t) 0x801CAB20,
7068
(DVDClose_t) 0x801CAA00,
7169
(sprintf_t) 0x802E17DC,
70+
(memcpy_t) 0x80004364,
7271
allocAdapter,
7372
freeAdapter},
7473
(EGG_Heap_alloc_t) 0x802B8CC0,
7574
(EGG_Heap_free_t) 0x802B8F70,
76-
(memcpy_t) 0x80004364,
7775
(flush_cache_t) 0x80004330,
7876
(void **) 0x80377C48,
7977
(void **) 0x8042A44C,
@@ -89,11 +87,11 @@ const loaderFunctionsEx functions_j = {
8987
(DVDReadPrio_t) 0x801CA930,
9088
(DVDClose_t) 0x801CA810,
9189
(sprintf_t) 0x802E15EC,
90+
(memcpy_t) 0x80004364,
9291
allocAdapter,
9392
freeAdapter},
9493
(EGG_Heap_alloc_t) 0x802B8AD0,
9594
(EGG_Heap_free_t) 0x802B8D80,
96-
(memcpy_t) 0x80004364,
9795
(flush_cache_t) 0x80004330,
9896
(void **) 0x803779C8,
9997
(void **) 0x8042A16C,
@@ -108,11 +106,11 @@ const loaderFunctionsEx functions_k = {
108106
(DVDReadPrio_t) 0x801CB060,
109107
(DVDClose_t) 0x801CAF40,
110108
(sprintf_t) 0x802E1D1C,
109+
(memcpy_t) 0x80004364,
111110
allocAdapter,
112111
freeAdapter},
113112
(EGG_Heap_alloc_t) 0x802B9200,
114113
(EGG_Heap_free_t) 0x802B94B0,
115-
(memcpy_t) 0x80004364,
116114
(flush_cache_t) 0x80004330,
117115
(void **) 0x80384948,
118116
(void **) 0x804370EC,
@@ -127,11 +125,11 @@ const loaderFunctionsEx functions_w = {
127125
(DVDReadPrio_t) 0x801CB060,
128126
(DVDClose_t) 0x801CAF40,
129127
(sprintf_t) 0x802E1D1C,
128+
(memcpy_t) 0x80004364,
130129
allocAdapter,
131130
freeAdapter},
132131
(EGG_Heap_alloc_t) 0x802B9200,
133132
(EGG_Heap_free_t) 0x802B94B0,
134-
(memcpy_t) 0x80004364,
135133
(flush_cache_t) 0x80004330,
136134
(void **) 0x80382D48,
137135
(void **) 0x804354EC,
@@ -147,11 +145,11 @@ const loaderFunctionsEx functions_c = {
147145
(DVDReadPrio_t) 0x801CCE80,
148146
(DVDClose_t) 0x801CCD60,
149147
(sprintf_t) 0x802E4DF8,
148+
(memcpy_t) 0x80004364,
150149
allocAdapter,
151150
freeAdapter},
152151
(EGG_Heap_alloc_t) 0x802BB360,
153152
(EGG_Heap_free_t) 0x802BB610,
154-
(memcpy_t) 0x80004364,
155153
(flush_cache_t) 0x80004330,
156154
(void **) 0x8037D4C8,
157155
(void **) 0x8042FCCC,
@@ -254,7 +252,7 @@ void loadIntoNSMBW() {
254252

255253
// modify myBackGround_PhaseMethod to load rels earlier & load the kamek binary
256254
u32 temp[20];
257-
sFuncs->memcpy(&temp, sFuncs->myBackGround_PhaseMethod, 0x50);
255+
sFuncs->base.memcpy(&temp, sFuncs->myBackGround_PhaseMethod, 0x50);
258256

259257
// set rel loading functions as the first entries in the table
260258
sFuncs->myBackGround_PhaseMethod[0] = temp[15];
@@ -265,7 +263,7 @@ void loadIntoNSMBW() {
265263
sFuncs->myBackGround_PhaseMethod[3] = (u32)&loadBinary;
266264

267265
// set all the other functions
268-
sFuncs->memcpy(&sFuncs->myBackGround_PhaseMethod[4], &temp, 0x3C);
266+
sFuncs->base.memcpy(&sFuncs->myBackGround_PhaseMethod[4], &temp, 0x3C);
269267
sFuncs->myBackGround_PhaseMethod[19] = temp[18];
270268
sFuncs->myBackGround_PhaseMethod[20] = temp[19];
271269

0 commit comments

Comments
 (0)