[SOLVED] write a secured FW on Kinetis usinj J-Link

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] write a secured FW on Kinetis usinj J-Link

    I need to write a secured FW on Kinetis.

    J-Link write this:

    Source Code

    1. Downloading file [./4chMpu_Ver1.2.4.0.protected.bin]...Comparing flash [050%]
    2. *** Warning ***
    3. Your application image would set the system security of the device.
    4. This is not supported in the current configuration.
    5. Application data has been changed accordingly.
    6. *** Warning ***
    7. 100%] Done.


    So it writes an unprotected FW, infact when I verify the FW:

    Source Code

    1. Loading binary file ./4chMpu_Ver1.2.4.0.protected.bin
    2. Reading 140440 bytes data from target memory @ 0x00000000.
    3. Verify failed @ address 0x0000040C.
    4. Expected FC read FE


    Is it possible to write a protected FW? How?

    best regards
    Max
  • Hi Max,


    you need to add " (allow security)" to the device name you select in J-Link Commander.
    For example "MK65FN2M0xxx18 (allow security)" instead of "MK65FN2M0xxx18".

    Best regards,
    Niklas
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • With "MK64FN1M0XXX12 (allow security)", I can Program and Verify. And I wonder how, since the FW should be unreadable from debug port.
    the script wr-fw.jlink is:

    Source Code

    1. unlock Kinetis
    2. r
    3. sleep 100
    4. erase
    5. loadbin ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    6. r
    7. sleep 100
    8. verifybin ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    9. exit

    and I run:

    Source Code

    1. /opt/SEGGER/JLink/JLinkExe -device "MK64FN1M0XXX12 (allow security)" -if SWD -speed 4000 -JTAGConf -1,-1 -CommanderScript ./wr-fw.jlink


    the output is:

    Source Code

    1. SEGGER J-Link Commander V5.10i (Compiled Jan 28 2016 09:41:48)
    2. DLL version V5.10i, compiled Jan 28 2016 09:41:43
    3. Script file read successfully.
    4. Processing script file...
    5. J-Link connection not established yet but required for command.
    6. Connecting to J-Link via USB...O.K.
    7. Firmware: J-Link Ultra V4 compiled Apr 22 2016 11:48:35
    8. Hardware version: V4.00
    9. S/N: 504400379
    10. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    11. Emulator has Trace capability
    12. VTref = 3.416V
    13. Found SWD-DP with ID 0x2BA01477
    14. Unlocking device...O.K.
    15. Target connection not established yet but required for command.
    16. Device "MK64FN1M0XXX12 (ALLOW SECURITY)" selected.
    17. Found SWD-DP with ID 0x2BA01477
    18. Found SWD-DP with ID 0x2BA01477
    19. Found Cortex-M4 r0p1, Little endian.
    20. FPUnit: 6 code (BP) slots and 2 literal slots
    21. CoreSight components:
    22. ROMTbl 0 @ E00FF000
    23. ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 000BB00C SCS
    24. ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 003BB002 DWT
    25. ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 002BB003 FPB
    26. ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 003BB001 ITM
    27. ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 000BB9A1 TPIU
    28. ROMTbl 0 [5]: FFF42000, CID: B105900D, PID: 000BB925 ETM
    29. ROMTbl 0 [6]: FFF43000, CID: B105900D, PID: 003BB907 ETB
    30. ROMTbl 0 [7]: FFF44000, CID: B105900D, PID: 001BB908 CSTF
    31. Cortex-M4 identified.
    32. Reset delay: 0 ms
    33. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    34. Sleep(100)
    35. Erasing device (MK64FN1M0xxx12 (allow security))...
    36. Comparing flash [100%] Done.
    37. Erasing flash [100%] Done.
    38. Verifying flash [100%] Done.
    39. J-Link: Flash download: Total time needed: 1.071s (Prepare: 0.048s, Compare: 0.000s, Erase: 1.020s, Program: 0.000s, Verify: 0.000s, Restore: 0.002s)
    40. Erasing done.
    41. Downloading file [./4chMpu_Ver1.2.4.0.protected.bin]...Comparing flash [100%] Done.
    42. Erasing flash [100%] Done.
    43. Programming flash [100%] Done.
    44. Verifying flash [100%] Done.
    45. J-Link: Flash download: Flash programming performed for 1 range (143360 bytes)
    46. J-Link: Flash download: Total time needed: 1.711s (Prepare: 0.068s, Compare: 0.016s, Erase: 0.000s, Program: 1.590s, Verify: 0.009s, Restore: 0.026s)
    47. O.K.
    48. Reset delay: 0 ms
    49. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    50. SYSRESETREQ has confused core. Trying to reconnect and use VECTRESET.
    51. Found SWD-DP with ID 0x2BA01477
    52. **************************
    53. WARNING: Failed to reset CPU. VECTRESET has confused core.
    54. **************************
    55. **************************
    56. WARNING: CPU did not halt after reset.
    57. **************************
    58. **************************
    59. WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
    60. **************************
    61. **************************
    62. WARNING: Could not set S_RESET_ST
    63. **************************
    64. Found SWD-DP with ID 0x2BA01477
    65. **************************
    66. WARNING: Could not set S_RESET_ST
    67. **************************
    68. **************************
    69. WARNING: CPU did not halt after reset.
    70. **************************
    71. **************************
    72. WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
    73. **************************
    74. **************************
    75. WARNING: Could not set S_RESET_ST
    76. **************************
    77. Kinetis (setup): CPU did not halt after disabling watchdog.
    78. **************************
    79. WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
    80. **************************
    81. ****** Error: DAP error while reading AIRCR / CPUID register
    82. Sleep(100)
    83. Loading binary file ./4chMpu_Ver1.2.4.0.protected.bin
    84. Reading 140440 bytes data from target memory @ 0x00000000.
    85. Verify successful.
    86. Script processing completed.
    87. ****** Error: Could not start CPU core. (ErrorCode: -1)
    Display All


    So how the verify step can be successful if the FW is unreadable?

    best regards
    Max
  • Hi Max,


    is the memory protection active or is it only active after a power-cycle?
    Can be tested by slightly adjusting the script:

    C Source Code

    1. unlock Kinetis
    2. r
    3. sleep 100
    4. erase
    5. loadbin ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    6. mem32 0 10
    7. r
    8. sleep 100
    9. verifybin ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    10. mem32 0 10
    11. exit
    Display All


    If the flash memory is not readable, this is probably just a wrong output from J-Link Commander.
    Could you please give to most recent version of the J-Link software & documentation pack a try?

    Best regards,
    Niklas
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • tryed your script and new version of Jlink

    Source Code

    1. SEGGER J-Link Commander V6.14b (Compiled Mar 9 2017 08:48:28)
    2. DLL version V6.14b, compiled Mar 9 2017 08:48:20
    3. Script file read successfully.
    4. Processing script file...
    5. J-Link connection not established yet but required for command.
    6. Connecting to J-Link via USB...O.K.
    7. Firmware: J-Link Ultra V4 compiled Feb 21 2017 14:10:22
    8. Hardware version: V4.00
    9. S/N: 504400379
    10. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    11. VTref = 3.414V
    12. Found SWD-DP with ID 0x2BA01477
    13. Unlocking device...O.K.
    14. Target connection not established yet but required for command.
    15. Device "MK64FN1M0XXX12 (ALLOW SECURITY)" selected.
    16. Found SWD-DP with ID 0x2BA01477
    17. Found SWD-DP with ID 0x2BA01477
    18. AP-IDR: 0x24770011, Type: AHB-AP
    19. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    20. Found Cortex-M4 r0p1, Little endian.
    21. FPUnit: 6 code (BP) slots and 2 literal slots
    22. CoreSight components:
    23. ROMTbl 0 @ E00FF000
    24. ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 000BB00C SCS
    25. ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 003BB002 DWT
    26. ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 002BB003 FPB
    27. ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 003BB001 ITM
    28. ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 000BB9A1 TPIU
    29. ROMTbl 0 [5]: FFF42000, CID: B105900D, PID: 000BB925 ETM
    30. ROMTbl 0 [6]: FFF43000, CID: B105900D, PID: 003BB907 ETB
    31. ROMTbl 0 [7]: FFF44000, CID: B105900D, PID: 001BB908 CSTF
    32. Cortex-M4 identified.
    33. Reset delay: 0 ms
    34. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    35. Sleep(100)
    36. Erasing device (MK64FN1M0xxx12 (allow security))...
    37. Comparing flash [100%] Done.
    38. Erasing flash [100%] Done.
    39. Verifying flash [100%] Done.
    40. J-Link: Flash download: Total time needed: 1.039s (Prepare: 0.015s, Compare: 0.000s, Erase: 1.022s, Program: 0.000s, Verify: 0.000s, Restore: 0.001s)
    41. Erasing done.
    42. Downloading file [./4chMpu_Ver1.2.4.0.protected.bin]...
    43. Comparing flash [100%] Done.
    44. Erasing flash [100%] Done.
    45. Programming flash [100%] Done.
    46. Verifying flash [100%] Done.
    47. J-Link: Flash download: Flash programming performed for 1 range (143360 bytes)
    48. J-Link: Flash download: Total time needed: 1.673s (Prepare: 0.033s, Compare: 0.015s, Erase: 0.000s, Program: 1.593s, Verify: 0.008s, Restore: 0.022s)
    49. O.K.
    50. 00000000 = 20030000 000004D9 000004E9 000004E9
    51. 00000010 = 000004E9 000004E9 000004E9 00000000
    52. 00000020 = 00000000 00000000 00000000 00015EF9
    53. 00000030 = 000004E9 00000000 00015F15 00015FCD
    54. Reset delay: 0 ms
    55. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    56. SYSRESETREQ has confused core. Trying to reconnect and use VECTRESET.
    57. Found SWD-DP with ID 0x2BA01477
    58. AP-IDR: 0x24770011, Type: AHB-AP
    59. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    60. **************************
    61. WARNING: Failed to reset CPU. VECTRESET has confused core.
    62. **************************
    63. **************************
    64. WARNING: CPU did not halt after reset.
    65. **************************
    66. **************************
    67. WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
    68. **************************
    69. **************************
    70. WARNING: Could not set S_RESET_ST
    71. **************************
    72. Found SWD-DP with ID 0x2BA01477
    73. AP-IDR: 0x24770011, Type: AHB-AP
    74. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    75. SYSRESETREQ has confused core. Trying to reconnect and use VECTRESET.
    76. Found SWD-DP with ID 0x2BA01477
    77. AP-IDR: 0x24770011, Type: AHB-AP
    78. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    79. **************************
    80. WARNING: Failed to reset CPU. VECTRESET has confused core.
    81. **************************
    82. **************************
    83. WARNING: CPU did not halt after reset.
    84. **************************
    85. **************************
    86. WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
    87. **************************
    88. **************************
    89. WARNING: Could not set S_RESET_ST
    90. **************************
    91. Found SWD-DP with ID 0x2BA01477
    92. Protection bytes in flash at addr. 0x400 - 0x40F indicate that readout protection is set.
    93. For debugger connection the device needs to be unsecured.
    94. Note: Unsecuring will trigger a mass erase of the internal flash.
    95. Device will be unsecured now.
    96. ****** Error: DAP error while reading AIRCR / CPUID register
    97. Sleep(100)
    98. Loading binary file ./4chMpu_Ver1.2.4.0.protected.bin
    99. Reading 140440 bytes data from target memory @ 0x00000000.
    100. Verify successful.
    101. 00000000 = 20030000 000004D9 000004E9 000004E9
    102. 00000010 = 000004E9 000004E9 000004E9 00000000
    103. 00000020 = 00000000 00000000 00000000 00015EF9
    104. 00000030 = 000004E9 00000000 00015F15 00015FCD
    105. Script processing completed.
    Display All


    tomorrow I will do other test

    best regards

    max
  • if I do a power cycle, and execute only the verify with this script:

    Source Code

    1. r
    2. sleep 100
    3. verifybin ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    4. mem32 0 10
    5. exit


    the verify fails, as expected:

    Source Code

    1. SEGGER J-Link Commander V6.14b (Compiled Mar 9 2017 08:48:28)
    2. DLL version V6.14b, compiled Mar 9 2017 08:48:20
    3. Script file read successfully.
    4. Processing script file...
    5. J-Link connection not established yet but required for command.
    6. Connecting to J-Link via USB...O.K.
    7. Firmware: J-Link Ultra V4 compiled Feb 21 2017 14:10:22
    8. Hardware version: V4.00
    9. S/N: 504400379
    10. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    11. VTref = 3.412V
    12. Target connection not established yet but required for command.
    13. Device "MK64FN1M0XXX12 (ALLOW SECURITY)" selected.
    14. Found SWD-DP with ID 0x2BA01477
    15. Found SWD-DP with ID 0x2BA01477
    16. AP-IDR: 0x24770011, Type: AHB-AP
    17. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    18. Found Cortex-M4 r0p1, Little endian.
    19. FPUnit: 6 code (BP) slots and 2 literal slots
    20. CoreSight components:
    21. ROMTbl 0 @ E00FF000
    22. ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 000BB00C SCS
    23. ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 003BB002 DWT
    24. ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 002BB003 FPB
    25. ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 003BB001 ITM
    26. ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 000BB9A1 TPIU
    27. ROMTbl 0 [5]: FFF42000, CID: B105900D, PID: 000BB925 ETM
    28. ROMTbl 0 [6]: FFF43000, CID: B105900D, PID: 003BB907 ETB
    29. ROMTbl 0 [7]: FFF44000, CID: B105900D, PID: 001BB908 CSTF
    30. Cortex-M4 identified.
    31. Reset delay: 0 ms
    32. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    33. Sleep(100)
    34. Loading binary file ./4chMpu_Ver1.2.4.0.protected.bin
    35. Reading 140440 bytes data from target memory @ 0x00000000.
    36. Verify failed @ address 0x00000000.
    37. Expected 00 read FF
    38. 00000000 = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
    39. 00000010 = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
    40. 00000020 = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
    41. 00000030 = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
    42. Script processing completed.
    Display All


    best regards
    Max
  • When I write FW using "MK64FN1M0XXX12 (allow security)", the verify stage is successful, but FW doesn't work, it seems that it doesn't run.
    Instead if I write FW using "MK64FN1M0XXX12", the verify stage will fail. FW is not protected from further reads through JTAG/SWD, but it runs and works correctly.

    There is something else I need to know?

    best regards
    Max
  • Hi Max,


    You cannot verify the image after you activated the security (well that is the point of the security, nobody should be able to read it back).
    Therefore, when connecting while the security is set, the device will be unlocked and therefore mass-erased by the J-Link, as it is said by J-Link Commander:
    Protection bytes in flash at addr. 0x400 - 0x40F indicate that readout protection is set.
    For debugger connection the device needs to be unsecured.
    Note: Unsecuring will trigger a mass erase of the internal flash.
    Device will be unsecured now.


    Best regards,
    Niklas
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • SEGGER - Niklas wrote:

    You cannot verify the image after you activated the security (well that is the point of the security, nobody should be able to read it back).
    Therefore, when connecting while the security is set, the device will be unlocked and therefore mass-erased by the J-Link, as it is said by J-Link Commander:
    That's true after a power cycle (you suggested it).
    If I run the script:

    Source Code

    1. unlock Kinetis
    2. r
    3. sleep 100
    4. erase
    5. loadfile ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    6. mem32 0, 10
    7. mem8 400, 10
    8. r
    9. sleep 100
    10. verifybin ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    11. mem32 0, 10
    12. mem8 400, 10
    13. rnh
    14. exit
    Display All


    the result is:

    Source Code

    1. SEGGER J-Link Commander V6.14b (Compiled Mar 9 2017 08:48:28)
    2. DLL version V6.14b, compiled Mar 9 2017 08:48:20
    3. Script file read successfully.
    4. Processing script file...
    5. J-Link connection not established yet but required for command.
    6. Connecting to J-Link via USB...O.K.
    7. Firmware: J-Link Ultra V4 compiled Feb 21 2017 14:10:22
    8. Hardware version: V4.00
    9. S/N: 504400379
    10. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    11. VTref = 3.412V
    12. Found SWD-DP with ID 0x2BA01477
    13. Unlocking device...O.K.
    14. Target connection not established yet but required for command.
    15. Device "MK64FN1M0XXX12 (ALLOW SECURITY)" selected.
    16. Found SWD-DP with ID 0x2BA01477
    17. Found SWD-DP with ID 0x2BA01477
    18. AP-IDR: 0x24770011, Type: AHB-AP
    19. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    20. Found Cortex-M4 r0p1, Little endian.
    21. FPUnit: 6 code (BP) slots and 2 literal slots
    22. CoreSight components:
    23. ROMTbl 0 @ E00FF000
    24. ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 000BB00C SCS
    25. ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 003BB002 DWT
    26. ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 002BB003 FPB
    27. ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 003BB001 ITM
    28. ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 000BB9A1 TPIU
    29. ROMTbl 0 [5]: FFF42000, CID: B105900D, PID: 000BB925 ETM
    30. ROMTbl 0 [6]: FFF43000, CID: B105900D, PID: 003BB907 ETB
    31. ROMTbl 0 [7]: FFF44000, CID: B105900D, PID: 001BB908 CSTF
    32. Cortex-M4 identified.
    33. Reset delay: 0 ms
    34. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    35. Sleep(100)
    36. Erasing device (MK64FN1M0xxx12 (allow security))...
    37. Comparing flash [100%] Done.
    38. Erasing flash [100%] Done.
    39. Verifying flash [100%] Done.
    40. J-Link: Flash download: Total time needed: 1.040s (Prepare: 0.015s, Compare: 0.000s, Erase: 1.023s, Program: 0.000s, Verify: 0.000s, Restore: 0.002s)
    41. Erasing done.
    42. Downloading file [./4chMpu_Ver1.2.4.0.protected.bin]...
    43. Comparing flash [100%] Done.
    44. Erasing flash [100%] Done.
    45. Programming flash [100%] Done.
    46. Verifying flash [100%] Done.
    47. J-Link: Flash download: Flash programming performed for 1 range (143360 bytes)
    48. J-Link: Flash download: Total time needed: 1.672s (Prepare: 0.033s, Compare: 0.015s, Erase: 0.000s, Program: 1.591s, Verify: 0.009s, Restore: 0.022s)
    49. O.K.
    50. 00000000 = 20030000 000004D9 000004E9 000004E9
    51. 00000010 = 000004E9 000004E9 000004E9 00000000
    52. 00000020 = 00000000 00000000 00000000 00015EF9
    53. 00000030 = 000004E9 00000000 00015F15 00015FCD
    54. 00000400 = FF FF FF FF FF FF FF FF FF FF FF FF FC FF FF FF
    55. Reset delay: 0 ms
    56. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    57. SYSRESETREQ has confused core. Trying to reconnect and use VECTRESET.
    58. Found SWD-DP with ID 0x2BA01477
    59. AP-IDR: 0x24770011, Type: AHB-AP
    60. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    61. **************************
    62. WARNING: Failed to reset CPU. VECTRESET has confused core.
    63. **************************
    64. **************************
    65. WARNING: CPU did not halt after reset.
    66. **************************
    67. **************************
    68. WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
    69. **************************
    70. **************************
    71. WARNING: Could not set S_RESET_ST
    72. **************************
    73. Found SWD-DP with ID 0x2BA01477
    74. AP-IDR: 0x24770011, Type: AHB-AP
    75. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    76. SYSRESETREQ has confused core. Trying to reconnect and use VECTRESET.
    77. Found SWD-DP with ID 0x2BA01477
    78. AP-IDR: 0x24770011, Type: AHB-AP
    79. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    80. **************************
    81. WARNING: Failed to reset CPU. VECTRESET has confused core.
    82. **************************
    83. **************************
    84. WARNING: CPU did not halt after reset.
    85. **************************
    86. **************************
    87. WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
    88. **************************
    89. **************************
    90. WARNING: Could not set S_RESET_ST
    91. **************************
    92. Found SWD-DP with ID 0x2BA01477
    93. Protection bytes in flash at addr. 0x400 - 0x40F indicate that readout protection is set.
    94. For debugger connection the device needs to be unsecured.
    95. Note: Unsecuring will trigger a mass erase of the internal flash.
    96. Device will be unsecured now.
    97. ****** Error: DAP error while reading AIRCR / CPUID register
    98. Sleep(100)
    99. Loading binary file ./4chMpu_Ver1.2.4.0.protected.bin
    100. Reading 140440 bytes data from target memory @ 0x00000000.
    101. Verify successful.
    102. 00000000 = 20030000 000004D9 000004E9 000004E9
    103. 00000010 = 000004E9 000004E9 000004E9 00000000
    104. 00000020 = 00000000 00000000 00000000 00015EF9
    105. 00000030 = 000004E9 00000000 00015F15 00015FCD
    106. 00000400 = FF FF FF FF FF FF FF FF FF FF FF FF FC FF FF FF
    107. Script processing completed.
    Display All


    You can see that I can read the flash, and the FSEC bit are set to 00 (byte @0x40C is 0xFC), this mean thet FW should be protected. But (it seems to be) only after power cycle. unfortunately writing in this way FW doesn't run.

    Instead if I use "MK64FN1M0XXX12" device (no "(allow security)") the result is:

    Source Code

    1. SEGGER J-Link Commander V6.14b (Compiled Mar 9 2017 08:48:28)
    2. DLL version V6.14b, compiled Mar 9 2017 08:48:20
    3. Script file read successfully.
    4. Processing script file...
    5. J-Link connection not established yet but required for command.
    6. Connecting to J-Link via USB...O.K.
    7. Firmware: J-Link Ultra V4 compiled Feb 21 2017 14:10:22
    8. Hardware version: V4.00
    9. S/N: 504400379
    10. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    11. VTref = 3.407V
    12. Found SWD-DP with ID 0x2BA01477
    13. Unlocking device...O.K.
    14. Target connection not established yet but required for command.
    15. Device "MK64FN1M0XXX12" selected.
    16. Found SWD-DP with ID 0x2BA01477
    17. Found SWD-DP with ID 0x2BA01477
    18. AP-IDR: 0x24770011, Type: AHB-AP
    19. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    20. Found Cortex-M4 r0p1, Little endian.
    21. FPUnit: 6 code (BP) slots and 2 literal slots
    22. CoreSight components:
    23. ROMTbl 0 @ E00FF000
    24. ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 000BB00C SCS
    25. ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 003BB002 DWT
    26. ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 002BB003 FPB
    27. ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 003BB001 ITM
    28. ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 000BB9A1 TPIU
    29. ROMTbl 0 [5]: FFF42000, CID: B105900D, PID: 000BB925 ETM
    30. ROMTbl 0 [6]: FFF43000, CID: B105900D, PID: 003BB907 ETB
    31. ROMTbl 0 [7]: FFF44000, CID: B105900D, PID: 001BB908 CSTF
    32. Cortex-M4 identified.
    33. Reset delay: 0 ms
    34. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    35. Sleep(100)
    36. Erasing device (MK64FN1M0xxx12)...
    37. Comparing flash [100%] Done.
    38. Erasing flash [100%] Done.
    39. Verifying flash [100%] Done.
    40. J-Link: Flash download: Total time needed: 1.036s (Prepare: 0.014s, Compare: 0.000s, Erase: 1.019s, Program: 0.000s, Verify: 0.000s, Restore: 0.002s)
    41. Erasing done.
    42. Downloading file [./4chMpu_Ver1.2.4.0.protected.bin]...
    43. Comparing flash [100%] Done.
    44. Erasing flash [100%] Done.
    45. Programming flash [100%] Done.
    46. Verifying flash [100%] Done.
    47. J-Link: Flash download: Flash programming performed for 1 range (143360 bytes)
    48. J-Link: Flash download: Total time needed: 1.667s (Prepare: 0.033s, Compare: 0.014s, Erase: 0.000s, Program: 1.586s, Verify: 0.009s, Restore: 0.022s)
    49. O.K.
    50. 00000000 = 20030000 000004D9 000004E9 000004E9
    51. 00000010 = 000004E9 000004E9 000004E9 00000000
    52. 00000020 = 00000000 00000000 00000000 00015EF9
    53. 00000030 = 000004E9 00000000 00015F15 00015FCD
    54. 00000400 = FF FF FF FF FF FF FF FF FF FF FF FF FE FF FF FF
    55. Reset delay: 0 ms
    56. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    57. Sleep(100)
    58. Loading binary file ./4chMpu_Ver1.2.4.0.protected.bin
    59. Reading 140440 bytes data from target memory @ 0x00000000.
    60. Verify failed @ address 0x0000040C.
    61. Expected FC read FE
    62. 00000000 = 20030000 000004D9 000004E9 000004E9
    63. 00000010 = 000004E9 000004E9 000004E9 00000000
    64. 00000020 = 00000000 00000000 00000000 00015EF9
    65. 00000030 = 000004E9 00000000 00015F15 00015FCD
    66. 00000400 = FF FF FF FF FF FF FF FF FF FF FF FF FE FF FF FF
    67. Script processing completed.
    Display All


    you can see that now the FSEC bits are 10 (@0x40C => 0xFE) and the FW is unprotected, but runs correctly

    please note that my binary has FSEC bits set to 00

    Brainfuck Source Code

    1. $ xxd -g1 -s1024 -l16 4chMpu_Ver1.2.4.0.protected.bin
    2. 00000400: ff ff ff ff ff ff ff ff ff ff ff ff fc ff ff ff ................


    best regards
    Max
  • Hi Max,


    C Source Code

    1. Found SWD-DP with ID 0x2BA01477
    2. Protection bytes in flash at addr. 0x400 - 0x40F indicate that readout protection is set.
    3. For debugger connection the device needs to be unsecured.
    4. Note: Unsecuring will trigger a mass erase of the internal flash.
    5. Device will be unsecured now.

    An this point, the flash is definitely mass-erased.

    C Source Code

    1. ****** Error: DAP error while reading AIRCR / CPUID register
    2. Sleep(100)
    3. Loading binary file ./4chMpu_Ver1.2.4.0.protected.bin
    4. Reading 140440 bytes data from target memory @ 0x00000000.
    5. Verify successful.
    6. 00000000 = 20030000 000004D9 000004E9 000004E9
    7. 00000010 = 000004E9 000004E9 000004E9 00000000
    8. 00000020 = 00000000 00000000 00000000 00015EF9
    9. 00000030 = 000004E9 00000000 00015F15 00015FCD
    10. 00000400 = FF FF FF FF FF FF FF FF FF FF FF FF FC FF FF FF
    11. Script processing completed.
    Display All


    My guess is that this a cache error on J-Link side.
    Lets adjust the script once more:

    C Source Code

    1. unlock Kinetis
    2. r
    3. exec ExcludeFlashCacheRange 0x00000000 - 0x00100000
    4. sleep 100
    5. erase
    6. loadfile ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    7. mem32 0, 10
    8. mem8 400, 10
    9. r
    10. sleep 100
    11. verifybin ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    12. mem32 0, 10
    13. mem8 400, 10
    14. rnh
    15. exit
    Display All



    Best regards,
    Niklas
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • running modified script as you suggest:

    Source Code

    1. SEGGER J-Link Commander V6.14b (Compiled Mar 9 2017 08:48:28)
    2. DLL version V6.14b, compiled Mar 9 2017 08:48:20
    3. Script file read successfully.
    4. Processing script file...
    5. J-Link connection not established yet but required for command.
    6. Connecting to J-Link via USB...O.K.
    7. Firmware: J-Link Ultra V4 compiled Feb 21 2017 14:10:22
    8. Hardware version: V4.00
    9. S/N: 504400379
    10. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    11. VTref = 3.407V
    12. Found SWD-DP with ID 0x2BA01477
    13. Unlocking device...O.K.
    14. Target connection not established yet but required for command.
    15. Device "MK64FN1M0XXX12 (ALLOW SECURITY)" selected.
    16. Found SWD-DP with ID 0x2BA01477
    17. Found SWD-DP with ID 0x2BA01477
    18. AP-IDR: 0x24770011, Type: AHB-AP
    19. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    20. Found Cortex-M4 r0p1, Little endian.
    21. FPUnit: 6 code (BP) slots and 2 literal slots
    22. CoreSight components:
    23. ROMTbl 0 @ E00FF000
    24. ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 000BB00C SCS
    25. ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 003BB002 DWT
    26. ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 002BB003 FPB
    27. ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 003BB001 ITM
    28. ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 000BB9A1 TPIU
    29. ROMTbl 0 [5]: FFF42000, CID: B105900D, PID: 000BB925 ETM
    30. ROMTbl 0 [6]: FFF43000, CID: B105900D, PID: 003BB907 ETB
    31. ROMTbl 0 [7]: FFF44000, CID: B105900D, PID: 001BB908 CSTF
    32. Cortex-M4 identified.
    33. Reset delay: 0 ms
    34. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    35. Sleep(100)
    36. Erasing device (MK64FN1M0xxx12 (allow security))...
    37. Comparing flash [100%] Done.
    38. Erasing flash [100%] Done.
    39. Verifying flash [100%] Done.
    40. J-Link: Flash download: Total time needed: 1.038s (Prepare: 0.015s, Compare: 0.000s, Erase: 1.021s, Program: 0.000s, Verify: 0.000s, Restore: 0.001s)
    41. Erasing done.
    42. Downloading file [./4chMpu_Ver1.2.4.0.protected.bin]...
    43. Comparing flash [100%] Done.
    44. Erasing flash [100%] Done.
    45. Programming flash [100%] Done.
    46. Verifying flash [100%] Done.
    47. J-Link: Flash download: Flash programming performed for 1 range (143360 bytes)
    48. J-Link: Flash download: Total time needed: 1.668s (Prepare: 0.033s, Compare: 0.014s, Erase: 0.000s, Program: 1.587s, Verify: 0.009s, Restore: 0.022s)
    49. O.K.
    50. 00000000 = 20030000 000004D9 000004E9 000004E9
    51. 00000010 = 000004E9 000004E9 000004E9 00000000
    52. 00000020 = 00000000 00000000 00000000 00015EF9
    53. 00000030 = 000004E9 00000000 00015F15 00015FCD
    54. 00000400 = FF FF FF FF FF FF FF FF FF FF FF FF FC FF FF FF
    55. Reset delay: 0 ms
    56. Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
    57. SYSRESETREQ has confused core. Trying to reconnect and use VECTRESET.
    58. Found SWD-DP with ID 0x2BA01477
    59. AP-IDR: 0x24770011, Type: AHB-AP
    60. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    61. **************************
    62. WARNING: Failed to reset CPU. VECTRESET has confused core.
    63. **************************
    64. **************************
    65. WARNING: CPU did not halt after reset.
    66. **************************
    67. **************************
    68. WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
    69. **************************
    70. **************************
    71. WARNING: Could not set S_RESET_ST
    72. **************************
    73. Found SWD-DP with ID 0x2BA01477
    74. AP-IDR: 0x24770011, Type: AHB-AP
    75. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    76. SYSRESETREQ has confused core. Trying to reconnect and use VECTRESET.
    77. Found SWD-DP with ID 0x2BA01477
    78. AP-IDR: 0x24770011, Type: AHB-AP
    79. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    80. **************************
    81. WARNING: Failed to reset CPU. VECTRESET has confused core.
    82. **************************
    83. **************************
    84. WARNING: CPU did not halt after reset.
    85. **************************
    86. **************************
    87. WARNING: T-bit of XPSR is 0 but should be 1. Changed to 1.
    88. **************************
    89. **************************
    90. WARNING: Could not set S_RESET_ST
    91. **************************
    92. Found SWD-DP with ID 0x2BA01477
    93. Protection bytes in flash at addr. 0x400 - 0x40F indicate that readout protection is set.
    94. For debugger connection the device needs to be unsecured.
    95. Note: Unsecuring will trigger a mass erase of the internal flash.
    96. Device will be unsecured now.
    97. ****** Error: DAP error while reading AIRCR / CPUID register
    98. Sleep(100)
    99. Loading binary file ./4chMpu_Ver1.2.4.0.protected.bin
    100. Reading 140440 bytes data from target memory @ 0x00000000.
    101. Verify failed @ address 0x00000000.
    102. Expected 00 read FF
    103. 00000000 = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
    104. 00000010 = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
    105. 00000020 = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
    106. 00000030 = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
    107. 00000400 = FF FF FF FF FF FF FF FF FF FF FF FF FE FF FF FF
    108. Found SWD-DP with ID 0x2BA01477
    109. Found SWD-DP with ID 0x2BA01477
    110. AP-IDR: 0x24770011, Type: AHB-AP
    111. AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
    112. Found Cortex-M4 r0p1, Little endian.
    113. FPUnit: 6 code (BP) slots and 2 literal slots
    114. CoreSight components:
    115. ROMTbl 0 @ E00FF000
    116. ROMTbl 0 [0]: FFF0F000, CID: B105E00D, PID: 000BB00C SCS
    117. ROMTbl 0 [1]: FFF02000, CID: B105E00D, PID: 003BB002 DWT
    118. ROMTbl 0 [2]: FFF03000, CID: B105E00D, PID: 002BB003 FPB
    119. ROMTbl 0 [3]: FFF01000, CID: B105E00D, PID: 003BB001 ITM
    120. ROMTbl 0 [4]: FFF41000, CID: B105900D, PID: 000BB9A1 TPIU
    121. ROMTbl 0 [5]: FFF42000, CID: B105900D, PID: 000BB925 ETM
    122. ROMTbl 0 [6]: FFF43000, CID: B105900D, PID: 003BB907 ETB
    123. ROMTbl 0 [7]: FFF44000, CID: B105900D, PID: 001BB908 CSTF
    124. Script processing completed.
    Display All


    first time the ram is read (after loadfile) it is read correctly.

    then the verifybin command fails.

    FW still doesn't run

    best regards
    Max
  • Hi Max,

    FW still doesn't run

    This is the expected result. At the end of this script file, the flash is erased, as mentioned in the ouput of J-Link Commander:

    Found SWD-DP with ID 0x2BA01477
    Protection bytes in flash at addr. 0x400 - 0x40F indicate that readout protection is set.
    For debugger connection the device needs to be unsecured.
    Note: Unsecuring will trigger a mass erase of the internal flash.
    Device will be unsecured now.


    I just wanted to show that the reason why the verify seemed to work in the first place was the Flash cache of J-Link.
    The firmware will work with this script:

    C Source Code

    1. unlock Kinetis
    2. r
    3. sleep 100
    4. erase
    5. loadfile ./4chMpu_Ver1.2.4.0.protected.bin,0x0
    6. r0
    7. r1
    8. exit



    Best regards,
    Niklas
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.