TwlToolsRED/build/tools/sctools/auto_preinst_rom/src/main.c

505 lines
13 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <twl.h>
#include <nitro/nvram/nvram.h>
#include <twl/sea.h>
#include <twl/lcfg.h>
#include <twl/na.h>
#include <twl/nam.h>
#include "ecdl.h"
#include "font.h"
#include "text.h"
#include "mprintf.h"
#include "gfx.h"
#include "key.h"
#include "my_fs_util.h"
#include "hwi.h"
#include "mydata.h"
#include "error_report.h"
#include "myfilename.h"
#include "pre_install.h"
#define THREAD_COMMAND_INSTALL_APP 1
#define THREAD_COMMAND_INSTALL_TICKET 2
#define THREAD_COMMAND_DELETE_APP_CONTENT 3
#define THREAD_COMMAND_DELETE_APP_COMPLETELY 4
static void init_my_thread(void);
static BOOL start_my_thread(u32 command);
static BOOL development_console_flag = FALSE;
static OSHeapHandle hHeap;
static u32 allocator_total_size = 0;
static u8 org_region = 0;
static u64 org_fuseId = 0;
static BOOL pushed_power_button = FALSE;
static BOOL throw_pushed_power_button = FALSE;
static LCFGTWLHWNormalInfo hwn_info;
static LCFGTWLHWSecureInfo hws_info;
static MyData mydata;
static int vram_num_main = 1;
static int vram_num_sub = 0;
static u64 eticket_only_id_buf[100];
static int eticket_only_id_count = 0;
static void fill_command(void)
{
if( development_console_flag == FALSE ) {
/*
0x00030004484e474a, 0 , 0 , rom:/tads/TWL-HNGJ-v256.tad,
0x000300044b32444a, 0 , 0 , rom:/tads/TWL-K2DJ-v0.tad,
0x000300044b47554a, 0 , 0 , rom:/tads/TWL-KGUJ-v257.tad,
*/
eticket_only_id_buf[0] = 0x00030004484e474a;
eticket_only_id_buf[1] = 0x000300044b32444a;
eticket_only_id_buf[2] = 0x000300044b47554a;
eticket_only_id_count = 3;
}
else {
/*
0x0003000434424e41, 0 , 0 , rom:/tads_dev/backupSample.tad,
0x00030004344a4541, 0 , 0 , rom:/tads_dev/encodeSD.tad,
0x0003000434534e41, 0 , 0 , rom:/tads_dev/nandAppSample.tad,
*/
eticket_only_id_buf[2] = 0x0003000434424e41;
eticket_only_id_buf[1] = 0x00030004344a4541;
eticket_only_id_buf[0] = 0x0003000434534e41;
eticket_only_id_count = 3;
}
}
static BOOL myTWLCardCallback( void )
{
return FALSE; // means that not terminate.
}
static PMExitCallbackInfo pmexitcallbackinfo;
static void pmexitcallback(void *arg)
{
#pragma unused(arg)
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>i<EFBFBD>f<EFBFBD>[<5B>^<5E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݒ<EFBFBD><DD92>j<EFBFBD>Ȃ烊<C882>Z<EFBFBD>b<EFBFBD>g<EFBFBD><67><EFBFBD>u<EFBFBD><75><EFBFBD>b<EFBFBD>N<EFBFBD><4E><EFBFBD>Ȃ<EFBFBD><C882><EFBFBD><EFBFBD>΂Ȃ<CE82><C882>Ȃ<EFBFBD> */
pushed_power_button = TRUE;
}
static void *AllocForNAM(size_t size)
{
OSIntrMode old = OS_DisableInterrupts();
void* p = OS_Alloc(size);
OS_RestoreInterrupts(old);
return p;
}
static void FreeForNAM(void* ptr)
{
if( ptr != NULL ) {
OSIntrMode old = OS_DisableInterrupts();
OS_Free(ptr);
OS_RestoreInterrupts(old);
}
}
void TwlMain(void)
{
int i;
void* newArenaLo;
u8 macAddress[6];
ESError es_error_code;
u16 keyData;
int loop_counter = 0;
RTCDate rtc_date;
RTCTime rtc_time;
u16 BatterylevelBuf = 0;
BOOL isAcConnectedBuf = FALSE;
OS_Init();
OS_InitThread();
OS_InitTick();
OS_InitAlarm();
// <20>}<7D>X<EFBFBD>^<5E>[<5B><><EFBFBD><EFBFBD>݃t<DD83><74><EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD><EFBFBD>‚<EFBFBD>
(void)OS_EnableIrq();
// IRQ <20><><EFBFBD><EFBFBD>݂<EFBFBD><DD82><EFBFBD><EFBFBD>‚<EFBFBD><C282>܂<EFBFBD>
(void)OS_EnableInterrupts();
// ARM7<4D>Ƃ̒ʐMFIFO<46><4F><EFBFBD><EFBFBD>݋<EFBFBD><DD8B><EFBFBD>
(void)OS_EnableIrqMask(OS_IE_SPFIFO_RECV);
// <20>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD>V<EFBFBD>X<EFBFBD>e<EFBFBD><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
FS_Init( FS_DMA_NOT_USE );
PM_Init();
// <20><><EFBFBD>C<EFBFBD><43><EFBFBD>A<EFBFBD><41><EFBFBD>[<5B>i<EFBFBD>̃A<CC83><41><EFBFBD>P<EFBFBD>[<5B>g<EFBFBD>V<EFBFBD>X<EFBFBD>e<EFBFBD><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
newArenaLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
OS_SetMainArenaLo(newArenaLo);
// <20><><EFBFBD>C<EFBFBD><43><EFBFBD>A<EFBFBD><41><EFBFBD>[<5B>i<EFBFBD><69><EFBFBD>Ƀq<C983>[<5B>v<EFBFBD><76><EFBFBD>
hHeap = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
OS_SetCurrentHeap(OS_ARENA_MAIN, hHeap);
allocator_total_size = OS_GetTotalFreeSize(OS_ARENA_MAIN, hHeap);
Gfx_Init();
RTC_Init();
SCFG_Init();
NVRAMi_Init();
CARD_Init();
CARD_SetPulledOutCallback( myTWLCardCallback );
// <20>K<EFBFBD>{<7B>GSEA <20>̏<EFBFBD><CC8F><EFBFBD><EFBFBD><EFBFBD>
SEA_Init();
development_console_flag = IsThisDevelopmentConsole();
if(TRUE == development_console_flag ) {
mprintf("--development console--\n");
}
PM_SetAutoExit( FALSE );
PM_SetExitCallbackInfo( &pmexitcallbackinfo,pmexitcallback, NULL);
PM_PrependPreExitCallback( &pmexitcallbackinfo );
ES_InitLib();
if( FALSE == MiyaReadHWNormalInfo( &hwn_info ) ) {
m_set_palette(tc[0], 0x1); /* red */
mprintf("HW Normal Info. read error.\n");
m_set_palette(tc[0], 0xF);
}
// mprintf("HW Secure Info. read ");
if( FALSE == MiyaReadHWSecureInfo( &hws_info ) ) {
m_set_palette(tc[0], 0x1); /* red */
mprintf("HW Secure Info. read error.\n");
m_set_palette(tc[0], 0xF); /* white */
}
// <20>j<EFBFBD>b<EFBFBD>N<EFBFBD>l<EFBFBD>[<5B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȃ<EFBFBD><C882>K<EFBFBD><4B><EFBFBD>ɐݒ<C990>
if( *LCFG_TSD_GetNicknamePtr() == L'\0' ) {
LCFG_TSD_SetNickname((const u16*)(L"repair-tool"));
// mprintf("Set dummy Nickname\n");
}
// region
org_region = LCFG_THW_GetRegion();
// ES Device ID
// <20><><EFBFBD><EFBFBD><EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ă<EFBFBD><C482>Ȃ<EFBFBD><C882>Ȃ<EFBFBD><C882>K<EFBFBD><4B><EFBFBD>ɐݒ<C990>
if( LCFG_TSD_GetCountry() == LCFG_TWL_COUNTRY_UNDEFINED ) {
mprintf("Set dummy Country code ");
switch( org_region ) {
case OS_TWL_REGION_JAPAN:
LCFG_TSD_SetCountry(LCFG_TWL_COUNTRY_JAPAN);
mprintf("JPN");
break;
case OS_TWL_REGION_AMERICA:
LCFG_TSD_SetCountry(LCFG_TWL_COUNTRY_UNITED_STATES);
mprintf("USA");
break;
case OS_TWL_REGION_EUROPE:
LCFG_TSD_SetCountry(LCFG_TWL_COUNTRY_UNITED_KINGDOM);
mprintf("UK");
break;
case OS_TWL_REGION_AUSTRALIA:
LCFG_TSD_SetCountry(LCFG_TWL_COUNTRY_AUSTRALIA);
mprintf("AUS");
break;
case OS_TWL_REGION_CHINA:
LCFG_TSD_SetCountry(LCFG_TWL_COUNTRY_CHINA);
mprintf("CHN");
break;
case OS_TWL_REGION_KOREA:
LCFG_TSD_SetCountry(LCFG_TWL_COUNTRY_SOUTH_KOREA);
mprintf("KOR");
break;
case OS_TWL_REGION_MAX:
default:
mprintf("JPN");
LCFG_TSD_SetCountry(LCFG_TWL_COUNTRY_JAPAN);
break;
}
mprintf("\n");
}
org_fuseId = SCFG_ReadFuseData();
OS_TPrintf("eFuseID: %08X%08X\n", (u32)(org_fuseId >> 32), (u32)(org_fuseId));
OS_GetMacAddress( macAddress );
mydata.shop_record_flag = FALSE;
es_error_code = ES_GetDeviceId(&mydata.deviceId);
if( es_error_code == ES_ERR_OK ) {
if( TRUE == CheckShopRecord( NULL ) ) {
mydata.shop_record_flag = TRUE;
}
else {
mprintf("no shop record\n");
}
}
else {
OS_TPrintf("es_error_code = %d\n", es_error_code );
}
// NAM <20>̏<EFBFBD><CC8F><EFBFBD><EFBFBD><EFBFBD>
NAM_Init(&AllocForNAM, &FreeForNAM);
init_my_thread();
{
double d;
d = 0.1234567789;
mprintf("test %f\n", d);
}
while( 1 ) {
Gfx_Render( vram_num_main , vram_num_sub );
OS_WaitVBlankIntr();
(void)RTC_GetDate( &rtc_date );
(void)RTC_GetTime( &rtc_time );
keyData = m_get_key_trigger();
if ( keyData & PAD_BUTTON_R ) {
vram_num_main++;
if( vram_num_main > (MAX_VRAM_NUM-1) ) {
vram_num_main = 0;
}
}
else if ( keyData & PAD_BUTTON_L ) {
vram_num_main--;
if( vram_num_main < 0 ) {
vram_num_main = (MAX_VRAM_NUM-1);
}
}
else if ( keyData & PAD_BUTTON_A ) {
fill_command();
(void)start_my_thread(THREAD_COMMAND_INSTALL_APP);
}
else if ( keyData & PAD_BUTTON_B ) {
fill_command();
(void)start_my_thread(THREAD_COMMAND_INSTALL_TICKET);
}
else if ( keyData & PAD_BUTTON_X ) {
fill_command();
(void)start_my_thread(THREAD_COMMAND_DELETE_APP_CONTENT);
}
else if ( keyData & PAD_BUTTON_Y ) {
fill_command();
(void)start_my_thread(THREAD_COMMAND_DELETE_APP_COMPLETELY);
}
mfprintf(tc[1], "\fAuto Pre-install Tool");
/* <20><><EFBFBD>j<EFBFBD>[<5B>N<EFBFBD>h<EFBFBD>c<EFBFBD>͕\<5C><><EFBFBD><EFBFBD><EFBFBD>Ȃ<EFBFBD><C882>Ă<EFBFBD><C482><EFBFBD><EFBFBD>H */
m_set_palette(tc[1], M_TEXT_COLOR_LIGHTBLUE );
mfprintf(tc[1], "Unique ID:\n");
m_set_palette(tc[1], M_TEXT_COLOR_WHITE );
mfprintf(tc[1], " ");
for( i = 0; i < LCFG_TWL_HWINFO_MOVABLE_UNIQUE_ID_LEN/2 ; i++ ) {
mfprintf(tc[1], "%02X:", hwn_info.movableUniqueID[i]);
}
mfprintf(tc[1], "\n ");
for( ; i < LCFG_TWL_HWINFO_MOVABLE_UNIQUE_ID_LEN ; i++ ) {
mfprintf(tc[1], "%02X:", hwn_info.movableUniqueID[i]);
}
mfprintf(tc[1], "\n\n");
m_set_palette(tc[1], M_TEXT_COLOR_LIGHTBLUE );
mfprintf(tc[1], "Serial No. ");
m_set_palette(tc[1], M_TEXT_COLOR_WHITE );
mfprintf(tc[1], "%s\n", hws_info.serialNo);
mfprintf(tc[1], "\n");
m_set_palette(tc[1], M_TEXT_COLOR_LIGHTBLUE );
mfprintf(tc[1], "eFuse ID: ");
m_set_palette(tc[1], M_TEXT_COLOR_WHITE );
mfprintf(tc[1],"%08X%08X\n\n", (u32)(org_fuseId >> 32), (u32)(org_fuseId));
m_set_palette(tc[1], M_TEXT_COLOR_LIGHTBLUE );
mfprintf(tc[1],"MAC add.: ");
m_set_palette(tc[1], M_TEXT_COLOR_WHITE );
mfprintf(tc[1],"%02X:%02X:%02X:%02X:%02X:%02X", macAddress[0],macAddress[1],
macAddress[2], macAddress[3], macAddress[4], macAddress[5]);
mfprintf(tc[1],"\n\n");
m_set_palette(tc[1], M_TEXT_COLOR_LIGHTBLUE );
mfprintf(tc[1],"Device ID: ");
m_set_palette(tc[1], M_TEXT_COLOR_WHITE );
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> mydata.shop_record_flag<61>@<40>Ƃ<EFBFBD><C682><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if( TRUE == mydata.shop_record_flag ) {
mfprintf(tc[1],"%s\n", mydata.bmsDeviceId);
}
else {
m_set_palette(tc[1], M_TEXT_COLOR_YELLOW );
mfprintf(tc[1],"-----------\n");
m_set_palette(tc[1], M_TEXT_COLOR_WHITE );
}
mfprintf(tc[1],"\n");
if( (loop_counter % 60*5) == 0 ) {
// PM_RESULT_SUCCESS
(void)PM_GetACAdapter( &isAcConnectedBuf );
(void)PM_GetBatteryLevel( &BatterylevelBuf );
}
mfprintf(tc[1], "%4d/%02d/%02d %02d:%02d:%02d ",
rtc_date.year + 2000, rtc_date.month , rtc_date.day,
rtc_time.hour , rtc_time.minute , rtc_time.second );
if( isAcConnectedBuf == TRUE ) {
m_set_palette(tc[1], M_TEXT_COLOR_BLUE );
mfprintf(tc[1], "AC. \n\n");
m_set_palette(tc[1], M_TEXT_COLOR_WHITE );
}
else {
mfprintf(tc[1], "Batt.Lv ");
switch( BatterylevelBuf ) {
case 0:
case 1:
m_set_palette(tc[1], M_TEXT_COLOR_RED );
break;
case 2:
case 3:
case 4:
m_set_palette(tc[1], M_TEXT_COLOR_YELLOW );
break;
default:
m_set_palette(tc[1], M_TEXT_COLOR_GREEN );
break;
}
mfprintf(tc[1], "%d/5\n\n" , BatterylevelBuf);
m_set_palette(tc[1], M_TEXT_COLOR_WHITE );
}
mfprintf(tc[1], "A -> Install App.\n");
mfprintf(tc[1], "B -> Install Ticket\n");
mfprintf(tc[1], "X -> Delete App. content\n");
mfprintf(tc[1], "Y -> Delete App. completely\n");
if( pushed_power_button == TRUE ) {
OS_TPrintf("%s Power button pressed!\n",__FUNCTION__);
PM_ReadyToExit();
pushed_power_button = FALSE;
}
loop_counter++;
}
OS_Terminate();
}
#define MY_STACK_SIZE (1024*128) /* <20>ł<EFBFBD><C582><EFBFBD><EFBFBD>ق<EFBFBD><D982><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
//#define MY_THREAD_PRIO 20
#define MY_THREAD_PRIO 5
static OSThread MyThread;
static u64 MyStack[MY_STACK_SIZE/sizeof(u64)];
static OSMessage MyMesgBuffer_request[1];
static OSMessage MyMesgBuffer_response[1];
static OSMessageQueue MyMesgQueue_request;
static OSMessageQueue MyMesgQueue_response;
static void MyThreadProc(void *arg)
{
#pragma unused(arg)
OSMessage message;
BOOL flag;
u32 command;
m_set_palette(tc[0], M_TEXT_COLOR_WHITE );
while( 1 ) {
// (void)pre_install_process( NULL, NULL, 0, NULL, 0 , development_console_flag);
(void)OS_SendMessage(&MyMesgQueue_response, (OSMessage)0, OS_MESSAGE_NOBLOCK);
(void)OS_ReceiveMessage(&MyMesgQueue_request, &message, OS_MESSAGE_BLOCK);
flag = TRUE;
command = (u32)message;
switch( command ) {
case THREAD_COMMAND_INSTALL_APP:
flag = pre_install_command(NULL, eticket_only_id_buf, eticket_only_id_count,
1, development_console_flag );
break;
case THREAD_COMMAND_INSTALL_TICKET:
flag = pre_install_command(NULL, eticket_only_id_buf, eticket_only_id_count,
2, development_console_flag );
break;
case THREAD_COMMAND_DELETE_APP_CONTENT:
flag = pre_install_command(NULL, eticket_only_id_buf, eticket_only_id_count,
3, development_console_flag );
break;
case THREAD_COMMAND_DELETE_APP_COMPLETELY:
flag = pre_install_command(NULL, eticket_only_id_buf, eticket_only_id_count,
4, development_console_flag );
break;
default:
flag = FALSE;
mprintf("%s unknown command!\n",__FUNCTION__);
break;
}
mprintf("\n");
}
}
static void init_my_thread(void)
{
OS_InitMessageQueue(&MyMesgQueue_request, &MyMesgBuffer_request[0], 1);
OS_InitMessageQueue(&MyMesgQueue_response, &MyMesgBuffer_response[0], 1);
OS_CreateThread(&MyThread, MyThreadProc,
NULL, MyStack + MY_STACK_SIZE /sizeof(u64),
MY_STACK_SIZE, MY_THREAD_PRIO);
OS_WakeupThreadDirect(&MyThread);
}
static BOOL start_my_thread(u32 command)
{
OSMessage message;
if( TRUE == OS_ReceiveMessage(&MyMesgQueue_response, &message, OS_MESSAGE_NOBLOCK) ) {
(void)OS_SendMessage(&MyMesgQueue_request, (OSMessage)command, OS_MESSAGE_BLOCK);
return TRUE;
}
return FALSE;
}