8383
8484import static com .samsung .microbit .BuildConfig .DEBUG ;
8585import static com .samsung .microbit .ui .PopUp .TYPE_ALERT ;
86+ import static com .samsung .microbit .ui .PopUp .TYPE_CHOICE ;
8687import static com .samsung .microbit .ui .PopUp .TYPE_PROGRESS_NOT_CANCELABLE ;
8788import static com .samsung .microbit .ui .PopUp .TYPE_SPINNER_NOT_CANCELABLE ;
8889import static com .samsung .microbit .ui .activity .PopUpActivity .INTENT_ACTION_UPDATE_LAYOUT ;
@@ -1123,6 +1124,18 @@ protected void startFlashing(int flashingType) {
11231124
11241125 MBApp application = MBApp .getApp ();
11251126 int hardwareType = currentMicrobit .mhardwareVersion ;
1127+ if (hardwareType != MICROBIT_V1 && hardwareType != MICROBIT_V2 ) {
1128+ // Ask the user what type of hardware they have
1129+ PopUp .show (getString (R .string .dfu_what_hardware ),
1130+ getString (R .string .dfu_what_hardware_title ),
1131+ R .drawable .error_face ,
1132+ R .drawable .red_btn ,
1133+ PopUp .GIFF_ANIMATION_ERROR ,
1134+ TYPE_ALERT ,
1135+ null ,
1136+ null
1137+ );
1138+ }
11261139
11271140
11281141 // Create tmp hex for V1 or V2
@@ -1284,9 +1297,12 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
12841297
12851298 int application_size = 0 ;
12861299 int next = 0 ;
1287- boolean records_wanted = false ;
1300+ boolean records_wanted = true ;
12881301 boolean is_fat = false ;
1302+ boolean is_v2 = false ;
1303+ boolean uses_ESA = false ;
12891304 ByteArrayOutputStream lastELA = new ByteArrayOutputStream ();
1305+ ByteArrayOutputStream lastESA = new ByteArrayOutputStream ();
12901306
12911307 try {
12921308 fis = new FileInputStream (inputPath );
@@ -1308,6 +1324,7 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
13081324 switch (b_type ) {
13091325 case 'A' : // Block start
13101326 is_fat = true ;
1327+ records_wanted = false ;
13111328
13121329 // Check data for id
13131330 if (bs [b_x + 9 ] == '9' && bs [b_x + 10 ] == '9' && bs [b_x + 11 ] == '0' && bs [b_x + 12 ] == '1' ) {
@@ -1329,6 +1346,19 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
13291346 outputHex .write (bs , b_x , next );
13301347 }
13311348 break ;
1349+ case '2' :
1350+ uses_ESA = true ;
1351+
1352+ ByteArrayOutputStream currentESA = new ByteArrayOutputStream ();
1353+ currentESA .write (bs , b_x , next );
1354+
1355+ // If ESA has changed write
1356+ if (!Arrays .equals (currentESA .toByteArray (), lastESA .toByteArray ())) {
1357+ lastESA .reset ();
1358+ lastESA .write (bs , b_x , next );
1359+ outputHex .write (bs , b_x , next );
1360+ }
1361+ break ;
13321362 case '1' :
13331363 // EOF
13341364 // Ensure KV storage is erased
@@ -1363,13 +1393,31 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
13631393 // Copy record to hex
13641394 // Record starts at b_x, next long
13651395 // Calculate address of record
1366- int b_ela = (charToInt ((char ) lastELA .toByteArray ()[9 ]) << 12 ) | (charToInt ((char ) lastELA .toByteArray ()[10 ]) << 8 ) | (charToInt ((char ) lastELA .toByteArray ()[11 ]) << 4 ) | (charToInt ((char ) lastELA .toByteArray ()[12 ]));
1396+ int b_a = 0 ;
1397+ if (lastELA .size () > 0 && !uses_ESA ) {
1398+ b_a = 0 ;
1399+ b_a = (charToInt ((char ) lastELA .toByteArray ()[9 ]) << 12 ) | (charToInt ((char ) lastELA .toByteArray ()[10 ]) << 8 ) | (charToInt ((char ) lastELA .toByteArray ()[11 ]) << 4 ) | (charToInt ((char ) lastELA .toByteArray ()[12 ]));
1400+ b_a = b_a << 16 ;
1401+ }
1402+ if (lastESA .size () > 0 && uses_ESA ) {
1403+ b_a = 0 ;
1404+ b_a = (charToInt ((char ) lastESA .toByteArray ()[9 ]) << 12 ) | (charToInt ((char ) lastESA .toByteArray ()[10 ]) << 8 ) | (charToInt ((char ) lastESA .toByteArray ()[11 ]) << 4 ) | (charToInt ((char ) lastESA .toByteArray ()[12 ]));
1405+ b_a = b_a * 16 ;
1406+ }
1407+
13671408 int b_raddr = (charToInt ((char ) bs [b_x + 3 ]) << 12 ) | (charToInt ((char ) bs [b_x + 4 ]) << 8 ) | (charToInt ((char ) bs [b_x + 5 ]) << 4 ) | (charToInt ((char ) bs [b_x + 6 ]));
1368- int b_addr = b_ela << 16 | b_raddr ;
1409+ int b_addr = b_a | b_raddr ;
1410+
13691411
13701412 int lower_bound = 0 ; int upper_bound = 0 ;
13711413 if (hardwareType == MICROBIT_V1 ) { lower_bound = 0x18000 ; upper_bound = 0x38000 ; }
1372- if (hardwareType == MICROBIT_V2 ) { lower_bound = 0x27000 ; upper_bound = 0x71FFF ; }
1414+ if (hardwareType == MICROBIT_V2 && is_fat ) { lower_bound = 0x27000 ; upper_bound = 0x71FFF ; } // Current Universal Hex files are s140
1415+ if (hardwareType == MICROBIT_V2 && !is_fat ) { lower_bound = 0x1C000 ; upper_bound = 0x77000 ; } // Current C++ programs are S113
1416+
1417+ // Check for Cortex-M4 Vector Table
1418+ if (b_addr == 0x10 && bs [b_x + 41 ] != 'E' && bs [b_x + 42 ] != '0' ) { // Vectors exist
1419+ is_v2 = true ;
1420+ }
13731421
13741422 if ((records_wanted || !is_fat ) && b_addr >= lower_bound && b_addr < upper_bound ) {
13751423 outputHex .write (bs , b_x , next );
@@ -1414,18 +1462,22 @@ private String[] universalHexToDFU(String inputPath, int hardwareType) {
14141462 ret [0 ] = hexToFlash .getAbsolutePath ();
14151463 ret [1 ] = Integer .toString (application_size );
14161464
1417- if (hardwareType == MICROBIT_V2 && is_fat == false ) {
1465+ /*
1466+ if(hardwareType == MICROBIT_V2 && (!is_v2 && !is_fat)) {
14181467 ret[1] = Integer.toString(-1); // Invalidate hex file
14191468 }
1469+ */
14201470
14211471 return ret ;
14221472 } catch (IOException e ) {
14231473 e .printStackTrace ();
14241474 }
14251475
14261476 } catch (FileNotFoundException e ) {
1477+ Log .v (TAG , "File not found." );
14271478 e .printStackTrace ();
14281479 } catch (IOException e ) {
1480+ Log .v (TAG , "IO Exception." );
14291481 e .printStackTrace ();
14301482 }
14311483
0 commit comments