Skip to content

Commit 79e426c

Browse files
committed
rerfactor: Refactor FastbootUtil to file-scoped partials
Flatten FastbootUtil implementation across command files: use file-scoped namespaces and a public partial FastbootUtil class (remove nested partial-class wrappers). Add XML summary comments and consolidate per-command methods into their own files. Improve I/O and protocol handling: refine DownloadData/UploadData streaming, progress notifications, short-write checks, and CRC handling for sparse flashes. Rewrite HandleResponse to parse OKAY/FAIL/INFO/TEXT/DATA more robustly, enforce data size limits, reset timeouts on progress, and improve error reporting. Update RawCommand to handle write exceptions and standardize console logging of responses. Minor test/usings whitespace cleanup. Add LICENSE and README.
1 parent 3e37a05 commit 79e426c

73 files changed

Lines changed: 4823 additions & 4684 deletions

Some content is hidden

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

FirmwareKit.Comm.Fastboot.Tests/AospFastbootDriverTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using FirmwareKit.Comm.Fastboot.DataModel;
2-
using FirmwareKit.Comm.Fastboot.Usb;
32
using System.Text;
4-
using Xunit;
53

64
namespace FirmwareKit.Comm.Fastboot.Tests
75
{
@@ -45,7 +43,7 @@ public void Test_GetVar_Aosp()
4543
transport.EnqueueResponse("OKAY0.4");
4644

4745
string output = util.GetVar("version");
48-
46+
4947
Assert.Equal("0.4", output);
5048
Assert.Contains("getvar:version", transport.WrittenCommands);
5149
}
@@ -61,7 +59,7 @@ public void Test_InfoMessage_Aosp()
6159
transport.EnqueueResponse("OKAY");
6260

6361
var response = util.RawCommand("oem dmesg");
64-
62+
6563
Assert.Equal(FastbootState.Success, response.Result);
6664
Assert.Single(response.Info);
6765
Assert.Equal("this is an info line", response.Info[0]);

FirmwareKit.Comm.Fastboot.Tests/FastbootInfoTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System.Text;
21
using System.Globalization;
2+
using System.Text;
33

44
namespace FirmwareKit.Comm.Fastboot.Tests
55
{
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
using FirmwareKit.Comm.Fastboot.DataModel;
1+
using FirmwareKit.Comm.Fastboot.DataModel;
22

3-
namespace FirmwareKit.Comm.Fastboot
3+
namespace FirmwareKit.Comm.Fastboot;
4+
5+
public partial class FastbootUtil
46
{
5-
public partial class FastbootUtil
7+
/// <summary>
8+
/// Sends and guides the kernel (not written to Flash)
9+
/// </summary>
10+
public FastbootResponse Boot(byte[] data)
611
{
7-
/// <summary>
8-
/// Sends and guides the kernel (not written to Flash)
9-
/// </summary>
10-
public FastbootResponse Boot(byte[] data)
11-
{
12-
DownloadData(data).ThrowIfError();
13-
return RawCommand("boot");
14-
}
12+
DownloadData(data).ThrowIfError();
13+
return RawCommand("boot");
1514
}
16-
}
15+
16+
17+
}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
using System.IO;
2-
using FirmwareKit.Comm.Fastboot.DataModel;
1+
using FirmwareKit.Comm.Fastboot.DataModel;
32

4-
namespace FirmwareKit.Comm.Fastboot
3+
namespace FirmwareKit.Comm.Fastboot;
4+
5+
public partial class FastbootUtil
56
{
6-
public partial class FastbootUtil
7+
/// <summary>
8+
/// Sends and guides the kernel image file
9+
/// </summary>
10+
public FastbootResponse Boot(string filePath)
711
{
8-
/// <summary>
9-
/// Sends and guides the kernel image file
10-
/// </summary>
11-
public FastbootResponse Boot(string filePath)
12-
{
13-
using var fs = File.OpenRead(filePath);
14-
DownloadData(fs, fs.Length).ThrowIfError();
15-
return RawCommand("boot");
16-
}
12+
using var fs = File.OpenRead(filePath);
13+
DownloadData(fs, fs.Length).ThrowIfError();
14+
return RawCommand("boot");
1715
}
18-
}
16+
17+
18+
}
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
using System.IO;
2-
using FirmwareKit.Comm.Fastboot.DataModel;
1+
using FirmwareKit.Comm.Fastboot.DataModel;
32

