1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-23 04:12:04 +00:00
TegraExplorer/source/tegraexplorer/tools.c

249 lines
6.4 KiB
C
Raw Normal View History

#include "tools.h"
#include "gfx.h"
#include "../libs/fatfs/ff.h"
#include "../gfx/gfx.h"
#include "../utils/btn.h"
2019-12-03 09:06:11 +00:00
#include "../soc/gpio.h"
#include "../utils/util.h"
2019-12-03 16:11:08 +00:00
#include "../utils/types.h"
2019-12-04 16:47:13 +00:00
#include "../libs/fatfs/diskio.h"
2019-12-09 10:56:30 +00:00
#include "../storage/sdmmc.h"
#include "../utils/sprintf.h"
2019-12-16 22:31:29 +00:00
#include "../soc/fuse.h"
2019-12-11 12:18:57 +00:00
#include "emmc.h"
#include "fs.h"
#include "io.h"
2019-12-03 16:11:08 +00:00
extern bool sd_mount();
extern void sd_unmount();
2019-12-09 10:56:30 +00:00
extern sdmmc_storage_t sd_storage;
void displayinfo(){
clearscreen();
2019-12-11 12:18:57 +00:00
FATFS *fs;
DWORD fre_clust, fre_sect, tot_sect;
u32 capacity;
2019-12-16 22:31:29 +00:00
u8 fuse_count = 0;
pkg1_info pkg1 = returnpkg1info();
2019-12-11 12:18:57 +00:00
int res;
2019-12-16 22:31:29 +00:00
for (u32 i = 0; i < 32; i++){
if ((fuse_read_odm(7) >> i) & 1)
fuse_count++;
}
SWAPCOLOR(COLOR_ORANGE);
gfx_printf("Fuse count: %d\nPKG1 version: %d\nPKG1 id: %s\n\n", fuse_count, pkg1.ver, pkg1.id);
2019-12-11 12:18:57 +00:00
print_biskeys();
2019-12-16 22:31:29 +00:00
RESETCOLOR;
gfx_printf("\n-----\n\n");
SWAPCOLOR(COLOR_BLUE);
2019-12-11 12:18:57 +00:00
if (!sd_mount()){
gfx_printf("SD mount failed!\nFailed to display SD info\n");
}
else {
gfx_printf("Getting storage info: please wait...");
2019-12-03 16:11:08 +00:00
2019-12-11 12:18:57 +00:00
res = f_getfree("sd:", &fre_clust, &fs);
gfx_printf("\nResult getfree: %d\n\n", res);
2019-12-04 16:47:13 +00:00
2019-12-11 12:18:57 +00:00
tot_sect = (fs->n_fatent - 2) * fs->csize;
fre_sect = fre_clust * fs->csize;
capacity = sd_storage.csd.capacity;
2019-12-04 16:47:13 +00:00
2019-12-11 12:18:57 +00:00
gfx_printf("Entire sd:\nSectors: %d\nSpace total: %d MB\n\n", capacity, capacity / 2048);
gfx_printf("First partition on SD:\nSectors: %d\nSpace total: %d MB\nSpace free: %d MB\n\n", tot_sect, tot_sect / 2048, fre_sect / 2048);
}
2019-12-03 16:11:08 +00:00
2019-12-16 22:31:29 +00:00
RESETCOLOR;
2019-12-11 12:18:57 +00:00
gfx_printf("Press any key to continue");
btn_wait();
2019-12-03 09:06:11 +00:00
}
void displaygpio(){
int res;
clearscreen();
2019-12-18 15:02:45 +00:00
gfx_printf("Updates gpio pins every 50ms:\nPress power to exit");
2019-12-03 09:06:11 +00:00
msleep(200);
while (1){
msleep(10);
gfx_con_setpos(0, 63);
for (int i = 0; i <= 30; i++){
gfx_printf("\nPort %d: ", i);
for (int i2 = 7; i2 >= 0; i2--)
gfx_printf("%d", gpio_read(i, (1 << i2)));
}
res = btn_read();
if (res & BTN_POWER)
break;
}
2019-12-03 16:11:08 +00:00
}
int dumpfirmware(int mmc){
DIR dir;
FILINFO fno;
bool fail = false;
int ret, amount = 0;
char path[100] = "emmc:/Contents/registered";
char sdfolderpath[100] = "";
char syspath[100] = "";
char sdpath[100] = "";
pkg1_info pkg1 = returnpkg1info();
u32 timer = get_tmr_s();
clearscreen();
connect_mmc(mmc);
2020-01-27 22:51:59 +00:00
mount_mmc("SYSTEM", 2);
gfx_printf("PKG1 version: %d\n", pkg1.ver);
ret = f_mkdir("sd:/tegraexplorer");
gfx_printf("Creating sd:/tegraexplorer %d\n", ret);
ret = f_mkdir("sd:/tegraexplorer/Firmware");
gfx_printf("Creating sd:/tegraexplorer/Firmware %d\n", ret);
sprintf(sdfolderpath, "sd:/tegraexplorer/Firmware/%d (%s)", pkg1.ver, pkg1.id);
ret = f_mkdir(sdfolderpath);
gfx_printf("Creating %s %d\n", sdfolderpath, ret);
ret = f_opendir(&dir, path);
gfx_printf("Result opening system:/ %d\n\n%k", ret, COLOR_GREEN);
while(!f_readdir(&dir, &fno) && fno.fname[0] && !fail){
sprintf(sdpath, "%s/%s", sdfolderpath, fno.fname);
if (fno.fattrib & AM_DIR)
sprintf(syspath, "%s/%s/00", path, fno.fname);
else
sprintf(syspath, "%s/%s", path, fno.fname);
ret = copy(syspath, sdpath, false, false);
gfx_printf("%d %s\r", ++amount, fno.fname);
if (ret != 0)
fail = true;
}
if (fail)
gfx_printf("%k\n\nDump failed! Aborting (%d)", COLOR_RED, ret);
gfx_printf("%k\n\nPress any button to continue...\nTime taken: %ds", COLOR_WHITE, get_tmr_s() - timer);
btn_wait();
return fail;
}
void dumpusersaves(int mmc){
int res;
connect_mmc(mmc);
2020-01-27 22:51:59 +00:00
mount_mmc("USER", 2);
clearscreen();
res = f_mkdir("sd:/tegraexplorer");
gfx_printf("Creating sd:/tegraexplorer, res: %d\nCopying:\n", res);
SWAPCOLOR(COLOR_GREEN);
res = copy_recursive("emmc:/save", "sd:/tegraexplorer");
SWAPCOLOR(COLOR_ORANGE);
gfx_printf("\rResult copy_recursive() %d\n\n", res);
if (res){
SWAPCOLOR(COLOR_RED);
gfx_printf("Dump failed!\n");
}
else
gfx_printf("Saves are located in SD:/tegraexplorer/save\n");
gfx_printf("Press any key to continue");
btn_wait();
}
int format(int mode){
2019-12-03 16:11:08 +00:00
clearscreen();
int res;
2019-12-09 10:56:30 +00:00
bool fatalerror = false;
DWORD plist[] = {666, 61145088};
u32 timer, totalsectors, alignedsectors, extrasectors;
2019-12-03 16:11:08 +00:00
BYTE work[FF_MAX_SS];
DWORD clustsize = 32768;
2019-12-03 16:11:08 +00:00
BYTE formatoptions = 0;
formatoptions |= (FM_FAT32);
2019-12-11 12:18:57 +00:00
//formatoptions |= (FM_SFD);
2019-12-03 16:11:08 +00:00
2020-01-27 22:51:59 +00:00
disconnect_mmc();
2019-12-09 10:56:30 +00:00
timer = get_tmr_s();
2019-12-09 10:56:30 +00:00
totalsectors = sd_storage.csd.capacity;
if (mode == 0){
if (totalsectors < 83886080){
2019-12-18 15:02:45 +00:00
gfx_printf("%kYou seem to be running this on a <=32GB SD\nNot enough free space for emummc!", COLOR_RED);
fatalerror = true;
}
else {
totalsectors -= plist[1];
alignedsectors = (totalsectors / 2048) * 2048;
extrasectors = totalsectors - alignedsectors;
plist[0] = alignedsectors;
plist[1] += extrasectors;
gfx_printf("\nStarting SD partitioning:\nTotalSectors: %d\nPartition1 (SD): %d\nPartition2 (EMUMMC): %d\n", plist[0] + plist[1], plist[0], plist[1]);
}
}
else {
plist[0] = totalsectors;
plist[1] = 0;
2019-12-09 10:56:30 +00:00
}
if (!fatalerror){
gfx_printf("\nPartitioning SD...\n");
res = f_fdisk(0, plist, &work);
if (res){
gfx_printf("%kf_fdisk returned %d!\n", COLOR_RED, res);
fatalerror = true;
}
else
gfx_printf("Done!\n");
}
if (!fatalerror){
gfx_printf("\n\nFormatting Partition1...\n");
res = f_mkfs("0:", formatoptions, clustsize, &work, sizeof work);
if (res){
gfx_printf("%kf_mkfs returned %d!\n", COLOR_RED, res);
fatalerror = true;
2019-12-03 16:11:08 +00:00
}
2019-12-09 10:56:30 +00:00
else
gfx_printf("Smells like a formatted SD\n\n");
2019-12-03 16:11:08 +00:00
}
2019-12-09 10:56:30 +00:00
sd_unmount();
if (!fatalerror){
2019-12-03 16:11:08 +00:00
if (!sd_mount())
gfx_printf("%kSd failed to mount!\n", COLOR_ORANGE);
else {
gfx_printf("Sd mounted!\n");
}
}
2020-01-27 22:51:59 +00:00
connect_mmc(SYSMMC);
2019-12-03 16:11:08 +00:00
gfx_printf("\nPress any button to return%k\nTotal time taken: %ds", COLOR_WHITE, (get_tmr_s() - timer));
btn_wait();
return fatalerror;
}