Пример #1
0
        byte[] WaitRead(UInt32 address, UInt32 size)
        {
            SetStatus(Resources.WaitingForDevice);
            int errorCount = 0;

            while (true)
            {
                try
                {
                    if ((errorCount > 0) && (errorCount % 5) == 0)
                    {
                        fel.Close();
                        Thread.Sleep(1000);
                        fel.Open(vid, pid);
                    }
                    fel.ClearInputBuffer();
                    fel.VerifyDevice();
                    var data = fel.ReadMemory(address, size);
                    return(data);
                }
                catch (Exception ex)
                {
                    errorCount++;
                    if (errorCount >= 15)
                    {
                        throw ex;
                    }
                    Thread.Sleep(1000);
                }
            }
        }
Пример #2
0
        public Conclusion WaitForFelOrMembootableShell(Tasker tasker, Object syncObject = null)
        {
            if (tasker.HostForm.InvokeRequired)
            {
                return((Conclusion)tasker.HostForm.Invoke(new Func <Tasker, Object, Conclusion>(WaitForFelOrMembootableShell), new object[] { tasker, syncObject }));
            }

            tasker.SetStatus(Resources.WaitingForDevice);
            if (hakchi.Shell.IsOnline && hakchi.Shell.Execute("[ -f /proc/atags ]") == 0)
            {
                return(Conclusion.Success);
            }

            if (!WaitingFelForm.WaitForDevice(Shared.ClassicUSB.vid, Shared.ClassicUSB.pid, tasker.HostForm))
            {
                return(Conclusion.Abort);
            }

            fel.Fes1Bin = Resources.fes1;
            if (ConfigIni.Instance.MembootUboot == Fel.UbootType.SD)
            {
                fel.UBootBin = Resources.ubootSD;
            }
            else
            {
                fel.UBootBin = Resources.uboot;
            }
            if (!fel.Open(Shared.ClassicUSB.vid, Shared.ClassicUSB.pid))
            {
                throw new FelException("Can't open device");
            }
            tasker.SetStatus(Resources.UploadingFes1);
            fel.InitDram(true);
            return(Conclusion.Success);
        }
Пример #3
0
        public void StartThread()
        {
            fel = new Fel();
            SetProgress(0, 1);
            try
            {
                if (Task != Tasks.DownloadAllCovers)
                {
                    if (!File.Exists(fes1Path))
                    {
                        throw new FileNotFoundException(fes1Path + " not found");
                    }
                    if (!File.Exists(ubootPath))
                    {
                        throw new FileNotFoundException(ubootPath + " not found");
                    }
                    fel.Fes1Bin  = File.ReadAllBytes(fes1Path);
                    fel.UBootBin = File.ReadAllBytes(ubootPath);
                    fel.Open(vid, pid);
                    SetStatus(Resources.UploadingFes1);
                    fel.InitDram(true);
                }
                switch (Task)
                {
                case Tasks.DumpKernel:
                    DoKernelDump();
                    break;

                case Tasks.FlashKernel:
                    FlashKernel();
                    break;

                case Tasks.Memboot:
                    Memboot();
                    break;

                case Tasks.DownloadAllCovers:
                    DownloadAllCovers();
                    break;
                }
                Thread.Sleep(1000);
                DialogResult = DialogResult.OK;
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                ShowError(ex);
            }
            finally
            {
                thread = null;
                if (fel != null)
                {
                    fel.Close();
                    fel = null;
                }
            }
        }
Пример #4
0
        public void StartThread()
        {
            fel          = new Fel();
            fel.Fes1Bin  = Resources.fes1;
            fel.UBootBin = Resources.uboot;
            SetProgress(0, 100);
            try
            {
                fel.Open(vid, pid);
                SetStatus(Resources.UploadingFes1);
                fel.InitDram(true);
                switch (Task)
                {
                case Tasks.DumpKernel:
                    SetText(Resources.DumpingKernel);
                    DoKernelDump();
                    break;

                case Tasks.FlashKernel:
                    if (!string.IsNullOrEmpty(Mod))
                    {
                        SetText(Resources.FlasingCustom);
                    }
                    else
                    {
                        SetText(Resources.FlasingOriginal);
                    }
                    FlashKernel();
                    break;

                case Tasks.Memboot:
                    SetText(Resources.UploadingGames);
                    Memboot();
                    break;
                }
                Thread.Sleep(1000);
                DialogResult = DialogResult.OK;
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                ShowError(ex);
            }
            finally
            {
                thread = null;
                if (fel != null)
                {
                    fel.Close();
                    fel = null;
                }
            }
        }