4-
namespace FirmwareKit.Comm.Fastboot
3+
namespace FirmwareKit.Comm.Fastboot;
4+
5+
public partial class FastbootUtil
56
{
6-
public partial class FastbootUtil
7+
/// <summary>
8+
/// Mixed packing and guiding the kernel
9+
/// </summary>
10+
public FastbootResponse Boot(string kernelPath, string? ramdiskPath = null, string? secondPath = null, string? dtbPath = null, string? cmdline = null, uint header_version = 0, uint base_addr = 0x10000000, uint page_size = 2048)
711
{
8-
/// <summary>
9-
/// Mixed packing and guiding the kernel
10-
/// </summary>
11-
public FastbootResponse Boot(string kernelPath, string? ramdiskPath = null, string? secondPath = null, string? dtbPath = null, string? cmdline = null, uint header_version = 0, uint base_addr = 0x10000000, uint page_size = 2048)
12-
{
13-
byte[] kernel = File.ReadAllBytes(kernelPath);
14-
byte[]? ramdisk = ramdiskPath != null ? File.ReadAllBytes(ramdiskPath) : null;
15-
byte[]? second = secondPath != null ? File.ReadAllBytes(secondPath) : null;
16-
byte[]? dtb = dtbPath != null ? File.ReadAllBytes(dtbPath) : null;
12+
byte[] kernel = File.ReadAllBytes(kernelPath);
13+
byte[]? ramdisk = ramdiskPath != null ? File.ReadAllBytes(ramdiskPath) : null;
14+
byte[]? second = secondPath != null ? File.ReadAllBytes(secondPath) : null;
15+
byte[]? dtb = dtbPath != null ? File.ReadAllBytes(dtbPath) : null;
1716

18-
byte[] bootImg = CreateBootImageVersioned(kernel, ramdisk, second, dtb, cmdline, null, header_version, base_addr, page_size);
19-
return Boot(bootImg);
20-
}
17+
byte[] bootImg = CreateBootImageVersioned(kernel, ramdisk, second, dtb, cmdline, null, header_version, base_addr, page_size);
18+
return Boot(bootImg);
2119
}
22-
}
20+
21+
22+
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using FirmwareKit.Comm.Fastboot.DataModel;
1+
using FirmwareKit.Comm.Fastboot.DataModel;
22

3-
namespace FirmwareKit.Comm.Fastboot
3+
namespace FirmwareKit.Comm.Fastboot;
4+
5+
public partial class FastbootUtil
46
{
5-
public partial class FastbootUtil
6-
{
7-
/// <summary>
8-
/// Continues the boot process
9-
/// </summary>
10-
public FastbootResponse Continue() => RawCommand("continue");
11-
}
12-
}
7+
/// <summary>
8+
/// Continues the boot process
9+
/// </summary>
10+
public FastbootResponse Continue() => RawCommand("continue");
11+
12+
13+
}
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
using FirmwareKit.Comm.Fastboot.DataModel;
1+
using FirmwareKit.Comm.Fastboot.DataModel;
22

3-
namespace FirmwareKit.Comm.Fastboot
3+
namespace FirmwareKit.Comm.Fastboot;
4+
5+
public partial class FastbootUtil
46
{
5-
public partial class FastbootUtil
7+
/// <summary>
8+
/// Creates a logical partition
9+
/// </summary>
10+
public FastbootResponse CreateLogicalPartition(string partition, long size)
611
{
7-
/// <summary>
8-
/// Creates a logical partition
9-
/// </summary>
10-
public FastbootResponse CreateLogicalPartition(string partition, long size)
11-
{
12-
EnsureUserspace();
13-
return RawCommand($"create-logical-partition:{partition}:{size}");
14-
}
12+
EnsureUserspace();
13+
return RawCommand($"create-logical-partition:{partition}:{size}");
1514
}
16-
}
15+
16+
17+
}
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
using FirmwareKit.Comm.Fastboot.DataModel;
1+
using FirmwareKit.Comm.Fastboot.DataModel;
22

3-
namespace FirmwareKit.Comm.Fastboot
3+
namespace FirmwareKit.Comm.Fastboot;
4+
5+
public partial class FastbootUtil
46
{
5-
public partial class FastbootUtil
7+
/// <summary>
8+
/// Deletes a logical partition
9+
/// </summary>
10+
public FastbootResponse DeleteLogicalPartition(string partition)
611
{
7-
/// <summary>
8-
/// Deletes a logical partition
9-
/// </summary>
10-
public FastbootResponse DeleteLogicalPartition(string partition)
11-
{
12-
EnsureUserspace();
13-
return RawCommand($"delete-logical-partition:{partition}");
14-
}
12+
EnsureUserspace();
13+
return RawCommand($"delete-logical-partition:{partition}");
1514
}
16-
}
15+
16+
17+
}
Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
using System;
2-
using System.Security.Cryptography;
3-
using System.Text;
4-
using FirmwareKit.Comm.Fastboot.DataModel;
1+
using FirmwareKit.Comm.Fastboot.DataModel;
52

