Esempio n. 1
0
            public virtual void handle_table_entry(UInt32 entry)
            {
                // Nonzero entries can still be disabled, so check the present bit before handling.
                if ((entry & ROM_TABLE_ENTRY_PRESENT_MASK) == 0)
                {
                    return;
                }
                // Get the component's top 4k address.
                var offset = entry & ROM_TABLE_ADDR_OFFSET_MASK;

                if ((entry & ROM_TABLE_ADDR_OFFSET_NEG_MASK) != 0)
                {
                    offset = ~offset; // invert32(offset);
                }
                var address = this.address + offset;
                // Create component instance.
                var cmp = new CoreSightComponent(this.ap, address);

                cmp.read_id_registers();
                Trace.TraceInformation("[{0}]{1}", this.components.Count, cmp.ToString());
                // Recurse into child ROM tables.
                if (cmp.is_rom_table)
                {
                    cmp = new ROMTable(this.ap, address, parent_table: this);
                    ((ROMTable)cmp).init();
                }
                this.components.Add(cmp);
            }
Esempio n. 2
0
    static string GetStringNumBytes(string instring, int pos, int bytelen, ROMTable rt)
    {
        List <int> handled   = new List <int>();
        string     outstring = "";
        int        totallen  = 0;

        for (int c = pos; c < instring.Length; c++)
        {
            if (!(handled.Contains(c)))
            {
                int maxlen = 10;//each string value can be a max of 10 characters long-- otherwise we shan't be looking for it
                if (((pos + (bytelen * 10)) - c) < 10)
                {
                    maxlen = ((pos + (bytelen * 10)) - c);
                }
                if ((c + maxlen > instring.Length))
                {
                    maxlen = instring.Length - c;
                }

                string[] sc = new string[maxlen];
                for (int p = 0; p < maxlen; p++) //look up to 10 characters ahead
                {
                    if (p > 0)
                    {
                        sc[p] = sc[p - 1];
                    }
                    sc[p] += instring[c + p].ToString();
                }

                bool found = false;
                //now check from longest to shortest for a match...
                for (int p = (maxlen - 1); p > -1; p--)
                {
                    if (!found && rt.CharMap.ContainsKey(sc[p]))
                    {
                        found      = true;
                        totallen  += rt.CharMap[sc[p]].ByteLength;
                        outstring += sc[p];
                        //for (int b = 0; b < rt.CharMap[sc[p]].HexValue.Length; b += 2)
                        //{
                        //    //string val = rt.CharMap[sc[p]].HexValue.Substring(b, 2);
                        //    //outfile.Add(byte.Parse(val, NumberStyles.HexNumber));
                        //}
                    }
                    if (found)
                    {
                        handled.Add(c + p);
                    }
                }
            }
            if (totallen >= bytelen)
            {
                break;
            }
        }
        //outstring.Substring(b, brk) + "\r\n"
        return(outstring + "\r\n");
    }
Esempio n. 3
0
 public ROMTable(MEM_AP ap, UInt32?top_addr = null, ROMTable parent_table = null)
     : base(ap, top_addr ?? ap.rom_addr)
 {
     // // If no table address is provided, use the root ROM table for the AP.
     // if (top_addr == null)
     // {
     //     top_addr = ap.rom_addr;
     // }
     // super(ROMTable, this).@__init__(ap, top_addr);
     this.parent     = parent_table;
     this.number     = this.parent != null ? this.parent.number + 1 : 0;
     this.entry_size = 0;
     this.components = new List <CoreSightComponent>();
 }
