private static IVFCInfo GetIVFCInfoFromBytes(byte[] headerBytes) { IVFCInfo ivfc = new IVFCInfo(); uint magic1 = BitConverter.ToUInt32(headerBytes, 0x00); uint magic2 = BitConverter.ToUInt32(headerBytes, 0x04); if (magic1 != 0x43465649 || magic2 != 0x10000) { throw new ArgumentException("Bad IVFC Header"); } ivfc.MasterHashSize = BitConverter.ToUInt32(headerBytes, 0x08); ivfc.Levels = new IVFCLevel[3]; for (int i = 0; i < 3; i++) { ivfc.Levels[i] = new IVFCLevel(); ivfc.Levels[i].HashOffset = BitConverter.ToUInt64(headerBytes, 0x0C + (i * 0x18)); ivfc.Levels[i].DataLength = BitConverter.ToUInt64(headerBytes, 0x14 + (i * 0x18)); ivfc.Levels[i].BlockSize = 1U << BitConverter.ToInt32(headerBytes, 0x1C + (i * 0x18)); } return(ivfc); }
public void ExtractRomFS(string outputDirectory, RichTextBox TB_Progress = null, ProgressBar PB_Show = null) { byte[] ivfcHeaderBytes = new byte[0x5C]; using (FileStream file = new FileStream(FileName, FileMode.Open, FileAccess.Read)) { file.Read(ivfcHeaderBytes, 0, ivfcHeaderBytes.Length); } IVFCInfo ivfc = GetIVFCInfoFromBytes(ivfcHeaderBytes); ulong romfsDataOffset = Align(ivfc.Levels[0].HashOffset + ivfc.MasterHashSize, ivfc.Levels[2].BlockSize); byte[] romfsInfoHeaderBytes = new byte[0x28]; using (FileStream file = new FileStream(FileName, FileMode.Open, FileAccess.Read)) { file.Seek(Convert.ToInt64(romfsDataOffset), SeekOrigin.Begin); file.Read(romfsInfoHeaderBytes, 0, romfsInfoHeaderBytes.Length); } Romfs_InfoHeader infoHeader = GetRomfsInfoHeaderFromBytes(romfsInfoHeaderBytes, Convert.ToUInt32(romfsDataOffset)); byte[] directoryMetadataBlock = new byte[infoHeader.Sections[1].Size]; byte[] fileMetadataBlock = new byte[infoHeader.Sections[3].Size]; using (FileStream file = new FileStream(FileName, FileMode.Open, FileAccess.Read)) { file.Seek(Convert.ToInt64(infoHeader.Sections[1].Offset), SeekOrigin.Begin); file.Read(directoryMetadataBlock, 0, directoryMetadataBlock.Length); file.Seek(Convert.ToInt64(infoHeader.Sections[3].Offset), SeekOrigin.Begin); file.Read(fileMetadataBlock, 0, fileMetadataBlock.Length); } if (!Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } VisitDirectory(0, outputDirectory, directoryMetadataBlock, fileMetadataBlock, infoHeader.DataOffset, TB_Progress, PB_Show); }
internal static void MakeRomFSData(RomfsFile[] RomFiles, MemoryStream metadata, RichTextBox TB_Progress = null, ProgressBar PB_Show = null) { updateTB(TB_Progress, "Computing IVFC Header Data..."); IVFCInfo ivfc = new IVFCInfo { Levels = new IVFCLevel[3] }; for (int i = 0; i < ivfc.Levels.Length; i++) { ivfc.Levels[i] = new IVFCLevel { BlockSize = 0x1000 }; } ivfc.Levels[2].DataLength = RomfsFile.GetDataBlockLength(RomFiles, (ulong)metadata.Length); ivfc.Levels[1].DataLength = Align(ivfc.Levels[2].DataLength, ivfc.Levels[2].BlockSize) / ivfc.Levels[2].BlockSize * 0x20; //0x20 per SHA256 hash ivfc.Levels[0].DataLength = Align(ivfc.Levels[1].DataLength, ivfc.Levels[1].BlockSize) / ivfc.Levels[1].BlockSize * 0x20; //0x20 per SHA256 hash ulong MasterHashLen = Align(ivfc.Levels[0].DataLength, ivfc.Levels[0].BlockSize) / ivfc.Levels[0].BlockSize * 0x20; ulong lofs = 0; foreach (IVFCLevel t in ivfc.Levels) { t.HashOffset = lofs; lofs += Align(t.DataLength, t.BlockSize); } const uint IVFC_MAGIC = 0x43465649; //IVFC const uint RESERVED = 0x0; const uint HeaderLen = 0x5C; const uint MEDIA_UNIT_SIZE = 0x200; byte[] SuperBlockHash = new byte[0x20]; FileStream OutFileStream = new FileStream(TempFile, FileMode.Create, FileAccess.ReadWrite); try { OutFileStream.Seek(0, SeekOrigin.Begin); OutFileStream.Write(BitConverter.GetBytes(IVFC_MAGIC), 0, 0x4); OutFileStream.Write(BitConverter.GetBytes(0x10000), 0, 0x4); OutFileStream.Write(BitConverter.GetBytes(MasterHashLen), 0, 0x4); foreach (IVFCLevel t in ivfc.Levels) { OutFileStream.Write(BitConverter.GetBytes(t.HashOffset), 0, 0x8); OutFileStream.Write(BitConverter.GetBytes(t.DataLength), 0, 0x8); OutFileStream.Write(BitConverter.GetBytes((int)Math.Log(t.BlockSize, 2)), 0, 0x4); OutFileStream.Write(BitConverter.GetBytes(RESERVED), 0, 0x4); } OutFileStream.Write(BitConverter.GetBytes(HeaderLen), 0, 0x4); //IVFC Header is Written. OutFileStream.Seek((long)Align(MasterHashLen + 0x60, ivfc.Levels[0].BlockSize), SeekOrigin.Begin); byte[] metadataArray = metadata.ToArray(); OutFileStream.Write(metadataArray, 0, metadataArray.Length); long baseOfs = OutFileStream.Position; updateTB(TB_Progress, "Writing Level 2 Data..."); if (PB_Show.InvokeRequired) { PB_Show.Invoke((MethodInvoker) delegate { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = RomFiles.Length; }); } else { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = RomFiles.Length; } foreach (RomfsFile t in RomFiles) { OutFileStream.Seek(baseOfs + (long)t.Offset, SeekOrigin.Begin); using (FileStream inStream = new FileStream(t.FullName, FileMode.Open, FileAccess.Read)) { while (inStream.Position < inStream.Length) { byte[] buffer = new byte[inStream.Length - inStream.Position > 0x100000 ? 0x100000 : inStream.Length - inStream.Position]; inStream.Read(buffer, 0, buffer.Length); OutFileStream.Write(buffer, 0, buffer.Length); } } if (PB_Show.InvokeRequired) { PB_Show.Invoke((MethodInvoker)PB_Show.PerformStep); } else { PB_Show.PerformStep(); } } long hashBaseOfs = (long)Align((ulong)OutFileStream.Position, ivfc.Levels[2].BlockSize); long hOfs = (long)Align(MasterHashLen, ivfc.Levels[0].BlockSize); long cOfs = hashBaseOfs + (long)ivfc.Levels[1].HashOffset; SHA256Managed sha = new SHA256Managed(); for (int i = ivfc.Levels.Length - 1; i >= 0; i--) { updateTB(TB_Progress, "Computing Level " + i + " Hashes..."); byte[] buffer = new byte[(int)ivfc.Levels[i].BlockSize]; var count = (int)(ivfc.Levels[i].DataLength / ivfc.Levels[i].BlockSize); if (PB_Show.InvokeRequired) { PB_Show.Invoke((MethodInvoker) delegate { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = count; }); } else { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = count; } for (long ofs = 0; ofs < (long)ivfc.Levels[i].DataLength; ofs += ivfc.Levels[i].BlockSize) { OutFileStream.Seek(hOfs, SeekOrigin.Begin); OutFileStream.Read(buffer, 0, (int)ivfc.Levels[i].BlockSize); hOfs = OutFileStream.Position; byte[] hash = sha.ComputeHash(buffer); OutFileStream.Seek(cOfs, SeekOrigin.Begin); OutFileStream.Write(hash, 0, hash.Length); cOfs = OutFileStream.Position; if (PB_Show.InvokeRequired) { PB_Show.Invoke((MethodInvoker)PB_Show.PerformStep); } else { PB_Show.PerformStep(); } } if (i <= 0) { continue; } if (i == 2) { long len = OutFileStream.Position; if (len % 0x1000 != 0) { len = (long)Align((ulong)len, 0x1000); byte[] buf = new byte[len - OutFileStream.Position]; OutFileStream.Write(buf, 0, buf.Length); } } hOfs = hashBaseOfs + (long)ivfc.Levels[i - 1].HashOffset; if (i > 1) { cOfs = hashBaseOfs + (long)ivfc.Levels[i - 2].HashOffset; } else { cOfs = (long)Align(HeaderLen, PADDING_ALIGN); } } OutFileStream.Seek(0, SeekOrigin.Begin); uint SuperBlockLen = (uint)Align(MasterHashLen + 0x60, MEDIA_UNIT_SIZE); byte[] MasterHashes = new byte[SuperBlockLen]; OutFileStream.Read(MasterHashes, 0, (int)SuperBlockLen); SuperBlockHash = sha.ComputeHash(MasterHashes); } finally { OutFileStream.Dispose(); } if (OutFile == TempFile) { return; } if (File.Exists(OutFile)) { File.Delete(OutFile); } File.Move(TempFile, OutFile); }
internal static void MakeRomFSData(RomfsFile[] RomFiles, MemoryStream metadata, RichTextBox TB_Progress = null, ProgressBar PB_Show = null) { updateTB(TB_Progress, "Computing IVFC Header Data..."); IVFCInfo ivfc = new IVFCInfo { Levels = new IVFCLevel[3] }; for (int i = 0; i < ivfc.Levels.Length; i++) { ivfc.Levels[i] = new IVFCLevel { BlockSize = 0x1000 }; } ivfc.Levels[2].DataLength = RomfsFile.GetDataBlockLength(RomFiles, (ulong)metadata.Length); ivfc.Levels[1].DataLength = (Align(ivfc.Levels[2].DataLength, ivfc.Levels[2].BlockSize) / ivfc.Levels[2].BlockSize) * 0x20; //0x20 per SHA256 hash ivfc.Levels[0].DataLength = (Align(ivfc.Levels[1].DataLength, ivfc.Levels[1].BlockSize) / ivfc.Levels[1].BlockSize) * 0x20; //0x20 per SHA256 hash ulong MasterHashLen = (Align(ivfc.Levels[0].DataLength, ivfc.Levels[0].BlockSize) / ivfc.Levels[0].BlockSize) * 0x20; ulong lofs = 0; foreach (IVFCLevel t in ivfc.Levels) { t.HashOffset = lofs; lofs += Align(t.DataLength, t.BlockSize); } const uint IVFC_MAGIC = 0x43465649; //IVFC const uint RESERVED = 0x0; const uint HeaderLen = 0x5C; FileStream OutFileStream = new FileStream(TempFile, FileMode.Create, FileAccess.ReadWrite); try { OutFileStream.Seek(0, SeekOrigin.Begin); OutFileStream.Write(BitConverter.GetBytes(IVFC_MAGIC), 0, 0x4); OutFileStream.Write(BitConverter.GetBytes(0x10000), 0, 0x4); OutFileStream.Write(BitConverter.GetBytes(MasterHashLen), 0, 0x4); foreach (IVFCLevel t in ivfc.Levels) { OutFileStream.Write(BitConverter.GetBytes(t.HashOffset), 0, 0x8); OutFileStream.Write(BitConverter.GetBytes(t.DataLength), 0, 0x8); OutFileStream.Write(BitConverter.GetBytes((int)(Math.Log(t.BlockSize, 2))), 0, 0x4); OutFileStream.Write(BitConverter.GetBytes(RESERVED), 0, 0x4); } OutFileStream.Write(BitConverter.GetBytes(HeaderLen), 0, 0x4); //IVFC Header is Written. OutFileStream.Seek((long)Align(MasterHashLen + 0x60, ivfc.Levels[0].BlockSize), SeekOrigin.Begin); byte[] metadataArray = metadata.ToArray(); OutFileStream.Write(metadataArray, 0, metadataArray.Length); long baseOfs = OutFileStream.Position; updateTB(TB_Progress, "Writing Level 2 Data..."); if (PB_Show.InvokeRequired) PB_Show.Invoke((MethodInvoker)delegate { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = RomFiles.Length; }); else { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = RomFiles.Length; } foreach (RomfsFile t in RomFiles) { OutFileStream.Seek(baseOfs + (long)t.Offset, SeekOrigin.Begin); using (FileStream inStream = new FileStream(t.FullName, FileMode.Open, FileAccess.Read)) { while (inStream.Position < inStream.Length) { byte[] buffer = new byte[inStream.Length - inStream.Position > 0x100000 ? 0x100000 : inStream.Length - inStream.Position]; inStream.Read(buffer, 0, buffer.Length); OutFileStream.Write(buffer, 0, buffer.Length); } } if (PB_Show.InvokeRequired) PB_Show.Invoke((MethodInvoker)delegate { PB_Show.PerformStep(); }); else { PB_Show.PerformStep(); } } long hashBaseOfs = (long)Align((ulong)OutFileStream.Position, ivfc.Levels[2].BlockSize); long hOfs = (long)Align(MasterHashLen, ivfc.Levels[0].BlockSize); long cOfs = hashBaseOfs + (long)ivfc.Levels[1].HashOffset; SHA256Managed sha = new SHA256Managed(); for (int i = ivfc.Levels.Length - 1; i >= 0; i--) { updateTB(TB_Progress, "Computing Level " + i + " Hashes..."); byte[] buffer = new byte[(int)ivfc.Levels[i].BlockSize]; if (PB_Show.InvokeRequired) PB_Show.Invoke((MethodInvoker)delegate { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = (int)(ivfc.Levels[i].DataLength / ivfc.Levels[i].BlockSize); }); else { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = (int)(ivfc.Levels[i].DataLength / ivfc.Levels[i].BlockSize); } for (long ofs = 0; ofs < (long)ivfc.Levels[i].DataLength; ofs += ivfc.Levels[i].BlockSize) { OutFileStream.Seek(hOfs, SeekOrigin.Begin); OutFileStream.Read(buffer, 0, (int)ivfc.Levels[i].BlockSize); hOfs = OutFileStream.Position; byte[] hash = sha.ComputeHash(buffer); OutFileStream.Seek(cOfs, SeekOrigin.Begin); OutFileStream.Write(hash, 0, hash.Length); cOfs = OutFileStream.Position; if (PB_Show.InvokeRequired) PB_Show.Invoke((MethodInvoker)delegate { PB_Show.PerformStep(); }); else { PB_Show.PerformStep(); } } if (i == 2) { long len = OutFileStream.Position; if (len % 0x1000 != 0) { len = (long)Align((ulong)len, 0x1000); byte[] buf = new byte[len - OutFileStream.Position]; OutFileStream.Write(buf, 0, buf.Length); } } if (i <= 0) continue; hOfs = hashBaseOfs + (long)ivfc.Levels[i - 1].HashOffset; if (i > 1) cOfs = hashBaseOfs + (long)ivfc.Levels[i - 2].HashOffset; else cOfs = (long)Align(HeaderLen, PADDING_ALIGN); } } finally { if (OutFileStream != null) OutFileStream.Dispose(); } if (OutFile != TempFile) { if (File.Exists(OutFile)) File.Delete(OutFile); File.Move(TempFile, OutFile); } }
internal static Stream MakeRomFSData(RomfsFile[] RomFiles, MemoryStream metadata, string _writeDest) { updateTB("Computing IVFC Header Data..."); IVFCInfo ivfc = new IVFCInfo { Levels = new IVFCLevel[3] }; for (int i = 0; i < ivfc.Levels.Length; i++) { ivfc.Levels[i] = new IVFCLevel { BlockSize = 0x1000 }; } ivfc.Levels[2].DataLength = RomfsFile.GetDataBlockLength(RomFiles, (ulong)metadata.Length); ivfc.Levels[1].DataLength = (Align(ivfc.Levels[2].DataLength, ivfc.Levels[2].BlockSize) / ivfc.Levels[2].BlockSize) * 0x20; //0x20 per SHA256 hash ivfc.Levels[0].DataLength = (Align(ivfc.Levels[1].DataLength, ivfc.Levels[1].BlockSize) / ivfc.Levels[1].BlockSize) * 0x20; //0x20 per SHA256 hash ulong MasterHashLen = (Align(ivfc.Levels[0].DataLength, ivfc.Levels[0].BlockSize) / ivfc.Levels[0].BlockSize) * 0x20; ulong lofs = 0; foreach (IVFCLevel t in ivfc.Levels) { t.HashOffset = lofs; lofs += Align(t.DataLength, t.BlockSize); } const uint IVFC_MAGIC = 0x43465649; //IVFC const uint RESERVED = 0x0; const uint HeaderLen = 0x5C; Stream OutFileStream; if (_writeDest != null) { OutFileStream = new FileStream(TempFile, FileMode.Create, FileAccess.ReadWrite); } else { OutFileStream = new MemoryStream(); } try { OutFileStream.Seek(0, SeekOrigin.Begin); OutFileStream.Write(BitConverter.GetBytes(IVFC_MAGIC), 0, 0x4); OutFileStream.Write(BitConverter.GetBytes(0x10000), 0, 0x4); OutFileStream.Write(BitConverter.GetBytes(MasterHashLen), 0, 0x4); foreach (IVFCLevel t in ivfc.Levels) { OutFileStream.Write(BitConverter.GetBytes(t.HashOffset), 0, 0x8); OutFileStream.Write(BitConverter.GetBytes(t.DataLength), 0, 0x8); OutFileStream.Write(BitConverter.GetBytes((int)(Math.Log(t.BlockSize, 2))), 0, 0x4); OutFileStream.Write(BitConverter.GetBytes(RESERVED), 0, 0x4); } OutFileStream.Write(BitConverter.GetBytes(HeaderLen), 0, 0x4); //IVFC Header is Written. OutFileStream.Seek((long)Align(MasterHashLen + 0x60, ivfc.Levels[0].BlockSize), SeekOrigin.Begin); byte[] metadataArray = metadata.ToArray(); OutFileStream.Write(metadataArray, 0, metadataArray.Length); long baseOfs = OutFileStream.Position; updateTB("Writing Level 2 Data..."); foreach (RomfsFile t in RomFiles) { OutFileStream.Seek(baseOfs + (long)t.Offset, SeekOrigin.Begin); using (FileStream inStream = new FileStream(t.realFilePath, FileMode.Open, FileAccess.Read)) { while (inStream.Position < inStream.Length) { byte[] buffer = new byte[inStream.Length - inStream.Position > 0x100000 ? 0x100000 : inStream.Length - inStream.Position]; inStream.Read(buffer, 0, buffer.Length); OutFileStream.Write(buffer, 0, buffer.Length); } } } long hashBaseOfs = (long)Align((ulong)OutFileStream.Position, ivfc.Levels[2].BlockSize); long hOfs = (long)Align(MasterHashLen, ivfc.Levels[0].BlockSize); long cOfs = hashBaseOfs + (long)ivfc.Levels[1].HashOffset; SHA256Managed sha = new SHA256Managed(); for (int i = ivfc.Levels.Length - 1; i >= 0; i--) { updateTB("Computing Level " + i + " Hashes..."); byte[] buffer = new byte[(int)ivfc.Levels[i].BlockSize]; for (long ofs = 0; ofs < (long)ivfc.Levels[i].DataLength; ofs += ivfc.Levels[i].BlockSize) { OutFileStream.Seek(hOfs, SeekOrigin.Begin); OutFileStream.Read(buffer, 0, (int)ivfc.Levels[i].BlockSize); hOfs = OutFileStream.Position; byte[] hash = sha.ComputeHash(buffer); OutFileStream.Seek(cOfs, SeekOrigin.Begin); OutFileStream.Write(hash, 0, hash.Length); cOfs = OutFileStream.Position; } if (i == 2) { long len = OutFileStream.Position; if (len % 0x1000 != 0) { len = (long)Align((ulong)len, 0x1000); byte[] buf = new byte[len - OutFileStream.Position]; OutFileStream.Write(buf, 0, buf.Length); } } if (i <= 0) { continue; } hOfs = hashBaseOfs + (long)ivfc.Levels[i - 1].HashOffset; if (i > 1) { cOfs = hashBaseOfs + (long)ivfc.Levels[i - 2].HashOffset; } else { cOfs = (long)Align(HeaderLen, PADDING_ALIGN); } } } finally { if (OutFileStream != null && _writeDest != null) { OutFileStream.Dispose(); } } if (_writeDest != null) { OutFileStream.Dispose(); if (_writeDest != TempFile) { if (File.Exists(OutFile)) { File.Delete(OutFile); } File.Move(TempFile, OutFile); } return(null); } else { return(OutFileStream); } }