swd_probe_app.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 {
  88. KeyNone,
  89. KeyUp,
  90. KeyRight,
  91. KeyDown,
  92. KeyLeft,
  93. KeyOK
  94. } KeyCode;
  95. typedef enum {
  96. EventTimerTick,
  97. EventKeyPress,
  98. } EventType;
  99. typedef struct {
  100. EventType type;
  101. InputEvent input;
  102. } AppEvent;
  103. typedef struct {
  104. uint32_t ctrlstat;
  105. bool ctrlstat_ok;
  106. uint32_t dlcr;
  107. bool dlcr_ok;
  108. uint32_t dlpidr;
  109. bool dlpidr_ok;
  110. uint32_t dpidr;
  111. bool dpidr_ok;
  112. uint32_t eventstat;
  113. bool eventstat_ok;
  114. uint32_t select;
  115. bool select_ok;
  116. uint32_t targetid;
  117. bool targetid_ok;
  118. } swd_dpreg_t;
  119. typedef struct {
  120. bool ok;
  121. bool tested;
  122. uint8_t revision;
  123. uint16_t designer;
  124. uint8_t class;
  125. uint8_t variant;
  126. uint8_t type;
  127. uint32_t base;
  128. } swd_apidr_info_t;
  129. typedef struct {
  130. uint8_t revision;
  131. uint8_t partno;
  132. uint8_t version;
  133. uint16_t designer;
  134. } swd_dpidr_info_t;
  135. typedef struct {
  136. uint8_t revision;
  137. uint16_t partno;
  138. uint16_t designer;
  139. } swd_targetid_info_t;
  140. typedef struct sScriptContext ScriptContext;
  141. typedef struct {
  142. Storage* storage;
  143. Gui* gui;
  144. DialogsApp* dialogs;
  145. NotificationApp* notification;
  146. FuriTimer* timer;
  147. UsbUart* uart;
  148. ViewPort* view_port;
  149. FuriMessageQueue* event_queue;
  150. FuriMutex* swd_mutex;
  151. FuriMutex* gui_mutex;
  152. swd_targetid_info_t targetid_info;
  153. swd_dpidr_info_t dpidr_info;
  154. swd_dpreg_t dp_regs;
  155. swd_apidr_info_t apidr_info[256];
  156. ScriptContext* script;
  157. ScriptContext* commandline;
  158. uint8_t timeout_overdue;
  159. uint32_t loop_count;
  160. uint8_t current_mask_id;
  161. uint32_t current_mask;
  162. uint8_t io_swc;
  163. uint8_t io_swd;
  164. uint8_t io_num_swc;
  165. uint8_t io_num_swd;
  166. int32_t detected_timeout;
  167. uint32_t swd_clock_delay;
  168. uint32_t swd_idle_bits;
  169. bool detected;
  170. bool detected_device;
  171. bool detected_notified;
  172. uint32_t mode_page;
  173. uint8_t ap_pos;
  174. uint8_t ap_scanned;
  175. uint32_t coresight_pos[16];
  176. uint32_t coresight_count[16];
  177. uint8_t coresight_level;
  178. uint32_t coresight_bases[16];
  179. uint32_t hex_addr;
  180. uint8_t hex_select;
  181. uint8_t hex_buffer[32];
  182. uint8_t hex_buffer_valid[8];
  183. char state_string[64];
  184. char script_detected[MAX_FILE_LENGTH];
  185. bool script_detected_executed;
  186. } AppFSM;
  187. struct sScriptContext {
  188. AppFSM* app;
  189. ScriptContext* parent;
  190. char filename[MAX_FILE_LENGTH];
  191. /* when used with string input */
  192. char line_data[128];
  193. uint64_t line_pos;
  194. /* when used with file input */
  195. File* script_file;
  196. uint64_t position;
  197. uint32_t selected_ap;
  198. uint32_t max_tries;
  199. uint32_t block_size;
  200. bool abort;
  201. bool restart;
  202. bool errors_ignore;
  203. bool status_ignore;
  204. bool goto_active;
  205. char goto_label[64];
  206. };
  207. typedef struct {
  208. const char* prefix;
  209. bool (*func)(ScriptContext* ctx);
  210. } ScriptFunctionInfo;
  211. uint8_t swd_read_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t* data);
  212. #endif