Пример #5
0
        public byte[] ReadFlash(byte[] fes, byte[] uboot, UInt32 address, UInt32 length, string what = "NAND")
        {
            var fel = new Fel();

            fel.WriteLine += WriteLine;
            fel.Fes1Bin    = fes;
            fel.UBootBin   = uboot;
            if (!fel.Open())
            {
                throw new Exception("USB Device Not Found");
            }

            return(ReadFlash(fel, fes, uboot, address, length, what));
        }
Пример #6
0
        public void WriteFlash(byte[] fes, byte[] uboot, UInt32 address, byte[] data, bool verify = true, string what = "NAND")
        {
            var fel = new Fel();

            fel.WriteLine += WriteLine;
            fel.Fes1Bin    = fes;
            fel.UBootBin   = uboot;
            if (!fel.Open())
            {
                throw new Exception("USB Device Not Found");
            }

            WriteFlash(fel, fes, uboot, address, data, verify, what);
        }
Пример #7
0
        public void RunCommand(byte[] fes, byte[] uboot, string command, bool noreturn = false)
        {
            var fel = new Fel();

            fel.WriteLine += WriteLine;
            fel.Fes1Bin    = fes;
            fel.UBootBin   = uboot;
            if (!fel.Open())
            {
                throw new Exception("USB Device Not Found");
            }

            RunCommand(fel, command, noreturn);
        }
Пример #8
0
 static bool DeviceExists(UInt16 vid, UInt16 pid)
 {
     try
     {
         using (var fel = new Fel())
         {
             fel.Open(vid, pid);
             return(true);
         }
     }
     catch
     {
         return(false);
     }
 }
Пример #9
0
        public void Memboot(byte[] fes, byte[] uboot, byte[] bootImage, Fel.WriteLineHandler writeLineHandler = null)
        {
            var fel = new Fel();

            fel.WriteLine += WriteLine;
            fel.Fes1Bin    = fes;
            fel.UBootBin   = uboot;
            if (!fel.Open())
            {
                throw new Exception("USB Device Not Found");
            }

            var size = CalcKernelSize(bootImage);

            if (size > bootImage.Length || size > Fel.transfer_max_size)
            {
                throw new Exception($"Invalid boot image size: {size}");
            }
            size = (size + Fel.sector_size - 1) / Fel.sector_size;
            size = size * Fel.sector_size;
            if (bootImage.Length != size)
            {
                var newK = new byte[size];
                Array.Copy(bootImage, newK, bootImage.Length);
                bootImage = newK;
            }

            long maxProgress = size / 65536, progress = 0;

            // upload kernel through fel
            fel.WriteMemory(Fel.transfer_base_m, bootImage,
                            delegate(Fel.CurrentAction action, string command)
            {
                switch (action)
                {
                case Fel.CurrentAction.WritingMemory:
                    SetStatus?.Invoke("Uploading boot image");
                    break;
                }
                progress++;
                SetProgress?.Invoke(Math.Min(progress, maxProgress), maxProgress);
            }
                            );

            var bootCommand = string.Format("boota {0:x}", Fel.transfer_base_m);

            RunCommand(fel, bootCommand, true);
        }
Пример #10
0
        public void StartThread()
        {
            fel = new Fel();
            try
            {
                fel.Open(vid, pid);
                switch (Task)
                {
                case Tasks.DumpKernel:
                    SetText(Resources.DumpingKernel);
                    DoKernelDump();
                    break;

                case Tasks.FlashKernel:
                    if (!string.IsNullOrEmpty(Mod))
                    {
                        SetText(Resources.FlasingCustom);
                    }
                    else
                    {
                        SetText(Resources.FlasingOriginal);
                    }
                    FlashKernel();
                    break;

                case Tasks.Memboot:
                    SetText(Resources.UploadingGames);
                    Memboot();
                    break;
                }
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }
            finally
            {
                thread = null;
                if (fel != null)
                {
                    fel.Close();
                    fel = null;
                }
            }
        }
Пример #11
0
        private Conclusion WaitForFelOrMembootableShell(Tasker tasker, Object syncObject = null, bool sdRequired = false)
        {
            var hostForm = tasker.GetSpecificViews <Form>().FirstOrDefault();

            if (hostForm == default(Form))
            {
                hostForm = tasker.HostForm;
            }
            if (hostForm.InvokeRequired)
            {
                return((Conclusion)hostForm.Invoke(new Func <Tasker, Object, bool, Conclusion>(WaitForFelOrMembootableShell), new object[] { tasker, syncObject, sdRequired }));
            }

            tasker.SetStatus(Resources.WaitingForDevice);
            if (hakchi.Shell.IsOnline && hakchi.Shell.Execute("[ -f /proc/atags ]") == 0)
            {
                if (sdRequired)
                {
                    if (hakchi.Shell.Execute("hakchi mmcUsed") == 0)
                    {
                        return(Conclusion.Success);
                    }
                }
                else
                {
                    return(Conclusion.Success);
                }
            }

            if (!WaitingFelForm.WaitForDevice(hostForm))
            {
                return(Conclusion.Abort);
            }

            fel.Fes1Bin  = Resources.fes1;
            fel.UBootBin = hakchi.Hmod.GetUboot(UbootType.SD).ToArray();
            if (!fel.Open())
            {
                throw new FelException("Can't open device");
            }
            tasker.SetStatus(Resources.UploadingFes1);
            fel.InitDram(true);
            return(Conclusion.Success);
        }
Пример #12
0
        public void StartThread()
        {
            fel          = new Fel();
            fel.Fes1Bin  = Resources.fes1;
            fel.UBootBin = Resources.uboot;
            SetProgress(0, 100);
            try
            {
                fel.Open(vid, pid);
                SetStatus(Resources.UploadingFes1);
                fel.InitDram(true);
                switch (Task)
                {
                case Tasks.DumpKernel:
                    DoKernelDump();
                    break;

                case Tasks.FlashKernel:
                    FlashKernel();
                    break;

                case Tasks.Memboot:
                    Memboot();
                    break;
                }
                Thread.Sleep(1000);
                DialogResult = DialogResult.OK;
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                ShowError(ex);
            }
            finally
            {
                thread = null;
                if (fel != null)
                {
                    fel.Close();
                    fel = null;
                }
            }
        }
Пример #13
0
        public Conclusion WaitForFelOrMembootableShell(Tasker tasker, Object syncObject = null)
        {
            var hostForm = tasker.GetSpecificViews <Form>().FirstOrDefault();

            if (hostForm == default(Form))
            {
                hostForm = tasker.HostForm;
            }
            if (hostForm.InvokeRequired)
            {
                return((Conclusion)hostForm.Invoke(new Func <Tasker, Object, Conclusion>(WaitForFelOrMembootableShell), new object[] { tasker, syncObject }));
            }

            tasker.SetStatus(Resources.WaitingForDevice);
            if (hakchi.Shell.IsOnline && hakchi.Shell.Execute("[ -f /proc/atags ]") == 0)
            {
                return(Conclusion.Success);
            }

            if (!WaitingFelForm.WaitForDevice(hostForm))
            {
                return(Conclusion.Abort);
            }

            fel.Fes1Bin = Resources.fes1;
            if (ConfigIni.Instance.MembootUboot == ConfigIni.UbootType.SD)
            {
                fel.UBootBin = Resources.ubootSD;
            }
            else
            {
                fel.UBootBin = Resources.uboot;
            }
            if (!fel.Open())
            {
                throw new FelException("Can't open device");
            }
            tasker.SetStatus(Resources.UploadingFes1);
            fel.InitDram(true);
            return(Conclusion.Success);
        }
Пример #14
0
        public void FlashUboot(byte[] fes, byte[] uboot, bool flashAll = false)
        {
            var fel = new Fel();

            fel.WriteLine += WriteLine;
            fel.Fes1Bin    = fes;
            fel.UBootBin   = uboot;
            if (!fel.Open())
            {
                throw new Exception("USB Device Not Found");
            }

            if (uboot.Length > Fel.uboot_maxsize_f)
            {
                throw new Exception($"Invalid uboot size: {uboot.Length}");
            }

            WriteFlash(fel, fes, uboot, Fel.uboot_base_f, uboot, true, "u-boot");

            RunCommand(fel, "shutdown", true);
            SetProgress(1, 1);
        }
