예제 #1
0
 protected virtual void Dispose(bool aDisposing)
 {
     if (aDisposing)
     {
         VirtualDisk?.Dispose();
         FileSystem?.Dispose();
     }
 }
예제 #2
0
 protected override void Dispose(bool disposing)
 {
     base.Dispose(disposing);
     if (disposing)
     {
         disk.Dispose();
         stream?.Close();
         stream?.Dispose();
     }
 }
예제 #3
0
 public static void ClassCleanup()
 {
     if (s_vdisk != null)
     {
         s_vdisk.Dispose();
     }
     if (File.Exists(s_vdiskpath))
     {
         File.Delete(s_vdiskpath);
     }
 }
예제 #4
0
        public void CloseContainer()
        {
            fsHandler = null; discFs.Dispose();
            fsInfo    = null; volume = null;
            disk.Dispose();

            File.Delete(originalPath);

            Crypto.EncryptFile(temporaryPath, originalPath, pwdKey, new AesEngine(), 128, 128);

            File.Delete(temporaryPath);
        }
예제 #5
0
        private void CreateDiffDisk()
        {
            string   child;
            PSObject parentObj = ResolveNewDiskPath(out child);

            PSObject baseDiskObj = SessionState.InvokeProvider.Item.Get(new string[] { BaseDisk }, false, true)[0];

            VirtualDisk baseDisk = null;

            try
            {
                if (baseDiskObj.BaseObject is FileInfo)
                {
                    baseDisk = VirtualDisk.OpenDisk(((FileInfo)baseDiskObj.BaseObject).FullName, FileAccess.Read);
                }
                else if (baseDiskObj.BaseObject is DiscFileInfo)
                {
                    DiscFileInfo dfi = (DiscFileInfo)baseDiskObj.BaseObject;
                    baseDisk = VirtualDisk.OpenDisk(dfi.FileSystem, dfi.FullName, FileAccess.Read);
                }
                else
                {
                    WriteError(new ErrorRecord(
                                   new FileNotFoundException("The file specified by the BaseDisk parameter doesn't exist"),
                                   "BadBaseDiskLocation",
                                   ErrorCategory.InvalidArgument,
                                   null));
                    return;
                }

                VirtualDisk newDisk = null;
                if (parentObj.BaseObject is DirectoryInfo)
                {
                    string path = Path.Combine(((DirectoryInfo)parentObj.BaseObject).FullName, child);
                    using (baseDisk.CreateDifferencingDisk(path)) { }
                    newDisk = new OnDemandVirtualDisk(path, FileAccess.ReadWrite);
                }
                else if (parentObj.BaseObject is DiscDirectoryInfo)
                {
                    DiscDirectoryInfo ddi  = (DiscDirectoryInfo)parentObj.BaseObject;
                    string            path = Path.Combine(ddi.FullName, child);
                    using (baseDisk.CreateDifferencingDisk(ddi.FileSystem, path)) { }
                    newDisk = new OnDemandVirtualDisk(ddi.FileSystem, path, FileAccess.ReadWrite);
                }
                else
                {
                    WriteError(new ErrorRecord(
                                   new DirectoryNotFoundException("Cannot create a virtual disk in that location"),
                                   "BadDiskLocation",
                                   ErrorCategory.InvalidArgument,
                                   null));
                    return;
                }

                WriteObject(newDisk, false);
            }
            finally
            {
                if (baseDisk != null)
                {
                    baseDisk.Dispose();
                }
            }
        }
