Exemple #1
0
        private static long FloppyCapacity(FloppyDiskType type)
        {
            switch (type)
            {
            case FloppyDiskType.DoubleDensity:
                return(Sizes.Sector * 1440);

            case FloppyDiskType.HighDensity:
                return(Sizes.Sector * 2880);

            case FloppyDiskType.Extended:
                return(Sizes.Sector * 5760);

            default:
                throw new ArgumentException("Invalid floppy disk type", nameof(type));
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates a formatted floppy disk image in a stream.
        /// </summary>
        /// <param name="stream">The stream to write the blank image to</param>
        /// <param name="type">The type of floppy to create</param>
        /// <param name="label">The volume label for the floppy (or null)</param>
        /// <returns>An object that provides access to the newly created floppy disk image</returns>
        public static FatFileSystem FormatFloppy(Stream stream, FloppyDiskType type, string label)
        {
            long pos = stream.Position;

            long ticks = DateTime.UtcNow.Ticks;
            uint volId = (uint)((ticks & 0xFFFF) | (ticks >> 32));

            // Write the BIOS Parameter Block (BPB) - a single sector
            byte[] bpb = new byte[512];
            uint sectors;
            if (type == FloppyDiskType.DoubleDensity)
            {
                sectors = 1440;
                WriteBPB(bpb, sectors, FatType.Fat12, 224, 0, 1, 1, new Geometry(80, 2, 9), true, volId, label);
            }
            else if (type == FloppyDiskType.HighDensity)
            {
                sectors = 2880;
                WriteBPB(bpb, sectors, FatType.Fat12, 224, 0, 1, 1, new Geometry(80, 2, 18), true, volId, label);
            }
            else if (type == FloppyDiskType.Extended)
            {
                sectors = 5760;
                WriteBPB(bpb, sectors, FatType.Fat12, 224, 0, 1, 1, new Geometry(80, 2, 36), true, volId, label);
            }
            else
            {
                throw new ArgumentException("Unrecognised Floppy Disk type", "type");
            }

            stream.Write(bpb, 0, bpb.Length);

            // Write both FAT copies
            uint fatSize = CalcFatSize(sectors, FatType.Fat12, 1);
            byte[] fat = new byte[fatSize * Utilities.SectorSize];
            FatBuffer fatBuffer = new FatBuffer(FatType.Fat12, fat);
            fatBuffer.SetNext(0, 0xFFFFFFF0);
            fatBuffer.SetEndOfChain(1);
            stream.Write(fat, 0, fat.Length);
            stream.Write(fat, 0, fat.Length);

            // Write the (empty) root directory
            uint rootDirSectors = ((224 * 32) + Utilities.SectorSize - 1) / Utilities.SectorSize;
            byte[] rootDir = new byte[rootDirSectors * Utilities.SectorSize];
            stream.Write(rootDir, 0, rootDir.Length);

            // Write a single byte at the end of the disk to ensure the stream is at least as big
            // as needed for this disk image.
            stream.Position = pos + (sectors * Utilities.SectorSize) - 1;
            stream.WriteByte(0);

            // Give the caller access to the new file system
            stream.Position = pos;
            return new FatFileSystem(stream);
        }
Exemple #3
0
 private static long FloppyCapacity(FloppyDiskType type)
 {
     switch (type)
     {
         case FloppyDiskType.DoubleDensity:
             return Sizes.Sector * 1440;
         case FloppyDiskType.HighDensity:
             return Sizes.Sector * 2880;
         case FloppyDiskType.Extended:
             return Sizes.Sector * 5760;
         default:
             throw new ArgumentException("Invalid floppy disk type", "type");
     }
 }
Exemple #4
0
 /// <summary>
 /// Initializes a stream as an unformatted floppy disk.
 /// </summary>
 /// <param name="stream">The stream to initialize.</param>
 /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
 /// <param name="type">The type of floppy disk image to create</param>
 /// <returns>An object that accesses the stream as a disk</returns>
 public static DiskImageFile Initialize(Stream stream, Ownership ownsStream, FloppyDiskType type)
 {
     return Initialize(stream, ownsStream, FloppyCapacity(type), null);
 }
Exemple #5
0
 /// <summary>
 /// Initializes a stream as an unformatted floppy disk.
 /// </summary>
 /// <param name="stream">The stream to initialize.</param>
 /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
 /// <param name="type">The type of floppy disk image to create</param>
 /// <returns>An object that accesses the stream as a disk</returns>
 public static Disk Initialize(Stream stream, Ownership ownsStream, FloppyDiskType type)
 {
     return(new Disk(DiskImageFile.Initialize(stream, ownsStream, type)));
 }
Exemple #6
0
 /// <summary>
 /// Initializes a stream as an unformatted floppy disk.
 /// </summary>
 /// <param name="stream">The stream to initialize.</param>
 /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
 /// <param name="type">The type of floppy disk image to create.</param>
 /// <returns>An object that accesses the stream as a disk.</returns>
 public static Disk Initialize(Stream stream, Ownership ownsStream, FloppyDiskType type)
 {
     return new Disk(DiskImageFile.Initialize(stream, ownsStream, type));
 }
Exemple #7
0
 /// <summary>
 /// Initializes a stream as an unformatted floppy disk.
 /// </summary>
 /// <param name="stream">The stream to initialize.</param>
 /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
 /// <param name="type">The type of floppy disk image to create.</param>
 /// <returns>An object that accesses the stream as a disk.</returns>
 public static DiskImageFile Initialize(Stream stream, Ownership ownsStream, FloppyDiskType type)
 {
     return(Initialize(stream, ownsStream, FloppyCapacity(type), null));
 }