#include "../firm/headers.h"
#include "../patch_format.h"
-uint8_t *temp = (uint8_t *)FCRAM_JUNK_LOC;
+uint8_t *emunand_temp = (uint8_t *)FCRAM_JUNK_LOC;
void
verify_loop_emunand(char *filename)
uint32_t offset = get_file_sector(filename); // Get the sector of the file
// Check for RedNAND image on SD
- if (!sdmmc_sdcard_readsectors(offset, 1, temp) && *(uint32_t *)(temp + 0x100) == NCSD_MAGIC) {
+ if (!sdmmc_sdcard_readsectors(offset, 1, emunand_temp) && *(uint32_t *)(emunand_temp + 0x100) == NCSD_MAGIC) {
fprintf(stderr, "emunand: found NCSD magic\n");
} else {
abort("emunand: selected NAND image is not valid.\n");
offset = 0x200000 * index;
// Check for RedNAND/Normal physical layout on SD
- if (!sdmmc_sdcard_readsectors(offset + 1, 1, temp) && *(uint32_t *)(temp + 0x100) == NCSD_MAGIC) {
+ if (!sdmmc_sdcard_readsectors(offset + 1, 1, emunand_temp) && *(uint32_t *)(emunand_temp + 0x100) == NCSD_MAGIC) {
*off = offset + 1;
*head = offset + 1;
fprintf(stderr, "emunand: layout is normal\n");
}
// Check for GW EmuNAND on SD
- else if (!sdmmc_sdcard_readsectors(offset + nandSize, 1, temp) && *(uint32_t *)(temp + 0x100) == NCSD_MAGIC) {
+ else if (!sdmmc_sdcard_readsectors(offset + nandSize, 1, emunand_temp) && *(uint32_t *)(emunand_temp + 0x100) == NCSD_MAGIC) {
*off = offset;
*head = offset + nandSize;
const uint8_t pattern[] = { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
// Looking for the last free space before Process9
- void *ret = memfind(pos + 0x13500, size - 0x13500, pattern, 6) + 0x455;
+ uint8_t *ret = memfind(pos + 0x13500, size - 0x13500, pattern, 6) + 0x455;
if (ret) {
fprintf(stderr, "emunand: free space @ %x\n", ret);
const uint8_t pattern[] = { 0x1E, 0x00, 0xC8, 0x05 };
uint16_t *readOffset = (uint16_t *)memfind(pos, size, pattern, 4) - 3;
- uint16_t *writeOffset = (uint16_t *)memfind((uint8_t *)(readOffset + 5), 0x100, pattern, 4) - 3;
+ uint16_t *writeOffset = (uint16_t *)memfind((uint8_t*)(readOffset + 5), 0x100, pattern, 4) - 3;
- *readOffset = nandRedir[0];
+ if (!readOffset || !writeOffset)
+ abort("emunand: pattern for r/w missing!\n");
+
+ readOffset[0] = nandRedir[0];
readOffset[1] = nandRedir[1];
((uint32_t *)readOffset)[1] = branchOffset;
- *writeOffset = nandRedir[0];
+ writeOffset[0] = nandRedir[0];
writeOffset[1] = nandRedir[1];
((uint32_t *)writeOffset)[1] = branchOffset;
void
patch_emunand(uint32_t index)
{
- // uint8_t *arm9Section, uint32_t arm9SectionSize, uint8_t *process9Offset, uint32_t process9Size, uint32_t emuOffset, uint32_t emuHeader, uint32_t
- // branchAdditive)
-
// ARM9 section.
uint8_t *arm9Section = (uint8_t *)firm_loc + firm_loc->section[2].offset;
uint32_t arm9SectionSize = firm_loc->section[2].size;
fread(emuCodeOffset, 1, emunand_size, f);
fclose(f);
- uint32_t branchOffset = (uint32_t)emuCodeOffset - ((uint32_t)firm_loc + firm_loc->section[2].offset - firm_loc->section[2].address);
+ uint32_t branchOffset = (uintptr_t)emuCodeOffset - ((uintptr_t)firm_loc + firm_loc->section[2].offset - firm_loc->section[2].address);
- fprintf(stderr, "emunand: read in emunand code - %x\n", emuCodeOffset);
+ fprintf(stderr, "emunand: read in emunand code\n");
// Add the data of the found emuNAND
- uint32_t *pos_offset = (uint32_t *)memfind(emuCodeOffset, emunand_size, "NAND", 4), *pos_head = (uint32_t *)memfind(emuCodeOffset, emunand_size, "NCSD", 4),
- *pos_sdmmc = (uint32_t *)memfind(emuCodeOffset, emunand_size, "SDMC", 4);
+ uint32_t *pos_offset = (uint32_t *)memfind(emuCodeOffset, emunand_size, "NAND", 4),
+ *pos_head = (uint32_t *)memfind(emuCodeOffset, emunand_size, "NCSD", 4),
+ *pos_sdmmc = (uint32_t *)memfind(emuCodeOffset, emunand_size, "SDMC", 4);
+
+ if (!pos_offset || !pos_head || !pos_sdmmc)
+ abort("emunand: couldn't find pattern in hook?\n");
verify_emunand(index, pos_offset, pos_head);
fprintf(stderr, "emunand: nand is on sector %u\n", *pos_offset);
fprintf(stderr, "emunand: head is on sector %u\n", *pos_head);
- if (!pos_offset || !pos_head || !pos_sdmmc)
- abort("emunand: couldn't find pattern in hook?\n");
-
// Add emuNAND hooks
patchNANDRW(process9Offset, process9Size, branchOffset);