Ejemplo n.º 1
0
        public void WriteFlash(UInt32 address, byte[] buffer, OnFelProgress callback = null)
        {
            int length = buffer.Length;
            int pos    = 0;

            if ((address % sector_size) != 0)
            {
                throw new FelException(string.Format("Invalid address to flash: 0x{0:X8}", address));
            }
            if ((length % sector_size) != 0)
            {
                throw new FelException(string.Format("Invalid length to flash: 0x{0:X8}", length));
            }
            byte[] newBuf;
            while ((length / sector_size) > flash_mem_size)
            {
                var sectors = (length / sector_size) - flash_mem_size;
                newBuf = new byte[sectors * sector_size];
                Array.Copy(buffer, pos, newBuf, 0, newBuf.Length);
                WriteFlash(address, newBuf, callback);
                address += (UInt32)newBuf.Length;
                length  -= newBuf.Length;
                pos     += newBuf.Length;
            }
            newBuf = new byte[length - pos];
            Array.Copy(buffer, pos, newBuf, 0, newBuf.Length);
            WriteMemory(flash_mem_base, newBuf, callback);
            var command = string.Format("sunxi_flash phy_write {0:x} {1:x} {2:x};{3}", flash_mem_base, address / sector_size, length / sector_size, fastboot);

            RunUbootCmd(command, false, callback);
        }
Ejemplo n.º 2
0
        private byte[] ReadMemory(UInt32 address, UInt32 length, OnFelProgress callback = null)
        {
            if (address >= dram_base)
            {
                InitDram();
            }

            length = (length + 3) & ~((UInt32)3);

            var result = new List <byte>();

            while (length > 0)
            {
                if (callback != null)
                {
                    callback(CurrentAction.ReadingMemory, null);
                }
                var l = Math.Min(length, MaxBulkSize);
                FelRequest(AWFELStandardRequest.RequestType.FEL_UPLOAD, address, l);
                var r = FelRead((UInt32)l);
                result.AddRange(r);
                var status = new AWFELStatusResponse(FelRead(8));
                if (status.State != 0)
                {
                    throw new FelException("FEL read error");
                }
                length  -= l;
                address += l;
            }
            return(result.ToArray());
        }
Ejemplo n.º 3
0
        public void WriteFlash(UInt32 address, byte[] buffer, OnFelProgress callback = null)
        {
            var  length = (uint)buffer.Length;
            uint pos    = 0;

            if ((address % sector_size) != 0)
            {
                throw new FelException(string.Format("Invalid flash address : 0x{0:X8}", address));
            }
            if ((length % sector_size) != 0)
            {
                throw new FelException(string.Format("Invalid flash length: 0x{0:X8}", length));
            }
            while (length > 0)
            {
                var wrLength = Math.Min(length, transfer_max_size / 8);
                var newBuf   = new byte[wrLength];
                Array.Copy(buffer, pos, newBuf, 0, wrLength);
                WriteMemory(transfer_base_m, newBuf, callback);
                var command = string.Format("sunxi_flash phy_write {0:x} {1:x} {2:x};{3}", transfer_base_m, address / sector_size, (int)Math.Floor((double)wrLength / (double)sector_size), fastboot);
                RunUbootCmd(command, false, callback);
                pos     += (uint)wrLength;
                address += (uint)wrLength;
                length  -= (uint)wrLength;
            }
        }
Ejemplo n.º 4
0
        public byte[] ReadFlash(UInt32 address, UInt32 length, OnFelProgress callback = null)
        {
            var    result = new List <byte>();
            string command;

            if ((address % sector_size) != 0)
            {
                throw new FelException(string.Format("Invalid flash address : 0x{0:X8}", address));
            }
            if ((length % sector_size) != 0)
            {
                throw new FelException(string.Format("Invalid flash length: 0x{0:X8}", length));
            }
            while (length > 0)
            {
                var reqLen = Math.Min(length, transfer_max_size);
                command = string.Format("sunxi_flash phy_read {0:x} {1:x} {2:x};{3}", transfer_base_m, address / sector_size, (int)Math.Floor((double)reqLen / (double)sector_size), fastboot);
                RunUbootCmd(command, false, callback);
                var buf = ReadMemory(transfer_base_m + address % sector_size, reqLen, callback);
                result.AddRange(buf);
                address += (uint)buf.Length;
                length  -= (uint)buf.Length;
            }
            return(result.ToArray());
        }
