Skip to content

Instantly share code, notes, and snippets.

@patois
Created June 10, 2015 22:56
Show Gist options
  • Save patois/4627f2d852ef863732c4 to your computer and use it in GitHub Desktop.
Save patois/4627f2d852ef863732c4 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "draw.h"
#include "hid.h"
#include "firm.h"
void ClearTop (void) {
ClearScreen(TOP_SCREEN0, RGB(255, 255, 255));
ClearScreen(TOP_SCREEN1, RGB(255, 255, 255));
current_y = 0;
return;
}
int patch (void *address, u8 *sig, u32 size) {
int result = 0;
if (address && sig && size) {
memcpy(address, sig, size);
result = 1;
}
return result;
}
void *find_signature (void *start_addr, void *end_addr, u8 *sig, u32 size) {
u8 *p = start_addr;
void *max_addr = 0;
if (sig &&
(u32)start_addr < (u32)end_addr &&
(u32)end_addr > size) {
max_addr = end_addr - size;
while (p < (u8 *)max_addr) {
if ((*p == *sig) && (memcmp(p, sig, size) == 0)) {
return p;
}
p++;
}
}
return 0;
}
void fatal(void) {
Debug("Signature not found! Please Turn off your 3DS");
while (1)
;
}
int main (void) {
ClearTop();
Debug("");
int sect;
u8 *sig_addr = 0;
u32 key;
u8 sig[32] = {
0x20,0xA0,0xDA,0xE5, 0x07,0xE0,0x09,0xE2,
0x1B,0x0E,0x1A,0xE1, 0x31,0xA0,0xDD,0xE5,
0xEA,0xFF,0xFF,0x0A, 0x00,0x00,0x5A,0xE3,
0x0D,0x00,0x00,0x0A, 0x00,0xE0,0xA0,0xE3};
u8 sig_patch[32] = {
0x20,0xA0,0xDA,0xE5, 0x07,0xE0,0x09,0xE2,
0x1B,0x0E,0x1A,0xE1, 0x31,0xA0,0xDD,0xE5,
0x00,0xF0,0x20,0xE3, 0x00,0x00,0x5A,0xE3,
0x00,0xF0,0x20,0xE3, 0x00,0xE0,0xA0,0xE3};
Debug("Simple memory patcher example");
Debug("=============================");
Debug("");
Debug("Finds a byte signature in memory and");
Debug("replaces it with a new signature");
Debug("");
if (is_valid_firm()) {
sect = get_section_by_address(firm->arm11_ep);
if (sect != -1) {
Debug("ARM11 entry point found in:");
dump_section_header(sect);
if (wait_key("Press START to find signature in memory") & BUTTON_START) {
sig_addr = firm->section_headers[sect].address;
while ((sig_addr = find_signature(sig_addr,
firm->section_headers[sect].address +
firm->section_headers[sect].size,
(u8 *)&sig,
sizeof(sig))) != 0) {
ClearTop();
Debug("Signature found at %08X", sig_addr);
Debug("X : Confirm patch (at your own risk)");
Debug("B : Skip/Cancel");
while (1) {
key = wait_key("");
if (key & BUTTON_X) {
patch(sig_addr, (u8 *)&sig_patch, sizeof(sig_patch));
Debug("Signature patched");
break;
} else if (key & BUTTON_B) {
break;
}
}
sig_addr++;
}
} else {
Debug("Aborted");
}
} else { /* ARM11 entry point outside of section headers */
fatal();
}
} else { /* invalid FIRM header */
fatal();
}
wait_key("Press any key to launch firm.");
// return control to FIRM ARM9 code (performs firmlaunch)
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment