Ejemplo n.º 1
0
        private void SaharaDownloadProgrammer()
        {
            if (comm.IsOpen)
            {
                string msg1 = string.Format("[{0}]:{1}", comm.serialPort.PortName, "start flash.");
                FlashingDevice.UpdateDeviceStatus(comm.serialPort.PortName, new float?(0.0f), "read hello packet", "flashing", false);
                Log.w(comm.serialPort.PortName, msg1);
                comm.getRecData();
                if (comm.recData.Length == 0)
                {
                    comm.recData = new byte[48];
                }
                sahara_packet       saharaPacket = new sahara_packet();
                sahara_hello_packet stuct1       = (sahara_hello_packet)CommandFormat.BytesToStuct(comm.recData, typeof(sahara_hello_packet));
                stuct1.Reserved = new uint[6];
                sahara_hello_response saharaHelloResponse = new sahara_hello_response();
                saharaHelloResponse.Reserved = new uint[6];
                sahara_switch_Mode_packet  switchModePacket     = new sahara_switch_Mode_packet();
                sahara_readdata_packet     saharaReaddataPacket = new sahara_readdata_packet();
                sahara_64b_readdata_packet obj = new sahara_64b_readdata_packet();
                sahara_end_transfer_packet endTransferPacket  = new sahara_end_transfer_packet();
                sahara_done_response       saharaDoneResponse = new sahara_done_response();
                int num = 20;
                while (num-- > 0 && (int)stuct1.Command != 1)
                {
                    string str1 = "cannot receive hello packet,try agian";
                    Log.w(comm.serialPort.PortName, str1);
                    FlashingDevice.UpdateDeviceStatus(comm.serialPort.PortName, new float?(0.0f), str1, "flashing", false);
                    if (comm.recData.Length == 0)
                    {
                        comm.recData = new byte[48];
                    }
                    stuct1 = (sahara_hello_packet)CommandFormat.BytesToStuct(comm.recData, typeof(sahara_hello_packet));
                    Thread.Sleep(500);
                    if (num == 10)
                    {
                        string str2 = "cannot receive hello packet,try to reset";
                        Log.w(comm.serialPort.PortName, str2);
                        FlashingDevice.UpdateDeviceStatus(comm.serialPort.PortName, new float?(0.0f), str2, "flashing", false);
                        saharaHelloResponse.Command     = 2U;
                        saharaHelloResponse.Length      = (uint)Marshal.SizeOf(saharaHelloResponse);
                        saharaHelloResponse.Version     = 2U;
                        saharaHelloResponse.Version_min = 1U;
                        saharaHelloResponse.Mode        = 3U;
                        comm.WritePort(CommandFormat.StructToBytes(saharaHelloResponse), 0, Marshal.SizeOf(saharaHelloResponse));
                        string str3 = "Switch mode back";
                        Log.w(comm.serialPort.PortName, str3);
                        FlashingDevice.UpdateDeviceStatus(comm.serialPort.PortName, new float?(0.0f), str3, "flashing", false);
                        switchModePacket.Command = 12U;
                        switchModePacket.Length  = (uint)Marshal.SizeOf(switchModePacket);
                        switchModePacket.Mode    = 0U;
                        comm.WritePort(CommandFormat.StructToBytes(switchModePacket), 0, Marshal.SizeOf(switchModePacket));
                        if (comm.recData.Length == 0)
                        {
                            comm.recData = new byte[48];
                        }
                        stuct1 = (sahara_hello_packet)CommandFormat.BytesToStuct(comm.recData, typeof(sahara_hello_packet));
                    }
                }
                if ((int)stuct1.Command == 1)
                {
                    string str = "received hello packet";
                    FlashingDevice.UpdateDeviceStatus(comm.serialPort.PortName, new float?(0.0f), str, "flashing", false);
                    Log.w(comm.serialPort.PortName, str);
                }
                else
                {
                    string str = "cannot receive hello packet";
                    FlashingDevice.UpdateDeviceStatus(comm.serialPort.PortName, new float?(0.0f), str, "flashing", false);
                    Log.w(comm.serialPort.PortName, str);
                }
                saharaHelloResponse.Command     = 2U;
                saharaHelloResponse.Length      = 48U;
                saharaHelloResponse.Version     = 2U;
                saharaHelloResponse.Version_min = 1U;
                byte[] bytes1 = CommandFormat.StructToBytes(saharaHelloResponse);
                comm.WritePort(bytes1, 0, bytes1.Length);
                string[] strArray = FileSearcher.SearchFiles(swPath, SoftwareImage.ProgrammerPattern);
                if (strArray.Length != 1)
                {
                    throw new Exception("can not found programmer file.");
                }
                string   str4     = strArray[0];
                FileInfo fileInfo = new FileInfo(str4);
                if (fileInfo.Name.ToLower().IndexOf("firehose") >= 0)
                {
                    programmerType = Programmer.firehose;
                }
                if (fileInfo.Name.ToLower().IndexOf("ufs") >= 0)
                {
                    storageType = Storage.ufs;
                }
                else if (fileInfo.Name.ToLower().IndexOf("emmc") >= 0)
                {
                    storageType = Storage.emmc;
                }
                comm.intSectorSize = storageType == Storage.ufs ? comm.SECTOR_SIZE_UFS : comm.SECTOR_SIZE_EMMC;
                Log.w(comm.serialPort.PortName, "donwload programmer " + str4);
                FlashingDevice.UpdateDeviceStatus(comm.serialPort.PortName, new float?(0.0f), "download programmer " + str4, "flashing", false);
                FileTransfer fileTransfer = new FileTransfer(comm.serialPort.PortName, str4);
                bool         flag;
                do
                {
                    flag = false;
                    comm.getRecData();
                    byte[] recData = comm.recData;
                    saharaPacket = (sahara_packet)CommandFormat.BytesToStuct(comm.recData, typeof(sahara_packet));
                    switch (saharaPacket.Command)
                    {
                    case 3:
                        sahara_readdata_packet stuct3 = (sahara_readdata_packet)CommandFormat.BytesToStuct(comm.recData, typeof(sahara_readdata_packet));
                        string msg2 = string.Format("sahara read data:imgID {0}, offset {1},length {2}", stuct3.Image_id, stuct3.Offset, stuct3.SLength);
                        fileTransfer.transfer(comm.serialPort, (int)stuct3.Offset, (int)stuct3.SLength);
                        Log.w(comm.serialPort.PortName, msg2);
                        break;

                    case 4:
                        sahara_end_transfer_packet stuct4 = (sahara_end_transfer_packet)CommandFormat.BytesToStuct(comm.recData, typeof(sahara_end_transfer_packet));
                        string msg3 = string.Format("sahara read end  imgID:{0} status:{1}", stuct4.Image_id, stuct4.Status);
                        if ((int)stuct4.Status != 0)
                        {
                            Log.w(comm.serialPort.PortName, string.Format("sahara read end error with status:{0}", stuct4.Status));
                        }
                        flag = true;
                        Log.w(comm.serialPort.PortName, msg3);
                        break;

                    case 18:
                        sahara_64b_readdata_packet stuct5 = (sahara_64b_readdata_packet)CommandFormat.BytesToStuct(comm.recData, typeof(sahara_64b_readdata_packet));
                        string msg4 = string.Format("sahara read 64b data:imgID {0},offset {1},length {2}", stuct5.Image_id, stuct5.Offset, stuct5.SLength);
                        fileTransfer.transfer(comm.serialPort, (int)stuct5.Offset, (int)stuct5.SLength);
                        Log.w(comm.serialPort.PortName, msg4);
                        break;

                    default:
                        Log.w(comm.serialPort.PortName, string.Format("invalid command:{0}", saharaPacket.Command));
                        break;
                    }
                }while (!flag);
                saharaPacket.Command = 5U;
                saharaPacket.Length  = 8U;
                byte[] bytes2 = CommandFormat.StructToBytes(saharaPacket, 8);
                for (int index = 8; index < bytes2.Length; ++index)
                {
                    bytes2[index] = 0;
                }
                comm.WritePort(bytes2, 0, bytes2.Length);
                comm.getRecData();
                if (comm.recData.Length == 0)
                {
                    comm.recData = new byte[48];
                }
                if ((int)((sahara_done_response)CommandFormat.BytesToStuct(comm.recData, typeof(sahara_done_response))).Command == 6)
                {
                    string str1 = string.Format("file {0} transferred successfully", str4);
                    Thread.Sleep(2000);
                    Log.w(comm.serialPort.PortName, str1);
                    FlashingDevice.UpdateDeviceStatus(comm.serialPort.PortName, new float?(1f), str1, "flashing", false);
                }
                else
                {
                    string str1 = "programmer transfer error";
                    Log.w(comm.serialPort.PortName, str1);
                    FlashingDevice.UpdateDeviceStatus(comm.serialPort.PortName, new float?(), str1, "flashing", false);
                }
            }
            else
            {
                Log.w(comm.serialPort.PortName, string.Format("port {0} is not open.", comm.serialPort.PortName));
            }
        }
