From 79142880fdb062781c0261d15a52df6cc26e65e4 Mon Sep 17 00:00:00 2001 From: Gutemberg Ribeiro Date: Tue, 1 Jun 2021 16:27:21 -0300 Subject: [PATCH] Allow previous versions of the firmware. --- src/UFiber.Configurator/Program.cs | 34 ++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/UFiber.Configurator/Program.cs b/src/UFiber.Configurator/Program.cs index 5b18fb4..6447f6b 100644 --- a/src/UFiber.Configurator/Program.cs +++ b/src/UFiber.Configurator/Program.cs @@ -38,6 +38,9 @@ var rootCommand = new RootCommand("Apply configuration changes to UFiber devices new Option( "--mac", "The desired MAC address to clone.", ArgumentArity.ZeroOrOne), + new Option( + "--restore", + "Restore a previous version of the firmware.", ArgumentArity.ZeroOrOne) }; SshClient GetSSHClient(string userName, string password, string host, int port = 22) @@ -55,8 +58,8 @@ ScpClient GetSCPClient(string userName, string password, string host, int port = } rootCommand.Handler = CommandHandler - .Create( - (host, user, pw, port, dryRun, slid, vendor, serial, mac) => + .Create( + (host, user, pw, port, dryRun, slid, vendor, serial, mac, fwToRestore) => { if (string.IsNullOrWhiteSpace(host)) { @@ -112,6 +115,33 @@ rootCommand.Handler = CommandHandler return; } + if (!string.IsNullOrWhiteSpace(fwToRestore)) + { + const string targetFileToRestore = "/tmp/restore.bin"; + Console.WriteLine("Uploading original file to the target UFiber device..."); + try + { + scp.Upload(new FileInfo(fwToRestore), targetFileToRestore); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Failure uploading original image file to the UFiber device. Error: {ex.Message}."); + Environment.ExitCode = -1; + return; + } + Console.WriteLine("Uploaded!"); + Console.WriteLine("### Applying original file on the target UFiber device..."); + cmd = ssh.RunCommand($"dd if={targetFileToRestore} of=/dev/mtdblock3 && rm {targetFileToRestore}"); + if (cmd.ExitStatus != 0) + { + Console.Error.WriteLine($"Failure to apply original image file. Error: {cmd.Error}"); + Environment.ExitCode = cmd.ExitStatus; + return; + } + Console.WriteLine("### Applied patch! Please reboot your UFiber device to load the new image."); + return; + } + var ram = new NVRAM(File.ReadAllBytes(Path.Combine(localDumps, imgName))); Console.WriteLine("### Original Image ###"); Console.WriteLine(ram);