Skip to content

Commit

Permalink
Update main_patch.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
masagrator authored Aug 1, 2021
1 parent a963c4d commit 8c86521
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions source/MHR/main_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@

//09 E8 A7 D2 69 66 C6 F2 69 E6 E7 F2 09 01 00 F9
constexpr uint8_t code[16] = {0x09, 0xE8, 0xA7, 0xD2, //mov x9, #0x3f400000
0x69, 0x66, 0xC6, 0xF2, //movk x9, #0x3333, lsl #32
0x69, 0xE6, 0xE7, 0xF2, //movk x9, #0x3f33, lsl #48
0x09, 0x01, 0x00, 0xF9 //str x9, [x8]
};
constexpr uint8_t handheld_divider[4] = {0x09, 0xF0, 0xA7, 0xD2}; //mov x9, #0x3f800000
constexpr uint8_t dock_divider[8] = {0x09, 0x00, 0xC0, 0xF2, //movk x9, #0x0, lsl#32
0x09, 0xF0, 0xE7, 0xF2 //movk x9, #0x3f80, #lsl48
};
0x69, 0x66, 0xC6, 0xF2, //movk x9, #0x3333, lsl #32
0x69, 0xE6, 0xE7, 0xF2, //movk x9, #0x3f33, lsl #48
0x09, 0x01, 0x00, 0xF9 //str x9, [x8]
};
constexpr uint8_t handheld_divider[4] = {0x09, 0xF0, 0xA7, 0xD2}; //mov x9, #0x3f800000
constexpr uint8_t dock_divider[8] = {0x09, 0x00, 0xC0, 0xF2, //movk x9, #0x0, lsl#32
0x09, 0xF0, 0xE7, 0xF2 //movk x9, #0x3f80, #lsl48
};

bool memcmp_f (const unsigned char *s1, const unsigned char *s2, size_t count) {
while (count-- > 0)
if (*s1++ != *s2++)
return false;
return true;
while (count-- > 0)
if (*s1++ != *s2++)
return false;
return true;
}

uint64_t getCodeSize() {
return (uint64_t)skyline::utils::getRegionAddress(skyline::utils::region::Rodata) - (uint64_t)skyline::utils::getRegionAddress(skyline::utils::region::Text);
return (uint64_t)skyline::utils::getRegionAddress(skyline::utils::region::Rodata) - (uint64_t)skyline::utils::getRegionAddress(skyline::utils::region::Text);
}

/* You must provide full instructions in arrays of uint8_t.
It returns pointer to first found code in main
or 0 if nothing have been found. */
uintptr_t findTextCode(const uint8_t* code, size_t size) {
if (size % 4 != 0) return 0;
uintptr_t addr = (uintptr_t)skyline::utils::getRegionAddress(skyline::utils::region::Text);
uint64_t addr_size = getCodeSize();
uintptr_t addr_start = addr;
while (addr != addr_start + addr_size) {
bool result = memcmp_f((const unsigned char*)addr, code, 4);
if (result) {
result = memcmp_f((const unsigned char*)addr, code, size);
if (result) return addr;
}
addr += 4;
}
return 0;
if (size % 4 != 0) return 0;
uintptr_t addr = (uintptr_t)skyline::utils::getRegionAddress(skyline::utils::region::Text);
uint64_t addr_size = getCodeSize();
uintptr_t addr_start = addr;
while (addr != addr_start + addr_size) {
bool result = memcmp_f((const unsigned char*)addr, code, 4);
if (result) {
result = memcmp_f((const unsigned char*)addr, code, size);
if (result) return addr;
}
addr += 4;
}
return 0;
}

void MHR_main() {
uintptr_t pointer = findTextCode(&code[0], sizeof(code));
if (pointer) {
sky_memcpy((void*)pointer, &handheld_divider[0], sizeof(handheld_divider));
sky_memcpy((void*)(pointer+4), &dock_divider[0], sizeof(dock_divider));
}
uintptr_t pointer = findTextCode(&code[0], sizeof(code));
if (pointer) {
sky_memcpy((void*)pointer, &handheld_divider[0], sizeof(handheld_divider));
sky_memcpy((void*)(pointer+4), &dock_divider[0], sizeof(dock_divider));
}
}

0 comments on commit 8c86521

Please sign in to comment.