Ejemplo n.º 5
0
        public void WriteMemory(UInt32 address, byte[] buffer, OnFelProgress callback = null)
        {
            if (address >= dram_base)
            {
                InitDram();
            }

            UInt32 length = (UInt32)buffer.Length;

            if (length != (length & ~((UInt32)3)))
            {
                length = (length + 3) & ~((UInt32)3);
                var newBuffer = new byte[length];
                Array.Copy(buffer, 0, newBuffer, 0, buffer.Length);
                buffer = newBuffer;
            }

            int pos = 0;

            while (pos < buffer.Length)
            {
                callback?.Invoke(CurrentAction.WritingMemory, null);
                var buf = new byte[Math.Min(buffer.Length - pos, MaxBulkSize)];
                Array.Copy(buffer, pos, buf, 0, buf.Length);
                FelRequest(AWFELStandardRequest.RequestType.FEL_DOWNLOAD, (UInt32)(address + pos), (uint)buf.Length);
                FelWrite(buf);
                var status = new AWFELStatusResponse(FelRead(8));
                if (status.State != 0)
                {
                    throw new FelException("FEL write error");
                }
                pos += buf.Length;
            }
        }
Ejemplo n.º 6
0
        public void RunUbootCmd(string command, bool noreturn = false, OnFelProgress callback = null)
        {
            if (callback != null)
            {
                callback(CurrentAction.RunningCommand, command);
            }
            const UInt32 testSize = 0x20;

            if (UBootBin == null || UBootBin.Length < testSize)
            {
                throw new FelException("Can't init Uboot, incorrect Uboot binary");
            }
            var buf  = ReadMemory(uboot_base_m, testSize);
            var buf2 = new byte[testSize];

            Array.Copy(UBootBin, 0, buf2, 0, testSize);
            if (!buf.SequenceEqual(buf2))
            {
                WriteMemory(uboot_base_m, UBootBin);
            }
            var cmdBuff = Encoding.ASCII.GetBytes(command + "\0");

            WriteMemory((uint)(uboot_base_m + cmdOffset), cmdBuff);
            Exec((uint)uboot_base_m);
            if (noreturn)
            {
                return;
            }
            Close();
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(1000);
                if (callback != null)
                {
                    callback(CurrentAction.RunningCommand, command);
                }
            }
            int errorCount = 0;

            while (true)
            {
                try
                {
                    Open(vid, pid);
                    break;
                }
                catch (Exception ex)
                {
                    errorCount++;
                    if (errorCount >= 10)
                    {
                        throw ex;
                    }
                    Thread.Sleep(2000);
                }
            }
        }
Ejemplo n.º 7
0
        public byte[] ReadFlash(UInt32 address, UInt32 length, OnFelProgress callback = null)
        {
            var result = new List <byte>();

            while (((length + address % sector_size + sector_size - 1) / sector_size) > flash_mem_size)
            {
                var sectors = (length + address % sector_size + sector_size - 1) / sector_size - flash_mem_size;
                var buf     = ReadFlash(address, sectors * sector_size - address % sector_size, callback);
                address += (uint)buf.Length;
                length  -= (uint)buf.Length;
                result.AddRange(buf);
            }
            if (result.Count > 0)
            {
                return(result.ToArray());
            }
            var command = string.Format("sunxi_flash phy_read {0:x} {1:x} {2:x};{3}", flash_mem_base, address / sector_size, (length + address % sector_size + sector_size - 1) / sector_size, fastboot);

            RunUbootCmd(command, false, callback);
            result.AddRange(ReadMemory(flash_mem_base + address % sector_size, length, callback));
            return(result.ToArray());
        }
Ejemplo n.º 8
0
 public void RunUbootCmd(string command, bool noreturn = false, OnFelProgress callback = null)
 {
     callback?.Invoke(CurrentAction.RunningCommand, command);
     if (cmdOffset < 0) throw new Exception("Invalid Uboot binary, command variable not found");
     const UInt32 testSize = 0x20;
     if (UBootBin == null || UBootBin.Length < testSize)
         throw new FelException("Can't init Uboot, incorrect Uboot binary");
     var buf = ReadMemory(uboot_base_m, testSize);
     var buf2 = new byte[testSize];
     Array.Copy(UBootBin, 0, buf2, 0, testSize);
     if (!buf.SequenceEqual(buf2))
         WriteMemory(uboot_base_m, UBootBin);
     var cmdBuff = Encoding.ASCII.GetBytes(command + "\0");
     WriteMemory((uint)(uboot_base_m + cmdOffset), cmdBuff);
     Exec((uint)uboot_base_m);
     if (noreturn) return;
     Close();
     for (int i = 0; i < 10; i++)
     {
         Thread.Sleep(500);
         callback?.Invoke(CurrentAction.RunningCommand, command);
     }
     int errorCount = 0;
     while (true)
     {
         if (!Open(vid, pid))
         {
             errorCount++;
             if (errorCount >= 10)
             {
                 Close();
                 throw new Exception("No answer from device");
             }
             Thread.Sleep(2000);
         }
         else break;
     }
 }