mirror of
https://github.com/rvtr/TwlIPL.git
synced 2025-10-31 06:01:12 -04:00
・HW情報や本体設定データを共通のTSFフォーマットにするため、TSF フォーマットのコードを追加。(現状はHW情報のみ対応) ・HW情報用のRSA鍵ペアを追加。 ・acsignライブラリにRSA暗号化関数を追加。 ・acsignライブラリのRSAでコード関数の値チェックを修正。 ・HW情報をライトするHWInfoWriterを追加。 ・NANDフラッシュマップ&フォルダツリーのフォルダ構成における各ファイルに ついて、RSA署名の有無と使用する鍵による色分けを行う。 ・TWL_SystemMenuメモリマップにNANDファーム用バッファを記載。 (ランチャーとメモリが重ならないように) git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/TwlIPL/trunk@312 b08762b0-b915-fc4b-9d8c-17b2551a87ff
369 lines
10 KiB
C
369 lines
10 KiB
C
/*---------------------------------------------------------------------------*
|
||
Project: TwlIPL
|
||
File: DS_Chat.c
|
||
|
||
Copyright 2007 Nintendo. All rights reserved.
|
||
|
||
These coded instructions, statements, and computer programs contain
|
||
proprietary information of Nintendo of America Inc. and/or Nintendo
|
||
Company Ltd., and are protected by Federal copyright law. They may
|
||
not be disclosed to third parties or copied or duplicated in any form,
|
||
in whole or in part, without the prior written consent of Nintendo.
|
||
|
||
$Date:: $
|
||
$Rev$
|
||
$Author$
|
||
*---------------------------------------------------------------------------*/
|
||
|
||
#include <twl.h>
|
||
#include <sysmenu.h>
|
||
#include "misc.h"
|
||
#include "HWInfoWriter.h"
|
||
|
||
// define data------------------------------------------
|
||
#define WRITER_ELEMENT_NUM 7
|
||
#define MSG_X 3
|
||
#define MSG_Y 19
|
||
|
||
// extern data------------------------------------------
|
||
const TWLHWNormalInfo *THW_GetDefaultNormalInfo( void );
|
||
const TWLHWSecureInfo *THW_GetDefaultSecureInfo( void );
|
||
const TWLHWNormalInfo *THW_GetNormalInfo( void );
|
||
const TWLHWSecureInfo *THW_GetSecureInfo( void );
|
||
|
||
// function's prototype declaration---------------------
|
||
static void ReadPrivateKey( void );
|
||
static void ReadHWInfoFile( void );
|
||
static void WriteHWInfoFile( u8 region );
|
||
static BOOL WriteHWNormalInfoFile( void );
|
||
static BOOL WriteHWSecureInfoFile( u8 region );
|
||
static void DeleteHWInfoFile( void );
|
||
static void VerifyHWInfo( void );
|
||
static BOOL VerifyData( const u8 *pTgt, const u8 *pOrg, u32 len );
|
||
static void DispMessage( int x, int y, u16 color, const u16 *pMsg );
|
||
|
||
// global variable -------------------------------------
|
||
RTCDrawProperty g_rtcDraw = {
|
||
TRUE, RTC_DATE_TOP_X, RTC_DATE_TOP_Y, RTC_TIME_TOP_X, RTC_TIME_TOP_Y
|
||
};
|
||
|
||
// static variable -------------------------------------
|
||
static u16 s_csr;
|
||
static u8 *s_pPrivKeyBuffer = NULL;
|
||
|
||
// const data -----------------------------------------
|
||
static const u16 *const s_pStrWriter[ WRITER_ELEMENT_NUM ] = {
|
||
(const u16 *)L"Write HW Info REGION=JAPAN",
|
||
(const u16 *)L"Write HW Info REGION=AMERICA",
|
||
(const u16 *)L"Write HW Info REGION=EUROPE",
|
||
(const u16 *)L"Write HW Info REGION=AUSTRALIA",
|
||
(const u16 *)L"Write HW Info REGION=CHINA",
|
||
(const u16 *)L"Write HW Info REGION=KOREA",
|
||
(const u16 *)L"Delete HW Info",
|
||
};
|
||
|
||
static MenuPos s_writerPos[] = {
|
||
{ TRUE, 3 * 8, 4 * 8 },
|
||
{ TRUE, 3 * 8, 6 * 8 },
|
||
{ TRUE, 3 * 8, 8 * 8 },
|
||
{ TRUE, 3 * 8, 10 * 8 },
|
||
{ TRUE, 3 * 8, 12 * 8 },
|
||
{ TRUE, 3 * 8, 14 * 8 },
|
||
{ TRUE, 3 * 8, 16 * 8 },
|
||
};
|
||
|
||
|
||
static const MenuParam s_writerParam = {
|
||
WRITER_ELEMENT_NUM,
|
||
TXT_COLOR_BLACK,
|
||
TXT_COLOR_GREEN,
|
||
TXT_COLOR_RED,
|
||
&s_writerPos[ 0 ],
|
||
(const u16 **)&s_pStrWriter,
|
||
};
|
||
|
||
//======================================================
|
||
// HW<48><57><EFBFBD>C<F1838983>^<5E>[
|
||
//======================================================
|
||
|
||
// HW<48><57><EFBFBD>C<F1838983>^<5E>[<5B>̏<EFBFBD><CC8F><EFBFBD><EFBFBD><EFBFBD>
|
||
void HWInfoWriterInit( void )
|
||
{
|
||
GX_DispOff();
|
||
GXS_DispOff();
|
||
|
||
InitBG();
|
||
|
||
NNS_G2dCharCanvasClear( &gCanvas, TXT_COLOR_WHITE );
|
||
|
||
PutStringUTF16( 1 * 8, 0 * 8, TXT_COLOR_BLUE, (const u16 *)L"HW Info Writer");
|
||
GetAndDrawRTCData( &g_rtcDraw, TRUE );
|
||
|
||
ACSign_SetAllocFunc( Alloc, Free );
|
||
ReadPrivateKey();
|
||
ReadHWInfoFile();
|
||
// VerifyHWInfo();
|
||
OS_TPrintf( "region = %d\n", THW_GetRegion() );
|
||
|
||
s_csr = 0;
|
||
DrawMenu( s_csr, &s_writerParam );
|
||
|
||
GXS_SetVisiblePlane( GX_PLANEMASK_BG0 );
|
||
GX_DispOn();
|
||
GXS_DispOn();
|
||
}
|
||
|
||
|
||
// HW<48><57><EFBFBD>C<F1838983>^<5E>[<5B>̃<EFBFBD><CC83>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD>[<5B>v
|
||
void HWInfoWriterMain( void )
|
||
{
|
||
// <20>J<EFBFBD>[<5B>\<5C><><EFBFBD>ړ<EFBFBD>
|
||
if( pad.trg & PAD_KEY_DOWN ){
|
||
if( ++s_csr == WRITER_ELEMENT_NUM ) {
|
||
s_csr = 0;
|
||
}
|
||
}
|
||
if( pad.trg & PAD_KEY_UP ){
|
||
if( --s_csr & 0x8000 ) {
|
||
s_csr = WRITER_ELEMENT_NUM - 1;
|
||
}
|
||
}
|
||
DrawMenu( s_csr, &s_writerParam );
|
||
|
||
// <20><><EFBFBD>s
|
||
if( pad.trg == PAD_BUTTON_A ) {
|
||
if( s_csr == WRITER_ELEMENT_NUM - 1 ) {
|
||
OS_TPrintf( "Delete start.\n" );
|
||
(void)DeleteHWInfoFile();
|
||
}else {
|
||
OS_TPrintf( "Write start.\n" );
|
||
WriteHWInfoFile( (u8)s_csr );
|
||
}
|
||
}
|
||
|
||
GetAndDrawRTCData( &g_rtcDraw, FALSE );
|
||
}
|
||
|
||
|
||
// <20>閧<EFBFBD><E996A7><EFBFBD>̃<EFBFBD><CC83>[<5B>h
|
||
static void ReadPrivateKey( void )
|
||
{
|
||
BOOL result = FALSE;
|
||
u32 keyLength;
|
||
FSFile file;
|
||
|
||
FS_InitFile( &file );
|
||
if( !FS_OpenFileEx( &file, "rom:key/privKeyHWInfo.der", FS_FILEMODE_R ) ) {
|
||
OS_TPrintf( "PrivateKey read failed.\n" );
|
||
}else {
|
||
keyLength = FS_GetFileLength( &file );
|
||
if( keyLength > 0 ) {
|
||
s_pPrivKeyBuffer = Alloc( keyLength );
|
||
if( FS_ReadFile( &file, s_pPrivKeyBuffer, (s32)keyLength ) == keyLength ) {
|
||
OS_TPrintf( "PrivateKey read succeeded.\n" );
|
||
result = TRUE;
|
||
}else {
|
||
OS_TPrintf( "PrivateKey read failed.\n" );
|
||
}
|
||
}
|
||
FS_CloseFile( &file );
|
||
}
|
||
if( !result && s_pPrivKeyBuffer ) {
|
||
Free( s_pPrivKeyBuffer );
|
||
s_pPrivKeyBuffer = NULL;
|
||
}
|
||
}
|
||
|
||
|
||
// HW<48><57><EFBFBD><EFBFBD><EFBFBD>S<EFBFBD>̂̃<CC82><CC83>[<5B>h
|
||
static void ReadHWInfoFile( void )
|
||
{
|
||
TSFReadResult retval;
|
||
|
||
retval = THW_ReadNormalInfo();
|
||
if( retval == TSF_READ_RESULT_SUCCEEDED ) {
|
||
OS_TPrintf( "HW Normal Info Read succeeded.\n" );
|
||
}else {
|
||
OS_TPrintf( "HW Normal Info Read failed.\n" );
|
||
}
|
||
|
||
retval = THW_ReadSecureInfo();
|
||
if( retval == TSF_READ_RESULT_SUCCEEDED ) {
|
||
OS_TPrintf( "HW Secure Info Read succeeded.\n" );
|
||
}else {
|
||
OS_TPrintf( "HW Secure Info Read failed.\n" );
|
||
}
|
||
}
|
||
|
||
|
||
// HW<48><57><EFBFBD><EFBFBD><EFBFBD>S<EFBFBD>̂̃<CC82><CC83>C<EFBFBD>g
|
||
static void WriteHWInfoFile( u8 region )
|
||
{
|
||
static const u16 *pMsgNormalWriting = (const u16 *)L"Writing Normal File...";
|
||
static const u16 *pMsgSecureWriting = (const u16 *)L"Writing Secure File...";
|
||
static const u16 *pMsgSucceeded = (const u16 *)L"Succeeded!";
|
||
static const u16 *pMsgFailed = (const u16 *)L"Failed!";
|
||
|
||
// <20>m<EFBFBD>[<5B>}<7D><><EFBFBD>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD>̃<EFBFBD><CC83>C<EFBFBD>g
|
||
(void)PutStringUTF16( MSG_X * 8, MSG_Y * 8, TXT_COLOR_BLACK, pMsgNormalWriting );
|
||
|
||
if( WriteHWNormalInfoFile() ) {
|
||
(void)PutStringUTF16( ( MSG_X + 18 ) * 8, MSG_Y * 8, TXT_COLOR_BLUE, pMsgSucceeded );
|
||
}else {
|
||
(void)PutStringUTF16( ( MSG_X + 18 ) * 8, MSG_Y * 8, TXT_COLOR_RED, pMsgFailed );
|
||
}
|
||
|
||
// <20>Z<EFBFBD>L<EFBFBD><4C><EFBFBD>A<EFBFBD>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD>̃<EFBFBD><CC83>C<EFBFBD>g
|
||
(void)PutStringUTF16( MSG_X * 8, ( MSG_Y + 2 ) * 8, TXT_COLOR_BLACK, pMsgSecureWriting );
|
||
|
||
if( WriteHWSecureInfoFile( region ) ) {
|
||
(void)PutStringUTF16( ( MSG_X + 18 ) * 8, ( MSG_Y + 2 ) * 8, TXT_COLOR_BLUE, pMsgSucceeded );
|
||
}else {
|
||
(void)PutStringUTF16( ( MSG_X + 18 ) * 8, ( MSG_Y + 2 ) * 8, TXT_COLOR_RED, pMsgFailed );
|
||
}
|
||
|
||
// <20><><EFBFBD>b<EFBFBD>Z<EFBFBD>[<5B>W<EFBFBD><57><EFBFBD><EFBFBD><EFBFBD>莞<EFBFBD>ԕ\<5C><><EFBFBD><EFBFBD><EFBFBD>ď<EFBFBD><C48F><EFBFBD>
|
||
DispMessage( 0, 0, TXT_COLOR_NULL, NULL );
|
||
NNS_G2dCharCanvasClearArea( &gCanvas, TXT_COLOR_WHITE,
|
||
MSG_X * 8 , MSG_Y * 8, ( 32 - MSG_X ) * 8, ( MSG_Y + 4 ) * 8 );
|
||
}
|
||
|
||
|
||
// HW<48>m<EFBFBD>[<5B>}<7D><>Info<66>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD>̃<EFBFBD><CC83>C<EFBFBD>g
|
||
static BOOL WriteHWNormalInfoFile( void )
|
||
{
|
||
BOOL isWrite = TRUE;
|
||
TSFReadResult result;
|
||
|
||
result = THW_ReadNormalInfo();
|
||
if( result != TSF_READ_RESULT_SUCCEEDED ) {
|
||
if( !THW_RecoveryNormalInfo( result ) ) {
|
||
OS_TPrintf( "HW Normal Info Recovery failed.\n" );
|
||
isWrite = FALSE;
|
||
}
|
||
}
|
||
if( isWrite &&
|
||
!THW_WriteNormalInfo() ) {
|
||
OS_TPrintf( "HW Normal Info Write failed.\n" );
|
||
}
|
||
|
||
return isWrite;
|
||
}
|
||
|
||
|
||
// HW<48>Z<EFBFBD>L<EFBFBD><4C><EFBFBD>AInfo<66>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD>̃<EFBFBD><CC83>C<EFBFBD>g
|
||
static BOOL WriteHWSecureInfoFile( u8 region )
|
||
{
|
||
BOOL isWrite = TRUE;
|
||
TSFReadResult result;
|
||
|
||
// <20>閧<EFBFBD><E996A7><EFBFBD><EFBFBD><EFBFBD>ǂݍ<C782><DD8D>߂Ă<DF82><C482>Ȃ<EFBFBD><C882>Ȃ<EFBFBD><C882>A<EFBFBD>G<EFBFBD><47><EFBFBD>[
|
||
if( s_pPrivKeyBuffer == NULL ) {
|
||
OS_TPrintf( "Private key not read.\n" );
|
||
return FALSE;
|
||
}
|
||
|
||
// <20>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD>̃<EFBFBD><CC83>[<5B>h
|
||
result = THW_ReadSecureInfo();
|
||
|
||
// <20><><EFBFBD>[<5B>h<EFBFBD>Ɏ<EFBFBD><C98E>s<EFBFBD><73><EFBFBD><EFBFBD><EFBFBD>烊<EFBFBD>J<EFBFBD>o<EFBFBD><6F>
|
||
if( result != TSF_READ_RESULT_SUCCEEDED ) {
|
||
if( !THW_RecoverySecureInfo( result ) ) {
|
||
OS_TPrintf( "HW Secure Info Recovery failed.\n" );
|
||
isWrite = FALSE;
|
||
}
|
||
}
|
||
|
||
// <20><><EFBFBD>[<5B>W<EFBFBD><57><EFBFBD><EFBFBD><EFBFBD>̃Z<CC83>b<EFBFBD>g
|
||
THW_SetRegion( region );
|
||
|
||
if( isWrite &&
|
||
!THW_WriteSecureInfo( s_pPrivKeyBuffer ) ) {
|
||
isWrite = FALSE;
|
||
OS_TPrintf( "HW Secure Info Write failed.\n" );
|
||
}
|
||
|
||
return isWrite;
|
||
}
|
||
|
||
|
||
// HWInfo<66>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD>̍폜
|
||
static void DeleteHWInfoFile( void )
|
||
{
|
||
static const u16 *pMsgNormalDeleting = (const u16 *)L"Deleting Normal File...";
|
||
static const u16 *pMsgSecureDeleting = (const u16 *)L"Deteting Secure File...";
|
||
static const u16 *pMsgSucceeded = (const u16 *)L"Succeeded!";
|
||
static const u16 *pMsgFailed = (const u16 *)L"Failed!";
|
||
|
||
// <20>m<EFBFBD>[<5B>}<7D><><EFBFBD>t<EFBFBD>@<40>C<EFBFBD><43>
|
||
(void)PutStringUTF16( MSG_X * 8, MSG_Y * 8, TXT_COLOR_BLACK, pMsgNormalDeleting );
|
||
if( FS_DeleteFile( (char *)TWL_HWINFO_NORMAL_PATH ) ) {
|
||
OS_TPrintf( "%s delete succeeded.\n", (char *)TWL_HWINFO_NORMAL_PATH );
|
||
(void)PutStringUTF16( ( MSG_X + 19 ) * 8, MSG_Y * 8, TXT_COLOR_BLUE, pMsgSucceeded );
|
||
}else {
|
||
OS_TPrintf( "%s delete failed.\n", (char *)TWL_HWINFO_NORMAL_PATH );
|
||
(void)PutStringUTF16( ( MSG_X + 19 ) * 8, MSG_Y * 8, TXT_COLOR_RED, pMsgFailed );
|
||
}
|
||
|
||
// <20>Z<EFBFBD>L<EFBFBD><4C><EFBFBD>A<EFBFBD>t<EFBFBD>@<40>C<EFBFBD><43>
|
||
(void)PutStringUTF16( MSG_X * 8, ( MSG_Y + 2 ) * 8, TXT_COLOR_BLACK, pMsgSecureDeleting );
|
||
if( FS_DeleteFile( (char *)TWL_HWINFO_SECURE_PATH ) ) {
|
||
OS_TPrintf( "%s delete succeeded.\n", (char *)TWL_HWINFO_SECURE_PATH );
|
||
(void)PutStringUTF16( ( MSG_X + 19 ) * 8, ( MSG_Y + 2 ) * 8, TXT_COLOR_BLUE, pMsgSucceeded );
|
||
}else {
|
||
OS_TPrintf( "%s delete failed.\n", (char *)TWL_HWINFO_SECURE_PATH );
|
||
(void)PutStringUTF16( ( MSG_X + 19 ) * 8, ( MSG_Y + 2 ) * 8, TXT_COLOR_RED, pMsgFailed );
|
||
}
|
||
DispMessage( 0, 0, TXT_COLOR_NULL, NULL );
|
||
NNS_G2dCharCanvasClearArea( &gCanvas, TXT_COLOR_WHITE,
|
||
MSG_X * 8 , MSG_Y * 8, ( 32 - MSG_X ) * 8, ( MSG_Y + 4 ) * 8 );
|
||
}
|
||
|
||
|
||
// HWInfo<66>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD>̃x<CC83><78><EFBFBD>t<EFBFBD>@<40>C
|
||
static void VerifyHWInfo( void )
|
||
{
|
||
if( VerifyData( (const u8 *)THW_GetNormalInfo(), (const u8 *)THW_GetDefaultNormalInfo(), sizeof(TWLHWNormalInfo) ) ) {
|
||
OS_TPrintf( "HW normal Info verify succeeded.\n" );
|
||
}else {
|
||
OS_TPrintf( "HW normal Info verify failed.\n" );
|
||
}
|
||
if( VerifyData( (const u8 *)THW_GetSecureInfo(), (const u8 *)THW_GetDefaultSecureInfo(), sizeof(TWLHWSecureInfo) ) ) {
|
||
OS_TPrintf( "HW secure Info verify succeeded.\n" );
|
||
}else {
|
||
OS_TPrintf( "HW secure Info verify failed.\n" );
|
||
}
|
||
|
||
}
|
||
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̃f<CC83>[<5B>^<5E>x<EFBFBD><78><EFBFBD>t<EFBFBD>@<40>C
|
||
static BOOL VerifyData( const u8 *pTgt, const u8 *pOrg, u32 len )
|
||
{
|
||
while( len-- ) {
|
||
if( *pTgt++ != *pOrg++ ) {
|
||
return FALSE;
|
||
}
|
||
}
|
||
return TRUE;
|
||
}
|
||
|
||
|
||
// <20><><EFBFBD>b<EFBFBD>Z<EFBFBD>[<5B>W<EFBFBD>\<5C><>
|
||
static void DispMessage( int x, int y, u16 color, const u16 *pMsg )
|
||
{
|
||
OSTick start = OS_GetTick();
|
||
// <20><><EFBFBD>b<EFBFBD>Z<EFBFBD>[<5B>W<EFBFBD>\<5C><>
|
||
if( pMsg ) {
|
||
(void)PutStringUTF16( x, y, color, pMsg );
|
||
}
|
||
// <20>E<EFBFBD>F<EFBFBD>C<EFBFBD>g
|
||
while( OS_TicksToSeconds( OS_GetTick() - start ) < 2 ) {
|
||
OS_SpinWait( 0x1000 );
|
||
}
|
||
// <20><><EFBFBD>b<EFBFBD>Z<EFBFBD>[<5B>W<EFBFBD><57><EFBFBD><EFBFBD>
|
||
if( pMsg ) {
|
||
(void)PutStringUTF16( x, y, TXT_COLOR_WHITE, pMsg );
|
||
}
|
||
}
|