@@ -31,14 +31,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
31
31
// then the device is not pretected. For now though, let's assume the device is in normal mode and not in dfu mode.
32
32
33
33
return this . _withDevice ( async ( ) => {
34
- let s ;
35
- try {
36
- s = await this . device . getProtectionState ( ) ;
37
- } catch ( error ) {
38
- if ( error . message === 'Not supported' ) {
39
- throw new Error ( `Device protection feature is not supported on this device` ) ;
40
- }
41
- }
34
+ const s = await this . _getDeviceProtection ( ) ;
42
35
43
36
let res ;
44
37
if ( ! s . protected && ! s . overridden ) {
@@ -57,16 +50,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
57
50
58
51
async disableProtection ( { open } = { } ) {
59
52
return this . _withDevice ( async ( ) => {
60
- let s ;
61
- try {
62
- s = await this . device . getProtectionState ( ) ;
63
- } catch ( error ) {
64
- if ( error . message === 'Not supported' ) {
65
- throw new Error ( `Device protection feature is not supported on this device${ os . EOL } ` ) ;
66
- }
67
- }
53
+ const s = await this . _getDeviceProtection ( ) ;
54
+
68
55
if ( ! s . protected && ! s . overridden ) {
69
- this . ui . stdout . write ( `Device is not protected${ os . EOL } ` ) ;
56
+ this . ui . stdout . write ( `Device is not protected` ) ;
70
57
return ;
71
58
}
72
59
@@ -79,22 +66,22 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
79
66
// console.log(`CLI -> Device:\n\tserver_nonce=${serverNonce.toString('base64')}`);
80
67
r = await this . device . unprotectDevice ( { action : 'prepare' , serverNonce } ) ;
81
68
if ( ! r . protected ) {
82
- console . log ( 'Device is not protected' ) ;
83
- return ;
69
+ this . ui . stdout . write ( 'Device is not protected' ) ;
70
+ return ;
84
71
}
85
72
const { deviceNonce, deviceSignature, devicePublicKeyFingerprint } = r ;
86
73
// console.log(`Device -> CLI:\n\tdevice_signature=${deviceSignature.toString('base64')}`);
87
74
88
75
// Verify the device signature and get a server signature
89
76
// console.log(`CLI -> Server:\n\tdevice_signature=${deviceSignature.toString('base64')}`);
90
77
r = await this . api . unprotectDevice ( {
91
- deviceId : this . deviceId ,
92
- action : 'confirm' ,
93
- serverNonce : serverNonce . toString ( 'base64' ) ,
94
- deviceNonce : deviceNonce . toString ( 'base64' ) ,
95
- deviceSignature : deviceSignature . toString ( 'base64' ) ,
96
- devicePublicKeyFingerprint : devicePublicKeyFingerprint . toString ( 'base64' ) ,
97
- auth : settings . access_token
78
+ deviceId : this . deviceId ,
79
+ action : 'confirm' ,
80
+ serverNonce : serverNonce . toString ( 'base64' ) ,
81
+ deviceNonce : deviceNonce . toString ( 'base64' ) ,
82
+ deviceSignature : deviceSignature . toString ( 'base64' ) ,
83
+ devicePublicKeyFingerprint : devicePublicKeyFingerprint . toString ( 'base64' ) ,
84
+ auth : settings . access_token
98
85
} ) ;
99
86
100
87
const serverSignature = Buffer . from ( r . server_signature , 'base64' ) ;
@@ -104,25 +91,22 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
104
91
// Unprotect the device
105
92
await this . device . unprotectDevice ( { action : 'confirm' , serverSignature, serverPublicKeyFingerprint } ) ;
106
93
107
- s = await this . device . getProtectionState ( ) ;
94
+ s = await this . _getDeviceProtection ( ) ;
108
95
if ( ! open ) {
109
96
this . ui . stdout . write ( `Device protection temporarily disabled.${ os . EOL } Device is put into service mode for 20 reboots or 24 hours.${ os . EOL } ` ) ;
110
97
return ;
111
98
}
112
99
113
- if ( open ) {
114
-
115
- const localBootloaderPath = await this . _downloadBootloader ( ) ;
100
+ const localBootloaderPath = await this . _downloadBootloader ( ) ;
116
101
117
- await this . _flashBootloader ( localBootloaderPath , 'disable' ) ;
102
+ await this . _flashBootloader ( localBootloaderPath , 'disable' ) ;
118
103
119
- this . ui . stdout . write ( `Device protection disabled.${ os . EOL } Device is open${ os . EOL } ` ) ;
104
+ this . ui . stdout . write ( `Device protection disabled.${ os . EOL } Device is open${ os . EOL } ` ) ;
120
105
121
- const success = await this . _markAsDevelopmentDevice ( true ) ;
106
+ const success = await this . _markAsDevelopmentDevice ( true ) ;
122
107
123
- if ( ! success ) {
124
- this . ui . stdout . write ( `Failed to mark device as development device. Protection will be automatically enabled after a power cycle${ os . EOL } ` ) ;
125
- }
108
+ if ( ! success ) {
109
+ this . ui . stdout . write ( `Failed to mark device as development device. Protection will be automatically enabled after a power cycle${ os . EOL } ` ) ;
126
110
}
127
111
} ) ;
128
112
}
@@ -155,15 +139,8 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
155
139
let protectedBinary = file ;
156
140
157
141
return this . _withDevice ( async ( ) => {
158
- let s ;
159
- try {
160
- s = await this . device . getProtectionState ( ) ;
161
- } catch ( error ) {
162
- if ( error . message === 'Not supported' ) {
163
- throw new Error ( `Device protection feature is not supported on this device` ) ;
164
- }
165
- }
166
-
142
+ const s = await this . _getDeviceProtection ( ) ;
143
+
167
144
const attrs = await this . api . getDeviceAttributes ( this . deviceId ) ;
168
145
let deviceProtectionActiveInProduct = false ;
169
146
if ( attrs . platform_id !== attrs . product_id ) {
@@ -209,6 +186,17 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
209
186
} ) ;
210
187
}
211
188
189
+ async _getDeviceProtection ( ) {
190
+ try {
191
+ const s = await this . device . getProtectionState ( ) ;
192
+ return s ;
193
+ } catch ( error ) {
194
+ if ( error . message === 'Not supported' ) {
195
+ throw new Error ( `Device protection feature is not supported on this device${ os . EOL } ` ) ;
196
+ }
197
+ }
198
+ }
199
+
212
200
async _flashBootloader ( path , action ) {
213
201
let msg ;
214
202
switch ( action ) {
0 commit comments