void DumpNandPartitions(){ int isEmuNand = checkEmuNAND() ? NandSwitch() : 0; if(isEmuNand == -1) return; char* p_name[] = { "twln.bin", "twlp.bin", "agb_save.bin", "firm0.bin", "firm1.bin", "ctrnand.bin" }; unsigned int p_size[] = { 0x08FB5200, 0x020B6600, 0x00030000, 0x00400000, 0x00400000, 0x2F3E3600}; unsigned int p_addr[] = { TWLN, TWLP, AGB_SAVE, FIRM0, FIRM1, CTRNAND }; int sect_row = 0x80; ConsoleInit(); ConsoleAddText(isEmuNand ? "EmuNAND Partitions Decryptor\n \n" : "NAND Partitions Decryptor\n \n"); for(int i = 3; i < 6; i++){ //Cutting out twln, twlp and agb_save. Todo: Properly decrypt them File out; sprintf(myString, isEmuNand ? "nand/emu_%s" : "nand/%s", p_name[i]); FileOpen(&out, myString, 1); sprintf(myString, "Dumping %s ...", p_name[i]); ConsoleAddText(myString); ConsoleShow(); for(int j = 0; j*0x200 < p_size[i]; j += sect_row){ sprintf(myString, "%08X / %08X", j*0x200, p_size[i]); int x, y; ConsoleGetXY(&x, &y); y += CHAR_WIDTH * 4; x += CHAR_WIDTH*2; DrawString(TOP_SCREEN, myString, x, y, ConsoleGetTextColor(), ConsoleGetBackgroundColor()); if(isEmuNand) emunand_readsectors(j, sect_row, BUF1, p_addr[i]); else nand_readsectors(j, sect_row, BUF1, p_addr[i]); FileWrite(&out, BUF1, sect_row*0x200, j*0x200); } FileClose(&out); } ConsoleAddText("\nPress A to exit"); ConsoleShow(); WaitForButton(BUTTON_A); }
int NandSwitch(){ ConsoleInit(); ConsoleAddText("Choose the NAND you want to use\n"); ConsoleAddText("Press X : SysNAND"); ConsoleAddText("Press Y : EmuNAND"); ConsoleAddText("Press B : Back"); ConsoleShow(); while (true) { u32 pad_state = InputWait(); if(pad_state & BUTTON_X) return 0; if(pad_state & BUTTON_Y) return 1; if(pad_state & BUTTON_B) return -1; } }
void CTRDecryptor(){ ConsoleInit(); ConsoleAddText(TITLE); ConsoleShow(); int nfiles = ExploreFolders(""); char str[256]; ConsoleInit(); ConsoleAddText(TITLE); sprintf(str, "Decrypted %d files\n", nfiles); ConsoleAddText(str); ConsoleAddText("Press A to exit"); ConsoleShow(); WaitForButton(BUTTON_A); }
void print(const char *format, ...){ char str[256]; va_list va; va_start(va, format); vsnprintf(str, 256, format, va); va_end(va); ConsoleAddText(str); }
void NandDumper(){ File myFile; int isEmuNand = checkEmuNAND() ? NandSwitch() : 0; if(isEmuNand == -1) return; ConsoleInit(); ConsoleAddText(isEmuNand ? "EmuNAND Dumper\n" : "NAND Dumper\n"); unsigned char* buf = 0x21000000; unsigned int nsectors = 0x200; //sectors in a row char ProgressBar[] = "[ ]"; unsigned int progress = 1; int BACKCOLOR = ConsoleGetBackgroundColor(); if(FileOpen(&myFile, isEmuNand ? "/nand/EMUNAND.bin" : "/nand/NAND.bin", 1)){ ConsoleAddText("Dumping...\n"); ConsoleShow(); int x, y; ConsoleGetXY(&x, &y); y += CHAR_WIDTH * 6; x += CHAR_WIDTH*2; DrawString(TOP_SCREEN, ProgressBar, x, y, ConsoleGetTextColor(), ConsoleGetBackgroundColor()); DrawString(TOP_SCREEN, "Press B anytime to abort", x, y + CHAR_WIDTH*2, ConsoleGetTextColor(), ConsoleGetBackgroundColor()); for(int count = 0; count < NAND_SIZE/NAND_SECTOR_SIZE/nsectors; count++){ if(isEmuNand) sdmmc_sdcard_readsectors(count*nsectors, nsectors, buf); else sdmmc_nand_readsectors(count*nsectors, nsectors, buf); FileWrite(&myFile, buf, nsectors*NAND_SECTOR_SIZE, count*NAND_SECTOR_SIZE*nsectors); TryScreenShot(); if((count % (int)(NAND_SIZE/NAND_SECTOR_SIZE/nsectors/25)) == 0 && count != 0){ DrawString(TOP_SCREEN, "-", x+(CHAR_WIDTH*(progress++)), y, ConsoleGetTextColor(), ConsoleGetBackgroundColor()); } unsigned int pad = GetInput(); if(pad & BUTTON_B) break; } if(isEmuNand){ sdmmc_sdcard_readsectors(checkEmuNAND()/0x200, 1, buf); FileWrite(&myFile, buf, 0x200, 0); } FileClose(&myFile); ConsoleAddText("\nFinished dumping!"); ConsoleShow(); }else{ ConsoleAddText("Failed to create the dump"); ConsoleShow(); } ConsoleAddText("\nPress A to exit"); ConsoleShow(); WaitForButton(BUTTON_A); }
/* ================ Sys_Printf ================ */ void Sys_Printf (char *fmt, ...) { va_list argptr; unsigned char text[MAXCMDBUF]; unsigned char *p; if (( #ifdef _CONSOLE (int)sys_nostdout.value || #endif //_CONSOLE isdaemon) ) return; va_start (argptr, fmt); vsnprintf ((char *)text, MAXCMDBUF, fmt, argptr); va_end (argptr); // normalize text before add to console. Q_normalizetext(text); #ifndef _CONSOLE if (!isdaemon) ConsoleAddText((char *)text); #endif //_CONSOLE for (p = text; *p; p++) { #ifdef _CONSOLE if (!((int)sys_nostdout.value || isdaemon)) putc(*p, stdout); #endif //_CONSOLE } #ifdef _CONSOLE if (!((int)sys_nostdout.value || isdaemon)) fflush(stdout); #endif //_CONSOLE }
int ProcessCTR(char* path){ PartitionInfo myInfo; File myFile; char myString[256]; //In case it is needed... if(FileOpen(&myFile, path, 0)){ ConsoleInit(); ConsoleAddText(TITLE); unsigned int ncch_base = 0x100; unsigned char magic[] = { 0, 0, 0, 0, 0}; FileRead(&myFile, magic, 4, ncch_base); if(magic[0] == 'N' && magic[1] == 'C' && magic[2] == 'S' && magic[3] == 'D'){ ncch_base = 0x4000; FileRead(&myFile, magic, 4, ncch_base+0x100); if(!(magic[0] == 'N' && magic[1] == 'C' && magic[2] == 'C' && magic[3] == 'H')){ FileClose(&myFile); return 2; } }else if(magic[0] == 'N' && magic[1] == 'C' && magic[2] == 'C' && magic[3] == 'H'){ ncch_base = 0x0; }else{ FileClose(&myFile); return 2; } ctr_ncchheader NCCH; unsigned int mediaunitsize = 0x200; FileRead(&myFile, &NCCH, 0x200, ncch_base); //ConsoleAddText(path); ConsoleAddText(NCCH.productcode); unsigned int NEWCRYPTO = 0, CRYPTO = 1; if(NCCH.flags[3] != 0) NEWCRYPTO = 1; if(NCCH.flags[7] & 4) CRYPTO = 0; if(NEWCRYPTO){ ConsoleAddText("\nCryptoType : 7.X Key security"); }else if(CRYPTO){ ConsoleAddText("\nCryptoType : Secure"); }else{ ConsoleAddText("\nCryptoType : None"); ConsoleAddText("Decryption completed!"); FileClose(&myFile); ConsoleShow(); return 3; } ConsoleShow(); u8 CTR[16]; if(getle32(NCCH.extendedheadersize) > 0){ ConsoleAddText("Decrypting ExHeader..."); ConsoleShow(); ncch_get_counter(NCCH, CTR, 1); FileRead(&myFile, BUFFER_ADDR, 0x800, ncch_base + 0x200); myInfo.buffer = BUFFER_ADDR; myInfo.size = 0x800; myInfo.keyslot = 0x2C; myInfo.ctr = CTR; myInfo.keyY = NCCH.signature; DecryptPartition(&myInfo); FileWrite(&myFile, BUFFER_ADDR, 0x800, ncch_base + 0x200); } if(getle32(NCCH.exefssize) > 0){ ConsoleAddText("Decrypting ExeFS..."); ConsoleShow(); ncch_get_counter(NCCH, CTR, 2); myInfo.buffer = BUFFER_ADDR; myInfo.keyslot = NEWCRYPTO ? 0x25 : 0x2C; myInfo.ctr = CTR; myInfo.keyY = NCCH.signature; size_t bytesRead = FileRead(&myFile, BUFFER_ADDR, getle32(NCCH.exefssize) * mediaunitsize, ncch_base + getle32(NCCH.exefsoffset) * mediaunitsize); myInfo.size = bytesRead; ProcessExeFS(&myInfo); //Explanation at function definition FileWrite(&myFile, BUFFER_ADDR, getle32(NCCH.exefssize) * mediaunitsize, ncch_base + getle32(NCCH.exefsoffset) * mediaunitsize); } if(getle32(NCCH.romfssize) > 0){ ConsoleAddText("Decrypting RomFS..."); ConsoleShow(); ncch_get_counter(NCCH, CTR, 3); myInfo.buffer = BUFFER_ADDR; myInfo.keyslot = NEWCRYPTO ? 0x25 : 0x2C; myInfo.ctr = CTR; myInfo.keyY = NCCH.signature; for(int i = 0; i < getle32(NCCH.romfssize) * mediaunitsize / BLOCK_SIZE; i++){ sprintf(myString, "%i%%", (int)((i*BLOCK_SIZE)/(getle32(NCCH.romfssize) * mediaunitsize/ 100))); int x, y; ConsoleGetXY(&x, &y); y += CHAR_WIDTH * 4; x += CHAR_WIDTH*22; DrawString(TOP_SCREEN, myString, x, y, ConsoleGetTextColor(), ConsoleGetBackgroundColor()); size_t bytesRead = FileRead(&myFile, BUFFER_ADDR, BLOCK_SIZE, ncch_base + getle32(NCCH.romfsoffset) * mediaunitsize + i*BLOCK_SIZE); myInfo.size = bytesRead; DecryptPartition(&myInfo); add_ctr(myInfo.ctr, bytesRead/16); FileWrite(&myFile, BUFFER_ADDR, BLOCK_SIZE, ncch_base + getle32(NCCH.romfsoffset) * mediaunitsize + i*BLOCK_SIZE); } } NCCH.flags[7] |= 4; //Disable encryption NCCH.flags[3] = 0; //Disable 7.XKey usage FileWrite(&myFile, &NCCH, 0x200, ncch_base); if(ncch_base == 0x4000) FileWrite(&myFile, ((u8*)&NCCH) + 0x100, 0x100, 0x1100); //Only for NCSD FileClose(&myFile); ConsoleAddText("Decryption completed!"); ConsoleShow(); return 0; }else return 1; }
void vprint(const wchar_t *format, va_list va) { wchar_t str[256]; vswprintf(str, sizeof(str) / sizeof(str[0]), format, va); ConsoleAddText(str); }
BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: HEdit1 = GetDlgItem(hwndDlg, IDC_EDIT1); HEdit2 = GetDlgItem(hwndDlg, IDC_EDIT2); SetFocus(HEdit2); // SendMessage(HEdit1, EM_LIMITTEXT, 1000, 0); HEdit1_size = SendMessage(HEdit1, EM_GETLIMITTEXT, 0, 0) + 1; HEdit1_buf = (char *) Q_malloc (HEdit1_size); //Sys_Printf("%d\n", HEdit1_size); break; case WM_CTLCOLORSTATIC: if ((HWND)lParam != HEdit1) break; SetTextColor((HDC)wParam, EditBoxColor); SetBkColor((HDC)wParam, EditBoxBgColor); return (LONG)g_hbrBackground; case WM_TRAY: switch (lParam) { case 515: ShowWindow(hwndDlg,SW_RESTORE); SetForegroundWindow(hwndDlg); RemoveNotifyIcon(); break; case 516: { static DWORD id; CreateThread(NULL, 0, TrackPopup, NULL, 0, &id); break; } } break; case WM_SIZE: // we don't care until window is fully created if (DlgHwnd == NULL) break; if ((int)wParam == SIZE_MINIMIZED) { ShowWindow(hwndDlg,SW_HIDE); ShowNotifyIcon(); } break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_OK: { char str[1024]; SendMessage(HEdit2, WM_GETTEXT, (WPARAM)sizeof(str),(LPARAM)str); if (!str[0]) break; SendMessage(HEdit2, WM_SETTEXT, 0, (LPARAM)0); // normalize text before add to console. ConsoleAddText(Q_normalizetext(va("] %s\n", str))); Cbuf_AddText (str); Cbuf_AddText ("\n"); return TRUE; } case IDC_QUIT: Cbuf_AddText("quit\n"); return TRUE; case IDC_RESTORE: ShowWindow(hwndDlg,SW_RESTORE); RemoveNotifyIcon(); return TRUE; case IDC_CLEAR: SendMessage(HEdit1, WM_SETTEXT, 0, (LPARAM)0); SetFocus(HEdit2); break; } break; case WM_ACTIVATE: break; case WM_CLOSE: SV_Quit_f(); break; } return FALSE; }