@@ -145,24 +145,24 @@ func NewDevice(
145
145
146
146
// info uses the opInfo api endpoint to learn about the version, platform/edition, and unlock
147
147
// status (true if unlocked).
148
- func (device * Device ) info () (* semver.SemVer , common.Product , bool , error ) {
148
+ func (device * Device ) info () (* semver.SemVer , common.Product , bool , bool , error ) {
149
149
150
150
// CAREFUL: hwwInfo is called on the raw transport, not on device.rawQuery, which behaves
151
151
// differently depending on the firmware version. Reason: the version is not
152
152
// available (this call is used to get the version), so it must work for all firmware versions.
153
153
response , err := device .communication .Query ([]byte (hwwInfo ))
154
154
if err != nil {
155
- return nil , "" , false , err
155
+ return nil , "" , false , false , err
156
156
}
157
157
158
158
if len (response ) < 4 {
159
- return nil , "" , false , errp .New ("unexpected response" )
159
+ return nil , "" , false , false , errp .New ("unexpected response" )
160
160
}
161
161
versionStrLen , response := int (response [0 ]), response [1 :]
162
162
versionBytes , response := response [:versionStrLen ], response [versionStrLen :]
163
163
version , err := semver .NewSemVerFromString (string (versionBytes ))
164
164
if err != nil {
165
- return nil , "" , false , err
165
+ return nil , "" , false , false , err
166
166
}
167
167
platformByte , response := response [0 ], response [1 :]
168
168
editionByte , response := response [0 ], response [1 :]
@@ -175,24 +175,36 @@ func (device *Device) info() (*semver.SemVer, common.Product, bool, error) {
175
175
}
176
176
editions , ok := products [platformByte ]
177
177
if ! ok {
178
- return nil , "" , false , errp .Newf ("unrecognized platform: %v" , platformByte )
178
+ return nil , "" , false , false , errp .Newf ("unrecognized platform: %v" , platformByte )
179
179
}
180
180
product , ok := editions [editionByte ]
181
181
if ! ok {
182
- return nil , "" , false , errp .Newf ("unrecognized platform/edition: %v/%v" , platformByte , editionByte )
182
+ return nil , "" , false , false , errp .Newf ("unrecognized platform/edition: %v/%v" , platformByte , editionByte )
183
183
}
184
184
185
185
var unlocked bool
186
- unlockedByte := response [0 ]
186
+ unlockedByte , response := response [0 ], response [ 1 : ]
187
187
switch unlockedByte {
188
188
case 0x00 :
189
189
unlocked = false
190
190
case 0x01 :
191
191
unlocked = true
192
192
default :
193
- return nil , "" , false , errp .New ("unexpected reply" )
193
+ return nil , "" , false , false , errp .New ("unexpected reply" )
194
194
}
195
- return version , product , unlocked , nil
195
+
196
+ var initialized bool
197
+ initializedByte := response [0 ]
198
+ switch initializedByte {
199
+ case 0x00 :
200
+ initialized = false
201
+ case 0x01 :
202
+ initialized = true
203
+ default :
204
+ return nil , "" , false , false , errp .New ("unexpected reply" )
205
+ }
206
+
207
+ return version , product , unlocked , initialized , nil
196
208
}
197
209
198
210
// Version returns the firmware version.
@@ -206,14 +218,9 @@ func (device *Device) Version() *semver.SemVer {
206
218
// inferVersionAndProduct either sets the version and product by using OP_INFO if they were not
207
219
// provided. In this case, the firmware is assumed to be >=v4.3.0, before that OP_INFO was not
208
220
// available.
209
- func (device * Device ) inferVersionAndProduct () error {
221
+ func (device * Device ) inferVersionAndProduct (version * semver. SemVer , product common. Product ) error {
210
222
// The version has not been provided, so we try to get it from OP_INFO.
211
223
if device .version == nil {
212
- version , product , _ , err := device .info ()
213
- if err != nil {
214
- return errp .New (
215
- "OP_INFO unavailable; need to provide version and product via the USB HID descriptor" )
216
- }
217
224
device .log .Info (fmt .Sprintf ("OP_INFO: version=%s, product=%s" , version , product ))
218
225
219
226
// sanity check
@@ -241,11 +248,25 @@ func (device *Device) Init() error {
241
248
device .channelHashDeviceVerified = false
242
249
device .sendCipher = nil
243
250
device .receiveCipher = nil
244
- device .changeStatus (StatusConnected )
245
251
246
- if err := device .inferVersionAndProduct (); err != nil {
247
- return err
252
+ version , product , unlocked , initialized , err := device .info ()
253
+ if err != nil {
254
+ return errp .New (
255
+ "OP_INFO unavailable; need to provide version and product via the USB HID descriptor" )
256
+ }
257
+ // The semantics of the firmware and status.Status are not the same.
258
+ // status.Status StatusInitialized means the device is unlocked.
259
+ // The firmware initialized = true means the device can be unlocked.
260
+ if unlocked {
261
+ device .changeStatus (StatusInitialized )
262
+ } else if initialized {
263
+ device .changeStatus (StatusConnected )
264
+ } else {
265
+ device .changeStatus (StatusUninitialized )
248
266
}
267
+
268
+ device .inferVersionAndProduct (version , product )
269
+
249
270
if device .version .AtLeast (lowestNonSupportedFirmwareVersion ) {
250
271
device .changeStatus (StatusRequireAppUpgrade )
251
272
return nil
@@ -261,7 +282,7 @@ func (device *Device) Init() error {
261
282
262
283
// Before 2.0.0, unlock was invoked automatically by the device before USB communication
263
284
// started.
264
- if device .version .AtLeast (semver .NewSemVer (2 , 0 , 0 )) {
285
+ if ( device .version .AtLeast (semver .NewSemVer (2 , 0 , 0 )) && ( device . status != StatusInitialized )) {
265
286
_ , err := device .rawQuery ([]byte (opUnlock ))
266
287
if err != nil {
267
288
// Most likely the device has been unplugged.
0 commit comments