示例#1
0
        /// <summary>
        /// Compares the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public bool Compare(byte[] data, uint offset, FatType type)
        {
            BinaryFormat entry = new BinaryFormat(data);

            byte first = entry.GetByte(Entry.DOSName + offset);

            if (first == FileNameAttribute.LastEntry)
            {
                return(false);
            }

            if ((first == FileNameAttribute.Deleted) | (first == FileNameAttribute.Dot))
            {
                return(false);
            }

            if (first == FileNameAttribute.Escape)
            {
                return(false);
            }

            FatFileAttributes attribute = (FatFileAttributes)entry.GetByte(Entry.FileAttributes + offset);

            if ((attribute & FatFileAttributes.VolumeLabel) == FatFileAttributes.VolumeLabel)
            {
                return(true);
            }

            return(false);
        }
示例#2
0
        private FatFileLocation FindEntry(Comparison compare, uint startCluster)
        {
            uint activeSector = ((startCluster - RootCluster) * SectorsPerCluster) + DataSector;

            if (startCluster == 0)
            {
                activeSector = (FatType == FatType.FAT32) ? GetSectorByCluster(RootCluster) : RootSector;
            }

            byte[] aData = new byte[BytePerSector * SectorsPerCluster];
            this.IDevice.Read(activeSector, SectorsPerCluster, aData);

            for (uint index = 0; index < EntriesPerSector * SectorsPerCluster; index++)
            {
                int offset = (int)(index * (int)Entry.EntrySize);
                if (compare.Compare(aData, offset, FatType))
                {
                    FatFileAttributes attribute = (FatFileAttributes)aData[offset + (int)Entry.FileAttributes];
                    return(new FatFileLocation(
                               GetClusterEntry(aData, index, FatType),
                               activeSector,
                               index,
                               (attribute & FatFileAttributes.SubDirectory) != 0,
                               BitConverter.ToInt32(aData, offset + (int)Entry.FileSize)));
                }

                if (aData[(int)Entry.DOSName + offset] == (int)FileNameAttribute.LastEntry)
                {
                    break;
                }
            }

            return(null);
        }
示例#3
0
        public FatFileLocation FindEntry(ACompare compare, uint startCluster)
        {
            uint activeSector = ((startCluster - RootCluster) * SectorsPerCluster) + DataSector;

            if (startCluster == 0)
            {
                activeSector = (FatType == FatType.FAT32) ? GetSectorByCluster(RootCluster) : RootSector;
            }

            byte[] aData = new byte[512 * SectorsPerCluster];
            this.IDevice.Read(activeSector, SectorsPerCluster, aData);

            BinaryFormat directory = new BinaryFormat(aData);

            for (uint index = 0; index < EntriesPerSector * SectorsPerCluster; index++)
            {
                Console.WriteLine("Lawl: %d\n" + ((uint)(index * 32)).ToString());
                if (compare.Compare(directory.Data, index * 32, FatType))
                {
                    FatFileAttributes attribute = (FatFileAttributes)directory.GetByte((index * Entry.EntrySize) + Entry.FileAttributes);
                    return(new FatFileLocation(GetClusterEntry(directory.Data, index, FatType), activeSector, index, (attribute & FatFileAttributes.SubDirectory) != 0, directory.GetUInt((index * Entry.EntrySize) + Entry.FileSize)));
                }

                if (directory.GetByte(Entry.DOSName + (index * Entry.EntrySize)) == FileNameAttribute.LastEntry)
                {
                    return(null);
                }
            }
            return(null);
        }
