-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fw-updater): protect against hardware/firmware mismatches (and t…
…herefore hard bricks) by analysing firmware files Note: Compare with the SKU reported by the earbuds instead of BluetoothImpl.ActiveModel because users can spoof the device model during setup
- Loading branch information
1 parent
77f9d81
commit 8375c23
Showing
21 changed files
with
272 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
using GalaxyBudsClient.Model.Attributes; | ||
using GalaxyBudsClient.Model.Constants; | ||
|
||
namespace GalaxyBudsClient.Message.Decoder | ||
{ | ||
class DebugSkuParser : BaseMessageParser | ||
public class DebugSkuParser : BaseMessageParser | ||
{ | ||
public override SPPMessage.MessageIds HandledType => SPPMessage.MessageIds.DEBUG_SKU; | ||
|
||
public char a { set; get; } | ||
public char b { set; get; } | ||
public char c { set; get; } | ||
public char d { set; get; } | ||
public string? LeftSku { set; get; } | ||
public string? RightSku { set; get; } | ||
|
||
public override void ParseMessage(SPPMessage msg) | ||
{ | ||
if (msg.Id != HandledType) | ||
return; | ||
|
||
a = (char)msg.Payload[12]; | ||
b = (char)msg.Payload[13]; | ||
c = (char)msg.Payload[26]; | ||
d = (char)msg.Payload[27]; | ||
var payload = msg.Payload; | ||
if (payload.Length >= 14) | ||
{ | ||
LeftSku = Encoding.UTF8.GetString(payload, 0, 14); | ||
} | ||
if (payload.Length >= 14 * 2) | ||
{ | ||
RightSku = Encoding.UTF8.GetString(payload, 14, 14); | ||
} | ||
} | ||
|
||
|
||
public Models? ModelFromSku() | ||
{ | ||
var build = LeftSku ?? RightSku; | ||
if (build == null) | ||
return null; | ||
|
||
foreach (var model in Enum.GetValues<Models>()) | ||
{ | ||
var pattern = model.GetModelMetadata()?.FwPattern; | ||
if(pattern == null) | ||
continue; | ||
|
||
if (build.Contains(pattern)) | ||
return model; | ||
} | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
GalaxyBudsClient/Model/Attributes/ModelMetadataAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Linq; | ||
|
||
namespace GalaxyBudsClient.Model.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Field)] | ||
internal class ModelMetadataAttribute : Attribute | ||
{ | ||
/** | ||
* Friendly name, used for display purposes | ||
*/ | ||
public required string Name { get; set; } | ||
/** | ||
* Used to detect corresponding device models for firmware update files | ||
* Firmware archives do not contain model information in their headers, | ||
* so we check whether the pattern matches the binary | ||
*/ | ||
public required string FwPattern { get; set; } | ||
/** | ||
* Used to detect corresponding device models from build strings found in DEBUG_GET_ALL_DATA | ||
*/ | ||
public required string BuildPrefix { get; set; } | ||
} | ||
|
||
// TODO: all of these Get<Attribute> methods are very similar, they could be refactored into a single method | ||
internal static class ModelMetadataAttributeExtension | ||
{ | ||
public static ModelMetadataAttribute? GetModelMetadata<T>(this T e) where T : IConvertible | ||
{ | ||
if (e is not Enum) | ||
return null; | ||
|
||
var type = e.GetType(); | ||
foreach (var obj in Enum.GetValues(type)) | ||
{ | ||
if (obj == null || (int)obj != e.ToInt32(CultureInfo.InvariantCulture)) | ||
continue; | ||
|
||
var memInfo = type.GetMember(type.GetEnumName((int) obj) ?? string.Empty); | ||
|
||
if (memInfo[0] | ||
.GetCustomAttributes(typeof(ModelMetadataAttribute), false) | ||
.FirstOrDefault() is ModelMetadataAttribute attribute) | ||
{ | ||
return attribute; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.