Пример #15
0
        DialogResult WaitForFelFromThread()
        {
            if (InvokeRequired)
            {
                return((DialogResult)Invoke(new Func <DialogResult>(WaitForFelFromThread)));
            }
            SetStatus(Resources.WaitingForDevice);
            if (fel != null)
            {
                fel.Close();
            }
            TaskbarProgress.SetState(this, TaskbarProgress.TaskbarStates.Paused);
            var result = WaitingFelForm.WaitForDevice(vid, pid, this);

            if (result)
            {
                fel = new Fel();
                if (!File.Exists(fes1Path))
                {
                    throw new FileNotFoundException(fes1Path + " not found");
                }
                if (!File.Exists(ubootPath))
                {
                    throw new FileNotFoundException(ubootPath + " not found");
                }
                fel.Fes1Bin  = File.ReadAllBytes(fes1Path);
                fel.UBootBin = File.ReadAllBytes(ubootPath);
                fel.Open(vid, pid);
                SetStatus(Resources.UploadingFes1);
                fel.InitDram(true);
                TaskbarProgress.SetState(this, TaskbarProgress.TaskbarStates.Normal);
                return(DialogResult.OK);
            }
            TaskbarProgress.SetState(this, TaskbarProgress.TaskbarStates.Normal);
            return(DialogResult.Abort);
        }
Пример #16
0
        public void FlashBoot(byte[] fes, byte[] uboot, byte[] bootImage)
        {
            var fel = new Fel();

            fel.WriteLine += WriteLine;
            fel.Fes1Bin    = fes;
            fel.UBootBin   = uboot;
            if (!fel.Open())
            {
                throw new Exception("USB Device Not Found");
            }

            var size = CalcKernelSize(bootImage);

            if (size > bootImage.Length || size > Fel.kernel_max_size)
            {
                throw new Exception($"Invalid boot image size: {size}");
            }

            WriteFlash(fel, fes, uboot, Fel.kernel_base_f, bootImage, true, "boot image");

            RunCommand(fel, "shutdown", true);
            SetProgress(1, 1);
        }
Пример #17
0
        public void FlashKernel()
        {
            int progress    = 5;
            int maxProgress = 120 + (string.IsNullOrEmpty(Mod) ? 0 : 5);

            SetProgress(progress, maxProgress);

            byte[] kernel;
            if (!string.IsNullOrEmpty(Mod))
            {
                kernel    = CreatePatchedKernel(Mod);
                progress += 5;
                SetProgress(progress, maxProgress);
            }
            else
            {
                kernel = File.ReadAllBytes(KernelDump);
            }
            var size = CalKernelSize(kernel);

            if (size > kernel.Length || size > kernel_max_size)
            {
                throw new Exception(Resources.InvalidKernelSize + " " + size);
            }

            size = (size + sector_size - 1) / sector_size;
            size = size * sector_size;
            if (kernel.Length != size)
            {
                var newK = new byte[size];
                Array.Copy(kernel, newK, kernel.Length);
                kernel = newK;
            }

            bool flashCommandExecuted = false;

            try
            {
                fel.WriteFlash(kernel_base_f, kernel,
                               delegate(Fel.CurrentAction action, string command)
                {
                    switch (action)
                    {
                    case Fel.CurrentAction.RunningCommand:
                        SetStatus(Resources.ExecutingCommand + " " + command);
                        flashCommandExecuted = true;
                        break;

                    case Fel.CurrentAction.WritingMemory:
                        SetStatus(Resources.UploadingKernel);
                        break;
                    }
                    progress++;
                    SetProgress(progress, maxProgress);
                }
                               );
            }
            catch (USBException ex)
            {
                fel.Close();
                if (flashCommandExecuted)
                {
                    SetStatus(Resources.WaitingForDevice);
                    waitDeviceResult = null;
                    WaitForDeviceInvoke(vid, pid);
                    while (waitDeviceResult == null)
                    {
                        Thread.Sleep(100);
                    }
                    if (!(waitDeviceResult ?? false))
                    {
                        DialogResult = DialogResult.Abort;
                        return;
                    }
                    Thread.Sleep(500);
                    fel          = new Fel();
                    fel.Fes1Bin  = Resources.fes1;
                    fel.UBootBin = Resources.uboot;
                    fel.Open(vid, pid);
                    SetStatus(Resources.UploadingFes1);
                    fel.InitDram(true);
                }
                else
                {
                    throw ex;
                }
            }
            var r = fel.ReadFlash((UInt32)kernel_base_f, (UInt32)kernel.Length,
                                  delegate(Fel.CurrentAction action, string command)
            {
                switch (action)
                {
                case Fel.CurrentAction.RunningCommand:
                    SetStatus(Resources.ExecutingCommand + " " + command);
                    break;

                case Fel.CurrentAction.ReadingMemory:
                    SetStatus(Resources.Verifying);
                    break;
                }
                progress++;
                SetProgress(progress, maxProgress);
            }
                                  );

            for (int i = 0; i < kernel.Length; i++)
            {
                if (kernel[i] != r[i])
                {
                    throw new Exception(Resources.VerifyFailed);
                }
            }
            var bootCommand = string.Format("boota {0:x}", kernel_base_m);

            SetStatus(Resources.ExecutingCommand + " " + bootCommand);
            fel.RunUbootCmd(bootCommand, true);
            SetStatus(Resources.Done);
            SetProgress(maxProgress, maxProgress);
        }