示例#4
0
        /// <summary>
        /// Creates the file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="fileAttributes">The file attributes.</param>
        /// <param name="directoryCluster">The directory cluster.</param>
        /// <returns></returns>
        public FatFileLocation CreateFile(string filename, FatFileAttributes fileAttributes, uint directoryCluster)
        {
            FatFileLocation location = FindEntry(new Find.WithName(filename), directoryCluster);

            if (location.Valid)
            {
                // Truncate the file
                BinaryFormat entry = new BinaryFormat(partition.ReadBlock(location.DirectorySector, 1));

                // Truncate the file length and reset the start cluster
                entry.SetUInt(Entry.FileSize + (location.DirectorySectorIndex * Entry.EntrySize), 0);
                entry.SetUInt(Entry.FirstCluster + (location.DirectorySectorIndex * Entry.EntrySize), 0);

                partition.WriteBlock(location.DirectorySector, 1, entry.Data);

                FreeClusterChain(location.FirstCluster);

                location.FirstCluster = 0;

                return location;
            }

            // Find an empty location in the directory
            location = FindEntry(new Find.Empty(), directoryCluster);

            if (!location.Valid)
            {
                // Extend Directory

                // TODO

                return location;
            }

            BinaryFormat directory = new BinaryFormat(partition.ReadBlock(location.DirectorySector, 1));

            if (filename.Length > 11)
                filename = filename.Substring(0, 11);

            // Create Entry
            directory.SetString(Entry.DOSName + (location.DirectorySectorIndex * Entry.EntrySize), "            ", 11);
            directory.SetString(Entry.DOSName + (location.DirectorySectorIndex * Entry.EntrySize), filename);
            directory.SetByte(Entry.FileAttributes + (location.DirectorySectorIndex * Entry.EntrySize), (byte)fileAttributes);
            directory.SetByte(Entry.Reserved + (location.DirectorySectorIndex * Entry.EntrySize), 0);
            directory.SetByte(Entry.CreationTimeFine + (location.DirectorySectorIndex * Entry.EntrySize), 0);
            directory.SetUShort(Entry.CreationTime + (location.DirectorySectorIndex * Entry.EntrySize), 0);
            directory.SetUShort(Entry.CreationDate + (location.DirectorySectorIndex * Entry.EntrySize), 0);
            directory.SetUShort(Entry.LastAccessDate + (location.DirectorySectorIndex * Entry.EntrySize), 0);
            directory.SetUShort(Entry.LastModifiedTime + (location.DirectorySectorIndex * Entry.EntrySize), 0);
            directory.SetUShort(Entry.LastModifiedDate + (location.DirectorySectorIndex * Entry.EntrySize), 0);
            directory.SetUShort(Entry.FirstCluster + (location.DirectorySectorIndex * Entry.EntrySize), 0);
            directory.SetUInt(Entry.FileSize + (location.DirectorySectorIndex * Entry.EntrySize), 0);

            partition.WriteBlock(location.DirectorySector, 1, directory.Data);

            return location;
        }