Ejemplo n.º 2
0
        public void WriteSparseFileToDevice(SerialPortDevice portCnn, string pszPartitionStartSector,
                                            string pszPartitionSectorNumber, string pszImageFile, string pszFileStartSector,
                                            string pszSectorSizeInBytes, string pszPhysicalPartitionNumber)
        {
            var  int32_1 = Convert.ToInt32(pszPartitionStartSector);
            var  int32_2 = Convert.ToInt32(pszPartitionSectorNumber);
            var  int32_3 = Convert.ToInt32(pszFileStartSector);
            long offset1 = 0;
            var  int32_4 = Convert.ToInt32(pszSectorSizeInBytes);

            Convert.ToInt32(pszPhysicalPartitionNumber);
            var sparseImageHeader = new SparseImageHeader();
            var str = "";

            if (int32_3 != 0)
            {
                str = "ERROR_BAD_FORMAT";
                Log.w(portCnn.comm.serialPort.PortName, str);
            }

            if (int32_4 == 0)
            {
                str = "ERROR_BAD_FORMAT";
                Log.w(portCnn.comm.serialPort.PortName, "ERROR_BAD_FORMAT");
            }

            var size1  = Marshal.SizeOf(sparseImageHeader);
            var n      = 0;
            var stuct1 = (SparseImageHeader)CommandFormat.BytesToStuct(GetBytesFromFile(offset1, size1, out n),
                                                                       typeof(SparseImageHeader));
            var offset2 = offset1 + stuct1.uFileHeaderSize;

            if ((int)stuct1.uMagic != -316211398)
            {
                str = "ERROR_BAD_FORMAT";
                Log.w(portCnn.comm.serialPort.PortName, "ERROR_BAD_FORMAT");
            }

            if (stuct1.uMajorVersion != 1)
            {
                str = "ERROR_UNSUPPORTED_TYPE";
                Log.w(portCnn.comm.serialPort.PortName, "ERROR_UNSUPPORTED_TYPE");
            }

            if (stuct1.uBlockSize % int32_4 != 0L)
            {
                str = "ERROR_BAD_FORMAT";
                Log.w(portCnn.comm.serialPort.PortName, "ERROR_BAD_FORMAT");
            }

            if (int32_2 != 0 && stuct1.uBlockSize * stuct1.uTotalBlocks / int32_4 > int32_2)
            {
                str = "ERROR_FILE_TOO_LARGE";
                Log.w(portCnn.comm.serialPort.PortName, "ERROR_FILE_TOO_LARGE");
            }

            if (!string.IsNullOrEmpty(str))
            {
                FlashingDevice.UpdateDeviceStatus(portName, new float?(), str, "error", false);
            }
            else
            {
                for (var index = 0; index < stuct1.uTotalChunks; ++index)
                {
                    Log.w(portCnn.comm.serialPort.PortName,
                          string.Format("total chunks {0}, current chunk {1}", stuct1.uTotalChunks, index));
                    var size2  = Marshal.SizeOf(new SparseChunkHeader());
                    var stuct2 = (SparseChunkHeader)CommandFormat.BytesToStuct(GetBytesFromFile(offset2, size2, out n),
                                                                               typeof(SparseChunkHeader));
                    offset2 += stuct1.uChunkHeaderSize;
                    var num1 = (int)stuct1.uBlockSize * (int)stuct2.uChunkSize;
                    var num2 = num1 / int32_4;
                    switch (stuct2.uChunkType)
                    {
                    case 51905:
                        if (stuct2.uTotalSize != stuct1.uChunkHeaderSize + num1)
                        {
                            Log.w(portCnn.comm.serialPort.PortName, "ERROR_BAD_FORMAT");
                            FlashingDevice.UpdateDeviceStatus(portName, new float?(), "ERROR_BAD_FORMAT", "error",
                                                              false);
                            return;
                        }

                        var strPartitionStartSector  = int32_1.ToString();
                        var strPartitionSectorNumber = num2.ToString();
                        var strFileStartSector       = (offset2 / int32_4).ToString();
                        var strFileSectorOffset      = (offset2 % int32_4).ToString();
                        WriteFile(portCnn, strPartitionStartSector, strPartitionSectorNumber, pszImageFile,
                                  strFileStartSector, strFileSectorOffset, pszSectorSizeInBytes,
                                  pszPhysicalPartitionNumber);
                        offset2 += int32_4 * num2;
                        int32_1 += num2;
                        break;

                    case 51907:
                        if ((int)stuct2.uTotalSize != stuct1.uChunkHeaderSize)
                        {
                            Log.w(portCnn.comm.serialPort.PortName, "ERROR_BAD_FORMAT");
                        }
                        int32_1 += num2;
                        break;

                    default:
                        Log.w(portCnn.comm.serialPort.PortName, "ERROR_UNSUPPORTED_TYPE");
                        break;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        // Token: 0x060000FD RID: 253 RVA: 0x0000DC64 File Offset: 0x0000BE64
        public void WriteSparseFileToDevice(SerialPortDevice portCnn, string pszPartitionStartSector, string pszPartitionSectorNumber, string pszImageFile, string pszFileStartSector, string pszSectorSizeInBytes, string pszPhysicalPartitionNumber, string addtionalFirehose)
        {
            int  num  = Convert.ToInt32(pszPartitionStartSector);
            int  num2 = Convert.ToInt32(pszPartitionSectorNumber);
            int  num3 = Convert.ToInt32(pszFileStartSector);
            long num4 = 0L;
            int  num5 = Convert.ToInt32(pszSectorSizeInBytes);

            Convert.ToInt32(pszPhysicalPartitionNumber);
            SparseImageHeader sparseImageHeader = default(SparseImageHeader);
            string            text = "";

            if (num3 != 0)
            {
                text = "ERROR_BAD_FORMAT";
                Log.w(portCnn.comm.serialPort.PortName, text);
            }
            if (num5 == 0)
            {
                text = "ERROR_BAD_FORMAT";
                Log.w(portCnn.comm.serialPort.PortName, "ERROR_BAD_FORMAT");
            }
            int size = Marshal.SizeOf(sparseImageHeader);
            int num6 = 0;

            byte[] bytesFromFile = this.GetBytesFromFile(num4, size, out num6);
            sparseImageHeader = (SparseImageHeader)CommandFormat.BytesToStuct(bytesFromFile, typeof(SparseImageHeader));
            num4 += (long)((ulong)sparseImageHeader.uFileHeaderSize);
            if (sparseImageHeader.uMagic != 3978755898u)
            {
                text = "ERROR_BAD_FORMAT";
                Log.w(portCnn.comm.serialPort.PortName, string.Format("ERROR_BAD_FORMAT {0}", sparseImageHeader.uMagic.ToString()));
            }
            if (sparseImageHeader.uMajorVersion != 1)
            {
                text = "ERROR_UNSUPPORTED_TYPE";
                Log.w(portCnn.comm.serialPort.PortName, string.Format("ERROR_UNSUPPORTED_TYPE {0}", sparseImageHeader.uMajorVersion.ToString()));
            }
            if ((ulong)sparseImageHeader.uBlockSize % (ulong)((long)num5) != 0UL)
            {
                text = "ERROR_BAD_FORMAT";
                Log.w(portCnn.comm.serialPort.PortName, string.Format("ERROR_BAD_FORMAT {0}", sparseImageHeader.uBlockSize.ToString()));
            }
            if (num2 != 0 && (ulong)(sparseImageHeader.uBlockSize * sparseImageHeader.uTotalBlocks) / (ulong)((long)num5) > (ulong)((long)num2))
            {
                text = "ERROR_FILE_TOO_LARGE";
                Log.w(portCnn.comm.serialPort.PortName, string.Format("ERROR_FILE_TOO_LARGE size {0} ullPartitionSectorNumber {1}", ((long)((ulong)(sparseImageHeader.uBlockSize * sparseImageHeader.uTotalBlocks) / (ulong)((long)num5))).ToString(), num2.ToString()));
            }
            if (!string.IsNullOrEmpty(text))
            {
                throw new Exception(text);
            }
            int num7 = 0;
            int num8 = 1;

            while ((long)num8 <= (long)((ulong)sparseImageHeader.uTotalChunks))
            {
                Log.w(portCnn.comm.serialPort.PortName, string.Format("total chunks {0}, current chunk {1}", sparseImageHeader.uTotalChunks, num8));
                SparseChunkHeader sparseChunkHeader = default(SparseChunkHeader);
                size = Marshal.SizeOf(sparseChunkHeader);
                float num9 = 0f;
                bytesFromFile     = this.GetBytesFromFile(num4, size, out num6, out num9);
                sparseChunkHeader = (SparseChunkHeader)CommandFormat.BytesToStuct(bytesFromFile, typeof(SparseChunkHeader));
                num4 += (long)((ulong)sparseImageHeader.uChunkHeaderSize);
                int num10 = (int)(sparseImageHeader.uBlockSize * sparseChunkHeader.uChunkSize);
                int num11 = num10 / num5;
                if (sparseChunkHeader.uChunkType == 51905)
                {
                    if ((ulong)sparseChunkHeader.uTotalSize != (ulong)((long)((int)sparseImageHeader.uChunkHeaderSize + num10)))
                    {
                        Log.w(portCnn.comm.serialPort.PortName, "ERROR_BAD_FORMAT");
                        throw new Exception("ERROR_BAD_FORMAT");
                    }
                    string strPartitionStartSector  = num.ToString();
                    string strPartitionSectorNumber = num11.ToString();
                    string strFileStartSector       = (num4 / (long)num5).ToString();
                    string strFileSectorOffset      = (num4 % (long)num5).ToString();
                    num7++;
                    bool chkAck = false;
                    int  value  = 0;
                    if (sparseImageHeader.uTotalChunks <= 10u)
                    {
                        if ((long)num8 == (long)((ulong)sparseImageHeader.uTotalChunks))
                        {
                            value  = num7;
                            chkAck = true;
                        }
                    }
                    else
                    {
                        if (num7 % 10 == 0)
                        {
                            value  = 10;
                            chkAck = true;
                        }
                        if ((long)num8 == (long)((ulong)sparseImageHeader.uTotalChunks))
                        {
                            value  = num7 % 10;
                            chkAck = true;
                        }
                    }
                    this.WriteFile(portCnn, strPartitionStartSector, strPartitionSectorNumber, pszImageFile, strFileStartSector, strFileSectorOffset, pszSectorSizeInBytes, pszPhysicalPartitionNumber, addtionalFirehose, chkAck, new int?(value));
                    num4 += (long)(num5 * num11);
                    num  += num11;
                }
                else if (sparseChunkHeader.uChunkType == 51907)
                {
                    if (sparseChunkHeader.uTotalSize != (uint)sparseImageHeader.uChunkHeaderSize)
                    {
                        Log.w(portCnn.comm.serialPort.PortName, "ERROR_BAD_FORMAT");
                    }
                    num += num11;
                    if ((long)num8 == (long)((ulong)sparseImageHeader.uTotalChunks))
                    {
                        int num12 = num7 % 10;
                        if (num12 > 0)
                        {
                            string message = null;
                            if (!portCnn.comm.chkRspAck(out message, num12))
                            {
                                throw new Exception(message);
                            }
                        }
                    }
                }
                else
                {
                    Log.w(portCnn.comm.serialPort.PortName, string.Format("ERROR_UNSUPPORTED_TYPE {0}", sparseChunkHeader.uChunkType.ToString()));
                }
                num8++;
            }
        }