#include #include #include #include "types.h" #include "utils.h" #include "ivfc.h" #include "ctr.h" void ivfc_init(ivfc_context* ctx) { memset(ctx, 0, sizeof(ivfc_context)); } void ivfc_set_usersettings(ivfc_context* ctx, settings* usersettings) { ctx->usersettings = usersettings; } void ivfc_set_offset(ivfc_context* ctx, u64 offset) { ctx->offset = offset; } void ivfc_set_size(ivfc_context* ctx, u64 size) { ctx->size = size; } void ivfc_set_file(ivfc_context* ctx, FILE* file) { ctx->file = file; } void ivfc_process(ivfc_context* ctx, u32 actions) { fseeko64(ctx->file, ctx->offset, SEEK_SET); fread(&ctx->header, 1, sizeof(ivfc_header), ctx->file); if (getle32(ctx->header.magic) != MAGIC_IVFC) { fprintf(stdout, "Error, IVFC segment corrupted\n"); return; } if (getle32(ctx->header.id) == 0x10000) { fread(&ctx->romfsheader, 1, sizeof(ivfc_header_romfs), ctx->file); ctx->levelcount = 3; ctx->level[2].hashblocksize = 1 << getle32(ctx->romfsheader.level3.blocksize); ctx->level[1].hashblocksize = 1 << getle32(ctx->romfsheader.level2.blocksize); ctx->level[0].hashblocksize = 1 << getle32(ctx->romfsheader.level1.blocksize); ctx->bodyoffset = align64(IVFC_HEADER_SIZE + getle32(ctx->romfsheader.masterhashsize), ctx->level[2].hashblocksize); ctx->bodysize = getle64(ctx->romfsheader.level3.hashdatasize); ctx->level[2].dataoffset = ctx->bodyoffset; ctx->level[2].datasize = align64(ctx->bodysize, ctx->level[2].hashblocksize); ctx->level[0].dataoffset = ctx->level[2].dataoffset + ctx->level[2].datasize; ctx->level[0].datasize = align64(getle64(ctx->romfsheader.level1.hashdatasize), ctx->level[0].hashblocksize); ctx->level[1].dataoffset = ctx->level[0].dataoffset + ctx->level[0].datasize; ctx->level[1].datasize = align64(getle64(ctx->romfsheader.level2.hashdatasize), ctx->level[1].hashblocksize); ctx->level[0].hashoffset = IVFC_HEADER_SIZE; ctx->level[1].hashoffset = ctx->level[0].dataoffset; ctx->level[2].hashoffset = ctx->level[1].dataoffset; } if (actions & VerifyFlag) ivfc_verify(ctx, actions); if (actions & InfoFlag) ivfc_print(ctx); } void ivfc_verify(ivfc_context* ctx, u32 flags) { u32 i, j; u32 blockcount; for(i=0; ilevelcount; i++) { ivfc_level* level = ctx->level + i; level->hashcheck = Fail; } for(i=0; ilevelcount; i++) { ivfc_level* level = ctx->level + i; blockcount = level->datasize / level->hashblocksize; if (level->datasize % level->hashblocksize != 0) { fprintf(stderr, "Error, IVFC block size mismatch\n"); return; } level->hashcheck = Good; for(j=0; jdataoffset + level->hashblocksize * j, level->hashblocksize, calchash); ivfc_read(ctx, level->hashoffset + 0x20 * j, 0x20, testhash); if (memcmp(calchash, testhash, 0x20) != 0) level->hashcheck = Fail; } } } void ivfc_read(ivfc_context* ctx, u64 offset, u64 size, u8* buffer) { if ( (offset > ctx->size) || (offset+size > ctx->size) ) { fprintf(stderr, "Error, IVFC offset out of range (offset=0x%08"PRIx64", size=0x%08"PRIx64")\n", offset, size); return; } fseeko64(ctx->file, ctx->offset + offset, SEEK_SET); if (size != fread(buffer, 1, size, ctx->file)) { fprintf(stderr, "Error, IVFC could not read file\n"); return; } } void ivfc_hash(ivfc_context* ctx, u64 offset, u64 size, u8* hash) { if (size > IVFC_MAX_BUFFERSIZE) { fprintf(stderr, "Error, IVFC hash block size too big.\n"); return; } ivfc_read(ctx, offset, size, ctx->buffer); ctr_sha_256(ctx->buffer, size, hash); } void ivfc_print(ivfc_context* ctx) { u32 i; ivfc_header* header = &ctx->header; fprintf(stdout, "\nIVFC:\n"); fprintf(stdout, "Header: %.4s\n", header->magic); fprintf(stdout, "Id: %08x\n", getle32(header->id)); for(i=0; ilevelcount; i++) { ivfc_level* level = ctx->level + i; fprintf(stdout, "\n"); if (level->hashcheck == Unchecked) fprintf(stdout, "Level %d: \n", i); else fprintf(stdout, "Level %d (%s): \n", i, level->hashcheck == Good? "GOOD" : "FAIL"); fprintf(stdout, " Data offset: 0x%08"PRIx64"\n", ctx->offset + level->dataoffset); fprintf(stdout, " Data size: 0x%08"PRIx64"\n", level->datasize); fprintf(stdout, " Hash offset: 0x%08"PRIx64"\n", ctx->offset + level->hashoffset); fprintf(stdout, " Hash block size: 0x%08x\n", level->hashblocksize); } } u64 ivfc_get_body_offset(ivfc_context* ctx) { return ctx->bodyoffset; } u64 ivfc_get_body_size(ivfc_context* ctx) { return ctx->bodysize; }