void PartitionHandle::CheckEBR(u8 PartNum, sec_t ebr_lba) { EXTENDED_BOOT_RECORD *ebr = (EXTENDED_BOOT_RECORD*)MEM2_alloc(MAX_BYTES_PER_SECTOR); if(ebr == NULL) return; sec_t next_erb_lba = 0; do { // Read and validate the extended boot record if(!interface->readSectors(ebr_lba + next_erb_lba, 1, ebr)) { MEM2_free(ebr); return; } if(ebr->signature != EBR_SIGNATURE && ebr->signature != EBR_SIGNATURE_MOD) { MEM2_free(ebr); return; } if(le32(ebr->partition.block_count) > 0 && !IsExisting(ebr_lba + next_erb_lba + le32(ebr->partition.lba_start))) { AddPartition(PartFromType(ebr->partition.type), ebr_lba + next_erb_lba + le32(ebr->partition.lba_start), le32(ebr->partition.block_count), (ebr->partition.status == PARTITION_BOOTABLE), ebr->partition.type, PartNum); } // Get the start sector of the current partition // and the next extended boot record in the chain next_erb_lba = le32(ebr->next_ebr.lba_start); } while(next_erb_lba > 0); MEM2_free(ebr); }
void BNSDecoder::OpenFile() { u8 *tempbuff = (u8*)MEM2_alloc(file_fd->size()); if(!tempbuff) { CloseFile(); return; } int done = 0; while(done < file_fd->size()) { int read = file_fd->read(tempbuff+done, file_fd->size()-done); if(read > 0) done += read; else { CloseFile(); MEM2_free(tempbuff); return; } } SoundData = DecodefromBNS(tempbuff, done); MEM2_free(tempbuff); if(SoundData.buffer == NULL) { CloseFile(); return; } Decode(); }
s8 PartitionHandle::CheckGPT(u8 PartNum) { GPT_HEADER *gpt_header = (GPT_HEADER*)MEM2_alloc(MAX_BYTES_PER_SECTOR); if(gpt_header == NULL) return -1; // Read and validate the extended boot record if(!interface->readSectors(1, 1, gpt_header)) { MEM2_free(gpt_header); return -1; } if(strncmp(gpt_header->magic, "EFI PART", 8) != 0) { MEM2_free(gpt_header); return -1; } gpt_header->part_table_lba = le64(gpt_header->part_table_lba); gpt_header->part_entries = le32(gpt_header->part_entries); gpt_header->part_entry_size = le32(gpt_header->part_entry_size); gpt_header->part_entry_checksum = le32(gpt_header->part_entry_checksum); u8 *sector_buf = (u8*)MEM2_alloc(MAX_BYTES_PER_SECTOR); if(sector_buf == NULL) { MEM2_free(gpt_header); return -1; } u64 next_lba = gpt_header->part_table_lba; for(u32 i = 0; i < gpt_header->part_entries; ++i) { if(!interface->readSectors(next_lba, 1, sector_buf)) break; for(u32 n = 0; n < BYTES_PER_SECTOR/gpt_header->part_entry_size; ++n, ++i) { GUID_PART_ENTRY *part_entry = (GUID_PART_ENTRY*)(sector_buf+gpt_header->part_entry_size*n); if(memcmp(part_entry->part_type_guid, TYPE_UNUSED, 16) == 0) continue; if(IsExisting(le64(part_entry->part_first_lba))) continue; bool bootable = (memcmp(part_entry->part_type_guid, TYPE_BIOS, 16) == 0); AddPartition("GUID-Entry", le64(part_entry->part_first_lba), le64(part_entry->part_last_lba), bootable, PARTITION_TYPE_GPT, PartNum); } next_lba++; } MEM2_free(sector_buf); MEM2_free(gpt_header); return 0; }
void GC_Disc::clear() { if(opening_bnr != NULL) { MEM2_free(opening_bnr); opening_bnr = NULL; } if(FSTable != NULL) { MEM2_free(FSTable); FSTable = NULL; } }
u8 IOS_GetType(u8 slot) { /* No more checks needed */ if(neek2o() || Sys_DolphinMode()) return IOS_TYPE_NEEK2O; /* Lets do this */ u32 TMD_Length; signed_blob *TMD_Buffer = GetTMD(slot, &TMD_Length); if(TMD_Buffer == NULL) return IOS_TYPE_STUB; tmd *iosTMD = (tmd*)SIGNATURE_PAYLOAD(TMD_Buffer); if(Title_GetSize_FromTMD(iosTMD) < 0x100000 || iosTMD->title_version == 65280) { MEM2_free(TMD_Buffer); return IOS_TYPE_STUB; } u32 title_rev = iosTMD->title_version; MEM2_free(TMD_Buffer); u8 base = 0; switch(slot) { case 222: case 223: case 224: case 225: if(title_rev == 1) return IOS_TYPE_KWIIRK; else return IOS_TYPE_HERMES; case 245: case 246: case 247: case 248: case 249: case 250: case 251: if(IOS_D2X(slot, &base)) return IOS_TYPE_D2X; else return IOS_TYPE_WANIN; default: if(IOS_D2X(slot, &base)) return IOS_TYPE_D2X; else return IOS_TYPE_NORMAL_IOS; } }
void ClearDOLList() { if(dolList) MEM2_free(dolList); dolList = NULL; dolCount = 0; }
void IOS_GetCurrentIOSInfo() { memset(&CurrentIOS, 0, sizeof(IOS_Info)); CurrentIOS.Version = IOS_GetVersion(); CurrentIOS.Base = CurrentIOS.Version; CurrentIOS.Revision = IOS_GetRevision(); CurrentIOS.SubRevision = 0; CurrentIOS.Type = IOS_GetType(CurrentIOS.Version); if(CurrentIOS.Type == IOS_TYPE_D2X) { iosinfo_t *iosInfo = IOS_GetInfo(CurrentIOS.Version); CurrentIOS.Revision = iosInfo->version; CurrentIOS.Base = iosInfo->baseios; gprintf("D2X IOS%i[%i] v%i\n", CurrentIOS.Version, CurrentIOS.Base, CurrentIOS.Revision); MEM2_free(iosInfo); } else if(CurrentIOS.Type == IOS_TYPE_WANIN) { if(CurrentIOS.Revision >= 18) CurrentIOS.Base = wanin_mload_get_IOS_base(); gprintf("Waninkoko IOS%i[%i] v%i\n", CurrentIOS.Version, CurrentIOS.Base, CurrentIOS.Revision); } else if(CurrentIOS.Type == IOS_TYPE_HERMES) { CurrentIOS.Base = mload_get_IOS_base(); if(CurrentIOS.Revision > 4) { CurrentIOS.Revision = mload_get_version() >> 4; CurrentIOS.SubRevision = mload_get_version() & 0xF; }
void CMenu::_initCodeMenu() { _addUserLabels(m_codeLblUser, ARRAY_SIZE(m_codeLblUser), "CODE"); m_codeBg = _texture("CODE/BG", "texture", theme.bg, false); m_codeLblTitle = _addLabel("CODE/CODE", theme.titleFont, L"_ _ _ _", 0, 10, 640, 60, theme.titleFontColor, FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE); m_codeBtnKey[0] = _addButton("CODE/0_BTN", theme.btnFont, L"0", 270, 320, 100, 50, theme.btnFontColor); m_codeBtnErase = _addButton("CODE/ERASE_BTN", theme.btnFont, L"", 20, 400, 200, 48, theme.btnFontColor); m_codeBtnBack = _addButton("CODE/BACK_BTN", theme.btnFont, L"", 420, 400, 200, 48, theme.btnFontColor); m_codeBtnAge = _addButton("CODE/AGE_BTN", theme.btnFont, L"", 220, 400, 200, 48, theme.btnFontColor); m_codeLblAge = _addLabel("CODE/AGE", theme.lblFont, L"", 220, 412, 200, 20, theme.lblFontColor, FTGX_JUSTIFY_CENTER | FTGX_ALIGN_MIDDLE); for (int i = 0; i < 10; ++i) { char *codeText = fmt_malloc("CODE/%i_BTN", i); if(codeText == NULL) continue; if (i > 0) { int x = i - 1; int y = x / 3; x %= 3; x = 160 + x * 110; y = 240 - y * 80; m_codeBtnKey[i] = _addButton(codeText, theme.btnFont, wfmt(L"%i", i), x, y, 100, 50, theme.btnFontColor); } _setHideAnim(m_codeBtnKey[i], codeText, 0, 0, 0.f, 0.f); MEM2_free(codeText); } _setHideAnim(m_codeBtnErase, "CODE/ERASE_BTN", 0, 0, -2.f, 0.f); _setHideAnim(m_codeBtnBack, "CODE/BACK_BTN", 0, 0, 1.f, -1.f); _setHideAnim(m_codeBtnAge, "CODE/AGE_BTN", 0, 0, -2.f, 0.f); _hideCode(true); _textCode(); }
bool PoPPatch() { if (memcmp("SPX", (char *) 0x80000000, 3) != 0 && memcmp("RPW", (char *) 0x80000000, 3) != 0) return false; WIP_Code * CodeList = MEM2_alloc(5 * sizeof(WIP_Code)); CodeList[0].offset = 0x007AAC6A; CodeList[0].srcaddress = 0x7A6B6F6A; CodeList[0].dstaddress = 0x6F6A7A6B; CodeList[1].offset = 0x007AAC75; CodeList[1].srcaddress = 0x7C7A6939; CodeList[1].dstaddress = 0x69397C7A; CodeList[2].offset = 0x007AAC82; CodeList[2].srcaddress = 0x7376686B; CodeList[2].dstaddress = 0x686B7376; CodeList[3].offset = 0x007AAC92; CodeList[3].srcaddress = 0x80717570; CodeList[3].dstaddress = 0x75708071; CodeList[4].offset = 0x007AAC9D; CodeList[4].srcaddress = 0x82806F3F; CodeList[4].dstaddress = 0x6F3F8280; if (set_wip_list(CodeList, 5) == false) { MEM2_free(CodeList); CodeList = NULL; return false; } return true; }
void PartitionHandle::AddPartition(const char *name, u64 lba_start, u64 sec_count, bool bootable, u8 part_type, u8 part_num) { u8 *buffer = (u8*)MEM2_alloc(MAX_BYTES_PER_SECTOR); if(buffer == NULL) return; if(!interface->readSectors(lba_start, 1, buffer)) { MEM2_free(buffer); return; } wbfs_head_t *head = (wbfs_head_t*)buffer; if(head->magic == wbfs_htonl(WBFS_MAGIC)) { name = "WBFS"; part_type = 0xBF; //Override partition type on WBFS //! correct sector size in physical sectors (512 bytes per sector) sec_count = (u64) head->n_hd_sec * (u64) (1 << head->hd_sec_sz_s) / (u64) BYTES_PER_SECTOR; } else if(*((u16 *)(buffer + 0x1FE)) == 0x55AA) { //! Partition type can be missleading the correct partition format. Stupid lazy ass Partition Editors. if((memcmp(buffer + 0x36, "FAT", 3) == 0 || memcmp(buffer + 0x52, "FAT", 3) == 0) && strncmp(PartFromType(part_type), "FAT", 3) != 0) { name = "FAT32"; part_type = 0x0c; } if(memcmp(buffer + 0x03, "NTFS", 4) == 0) { name = "NTFS"; part_type = 0x07; } } PartitionFS PartitionEntry; PartitionEntry.FSName = name; PartitionEntry.LBA_Start = lba_start; PartitionEntry.SecCount = sec_count; PartitionEntry.Bootable = bootable; PartitionEntry.PartitionType = part_type; PartitionEntry.PartitionNum = part_num; PartitionList.push_back(PartitionEntry); MEM2_free(buffer); }
void gfd_free(GFDFile* gfd) { if(gfd) { MEM2_free(gfd->data); free(gfd); } }
s8 PartitionHandle::FindPartitions() { MASTER_BOOT_RECORD *mbr = (MASTER_BOOT_RECORD*)MEM2_alloc(MAX_BYTES_PER_SECTOR); if(mbr == NULL) return -1; // Read the first sector on the device if(!interface->readSectors(0, 1, mbr)) { MEM2_free(mbr); return -1; } // If this is the devices master boot record if(mbr->signature != MBR_SIGNATURE && mbr->signature != MBR_SIGNATURE_MOD) { MEM2_free(mbr); return -1; } for(u8 i = 0; i < 4; i++) { PARTITION_RECORD *partition = (PARTITION_RECORD *)&mbr->partitions[i]; if(partition->type == PARTITION_TYPE_GPT) { s8 ret = CheckGPT(i); if(ret == 0) // if it's a GPT we don't need to go on looking through the mbr anymore return ret; } if(partition->type == PARTITION_TYPE_DOS33_EXTENDED || partition->type == PARTITION_TYPE_WIN95_EXTENDED) { CheckEBR(i, le32(partition->lba_start)); continue; } if(le32(partition->block_count) > 0 && !IsExisting(le32(partition->lba_start))) { AddPartition(PartFromType(partition->type), le32(partition->lba_start), le32(partition->block_count), (partition->status == PARTITION_BOOTABLE), partition->type, i); } } MEM2_free(mbr); return 0; }
void GC_Disc::init(const char *path) { strncpy(GamePath, path, MAX_FAT_PATH); opening_bnr = NULL; FSTable = NULL; FILE *f = NULL; u32 FSTSize = 0; if(strstr(GamePath, "boot.bin") != NULL) { GameType = TYPE_FST; if(strchr(GamePath, '/') != NULL) //boot.bin *strrchr(GamePath, '/') = '\0'; if(strchr(GamePath, '/') != NULL) //sys *strrchr(GamePath, '/') = '\0'; char *FstPath = fmt_malloc("%s/sys/fst.bin", GamePath); if(FstPath != NULL) { fsop_GetFileSizeBytes(FstPath, &FSTSize); f = fopen(FstPath, "rb"); MEM2_free(FstPath); } } else { GameType = TYPE_ISO; f = fopen(GamePath, "rb"); if(f == NULL) return; u8 *ReadBuffer = (u8*)MEM2_alloc(0x440); if(ReadBuffer == NULL) return; fread(ReadBuffer, 1, 0x440, f); u32 FSTOffset = *(u32*)(ReadBuffer + 0x424); FSTSize = *(u32*)(ReadBuffer + 0x428); MEM2_free(ReadBuffer); fseek(f, FSTOffset, SEEK_SET); } if(f != NULL) { Read_FST(f, FSTSize); fclose(f); } }
/* Check if the cIOS is a D2X. */ bool IOS_D2X(u8 ios, u8 *base) { iosinfo_t *info = IOS_GetInfo(ios); if(!info) return false; if(info->magicword != 0x1ee7c105 /* Magic Word */ || info->magicversion != 1 /* Magic Version */ || info->version < 6 /* Version */ || strncasecmp(info->name, "d2x", 3) != 0) /* Name */ { MEM2_free(info); return false; } *base = (u8)info->baseios; MEM2_free(info); return true; }
void GX2DestroyShader(GX2Shader *shader) { #ifndef GX2_CAN_ACCESS_DATA_SECTION MEM2_free(shader->vs.program); MEM2_free(shader->ps.program); MEM2_free(shader->gs.program); MEM2_free(shader->gs.copyProgram); org_programs_t *org = (org_programs_t *)(shader->fs.program + shader->fs.size); shader->vs.program = org->vs_program; shader->ps.program = org->ps_program; shader->gs.program = org->gs_program; shader->gs.copyProgram = org->gs_copy_program; #endif MEM2_free(shader->fs.program); shader->fs.program = NULL; }
void __wrap_free(void *p) { if(!p) return; if(((u32)p < (u32)&__init_start - 0x100 && (u32)p >= (u32)0x80004000) || ((u32)p > (u32)APPLOADER_START && (u32)p < (u32)APPLOADER_END)) MEM1_free(p); else if((u32)p & 0x10000000) MEM2_free(p); else __real_free(p); }
Mp3Decoder::~Mp3Decoder() { ExitRequested = true; while(Decoding) usleep(100); mad_synth_finish(&Synth); mad_frame_finish(&Frame); mad_stream_finish(&Stream); MEM2_free(ReadBuffer); }
signed_blob *GetTMD(u8 ios, u32 *TMD_Length) { if(ES_GetStoredTMDSize(TITLE_ID(1, ios), TMD_Length) < 0) return NULL; signed_blob *TMD = (signed_blob*)MEM2_alloc(*TMD_Length); if(TMD == NULL) return NULL; if(ES_GetStoredTMD(TITLE_ID(1, ios), TMD, *TMD_Length) < 0) { MEM2_free(TMD); return NULL; } return TMD; }
iosinfo_t *IOS_GetInfo(u8 ios) { u32 TMD_Length; signed_blob *TMD = GetTMD(ios, &TMD_Length); if(TMD == NULL) return NULL; char filepath[ISFS_MAXPATH] ATTRIBUTE_ALIGN(32); sprintf(filepath, "/title/00000001/%08x/content/%08x.app", ios, *(u8 *)((u32)TMD+0x1E7)); MEM2_free(TMD); u32 size = 0; u8 *buffer = ISFS_GetFile(filepath, &size, sizeof(iosinfo_t)); if(buffer == NULL || size == 0) return NULL; iosinfo_t *iosinfo = (iosinfo_t *)buffer; return iosinfo; }
void RegisterDOL(u8 *dst, int len) { if(!dolList) dolList = (appDOL *) MEM2_alloc(sizeof(appDOL)); appDOL *tmp = (appDOL *) MEM2_realloc(dolList, (dolCount+1)*sizeof(appDOL)); if(!tmp) { MEM2_free(dolList); dolCount = 0; return; } dolList = tmp; dolList[dolCount].dst = dst; dolList[dolCount].len = len; dolCount++; }
u8 *ISFS_GetFile(u8 *path, u32 *size, s32 length) { *size = 0; s32 fd = ISFS_Open((const char *) path, ISFS_OPEN_READ); u8 *buf = NULL; static fstats stats ATTRIBUTE_ALIGN(32); if(fd >= 0) { if(ISFS_GetFileStats(fd, &stats) >= 0) { if(length <= 0) length = stats.file_length; if(length > 0) buf = (u8 *)MEM2_memalign(32, length); if(buf) { *size = stats.file_length; if(ISFS_Read(fd, (char*)buf, length) != length) { *size = 0; MEM2_free(buf); } } } ISFS_Close(fd); } if(*size > 0) { DCFlushRange(buf, *size); ICInvalidateRange(buf, *size); } return buf; }
void *__wrap_realloc(void *p, size_t size) { void *n; // ptr from mem2 if (((u32)p & 0x10000000) != 0 || (p == 0 && size > MEM2_PRIORITY_SIZE)) { n = MEM2_realloc(p, size); if (n != 0) { return n; } n = __real_malloc(size); if (n == 0) { return 0; } if (p != 0) { memcpy(n, p, MEM2_usableSize(p) < size ? MEM2_usableSize(p) : size); MEM2_free(p); } return n; } // ptr from malloc n = __real_realloc(p, size); if (n != 0) { return n; } n = MEM2_alloc(size); if (n == 0) { return 0; } if (p != 0) { memcpy(n, p, __real_malloc_usable_size(p) < size ? __real_malloc_usable_size(p) : size); __real_free(p); } return n; }
u8 *GC_Disc::GetGameCubeBanner() { if(FSTable == NULL || GamePath == NULL) return NULL; FILE *bnr_fp = NULL; u32 BnrSize = 0; FST *fst = (FST*)FSTable; for(u32 i = 1; i < FSTEnt; ++i) { if(fst[i].Type) //Folder continue; else if(strcmp(FSTNameOff + fst[i].NameOffset, "opening.bnr") == 0) { if(GameType == TYPE_FST) bnr_fp = fopen(fmt("%s/root/opening.bnr", GamePath), "rb"); else { bnr_fp = fopen(GamePath, "rb"); if(bnr_fp != NULL) fseek(bnr_fp, fst[i].FileOffset, SEEK_SET); } BnrSize = fst[i].FileLength; break; } } if(bnr_fp != NULL) { opening_bnr = (u8*)MEM2_alloc(BnrSize); if(opening_bnr != NULL && fread(opening_bnr, 1, BnrSize, bnr_fp) != BnrSize) MEM2_free(opening_bnr); fclose(bnr_fp); } return opening_bnr; }
static void wiiu_gfx_free(void* data) { wiiu_video_t* wiiu = (wiiu_video_t*) data; if (!wiiu) return; /* clear leftover image */ GX2ClearColor(&wiiu->color_buffer, 0.0f, 0.0f, 0.0f, 1.0f); GX2CopyColorBufferToScanBuffer(&wiiu->color_buffer, GX2_SCAN_TARGET_DRC); GX2CopyColorBufferToScanBuffer(&wiiu->color_buffer, GX2_SCAN_TARGET_TV); GX2SwapScanBuffers(); GX2Flush(); GX2DrawDone(); GX2WaitForVsync(); GX2Shutdown(); GX2SetTVEnable(GX2_DISABLE); GX2SetDRCEnable(GX2_DISABLE); MEM2_free(wiiu->ctx_state); MEM2_free(wiiu->cmd_buffer); MEM2_free(wiiu->texture.surface.image); MEM2_free(wiiu->menu.texture.surface.image); MEM1_free(wiiu->color_buffer.surface.image); MEMBucket_free(wiiu->tv_scan_buffer); MEMBucket_free(wiiu->drc_scan_buffer); MEM2_free(wiiu->shader->fs.program); #ifndef GX2_CAN_ACCESS_DATA_SECTION MEM2_free(wiiu->shader->vs.program); MEM2_free(wiiu->shader->vs.attribVars); MEM2_free(wiiu->shader->ps.program); MEM2_free(wiiu->shader->ps.samplerVars); MEM2_free(wiiu->shader); #endif MEM2_free(wiiu->position); MEM2_free(wiiu->tex_coord); MEM2_free(wiiu->menu.position); MEM2_free(wiiu->menu.tex_coord); free(wiiu); }
bool NSMBPatch() { WIP_Code * CodeList = NULL; if (memcmp("SMNE01", (char *) 0x80000000, 6) == 0) { CodeList = MEM2_alloc(3 * sizeof(WIP_Code)); if(!CodeList) return false; CodeList[0].offset = 0x001AB610; CodeList[0].srcaddress = 0x9421FFD0; CodeList[0].dstaddress = 0x4E800020; CodeList[1].offset = 0x001CED53; CodeList[1].srcaddress = 0xDA000000; CodeList[1].dstaddress = 0x71000000; CodeList[2].offset = 0x001CED6B; CodeList[2].srcaddress = 0xDA000000; CodeList[2].dstaddress = 0x71000000; } else if (memcmp("SMNP01", (char *) 0x80000000, 6) == 0) { CodeList = MEM2_alloc(3 * sizeof(WIP_Code)); if(!CodeList) return false; CodeList[0].offset = 0x001AB750; CodeList[0].srcaddress = 0x9421FFD0; CodeList[0].dstaddress = 0x4E800020; CodeList[1].offset = 0x001CEE90; CodeList[1].srcaddress = 0x38A000DA; CodeList[1].dstaddress = 0x38A00071; CodeList[2].offset = 0x001CEEA8; CodeList[2].srcaddress = 0x388000DA; CodeList[2].dstaddress = 0x38800071; } else if (memcmp("SMNJ01", (char *) 0x80000000, 6) == 0) { CodeList = MEM2_alloc(3 * sizeof(WIP_Code)); if(!CodeList) return false; CodeList[0].offset = 0x001AB420; CodeList[0].srcaddress = 0x9421FFD0; CodeList[0].dstaddress = 0x4E800020; CodeList[1].offset = 0x001CEB63; CodeList[1].srcaddress = 0xDA000000; CodeList[1].dstaddress = 0x71000000; CodeList[2].offset = 0x001CEB7B; CodeList[2].srcaddress = 0xDA000000; CodeList[2].dstaddress = 0x71000000; } if (CodeList && set_wip_list(CodeList, 3) == false) { MEM2_free(CodeList); CodeList = NULL; return false; } return CodeList != NULL; }