mirror of
https://github.com/rvtr/TwlToolsRED.git
synced 2025-10-31 06:41:18 -04:00
331 lines
8.8 KiB
C
331 lines
8.8 KiB
C
|
||
#include <twl.h>
|
||
#include <nitro/nvram/nvram.h>
|
||
#include <nitroWiFi/ncfg.h>
|
||
|
||
#include "font.h"
|
||
#include "text.h"
|
||
#include "mprintf.h"
|
||
#include "mynvram.h"
|
||
#include "my_fs_util.h"
|
||
|
||
|
||
#define NVRAM_PERSONAL_DATA_OFFSET 0x20
|
||
|
||
/* Wifi<66>ݒ肾‚¯‚È‚ç0x0A00 */
|
||
//#define NVRAM_PERSONAL_DATA_SIZE 0x0C00
|
||
#define NVRAM_PERSONAL_DATA_SIZE 0x0A00
|
||
|
||
|
||
#define NVRAM_INTERNAL_BUF_SIZE 0x100
|
||
static u8 nvram_buffer[NVRAM_INTERNAL_BUF_SIZE] ATTRIBUTE_ALIGN(32);
|
||
|
||
|
||
static BOOL my_nvram_read( u32 offset, u32 size, void *buf)
|
||
{
|
||
u32 internal_size = size;
|
||
u32 internal_offset = offset;
|
||
void *temp_buf = buf;
|
||
u32 temp_size;
|
||
int i;
|
||
|
||
while( internal_size ) {
|
||
if( internal_size > NVRAM_INTERNAL_BUF_SIZE ) {
|
||
temp_size = NVRAM_INTERNAL_BUF_SIZE;
|
||
}
|
||
else {
|
||
temp_size = internal_size;
|
||
}
|
||
|
||
DC_InvalidateRange(nvram_buffer, NVRAM_INTERNAL_BUF_SIZE);
|
||
if( NVRAM_RESULT_SUCCESS != NVRAMi_Read( internal_offset , temp_size, (void* )nvram_buffer) ) {
|
||
mprintf("nvram read error\n");
|
||
OS_TPrintf( "nvram error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||
return FALSE;
|
||
}
|
||
for( i = 0 ; i < temp_size ; i++ ) {
|
||
*(u8 *)temp_buf = nvram_buffer[i];
|
||
((u8 *)temp_buf)++;
|
||
}
|
||
|
||
internal_offset += temp_size;
|
||
internal_size -= temp_size;
|
||
}
|
||
// OS_TPrintf( "nvram success: offset = 0x%02x\n", offset);
|
||
return TRUE;
|
||
}
|
||
|
||
static BOOL my_nvram_write( u32 offset, u32 size, void *buf)
|
||
{
|
||
u32 internal_size = size;
|
||
u32 internal_offset = offset;
|
||
void *temp_buf = buf;
|
||
u32 temp_size;
|
||
int i;
|
||
|
||
while( internal_size ) {
|
||
|
||
if( internal_size > NVRAM_INTERNAL_BUF_SIZE ) {
|
||
temp_size = NVRAM_INTERNAL_BUF_SIZE;
|
||
}
|
||
else {
|
||
temp_size = internal_size;
|
||
}
|
||
|
||
for( i = 0 ; i < temp_size ; i++ ) {
|
||
nvram_buffer[i] = *(u8 *)temp_buf;
|
||
((u8 *)temp_buf)++;
|
||
}
|
||
|
||
DC_FlushRange(nvram_buffer, NVRAM_INTERNAL_BUF_SIZE);
|
||
if( NVRAM_RESULT_SUCCESS != NVRAMi_Write( internal_offset , temp_size, (void* )nvram_buffer) ) {
|
||
OS_TPrintf( "nvram write error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||
return FALSE;
|
||
}
|
||
|
||
internal_offset += temp_size;
|
||
internal_size -= temp_size;
|
||
}
|
||
// OS_TPrintf( "nvram success: offset = 0x%02x\n", offset);
|
||
return TRUE;
|
||
}
|
||
|
||
|
||
/* *INDENT-OFF* */
|
||
static const char * const fs_result_strings[] =
|
||
{
|
||
"FS_RESULT_SUCCESS",
|
||
"FS_RESULT_FAILURE",
|
||
"FS_RESULT_BUSY",
|
||
"FS_RESULT_CANCELED",
|
||
"FS_RESULT_UNSUPPORTED",
|
||
"FS_RESULT_ERROR",
|
||
"FS_RESULT_INVALID_PARAMETER",
|
||
"FS_RESULT_NO_MORE_RESOUCE",
|
||
"FS_RESULT_ALREADY_DONE",
|
||
"FS_RESULT_PERMISSION_DENIED",
|
||
"FS_RESULT_MEDIA_FATAL",
|
||
"FS_RESULT_NO_ENTRY",
|
||
"FS_RESULT_MEDIA_NOTHING",
|
||
"FS_RESULT_MEDIA_UNKNOWN",
|
||
"FS_RESULT_BAD_FORMAT"
|
||
};
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
static const size_t fs_result_string_max = sizeof(fs_result_strings) / sizeof(*fs_result_strings);
|
||
|
||
static void ReportLastErrorPath(const char *path)
|
||
{
|
||
FSResult result = FS_GetArchiveResultCode(path);
|
||
if( (result >= 0) && (result < fs_result_string_max)) {
|
||
OS_TPrintf("FS error: \"%s\" %s\n", path, fs_result_strings[result]);
|
||
mprintf("FS error: \"%s\" %s\n", path, fs_result_strings[result]);
|
||
}
|
||
else {
|
||
OS_TPrintf("FS error: unknown\n");
|
||
mprintf("FS error:\n unknown\n");
|
||
}
|
||
|
||
}
|
||
|
||
|
||
static u8 my_nor_buf[NVRAM_PERSONAL_DATA_SIZE] ATTRIBUTE_ALIGN(32);
|
||
static u8 work_content[NCFG_CHECKCONFIGEX_WORK_SIZE];
|
||
|
||
BOOL nvram_backup(char *path)
|
||
{
|
||
BOOL bSuccess;
|
||
BOOL ret_flag = TRUE;
|
||
FSFile nor_fd;
|
||
u32 offset;
|
||
int len;
|
||
|
||
char *nor_file_path = path;
|
||
|
||
void *work = (void *)work_content;
|
||
|
||
MI_CpuClear8(work, NCFG_CHECKCONFIGEX_WORK_SIZE);
|
||
if( NCFG_RESULT_INIT_OK != NCFG_CheckConfigEx( work ) ) {
|
||
return FALSE;
|
||
}
|
||
|
||
|
||
FS_InitFile(&nor_fd);
|
||
|
||
// STD_TSNPrintf(nor_file_path, sizeof(nor_file_path), path );
|
||
bSuccess = FS_OpenFileEx(&nor_fd, nor_file_path, (FS_FILEMODE_R|FS_FILEMODE_W));
|
||
if( ! bSuccess )
|
||
{
|
||
if( !FS_CreateFileAuto(nor_file_path, FS_PERMIT_R | FS_PERMIT_W)) {
|
||
ReportLastErrorPath(nor_file_path);
|
||
OS_TPrintf("2 FS_CreateFileAuto(%s) failed.", nor_file_path);
|
||
return FALSE;
|
||
}
|
||
bSuccess = FS_OpenFileEx(&nor_fd, nor_file_path, (FS_FILEMODE_R|FS_FILEMODE_W));
|
||
if( ! bSuccess ) {
|
||
OS_TPrintf("error %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||
return FALSE;
|
||
}
|
||
}
|
||
if( TRUE != my_nvram_read( NVRAM_PERSONAL_DATA_OFFSET , sizeof(u16), (void* )&offset) ) {
|
||
OS_TPrintf( "nvram error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||
}
|
||
|
||
if( offset == 0 ) {
|
||
OS_TPrintf( "nvram error: offset = 0x%02x\n", offset);
|
||
(void)FS_CloseFile(&nor_fd);
|
||
return FALSE;
|
||
}
|
||
|
||
offset *= 8;
|
||
offset -= 0xA00;
|
||
|
||
if( TRUE != my_nvram_read( offset , NVRAM_PERSONAL_DATA_SIZE, (void* )my_nor_buf) ) {
|
||
OS_TPrintf( "nvram error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||
ret_flag = FALSE;
|
||
}
|
||
else {
|
||
len = my_fs_crypto_write(&nor_fd, my_nor_buf, NVRAM_PERSONAL_DATA_SIZE);
|
||
if (len != NVRAM_PERSONAL_DATA_SIZE) {
|
||
OS_TPrintf("FS_WriteFile() failed.");
|
||
ret_flag = FALSE;
|
||
}
|
||
}
|
||
|
||
OS_TPrintf("\n");
|
||
|
||
FS_FlushFile(&nor_fd);
|
||
|
||
bSuccess = FS_CloseFile(&nor_fd);
|
||
|
||
// OS_TPrintf( "nvram read completed.\n");
|
||
return ret_flag;
|
||
}
|
||
|
||
|
||
typedef struct tagDWCWiFiInfo {
|
||
u64 attestedUserId; // ƒ†<C692>[ƒU ID (”F<E2809D>Ø<EFBFBD>Ï‚Ý)
|
||
u64 notAttestedId; // ƒ†<C692>[ƒU ID (”F<E2809D>Ø‘O)
|
||
u16 pass; // ƒpƒXƒ<58><C692>[ƒh
|
||
u16 randomHistory; // —<><E28094>”—š—ð
|
||
} DWCWiFiInfo;
|
||
|
||
BOOL nvram_restore(char *path)
|
||
{
|
||
BOOL bSuccess;
|
||
BOOL ret_flag = TRUE;
|
||
FSFile nor_fd;
|
||
u32 offset;
|
||
int len;
|
||
// char nor_file_path[FS_FILE_NAME_MAX];
|
||
char *nor_file_path = path;
|
||
|
||
u64 id1;
|
||
u64 id2;
|
||
|
||
FS_InitFile(&nor_fd);
|
||
// STD_TSNPrintf(nor_file_path, sizeof(nor_file_path), path );
|
||
bSuccess = FS_OpenFileEx(&nor_fd, nor_file_path, FS_FILEMODE_R);
|
||
if( ! bSuccess ) {
|
||
OS_TPrintf("error %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||
return FALSE;
|
||
}
|
||
|
||
/* offsetƒAƒhƒŒƒX‚̎擾 */
|
||
if( TRUE != my_nvram_read( NVRAM_PERSONAL_DATA_OFFSET , sizeof(u16), (void* )&offset) ) {
|
||
OS_TPrintf( "nvram error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||
ret_flag = FALSE;
|
||
}
|
||
else {
|
||
OS_TPrintf( "nvram success: offset = 0x%02x\n", offset);
|
||
}
|
||
|
||
if( offset == 0 ) {
|
||
OS_TPrintf( "nvram error: offset = 0x%02x\n", offset);
|
||
return FALSE;
|
||
}
|
||
|
||
/* offset‚̃`ƒFƒbƒN‚Í<E2809A>H */
|
||
offset *= 8;
|
||
offset -= 0xA00;
|
||
|
||
len = my_fs_crypto_read(&nor_fd, my_nor_buf, NVRAM_PERSONAL_DATA_SIZE);
|
||
if (len != NVRAM_PERSONAL_DATA_SIZE) {
|
||
ret_flag = FALSE;
|
||
OS_TPrintf("FS_ReadFile() failed.");
|
||
}
|
||
|
||
|
||
|
||
// DWCWiFiInfo *buf;
|
||
// u8 Wifi[14];
|
||
|
||
// > ‚ƂȂè‚Ü‚·<E2809A>BDWCƒ‰ƒCƒuƒ‰ƒŠ‚É‚æ‚é<E2809A>C•œ‚ðŽg‚í‚È‚¢<E2809A>ê<EFBFBD>‡‚Í
|
||
// > <20>E0x0f0‚Æ0x1f0‚É‚ ‚éID‚ðŠm”F‚µ‚Ä<E2809A>A—¼•û‚ÌID‚ª“¯‚¶<E2809A>A‚©‚Â0‚łȂ¢<E2809A>ê<EFBFBD>‡‚ɃRƒs<C692>[‚·‚é
|
||
// MI_CpuCopy8(&p_ncfgc->slot[0].wifi[0], &id1, 6);
|
||
MI_CpuCopy8(&my_nor_buf[0x600+ 0xf0], &id1, 6);
|
||
id1 &= 0x07FFFFFFFFFF;
|
||
|
||
MI_CpuCopy8(&my_nor_buf[0x600+ 0x1f0], &id2, 6);
|
||
id2 &= 0x07FFFFFFFFFF;
|
||
|
||
if( (id1 == id2) && (id1 != 0) ) {
|
||
if( TRUE != my_nvram_write( offset , /* size */ NVRAM_PERSONAL_DATA_SIZE, (void* )my_nor_buf) ) {
|
||
ret_flag = FALSE;
|
||
OS_TPrintf( "nvram write error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||
}
|
||
}
|
||
else {
|
||
OS_TPrintf( "nvram write id1 id2 - 0 %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
|
||
ret_flag = 3;
|
||
}
|
||
|
||
/*
|
||
#define NVRAM_PERSONAL_DATA_OFFSET 0x20
|
||
|
||
Wifi<66>ݒ肾‚¯‚È‚ç0x0A00
|
||
#define NVRAM_PERSONAL_DATA_SIZE 0x0A00
|
||
#define NVRAM_INTERNAL_BUF_SIZE 0x100
|
||
|
||
|
||
<20>ENOR‚Ì0x20‚©‚ç2byte‚ð“Ç‚Ý<E2809A>o‚µ<E2809A>A8”{‚·‚é<E2809A>i‚¾‚¢‚½‚¢0x1fe00‚®‚ç‚¢‚ɂȂé<E2809A>j
|
||
<20>E‚»‚±‚©‚ç-0x400‚µ‚½’l<E28099>i‚¾‚¢‚½‚¢0x1fa00<30>j‚ªWi-Fiƒ†<C692>[ƒU<C692>[<5B>î•ñ‚Ì<E2809A>擪‚ƂȂé
|
||
|
||
|
||
‚»‚µ‚Ä<E2809A>A–{ID‚ª—L‚é‚©<E2809A>A–³‚¢‚©‚ÌŠm”F‚Í
|
||
Wi-Fiƒ†<C692>[ƒU<C692>[<5B>î•ñ‚Ì0xf0‚©‚ç14byte‚ªWi-FiƒRƒlƒNƒVƒ‡ƒ“ID<49>î•ñ‚ɂȂè‚Ü‚·<E2809A>B
|
||
‚±‚Ì14byte‚ÌWi-FiƒRƒlƒNƒVƒ‡ƒ“ID<49>î•ñ‚ÌŒã‚ë43bit‚ª”F<E2809D>Ø<EFBFBD>ς݃†<C692>[ƒU<C692>[ID‚ªŠi”[‚³‚ê‚é
|
||
<20>ê<EFBFBD>Š‚Æ‚È‚è‚Ü‚·‚Ì‚Å<E2809A>A‚±‚±‚Ì’l‚ª‚·‚ׂÄ<E2809A>u0<75>v‚Ì<E2809A>ê<EFBFBD>‡‚Í<E2809A>A–{ID‚ª–³‚¢‚ÆŒ©‚È‚·Ž–‚ª‚Å‚«‚Ü‚·<E2809A>B
|
||
# <20>Ú<EFBFBD>ׂ͓Y•t‚ÌŽ‘—¿‚ð‚²Šm”F‚‚¾‚³‚¢<E2809A>B
|
||
# Ž‘—¿‚ÍDS‚Ì‚à‚̂ł·‚ª<E2809A>AWi-FiƒRƒlƒNƒVƒ‡ƒ“ID<49>î•ñ‚Ì•”•ª‚Í‹¤’ʂƂȂè‚Ü‚·<E2809A>B
|
||
|
||
‚»‚µ‚Ä<E2809A>A<EFBFBD>¡‰ñ‚̃c<C692>[ƒ‹‚Å<E2809A>C<EFBFBD>³‚ð‚¨Šè‚¢‚µ‚½‚¢“à—e‚Æ‚µ‚Ü‚µ‚Ä‚Í
|
||
<20>E<EFBFBD>ã‹L‚ÌWi-FiƒRƒlƒNƒVƒ‡ƒ“ID<49>î•ñ‚É–{ID‚ª‘¶<E28098>Ý‚µ‚È‚¢<E2809A>ê<EFBFBD>‡‚Í<E2809A>A
|
||
<20>@Wi-Fiƒ†<C692>[ƒU<C692>[<5B>î•ñ‘S‚Ä‚ðˆÚ<CB86>s‚µ‚È‚¢
|
||
‚ɂȂè‚Ü‚·<E2809A>B
|
||
|
||
ˆÚ<CB86>s‚µ‚È‚¢—̈æ‚Í
|
||
<20>EWi-Fiƒ†<C692>[ƒU<C692>[<5B>î•ñ‚Ì<E2809A>擪<E2809C>i‚¾‚¢‚½‚¢0x1fa00<30>j‚©‚ç<E2809A>X‚É-0x600‚µ‚½‚Æ‚±‚ë<E2809A>i‚¾‚¢‚½‚¢0x1f400<30>j
|
||
<20>@‚©‚ç0x1000byte
|
||
<20>@# -0x600‚É‚ÍTWL‚ÅŠg’£‚³‚ꂽ—̈悪“ü‚è‚Ü‚·<E2809A>B
|
||
‚Æ<E2809A>A‚È‚è‚Ü‚·<E2809A>B
|
||
*/
|
||
|
||
|
||
|
||
|
||
OS_TPrintf("\n");
|
||
bSuccess = FS_CloseFile(&nor_fd);
|
||
|
||
OS_TPrintf( "nvram write completed.\n");
|
||
|
||
return ret_flag;
|
||
}
|
||
|