@@ -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 )
0 commit comments