Esempio n. 4
0
    public Dictionary <string, ROMChar> CharMap = new Dictionary <string, ROMChar>(); //indexed by character

    public static ROMTable LoadTable(string file)
    {
        ROMTable rt = new ROMTable();

        int      errCount = 0;
        Encoding enc      = TextFileEncodingDetector.DetectTextFileEncoding(file);

        if (enc == null)
        {
            enc = ASCIIEncoding.ASCII;
        }
        using (StreamReader sr = new StreamReader(file, enc))
        {
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                try
                {
                    if (str.Split('=')[0].Contains("**"))
                    {//if other similar mappings are well defined, they should be before this definition
                        for (int d = 0; d < 256; d++)
                        {
                            if (str.Split('=')[0].Contains("%%"))
                            {
                                for (int e = 0; e < 256; e++)
                                {
                                    ROMTable.ROMChar chr = new ROMTable.ROMChar();
                                    chr.AsciiUnicode = str.Split('=')[1].Replace("**", d.ToString("X2")).Replace("%%", e.ToString("X2"));
                                    chr.HexValue     = str.Split('=')[0].Replace("**", d.ToString("X2")).Replace("%%", e.ToString("X2"));
                                    chr.DecValue     = new HexWord().StringToDec(chr.HexValue);
                                    chr.ByteLength   = (chr.HexValue.Length / 2);//get the number of bytes...
                                    if (!rt.HexMap.ContainsKey(chr.HexValue))
                                    {
                                        rt.HexMap.Add(chr.HexValue, chr);
                                    }
                                    string chrprep = chr.AsciiUnicode.Replace("\\", "");
                                    if (!rt.CharMap.ContainsKey(chrprep))
                                    {
                                        rt.CharMap.Add(chrprep, chr);
                                    }
                                }
                            }
                            else
                            {
                                ROMTable.ROMChar chr = new ROMTable.ROMChar();
                                chr.AsciiUnicode = str.Split('=')[1].Replace("**", d.ToString("X2"));
                                chr.HexValue     = str.Split('=')[0].Replace("**", d.ToString("X2"));
                                chr.DecValue     = new HexWord().StringToDec(chr.HexValue);
                                chr.ByteLength   = (chr.HexValue.Length / 2);//get the number of bytes...
                                if (!rt.HexMap.ContainsKey(chr.HexValue))
                                {
                                    rt.HexMap.Add(chr.HexValue, chr);
                                }
                                string chrprep = chr.AsciiUnicode.Replace("\\", "");
                                if (!rt.CharMap.ContainsKey(chrprep))
                                {
                                    rt.CharMap.Add(chrprep, chr);
                                }
                            }
                        }
                    }
                    else
                    {
                        ROMTable.ROMChar chr = new ROMTable.ROMChar();
                        chr.AsciiUnicode = str.Split('=')[1];
                        chr.HexValue     = str.Split('=')[0];
                        chr.DecValue     = new HexWord().StringToDec(chr.HexValue);
                        chr.ByteLength   = (chr.HexValue.Length / 2);//get the number of bytes...
                        if (!rt.HexMap.ContainsKey(chr.HexValue))
                        {
                            rt.HexMap.Add(chr.HexValue, chr);
                        }
                        string chrprep = chr.AsciiUnicode.Replace("\\", "");
                        if (!rt.CharMap.ContainsKey(chrprep))
                        {
                            rt.CharMap.Add(chrprep, chr);
                        }
                    }
                }
                catch { errCount++; }
            }
        }
        return(rt);
    }
Esempio n. 5
0
    static string GetStringNumBytes(string instring, int pos, int bytelen, ROMTable rt)
    {
        List<int> handled = new List<int>();
        string outstring = "";
        int totallen = 0;

        for (int c = pos; c < instring.Length; c++)
        {
            if (!(handled.Contains(c)))
            {
                int maxlen = 10;//each string value can be a max of 10 characters long-- otherwise we shan't be looking for it
                if (((pos + (bytelen * 10)) - c) < 10)
                {
                    maxlen = ((pos + (bytelen * 10)) - c);
                }
                if ((c + maxlen > instring.Length))
                {
                    maxlen = instring.Length - c;
                }

                string[] sc = new string[maxlen];
                for (int p = 0; p < maxlen; p++) //look up to 10 characters ahead
                {
                    if (p > 0)
                    {
                        sc[p] = sc[p - 1];
                    }
                    sc[p] += instring[c + p].ToString();
                }

                bool found = false;
                //now check from longest to shortest for a match...
                for (int p = (maxlen - 1); p > -1; p--)
                {
                    if (!found && rt.CharMap.ContainsKey(sc[p]))
                    {
                        found = true;
                        totallen += rt.CharMap[sc[p]].ByteLength;
                        outstring += sc[p];
                        //for (int b = 0; b < rt.CharMap[sc[p]].HexValue.Length; b += 2)
                        //{
                        //    //string val = rt.CharMap[sc[p]].HexValue.Substring(b, 2);
                        //    //outfile.Add(byte.Parse(val, NumberStyles.HexNumber));
                        //}
                    }
                    if (found)
                    {
                        handled.Add(c + p);
                    }
                }
            }
            if (totallen >= bytelen)
            {
                break;
            }
        }
        //outstring.Substring(b, brk) + "\r\n"
        return outstring + "\r\n";
    }
