@@ -258,15 +258,15 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
258
258
Log .v (TAG , "startAddress: " + bytesToHex (startAddress ) + " endAddress: " + bytesToHex (endAddress ));
259
259
260
260
if ( notificationValue [1 ] == REGION_MAKECODE ) {
261
- code_startAddress = Byte .toUnsignedLong (startAddress [ 0 ])
262
- + Byte .toUnsignedLong (startAddress [ 0 ]) * 256
263
- + Byte .toUnsignedLong (startAddress [ 0 ]) * 256 * 256
264
- + Byte .toUnsignedLong (startAddress [ 0 ]) * 256 * 256 * 256 ;
265
-
266
- code_endAddress = Byte .toUnsignedLong (endAddress [ 0 ])
267
- + Byte .toUnsignedLong (endAddress [ 0 ]) * 256
268
- + Byte .toUnsignedLong (endAddress [ 0 ]) * 256 * 256
269
- + Byte .toUnsignedLong (endAddress [ 0 ]) * 256 * 256 * 256 ;
261
+ code_startAddress = Byte .toUnsignedLong (notificationValue [ 5 ])
262
+ + Byte .toUnsignedLong (notificationValue [ 4 ]) * 256
263
+ + Byte .toUnsignedLong (notificationValue [ 3 ]) * 256 * 256
264
+ + Byte .toUnsignedLong (notificationValue [ 2 ]) * 256 * 256 * 256 ;
265
+
266
+ code_endAddress = Byte .toUnsignedLong (notificationValue [ 9 ])
267
+ + Byte .toUnsignedLong (notificationValue [ 8 ]) * 256
268
+ + Byte .toUnsignedLong (notificationValue [ 7 ]) * 256 * 256
269
+ + Byte .toUnsignedLong (notificationValue [ 6 ]) * 256 * 256 * 256 ;
270
270
}
271
271
272
272
byte [] hash = Arrays .copyOfRange (notificationValue , 10 , 18 );
@@ -375,13 +375,15 @@ public int attemptPartialFlash(String filePath) {
375
375
HexUtils hex = new HexUtils (filePath );
376
376
Log .v (TAG , "searchForData()" );
377
377
String hash = "" ;
378
+ int magicPart = 0 ;
378
379
int magicIndex = hex .searchForData (PXT_MAGIC );
379
380
if (magicIndex > -1 ) {
380
381
python = false ;
381
382
String magicData = hex .getDataFromIndex (magicIndex );
382
- int next = magicData .indexOf (PXT_MAGIC ) + PXT_MAGIC .length ();
383
+ magicPart = magicData .indexOf (PXT_MAGIC );
384
+ int next = magicPart + PXT_MAGIC .length ();
383
385
if ( next < magicData .length ()) {
384
- hash = magicData .substring ( next );
386
+ hash = magicData .substring ( magicPart );
385
387
}
386
388
if ( hash .length () < 16 ) {
387
389
String nextData = hex .getDataFromIndex ( magicIndex + 1 );
@@ -397,18 +399,24 @@ public int attemptPartialFlash(String filePath) {
397
399
return PF_ATTEMPT_DFU ;
398
400
}
399
401
400
- Log .v (TAG , "Found PXT_MAGIC at " + magicIndex );
402
+ Log .v (TAG , "Found PXT_MAGIC at " + magicIndex + " at offset " + magicPart );
401
403
402
404
// Get Memory Map from Microbit
403
- try {
404
- Log . v ( TAG , " readMemoryMap()" );
405
- readMemoryMap ();
406
- } catch ( InterruptedException e ) {
407
- e . printStackTrace () ;
405
+ code_startAddress = code_endAddress = 0 ;
406
+ if ( ! readMemoryMap ())
407
+ {
408
+ Log . w ( TAG , "Failed to read memory map" );
409
+ return PF_ATTEMPT_DFU ;
408
410
}
411
+ // TODO: readMemoryMap may still have failed - more checks are needed
409
412
410
413
// Compare DAL hash
411
414
if ( !python ) {
415
+ if ( code_startAddress == 0 || code_endAddress <= code_startAddress )
416
+ {
417
+ Log .v (TAG , "Failed to read memory map code address" );
418
+ return PF_ATTEMPT_DFU ;
419
+ }
412
420
if (!hash .equals (dalHash )) {
413
421
Log .v (TAG , hash + " " + (dalHash ));
414
422
return PF_ATTEMPT_DFU ;
@@ -450,15 +458,13 @@ public int attemptPartialFlash(String filePath) {
450
458
451
459
int magicLo = hex .getRecordAddressFromIndex (magicIndex );
452
460
int magicHi = hex .getSegmentAddress (magicIndex );
453
- long magicA = (long ) magicLo + (long ) magicHi * 256 * 256 ;
461
+ long magicA = (long ) magicLo + (long ) magicHi * 256 * 256 + magicPart ;
454
462
455
- // Ready to flash!
456
- // Loop through data
457
463
int packetNum = 0 ;
458
464
int lineCount = 0 ;
459
- int part = 0 ;
460
- int line0 = 0 ;
461
- int part0 = 0 ;
465
+ int part = magicPart ; // magic is first data to be copied
466
+ int line0 = lineCount ;
467
+ int part0 = part ;
462
468
463
469
int addrLo = hex .getRecordAddressFromIndex (magicIndex + lineCount );
464
470
int addrHi = hex .getSegmentAddress (magicIndex + lineCount );
@@ -467,8 +473,22 @@ public int attemptPartialFlash(String filePath) {
467
473
String hexData ;
468
474
String partData ;
469
475
476
+ Log .w (TAG , "Code start " + code_startAddress + " end " + code_endAddress );
477
+ Log .w (TAG , "First line " + addr );
478
+
479
+ // Ready to flash!
480
+ // Loop through data
470
481
Log .v (TAG , "enter flashing loop" );
471
482
483
+ long addr0 = addr + part / 2 ; // two hex digits per byte
484
+ int addr0Lo = (int ) ( addr0 % (256 * 256 ));
485
+ int addr0Hi = (int ) ( addr0 / (256 * 256 ));
486
+
487
+ if ( code_startAddress != addr0 ) {
488
+ Log .v (TAG , "Code start address doesn't match" );
489
+ return PF_ATTEMPT_DFU ;
490
+ }
491
+
472
492
long startTime = SystemClock .elapsedRealtime ();
473
493
while (true ) {
474
494
// Timeout if total is > 30 seconds
@@ -492,27 +512,29 @@ public int attemptPartialFlash(String filePath) {
492
512
} else {
493
513
partData = hexData .substring (part , part + 32 );
494
514
}
495
- Log .v (TAG , partData );
496
515
497
- // recordToByteArray() build a PF command block with the data
498
516
int offsetToSend = 0 ;
499
- if (count == 0 ) {
500
- offsetToSend = addrLo + part ;
517
+ if ( count == 0 )
518
+ {
519
+ line0 = lineCount ;
520
+ part0 = part ;
521
+ addr0 = addr + part / 2 ; // two hex digits per byte
522
+ addr0Lo = (int ) ( addr0 % (256 * 256 ));
523
+ addr0Hi = (int ) ( addr0 / (256 * 256 ));
524
+ offsetToSend = addr0Lo ;
501
525
} else if (count == 1 ) {
502
- offsetToSend = addrHi ;
526
+ offsetToSend = addr0Hi ;
503
527
}
528
+
529
+ Log .v (TAG , packetNum + " " + count + " addr0 " + addr0 + " offsetToSend " + offsetToSend + " line " + lineCount + " addr " + addr + " part " + part + " data " + partData );
530
+
531
+ // recordToByteArray() builds a PF command block with the data
504
532
byte chunk [] = HexUtils .recordToByteArray (partData , offsetToSend , packetNum );
505
533
506
534
// Write without response
507
535
// Wait for previous write to complete
508
536
boolean writeStatus = writePartialFlash (partialFlashCharacteristic , chunk );
509
537
510
- if ( count == 0 )
511
- {
512
- line0 = lineCount ;
513
- part0 = part ;
514
- }
515
-
516
538
// Sleep after 4 packets
517
539
count ++;
518
540
if ( count == 4 ){
@@ -778,7 +800,7 @@ protected void onHandleIntent(@Nullable Intent intent) {
778
800
/*
779
801
Read Memory Map from the MB
780
802
*/
781
- private Boolean readMemoryMap () throws InterruptedException {
803
+ private boolean readMemoryMap () {
782
804
boolean status ; // Gatt Status
783
805
784
806
try {
@@ -799,12 +821,10 @@ private Boolean readMemoryMap() throws InterruptedException {
799
821
synchronized (region_lock ) {
800
822
region_lock .wait (2000 );
801
823
}
802
-
803
824
}
804
-
805
-
806
825
} catch (Exception e ){
807
826
Log .e (TAG , e .toString ());
827
+ return false ;
808
828
}
809
829
810
830
return true ;
0 commit comments