示例#5
0
        /// <summary>
        /// Creates the specified options.
        /// </summary>
        /// <param name="options">The options.</param>
        public static void Create(BootImageOptions options)
        {
            if (File.Exists(options.DiskImageFileName))
            {
                File.Delete(options.DiskImageFileName);
            }

            uint blockCount = options.BlockCount;

            if (blockCount == 0)
            {
                blockCount = 8400 + 1;
                foreach (var file in options.IncludeFiles)
                {
                    blockCount += ((uint)file.Content.Length / SectorSize) + 1;
                }
            }

            var diskGeometry = new DiskGeometry();
            diskGeometry.GuessGeometry(blockCount);

            // Create disk image file
            var diskDevice = new BlockFileStream(options.DiskImageFileName);

            if (options.ImageFormat == ImageFormat.VDI)
            {
                // Create header
                var header = VDI.CreateHeader(
                    blockCount,
                    options.MediaGuid.ToByteArray(),
                    options.MediaLastSnapGuid.ToByteArray(),
                    diskGeometry
                );

                diskDevice.WriteBlock(0, 1, header);

                var map = VDI.CreateImageMap(blockCount);

                diskDevice.WriteBlock(1, (uint)(map.Length / SectorSize), map);

                diskDevice.BlockOffset = 1 + (uint)(map.Length / 512);
            }

            // Expand disk image
            diskDevice.WriteBlock(blockCount - 1, 1, new byte[SectorSize]);

            // Create partition device
            PartitionDevice partitionDevice;

            if (options.MBROption)
            {
                // Create master boot block record
                var mbr = new MasterBootBlock(diskDevice);

                // Setup partition entry
                mbr.DiskSignature = 0x12345678;
                mbr.Partitions[0].Bootable = true;
                mbr.Partitions[0].StartLBA = diskGeometry.SectorsPerTrack;
                mbr.Partitions[0].TotalBlocks = blockCount - mbr.Partitions[0].StartLBA;

                switch (options.FileSystem)
                {
                    case FileSystem.FAT12: mbr.Partitions[0].PartitionType = PartitionType.FAT12; break;
                    case FileSystem.FAT16: mbr.Partitions[0].PartitionType = PartitionType.FAT16; break;
                    case FileSystem.FAT32: mbr.Partitions[0].PartitionType = PartitionType.FAT32; break;
                    default: break;
                }

                mbr.Code = options.MBRCode;

                mbr.Write();

                partitionDevice = new PartitionDevice(diskDevice, mbr.Partitions[0], false);
            }
            else
            {
                partitionDevice = new PartitionDevice(diskDevice, false);
            }

            // Set FAT settings
            var fatSettings = new FatSettings();

            switch (options.FileSystem)
            {
                case FileSystem.FAT12: fatSettings.FATType = FatType.FAT12; break;
                case FileSystem.FAT16: fatSettings.FATType = FatType.FAT16; break;
                case FileSystem.FAT32: fatSettings.FATType = FatType.FAT32; break;
                default: break;
            }

            fatSettings.FloppyMedia = false;
            fatSettings.VolumeLabel = options.VolumeLabel;
            fatSettings.SerialID = new byte[4] { 0x01, 0x02, 0x03, 0x04 };
            fatSettings.SectorsPerTrack = diskGeometry.SectorsPerTrack;
            fatSettings.NumberOfHeads = diskGeometry.Heads;
            fatSettings.HiddenSectors = diskGeometry.SectorsPerTrack;
            fatSettings.OSBootCode = options.FatBootCode;

            // Create FAT file system
            var fat = new FatFileSystem(partitionDevice);

            if (!fat.Format(fatSettings))
            {
                throw new Exception("ERROR: Invalid FAT settings");
            }

            fat.SetVolumeName(options.VolumeLabel);

            foreach (var includeFile in options.IncludeFiles)
            {
                var fileAttributes = new FatFileAttributes();
                if (includeFile.Archive) fileAttributes |= FatFileAttributes.Archive;
                if (includeFile.ReadOnly) fileAttributes |= FatFileAttributes.ReadOnly;
                if (includeFile.Hidden) fileAttributes |= FatFileAttributes.Hidden;
                if (includeFile.System) fileAttributes |= FatFileAttributes.System;

                string newname = (Path.GetFileNameWithoutExtension(includeFile.Filename).PadRight(8).Substring(0, 8) + Path.GetExtension(includeFile.Filename).PadRight(4).Substring(1, 3)).ToUpper();
                var location = fat.CreateFile(newname, fileAttributes);

                if (!location.IsValid)
                    throw new Exception("Unable to write file");

                var fatFileStream = new FatFileStream(fat, location);
                fatFileStream.Write(includeFile.Content, 0, includeFile.Content.Length);
                fatFileStream.Flush();
            }

            if (options.PatchSyslinuxOption)
            {
                if (options.BootLoader == BootLoader.Syslinux_6_03)
                {
                    Syslinux.PatchSyslinux_6_03(partitionDevice, fat);
                }
                else if (options.BootLoader == BootLoader.Syslinux_3_72)
                {
                    Syslinux.PatchSyslinux_3_72(partitionDevice, fat);
                }
            }

            if (options.ImageFormat == ImageFormat.VHD)
            {
                // Create footer
                var footer = VHD.CreateFooter(
                    blockCount,
                    (uint)(DateTime.Now - (new DateTime(2000, 1, 1, 0, 0, 0))).Seconds,
                    options.MediaGuid.ToByteArray(),
                    diskGeometry
                );

                diskDevice.WriteBlock(blockCount, 1, footer);
            }

            diskDevice.Dispose();
        }