Esempio n. 6
0
    public Dictionary<string, ROMChar> HexMap = new Dictionary<string, ROMChar>(); //indexed by hex

    #endregion Fields

    #region Methods

    public static ROMTable LoadTable(string file)
    {
        ROMTable rt = new ROMTable();

        int errCount = 0;
        Encoding enc = TextFileEncodingDetector.DetectTextFileEncoding(file);
        if (enc == null)
        {
            enc = ASCIIEncoding.ASCII;
        }
        using (StreamReader sr = new StreamReader(file, enc))
        {
            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                try
                {
                    if (str.Split('=')[0].Contains("**"))
                    {//if other similar mappings are well defined, they should be before this definition
                        for (int d = 0; d < 256; d++)
                        {
                            if (str.Split('=')[0].Contains("%%"))
                            {
                                for (int e = 0; e < 256; e++)
                                {
                                    ROMTable.ROMChar chr = new ROMTable.ROMChar();
                                    chr.AsciiUnicode = str.Split('=')[1].Replace("**", d.ToString("X2")).Replace("%%", e.ToString("X2"));
                                    chr.HexValue = str.Split('=')[0].Replace("**", d.ToString("X2")).Replace("%%", e.ToString("X2"));
                                    chr.DecValue = new HexWord().StringToDec(chr.HexValue);
                                    chr.ByteLength = (chr.HexValue.Length / 2);//get the number of bytes...
                                    if (!rt.HexMap.ContainsKey(chr.HexValue))
                                    {
                                        rt.HexMap.Add(chr.HexValue, chr);
                                    }
                                    string chrprep = chr.AsciiUnicode.Replace("\\", "");
                                    if (!rt.CharMap.ContainsKey(chrprep))
                                    {
                                        rt.CharMap.Add(chrprep, chr);
                                    }
                                }
                            }
                            else
                            {
                                ROMTable.ROMChar chr = new ROMTable.ROMChar();
                                chr.AsciiUnicode = str.Split('=')[1].Replace("**", d.ToString("X2"));
                                chr.HexValue = str.Split('=')[0].Replace("**", d.ToString("X2"));
                                chr.DecValue = new HexWord().StringToDec(chr.HexValue);
                                chr.ByteLength = (chr.HexValue.Length / 2);//get the number of bytes...
                                if (!rt.HexMap.ContainsKey(chr.HexValue))
                                {
                                    rt.HexMap.Add(chr.HexValue, chr);
                                }
                                string chrprep = chr.AsciiUnicode.Replace("\\", "");
                                if (!rt.CharMap.ContainsKey(chrprep))
                                {
                                    rt.CharMap.Add(chrprep, chr);
                                }
                            }
                        }
                    }
                    else
                    {
                        ROMTable.ROMChar chr = new ROMTable.ROMChar();
                        chr.AsciiUnicode = str.Split('=')[1];
                        chr.HexValue = str.Split('=')[0];
                        chr.DecValue = new HexWord().StringToDec(chr.HexValue);
                        chr.ByteLength = (chr.HexValue.Length / 2);//get the number of bytes...
                        if (!rt.HexMap.ContainsKey(chr.HexValue))
                        {
                            rt.HexMap.Add(chr.HexValue, chr);
                        }
                        string chrprep = chr.AsciiUnicode.Replace("\\", "");
                        if (!rt.CharMap.ContainsKey(chrprep))
                        {
                            rt.CharMap.Add(chrprep, chr);
                        }
                    }
                }
                catch { errCount++; }
            }
        }
        return rt;
    }
