git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/TwlToolsRED@273 7061adef-622a-194b-ae81-725974e89856

This commit is contained in:
miya 2009-05-13 10:31:19 +00:00
parent 4e5bf01464
commit f13453f0af
10 changed files with 617 additions and 324 deletions

View File

@ -18,7 +18,7 @@
SUBDIRS = my_armadillo.TWL copy_org copy_dst
SUBDIRS = make_tad_table my_armadillo.TWL copy_org copy_dst
include $(TWLSDK_ROOT)/build/buildtools/commondefs

View File

@ -11,7 +11,6 @@
#define COPY_FILE_ENCRYPTION 1
#define LIMIT_BUF 1
#define LIMIT_BUF_ALLOC 1
static BOOL miya_debug_flag = FALSE;
@ -318,127 +317,7 @@ s32 my_fs_crypto_write(FSFile *f, void *ptr, s32 size)
return writtenSize;
}
#if 0
static BOOL LoadFile(const char* path, char **alloc_ptr, int *alloc_size, FSFile *log_fd)
{
FSFile f;
BOOL bSuccess;
char* pBuffer;
u32 fileSize;
s32 readSize = 0;
if( alloc_ptr == 0 || alloc_size == NULL ) {
miya_log_fprintf(log_fd, "Failed LoadFile argument error ptr=0x%08p, size=%d\n", alloc_ptr , alloc_size);
return FALSE;
}
FS_InitFile(&f);
bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_R);
if( ! bSuccess ) {
miya_log_fprintf(log_fd, "%s Failed Open File\n",__FUNCTION__);
miya_log_fprintf(log_fd, " path=%s\n", path );
miya_log_fprintf(log_fd, " res=%s\n", my_fs_util_get_fs_result_word( FS_GetArchiveResultCode(path) ));
return FALSE;
}
fileSize = FS_GetFileLength(&f);
// pBuffer = (char*)OS_Alloc(fileSize + 1);
pBuffer = (char*)OS_Alloc( fileSize );
if( pBuffer == NULL ) {
miya_log_fprintf(log_fd, "%s Mem alloc error: %d\n", __FUNCTION__, fileSize);
*alloc_size = 0;
*alloc_ptr = NULL;
return FALSE;
}
readSize = FS_ReadFile(&f, pBuffer, (s32)fileSize);
if( readSize != fileSize ) {
miya_log_fprintf(log_fd, "%s Failed Read File:%s\n",__FUNCTION__ , path );
miya_log_fprintf(log_fd, " request size=%d read size=%d\n", fileSize, readSize);
if( pBuffer != NULL ) {
OS_Free(pBuffer);
}
*alloc_size = 0;
*alloc_ptr = NULL;
return FALSE;
}
*alloc_size = (int)readSize;
bSuccess = FS_CloseFile(&f);
if( ! bSuccess ) {
miya_log_fprintf(log_fd, "%s Failed Close File\n", __FUNCTION__ );
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word( FS_GetArchiveResultCode(path)));
}
// pBuffer[fileSize] = '\0';
*alloc_ptr = pBuffer;
return TRUE;
}
static BOOL SaveFile(const char* path, void* pData, u32 size, FSFile *log_fd)
{
FSFile f;
BOOL bSuccess;
FSResult fsResult;
s32 writtenSize;
FS_InitFile(&f);
bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_W);
if (bSuccess == FALSE) {
FSResult res = FS_GetArchiveResultCode(path);
if( res == FS_RESULT_NO_ENTRY ) {
/* 本来ここで問題なし */
}
else {
miya_log_fprintf(log_fd, "%s Failed open file %d:\n", __FUNCTION__,__LINE__);
miya_log_fprintf(log_fd, " %s\n", path );
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word(res) );
}
}
else {
FS_CloseFile(&f);
/* backup バックアップを取っておくべき?? */
FS_DeleteFile(path);
}
FS_CreateFile(path, (FS_PERMIT_R|FS_PERMIT_W));
bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_W);
if (bSuccess == FALSE) {
FSResult res = FS_GetArchiveResultCode(path);
miya_log_fprintf(log_fd, "%s Failed open file %d:\n", __FUNCTION__,__LINE__);
miya_log_fprintf(log_fd, " %s\n", path );
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word(res) );
return FALSE;
}
fsResult = FS_SetFileLength(&f, 0);
if( fsResult != FS_RESULT_SUCCESS ) {
}
writtenSize = FS_WriteFile(&f, pData, (s32)size);
if( writtenSize != size ) {
FSResult res = FS_GetArchiveResultCode(path);
miya_log_fprintf(log_fd, "%s Failed write file %d:\n", __FUNCTION__,__LINE__);
miya_log_fprintf(log_fd, " %s\n", path );
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word(res) );
(void)FS_CloseFile(&f);
return FALSE;
}
FS_FlushFile(&f);
(void)FS_CloseFile(&f);
return TRUE;
}
#endif
#ifdef LIMIT_BUF
//#define BUF_SIZE (16*1024)
//#define BUF_SIZE (512*1024)
#define BUF_SIZE (1*1024*1024)
@ -449,14 +328,13 @@ static char *pBuffer_crypto = NULL;
static char pBuffer[BUF_SIZE];
static char pBuffer_crypto[BUF_SIZE];
#endif /* LIMIT_BUF_ALLOC */
#endif
BOOL CopyFile(const char *dst_path, const char *src_path, FSFile *log_fd )
{
BOOL ret_flag = TRUE;
#ifdef LIMIT_BUF
FSFile f_src;
FSFile f_dst;
u32 restSize;
@ -586,44 +464,20 @@ BOOL CopyFile(const char *dst_path, const char *src_path, FSFile *log_fd )
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word( FS_GetArchiveResultCode(dst_path)));
}
#else
int alloc_size = 0;
char *alloc_ptr = NULL;
if( TRUE == LoadFile(src_path, &alloc_ptr, &alloc_size, log_fd) ) {
if( TRUE == SaveFile(dst_path, alloc_ptr, (u32)alloc_size, log_fd) ) {
}
else {
ret_flag = FALSE;
}
}
else {
ret_flag = FALSE;
}
if( alloc_ptr ) {
OS_Free(alloc_ptr);
}
#endif
return ret_flag;
}
#ifdef COPY_FILE_ENCRYPTION
static BOOL CopyFileCrypto(const char *dst_path, const char *src_path, FSFile *log_fd )
{
FSFile f_src;
FSFile f_dst;
#ifdef LIMIT_BUF
u32 restSize;
u32 tempSize;
#else
char* pBuffer;
char* pBuffer_crypto;
#endif
u32 fileSize;
s32 readSize = 0;
FSResult fsResult;
@ -652,7 +506,6 @@ static BOOL CopyFileCrypto(const char *dst_path, const char *src_path, FSFile *l
fileSize = FS_GetFileLength(&f_src);
#ifdef LIMIT_BUF
FS_InitFile(&f_dst);
if( FALSE == FS_OpenFileEx(&f_dst, dst_path, FS_FILEMODE_W) ) {
@ -692,7 +545,6 @@ static BOOL CopyFileCrypto(const char *dst_path, const char *src_path, FSFile *l
}
}
#ifdef LIMIT_BUF_ALLOC
if( pBuffer == NULL ) {
pBuffer = (char*)OS_Alloc( BUF_SIZE );
@ -759,89 +611,6 @@ static BOOL CopyFileCrypto(const char *dst_path, const char *src_path, FSFile *l
}
}
#else /* LIMIT_BUF */
pBuffer = (char*)OS_Alloc( fileSize );
if( pBuffer == NULL ) {
miya_log_fprintf(log_fd, "%s Mem alloc error: %d\n", __FUNCTION__, fileSize);
}
else {
readSize = FS_ReadFile(&f_src, pBuffer, (s32)fileSize);
if( readSize != fileSize ) {
miya_log_fprintf(log_fd, "%s Failed Read File:%s\n",__FUNCTION__ , src_path );
miya_log_fprintf(log_fd, " request size=%d read size=%d\n", fileSize, readSize);
}
else {
/* crypto start */
pBuffer_crypto = (char*)OS_Alloc( fileSize );
if( pBuffer_crypto == NULL ) {
miya_log_fprintf(log_fd, "%s Mem(cryto) alloc error: %d\n", __FUNCTION__, fileSize);
}
else {
/* ここからWrite */
CRYPTO_RC4FastInit(&context, my_fs_key, my_fs_GetStringLength(my_fs_key));
CRYPTO_RC4FastEncrypt(&context, pBuffer, (u32)fileSize, pBuffer_crypto);
FS_InitFile(&f_dst);
if( FALSE == FS_OpenFileEx(&f_dst, dst_path, FS_FILEMODE_W) ) {
FSResult res = FS_GetArchiveResultCode(dst_path);
if( res == FS_RESULT_NO_ENTRY ) {
/* 本来ここで問題なし */
}
else {
miya_log_fprintf(log_fd, "%s Failed open file %d:\n", __FUNCTION__,__LINE__);
miya_log_fprintf(log_fd, " %s\n", dst_path );
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word(res) );
}
}
else {
FS_CloseFile(&f_dst);
/* backup バックアップを取っておくべき?? */
FS_DeleteFile(dst_path);
}
FS_CreateFile(dst_path, (FS_PERMIT_R|FS_PERMIT_W));
if( FALSE == FS_OpenFileEx(&f_dst, dst_path, FS_FILEMODE_W) ) {
miya_log_fprintf(log_fd, "%s Failed open file %d:\n", __FUNCTION__,__LINE__);
miya_log_fprintf(log_fd, " %s\n", dst_path );
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word(FS_GetArchiveResultCode(dst_path)) );
}
else {
fsResult = FS_SetFileLength(&f_dst, 0);
if( fsResult != FS_RESULT_SUCCESS ) {
miya_log_fprintf(log_fd, "%s Error: Set file len\n", __FUNCTION__);
miya_log_fprintf(log_fd, " %s\n", dst_path );
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word(fsResult) );
}
else {
writtenSize = FS_WriteFile(&f_dst, pBuffer_crypto, readSize);
if( writtenSize != readSize ) {
miya_log_fprintf(log_fd, "%s Failed write file %d:\n", __FUNCTION__,__LINE__);
miya_log_fprintf(log_fd, " %s\n", dst_path );
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word(FS_GetArchiveResultCode(dst_path)) );
}
else {
FS_FlushFile(&f_dst);
ret_flag = TRUE;
}
}
}
/* Write終了 */
}
/* crypto end */
}
}
if( pBuffer != NULL ) {
OS_Free(pBuffer);
}
if( pBuffer_crypto != NULL ) {
OS_Free(pBuffer_crypto);
}
#endif /* LIMIT_BUF */
exit_label:
if( FALSE == FS_CloseFile(&f_src) ) {
miya_log_fprintf(log_fd, "%s Failed Close File\n", __FUNCTION__ );
@ -854,7 +623,6 @@ static BOOL CopyFileCrypto(const char *dst_path, const char *src_path, FSFile *l
return ret_flag;
}
#endif
static BOOL CompareFsDateTime( FSDateTime *dt1, FSDateTime *dt2)
{
@ -1806,6 +1574,10 @@ BOOL RestoreDirEntryList( char *path , char *log_file_name, int *list_count, int
exit_label:
// (void)ClearDirEntryList( &readonly_list_head );
// FS_FlushFile(&f); //リードだからいらない
if( FS_CloseFile(&f) == FALSE) {
fsResult = FS_GetArchiveResultCode(path);
@ -1871,6 +1643,10 @@ BOOL RestoreDirEntryList_System_And_InstallSuccessApp(char *path , char *log_fil
return FALSE; /* error */
}
if( print_debug_flag == TRUE ) {
mprintf("\n");
}
while( 1 ) {
/* リストはルートディレクトリに近い順から入っている */
readSize = FS_ReadFile(&f, (void *)&list_temp, (s32)sizeof(MY_DIR_ENTRY_LIST) );
@ -2056,6 +1832,8 @@ BOOL RestoreDirEntryList_System_And_InstallSuccessApp(char *path , char *log_fil
miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word( fsResult ) );
}
// (void)ClearDirEntryList( &readonly_list_head );
miya_log_fprintf(log_fd, "%s END\n\n", __FUNCTION__);
if( log_active ) {
Log_File_Close(log_fd);

View File

@ -475,7 +475,6 @@ BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int
pTitleIds[i].is_personalized = 2 -> personalized
*/
if( title_id_buf_ptr[i].is_personalized == 1 ) {
/* common e-ticketのやつ */
/*
0x00030004484E474A "rom:/tads/TWL-KGUJ-v257.tad.out"
0x000300044B32444A "rom:/tads/TWL-K2DJ-v0.tad.out"
@ -502,7 +501,7 @@ BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int
m_set_palette(tc[0], M_TEXT_COLOR_GREEN ); /* green */
mprintf("OK.\n");
m_set_palette(tc[0], M_TEXT_COLOR_WHITE );
title_id_buf_ptr[i].install_success_flag = TRUE;
title_id_buf_ptr[i].install_success_flag = TRUE; /* これを入れとかないと後でセーブデータを復活しない */
}
}
else {

View File

@ -15,7 +15,7 @@
# $Rev: 3650 $
# $Author: okubata_ryoma $
#----------------------------------------------------------------------------
SUBDIRS = ./banner
SUBDIRS = ./banner ../files/tads
TARGET_PLATFORM := TWL
TWL_ARCHGEN := LIMITED
@ -58,18 +58,16 @@ LLIBRARIES += libecx$(TWL_LIBSUFFIX).a \
libsfs$(TWL_LIBSUFFIX).a \
libna$(TWL_LIBSUFFIX).a \
# libnssl$(TWL_LIBSUFFIX).a \
# libnhttp$(TWL_LIBSUFFIX).a \
MAKEROM_ROMROOT = ../files
MAKEROM_TAD_ROMFILES = tads/tad_table.txt \
tads/nandAppSample.tad tads/backupSample.tad tads/encodeSD.tad \
tads/TWL-KGUJ-v257.tad.out \
tads/TWL-K2DJ-v0.tad.out \
tads/TWL-HNGJ-v256.tad.out
MAKEROM_ROMFILES = fanfare.32.wav ok.wav ng.wav cursor.wav $(MAKEROM_TAD_ROMFILES)
TAD_TABLE_TXT = ../files/tads/tad_table.txt
include Makefile.TadIncludes
MAKEROM_ROMFILES = fanfare.32.wav ok.wav ng.wav cursor.wav tads/tad_table.txt $(MAKEROM_TAD_ROMFILES)
@ -95,10 +93,13 @@ MAKEROM := $(TWL_TOOLSDIR)/bin/makerom.TWL.secure.exe
#----------------------------------------------------------------------------
$(TARGET_AUTOBOOT): ./bin/$(TWL_BUILDTYPE)/$(TARGET_BIN)
../bin/FakeRomHeader.secure.exe -c -f $< $@
do-build: $(TARGETS) $(TARGET_AUTOBOOT)
do-build: $(TARGETS) $(TARGET_AUTOBOOT)
#----------------------------------------------------------------------------

View File

@ -214,8 +214,16 @@ void SetupTitlesDataFile(const MY_USER_APP_TID* pTitleIds, u32 numTitleIds)
for( u32 i = 0; i < numTitleIds; i++ )
{
// tid = pTitleIds[i];
tid = pTitleIds[i].tid;
NAM_SetupTitleDataFile(tid);
if( (pTitleIds[i].is_personalized == 2) &&
(pTitleIds[i].install_success_flag == TRUE) ) {
/*
personalized ticketのときは何もしない
pTitleIds[i].is_personalized = 1 -> common
pTitleIds[i].is_personalized = 2 -> personalized
*/
tid = pTitleIds[i].tid;
NAM_SetupTitleDataFile(tid);
}
}
}

View File

@ -207,58 +207,36 @@ static BOOL start_my_thread(u32 command)
return FALSE;
}
#if 0
static void DeleteKnownTitles(NAMTitleId* pTitleIds, int num)
{
int i;
NAMTitleId tid;
u32 hi;
/*
IDリスト
TitleID_Hi_TitleID_Lo
------------------------------------------
  00030017_484E41?? [HNA?] DSiメニュー
  00030015_484E42?? [HNB?]
  00030015_484E46?? [HNF?] DSiショップ
  0003000F_484E4341 [HNCA]
  0003000F_484E4841 [HNHA]
  0003000F_484E4C?? [HNL?]
  00030005_484E4441 [HNDA]
  00030005_484E4541 [HNEA] PictChat
  00030005_484E49?? [HNI?] DSiカメラ
  00030005_484E4A?? [HNJ?]
  00030005_484E4B?? [HNK?] DSiサウンド
  00030004_484E47?? [HNG?] DSiブラウザ
  00030004_4B4755?? [KGU?]
TitleID_Loの桁目
  ?=[J, E, P, U]=[4A, 45, 50, 55]
  "nand:/title/<titleID_Hi>/<titleID_Lo>"
*/
for( i = 0; i < num; i++ ) {
tid = *pTitleIds++;
hi = NAM_GetTitleIdHi(tid);
if( hi == 0x00030004 ) {
// NAM_DeleteTitleCompletely(tid);
NAM_DeleteTitle(tid);
}
}
}
#endif
/*
IDリスト
TitleID_Hi_TitleID_Lo
------------------------------------------
  00030017_484E41?? [HNA?] DSiメニュー
  00030015_484E42?? [HNB?]
  00030015_484E46?? [HNF?] DSiショップ
  0003000F_484E4341 [HNCA]
  0003000F_484E4841 [HNHA]
  0003000F_484E4C?? [HNL?]
  00030005_484E4441 [HNDA]
  00030005_484E4541 [HNEA] PictChat
  00030005_484E49?? [HNI?] DSiカメラ
  00030005_484E4A?? [HNJ?]
  00030005_484E4B?? [HNK?] DSiサウンド
  00030004_484E47?? [HNG?] DSiブラウザ
  00030004_4B4755?? [KGU?]
TitleID_Loの桁目
  ?=[J, E, P, U]=[4A, 45, 50, 55]
  "nand:/title/<titleID_Hi>/<titleID_Lo>"
*/
static void RTC_NTP_SYNC(void)
{
@ -738,7 +716,6 @@ static BOOL RestoreFromSDCard7(void)
(void)pre_install_Cleanup_User_Titles( log_fd );
}
if( no_network_flag == TRUE ) {
miya_log_fprintf(log_fd,"no network flag ON\n");
goto pre_install_label;
@ -825,17 +802,16 @@ static BOOL RestoreFromSDCard7(void)
if( ec_download_ret == ECDOWNLOAD_FAILURE ) {
ret_flag = FALSE;
miya_log_fprintf(log_fd, "%s failed ECDownload 1\n", __FUNCTION__);
goto end_ec_f;
}
else if( ec_download_ret == ECDOWNLOAD_DUMMY ) {
ret_flag = FALSE;
miya_log_fprintf(log_fd, "%s failed ECDownload 2\n", __FUNCTION__);
goto end_ec_f;
}
// 不要:セーブデータ領域を作成
// NAM_Init を忘れてた
// SetupTitlesDataFile((NAMTitleId *)title_id_buf_ptr , (u32)title_id_count);
SetupTitlesDataFile((MY_USER_APP_TID *)title_id_buf_ptr , (u32)title_id_count);
end_ec_f:
@ -917,6 +893,7 @@ static BOOL RestoreFromSDCard8(void)
if( mydata.num_of_app_save_data > 0 ) {
mprintf("App. save data restore ");
if( TRUE == RestoreDirEntryList_System_And_InstallSuccessApp( MyFile_GetSaveDataListFileName() ,
MyFile_GetSaveDataRestoreLogFileName(),
&list_count, &error_count,
@ -1000,7 +977,7 @@ static void MyThreadProc(void *arg)
m_set_palette(tc[0], M_TEXT_COLOR_WHITE );
if( miya_debug_level == 1 ) {
if( miya_debug_level == 1 || ( my_fs_get_print_debug_flag() == TRUE )) {
m_set_palette(tc[0], M_TEXT_COLOR_PINK );
mprintf("Free mem size %d bytes\n", OS_GetTotalFreeSize(OS_ARENA_MAIN, hHeap));
m_set_palette(tc[0], M_TEXT_COLOR_WHITE );
@ -1009,7 +986,7 @@ static void MyThreadProc(void *arg)
if( FALSE == (function_table[function_counter])() ) {
flag = FALSE;
}
if( miya_debug_level == 1 ) {
if( miya_debug_level == 1 || ( my_fs_get_print_debug_flag() == TRUE )) {
m_set_palette(tc[0], M_TEXT_COLOR_PINK );
mprintf("Free mem size %d bytes\n", OS_GetTotalFreeSize(OS_ARENA_MAIN, hHeap));
m_set_palette(tc[0], M_TEXT_COLOR_WHITE );

View File

@ -0,0 +1,4 @@
TAD_TABLE_FILE = tad_table.txt
$(TAD_TABLE_FILE):
../../bin/make_tad_table.exe -dir . -o $@ -mk ../../copy_dst/Makefile.TadIncludes

View File

@ -1,18 +0,0 @@
/*
title id
0x00000000 00000000, region , country code, file name,
*/
--00030004_484E47?? [HNG?] DSiブラウザ
--00030004_4B4755?? [KGU?] うごくメモ帳
-- TitleID_Loの桁目言語コード
-- ?=[J, E, P, U]=[4A, 45, 50, 55]
0x00030004484E474A, 0 , 0 ,rom:/tads/TWL-HNGJ-v256.tad.out,
0x000300044B32444A, 0 , 0 ,rom:/tads/TWL-K2DJ-v0.tad.out,
0x000300044B47554A, 0 , 0 ,rom:/tads/TWL-KGUJ-v257.tad.out,

View File

@ -0,0 +1,42 @@
PACKAGE = make_tad_table
SRCS = make_tad_table.c
HEADS =
OBJS = $(SRCS:.c=.o)
CC := C:/Cygwin/bin/gcc
CFLAGS = -mno-cygwin
CPPFLAGS= -I.
LD = gcc
LDFLAGS = -Wl,--subsystem,console -mwindows -mno-cygwin
LDLIBS =
.SUFFIXES:
.SUFFIXES: .o .c
all: install
install: $(PACKAGE)
install -c -m 777 $(PACKAGE) ../bin
$(PACKAGE): $(OBJS)
$(LD) $(LDFLAGS) $(OBJS) -o $@ $(LDLIBS)
$(OBJS): $(HEADS) Makefile
.c.o:
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
.PHONY: clean clobber
clean clobber:
$(RM) $(OBJS) $(PACKAGE).exe

View File

@ -0,0 +1,502 @@
/*
gcc -mwindows -Wl,--subsystem,console -mno-cygwin -o titleidchecker titleidchecker.c
titleidchecker TWL-HNGJ-v256.tad.out
titleidchecker nandAppSample.tad
Makefile.TadIncludesも作る
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned long u32;
typedef unsigned long long u64;
typedef int BOOL;
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* tadのデータは基本的にビッグエンディアン */
typedef struct {
u32 hdrSize;
u16 tadType;
u16 tadVersion;
u32 certSize;
u32 crlSize;
u32 ticketSize;
u32 tmdSize;
u32 contentSize;
u32 metaSize;
u32 certOffset;
u32 crlOffset;
u32 ticketOffset;
u32 tmdOffset;
u32 contentOffset;
u32 metaOffset;
u32 fileSize;
} TAD_INFO;
static BOOL debug_print_flag = FALSE;
static u32 reverseEndian4( const u32 v )
{
u32 ret = (v<<24) | ((v<<8) & 0x00FF0000) | ((v>>8) & 0x0000FF00) | (v>>24);
return ret;
}
static u16 reverseEndian2( const u16 v )
{
u16 ret = (v<<8) | (v>>8);
return ret;
}
static u32 roundUp4( const u32 v, const u32 align )
{
u32 r = ((v + align - 1) / align) * align;
return r;
}
static u16 roundUp2( const u16 v, const u16 align )
{
u16 r = ((v + align - 1) / align) * align;
return r;
}
static u16 read2bytes(FILE *fp)
{
u16 buf;
if( 2 != fread(&buf,1,2,fp) ) {
fprintf(stderr, "Error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(-1);
}
return buf;
}
static u32 read4bytes(FILE *fp)
{
u32 buf;
if( 4 != fread(&buf,1,4,fp) ) {
fprintf(stderr, "Error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
exit(-1);
}
return buf;
}
static BOOL read_tad_info(FILE *fp, TAD_INFO *tad_info)
{
tad_info->hdrSize = reverseEndian4( read4bytes(fp) ); // 基本的にビッグエンディアン
tad_info->tadType = reverseEndian2( read2bytes(fp) );
tad_info->tadVersion = reverseEndian2( read2bytes(fp) );
tad_info->certSize = reverseEndian4( read4bytes(fp) );
tad_info->crlSize = reverseEndian4( read4bytes(fp) );
tad_info->ticketSize = reverseEndian4( read4bytes(fp) );
tad_info->tmdSize = reverseEndian4( read4bytes(fp) );
tad_info->contentSize = reverseEndian4( read4bytes(fp) );
tad_info->metaSize = reverseEndian4( read4bytes(fp) );
if( tad_info->hdrSize != 32) {
return FALSE;
}
if( debug_print_flag ) {
printf( "hdrSize %d\n", tad_info->hdrSize );
printf( "tadType %c%c\n", tad_info->tadType>>8, tad_info->tadType&0xFF );
printf( "tadVersion %d\n", tad_info->tadVersion );
printf( "certSize %d\n", tad_info->certSize );
printf( "crlSize %d\n", tad_info->crlSize );
printf( "ticketSize %d\n", tad_info->ticketSize );
printf( "tmdSize %d\n", tad_info->tmdSize );
printf( "contentSize %d\n", tad_info->contentSize );
printf( "metaSize %d\n", tad_info->metaSize );
}
tad_info->certOffset = roundUp4( tad_info->hdrSize , 64 );
tad_info->crlOffset = roundUp4( tad_info->certOffset + tad_info->certSize , 64 );
tad_info->ticketOffset = roundUp4( tad_info->crlOffset + tad_info->crlSize , 64 );
tad_info->tmdOffset = roundUp4( tad_info->ticketOffset + tad_info->ticketSize , 64 );
tad_info->contentOffset = roundUp4( tad_info->tmdOffset + tad_info->tmdSize , 64 );
tad_info->metaOffset = roundUp4( tad_info->contentOffset+ tad_info->contentSize , 64 );
tad_info->fileSize = roundUp4( tad_info->metaOffset + tad_info->metaSize , 64 );
return TRUE;
}
static u8 *alloc_and_read(FILE *fp, int offset, int size)
{
u8 *buf;
if( fp == NULL || size < 1 ) {
fprintf(stderr, "Error %s open file\n",__FUNCTION__);
return NULL;
}
buf = malloc(size);
if( buf == NULL ) {
fprintf(stderr, "Error %s memory allocate \n",__FUNCTION__);
return NULL;
}
if( -1 == fseek( fp, offset, SEEK_SET ) ) {
fprintf(stderr, "Error: %s %s %d fseek offset=%d\n",__FILE__,__FUNCTION__,__LINE__,offset);
return NULL;
}
if( size != fread(buf,1,size,fp) ) {
fprintf(stderr, "Error: %s %s %d fread size=%d\n",__FILE__,__FUNCTION__,__LINE__,size);
return NULL;
}
return buf;
}
static BOOL saveFile(char *name, char *buf, int size)
{
FILE *fp;
fp = fopen(name, "wb");
if( fp == NULL ) {
fprintf(stderr, "Error %s %d file open\n",__FUNCTION__,__LINE__);
return FALSE;
}
if( fwrite(buf, 1, size, fp) != size ) {
fprintf(stderr, "Error %s %d file write\n",__FUNCTION__,__LINE__);
return FALSE;
}
fclose( fp );
return TRUE;
}
static void print_gamecode(u32 tid_lo)
{
char gamecode[5];
char *str;
str = gamecode;
*str++ = (char)((tid_lo >> 24) & 0xff);
*str++ = (char)((tid_lo >> 16) & 0xff);
*str++ = (char)((tid_lo >> 8) & 0xff);
*str++ = (char)(tid_lo & 0xff);
*str = '\0';
printf( "%s", gamecode );
}
static write_tad_table_form(FILE *fp, u32 tid_hi, u32 tid_lo, char *filename)
{
fprintf(fp, "0x%08x%08x, %d , %d , rom:/tads/%s\n",tid_hi, tid_lo, 0 , 0 , filename);
}
static void read_file_and_print_titleid( char *path , char *d_name, FILE *fp_out, FILE *fp_mk)
{
FILE *fp_in = NULL;
TAD_INFO tad_info;
u8 *tmd = NULL;
u32 titleid_hi = 0;
u32 titleid_lo = 0;
fp_in = fopen(path,"rb"); /* fseek効かすため */
if( fp_in == NULL ) {
fprintf(stderr, "error: file open %s\n",path);
goto end_file;
}
if( FALSE == read_tad_info(fp_in, &tad_info) ) {
// fprintf(stderr, "error:%s %d\n",__FUNCTION__,__LINE__);
goto end_file;
}
fseek( fp_in, 0, SEEK_END );
if( tad_info.fileSize != ftell( fp_in ) ) {
printf( "file size is not expected size(=%d)", tad_info.fileSize );
goto end_file;
}
tmd = alloc_and_read( fp_in, tad_info.tmdOffset, tad_info.tmdSize );
titleid_hi = reverseEndian4( *((u32 *)( tmd + 0x18C )) );
titleid_lo = reverseEndian4( *((u32 *)( tmd + 0x18C + 4 )) );
if( debug_print_flag ) {
printf("inputfile = %s\n", path);
}
if( fp_out ) {
fprintf(fp_out, "0x%08x%08x, %d , %d , rom:/tads/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name);
if( fp_mk ) {
fprintf(fp_mk, "\t\ttads/%s \\\n", d_name);
}
}
else {
printf("0x%08x%08x, %d , %d , rom:/tads/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name);
}
end_file:
if(tmd) {
free(tmd);
}
if( fp_in ) {
fclose(fp_in);
}
}
int main(int argc, char **argv)
{
FILE *fp_in = NULL;
FILE *fp_out = NULL;
FILE *fp_mk = NULL;
char *infile = NULL;
char *outfile = NULL;
char *dir_name = NULL;
char *mkfile = NULL;
BOOL read_file_flag = FALSE;
BOOL write_file_flag = FALSE;
BOOL dir_read_flag = FALSE;
BOOL mk_file_flag = FALSE;
char *prog;
int badops = 0;
TAD_INFO tad_info;
u8 *ticket = NULL;
u8 *tmd = NULL;
u8 *content = NULL;
u64 titleid = 0;
u32 titleid_hi = 0;
u32 titleid_lo = 0;
u16 groupid = 0;
DIR *dir;
struct dirent *dr;
struct stat st;
char *full_path;
prog=argv[0];
argc--;
argv++;
while (argc >= 1) {
if (strcmp(*argv,"-dir") == 0 && !dir_read_flag ) {
if (--argc < 1) {
goto bad;
}
dir_name = *++argv;
dir_read_flag = TRUE;
}
else if (strcmp(*argv,"-o") == 0 && !write_file_flag ) {
if (--argc < 1) {
goto bad;
}
outfile = *++argv;
write_file_flag = TRUE;
}
else if ( strcmp(*argv,"-mk") == 0 && !mk_file_flag ) {
if (--argc < 1) {
goto bad;
}
mkfile = *++argv;
mk_file_flag = TRUE;
}
else if (strcmp(*argv,"-d") == 0 ) {
debug_print_flag = TRUE;
}
else if ( !read_file_flag ) {
infile = *argv;
read_file_flag = TRUE;
}
else {
goto bad;
}
argc--;
argv++;
}
if (badops) {
bad:
fprintf(stderr, "%s -dir dirname -o outfile -mk mkfile\n",prog);
goto end;
}
if( dir_read_flag == TRUE) {
if( debug_print_flag ) {
printf("dir name = %s\n", dir_name);
}
dir = opendir(dir_name);
if( dir == NULL ) {
fprintf(stderr, "error: dir open %s\n",dir_name);
goto end;
}
if( write_file_flag ) {
fp_out = fopen(outfile,"wb"); /* fseek効かすため */
if( fp_out == NULL ) {
fprintf(stderr, "error: file open %s\n",outfile);
goto end;
}
}
if( mk_file_flag ) {
fp_mk = fopen(mkfile,"wb"); /* fseek効かすため */
if( fp_mk == NULL ) {
fprintf(stderr, "error: file open %s\n",mkfile);
goto end;
}
fprintf(fp_mk, "MAKEROM_TAD_ROMFILES = \\\n");
}
while( (dr = readdir(dir)) != NULL ) {
if (!strcmp(dr->d_name, ".") || !strcmp(dr->d_name, "..")) {
continue;
}
full_path = malloc( strlen(dir_name) + strlen(dr->d_name) + 2);
strcpy(full_path, dir_name);
strcat(full_path, "/");
strcat(full_path, dr->d_name);
// printf("%s\n", full_path);
if ( stat(full_path, &st) != 0 ) {
free( full_path);
continue;
}
if (S_ISDIR(st.st_mode) == 1) {
if( debug_print_flag ) {
printf("DIR %s\n", dr->d_name);
}
}
else {
if( debug_print_flag ) {
printf("FILE %s\n", dr->d_name);
}
if( st.st_size >= 32 ) {
read_file_and_print_titleid( full_path ,dr->d_name, fp_out , fp_mk );
}
}
free( full_path);
}
if( dir ) {
(void)closedir( dir );
}
if( write_file_flag ) {
if( fp_out ) {
fclose(fp_out);
fp_out = NULL;
}
}
if( mk_file_flag ) {
if( fp_mk ) {
fclose(fp_mk);
fp_mk = NULL;
}
}
}
else {
fp_in = fopen(infile,"rb"); /* fseek効かすため */
if( fp_in == NULL ) {
fprintf(stderr, "error: file open %s\n",infile);
goto end;
}
if( FALSE == read_tad_info(fp_in, &tad_info) ) {
fprintf(stderr, "%s infile\n",prog);
goto end_file;
}
fseek( fp_in, 0, SEEK_END );
if( tad_info.fileSize != ftell( fp_in ) ) {
printf( "file size is not expected size(=%d)", tad_info.fileSize );
goto end_file;
}
ticket = alloc_and_read( fp_in, tad_info.ticketOffset, tad_info.ticketSize );
tmd = alloc_and_read( fp_in, tad_info.tmdOffset, tad_info.tmdSize );
content = alloc_and_read( fp_in, tad_info.contentOffset, tad_info.contentSize );
// memcpy( (void *)&titleid, (void *)(tmd + 0x18C), 8 );
// memcpy( (void *)&groupid, (void *)(tmd + 0x198), 2 );
// printf("titleid = 0x%08x %08x\n", ((titleid) >> 32), (0xffffffff & titleid));
titleid_hi = reverseEndian4( *((u32 *)( tmd + 0x18C )) );
titleid_lo = reverseEndian4( *((u32 *)( tmd + 0x18C + 4 )) );
groupid = reverseEndian2( *((u16 *)( tmd + 0x198)) );
printf("inputfile = %s\n", infile);
printf("titleid = 0x%08x %08x ", titleid_hi, titleid_lo);
printf("[");
print_gamecode(titleid_lo);
printf("]");
printf("\n");
end_file:
if( ticket ) {
free(ticket);
}
if(tmd) {
free(tmd);
}
if(content) {
free(content);
}
if( fp_in ) {
fclose(fp_in);
}
if( fp_out ) {
fclose(fp_out);
}
if( fp_mk ) {
fclose(fp_mk);
}
}
end:
return 0;
}