Skip to content

Commit 852344f

Browse files
committed
feat: Enhance fastboot transfers and USB backend also rename multiple code namespace
Multiple improvements across the fastboot CLI and library: add retry/recovery and CRC validation to DownloadData; detect sparse images, split large raw images into chunks, and handle AVB footer padding in FlashUnsparseImage; switch FlashRaw to use FlashUnsparseImage. Add AdvancedStreams (SubStream, ConcatenatedStream, PaddingStream) to support streaming large/stitched payloads. Make USB backend default to libusb on Linux (UsbManager) and only return Linux devices that can open a handle (LinuxUsbFinder); add a --fallback CLI flag and change CLI to wait for a device if none are connected. Add user-facing NotifyCurrentStep messages for erase/format/reboot/set_active/flashall flows. Add unit tests for INFO/DATA parsing edge cases and update project dependencies (Crc32.NET, FirmwareKit.AVB). Also update .gitignore with common file patterns.
1 parent 56a4129 commit 852344f

File tree

83 files changed

+894
-345
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+894
-345
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
##
44
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
55

6+
*.c
7+
*.h
8+
*.img
9+
*.bat
10+
*.cpp
11+
fastboot/
12+
*.txt
13+
614
# User-specific files
715
*.rsuser
816
*.suo

FastbootCLI/Program.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using FirmwareKit.Comm.Fastboot;
2-
using FirmwareKit.Comm.Fastboot.Backend.Usb;
2+
using FirmwareKit.Comm.Fastboot.Usb;
33

44

55
namespace FastbootCLI
@@ -44,7 +44,7 @@ static void Main(string[] args)
4444
sparseLimit = ParseSize(sizeStr);
4545
}
4646
else if (arg == "--debug") FastbootDebug.IsEnabled = true;
47-
else if (arg == "--libusb") UsbManager.ForceLibUsb = true;
47+
else if (arg == "--fallback") UsbManager.ForceLibUsb = false;
4848
else if (arg == "--version" || arg == "version") { Console.WriteLine("fastboot version 1.2.5"); return; }
4949
else if (arg == "-h" || arg == "--help" || arg == "help") { ShowHelp(); return; }
5050
else if (!arg.StartsWith("-"))
@@ -75,16 +75,36 @@ static void Main(string[] args)
7575
}
7676

7777
var devices = UsbManager.GetAllDevices();
78-
UsbDevice? target = serial != null ? devices.FirstOrDefault(d => d.SerialNumber == serial) : (devices.Count > 0 ? devices[0] : null);
78+
UsbDevice? target = null;
79+
80+
if (serial != null)
81+
{
82+
target = devices.FirstOrDefault(d => d.SerialNumber == serial);
83+
}
84+
else if (devices.Count > 0)
85+
{
86+
target = devices[0];
87+
}
88+
else
89+
{
90+
// Wait for device matching AOSP behavior
91+
Console.Error.WriteLine("< waiting for any device >");
92+
while (target == null)
93+
{
94+
System.Threading.Thread.Sleep(500);
95+
devices = UsbManager.GetAllDevices();
96+
target = devices.FirstOrDefault();
97+
}
98+
}
7999

80100
if (target == null)
81101
{
82102
Console.Error.WriteLine("fastboot: error: no devices/found");
83103
Environment.Exit(1);
84104
}
85105

86-
using FastbootUtil util = new FastbootUtil(target);
87-
if (sparseLimit.HasValue) FastbootUtil.SparseMaxDownloadSize = (int)Math.Min(int.MaxValue, sparseLimit.Value);
106+
using FastbootDriver util = new FastbootDriver(target);
107+
if (sparseLimit.HasValue) FastbootDriver.SparseMaxDownloadSize = (int)Math.Min(int.MaxValue, sparseLimit.Value);
88108

89109
util.ReceivedFromDevice += (s, e) =>
90110
{
@@ -127,7 +147,7 @@ static long ParseSize(string sizeStr)
127147
return long.Parse(sizeStr) * multiplier;
128148
}
129149