Esempio n. 7
0
    public static void DumpScript(string input, string table, string output, int brk)
    {
        Console.WriteLine("Reading Table file...");
        ROMTable rt = ROMTable.LoadTable(table);

        Console.WriteLine("Reading input file...");
        byte[]     myfile    = File.ReadAllBytes(input);
        string     outstring = "";
        List <int> handled   = new List <int>();

        Console.WriteLine("Evaluating data...");
        int left = Console.CursorLeft;
        int top  = Console.CursorTop;

        for (int c = 0; c < myfile.Length; c++)
        {
            decimal per = ((decimal)(c + 1) / (decimal)(myfile.Length)) * (100);
            Console.SetCursorPosition(left, top);
            Console.Write(((int)per).ToString() + "%");
            if (!(handled.Contains(c)))
            {
                string hx3   = ((c + 2) < myfile.Length) ? myfile[c].ToString("X2") + myfile[c + 1].ToString("X2") + myfile[c + 2].ToString("X2") : "@@";
                string hx2   = ((c + 1) < myfile.Length) ? myfile[c].ToString("X2") + myfile[c + 1].ToString("X2") : "@@";
                string hx1   = myfile[c].ToString("X2");
                string value = "";
                if (rt.HexMap.ContainsKey(hx3))
                {
                    value = rt.HexMap[hx3].AsciiUnicode;
                    handled.AddRange(new int[] { c, c + 1, c + 2 });
                }
                else if (rt.HexMap.ContainsKey(hx2))
                {
                    value = rt.HexMap[hx2].AsciiUnicode;
                    handled.AddRange(new int[] { c + 1, c });
                }
                else if (rt.HexMap.ContainsKey(hx1))
                {
                    value = rt.HexMap[hx1].AsciiUnicode;
                    handled.Add(c);
                }
                else
                {//not in table... dump hex code
                    value = "[" + hx1 + "]";
                    handled.Add(c);
                }
                outstring += (value.Contains(']')) ? value.Replace("\\", "\r\n") : value; //supports new line triggers
            }
        }
        Console.SetCursorPosition(left, top);
        Console.Write("Process Complete!!\r\n");
        Console.WriteLine("Writing Output file...");
        if (brk > 0)
        {//line-break every so many characters
            int lastslen = 0;
            using (StreamWriter sr = new StreamWriter(output))
            {
                for (int b = 0; b < outstring.Length; b += lastslen)
                {
                    string line = GetStringNumBytes(outstring, b, brk, rt);
                    lastslen = (line.Length - 2); //subtract 2 to get the length without the newline flags
                    sr.Write(line);
                }
            }
        }
        else
        {
            File.WriteAllText(output, outstring);
        }
        Console.WriteLine("Done!");
    }