示例#6
0
        /// <summary>
        /// Creates the specified options.
        /// </summary>
        /// <param name="options">The options.</param>
        static public void Create(BootImageOptions options)
        {
            if (File.Exists(options.DiskImageFileName))
            {
                File.Delete(options.DiskImageFileName);
            }

            uint blockCount = options.BlockCount;

            if (blockCount == 0)
            {
                blockCount = 8400 + 1;
                foreach (var file in options.IncludeFiles)
                {
                    blockCount += ((uint)file.Content.Length / SectorSize) + 1;
                }
            }

            var diskGeometry = new DiskGeometry();

            diskGeometry.GuessGeometry(blockCount);

            // Create disk image file
            var diskDeviceDriver = new BlockFileStreamDriver(options.DiskImageFileName);

            var diskDevice = new Device()
            {
                DeviceDriver = diskDeviceDriver
            };

            // Setup device -- required as part of framework in operating system
            diskDeviceDriver.Setup(diskDevice);
            diskDeviceDriver.Initialize();
            diskDeviceDriver.Start();

            if (options.ImageFormat == ImageFormat.VDI)
            {
                // Create header
                var header = VDI.CreateHeader(
                    blockCount,
                    options.MediaGuid.ToByteArray(),
                    options.MediaLastSnapGuid.ToByteArray(),
                    diskGeometry
                    );

                diskDeviceDriver.WriteBlock(0, 1, header);

                var map = VDI.CreateImageMap(blockCount);

                diskDeviceDriver.WriteBlock(1, (uint)(map.Length / SectorSize), map);

                diskDeviceDriver.BlockOffset = 1 + (uint)(map.Length / 512);
            }

            // Expand disk image
            diskDeviceDriver.WriteBlock(blockCount - 1, 1, new byte[SectorSize]);

            // Create partition device driver
            var partitionDevice = new PartitionDeviceDriver();

            // Setup partition configuration
            var configuraiton = new DiskPartitionConfiguration()
            {
                Index    = 0,
                ReadOnly = false,
            };

            if (options.MBROption)
            {
                // Create master boot block record
                var mbr = new MasterBootBlock(diskDeviceDriver)
                {
                    // Setup partition entry
                    DiskSignature = 0x12345678
                };

                mbr.Partitions[0].Bootable    = true;
                mbr.Partitions[0].StartLBA    = diskGeometry.SectorsPerTrack;
                mbr.Partitions[0].TotalBlocks = blockCount - mbr.Partitions[0].StartLBA;

                switch (options.FileSystem)
                {
                case FileSystem.FAT12: mbr.Partitions[0].PartitionType = PartitionType.FAT12; break;

                case FileSystem.FAT16: mbr.Partitions[0].PartitionType = PartitionType.FAT16; break;

                case FileSystem.FAT32: mbr.Partitions[0].PartitionType = PartitionType.FAT32; break;

                default: break;
                }

                mbr.Code = options.MBRCode;

                mbr.Write();

                configuraiton.StartLBA    = mbr.Partitions[0].StartLBA;
                configuraiton.TotalBlocks = mbr.Partitions[0].TotalBlocks;
            }
            else
            {
                configuraiton.StartLBA    = 0;
                configuraiton.TotalBlocks = diskDeviceDriver.TotalBlocks;
            }

            // Setup device -- required as part of framework in operating system
            var device = new Device()
            {
                Configuration = configuraiton,
                DeviceDriver  = partitionDevice,
                Parent        = diskDevice,
            };

            // Setup and initialize
            partitionDevice.Setup(device);
            partitionDevice.Initialize();
            partitionDevice.Start();

            // Set FAT settings
            var fatSettings = new FatSettings();

            switch (options.FileSystem)
            {
            case FileSystem.FAT12: fatSettings.FATType = FatType.FAT12; break;

            case FileSystem.FAT16: fatSettings.FATType = FatType.FAT16; break;

            case FileSystem.FAT32: fatSettings.FATType = FatType.FAT32; break;

            default: break;
            }

            fatSettings.FloppyMedia = false;
            fatSettings.VolumeLabel = options.VolumeLabel;
            fatSettings.SerialID    = new byte[4] {
                0x01, 0x02, 0x03, 0x04
            };
            fatSettings.SectorsPerTrack = diskGeometry.SectorsPerTrack;
            fatSettings.NumberOfHeads   = diskGeometry.Heads;
            fatSettings.HiddenSectors   = diskGeometry.SectorsPerTrack;
            fatSettings.OSBootCode      = options.FatBootCode;

            // Create FAT file system
            var fat = new FatFileSystem(partitionDevice);

            if (!fat.Format(fatSettings))
            {
                throw new Exception("ERROR: Invalid FAT settings");
            }

            fat.SetVolumeName(options.VolumeLabel);

            foreach (var includeFile in options.IncludeFiles)
            {
                var fileAttributes = new FatFileAttributes();
                if (includeFile.Archive)
                {
                    fileAttributes |= FatFileAttributes.Archive;
                }
                if (includeFile.ReadOnly)
                {
                    fileAttributes |= FatFileAttributes.ReadOnly;
                }
                if (includeFile.Hidden)
                {
                    fileAttributes |= FatFileAttributes.Hidden;
                }
                if (includeFile.System)
                {
                    fileAttributes |= FatFileAttributes.System;
                }

                string newname  = (Path.GetFileNameWithoutExtension(includeFile.Filename).PadRight(8).Substring(0, 8) + Path.GetExtension(includeFile.Filename).PadRight(4).Substring(1, 3)).ToUpper();
                var    location = fat.CreateFile(newname, fileAttributes);

                if (!location.IsValid)
                {
                    throw new Exception("Unable to write file");
                }

                var fatFileStream = new FatFileStream(fat, location);
                fatFileStream.Write(includeFile.Content, 0, includeFile.Content.Length);
                fatFileStream.Flush();
            }

            if (options.PatchSyslinuxOption)
            {
                if (options.BootLoader == BootLoader.Syslinux_6_03)
                {
                    Syslinux.PatchSyslinux_6_03(partitionDevice, fat);
                }
                else if (options.BootLoader == BootLoader.Syslinux_3_72)
                {
                    Syslinux.PatchSyslinux_3_72(partitionDevice, fat);
                }
            }

            if (options.ImageFormat == ImageFormat.VHD)
            {
                // Create footer
                var footer = VHD.CreateFooter(
                    blockCount,
                    (uint)(DateTime.Now - (new DateTime(2000, 1, 1, 0, 0, 0))).Seconds,
                    options.MediaGuid.ToByteArray(),
                    diskGeometry
                    );

                diskDeviceDriver.WriteBlock(blockCount, 1, footer);
            }

            diskDeviceDriver.Dispose();
        }
