swd_probe_app.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #ifndef __SWD_PROBE_APP_H
  2. #define __SWD_PROBE_APP_H
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <furi.h>
  9. #include <furi_hal.h>
  10. #include <furi_hal_speaker.h>
  11. #include <gui/gui.h>
  12. #include <gui/elements.h>
  13. #include <dialogs/dialogs.h>
  14. #include <input/input.h>
  15. #include <storage/storage.h>
  16. #include <dolphin/dolphin.h>
  17. #include <notification/notification.h>
  18. #include <notification/notification_messages.h>
  19. #include "usb_uart.h"
  20. #define TAG "SWD"
  21. /* short debug message */
  22. #define DBGS(format) furi_log_print_format(FuriLogLevelDebug, TAG, "%s: " format, __FUNCTION__)
  23. /* formatted debug message */
  24. #define DBG(format, ...) \
  25. furi_log_print_format(FuriLogLevelDebug, TAG, "%s: " format, __FUNCTION__, __VA_ARGS__)
  26. /* log message*/
  27. #define LOG(...) furi_log_print_format(FuriLogLevelDefault, TAG, __VA_ARGS__)
  28. #define COUNT(x) ((size_t)(sizeof(x) / sizeof((x)[0])))
  29. #define ARRAY_SIZE(x) COUNT(x)
  30. #define SWD_DELAY_US 0
  31. #define TIMER_HZ 25
  32. #define TIMEOUT 3
  33. #define QUEUE_SIZE 8
  34. #define IDLE_BITS 8
  35. #define CLOCK_DELAY 0
  36. #define MAX_FILE_LENGTH 128
  37. #define SCRIPT_MAX_LINES 1000
  38. typedef enum {
  39. ModePageScan = 0,
  40. ModePageFound = 1,
  41. ModePageDPRegs = 2,
  42. ModePageDPID = 3,
  43. ModePageAPID = 4,
  44. ModePageCount = 5,
  45. ModePageHexDump = 0x100,
  46. ModePageScript = 0x101,
  47. ModePageCoresight = 0x102,
  48. } ModePages;
  49. #define CDBGPWRUPREQ (1 << 28)
  50. #define CDBGPWRUPACK (1 << 29)
  51. #define CSYSPWRUPREQ (1 << 30)
  52. #define CSYSPWRUPACK (1 << 31)
  53. #define WDATAERR (1 << 7)
  54. #define STICKYERR (1 << 5)
  55. #define STAT_ERROR_FLAGS (WDATAERR | STICKYERR)
  56. #define REG_IDCODE 0x00
  57. #define REG_CTRLSTAT 0x01
  58. #define REG_CTRLSTAT_BANK 0x00
  59. #define REG_DLCR 0x01
  60. #define REG_DLCR_BANK 0x01
  61. #define REG_TARGETID 0x01
  62. #define REG_TARGETID_BANK 0x02
  63. #define REG_DLPIDR 0x01
  64. #define REG_DLPIDR_BANK 0x03
  65. #define REG_EVENTSTAT 0x01
  66. #define REG_EVENTSTAT_BANK 0x04
  67. #define REG_SELECT 0x02
  68. #define MEMAP_CSW 0x00
  69. #define MEMAP_TAR 0x04
  70. #define MEMAP_DRW 0x0C
  71. #define AP_IDR 0xFC
  72. #define AP_BASE 0xF8
  73. #define SCS_CPUID 0xE000ED00u
  74. #define SCS_CPACR 0xE000ED88u
  75. #define SCS_DHCSR 0xE000EDF0u
  76. #define SCS_DHCSR_S_HALT (1u << 17)
  77. #define SCS_DHCSR_C_MASKINTS (1u << 3)
  78. #define SCS_DHCSR_C_STEP (1u << 2)
  79. #define SCS_DHCSR_C_HALT (1u << 1)
  80. #define SCS_DHCSR_C_DEBUGEN (1u << 0)
  81. #define SCS_DHCSR_KEY 0xA05F0000u
  82. #define SCS_DCRSR 0xE000EDF4u
  83. #define SCS_DCRSR_RD 0x00000000u
  84. #define SCS_DCRSR_WR 0x00010000u
  85. #define SCS_DCRDR 0xE000EDF8u
  86. #define SCS_DEMCR 0xE000EDFCu
  87. typedef enum { KeyNone, KeyUp, KeyRight, KeyDown, KeyLeft, KeyOK } KeyCode;
  88. typedef enum {
  89. EventTimerTick,
  90. EventKeyPress,
  91. } EventType;
  92. typedef struct {
  93. EventType type;
  94. InputEvent input;
  95. } AppEvent;
  96. typedef struct {
  97. uint32_t ctrlstat;
  98. bool ctrlstat_ok;
  99. uint32_t dlcr;
  100. bool dlcr_ok;
  101. uint32_t dlpidr;
  102. bool dlpidr_ok;
  103. uint32_t dpidr;
  104. bool dpidr_ok;
  105. uint32_t eventstat;
  106. bool eventstat_ok;
  107. uint32_t select;
  108. bool select_ok;
  109. uint32_t targetid;
  110. bool targetid_ok;
  111. } swd_dpreg_t;
  112. typedef struct {
  113. bool ok;
  114. bool tested;
  115. uint8_t revision;
  116. uint16_t designer;
  117. uint8_t class;
  118. uint8_t variant;
  119. uint8_t type;
  120. uint32_t base;
  121. } swd_apidr_info_t;
  122. typedef struct {
  123. uint8_t revision;
  124. uint8_t partno;
  125. uint8_t version;
  126. uint16_t designer;
  127. } swd_dpidr_info_t;
  128. typedef struct {
  129. uint8_t revision;
  130. uint16_t partno;
  131. uint16_t designer;
  132. } swd_targetid_info_t;
  133. typedef struct sScriptContext ScriptContext;
  134. typedef struct {
  135. Storage* storage;
  136. Gui* gui;
  137. DialogsApp* dialogs;
  138. NotificationApp* notification;
  139. FuriTimer* timer;
  140. UsbUart* uart;
  141. ViewPort* view_port;
  142. FuriMessageQueue* event_queue;
  143. FuriMutex* swd_mutex;
  144. FuriMutex* gui_mutex;
  145. swd_targetid_info_t targetid_info;
  146. swd_dpidr_info_t dpidr_info;
  147. swd_dpreg_t dp_regs;
  148. swd_apidr_info_t apidr_info[256];
  149. ScriptContext* script;
  150. ScriptContext* commandline;
  151. uint8_t timeout_overdue;
  152. uint32_t loop_count;
  153. uint8_t current_mask_id;
  154. uint32_t current_mask;
  155. uint8_t io_swc;
  156. uint8_t io_swd;
  157. uint8_t io_num_swc;
  158. uint8_t io_num_swd;
  159. int32_t detected_timeout;
  160. uint32_t swd_clock_delay;
  161. uint32_t swd_idle_bits;
  162. bool detected;
  163. bool detected_device;
  164. bool detected_notified;
  165. uint32_t mode_page;
  166. uint8_t ap_pos;
  167. uint8_t ap_scanned;
  168. uint32_t coresight_pos[16];
  169. uint32_t coresight_count[16];
  170. uint8_t coresight_level;
  171. uint32_t coresight_bases[16];
  172. uint32_t hex_addr;
  173. uint8_t hex_select;
  174. uint8_t hex_buffer[32];
  175. uint8_t hex_buffer_valid[8];
  176. char state_string[64];
  177. char script_detected[MAX_FILE_LENGTH];
  178. bool script_detected_executed;
  179. } AppFSM;
  180. struct sScriptContext {
  181. AppFSM* app;
  182. ScriptContext* parent;
  183. char filename[MAX_FILE_LENGTH];
  184. /* when used with string input */
  185. char line_data[128];
  186. uint64_t line_pos;
  187. /* when used with file input */
  188. File* script_file;
  189. uint64_t position;
  190. uint32_t selected_ap;
  191. uint32_t max_tries;
  192. uint32_t block_size;
  193. bool abort;
  194. bool restart;
  195. bool errors_ignore;
  196. bool status_ignore;
  197. bool goto_active;
  198. char goto_label[64];
  199. };
  200. typedef struct {
  201. const char* prefix;
  202. bool (*func)(ScriptContext* ctx);
  203. } ScriptFunctionInfo;
  204. uint8_t swd_read_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t* data);
  205. #endif