Esempio n. 8
0
    public static string BuildScriptThread(string input, string output, string table)//return binary script path...
    {
        Final = new List <WorkerData>();
        Console.WriteLine("Started Binary Script File Build...");
        if (input == "" || table == "")
        {
            Console.WriteLine("Invalid or missing arguments. Failed.");
            return("");
        }
        else if (output == "")
        {
            //generate output name based on the input
            output = Path.GetFileNameWithoutExtension(input);//.Split('.')[0];
        }
        else
        {
            output = Path.GetFullPath(output);
            if (!File.Exists(output))
            {
                output = output.Split('.')[0] + "_" + DateTime.Now.ToString("yyyyMMdd") + ".bin";
            }
        }

        Console.WriteLine("Reading Table file...");
        ROMTable rt = ROMTable.LoadTable(table);

        Console.WriteLine("Reading Script file...");
        string        script    = File.ReadAllText(input).Replace("\r\n", "");
        List <byte>   outfile   = new List <byte>();
        List <byte[]> splitdata = new List <byte[]>();


        List <int> handled = new List <int>();

        Console.WriteLine("Evaluating data...");
        int left = Console.CursorLeft;
        int top  = Console.CursorTop;

        List <int> ControlPos = new List <int>();

        int lastpos = 0;

        //find each index of a control character...
        for (int c = 0; c < script.Length; c = lastpos + 3)
        {
            decimal per = ((decimal)(c + 1) / (decimal)(script.Length)) * (100);
            Console.SetCursorPosition(left, top);
            Console.Write(((int)per).ToString() + "%");

            lastpos = script.IndexOf("[en", c);
            if (!(lastpos == -1))
            {
                ControlPos.Add(lastpos);
            }
            else
            {
                break;
            }
        }

        //make background workers...
        BackgroundWorker w1 = new BackgroundWorker();
        BackgroundWorker w2 = new BackgroundWorker();
        BackgroundWorker w3 = new BackgroundWorker();
        BackgroundWorker w4 = new BackgroundWorker();
        int piece           = ControlPos.Count / 4; // make 4 worker threads

        if (piece < 4)
        {//then just do a single worker...
            WorkerData d1 = new WorkerData();
            d1.piececount = 1;
            d1.indata     = script;
            d1.total      = script.Length;
            d1.pos        = 0;
            d1.rt         = rt;

            w1.DoWork             += new DoWorkEventHandler(ScriptWorker_DoWork);
            w1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ScriptWorker_RunWorkerCompleted);
            w1.RunWorkerAsync(d1);

            Console.SetCursorPosition(left, top);
            Console.Write(("100").ToString() + "%");
        }
        else
        {
            WorkerData d1 = new WorkerData();
            d1.piececount = 1;
            d1.indata     = script.Substring(0, ControlPos[piece]);
            d1.total      = piece;
            d1.pos        = 0;
            d1.rt         = rt;
            int        nextpiece = ControlPos[piece * 2] - ControlPos[piece];
            WorkerData d2        = new WorkerData();
            d2.piececount = 2;
            d2.indata     = script.Substring(ControlPos[piece], nextpiece);
            d2.total      = piece;
            d2.pos        = 0;
            d2.rt         = rt;
            nextpiece     = ControlPos[piece * 3] - ControlPos[piece * 2];
            WorkerData d3 = new WorkerData();
            d3.piececount = 3;
            d3.indata     = script.Substring(ControlPos[piece * 2], nextpiece);
            d3.total      = piece;
            d3.pos        = 0;
            d3.rt         = rt;
            int        lastpiece = script.Length - (ControlPos[piece * 3]);
            WorkerData d4        = new WorkerData();
            d4.piececount = 4;
            d4.indata     = script.Substring(ControlPos[piece * 3], lastpiece);
            d4.total      = lastpiece;
            d4.pos        = 0;
            d4.rt         = rt;

            w1.DoWork             += new DoWorkEventHandler(ScriptWorker_DoWork);
            w1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ScriptWorker_RunWorkerCompleted);
            w1.RunWorkerAsync(d1);
            w2.DoWork             += new DoWorkEventHandler(ScriptWorker_DoWork);
            w2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ScriptWorker_RunWorkerCompleted);
            w2.RunWorkerAsync(d2);
            w3.DoWork             += new DoWorkEventHandler(ScriptWorker_DoWork);
            w3.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ScriptWorker_RunWorkerCompleted);
            w3.RunWorkerAsync(d3);
            w4.DoWork             += new DoWorkEventHandler(ScriptWorker_DoWork);
            w4.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ScriptWorker_RunWorkerCompleted);
            w4.RunWorkerAsync(d4);
            Console.SetCursorPosition(left, top);
            Console.Write(("100").ToString() + "%");
        }

        Console.WriteLine("\r\nWaiting for build process to complete...");
        left = Console.CursorLeft;
        top  = Console.CursorTop;
        int permod = 0;

        while (w1.IsBusy || w2.IsBusy || w3.IsBusy || w4.IsBusy)
        {
            int percent = ((w1.IsBusy) ? 0 : 25) + ((w2.IsBusy) ? 0 : 25) + ((w3.IsBusy) ? 0 : 25) + ((w4.IsBusy) ? 0 : 25) + permod;
            Console.SetCursorPosition(left, top);
            Console.Write("~" + (percent).ToString() + "%");
            permod++;
            if (permod > (100 - percent))
            {
                permod = (100 - percent);
            }
            System.Threading.Thread.Sleep(100);
        }
        Console.SetCursorPosition(left, top);
        Console.Write(("100").ToString() + "%");


        Console.WriteLine("\r\nWriting binary script file...");
        outfile.Clear();
        if (Final.Count > 0)
        {
            for (int c = 1; c < 5; c++)
            {
                foreach (WorkerData wd in Final)
                {
                    if (wd.piececount == c)
                    {
                        outfile.AddRange(wd.outfile);
                        break;
                    }
                }
            }
            if (File.Exists(output))
            {
                File.Delete(output);
            }
            using (FileStream fs = new FileStream(output, FileMode.Append))
            {
                fs.Write(outfile.ToArray(), 0, outfile.Count);
                outfile.Clear();
            }
            Console.Write("File saved: " + output + "\r\n");
        }

        //Console.SetCursorPosition(left, top);
        Console.Write("Process Complete!!\r\n");

        Console.WriteLine("Done!");
        return(output);
    }
