Exemplo n.º 1
0
        public static void PatchSyslinux_6_03(PartitionDeviceDriver partitionDevice, FatFileSystem fat)
        {
            // Locate ldlinux.sys file for patching
            string filename = "ldlinux.sys";
            string name     = (Path.GetFileNameWithoutExtension(filename) + Path.GetExtension(filename).PadRight(4).Substring(0, 4)).ToUpper();

            var location = fat.FindEntry(name);

            if (!location.IsValid)
            {
                throw new InvalidProgramException("Unable to find syslinux.sys");
            }

            // Get the file size & number of sectors used
            uint fileSize = fat.GetFileSize(location.DirectorySector, location.DirectorySectorIndex);

            var sectors = new List <uint>();

            // Create list of the sectors of the file
            for (uint cluster = location.FirstCluster; (cluster != 0); cluster = fat.GetNextCluster(cluster))
            {
                uint sec = fat.GetSectorByCluster(cluster);
                for (uint i = 0; i < fat.SectorsPerCluster; i++)
                {
                    sectors.Add(sec + i);
                }
            }

            // Get the ldlinux.sys file stream
            var ldlinux = new FatFileStream(fat, location);

            var ldlinuxReader = new EndianAwareBinaryReader(ldlinux, Endianness.Little);

            // Search for 0x3EB202FE (magic)
            while ((ldlinuxReader.ReadUInt32() != Syslinux.LDLINUX_MAGIC) && (ldlinux.Position < ldlinux.Length))
            {
                ;
            }

            if (ldlinux.Position >= ldlinux.Length || ldlinux.Position <= 0)
            {
                throw new InvalidProgramException("Unable to find patch location for syslinux");
            }

            uint patchArea = (uint)ldlinux.Position - 4;

            // Get Extended Patch Area offset
            ldlinux.Position = patchArea + Syslinux.PatchAreaOffset.EPAOffset;
            ushort epa = ldlinuxReader.ReadUInt16();

            ldlinux.Position = epa + Syslinux.ExtendedPatchAreaOffset.Sect1Ptr0;
            uint sect1Ptr0 = ldlinuxReader.ReadUInt16();

            ldlinux.Position = epa + Syslinux.ExtendedPatchAreaOffset.Sect1Ptr1;
            uint sect1Ptr1 = ldlinuxReader.ReadUInt16();

            ldlinux.Position = epa + Syslinux.ExtendedPatchAreaOffset.SecPtrOffset;
            uint ex = ldlinuxReader.ReadUInt16();

            ldlinux.Position = epa + Syslinux.ExtendedPatchAreaOffset.SecPtrCnt;
            uint nptrs = ldlinuxReader.ReadUInt16();

            ldlinux.Position = epa + Syslinux.ExtendedPatchAreaOffset.AdvPtrOffset;
            uint advptrs = ldlinuxReader.ReadUInt16();

            if (sectors.Count > nptrs)
            {
                throw new InvalidProgramException("Insufficient space for patching syslinux");
            }

            var ldlinuxWriter = new EndianAwareBinaryWriter(ldlinux, Endianness.Little);

            // Set up the totals
            ldlinux.Position = patchArea + Syslinux.PatchAreaOffset.DataSectors;
            ldlinuxWriter.Write((ushort)sectors.Count);

            ldlinux.Position = patchArea + Syslinux.PatchAreaOffset.DataSectors;
            ldlinuxWriter.Write((ushort)2);

            ldlinux.Position = patchArea + Syslinux.PatchAreaOffset.DataSectors;
            ldlinuxWriter.Write(fileSize >> 2);

            // Generate Extents
            var extents = GenerateExtents(sectors);

            ldlinux.Position = ex;

            // Write out extents
            foreach (var extent in extents)
            {
                ldlinuxWriter.Write(extent.Start);
                ldlinuxWriter.Write(extent.Length);
            }

            // Write out ADV
            ldlinux.Position = advptrs;
            ldlinuxWriter.Write((ulong)sectors[sectors.Count - 2]);
            ldlinuxWriter.Write((ulong)sectors[sectors.Count - 1]);

            // Clear out checksum
            ldlinux.Position = patchArea + Syslinux.PatchAreaOffset.Checksum;
            ldlinuxWriter.Write((uint)0);

            // Write back the updated cluster
            ldlinuxWriter.Flush();

            // Re-Calculate checksum
            ldlinux.Position = 0;
            uint csum = Syslinux.LDLINUX_MAGIC;

            for (uint index = 0; index < (ldlinux.Length >> 2); index++)
            {
                csum = csum + ldlinuxReader.ReadUInt32();
            }

            // Set the checksum
            ldlinux.Position = patchArea + Syslinux.PatchAreaOffset.Checksum;
            ldlinuxWriter.Write(csum);

            // Write patched cluster back to disk
            ldlinuxWriter.Flush();

            // Read boot sector
            var fatBootSector = new DataBlock(partitionDevice.ReadBlock(0, 1));

            // Set the first sector location of the file
            fatBootSector.SetUInt(sect1Ptr0, fat.GetSectorByCluster(location.FirstCluster));
            fatBootSector.SetUInt(sect1Ptr1, 0);               // since only 32-bit offsets are support, the high portion of 64-bit value is zero

            // Write back patched boot sector
            partitionDevice.WriteBlock(0, 1, fatBootSector.Data);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Main
        /// </summary>
        public static void Main()
        {
            Kernel.x86.Kernel.Setup();

            Console = ConsoleManager.Controller.Boot;

            Console.Clear();
            Console.Goto(0, 0);
            Console.ScrollRow       = 23;
            Console.Color           = ScreenColor.White;
            Console.BackgroundColor = ScreenColor.Green;

            Debug = ConsoleManager.Controller.Debug;

            Console.Write("                   MOSA OS Version 2.2 - Compiler Version 2.2");
            FillLine();
            Console.Color           = ScreenColor.White;
            Console.BackgroundColor = ScreenColor.Black;

            Console.WriteLine("> Initializing services...");

            // Create Service manager and basic services
            var serviceManager = new ServiceManager();

            DeviceService = new DeviceService();

            var diskDeviceService    = new DiskDeviceService();
            var partitionService     = new PartitionService();
            var pciControllerService = new PCIControllerService();
            var pciDeviceService     = new PCIDeviceService();
            var pcService            = new PCService();

            serviceManager.AddService(DeviceService);
            serviceManager.AddService(diskDeviceService);
            serviceManager.AddService(partitionService);
            serviceManager.AddService(pciControllerService);
            serviceManager.AddService(pciDeviceService);
            serviceManager.AddService(pcService);

            Console.WriteLine("> Initializing hardware abstraction layer...");

            // Set device driver system with the hardware HAL
            var hardware = new HAL.Hardware();

            DeviceSystem.Setup.Initialize(hardware, DeviceService.ProcessInterrupt);

            Console.WriteLine("> Registering device drivers...");
            DeviceService.RegisterDeviceDriver(DeviceDriver.Setup.GetDeviceDriverRegistryEntries());

            Console.WriteLine("> Starting devices...");

            DeviceService.Initialize(new X86System(), null);

            var acpi = DeviceService.GetFirstDevice <IACPI>().DeviceDriver as IACPI;

            // Setup APIC
            var localApic = Mosa.DeviceSystem.HAL.GetPhysicalMemory((Pointer)acpi.LocalApicAddress, 0xFFFF).Address;
            var ioApic    = Mosa.DeviceSystem.HAL.GetPhysicalMemory((Pointer)acpi.IOApicAddress, 0xFFFF).Address;

            APIC.Setup(localApic, ioApic);

            Console.Write("> Probing for ISA devices...");
            var isaDevices = DeviceService.GetChildrenOf(DeviceService.GetFirstDevice <ISABus>());

            Console.WriteLine("[Completed: " + isaDevices.Count.ToString() + " found]");

            foreach (var device in isaDevices)
            {
                Console.Write("  ");
                Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                InBrackets(device.Name, ScreenColor.White, ScreenColor.Green);
                Console.WriteLine();
            }

            Console.Write("> Probing for PCI devices...");
            var devices = DeviceService.GetDevices <PCIDevice>();

            Console.WriteLine("[Completed: " + devices.Count.ToString() + " found]");

            foreach (var device in devices)
            {
                Console.Write("  ");
                Bullet(ScreenColor.Yellow);
                Console.Write(" ");

                var pciDevice = device.DeviceDriver as PCIDevice;
                InBrackets(device.Name + ": " + pciDevice.VendorID.ToString("x") + ":" + pciDevice.DeviceID.ToString("x") + " " + pciDevice.SubSystemID.ToString("x") + ":" + pciDevice.SubSystemVendorID.ToString("x") + " (" + pciDevice.ClassCode.ToString("x") + ":" + pciDevice.SubClassCode.ToString("x") + ":" + pciDevice.ProgIF.ToString("x") + ":" + pciDevice.RevisionID.ToString("x") + ")", ScreenColor.White, ScreenColor.Green);

                var children = DeviceService.GetChildrenOf(device);

                if (children.Count != 0)
                {
                    var child = children[0];

                    Console.WriteLine();
                    Console.Write("    ");

                    var pciDevice2 = child.DeviceDriver as PCIDevice;
                    InBrackets(child.Name, ScreenColor.White, ScreenColor.Green);
                }

                Console.WriteLine();
            }

            Console.Write("> Probing for disk controllers...");
            var diskcontrollers = DeviceService.GetDevices <IDiskControllerDevice>();

            Console.WriteLine("[Completed: " + diskcontrollers.Count.ToString() + " found]");

            foreach (var device in diskcontrollers)
            {
                Console.Write("  ");
                Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                InBrackets(device.Name, ScreenColor.White, ScreenColor.Green);
                Console.WriteLine();
            }

            Console.Write("> Probing for disks...");
            var disks = DeviceService.GetDevices <IDiskDevice>();

            Console.WriteLine("[Completed: " + disks.Count.ToString() + " found]");

            foreach (var disk in disks)
            {
                Console.Write("  ");
                Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                InBrackets(disk.Name, ScreenColor.White, ScreenColor.Green);
                Console.Write(" " + (disk.DeviceDriver as IDiskDevice).TotalBlocks.ToString() + " blocks");
                Console.WriteLine();
            }

            partitionService.CreatePartitionDevices();

            Console.Write("> Finding partitions...");
            var partitions = DeviceService.GetDevices <IPartitionDevice>();

            Console.WriteLine("[Completed: " + partitions.Count.ToString() + " found]");

            //foreach (var partition in partitions)
            //{
            //	Console.Write("  ");
            //	Bullet(ScreenColor.Yellow);
            //	Console.Write(" ");
            //	InBrackets(partition.Name, ScreenColor.White, ScreenColor.Green);
            //	Console.Write(" " + (partition.DeviceDriver as IPartitionDevice).BlockCount.ToString() + " blocks");
            //	Console.WriteLine();
            //}

            Console.Write("> Finding file systems...");

            foreach (var partition in partitions)
            {
                var fat = new FatFileSystem(partition.DeviceDriver as IPartitionDevice);

                if (fat.IsValid)
                {
                    Console.WriteLine("Found a FAT file system!");

                    const string filename = "TEST.TXT";

                    var location = fat.FindEntry(filename);

                    if (location.IsValid)
                    {
                        Console.Write("Found: " + filename);

                        var fatFileStream = new FatFileStream(fat, location);

                        uint len = (uint)fatFileStream.Length;

                        Console.WriteLine(" - Length: " + len.ToString() + " bytes");

                        Console.Write("Reading File: ");

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

                            if (i < 0)
                            {
                                break;
                            }

                            Console.Write((char)i);
                        }

                        Console.WriteLine();
                    }

                    const string bmpname = "WALLP.BMP";

                    var bmploc = fat.FindEntry(bmpname);

                    if (bmploc.IsValid)
                    {
                        Console.Write("Found: " + bmpname);

                        var fatFileStream = new FatFileStream(fat, bmploc);

                        uint len = (uint)fatFileStream.Length;

                        Console.WriteLine(" - Length: " + len.ToString() + " bytes");
                        Console.WriteLine();
                    }
                }
            }

            // Get StandardKeyboard
            var keyboards = DeviceService.GetDevices("StandardKeyboard");

            if (keyboards.Count == 0)
            {
                Console.WriteLine("No Keyboard!");
                ForeverLoop();
            }

            var stdKeyboard = keyboards[0].DeviceDriver as IKeyboardDevice;

            // setup keymap
            var keymap = new US();

            // setup keyboard (state machine)
            var keyboard = new DeviceSystem.Keyboard(stdKeyboard, keymap);

            // setup app manager
            var manager = new AppManager(Console, keyboard, serviceManager);

            IDT.SetInterruptHandler(manager.ProcessInterrupt);

            Logger.Log("<SELFTEST:PASSED>");

            manager.Start();
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
0
        public static void PatchSyslinux_3_72(PartitionDeviceDriver partitionDevice, FatFileSystem fat)
        {
            // Locate ldlinux.sys file for patching
            string filename = "ldlinux.sys";
            string name     = (Path.GetFileNameWithoutExtension(filename) + Path.GetExtension(filename).PadRight(4).Substring(0, 4)).ToUpper();

            var location = fat.FindEntry(name);

            if (location.IsValid)
            {
                // Read boot sector
                var bootSector = new DataBlock(partitionDevice.ReadBlock(0, 1));

                // Set the first sector location of the file
                bootSector.SetUInt(0x1F8, fat.GetSectorByCluster(location.FirstCluster));

                // Change jump address
                bootSector.SetUInt(0x01, 0x58);

                // Write back patched boot sector
                partitionDevice.WriteBlock(0, 1, bootSector.Data);

                // Get the file size & number of sectors used
                uint fileSize    = fat.GetFileSize(location.DirectorySector, location.DirectorySectorIndex);
                uint sectorCount = (fileSize + 511) >> 9;

                uint[] sectors = new uint[65];
                uint   nsec    = 0;

                // Create list of the first 65 sectors of the file
                for (uint cluster = location.FirstCluster; ((cluster != 0) & (nsec <= 64)); cluster = fat.GetNextCluster(cluster))
                {
                    uint sec = fat.GetSectorByCluster(cluster);
                    for (uint s = 0; s < fat.SectorsPerCluster; s++)
                    {
                        sectors[nsec++] = sec + s;
                    }
                }

                // Read the first cluster of the file
                var firstCluster = new DataBlock(fat.ReadCluster(location.FirstCluster));

                uint patchArea = 0;

                // Search for 0x3EB202FE (magic)
                for (patchArea = 0; (firstCluster.GetUInt(patchArea) != 0x3EB202FE) && (patchArea < fat.ClusterSizeInBytes); patchArea += 4)
                {
                    ;
                }

                patchArea = patchArea + 8;

                if (patchArea < fat.ClusterSizeInBytes)
                {
                    // Set up the totals
                    firstCluster.SetUShort(patchArea, (ushort)(fileSize >> 2));
                    firstCluster.SetUShort(patchArea + 2, (ushort)(sectorCount - 1));

                    // Clear sector entries
                    firstCluster.Fill(patchArea + 8, 0, 64 * 4);

                    // Set sector entries
                    for (nsec = 0; nsec < 64; nsec++)
                    {
                        firstCluster.SetUInt(patchArea + 8 + (nsec * 4), sectors[nsec + 1]);
                    }

                    // Clear out checksum
                    firstCluster.SetUInt(patchArea + 4, 0);

                    // Write back the updated cluster
                    fat.WriteCluster(location.FirstCluster, firstCluster.Data);

                    // Re-Calculate checksum by opening the file
                    var file = new FatFileStream(fat, location);

                    uint csum = 0x3EB202FE;
                    for (uint index = 0; index < (file.Length >> 2); index++)
                    {
                        uint value = (uint)file.ReadByte() | ((uint)file.ReadByte() << 8) | ((uint)file.ReadByte() << 16) | ((uint)file.ReadByte() << 24);
                        csum -= value;
                    }

                    // Set the checksum
                    firstCluster.SetUInt(patchArea + 4, csum);

                    // Write patched cluster back to disk
                    fat.WriteCluster(location.FirstCluster, firstCluster.Data);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Main
        /// </summary>
        public static void Main()
        {
            Kernel.x86.Kernel.Setup();

            Console = ConsoleManager.Controller.Boot;
            Console.Clear();

            Serial.SetupPort(Serial.COM1);
            IDT.SetInterruptHandler(ProcessInterrupt);

            hal = new Hardware();

            // Create Service manager and basic services
            var serviceManager = new ServiceManager();

            DeviceService = new DeviceService();

            var diskDeviceService    = new DiskDeviceService();
            var partitionService     = new PartitionService();
            var pciControllerService = new PCIControllerService();
            var pciDeviceService     = new PCIDeviceService();

            serviceManager.AddService(DeviceService);
            serviceManager.AddService(diskDeviceService);
            serviceManager.AddService(partitionService);
            serviceManager.AddService(pciControllerService);
            serviceManager.AddService(pciDeviceService);

            DeviceSystem.Setup.Initialize(hal, DeviceService.ProcessInterrupt);

            DeviceService.RegisterDeviceDriver(DeviceDriver.Setup.GetDeviceDriverRegistryEntries());
            DeviceService.Initialize(new X86System(), null);

            partitionService.CreatePartitionDevices();
            var partitions = DeviceService.GetDevices <IPartitionDevice>();

            foreach (var partition in partitions)
            {
                var fat = new FatFileSystem(partition.DeviceDriver as IPartitionDevice);
                hasFS = fat.IsValid;

                if (hasFS)
                {
                    var location = fat.FindEntry("WALLP.BMP");

                    if (location.IsValid)
                    {
                        var fatFileStream = new FatFileStream(fat, location);
                        var _wall         = new byte[(uint)fatFileStream.Length];

                        for (int k = 0; k < _wall.Length; k++)
                        {
                            _wall[k] = (byte)(char)fatFileStream.ReadByte();
                        }

                        wallpaper = new Bitmap(_wall);
                    }
                }
            }

            var standardMice = DeviceService.GetDevices("StandardMouse");

            if (standardMice.Count == 0)
            {
                hal.Pause();
                hal.Abort("Catastrophic failure, mouse and/or PIT not found.");
            }

            mouse = standardMice[0].DeviceDriver as StandardMouse;
            mouse.SetScreenResolution(VBE.ScreenWidth, VBE.ScreenHeight);

            if (VBEDisplay.InitVBE(hal))
            {
                Log("VBE setup OK!");
                DoGraphics();
            }
            else
            {
                Log("VBE setup ERROR!");
            }
        }
Exemplo n.º 6
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;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Creates the specified options.
        /// </summary>
        /// <param name="options">The options.</param>
        static public void Create(Options 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 / 512) + 1;
                }
            }

            var diskGeometry = new Mosa.DeviceSystem.DiskGeometry();

            diskGeometry.GuessGeometry(blockCount);

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

            if (options.ImageFormat == ImageFormatType.VDI)
            {
                // Create header
                byte[] header = Mosa.DeviceSystem.VDI.CreateHeader(
                    blockCount,
                    options.MediaGuid.ToByteArray(),
                    options.MediaLastSnapGuid.ToByteArray(),
                    diskGeometry
                    );

                diskDevice.WriteBlock(0, 1, header);

                byte[] map = Mosa.DeviceSystem.VDI.CreateImageMap(blockCount);

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

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

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

            // Create partition device
            PartitionDevice partitionDevice;

            if (options.MBROption)
            {
                // Create master boot block record
                MasterBootBlock 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 FileSystemType.FAT12: mbr.Partitions[0].PartitionType = PartitionType.FAT12; break;

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

                case FileSystemType.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
            FatSettings fatSettings = new FatSettings();

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

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

            case FileSystemType.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
            FatFileSystem 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)
            {
                Mosa.FileSystem.FAT.FatFileAttributes fileAttributes = new Mosa.FileSystem.FAT.FatFileAttributes();
                if (includeFile.Archive)
                {
                    fileAttributes |= Mosa.FileSystem.FAT.FatFileAttributes.Archive;
                }
                if (includeFile.ReadOnly)
                {
                    fileAttributes |= Mosa.FileSystem.FAT.FatFileAttributes.ReadOnly;
                }
                if (includeFile.Hidden)
                {
                    fileAttributes |= Mosa.FileSystem.FAT.FatFileAttributes.Hidden;
                }
                if (includeFile.System)
                {
                    fileAttributes |= Mosa.FileSystem.FAT.FatFileAttributes.System;
                }

                //byte[] file = File.ReadAllBytes(includeFile.Filename);
                string          newname  = (Path.GetFileNameWithoutExtension(includeFile.Filename).PadRight(8).Substring(0, 8) + Path.GetExtension(includeFile.Filename).PadRight(4).Substring(1, 3)).ToUpper();
                FatFileLocation location = fat.CreateFile(newname, fileAttributes, 0);

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

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

            if (options.PatchSyslinuxOption)
            {
                // Locate ldlinux.sys file for patching
                string filename = "ldlinux.sys";
                string name     = (Path.GetFileNameWithoutExtension(filename) + Path.GetExtension(filename).PadRight(4).Substring(0, 4)).ToUpper();

                FatFileLocation location = fat.FindEntry(new Mosa.FileSystem.FAT.Find.WithName(name), 0);

                if (location.Valid)
                {
                    // Read boot sector
                    Mosa.ClassLib.BinaryFormat bootSector = new Mosa.ClassLib.BinaryFormat(partitionDevice.ReadBlock(0, 1));

                    // Set the first sector location of the file
                    bootSector.SetUInt(0x1F8, fat.GetSectorByCluster(location.FirstCluster));

                    // Change jump address
                    bootSector.SetUInt(0x01, 0x58);

                    // Write back patched boot sector
                    partitionDevice.WriteBlock(0, 1, bootSector.Data);

                    // Get the file size & number of sectors used
                    uint fileSize    = fat.GetFileSize(location.DirectorySector, location.DirectorySectorIndex);
                    uint sectorCount = (fileSize + 511) >> 9;

                    uint[] sectors = new uint[65];
                    uint   nsec    = 0;

                    // Create list of the first 65 sectors of the file
                    for (uint cluster = location.FirstCluster; ((cluster != 0) & (nsec <= 64)); cluster = fat.GetNextCluster(cluster))
                    {
                        uint sec = fat.GetSectorByCluster(cluster);
                        for (uint s = 0; s < fat.SectorsPerCluster; s++)
                        {
                            sectors[nsec++] = sec + s;
                        }
                    }

                    // Read the first cluster of the file
                    Mosa.ClassLib.BinaryFormat firstCluster = new Mosa.ClassLib.BinaryFormat(fat.ReadCluster(location.FirstCluster));

                    uint patchArea = 0;

                    // Search for 0x3EB202FE (magic)
                    for (patchArea = 0; (firstCluster.GetUInt(patchArea) != 0x3EB202FE) && (patchArea < fat.ClusterSizeInBytes); patchArea += 4)
                    {
                        ;
                    }

                    patchArea = patchArea + 8;

                    if (patchArea < fat.ClusterSizeInBytes)
                    {
                        // Set up the totals
                        firstCluster.SetUShort(patchArea, (ushort)(fileSize >> 2));
                        firstCluster.SetUShort(patchArea + 2, (ushort)(sectorCount - 1));

                        // Clear sector entries
                        firstCluster.Fill(patchArea + 8, 0, 64 * 4);

                        // Set sector entries
                        for (nsec = 0; nsec < 64; nsec++)
                        {
                            firstCluster.SetUInt(patchArea + 8 + (nsec * 4), sectors[nsec + 1]);
                        }

                        // Clear out checksum
                        firstCluster.SetUInt(patchArea + 4, 0);

                        // Write back the updated cluster
                        fat.WriteCluster(location.FirstCluster, firstCluster.Data);

                        // Re-Calculate checksum by opening the file
                        FatFileStream file = new FatFileStream(fat, location);

                        uint csum = 0x3EB202FE;
                        for (uint index = 0; index < (file.Length >> 2); index++)
                        {
                            uint value = (uint)file.ReadByte() | ((uint)file.ReadByte() << 8) | ((uint)file.ReadByte() << 16) | ((uint)file.ReadByte() << 24);
                            csum -= value;
                        }

                        // Set the checksum
                        firstCluster.SetUInt(patchArea + 4, csum);

                        // Write patched cluster back to disk
                        fat.WriteCluster(location.FirstCluster, firstCluster.Data);
                    }
                }
            }

            if (options.ImageFormat == ImageFormatType.VHD)
            {
                // Create footer
                byte[] footer = Mosa.DeviceSystem.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();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Start the Device Driver System.
        /// </summary>
        static public void Start()
        {
            // Find all drivers
            Boot.Console.Write("Finding Drivers...");
            deviceDriverRegistry.RegisterBuiltInDeviceDrivers();
            var count = deviceDriverRegistry.GetPCIDeviceDrivers().Count + deviceDriverRegistry.GetISADeviceDrivers().Count;

            Boot.Console.WriteLine("[Completed: " + count.ToString() + " found]");

            // Start drivers for ISA devices
            StartISADevices();

            // Start drivers for PCI devices
            StartPCIDevices();

            // Get CMOS, StandardKeyboard, and PIC driver instances
            CMOS             = (CMOS)deviceManager.GetDevices(new WithName("CMOS")).First.Value;
            StandardKeyboard = (StandardKeyboard)deviceManager.GetDevices(new WithName("StandardKeyboard")).First.Value;

            Boot.Console.Write("Finding disk controllers...");
            var diskcontroller = new DiskControllerManager(Setup.DeviceManager);

            diskcontroller.CreateDiskDevices();

            var diskcontrollers = deviceManager.GetDevices(new IsDiskControllerDevice());

            Boot.Console.WriteLine("[Completed: " + diskcontrollers.Count.ToString() + " found]");
            foreach (var device in diskcontrollers)
            {
                Boot.Console.Write("Found controller ");
                Boot.InBrackets(device.Name, Mosa.Kernel.x86.Colors.White, Mosa.Kernel.x86.Colors.LightGreen);
                Boot.Console.WriteLine();
            }

            Boot.Console.Write("Finding disks...");
            var disks = deviceManager.GetDevices(new IsDiskDevice());

            Boot.Console.WriteLine("[Completed: " + disks.Count.ToString() + " found]");
            foreach (var disk in disks)
            {
                Boot.Console.Write("Spinning up disk ");
                Boot.InBrackets(disk.Name, Mosa.Kernel.x86.Colors.White, Mosa.Kernel.x86.Colors.LightGreen);
                Boot.Console.Write(" " + (disk as IDiskDevice).TotalBlocks.ToString() + " blocks");
                Boot.Console.WriteLine();
            }

            partitionManager.CreatePartitionDevices();

            Boot.Console.Write("Finding partitions...");
            var partitions = deviceManager.GetDevices(new IsPartitionDevice());

            Boot.Console.WriteLine("[Completed: " + partitions.Count.ToString() + " found]");
            foreach (var partition in partitions)
            {
                Boot.Console.Write("Opening partition: ");
                Boot.InBrackets(partition.Name, Mosa.Kernel.x86.Colors.White, Mosa.Kernel.x86.Colors.LightGreen);
                Boot.Console.Write(" " + (partition as IPartitionDevice).BlockCount.ToString() + " blocks");
                Boot.Console.WriteLine();
            }

            Boot.Console.Write("Finding file systems...");
            var filesystem = deviceManager.GetDevices(new IsPartitionDevice());

            //Boot.Console.WriteLine("[Completed: " + filesystem.Count.ToString() + " found]");
            foreach (var partition in partitions)
            {
                var fat = new FatFileSystem(partition as IPartitionDevice);

                if (fat.IsValid)
                {
                    Boot.Console.WriteLine("Found a FAT file system!");

                    var filename = "TEST.TXT";

                    var location = fat.FindEntry(filename);

                    if (location.IsValid)
                    {
                        Boot.Console.WriteLine("Found: " + filename);

                        var fatFileStream = new FatFileStream(fat, location);

                        uint len = (uint)fatFileStream.Length;

                        Boot.Console.WriteLine("Length: " + len.ToString());

                        Boot.Console.WriteLine("Reading File:");

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

                            if (i < 0)
                            {
                                break;
                            }

                            Boot.Console.Write((char)i);
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Main
        /// </summary>
        public static void Main()
        {
            Kernel.x86.Kernel.Setup();

            Console = ConsoleManager.Controller.Boot;

            Console.Clear();
            Console.Goto(0, 0);

            //IDT.SetInterruptHandler(ProcessInterrupt);

            Console.ScrollRow       = 23;
            Console.Color           = ScreenColor.White;
            Console.BackgroundColor = ScreenColor.Green;

            Debug = ConsoleManager.Controller.Boot;

            Console.Write("                   MOSA OS Version 1.5 - Compiler Version 1.5");
            FillLine();
            Console.Color           = ScreenColor.White;
            Console.BackgroundColor = ScreenColor.Black;

            Console.WriteLine("> Initializing hardware abstraction layer...");
            var hardware = new HAL.Hardware();

            var DeviceManager = Setup.Initialize(PlatformArchitecture.X86, hardware);

            Console.WriteLine("> Registering device drivers...");
            DeviceDriver.Setup.Register(DeviceManager);

            Console.WriteLine("> Starting devices...");
            DeviceDriver.Setup.Start(DeviceManager);

            Console.Write("> Probing for ISA devices...");
            var isaDevices = DeviceManager.GetAllDevices();

            Console.WriteLine("[Completed: " + isaDevices.Count.ToString() + " found]");

            foreach (var device in isaDevices)
            {
                Console.Write("  ");
                Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                InBrackets(device.Name, ScreenColor.White, ScreenColor.Green);
                Console.WriteLine();
            }

            Console.Write("> Probing for PCI devices...");

            //Setup.StartPCIDevices();
            var pciDevices = DeviceManager.GetDevices <DeviceSystem.PCI.IPCIDevice>(DeviceStatus.Available);

            Console.WriteLine("[Completed: " + pciDevices.Count.ToString() + " found]");

            foreach (var device in pciDevices)
            {
                var pciDevice = device.DeviceDriver as DeviceSystem.PCI.IPCIDevice;

                Console.Write("  ");
                Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                InBrackets(device.Name + ": " + pciDevice.VendorID.ToString("x") + ":" + pciDevice.DeviceID.ToString("x") + " " + pciDevice.SubSystemID.ToString("x") + ":" + pciDevice.SubVendorID.ToString("x") + " (" + pciDevice.Function.ToString("x") + ":" + pciDevice.ClassCode.ToString("x") + ":" + pciDevice.SubClassCode.ToString("x") + ":" + pciDevice.ProgIF.ToString("x") + ":" + pciDevice.RevisionID.ToString("x") + ")", ScreenColor.White, ScreenColor.Green);
                Console.WriteLine();
            }

            Console.Write("> Probing for disk controllers...");
            var diskcontrollers = DeviceManager.GetDevices <IDiskControllerDevice>();

            Console.WriteLine("[Completed: " + diskcontrollers.Count.ToString() + " found]");

            foreach (var device in diskcontrollers)
            {
                Console.Write("  ");
                Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                InBrackets(device.Name, ScreenColor.White, ScreenColor.Green);
                Console.WriteLine();
            }

            Console.Write("> Probing for disks...");
            var disks = DeviceManager.GetDevices <IDiskDevice>();

            Console.WriteLine("[Completed: " + disks.Count.ToString() + " found]");

            foreach (var disk in disks)
            {
                Console.Write("  ");
                Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                InBrackets(disk.Name, ScreenColor.White, ScreenColor.Green);
                Console.Write(" " + (disk.DeviceDriver as IDiskDevice).TotalBlocks.ToString() + " blocks");
                Console.WriteLine();
            }

            var partitionManager = new PartitionManager(DeviceManager);

            partitionManager.CreatePartitionDevices();

            Console.Write("> Finding partitions...");
            var partitions = DeviceManager.GetDevices <IPartitionDevice>();

            Console.WriteLine("[Completed: " + partitions.Count.ToString() + " found]");

            foreach (var partition in partitions)
            {
                Console.Write("  ");
                Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                InBrackets(partition.Name, ScreenColor.White, ScreenColor.Green);
                Console.Write(" " + (partition.DeviceDriver as IPartitionDevice).BlockCount.ToString() + " blocks");
                Console.WriteLine();
            }

            Console.Write("> Finding file systems...");

            foreach (var partition in partitions)
            {
                var fat = new FatFileSystem(partition.DeviceDriver as IPartitionDevice);

                if (fat.IsValid)
                {
                    Console.WriteLine("Found a FAT file system!");

                    const string filename = "TEST.TXT";

                    var location = fat.FindEntry(filename);

                    if (location.IsValid)
                    {
                        Console.Write("Found: " + filename);

                        var fatFileStream = new FatFileStream(fat, location);

                        uint len = (uint)fatFileStream.Length;

                        Console.WriteLine(" - Length: " + len.ToString());

                        Console.Write("Reading File: ");

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

                            if (i < 0)
                            {
                                break;
                            }

                            Console.Write((char)i);
                        }

                        Console.WriteLine();
                    }
                }
            }

            // Get StandardKeyboard
            var standardKeyboards = DeviceManager.GetDevices("StandardKeyboard");

            if (standardKeyboards.Count == 0)
            {
                Console.WriteLine("No Keyboard!");
                ForeverLoop();
            }

            var standardKeyboard = standardKeyboards[0].DeviceDriver as IKeyboardDevice;

            Debug = ConsoleManager.Controller.Debug;

            // setup keymap
            var keymap = new US();

            // setup keyboard (state machine)
            var keyboard = new DeviceSystem.Keyboard(standardKeyboard, keymap);

            // setup app manager
            var manager = new AppManager(Console, keyboard);

            IDT.SetInterruptHandler(manager.ProcessInterrupt);

            Logger.Log("<SELFTEST:PASSED>");

            manager.Start();
        }
Exemplo n.º 10
0
        private static void SetupDrivers()
        {
            Console.WriteLine("> Initializing services...");

            // Create Service manager and basic services
            var serviceManager = new ServiceManager();

            var deviceService        = new DeviceService();
            var diskDeviceService    = new DiskDeviceService();
            var partitionService     = new PartitionService();
            var pciControllerService = new PCIControllerService();
            var pciDeviceService     = new PCIDeviceService();

            serviceManager.AddService(deviceService);
            serviceManager.AddService(diskDeviceService);
            serviceManager.AddService(partitionService);
            serviceManager.AddService(pciControllerService);
            serviceManager.AddService(pciDeviceService);

            Console.WriteLine("> Initializing hardware abstraction layer...");

            // Set device driver system with the hardware HAL
            var hardware = new Hardware();

            Mosa.DeviceSystem.Setup.Initialize(hardware, deviceService.ProcessInterrupt);

            Console.WriteLine("> Registering device drivers...");
            deviceService.RegisterDeviceDriver(Mosa.DeviceDriver.Setup.GetDeviceDriverRegistryEntries());

            Console.WriteLine("> Starting devices...");
            deviceService.Initialize(new X86System(), null);

            Console.Write("> Probing for ISA devices...");
            var isaDevices = deviceService.GetChildrenOf(deviceService.GetFirstDevice <ISABus>());

            Console.WriteLine("[Completed: " + isaDevices.Count.ToString() + " found]");

            foreach (var device in isaDevices)
            {
                Console.Write("  ");
                //Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                //InBrackets(device.Name, ScreenColor.White, ScreenColor.Green);
                Console.Write(device.Name);
                Console.WriteLine();
            }

            Console.Write("> Probing for PCI devices...");
            var devices = deviceService.GetDevices <PCIDevice>();

            Console.WriteLine("[Completed: " + devices.Count.ToString() + " found]");

            foreach (var device in devices)
            {
                Console.Write("  ");
                //Bullet(ScreenColor.Yellow);
                Console.Write(" ");

                var pciDevice = device.DeviceDriver as PCIDevice;
                Console.Write(device.Name + ": " + pciDevice.VendorID.ToString("x") + ":" + pciDevice.DeviceID.ToString("x") + " " + pciDevice.SubSystemID.ToString("x") + ":" + pciDevice.SubSystemVendorID.ToString("x") + " (" + pciDevice.Function.ToString("x") + ":" + pciDevice.ClassCode.ToString("x") + ":" + pciDevice.SubClassCode.ToString("x") + ":" + pciDevice.ProgIF.ToString("x") + ":" + pciDevice.RevisionID.ToString("x") + ")");

                var children = deviceService.GetChildrenOf(device);

                if (children.Count != 0)
                {
                    var child = children[0];

                    Console.WriteLine();
                    Console.Write("    ");

                    var pciDevice2 = child.DeviceDriver as PCIDevice;
                    Console.Write(child.Name);
                }

                Console.WriteLine();
            }

            Console.Write("> Probing for disk controllers...");
            var diskcontrollers = deviceService.GetDevices <IDiskControllerDevice>();

            Console.WriteLine("[Completed: " + diskcontrollers.Count.ToString() + " found]");

            foreach (var device in diskcontrollers)
            {
                Console.Write("  ");
                //Bullet(ScreenColor.Yellow);
                Console.Write(" ");
                Console.Write(device.Name);
                Console.WriteLine();
            }

            Console.Write("> Probing for disks...");
            var disks = deviceService.GetDevices <IDiskDevice>();

            Console.WriteLine("[Completed: " + disks.Count.ToString() + " found]");

            foreach (var disk in disks)
            {
                Console.Write("  ");
                Console.Write(" ");
                Console.Write(disk.Name);
                Console.Write(" " + (disk.DeviceDriver as IDiskDevice).TotalBlocks.ToString() + " blocks");
                Console.WriteLine();
            }

            if (disks.Count >= 2 && disks[1].DeviceDriver != null && disks[1].DeviceDriver is IDiskDevice)
            {
                HostCommunicator.Init(disks[1].DeviceDriver as IDiskDevice);
            }

            partitionService.CreatePartitionDevices();

            Console.Write("> Finding partitions...");
            var partitions = deviceService.GetDevices <IPartitionDevice>();

            Console.WriteLine("[Completed: " + partitions.Count.ToString() + " found]");

            //foreach (var partition in partitions)
            //{
            //  Console.Write("  ");
            //  Bullet(ScreenColor.Yellow);
            //  Console.Write(" ");
            //  InBrackets(partition.Name, ScreenColor.White, ScreenColor.Green);
            //  Console.Write(" " + (partition.DeviceDriver as IPartitionDevice).BlockCount.ToString() + " blocks");
            //  Console.WriteLine();
            //}

            Console.Write("> Finding file systems...");

            foreach (var partition in partitions)
            {
                var fat = new FatFileSystem(partition.DeviceDriver as IPartitionDevice);

                if (fat.IsValid)
                {
                    Console.WriteLine("Found a FAT file system!");

                    const string filename = "TEST.TXT";

                    var location = fat.FindEntry(filename);

                    if (location.IsValid)
                    {
                        Console.Write("Found: " + filename);

                        using (var fatFileStream = new FatFileStream(fat, location))
                        {
                            uint len = (uint)fatFileStream.Length;

                            Console.WriteLine(" - Length: " + len.ToString());

                            Console.Write("Reading File: ");

                            while (true)
                            {
                                int i = fatFileStream.ReadByte();

                                if (i < 0)
                                {
                                    break;
                                }

                                Console.Write((char)i);
                            }
                        }

                        Console.WriteLine();
                    }
                }
            }

            // Get StandardKeyboard
            var standardKeyboards = deviceService.GetDevices("StandardKeyboard");

            if (standardKeyboards.Count == 0)
            {
                Console.WriteLine("No Keyboard!");
                //ForeverLoop();
            }

            var standardKeyboard = standardKeyboards[0].DeviceDriver as IKeyboardDevice;

            //Debug = ConsoleManager.Controller.Debug;

            // setup keymap
            var keymap = new US();

            // setup keyboard (state machine)
            var keyboard = new Mosa.DeviceSystem.Keyboard(standardKeyboard, keymap);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Main
        /// </summary>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        static int Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("MakeImageBoot v1.0 [www.mosa-project.org]");
            Console.WriteLine("Copyright 2010. New BSD License.");
            Console.WriteLine("Written by Philipp Garcia ([email protected])");
            Console.WriteLine();

            string             mbrFilename         = string.Empty;
            string             fatcodeFilename     = string.Empty;
            string             volumeLabel         = string.Empty;
            ImageFormat        imageFormat         = ImageFormat.VHD;
            bool               mbrOption           = true;
            bool               patchSyslinuxOption = false;
            bool               floppyMedia         = false;
            uint               blockCount          = 1024 * 1024 / 512;
            FileSystem         fileSystem          = FileSystem.FAT12;
            List <IncludeFile> includeFiles        = new List <IncludeFile>();

            bool valid = (args.Length < 2);

            if (valid)
            {
                valid = System.IO.File.Exists(args[0]);
            }

            if (valid)
            {
                Console.WriteLine("Usage: CreateBootImage <boot.config file> <image name>");
                Console.Error.WriteLine("ERROR: Missing arguments");
                return(-1);
            }

            Console.WriteLine("Building image...");

            try
            {
                StreamReader reader = File.OpenText(args[0]);

                while (true)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    string[] parts = line.Split('\t');

                    switch (parts[0].Trim())
                    {
                    case "-mbr": mbrOption = true; mbrFilename = (parts.Length > 1) ? parts[1] : null; break;

                    case "-boot": fatcodeFilename = (parts.Length > 1) ? parts[1] : null; break;

                    case "-vhd": imageFormat = ImageFormat.VHD; break;

                    case "-img": imageFormat = ImageFormat.IMG; break;

                    case "-vdi": imageFormat = ImageFormat.VDI; break;

                    case "-syslinux": patchSyslinuxOption = true; break;

                    case "-fat12": fileSystem = FileSystem.FAT12; break;

                    case "-fat16": fileSystem = FileSystem.FAT16; break;

                    case "-fat32": fileSystem = FileSystem.FAT32; break;

                    case "-file": if (parts.Length > 2)
                        {
                            includeFiles.Add(new IncludeFile(parts[1], parts[2]));
                        }
                        else
                        {
                            includeFiles.Add(new IncludeFile(parts[1]));
                        } break;

                    case "-blocks": blockCount = Convert.ToUInt32(parts[1]); break;

                    case "-volume": volumeLabel = parts[1]; break;

                    case "-floppy": floppyMedia = true; break;

                    default: break;
                    }
                }

                reader.Close();

                if (System.IO.File.Exists(args[1]))
                {
                    System.IO.File.Delete(args[1]);
                }

                DiskGeometry diskGeometry = new Mosa.DeviceSystem.DiskGeometry();
                diskGeometry.GuessGeometry(blockCount);

                Console.WriteLine("Disk Geometry CHS = " + diskGeometry.Cylinders + "/" + diskGeometry.Heads + "/" + diskGeometry.SectorsPerTrack);

                // Create disk image file
                Mosa.EmulatedDevices.Synthetic.DiskDevice diskDevice = new Mosa.EmulatedDevices.Synthetic.DiskDevice(args[1]);

                if (imageFormat == ImageFormat.VDI)
                {
                    // Create header
                    byte[] header = Mosa.DeviceSystem.VDI.CreateHeader(
                        blockCount,
                        Guid.NewGuid().ToByteArray(),
                        Guid.NewGuid().ToByteArray(),
                        diskGeometry
                        );

                    diskDevice.WriteBlock(0, 1, header);

                    byte[] map = Mosa.DeviceSystem.VDI.CreateImageMap(blockCount);

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

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

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

                // Create partition device
                PartitionDevice partitionDevice;

                if (mbrOption)
                {
                    // Create master boot block record
                    MasterBootBlock 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 (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;
                    }

                    if (!string.IsNullOrEmpty(mbrFilename))
                    {
                        mbr.Code = ReadFile(mbrFilename);
                    }

                    mbr.Write();

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

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

                switch (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 = floppyMedia;
                fatSettings.VolumeLabel = volumeLabel;
                fatSettings.SerialID    = new byte[4] {
                    0x01, 0x02, 0x03, 0x04
                };
                fatSettings.SectorsPerTrack = diskGeometry.SectorsPerTrack;
                fatSettings.NumberOfHeads   = diskGeometry.Heads;
                fatSettings.HiddenSectors   = diskGeometry.SectorsPerTrack;
                if (!string.IsNullOrEmpty(fatcodeFilename))
                {
                    fatSettings.OSBootCode = ReadFile(fatcodeFilename);
                }

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

                fat.SetVolumeName(volumeLabel);

                foreach (IncludeFile includeFile in includeFiles)
                {
                    string filename = includeFile.Filename;

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

                    byte[]          file     = ReadFile(filename);
                    string          newname  = (Path.GetFileNameWithoutExtension(includeFile.Newname).PadRight(8).Substring(0, 8) + Path.GetExtension(includeFile.Newname).PadRight(3).Substring(1, 3)).ToUpper();
                    FatFileLocation location = fat.CreateFile(newname, fileAttributes, 0);

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

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

                if (patchSyslinuxOption)
                {
                    // Locate ldlinux.sys file for patching
                    string filename = "ldlinux.sys";
                    string name     = (Path.GetFileNameWithoutExtension(filename) + Path.GetExtension(filename).PadRight(3).Substring(0, 4)).ToUpper();

                    FatFileLocation location = fat.FindEntry(new Mosa.FileSystem.FAT.Find.WithName(name), 0);

                    if (location.Valid)
                    {
                        // Read boot sector
                        Mosa.ClassLib.BinaryFormat bootSector = new Mosa.ClassLib.BinaryFormat(partitionDevice.ReadBlock(0, 1));

                        // Set the first sector location of the file
                        bootSector.SetUInt(0x1F8, fat.GetSectorByCluster(location.FirstCluster));

                        // Change jump address
                        bootSector.SetUInt(0x01, 0x58);

                        // Write back patched boot sector
                        partitionDevice.WriteBlock(0, 1, bootSector.Data);

                        // Get the file size & number of sectors used
                        uint fileSize    = fat.GetFileSize(location.DirectorySector, location.DirectorySectorIndex);
                        uint sectorCount = (fileSize + 511) >> 9;

                        uint[] sectors = new uint[65];
                        uint   nsec    = 0;

                        // Create list of the first 65 sectors of the file
                        for (uint cluster = location.FirstCluster; ((cluster != 0) & (nsec <= 64)); cluster = fat.GetNextCluster(cluster))
                        {
                            uint sec = fat.GetSectorByCluster(cluster);
                            for (uint s = 0; s < fat.SectorsPerCluster; s++)
                            {
                                sectors[nsec++] = sec + s;
                            }
                        }

                        // Read the first cluster of the file
                        Mosa.ClassLib.BinaryFormat firstCluster = new Mosa.ClassLib.BinaryFormat(fat.ReadCluster(location.FirstCluster));

                        uint patchArea = 0;

                        // Search for 0x3EB202FE (magic)
                        for (patchArea = 0; (firstCluster.GetUInt(patchArea) != 0x3EB202FE) && (patchArea < fat.ClusterSizeInBytes); patchArea += 4)
                        {
                            ;
                        }

                        patchArea = patchArea + 8;

                        if (patchArea < fat.ClusterSizeInBytes)
                        {
                            // Set up the totals
                            firstCluster.SetUShort(patchArea, (ushort)(fileSize >> 2));
                            firstCluster.SetUShort(patchArea + 2, (ushort)(sectorCount - 1));

                            // Clear sector entries
                            firstCluster.Fill(patchArea + 8, 0, 64 * 4);

                            // Set sector entries
                            for (nsec = 0; nsec < 64; nsec++)
                            {
                                firstCluster.SetUInt(patchArea + 8 + (nsec * 4), sectors[nsec + 1]);
                            }

                            // Clear out checksum
                            firstCluster.SetUInt(patchArea + 4, 0);

                            // Write back the updated cluster
                            fat.WriteCluster(location.FirstCluster, firstCluster.Data);

                            // Re-Calculate checksum by openning the file
                            FatFileStream file = new FatFileStream(fat, location);

                            uint csum = 0x3EB202FE;
                            for (uint index = 0; index < (file.Length >> 2); index++)
                            {
                                uint value = (uint)file.ReadByte() | ((uint)file.ReadByte() << 8) | ((uint)file.ReadByte() << 16) | ((uint)file.ReadByte() << 24);
                                csum -= value;
                            }

                            // Set the checksum
                            firstCluster.SetUInt(patchArea + 4, csum);

                            // Write patched cluster back to disk
                            fat.WriteCluster(location.FirstCluster, firstCluster.Data);
                        }
                    }
                }

                if (imageFormat == ImageFormat.VHD)
                {
                    // Create footer
                    byte[] footer = Mosa.DeviceSystem.VHD.CreateFooter(
                        blockCount,
                        (uint)(DateTime.Now - (new DateTime(2000, 1, 1, 0, 0, 0))).Seconds,
                        Guid.NewGuid().ToByteArray(),
                        diskGeometry
                        );

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

                Console.WriteLine("Completed!");
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error: " + e.ToString());
                return(-1);
            }

            return(0);
        }