public static Process LoadProcess_FromRawExe(File RawExeFile, bool UserMode) { // - Read in file contents // - Map enough memory for the exe file contents // - Copy the memory over //bool reenable = Scheduler.Enabled; //if (reenable) //{ // Scheduler.Disable(); //} //TODO - Handle case of EXE being bigger than 4KiB? // - Would need to map contiguous (virtual) pages. BasicConsole.WriteLine("Mapping free page for process..."); byte* destMemPtr = (byte*)Hardware.VirtMemManager.MapFreePage( UserMode ? Hardware.VirtMem.VirtMemImpl.PageFlags.None : Hardware.VirtMem.VirtMemImpl.PageFlags.KernelOnly); BasicConsole.WriteLine(((FOS_System.String)"Physical address = ") + (uint)Hardware.VirtMemManager.GetPhysicalAddress(destMemPtr)); BasicConsole.WriteLine(((FOS_System.String)"Virtual address = ") + (uint)destMemPtr); // Add the page to the current processes memory layout // So we can access the memory while we load the process. // Otherwise we will hit a page fault as soon as we try copying the memory // further down. // Note: The page fault will only be hit on starting a second process because // the scheduler doesn't change the context when only one process is running. ProcessManager.CurrentProcess.TheMemoryLayout.AddDataPage( (uint)Hardware.VirtMemManager.GetPhysicalAddress(destMemPtr), (uint)destMemPtr); // We could have been "scheduler interrupted" just after the map but just before the // add data page... ProcessManager.CurrentProcess.LoadMemLayout(); //BasicConsole.WriteLine("Reading file..."); int bytesRead = 0; byte[] readBuffer = new byte[4096]; bytesRead = RawExeFile.GetStream().Read(readBuffer, 0, 4096); //BasicConsole.WriteLine("Copying data..."); Utilities.MemoryUtils.MemCpy_32(destMemPtr, ((byte*)Utilities.ObjectUtilities.GetHandle(readBuffer)) + FOS_System.Array.FieldsBytesSize, (uint)bytesRead); //BasicConsole.WriteLine("Converting destMemPtr..."); ThreadStartMethod mainMethod = (ThreadStartMethod)Utilities.ObjectUtilities.GetObject(destMemPtr); //BasicConsole.WriteLine("Getting process name..."); FOS_System.String name = RawExeFile.Name; // - Create the process //BasicConsole.WriteLine("Creating process..."); Process process = ProcessManager.CreateProcess( mainMethod, name, UserMode); //BasicConsole.WriteLine("Adding process' code page..."); // Add code page to new processes memory layout process.TheMemoryLayout.AddCodePage((uint)Hardware.VirtMemManager.GetPhysicalAddress(destMemPtr), (uint)destMemPtr); //BasicConsole.WriteLine("Removing process' code page from current process..."); //Remove from current processes memory layout ProcessManager.CurrentProcess.TheMemoryLayout.RemovePage((uint)destMemPtr); //if (reenable) //{ // Scheduler.Enable(); //} return process; }
public static ELFSharedObject LoadLibrary_FromELFSO(File ELFSharedObjectFile, ELFProcess theProcess) { //bool reenable = Scheduler.Enabled; //if (reenable) //{ // Scheduler.Disable(); //} ELFSharedObject result = new ELFFile(ELFSharedObjectFile).LoadSharedObject(theProcess); //if (reenable) //{ // Scheduler.Enable(); //} return result; }
public ELFFile(File file) { theFile = file; if (theFile == null) { Console.Default.ErrorColour(); Console.Default.Write("Error constructing ELF file! theFile is null"); BasicConsole.Write("Error constructing ELF file! theFile is null"); if (file == null) { Console.Default.Write(" and file is null"); BasicConsole.Write(" and file is null"); } else { Console.Default.Write(" and file is NOT null"); BasicConsole.Write(" and file is NOT null"); } Console.Default.WriteLine("."); BasicConsole.WriteLine("."); Console.Default.DefaultColour(); ExceptionMethods.Throw(new FOS_System.Exception("Error loading ELF file! Supplied file is null.")); } theStream = new CachedFileStream(theFile.GetStream()); ReadHeader(); if (IsValidFile()) { ReadSectionHeaders(); ReadSegmentHeaders(); } }
public static ELFProcess LoadProcess_FromELFExe(File ELFExeFile, bool UserMode) { //bool reenable = Scheduler.Enabled; //if (reenable) //{ // Scheduler.Disable(); //} ELFProcess result = new ELFFile(ELFExeFile).LoadExecutable(UserMode); //if (reenable) //{ // Scheduler.Enable(); //} return result; }
/// <summary> /// Copies the specified file. /// </summary> /// <param name="srcFile">The file to copy.</param> /// <param name="dst">The path to copy to.</param> private void CopyFile(File srcFile, FOS_System.String dst) { //If source file is null, it means it wasn't found by the caller (or some // other error but we will assume not found since that is the expected use // case from the other overload of CopyFile). if(srcFile == null) { console.WriteLine("Source file not found!"); return; } //Attempt to open the destination file. File dstFile = File.Open(dst); //If opening failed, the file was not found so either the path // was invalid or the file does not currently exist. We assume // the latter. If the path is invalid, it will be caught later. if (dstFile == null) { //console.WriteLine("Creating destination file..."); //If the path is invalid because of path mapping, it will be // caught here. FileSystemMapping mapping = FileSystemManager.GetMapping(dst); if (mapping == null) { console.WriteLine("Destination file system not found!"); return; } //+1 to include the slash in dir name int lastIdx = dst.LastIndexOf(FileSystemManager.PathDelimiter) + 1; FOS_System.String dstDir = dst.Substring(0, lastIdx); FOS_System.String dstName = dst.Substring(lastIdx, dst.length - lastIdx); //console.WriteLine("dstDir: " + dstDir); //console.WriteLine("dstName: " + dstName); //New directory either creates the directory, or returns the existing directory Directory parentDir = NewDirectory(dstDir); //If the parent dir is null, the path must be invalid so we cannot create the // dest file. if (parentDir != null) { dstFile = mapping.TheFileSystem.NewFile(dstName, parentDir); } //If we still failed to create the file, then the path was totally invalid if (dstFile == null) { console.WriteLine("Failed to create destination file!"); return; } console.WriteLine("Destination file created."); } else { console.WriteLine("Destination file already exists."); } //Get full path resolves the path of the file without using short-hands // such as ./ and .. which can be used in the arguments to this method. // So, GetFullPath allows us to do a consistent comparison of the paths. FOS_System.String srcFullPath = srcFile.GetFullPath(); FOS_System.String dstFullPath = dstFile.GetFullPath(); //If we are about to copy a file onto itself, well that wouldn't technically // give us an issue given our copy implementation, but it is pretty pointless. // Also, it would give a more sofisticated copy algorithm (e.g. block copying // for large files) a big problem! if (srcFullPath == dstFullPath) { console.WriteLine("Atempted to copy a file to itself! (" + srcFullPath + ")"); return; } else { console.WriteLine("Copying " + srcFullPath + " to " + dstFullPath); } //Get the streams to read from / write to FOS_System.IO.Streams.FileStream srcStr = srcFile.GetStream(); FOS_System.IO.Streams.FileStream dstStr = dstFile.GetStream(); //Temporary storage. Note: If the file is to big, this will just fail // as there won't be enough heap memory available byte[] data = new byte[(uint)srcFile.TheFileSystem.ThePartition.BlockSize]; //Force stream positions srcStr.Position = 0; dstStr.Position = 0; console.Write("["); int percentile = (int)(uint)FOS_System.Math.Divide(srcFile.Size, 78u); int dist = 0; while ((ulong)srcStr.Position < srcFile.Size) { //Read in source data. srcStr.Read(data, 0, data.Length); //Write out data to destination. dstStr.Write(data, 0, data.Length); dist += data.Length; if (dist >= percentile) { console.Write("."); dist -= percentile; } } console.WriteLine("]"); console.WriteLine("Copied successfully."); }