Esempio n. 9
0
    public static string BuildScript(string input, string output, string table)//return binary script path...
    {
        Console.WriteLine("Started Binary Script File Build...");
        if (input == "" || table == "")
        {
            Console.WriteLine("Invalid or missing arguments. Failed.");
            return("");
        }
        else if (output == "")
        {
            //generate output name based on the input
            output = input.Split('.')[0];
        }
        output = output.Split('.')[0] + "_" + DateTime.Now.ToString("yyyyMMdd") + ".bin";

        Console.WriteLine("Reading Table file...");
        ROMTable rt = ROMTable.LoadTable(table);

        Console.WriteLine("Reading Script file...");
        string        script    = File.ReadAllText(input).Replace("\r\n", "");
        List <byte>   outfile   = new List <byte>();
        List <byte[]> splitdata = new List <byte[]>();


        List <int> handled = new List <int>();

        Console.WriteLine("Evaluating data...");
        int left = Console.CursorLeft;
        int top  = Console.CursorTop;

        int offset = 0;

        for (int c = 0; c < script.Length; c++)
        {
            decimal per = ((decimal)(c + 1) / (decimal)(script.Length)) * (100);
            Console.SetCursorPosition(left, top);
            Console.Write(((int)per).ToString() + "%");
            if (!(handled.Contains(c)))
            {
                int maxlen = 10;
                if ((script.Length - c) < 10)
                {
                    maxlen = (script.Length - c);
                }

                string[] sc = new string[maxlen];
                for (int p = 0; p < maxlen; p++) //look up to 10 characters ahead
                {
                    if (p > 0)
                    {
                        sc[p] = sc[p - 1];
                    }
                    sc[p] += script[c + p].ToString();
                }

                bool found = false;
                //now check from longest to shortest for a match...
                for (int p = (maxlen - 1); p > -1; p--)
                {
                    if (!found && rt.CharMap.ContainsKey(sc[p]))
                    {
                        found = true;
                        for (int b = 0; b < rt.CharMap[sc[p]].HexValue.Length; b += 2)
                        {
                            string val = rt.CharMap[sc[p]].HexValue.Substring(b, 2);
                            outfile.Add(byte.Parse(val, NumberStyles.HexNumber));
                        }
                    }
                    if (found)
                    {
                        handled.Add(c + p);
                    }
                }
            }
            if (outfile.Count > 50000)
            {
                using (FileStream fs = new FileStream(output, FileMode.Append))
                {
                    fs.Seek(offset, SeekOrigin.Begin);
                    fs.Write(outfile.ToArray(), 0, outfile.Count);
                    offset += outfile.Count;
                    outfile.Clear();
                }
            }
        }
        if (outfile.Count > 0)
        {
            using (FileStream fs = new FileStream(output, FileMode.Append))
            {
                fs.Seek(offset, SeekOrigin.Begin);
                fs.Write(outfile.ToArray(), 0, outfile.Count);
                offset += outfile.Count;
                outfile.Clear();
            }
        }

        Console.SetCursorPosition(left, top);
        Console.Write("Process Complete!!\r\n");

        Console.WriteLine("Done!");
        return(output);
    }