6-
namespace FirmwareKit.Comm.Fastboot
3+
namespace FirmwareKit.Comm.Fastboot;
4+
5+
public partial class FastbootUtil
76
{
8-
public partial class FastbootUtil
7+
/// <summary>
8+
/// Downloads data
9+
/// </summary>
10+
public FastbootResponse DownloadData(byte[] data)
911
{
10-
/// <summary>
11-
/// Downloads data
12-
/// </summary>
13-
public FastbootResponse DownloadData(byte[] data)
14-
{
15-
FastbootResponse response = RawCommand("download:" + data.Length.ToString("x8"));
16-
if (response.Result != FastbootState.Data)
17-
return response;
12+
FastbootResponse response = RawCommand("download:" + data.Length.ToString("x8"));
13+
if (response.Result != FastbootState.Data)
14+
return response;
1815

19-
long written = Transport.Write(data, data.Length);
20-
if (written != data.Length)
21-
{
22-
return new FastbootResponse { Result = FastbootState.Fail, Response = $"Short write: {written}/{data.Length}" };
23-
}
24-
25-
return HandleResponse();
16+
long written = Transport.Write(data, data.Length);
17+
if (written != data.Length)
18+
{
19+
return new FastbootResponse { Result = FastbootState.Fail, Response = $"Short write: {written}/{data.Length}" };
2620
}
21+
22+
return HandleResponse();
2723
}
28-
}
24+
25+
26+
}
Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
1-
using System;
2-
using System.IO;
3-
using System.Security.Cryptography;
4-
using FirmwareKit.Comm.Fastboot.DataModel;
1+
using FirmwareKit.Comm.Fastboot.DataModel;
52

6-
namespace FirmwareKit.Comm.Fastboot
3+
namespace FirmwareKit.Comm.Fastboot;
4+
5+
public partial class FastbootUtil
76
{
8-
public partial class FastbootUtil
7+
/// <summary>
8+
/// Downloads data
9+
/// </summary>
10+
public FastbootResponse DownloadData(Stream stream, long length, bool onEvent = true)
911
{
10-
/// <summary>
11-
/// Downloads data
12-
/// </summary>
13-
public FastbootResponse DownloadData(Stream stream, long length, bool onEvent = true)
12+
// AOSP uses %08" PRIx32 which is 8 chars hex with leading zeros
13+
FastbootResponse response = RawCommand("download:" + length.ToString("x8"));
14+
if (response.Result != FastbootState.Data)
15+
return response;
16+
17+
byte[] buffer = new byte[OnceSendDataSize];
18+
long bytesWritten = 0;
19+
while (bytesWritten < length)
1420
{
15-
// AOSP uses %08" PRIx32 which is 8 chars hex with leading zeros
16-
FastbootResponse response = RawCommand("download:" + length.ToString("x8"));
17-
if (response.Result != FastbootState.Data)
18-
return response;
21+
int toRead = (int)Math.Min(OnceSendDataSize, length - bytesWritten);
22+
int readSize = stream.Read(buffer, 0, toRead);
23+
if (readSize <= 0) break;
1924

20-
byte[] buffer = new byte[OnceSendDataSize];
21-
long bytesWritten = 0;
22-
while (bytesWritten < length)
25+
long written = Transport.Write(buffer, readSize);
26+
if (written != readSize)
2327
{
24-
int toRead = (int)Math.Min(OnceSendDataSize, length - bytesWritten);
25-
int readSize = stream.Read(buffer, 0, toRead);
26-
if (readSize <= 0) break;
27-
28-
long written = Transport.Write(buffer, readSize);
29-
if (written != readSize)
30-
{
31-
return new FastbootResponse { Result = FastbootState.Fail, Response = $"Short write: {written}/{readSize}" };
32-
}
33-
bytesWritten += written;
34-
if (onEvent)
35-
NotifyProgress(bytesWritten, length);
28+
return new FastbootResponse { Result = FastbootState.Fail, Response = $"Short write: {written}/{readSize}" };
3629
}
37-
38-
return HandleResponse();
30+
bytesWritten += written;
31+
if (onEvent)
32+
NotifyProgress(bytesWritten, length);
3933
}
34+
35+
return HandleResponse();
4036
}
41-
}
37+
38+
39+
}

0 commit comments

Comments
 (0)