Ejemplo n.º 1
0
 private void MakeRomFSData(RomfsFile[] RomFiles, MemoryStream metadata)
 {
     TempFile = Path.GetRandomFileName();
     Console.WriteLine("Computing IVFC Header Data...");
     IVFCInfo ivfc = new IVFCInfo();
     ivfc.Levels = new IVFCLevel[3];
     for (int i = 0; i < ivfc.Levels.Length; i++)
     {
         ivfc.Levels[i] = new IVFCLevel();
         ivfc.Levels[i].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;
     for (int i = 0; i < ivfc.Levels.Length; i++)
     {
         ivfc.Levels[i].HashOffset = lofs;
         lofs += Align(ivfc.Levels[i].DataLength, ivfc.Levels[i].BlockSize);
     }
     uint IVFC_MAGIC = 0x43465649; //IVFC
     uint RESERVED = 0x0;
     uint HeaderLen = 0x5C;
     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);
         for (int i = 0; i < ivfc.Levels.Length; i++)
         {
             OutFileStream.Write(BitConverter.GetBytes(ivfc.Levels[i].HashOffset), 0, 0x8);
             OutFileStream.Write(BitConverter.GetBytes(ivfc.Levels[i].DataLength), 0, 0x8);
             OutFileStream.Write(BitConverter.GetBytes((int)(Math.Log(ivfc.Levels[i].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;
         Console.WriteLine("Writing Level 2 Data...");
         for (int i = 0; i < RomFiles.Length; i++)
         {
             OutFileStream.Seek((long)(baseOfs + (long)RomFiles[i].Offset), SeekOrigin.Begin);
             using (FileStream inStream = new FileStream(RomFiles[i].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);
                 }
             }
         }
         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--)
         {
             Console.WriteLine("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)
             {
                 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
     {
         if (OutFileStream != null)
             OutFileStream.Dispose();
     }
     Console.WriteLine("RomFS Super Block Hash: " + ByteArrayToString(SuperBlockHash));
     Console.WriteLine("Writing Binary to " + this.FileName + "...");
     WriteBinary(TempFile, this.FileName);
 }
Ejemplo n.º 2
0
 public void Build()
 {
     FileNameTable fnt = new FileNameTable(this.RomFSDirectory);
     RomfsFile[] romFiles = new RomfsFile[fnt.NumFiles];
     LayoutManager.Input[] In = new LayoutManager.Input[fnt.NumFiles];
     Console.WriteLine("\nCreating layout...");
     for (int i = 0; i < fnt.NumFiles; i++)
     {
         In[i] = new LayoutManager.Input();
         In[i].FilePath = fnt.NameEntryTable[i].FullName;
         In[i].AlignmentSize = 0x10;
     }
     LayoutManager.Output[] Out = LayoutManager.Create(In);
     for (int i = 0; i < Out.Length; i++)
     {
         romFiles[i] = new RomfsFile();
         romFiles[i].Offset = Out[i].Offset;
         romFiles[i].PathName = Out[i].FilePath.Replace(Path.GetFullPath(this.RomFSDirectory), "").Replace("\\", "/");
         romFiles[i].FullName = Out[i].FilePath;
         romFiles[i].Size = Out[i].Size;
     }
     using (MemoryStream memoryStream = new MemoryStream())
     {
         Console.WriteLine("Creating RomFS MetaData...");
         MetaDataBuilder mdb = new MetaDataBuilder();
         mdb.BuildRomFSHeader(memoryStream, romFiles, this.RomFSDirectory);
         MakeRomFSData(romFiles, memoryStream);
     }
 }
Ejemplo n.º 3
0
 public static ulong GetDataBlockLength(RomfsFile[] files, ulong PreData)
 {
     return (files.Length == 0) ? PreData : PreData + files[files.Length - 1].Offset + files[files.Length - 1].Size;
 }
Ejemplo n.º 4
0
        private void PopulateRomfs(Romfs_MetaData MetaData, RomfsFile[] Entries)
        {
            //Recursively Add All Directories to DirectoryTable
            AddDir(MetaData, new DirectoryInfo(ROOT_DIR), 0, ROMFS_UNUSED_ENTRY);

            //Iteratively Add All Files to FileTable
            AddFiles(MetaData, Entries);

            //Set HashKeyPointers, Build HashTables
            PopulateHashTables(MetaData);

            //Thats it.
        }
Ejemplo n.º 5
0
 private void AddFiles(Romfs_MetaData MetaData, RomfsFile[] Entries)
 {
     string PrevDirPath = "";
     for (int i = 0; i < Entries.Length; i++)
     {
         FileInfo file = new FileInfo(Entries[i].FullName);
         Romfs_FileEntry Entry = new Romfs_FileEntry();
         string DirPath = Path.GetDirectoryName(Entries[i].FullName);
         int ParentIndex = GetRomfsDirEntry(MetaData, DirPath);
         Entry.FullName = Entries[i].FullName;
         Entry.Offset = MetaData.FileTableLen;
         Entry.ParentDirOffset = MetaData.DirTable.DirectoryTable[ParentIndex].Offset;
         Entry.SiblingOffset = ROMFS_UNUSED_ENTRY;
         if (DirPath == PrevDirPath)
         {
             MetaData.FileTable.FileTable[i - 1].SiblingOffset = Entry.Offset;
         }
         if (MetaData.DirTable.DirectoryTable[ParentIndex].FileOffset == ROMFS_UNUSED_ENTRY)
         {
             MetaData.DirTable.DirectoryTable[ParentIndex].FileOffset = Entry.Offset;
         }
         Entry.HashKeyPointer = ROMFS_UNUSED_ENTRY;
         Entry.NameSize = (uint)file.Name.Length * 2;
         Entry.Name = file.Name;
         Entry.DataOffset = Entries[i].Offset;
         Entry.DataSize = Entries[i].Size;
         MetaData.FileTable.FileTable.Add(Entry);
         MetaData.FileTableLen += 0x20 + (uint)Align((ulong)file.Name.Length * 2, 4);
         PrevDirPath = DirPath;
     }
 }
Ejemplo n.º 6
0
        public void BuildRomFSHeader(MemoryStream romfs_stream, RomfsFile[] Entries, string DIR)
        {
            ROOT_DIR = DIR;

            Romfs_MetaData MetaData = new Romfs_MetaData();

            InitializeMetaData(MetaData);

            CalcRomfsSize(MetaData);

            PopulateRomfs(MetaData, Entries);

            WriteMetaDataToStream(MetaData, romfs_stream);
        }