From 291b64b5985d0a46ec25f6ca21232585bd53d4ac Mon Sep 17 00:00:00 2001 From: applestash Date: Tue, 26 Aug 2014 13:32:55 +1000 Subject: [PATCH 1/3] ncch key stuff --- makerom/ncch.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/makerom/ncch.c b/makerom/ncch.c index e81ede8..9877872 100644 --- a/makerom/ncch.c +++ b/makerom/ncch.c @@ -629,6 +629,7 @@ int SetCommonHeaderBasicData(ncch_settings *set, ncch_hdr *hdr) if(!SetNcchKeys(set->keys,hdr) && set->options.Encrypt){ hdr->flags[ncchflag_OTHER_FLAG] = (otherflag_NoCrypto|otherflag_FixedCryptoKey); + hdr->flags[ncchflag_CONTENT_KEYX] = 0; set->options.Encrypt = false; fprintf(stderr,"[NCCH WARNING] NCCH AES Key could not be loaded, NCCH will not be encrypted\n"); } @@ -992,17 +993,20 @@ bool SetNcchKeys(keys_struct *keys, ncch_hdr *hdr) return true; if((hdr->flags[ncchflag_OTHER_FLAG] & otherflag_FixedCryptoKey) == otherflag_FixedCryptoKey){ - if((hdr->programId[4] & 0x10) == 0x10 && keys->aes.systemFixedKey){ + if((hdr->programId[4] & 0x10) == 0x10){ + if(!keys->aes.systemFixedKey) + return false; memcpy(keys->aes.ncchKey0,keys->aes.systemFixedKey,AES_128_KEY_SIZE); memcpy(keys->aes.ncchKey1,keys->aes.systemFixedKey,AES_128_KEY_SIZE); return true; } - else if(keys->aes.normalKey){ + else{ + if(!keys->aes.normalKey) + return false; memcpy(keys->aes.ncchKey0,keys->aes.normalKey,AES_128_KEY_SIZE); memcpy(keys->aes.ncchKey1,keys->aes.normalKey,AES_128_KEY_SIZE); return true; } - return false; } if(keys->aes.ncchKeyX[0]) @@ -1024,7 +1028,7 @@ int GetNcchInfo(ncch_info *info, ncch_hdr *hdr) memcpy(info->programId,hdr->programId,8); - u32 media_unit = GetNcchBlockSize(hdr); + u32 block_size = GetNcchBlockSize(hdr); info->formatVersion = u8_to_u16(hdr->formatVersion,LE); if(!IsCfa(hdr)){ @@ -1032,18 +1036,18 @@ int GetNcchInfo(ncch_info *info, ncch_hdr *hdr) info->exhdrSize = u8_to_u32(hdr->exhdrSize,LE); info->acexOffset = (info->exhdrOffset + info->exhdrSize); info->acexSize = sizeof(access_descriptor); - info->plainRegionOffset = (u64)(u8_to_u32(hdr->plainRegionOffset,LE)*media_unit); - info->plainRegionSize = (u64)(u8_to_u32(hdr->plainRegionSize,LE)*media_unit); + info->plainRegionOffset = (u64)(u8_to_u32(hdr->plainRegionOffset,LE)*block_size); + info->plainRegionSize = (u64)(u8_to_u32(hdr->plainRegionSize,LE)*block_size); } - info->logoOffset = (u64)(u8_to_u32(hdr->logoOffset,LE)*media_unit); - info->logoSize = (u64)(u8_to_u32(hdr->logoSize,LE)*media_unit); - info->exefsOffset = (u64)(u8_to_u32(hdr->exefsOffset,LE)*media_unit); - info->exefsSize = (u64)(u8_to_u32(hdr->exefsSize,LE)*media_unit); - info->exefsHashDataSize = (u64)(u8_to_u32(hdr->exefsHashSize,LE)*media_unit); - info->romfsOffset = (u64) (u8_to_u32(hdr->romfsOffset,LE)*media_unit); - info->romfsSize = (u64) (u8_to_u32(hdr->romfsSize,LE)*media_unit); - info->romfsHashDataSize = (u64)(u8_to_u32(hdr->romfsHashSize,LE)*media_unit); + info->logoOffset = (u64)(u8_to_u32(hdr->logoOffset,LE)*block_size); + info->logoSize = (u64)(u8_to_u32(hdr->logoSize,LE)*block_size); + info->exefsOffset = (u64)(u8_to_u32(hdr->exefsOffset,LE)*block_size); + info->exefsSize = (u64)(u8_to_u32(hdr->exefsSize,LE)*block_size); + info->exefsHashDataSize = (u64)(u8_to_u32(hdr->exefsHashSize,LE)*block_size); + info->romfsOffset = (u64) (u8_to_u32(hdr->romfsOffset,LE)*block_size); + info->romfsSize = (u64) (u8_to_u32(hdr->romfsSize,LE)*block_size); + info->romfsHashDataSize = (u64)(u8_to_u32(hdr->romfsHashSize,LE)*block_size); return 0; } @@ -1079,8 +1083,7 @@ void GetNcchAesCounter(ncch_info *ctx, u8 counter[16], u8 type) if (ctx->formatVersion == 2 || ctx->formatVersion == 0) { - for(i=0; i<8; i++) - counter[i] = titleId[7-i]; + endian_memcpy(counter,titleId,8,LE); counter[8] = type; } else if (ctx->formatVersion == 1) From a90378b6f4db83569a8953765146310fe398e3bf Mon Sep 17 00:00:00 2001 From: applestash Date: Tue, 26 Aug 2014 15:44:15 +1000 Subject: [PATCH 2/3] misc --- makerom/ncsd.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/makerom/ncsd.c b/makerom/ncsd.c index 2827f5b..4f01c64 100644 --- a/makerom/ncsd.c +++ b/makerom/ncsd.c @@ -681,17 +681,14 @@ bool IsCci(u8 *ncsd) u64 GetPartitionOffset(u8 *ncsd, u8 index) { cci_hdr *hdr = (cci_hdr*)ncsd; - u32 media_size = 1 << (hdr->flags[cciflag_MEDIA_BLOCK_SIZE] + 9); - u32 offset = u8_to_u64(hdr->offset_sizeTable[index].offset,LE); - return (u64)offset*(u64)media_size; + u32 offset = ; + return (u64)u8_to_u64(hdr->offset_sizeTable[index].offset,LE) * (u64)GetCtrBlockSize(hdr->flags[cciflag_MEDIA_BLOCK_SIZE]); } u64 GetPartitionSize(u8 *ncsd, u8 index) { cci_hdr *hdr = (cci_hdr*)ncsd; - u32 media_size = 1 << (hdr->flags[cciflag_MEDIA_BLOCK_SIZE] + 9); - u32 size = u8_to_u64(hdr->offset_sizeTable[index].size,LE); - return (u64)size*(u64)media_size; + return (u64)u8_to_u64(hdr->offset_sizeTable[index].size,LE) * (u64)GetCtrBlockSize(hdr->flags[cciflag_MEDIA_BLOCK_SIZE]); } u8* GetPartition(u8 *ncsd, u8 index) From e5b6a1bd722814204d831cd13c7279b122eaaf3a Mon Sep 17 00:00:00 2001 From: applestash Date: Tue, 26 Aug 2014 16:03:06 +1000 Subject: [PATCH 3/3] misc --- makerom/ncsd.c | 1 - 1 file changed, 1 deletion(-) diff --git a/makerom/ncsd.c b/makerom/ncsd.c index 4f01c64..c554eb0 100644 --- a/makerom/ncsd.c +++ b/makerom/ncsd.c @@ -681,7 +681,6 @@ bool IsCci(u8 *ncsd) u64 GetPartitionOffset(u8 *ncsd, u8 index) { cci_hdr *hdr = (cci_hdr*)ncsd; - u32 offset = ; return (u64)u8_to_u64(hdr->offset_sizeTable[index].offset,LE) * (u64)GetCtrBlockSize(hdr->flags[cciflag_MEDIA_BLOCK_SIZE]); }