示例#7
0
 /// <summary>
 /// Creates the file.
 /// </summary>
 /// <param name="filename">The filename.</param>
 /// <param name="fileAttributes">The file attributes.</param>
 /// <returns></returns>
 public FatFileLocation CreateFile(string filename, FatFileAttributes fileAttributes)
 {
     return CreateFile(filename, fileAttributes, 0);
 }
示例#8
0
        private static void Main(string[] args)
        {
            // Create synthetic ram disk device
            var ramDiskDevice = new RamDiskDevice(1024 * 1024 * 10 / 512);

            var ramDevice = new Device()
            {
                DeviceDriver = ramDiskDevice
            };

            // Setup device -- required as part of framework in operating system
            ramDiskDevice.Setup(ramDevice);
            ramDiskDevice.Initialize();
            ramDiskDevice.Start();

            // Create master boot block record
            var mbr = new MasterBootBlock(ramDiskDevice)
            {
                DiskSignature = 0x12345678
            };

            mbr.Partitions[0].Bootable      = true;
            mbr.Partitions[0].StartLBA      = 17;
            mbr.Partitions[0].TotalBlocks   = ramDiskDevice.TotalBlocks - 17;
            mbr.Partitions[0].PartitionType = PartitionType.FAT12;
            mbr.Write();

            // Create partition device driver
            var partitionDevice = new PartitionDeviceDriver();

            // Setup partition configuration
            var configuraiton = new DiskPartitionConfiguration()
            {
                StartLBA    = mbr.Partitions[0].StartLBA,
                TotalBlocks = mbr.Partitions[0].TotalBlocks,
                Index       = 0,
                ReadOnly    = false,
            };

            // Setup device -- required as part of framework in operating system
            var device = new Device()
            {
                Configuration = configuraiton,
                DeviceDriver  = partitionDevice,
            };

            // Setup and initialize
            partitionDevice.Setup(device);
            partitionDevice.Initialize();
            partitionDevice.Start();

            // Set FAT settings
            var fatSettings = new FatSettings();

            fatSettings.FATType     = FatType.FAT12;
            fatSettings.FloppyMedia = false;
            fatSettings.VolumeLabel = "MOSADISK";
            fatSettings.SerialID    = new byte[4] {
                0x01, 0x02, 0x03, 0x04
            };

            // Create FAT file system
            var fat = new FatFileSystem(partitionDevice);

            fat.Format(fatSettings);

            if (fat.IsValid)
            {
                switch (fat.FATType)
                {
                case FatType.FAT12: Console.WriteLine("FAT12"); break;

                case FatType.FAT16: Console.WriteLine("FAT16"); break;

                case FatType.FAT32: Console.WriteLine("FAT32"); break;

                default: Console.WriteLine("Unknown"); break;
                }
                Console.WriteLine("  Volume Name: " + fat.VolumeLabel);
            }
            else
            {
                Console.WriteLine("Unknown File System");
            }

            var files = new List <IncludeFile>();

            files.Add(new IncludeFile("CREDITS.TXT", GetResource("Credits.txt")));
            files.Add(new IncludeFile("LICENSE.TXT", GetResource("LICENSE.txt")));

            foreach (var includeFile in files)
            {
                Console.WriteLine("Writing File: " + includeFile.Filename);

                var fileAttributes = new FatFileAttributes();
                if (includeFile.Archive)
                {
                    fileAttributes |= FatFileAttributes.Archive;
                }
                if (includeFile.ReadOnly)
                {
                    fileAttributes |= FatFileAttributes.ReadOnly;
                }
                if (includeFile.Hidden)
                {
                    fileAttributes |= FatFileAttributes.Hidden;
                }
                if (includeFile.System)
                {
                    fileAttributes |= FatFileAttributes.System;
                }

                string newname  = (Path.GetFileNameWithoutExtension(includeFile.Filename).PadRight(8).Substring(0, 8) + Path.GetExtension(includeFile.Filename).PadRight(4).Substring(1, 3)).ToUpperInvariant();
                var    location = fat.CreateFile(newname, fileAttributes);

                if (!location.IsValid)
                {
                    throw new Exception("Unable to write file");
                }

                var fatFileStream = new FatFileStream(fat, location);
                fatFileStream.Write(includeFile.Content, 0, includeFile.Content.Length);
                fatFileStream.Flush();

                Console.WriteLine("  Source Length: " + includeFile.Content.Length.ToString());
                Console.WriteLine("  Stream Length: " + fatFileStream.Length.ToString());
            }

            foreach (var includeFile in files)
            {
                Console.WriteLine("Searching File: " + includeFile.Filename);

                var location = fat.FindEntry(includeFile.Filename);

                if (location.IsValid)
                {
                    Console.WriteLine("  Found: " + includeFile.Filename);

                    var fatFileStream = new FatFileStream(fat, location);
                    Console.WriteLine("  Length: " + fatFileStream.Length.ToString());

                    for (; ;)
                    {
                        int i = fatFileStream.ReadByte();

                        if (i < 0)
                        {
                            break;
                        }

                        Console.Write((char)i);
                    }
                    Console.WriteLine();
                    fatFileStream.Position = 0;

                    var buffer = new byte[fatFileStream.Length];
                    fatFileStream.Read(buffer, 0, (int)fatFileStream.Length);

                    Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer));
                }
                else
                {
                    Console.WriteLine("  Not Found: " + includeFile.Filename);
                }
            }

            return;
        }
