Skip to content

Commit cda463c

Browse files
v2.1.0 (#49)
Widevine, rm BID & SN; read the factory key (WVL)
1 parent 1ae13f1 commit cda463c

17 files changed

+163
-125
lines changed

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[submodule "Potato.ImageFlasher"]
2+
path = Potato.ImageFlasher
3+
url = https://github.com/mashed-potatoes/Potato.ImageFlasher.git
4+
[submodule "Potato.Fastboot"]
5+
path = Potato.Fastboot
6+
url = https://github.com/mashed-potatoes/Potato.Fastboot.git
7+
[submodule "HiSiBootloaders"]
8+
path = HiSiBootloaders
9+
url = https://github.com/mashed-potatoes/HiSiBootloaders.git

HiSiBootloaders

Submodule HiSiBootloaders added at 013b9ae

Potato.Fastboot

Submodule Potato.Fastboot added at 6267600

Potato.ImageFlasher

Submodule Potato.ImageFlasher added at 49b4ab4

PotatoNV-next.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ VisualStudioVersion = 16.0.30011.22
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PotatoNV-next", "PotatoNV-next\PotatoNV-next.csproj", "{2F103DCF-DFBA-48B0-BEA4-D4B845532A42}"
77
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Potato.Fastboot", "Potato.Fastboot\Potato.Fastboot\Potato.Fastboot.csproj", "{6C04F303-A659-4872-8136-D5C1D48A8F75}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Potato.ImageFlasher", "Potato.ImageFlasher\Potato.ImageFlasher\Potato.ImageFlasher.csproj", "{B74D03CC-AC13-447A-BCD8-C2044DA84EEB}"
11+
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1014
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +25,22 @@ Global
2125
{2F103DCF-DFBA-48B0-BEA4-D4B845532A42}.Release|Any CPU.Build.0 = Release|Any CPU
2226
{2F103DCF-DFBA-48B0-BEA4-D4B845532A42}.Release|x64.ActiveCfg = Release|x64
2327
{2F103DCF-DFBA-48B0-BEA4-D4B845532A42}.Release|x64.Build.0 = Release|x64
28+
{6C04F303-A659-4872-8136-D5C1D48A8F75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{6C04F303-A659-4872-8136-D5C1D48A8F75}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{6C04F303-A659-4872-8136-D5C1D48A8F75}.Debug|x64.ActiveCfg = Debug|Any CPU
31+
{6C04F303-A659-4872-8136-D5C1D48A8F75}.Debug|x64.Build.0 = Debug|Any CPU
32+
{6C04F303-A659-4872-8136-D5C1D48A8F75}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{6C04F303-A659-4872-8136-D5C1D48A8F75}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{6C04F303-A659-4872-8136-D5C1D48A8F75}.Release|x64.ActiveCfg = Release|Any CPU
35+
{6C04F303-A659-4872-8136-D5C1D48A8F75}.Release|x64.Build.0 = Release|Any CPU
36+
{B74D03CC-AC13-447A-BCD8-C2044DA84EEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{B74D03CC-AC13-447A-BCD8-C2044DA84EEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{B74D03CC-AC13-447A-BCD8-C2044DA84EEB}.Debug|x64.ActiveCfg = Debug|Any CPU
39+
{B74D03CC-AC13-447A-BCD8-C2044DA84EEB}.Debug|x64.Build.0 = Debug|Any CPU
40+
{B74D03CC-AC13-447A-BCD8-C2044DA84EEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{B74D03CC-AC13-447A-BCD8-C2044DA84EEB}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{B74D03CC-AC13-447A-BCD8-C2044DA84EEB}.Release|x64.ActiveCfg = Release|Any CPU
43+
{B74D03CC-AC13-447A-BCD8-C2044DA84EEB}.Release|x64.Build.0 = Release|Any CPU
2444
EndGlobalSection
2545
GlobalSection(SolutionProperties) = preSolution
2646
HideSolutionNode = FALSE

PotatoNV-next/Controls/NVForm.xaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,10 @@
1414
<Label Content="Bootloader" />
1515
<ComboBox Name="deviceBootloader" />
1616
</StackPanel>
17-
<StackPanel>
18-
<Label Content="Serial number (optional)" />
19-
<TextBox x:Name="nvSerialNumber" />
20-
</StackPanel>
21-
<StackPanel>
22-
<Label Content="Board ID (optional)" />
23-
<TextBox x:Name="nvBidNumber" />
24-
</StackPanel>
2517
<StackPanel>
2618
<Label Content="Unlock code" />
2719
<TextBox x:Name="nvUnlockCode" />
2820
</StackPanel>
29-
3021
<StackPanel>
3122
<CheckBox x:Name="disableFBLOCK" Content="Disable FBLOCK" IsChecked="True" />
3223
<Button x:Name="startButton" Content="Start!" Height="40" Margin="0,10,0,0" Click="StartButton_Click" />

PotatoNV-next/Controls/NVForm.xaml.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public class FormEventArgs : EventArgs
4848
{
4949
public UsbController.Device.DMode TargetMode { get; set; }
5050
public string Target { get; set; }
51-
public string BoardID { get; set; }
5251
public string UnlockCode { get; set; }
53-
public string SerialNumber { get; set; }
5452
public bool DisableFBLOCK { get; set; }
5553
public Bootloader Bootloader { get; set; } = null;
5654
}
@@ -113,10 +111,6 @@ private void StartButton_Click(object sender, RoutedEventArgs e)
113111

114112
Assert(deviceBootloader.SelectedIndex != -1, "Couldn't find any valid bootloader!");
115113

116-
Assert(VerifyNVValue(nvSerialNumber.Text), "Serial number is not valid.");
117-
118-
Assert(VerifyNVValue(nvBidNumber.Text), "BoardID is not valid.");
119-
120114
Assert(VerifyNVValue(nvUnlockCode.Text, true), "Unlock code is not valid.");
121115
}
122116
catch
@@ -132,9 +126,7 @@ private void StartButton_Click(object sender, RoutedEventArgs e)
132126
? UsbController.Device.DMode.Fastboot
133127
: UsbController.Device.DMode.DownloadVCOM,
134128
Target = deviceList.SelectedItem.ToString(),
135-
BoardID = nvBidNumber.Text,
136129
UnlockCode = nvUnlockCode.Text,
137-
SerialNumber = nvSerialNumber.Text,
138130
DisableFBLOCK = disableFBLOCK.IsChecked.Value
139131
};
140132

@@ -150,8 +142,6 @@ private void NVForm_IsEnabledChanged(object sender, DependencyPropertyChangedEve
150142
{
151143
deviceList.IsEnabled = IsEnabled;
152144
deviceBootloader.IsEnabled = IsEnabled;
153-
nvBidNumber.IsEnabled = IsEnabled;
154-
nvSerialNumber.IsEnabled = IsEnabled;
155145
nvUnlockCode.IsEnabled = IsEnabled;
156146
disableFBLOCK.IsEnabled = IsEnabled;
157147
startButton.IsEnabled = IsEnabled;

PotatoNV-next/Core.cs

Lines changed: 83 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Core
1818
private Fastboot fb;
1919
private Controls.NVForm.FormEventArgs args;
2020

21+
private void LogResponse(Fastboot.Response response)
22+
{
23+
Log.Debug($"response: {Encoding.UTF8.GetString(response.RawData)}");
24+
}
25+
2126
private void FlashBootloader(Bootloader bootloader, string port)
2227
{
2328
var flasher = new ImageFlasher();
@@ -27,23 +32,20 @@ private void FlashBootloader(Bootloader bootloader, string port)
2732
int asize = 0, dsize = 0;
2833

2934
foreach (var image in bootloader.Images)
30-
{
31-
Log.Debug($"VrStat of {image.Role}: {image.IsValid}");
32-
35+
{
3336
if (!image.IsValid)
3437
{
35-
throw new Exception($"Image `{image.Role}` is invalid!");
38+
throw new Exception($"Image `{image.Role}` is not valid!");
3639
}
3740

3841
asize += image.Size;
3942
}
4043

41-
Log.Success("Verification passed!");
4244
Log.Debug($"Opening {port}...");
4345

4446
flasher.Open(port);
4547

46-
Log.Info($"Uploading {bootloader.Name} bootloader");
48+
Log.Info($"Uploading {bootloader.Name}...");
4749

4850
foreach (var image in bootloader.Images)
4951
{
@@ -60,39 +62,43 @@ private void FlashBootloader(Bootloader bootloader, string port)
6062

6163
flasher.Close();
6264

63-
Log.Success("Bootloader uploaded");
6465
Log.SetProgressBar(false);
6566
}
6667

6768
private void ReadInfo()
6869
{
6970
var serial = fb.GetSerialNumber();
70-
Log.Info($"- Serial number: {serial}");
71+
Log.Info($"Serial number: {serial}");
7172

7273
var bsn = fb.Command("oem read_bsn");
73-
Log.Info($"- Board ID: {bsn.Payload}");
74+
if (bsn.Status == Fastboot.Status.Okay)
75+
{
76+
Log.Info($"Board ID: {bsn.Payload}");
77+
}
7478

7579
var model = fb.Command("oem get-product-model");
76-
Log.Info($"- Model: {model.Payload}");
80+
Log.Info($"Model: {model.Payload}");
7781

7882
var build = fb.Command("oem get-build-number");
79-
Log.Info($"- Build number: {build.Payload.Replace(":", "")}");
83+
Log.Info($"Build number: {build.Payload.Replace(":", "")}");
8084

8185
var regex = new Regex(@"FB[\w: ]{1,}UNLOCKED");
8286
var fblock = fb.Command("oem lock-state info");
8387
var state = regex.IsMatch(fblock.Payload);
8488

85-
Log.Info($"- FBLOCK state: {(state ? "unlocked" : "locked")}");
89+
Log.Info($"FBLOCK state: {(state ? "unlocked" : "locked")}");
90+
LogResponse(fblock);
8691

8792
if (!state)
8893
{
89-
throw new Exception("FBLOCK is locked!");
94+
Log.Error("FBLOCK is locked!");
95+
// throw new Exception("FBLOCK is locked!");
9096
}
9197
}
9298

93-
private void SetNVMEProp(string prop, byte[] value, string role = null)
99+
private void SetNVMEProp(string prop, byte[] value)
94100
{
95-
Log.Info($"- Writing {role ?? prop}");
101+
Log.Info($"Writing {prop}...");
96102

97103
var cmd = new List<byte>();
98104

@@ -101,17 +107,14 @@ private void SetNVMEProp(string prop, byte[] value, string role = null)
101107

102108
var res = fb.Command(cmd.ToArray());
103109

110+
LogResponse(res);
111+
104112
if (!res.Payload.Contains("set nv ok"))
105113
{
106-
throw new Exception($"Failed to set prop: {res.Payload}");
114+
throw new Exception($"Failed to set: {res.Payload}");
107115
}
108116
}
109117

110-
private void SetNVMEProp(string prop, string value, string role = null)
111-
{
112-
SetNVMEProp(prop, Encoding.ASCII.GetBytes(value), role);
113-
}
114-
115118
public static byte[] GetSHA256(string str)
116119
{
117120
using (var sha256 = SHA256.Create())
@@ -120,20 +123,66 @@ public static byte[] GetSHA256(string str)
120123
}
121124
}
122125

123-
private void WriteNVME()
126+
private void SetHWDogCertify(byte state)
124127
{
125-
SetNVMEProp("FBLOCK", new[] { (byte)(args.DisableFBLOCK ? 0 : 1) }, "FBLOCK state");
128+
foreach (var command in new[] { "hwdog certify set", "backdoor set" })
129+
{
130+
Log.Info($"Trying {command}...");
131+
var res = fb.Command($"oem {command} {state}");
132+
LogResponse(res);
133+
if (res.Status == Fastboot.Status.Okay || res.Payload.Contains("equal"))
134+
{
135+
Log.Success($"{command}: success");
136+
return;
137+
}
138+
}
139+
Log.Error("Failed to set FBLOCK state!");
140+
}
126141

127-
SetNVMEProp("USRKEY", GetSHA256(args.UnlockCode), "User key");
142+
private void WidevineLock()
143+
{
144+
Log.Debug("WV Lock");
145+
var res = fb.Command("getvar:nve:WVLOCK");
146+
LogResponse(res);
147+
if (res.Status != Fastboot.Status.Fail && res.Payload.Replace("\n", "").Trim() != "UUUUUUUUUUUUUUUU")
148+
{
149+
Log.Info($"Read factory key: {res.Payload}");
150+
}
128151

129-
if (!string.IsNullOrWhiteSpace(args.SerialNumber))
152+
try
130153
{
131-
SetNVMEProp("SN", args.SerialNumber, "Serial number");
154+
SetNVMEProp("WVLOCK", Encoding.ASCII.GetBytes(args.UnlockCode));
155+
}
156+
catch
157+
{
158+
Log.Error("Failed to set the WVLOCK.");
159+
}
160+
}
161+
162+
private void WriteNVME()
163+
{
164+
var fblockState = (byte)(args.DisableFBLOCK ? 0 : 1);
165+
166+
try
167+
{
168+
SetNVMEProp("FBLOCK", new[] { fblockState });
169+
}
170+
catch (Exception ex)
171+
{
172+
Log.Error("Failed to set the FBLOCK, using the alternative method...");
173+
Log.Debug(ex.Message);
174+
SetHWDogCertify(fblockState);
132175
}
133176

134-
if (!string.IsNullOrWhiteSpace(args.BoardID))
177+
try
178+
{
179+
SetNVMEProp("USRKEY", GetSHA256(args.UnlockCode));
180+
WidevineLock();
181+
}
182+
catch (Exception ex)
135183
{
136-
SetNVMEProp("BOARDID", args.BoardID, "Board ID");
184+
Log.Error("Failed to set the key.");
185+
Log.Debug(ex.Message);
137186
}
138187
}
139188

@@ -146,28 +195,23 @@ private void Worker_DoWork(object sender, DoWorkEventArgs e)
146195
{
147196
if (args.TargetMode == UsbController.Device.DMode.DownloadVCOM)
148197
{
149-
Log.Info("--> Flashing bootloader");
150198
FlashBootloader(args.Bootloader, args.Target.Split(':')[0]);
151199

152200
Log.Info("Waiting for any device...");
153201
fb.Wait();
154202
}
155203

156-
Log.Info("--> Reading information");
157-
Log.Info("Connecting to fastboot device...");
204+
Log.Info("Connecting...");
158205

159206
fb.Connect();
160207
ReadInfo();
161-
162-
Log.Info("--> Updating NVME");
163208
WriteNVME();
164209

165-
Log.Success("Update done!");
166210
Log.Info("Rebooting...");
167211

168212
fb.Command("reboot");
169213

170-
Log.Info($"Bootloader unlock code: {args.UnlockCode}");
214+
Log.Info($"New bootloader unlock code: {args.UnlockCode}");
171215

172216
fb.Disconnect();
173217
}
@@ -176,6 +220,10 @@ private void Worker_DoWork(object sender, DoWorkEventArgs e)
176220
Log.Error(ex.Message);
177221
Log.Debug(ex.StackTrace);
178222
}
223+
finally
224+
{
225+
fb.Disconnect();
226+
}
179227
}
180228

181229
public void StartProcess(Controls.NVForm.FormEventArgs args)

PotatoNV-next/MainWindow.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
</Style>
1818

1919
<Style TargetType="TextBox">
20-
<Setter Property="FontFamily" Value="Consolas" />
2120
<Setter Property="MaxLength" Value="16" />
2221
</Style>
2322

PotatoNV-next/MainWindow.xaml.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam
5454

5555
public MainWindow()
5656
{
57-
IntegrityCheck.Run();
58-
5957
Icon = MediaConverter.ImageSourceFromBitmap(Properties.Resources.Fire.ToBitmap());
6058
InitializeComponent();
6159

0 commit comments

Comments
 (0)