private static void InitAta(Ata.ControllerIdEnum aControllerID, Ata.BusPositionEnum aBusPosition) { var xIO = aControllerID == Ata.ControllerIdEnum.Primary ? Core.Global.BaseIOGroups.ATA1 : Core.Global.BaseIOGroups.ATA2; var xATA = new AtaPio(xIO, aControllerID, aBusPosition); if (xATA.DriveType == AtaPio.SpecLevel.Null) { return; } if (xATA.DriveType == AtaPio.SpecLevel.ATA) { BlockDevice.BlockDevice.Devices.Add(xATA); Ata.AtaDebugger.Send("ATA device with speclevel ATA found."); } else { //Ata.AtaDebugger.Send("ATA device with spec level " + (int)xATA.DriveType + // " found, which is not supported!"); return; } var xMbrData = new byte[512]; xATA.ReadBlock(0UL, 1U, xMbrData); var xMBR = new MBR(xMbrData); if (xMBR.EBRLocation != 0) { //EBR Detected var xEbrData = new byte[512]; xATA.ReadBlock(xMBR.EBRLocation, 1U, xEbrData); var xEBR = new EBR(xEbrData); for (int i = 0; i < xEBR.Partitions.Count; i++) { //var xPart = xEBR.Partitions[i]; //var xPartDevice = new BlockDevice.Partition(xATA, xPart.StartSector, xPart.SectorCount); //BlockDevice.BlockDevice.Devices.Add(xPartDevice); } } // TODO Change this to foreach when foreach is supported Ata.AtaDebugger.Send("Number of MBR partitions found: " + xMBR.Partitions.Count); for (int i = 0; i < xMBR.Partitions.Count; i++) { var xPart = xMBR.Partitions[i]; if (xPart == null) { Console.WriteLine("Null partition found at idx " + i); } else { var xPartDevice = new Partition(xATA, xPart.StartSector, xPart.SectorCount); BlockDevice.BlockDevice.Devices.Add(xPartDevice); Console.WriteLine("Found partition at idx " + i); } } }
private bool FindHardDrive() { int harddrives = 0; for (int i = 0; i < BlockDevice.Devices.Count; i++) { if (BlockDevice.Devices[i] is AtaPio) { ATA = (AtaPio)BlockDevice.Devices[i]; ++harddrives; break; } } if (harddrives > 0) { return(true); } else { return(false); } }
public void Detect() { // Detect a hard drive if (this.vlevel == 1) { Out.printf("+-- Detecting...\n"); } if (this.vlevel == 2) { Out.printf("+-- Detecting hard drives...\n"); } for (int i = 0; i < BlockDevice.Devices.Count; i++) { var xDevice = BlockDevice.Devices[i]; if (xDevice is AtaPio) { if (this.vlevel == 2) { Out.printf("| +-- Hard drive found!\n"); } this.xATA = (AtaPio)xDevice; } } }
protected void TestATA() { #region Comment(OLD) /* * try * { * Console.WriteLine(); * Console.WriteLine("Block devices found: " + BlockDevice.Devices.Count); * * AtaPio xATA = null; * for (int i = 0; i < BlockDevice.Devices.Count; i++) * { * var xDevice = BlockDevice.Devices[i]; * if (xDevice is AtaPio) * { * xATA = (AtaPio)xDevice; * } * } * Console.WriteLine("--------------------------"); * Console.WriteLine("Type: " + (xATA.DriveType == AtaPio.SpecLevel.ATA ? "ATA" : "ATAPI")); * Console.WriteLine("Serial No: " + xATA.SerialNo); * Console.WriteLine("Firmware Rev: " + xATA.FirmwareRev); * Console.WriteLine("Model No: " + xATA.ModelNo); * Console.WriteLine("Block Size: " + xATA.BlockSize + " bytes"); * Console.WriteLine("Size: " + xATA.BlockCount * xATA.BlockSize / 1024 / 1024 + " MB"); * * //Partition xPartition = null; * //for (int i = 0; i < BlockDevice.Devices.Count; i++) * //{ * // var xDevice = BlockDevice.Devices[i]; * // if (xDevice is Partition) * // { * // xPartition = (Partition)xDevice; * // } * //} * * //var xFS = new FAT.FatFileSystem(xPartition); * //Sys.Filesystem.FileSystem.AddMapping("C", xFS); * * //Console.WriteLine(); * //Console.WriteLine("Root directory"); * //var xListing = xFS.GetRoot(); * //FAT.Listing.FatFile xRootFile = null; * //FAT.Listing.FatFile xKudzuFile = null; * //for (int i = 0; i < xListing.Count; i++) * //{ * // var xItem = xListing[i]; * // if (xItem is Sys.Filesystem.Listing.Directory) * // { * // Console.WriteLine("<" + xListing[i].Name + ">"); * // } * // else if (xItem is Sys.Filesystem.Listing.File) * // { * // Console.WriteLine(); * // Console.WriteLine(xListing[i].Name); * // Console.WriteLine(xListing[i].Size); * // if (xListing[i].Name == "Root.txt") * // { * // xRootFile = (FAT.Listing.FatFile)xListing[i]; * // } * // else if (xListing[i].Name == "Kudzu.txt") * // { * // xKudzuFile = (FAT.Listing.FatFile)xListing[i]; * // } * // } * //} * * //{ * // var xStream = new Sys.Filesystem.FAT.FatStream(xRootFile); * // var xData = new byte[xRootFile.Size]; * // xStream.Read(xData, 0, (int)xRootFile.Size); * // var xText = Encoding.ASCII.GetString(xData); * // Console.WriteLine(xText); * //} * * //{ * // Console.WriteLine(); * // Console.WriteLine("StreamReader"); * // var xStream = new Sys.Filesystem.FAT.FatStream(xRootFile); * // var xReader = new System.IO.StreamReader(xStream); * // string xText = xReader.ReadToEnd(); * // Console.WriteLine(xText); * //} * * //var xKudzuStream = new Sys.Filesystem.FAT.FatStream(xKudzuFile); * //var xKudzuData = new byte[xKudzuFile.Size]; * //xKudzuStream.Read(xKudzuData, 0, (int)xKudzuFile.Size); * * //string xLower = "Hello"; * //Console.WriteLine(xLower.ToUpper()); * //Console.WriteLine(xLower.ToLower()); * * //var xFile = new System.IO.FileStream(@"c:\Root.txt", System.IO.FileMode.Open); * * //int dummy = 42; * * //var xWrite = new byte[512]; * //for (int i = 0; i < 512; i++) * //{ * // xWrite[i] = (byte)i; * //} * //xATA.WriteBlock(0, 1, xWrite); * * //var xRead = xATA.NewBlockArray(1); * //xATA.ReadBlock(0, 1, xRead); * //string xDisplay = ""; * //for (int i = 0; i < 512; i++) * //{ * // xDisplay = xDisplay + xRead[i].ToHex(); * //} * //Console.WriteLine(xDisplay); * * } * catch (Exception e) * { * Console.WriteLine("Exception: " + e.Message); * Stop(); * }*/ #endregion try { Console.WriteLine(); Console.WriteLine("Block devices found: " + BlockDevice.Devices.Count); AtaPio xATA = null; for (int i = 0; i < BlockDevice.Devices.Count; i++) { var xDevice = BlockDevice.Devices[i]; if (xDevice is AtaPio) { xATA = (AtaPio)xDevice; } } //Info Console.WriteLine("--------------------------"); Console.WriteLine("Type: " + (xATA.DriveType == AtaPio.SpecLevel.ATA ? "ATA" : "ATAPI")); Console.WriteLine("Serial No: " + xATA.SerialNo); Console.WriteLine("Firmware Rev: " + xATA.FirmwareRev); Console.WriteLine("Model No: " + xATA.ModelNo); Console.WriteLine("Block Size: " + xATA.BlockSize + " bytes"); Console.WriteLine("Size: " + xATA.BlockCount * xATA.BlockSize / 1024 / 1024 + " MB"); //Partition Detecting Partition xPartition = null; if (BlockDevice.Devices.Count > 0) { for (int i = 0; i < BlockDevice.Devices.Count; i++) { var xDevice = BlockDevice.Devices[i]; if (xDevice is Partition) { xPartition = (Partition)xDevice; Console.WriteLine("FAT FS"); var xFS = new FAT.FatFileSystem(xPartition); Console.WriteLine("Mapping..."); FileSystem.AddMapping(xFS); Console.WriteLine(); Console.WriteLine("Root directory"); var xListing = xFS.GetRoot(); FAT.Listing.FatFile xRootFile = null; FAT.Listing.FatFile xKudzuFile = null; var xFile = default(FAT.Listing.FatFile); for (int j = 0; j < xListing.Count; j++) { var xItem = xListing[j]; if (xItem is Sys.Filesystem.Listing.Directory) { //Detecting Dir in HDD Console.WriteLine("<DIR> " + xListing[j].Name); } else if (xItem is Sys.Filesystem.Listing.File) { //Detecting File in HDD Console.WriteLine("<FILE> " + xListing[j].Name + " (" + xListing[j].Size + ")"); if (xListing[j].Name == "Root.txt") { xRootFile = (FAT.Listing.FatFile)xListing[j]; } else if (xListing[j].Name == "Kudzu.txt") { xKudzuFile = (FAT.Listing.FatFile)xListing[j]; } else if (xListing[j].Name == "Amanp.txt") { xFile = (FAT.Listing.FatFile)xListing[j]; } } } try { Console.WriteLine(); Console.WriteLine("StreamReader - Root File"); var xStream = new Sys.Filesystem.FAT.FatStream(xRootFile); var xData = new byte[xRootFile.Size]; xStream.Read(xData, 0, (int)xRootFile.Size); var xText = Encoding.ASCII.GetString(xData); Console.WriteLine(xText); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } var xKudzuStream = new Sys.Filesystem.FAT.FatStream(xKudzuFile); var xKudzuData = new byte[xKudzuFile.Size]; xKudzuStream.Read(xKudzuData, 0, (int)xKudzuFile.Size); if (xFile != null) { //var xFs = new Sys.Filesystem.FAT.FatStream(xFile); //xFs.Write(new byte[1024], 0, 1024); } } } } else { Console.WriteLine("No Block Device Found! "); } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } }
/// <summary> /// Creates a new IDE object based on the blockdevice /// </summary> /// <param name="blockDevice">The blockdevice to use</param> public IDE(AtaPio blockDevice) { this.blockDevice = blockDevice; }
protected override void Run() { try { Console.ReadLine(); Console.WriteLine(); Console.WriteLine("Block devices found: " + BlockDevice.Devices.Count); AtaPio xATA = null; for (int i = 0; i < BlockDevice.Devices.Count; i++) { var xDevice = BlockDevice.Devices[i]; if (xDevice is AtaPio) { xATA = (AtaPio)xDevice; } } //Info Console.WriteLine("--------------------------"); Console.WriteLine("Type: " + (xATA.DriveType == AtaPio.SpecLevel.ATA ? "ATA" : "ATAPI")); Console.WriteLine("Serial No: " + xATA.SerialNo); Console.WriteLine("Firmware Rev: " + xATA.FirmwareRev); Console.WriteLine("Model No: " + xATA.ModelNo); Console.WriteLine("Block Size: " + xATA.BlockSize + " bytes"); Console.WriteLine("Size: " + xATA.BlockCount * xATA.BlockSize / 1024 / 1024 + " MB"); //Partition Detecting Partition xPartition = null; if (BlockDevice.Devices.Count > 0) { for (int i = 0; i < BlockDevice.Devices.Count; i++) { var xDevice = BlockDevice.Devices[i]; if (xDevice is Partition) { xPartition = (Partition)xDevice; Console.WriteLine("FAT FS"); var xFS = new FAT.FatFileSystem(xPartition); Console.WriteLine("Mapping..."); Sys.Filesystem.FileSystem.AddMapping("C", xFS); Console.WriteLine(); Console.WriteLine("Root directory"); var xListing = xFS.GetRoot(); FAT.Listing.FatFile xRootFile = null; FAT.Listing.FatFile xKudzuFile = null; for (int j = 0; j < xListing.Count; j++) { var xItem = xListing[j]; if (xItem is Sys.Filesystem.Listing.Directory) { //Detecting Dir in HDD Console.WriteLine("<DIR> " + xListing[j].Name); } else if (xItem is Sys.Filesystem.Listing.File) { //Detecting File in HDD Console.WriteLine("<FILE> " + xListing[j].Name + " (" + xListing[j].Size + ")"); if (xListing[j].Name == "Root.txt") { xRootFile = (FAT.Listing.FatFile)xListing[j]; } else if (xListing[j].Name == "Kudzu.txt") { xKudzuFile = (FAT.Listing.FatFile)xListing[j]; } } } try { Console.WriteLine(); Console.WriteLine("StreamReader - Root.txt File"); var xStream = new FAT.FatStream(xRootFile); var xData = new byte[xRootFile.Size]; xStream.Read(xData, 0, (int)xData.Length); var xText = Encoding.ASCII.GetString(xData); Console.WriteLine(xText); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } try { Console.WriteLine(); Console.WriteLine("StreamReader - Kudzu.txt File"); var xStream = new FAT.FatStream(xKudzuFile); var xData = new byte[xKudzuFile.Size]; xStream.Read(xData, 0, (int)xData.Length); var xText = Encoding.ASCII.GetString(xData); Console.WriteLine(xText); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } //var xKudzuStream = new Sys.Filesystem.FAT.FatStream(xKudzuFile); //var xKudzuData = new byte[xKudzuFile.Size]; //xKudzuStream.Read(xKudzuData, 0, (int)xKudzuFile.Size); try { //Console.WriteLine(); //Console.WriteLine("FileStream - Root File"); //var xRootFileStream = new System.IO.FileStream(@"c:\Root.txt", System.IO.FileMode.Open); //var xData = new byte[xRootFileStream.Length]; //xRootFileStream.Read(xData, 0, (int)xRootFile.Size); //var xText = Encoding.ASCII.GetString(xData); //Console.WriteLine(xText); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } } } } else { Console.WriteLine("No Block Device Found! "); } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } }
public void Execute(string[] args) { string devn = "/dev/sda"; AtaPio bd = (AtaPio)Devices.getDevice(devn); bd.ReadBlock(0, 1, bw.BaseStream.Data); Console.WriteLine("Grunty OS Fixed disk utility"); Console.WriteLine("Enter command (m for help)"); while (true) { Console.Write("> "); string command = Console.ReadLine(); if (command == "n") { Console.Write("Partition(1-4):"); string part = Console.ReadLine(); Console.Write("Blocks:"); int size = Conversions.StringToInt(Console.ReadLine()); bool greater_than_one = false; int address = 446; byte head_start = 1; ushort start_sect = 1; byte start_cyln = 1; uint rel_sect = 1; if (part == "1") { address = 446; } else if (part == "2") { address = 462; } else if (part == "3") { address = 478; } else if (part == "4") { address = 494; } if (address != 446) { BinaryReader br = new BinaryReader(bw.BaseStream); br.BaseStream.Position = address - 16; br.BaseStream.Position++; br.BaseStream.Position += 3; head_start = br.BaseStream.Read(); byte b1 = br.BaseStream.Read(); byte b2 = br.BaseStream.Read(); ushort us = BitConverter.ToUInt16(new byte[] { b1, b2 }, 0); start_sect = getShort1(us); start_cyln = (byte)getShort2(us); rel_sect = br.ReadUInt32(); greater_than_one = true; } bw.BaseStream.Position = address; bw.Write((byte)0); bw.Write(head_start); bw.Write((ushort)getShort(start_sect, start_cyln)); bw.Write((byte)1); uint c = (uint)(size / 12); if (greater_than_one) { c = (uint)((rel_sect + size) / 12); } bw.Write((byte)(c * 6)); bw.Write((ushort)getShort(12, (byte)c)); bw.Write((uint)rel_sect); bw.Write((uint)size); } else if (command == "a") { Console.Write("Partition(1-4):"); string part = Console.ReadLine(); int address = 462; if (part == "1") { address = 446; } else if (part == "2") { address = 462; } else if (part == "3") { address = 478; } else if (part == "4") { address = 494; } if (bw.BaseStream.Data[address] == 0) { bw.BaseStream.Data[address] = (byte)1; } else { bw.BaseStream.Data[address] = (byte)0; } } else if (command == "t") { Console.Write("Partition(1-4):"); string part = Console.ReadLine(); Console.Write("System Label: "); int t = Conversions.StringToInt(Console.ReadLine()); int address = 462; if (part == "1") { address = 446; } else if (part == "2") { address = 462; } else if (part == "3") { address = 478; } else if (part == "4") { address = 494; } address += 5; bw.BaseStream.Position = address; bw.Write((byte)(uint)t); } else if (command == "q") { break; } else if (command == "w") { Console.WriteLine("Writing to partition table..."); bw.BaseStream.Close(); bd.WriteBlock(0, 1, bw.BaseStream.Data); Console.WriteLine("Changes saved!"); break; } else if (command == "d") { Console.Write("Partition(1-4):"); string part = Console.ReadLine(); int address = 446; if (part == "1") { address = 446; } else if (part == "2") { address = 462; } else if (part == "3") { address = 478; } else if (part == "4") { address = 494; } bw.BaseStream.Position = address; for (int i = 1; i < 16; i++) { bw.Write((byte)0); } } else if (command == "help" || command == "m") { Console.WriteLine(@"Command (m for help): m Command action a toggle a bootable flag d delete a partition m print this menu n add a new partition p print the partition table q quit t change a partition's system id w write table to disk and exit"); } } }