130-
static void ExecuteCommand(FastbootUtil util, string command, List<string> args)
150+
static void ExecuteCommand(FastbootDriver util, string command, List<string> args)
131151
{
132152
if (command == "devices")
133153
{
@@ -257,7 +277,6 @@ string GetPartition(string baseName)
257277
using var fs = File.OpenRead(file);
258278
util.FlashUnsparseImage(part, fs, fs.Length).ThrowIfError();
259279
}
260-
if (!skipReboot && (part.StartsWith("boot") || part.StartsWith("system"))) Console.Error.WriteLine("Note: Device may need a manual reboot or use 'fastboot reboot'.");
261280
break;
262281

263282
case "flashall":
@@ -398,6 +417,7 @@ static void ShowHelp()
398417
Console.Error.WriteLine(" --skip-reboot Don't reboot device after flashing all.");
399418
Console.Error.WriteLine(" --force Force execute command (e.g. skip snapshot check).");
400419
Console.Error.WriteLine(" --fs-options <opt> File system options for format (e.g. casefold).");
420+
Console.Error.WriteLine(" --fallback Use platform native USB backend instead of libusb (on Linux libusb is default).");
401421

402422
Console.Error.WriteLine("\nbasics:");
403423
Console.Error.WriteLine(" devices [-l] List connected devices.");

FirmwareKit.Comm.Fastboot.Tests/AospDriverTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using FirmwareKit.Comm.Fastboot.Backend.Network;
2-
using FirmwareKit.Comm.Fastboot.DataModel;
31
using System.Text;
42

53
namespace FirmwareKit.Comm.Fastboot.Tests

FirmwareKit.Comm.Fastboot.Tests/AospFastbootDriverTests.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using FirmwareKit.Comm.Fastboot.Backend.Network;
2-
using FirmwareKit.Comm.Fastboot.DataModel;
31
using System.Text;
42

53
namespace FirmwareKit.Comm.Fastboot.Tests
@@ -115,5 +113,53 @@ public void Test_Download_Fail_Aosp()
115113
Assert.Equal(FastbootState.Fail, response.Result);
116114
Assert.Equal("data too large", response.Response);
117115
}
116+
117+
[Fact]
118+
public void Test_ConsecutiveInfoInSinglePacket()
119+
{
120+
var transport = new MockTransport();
121+
var util = new FastbootUtil(transport);
122+
123+
// Single packet contains two INFO frames followed by OKAY
124+
transport.EnqueueResponse("INFOfirst lineINFOsecond lineOKAY");
125+
126+
var response = util.RawCommand("oem dmesg");
127+
128+
Assert.Equal(FastbootState.Success, response.Result);
129+
Assert.Equal(2, response.Info.Count);
130+
Assert.Equal("first line", response.Info[0]);
131+
Assert.Equal("second line", response.Info[1]);
132+
}
133+
134+
[Fact]
135+
public void Test_InfoWithoutDelimiterFollowedByOkay()
136+
{
137+
var transport = new MockTransport();
138+
var util = new FastbootUtil(transport);
139+
140+
// INFO payload without newline then OKAY immediately in same packet
141+
transport.EnqueueResponse("INFOpartial infoOKAY");
142+
143+
var response = util.RawCommand("getvar:all");
144+
145+
Assert.Equal(FastbootState.Success, response.Result);
146+
Assert.Single(response.Info);
147+
Assert.Equal("partial info", response.Info[0]);
148+
}
149+
150+
[Fact]
151+
public void Test_MalformedDataLength()
152+
{
153+
var transport = new MockTransport();
154+
var util = new FastbootUtil(transport);
155+
156+
// DATA followed by non-hex content should be treated as malformed
157+
transport.EnqueueResponse("DATAGARBAGE");
158+
159+
var response = util.RawCommand("download:00000010");
160+
161+
Assert.Equal(FastbootState.Fail, response.Result);
162+
Assert.Contains("data size malformed", response.Response);
163+
}
118164
}
119165
}

FirmwareKit.Comm.Fastboot.Tests/BootImageTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using FirmwareKit.Comm.Fastboot.DataModel;
21
using System.Runtime.InteropServices;
32
using System.Text;
43

FirmwareKit.Comm.Fastboot.Tests/FastbootInfoTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using FirmwareKit.Comm.Fastboot.Backend.Network;
21
using System.Globalization;
32
using System.Text;
43

FirmwareKit.Comm.Fastboot.Tests/FastbootProtocolTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using FirmwareKit.Comm.Fastboot.Backend.Usb;
2-
using FirmwareKit.Comm.Fastboot.DataModel;
3-
using System.Text;
4-
using FirmwareKit.Comm.Fastboot.Backend.Network;
51
using System.Globalization;
2+
using System.Text;
63

74
namespace FirmwareKit.Comm.Fastboot.Tests
85
{

FirmwareKit.Comm.Fastboot.Tests/TcpTransportTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using FirmwareKit.Comm.Fastboot.Backend.Network;
21
using System.Buffers.Binary;
32
using System.Net;
43
using System.Net.Sockets;

FirmwareKit.Comm.Fastboot.Tests/UdpTransportTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using FirmwareKit.Comm.Fastboot.Backend.Network;
21
using System.Buffers.Binary;
32
using System.Net;
43
using System.Net.Sockets;

FirmwareKit.Comm.Fastboot/Backend/Network/IFastbootTransport.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
namespace FirmwareKit.Comm.Fastboot.Backend.Network;
1+
namespace FirmwareKit.Comm.Fastboot;
2+
23

34
public interface IFastbootTransport : IDisposable
45
{
56
byte[] Read(int length);
67
long Write(byte[] data, int length);
8+
}
79

810

9-
}

0 commit comments

Comments
 (0)