private bool LoadRamHex(string fname, bool blow) { TTransaction m_Xaction = new TTransaction(); list.Clear(); list1.Clear(); string line, sOffset, tmp; int v; FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); while (!sr.EndOfStream) { list.Add(sr.ReadLine()); } sr.Close(); fs.Close(); int Ramsize = 0x4000; // Delete non-data records for (int i = list.Count - 1; i >= 0; i--) { line = (string)list[i]; if (line.Length > 0) { tmp = line.Substring(7, 2); // Get the Record Type into v v = (int)Util.HexToInt(tmp); if (v != 0) { list.Remove(list[i]); // Data records are type == 0 } } } for (int i = 0; i < list.Count; i++) { line = (string)list[i]; // Remove comments v = line.IndexOf("//"); if (v > -1) { line = line.Substring(0, v - 1); } // Build string that just contains the offset followed by the data bytes if (line.Length > 0) { // Get the offset sOffset = line.Substring(3, 4); // Get the string of data chars tmp = line.Substring(1, 2); v = (int)Util.HexToInt(tmp) * 2; string s = line.Substring(9, v); list1.Add(sOffset + s); } } if (blow) { Reset(1); } byte Reqcode = blow ? (byte)0xA0 : (byte)0xA3; ushort windex = 0; int iindex = 0; string Datastring = ""; int nxtoffset = 0; int xferLen = 0; ushort wvalue = 0; foreach (string lines in list1) { line = lines.Substring(0, 4); ushort offset = (ushort)Util.HexToInt(line); int slen = lines.Length; int no_bytes = (slen - 4) / 2; int lastaddr = offset + no_bytes; //if (blow && (offset < Ramsize) && (lastaddr > Ramsize)) // no_bytes = Ramsize - offset; //if (!blow && (offset < Ramsize) && (lastaddr > Ramsize)) //{ // no_bytes = lastaddr - (int)Ramsize; // string s = "xxxx" + lines.Substring(slen - (no_bytes * 2), no_bytes * 2); // list1[iindex] = s; // offset = (ushort)Ramsize; // line = "4000"; //} //if ((blow && (offset < Ramsize)) || (!blow && (offset >= Ramsize))) if ((blow && (lastaddr < Ramsize)) || (!blow && (lastaddr >= Ramsize))) { xferLen += no_bytes; if ((offset == nxtoffset) && (xferLen < 0x1000)) { Datastring += lines.Substring(4, no_bytes * 2); } else { int len = Datastring.Length; if (!len.Equals(0)) { int bufLen = len / 2; byte[] buf = new byte[bufLen]; string d; for (int j = 0; j < bufLen; j++) { d = Datastring.Substring(j * 2, 2); buf[j] = (byte)Util.HexToInt(d); } ControlEndPt.Target = CyConst.TGT_DEVICE; ControlEndPt.ReqType = CyConst.REQ_VENDOR; ControlEndPt.Direction = CyConst.DIR_TO_DEVICE; ControlEndPt.ReqCode = Reqcode; ControlEndPt.Value = wvalue; ControlEndPt.Index = windex; ControlEndPt.Write(ref buf, ref bufLen); if (m_bRecording && (m_script_file_name != null)) { m_Xaction.AltIntfc = m_AltIntfc; m_Xaction.ConfigNum = m_ConfigNum; m_Xaction.IntfcNum = m_IntfcNum; m_Xaction.EndPtAddr = ControlEndPt.Address; m_Xaction.Tag = 0; m_Xaction.bReqType = (byte)(ControlEndPt.Direction | ControlEndPt.ReqType | ControlEndPt.Target); m_Xaction.CtlReqCode = ControlEndPt.ReqCode; m_Xaction.wValue = ControlEndPt.Value; m_Xaction.wIndex = ControlEndPt.Index; m_Xaction.DataLen = (uint)bufLen; m_Xaction.Timeout = ControlEndPt.TimeOut / 1000; m_Xaction.RecordSize = (uint)bufLen + TTransaction.TotalHeaderSize; //Write m_Xaction and buffer m_Xaction.WriteToStream(m_script_file_name); m_Xaction.WriteFromBuffer(m_script_file_name, ref buf, ref bufLen); } } wvalue = (ushort)Util.HexToInt(line); Datastring = lines.Substring(4, no_bytes * 2); xferLen = no_bytes; } nxtoffset = offset + no_bytes; } iindex++; } int len1 = Datastring.Length; if (!len1.Equals(0)) { int bufLen = len1 / 2; byte[] buf1 = new byte[bufLen]; string d; for (int j = 0; j < bufLen; j++) { d = Datastring.Substring(j * 2, 2); buf1[j] = (byte)Util.HexToInt(d); } ControlEndPt.Target = CyConst.TGT_DEVICE; ControlEndPt.ReqType = CyConst.REQ_VENDOR; ControlEndPt.Direction = CyConst.DIR_TO_DEVICE; ControlEndPt.ReqCode = Reqcode; ControlEndPt.Value = wvalue; ControlEndPt.Index = windex; ControlEndPt.Write(ref buf1, ref bufLen); if (m_bRecording && (m_script_file_name != null)) { m_Xaction.AltIntfc = m_AltIntfc; m_Xaction.ConfigNum = m_ConfigNum; m_Xaction.IntfcNum = m_IntfcNum; m_Xaction.EndPtAddr = ControlEndPt.Address; m_Xaction.Tag = 0; m_Xaction.bReqType = (byte)(ControlEndPt.Direction | ControlEndPt.ReqType | ControlEndPt.Target); m_Xaction.CtlReqCode = ControlEndPt.ReqCode; m_Xaction.wValue = ControlEndPt.Value; m_Xaction.wIndex = ControlEndPt.Index; m_Xaction.DataLen = (uint)bufLen; m_Xaction.Timeout = ControlEndPt.TimeOut / 1000; m_Xaction.RecordSize = (uint)bufLen + TTransaction.TotalHeaderSize; //Write m_Xaction and buffer m_Xaction.WriteToStream(m_script_file_name); m_Xaction.WriteFromBuffer(m_script_file_name, ref buf1, ref bufLen); } } if (blow) { Reset(0); } return(true); }
public static bool ParseHexData(ArrayList rawList, byte[] FwBuf, ref ushort FwLen, ref ushort FwOff) { string line, tmp; int v; // Delete non-data records for (int i = rawList.Count - 1; i >= 0; i--) { line = (string)rawList[i]; if (line.Length > 0) { tmp = line.Substring(7, 2); // Get the Record Type into v v = (int)Util.HexToInt(tmp); if (v != 0) { rawList.Remove(rawList[i]); // Data records are type == 0 } } } FwLen = 0; FwOff = _MAX_FW_SIZE; // Initialize the FwImage[] buffer for (int i = 0; i < _MAX_FW_SIZE; i++) { FwBuf[i] = 0xFF; } // Extract the FW data bytes, placing into location of FwImage indicated for (int i = 0; i < rawList.Count; i++) { line = (string)rawList[i]; // Remove comments v = line.IndexOf("//"); if (v > -1) { line = line.Substring(0, v - 1); } // Build string that just contains the offset followed by the data bytes if (line.Length > 0) { // Get the offset string sOffset = line.Substring(3, 4); ushort dx = (ushort)Util.HexToInt(sOffset); if (dx >= _MAX_FW_SIZE) { return(false); } if (dx < FwOff) { FwOff = dx; } // Get the string of data chars tmp = line.Substring(1, 2); v = (int)Util.HexToInt(tmp) * 2; string s = line.Substring(9, v); int bytes = v / 2; for (int b = 0; b < bytes; b++, dx++) { FwBuf[dx] = (byte)Util.HexToInt(s.Substring((b * 2), 2)); } if (dx > FwLen) { FwLen = dx; } } } return(true); }
private void GetDeviceParameters() { int a = Path.IndexOf("usbstor"); int len = Path.IndexOf("#{") - a; string tmp = Path.Substring(a, len).Replace("#", "\\"); string DevKeyString = "SYSTEM\\CurrentControlSet\\Enum\\" + tmp; // Location in XP RegistryKey LocalMachine = Registry.LocalMachine; RegistryKey DevKey = LocalMachine.OpenSubKey(DevKeyString); if (DevKey == null) { DevKeyString = "Enum\\" + tmp; // Location in 98 DevKey = LocalMachine.OpenSubKey(DevKeyString); if (DevKey == null) { return; } } _friendlyName = (string)DevKey.GetValue("FriendlyName"); int x = tmp.LastIndexOf("\\") + 1; len = tmp.Length; _serialNumber = tmp.Substring(x, len - x).ToUpper(); // Many Serial Numbers have '&0' tacked-on at the end x = _serialNumber.LastIndexOf("&"); if (x > 0) { len = _serialNumber.Length; _serialNumber = _serialNumber.Substring(0, x); } int lastAmpersand = _serialNumber.LastIndexOf("&"); string ParentIDPrefix = (lastAmpersand >= 0) ? _serialNumber.Substring(0, lastAmpersand) : ""; DevKey = GetUSBDevKey(ParentIDPrefix); if (DevKey == null) { return; } _manufacturer = (string)DevKey.GetValue("Mfg"); _name = (string)DevKey.GetValue("DeviceDesc"); _product = (string)DevKey.GetValue("LocationInformation"); string[] vidpid = (string[])DevKey.GetValue("HardwareID"); x = vidpid[0].LastIndexOf("Vid_"); if (x == -1) { x = vidpid[0].LastIndexOf("VID_"); } x = x + 4; string sVid = vidpid[0].Substring(x, 4); _vendorID = (ushort)Util.HexToInt(sVid); x = vidpid[0].LastIndexOf("Pid_"); if (x == -1) { x = vidpid[0].LastIndexOf("PID_"); } x = x + 4; string sPid = vidpid[0].Substring(x, 4); _productID = (ushort)Util.HexToInt(sPid); x = vidpid[0].LastIndexOf("Rev_"); if (x == -1) { x = vidpid[0].LastIndexOf("REV_"); } x = x + 4; string sRev = vidpid[0].Substring(x, 4); _bcdUSB = (ushort)Util.HexToInt(sRev); string[] classids = (string[])DevKey.GetValue("CompatibleIDs"); x = classids[0].IndexOf("Class_") + 6; string sClass = classids[0].Substring(x, 2); _devClass = (byte)Util.HexToInt(sClass); x = classids[0].IndexOf("SubClass_") + 9; string sSub = classids[0].Substring(x, 2); _devSubClass = (byte)Util.HexToInt(sSub); x = classids[0].IndexOf("Prot_") + 5; string sProt = classids[0].Substring(x, 2); _devProtocol = (byte)Util.HexToInt(sProt); }