Skip to content

Commit 3fa193b

Browse files
committed
Fix long line logic
1 parent 2b58494 commit 3fa193b

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed

src/main/java/org/microbit/android/partialflashing/PartialFlashingBaseService.java

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,15 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
258258
Log.v(TAG, "startAddress: " + bytesToHex(startAddress) + " endAddress: " + bytesToHex(endAddress));
259259

260260
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;
270270
}
271271

272272
byte[] hash = Arrays.copyOfRange(notificationValue, 10, 18);
@@ -375,13 +375,15 @@ public int attemptPartialFlash(String filePath) {
375375
HexUtils hex = new HexUtils(filePath);
376376
Log.v(TAG, "searchForData()");
377377
String hash = "";
378+
int magicPart = 0;
378379
int magicIndex = hex.searchForData(PXT_MAGIC);
379380
if (magicIndex > -1) {
380381
python = false;
381382
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();
383385
if ( next < magicData.length()) {
384-
hash = magicData.substring( next);
386+
hash = magicData.substring( magicPart);
385387
}
386388
if ( hash.length() < 16) {
387389
String nextData = hex.getDataFromIndex( magicIndex + 1);
@@ -397,18 +399,24 @@ public int attemptPartialFlash(String filePath) {
397399
return PF_ATTEMPT_DFU;
398400
}
399401

400-
Log.v(TAG, "Found PXT_MAGIC at " + magicIndex);
402+
Log.v(TAG, "Found PXT_MAGIC at " + magicIndex + " at offset " + magicPart);
401403

402404
// 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;
408410
}
411+
// TODO: readMemoryMap may still have failed - more checks are needed
409412

410413
// Compare DAL hash
411414
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+
}
412420
if (!hash.equals(dalHash)) {
413421
Log.v(TAG, hash + " " + (dalHash));
414422
return PF_ATTEMPT_DFU;
@@ -450,15 +458,13 @@ public int attemptPartialFlash(String filePath) {
450458

451459
int magicLo = hex.getRecordAddressFromIndex(magicIndex);
452460
int magicHi = hex.getSegmentAddress(magicIndex);
453-
long magicA = (long) magicLo + (long) magicHi * 256 * 256;
461+
long magicA = (long) magicLo + (long) magicHi * 256 * 256 + magicPart;
454462

455-
// Ready to flash!
456-
// Loop through data
457463
int packetNum = 0;
458464
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;
462468

463469
int addrLo = hex.getRecordAddressFromIndex(magicIndex + lineCount);
464470
int addrHi = hex.getSegmentAddress(magicIndex + lineCount);
@@ -467,8 +473,22 @@ public int attemptPartialFlash(String filePath) {
467473
String hexData;
468474
String partData;
469475

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
470481
Log.v(TAG, "enter flashing loop");
471482

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+
472492
long startTime = SystemClock.elapsedRealtime();
473493
while (true) {
474494
// Timeout if total is > 30 seconds
@@ -492,27 +512,29 @@ public int attemptPartialFlash(String filePath) {
492512
} else {
493513
partData = hexData.substring(part, part + 32);
494514
}
495-
Log.v(TAG, partData);
496515

497-
// recordToByteArray() build a PF command block with the data
498516
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;
501525
} else if (count == 1) {
502-
offsetToSend = addrHi;
526+
offsetToSend = addr0Hi;
503527
}
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
504532
byte chunk[] = HexUtils.recordToByteArray(partData, offsetToSend, packetNum);
505533

506534
// Write without response
507535
// Wait for previous write to complete
508536
boolean writeStatus = writePartialFlash(partialFlashCharacteristic, chunk);
509537

510-
if ( count == 0)
511-
{
512-
line0 = lineCount;
513-
part0 = part;
514-
}
515-
516538
// Sleep after 4 packets
517539
count++;
518540
if ( count == 4){
@@ -778,7 +800,7 @@ protected void onHandleIntent(@Nullable Intent intent) {
778800
/*
779801
Read Memory Map from the MB
780802
*/
781-
private Boolean readMemoryMap() throws InterruptedException {
803+
private boolean readMemoryMap() {
782804
boolean status; // Gatt Status
783805

784806
try {
@@ -799,12 +821,10 @@ private Boolean readMemoryMap() throws InterruptedException {
799821
synchronized (region_lock) {
800822
region_lock.wait(2000);
801823
}
802-
803824
}
804-
805-
806825
} catch (Exception e){
807826
Log.e(TAG, e.toString());
827+
return false;
808828
}
809829

810830
return true;

0 commit comments

Comments
 (0)