示例#9
0
        static void SetRootEntry(int entryNumber, int startCluster, FileInfo origFile, FatFileAttributes attrs)
        {
            image.Position = diskDesc.RootDirOffset + 32 * entryNumber;

            // write 8-byte name
            string name = Path.GetFileNameWithoutExtension(origFile.Name);

            var bytes = new byte[8];

            bytes.Fill((byte)' ');
            Encoding.ASCII.GetBytes(name, 0, Math.Min(8, name.Length), bytes, 0);
            image.Write(bytes, 0, 8);

            // write 3-byte extension
            string ext = Path.GetExtension(origFile.Name);

            if (string.IsNullOrEmpty(ext))
            {
                ext = string.Empty;
            }
            else if (ext.Length > 0)
            {
                ext = ext.Substring(1, 3);
            }

            bytes.Fill((byte)' ');
            Encoding.ASCII.GetBytes(ext, 0, Math.Min(3, ext.Length), bytes, 0);
            image.Write(bytes, 0, 3);

            // attributes
            image.WriteByte((byte)attrs);

            // WinNT reserved
            image.WriteByte(0);

            // creation time (10ths of a second):
            image.WriteByte((byte)(((origFile.CreationTime.Second & 1) == 0) ? 10 : 0));

            // creation time rest:
            bytes.Fill((byte)0);
            FillDateTime(bytes, origFile.CreationTime);
            image.Write(bytes, 0, 4);

            // skip access date and high word of start cluster number
            image.Position += 4;

            // modification time:
            FillDateTime(bytes, origFile.LastWriteTime);
            image.Write(bytes, 0, 4);

            // low word of first cluster number:
            bytes = BitConverter.GetBytes((ushort)(startCluster & 0xFFFF));
            image.Write(bytes, 0, sizeof(ushort));

            // file size:
            bytes = BitConverter.GetBytes((uint)origFile.Length);
            image.Write(bytes, 0, sizeof(uint));
        }