예제 #6
0
        private static void GenerateFFU(string ImageFile, string FFUFile, string PlatformId, UInt32 chunkSize, string AntiTheftVersion, string Osversion, string[] excluded, UInt32 BlankSectorBufferSize)
        {
            Logging.Log("Input image: " + ImageFile);
            Logging.Log("Destination image: " + FFUFile);
            Logging.Log("Platform ID: " + PlatformId);
            Logging.Log("");

            Stream      stream;
            VirtualDisk destDisk = null;

            if (ImageFile.ToLower().Contains(@"\\.\physicaldrive"))
            {
                stream = new DeviceStream(ImageFile, FileAccess.Read);
            }
            else if (File.Exists(ImageFile) && Path.GetExtension(ImageFile).ToLowerInvariant() == ".vhd")
            {
                DiscUtils.Setup.SetupHelper.RegisterAssembly(typeof(DiscUtils.Vhd.Disk).Assembly);
                DiscUtils.Setup.SetupHelper.RegisterAssembly(typeof(DiscUtils.Vhdx.Disk).Assembly);
                destDisk = VirtualDisk.OpenDisk(ImageFile, FileAccess.Read);
                stream   = destDisk.Content;
            }
            else if (File.Exists(ImageFile) && Path.GetExtension(ImageFile).ToLowerInvariant() == ".vhdx")
            {
                DiscUtils.Setup.SetupHelper.RegisterAssembly(typeof(DiscUtils.Vhd.Disk).Assembly);
                DiscUtils.Setup.SetupHelper.RegisterAssembly(typeof(DiscUtils.Vhdx.Disk).Assembly);
                destDisk = VirtualDisk.OpenDisk(ImageFile, FileAccess.Read);
                stream   = destDisk.Content;
            }
            else if (File.Exists(ImageFile))
            {
                stream = new FileStream(ImageFile, FileMode.Open);
            }
            else
            {
                Logging.Log("Unknown input specified");
                return;
            }

            (FlashPart[] flashParts, ulong PlatEnd, List <GPT.Partition> partitions) = ImageSplitter.GetImageSlices(stream, chunkSize, excluded);

            IOrderedEnumerable <FlashingPayload> payloads = FlashingPayloadGenerator.GetOptimizedPayloads(flashParts, chunkSize, BlankSectorBufferSize).OrderBy(x => x.TargetLocations.First());

            Logging.Log("");
            Logging.Log("Building image headers...");

            string     header1       = Path.GetTempFileName();
            FileStream Headerstream1 = new FileStream(header1, FileMode.OpenOrCreate);

            // ==============================
            // Header 1 start

            ImageHeader image   = new ImageHeader();
            FullFlash   ffimage = new FullFlash();
            Store       simage  = new Store();

            // Todo make this read the image itself
            ffimage.OSVersion         = Osversion;
            ffimage.DevicePlatformId0 = PlatformId;
            ffimage.AntiTheftVersion  = AntiTheftVersion;

            simage.SectorSize     = 512;
            simage.MinSectorCount = (UInt32)(stream.Length / 512);

            Logging.Log("Generating image manifest...");
            string manifest = ManifestIni.BuildUpManifest(ffimage, simage, partitions);

            byte[] TextBytes = System.Text.Encoding.ASCII.GetBytes(manifest);

            image.ManifestLength = (UInt32)TextBytes.Length;

            byte[] ImageHeaderBuffer = new byte[0x18];

            ByteOperations.WriteUInt32(ImageHeaderBuffer, 0, image.Size);
            ByteOperations.WriteAsciiString(ImageHeaderBuffer, 0x04, image.Signature);
            ByteOperations.WriteUInt32(ImageHeaderBuffer, 0x10, image.ManifestLength);
            ByteOperations.WriteUInt32(ImageHeaderBuffer, 0x14, image.ChunkSize);

            Headerstream1.Write(ImageHeaderBuffer, 0, 0x18);
            Headerstream1.Write(TextBytes, 0, TextBytes.Length);

            RoundUpToChunks(Headerstream1, chunkSize);

            // Header 1 stop + round
            // ==============================

            string     header2       = Path.GetTempFileName();
            FileStream Headerstream2 = new FileStream(header2, FileMode.OpenOrCreate);

            // ==============================
            // Header 2 start

            StoreHeader store = new StoreHeader();

            store.WriteDescriptorCount = (UInt32)payloads.Count();
            store.FinalTableIndex      = (UInt32)payloads.Count() - store.FinalTableCount;
            store.PlatformId           = PlatformId;

            byte[] WriteDescriptorBuffer = GetResultingBuffer(payloads);
            store.WriteDescriptorLength = (UInt32)WriteDescriptorBuffer.Length;

            foreach (FlashingPayload payload in payloads)
            {
                if (payload.TargetLocations.First() > PlatEnd)
                {
                    break;
                }
                store.FlashOnlyTableIndex += 1;
            }

            Headerstream2.Write(store.GetResultingBuffer(), 0, 0xF8);
            Headerstream2.Write(WriteDescriptorBuffer, 0, (Int32)store.WriteDescriptorLength);

            RoundUpToChunks(Headerstream2, chunkSize);

            // Header 2 stop + round
            // ==============================

            SecurityHeader security = new SecurityHeader();

            Headerstream1.Seek(0, SeekOrigin.Begin);
            Headerstream2.Seek(0, SeekOrigin.Begin);

            security.HashTableSize = 0x20 * (UInt32)((Headerstream1.Length + Headerstream2.Length) / chunkSize);

            foreach (FlashingPayload payload in payloads)
            {
                security.HashTableSize += payload.GetSecurityHeaderSize();
            }

            byte[]       HashTable = new byte[security.HashTableSize];
            BinaryWriter bw        = new BinaryWriter(new MemoryStream(HashTable));

            SHA256 crypto = SHA256.Create();

            for (int i = 0; i < Headerstream1.Length / chunkSize; i++)
            {
                byte[] buffer = new byte[chunkSize];
                Headerstream1.Read(buffer, 0, (Int32)chunkSize);
                byte[] hash = crypto.ComputeHash(buffer);
                bw.Write(hash, 0, hash.Length);
            }

            for (int i = 0; i < Headerstream2.Length / chunkSize; i++)
            {
                byte[] buffer = new byte[chunkSize];
                Headerstream2.Read(buffer, 0, (Int32)chunkSize);
                byte[] hash = crypto.ComputeHash(buffer);
                bw.Write(hash, 0, hash.Length);
            }

            foreach (FlashingPayload payload in payloads)
            {
                foreach (var chunkHash in payload.ChunkHashes)
                {
                    bw.Write(chunkHash, 0, chunkHash.Length);
                }
            }

            bw.Close();

            Logging.Log("Generating image catalog...");
            byte[] catalog = GenerateCatalogFile(HashTable);

            security.CatalogSize = (UInt32)catalog.Length;

            byte[] SecurityHeaderBuffer = new byte[0x20];

            ByteOperations.WriteUInt32(SecurityHeaderBuffer, 0, security.Size);
            ByteOperations.WriteAsciiString(SecurityHeaderBuffer, 0x04, security.Signature);
            ByteOperations.WriteUInt32(SecurityHeaderBuffer, 0x10, security.ChunkSizeInKb);
            ByteOperations.WriteUInt32(SecurityHeaderBuffer, 0x14, security.HashAlgorithm);
            ByteOperations.WriteUInt32(SecurityHeaderBuffer, 0x18, security.CatalogSize);
            ByteOperations.WriteUInt32(SecurityHeaderBuffer, 0x1C, security.HashTableSize);

            FileStream retstream = new FileStream(FFUFile, FileMode.CreateNew);

            retstream.Write(SecurityHeaderBuffer, 0, 0x20);

            retstream.Write(catalog, 0, (Int32)security.CatalogSize);
            retstream.Write(HashTable, 0, (Int32)security.HashTableSize);

            RoundUpToChunks(retstream, chunkSize);

            Headerstream1.Seek(0, SeekOrigin.Begin);
            Headerstream2.Seek(0, SeekOrigin.Begin);

            byte[] buff = new byte[Headerstream1.Length];
            Headerstream1.Read(buff, 0, (Int32)Headerstream1.Length);

            Headerstream1.Close();
            File.Delete(header1);

            retstream.Write(buff, 0, buff.Length);

            buff = new byte[Headerstream2.Length];
            Headerstream2.Read(buff, 0, (Int32)Headerstream2.Length);

            Headerstream2.Close();
            File.Delete(header2);

            retstream.Write(buff, 0, buff.Length);

            Logging.Log("Writing payloads...");
            UInt64 counter = 0;

            DateTime startTime = DateTime.Now;

            foreach (FlashingPayload payload in payloads)
            {
                for (int i = 0; i < payload.StreamIndexes.Length; i++)
                {
                    UInt32    StreamIndex = payload.StreamIndexes[i];
                    FlashPart flashPart   = flashParts[StreamIndex];
                    Stream    Stream      = flashPart.Stream;
                    Stream.Seek(payload.StreamLocations[i], SeekOrigin.Begin);

                    byte[] buffer = new byte[chunkSize];
                    Stream.Read(buffer, 0, (Int32)chunkSize);
                    retstream.Write(buffer, 0, (Int32)chunkSize);
                    counter++;

                    ShowProgress((UInt64)payloads.Count() * chunkSize, startTime, counter * chunkSize, counter * chunkSize, payload.TargetLocations[i] * chunkSize < PlatEnd);
                }
            }

            retstream.Close();
            if (destDisk != null)
            {
                destDisk.Dispose();
            }
            Logging.Log("");
        }