diff --git a/build/tools/sctools/Makefile b/build/tools/sctools/Makefile index d0bb45c..b6b7ae7 100644 --- a/build/tools/sctools/Makefile +++ b/build/tools/sctools/Makefile @@ -18,7 +18,7 @@ -SUBDIRS = make_tad_table my_armadillo.TWL copy_org copy_dst +SUBDIRS = make_tad_table my_armadillo.TWL copy_org copy_dst auto_preinst_rom include $(TWLSDK_ROOT)/build/buildtools/commondefs diff --git a/build/tools/sctools/auto_preinst_rom/Makefile b/build/tools/sctools/auto_preinst_rom/Makefile new file mode 100644 index 0000000..825545c --- /dev/null +++ b/build/tools/sctools/auto_preinst_rom/Makefile @@ -0,0 +1,119 @@ +#! make -f +#---------------------------------------------------------------------------- +# Project: TwlSDK - demos - MI - ndma-1 +# File: Makefile +# +# 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:: 2008-01-17#$ +# $Rev: 3650 $ +# $Author: okubata_ryoma $ +#---------------------------------------------------------------------------- +SUBDIRS = ./banner ../files/tads ../files/taddevs + +TARGET_PLATFORM := TWL +TWL_ARCHGEN := LIMITED + +SRCDIR = ../common/src ./src + +SRCS = main.c key.c font.c text.c mprintf.c logprintf.c \ + gfx.c hwi.c my_fs_util.c \ + error_report.c \ + myfilename.c menu_version.c \ + myimport.c pre_install.c + +TARGET_BIN = auto_preinst_rom.srl +ROM_SPEC = auto_preinst_rom.rsf +# TARGET_AUTOBOOT = ./bin/$(TWL_BUILDTYPE)/auto_preinst_rom.srl + + +ES_DIR = $(ROOT)/add-ins/es +ES_LATEST = $(shell ls -d $(ES_DIR)/es-sdk-*|sort|tail -n 1) +ES_ROOT = $(ES_LATEST) + +LINCLUDES = $(TWLSDK_ROOT)/build/libraries/lcfg/ARM9.TWL/include ../common/src \ + $(TWLSDK_ROOT)/build/libraries/nam/ARM9.TWL/include \ + $(TWLSDK_ROOT)/build/libraries/ese/common/include \ + $(ES_ROOT)/twl/include $(ES_ROOT)/common/lib/ec/include + +LLIBRARY_DIRS += $(ES_ROOT)/twl/lib/$(TWL_BUILDTYPE) ../wifilib/$(TWL_BUILDTYPE) +# LLIBRARY_DIRS += $(ES_ROOT)/twl/lib/$(TWL_BUILDTYPE) + + +LLIBRARIES += libecx$(TWL_LIBSUFFIX).a \ + liblcfg$(TWL_LIBSUFFIX).a \ + libnhttp.nssl$(TWL_LIBSUFFIX).a \ + libshr$(TWL_LIBSUFFIX).a \ + libnuc$(TWL_LIBSUFFIX).a \ + libnup$(TWL_LIBSUFFIX).a \ + libnam$(TWL_LIBSUFFIX).a \ + libes$(TWL_LIBSUFFIX).a \ + libsea$(TWL_LIBSUFFIX).a \ + libboc$(TWL_LIBSUFFIX).a \ + libsfs$(TWL_LIBSUFFIX).a \ + libna$(TWL_LIBSUFFIX).a \ + + +MAKEROM_ROMROOT = ../files + +TAD_TABLE_TXT = ../files/tads/tad_table.txt + +TADDEV_TABLE_TXT = ../files/taddevs/taddev_table.txt + +MAKE_TAD_INCLUDE_FILE = ../copy_dst/MakefileTad.inc + +MAKE_TADDEV_INCLUDE_FILE = ../copy_dst/MakefileTadDev.inc + +-include $(MAKE_TAD_INCLUDE_FILE) + +-include $(MAKE_TADDEV_INCLUDE_FILE) + +MAKEROM_ROMFILES = tads/tad_table.txt taddevs/taddev_table.txt $(MAKEROM_TAD_ROMFILES) $(MAKEROM_TADDEV_ROMFILES) + +MY_TWL_COMPONENTSDIR = ../my_armadillo.TWL/bin + +COMPONENT_NAME = armadillo + +MAKEROM_ARM7_BASE = $(MY_TWL_COMPONENTSDIR)/$(TWL_BUILDTYPE_ARM7)/$(COMPONENT_NAME) + +MAKEROM_ARM7 = $(MAKEROM_ARM7_BASE).$(TWL_ELF_EXT) + + +LDIRT_CLEAN = + + +include $(TWLWIFI_ROOT)/build/buildtools/commondefs +include $(TWLSDK_ROOT)/build/buildtools/commondefs + + +#---------------------------------- +# セキュアアプリ指定 + +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) + + +$(BINDIR)/$(TARGET_BIN_BASENAME).$(TWL_ELF_EXT): $(MAKE_TAD_INCLUDE_FILE) $(MAKE_TADDEV_INCLUDE_FILE) $(TAD_TABLE_TXT) $(TADDEV_TABLE_TXT) + + +#---------------------------------------------------------------------------- + +include $(TWLWIFI_ROOT)/build/buildtools/modulerules +include $(TWLSDK_ROOT)/build/buildtools/modulerules diff --git a/build/tools/sctools/auto_preinst_rom/auto_preinst_rom.rsf b/build/tools/sctools/auto_preinst_rom/auto_preinst_rom.rsf new file mode 100644 index 0000000..121fc9a --- /dev/null +++ b/build/tools/sctools/auto_preinst_rom/auto_preinst_rom.rsf @@ -0,0 +1,325 @@ +#---------------------------------------------------------------------------- +# Project: TwlSDK - include +# File: ROM-TS.rsf +# +# Copyright 2007-2008 Nintendo. All rights reserved. +# +# These coded insructions, 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:: 2008-09-26#$ +# $Rev: 8709 $ +# $Author: hatamoto_minoru $ +#---------------------------------------------------------------------------- +# +# TWL ROM SPEC FILE +# + +Arm9 +{ + Static "$(MAKEROM_ARM9:r).TWL.FLX.sbin$(COMPSUFFIX9)" + OverlayDefs "$(MAKEROM_ARM9:r)_defs.TWL.FLX.sbin$(COMPSUFFIX9)" + OverlayTable "$(MAKEROM_ARM9:r)_table.TWL.FLX.sbin$(COMPSUFFIX9)" + Elf "$(MAKEROM_ARM9:r).tef" +} + +Arm7 +{ + Static "$(MAKEROM_ARM7_BASE:r).TWL.FLX.sbin$(COMPSUFFIX7)" + OverlayDefs "$(MAKEROM_ARM7_BASE:r)_defs.TWL.FLX.sbin$(COMPSUFFIX7)" + OverlayTable "$(MAKEROM_ARM7_BASE:r)_table.TWL.FLX.sbin$(COMPSUFFIX7)" + Elf "$(MAKEROM_ARM7_BASE:r).tef" +} + +Arm9.Ltd +{ + Static "$(MAKEROM_ARM9:r).TWL.LTD.sbin$(COMPSUFFIX9)" + OverlayDefs "$(MAKEROM_ARM9:r)_defs.TWL.LTD.sbin$(COMPSUFFIX9)" + OverlayTable "$(MAKEROM_ARM9:r)_table.TWL.LTD.sbin$(COMPSUFFIX9)" +} + +Arm7.Ltd +{ + Static "$(MAKEROM_ARM7_BASE:r).TWL.LTD.sbin$(COMPSUFFIX7)" + OverlayDefs "$(MAKEROM_ARM7_BASE:r)_defs.TWL.LTD.sbin$(COMPSUFFIX7)" + OverlayTable "$(MAKEROM_ARM7_BASE:r)_table.TWL.LTD.sbin$(COMPSUFFIX7)" +} + +Property +{ + ### + ### Settings for FinalROM + ### + #### BEGIN + # + # TITLE NAME: Your product name within 12bytes + # + #TitleName "MY APP NAME" + + # + # MAKER CODE: Your company ID# in 2 ascii words + # issued by NINTENDO + # + MakerCode 01 + + # + # REMASTER VERSION: Mastering version + # + #RemasterVersion 0 + + # + # ROM SPEED TYPE: [MROM/1TROM/UNDEFINED] + # + RomSpeedType $(MAKEROM_ROMSPEED) + + # + # ROM SIZE: in bit [64M/128M/256M/512M/1G/2G/4G] + # + #RomSize 256M + + # + # ROM PADDING: TRUE if finalrom + # + #RomFootPadding TRUE + + # + # ROM HEADER TEMPLATE: Provided to every product by NINTENDO + # + #RomHeaderTemplate ./etc/rom_header.template.sbin + + # + # BANNER FILE: generated from Banner Spec File + # + BannerFile "banner/banner.bnr" + + + # + # Permit LandingNormalJump: for TWL "ApplicationJump" function [TRUE/FALSE] + # + #PermitLandingNormalJump FALSE + + # + # Permit LandingTmpJump: for TWL "ApplicationJump" function [TRUE/FALSE] + # + #PermitLandingTmpJump FALSE + + ### + ### Setting for TWL + ### + + # + # ROM HEADER Ltd: Provided to every product by NINTENDO + # + RomHeaderLtd $(TWLSDK_ROOT)/tools/bin/rom_header.LTD.sbin + + # + # Digest parameters: + # + DigestParam 1024 32 + + # + # WRAM mapping: [MAP_BB_HYB/MAP_BB_LTD/MAP_TS_HYB/MAP_TS_LTD + # MAP2_BB_HYB/MAP2_BB_LTD/MAP2_TS_HYB/MAP2_TS_LTD] + # don't have to edit + # + WramMapping MAP_TS_SCR + + # + # CardRegion: card region [Japan/America/Europe/Australia/China/Korea] + # + CardRegion ALL + + # + # CommonClientKey: launcher deliver common client Key [TRUE/FALSE] + # + CommonClientKey TRUE + + # + # HwAESSlotA_SSLClientCert: launcher deliver HW AES slot A setting [TRUE/FALSE] + # + HwAESSlotA_SSLClientCert TRUE + + # + # HwAESSlotB: launcher deliver HW AES slot B setting [TRUE/FALSE] + # + HwAESSlotB TRUE + + # + # HwAESSlotB for Sign JPEG for Launcher: launcher deliver HW AES slot B setting for Sign JPEG for Launcher [TRUE/FALSE] + # + #HwAESSlotB_SignJPEGForLauncher FALSE + + # + # HwAESSlotB for Sign JPEG for User: launcher deliver HW AES slot B setting for Sign JPEG for User [TRUE/FALSE] + # + #HwAESSlotB_SignJPEGForUser FALSE + + # + # HwAESSlotC: launcher deliver HW AES slot C setting [TRUE/FALSE] + # + #HwAESSlotC FALSE + + # + # SDCardAccess: sd card access control [TRUE/FALSE] + # + SDCardAccess ReadWrite + + # + # NANDAccess: NAND access control [TRUE/FALSE] + # + NANDAccess TRUE + + # + # Codec mode: + # don't have to edit + # + CodecMode $(MAKEROM_CODEC_MODE) + + # + # Disp WiFiConnection Icon for Launcher [TRUE/FALSE] + # + #WiFiConnectionIcon FALSE + + # + # Disp DSWireless Icon for Launcher [TRUE/FALSE] + # + #DSWirelessIcon FALSE + + # + # Disable debug [TRUE/FALSE] + # + DisableDebug FALSE + + # + # Agree EULA [TRUE/FALSE] + # + #AgreeEULA FALSE + + # + # Agree EULA version [1 - 255] + # + #AgreeEULAVersion 1 + + ### + #### END +} + +AppendProperty +{ + # + # Publisher : "Nintendo" + # don't have to edit + #Publisher Nintendo + + # + # Application type : [USER/SYSTEM] + # don't have to edit + AppType System + + # + # launch title on the launcher : [TRUE/FALSE] + # don't have to edit + #Launch TRUE + + # + # Boot allowed Media: [GameCard] + # + Media GameCard + + # + # Data only title : [TRUE/FALSE] + # don't have to edit + #DataOnly FALSE + + # + # Secure title : [TRUE/FALSE] + # don't have to edit + Secure TRUE + + # + # InitialCode : Your InitialCode in 4 ascii words + # + #InitialCode ABCJ + + # + # Public save data size: [0-/0K-/0M-] + # + #PublicSaveDataSize 0K + + # + # Private save data size: [0-/0K-/0M-] + # + #PrivateSaveDataSize 0K + + # + # Enable SubBannerFile: [TRUE/FALSE] + # + #SubBannerFile FALSE + + # + # Game card power on: [TRUE/FALSE] + # + GameCardOn FALSE + + # + # Game card transferd to nitro mode: [TRUE/FALSE] + # + #GameCardNitroMode FALSE + + # + # Shared2 file size: [DISABLE/0-4194304/0K-4096K/0M-4M] + # + #Shared2File0Size Disable + #Shared2File1Size Disable + #Shared2File2Size Disable + #Shared2File3Size Disable + #Shared2File4Size Disable + #Shared2File5Size Disable +} + +RomSpec +{ + Offset 0x00000000 + Segment ALL + HostRoot $(MAKEROM_ROMROOT) + Root / + File $(MAKEROM_ROMFILES) +} + +Rating +{ + # + # Permited age to play for each rating organization + # + # Supported organization + # - CERO (OGN0) : for Japan + # - ESRB (OGN1) : for North America + # - BBFC (OGN2) : obsolete organization + # - USK (OGN3) : for German + # - PEGI_GEN (OGN4) : for Europe + # - PEGI_FINLAND (OGN5) : obsolete organization + # - PEGI_PRT (OGN6) : for Portugal + # - PEGI_BBFC (OGN7) : for UK + # - OFLC (OGN8) : for Australia and NewZealand + # - GRB (OGN9) : for Korea + # - OGN10 : reserved + # - OGN11 : reserved + # - OGN12 : reserved + # - OGN13 : reserved + # - OGN14 : reserved + # - OGN15 : reserved + # + # Available age [ 0 - 31 / PENDING / FREE ] + + CERO FREE +# ESRB FREE +# USK FREE +# PEGI_GEN FREE +# PEGI_PRT FREE +# PEGI_BBFC FREE +# OFLC FREE +# GRB FREE +} diff --git a/build/tools/sctools/auto_preinst_rom/banner/Makefile b/build/tools/sctools/auto_preinst_rom/banner/Makefile new file mode 100644 index 0000000..4f2a5fc --- /dev/null +++ b/build/tools/sctools/auto_preinst_rom/banner/Makefile @@ -0,0 +1,59 @@ +#! make -f +#---------------------------------------------------------------------------- +# Project: TwlSDK - nandApp - demos - SubBanner +# File: Makefile +# +# 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:: 2008-06-27#$ +# $Rev: 6887 $ +# $Author: nakasima $ +#---------------------------------------------------------------------------- +TARGET_PLATFORM = TWL + +include $(TWLSDK_ROOT)/build/buildtools/commondefs +MAKEBANNER = $(TWL_TOOLSDIR)/bin/makebanner.TWL.exe + +ICON_DIR = ./icon + +BANNER_ICON = $(ICON_DIR)/gameIcon.bmp +BANNER_SPEC = banner_v3.bsf + +SUB_BANNER_ICON = $(ICON_DIR)/subIcon.bmp +SUB_BANNER_SPEC = sub_banner_v3.bsf + + +TARGETS = banner.bnr +SUB_TARGETS = sub_banner.bnr +INSTALL_DIR = ./ +INSTALL_TARGETS = $(TARGETS) $(SUB_TARGETS) + +BANNER_ICON_NAME = $(basename $(BANNER_ICON)) +BANNER_ICON_MIDDLE = $(addprefix $(BANNER_ICON_NAME), .nbfs .nbfc .nbfp) +SUB_BANNER_ICON_NAME = $(basename $(SUB_BANNER_ICON)) +SUB_BANNER_ICON_MIDDLE = $(addprefix $(SUB_BANNER_ICON_NAME), .nbfs .nbfc .nbfp) + +LDIRT_CLEAN = $(TARGETS) $(SUB_TARGETS) \ + $(BANNER_ICON_MIDDLE) \ + $(SUB_BANNER_ICON_MIDDLE) \ + $(TARGETS:.bnr=.srl) + +include $(TWLSDK_ROOT)/build/buildtools/modulerules + +#---------------------------------------------------------------------------- +# build +#---------------------------------------------------------------------------- +do-build: $(TARGETS) $(SUB_TARGETS) + +$(TARGETS): $(BANNER_SPEC) $(BANNER_ICON) $(BANNER_ICON_MIDDLE) + $(MAKEBANNER) -N $(BANNER_ICON_NAME) $(BANNER_SPEC) $(TARGETS) + +$(SUB_TARGETS): $(SUB_BANNER_SPEC) $(SUB_BANNER_ICON) $(SUB_BANNER_ICON_MIDDLE) + $(MAKEBANNER) -s -N $(SUB_BANNER_ICON_NAME) $(SUB_BANNER_SPEC) $(SUB_TARGETS) +# diff --git a/build/tools/sctools/auto_preinst_rom/banner/banner_v3.bsf b/build/tools/sctools/auto_preinst_rom/banner/banner_v3.bsf new file mode 100644 index 0000000..83ddd0a Binary files /dev/null and b/build/tools/sctools/auto_preinst_rom/banner/banner_v3.bsf differ diff --git a/build/tools/sctools/auto_preinst_rom/banner/icon/gameIcon.bmp b/build/tools/sctools/auto_preinst_rom/banner/icon/gameIcon.bmp new file mode 100644 index 0000000..5d968bd Binary files /dev/null and b/build/tools/sctools/auto_preinst_rom/banner/icon/gameIcon.bmp differ diff --git a/build/tools/sctools/auto_preinst_rom/banner/icon/subIcon.bmp b/build/tools/sctools/auto_preinst_rom/banner/icon/subIcon.bmp new file mode 100644 index 0000000..5fc9e3b Binary files /dev/null and b/build/tools/sctools/auto_preinst_rom/banner/icon/subIcon.bmp differ diff --git a/build/tools/sctools/auto_preinst_rom/banner/sub_banner_v3.bsf b/build/tools/sctools/auto_preinst_rom/banner/sub_banner_v3.bsf new file mode 100644 index 0000000..2fd176b Binary files /dev/null and b/build/tools/sctools/auto_preinst_rom/banner/sub_banner_v3.bsf differ diff --git a/build/tools/sctools/auto_preinst_rom/src/main.c b/build/tools/sctools/auto_preinst_rom/src/main.c new file mode 100644 index 0000000..4ffd7ed --- /dev/null +++ b/build/tools/sctools/auto_preinst_rom/src/main.c @@ -0,0 +1,504 @@ +#include +#include +#include +#include +#include +#include + +#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) + /* 処理中(データ書き込み中)ならリセットをブロックしなければならない */ + 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(); + // マスター割り込みフラグを許可に + (void)OS_EnableIrq(); + + // IRQ 割り込みを許可します + (void)OS_EnableInterrupts(); + + // ARM7との通信FIFO割り込み許可 + (void)OS_EnableIrqMask(OS_IE_SPFIFO_RECV); + + // ファイルシステム初期化 + FS_Init( FS_DMA_NOT_USE ); + + PM_Init(); + // メインアリーナのアロケートシステムを初期化 + newArenaLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1); + OS_SetMainArenaLo(newArenaLo); + + // メインアリーナ上にヒープを作成 + 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 ); + + // 必須;SEA の初期化 + 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 */ + } + + + // ニックネームが空なら適当に設定 + 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 + + // 国が選択されていないなら適当に設定 + 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 の初期化 + 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"); + + + /* ユニークIDは表示しなくていい? */ + 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 ); + + /* ここで mydata.shop_record_flag といっしょに */ + 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) /* でかいほうがいい */ +//#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; +} diff --git a/build/tools/sctools/common/src/logprintf.c b/build/tools/sctools/common/src/logprintf.c index e40f9b3..1e45bd5 100644 --- a/build/tools/sctools/common/src/logprintf.c +++ b/build/tools/sctools/common/src/logprintf.c @@ -11,10 +11,9 @@ #define __std(ref) ref // #define __std(ref) ::std::ref -#include -#include "text.h" -#include "mprintf.h" -#include "logprintf.h" +#define size_t my_size_t + +typedef signed long my_size_t; #define __fourbytealign(n) ((((unsigned long) (n)) + 3U) & ~3U) #define __va_start(parm) ((__std(va_list)) ((char*) ((unsigned long)(&parm) & ~3U) + __fourbytealign(sizeof(parm)))) @@ -153,15 +152,7 @@ static void miya_Litob(_Pft *, char); #endif #define ISDIGIT(c) ((c >= '0') && (c <= '9')) -#define MAX_PAD (sizeof(spaces) - 1) -#define PAD(s, n) \ - if (0 < (n)) { \ - int i, j = (n); \ - for (; 0 < j; j -= i) { \ - i = MAX_PAD < j ? MAX_PAD : j; \ - PUT(s, i); \ - } \ - } + #if 0 // miyamoto #define PUT(s, n) \ if( len > n ) {\ @@ -186,10 +177,22 @@ static void miya_Litob(_Pft *, char); } \ } \ else { \ - return (x.nchar); \ + return (x.nchar); \ } #endif + + +#define MAX_PAD (sizeof(spaces) - 1) +#define PAD(s, n) \ + if (0 < (n)) { \ + int i, j = (n); \ + for (; 0 < j; j -= i) { \ + i = MAX_PAD < j ? (int)MAX_PAD : j; \ + PUT(s, i); \ + } \ + } + static char spaces[] = " "; static char zeroes[] = "00000000000000000000000000000000"; @@ -243,7 +246,7 @@ static int _Printf(void *(*pfn)(void *, const char *, size_t), } --s; #if 1 - PUT(fmt, s - fmt); + PUT(fmt, (s - fmt)); #else if (0 < (s-fmt)) { if ((arg = (*pfn)(arg, fmt, s-fmt)) != NULL) @@ -258,7 +261,7 @@ static int _Printf(void *(*pfn)(void *, const char *, size_t), #endif if (c == '\0') - return (x.nchar); + return (int)(x.nchar); fmt = ++s; /* parse a conversion specifier */ @@ -391,7 +394,7 @@ static void _Putfld(_Pft *px, va_list *pap, char code, char *ac) break; case 's': /* convert a string */ px->s = va_arg(*pap, char *); - px->n1 = strlen(px->s); + px->n1 = (int)strlen(px->s); if (0 <= px->prec && px->prec < px->n1) px->n1 = px->prec; break; @@ -445,20 +448,20 @@ static void miya_Litob(_Pft *px, char code) char *digs = code == 'X' ? udigs : ldigs; int base = code == 'o' ? 8 : code != 'x' && code != 'X' ? 10 : 16; int i = sizeof(ac); - unsigned long long ullval = px->v.ll; + unsigned long long ullval = (unsigned long long)(px->v.ll); if ((code == 'd' || code == 'i') && px->v.ll < 0) ullval = -ullval; /* safe against overflow */ if (ullval || px->prec) ac[--i] = digs[ullval % base]; - px->v.ll = ullval / base; + px->v.ll = (long long)(ullval / base); while (0 < px->v.ll && 0 < i) { /* convert digits */ miya_lldiv_t qr = miya_lldiv(px->v.ll, (long long) base); px->v.ll = qr.quot; ac[--i] = digs[qr.rem]; } - px->n1 = sizeof(ac) - i; + px->n1 = (int)sizeof(ac) - i; (void)my_memcpy(px->s, &ac[i], px->n1); if (px->n1 < px->prec) px->nz0 = px->prec - px->n1; @@ -686,7 +689,7 @@ static short _Ldunscale(short *pex, ldouble *px) _NAN : INF); } else if (0 < xchar /* || (xchar = _Dnorm(ps)) != 0 */) { /* finite, reduce to [1/2, 1) */ - ps[_D0] = (short)(ps[_D0] & ~_DMASK | _DBIAS << _DOFF); + ps[_D0] = (unsigned short)(ps[_D0] & ~_DMASK | _DBIAS << _DOFF); #if _LONG_DOUBLE *pex = (short)(xchar - _DBIAS); #else diff --git a/build/tools/sctools/common/src/mprintf.c b/build/tools/sctools/common/src/mprintf.c index d9bbf0f..37fc8a6 100644 --- a/build/tools/sctools/common/src/mprintf.c +++ b/build/tools/sctools/common/src/mprintf.c @@ -27,6 +27,10 @@ #include "text.h" #include "mprintf.h" +#define size_t my_size_t +typedef signed long my_size_t; + + #define CODE32 @@ -68,7 +72,7 @@ static void *memcpy(void *dest, const void *src, size_t n) unsigned char *ss = (unsigned char *)src; while(n--) { - My_WriteByte( s++,My_ReadByte(ss++)); + *s++ = *ss++ ; } return dest; } @@ -78,7 +82,7 @@ static void *memcpy(void *dest, const void *src, size_t n) static const char *strchr(const char *s,int c) { int a; - while( (a = (int)My_ReadByte(s)) != NULL ) { + while( (a = (int)*s) != NULL ) { if(a == c) return s; s++; @@ -89,7 +93,7 @@ static const char *strchr(const char *s,int c) static size_t strlen(const char *s) { size_t n=0; - while( My_ReadByte(s) != NULL) { + while( *s != NULL) { n++; s++; } @@ -110,7 +114,7 @@ static void *proutPrintf(void *txb, const char *buf, size_t n) int i; for (i = 0; i < n; i++) - MIYA_PUTCHAR( txb, My_ReadByte(&(buf[i]))); + MIYA_PUTCHAR( txb, buf[i]); /* return a fake pointer so that it's not NULL */ return ((void *)txb); } @@ -206,7 +210,7 @@ static void miya_Litob(_Pft *, char); if (0 < (n)) { \ int i, j = (n); \ for (; 0 < j; j -= i) { \ - i = MAX_PAD < j ? MAX_PAD : j; \ + i = MAX_PAD < j ? (int)MAX_PAD : j; \ PUT(s, i); \ } \ } @@ -270,7 +274,7 @@ static int _Printf(void *(*pfn)(void *, const char *, size_t), /* copy any literal text */ - while ( (0 < (c = (int)My_ReadByte(s++)) ) && (c != '%')) { + while ( (0 < (c = (int)*s++) ) && (c != '%')) { } --s; #if 1 @@ -291,7 +295,7 @@ static int _Printf(void *(*pfn)(void *, const char *, size_t), /* parse a conversion specifier */ for (x.flags = 0; (t = strchr(fchar, *s)) != NULL; ++s) x.flags |= fbit[t - fchar]; - if (My_ReadByte(s) == '*') { /* get width argument */ + if ( *s == '*') { /* get width argument */ x.width = va_arg(ap, int); if (x.width < 0) { /* same as '-' flag */ x.width = -x.width; @@ -302,24 +306,23 @@ static int _Printf(void *(*pfn)(void *, const char *, size_t), for (x.width = 0; ISDIGIT((int)*s); ++s) if (x.width < _WMAX) x.width = x.width * 10 + *s - '0'; - if (My_ReadByte(s) != '.') + if ( *s != '.') x.prec = -1; - else if ( My_ReadByte(++s) == '*') { /* get precision argument */ + else if ( *++s == '*') { /* get precision argument */ x.prec = va_arg(ap, int); ++s; } else /* accumulate precision digits */ for (x.prec = 0; ISDIGIT(*s); ++s) if (x.prec < _WMAX) x.prec = x.prec * 10 + *s - '0'; - My_WriteByte(&(x.qual), - (char)(strchr("hlL", My_ReadByte(s)) ? My_ReadByte(s++) : '\0')); - if ((My_ReadByte(&(x.qual)) == 'l') && (My_ReadByte(s) == 'l')) { - My_WriteByte(&(x.qual), 'L'); /* the %ll qualifier */ + x.qual = (char)(strchr("hlL", *s) ? *s++ : '\0'); + if( (x.qual == 'l') && ( *s == 'l')) { + x.qual= 'L'; /* the %ll qualifier */ s++; } /* do the conversion */ - _Putfld(&x, &ap, My_ReadByte(s), ac); + _Putfld(&x, &ap, *s, ac); x.width -= x.n0 + x.nz0 + x.n1 + x.nz1 + x.n2 + x.nz2; if (!(x.flags & _FMI)) PAD(spaces, x.width); @@ -337,6 +340,104 @@ static int _Printf(void *(*pfn)(void *, const char *, size_t), return 0; } + +#if 1 +static void _Putfld(_Pft *px, va_list *pap, char code, char *ac) +{ /* convert a field for _Printf */ + + px->n0 = px->nz0 = px->n1 = px->nz1 = px->n2 = px->nz2 = 0; + switch (code) { /* switch on conversion specifier */ + case 'c': + ac[px->n0++] = (char)va_arg(*pap, int); + break; + case 'd': + case 'i': /* convert a signed decimal integer */ + if (px->qual == 'l') + px->v.ll = va_arg(*pap, long); + else if (px->qual == 'L') + px->v.ll = va_arg(*pap, long long); + else + px->v.ll = va_arg(*pap, int); + if ( px->qual == 'h') + px->v.ll = (short) px->v.ll; + if (px->v.ll < 0) /* negate safely in miya_Litob */ + ac[px->n0++] = '-'; + else if (px->flags & _FPL) + ac[px->n0++] = '+'; + else if (px->flags & _FSP) + ac[px->n0++] = ' '; + px->s = &ac[px->n0]; + miya_Litob(px, code); + break; + case 'o': + case 'u': + case 'x': + case 'X': /* convert unsigned */ + if (px->qual == 'l') + px->v.ll = va_arg(*pap, long); + else if (px->qual == 'L') + px->v.ll = va_arg(*pap, long long); + else + px->v.ll = va_arg(*pap, int); + if (px->qual == 'h') + px->v.ll = (unsigned short) px->v.ll; + else if (px->qual == '\0') + px->v.ll = (unsigned int) px->v.ll; + if (px->flags & _FNO) { /* indicate base with prefix */ + ac[px->n0++] = '0'; + if (code == 'x' || code == 'X') + ac[px->n0++] = code; + } + px->s = &ac[px->n0]; + miya_Litob(px, code); + break; + case 'e': + case 'E': + case 'f': + case 'g': + case 'G': /* convert floating */ + px->v.ld = px->qual == 'L' ? + va_arg(*pap, ldouble) : va_arg(*pap, double); + if (LDSIGN(px->v.ld)) + ac[px->n0++] = '-'; + else if (px->flags & _FPL) + ac[px->n0++] = '+'; + else if (px->flags & _FSP) + ac[px->n0++] = ' '; + px->s = &ac[px->n0]; + miya_Ldtob(px, code); + break; + case 'n': /* return output count */ + if (px->qual == 'h') + *va_arg(*pap, short *) = (short)(px->nchar); + else if (px->qual == 'l') + *va_arg(*pap, long *) = px->nchar; + else if (px->qual == 'L') + *va_arg(*pap, long long *) = px->nchar; + else + *va_arg(*pap, int *) = px->nchar; + break; + case 'p': /* convert a pointer, hex long version */ + px->v.ll = (long) va_arg(*pap, void *); + px->s = &ac[px->n0]; + miya_Litob(px, 'x'); + break; + case 's': /* convert a string */ + px->s = va_arg(*pap, char *); + px->n1 = (int)strlen(px->s); + if (0 <= px->prec && px->prec < px->n1) + px->n1 = px->prec; + break; + case '%': /* put a '%' */ + ac[px->n0++] = '%'; + break; + default: /* undefined specifier, print it out */ + ac[px->n0++] = code; + break; + } +} +#else + static void _Putfld(_Pft *px, va_list *pap, char code, char *ac) { /* convert a field for _Printf */ @@ -353,14 +454,14 @@ static void _Putfld(_Pft *px, va_list *pap, char code, char *ac) px->v.ll = va_arg(*pap, long long); else px->v.ll = va_arg(*pap, int); - if (My_ReadByte(&(px->qual)) == 'h') + if (px->qual == 'h') px->v.ll = (short) px->v.ll; if (px->v.ll < 0) /* negate safely in miya_Litob */ - My_WriteByte(&(ac[px->n0++]), '-'); + ac[px->n0++] = '-'; else if (px->flags & _FPL) - My_WriteByte(&(ac[px->n0++]),'+'); + ac[px->n0++] = '+'; else if (px->flags & _FSP) - My_WriteByte(&(ac[px->n0++]),' '); + ac[px->n0++] = ' '; px->s = &ac[px->n0]; miya_Litob(px, code); break; @@ -431,7 +532,7 @@ static void _Putfld(_Pft *px, va_list *pap, char code, char *ac) break; } } - +#endif /*****************************************************************/ /* miya_Litob function */ @@ -473,20 +574,21 @@ static void miya_Litob(_Pft *px, char code) char *digs = code == 'X' ? udigs : ldigs; int base = code == 'o' ? 8 : code != 'x' && code != 'X' ? 10 : 16; int i = sizeof(ac); - unsigned long long ullval = px->v.ll; + unsigned long long ullval = (unsigned long long)(px->v.ll); if ((code == 'd' || code == 'i') && px->v.ll < 0) ullval = -ullval; /* safe against overflow */ if (ullval || px->prec) - My_WriteByte(&(ac[--i]), My_ReadByte(&(digs[ullval % base]))); - px->v.ll = ullval / base; + ac[--i] = digs[ullval % base]; + px->v.ll = (long long)(ullval / base); while (0 < px->v.ll && 0 < i) { /* convert digits */ miya_lldiv_t qr = miya_lldiv(px->v.ll, (long long) base); px->v.ll = qr.quot; - My_WriteByte(&(ac[--i]), My_ReadByte(&(digs[qr.rem]))); + ac[--i] = digs[qr.rem]; } - px->n1 = sizeof(ac) - i; + px->n1 = (int)sizeof(ac) - i; + (void)memcpy(px->s, &ac[i], px->n1); if (px->n1 < px->prec) px->nz0 = px->prec - px->n1; @@ -519,6 +621,107 @@ static const ldouble pows[] = { static short _Ldunscale(short *, ldouble *); static void _Genld(_Pft *, char, char *, short, short); +#if 1 +static void miya_Ldtob(_Pft *px, char code) +{ /* convert long double to text */ + char ac[32]; + char *p = ac; + ldouble ldval = px->v.ld; + short errx, nsig, xexp; + + if (px->prec < 0) + px->prec = 6; + else if (px->prec == 0 && (code == 'g' || code == 'G')) + px->prec = 1; + if (0 < (errx = _Ldunscale(&xexp, &px->v.ld))) { + /* x == Nan, x == INF */ + // (void)memcpy(px->s, errx == _NAN ? "NaN" : "Inf", px->n1 = 3); + (void)memcpy(px->s, errx == NAN ? "NaN" : "Inf", px->n1 = 3); + return; + } else if (0 == errx) /*x == 0 */ + nsig = 0, xexp = 0; + else { /* 0 < |x|, convert it */ + { /* scale ldval to ~~10^(NDIG/2) */ + int i, n; + + if (ldval < 0.0) + ldval = -ldval; + if ((xexp = (short)(xexp * 30103L / 100000L - NDIG/2)) < 0) { + /* scale up */ + n = (-xexp + (NDIG/2-1)) & ~(NDIG/2-1), xexp = (short)(-n); + for (i = 0; 0 < n; n >>= 1, ++i) + if (n & 1) + ldval *= pows[i]; + } else if (0 < xexp) { /* scale down */ + ldouble factor = 1.0; + + xexp &= ~(NDIG/2-1); + for (n = xexp, i = 0; 0 < n; n >>= 1, ++i) + if (n & 1) + factor *= pows[i]; + ldval /= factor; + } + } + { /* convert significant digits */ + int gen = px->prec + + (code == 'f' ? xexp + 2 + NDIG : 2 + NDIG / 2); + + if (LDBL_DIG + NDIG / 2 < gen) + gen = LDBL_DIG + NDIG / 2; + for ( *p++ = '0' ; 0 < gen && 0.0 < ldval; p += NDIG) { + /* convert NDIG at a time */ + int j; + long lo = (long) ldval; + + if (0 < (gen -= NDIG)) + ldval = (ldval - (ldouble) lo) * 1e8L; + for (p += NDIG, j = NDIG; 0 < lo && 0 <= --j; ) { + /* convert NDIG digits */ + miya_ldiv_t qr; + + qr = miya_ldiv(lo, 10); + *--p = (char)(qr.rem + '0'), lo = qr.quot; + } + while (0 <= --j) + *--p = '0'; + } + gen = p - &ac[1]; + for (p = &ac[1], xexp += NDIG - 1; *p == '0'; ++p) + --gen, --xexp; /* correct xexp */ + + /* miya + char code + int prec + short nsig, xexp; + */ + nsig = (short)(px->prec + (code == 'f' ? xexp + 1 : + code == 'e' || code == 'E' ? 1 : 0)); + if (gen < nsig) + nsig = (short)gen; + if (0 < nsig) { /* round and strip trailing zeros */ + /* const char drop; */ + char drop; + int n; + drop = (char)(nsig < gen && '5' <= p[nsig] ? '9' : '0'); + + for (n = nsig; p[--n] == drop; ) + --nsig; + + if ( drop == '9') { + p[n] = *(&(p[n])+1); + + // ++p[n]; + } + if (n < 0) + --p, ++nsig, ++xexp; + } + } + } + _Genld(px, code, p, nsig, xexp); +} + +#else + static void miya_Ldtob(_Pft *px, char code) { /* convert long double to text */ char ac[32]; @@ -582,7 +785,7 @@ static void miya_Ldtob(_Pft *px, char code) My_WriteByte(--p , '0'); } gen = p - &ac[1]; - for (p = &ac[1], xexp += NDIG - 1; My_ReadByte(p) == '0'; ++p) + for (p = &ac[1], xexp += NDIG - 1; *p == '0'; ++p) --gen, --xexp; /* correct xexp */ /* miya @@ -595,17 +798,15 @@ static void miya_Ldtob(_Pft *px, char code) if (gen < nsig) nsig = (short)gen; if (0 < nsig) { /* round and strip trailing zeros */ - const char drop; + char drop; int n; - My_WriteByte((void *)&drop, (char)(nsig < gen && '5' <= My_ReadByte(&(p[nsig])) ? '9' : '0') ); + drop = (char)(nsig < gen && '5' <= p[nsig] ? '9' : '0'); - for (n = nsig; My_ReadByte(&(p[--n])) == My_ReadByte(&drop); ) + for (n = nsig; p[--n] == drop; ) --nsig; - if (My_ReadByte(&drop) == '9') { - My_WriteByte(&(p[n]), My_ReadByte(&(p[n])+1)); - - // ++p[n]; + if ( drop == '9') { + ++p[n]; } if (n < 0) --p, ++nsig, ++xexp; @@ -614,6 +815,7 @@ static void miya_Ldtob(_Pft *px, char code) } _Genld(px, code, p, nsig, xexp); } +#endif #if _DLONG /* 10-byte IEEE format */ #define _LMASK 0x7fff @@ -713,7 +915,7 @@ static short _Ldunscale(short *pex, ldouble *px) NAN : INF); } else if (0 < xchar /* || (xchar = _Dnorm(ps)) != 0 */) { /* finite, reduce to [1/2, 1) */ - ps[_D0] = (short)(ps[_D0] & ~_DMASK | _DBIAS << _DOFF); + ps[_D0] = (unsigned short)(ps[_D0] & ~_DMASK | _DBIAS << _DOFF); #if _LONG_DOUBLE *pex = (short)(xchar - _DBIAS); #else @@ -730,6 +932,99 @@ static short _Ldunscale(short *pex, ldouble *px) #endif +#if 1 +static void _Genld(_Pft *px, char code, char *p, short nsig, short xexp) +{ /* generate long double text */ + const char point = '.'; + + if (nsig <= 0) + nsig = 1, p = "0"; + if (code == 'f' || (code == 'g' || code == 'G') && + -4 <= xexp && xexp < px->prec) { /* 'f' format */ + ++xexp; /* change to leading digit count */ + if (code != 'f') { /* fixup for 'g' */ + if (!(px->flags & _FNO) && nsig < px->prec) + px->prec = nsig; + if ((px->prec -= xexp) < 0) + px->prec = 0; + } + if (xexp <= 0) { /* digits only to right of point */ + px->s[px->n1++] = '0'; + + if (0 < px->prec || px->flags & _FNO) + px->s[px->n1++] = point; + if (px->prec < -xexp) + xexp = (short)(-px->prec); + px->nz1 = -xexp; + px->prec += xexp; + if (px->prec < nsig) + nsig = (short)(px->prec); + (void)memcpy(&px->s[px->n1], p, px->n2 = nsig); + px->nz2 = px->prec - nsig; + } else if (nsig < xexp) { /* zeros before point */ + (void)memcpy(&px->s[px->n1], p, nsig); + px->n1 += nsig; + px->nz1 = xexp - nsig; + if (0 < px->prec || px->flags & _FNO) + px->s[px->n1] = point, ++px->n2; + px->nz2 = px->prec; + } else { /* enough digits before point */ + (void)memcpy(&px->s[px->n1], p, xexp); + px->n1 += xexp; + nsig -= xexp; + if (0 < px->prec || px->flags & _FNO) + px->s[px->n1++] = point; + if (px->prec < nsig) + nsig = (short)(px->prec); + (void)memcpy(&px->s[px->n1], p + xexp, nsig); + px->n1 += nsig; + px->nz1 = px->prec - nsig; + } + } else { /* 'e' format */ + if (code == 'g' || code == 'G') { /* fixup for 'g' */ + if (nsig < px->prec) + px->prec = nsig; + if (--px->prec < 0) + px->prec = 0; + code = (char)(code == 'g' ? 'e' : 'E'); + } + px->s[px->n1++] = *p++; + if (0 < px->prec || px->flags & _FNO) + px->s[px->n1++] = point; + if (0 < px->prec) { /* put fraction digits */ + if (px->prec < --nsig) + nsig = (short)(px->prec); + (void)memcpy(&px->s[px->n1], p, nsig); + px->n1 += nsig; + px->nz1 = px->prec - nsig; + } + p = &px->s[px->n1]; /* put exponent */ + *p++ = code; + if (0 <= xexp) + *p++ = '+'; + else { /* negative exponent */ + *p++ = '-'; + xexp = (short)(-xexp); + } + if (100 <= xexp) { /* put oversize exponent */ + if (1000 <= xexp) + *p = (char)(xexp / 1000 + '0'), p++, xexp %= 1000; + *p = (char)(xexp / 100 + '0'), p++, xexp %= 100; + } + *p = (char)(xexp / 10 + '0'),p++, xexp %= 10; + *p = (char)(xexp + '0'); p++; + px->n2 = p - &px->s[px->n1]; + } + if ((px->flags & (_FMI | _FZE)) == _FZE) { /* pad with leading zeros */ + int n = px->n0 + px->n1 + px->nz1 + px->n2 + px->nz2; + + if (n < px->width) + px->nz0 = px->width - n; + } +} + +#else + static void _Genld(_Pft *px, char code, char *p, short nsig, short xexp) { /* generate long double text */ const char point = '.'; @@ -820,3 +1115,4 @@ static void _Genld(_Pft *px, char code, char *p, short nsig, short xexp) px->nz0 = px->width - n; } } +#endif diff --git a/build/tools/sctools/common/src/my_fs_util.c b/build/tools/sctools/common/src/my_fs_util.c index 8520dab..a1028dd 100644 --- a/build/tools/sctools/common/src/my_fs_util.c +++ b/build/tools/sctools/common/src/my_fs_util.c @@ -2182,11 +2182,8 @@ BOOL TitleIDLoad(const char *path, MY_USER_APP_TID **pBuffer, int *count, char * return ret_flag; } -// BOOL TitleIDSave(const char *path, u64 *pData, int count, char *log_file_name ) BOOL TitleIDSave(const char *path, MY_USER_APP_TID *pData, int count, char *log_file_name ) - { - FSFile f; BOOL bSuccess; FSResult res; @@ -2210,6 +2207,19 @@ BOOL TitleIDSave(const char *path, MY_USER_APP_TID *pData, int count, char *log_ miya_log_fprintf(log_fd, "%s START\n", __FUNCTION__); + if( count < 1 ) { + miya_log_fprintf(log_fd, "%s path=%s count=%d pData=0x%08x\n", __FUNCTION__,path,count, pData ); + goto function_end; + } + if( path == NULL ) { + miya_log_fprintf(log_fd, "%s path=%s count=%d pData=0x%08x\n", __FUNCTION__,path,count, pData ); + goto function_end; + } + if( pData == NULL ) { + miya_log_fprintf(log_fd, "%s path=%s count=%d pData=0x%08x\n", __FUNCTION__,path,count, pData ); + goto function_end; + } + bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_W); if( ! bSuccess ) { FS_CreateFileAuto( path, FS_PERMIT_W|FS_PERMIT_R); @@ -2297,6 +2307,181 @@ BOOL TitleIDSave(const char *path, MY_USER_APP_TID *pData, int count, char *log_ } +BOOL TitleIDLoadETicketOnly(const char *path, u64 **pBuffer, int *count, char *log_file_name) +{ + FSFile f; + BOOL bSuccess; + // u32 fileSize; + s32 readSize = 0; + int id_count= 0; + int size; + BOOL log_active = FALSE; + FSFile *log_fd; + FSFile log_fd_real; + BOOL ret_flag = TRUE; + + log_fd = &log_fd_real; + + FS_InitFile(&f); + + log_active = Log_File_Open( log_fd, log_file_name ); + if( !log_active ) { + log_fd = NULL; + } + miya_log_fprintf(log_fd, "%s START\n", __FUNCTION__); + + bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_R); + if( ! bSuccess ) { + miya_log_fprintf(log_fd, "Failed Open File %s\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) )); + ret_flag = FALSE; + goto function_end; + } + + if( sizeof(int) != FS_ReadFile(&f, &id_count, (s32)sizeof(int)) ) { + miya_log_fprintf(log_fd, "Failed Read File %s\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) )); + ret_flag = FALSE; + goto function_end; + } + + *count = id_count; + size = (int)sizeof(u64) * id_count; + + *pBuffer = (u64 *)OS_Alloc( (u32)size ); + if( *pBuffer == NULL ) { + ret_flag = FALSE; + miya_log_fprintf(log_fd, "%s Failed memory alloc size %d\n",__FUNCTION__, size); + goto function_end; + } + readSize = FS_ReadFile(&f, (void *)*pBuffer, (s32)size ); + if( readSize != size ) { + miya_log_fprintf(log_fd, "Failed Read File: %s request size %d read size %d\n",path, size, readSize); + if( readSize != size ) { + ret_flag = FALSE; + goto function_end; + } + } + function_end: + + bSuccess = FS_CloseFile(&f); + if( ! bSuccess ) { + miya_log_fprintf(log_fd, "Failed Close File\n"); + miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word( FS_GetArchiveResultCode(path))); + } + + if( log_active ) { + miya_log_fprintf(log_fd, "%s END\n\n", __FUNCTION__); + Log_File_Close(log_fd); + } + return ret_flag; +} + +BOOL TitleIDSaveETicketOnly(const char *path, u64 *pData, int count, char *log_file_name ) +{ + FSFile f; + BOOL bSuccess; + FSResult res; + FSResult fsResult; + // s32 writtenSize; + BOOL log_active = FALSE; + FSFile *log_fd; + FSFile log_fd_real; + BOOL ret_flag = TRUE; + + log_fd = &log_fd_real; + + FS_InitFile(&f); + + log_active = Log_File_Open( log_fd, log_file_name ); + if( !log_active ) { + log_fd = NULL; + } + miya_log_fprintf(log_fd, "%s START\n", __FUNCTION__); + + if( count < 1 ) { + miya_log_fprintf(log_fd, "%s path=%s count=%d pData=0x%08x\n", __FUNCTION__,path,count, pData ); + goto function_end; + } + if( path == NULL ) { + miya_log_fprintf(log_fd, "%s path=%s count=%d pData=0x%08x\n", __FUNCTION__,path,count, pData ); + goto function_end; + } + if( pData == NULL ) { + miya_log_fprintf(log_fd, "%s path=%s count=%d pData=0x%08x\n", __FUNCTION__,path,count, pData ); + goto function_end; + } + + bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_W); + if( ! bSuccess ) { + FS_CreateFileAuto( path, FS_PERMIT_W|FS_PERMIT_R); + bSuccess = FS_OpenFileEx(&f, path , FS_FILEMODE_W ); + if( ! bSuccess ) { + res = FS_GetArchiveResultCode( path ); + miya_log_fprintf(log_fd, "%s file open error %s\n", __FUNCTION__,path ); + miya_log_fprintf(log_fd, " Failed open file:%s\n", my_fs_util_get_fs_result_word( res )); + ret_flag = FALSE; + goto function_end; + } + } + + fsResult = FS_SetFileLength(&f, 0); + if( fsResult != FS_RESULT_SUCCESS ) { + res = FS_GetArchiveResultCode( path ); + miya_log_fprintf(log_fd, "%s file length error %s\n", __FUNCTION__,path ); + miya_log_fprintf(log_fd, " Failed file lenght :%s\n", my_fs_util_get_fs_result_word( res )); + ret_flag = FALSE; + goto function_end; + } + + if( sizeof(int) != FS_WriteFile(&f, &count, (s32)sizeof(int)) ) { + res = FS_GetArchiveResultCode( path ); + miya_log_fprintf(log_fd, "%s file write error %s\n", __FUNCTION__,path ); + miya_log_fprintf(log_fd, " Failed write file:%s\n", my_fs_util_get_fs_result_word( res )); + ret_flag = FALSE; + goto function_end; + } + else { + miya_log_fprintf(log_fd, "num of title id = %d\n", count); + } + + if( ( pData != NULL ) && ( count != 0 ) ) { + /* 16文字だから */ + if( (count*sizeof(u64)) != FS_WriteFile(&f, pData, (s32)(count*sizeof(u64)) )) { + res = FS_GetArchiveResultCode( path ); + miya_log_fprintf(log_fd, "%s file write error %s\n", __FUNCTION__,path ); + miya_log_fprintf(log_fd, " Failed write file:%s\n", my_fs_util_get_fs_result_word( res )); + ret_flag = FALSE; + goto function_end; + } + else { + int j; + u64 *ptr = pData; + + if( ptr != NULL && count > 0 ) { + for( j = 0 ; j < count ; j++ ) { + miya_log_fprintf(log_fd,"No. %d 0x%016llx\n",j, *ptr); + ptr++; + } + } + } + } + FS_FlushFile(&f); + function_end: + bSuccess = FS_CloseFile(&f); + if( bSuccess ) { + + } + if( log_active ) { + miya_log_fprintf(log_fd, "%s END\n\n", __FUNCTION__); + Log_File_Close(log_fd); + } + return ret_flag; +} + + /* TWLカードがあるかどうか */ diff --git a/build/tools/sctools/common/src/my_fs_util.h b/build/tools/sctools/common/src/my_fs_util.h index f8851e9..1c49960 100644 --- a/build/tools/sctools/common/src/my_fs_util.h +++ b/build/tools/sctools/common/src/my_fs_util.h @@ -74,6 +74,10 @@ BOOL MydataSaveEncrypt(const char *path, void *pData, int size, FSFile *log_fd); BOOL TitleIDSave(const char *path, MY_USER_APP_TID *pData, int count, char *log_file_name); BOOL TitleIDLoad(const char *path, MY_USER_APP_TID **pBuffer, int *count, char *log_file_name); +BOOL TitleIDSaveETicketOnly(const char *path, u64 *pData, int count, char *log_file_name ); +BOOL TitleIDLoadETicketOnly(const char *path, u64 **pBuffer, int *count, char *log_file_name); + + BOOL CopyFile(const char *dst_path, const char *src_path, FSFile *log_fd ); void Log_File_Close(FSFile *log_fd); diff --git a/build/tools/sctools/common/src/mydata.h b/build/tools/sctools/common/src/mydata.h index a01a891..051f76a 100644 --- a/build/tools/sctools/common/src/mydata.h +++ b/build/tools/sctools/common/src/mydata.h @@ -3,7 +3,7 @@ #define MY_DATA_VERSION_MAJOR 1 -#define MY_DATA_VERSION_MINOR 4 +#define MY_DATA_VERSION_MINOR 5 typedef struct { u8 version_major; @@ -20,6 +20,7 @@ typedef struct { BOOL shop_record_flag; int num_of_user_download_app; int num_of_user_pre_installed_app; + int num_of_user_pre_installed_eticket_only; int num_of_app_save_data; int num_of_photo_files; int num_of_shared2_files; diff --git a/build/tools/sctools/common/src/myfilename.c b/build/tools/sctools/common/src/myfilename.c index b0293de..327a02f 100644 --- a/build/tools/sctools/common/src/myfilename.c +++ b/build/tools/sctools/common/src/myfilename.c @@ -213,7 +213,7 @@ char *MyFile_GetDownloadTitleIDRestoreLogFileName(void) char *MyFile_GetDownloadTitleIDSaveLogFileName(void) { STD_StrCpy( path_log , path_base ); - STD_StrCat( path_log , MY_FILE_NAME_DOWNLOAD_TITLE_ID_RESTORE_LOG ); + STD_StrCat( path_log , MY_FILE_NAME_DOWNLOAD_TITLE_ID_SAVE_LOG ); return path_log; } @@ -224,6 +224,28 @@ char *MyFile_GetDownloadTitleIDFileName(void) return path; } +char *MyFile_GetDownloadTitleIDTicketOnlyFileName(void) +{ + STD_StrCpy( path , path_base ); + STD_StrCat( path , MY_FILE_NAME_DOWNLOAD_TITLE_ID_TICKET_DATA ); + return path; +} + +char *MyFile_GetDownloadTitleIDTicketOnlyRestoreLogFileName(void) +{ + STD_StrCpy( path_log , path_base ); + STD_StrCat( path_log , MY_FILE_NAME_DOWNLOAD_TITLE_ID_TICKET_RESTORE_LOG ); + return path_log; +} + +char *MyFile_GetDownloadTitleIDTicketOnlySaveLogFileName(void) +{ + STD_StrCpy( path_log , path_base ); + STD_StrCat( path_log , MY_FILE_NAME_DOWNLOAD_TITLE_ID_TICKET_SAVE_LOG ); + return path_log; +} + + char *MyFile_GetGlobalInformationFileName(void) { STD_StrCpy( path , path_base ); diff --git a/build/tools/sctools/common/src/myfilename.h b/build/tools/sctools/common/src/myfilename.h index 1bf9dd5..ef17c0f 100644 --- a/build/tools/sctools/common/src/myfilename.h +++ b/build/tools/sctools/common/src/myfilename.h @@ -39,6 +39,11 @@ #define MY_FILE_NAME_DOWNLOAD_TITLE_ID_RESTORE_LOG ("title_id_rst.txt" ) #define MY_FILE_NAME_DOWNLOAD_TITLE_ID_SAVE_LOG ("title_id_sv.txt" ) +#define MY_FILE_NAME_DOWNLOAD_TITLE_ID_TICKET_DATA ("title_tk.dat") +#define MY_FILE_NAME_DOWNLOAD_TITLE_ID_TICKET_LOG ("title_tk.txt" ) +#define MY_FILE_NAME_DOWNLOAD_TITLE_ID_TICKET_RESTORE_LOG ("title_tk_rst.txt" ) +#define MY_FILE_NAME_DOWNLOAD_TITLE_ID_TICKET_SAVE_LOG ("title_id_sv.txt" ) + #define MY_FILE_NAME_ORG_DATA ("personal.dat") #define MY_FILE_NAME_ORG_LOG ("personal.txt") #define MY_FILE_NAME_ORG_RESTORE_LOG ("personal_rst.txt") @@ -94,6 +99,10 @@ char *MyFile_GetDownloadTitleIDFileName(void); char *MyFile_GetDownloadTitleIDRestoreLogFileName(void); char *MyFile_GetDownloadTitleIDSaveLogFileName(void); +char *MyFile_GetDownloadTitleIDTicketOnlyFileName(void); +char *MyFile_GetDownloadTitleIDTicketOnlySaveLogFileName(void); +char *MyFile_GetDownloadTitleIDTicketOnlyRestoreLogFileName(void); + char *MyFile_GetGlobalInformationFileName(void); char *MyFile_GetGlobalInformationRestoreFileName(void); char *MyFile_GetGlobalInformationLogFileName(void); diff --git a/build/tools/sctools/common/src/myimport.c b/build/tools/sctools/common/src/myimport.c index 6999035..7364521 100644 --- a/build/tools/sctools/common/src/myimport.c +++ b/build/tools/sctools/common/src/myimport.c @@ -53,11 +53,118 @@ Grep finished (matches found) at Tue Apr 07 11:12:16 #include "text.h" #include "mprintf.h" #include "logprintf.h" +#include "my_fs_util.h" + + #include "myimport.h" +#include "ecdl.h" +#include "nam_common.h" +#include "ese_int.h" -// #define MIYA_IMPORT 1 +/* miyamoto */ + +#ifdef PRINT_RESULT +#undef PRINT_RESULT +#endif + +#define PRINT_RESULT(x) OS_TPrintf("%s: %4d %5d\n", __FILE__, __LINE__, x ) +// #define PRINT_RESULT(x) (void)(0) + + + +//#define SD_CARD_TEST +#ifdef SD_CARD_TEST +#define BASE_TICKET_DIR "sdmc:/ticket" +#define BASE_TITLE_DIR "sdmc:/title" +#define BASE_IMPORT_DIR "sdmc:/import" +#define BASE_TMP_DIR "sdmc:/tmp" +#else +#define BASE_TICKET_DIR "nand:/ticket" +#define BASE_TITLE_DIR "nand:/title" +#define BASE_IMPORT_DIR "nand:/import" +#define BASE_TMP_DIR "nand:/tmp" +#endif + + +/* ************************************* */ + +typedef s32 (*NAMiTadFunc)(FSFile* pTadFile, void* arg); + + +#define TAD_ALIGN 64 // Please load tads to this alignment in memory + +typedef struct NAMiTADHeader +{ + u32 hdrSize; // Size of TADHeader1 96 + u8 tadType[2]; + u16 tadVersion; // TAD_VERSION_1 + u32 certSize; + u32 crlSize; + u32 ticketSize; + u32 tmdSize; + u32 contentSize; + u32 metaSize; + +} NAMiTADHeader; + +#define NAM_TAD_SIZE_ADJUST_INITLIB 43071 +#define NAM_TAD_SIZE_ADJUST_IMPORTTICKET 68624 +#define NAM_TAD_SIZE_ADJUST_IMPORTTITLEINIT 127238 +#define NAM_TAD_SIZE_ADJUST_IMPORTTITLEDONE 16069 +#define NAM_TAD_SIZE_ADJUST_SUM ( NAM_TAD_SIZE_ADJUST_INITLIB \ + + NAM_TAD_SIZE_ADJUST_IMPORTTICKET \ + + NAM_TAD_SIZE_ADJUST_IMPORTTITLEINIT \ + + NAM_TAD_SIZE_ADJUST_IMPORTTITLEDONE ) + + +extern FSFATFSArchiveWork sArchiveWork ATTRIBUTE_ALIGN(32); + + +//---- tad 構成情報の要素 +typedef struct NAMiTadParams +{ + u32 cert; + u32 crl; + u32 ticket; + u32 tmd; + u32 content; + u32 meta; +} +NAMiTadParams; + +//---- tad 構成情報 +typedef struct NAMiTadInfo +{ + NAMiTadParams sizes; // 各領域のサイズ + NAMiTadParams offsets; // ファイル先頭から各領域へのオフセット +} +NAMiTadInfo; + +//---- tad のコンテンツインポート時に使用する情報 +typedef struct NAMiTadContentInfo +{ + ESContentId id; // コンテンツ ID + u32 index; // コンテンツ番号 + u32 size; // コンテンツサイズ + u32 offset; // コンテンツ領域先頭から各コンテンツへのオフセット +} +NAMiTadContentInfo; + +//---- tad のインポート時に使用する情報 +typedef struct NAMiTadImportContext +{ + void* cert; // tad から読み込んだ cert + void* crl; // tad から読み込んだ crl + ESTitleId titleId; // Title ID + u32 publicSaveSize; // バックアップ可能セーブデータサイズ + u32 privateSaveSize; // バックアップ禁止セーブデータサイズ + BOOL bCreateBanner; // バナーファイルを作成する + u32 numContents; // コンテンツ数 + NAMiTadContentInfo* pContentInfo; // コンテンツインポート情報へのポインタ。numContents ぶんのサイズがある +} +NAMiTadImportContext; @@ -108,786 +215,57 @@ static BOOL NAMUT_UpdateSoftBoxCount( void ) return retval; } - -/*---------------------------------------------------------------------------* - Name: ImportTad - - Description: .tad ファイルインポート - - Arguments: no - - Returns: None. - *---------------------------------------------------------------------------*/ -BOOL myImportTad(char* file_name, FSFile *log_fd) +BOOL myDeleteTitle(u64 tid, BOOL with_ticket, FSFile *log_fd) { - NAMTadInfo tadInfo; NAMTitleInfo titleInfoTmp; - char full_path[FS_ENTRY_LONGNAME_MAX+6]; - BOOL ret = FALSE; s32 nam_result; - BOOL overwrite = FALSE; - // フルパスを作成 - STD_StrCpy(full_path, file_name); - miya_log_fprintf(log_fd,"start myImportTad %s\n",full_path); + miya_log_fprintf(log_fd,"start %s\n",__FUNCTION__); - // mputchar('.'); - // tadファイルの情報取得 - if (NAM_ReadTadInfo(&tadInfo, full_path) != NAM_OK) { - miya_log_fprintf(log_fd,"Error:NAM_ReadTadInfo failed %s\n",full_path); - return FALSE; - } - - // mputchar('.'); /* 1851879012 -> 0x6E616E64 */ /* HNCAだったらはねる。無線ファーム */ // NANDの情報を取得 - if ( NAM_ReadTitleInfo(&titleInfoTmp, tadInfo.titleInfo.titleId) == NAM_OK ) { + if ( NAM_ReadTitleInfo(&titleInfoTmp, tid) == NAM_OK ) { // NANDに既にインストールされているかどうか確認する - if (tadInfo.titleInfo.titleId == titleInfoTmp.titleId) { - miya_log_fprintf(log_fd,"id=0x%08x already installed\n",titleInfoTmp.titleId); - miya_log_fprintf(log_fd," (%s)\n",full_path); - -#if 1 - miya_log_fprintf(log_fd," delete title..\n"); - if( NAM_OK != NAM_DeleteTitle( titleInfoTmp.titleId ) ) { - miya_log_fprintf(log_fd," Error: NAM_DeleteTitle id = 0x%08x\n", titleInfoTmp.titleId); - return FALSE; + if (tid == titleInfoTmp.titleId) { + // miya_log_fprintf(log_fd,"id=0x%08x already installed\n",titleInfoTmp.titleId); + // miya_log_fprintf(log_fd," delete title..\n"); + if( with_ticket ) { + nam_result = NAM_DeleteTitleCompletely( titleInfoTmp.titleId ); } -#else - return FALSE; -#endif + else { + nam_result = NAM_DeleteTitle( titleInfoTmp.titleId ); + } + if ( nam_result != NAM_OK ) { + miya_log_fprintf(log_fd,"Error:NAM_DeleteTitleCompletely RetCode=%x\n", nam_result); + return FALSE; + } + } + else { + /* インストールされていない。 */ + // miya_log_fprintf(log_fd,"Error:NAM_ReadTitleInfo failed 0x%08x\n",tadInfo.titleInfo.titleId); + // return FALSE; + OS_TPrintf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__); + return TRUE; } } else { - /* インストールされていない。 */ - // miya_log_fprintf(log_fd,"Error:NAM_ReadTitleInfo failed 0x%08x\n",tadInfo.titleInfo.titleId); - // return FALSE; + /* インストールされていない。 */ /* NAM_ReadTitleInfo失敗 */ + OS_TPrintf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__); + return TRUE; + // return FALSE; } - // mputchar('.'); - - // NOT_LAUNCH_FLAG または DATA_ONLY_FLAG が立っていないタイトルの場合 - // freeSoftBoxCountに空きがなければインポートしない - if (!(tadInfo.titleInfo.titleId & (TITLE_ID_NOT_LAUNCH_FLAG_MASK | TITLE_ID_DATA_ONLY_FLAG_MASK))) { - // 上書きインポートの場合はfreeSoftBoxCountはチェックしない - miya_log_fprintf(log_fd,"%s %s %d\n",__FILE__,__FUNCTION__,__LINE__); - if (!overwrite) { - u8 installed, free; - if (!NAMUT_GetSoftBoxCount( &installed, &free )) { - miya_log_fprintf(log_fd,"Error:%s %s %d\n",__FILE__,__FUNCTION__,__LINE__); - return FALSE; - } - - if (free == 0) { - miya_log_fprintf(log_fd,"Error:NAND FreeSoftBoxCount == 0"); - return FALSE; - } - miya_log_fprintf(log_fd,"%s %s %d\n",__FILE__,__FUNCTION__,__LINE__); - } - - // ESの仕様で古い e-ticket があると新しい e-ticket を使ったインポートができない - // 暫定対応として該当タイトルを完全削除してからインポートする - nam_result = NAM_DeleteTitleCompletely(tadInfo.titleInfo.titleId); - if ( nam_result != NAM_OK ) { - miya_log_fprintf(log_fd,"Error:NAM_DeleteTitleCompletely RetCode=%x\n", nam_result); - return FALSE; - } - } - - // mputchar('.'); - - // Import開始 - miya_log_fprintf(log_fd,"Import %s Start.\n", full_path ); - -#ifdef MIYA_IMPORT - nam_result = my_NAM_ImportTad( full_path ); -#else - nam_result = NAM_ImportTad( full_path ); -#endif - - // mputchar('.'); - - if ( nam_result == NAM_OK ) { - miya_log_fprintf(log_fd, "Import Success!\n"); - ret = TRUE; - } - else { - /* - [ES] verify publickey sign failed: 7 - [ES] verify ticket failed rv=7 - Error:NAM_ImportTad RetCode=7 - */ - - miya_log_fprintf(log_fd,"Error:NAM_ImportTad RetCode=%d\n", nam_result); - } - - // mputchar('.'); - // InstalledSoftBoxCount, FreeSoftBoxCount の値を現在のNANDの状態に合わせて更新します。 NAMUT_UpdateSoftBoxCount(); - - return ret; -} - -/* */ - -#ifdef MIYA_IMPORT - - -#include "ecdl.h" -#include "c:/twlsdk/build/libraries/nam/ARM9.TWL/include/nam_common.h" -#include "c:/twlsdk/build/libraries/ese/common/include/ese_int.h" - -#ifdef PRINT_RESULT -#undef PRINT_RESULT -#endif - -#define PRINT_RESULT(x) OS_TPrintf("%s: %4d %5d\n", __FILE__, __LINE__, x ) -// #define PRINT_RESULT(x) (void)(0) - - -//#define SD_CARD_TEST -#ifdef SD_CARD_TEST -#define BASE_TICKET_DIR "sdmc:/ticket" -#define BASE_TITLE_DIR "sdmc:/title" -#define BASE_IMPORT_DIR "sdmc:/import" -#define BASE_TMP_DIR "sdmc:/tmp" -#else -#define BASE_TICKET_DIR "nand:/ticket" -#define BASE_TITLE_DIR "nand:/title" -#define BASE_IMPORT_DIR "nand:/import" -#define BASE_TMP_DIR "nand:/tmp" -#endif - - -/* *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", -}; -static const size_t fs_result_string_max = sizeof(fs_result_strings) / sizeof(*fs_result_strings); -/* *INDENT-ON* */ - -// FSのエラーを出力します -static void ReportLastErrorPath(const char *path) -{ - FSResult result = FS_GetArchiveResultCode(path); - - if ((result >= 0) && (result < fs_result_string_max)) - { - OS_TPrintf("FS error:\n \"%s\"\n %s\n", - path, fs_result_strings[result]); - } - else - { - OS_TPrintf("FS error:\n \"%s\"\n (%s result code:%d)\n", - path, ((result >= fs_result_string_max) && (result < FS_RESULT_MAX)) ? "new" : "unknown", result); - } -} - - - -// 指定フォルダを上から順番に作成します(一度に深いフォルダは作成できません) -static BOOL ESi_CreateDirectory(const char* path) -{ - FSFile dir[1]; - const char* marker = "/"; - char tmppath[FS_ENTRY_LONGNAME_MAX]; - - // ルートフォルダは作らない - char* pos = STD_SearchString(path, marker) + 1; - - FS_InitFile(dir); - - for (;;) - { - pos = STD_SearchString(pos, marker); - if (pos == NULL) - { - break; - } - pos++; - (void)STD_CopyLStringZeroFill(tmppath, path, pos - path);// n-1文字コピー - - if (FS_OpenDirectory(dir, tmppath, FS_FILEMODE_R | FS_FILEMODE_W)) - { // 既にフォルダが存在しているので作らない - (void)FS_CloseDirectory(dir); - } - else if (!FS_CreateDirectory(tmppath, FS_PERMIT_R | FS_PERMIT_W)) - { - ReportLastErrorPath(tmppath); - OS_TWarning("FS_CreateDirectory(\"%s\") failed.", tmppath); - return FALSE; - } - } - - return TRUE; -} - -// ファイルを新規に作成して書き込みます。 -static BOOL ESi_CreateFile(const char* path, - const void* src, s32 len) -{ - FSFile file[1]; - - FS_InitFile(file); - - if (!ESi_CreateDirectory(path)) - { - return FALSE; - } - - if (FS_OpenFileEx(file, path, FS_PERMIT_W)) - { // ファイルが存在すれば上書きします - //OS_TPrintf("\"%s\" already exists.\n", path); - } - else if (!FS_CreateFile(path, FS_PERMIT_R | FS_PERMIT_W)) - { - ReportLastErrorPath(path); - OS_TWarning("FS_CreateFile(%s) failed.", path); - return FALSE; - } - else - { - //OS_TPrintf("FS_CreateFile(%s) succeeded.\n", path); - if (!FS_OpenFileEx(file, path, FS_PERMIT_W)) - { - OS_TWarning("FS_OpenFileEx(%s) failed.", path); - return FALSE; - } - } - - if (len > 0 && FS_WriteFile(file, src, len) < 0) - { - OS_TWarning("FS_WritFile() failed."); - return FALSE; - } - else - { - //OS_TPrintf("FS_WritFile() succeeded.\n"); - } - (void)FS_CloseFile(file); - return TRUE; -} - - -static void ESi_GetTicketPath(char* path, ESTitleId titleId) -{ - char titleStr[32]; - char titleIdH[16]; - char titleIdL[16]; - - // titleID取得してHとLに分解 - (void)STD_TSPrintf(titleStr, "%016llx", titleId); - (void)STD_CopyLStringZeroFill(titleIdH, titleStr, 9); // (n - 1) 文字コピーします。 - (void)STD_CopyLStringZeroFill(titleIdL, titleStr + 8, 9); // (n - 1) 文字コピーします。 - - (void)STD_TSPrintf(path, "%s/%s/%s.tik", BASE_TICKET_DIR, titleIdH, titleIdL); + return TRUE; } -static ESError ES_ImportTicket(const void* ticket, const void* certList, u32 certSizeInBytes, - const void* crlList, u32 crlSizeInBytes, ESTransferType source) -{ -#pragma unused(certList, certSizeInBytes, crlList, crlSizeInBytes, source) - - ESError rv = ES_ERR_OK; - - char tmppath[FS_ENTRY_LONGNAME_MAX]; - const ESTicket* esTicket = (ESTicket*)ticket; - -#ifdef VERBOSE_MODE - ESi_DumpCert(certList); - ESi_DumpTicket((ESTicket*)ticket); -#endif - - if (ticket==NULL || certList==NULL || certSizeInBytes==0) - { - rv = ES_ERR_INVALID; - goto out; - } -#if defined(NEXT_GEN) || defined(BROADWAY_REV) - if ( ((u32)ticket & CACHE_LINE_MASK) || - ((u32)certList & CACHE_LINE_MASK) || - ((u32)crlList & CACHE_LINE_MASK) ) { - rv = ES_ERR_INVALID; - goto out; - } -#endif - -#if 0 - if (!ESi_Initialized) - { - rv = ES_ERR_INVALID; - goto out; - } -#endif - - ESi_GetTicketPath(tmppath, MI_BEToH64(esTicket->titleId)); - - if (!ESi_CreateFile(tmppath, ticket, sizeof(ESTicket))) - { - rv = ES_ERR_FILE_WRITE_FAILED; - } - out: - return rv; -} - -#define TMD_FILE "title.tmd" -#define CID_FORMAT "%08x.app" - -static void ESi_GetTmpPath(char* path) -{ - (void)STD_TSPrintf(path, "%s/%s", BASE_TMP_DIR, TMD_FILE); -} - -static ESError ES_GetTmdSize(void* tmd, u32* size) -{ - ESError rv = ES_ERR_OK; - - if (tmd==NULL) - { - rv = ES_ERR_INVALID; - goto out; - } - - *size = sizeof(IOSCSigRsa2048) + sizeof(ESTitleMetaHeader) + - sizeof(ESContentMeta) * (MI_BEToH16(((ESTitleMeta*)tmd)->head.numContents)); - -out: - PRINT_RESULT(rv); - return rv; -} - - -static ESError ES_ImportTitleInit(const void* tmd, u32 tmdSize, - const void* certList, u32 certSizeInBytes, - const void* crlList, u32 crlSizeInBytes, - ESTransferType source, int safeUpdate) -{ -#pragma unused(certList, certSizeInBytes, crlList, crlSizeInBytes, safeUpdate) - - ESError rv = ES_ERR_OK; - char tmppath[FS_ENTRY_LONGNAME_MAX]; - - u32 size; -#ifdef VERBOSE_MODE - ESi_DumpCert(certList); - ESi_DumpTMD((ESTitleMeta*)tmd); -#endif - /*if ( ((u32)tmd & CACHE_LINE_MASK) || - ((u32)certList & CACHE_LINE_MASK) || - ((u32)crlList & CACHE_LINE_MASK) ) { - rv = ES_ERR_INVALID; - goto out; - }*/ -#if 0 - if (!ESi_Initialized) - { - rv = ES_ERR_INVALID; - goto out; - } -#endif - - switch (source) - { - case ES_TRANSFER_SERVER: - if (tmd==NULL || tmdSize==0 || certList==NULL || certSizeInBytes==0) - { - rv = ES_ERR_INVALID; - goto out; - } - break; - default: - rv = ES_ERR_UNSUPPORTED_TRANSFER_SOURCE; - goto out; - break; - } - - (void)ES_GetTmdSize((void *)tmd, &size); - if (tmdSize != size) - { - rv = ES_ERR_INVALID; - goto out; - } - ESi_GetTmpPath(tmppath); - if (!ESi_CreateFile(tmppath, tmd, (s32)size)) - { - rv = ES_ERR_FILE_WRITE_FAILED; - } - out: - PRINT_RESULT(rv); - return rv; -} - - -static void ESi_GetImportPath(char* path, ESTitleId titleId, const char* file) -{ - char titleStr[32]; - char titleIdH[16]; - char titleIdL[16]; - - // titleID取得してHとLに分解 - (void)STD_TSPrintf(titleStr, "%016llx", titleId); - (void)STD_CopyLStringZeroFill(titleIdH, titleStr, 9); // (n - 1) 文字コピーします。 - (void)STD_CopyLStringZeroFill(titleIdL, titleStr + 8, 9); // (n - 1) 文字コピーします。 - - (void)STD_TSPrintf(path, "%s/%s/%s/content/%s", - BASE_IMPORT_DIR, titleIdH, titleIdL, file); -} - - -static FSFile ContentFile[1]; -#define DUMMY_FILE_DESC 1 - -static s32 ES_ImportContentBegin(ESTitleId titleId, ESContentId cid) -{ - ESError rv = ES_ERR_OK; - - char cidStr[32]; - char tmppath[FS_ENTRY_LONGNAME_MAX]; -#if 0 - if (!ESi_Initialized) - { - rv = ES_ERR_INVALID; - goto out; - } -#endif - (void)STD_TSPrintf(cidStr, CID_FORMAT, cid); - ESi_GetImportPath(tmppath, titleId, cidStr); - - FS_InitFile(ContentFile); - if (FS_OpenFileEx(ContentFile, tmppath, FS_PERMIT_W)) - { - (void)FS_CloseFile(ContentFile); - if (!FS_DeleteFile(tmppath)) - { // ファイルを削除します。 - ReportLastErrorPath(tmppath); - OS_TPrintf("FS_DeleteFile(%s) failed.", tmppath); - return ES_ERR_FILE_OPEN_FAILED; - } - } - - if (!ESi_CreateFile(tmppath, NULL, 0)) - { - ReportLastErrorPath(tmppath); - OS_TPrintf("FS_CreateFile(%s) failed.", tmppath); - return ES_ERR_FILE_OPEN_FAILED; - } - - if (!FS_OpenFileEx(ContentFile, tmppath, FS_PERMIT_W)) - { - ReportLastErrorPath(tmppath); - OS_TPrintf("FS_OpenFileEx(%s) failed.", tmppath); - return ES_ERR_FILE_OPEN_FAILED; - } - -out: - // 暫定的にFSFile構造体のポインタを返す - return DUMMY_FILE_DESC; -} - -static ESError ES_ImportContentData(s32 fd, const void* inBuf, u32 inBufSize) -{ - ESError rv = ES_ERR_OK; - - if (fd<0 || inBuf==NULL || inBufSize==0) - { - rv = ES_ERR_INVALID; - goto out; - } -#if 0 - if (!ESi_Initialized) - { - rv = ES_ERR_INVALID; - goto out; - } -#endif - if (FS_WriteFile(ContentFile, inBuf, (s32)inBufSize) < 0) - { - OS_TPrintf("FS_WritFile() failed."); - rv = ES_ERR_FILE_WRITE_FAILED; - } - -out: - return rv; -} - -/*---------------------------------------------------------------------------* - Name: ES_ImportContentEnd - - Description: The ES_ImportContentEnd function validates the content data - written with the hash in the TMD. If the data is valid, - it commits the data to Flash and closes the file. - - Arguments: None. - - Returns: ESError - *---------------------------------------------------------------------------*/ -static ESError ES_ImportContentEnd(s32 fd) -{ -#pragma unused(fd) - ESError rv = ES_ERR_OK; -#if 0 - if (fd < 0 || !ESi_Initialized) - { - rv = ES_ERR_INVALID; - goto out; - } -#endif - (void)FS_CloseFile(ContentFile); -out: - return rv; -} - - -static ESError ES_ImportTitleDone(void) -{ - ESError rv = ES_ERR_OK; - char srcpath[FS_ENTRY_LONGNAME_MAX]; - char dstpath[FS_ENTRY_LONGNAME_MAX]; - - ESTitleId titleId; - s32 i, numContents; - const ESContentMeta* content; - ESContentId cid; - BOOL isOK; - -#if 0 - if (!ESi_Initialized) - { - rv = ES_ERR_INVALID; - goto out; - } -#endif - - ESi_GetTmpPath(srcpath); - isOK = ESi_ReadFile(srcpath, &TmdBuf, sizeof(TmdBuf)); - if (!isOK) - { // /tmp/title.tmdがない場合、エラーとすべきか不明 - goto out; - } - - titleId = MI_BEToH64(TmdBuf.head.titleId); - ESi_GetTitlePath(dstpath, titleId, TMD_FILE); - - if (!ESi_RenameFile(srcpath, dstpath)) - { - rv = ES_ERR_FILE_OPEN_FAILED; - goto out; - } - - numContents = MI_BEToH16(TmdBuf.head.numContents); - for (i = 0; i < numContents; i++) - { - char cidStr[32]; - - content = &TmdBuf.contents[i]; - cid = MI_BEToH32(content->cid); - - (void)STD_TSPrintf(cidStr, CID_FORMAT, cid); - - ESi_GetImportPath(srcpath, titleId, cidStr); - ESi_GetTitlePath(dstpath, titleId, cidStr); - - if (!ESi_RenameFile(srcpath, dstpath)) - { - rv = ES_ERR_FILE_OPEN_FAILED; - goto out; - } - } - -out: - return rv; -} - - -/* *********************************** */ - - -#define NAM_PUBLIC_SAVE_FILE_NAME "public.sav" -#define NAM_PRIVATE_SAVE_FILE_NAME "private.sav" -#define NAM_SUB_BANNER_FILE_NAME "banner.sav" -#define NAM_SUPPORT_BANNER_FILE_NAME "supbnr.sav" -// パス -#define NAM_ARCHIVE_NAND "nand" -#define NAM_NAND_ROOT NAM_ARCHIVE_NAND ":/" - -#define NAM_SHARED2_FILE_PATH NAM_NAND_ROOT "shared2/%04X" -#define NAM_TICKET_FILE_PATH NAM_NAND_ROOT "ticket/%08x/%08x.tik" -#define NAM_TITLE_ROOT_PATH NAM_NAND_ROOT "title/%08x/%08x" - -#define NAM_CONTENT_DIR_PATH NAM_TITLE_ROOT_PATH "/content" -#define NAM_DATA_DIR_PATH NAM_TITLE_ROOT_PATH "/data" - -#define NAM_TMD_FILE_PATH NAM_CONTENT_DIR_PATH "/title.tmd" -#define NAM_CONTENT_FILE_PATH NAM_CONTENT_DIR_PATH "/%08x.app" - - -static s32 NAMi_GetTitleSaveFilePath(char* savePublic, char* savePrivate, NAMTitleId titleId) -{ - ESError result; - char dataDirPath[FS_ENTRY_LONGNAME_MAX]; - - SDK_POINTER_OR_NULL_ASSERT(savePublic); - SDK_POINTER_OR_NULL_ASSERT(savePrivate); - - // ES_GetDataDir は第2引数に必ず 256 byte 書き込むので別バッファを使用する - result = ES_GetDataDir(titleId, dataDirPath); - PRINT_RESULT(result); - if( result == ES_ERR_OK ) - { - SDK_ASSERT( dataDirPath[STD_GetStringLength(dataDirPath) - 1] != '/' ); - - if( savePublic != NULL ) - { - (void)STD_TSNPrintf(savePublic, NAM_PATH_LEN, "%s/%s", dataDirPath, NAM_PUBLIC_SAVE_FILE_NAME); - } - if( savePrivate != NULL ) - { - (void)STD_TSNPrintf(savePrivate, NAM_PATH_LEN, "%s/%s", dataDirPath, NAM_PRIVATE_SAVE_FILE_NAME); - } - } - - return result; -} - - - - - -static s32 NAMi_GetShared2FilePath(char* path, u16 index) -{ - (void)STD_TSNPrintf(path, NAM_PATH_LEN, NAM_SHARED2_FILE_PATH, index); - - return NAM_OK; -} - - -static s32 NAMi_GetTitleSubBannerFilePath(char* banner, NAMTitleId titleId) -{ - ESError result; - char dataDirPath[FS_ENTRY_LONGNAME_MAX]; - - SDK_POINTER_ASSERT(banner); - - // ES_GetDataDir は第2引数に必ず 256 byte 書き込むので別バッファを使用する - result = ES_GetDataDir(titleId, dataDirPath); - PRINT_RESULT(result); - if( result == ES_ERR_OK ) - { - SDK_ASSERT( dataDirPath[STD_GetStringLength(dataDirPath) - 1] != '/' ); - (void)STD_TSNPrintf(banner, NAM_PATH_LEN, "%s/%s", dataDirPath, NAM_SUB_BANNER_FILE_NAME); - } - - return result; -} - - - - - - - -/* ************************************* */ - - - -typedef s32 (*NAMiTadFunc)(FSFile* pTadFile, void* arg); - - -#define TAD_ALIGN 64 // Please load tads to this alignment in memory - -typedef struct NAMiTADHeader -{ - u32 hdrSize; // Size of TADHeader1 96 - u8 tadType[2]; - u16 tadVersion; // TAD_VERSION_1 - u32 certSize; - u32 crlSize; - u32 ticketSize; - u32 tmdSize; - u32 contentSize; - u32 metaSize; - -} NAMiTADHeader; - -#define NAM_TAD_SIZE_ADJUST_INITLIB 43071 -#define NAM_TAD_SIZE_ADJUST_IMPORTTICKET 68624 -#define NAM_TAD_SIZE_ADJUST_IMPORTTITLEINIT 127238 -#define NAM_TAD_SIZE_ADJUST_IMPORTTITLEDONE 16069 -#define NAM_TAD_SIZE_ADJUST_SUM ( NAM_TAD_SIZE_ADJUST_INITLIB \ - + NAM_TAD_SIZE_ADJUST_IMPORTTICKET \ - + NAM_TAD_SIZE_ADJUST_IMPORTTITLEINIT \ - + NAM_TAD_SIZE_ADJUST_IMPORTTITLEDONE ) - -#define NAM_ARCHIVE_OTHER_PUB "otherPub" -#define NAM_ARCHIVE_OTHER_PRV "otherPrv" -#define NAM_ARCHIVE_SHARE "share" - - -extern FSFATFSArchiveWork sArchiveWork ATTRIBUTE_ALIGN(32); - - -//---- tad 構成情報の要素 -typedef struct NAMiTadParams -{ - u32 cert; - u32 crl; - u32 ticket; - u32 tmd; - u32 content; - u32 meta; -} -NAMiTadParams; - -//---- tad 構成情報 -typedef struct NAMiTadInfo -{ - NAMiTadParams sizes; // 各領域のサイズ - NAMiTadParams offsets; // ファイル先頭から各領域へのオフセット -} -NAMiTadInfo; - -//---- tad のコンテンツインポート時に使用する情報 -typedef struct NAMiTadContentInfo -{ - ESContentId id; // コンテンツ ID - u32 index; // コンテンツ番号 - u32 size; // コンテンツサイズ - u32 offset; // コンテンツ領域先頭から各コンテンツへのオフセット -} -NAMiTadContentInfo; - -//---- tad のインポート時に使用する情報 -typedef struct NAMiTadImportContext -{ - void* cert; // tad から読み込んだ cert - void* crl; // tad から読み込んだ crl - ESTitleId titleId; // Title ID - u32 publicSaveSize; // バックアップ可能セーブデータサイズ - u32 privateSaveSize; // バックアップ禁止セーブデータサイズ - BOOL bCreateBanner; // バナーファイルを作成する - u32 numContents; // コンテンツ数 - NAMiTadContentInfo* pContentInfo; // コンテンツインポート情報へのポインタ。numContents ぶんのサイズがある -} -NAMiTadImportContext; static BOOL IsValidTadHeader(NAMiTadInfo* pInfo, const NAMiTADHeader* pHeader, u32 tadFileSize) { @@ -988,81 +366,8 @@ static void UnloadCertCrl(NAMiTadImportContext* pContext) pContext->crl = NULL; } -static s32 SetupContentInfo(NAMiTadImportContext* pContext, const ESTitleMeta* tmd) -{ - u32 i; - u32 offset; - NAMiTadContentInfo* pContentInfo = NULL; - const u16 numContents = NAMi_EndianU16(tmd->head.numContents); - //---- コンテンツごとに NAMiTadContentInfo を確保し情報を格納します。 - - pContentInfo = NAMi_Alloc(sizeof(NAMiTadContentInfo) * numContents); - - if( pContentInfo == NULL ) - { - OS_TPrintf("NAM::SetupContentInfo NAM_NO_MEMORY (%d)", sizeof(NAMiTadContentInfo) * numContents); - NAMi_Free(pContentInfo); - return NAM_NO_MEMORY; - } - - offset = 0; - for( i = 0; i < numContents; ++i ) - { - const ESContentMeta* pSrc = &tmd->contents[i]; - NAMiTadContentInfo* pDst = &pContentInfo[i]; - u64 contentSize = NAMi_EndianU64(pSrc->size); - u32 contentIndex = NAMi_EndianU16(pSrc->index); - - //---- tmd 中の ESContentMeta の並びと contentIndex は一致しているものとします。 - if( contentIndex != i ) - { - OS_TPrintf("NAM::SetupContentInfo NAM_INVALID_CONTENT_INDEX: (%d != %d)", i, contentIndex); - NAMi_Free(pContentInfo); - return NAM_INVALID_CONTENT_INDEX; - } - //---- 32bit を超えるサイズのコンテンツはエラーとします。 - if( contentSize >= (1ull << 32) ) - { - OS_TPrintf("NAM::SetupContentInfo NAM_TOO_LARGE_CONTENT_SIZE: %016llX %lld", contentSize, contentSize); - NAMi_Free(pContentInfo); - return NAM_TOO_LARGE_CONTENT_SIZE; - } - - pDst->id = NAMi_EndianU32(pSrc->cid); - pDst->index = contentIndex; - pDst->size = (u32)contentSize; - pDst->offset = offset; - - offset += MATH_ROUNDUP(pDst->size, 64); - } - - { - NAMiTmdReserved tmdReserved; - - // アライメントを気にしなくて良いように一旦コピーする - MI_CpuCopy8(tmd->head.reserved, &tmdReserved, sizeof(tmdReserved)); - - pContext->titleId = NAMi_EndianU64(tmd->head.titleId); - pContext->publicSaveSize = tmdReserved.publicSaveSize; - pContext->privateSaveSize = tmdReserved.privateSaveSize; - pContext->bCreateBanner = ((tmdReserved.flags & NAM_TMD_FLAG_NAND_BANNER) != 0); - pContext->numContents = numContents; - pContext->pContentInfo = pContentInfo; - } - - return NAM_OK; -} - -static void UnloadContentInfo(NAMiTadImportContext* pContext) -{ - NAMi_Free(pContext->pContentInfo); - pContext->pContentInfo = NULL; -} - - - -static s32 ReadTadHeader(FSFile* pTadFile, NAMiTadInfo* pInfo) +static s32 myReadTadHeader(FSFile* pTadFile, NAMiTadInfo* pInfo) { s32 result; u32 fileSize; @@ -1078,7 +383,7 @@ static s32 ReadTadHeader(FSFile* pTadFile, NAMiTadInfo* pInfo) // ヘッダを読む result = NAMi_Load(pTadFile, (void**)&pHeader, sizeof(*pHeader), 0, FALSE); - PRINT_RESULT(result); + if( result != NAM_OK ) { return result; @@ -1103,7 +408,6 @@ static s32 ImportTicket(FSFile* pTadFile, const NAMiTadInfo* pInfo, const NAMiTa void* ticket = NULL; result = NAMi_Load(pTadFile, &ticket, pInfo->sizes.ticket, pInfo->offsets.ticket, FALSE); - PRINT_RESULT(result); NAMi_Work.currentSize += pInfo->sizes.ticket; if( result != NAM_OK ) @@ -1116,7 +420,7 @@ static s32 ImportTicket(FSFile* pTadFile, const NAMiTadInfo* pInfo, const NAMiTa pContext->cert, pInfo->sizes.cert, pContext->crl, pInfo->sizes.crl, ES_TRANSFER_SERVER ); - PRINT_RESULT(result); + NAMi_Work.currentSize += NAM_TAD_SIZE_ADJUST_IMPORTTICKET; NAMi_Free(ticket); @@ -1124,978 +428,16 @@ static s32 ImportTicket(FSFile* pTadFile, const NAMiTadInfo* pInfo, const NAMiTa } -static s32 ImportTitleInit(FSFile* pTadFile, const NAMiTadInfo* pInfo, NAMiTadImportContext* pContext) + +/* **************************** */ +static s32 ImportTadTicketOnly(FSFile* pTadFile, const NAMiTadInfo* pInfo) { s32 result; - void* tmd = NULL; - - result = NAMi_Load(pTadFile, &tmd, pInfo->sizes.tmd, pInfo->offsets.tmd, FALSE); - PRINT_RESULT(result); - NAMi_Work.currentSize += pInfo->sizes.tmd; - - if( result != NAM_OK ) - { - return result; - } - - //---- コンテンツインポート用の情報をキャッシュしておきます - result = SetupContentInfo(pContext, (ESTitleMeta*)tmd); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - NAMi_Free(tmd); - return result; - } - - DC_FlushRange( tmd, pInfo->sizes.tmd ); - result = ES_ImportTitleInit( tmd, pInfo->sizes.tmd, - pContext->cert, pInfo->sizes.cert, - pContext->crl, pInfo->sizes.crl, - ES_TRANSFER_SERVER, - 1 ); - PRINT_RESULT(result); - NAMi_Work.currentSize += NAM_TAD_SIZE_ADJUST_IMPORTTITLEINIT; - NAMi_Free(tmd); - - return result; -} - - -static s32 ImportTitleFinish(NAMiTadImportContext* pContext, s32 result) -{ - UnloadContentInfo(pContext); - - if( result == NAM_OK ) - { - result = ES_ImportTitleDone(); - PRINT_RESULT(result); - NAMi_Work.currentSize += NAM_TAD_SIZE_ADJUST_IMPORTTITLEDONE; - } - else - { - s32 check = ES_ImportTitleCancel();; -#pragma unused(check) - SDK_WARNING( check == ES_ERR_OK, "ES_ImportTitleCancel failed %d", check ); - } - - return result; -} - - - -static s32 ImportContent(FSFile* pTadFile, const NAMiTadInfo* pInfo, const NAMiTadImportContext* pContext, int idx) -{ - s32 fd; - const NAMiTadContentInfo* pContentInfo; - void* readBuffer; - u32 processedSize; - BOOL bSuccess; - s32 result; - s32 result2; - u32 offset; - - pContentInfo = &pContext->pContentInfo[idx]; - - offset = pInfo->offsets.content + pContentInfo->offset; - bSuccess = FS_SeekFile(pTadFile, (s32)offset, FS_SEEK_SET); - - if( ! bSuccess ) - { - OS_TPrintf("NAM::ImportContent NAM_SEEK_FILE_FAILED_USER (%d)", offset); - return NAM_SEEK_FILE_FAILED_USER; - } - - - readBuffer = NAMi_Alloc(NAM_STREAMING_BUFFER_SIZE); - - if( readBuffer == NULL ) - { - OS_TPrintf("NAM::ImportContent NAM_NO_MEMORY (%d)", NAM_STREAMING_BUFFER_SIZE); - return NAM_NO_MEMORY; - } - - //---- コンテンツインポート前処理 - fd = ES_ImportContentBegin(pContext->titleId, pContentInfo->id); - - - if( fd < 0 ) - { - NAMi_Free(readBuffer); - OS_TPrintf("%s: %s %5d\n", __FILE__, __FUNCTION__, __LINE__ ); - return fd; - } - - - processedSize = 0; - - //---- コンテンツインポート処理 - while( processedSize < pContentInfo->size ) - { - u32 nextSize = MATH_MIN(NAM_STREAMING_BUFFER_SIZE, pContentInfo->size - processedSize); - u32 readSize; - - DC_InvalidateRange(readBuffer, NAM_STREAMING_BUFFER_SIZE); - readSize = (u32)FS_ReadFile(pTadFile, readBuffer, (s32)nextSize); - - if( readSize != nextSize ) - { - result = NAM_READ_FILE_FAILED_USER; - PRINT_RESULT(result); - break; - } - - DC_FlushRange( readBuffer, readSize ); - - result = ES_ImportContentData(fd, readBuffer, readSize); - PRINT_RESULT(result); - NAMi_Work.currentSize += readSize; - - if( result != ES_ERR_OK ) - { - break; - } - - processedSize += readSize; - } - -OS_TPrintf("%s: %s %5d\n", __FILE__, __FUNCTION__, __LINE__ ); - - //---- コンテンツインポート後処理 - result2 = ES_ImportContentEnd(fd); - NAMi_Free(readBuffer); - - return (result == ES_ERR_OK) ? result2: result; -} - - -static s32 CheckFileSize(const char* path, u32 size) -{ - FSFileInfo info; - FSResult fsResult; - - fsResult = FS_GetFileInfo(path, &info); - - if( fsResult != FS_RESULT_SUCCESS ) - { - return NAM_GET_FILE_INFO_FAILED; - } - - return (info.filesize == size) ? NAM_OK: NAM_SIZE_MISMATCH; -} - -static s32 CheckFatFile(ESTitleId titleId, const char* archName) -{ - FSResult fsResult; - s32 result; - - fsResult = FSi_MountSpecialArchive(titleId, archName, &sArchiveWork); - - if( fsResult == FS_RESULT_SUCCESS ) - { - char tmpArchPath[OS_MOUNT_ARCHIVE_NAME_LEN + 3]; // 3 = ":/\0" - BOOL bSuccess; - FSFile dir; - - (void)STD_TSNPrintf(tmpArchPath, sizeof(tmpArchPath), "%s:/", archName); - - FS_InitFile(&dir); - bSuccess = FS_OpenDirectory(&dir, tmpArchPath, FS_FILEMODE_R); - - if( bSuccess ) - { - (void)FS_CloseDirectory(&dir); - result = NAM_OK; - } - else - { - result = NAM_OPEN_DIRECTORY_FAILED; - } - - // アンマウント - (void)FSi_MountSpecialArchive(titleId, NULL, &sArchiveWork); - } - else - { - result = NAM_MOUNT_DRIVE_FAILED; - PRINT_RESULT(result); - } - - return result; -} - -static s32 ConvertCheckResult(s32 result, BOOL bCreate) -{ - if( bCreate ) - { - if( (result == NAM_SIZE_MISMATCH) - || (result == NAM_GET_FILE_INFO_FAILED) ) - { - // エラー or サイズが違うなら要 Setup - return NAM_REQUIRE_SETUP; - } - if( result != NAM_OK ) - { - // 未知のエラー - OS_TPrintf("ConvertCheckResult: unknown result %d\n", result); - return result; - } - } - else - { - if( result != NAM_GET_FILE_INFO_FAILED ) - { - // エラー=ファイルが存在しない でなければ要 Setup - return NAM_REQUIRE_SETUP; - } - } - - return NAM_OK; -} - -static s32 CheckSaveDataFile(ESTitleId titleId, BOOL bPrivate, u32 size) -{ - char path[NAM_PATH_LEN]; - s32 result; - - if( bPrivate ) - { - result = NAMi_GetTitleSaveFilePath(NULL, path, titleId); - PRINT_RESULT(result); - } - else - { - result = NAMi_GetTitleSaveFilePath(path, NULL, titleId); - PRINT_RESULT(result); - } - - if( result != NAM_OK ) - { - return result; - } - - result = ConvertCheckResult(CheckFileSize(path, size), (size > 0)); - if( result != NAM_OK ) - { - return result; - } - - if( size > 0 ) - { - result = CheckFatFile(titleId, (bPrivate ? NAM_ARCHIVE_OTHER_PRV: NAM_ARCHIVE_OTHER_PUB)); - if( result != NAM_OK ) - { - result = NAM_REQUIRE_SETUP; - } - } - - return result; -} - -static s32 CheckShared2(u16 index, u32 size) -{ - char path[NAM_PATH_LEN]; - s32 result; - - // パス生成 - result = NAMi_GetShared2FilePath(path, index); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - - result = CheckFileSize(path, size); - - // ファイルが存在しないなら NG - if( result == NAM_GET_FILE_INFO_FAILED ) - { - return result; - } - - // FAT in File かどうかをチェック - result = CheckFatFile(index, NAM_ARCHIVE_SHARE); - - if( result != NAM_OK ) - { - return result; - } - - return result; -} - - -static s32 CheckSubBannerFile(ESTitleId titleId, BOOL bCreate) -{ - char path[NAM_PATH_LEN]; - const u32 size = (u32)(bCreate ? NAM_SUB_BANNER_FILE_SIZE: 0); - s32 result; - - result = NAMi_GetTitleSubBannerFilePath(path, titleId); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - - return ConvertCheckResult(CheckFileSize(path, size), bCreate); -} - - -static s32 NAMi_CheckTitleDataFile( ESTitleId titleId, - u32 targetMask, - u32 publicSize, - u32 privateSize, - BOOL bCreateBanner ) -{ - s32 result; - - // public セーブデータファイルをチェック - if( (targetMask & NAM_DATA_FILE_PUBLIC) != 0 ) - { - result = CheckSaveDataFile(titleId, FALSE, publicSize); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - } - - // private セーブデータファイルをチェック - if( (targetMask & NAM_DATA_FILE_PRIVATE) != 0 ) - { - result = CheckSaveDataFile(titleId, TRUE, privateSize); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - } - - // NAND バナーファイルをチェック - if( (targetMask & NAM_DATA_FILE_BANNER) != 0 ) - { - result = CheckSubBannerFile(titleId, bCreateBanner); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - } - return NAM_OK; -} - - -static s32 FormatFatFile(ESTitleId titleId, const char* archName) -{ - FSResult fsResult; - s32 result; - - fsResult = FSi_MountSpecialArchive(titleId, archName, &sArchiveWork); - - if( fsResult == FS_RESULT_SUCCESS ) - { - char tmpArchPath[OS_MOUNT_ARCHIVE_NAME_LEN + 3]; // 3 = ":/\0" - BOOL bSuccess; - - (void)STD_TSNPrintf(tmpArchPath, sizeof(tmpArchPath), "%s:/", archName); - - bSuccess = FATFSi_FormatMedia(tmpArchPath); - - if( bSuccess ) - { - bSuccess = FATFS_FormatDrive(tmpArchPath); - result = bSuccess ? NAM_OK: NAM_FORMAT_DRIVE_FAILED; - PRINT_RESULT(result); - } - else - { - result = NAM_FORMAT_MEDIA_FAILED; - PRINT_RESULT(result); - } - - // アンマウント - (void)FSi_MountSpecialArchive(titleId, NULL, &sArchiveWork); - } - else - { - result = NAM_MOUNT_DRIVE_FAILED; - PRINT_RESULT(result); - } - - return result; -} - - -static s32 CreateSizedFile(const char* path, u32 size) -{ - s32 result; - - result = CheckFileSize(path, size); - PRINT_RESULT(result); - - switch( result ) - { - case NAM_OK: - // ファイルサイズが一致するなら処理完了 - return NAM_OK; - - case NAM_SIZE_MISMATCH: - // ファイルが存在するなら次へ - break; - - case NAM_GET_FILE_INFO_FAILED: - { - // エラーならファイルが存在しないとみなして新規に作成 - BOOL bSuccess; - - bSuccess = FS_CreateFileAuto(path, (FS_PERMIT_R|FS_PERMIT_W)); - - if( ! bSuccess ) - { - return NAM_CREATE_FILE_FAILED_SYSTEM; - } - } - break; - - default: - // 未知のエラー - return result; - } - - // ファイルサイズを変更 - { - FSFile f; - BOOL bSuccess; - FSResult fsResult; - - FS_InitFile(&f); - - bSuccess = FS_OpenFileEx(&f, path, (FS_FILEMODE_R|FS_FILEMODE_W)); - - if( ! bSuccess ) - { - (void)FS_DeleteFile(path); - return NAM_OPEN_FILE_FAILED_SYSTEM; - } - - fsResult = FS_SetFileLength(&f, size); - result = NAMi_CloseFile(&f, TRUE); - - if( fsResult != FS_RESULT_SUCCESS ) - { - (void)FS_DeleteFile(path); - return NAM_SET_FILE_LENGTH_FAILED_SYSTEM; - } - } - - return result; -} - -static s32 FillFileRandom(const char* path) -{ - u64 seed; - MATHRandContext32 rndctx; - u32* pBuffer; - s32 result; - AESResult aesResult; - - aesResult = AES_Rand(&seed, sizeof(seed)); - if( aesResult != AES_RESULT_SUCCESS ) - { - return NAM_RAND_FAILED; - } - MATH_InitRand32(&rndctx, seed); - - result = NAMi_Alloc2((void**)&pBuffer, NAM_STREAMING_BUFFER_SIZE); - if( result == NAM_OK ) - { - BOOL bSuccess; - FSFile f; - - FS_InitFile(&f); - - bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_RWL); - if( bSuccess ) - { - s32 resultClose; - u32 fileSize; - u32 current; - - fileSize = FS_GetFileLength(&f); - - for( current = 0; current < fileSize; current += NAM_STREAMING_BUFFER_SIZE ) - { - const u32 nextSize = MATH_MIN(NAM_STREAMING_BUFFER_SIZE, fileSize - current); - const u32 nextU32 = MATH_DIVUP(nextSize, sizeof(u32)); - u32* p = pBuffer; - u32 i; - s32 writtenSize; - - for( i = 0; i < nextU32; ++i ) - { - *p++ = MATH_Rand32(&rndctx, 0); - } - - writtenSize = FS_WriteFile(&f, pBuffer, (s32)nextSize); - if( writtenSize != (s32)nextSize ) - { - result = NAM_WRITE_FILE_FAILED_SYSTEM; - break; - } - } - - resultClose = NAMi_CloseFile(&f, TRUE); - } - else - { - result = NAM_OPEN_FILE_FAILED_SYSTEM; - } - - NAMi_Free(pBuffer); - } - - return result; -} - -static inline s32 DeleteFile(const char* path) -{ - BOOL bSuccess; - bSuccess = FS_DeleteFile(path); - if( bSuccess ) - { - return NAM_OK; - } - else - { - if( FS_GetArchiveResultCode(path) == FS_RESULT_ALREADY_DONE ) - { - return NAM_OK; - } - else - { - return NAM_DELETE_FILE_FAILED; - } - } -} - - -static s32 UpdateSaveDataFileSecure(ESTitleId titleId, BOOL bPrivate, u32 size) -{ - char path[NAM_PATH_LEN]; - s32 result; - - if( bPrivate ) - { - result = NAMi_GetTitleSaveFilePath(NULL, path, titleId); - PRINT_RESULT(result); - } - else - { - result = NAMi_GetTitleSaveFilePath(path, NULL, titleId); - PRINT_RESULT(result); - } - - if( result != NAM_OK ) - { - return result; - } - - if( size > 0 ) - { - // ファイルを作成 - result = CreateSizedFile(path, size); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - - // ファイルをランダムクリア - result = FillFileRandom(path); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - - // ファイルをフォーマット - result = FormatFatFile(titleId, (bPrivate ? NAM_ARCHIVE_OTHER_PRV: NAM_ARCHIVE_OTHER_PUB)); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - (void)FS_DeleteFile(path); - return result; - } - } - else - { - return DeleteFile(path); - } - - - return NAM_OK; -} - -static s32 MakeSubBannerFillData(void* pBuffer) -{ - TWLSubBannerFile* pBanner; - u16 crc; - s32 result; - - { - AESResult aesResult; - - aesResult = AES_Rand(pBuffer, NAM_SUB_BANNER_FILE_SIZE); - if( aesResult != AES_RESULT_SUCCESS ) - { - return NAM_RAND_FAILED; - } - } - - pBanner = (TWLSubBannerFile*)pBuffer; - - result = NAMi_CalcSubBannerCRC(&crc, pBanner); - if( result != NAM_OK ) - { - return NAM_OK; - } - - if( pBanner->h.crc16_anime == 0 ) - { - pBanner->h.crc16_anime = 0xFFFF; - } - - pBanner->h.crc16_anime ^= crc; - - return NAM_OK; -} - -static s32 CreateSubBannerFile(const char* path) -{ - s32 result; - FSFile f; - void* pFillData; - - result = CreateSizedFile(path, NAM_SUB_BANNER_FILE_SIZE); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - - { - BOOL bSuccess; - s32 written; - - pFillData = NAMi_Alloc(NAM_SUB_BANNER_FILE_SIZE); - if( pFillData == NULL ) - { - result = NAM_NO_MEMORY; - PRINT_RESULT(result); - goto fail_on_alloc; - } - - result = MakeSubBannerFillData(pFillData); - if( result != NAM_OK ) - { - goto fail_on_open; - } - - FS_InitFile(&f); - - bSuccess = FS_OpenFileEx(&f, path, (FS_FILEMODE_R|FS_FILEMODE_W)); - if( ! bSuccess ) - { - result = NAM_OPEN_FILE_FAILED_SYSTEM; - PRINT_RESULT(result); - goto fail_on_open; - } - - written = FS_WriteFile(&f, pFillData, NAM_SUB_BANNER_FILE_SIZE); - if( written != NAM_SUB_BANNER_FILE_SIZE ) - { - result = NAM_WRITE_FILE_FAILED_SYSTEM; - PRINT_RESULT(result); - goto fail_on_write; - } - - result = NAMi_CloseFile(&f, TRUE); - if( result != NAM_OK ) - { - goto fail_on_open; - } - - NAMi_Free(pFillData); - } - - return NAM_OK; -//------------------------- -fail_on_write: - (void)FS_CloseFile(&f); -fail_on_open: - NAMi_Free(pFillData); -fail_on_alloc: - (void)FS_DeleteFile(path); - - return result; -} - -static s32 UpdateSubBannerFile(ESTitleId titleId, BOOL bCreate) -{ - char bannerPath[NAM_PATH_LEN]; - s32 result; - - result = NAMi_GetTitleSubBannerFilePath(bannerPath, titleId); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - - if( bCreate ) - { - return CreateSubBannerFile(bannerPath); - } - else - { - return DeleteFile(bannerPath); - } -} - - - -static s32 NAMi_SetupTitleDataFileSecure( ESTitleId titleId, - u32 targetMask, - u32 publicSize, - u32 privateSize, - BOOL bCreateBanner ) -{ - s32 result; - - // public セーブデータファイルを作成 - if( (targetMask & NAM_DATA_FILE_PUBLIC) != 0 ) - { - result = UpdateSaveDataFileSecure(titleId, FALSE, publicSize); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - } - - // private セーブデータファイルを作成 - if( (targetMask & NAM_DATA_FILE_PRIVATE) != 0 ) - { - result = UpdateSaveDataFileSecure(titleId, TRUE, privateSize); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - } - - // NAND バナーファイルを作成 - if( (targetMask & NAM_DATA_FILE_BANNER) != 0 ) - { - result = UpdateSubBannerFile(titleId, bCreateBanner); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - } - - return NAM_OK; -} - -static s32 CreateShared2Secure(u16 index, u32 size) -{ - char path[NAM_PATH_LEN]; - s32 result; - - // パス生成 - result = NAMi_GetShared2FilePath(path, index); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - - // ファイルを作成 - result = CreateSizedFile(path, size); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - - // ファイルをランダムクリア - result = FillFileRandom(path); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - return result; - } - - // ファイルをフォーマット - result = FormatFatFile(index, NAM_ARCHIVE_SHARE); - PRINT_RESULT(result); - - if( result != NAM_OK ) - { - (void)FS_DeleteFile(path); - return result; - } - return result; -} - - -static s32 NAMi_SetupShared2Secure(u16 index, u32 size) -{ - s32 result; - - // FS ブロックサイズより小さいなら何もしないで NAM_OK - if( size < 16 * 1024 ) - { - return NAM_OK; - } - - result = CheckShared2(index, size); - - if( result != NAM_OK ) - { - result = CreateShared2Secure(index, size); - } - - return result; -} - -#define NAM_SHARED2_NUM 6 - -typedef struct NAMShared2Param -{ - u32 sizes[NAM_SHARED2_NUM]; -} -NAMShared2Param; - - -static inline u32 ExtendShared2Size(u8 x) -{ - return (x == 0) ? 0: (u32)((x + 1) * 16 * 1024); -} - - -static s32 ReadShared2Param(NAMShared2Param* psp, NAMTitleId titleId) -{ - char path[NAM_PATH_LEN]; - s32 result; - - MI_CpuClear8(psp, sizeof(*psp)); - - result = NAMi_GetBootContentPath(path, titleId); - if( result == NAM_OK ) - { - FSFile f; - ROM_Header_Short* pHeader; - BOOL bSuccess; - - bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_R); - if( bSuccess ) - { - s32 resultClose; - - result = NAMi_Load(&f, (void**)&pHeader, sizeof(ROM_Header_Short), 0, TRUE); - resultClose = NAMi_CloseFile(&f, TRUE); - - if( result == NAM_OK ) - { - if( pHeader->access_control.shared2_file ) - { - psp->sizes[0] = ExtendShared2Size(pHeader->shared2_file0_size); - psp->sizes[1] = ExtendShared2Size(pHeader->shared2_file1_size); - psp->sizes[2] = ExtendShared2Size(pHeader->shared2_file2_size); - psp->sizes[3] = ExtendShared2Size(pHeader->shared2_file3_size); - psp->sizes[4] = ExtendShared2Size(pHeader->shared2_file4_size); - psp->sizes[5] = ExtendShared2Size(pHeader->shared2_file5_size); - } - - NAMi_Free(pHeader); - result = resultClose; - } - } - else - { - result = NAM_OPEN_FILE_FAILED_SYSTEM; - } - } - - return result; -} - - -static s32 NAMi_SetupTitleShared2Secure(NAMTitleId titleId) -{ - s32 result = NAM_OK; - - if( NAM_IsExecTitle(titleId) ) - { - NAMShared2Param sp; - - // SRL から shared2 情報を取得 - result = ReadShared2Param(&sp, titleId); - if( result == NAM_OK ) - { - // shared2 ファイルを準備 - u16 i; - - for( i = 0; i < NAM_SHARED2_NUM; ++i ) - { - if( sp.sizes[i] > 0 ) - { - result = NAMi_SetupShared2Secure(i, sp.sizes[i]); - - if( result != NAM_OK ) - { - break; - } - } - } - } - } - - return result; -} - - - - - -static s32 ImportTad(FSFile* pTadFile, const NAMiTadInfo* pInfo) -{ - s32 result; - int idx; + // int idx; NAMiTadImportContext tadContext; // cert と crl をメモリに読み込み result = LoadCertCrl(&tadContext, pTadFile, pInfo); - PRINT_RESULT(result); if( result != NAM_OK ) { @@ -2104,7 +446,6 @@ static s32 ImportTad(FSFile* pTadFile, const NAMiTadInfo* pInfo) // eTicket をインポート result = ImportTicket(pTadFile, pInfo, &tadContext); - PRINT_RESULT(result); if( result != NAM_OK ) { @@ -2113,8 +454,8 @@ static s32 ImportTad(FSFile* pTadFile, const NAMiTadInfo* pInfo) } // 本体のインポート準備 +#if 0 result = ImportTitleInit(pTadFile, pInfo, &tadContext); - PRINT_RESULT(result); if( result == NAM_OK ) { @@ -2122,7 +463,6 @@ static s32 ImportTad(FSFile* pTadFile, const NAMiTadInfo* pInfo) for( idx = 0; idx < tadContext.numContents; ++idx ) { result = ImportContent(pTadFile, pInfo, &tadContext, idx); - PRINT_RESULT(result); if( result != NAM_OK ) { @@ -2133,7 +473,7 @@ static s32 ImportTad(FSFile* pTadFile, const NAMiTadInfo* pInfo) // インポートの完了 result = ImportTitleFinish(&tadContext, result); - PRINT_RESULT(result); +#endif UnloadCertCrl(&tadContext); if( result != NAM_OK ) @@ -2142,42 +482,15 @@ static s32 ImportTad(FSFile* pTadFile, const NAMiTadInfo* pInfo) } // セーブデータファイルのチェック - result = NAMi_CheckTitleDataFile( tadContext.titleId, - NAM_DATA_FILE_ALL, - tadContext.publicSaveSize, - tadContext.privateSaveSize, - tadContext.bCreateBanner ); - PRINT_RESULT(result); - // Setup が必要なら行う - if( result == NAM_REQUIRE_SETUP ) - { - result = NAMi_SetupTitleDataFileSecure( tadContext.titleId, - NAM_DATA_FILE_ALL, - tadContext.publicSaveSize, - tadContext.privateSaveSize, - tadContext.bCreateBanner ); - PRINT_RESULT(result); - } - // shared2 ファイルの準備 - if( result == NAM_OK ) - { - result = NAMi_SetupTitleShared2Secure(tadContext.titleId); - } - if( result != NAM_OK ) - { - // 失敗したのならタイトルを削除 - (void)NAM_DeleteTitle(tadContext.titleId); - return result; - } return result; } -static s32 my_NAM_ImportTadWithFile(FSFile* pTadFile) +static s32 my_NAM_ImportTadWithFileTicketOnly(FSFile* pTadFile) { extern NAMiWork NAMi_Work; s32 result; @@ -2186,14 +499,15 @@ static s32 my_NAM_ImportTadWithFile(FSFile* pTadFile) SDK_POINTER_ASSERT(pTadFile); (void)FS_SeekFileToBegin(pTadFile); + NAMi_Lock(); NAMi_Work.totalSize = 0; NAMi_Work.currentSize = 0; NAMi_Work.bRunning = TRUE; - result = ReadTadHeader(pTadFile, &tadInfo); - PRINT_RESULT(result); + result = myReadTadHeader(pTadFile, &tadInfo); + if( result == NAM_OK ) { NAMi_Work.totalSize = tadInfo.sizes.cert @@ -2205,8 +519,7 @@ static s32 my_NAM_ImportTadWithFile(FSFile* pTadFile) + NAM_TAD_SIZE_ADJUST_SUM; NAMi_Work.currentSize += NAM_TAD_SIZE_ADJUST_INITLIB; - result = ImportTad(pTadFile, &tadInfo); - PRINT_RESULT(result); + result = ImportTadTicketOnly(pTadFile, &tadInfo); } NAMi_Unlock(); @@ -2218,13 +531,13 @@ static s32 my_NAM_ImportTadWithFile(FSFile* pTadFile) -static s32 FuncImportTad(FSFile* pTadFile, void* arg) +static s32 FuncImportTadTicketOnly(FSFile* pTadFile, void* arg) { #pragma unused(arg) - return my_NAM_ImportTadWithFile(pTadFile); + return my_NAM_ImportTadWithFileTicketOnly(pTadFile); } -static s32 TadFramework(const char* path, NAMiTadFunc pTadFunc, void* arg) +static s32 TadFrameworkTicketOnly(const char* path, NAMiTadFunc pTadFunc, void* arg) { FSFile tadFile; BOOL bSuccess; @@ -2240,17 +553,372 @@ static s32 TadFramework(const char* path, NAMiTadFunc pTadFunc, void* arg) } result = pTadFunc(&tadFile, arg); - PRINT_RESULT(result); resultClose = NAMi_CloseFile(&tadFile, FALSE); - PRINT_RESULT(resultClose); return (result == NAM_OK) ? resultClose: result; } -static s32 my_NAM_ImportTad(const char* path) +BOOL my_NAM_ImportTadTicketOnly(const char* path) { - return TadFramework(path, &FuncImportTad, NULL); +#if 1 + s32 result; + + + result = TadFrameworkTicketOnly(path, &FuncImportTadTicketOnly, NULL); + + if(result != NAM_OK) { + return result; + } + return TRUE; +#else + + + FSFile tadFile; + BOOL bSuccess; + s32 result; + s32 resultClose; + + FS_InitFile(&tadFile); + + bSuccess = FS_OpenFile(&tadFile, path); + if( !bSuccess ) + { + OS_TPrintf("open file error:%s %s %d\n", __FILE__,__FUNCTION__,__LINE__); + return FALSE; + } + + result = FuncImportTadTicketOnly(&tadFile, NULL); + + resultClose = NAMi_CloseFile(&tadFile, FALSE); + + if( result != NAM_OK) { + return FALSE; + } + return TRUE; + + +#endif +} + + + + +#if 1 + + +#define DEFINE_CALC_CONTENTS_BLOCKS(name,type,endianU16,endianU64) \ + static u32 \ + CalcContentsBlocksFrom##name(const type* pTmd) \ + { \ + u32 fsBlocks = 0; \ + u32 tmdSize; \ + \ + { \ + const u16 numContents = endianU16(pTmd->head.numContents); \ + int i; \ + \ + for( i = 0; i < numContents; ++i ) \ + { \ + const u32 contentSize = (u32)endianU64(pTmd->contents[i].size); \ + fsBlocks += MATH_DIVUP(contentSize, NAMi_Work.nandClusterSize); \ + } \ + \ + tmdSize = sizeof(ESTitleMeta) \ + - sizeof(((ESTitleMeta*)0)->contents) \ + + sizeof(ESContentMeta) * numContents; \ + } \ + \ + fsBlocks += MATH_DIVUP(tmdSize, NAMi_Work.nandClusterSize); \ + \ + return fsBlocks; \ + } + +SDK_COMPILER_ASSERT( sizeof(((ESTitleMeta*)0)->contents) == sizeof(ESContentMeta) * ES_MAX_CONTENT ); + +DEFINE_CALC_CONTENTS_BLOCKS(Tmd, ESTitleMeta, NAMi_EndianU16, NAMi_EndianU64) + + +static u32 +CalcTitleBlocksWithoutContents(const void* pReserved) +{ + NAMiTmdReserved tmdReserved; + u32 fsBlocks = 0; + BOOL hasSubBanner; + + MI_CpuCopy8(pReserved, &tmdReserved, sizeof(tmdReserved)); + + hasSubBanner = (tmdReserved.flags & NAM_TMD_FLAG_NAND_BANNER); + + fsBlocks += MATH_DIVUP(tmdReserved.publicSaveSize, NAMi_Work.nandClusterSize); + fsBlocks += MATH_DIVUP(tmdReserved.privateSaveSize, NAMi_Work.nandClusterSize); + + if( hasSubBanner ) + { + fsBlocks += MATH_DIVUP(NAM_SUB_BANNER_FILE_SIZE, NAMi_Work.nandClusterSize); + } + + return fsBlocks; +} + + +static u32 +my_NAMi_CalcTitleBlocksFromTmd(const ESTitleMeta* pTmd) +{ + u32 fsBlocks = CalcTitleBlocksWithoutContents(&pTmd->head.reserved) + + CalcContentsBlocksFromTmd(pTmd); + return fsBlocks; +} + + + + +static s32 my_NAM_ReadTadInfoWithFile(NAMTadInfo* pInfo, FSFile* pTadFile) +{ + s32 result; + NAMiTadInfo tadInfo; + ESTitleMeta* pTmd; + + + SDK_POINTER_ASSERT(pInfo); + SDK_POINTER_ASSERT(pTadFile); + (void)FS_SeekFileToBegin(pTadFile); + + result = myReadTadHeader(pTadFile, &tadInfo); + + if( result != NAM_OK ) + { +OS_TPrintf("ERROR:%s %s %d\n", __FILE__,__FUNCTION__,__LINE__); + return result; + } + + // TMD を読む + result = NAMi_Load( pTadFile, + (void**)&pTmd, + tadInfo.sizes.tmd, + tadInfo.offsets.tmd, + FALSE ); + if( result != NAM_OK ) + { +OS_TPrintf("ERROR:%s %s %d\n", __FILE__,__FUNCTION__,__LINE__); + + return result; + } + + NAMi_CopyTmdReservedInfo(&pInfo->titleInfo, &pTmd->head.reserved); + pInfo->titleInfo.titleId = NAMi_EndianU64(pTmd->head.titleId); + pInfo->titleInfo.companyCode = NAMi_EndianU16(pTmd->head.groupId); + pInfo->titleInfo.version = NAMi_EndianU16(pTmd->head.titleVersion); + pInfo->titleInfo.blockSize = my_NAMi_CalcTitleBlocksFromTmd(pTmd); + pInfo->fileSize = FS_GetLength(pTadFile); + + NAMi_Free(pTmd); + + return NAM_OK; +} + + + + +static s32 FuncReadTadInfo(FSFile* pTadFile, void* arg) +{ + return my_NAM_ReadTadInfoWithFile((NAMTadInfo*)arg, pTadFile); +} + + +static s32 myTadFramework(const char* path, NAMiTadFunc pTadFunc, void* arg) +{ + FSFile tadFile; + BOOL bSuccess; + s32 result; + s32 resultClose; + + FS_InitFile(&tadFile); + + + bSuccess = FS_OpenFile(&tadFile, path); + if( ! bSuccess ) + { + + return NAM_OPEN_FILE_FAILED_USER; + } + + result = pTadFunc(&tadFile, arg); + + + resultClose = NAMi_CloseFile(&tadFile, FALSE); + + return (result == NAM_OK) ? resultClose: result; +} + + +static BOOL my_NAM_ReadTadInfo(NAMTadInfo* pInfo, const char* path) +{ + + if( NAM_OK == myTadFramework(path, &FuncReadTadInfo, pInfo )) { + + return TRUE; + } + + return FALSE; +} + + +#else + + +static BOOL my_NAM_ReadTadInfo(NAMTadInfo* pInfo, char *path) +{ + FSFile file; + BOOL bSuccess = TRUE; + BOOL ret_value = TRUE; + FSResult fsres; + // extern char *my_fs_util_get_fs_result_word( FSResult res ); + + + if( pInfo == NULL ) { + OS_TPrintf("%s pInfo NULL\n",__FUNCTION__); + return FALSE; + + } + if( path == NULL ) { + OS_TPrintf("%s path NULL\n",__FUNCTION__); + return FALSE; + } + + FS_InitFile(&file); + + bSuccess = FS_OpenFile(&file, path); + if( bSuccess != TRUE ) { + OS_TPrintf("%s file open error %s\n",__FUNCTION__,path); + + fsres = FS_GetArchiveResultCode(path); + OS_TPrintf(" %s\n", my_fs_util_get_fs_result_word(fsres)); + return FALSE; + } + if( NAM_OK != NAM_ReadTadInfoWithFile(pInfo, &file) ) { + ret_value = FALSE; + } + (void)FS_CloseFile(&file); + return ret_value; } #endif + + + +/*---------------------------------------------------------------------------* + Name: ImportTad + + Description: .tad ファイルインポート + + Arguments: no + + Returns: None. + *---------------------------------------------------------------------------*/ +//BOOL myImportTad(char* file_name, FSFile *log_fd) +BOOL myImportTad(char* full_path, FSFile *log_fd) +{ + NAMTadInfo tadInfo; + NAMTitleInfo titleInfoTmp; + // char full_path[FS_ENTRY_LONGNAME_MAX+6]; + BOOL ret = FALSE; + s32 nam_result; + BOOL overwrite = FALSE; + + // フルパスを作成 + // STD_StrCpy(full_path, file_name); + + miya_log_fprintf(log_fd,"start myImportTad %s\n",full_path); + + + // tadファイルの情報取得 +#if 0 + if (NAM_ReadTadInfo(&tadInfo, full_path) != NAM_OK) { + miya_log_fprintf(log_fd,"Error:NAM_ReadTadInfo failed %s\n",full_path); + return FALSE; + } +#else + if ( my_NAM_ReadTadInfo(&tadInfo, full_path) != TRUE ) { + miya_log_fprintf(log_fd,"Error:NAM_ReadTadInfo failed %s\n",full_path); + return FALSE; + } +#endif + + /* 1851879012 -> 0x6E616E64 */ + /* HNCAだったらはねる。無線ファーム */ + + // NANDの情報を取得 + if ( NAM_ReadTitleInfo(&titleInfoTmp, tadInfo.titleInfo.titleId) == NAM_OK ) { + // NANDに既にインストールされているかどうか確認する + if (tadInfo.titleInfo.titleId == titleInfoTmp.titleId) { + miya_log_fprintf(log_fd,"id=0x%08x already installed\n",titleInfoTmp.titleId); + miya_log_fprintf(log_fd," (%s)\n",full_path); + + miya_log_fprintf(log_fd," delete title..\n"); + if( NAM_OK != NAM_DeleteTitleCompletely( titleInfoTmp.titleId ) ) { + miya_log_fprintf(log_fd," Error: NAM_DeleteTitle id = 0x%08x\n", titleInfoTmp.titleId); + return FALSE; + } + } + } + else { + /* インストールされていない。 */ + // miya_log_fprintf(log_fd,"Error:NAM_ReadTitleInfo failed 0x%08x\n",tadInfo.titleInfo.titleId); + // return FALSE; + } + + // NOT_LAUNCH_FLAG または DATA_ONLY_FLAG が立っていないタイトルの場合 + // freeSoftBoxCountに空きがなければインポートしない + if (!(tadInfo.titleInfo.titleId & (TITLE_ID_NOT_LAUNCH_FLAG_MASK | TITLE_ID_DATA_ONLY_FLAG_MASK))) { + // 上書きインポートの場合はfreeSoftBoxCountはチェックしない + // miya_log_fprintf(log_fd,"%s %s %d\n",__FILE__,__FUNCTION__,__LINE__); + if (!overwrite) { + u8 installed, free; + if (!NAMUT_GetSoftBoxCount( &installed, &free )) { + miya_log_fprintf(log_fd,"Error:%s %s %d\n",__FILE__,__FUNCTION__,__LINE__); + return FALSE; + } + + if (free == 0) { + miya_log_fprintf(log_fd,"Error:NAND FreeSoftBoxCount == 0"); + return FALSE; + } + // miya_log_fprintf(log_fd,"%s %s %d\n",__FILE__,__FUNCTION__,__LINE__); + } + + // ESの仕様で古い e-ticket があると新しい e-ticket を使ったインポートができない + // 暫定対応として該当タイトルを完全削除してからインポートする + nam_result = NAM_DeleteTitleCompletely(tadInfo.titleInfo.titleId); + if ( nam_result != NAM_OK ) { + miya_log_fprintf(log_fd,"Error:NAM_DeleteTitleCompletely RetCode=%x\n", nam_result); + return FALSE; + } + } + miya_log_fprintf(log_fd,"Import %s Start.\n", full_path ); + + nam_result = NAM_ImportTad( full_path ); + + if ( nam_result == NAM_OK ) { + miya_log_fprintf(log_fd, "Import Success!\n"); + ret = TRUE; + } + else { + /* + [ES] verify publickey sign failed: 7 + [ES] verify ticket failed rv=7 + Error:NAM_ImportTad RetCode=7 + */ + miya_log_fprintf(log_fd,"Error:NAM_ImportTad RetCode=%d\n", nam_result); + } + // InstalledSoftBoxCount, FreeSoftBoxCount の値を現在のNANDの状態に合わせて更新します。 + NAMUT_UpdateSoftBoxCount(); + + return ret; +} + + + + + + diff --git a/build/tools/sctools/common/src/myimport.h b/build/tools/sctools/common/src/myimport.h index 0e8a45c..2b0aadc 100644 --- a/build/tools/sctools/common/src/myimport.h +++ b/build/tools/sctools/common/src/myimport.h @@ -6,7 +6,8 @@ extern "C" { #endif BOOL myImportTad(char* file_name, FSFile *log_fd); - +BOOL myDeleteTitle(u64 tid, BOOL with_ticket, FSFile *log_fd); +BOOL my_NAM_ImportTadTicketOnly(const char* path); #ifdef __cplusplus } diff --git a/build/tools/sctools/common/src/pre_install.c b/build/tools/sctools/common/src/pre_install.c index 6b76ac8..cadb666 100644 --- a/build/tools/sctools/common/src/pre_install.c +++ b/build/tools/sctools/common/src/pre_install.c @@ -16,6 +16,7 @@ #include "pre_install.h" #define PRE_INSTALL_TABLE_FILE_NAND "rom:/tads/tad_table.txt" +#define PRE_INSTALL_TABLE_DEV_FILE_NAND "rom:/taddevs/taddev_table.txt" #define PRE_INSTALL_TABLE_FILE_SD "sdmc:/tads/tad_table.txt" static PRE_INSTALL_FILE *pre_install_file_list = NULL; @@ -74,6 +75,16 @@ BOOL pre_install_check_download_or_pre_install(u64 tid, int *flag, FSFile *log_f } } + else { +#if 0 + miya_log_fprintf(log_fd,"ES_GetTicketViews failed numTickets = 0: %d tid=0x%08x%08x\n", + rv, (u32)(tid >> 32) , (u32)(tid & 0xffffffff) ); + return FALSE; +#endif + /* error ??? */ + *flag = 0; + return TRUE; + } #if 0 @@ -133,20 +144,19 @@ static BOOL pre_install_add_list(u64 tid, u8 region, u8 country_code, char *temp return TRUE; } - char *pre_install_search_tid(u64 tid, FSFile *log_fd) { PRE_INSTALL_FILE *temp_list; for( temp_list = pre_install_file_list ; temp_list != NULL ; temp_list = temp_list->next ) { if( temp_list->tid == tid ) { - miya_log_fprintf(log_fd,"tad file entry tid=0x%08x%08x %s\n", + miya_log_fprintf(log_fd,"\ntad file entry tid=0x%08x%08x\n%s\n", (u32)(tid >> 32) , (u32)(tid & 0xffffffff), temp_list->file_name ); return temp_list->file_name; } } - miya_log_fprintf(log_fd,"%s %s:No entry tid 0x%08x%08x\n",__FILE__,__FUNCTION__, + miya_log_fprintf(log_fd,"\n%s:No entry\ntid 0x%08x%08x\n",__FUNCTION__, (u32)(tid >> 32) , (u32)(tid & 0xffffffff)); return NULL; @@ -163,10 +173,23 @@ BOOL pre_install_discard_list(void) OS_Free( temp_list ); temp_list = temp_list2; } + pre_install_file_list = NULL; return TRUE; } +void pre_install_print_list(FSFile *log_fd) +{ + PRE_INSTALL_FILE *temp_list; + u64 tid; + + for( temp_list = pre_install_file_list ; temp_list != NULL ; temp_list = temp_list->next ) { + tid = temp_list->tid; + miya_log_fprintf(log_fd,"tad file entry tid=0x%08x%08x %s\n", + (u32)(tid >> 32) , (u32)(tid & 0xffffffff), temp_list->file_name ); + } +} + static int ReadLine(FSFile *f, char *buf, int buf_size) @@ -197,15 +220,10 @@ static int ReadLine(FSFile *f, char *buf, int buf_size) *buf = '\0'; break; } - - } return count; } - - - static int my_char_to_hex(char c) { if( '0' <= c && c <= '9' ) { @@ -220,7 +238,6 @@ static int my_char_to_hex(char c) return -1; /* error */ } - BOOL pre_install_load_file(char *path, FSFile *log_fd) { FSFile file; @@ -237,7 +254,6 @@ BOOL pre_install_load_file(char *path, FSFile *log_fd) // PRE_INSTALL_FILE temp_pre_install_file; char temp_file_name[FS_FILE_NAME_MAX]; - u64 temp_tid; u8 temp_region; u8 temp_country_code; @@ -358,8 +374,10 @@ BOOL pre_install_load_file(char *path, FSFile *log_fd) buf_state = 5; /* next state */ } else { - temp_file_name[temp_filename_count] = c; - temp_filename_count++; + if( c != ' ' ) { + temp_file_name[temp_filename_count] = c; + temp_filename_count++; + } } break; case 5: /* until line end */ @@ -370,8 +388,6 @@ BOOL pre_install_load_file(char *path, FSFile *log_fd) } buf_counter++; } - - } else { /* 妙なフォーマットは全部コメント扱い. */ @@ -397,11 +413,23 @@ BOOL pre_install_Cleanup_User_Titles( FSFile *log_fd ) u64 tid; char game_code_buf[5]; +#if 1 + num = NAM_GetNumInstalledTitles(); +#else num = NAM_GetNumTitles(); +#endif + if( num > 0 ) { +#if 1 + if( NAM_OK != NAM_GetInstalledTitleList( pArray , NAM_TITLE_ID_S ) ) { + return FALSE; + } +#else if( NAM_OK != NAM_GetTitleList( pArray , NAM_TITLE_ID_S ) ) { return FALSE; } +#endif + miya_log_fprintf(log_fd, "NAND Installed titles\n"); mprintf("Clean-up NAND installed titles\n"); @@ -431,13 +459,13 @@ BOOL pre_install_Cleanup_User_Titles( FSFile *log_fd ) miya_log_fprintf(log_fd, " usr.id %08X %08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); - if( NAM_OK != NAM_DeleteTitle( tid ) ) { + if( NAM_OK != NAM_DeleteTitleCompletely( tid ) ) { m_set_palette(tc[0], M_TEXT_COLOR_RED ); /* green */ mprintf("NG.\n"); m_set_palette(tc[0], M_TEXT_COLOR_WHITE ); miya_log_fprintf(log_fd, "NG.\n"); - miya_log_fprintf(log_fd, " Error: NAM_DeleteTitle id = %08X%08X\n", (u32)(tid >> 32), (u32)tid); + miya_log_fprintf(log_fd, " Error: NAM_DeleteTitleCompletely id = %08X%08X\n", (u32)(tid >> 32), (u32)tid); ret_flag = FALSE; } else { @@ -453,7 +481,110 @@ BOOL pre_install_Cleanup_User_Titles( FSFile *log_fd ) } -BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int title_id_count ) +BOOL pre_install_command(FSFile *log_fd, u64 *tid_array, int tid_count, int command, BOOL development_version_flag ) +{ + char *tad_file_name; + int i; + u64 tid; + char game_code_buf[5]; + BOOL ret_flag; + + if( development_version_flag ) { + (void)pre_install_load_file(PRE_INSTALL_TABLE_DEV_FILE_NAND, log_fd); + } + else { + (void)pre_install_load_file(PRE_INSTALL_TABLE_FILE_NAND, log_fd); + } + + switch( command ) { + case 1: + mprintf("Install App.\n"); + break; + case 2: + mprintf("Install TicketOnly.\n"); + break; + case 3: + mprintf("Uninstall App.(except ticket)\n"); + break; + case 4: + mprintf("Uninstall App. with ticket\n"); + break; + default: + break; + } + + for( i = 0 ; i < tid_count ; i++ ) { + ret_flag = TRUE; + tid = tid_array[i]; + (void)my_fs_Tid_To_GameCode(tid, game_code_buf); + mprintf(" %08X%08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); + miya_log_fprintf(log_fd, " %08X%08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); + if( tid == 0 ) { + /* errorか */ + continue; + } + + switch( command ) { + case 1: + mprintf("IA "); + miya_log_fprintf(log_fd, "IA "); + tad_file_name = pre_install_search_tid( tid , log_fd); + if( tad_file_name ) { + ret_flag = myImportTad( tad_file_name , log_fd ); + } + + break; + case 2: + mprintf("TO "); + miya_log_fprintf(log_fd, "TO "); + tad_file_name = pre_install_search_tid( tid , log_fd); + if( tad_file_name ) { + ret_flag = my_NAM_ImportTadTicketOnly( tad_file_name ); + } + break; + case 3: + mprintf("DA "); + miya_log_fprintf(log_fd, "DA "); + ret_flag = myDeleteTitle( tid, 0 , NULL ); + break; + case 4: + mprintf("DC "); + miya_log_fprintf(log_fd, "DC "); + ret_flag = myDeleteTitle( tid, 1 , NULL ); + break; + default: + mprintf("?? "); + miya_log_fprintf(log_fd, "?? "); + break; + } + + + if( ret_flag == FALSE ) { + /* error チケットインストール失敗? */ + miya_log_fprintf(log_fd, "NG.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_RED ); /* green */ + mprintf("NG.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_WHITE ); + + } + else { + miya_log_fprintf(log_fd, "OK.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_GREEN ); /* green */ + mprintf("OK.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_WHITE ); + } + + } + + mprintf("Done.\n"); + + (void)pre_install_discard_list(); + return ret_flag; + +} + +BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int title_id_count, + u64 *ticket_id_array, int ticket_id_count, BOOL development_version_flag ) { char *tad_file_name; int i; @@ -467,14 +598,68 @@ BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int (void)pre_install_load_file(PRE_INSTALL_TABLE_FILE_SD, log_fd); #endif - (void)pre_install_load_file(PRE_INSTALL_TABLE_FILE_NAND, log_fd); + if( development_version_flag ) { + (void)pre_install_load_file(PRE_INSTALL_TABLE_DEV_FILE_NAND, log_fd); + } + else { + (void)pre_install_load_file(PRE_INSTALL_TABLE_FILE_NAND, log_fd); + } + + + pre_install_print_list(NULL); + + + /* チケットだけのインストール */ + for( i = 0 ; i < ticket_id_count ; i++ ) { + tid = ticket_id_array[i]; + (void)my_fs_Tid_To_GameCode(tid, game_code_buf); + mprintf(" TO %08X %08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); + miya_log_fprintf(log_fd, " TO %08X %08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); + if( tid == 0 ) { + /* errorか */ + continue; + } + + tad_file_name = pre_install_search_tid( tid , log_fd); + if( tad_file_name ) { + if( FALSE == my_NAM_ImportTadTicketOnly( tad_file_name ) ) { + /* error チケットインストール失敗? */ + miya_log_fprintf(log_fd, "NG.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_RED ); /* green */ + mprintf("NG.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_WHITE ); + miya_log_fprintf(log_fd, "error: import tid=0x%08x%08x %s\n", + (u32)(tid >> 32) , (u32)(tid & 0xffffffff), tad_file_name); + ret_flag = FALSE; + } + else { + miya_log_fprintf(log_fd, "OK.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_GREEN ); /* green */ + mprintf("OK.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_WHITE ); + } + } + else { + /* error ROMにファイルがない */ + miya_log_fprintf(log_fd, "NG.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_RED ); /* green */ + mprintf("NG.\n"); + m_set_palette(tc[0], M_TEXT_COLOR_WHITE ); + mprintf("No file\n"); + miya_log_fprintf(log_fd, "error: no file\n"); + ret_flag = FALSE; + } + } + + + /* アプリ(common eticket)のインストール */ for( i = 0 ; i < title_id_count ; i++ ) { /* pTitleIds[i].is_personalized = 1 -> common (pre installed) pTitleIds[i].is_personalized = 2 -> personalized */ - if( title_id_buf_ptr[i].is_personalized == 1 ) { + if( title_id_buf_ptr[i].is_personalized == 1 /* commonの場合 */ ) { /* 0x00030004484E474A "rom:/tads/TWL-KGUJ-v257.tad.out" 0x000300044B32444A "rom:/tads/TWL-K2DJ-v0.tad.out" @@ -482,8 +667,8 @@ BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int */ tid = title_id_buf_ptr[i].tid; (void)my_fs_Tid_To_GameCode(tid, game_code_buf); - mprintf(" id %08X %08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); - miya_log_fprintf(log_fd, " id %08X %08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); + mprintf(" AP %08X %08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); + miya_log_fprintf(log_fd, " AP %08X %08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); tad_file_name = pre_install_search_tid( tid , log_fd); if( tad_file_name ) { diff --git a/build/tools/sctools/common/src/pre_install.h b/build/tools/sctools/common/src/pre_install.h index e8bc827..d5c73a3 100644 --- a/build/tools/sctools/common/src/pre_install.h +++ b/build/tools/sctools/common/src/pre_install.h @@ -19,8 +19,11 @@ BOOL pre_install_Cleanup_User_Titles( FSFile *log_fd ); BOOL pre_install_load_file(char *path, FSFile *log_fd); char *pre_install_search_tid(u64 tid, FSFile *log_fd); BOOL pre_install_discard_list(void); -BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int tile_id_count ); +BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int tile_id_count, + u64 *ticket_id_array, int ticket_id_count ,BOOL development_version_flag ); BOOL pre_install_check_download_or_pre_install(u64 tid, int *flag, FSFile *log_fd); +void pre_install_print_list(FSFile *log_fd); +BOOL pre_install_command(FSFile *log_fd, u64 *tid_array, int tid_count, int command, BOOL development_version_flag ); #ifdef __cplusplus } diff --git a/build/tools/sctools/copy_dst/Makefile b/build/tools/sctools/copy_dst/Makefile index ef24715..17c3e45 100644 --- a/build/tools/sctools/copy_dst/Makefile +++ b/build/tools/sctools/copy_dst/Makefile @@ -15,7 +15,7 @@ # $Rev: 3650 $ # $Author: okubata_ryoma $ #---------------------------------------------------------------------------- -SUBDIRS = ./banner ../files/tads +SUBDIRS = ./banner ../files/tads ../files/taddevs TARGET_PLATFORM := TWL TWL_ARCHGEN := LIMITED @@ -39,6 +39,8 @@ ES_LATEST = $(shell ls -d $(ES_DIR)/es-sdk-*|sort|tail -n 1) ES_ROOT = $(ES_LATEST) LINCLUDES = $(TWLSDK_ROOT)/build/libraries/lcfg/ARM9.TWL/include ../common/src \ + $(TWLSDK_ROOT)/build/libraries/nam/ARM9.TWL/include \ + $(TWLSDK_ROOT)/build/libraries/ese/common/include \ $(ES_ROOT)/twl/include $(ES_ROOT)/common/lib/ec/include LLIBRARY_DIRS += $(ES_ROOT)/twl/lib/$(TWL_BUILDTYPE) ../wifilib/$(TWL_BUILDTYPE) @@ -62,12 +64,15 @@ LLIBRARIES += libecx$(TWL_LIBSUFFIX).a \ MAKEROM_ROMROOT = ../files -TAD_TABLE_TXT = ../files/tads/tad_table.txt +TAD_TABLE_TXT = ../files/tads/tad_table.txt +TADDEV_TABLE_TXT = ../files/taddevs/taddev_table.txt +-include MakefileTad.inc -include Makefile.TadIncludes +-include MakefileTadDev.inc -MAKEROM_ROMFILES = fanfare.32.wav ok.wav ng.wav cursor.wav tads/tad_table.txt $(MAKEROM_TAD_ROMFILES) +MAKEROM_ROMFILES = fanfare.32.wav ok.wav ng.wav cursor.wav \ + tads/tad_table.txt taddevs/taddev_table.txt $(MAKEROM_TAD_ROMFILES) $(MAKEROM_TADDEV_ROMFILES) @@ -105,7 +110,9 @@ $(TARGET_AUTOBOOT): ./bin/$(TWL_BUILDTYPE)/$(TARGET_BIN) do-build: $(TARGETS) $(TARGET_AUTOBOOT) -$(BINDIR)/$(TARGET_BIN_BASENAME).$(TWL_ELF_EXT): Makefile.TadIncludes $(TAD_TABLE_TXT) +$(BINDIR)/$(TARGET_BIN_BASENAME).$(TWL_ELF_EXT): MakefileTad.inc MakefileTadDev.inc $(TAD_TABLE_TXT) $(TADDEV_TABLE_TXT) + + #---------------------------------------------------------------------------- diff --git a/build/tools/sctools/copy_dst/copy_dst.rsf b/build/tools/sctools/copy_dst/copy_dst.rsf index be441f2..121fc9a 100644 --- a/build/tools/sctools/copy_dst/copy_dst.rsf +++ b/build/tools/sctools/copy_dst/copy_dst.rsf @@ -165,7 +165,7 @@ Property # # SDCardAccess: sd card access control [TRUE/FALSE] # - SDCardAccess TRUE + SDCardAccess ReadWrite # # NANDAccess: NAND access control [TRUE/FALSE] diff --git a/build/tools/sctools/copy_dst/src/main.c b/build/tools/sctools/copy_dst/src/main.c index b5ada0a..17fe8fd 100644 --- a/build/tools/sctools/copy_dst/src/main.c +++ b/build/tools/sctools/copy_dst/src/main.c @@ -642,6 +642,8 @@ static BOOL RestoreFromSDCard7(void) char game_code_buf[5]; int is_personalized; u64 tid; + u64 *eticket_only_id_buf = NULL; + int num_of_eticket_only_titles = 0; title_id_count = 0; if( title_id_buf_ptr != NULL ) { @@ -721,7 +723,6 @@ static BOOL RestoreFromSDCard7(void) goto pre_install_label; } - /* EC downloadの後にNAM_ImportをやらないとLoadCertのSEA_Decryptでこける。 理由はAESエンジンのスロットをNAM_Importでつぶしちゃうから。 @@ -752,7 +753,6 @@ static BOOL RestoreFromSDCard7(void) SetupShopTitleId(); /* エラーはない */ miya_log_fprintf(log_fd,"SetupShopTitleId\n"); - // ?:ユーザ設定がされていないと接続できないので適当に設定 // SetupUserInfo(); // 必須:バージョンデータのマウント @@ -866,16 +866,28 @@ static BOOL RestoreFromSDCard7(void) pre_install_label: - if( mydata.num_of_user_pre_installed_app > 0 ) { + if( (mydata.num_of_user_pre_installed_app > 0) || + (mydata.num_of_user_pre_installed_eticket_only > 0) ) { /* プリンストール対応 */ miya_log_fprintf(log_fd,"Import Pre-installed apps.\n"); mprintf("Import Pre-installed apps..\n"); - if( FALSE == pre_install_process( log_fd, title_id_buf_ptr, title_id_count ) ) { + if( mydata.num_of_user_pre_installed_eticket_only > 0 ) { + if( FALSE == TitleIDLoadETicketOnly( MyFile_GetDownloadTitleIDTicketOnlyFileName(), + &eticket_only_id_buf, &num_of_eticket_only_titles, + MyFile_GetDownloadTitleIDTicketOnlyRestoreLogFileName()) ) { + ; + } + } + + /* pre_install_process関数の中でEチケットだけのやつもやりたい。 */ + if( FALSE == pre_install_process( log_fd, title_id_buf_ptr, title_id_count, + eticket_only_id_buf, + mydata.num_of_user_pre_installed_eticket_only , + development_console_flag ) ) { ret_flag = FALSE; } } - hatamotolib_log_end(); return ret_flag; diff --git a/build/tools/sctools/copy_org/Makefile b/build/tools/sctools/copy_org/Makefile index 843b775..6dc757f 100644 --- a/build/tools/sctools/copy_org/Makefile +++ b/build/tools/sctools/copy_org/Makefile @@ -37,6 +37,8 @@ ES_LATEST = $(shell ls -d $(ES_DIR)/es-sdk-*|sort|tail -n 1) ES_ROOT = $(ES_LATEST) LINCLUDES = $(TWLSDK_ROOT)/build/libraries/lcfg/ARM9.TWL/include ../common/src \ + $(TWLSDK_ROOT)/build/libraries/nam/ARM9.TWL/include \ + $(TWLSDK_ROOT)/build/libraries/ese/common/include \ $(ES_ROOT)/twl/include $(ES_ROOT)/common/lib/ec/include LLIBRARY_DIRS += $(ES_ROOT)/twl/lib/$(TWL_BUILDTYPE) ../wifilib/$(TWL_BUILDTYPE) diff --git a/build/tools/sctools/copy_org/copy_org.rsf b/build/tools/sctools/copy_org/copy_org.rsf index be441f2..c93f5aa 100644 --- a/build/tools/sctools/copy_org/copy_org.rsf +++ b/build/tools/sctools/copy_org/copy_org.rsf @@ -165,7 +165,7 @@ Property # # SDCardAccess: sd card access control [TRUE/FALSE] # - SDCardAccess TRUE + SDCardAccess ReadWrite # # NANDAccess: NAND access control [TRUE/FALSE] diff --git a/build/tools/sctools/copy_org/src/main.c b/build/tools/sctools/copy_org/src/main.c index 7510e86..b2ae383 100644 --- a/build/tools/sctools/copy_org/src/main.c +++ b/build/tools/sctools/copy_org/src/main.c @@ -97,6 +97,14 @@ static int vram_num_sub = 0; static LCFGTWLHWNormalInfo hwn_info; static LCFGTWLHWSecureInfo hws_info; +#define NAM_TITLE_ID_S 128 + +static NAMTitleId array_eticket_only_titles[NAM_TITLE_ID_S]; +static NAMTitleId array_app_titles[NAM_TITLE_ID_S]; +static int num_of_eticket_only_titles = 0; +static int num_of_app_titles = 0; +static int num_of_all_titles = 0; + #define MY_STACK_SIZE (1024*16) /* でかいほうがいい */ #define MY_THREAD_PRIO 20 static OSThread MyThread; @@ -147,13 +155,93 @@ static void FreeForNAM(void* ptr) } } +static NAMTitleId pArray[NAM_TITLE_ID_S]; + + +static int Check_User_Titles_ETicket_Only(void) +{ + s32 i,j; + s32 num = 0; + int user_title_count = 0; + BOOL now_installed_flag; + u64 id; + char game_code[5]; + int common_or_personalized_flag; + + // num = NAM_GetNumTitles(); + num = NAM_GetNumInstalledTitles(); + if( num >= 0 ) { + if( NAM_OK != NAM_GetInstalledTitleList( pArray , NAM_TITLE_ID_S ) ) { + OS_TPrintf("error:NAM_GetInstalledTitleList\n"); + return -1; /* error */ + } + OS_TPrintf("NAND Ticket only titles\n"); + for( i = 0 ; i < num ; i++ ) { + now_installed_flag = FALSE; + id = pArray[i]; + for( j = 0 ; j < num_of_all_titles ; j++ ) { + if( id == array_app_titles[ j ] ) { + now_installed_flag = TRUE; + } + } + if( now_installed_flag == TRUE ) { + } + else { + /* + No. 0 0003000f484e4c41 + No. 1 0003000f484e4841 + No. 2 0003000f484e4341 + No. 3 00030015484e4241 + No. 4 00030017484e4141 launcher + ^ + | ここの最下位ビットが1のやつがシステムアプリ + | + システムアプリはダウンロード対象外 + */ + (void)my_fs_Tid_To_GameCode(id, game_code); + + if( id & 0x0000000100000000 ) { + /* system app. */ + OS_TPrintf(" sys.:%3d:0x%llx %s\n", i, id, game_code); + + } + else { + /* user app. */ + // OS_TPrintf(" usr.:%3d:0x%llx %s\n", i, id, game_code); + common_or_personalized_flag = 1; + if( FALSE == pre_install_check_download_or_pre_install(id, &common_or_personalized_flag, NULL) ) { + OS_TPrintf(" pre_install_check_download_or_pre_install failed\n"); + } + else { + if( common_or_personalized_flag == 1 ) { + OS_TPrintf(" usr.:%3d:0x%llx %s common\n", i, id, game_code); + array_eticket_only_titles[user_title_count] = id; + user_title_count++; + } + else { + OS_TPrintf(" usr.:%3d:0x%llx %s personalized\n", i, id, game_code); + } + } + + + + } + } + } + } + else { + OS_TPrintf("error:NAM_GetInstalledTitles\n"); + return -1; + } + + num_of_eticket_only_titles = user_title_count; + return user_title_count; +} + + static int Check_User_Titles(void) { - -#define NAM_TITLE_ID_S 128 - - NAMTitleId pArray[NAM_TITLE_ID_S]; s32 i; s32 num = 0; int user_tilte_count = 0; @@ -161,13 +249,16 @@ static int Check_User_Titles(void) char game_code[5]; num = NAM_GetNumTitles(); - if( num > 0 ) { - if( NAM_OK != NAM_GetTitleList( pArray , NAM_TITLE_ID_S ) ) { + if( num >= 0 ) { + + + if( NAM_OK != NAM_GetTitleList( array_app_titles , NAM_TITLE_ID_S ) ) { + OS_TPrintf("error:NAM_GetTitleList\n"); return -1; /* error */ } - OS_TPrintf("NAND Installed titles\n"); + OS_TPrintf("NAND Installed titles %d\n",num); for( i = 0 ; i < num ; i++ ) { - id = pArray[i]; + id = array_app_titles[i]; /* No. 0 0003000f484e4c41 @@ -194,6 +285,12 @@ static int Check_User_Titles(void) } } } + else { + OS_TPrintf("error:NAM_GetNumTitles\n"); + return -1; + } + num_of_app_titles = user_tilte_count; + num_of_all_titles = num; return user_tilte_count; } @@ -433,6 +530,7 @@ static BOOL SDBackupToSDCard6(void) return ret_flag; } + static BOOL SDBackupToSDCard7(void) { MY_DIR_ENTRY_LIST *dir_entry_list_head = NULL; @@ -482,16 +580,31 @@ static BOOL SDBackupToSDCard7(void) flag = GetUserAppTitleList( dir_entry_list_head, &pBuffer, &count, MyFile_GetUserAppTitleListLogFileName()) ; + if( TRUE == flag ) { ptr = pBuffer; mydata.num_of_user_download_app = count; mydata.num_of_error_user_download_app = 0; mydata.num_of_user_pre_installed_app = 0; + mydata.num_of_user_pre_installed_eticket_only = 0; if( no_sd_clean_flag == TRUE ) { mprintf("\n"); } + + //char *MyFile_GetDownloadTitleIDTicketOnlyFileName(void); + //char *MyFile_GetDownloadTitleIDTicketOnlySaveLogFileName(void); + //char *MyFile_GetDownloadTitleIDTicketOnlyRestoreLogFileName(void); + + + mydata.num_of_user_pre_installed_eticket_only = num_of_eticket_only_titles; + if( FALSE == TitleIDSaveETicketOnly( MyFile_GetDownloadTitleIDTicketOnlyFileName(), + array_eticket_only_titles, num_of_eticket_only_titles, + MyFile_GetDownloadTitleIDTicketOnlySaveLogFileName()) ) { + ; + } + if( ptr != NULL && count != 0 ) { for( j = 0 ; j < count ; j++ ) { @@ -546,7 +659,7 @@ static BOOL SDBackupToSDCard7(void) ptr++; } } - + // PrintSrcDirEntryListBackward( dir_entry_list_head, NULL ); flag = TitleIDSave( MyFile_GetDownloadTitleIDFileName(), pBuffer, count, MyFile_GetDownloadTitleIDSaveLogFileName()); @@ -962,7 +1075,9 @@ void TwlMain(void) // mydata.num_of_user_download_app_by_nam = Check_User_Titles(); // mprintf("num of user tiltes = %d\n",mydata.num_of_user_download_app_by_nam); - mprintf("num of user tiltes = %d\n",Check_User_Titles()); + /* 順番厳守 */ + mprintf("user tiltes(installed) = %d\n", Check_User_Titles()); + mprintf("user tiltes(ticket only) = %d\n", Check_User_Titles_ETicket_Only()); mprintf("\n"); diff --git a/build/tools/sctools/files/taddevs/Makefile b/build/tools/sctools/files/taddevs/Makefile new file mode 100644 index 0000000..a039213 --- /dev/null +++ b/build/tools/sctools/files/taddevs/Makefile @@ -0,0 +1,13 @@ +RM = rm.exe -f + +TAD_TABLE_FILE = taddev_table.txt +TAD_MAKE_INC_FILE = ../../copy_dst/MakefileTadDev.inc + +$(TAD_TABLE_FILE): *.tad + ../../bin/make_tad_table.exe -dir . -o $@ -var MAKEROM_TADDEV_ROMFILES -fdir taddevs -mk $(TAD_MAKE_INC_FILE) + + + +.PHONY: clean clobber +clean clobber: + $(RM) $(TAD_TABLE_FILE) $(TAD_MAKE_INC_FILE) diff --git a/build/tools/sctools/files/taddevs/backupSample.tad b/build/tools/sctools/files/taddevs/backupSample.tad new file mode 100644 index 0000000..703be40 Binary files /dev/null and b/build/tools/sctools/files/taddevs/backupSample.tad differ diff --git a/build/tools/sctools/files/taddevs/encodeSD.tad b/build/tools/sctools/files/taddevs/encodeSD.tad new file mode 100644 index 0000000..3c1d1bd Binary files /dev/null and b/build/tools/sctools/files/taddevs/encodeSD.tad differ diff --git a/build/tools/sctools/files/taddevs/nandAppSample.tad b/build/tools/sctools/files/taddevs/nandAppSample.tad new file mode 100644 index 0000000..896e754 Binary files /dev/null and b/build/tools/sctools/files/taddevs/nandAppSample.tad differ diff --git a/build/tools/sctools/files/tads/Makefile b/build/tools/sctools/files/tads/Makefile index 396f3f1..b475ae2 100644 --- a/build/tools/sctools/files/tads/Makefile +++ b/build/tools/sctools/files/tads/Makefile @@ -1,4 +1,12 @@ -TAD_TABLE_FILE = tad_table.txt +RM = rm.exe -f -$(TAD_TABLE_FILE): - ../../bin/make_tad_table.exe -dir . -o $@ -mk ../../copy_dst/Makefile.TadIncludes \ No newline at end of file +TAD_TABLE_FILE = tad_table.txt +TAD_MAKE_INC_FILE = ../../copy_dst/MakefileTad.inc + +$(TAD_TABLE_FILE): *.tad + ../../bin/make_tad_table.exe -dir . -o $@ -var MAKEROM_TAD_ROMFILES -fdir tads -mk $(TAD_MAKE_INC_FILE) + + +.PHONY: clean clobber +clean clobber: + $(RM) $(TAD_TABLE_FILE) $(TAD_MAKE_INC_FILE) diff --git a/build/tools/sctools/make_tad_table/Makefile b/build/tools/sctools/make_tad_table/Makefile index 7bed235..6450828 100644 --- a/build/tools/sctools/make_tad_table/Makefile +++ b/build/tools/sctools/make_tad_table/Makefile @@ -18,6 +18,7 @@ LD = gcc LDFLAGS = -Wl,--subsystem,console -mwindows -mno-cygwin LDLIBS = +RM = rm.exe -f .SUFFIXES: .SUFFIXES: .o .c diff --git a/build/tools/sctools/make_tad_table/make_tad_table.c b/build/tools/sctools/make_tad_table/make_tad_table.c index 734e044..35b70fc 100644 --- a/build/tools/sctools/make_tad_table/make_tad_table.c +++ b/build/tools/sctools/make_tad_table/make_tad_table.c @@ -200,10 +200,12 @@ static void print_gamecode(u32 tid_lo) } +#if 0 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); } +#endif static void read_file_and_print_titleid( char *path , char *d_name, FILE *fp_out, FILE *fp_mk) { @@ -243,13 +245,13 @@ static void read_file_and_print_titleid( char *path , char *d_name, FILE *fp_out } if( fp_out ) { - fprintf(fp_out, "0x%08x%08x, %d , %d , rom:/tads/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); + fprintf(fp_out, "0x%08x%08x, %d , %d , rom:/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); if( fp_mk ) { - fprintf(fp_mk, "\t\ttads/%s \\\n", d_name); + fprintf(fp_mk, "\t\t%s \\\n", d_name); } } else { - printf("0x%08x%08x, %d , %d , rom:/tads/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); + printf("0x%08x%08x, %d , %d , rom:/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); } @@ -273,10 +275,15 @@ int main(int argc, char **argv) char *outfile = NULL; char *dir_name = NULL; char *mkfile = NULL; + char *var_name = NULL; + char *file_dir = NULL; + BOOL read_file_flag = FALSE; BOOL write_file_flag = FALSE; BOOL dir_read_flag = FALSE; BOOL mk_file_flag = FALSE; + BOOL var_name_flag = FALSE; + BOOL file_dir_flag = FALSE; char *prog; int badops = 0; @@ -293,6 +300,7 @@ int main(int argc, char **argv) struct dirent *dr; struct stat st; char *full_path; + char rom_file_full_path[256]; prog=argv[0]; @@ -307,6 +315,20 @@ int main(int argc, char **argv) dir_name = *++argv; dir_read_flag = TRUE; } + else if (strcmp(*argv,"-var") == 0 && !var_name_flag ) { + if (--argc < 1) { + goto bad; + } + var_name = *++argv; + var_name_flag = TRUE; + } + else if (strcmp(*argv,"-fdir") == 0 && !file_dir_flag ) { + if (--argc < 1) { + goto bad; + } + file_dir = *++argv; + file_dir_flag = TRUE; + } else if (strcmp(*argv,"-o") == 0 && !write_file_flag ) { if (--argc < 1) { goto bad; @@ -369,9 +391,12 @@ int main(int argc, char **argv) fprintf(stderr, "error: file open %s\n",mkfile); goto end; } - - fprintf(fp_mk, "MAKEROM_TAD_ROMFILES = \\\n"); - + if( var_name_flag ) { + fprintf(fp_mk, "%s = \\\n",var_name); + } + else { + fprintf(fp_mk, "MAKEROM_TAD_ROMFILES = \\\n"); + } } @@ -403,7 +428,15 @@ int main(int argc, char **argv) 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 ); + if( file_dir_flag ) { + strcpy( rom_file_full_path, file_dir); + strcat( rom_file_full_path, "/"); + strcat( rom_file_full_path, dr->d_name); + read_file_and_print_titleid( full_path ,rom_file_full_path, fp_out , fp_mk ); + } + else { + read_file_and_print_titleid( full_path ,dr->d_name, fp_out , fp_mk ); + } } }