Skip to content

Commit 345097e

Browse files
committed
fix: Improve fastboot userspace reboot & sparse limits
Switch SparseMaxDownloadSize to a long and default it to uint.MaxValue, and clamp computed download sizes against both the host-side limit and the protocol (uint32) max. Adjust CLI and tests to use the new type and to apply the correct min/max. Improve reboot-to-userspace behavior: shorten the reconnect pause, print a waiting message, use EnsureUserspace for fastboot-target reboots, and add clearer error messages plus a final IsUserspace check after reconnect. Also bump FirmwareKit.Sparse to 1.0.1 and remove an AVB package reference.
1 parent b58507d commit 345097e

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

FastbootCLI/Program.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static void Main(string[] args)
104104
}
105105

106106
using FastbootDriver util = new FastbootDriver(target);
107-
if (sparseLimit.HasValue) FastbootDriver.SparseMaxDownloadSize = (int)Math.Min(int.MaxValue, sparseLimit.Value);
107+
if (sparseLimit.HasValue) FastbootDriver.SparseMaxDownloadSize = Math.Min((long)uint.MaxValue, sparseLimit.Value);
108108

109109
util.ReceivedFromDevice += (s, e) =>
110110
{
@@ -217,15 +217,24 @@ string GetPartition(string baseName)
217217

218218
case "reboot":
219219
string targetStr = args.Count > 0 ? args[0] : "";
220-
util.Reboot(targetStr).ThrowIfError();
220+
if (targetStr == "fastboot")
221+
{
222+
Console.Error.WriteLine("waiting for any device >");
223+
util.EnsureUserspace();
224+
}
225+
else
226+
{
227+
util.Reboot(targetStr).ThrowIfError();
228+
}
221229
break;
222230

223231
case "reboot-bootloader":
224232
util.Reboot("bootloader").ThrowIfError();
225233
break;
226234

227235
case "reboot-fastboot":
228-
util.Reboot("fastboot").ThrowIfError();
236+
Console.Error.WriteLine("waiting for any device >");
237+
util.EnsureUserspace();
229238
break;
230239

231240
case "reboot-recovery":

FirmwareKit.Comm.Fastboot.Tests/FastbootInfoTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void FlashFromInfo_RebootCommands_MappedCorrectly()
199199
[Fact]
200200
public void GetMaxDownloadSize_ClampsToSparseLimit()
201201
{
202-
int originalLimit = FastbootDriver.SparseMaxDownloadSize;
202+
long originalLimit = FastbootDriver.SparseMaxDownloadSize;
203203
try
204204
{
205205
FastbootDriver.SparseMaxDownloadSize = 1024 * 1024 * 1024;

FirmwareKit.Comm.Fastboot/FastbootDriver.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ public static LpMetadata ReadFromImageStream(Stream stream)
143143
/// Size of data to send in a single chunk (512KB for better WinUSB/Qualcomm compatibility)
144144
/// </summary>
145145
public static int OnceSendDataSize = 512 * 1024;
146-
public static int SparseMaxDownloadSize = 1024 * 1024 * 1024; // 1GB default limit to match AOSP RESPARSE_LIMIT
146+
// Host-side resparse limit. Keep this <= fastboot protocol DATA field max (uint32).
147+
// A wider default avoids unnecessary sparse conversion for large images, reducing peak memory usage.
148+
public static long SparseMaxDownloadSize = uint.MaxValue;
147149

148150
private static readonly string[] PartitionPriority = {
149151
"preloader", "bootloader", "radio", "dram", "md1img", "xbl", "abl", "keystore",
@@ -341,12 +343,14 @@ public void EnsureUserspace()
341343
NotifyCurrentStep("Operation requires fastbootd, rebooting...");
342344
Reboot("fastboot").ThrowIfError();
343345

344-
System.Threading.Thread.Sleep(2000);
346+
// Match AOSP behavior: allow disconnect to happen before attempting reconnect.
347+
System.Threading.Thread.Sleep(1000);
348+
NotifyCurrentStep("waiting for any device >");
345349

346350
if (Transport is UsbDevice usbDev)
347351
{
348352
var newUtil = WaitForDevice(UsbManager.GetAllDevices, usbDev.SerialNumber, 30);
349-
if (newUtil == null) throw new Exception("Failed to reconnect to device after rebooting to fastbootd.");
353+
if (newUtil == null) throw new Exception("Failed to boot into userspace fastboot; one or more components might be unbootable.");
350354

351355
this.Transport = newUtil.Transport;
352356
}
@@ -368,7 +372,7 @@ public void EnsureUserspace()
368372
}
369373
catch { System.Threading.Thread.Sleep(1000); }
370374
}
371-
if (!connected) throw new Exception("Failed to reconnect to TCP device after rebooting to fastbootd.");
375+
if (!connected) throw new Exception("Failed to boot into userspace fastboot; one or more components might be unbootable.");
372376
}
373377
else if (Transport is UdpTransport udp)
374378
{
@@ -388,12 +392,18 @@ public void EnsureUserspace()
388392
}
389393
catch { System.Threading.Thread.Sleep(1000); }
390394
}
391-
if (!connected) throw new Exception("Failed to reconnect to UDP device after rebooting to fastbootd.");
395+
if (!connected) throw new Exception("Failed to boot into userspace fastboot; one or more components might be unbootable.");
392396
}
393397
else
394398
{
395399
throw new NotSupportedException("Automatic reboot to userspace is only supported for USB, TCP and UDP transports.");
396400
}
401+
402+
if (!IsUserspace())
403+
{
404+
throw new Exception("Failed to boot into userspace fastboot; one or more components might be unbootable.");
405+
}
406+
397407
_varCache.Clear();
398408
}
399409
}
@@ -481,8 +491,8 @@ public long GetMaxDownloadSize()
481491
return SparseMaxDownloadSize;
482492
}
483493

484-
// Match AOSP's host-side safeguard: never exceed the resparse limit.
485-
return Math.Min(parsedSize, SparseMaxDownloadSize);
494+
// Keep within protocol limits and the configurable host-side resparse limit.
495+
return Math.Min(Math.Min(parsedSize, SparseMaxDownloadSize), uint.MaxValue);
486496
}
487497

488498
/// <summary>

FirmwareKit.Comm.Fastboot/FirmwareKit.Comm.Fastboot.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@
3030

3131
<ItemGroup>
3232
<PackageReference Include="Crc32.NET" Version="1.2.0" />
33-
<PackageReference Include="FirmwareKit.AVB" Version="1.0.0" />
3433
<PackageReference Include="FirmwareKit.Lp" Version="1.0.0" />
35-
<PackageReference Include="FirmwareKit.Sparse" Version="1.0.0" />
34+
<PackageReference Include="FirmwareKit.Sparse" Version="1.0.1" />
3635
<PackageReference Include="LibUsbDotNet" Version="3.0.167-alpha" />
3736
</ItemGroup>
3837

0 commit comments

Comments
 (0)