Esempio n. 1
0
        private void WriteElfFileToMemory(FileStream strm, ELF elfHeader, byte[] data)
        {
            Trace.WriteLine("Loader: Reading ELF File...");
            strm.Seek(elfHeader.e_phoff, SeekOrigin.Begin);       // seek to the program header offset
            data = new byte[elfHeader.e_phentsize];               // allocate byte array the size of the program header
            strm.Read(data, 0, (int)elfHeader.e_phentsize);       // <data> contains program header

            int  p_offset = (int)BitConverter.ToUInt16(data, 4);  // read start of segment
            int  p_filesz = (int)BitConverter.ToUInt16(data, 16); // read file size
            uint p_vaddr  = BitConverter.ToUInt16(data, 8);       // read storage address

            Trace.WriteLine("Loader: Reading program header...");
            strm.Seek(p_offset, SeekOrigin.Begin);   // seek to beginning of program segment
            byte[] programData = new byte[p_filesz]; // create holder for program data

            Trace.WriteLine("Loader: Program header offset: " + p_offset);
            Trace.WriteLine("Loader: Program file size: " + p_filesz);

            Trace.WriteLine("Loader: Writing program to RAM...");
            strm.Read(programData, 0, p_filesz); // read program into <programData>

            for (uint j = 0; j < programData.Length; j++)
            {
                Computer.GetCPU().progRAM.WriteByte(p_vaddr + j, programData[j]);
            }

            elfHeader.e_phoff += elfHeader.e_phentsize; // move to next program header

            Trace.WriteLine("Loader: Program written to RAM successfully!");
            Trace.WriteLine(""); // blank line for file readability
        }
Esempio n. 2
0
        //--------------------------------------------------------------
        // Purpose: Reads ELF files into simulated ram after validating their ELFness
        // Returns: bool value true if loading completed successfully
        //--------------------------------------------------------------
        public bool ReadELF(string ELFpath)
        {
            bool success = false;

            try
            {
                Trace.WriteLine("Loader: Opening file " + ELFpath + "...");

                using (FileStream strm = new FileStream(ELFpath, FileMode.Open))
                {
                    ELF    elfHeader = new ELF();
                    byte[] data      = new byte[Marshal.SizeOf(elfHeader)];

                    // read ELF header data
                    strm.Read(data, 0, data.Length);
                    // convert to struct
                    elfHeader = ByteArrayToStructure <ELF>(data);

                    Trace.WriteLine("Loader: Checking if valid ELF file...");

                    // check to ensure file is an ELF file
                    if (!(data[0] == '\x7f' &&
                          data[1] == 'E' &&
                          data[2] == 'L' &&
                          data[3] == 'F'))
                    {
                        throw new InvalidDataException();
                    }

                    Trace.WriteLine("Loader: File check complete!");
                    Trace.WriteLine("Loader: Number of segments: " + elfHeader.e_phnum);
                    Trace.WriteLine(""); // blank line for file readability

                    registers.WriteWord((uint)regs.PC, elfHeader.e_entry);
                    registers.WriteWord((uint)regs.SP, 0x00007000);

                    // Read first program header entry
                    for (int i = 0; i < elfHeader.e_phnum; i++)
                    {
                        WriteElfFileToMemory(strm, elfHeader, data);
                    }

                    success = true;
                }
            }
            catch (InvalidDataException)
            {
                Console.WriteLine("Loader: The file provided is not an ELF file. Please try again.");
                Trace.WriteLine("Loader: The file provided is not an ELF file. Please try again.");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Loader: Could not locate file " + ELFpath + ". Please try again.");
                Trace.WriteLine("Loader: Could not locate file " + ELFpath + ". Please try again.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Loader: Unknown error. " + ex.ToString());
                Trace.WriteLine("Loader: Unknown error. " + ex.ToString());
            }

            return(success);
        }