From c3871ba1fa13d074a70e961213ecdedd4cb74dfb Mon Sep 17 00:00:00 2001 From: yutaka Date: Thu, 11 Oct 2007 08:15:35 +0000 Subject: [PATCH] =?UTF-8?q?AES=E6=9A=97=E5=8F=B7=E5=8C=96=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/TwlIPL/trunk@45 b08762b0-b915-fc4b-9d8c-17b2551a87ff --- build/libraries/fatfs/ARM7/src/fatfs_firm.c | 4 +- build/libraries/fatfs/ARM7/src/fatfs_loader.c | 47 ++++++++++++------- build/libraries/mi/ARM9/mi_loader.c | 12 ++--- include/firm/memorymap.h | 10 ++-- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/build/libraries/fatfs/ARM7/src/fatfs_firm.c b/build/libraries/fatfs/ARM7/src/fatfs_firm.c index d8f65d00..a790c165 100644 --- a/build/libraries/fatfs/ARM7/src/fatfs_firm.c +++ b/build/libraries/fatfs/ARM7/src/fatfs_firm.c @@ -216,7 +216,7 @@ static u16 ReadAES(u32 block, void *dest, u16 count) { u32 offset = 0; // in bytes - //OS_TPrintf("ReadAES(%d, 0x%08X, %d) is calling.\n", block, dest, count); + OS_TPrintf("ReadAES(%d, 0x%08X, %d) is calling.\n", block, dest, count); MIi_NDmaPipeSetup( DMA_PIPE, (void*)SDIF_FI, (void*)REG_AES_IFIFO_ADDR, PIPE_SIZE ); @@ -243,7 +243,7 @@ static u16 ReadAES(u32 block, void *dest, u16 count) while ( AES_GET_CNT_BITS( reg_AES_AES_CNT, IFIFO_CNT ) ) { } - if ( (offset & SECTOR_SIZE) == 0 ) + if ( (offset & (SECTOR_SIZE-1)) == 0 ) { WaitFifoFull(); if ( SDCARD_ErrStatus != SDMC_NORMAL ) diff --git a/build/libraries/fatfs/ARM7/src/fatfs_loader.c b/build/libraries/fatfs/ARM7/src/fatfs_loader.c index 1ac66c37..3ede3296 100644 --- a/build/libraries/fatfs/ARM7/src/fatfs_loader.c +++ b/build/libraries/fatfs/ARM7/src/fatfs_loader.c @@ -74,8 +74,6 @@ BOOL FATFS_OpenSpecifiedMenu( const char* menufile ) #define HEADER_SIZE 0x1000 #define AUTH_SIZE ROM_HEADER_SIGN_TARGET_SIZE -#define SLOT_SIZE 0x8000 - #ifndef SDK_FINALROM #define PROFILE_PXI_SEND 1000000000 #define PROFILE_PXI_RECV 2000000000 @@ -85,7 +83,7 @@ extern u32 pf_cnt; static BOOL FATFS_LoadBuffer(u32 offset, u32 size) { - u8* base = (u8*)MI_GetWramMapStart_B(); + u8* base = (u8*)HW_FIRM_LOAD_BUFFER_BASE; static int count = 0; // seek first @@ -100,9 +98,10 @@ static BOOL FATFS_LoadBuffer(u32 offset, u32 size) // loading loop while (size > 0) { - u8* dest = base + count * SLOT_SIZE; // target buffer address - u32 unit = size < SLOT_SIZE ? size : SLOT_SIZE; // size - while (MI_GetWramBankMaster_B(count) != MI_WRAM_ARM7) // waiting to be master + u8* dest = base + count * HW_FIRM_LOAD_BUFFER_UNIT_SIZE; // target buffer address + u32 unit = size < HW_FIRM_LOAD_BUFFER_UNIT_SIZE ? size : HW_FIRM_LOAD_BUFFER_UNIT_SIZE; // size +OS_TPrintf("%s: dest=%X, unit=%X\n", __func__, dest, unit); + while (MI_GetWramBankMaster_B(count) != MI_WRAM_ARM7) // waiting to be master { } #ifndef SDK_FINALROM @@ -119,7 +118,7 @@ static BOOL FATFS_LoadBuffer(u32 offset, u32 size) profile[pf_cnt++] = (u32)PROFILE_PXI_SEND | FIRM_PXI_ID_LOAD_PIRIOD; // checkpoint #endif PXI_NotifyID( FIRM_PXI_ID_LOAD_PIRIOD ); - count = (count + 1) & 0x7; + count = (count + 1) % HW_FIRM_LOAD_BUFFER_UNIT_NUMS; size -= unit; } return TRUE; @@ -176,10 +175,15 @@ BOOL FATFS_LoadHeader( void ) AESKeySeed seed; AESi_InitGameKeys((u8*)rh->s.game_code); PXI_RecvDataByFifo( PXI_FIFO_TAG_DATA, &seed, AES_BLOCK_SIZE ); + AESi_WaitKey(); AESi_SetKeySeedA(&seed); // APP + //AESi_WaitKey(); //AESi_SetKeySeedB(&seed); // APP & HARD + //AESi_WaitKey(); //AESi_SetKeySeedC(&seed); // + //AESi_WaitKey(); //AESi_SetKeySeedD(&seed); // HARD + AESi_WaitKey(); AESi_SetKeyC(&seed); // Direct } @@ -198,9 +202,9 @@ BOOL FATFS_LoadHeader( void ) static AESCounter* FATFSi_GetCounter( u32 offset ) { static AESCounter counter; - MI_CpuCopy8( rh->s.main_static_digest, &counter, 12 ); - counter.words[3] = 0; - AESi_AddCounter( &counter, offset - offsetof(ROM_Header, s.aes_target_rom_offset) ); + + MI_CpuCopy8( rh->s.main_static_digest, &counter, 16 ); + AESi_AddCounter( &counter, offset - rh->s.aes_target_rom_offset ); return &counter; } @@ -216,17 +220,24 @@ static AESCounter* FATFSi_GetCounter( u32 offset ) *---------------------------------------------------------------------------*/ static void FATFSi_SetupAES( u32 offset, u32 size ) { - if ( !rh->s.enable_aes ) - { - FATFS_DisableAES(); - } - else if ( offset >= rh->s.aes_target_rom_offset && + if ( rh->s.enable_aes && + offset >= rh->s.aes_target_rom_offset && offset + size <= rh->s.aes_target_rom_offset + rh->s.aes_target_size ) { AESi_WaitKey(); - //AESi_LoadKey( AES_KEY_SLOT_A ); - AESi_LoadKey( AES_KEY_SLOT_C ); - FATFS_EnableAES( FATFSi_GetCounter( rh->s.main_ltd_rom_offset ) ); + if (rh->s.developer_encrypt) + { + AESi_LoadKey( AES_KEY_SLOT_C ); + } + else + { + AESi_LoadKey( AES_KEY_SLOT_A ); + } + FATFS_EnableAES( FATFSi_GetCounter( offset ) ); + } + else + { + FATFS_DisableAES(); } } diff --git a/build/libraries/mi/ARM9/mi_loader.c b/build/libraries/mi/ARM9/mi_loader.c index 1aff69e9..73b1072b 100644 --- a/build/libraries/mi/ARM9/mi_loader.c +++ b/build/libraries/mi/ARM9/mi_loader.c @@ -27,8 +27,6 @@ static ROM_Header* const rh = (ROM_Header*)HW_TWL_ROM_HEADER_BUF; #define AUTH_SIZE 0xe00 #define RSA_BLOCK_SIZE 128 -#define SLOT_SIZE 0x8000 - #define HASH_UNIT 0x800 // TODO: optimizing to maximize cache efficiency /* @@ -230,13 +228,13 @@ extern u32 pf_cnt; static BOOL MI_LoadBuffer(u8* dest, u32 size, SHA1_CTX *ctx) { - u8* base = (void*)MI_GetWramMapStart_B(); + u8* base = (u8*)HW_FIRM_LOAD_BUFFER_BASE; static int count = 0; - while (size > 0) { - u8* src = base + count * SLOT_SIZE; - u32 unit = size < SLOT_SIZE ? size : SLOT_SIZE; + u8* src = base + count * HW_FIRM_LOAD_BUFFER_UNIT_SIZE; + u32 unit = size < HW_FIRM_LOAD_BUFFER_UNIT_SIZE ? size : HW_FIRM_LOAD_BUFFER_UNIT_SIZE; +OS_TPrintf("%s: src=%X, unit=%X\n", __func__, src, unit); if ( PXI_RecvID() != FIRM_PXI_ID_LOAD_PIRIOD ) { return FALSE; @@ -266,7 +264,7 @@ static BOOL MI_LoadBuffer(u8* dest, u32 size, SHA1_CTX *ctx) MI_CpuClearFast( src, unit ); DC_FlushRange( src, unit ); MIi_SetWramBankMaster_B(count, MI_WRAM_ARM7); - count = (count + 1) & 0x07; + count = (count + 1) % HW_FIRM_LOAD_BUFFER_UNIT_NUMS; size -= unit; dest += unit; } diff --git a/include/firm/memorymap.h b/include/firm/memorymap.h index 6844c816..e1a8c98d 100644 --- a/include/firm/memorymap.h +++ b/include/firm/memorymap.h @@ -17,12 +17,14 @@ #ifndef FIRM_MEMORYMAP_H_ #define FIRM_MEMORYMAP_H_ -#include +#include -#ifdef SDK_ARM9 -#include +#include + +#ifdef SDK_ARM9 +#include #else //SDK_ARM7 -#include +#include #endif /* FIRM_MEMORYMAP_H_ */