flipperzero-firmware_official 994 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. commit 104a1998a5e2f77cea99c31311541864fb98d4c2
  2. Author: あく <alleteam@gmail.com>
  3. Date: Tue Nov 1 19:27:25 2022 +0900
  4. Furi: raise bkpt only if debug session initiated, add debug support for release builds (#1957)
  5. * Fix hard crash on some custom firmwares in RELEASE mode
  6. * Furi: anya wa erai, anya wa eleganto, raise bkpt only if debug session initiated, add debug support for release builds
  7. Co-authored-by: DerSkythe <skif@skif.ws>
  8. diff --git a/furi/core/check.c b/furi/core/check.c
  9. index 00c20575a..1c2a005f3 100644
  10. --- a/furi/core/check.c
  11. +++ b/furi/core/check.c
  12. @@ -1,6 +1,7 @@
  13. #include "check.h"
  14. #include "common_defines.h"
  15. +#include <stm32wbxx.h>
  16. #include <furi_hal_console.h>
  17. #include <furi_hal_power.h>
  18. #include <furi_hal_rtc.h>
  19. @@ -24,16 +25,30 @@ PLACE_IN_SECTION("MB_MEM2") uint32_t __furi_check_registers[12] = {0};
  20. : \
  21. : "memory");
  22. -// Restore registers and halt MCU
  23. -#define RESTORE_REGISTERS_AND_HALT_MCU() \
  24. - asm volatile("ldr r12, =__furi_check_registers \n" \
  25. - "ldm r12, {r0-r11} \n" \
  26. +/** Restore registers and halt MCU
  27. + *
  28. + * - Always use it with GET_MESSAGE_AND_STORE_REGISTERS
  29. + * - If debugger is(was) connected this routine will raise bkpt
  30. + * - If debugger is not connected then endless loop
  31. + *
  32. + */
  33. +#define RESTORE_REGISTERS_AND_HALT_MCU(debug) \
  34. + register const bool r0 asm("r0") = debug; \
  35. + asm volatile("cbnz r0, with_debugger%= \n" \
  36. + "ldr r12, =__furi_check_registers\n" \
  37. + "ldm r12, {r0-r11} \n" \
  38. "loop%=: \n" \
  39. - "bkpt 0x00 \n" \
  40. "wfi \n" \
  41. - "b loop%= \n" \
  42. - : \
  43. + "b loop%= \n" \
  44. + "with_debugger%=: \n" \
  45. + "ldr r12, =__furi_check_registers\n" \
  46. + "ldm r12, {r0-r11} \n" \
  47. + "debug_loop%=: \n" \
  48. + "bkpt 0x00 \n" \
  49. + "wfi \n" \
  50. + "b debug_loop%= \n" \
  51. : \
  52. + : "r"(r0) \
  53. : "memory");
  54. extern size_t xPortGetTotalHeapSize(void);
  55. @@ -96,16 +111,19 @@ FURI_NORETURN void __furi_crash() {
  56. }
  57. __furi_print_heap_info();
  58. -#ifdef FURI_DEBUG
  59. - furi_hal_console_puts("\r\nSystem halted. Connect debugger for more info\r\n");
  60. - furi_hal_console_puts("\033[0m\r\n");
  61. - RESTORE_REGISTERS_AND_HALT_MCU();
  62. -#else
  63. - furi_hal_rtc_set_fault_data((uint32_t)__furi_check_message);
  64. - furi_hal_console_puts("\r\nRebooting system.\r\n");
  65. - furi_hal_console_puts("\033[0m\r\n");
  66. - furi_hal_power_reset();
  67. -#endif
  68. + // Check if debug enabled by DAP
  69. + // https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR?lang=en
  70. + bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
  71. + if(debug) {
  72. + furi_hal_console_puts("\r\nSystem halted. Connect debugger for more info\r\n");
  73. + furi_hal_console_puts("\033[0m\r\n");
  74. + RESTORE_REGISTERS_AND_HALT_MCU(debug);
  75. + } else {
  76. + furi_hal_rtc_set_fault_data((uint32_t)__furi_check_message);
  77. + furi_hal_console_puts("\r\nRebooting system.\r\n");
  78. + furi_hal_console_puts("\033[0m\r\n");
  79. + furi_hal_power_reset();
  80. + }
  81. __builtin_unreachable();
  82. }
  83. @@ -124,6 +142,11 @@ FURI_NORETURN void __furi_halt() {
  84. furi_hal_console_puts(__furi_check_message);
  85. furi_hal_console_puts("\r\nSystem halted. Bye-bye!\r\n");
  86. furi_hal_console_puts("\033[0m\r\n");
  87. - RESTORE_REGISTERS_AND_HALT_MCU();
  88. +
  89. + // Check if debug enabled by DAP
  90. + // https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR?lang=en
  91. + bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
  92. + RESTORE_REGISTERS_AND_HALT_MCU(debug);
  93. +
  94. __builtin_unreachable();
  95. }