2019-06-30 01:03:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 naehrwert
|
|
|
|
* Copyright (c) 2018 Rajko Stojadinovic
|
2022-01-20 11:15:04 +00:00
|
|
|
* Copyright (c) 2018-2022 CTCaer
|
2019-06-30 01:03:00 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! fix the dram stuff and the pop ups
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-01-15 22:04:34 +00:00
|
|
|
#include <bdk.h>
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
#include "gui.h"
|
|
|
|
#include "fe_emummc_tools.h"
|
2020-06-14 13:45:45 +00:00
|
|
|
#include "../config.h"
|
2021-02-06 15:19:37 +00:00
|
|
|
#include <libs/fatfs/diskio.h>
|
2020-06-14 13:45:45 +00:00
|
|
|
#include <libs/fatfs/ff.h>
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
#define OUT_FILENAME_SZ 128
|
2021-08-28 15:09:34 +00:00
|
|
|
#define NAND_PATROL_SECTOR 0xC20
|
|
|
|
#define NUM_SECTORS_PER_ITER 8192 // 4MB Cache.
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
extern hekate_config h_cfg;
|
2021-02-06 15:19:37 +00:00
|
|
|
extern volatile boot_cfg_t *b_cfg;
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2020-04-27 06:18:31 +00:00
|
|
|
void load_emummc_cfg(emummc_cfg_t *emu_info)
|
|
|
|
{
|
|
|
|
memset(emu_info, 0, sizeof(emummc_cfg_t));
|
|
|
|
|
|
|
|
// Parse emuMMC configuration.
|
|
|
|
LIST_INIT(ini_sections);
|
2022-01-28 23:23:40 +00:00
|
|
|
if (!ini_parse(&ini_sections, "emuMMC/emummc.ini", false))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LIST_FOREACH_ENTRY(ini_sec_t, ini_sec, &ini_sections, link)
|
2020-04-27 06:18:31 +00:00
|
|
|
{
|
2022-01-28 23:23:40 +00:00
|
|
|
if (!strcmp(ini_sec->name, "emummc"))
|
2020-04-27 06:18:31 +00:00
|
|
|
{
|
2022-01-28 23:23:40 +00:00
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &ini_sec->kvs, link)
|
2020-04-27 06:18:31 +00:00
|
|
|
{
|
2022-10-11 01:37:17 +00:00
|
|
|
if (!strcmp("enabled", kv->key))
|
2022-01-28 23:23:40 +00:00
|
|
|
emu_info->enabled = atoi(kv->val);
|
|
|
|
else if (!strcmp("sector", kv->key))
|
|
|
|
emu_info->sector = strtol(kv->val, NULL, 16);
|
2022-10-11 01:37:17 +00:00
|
|
|
else if (!strcmp("id", kv->key))
|
|
|
|
emu_info->id = strtol(kv->val, NULL, 16);
|
|
|
|
else if (!strcmp("path", kv->key))
|
|
|
|
{
|
|
|
|
emu_info->path = (char *)malloc(strlen(kv->val) + 1);
|
|
|
|
strcpy(emu_info->path, kv->val);
|
|
|
|
}
|
2022-01-28 23:23:40 +00:00
|
|
|
else if (!strcmp("nintendo_path", kv->key))
|
2022-10-11 01:37:17 +00:00
|
|
|
{
|
|
|
|
emu_info->nintendo_path = (char *)malloc(strlen(kv->val) + 1);
|
|
|
|
strcpy(emu_info->nintendo_path, kv->val);
|
|
|
|
}
|
2020-04-27 06:18:31 +00:00
|
|
|
}
|
2022-01-28 23:23:40 +00:00
|
|
|
|
|
|
|
break;
|
2020-04-27 06:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-19 12:14:05 +00:00
|
|
|
|
|
|
|
ini_free(&ini_sections);
|
2020-04-27 06:18:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
void save_emummc_cfg(u32 part_idx, u32 sector_start, const char *path)
|
|
|
|
{
|
|
|
|
sd_mount();
|
|
|
|
|
|
|
|
char lbuf[16];
|
|
|
|
FIL fp;
|
|
|
|
|
|
|
|
if (f_open(&fp, "emuMMC/emummc.ini", FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add config entry.
|
|
|
|
f_puts("[emummc]\nenabled=", &fp);
|
|
|
|
if (part_idx && sector_start)
|
|
|
|
{
|
|
|
|
itoa(part_idx, lbuf, 10);
|
|
|
|
f_puts(lbuf, &fp);
|
|
|
|
}
|
|
|
|
else if(path)
|
|
|
|
f_puts("1", &fp);
|
|
|
|
else
|
|
|
|
f_puts("0", &fp);
|
|
|
|
|
|
|
|
if (!sector_start)
|
|
|
|
f_puts("\nsector=0x0", &fp);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
f_puts("\nsector=0x", &fp);
|
|
|
|
itoa(sector_start, lbuf, 16);
|
|
|
|
f_puts(lbuf, &fp);
|
|
|
|
}
|
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
f_puts("\npath=", &fp);
|
|
|
|
f_puts(path, &fp);
|
|
|
|
}
|
2021-09-17 20:35:13 +00:00
|
|
|
|
|
|
|
// Get ID from path.
|
|
|
|
u32 id_from_path = 0;
|
2021-09-26 09:15:25 +00:00
|
|
|
if (path && strlen(path) >= 4)
|
2021-09-17 20:35:13 +00:00
|
|
|
memcpy(&id_from_path, path + strlen(path) - 4, 4);
|
|
|
|
f_puts("\nid=0x", &fp);
|
|
|
|
itoa(id_from_path, lbuf, 16);
|
|
|
|
f_puts(lbuf, &fp);
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
f_puts("\nnintendo_path=", &fp);
|
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
f_puts(path, &fp);
|
|
|
|
f_puts("/Nintendo\n", &fp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
f_puts("\n", &fp);
|
|
|
|
|
|
|
|
f_close(&fp);
|
|
|
|
}
|
|
|
|
|
2020-06-14 09:52:54 +00:00
|
|
|
void update_emummc_base_folder(char *outFilename, u32 sdPathLen, u32 currPartIdx)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
if (currPartIdx < 10)
|
|
|
|
{
|
|
|
|
outFilename[sdPathLen] = '0';
|
|
|
|
itoa(currPartIdx, &outFilename[sdPathLen + 1], 10);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
itoa(currPartIdx, &outFilename[sdPathLen], 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _dump_emummc_file_part(emmc_tool_gui_t *gui, char *sd_path, sdmmc_storage_t *storage, emmc_part_t *part)
|
|
|
|
{
|
|
|
|
static const u32 FAT32_FILESIZE_LIMIT = 0xFFFFFFFF;
|
|
|
|
static const u32 SECTORS_TO_MIB_COEFF = 11;
|
|
|
|
|
|
|
|
u32 multipartSplitSize = 0xFE000000;
|
|
|
|
u32 totalSectors = part->lba_end - part->lba_start + 1;
|
|
|
|
u32 currPartIdx = 0;
|
|
|
|
u32 numSplitParts = 0;
|
|
|
|
int res = 0;
|
|
|
|
char *outFilename = sd_path;
|
|
|
|
u32 sdPathLen = strlen(sd_path);
|
|
|
|
|
2022-10-11 03:18:02 +00:00
|
|
|
s_printf(gui->txt_buf, "#96FF00 SD Card free space:# %d MiB\n#96FF00 Total size:# %d MiB\n\n",
|
2022-05-12 13:43:18 +00:00
|
|
|
(u32)(sd_fs.free_clst * sd_fs.csize >> SECTORS_TO_MIB_COEFF),
|
2019-06-30 01:03:00 +00:00
|
|
|
totalSectors >> SECTORS_TO_MIB_COEFF);
|
|
|
|
lv_label_ins_text(gui->label_info, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
lv_bar_set_value(gui->bar, 0);
|
|
|
|
lv_label_set_text(gui->label_pct, " "SYMBOL_DOT" 0%");
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
// Check if the USER partition or the RAW eMMC fits the sd card free space.
|
|
|
|
if (totalSectors > (sd_fs.free_clst * sd_fs.csize))
|
|
|
|
{
|
2022-10-11 03:18:02 +00:00
|
|
|
s_printf(gui->txt_buf, "\n#FFDD00 Not enough free space for file based emuMMC!#\n");
|
2019-06-30 01:03:00 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-11 03:18:02 +00:00
|
|
|
// Check if filesystem is FAT32 or the free space is smaller and dump in parts.
|
2022-01-20 11:15:04 +00:00
|
|
|
if (totalSectors > (FAT32_FILESIZE_LIMIT / EMMC_BLOCKSIZE))
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
2022-01-20 11:15:04 +00:00
|
|
|
u32 multipartSplitSectors = multipartSplitSize / EMMC_BLOCKSIZE;
|
2019-06-30 01:03:00 +00:00
|
|
|
numSplitParts = (totalSectors + multipartSplitSectors - 1) / multipartSplitSectors;
|
|
|
|
|
2022-10-11 03:18:02 +00:00
|
|
|
// Get first part filename.
|
2020-06-14 09:52:54 +00:00
|
|
|
update_emummc_base_folder(outFilename, sdPathLen, 0);
|
2019-06-30 01:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FIL fp;
|
|
|
|
s_printf(gui->txt_buf, "#96FF00 Filepath:#\n%s\n#96FF00 Filename:# #FF8000 %s#",
|
|
|
|
gui->base_path, outFilename + strlen(gui->base_path));
|
|
|
|
lv_label_ins_text(gui->label_info, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
2019-10-18 15:02:06 +00:00
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
res = f_open(&fp, outFilename, FA_CREATE_ALWAYS | FA_WRITE);
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf, "\n#FF0000 Error (%d) while creating#\n#FFDD00 %s#\n", res, outFilename);
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 *buf = (u8 *)MIXD_BUF_ALIGNED;
|
|
|
|
|
|
|
|
u32 lba_curr = part->lba_start;
|
|
|
|
u32 bytesWritten = 0;
|
|
|
|
u32 prevPct = 200;
|
|
|
|
int retryCount = 0;
|
|
|
|
DWORD *clmt = NULL;
|
|
|
|
|
|
|
|
u64 totalSize = (u64)((u64)totalSectors << 9);
|
|
|
|
if (totalSize <= FAT32_FILESIZE_LIMIT)
|
2021-10-01 12:45:25 +00:00
|
|
|
clmt = f_expand_cltbl(&fp, SZ_4M, totalSize);
|
2019-06-30 01:03:00 +00:00
|
|
|
else
|
2021-10-01 12:45:25 +00:00
|
|
|
clmt = f_expand_cltbl(&fp, SZ_4M, MIN(totalSize, multipartSplitSize));
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
u32 num = 0;
|
|
|
|
u32 pct = 0;
|
|
|
|
|
|
|
|
lv_obj_set_opa_scale(gui->bar, LV_OPA_COVER);
|
|
|
|
lv_obj_set_opa_scale(gui->label_pct, LV_OPA_COVER);
|
|
|
|
while (totalSectors > 0)
|
|
|
|
{
|
|
|
|
if (numSplitParts != 0 && bytesWritten >= multipartSplitSize)
|
|
|
|
{
|
|
|
|
f_close(&fp);
|
|
|
|
free(clmt);
|
|
|
|
memset(&fp, 0, sizeof(fp));
|
|
|
|
currPartIdx++;
|
|
|
|
|
2020-06-14 09:52:54 +00:00
|
|
|
update_emummc_base_folder(outFilename, sdPathLen, currPartIdx);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
// Create next part.
|
|
|
|
s_printf(gui->txt_buf, "%s#", outFilename + strlen(gui->base_path));
|
|
|
|
lv_label_cut_text(gui->label_info, strlen(lv_label_get_text(gui->label_info)) - 3, 3);
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_ins_text(gui->label_info, LV_LABEL_POS_LAST, gui->txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
res = f_open(&fp, outFilename, FA_CREATE_ALWAYS | FA_WRITE);
|
|
|
|
if (res)
|
|
|
|
{
|
2021-02-06 02:46:10 +00:00
|
|
|
s_printf(gui->txt_buf, "\n#FF0000 Error (%d) while creating#\n#FFDD00 %s#\n", res, outFilename);
|
2019-06-30 01:03:00 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesWritten = 0;
|
|
|
|
|
|
|
|
totalSize = (u64)((u64)totalSectors << 9);
|
2021-10-01 12:45:25 +00:00
|
|
|
clmt = f_expand_cltbl(&fp, SZ_4M, MIN(totalSize, multipartSplitSize));
|
2019-06-30 01:03:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 20:27:17 +00:00
|
|
|
// Check for cancellation combo.
|
2020-04-14 14:51:42 +00:00
|
|
|
if (btn_read_vol() == (BTN_VOL_UP | BTN_VOL_DOWN))
|
2020-03-21 20:27:17 +00:00
|
|
|
{
|
2021-02-06 02:46:10 +00:00
|
|
|
s_printf(gui->txt_buf, "\n#FFDD00 The emuMMC was cancelled!#\n");
|
2020-03-21 20:27:17 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
f_close(&fp);
|
|
|
|
free(clmt);
|
|
|
|
f_unlink(outFilename);
|
|
|
|
|
|
|
|
msleep(1000);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
retryCount = 0;
|
|
|
|
num = MIN(totalSectors, NUM_SECTORS_PER_ITER);
|
2022-01-28 23:23:40 +00:00
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
while (!sdmmc_storage_read(storage, lba_curr, num, buf))
|
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf,
|
2021-02-06 02:46:10 +00:00
|
|
|
"\n#FFDD00 Error reading %d blocks @ LBA %08X,#\n"
|
2019-06-30 01:03:00 +00:00
|
|
|
"#FFDD00 from eMMC (try %d). #",
|
|
|
|
num, lba_curr, ++retryCount);
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
msleep(150);
|
|
|
|
if (retryCount >= 3)
|
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf, "#FF0000 Aborting...#\nPlease try again...\n");
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
f_close(&fp);
|
|
|
|
free(clmt);
|
|
|
|
f_unlink(outFilename);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-06 02:46:10 +00:00
|
|
|
s_printf(gui->txt_buf, "#FFDD00 Retrying...#");
|
2019-06-30 01:03:00 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 06:37:41 +00:00
|
|
|
|
|
|
|
manual_system_maintenance(false);
|
|
|
|
|
2022-01-20 11:15:04 +00:00
|
|
|
res = f_write_fast(&fp, buf, EMMC_BLOCKSIZE * num);
|
2019-10-18 15:02:06 +00:00
|
|
|
|
2020-03-09 06:37:41 +00:00
|
|
|
manual_system_maintenance(false);
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
if (res)
|
|
|
|
{
|
2019-07-06 19:08:37 +00:00
|
|
|
s_printf(gui->txt_buf, "\n#FF0000 Fatal error (%d) when writing to SD Card#\nPlease try again...\n", res);
|
2019-06-30 01:03:00 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
f_close(&fp);
|
|
|
|
free(clmt);
|
|
|
|
f_unlink(outFilename);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pct = (u64)((u64)(lba_curr - part->lba_start) * 100u) / (u64)(part->lba_end - part->lba_start);
|
|
|
|
if (pct != prevPct)
|
|
|
|
{
|
|
|
|
lv_bar_set_value(gui->bar, pct);
|
|
|
|
s_printf(gui->txt_buf, " "SYMBOL_DOT" %d%%", pct);
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_set_text(gui->label_pct, gui->txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
prevPct = pct;
|
|
|
|
}
|
|
|
|
|
|
|
|
lba_curr += num;
|
|
|
|
totalSectors -= num;
|
2022-01-20 11:15:04 +00:00
|
|
|
bytesWritten += num * EMMC_BLOCKSIZE;
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
// Force a flush after a lot of data if not splitting.
|
|
|
|
if (numSplitParts == 0 && bytesWritten >= multipartSplitSize)
|
|
|
|
{
|
|
|
|
f_sync(&fp);
|
|
|
|
bytesWritten = 0;
|
|
|
|
}
|
2020-03-09 06:37:41 +00:00
|
|
|
|
|
|
|
manual_system_maintenance(false);
|
2019-06-30 01:03:00 +00:00
|
|
|
}
|
|
|
|
lv_bar_set_value(gui->bar, 100);
|
|
|
|
lv_label_set_text(gui->label_pct, " "SYMBOL_DOT" 100%");
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
2022-10-11 03:18:02 +00:00
|
|
|
// Operation ended successfully.
|
2019-06-30 01:03:00 +00:00
|
|
|
f_close(&fp);
|
|
|
|
free(clmt);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump_emummc_file(emmc_tool_gui_t *gui)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
int base_len = 0;
|
|
|
|
u32 timer = 0;
|
|
|
|
|
2021-10-01 12:45:25 +00:00
|
|
|
char *txt_buf = (char *)malloc(SZ_16K);
|
2021-02-06 01:19:42 +00:00
|
|
|
gui->base_path = (char *)malloc(OUT_FILENAME_SZ);
|
2019-06-30 01:03:00 +00:00
|
|
|
gui->txt_buf = txt_buf;
|
2020-03-09 06:37:41 +00:00
|
|
|
|
2022-05-12 13:43:18 +00:00
|
|
|
txt_buf[0] = 0;
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_set_text(gui->label_log, txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2019-07-06 19:08:37 +00:00
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
if (!sd_mount())
|
|
|
|
{
|
2020-04-27 05:47:00 +00:00
|
|
|
lv_label_set_text(gui->label_info, "#FFDD00 Failed to init SD!#");
|
2019-06-30 01:03:00 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-04-27 05:47:00 +00:00
|
|
|
lv_label_set_text(gui->label_info, "Checking for available free space...");
|
2019-06-30 01:03:00 +00:00
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
2022-10-11 03:18:02 +00:00
|
|
|
// Get SD Card free space for file based emuMMC.
|
2019-06-30 01:03:00 +00:00
|
|
|
f_getfree("", &sd_fs.free_clst, NULL);
|
|
|
|
|
2022-01-20 11:15:04 +00:00
|
|
|
if (!emmc_initialize(false))
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
2020-04-27 05:47:00 +00:00
|
|
|
lv_label_set_text(gui->label_info, "#FFDD00 Failed to init eMMC!#");
|
2019-06-30 01:03:00 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
char sdPath[OUT_FILENAME_SZ];
|
|
|
|
// Create Restore folders, if they do not exist.
|
|
|
|
f_mkdir("emuMMC");
|
|
|
|
strcpy(sdPath, "emuMMC/SD");
|
|
|
|
base_len = strlen(sdPath);
|
|
|
|
|
|
|
|
for (int j = 0; j < 100; j++)
|
|
|
|
{
|
2020-06-14 09:52:54 +00:00
|
|
|
update_emummc_base_folder(sdPath, base_len, j);
|
2019-07-06 19:08:37 +00:00
|
|
|
if(f_stat(sdPath, NULL) == FR_NO_FILE)
|
2019-06-30 01:03:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
f_mkdir(sdPath);
|
|
|
|
strcat(sdPath, "/eMMC");
|
|
|
|
f_mkdir(sdPath);
|
|
|
|
strcat(sdPath, "/");
|
|
|
|
strcpy(gui->base_path, sdPath);
|
|
|
|
|
|
|
|
timer = get_tmr_s();
|
2022-12-19 03:09:37 +00:00
|
|
|
const u32 BOOT_PART_SECTORS = 0x2000; // Force 4 MiB.
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
emmc_part_t bootPart;
|
|
|
|
memset(&bootPart, 0, sizeof(bootPart));
|
|
|
|
bootPart.lba_start = 0;
|
2022-12-19 03:09:37 +00:00
|
|
|
bootPart.lba_end = BOOT_PART_SECTORS - 1;
|
2022-01-28 23:23:40 +00:00
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
2019-12-04 13:56:53 +00:00
|
|
|
strcpy(bootPart.name, "BOOT");
|
2019-06-30 01:03:00 +00:00
|
|
|
bootPart.name[4] = (u8)('0' + i);
|
|
|
|
bootPart.name[5] = 0;
|
|
|
|
|
|
|
|
s_printf(txt_buf, "#00DDFF %02d: %s#\n#00DDFF Range: 0x%08X - 0x%08X#\n\n",
|
|
|
|
i, bootPart.name, bootPart.lba_start, bootPart.lba_end);
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_set_text(gui->label_info, txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
s_printf(txt_buf, "%02d: %s... ", i, bootPart.name);
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
2022-12-19 03:04:50 +00:00
|
|
|
emmc_set_partition(i + 1);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
strcat(sdPath, bootPart.name);
|
2021-02-06 01:19:42 +00:00
|
|
|
res = _dump_emummc_file_part(gui, sdPath, &emmc_storage, &bootPart);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
s_printf(txt_buf, "#FFDD00 Failed!#\n");
|
|
|
|
goto out_failed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s_printf(txt_buf, "Done!\n");
|
|
|
|
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
strcpy(sdPath, gui->base_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get GP partition size dynamically.
|
2022-12-19 03:04:50 +00:00
|
|
|
emmc_set_partition(EMMC_GPP);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
// Get GP partition size dynamically.
|
2021-02-06 01:19:42 +00:00
|
|
|
const u32 RAW_AREA_NUM_SECTORS = emmc_storage.sec_cnt;
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
emmc_part_t rawPart;
|
|
|
|
memset(&rawPart, 0, sizeof(rawPart));
|
|
|
|
rawPart.lba_start = 0;
|
|
|
|
rawPart.lba_end = RAW_AREA_NUM_SECTORS - 1;
|
|
|
|
strcpy(rawPart.name, "GPP");
|
|
|
|
|
2022-01-28 23:23:40 +00:00
|
|
|
s_printf(txt_buf, "#00DDFF %02d: %s#\n#00DDFF Range: 0x%08X - 0x%08X#\n\n",
|
|
|
|
i, rawPart.name, rawPart.lba_start, rawPart.lba_end);
|
|
|
|
lv_label_set_text(gui->label_info, txt_buf);
|
|
|
|
s_printf(txt_buf, "%02d: %s... ", i, rawPart.name);
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2022-01-28 23:23:40 +00:00
|
|
|
res = _dump_emummc_file_part(gui, sdPath, &emmc_storage, &rawPart);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2022-01-28 23:23:40 +00:00
|
|
|
if (!res)
|
|
|
|
s_printf(txt_buf, "#FFDD00 Failed!#\n");
|
|
|
|
else
|
|
|
|
s_printf(txt_buf, "Done!\n");
|
|
|
|
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
out_failed:
|
|
|
|
timer = get_tmr_s() - timer;
|
2022-10-11 01:19:29 +00:00
|
|
|
emmc_end();
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
s_printf(txt_buf, "Time taken: %dm %ds.\nFinished!", timer / 60, timer % 60);
|
|
|
|
gui->base_path[strlen(gui->base_path) - 5] = '\0';
|
|
|
|
strcpy(sdPath, gui->base_path);
|
|
|
|
strcat(sdPath, "file_based");
|
|
|
|
FIL fp;
|
|
|
|
f_open(&fp, sdPath, FA_CREATE_ALWAYS | FA_WRITE);
|
|
|
|
f_close(&fp);
|
2019-10-18 15:02:06 +00:00
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
gui->base_path[strlen(gui->base_path) - 1] = 0;
|
|
|
|
save_emummc_cfg(0, 0, gui->base_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s_printf(txt_buf, "Time taken: %dm %ds.", timer / 60, timer % 60);
|
2019-10-18 15:02:06 +00:00
|
|
|
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_set_text(gui->label_finish, txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
free(txt_buf);
|
|
|
|
free(gui->base_path);
|
2020-06-13 15:32:40 +00:00
|
|
|
sd_unmount();
|
2019-06-30 01:03:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:15:04 +00:00
|
|
|
static int _dump_emummc_raw_part(emmc_tool_gui_t *gui, int active_part, int part_idx, u32 sd_part_off, emmc_part_t *part, u32 resized_count)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
2021-02-06 15:19:37 +00:00
|
|
|
u32 num = 0;
|
|
|
|
u32 pct = 0;
|
|
|
|
u32 prevPct = 200;
|
|
|
|
int retryCount = 0;
|
|
|
|
u32 sd_sector_off = sd_part_off + (0x2000 * active_part);
|
|
|
|
u32 lba_curr = part->lba_start;
|
|
|
|
u8 *buf = (u8 *)MIXD_BUF_ALIGNED;
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
s_printf(gui->txt_buf, "\n\n\n");
|
|
|
|
lv_label_ins_text(gui->label_info, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
lv_bar_set_value(gui->bar, 0);
|
|
|
|
lv_label_set_text(gui->label_pct, " "SYMBOL_DOT" 0%");
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
s_printf(gui->txt_buf, "#96FF00 Base folder:#\n%s\n#96FF00 Partition offset:# #FF8000 0x%08X#",
|
|
|
|
gui->base_path, sd_part_off);
|
|
|
|
lv_label_ins_text(gui->label_info, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
lv_obj_set_opa_scale(gui->bar, LV_OPA_COVER);
|
|
|
|
lv_obj_set_opa_scale(gui->label_pct, LV_OPA_COVER);
|
|
|
|
|
2021-02-06 15:19:37 +00:00
|
|
|
u32 user_offset = 0;
|
|
|
|
|
|
|
|
if (resized_count)
|
|
|
|
{
|
|
|
|
// Get USER partition info.
|
2021-03-17 07:08:34 +00:00
|
|
|
LIST_INIT(gpt_parsed);
|
2022-01-20 11:15:04 +00:00
|
|
|
emmc_gpt_parse(&gpt_parsed);
|
|
|
|
emmc_part_t *user_part = emmc_part_find(&gpt_parsed, "USER");
|
2021-02-06 15:19:37 +00:00
|
|
|
if (!user_part)
|
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf, "\n#FFDD00 USER partition not found!#\n");
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
user_offset = user_part->lba_start;
|
|
|
|
part->lba_end = user_offset - 1;
|
2022-01-20 11:15:04 +00:00
|
|
|
emmc_gpt_free(&gpt_parsed);
|
2021-02-06 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 totalSectors = part->lba_end - part->lba_start + 1;
|
2019-06-30 01:03:00 +00:00
|
|
|
while (totalSectors > 0)
|
|
|
|
{
|
2020-03-21 20:27:17 +00:00
|
|
|
// Check for cancellation combo.
|
2020-04-14 14:51:42 +00:00
|
|
|
if (btn_read_vol() == (BTN_VOL_UP | BTN_VOL_DOWN))
|
2020-03-21 20:27:17 +00:00
|
|
|
{
|
2021-02-06 15:19:37 +00:00
|
|
|
s_printf(gui->txt_buf, "\n#FFDD00 The emuMMC was cancelled!#\n");
|
2020-03-21 20:27:17 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
msleep(1000);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
retryCount = 0;
|
|
|
|
num = MIN(totalSectors, NUM_SECTORS_PER_ITER);
|
|
|
|
|
|
|
|
// Read data from eMMC.
|
2022-01-20 11:15:04 +00:00
|
|
|
while (!sdmmc_storage_read(&emmc_storage, lba_curr, num, buf))
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf,
|
2019-07-06 19:08:37 +00:00
|
|
|
"\n#FFDD00 Error reading %d blocks @LBA %08X,#\n"
|
2019-06-30 01:03:00 +00:00
|
|
|
"#FFDD00 from eMMC (try %d). #",
|
|
|
|
num, lba_curr, ++retryCount);
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
msleep(150);
|
|
|
|
if (retryCount >= 3)
|
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf, "#FF0000 Aborting...#\nPlease try again...\n");
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf, "#FFDD00 Retrying...#\n");
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 06:37:41 +00:00
|
|
|
|
|
|
|
manual_system_maintenance(false);
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
// Write data to SD card.
|
2021-08-28 15:09:34 +00:00
|
|
|
retryCount = 0;
|
2019-06-30 01:03:00 +00:00
|
|
|
while (!sdmmc_storage_write(&sd_storage, sd_sector_off + lba_curr, num, buf))
|
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf,
|
2019-07-06 19:08:37 +00:00
|
|
|
"\n#FFDD00 Error writing %d blocks @LBA %08X,#\n"
|
2019-06-30 01:03:00 +00:00
|
|
|
"#FFDD00 to SD (try %d). #",
|
|
|
|
num, lba_curr, ++retryCount);
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
msleep(150);
|
|
|
|
if (retryCount >= 3)
|
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf, "#FF0000 Aborting...#\nPlease try again...\n");
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf, "#FFDD00 Retrying...#\n");
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 06:37:41 +00:00
|
|
|
|
|
|
|
manual_system_maintenance(false);
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
pct = (u64)((u64)(lba_curr - part->lba_start) * 100u) / (u64)(part->lba_end - part->lba_start);
|
|
|
|
if (pct != prevPct)
|
|
|
|
{
|
|
|
|
lv_bar_set_value(gui->bar, pct);
|
|
|
|
s_printf(gui->txt_buf, " "SYMBOL_DOT" %d%%", pct);
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_set_text(gui->label_pct, gui->txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
prevPct = pct;
|
|
|
|
}
|
|
|
|
|
|
|
|
lba_curr += num;
|
|
|
|
totalSectors -= num;
|
|
|
|
}
|
|
|
|
lv_bar_set_value(gui->bar, 100);
|
|
|
|
lv_label_set_text(gui->label_pct, " "SYMBOL_DOT" 100%");
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
2021-02-06 15:19:37 +00:00
|
|
|
// Set partition type to emuMMC (0xE0).
|
2019-06-30 01:03:00 +00:00
|
|
|
if (active_part == 2)
|
|
|
|
{
|
2021-02-06 15:19:37 +00:00
|
|
|
mbr_t mbr;
|
|
|
|
sdmmc_storage_read(&sd_storage, 0, 1, &mbr);
|
|
|
|
mbr.partitions[part_idx].type = 0xE0;
|
|
|
|
sdmmc_storage_write(&sd_storage, 0, 1, &mbr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resized_count)
|
|
|
|
{
|
2021-02-08 01:52:23 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, "Done!\n");
|
|
|
|
|
2021-02-06 15:19:37 +00:00
|
|
|
// Calculate USER size and set it for FatFS.
|
|
|
|
u32 user_sectors = resized_count - user_offset - 33;
|
2022-01-28 23:37:57 +00:00
|
|
|
user_sectors = ALIGN_DOWN(user_sectors, 0x20); // Align down to cluster size.
|
2021-02-06 15:19:37 +00:00
|
|
|
disk_set_info(DRIVE_EMU, SET_SECTOR_COUNT, &user_sectors);
|
|
|
|
|
|
|
|
// Initialize BIS for emuMMC. BIS keys should be already in place.
|
|
|
|
emmc_part_t user_part = {0};
|
|
|
|
user_part.lba_start = user_offset;
|
|
|
|
user_part.lba_end = user_offset + user_sectors - 1;
|
|
|
|
strcpy(user_part.name, "USER");
|
|
|
|
nx_emmc_bis_init(&user_part, true, sd_sector_off);
|
|
|
|
|
2021-02-08 01:52:23 +00:00
|
|
|
s_printf(gui->txt_buf, "Formatting USER... \n");
|
2021-02-06 15:19:37 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
// Format USER partition.
|
2021-10-01 12:45:25 +00:00
|
|
|
u8 *buff = malloc(SZ_4M);
|
|
|
|
int mkfs_error = f_mkfs("emu:", FM_FAT32 | FM_SFD | FM_PRF2, 16384, buff, SZ_4M);
|
2021-08-28 15:09:34 +00:00
|
|
|
free(buff);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
// Mount sd card back.
|
|
|
|
sd_mount();
|
|
|
|
|
|
|
|
if (mkfs_error)
|
|
|
|
{
|
2021-02-08 01:52:23 +00:00
|
|
|
s_printf(gui->txt_buf, "#FF0000 Failed (%d)!#\nPlease try again...\n", mkfs_error);
|
2021-02-06 15:19:37 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-08 01:52:23 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, "Done!\n");
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
// Flush BIS cache, deinit, clear BIS keys slots and reinstate SBK.
|
|
|
|
nx_emmc_bis_end();
|
|
|
|
hos_bis_keys_clear();
|
|
|
|
|
2021-02-08 01:52:23 +00:00
|
|
|
s_printf(gui->txt_buf, "Writing new GPT... ");
|
2021-02-06 15:19:37 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
// Read MBR, GPT and backup GPT.
|
|
|
|
mbr_t mbr;
|
2021-03-17 07:08:34 +00:00
|
|
|
gpt_t *gpt = calloc(1, sizeof(gpt_t));
|
2021-02-06 15:19:37 +00:00
|
|
|
gpt_header_t gpt_hdr_backup;
|
2022-01-20 11:15:04 +00:00
|
|
|
sdmmc_storage_read(&emmc_storage, 0, 1, &mbr);
|
|
|
|
sdmmc_storage_read(&emmc_storage, 1, sizeof(gpt_t) >> 9, gpt);
|
|
|
|
sdmmc_storage_read(&emmc_storage, gpt->header.alt_lba, 1, &gpt_hdr_backup);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
// Find USER partition.
|
|
|
|
u32 gpt_entry_idx = 0;
|
2021-03-17 07:08:34 +00:00
|
|
|
for (gpt_entry_idx = 0; gpt_entry_idx < gpt->header.num_part_ents; gpt_entry_idx++)
|
|
|
|
if (!memcmp(gpt->entries[gpt_entry_idx].name, (char[]) { 'U', 0, 'S', 0, 'E', 0, 'R', 0 }, 8))
|
2021-02-06 15:19:37 +00:00
|
|
|
break;
|
|
|
|
|
2021-03-17 07:08:34 +00:00
|
|
|
if (gpt_entry_idx >= gpt->header.num_part_ents)
|
2021-02-06 15:19:37 +00:00
|
|
|
{
|
2021-02-08 01:52:23 +00:00
|
|
|
s_printf(gui->txt_buf, "\n#FF0000 No USER partition...#\nPlease try again...\n");
|
2021-02-06 15:19:37 +00:00
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
2021-03-17 07:08:34 +00:00
|
|
|
free(gpt);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set new emuMMC size and USER size.
|
|
|
|
mbr.partitions[0].size_sct = resized_count;
|
2022-01-28 23:37:57 +00:00
|
|
|
gpt->entries[gpt_entry_idx].lba_end = user_part.lba_end;
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
// Update Main GPT.
|
2021-03-17 07:08:34 +00:00
|
|
|
gpt->header.alt_lba = resized_count - 1;
|
|
|
|
gpt->header.last_use_lba = resized_count - 34;
|
|
|
|
gpt->header.part_ents_crc32 = crc32_calc(0, (const u8 *)gpt->entries, sizeof(gpt_entry_t) * gpt->header.num_part_ents);
|
|
|
|
gpt->header.crc32 = 0; // Set to 0 for calculation.
|
|
|
|
gpt->header.crc32 = crc32_calc(0, (const u8 *)&gpt->header, gpt->header.size);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
// Update Backup GPT.
|
|
|
|
gpt_hdr_backup.my_lba = resized_count - 1;
|
|
|
|
gpt_hdr_backup.part_ent_lba = resized_count - 33;
|
2021-03-17 07:08:34 +00:00
|
|
|
gpt_hdr_backup.part_ents_crc32 = gpt->header.part_ents_crc32;
|
2021-02-06 15:19:37 +00:00
|
|
|
gpt_hdr_backup.crc32 = 0; // Set to 0 for calculation.
|
|
|
|
gpt_hdr_backup.crc32 = crc32_calc(0, (const u8 *)&gpt_hdr_backup, gpt_hdr_backup.size);
|
|
|
|
|
|
|
|
// Write main GPT.
|
2021-03-17 07:08:34 +00:00
|
|
|
sdmmc_storage_write(&sd_storage, sd_sector_off + gpt->header.my_lba, sizeof(gpt_t) >> 9, gpt);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
// Write backup GPT partition table.
|
2021-03-17 07:08:34 +00:00
|
|
|
sdmmc_storage_write(&sd_storage, sd_sector_off + gpt_hdr_backup.part_ent_lba, ((sizeof(gpt_entry_t) * 128) >> 9), gpt->entries);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
// Write backup GPT header.
|
|
|
|
sdmmc_storage_write(&sd_storage, sd_sector_off + gpt_hdr_backup.my_lba, 1, &gpt_hdr_backup);
|
|
|
|
|
|
|
|
// Write MBR.
|
|
|
|
sdmmc_storage_write(&sd_storage, sd_sector_off, 1, &mbr);
|
2021-03-17 07:08:34 +00:00
|
|
|
|
2021-08-28 15:09:34 +00:00
|
|
|
// Clear nand patrol.
|
2022-01-20 11:15:04 +00:00
|
|
|
memset(buf, 0, EMMC_BLOCKSIZE);
|
2021-08-28 15:09:34 +00:00
|
|
|
sdmmc_storage_write(&sd_storage, sd_part_off + NAND_PATROL_SECTOR, 1, buf);
|
|
|
|
|
2021-03-17 07:08:34 +00:00
|
|
|
free(gpt);
|
2019-06-30 01:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-08-28 14:53:14 +00:00
|
|
|
static int _emummc_raw_derive_bis_keys(emmc_tool_gui_t *gui, u32 resized_count)
|
2021-02-06 15:19:37 +00:00
|
|
|
{
|
|
|
|
if (!resized_count)
|
|
|
|
return 1;
|
|
|
|
|
2021-08-28 14:53:14 +00:00
|
|
|
bool error = false;
|
2021-02-06 15:19:37 +00:00
|
|
|
|
2021-10-01 12:45:25 +00:00
|
|
|
char *txt_buf = (char *)malloc(SZ_16K);
|
2021-02-06 15:19:37 +00:00
|
|
|
txt_buf[0] = 0;
|
|
|
|
|
2021-08-28 14:53:14 +00:00
|
|
|
// Generate BIS keys.
|
|
|
|
hos_bis_keygen();
|
2021-02-06 15:19:37 +00:00
|
|
|
|
2021-10-01 12:45:25 +00:00
|
|
|
u8 *cal0_buf = malloc(SZ_64K);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
// Read and decrypt CAL0 for validation of working BIS keys.
|
2022-12-19 03:04:50 +00:00
|
|
|
emmc_set_partition(EMMC_GPP);
|
2021-02-06 15:19:37 +00:00
|
|
|
LIST_INIT(gpt);
|
2022-01-20 11:15:04 +00:00
|
|
|
emmc_gpt_parse(&gpt);
|
|
|
|
emmc_part_t *cal0_part = emmc_part_find(&gpt, "PRODINFO"); // check if null
|
2021-02-06 15:19:37 +00:00
|
|
|
nx_emmc_bis_init(cal0_part, false, 0);
|
|
|
|
nx_emmc_bis_read(0, 0x40, cal0_buf);
|
|
|
|
nx_emmc_bis_end();
|
2022-01-20 11:15:04 +00:00
|
|
|
emmc_gpt_free(&gpt);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
nx_emmc_cal0_t *cal0 = (nx_emmc_cal0_t *)cal0_buf;
|
|
|
|
|
2021-08-28 14:53:14 +00:00
|
|
|
// Check keys validity.
|
2021-02-06 15:19:37 +00:00
|
|
|
if (memcmp(&cal0->magic, "CAL0", 4))
|
|
|
|
{
|
2021-08-28 14:53:14 +00:00
|
|
|
// Clear EKS keys.
|
|
|
|
hos_eks_clear(KB_FIRMWARE_VERSION_MAX);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
strcpy(txt_buf, "#FFDD00 BIS keys validation failed!#\n");
|
2021-08-28 14:53:14 +00:00
|
|
|
error = true;
|
2021-02-06 15:19:37 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 14:53:14 +00:00
|
|
|
free(cal0_buf);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
2021-08-28 14:53:14 +00:00
|
|
|
if (error)
|
2021-02-06 15:19:37 +00:00
|
|
|
{
|
|
|
|
lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL);
|
|
|
|
lv_obj_set_style(dark_bg, &mbox_darken);
|
|
|
|
lv_obj_set_size(dark_bg, LV_HOR_RES, LV_VER_RES);
|
|
|
|
|
2022-10-11 01:32:53 +00:00
|
|
|
static const char * mbox_btn_map[] = { "\251", "\222Close", "\251", "" };
|
2021-02-06 15:19:37 +00:00
|
|
|
lv_obj_t * mbox = lv_mbox_create(dark_bg, NULL);
|
|
|
|
lv_mbox_set_recolor_text(mbox, true);
|
|
|
|
lv_obj_set_width(mbox, LV_HOR_RES / 9 * 5);
|
|
|
|
|
|
|
|
lv_mbox_set_text(mbox, "#C7EA46 BIS Keys Generation#");
|
|
|
|
|
|
|
|
lv_obj_t * lb_desc = lv_label_create(mbox, NULL);
|
|
|
|
lv_label_set_long_mode(lb_desc, LV_LABEL_LONG_BREAK);
|
|
|
|
lv_label_set_recolor(lb_desc, true);
|
|
|
|
lv_label_set_style(lb_desc, &monospace_text);
|
|
|
|
lv_obj_set_width(lb_desc, LV_HOR_RES / 9 * 4);
|
|
|
|
|
2021-08-28 14:53:14 +00:00
|
|
|
lv_label_set_text(lb_desc, txt_buf);
|
|
|
|
lv_mbox_add_btns(mbox, mbox_btn_map, mbox_action);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
lv_obj_align(mbox, NULL, LV_ALIGN_CENTER, 0, 0);
|
|
|
|
lv_obj_set_top(mbox, true);
|
|
|
|
|
|
|
|
free(txt_buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-28 14:53:14 +00:00
|
|
|
free(txt_buf);
|
2021-02-06 15:19:37 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump_emummc_raw(emmc_tool_gui_t *gui, int part_idx, u32 sector_start, u32 resized_count)
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
u32 timer = 0;
|
|
|
|
|
2021-10-01 12:45:25 +00:00
|
|
|
char *txt_buf = (char *)malloc(SZ_16K);
|
2021-02-06 01:19:42 +00:00
|
|
|
gui->base_path = (char *)malloc(OUT_FILENAME_SZ);
|
2019-06-30 01:03:00 +00:00
|
|
|
gui->txt_buf = txt_buf;
|
2020-03-09 06:37:41 +00:00
|
|
|
|
2022-05-12 13:43:18 +00:00
|
|
|
txt_buf[0] = 0;
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_set_text(gui->label_log, txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
2019-07-06 19:08:37 +00:00
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
if (!sd_mount())
|
|
|
|
{
|
2020-04-27 05:47:00 +00:00
|
|
|
lv_label_set_text(gui->label_info, "#FFDD00 Failed to init SD!#");
|
2019-06-30 01:03:00 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-01-20 11:15:04 +00:00
|
|
|
if (!emmc_initialize(false))
|
2019-06-30 01:03:00 +00:00
|
|
|
{
|
2020-04-27 05:47:00 +00:00
|
|
|
lv_label_set_text(gui->label_info, "#FFDD00 Failed to init eMMC!#");
|
2019-06-30 01:03:00 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-08-28 14:53:14 +00:00
|
|
|
if (!_emummc_raw_derive_bis_keys(gui, resized_count))
|
2021-02-06 15:19:37 +00:00
|
|
|
{
|
|
|
|
s_printf(gui->txt_buf, "#FFDD00 For formatting USER partition,#\n#FFDD00 BIS keys are needed!#\n");
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, gui->txt_buf);
|
2022-10-11 01:19:29 +00:00
|
|
|
emmc_end();
|
2021-02-06 15:19:37 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
int i = 0;
|
|
|
|
char sdPath[OUT_FILENAME_SZ];
|
|
|
|
// Create Restore folders, if they do not exist.
|
|
|
|
f_mkdir("emuMMC");
|
|
|
|
s_printf(sdPath, "emuMMC/RAW%d", part_idx);
|
|
|
|
f_mkdir(sdPath);
|
|
|
|
strcat(sdPath, "/");
|
|
|
|
strcpy(gui->base_path, sdPath);
|
|
|
|
|
|
|
|
timer = get_tmr_s();
|
2022-12-19 03:09:37 +00:00
|
|
|
const u32 BOOT_PART_SECTORS = 0x2000; // Force 4 MiB.
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
emmc_part_t bootPart;
|
|
|
|
memset(&bootPart, 0, sizeof(bootPart));
|
|
|
|
bootPart.lba_start = 0;
|
2022-12-19 03:09:37 +00:00
|
|
|
bootPart.lba_end = BOOT_PART_SECTORS - 1;
|
2020-04-29 23:05:24 +00:00
|
|
|
|
|
|
|
// Clear partition start.
|
2021-10-01 12:45:25 +00:00
|
|
|
memset((u8 *)MIXD_BUF_ALIGNED, 0, SZ_16M);
|
2020-04-29 23:05:24 +00:00
|
|
|
sdmmc_storage_write(&sd_storage, sector_start - 0x8000, 0x8000, (u8 *)MIXD_BUF_ALIGNED);
|
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
2019-12-04 13:56:53 +00:00
|
|
|
strcpy(bootPart.name, "BOOT");
|
2019-06-30 01:03:00 +00:00
|
|
|
bootPart.name[4] = (u8)('0' + i);
|
|
|
|
bootPart.name[5] = 0;
|
|
|
|
|
|
|
|
s_printf(txt_buf, "#00DDFF %02d: %s#\n#00DDFF Range: 0x%08X - 0x%08X#\n\n",
|
|
|
|
i, bootPart.name, bootPart.lba_start, bootPart.lba_end);
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_set_text(gui->label_info, txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
s_printf(txt_buf, "%02d: %s... ", i, bootPart.name);
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
2022-12-19 03:04:50 +00:00
|
|
|
emmc_set_partition(i + 1);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
strcat(sdPath, bootPart.name);
|
2022-01-20 11:15:04 +00:00
|
|
|
res = _dump_emummc_raw_part(gui, i, part_idx, sector_start, &bootPart, 0);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
s_printf(txt_buf, "#FFDD00 Failed!#\n");
|
|
|
|
goto out_failed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s_printf(txt_buf, "Done!\n");
|
|
|
|
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
|
|
|
strcpy(sdPath, gui->base_path);
|
|
|
|
}
|
|
|
|
|
2022-12-19 03:04:50 +00:00
|
|
|
emmc_set_partition(EMMC_GPP);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
// Get GP partition size dynamically.
|
2021-02-06 15:10:13 +00:00
|
|
|
const u32 RAW_AREA_NUM_SECTORS = emmc_storage.sec_cnt;
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
emmc_part_t rawPart;
|
|
|
|
memset(&rawPart, 0, sizeof(rawPart));
|
|
|
|
rawPart.lba_start = 0;
|
|
|
|
rawPart.lba_end = RAW_AREA_NUM_SECTORS - 1;
|
|
|
|
strcpy(rawPart.name, "GPP");
|
|
|
|
{
|
|
|
|
s_printf(txt_buf, "#00DDFF %02d: %s#\n#00DDFF Range: 0x%08X - 0x%08X#\n\n",
|
|
|
|
i, rawPart.name, rawPart.lba_start, rawPart.lba_end);
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_set_text(gui->label_info, txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
s_printf(txt_buf, "%02d: %s... ", i, rawPart.name);
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
|
2022-01-20 11:15:04 +00:00
|
|
|
res = _dump_emummc_raw_part(gui, 2, part_idx, sector_start, &rawPart, resized_count);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
if (!res)
|
|
|
|
s_printf(txt_buf, "#FFDD00 Failed!#\n");
|
|
|
|
else
|
|
|
|
s_printf(txt_buf, "Done!\n");
|
|
|
|
|
|
|
|
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
|
|
|
|
manual_system_maintenance(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
out_failed:
|
|
|
|
timer = get_tmr_s() - timer;
|
2022-10-11 01:19:29 +00:00
|
|
|
emmc_end();
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
s_printf(txt_buf, "Time taken: %dm %ds.\nFinished!", timer / 60, timer % 60);
|
|
|
|
strcpy(sdPath, gui->base_path);
|
|
|
|
strcat(sdPath, "raw_based");
|
|
|
|
FIL fp;
|
|
|
|
f_open(&fp, sdPath, FA_CREATE_ALWAYS | FA_WRITE);
|
|
|
|
f_write(&fp, §or_start, 4, NULL);
|
|
|
|
f_close(&fp);
|
2019-10-18 15:02:06 +00:00
|
|
|
|
2019-06-30 01:03:00 +00:00
|
|
|
gui->base_path[strlen(gui->base_path) - 1] = 0;
|
|
|
|
save_emummc_cfg(part_idx, sector_start, gui->base_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s_printf(txt_buf, "Time taken: %dm %ds.", timer / 60, timer % 60);
|
2019-10-18 15:02:06 +00:00
|
|
|
|
2020-03-09 06:37:41 +00:00
|
|
|
lv_label_set_text(gui->label_finish, txt_buf);
|
2019-06-30 01:03:00 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
free(txt_buf);
|
|
|
|
free(gui->base_path);
|
2020-06-13 15:32:40 +00:00
|
|
|
sd_unmount();
|
2019-06-30 01:03:00 +00:00
|
|
|
}
|