示例#10
0
        private static void Main(string[] args)
        {
            // Create synthetic ram disk device
            var ramDiskDevice = new RamDiskDevice(1024 * 1024 * 10 / 512);

            // Create master boot block record
            var mbr = new MasterBootBlock(ramDiskDevice);
            mbr.DiskSignature = 0x12345678;
            mbr.Partitions[0].Bootable = true;
            mbr.Partitions[0].StartLBA = 17;
            mbr.Partitions[0].TotalBlocks = ramDiskDevice.TotalBlocks - 17;
            mbr.Partitions[0].PartitionType = PartitionType.FAT12;
            mbr.Write();

            // Create partition device
            var partitionDevice = new PartitionDevice(ramDiskDevice, mbr.Partitions[0], false);

            // Set FAT settings
            var fatSettings = new FatSettings();

            fatSettings.FATType = FatType.FAT12;
            fatSettings.FloppyMedia = false;
            fatSettings.VolumeLabel = "MOSADISK";
            fatSettings.SerialID = new byte[4] { 0x01, 0x02, 0x03, 0x04 };

            // Create FAT file system
            var fat = new FatFileSystem(partitionDevice);
            fat.Format(fatSettings);

            if (fat.IsValid)
            {
                switch (fat.FATType)
                {
                    case FatType.FAT12: Console.WriteLine("FAT12"); break;
                    case FatType.FAT16: Console.WriteLine("FAT16"); break;
                    case FatType.FAT32: Console.WriteLine("FAT32"); break;
                    default: Console.WriteLine("Unknown"); break;
                }
                Console.WriteLine("  Volume Name: " + fat.VolumeLabel);
            }
            else
            {
                Console.WriteLine("Unknown File System");
            }

            var files = new List<IncludeFile>();

            files.Add(new IncludeFile("CREDITS.TXT", GetResource("CREDITS.txt")));
            files.Add(new IncludeFile("LICENSE.TXT", GetResource("LICENSE.txt")));

            foreach (var includeFile in files)
            {
                Console.WriteLine("Writing File: " + includeFile.Filename);

                var fileAttributes = new FatFileAttributes();
                if (includeFile.Archive) fileAttributes |= FatFileAttributes.Archive;
                if (includeFile.ReadOnly) fileAttributes |= FatFileAttributes.ReadOnly;
                if (includeFile.Hidden) fileAttributes |= FatFileAttributes.Hidden;
                if (includeFile.System) fileAttributes |= FatFileAttributes.System;

                string newname = (Path.GetFileNameWithoutExtension(includeFile.Filename).PadRight(8).Substring(0, 8) + Path.GetExtension(includeFile.Filename).PadRight(4).Substring(1, 3)).ToUpper();
                var location = fat.CreateFile(newname, fileAttributes);

                if (!location.IsValid)
                    throw new Exception("Unable to write file");

                var fatFileStream = new FatFileStream(fat, location);
                fatFileStream.Write(includeFile.Content, 0, includeFile.Content.Length);
                fatFileStream.Flush();

                Console.WriteLine("  Source Length: " + includeFile.Content.Length.ToString());
                Console.WriteLine("  Stream Length: " + fatFileStream.Length.ToString());
            }

            foreach (var includeFile in files)
            {
                Console.WriteLine("Searching File: " + includeFile.Filename);

                var location = fat.FindEntry(includeFile.Filename);

                if (location.IsValid)
                {
                    Console.WriteLine("  Found: " + includeFile.Filename);

                    var fatFileStream = new FatFileStream(fat, location);
                    Console.WriteLine("  Length: " + fatFileStream.Length.ToString());

                    for (;;)
                    {
                        int i = fatFileStream.ReadByte();

                        if (i < 0)
                            break;

                        Console.Write((char)i);
                    }
                    Console.WriteLine();
                    fatFileStream.Position = 0;

                    var buffer = new byte[fatFileStream.Length];
                    fatFileStream.Read(buffer, 0, (int)fatFileStream.Length);

                    Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer));
                }
                else
                {
                    Console.WriteLine("  Not Found: " + includeFile.Filename);
                }
            }

            return;
        }