Пример #18
0
        public void Memboot()
        {
            int pos = 0, totalFiles = 0;
            int progress = 5;

            SetProgress(progress, 300);
            int maxProgress = -1;

            do
            {
                if (pos > 0)
                {
                    ShowMessage(Resources.ParticallyBody, Resources.ParticallyTitle);
                    DeviceWaitResult = DialogResult.None;
                    WaitForDeviceFromThread();
                    while (DeviceWaitResult == DialogResult.None)
                    {
                        Thread.Sleep(500);
                    }
                    if (DeviceWaitResult != DialogResult.OK)
                    {
                        DialogResult = DialogResult.Abort;
                        return;
                    }
                    fel          = new Fel();
                    fel.Fes1Bin  = File.ReadAllBytes(fes1Path);
                    fel.UBootBin = File.ReadAllBytes(ubootPath);
                    fel.Open(vid, pid);
                    SetStatus(Resources.UploadingFes1);
                    fel.InitDram(true);
                }

                byte[] kernel;
                if (!string.IsNullOrEmpty(Mod))
                {
                    kernel = CreatePatchedKernel(ref pos, out totalFiles);
                }
                else
                {
                    kernel = File.ReadAllBytes(KernelDump);
                }
                var size = CalKernelSize(kernel);
                if (size > kernel.Length || size > Fel.kernel_max_size)
                {
                    throw new Exception(Resources.InvalidKernelSize + " " + size);
                }
                size = (size + Fel.sector_size - 1) / Fel.sector_size;
                size = size * Fel.sector_size;
                if (kernel.Length != size)
                {
                    var newK = new byte[size];
                    Array.Copy(kernel, newK, kernel.Length);
                    kernel = newK;
                }

                progress += 5;
                if (maxProgress < 0)
                {
                    if (pos > 0)
                    {
                        maxProgress = (kernel.Length / 67000 + 15) * totalFiles / pos + 75 * ((int)Math.Ceiling((float)totalFiles / (float)pos) - 1);
                    }
                    else
                    {
                        maxProgress = (kernel.Length / 67000 + 15);
                    }
                }
                SetProgress(progress, maxProgress);

                SetStatus(Resources.UploadingKernel);
                fel.WriteMemory(Fel.flash_mem_base, kernel,
                                delegate(Fel.CurrentAction action, string command)
                {
                    switch (action)
                    {
                    case Fel.CurrentAction.WritingMemory:
                        SetStatus(Resources.UploadingKernel);
                        break;
                    }
                    progress++;
                    SetProgress(progress, maxProgress);
                }
                                );

                var bootCommand = string.Format("boota {0:x}", Fel.kernel_base_m);
                SetStatus(Resources.ExecutingCommand + " " + bootCommand);
                fel.RunUbootCmd(bootCommand, true);
            } while (pos < totalFiles);
            SetStatus(Resources.Done);
            SetProgress(maxProgress, maxProgress);
        }