swd_probe_app.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <gui/elements.h>
  4. #include <dialogs/dialogs.h>
  5. #include <input/input.h>
  6. #include <storage/storage.h>
  7. #include <stdlib.h>
  8. #include <dolphin/dolphin.h>
  9. #include <notification/notification.h>
  10. #include <notification/notification_messages.h>
  11. #include "swd_probe_app.h"
  12. #include "swd_probe_icons.h"
  13. #include "jep106.h"
  14. #define COUNT(x) (sizeof(x) / sizeof((x)[0]))
  15. static void render_callback(Canvas* const canvas, void* cb_ctx);
  16. static bool swd_message_process(AppFSM* ctx);
  17. static uint8_t swd_transfer(AppFSM* const ctx, bool ap, bool write, uint8_t a23, uint32_t* data);
  18. static const GpioPin* gpios[] = {
  19. &gpio_ext_pc0,
  20. &gpio_ext_pc1,
  21. &gpio_ext_pc3,
  22. &gpio_ext_pb2,
  23. &gpio_ext_pb3,
  24. &gpio_ext_pa4,
  25. &gpio_ext_pa6,
  26. &gpio_ext_pa7};
  27. static const char* gpio_names[] = {"PC0", "PC1", "PC3", "PB2", "PB3", "PA4", "PA6", "PA7"};
  28. /* bit set: clock, else data */
  29. static const uint8_t gpio_direction_mask[6] =
  30. {0b10101010, 0b01010101, 0b11001100, 0b00110011, 0b11110000, 0b00001111};
  31. static const uint8_t gpio_direction_ind[6] = "-\\||/-";
  32. static bool has_multiple_bits(uint8_t x) {
  33. return (x & (x - 1)) != 0;
  34. }
  35. static int get_bit_num(uint8_t x) {
  36. return __builtin_ctz(x);
  37. }
  38. static const char* gpio_name(uint8_t mask) {
  39. if(has_multiple_bits(mask)) {
  40. return "Pxx";
  41. }
  42. int io = get_bit_num(mask);
  43. if(io >= COUNT(gpio_names)) {
  44. return "Pxx";
  45. }
  46. return gpio_names[io];
  47. }
  48. static void swd_configure_pins(AppFSM* const ctx, bool output) {
  49. if(ctx->mode_page != ModePageScan && ctx->io_num_swc < 8 && ctx->io_num_swd < 8) {
  50. furi_hal_gpio_init(
  51. gpios[ctx->io_num_swc], GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  52. if(!output) {
  53. furi_hal_gpio_init(
  54. gpios[ctx->io_num_swd], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  55. } else {
  56. furi_hal_gpio_init(
  57. gpios[ctx->io_num_swd], GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh);
  58. }
  59. return;
  60. }
  61. for(int io = 0; io < 8; io++) {
  62. uint8_t bitmask = 1 << io;
  63. /* if neither candidate for SWC nor SWD then skip */
  64. if(!(ctx->io_swc & bitmask) && !(ctx->io_swd & bitmask)) {
  65. furi_hal_gpio_init(gpios[io], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  66. continue;
  67. }
  68. if(ctx->current_mask & bitmask) {
  69. /* set for clock */
  70. furi_hal_gpio_init(gpios[io], GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  71. } else {
  72. /* set for data */
  73. if(!output) {
  74. furi_hal_gpio_init(gpios[io], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  75. } else {
  76. furi_hal_gpio_init(
  77. gpios[io], GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh);
  78. }
  79. }
  80. }
  81. }
  82. static void swd_set_clock(AppFSM* const ctx, const uint8_t level) {
  83. if(ctx->mode_page != ModePageScan && ctx->io_num_swc < 8) {
  84. furi_hal_gpio_write(gpios[ctx->io_num_swc], level);
  85. return;
  86. }
  87. for(int io = 0; io < 8; io++) {
  88. uint8_t bitmask = 1 << io;
  89. /* if no candidate for SWC then skip */
  90. if(!(ctx->io_swc & bitmask)) {
  91. continue;
  92. }
  93. if(ctx->current_mask & bitmask) {
  94. furi_hal_gpio_write(gpios[io], level);
  95. }
  96. }
  97. }
  98. static void swd_set_data(AppFSM* const ctx, const uint8_t level) {
  99. if(ctx->mode_page != ModePageScan && ctx->io_num_swd < 8) {
  100. furi_hal_gpio_write(gpios[ctx->io_num_swd], level);
  101. return;
  102. }
  103. for(int io = 0; io < 8; io++) {
  104. uint8_t bitmask = 1 << io;
  105. /* if no candidate for SWD then skip */
  106. if(!(ctx->io_swd & bitmask)) {
  107. continue;
  108. }
  109. if(!(ctx->current_mask & bitmask)) {
  110. furi_hal_gpio_write(gpios[io], level);
  111. }
  112. }
  113. }
  114. static uint8_t swd_get_data(AppFSM* const ctx) {
  115. if(ctx->mode_page != ModePageScan && ctx->io_num_swd < 8) {
  116. return furi_hal_gpio_read(gpios[ctx->io_num_swd]);
  117. }
  118. uint8_t bits = 0;
  119. for(int io = 0; io < 8; io++) {
  120. uint8_t bitmask = 1 << io;
  121. /* if no candidate for SWD then skip */
  122. if(!(ctx->io_swd & bitmask)) {
  123. continue;
  124. }
  125. bits |= furi_hal_gpio_read(gpios[io]) ? bitmask : 0;
  126. }
  127. return bits;
  128. }
  129. static void swd_clock_delay(AppFSM* const ctx) {
  130. if(ctx->swd_clock_delay) {
  131. furi_delay_us(ctx->swd_clock_delay);
  132. }
  133. }
  134. static void swd_write_bit(AppFSM* const ctx, bool level) {
  135. swd_set_clock(ctx, 0);
  136. swd_set_data(ctx, level);
  137. swd_clock_delay(ctx);
  138. swd_set_clock(ctx, 1);
  139. swd_clock_delay(ctx);
  140. swd_set_clock(ctx, 0);
  141. }
  142. static uint8_t swd_read_bit(AppFSM* const ctx) {
  143. swd_set_clock(ctx, 1);
  144. swd_clock_delay(ctx);
  145. swd_set_clock(ctx, 0);
  146. uint8_t bits = swd_get_data(ctx);
  147. swd_clock_delay(ctx);
  148. swd_set_clock(ctx, 1);
  149. return bits;
  150. }
  151. /* send a byte or less LSB-first */
  152. static void swd_write_byte(AppFSM* const ctx, const uint8_t data, size_t bits) {
  153. for(size_t pos = 0; pos < bits; pos++) {
  154. swd_write_bit(ctx, data & (1 << pos));
  155. }
  156. }
  157. /* send a sequence of bytes LSB-first */
  158. static void swd_write(AppFSM* const ctx, const uint8_t* data, size_t bits) {
  159. size_t byte_pos = 0;
  160. while(bits > 0) {
  161. size_t remain = (bits > 8) ? 8 : bits;
  162. swd_write_byte(ctx, data[byte_pos++], remain);
  163. bits -= remain;
  164. }
  165. }
  166. static uint8_t swd_transfer(AppFSM* const ctx, bool ap, bool write, uint8_t a23, uint32_t* data) {
  167. swd_set_data(ctx, false);
  168. swd_configure_pins(ctx, true);
  169. uint32_t idle = 0;
  170. swd_write(ctx, (uint8_t*)&idle, ctx->swd_idle_bits);
  171. uint8_t request[] = {0};
  172. request[0] |= 0x01; /* start bit*/
  173. request[0] |= ap ? 0x02 : 0; /* APnDP */
  174. request[0] |= write ? 0 : 0x04; /* operation */
  175. request[0] |= (a23 & 0x01) ? 0x08 : 0; /* A[2:3] */
  176. request[0] |= (a23 & 0x02) ? 0x10 : 0; /* A[2:3] */
  177. request[0] |= 0x80; /* park bit */
  178. request[0] |= __builtin_parity(request[0]) ? 0x20 : 0; /* parity */
  179. swd_write(ctx, request, sizeof(request) * 8);
  180. /* turnaround cycle */
  181. swd_configure_pins(ctx, false);
  182. uint8_t ack = 0;
  183. /* receive 3 ACK bits */
  184. for(int pos = 0; pos < 3; pos++) {
  185. ack >>= 1;
  186. ack |= swd_read_bit(ctx) ? 0x04 : 0;
  187. }
  188. /* force ABORT/CTRL to always work */
  189. if(!ap && a23 == 0) {
  190. ack = 1;
  191. }
  192. if(ack != 0x01) {
  193. return ack;
  194. }
  195. if(write) {
  196. swd_write_bit(ctx, 0);
  197. swd_configure_pins(ctx, true);
  198. /* send 32 WDATA bits */
  199. for(int pos = 0; pos < 32; pos++) {
  200. swd_write_bit(ctx, *data & (1 << pos));
  201. }
  202. /* send parity bit */
  203. swd_write_bit(ctx, __builtin_parity(*data));
  204. } else {
  205. *data = 0;
  206. /* receive 32 RDATA bits */
  207. for(int pos = 0; pos < 32; pos++) {
  208. *data >>= 1;
  209. *data |= swd_read_bit(ctx) ? 0x80000000 : 0;
  210. }
  211. /* receive parity bit */
  212. bool parity = swd_read_bit(ctx);
  213. if(parity != __builtin_parity(*data)) {
  214. return 8;
  215. }
  216. }
  217. swd_set_data(ctx, false);
  218. swd_configure_pins(ctx, true);
  219. return ack;
  220. }
  221. /* A line reset is achieved by holding the data signal HIGH for at least 50 clock cycles, followed by at least two idle cycles. */
  222. static void swd_line_reset(AppFSM* const ctx) {
  223. for(int bitcount = 0; bitcount < 50; bitcount += 8) {
  224. swd_write_byte(ctx, 0xFF, 8);
  225. }
  226. swd_write_byte(ctx, 0, 8);
  227. ctx->dp_regs.select_ok = false;
  228. }
  229. static void swd_abort(AppFSM* const ctx) {
  230. uint32_t dpidr;
  231. /* first reset the line */
  232. swd_line_reset(ctx);
  233. swd_transfer(ctx, false, false, 0, &dpidr);
  234. uint32_t abort = 0x0E;
  235. swd_transfer(ctx, false, true, 0, &abort);
  236. }
  237. /*
  238. static uint8_t swd_transferxx(AppFSM* const ctx, bool ap, bool write, uint8_t a23, uint32_t* data) {
  239. uint8_t ret = 0;
  240. for(int tries = 0; tries < 3; tries++) {
  241. ret = swd_transfer(ctx, ap, write, a23, data);
  242. if(ret == 1) {
  243. return ret;
  244. }
  245. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_transfer: failed: %d/3", tries);
  246. swd_abort(ctx);
  247. }
  248. return 9;
  249. }*/
  250. static void swd_abort_simple(AppFSM* const ctx) {
  251. uint32_t abort = 0x0E;
  252. swd_transfer(ctx, false, true, 0, &abort);
  253. uint32_t dpidr;
  254. if(swd_transfer(ctx, false, false, 0, &dpidr) != 1) {
  255. swd_abort(ctx);
  256. }
  257. }
  258. static uint8_t swd_select(AppFSM* const ctx, uint8_t ap_sel, uint8_t ap_bank, uint8_t dp_bank) {
  259. uint32_t bank_reg = (ap_sel << 24) | ((ap_bank & 0x0F) << 4) | (dp_bank & 0x0F);
  260. if(ctx->dp_regs.select_ok && bank_reg == ctx->dp_regs.select) {
  261. return 1;
  262. }
  263. uint8_t ret = swd_transfer(ctx, false, true, 2, &bank_reg);
  264. if(ret != 1) {
  265. ctx->dp_regs.select_ok = false;
  266. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_select: failed: %d", ret);
  267. }
  268. ctx->dp_regs.select = bank_reg;
  269. ctx->dp_regs.select_ok = true;
  270. return ret;
  271. }
  272. static uint8_t
  273. swd_read_dpbank(AppFSM* const ctx, uint8_t dp_off, uint8_t dp_bank, uint32_t* data) {
  274. /* select target bank */
  275. uint8_t ret = swd_select(ctx, 0, 0, dp_bank);
  276. if(ret != 1) {
  277. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_dpbank: swd_select failed");
  278. return ret;
  279. }
  280. /* read data from it */
  281. *data = 0;
  282. ret = swd_transfer(ctx, false, false, dp_off, data);
  283. if(ret != 1) {
  284. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_dpbank: failed: %d", ret);
  285. return ret;
  286. }
  287. return ret;
  288. }
  289. static uint8_t swd_read_ap(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t* data) {
  290. uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
  291. if(ret != 1) {
  292. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_ap: swd_select failed");
  293. return ret;
  294. }
  295. ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
  296. *data = 0;
  297. ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
  298. if(ret != 1) {
  299. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_ap: failed: %d", ret);
  300. return ret;
  301. }
  302. return ret;
  303. }
  304. static uint8_t swd_read_ap_single(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t* data) {
  305. uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
  306. if(ret != 1) {
  307. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_ap_single: swd_select failed");
  308. return ret;
  309. }
  310. *data = 0;
  311. ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
  312. if(ret != 1) {
  313. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_read_ap_single: failed: %d", ret);
  314. return ret;
  315. }
  316. return ret;
  317. }
  318. static uint8_t swd_write_ap(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t data) {
  319. uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
  320. if(ret != 1) {
  321. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_write_ap: swd_select failed");
  322. return ret;
  323. }
  324. ret = swd_transfer(ctx, true, true, (ap_off >> 2) & 3, &data);
  325. if(ret != 1) {
  326. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_write_ap: failed: %d", ret);
  327. return ret;
  328. }
  329. return ret;
  330. }
  331. static uint8_t swd_write_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t data) {
  332. uint8_t ret = 0;
  333. ret |= swd_write_ap(ctx, ap, MEMAP_CSW, 0x03000012);
  334. ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
  335. ret |= swd_write_ap(ctx, ap, MEMAP_DRW, data);
  336. if(ret != 1) {
  337. swd_abort(ctx);
  338. }
  339. return ret;
  340. }
  341. static uint8_t swd_read_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t* data) {
  342. uint8_t ret = 0;
  343. ret |= swd_write_ap(ctx, ap, MEMAP_CSW, 0x03000012);
  344. ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
  345. ret |= swd_read_ap(ctx, ap, MEMAP_DRW, data);
  346. if(ret != 1) {
  347. swd_abort(ctx);
  348. }
  349. return ret;
  350. }
  351. static uint8_t swd_read_memory_block(
  352. AppFSM* const ctx,
  353. uint8_t ap,
  354. uint32_t address,
  355. uint8_t* buf,
  356. uint32_t len) {
  357. uint8_t ret = 0;
  358. uint32_t data = 0;
  359. bool first = true;
  360. ret |= swd_write_ap(ctx, ap, MEMAP_CSW, 0x03000012);
  361. ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
  362. ret |= swd_read_ap_single(ctx, ap, MEMAP_DRW, &data);
  363. for(size_t pos = 0; pos < len; pos += 4) {
  364. data = 0xDEADBEEF;
  365. ret |= swd_read_ap_single(ctx, ap, MEMAP_DRW, &data);
  366. furi_log_print_format(FuriLogLevelDefault, TAG, "##read %lX", data);
  367. memcpy(&buf[pos], &data, 4);
  368. if(ret != 1) {
  369. swd_abort(ctx);
  370. return ret;
  371. }
  372. }
  373. return ret;
  374. }
  375. static uint32_t swd_detect(AppFSM* const ctx) {
  376. swd_set_data(ctx, false);
  377. swd_configure_pins(ctx, true);
  378. uint8_t data[] = {0xA5};
  379. swd_write(ctx, data, sizeof(data) * 8);
  380. /* turnaround cycle */
  381. swd_configure_pins(ctx, false);
  382. uint8_t ack_bits[3];
  383. uint8_t rdata[32];
  384. /* receive 3 ACK bits */
  385. for(int pos = 0; pos < 3; pos++) {
  386. ack_bits[pos] = swd_read_bit(ctx);
  387. }
  388. /* receive 32 RDATA bits */
  389. for(int pos = 0; pos < 32; pos++) {
  390. rdata[pos] = swd_read_bit(ctx);
  391. }
  392. /* receive parity bit */
  393. uint8_t parity = swd_read_bit(ctx);
  394. for(int io = 0; io < 8; io++) {
  395. uint8_t bitmask = 1 << io;
  396. /* skip if it's a clock */
  397. if(ctx->current_mask & bitmask) {
  398. continue;
  399. }
  400. uint8_t ack = 0;
  401. for(int pos = 0; pos < 3; pos++) {
  402. ack >>= 1;
  403. ack |= (ack_bits[pos] & bitmask) ? 4 : 0;
  404. }
  405. uint32_t dpidr = 0;
  406. for(int pos = 0; pos < 32; pos++) {
  407. dpidr >>= 1;
  408. dpidr |= (rdata[pos] & bitmask) ? 0x80000000 : 0;
  409. }
  410. if(ack == 1 && dpidr != 0 && dpidr != 0xFFFFFFFF) {
  411. bool received_parity = (parity & bitmask);
  412. if(__builtin_parity(dpidr) == received_parity) {
  413. ctx->dp_regs.dpidr = dpidr;
  414. ctx->dp_regs.dpidr_ok = true;
  415. ctx->detected = true;
  416. ctx->io_swd = bitmask;
  417. ctx->io_swc &= ctx->current_mask;
  418. furi_log_print_format(
  419. FuriLogLevelDefault,
  420. TAG,
  421. "swd_detect: data: %08lX, io_swd %02X, io_swc %02X",
  422. dpidr,
  423. ctx->io_swd,
  424. ctx->io_swc);
  425. if(!has_multiple_bits(ctx->io_swc)) {
  426. ctx->io_num_swd = get_bit_num(ctx->io_swd);
  427. ctx->io_num_swc = get_bit_num(ctx->io_swc);
  428. }
  429. }
  430. }
  431. }
  432. swd_set_data(ctx, false);
  433. swd_configure_pins(ctx, true);
  434. return 0;
  435. }
  436. static void swd_scan(AppFSM* const ctx) {
  437. /* To switch SWJ-DP from JTAG to SWD operation:
  438. 1. Send at least 50 SWCLKTCK cycles with SWDIOTMS HIGH. This ensures that the current interface is in its reset state. The JTAG interface only detects the 16-bit JTAG-to-SWD sequence starting from the Test-Logic-Reset state.
  439. 2. Send the 16-bit JTAG-to-SWD select sequence 0x79e7 on SWDIOTMS.
  440. 3. Send at least 50 SWCLKTCK cycles with SWDIOTMS HIGH. This ensures that if SWJ-DP was already in SWD operation before sending the select sequence, the SWD interface enters line reset state.
  441. */
  442. swd_configure_pins(ctx, true);
  443. /* reset JTAG interface */
  444. for(int bitcount = 0; bitcount < 50; bitcount += 8) {
  445. swd_write_byte(ctx, 0xFF, 8);
  446. }
  447. /* Send the 16-bit JTAG-to-SWD select sequence */
  448. swd_write_byte(ctx, 0x9E, 8);
  449. swd_write_byte(ctx, 0xE7, 8);
  450. /* resynchronize SWD */
  451. swd_line_reset(ctx);
  452. swd_detect(ctx);
  453. }
  454. static bool swd_ensure_powerup(AppFSM* const ctx) {
  455. if(!(ctx->dp_regs.ctrlstat & (CSYSPWRUPREQ | CDBGPWRUPREQ))) {
  456. /* fetch current CTRL/STAT */
  457. swd_transfer(ctx, false, false, 1, &ctx->dp_regs.ctrlstat);
  458. /* enable requests */
  459. ctx->dp_regs.ctrlstat |= CDBGPWRUPREQ;
  460. ctx->dp_regs.ctrlstat |= CSYSPWRUPREQ;
  461. furi_log_print_format(FuriLogLevelDefault, TAG, "no (CSYSPWRUPREQ | CDBGPWRUPREQ)");
  462. swd_transfer(ctx, false, true, 1, &ctx->dp_regs.ctrlstat);
  463. return false;
  464. }
  465. if(!(ctx->dp_regs.ctrlstat & CDBGPWRUPACK)) {
  466. /* fetch current CTRL/STAT */
  467. swd_transfer(ctx, false, false, 1, &ctx->dp_regs.ctrlstat);
  468. furi_log_print_format(FuriLogLevelDefault, TAG, "no CDBGPWRUPACK");
  469. return false;
  470. }
  471. return true;
  472. }
  473. static void swd_apscan_reset(AppFSM* const ctx) {
  474. for(int reset_ap = 0; reset_ap < COUNT(ctx->apidr_info); reset_ap++) {
  475. ctx->apidr_info[reset_ap].tested = false;
  476. }
  477. }
  478. static bool swd_apscan_test(AppFSM* const ctx, uint32_t ap) {
  479. furi_assert(ctx);
  480. furi_assert(ap < sizeof(ctx->apidr_info));
  481. ctx->apidr_info[ap].tested = true;
  482. uint32_t data = 0;
  483. uint32_t base = 0;
  484. if(swd_read_ap(ctx, ap, AP_IDR, &data) != 1) {
  485. swd_abort(ctx);
  486. return false;
  487. }
  488. if(data == 0) {
  489. return false;
  490. }
  491. furi_log_print_format(FuriLogLevelDefault, TAG, "AP%lu detected", ap);
  492. ctx->apidr_info[ap].ok = true;
  493. ctx->apidr_info[ap].revision = (data >> 24) & 0x0F;
  494. ctx->apidr_info[ap].designer = (data >> 17) & 0x3FF;
  495. ctx->apidr_info[ap].class = (data >> 13) & 0x0F;
  496. ctx->apidr_info[ap].variant = (data >> 4) & 0x0F;
  497. ctx->apidr_info[ap].type = (data >> 0) & 0x0F;
  498. if(swd_read_ap(ctx, ap, AP_BASE, &ctx->apidr_info[ap].base) != 1) {
  499. swd_abort(ctx);
  500. return false;
  501. }
  502. return true;
  503. }
  504. /************************** script helpers **************************/
  505. static bool swd_script_seek_newline(ScriptContext* ctx) {
  506. while(true) {
  507. uint8_t ch = 0;
  508. uint16_t ret = storage_file_read(ctx->script_file, &ch, 1);
  509. if(ret != 1) {
  510. return false;
  511. }
  512. if(ch == '\n') {
  513. return true;
  514. }
  515. }
  516. }
  517. static bool swd_script_skip_whitespace(ScriptContext* ctx) {
  518. while(true) {
  519. uint8_t ch = 0;
  520. uint64_t start_pos = storage_file_tell(ctx->script_file);
  521. uint16_t ret = storage_file_read(ctx->script_file, &ch, 1);
  522. if(ret != 1) {
  523. return false;
  524. }
  525. if(ch == '\n') {
  526. return false;
  527. }
  528. if(ch != ' ') {
  529. storage_file_seek(ctx->script_file, start_pos, true);
  530. return true;
  531. }
  532. }
  533. }
  534. static bool swd_script_get_string(ScriptContext* ctx, char* str, size_t max_length) {
  535. bool quot = false;
  536. size_t pos = 0;
  537. str[pos] = '\000';
  538. while(true) {
  539. char ch = 0;
  540. uint64_t start_pos = storage_file_tell(ctx->script_file);
  541. uint16_t ret = storage_file_read(ctx->script_file, &ch, 1);
  542. if(ret != 1) {
  543. return false;
  544. }
  545. if(ch == '"') {
  546. quot = !quot;
  547. continue;
  548. }
  549. if(!quot) {
  550. if(ch == ' ') {
  551. return true;
  552. }
  553. if(ch == '\r' || ch == '\n') {
  554. storage_file_seek(ctx->script_file, start_pos, true);
  555. return true;
  556. }
  557. }
  558. if(pos + 2 > max_length) {
  559. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_script_get_string: too long");
  560. return false;
  561. }
  562. str[pos++] = ch;
  563. str[pos] = '\000';
  564. }
  565. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_script_get_string: got '%s'", str);
  566. return true;
  567. }
  568. static bool swd_script_get_number(ScriptContext* ctx, uint32_t* number) {
  569. char str[16];
  570. if(!swd_script_get_string(ctx, str, sizeof(str))) {
  571. furi_log_print_format(
  572. FuriLogLevelDefault, TAG, "swd_script_get_number: could not get string");
  573. return false;
  574. }
  575. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_script_get_number: got '%s'", str);
  576. size_t pos = 0;
  577. *number = 0;
  578. /* hex number? */
  579. if(!strncmp(str, "0x", 2)) {
  580. pos += 2;
  581. while(str[pos]) {
  582. uint8_t ch = str[pos++];
  583. uint8_t ch_num = ch - '0';
  584. uint8_t ch_hex = (ch & ~0x20) - 'A';
  585. *number <<= 4;
  586. if(ch_num <= 10) {
  587. *number += ch_num;
  588. } else if(ch_hex <= 5) {
  589. *number += 10 + ch_hex;
  590. } else {
  591. return false;
  592. }
  593. }
  594. } else {
  595. while(str[pos]) {
  596. uint8_t ch = str[pos++];
  597. uint8_t ch_num = ch - '0';
  598. *number *= 10;
  599. if(ch_num < 10) {
  600. *number += ch_num;
  601. } else {
  602. return false;
  603. }
  604. }
  605. }
  606. return true;
  607. }
  608. static void swd_script_gui_refresh(ScriptContext* ctx) {
  609. //Canvas* canvas = gui_direct_draw_acquire(ctx->app->gui);
  610. furi_log_print_format(FuriLogLevelDefault, TAG, "Status: %s", ctx->app->state_string);
  611. //render_callback(canvas, &ctx->app->state_mutex);
  612. if(furi_message_queue_get_count(ctx->app->event_queue) > 0) {
  613. swd_message_process(ctx->app);
  614. }
  615. view_port_update(ctx->app->view_port);
  616. //gui_direct_draw_release(ctx->app->gui);
  617. }
  618. /************************** script functions **************************/
  619. static bool swd_scriptfunc_comment(ScriptContext* ctx) {
  620. furi_log_print_format(FuriLogLevelDefault, TAG, "comment");
  621. swd_script_seek_newline(ctx);
  622. return true;
  623. }
  624. static bool swd_scriptfunc_beep(ScriptContext* ctx) {
  625. uint32_t sound = 0;
  626. furi_log_print_format(FuriLogLevelDefault, TAG, "beep");
  627. swd_script_skip_whitespace(ctx);
  628. swd_script_get_number(ctx, &sound);
  629. notification_message_block(ctx->app->notification, seq_sounds[sound]);
  630. swd_script_seek_newline(ctx);
  631. return true;
  632. }
  633. static bool swd_scriptfunc_message(ScriptContext* ctx) {
  634. uint32_t wait_time = 0;
  635. char message[256];
  636. char type[256];
  637. bool success = true;
  638. bool show_dialog = false;
  639. if(!swd_script_skip_whitespace(ctx)) {
  640. furi_log_print_format(FuriLogLevelDefault, TAG, "message: missing whitespace");
  641. return false;
  642. }
  643. if(!swd_script_get_number(ctx, &wait_time)) {
  644. furi_log_print_format(FuriLogLevelDefault, TAG, "message: failed to parse wait_time");
  645. return false;
  646. }
  647. if(!swd_script_get_string(ctx, message, sizeof(message))) {
  648. furi_log_print_format(FuriLogLevelDefault, TAG, "message: failed to parse message");
  649. return false;
  650. }
  651. if(swd_script_get_string(ctx, type, sizeof(type))) {
  652. if(!strcmp(type, "dialog")) {
  653. show_dialog = true;
  654. }
  655. }
  656. if(wait_time <= 60 * 1000) {
  657. strcpy(ctx->app->state_string, message);
  658. swd_script_gui_refresh(ctx);
  659. furi_delay_ms(wait_time);
  660. if(show_dialog) {
  661. DialogMessage* message = dialog_message_alloc();
  662. dialog_message_set_header(message, "SWD Probe", 16, 2, AlignLeft, AlignTop);
  663. dialog_message_set_icon(message, &I_app, 3, 2);
  664. dialog_message_set_text(message, ctx->app->state_string, 3, 16, AlignLeft, AlignTop);
  665. dialog_message_set_buttons(message, "Abort", "Ok", NULL);
  666. success = dialog_message_show(ctx->app->dialogs, message) == DialogMessageButtonCenter;
  667. dialog_message_free(message);
  668. }
  669. }
  670. swd_script_seek_newline(ctx);
  671. return success;
  672. }
  673. static bool swd_scriptfunc_swd_idle_bits(ScriptContext* ctx) {
  674. uint32_t swd_idle_bits = 0;
  675. if(!swd_script_skip_whitespace(ctx)) {
  676. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_idle_bits: missing whitespace");
  677. return false;
  678. }
  679. if(!swd_script_get_number(ctx, &swd_idle_bits)) {
  680. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_idle_bits: failed to parse");
  681. return false;
  682. }
  683. if(swd_idle_bits <= 32) {
  684. ctx->app->swd_idle_bits = swd_idle_bits;
  685. } else {
  686. furi_log_print_format(
  687. FuriLogLevelDefault, TAG, "swd_idle_bits: value must be between 1 and 32");
  688. }
  689. swd_script_seek_newline(ctx);
  690. return true;
  691. }
  692. static bool swd_scriptfunc_swd_clock_delay(ScriptContext* ctx) {
  693. uint32_t swd_clock_delay = 0;
  694. if(!swd_script_skip_whitespace(ctx)) {
  695. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_clock_delay: missing whitespace");
  696. return false;
  697. }
  698. if(!swd_script_get_number(ctx, &swd_clock_delay)) {
  699. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_clock_delay: failed to parse");
  700. return false;
  701. }
  702. if(swd_clock_delay <= 1000000) {
  703. ctx->app->swd_clock_delay = swd_clock_delay;
  704. } else {
  705. furi_log_print_format(
  706. FuriLogLevelDefault, TAG, "swd_clock_delay: value must be between 1 and 1000000");
  707. }
  708. swd_script_seek_newline(ctx);
  709. return true;
  710. }
  711. static bool swd_scriptfunc_maxtries(ScriptContext* ctx) {
  712. uint32_t max_tries = 0;
  713. if(!swd_script_skip_whitespace(ctx)) {
  714. furi_log_print_format(FuriLogLevelDefault, TAG, "max_tries: missing whitespace");
  715. return false;
  716. }
  717. if(!swd_script_get_number(ctx, &max_tries)) {
  718. furi_log_print_format(FuriLogLevelDefault, TAG, "max_tries: failed to parse");
  719. return false;
  720. }
  721. if(max_tries >= 1 && max_tries <= 1024) {
  722. ctx->max_tries = max_tries;
  723. } else {
  724. furi_log_print_format(
  725. FuriLogLevelDefault, TAG, "max_tries: value must be between 1 and 1024");
  726. }
  727. swd_script_seek_newline(ctx);
  728. return true;
  729. }
  730. static bool swd_scriptfunc_blocksize(ScriptContext* ctx) {
  731. uint32_t block_size = 0;
  732. if(!swd_script_skip_whitespace(ctx)) {
  733. furi_log_print_format(FuriLogLevelDefault, TAG, "block_size: missing whitespace");
  734. return false;
  735. }
  736. if(!swd_script_get_number(ctx, &block_size)) {
  737. furi_log_print_format(FuriLogLevelDefault, TAG, "block_size: failed to parse");
  738. return false;
  739. }
  740. if(block_size >= 4 && block_size <= 0x1000) {
  741. ctx->block_size = block_size;
  742. } else {
  743. furi_log_print_format(
  744. FuriLogLevelDefault, TAG, "block_size: value must be between 4 and 4096");
  745. }
  746. swd_script_seek_newline(ctx);
  747. return true;
  748. }
  749. static bool swd_scriptfunc_apselect(ScriptContext* ctx) {
  750. uint32_t ap = 0;
  751. if(!swd_script_skip_whitespace(ctx)) {
  752. furi_log_print_format(FuriLogLevelDefault, TAG, "apselect: missing whitespace");
  753. return false;
  754. }
  755. if(!swd_script_get_number(ctx, &ap)) {
  756. furi_log_print_format(FuriLogLevelDefault, TAG, "apselect: failed to parse");
  757. return false;
  758. }
  759. if(!swd_apscan_test(ctx->app, ap)) {
  760. furi_log_print_format(FuriLogLevelDefault, TAG, "apselect: no selected AP");
  761. return false;
  762. }
  763. ctx->selected_ap = ap;
  764. swd_script_seek_newline(ctx);
  765. return true;
  766. }
  767. static bool swd_scriptfunc_apscan(ScriptContext* ctx) {
  768. furi_log_print_format(FuriLogLevelDefault, TAG, "apscan: Scanning APs");
  769. for(uint32_t ap = 0; ap < 255; ap++) {
  770. snprintf(ctx->app->state_string, sizeof(ctx->app->state_string), "Scan AP %lu", ap);
  771. swd_script_gui_refresh(ctx);
  772. if(swd_apscan_test(ctx->app, ap)) {
  773. furi_log_print_format(FuriLogLevelDefault, TAG, "apscan: AP%lu detected", ap);
  774. }
  775. }
  776. swd_script_seek_newline(ctx);
  777. return true;
  778. }
  779. static bool swd_scriptfunc_abort(ScriptContext* ctx) {
  780. furi_log_print_format(FuriLogLevelDefault, TAG, "abort: Aborting");
  781. swd_abort(ctx->app);
  782. swd_script_seek_newline(ctx);
  783. return true;
  784. }
  785. static bool swd_scriptfunc_mem_dump(ScriptContext* ctx) {
  786. char filename[MAX_FILE_LENGTH];
  787. uint32_t address = 0;
  788. uint32_t length = 0;
  789. uint32_t flags = 0;
  790. bool success = true;
  791. /* get file */
  792. if(!swd_script_skip_whitespace(ctx)) {
  793. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_dump: missing whitespace");
  794. return false;
  795. }
  796. if(!swd_script_get_string(ctx, filename, sizeof(filename))) {
  797. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_dump: failed to parse filename");
  798. return false;
  799. }
  800. /* get address */
  801. if(!swd_script_get_number(ctx, &address)) {
  802. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_dump: failed to parse address");
  803. return false;
  804. }
  805. /* get length */
  806. if(!swd_script_get_number(ctx, &length)) {
  807. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_dump: failed to parse length");
  808. return false;
  809. }
  810. /* get flags */
  811. if(swd_script_get_number(ctx, &flags)) {
  812. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_dump: found extra flags");
  813. }
  814. furi_log_print_format(
  815. FuriLogLevelDefault,
  816. TAG,
  817. "mem_dump: would dump %08lX, len %08lX into %s",
  818. address,
  819. length,
  820. filename);
  821. File* dump = storage_file_alloc(ctx->app->storage);
  822. if(!storage_file_open(dump, filename, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  823. storage_file_free(dump);
  824. snprintf(ctx->app->state_string, sizeof(ctx->app->state_string), "Failed to create file");
  825. swd_script_gui_refresh(ctx);
  826. notification_message_block(ctx->app->notification, &seq_error);
  827. return false;
  828. }
  829. if(ctx->block_size == 0) {
  830. ctx->block_size = 0x100;
  831. }
  832. if(ctx->block_size > 0x1000) {
  833. ctx->block_size = 0x1000;
  834. }
  835. uint8_t* buffer = malloc(ctx->block_size);
  836. for(uint32_t pos = 0; pos < length; pos += ctx->block_size) {
  837. if((pos & 0xFF) == 0) {
  838. int pct = pos * 100 / length;
  839. snprintf(
  840. ctx->app->state_string,
  841. sizeof(ctx->app->state_string),
  842. "Dump %08lX (%d%%)",
  843. pos,
  844. pct);
  845. swd_script_gui_refresh(ctx);
  846. }
  847. bool read_ok = false;
  848. uint32_t data = 0;
  849. for(uint32_t tries = 0; tries < ctx->max_tries; tries++) {
  850. if(ctx->abort) {
  851. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_dump: aborting read");
  852. break;
  853. }
  854. uint32_t ret = 0;
  855. if(ctx->block_size > 4) {
  856. ret = swd_read_memory_block(
  857. ctx->app, ctx->selected_ap, address + pos, buffer, ctx->block_size);
  858. } else {
  859. ret = swd_read_memory(
  860. ctx->app, ctx->selected_ap, address + pos, (uint8_t*)(void*)buffer);
  861. }
  862. read_ok = (ret == 1);
  863. if(!read_ok) {
  864. snprintf(
  865. ctx->app->state_string,
  866. sizeof(ctx->app->state_string),
  867. "Failed at 0x%08lX",
  868. address + pos);
  869. swd_script_gui_refresh(ctx);
  870. furi_delay_ms(100);
  871. } else {
  872. break;
  873. }
  874. }
  875. if(ctx->abort) {
  876. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_dump: aborting");
  877. break;
  878. }
  879. if(!read_ok) {
  880. /* flags == 1: "continue reading even if it fails" */
  881. /* flags == 2: "its okay if cannot dump fully" */
  882. if(flags & 1) {
  883. /* set all content to a known value as indication */
  884. for(size_t fill_pos = 0; fill_pos < ctx->block_size; fill_pos += 4) {
  885. *((uint32_t*)&buffer[fill_pos]) = 0xDEADFACE;
  886. }
  887. } else if(flags & 2) {
  888. success = (pos > 0);
  889. break;
  890. } else {
  891. notification_message_block(ctx->app->notification, &seq_error);
  892. success = false;
  893. break;
  894. }
  895. }
  896. storage_file_write(dump, buffer, ctx->block_size);
  897. }
  898. storage_file_close(dump);
  899. swd_script_seek_newline(ctx);
  900. free(buffer);
  901. return success;
  902. }
  903. static bool swd_scriptfunc_mem_write(ScriptContext* ctx) {
  904. uint32_t address = 0;
  905. uint32_t data = 0;
  906. bool success = true;
  907. /* get file */
  908. if(!swd_script_skip_whitespace(ctx)) {
  909. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_write: missing whitespace");
  910. return false;
  911. }
  912. /* get address */
  913. if(!swd_script_get_number(ctx, &address)) {
  914. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_write: failed to parse 1");
  915. return false;
  916. }
  917. /* get data */
  918. if(!swd_script_get_number(ctx, &data)) {
  919. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_write: failed to parse 2");
  920. return false;
  921. }
  922. furi_log_print_format(
  923. FuriLogLevelDefault, TAG, "mem_write: write %08lX to %08lX", data, address);
  924. bool access_ok = false;
  925. for(uint32_t tries = 0; tries < ctx->max_tries; tries++) {
  926. if(ctx->abort) {
  927. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_write: aborting");
  928. break;
  929. }
  930. access_ok = swd_write_memory(ctx->app, ctx->selected_ap, address, data) == 1;
  931. if(!access_ok) {
  932. snprintf(
  933. ctx->app->state_string,
  934. sizeof(ctx->app->state_string),
  935. "Failed write 0x%08lX",
  936. address);
  937. swd_script_gui_refresh(ctx);
  938. }
  939. }
  940. if(!access_ok) {
  941. notification_message_block(ctx->app->notification, &seq_error);
  942. success = false;
  943. }
  944. swd_script_seek_newline(ctx);
  945. return success;
  946. }
  947. static bool swd_scriptfunc_mem_ldmst(ScriptContext* ctx) {
  948. uint32_t address = 0;
  949. uint32_t data = 0;
  950. uint32_t mask = 0;
  951. bool success = true;
  952. /* get file */
  953. if(!swd_script_skip_whitespace(ctx)) {
  954. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_ldmst: missing whitespace");
  955. return false;
  956. }
  957. /* get address */
  958. if(!swd_script_get_number(ctx, &address)) {
  959. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_ldmst: failed to parse 1");
  960. return false;
  961. }
  962. /* get data */
  963. if(!swd_script_get_number(ctx, &data)) {
  964. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_ldmst: failed to parse 2");
  965. return false;
  966. }
  967. /* get mask */
  968. if(!swd_script_get_number(ctx, &mask)) {
  969. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_ldmst: failed to parse 2");
  970. return false;
  971. }
  972. furi_log_print_format(
  973. FuriLogLevelDefault,
  974. TAG,
  975. "mem_ldmst: write %08lX to %08lX, mask %08lX",
  976. data,
  977. address,
  978. mask);
  979. bool access_ok = false;
  980. uint32_t modified = 0;
  981. for(uint32_t tries = 0; tries < ctx->max_tries; tries++) {
  982. if(ctx->abort) {
  983. furi_log_print_format(FuriLogLevelDefault, TAG, "mem_ldmst: aborting");
  984. break;
  985. }
  986. access_ok = swd_read_memory(ctx->app, ctx->selected_ap, address, &modified) == 1;
  987. modified = (modified & mask) | data;
  988. access_ok &= swd_write_memory(ctx->app, ctx->selected_ap, address, modified) == 1;
  989. if(!access_ok) {
  990. snprintf(
  991. ctx->app->state_string,
  992. sizeof(ctx->app->state_string),
  993. "Failed access 0x%08lX",
  994. address);
  995. swd_script_gui_refresh(ctx);
  996. }
  997. }
  998. if(!access_ok) {
  999. notification_message_block(ctx->app->notification, &seq_error);
  1000. success = false;
  1001. }
  1002. swd_script_seek_newline(ctx);
  1003. return success;
  1004. }
  1005. static const ScriptFunctionInfo script_funcs[] = {
  1006. {"#", &swd_scriptfunc_comment},
  1007. {"message", &swd_scriptfunc_message},
  1008. {"beep", &swd_scriptfunc_beep},
  1009. {"apscan", &swd_scriptfunc_apscan},
  1010. {"apselect", &swd_scriptfunc_apselect},
  1011. {"max_tries", &swd_scriptfunc_maxtries},
  1012. {"swd_clock_delay", &swd_scriptfunc_swd_clock_delay},
  1013. {"swd_idle_bits", &swd_scriptfunc_swd_idle_bits},
  1014. {"block_size", &swd_scriptfunc_blocksize},
  1015. {"abort", &swd_scriptfunc_abort},
  1016. {"mem_dump", &swd_scriptfunc_mem_dump},
  1017. {"mem_ldmst", &swd_scriptfunc_mem_ldmst},
  1018. {"mem_write", &swd_scriptfunc_mem_write}};
  1019. /************************** script main code **************************/
  1020. static bool swd_execute_script_line(ScriptContext* const ctx, File* file) {
  1021. char buffer[64];
  1022. uint64_t start_pos = storage_file_tell(ctx->script_file);
  1023. uint16_t ret = storage_file_read(ctx->script_file, buffer, 2);
  1024. storage_file_seek(ctx->script_file, start_pos, true);
  1025. if(ret < 2) {
  1026. return true;
  1027. }
  1028. if(buffer[0] == '\n' || (buffer[0] == '\r' && buffer[1] == '\n')) {
  1029. return true;
  1030. }
  1031. for(size_t entry = 0; entry < COUNT(script_funcs); entry++) {
  1032. if(ctx->abort) {
  1033. furi_log_print_format(FuriLogLevelDefault, TAG, "swd_execute_script_line: aborting");
  1034. break;
  1035. }
  1036. char buffer[64];
  1037. storage_file_seek(ctx->script_file, start_pos, true);
  1038. size_t expected = strlen(script_funcs[entry].prefix);
  1039. uint16_t ret = storage_file_read(ctx->script_file, buffer, expected);
  1040. if(ret != expected) {
  1041. continue;
  1042. }
  1043. buffer[expected] = '\000';
  1044. if(strncmp(buffer, script_funcs[entry].prefix, expected)) {
  1045. continue;
  1046. }
  1047. furi_log_print_format(
  1048. FuriLogLevelDefault, TAG, "command: '%s'", script_funcs[entry].prefix);
  1049. snprintf(
  1050. ctx->app->state_string,
  1051. sizeof(ctx->app->state_string),
  1052. "CMD: %s",
  1053. script_funcs[entry].prefix);
  1054. swd_script_gui_refresh(ctx);
  1055. /* func function, execute */
  1056. bool success = script_funcs[entry].func(ctx);
  1057. if(!success) {
  1058. furi_log_print_format(
  1059. FuriLogLevelDefault, TAG, "Command failed: %s", script_funcs[entry].prefix);
  1060. snprintf(
  1061. ctx->app->state_string,
  1062. sizeof(ctx->app->state_string),
  1063. "Command failed: %s",
  1064. script_funcs[entry].prefix);
  1065. return false;
  1066. }
  1067. return true;
  1068. }
  1069. furi_log_print_format(FuriLogLevelDefault, TAG, "unknown command");
  1070. return false;
  1071. }
  1072. static bool swd_execute_script(AppFSM* const ctx, const char* filename) {
  1073. bool success = true;
  1074. bool restart = false;
  1075. do {
  1076. success = true;
  1077. restart = false;
  1078. ctx->script = malloc(sizeof(ScriptContext));
  1079. ctx->script->app = ctx;
  1080. ctx->script->max_tries = 1;
  1081. if(!storage_file_exists(ctx->storage, filename)) {
  1082. furi_log_print_format(FuriLogLevelDefault, TAG, "Does not exist '%s'", filename);
  1083. free(ctx->script);
  1084. ctx->script = NULL;
  1085. return false;
  1086. }
  1087. /* first allocate a file object */
  1088. ctx->script->script_file = storage_file_alloc(ctx->storage);
  1089. /* then get our script opened */
  1090. if(!storage_file_open(ctx->script->script_file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) {
  1091. FURI_LOG_E(TAG, "open, %s", storage_file_get_error_desc(ctx->script->script_file));
  1092. furi_log_print_format(FuriLogLevelDefault, TAG, "Failed to open '%s'", filename);
  1093. storage_file_free(ctx->script->script_file);
  1094. free(ctx->script);
  1095. ctx->script = NULL;
  1096. return false;
  1097. }
  1098. uint32_t line = 1;
  1099. while(line < SCRIPT_MAX_LINES) {
  1100. if(ctx->script->abort) {
  1101. furi_log_print_format(FuriLogLevelDefault, TAG, "Abort requested");
  1102. break;
  1103. }
  1104. if(storage_file_eof(ctx->script->script_file)) {
  1105. break;
  1106. }
  1107. furi_log_print_format(FuriLogLevelDefault, TAG, "line %lu", line);
  1108. if(!swd_execute_script_line(ctx->script, ctx->script->script_file)) {
  1109. success = false;
  1110. break;
  1111. }
  1112. line++;
  1113. }
  1114. furi_log_print_format(FuriLogLevelDefault, TAG, "Finished");
  1115. storage_file_close(ctx->script->script_file);
  1116. storage_file_free(ctx->script->script_file);
  1117. free(ctx->script);
  1118. ctx->script = NULL;
  1119. if(!success) {
  1120. char text_buf[128];
  1121. snprintf(text_buf, sizeof(text_buf), "Line %lu failed:\n%s", line, ctx->state_string);
  1122. DialogMessage* message = dialog_message_alloc();
  1123. dialog_message_set_header(message, "SWD Probe", 16, 2, AlignLeft, AlignTop);
  1124. dialog_message_set_icon(message, &I_app, 3, 2);
  1125. dialog_message_set_text(message, text_buf, 3, 16, AlignLeft, AlignTop);
  1126. dialog_message_set_buttons(message, "Back", "Retry", NULL);
  1127. if(dialog_message_show(ctx->dialogs, message) == DialogMessageButtonCenter) {
  1128. restart = true;
  1129. }
  1130. dialog_message_free(message);
  1131. }
  1132. } while(restart);
  1133. return success;
  1134. }
  1135. /************************** UI functions **************************/
  1136. static void render_callback(Canvas* const canvas, void* cb_ctx) {
  1137. AppFSM* ctx = acquire_mutex((ValueMutex*)cb_ctx, 25);
  1138. if(ctx == NULL) {
  1139. return;
  1140. }
  1141. char buffer[64];
  1142. int y = 10;
  1143. canvas_draw_frame(canvas, 0, 0, 128, 64);
  1144. canvas_set_font(canvas, FontPrimary);
  1145. if(ctx->detected_device) {
  1146. /* if seen less than a quarter second ago */
  1147. switch(ctx->mode_page) {
  1148. case ModePageScan: {
  1149. if((ctx->detected_timeout + TIMER_HZ / 4) >= TIMER_HZ * TIMEOUT) {
  1150. snprintf(buffer, sizeof(buffer), "FOUND!");
  1151. } else {
  1152. /* if it was seen more than a quarter second ago, show countdown */
  1153. snprintf(
  1154. buffer,
  1155. sizeof(buffer),
  1156. "FOUND! (%lus)",
  1157. (ctx->detected_timeout / TIMER_HZ) + 1);
  1158. }
  1159. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, buffer);
  1160. y += 10;
  1161. canvas_set_font(canvas, FontKeyboard);
  1162. snprintf(
  1163. buffer,
  1164. sizeof(buffer),
  1165. "SWC/SWD: %s/%s",
  1166. gpio_name(ctx->io_swc),
  1167. gpio_name(ctx->io_swd));
  1168. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1169. y += 10;
  1170. snprintf(buffer, sizeof(buffer), "DPIDR 0x%08lX", ctx->dp_regs.dpidr);
  1171. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1172. y += 10;
  1173. snprintf(
  1174. buffer,
  1175. sizeof(buffer),
  1176. "Part %02X Rev %X DAPv%d",
  1177. ctx->dpidr_info.partno,
  1178. ctx->dpidr_info.revision,
  1179. ctx->dpidr_info.version);
  1180. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1181. y += 10;
  1182. canvas_set_font(canvas, FontSecondary);
  1183. snprintf(buffer, sizeof(buffer), "%s", jep106_manufacturer(ctx->dpidr_info.designer));
  1184. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1185. y += 10;
  1186. canvas_set_font(canvas, FontSecondary);
  1187. elements_button_left(canvas, "Script");
  1188. elements_button_right(canvas, "DP Regs");
  1189. break;
  1190. }
  1191. case ModePageDPRegs: {
  1192. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "DP Registers");
  1193. y += 10;
  1194. canvas_set_font(canvas, FontKeyboard);
  1195. if(ctx->dp_regs.dpidr_ok) {
  1196. snprintf(buffer, sizeof(buffer), "DPIDR %08lX", ctx->dp_regs.dpidr);
  1197. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1198. }
  1199. y += 10;
  1200. if(ctx->dp_regs.ctrlstat_ok) {
  1201. snprintf(buffer, sizeof(buffer), "CTRL %08lX", ctx->dp_regs.ctrlstat);
  1202. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1203. }
  1204. y += 10;
  1205. if(ctx->dp_regs.targetid_ok) {
  1206. snprintf(buffer, sizeof(buffer), "TGTID %08lX", ctx->dp_regs.targetid);
  1207. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1208. }
  1209. y += 10;
  1210. if(ctx->dp_regs.eventstat_ok) {
  1211. snprintf(buffer, sizeof(buffer), "EVTST %08lX", ctx->dp_regs.eventstat);
  1212. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1213. }
  1214. y += 10;
  1215. canvas_set_font(canvas, FontSecondary);
  1216. elements_button_left(canvas, "Scan");
  1217. elements_button_right(canvas, "DPID");
  1218. break;
  1219. }
  1220. case ModePageDPID: {
  1221. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "DP ID Register");
  1222. y += 10;
  1223. canvas_set_font(canvas, FontKeyboard);
  1224. if(ctx->dpidr_info.version != 2) {
  1225. snprintf(buffer, sizeof(buffer), "TARGETID not supported");
  1226. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1227. y += 10;
  1228. } else {
  1229. if(ctx->dp_regs.targetid_ok) {
  1230. snprintf(buffer, sizeof(buffer), "TGTID %08lX", ctx->dp_regs.targetid);
  1231. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1232. y += 10;
  1233. snprintf(buffer, sizeof(buffer), "Part No. %04X", ctx->targetid_info.partno);
  1234. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1235. y += 10;
  1236. snprintf(
  1237. buffer,
  1238. sizeof(buffer),
  1239. "%s",
  1240. jep106_manufacturer(ctx->targetid_info.designer));
  1241. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1242. y += 10;
  1243. }
  1244. }
  1245. canvas_set_font(canvas, FontSecondary);
  1246. elements_button_left(canvas, "DP Regs");
  1247. elements_button_right(canvas, "APs");
  1248. break;
  1249. }
  1250. case ModePageAPID: {
  1251. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "AP Menu");
  1252. y += 10;
  1253. canvas_set_font(canvas, FontKeyboard);
  1254. char state = ' ';
  1255. if(ctx->ap_pos >= ctx->ap_scanned && ctx->ap_pos <= ctx->ap_scanned + 10) {
  1256. state = '*';
  1257. }
  1258. if(!ctx->apidr_info[ctx->ap_pos].ok) {
  1259. snprintf(buffer, sizeof(buffer), "[%d]%c<none>", ctx->ap_pos, state);
  1260. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1261. y += 10;
  1262. if(ctx->ap_pos == 0) {
  1263. for(int pos = 0; pos < COUNT(ctx->apidr_info); pos++) {
  1264. if(ctx->apidr_info[pos].ok) {
  1265. ctx->ap_pos = pos;
  1266. }
  1267. }
  1268. }
  1269. } else {
  1270. const char* class = "";
  1271. switch(ctx->apidr_info[ctx->ap_pos].class) {
  1272. case 0:
  1273. class = "und";
  1274. break;
  1275. case 1:
  1276. class = "COM";
  1277. break;
  1278. case 8:
  1279. class = "MEM";
  1280. break;
  1281. default:
  1282. class = "unk";
  1283. break;
  1284. }
  1285. const char* types[] = {
  1286. "COM-AP",
  1287. "AHB3",
  1288. "APB2 or APB3",
  1289. "Type unknown",
  1290. "AXI3 or AXI4",
  1291. "AHB5",
  1292. "APB4 and APB5",
  1293. "AXI5",
  1294. "AHB5 enh.",
  1295. };
  1296. const char* type = "Type unk";
  1297. if(ctx->apidr_info[ctx->ap_pos].type < COUNT(types)) {
  1298. type = types[ctx->apidr_info[ctx->ap_pos].type];
  1299. }
  1300. snprintf(buffer, sizeof(buffer), "[%d]%c%s, %s", ctx->ap_pos, state, class, type);
  1301. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1302. y += 10;
  1303. snprintf(
  1304. buffer, sizeof(buffer), "Base 0x%08lX", ctx->apidr_info[ctx->ap_pos].base);
  1305. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1306. y += 10;
  1307. snprintf(
  1308. buffer,
  1309. sizeof(buffer),
  1310. "Rev %d Var %d",
  1311. ctx->apidr_info[ctx->ap_pos].revision,
  1312. ctx->apidr_info[ctx->ap_pos].variant);
  1313. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1314. y += 10;
  1315. snprintf(
  1316. buffer,
  1317. sizeof(buffer),
  1318. "%s",
  1319. jep106_manufacturer(ctx->apidr_info[ctx->ap_pos].designer));
  1320. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1321. y += 10;
  1322. elements_button_center(canvas, "Show");
  1323. }
  1324. canvas_set_font(canvas, FontSecondary);
  1325. elements_button_left(canvas, "DPID");
  1326. elements_scrollbar_pos(
  1327. canvas, 4, 10, 40, ctx->ap_pos / 32, COUNT(ctx->apidr_info) / 32);
  1328. } break;
  1329. /* hex dump view */
  1330. case ModePageHexDump: {
  1331. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Hex dump");
  1332. y += 10;
  1333. canvas_set_font(canvas, FontKeyboard);
  1334. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, "Addr:");
  1335. snprintf(buffer, sizeof(buffer), "%08lX", ctx->hex_addr);
  1336. canvas_draw_str_aligned(canvas, 38, y, AlignLeft, AlignBottom, buffer);
  1337. uint32_t font_width = canvas_glyph_width(canvas, '0');
  1338. uint32_t x = 37 + (7 - ctx->hex_select) * font_width;
  1339. /* draw selection */
  1340. canvas_draw_line(canvas, x, y + 1, x + font_width, y + 1);
  1341. y += 10;
  1342. uint32_t byte_num = 0;
  1343. for(int line = 0; line < 4; line++) {
  1344. uint32_t x_pos = 5;
  1345. for(int byte_pos = 0; byte_pos < 8; byte_pos++) {
  1346. if(ctx->hex_buffer_valid[byte_num / 4]) {
  1347. snprintf(buffer, sizeof(buffer), "%02X", ctx->hex_buffer[byte_num]);
  1348. } else {
  1349. snprintf(buffer, sizeof(buffer), "--");
  1350. }
  1351. byte_num++;
  1352. canvas_draw_str_aligned(canvas, x_pos, y, AlignLeft, AlignBottom, buffer);
  1353. x_pos += font_width * 2 + font_width / 2;
  1354. }
  1355. y += 10;
  1356. }
  1357. break;
  1358. }
  1359. /* hex dump view */
  1360. case ModePageScript: {
  1361. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Script");
  1362. y += 10;
  1363. y += 10;
  1364. canvas_draw_str_aligned(canvas, 10, y, AlignLeft, AlignBottom, "Status:");
  1365. y += 10;
  1366. canvas_set_font(canvas, FontKeyboard);
  1367. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, ctx->state_string);
  1368. y += 10;
  1369. } break;
  1370. }
  1371. } else {
  1372. snprintf(
  1373. buffer, sizeof(buffer), "Searching... %c", gpio_direction_ind[ctx->current_mask_id]);
  1374. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, buffer);
  1375. y += 10;
  1376. y += 10;
  1377. canvas_draw_str_aligned(canvas, 10, y, AlignLeft, AlignBottom, "Script:");
  1378. y += 10;
  1379. if(strlen(ctx->script_detected) > 0) {
  1380. canvas_set_font(canvas, FontKeyboard);
  1381. const char* filename = strrchr(ctx->script_detected, '/');
  1382. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, &filename[1]);
  1383. y += 10;
  1384. canvas_set_font(canvas, FontSecondary);
  1385. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Up to clear");
  1386. y += 10;
  1387. } else {
  1388. canvas_set_font(canvas, FontKeyboard);
  1389. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "<none>");
  1390. y += 10;
  1391. canvas_set_font(canvas, FontSecondary);
  1392. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Down to select");
  1393. y += 10;
  1394. }
  1395. elements_button_left(canvas, "Script");
  1396. }
  1397. release_mutex((ValueMutex*)cb_ctx, ctx);
  1398. }
  1399. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  1400. furi_assert(event_queue);
  1401. /* better skip than sorry */
  1402. if(furi_message_queue_get_count(event_queue) < QUEUE_SIZE) {
  1403. AppEvent event = {.type = EventKeyPress, .input = *input_event};
  1404. furi_message_queue_put(event_queue, &event, 100);
  1405. }
  1406. }
  1407. static void timer_tick_callback(FuriMessageQueue* event_queue) {
  1408. furi_assert(event_queue);
  1409. /* filling buffer makes no sense, as we lost timing anyway */
  1410. if(furi_message_queue_get_count(event_queue) < 1) {
  1411. AppEvent event = {.type = EventTimerTick};
  1412. furi_message_queue_put(event_queue, &event, 100);
  1413. }
  1414. }
  1415. static void app_init(AppFSM* const app) {
  1416. app->current_mask_id = 0;
  1417. app->current_mask = gpio_direction_mask[app->current_mask_id];
  1418. app->io_swd = 0xFF;
  1419. app->io_swc = 0xFF;
  1420. app->hex_addr = 0x40002800;
  1421. app->swd_clock_delay = CLOCK_DELAY;
  1422. app->swd_idle_bits = IDLE_BITS;
  1423. strcpy(app->state_string, "none");
  1424. strcpy(app->script_detected, "");
  1425. }
  1426. static void app_deinit(AppFSM* const ctx) {
  1427. furi_timer_free(ctx->timer);
  1428. }
  1429. static void on_timer_tick(AppFSM* ctx) {
  1430. switch(ctx->mode_page) {
  1431. case ModePageScan: {
  1432. /* reset after timeout */
  1433. if(ctx->detected_timeout == 0) {
  1434. ctx->detected_device = false;
  1435. ctx->io_swd = 0xFF;
  1436. ctx->io_swc = 0xFF;
  1437. ctx->io_num_swd = 0xFF;
  1438. ctx->io_num_swc = 0xFF;
  1439. ctx->ap_scanned = 0;
  1440. memset(&ctx->dp_regs, 0x00, sizeof(ctx->dp_regs));
  1441. memset(&ctx->targetid_info, 0x00, sizeof(ctx->targetid_info));
  1442. memset(&ctx->apidr_info, 0x00, sizeof(ctx->apidr_info));
  1443. ctx->script_detected_executed = false;
  1444. } else {
  1445. ctx->detected_timeout--;
  1446. }
  1447. ctx->detected = false;
  1448. ctx->current_mask = gpio_direction_mask[ctx->current_mask_id];
  1449. /* when SWD was already detected, set it to data pin regardless of the mask */
  1450. if(ctx->detected_device) {
  1451. ctx->current_mask &= ~ctx->io_swd;
  1452. }
  1453. /* do the scan */
  1454. swd_scan(ctx);
  1455. /* now when detected a device, set the timeout */
  1456. if(ctx->detected) {
  1457. ctx->detected_device = true;
  1458. ctx->detected_timeout = TIMER_HZ * TIMEOUT;
  1459. /* update DPIDR fields */
  1460. ctx->dpidr_info.revision = (ctx->dp_regs.dpidr >> 28) & 0x0F;
  1461. ctx->dpidr_info.partno = (ctx->dp_regs.dpidr >> 20) & 0xFF;
  1462. ctx->dpidr_info.version = (ctx->dp_regs.dpidr >> 12) & 0x0F;
  1463. ctx->dpidr_info.designer = (ctx->dp_regs.dpidr >> 1) & 0x3FF;
  1464. if(!has_multiple_bits(ctx->io_swc)) {
  1465. furi_log_print_format(FuriLogLevelDebug, TAG, " - Detected pins");
  1466. furi_log_print_format(FuriLogLevelDebug, TAG, " - Resetting error");
  1467. /* reset error */
  1468. uint8_t ack = swd_transfer(ctx, false, false, 1, &ctx->dp_regs.ctrlstat);
  1469. furi_log_print_format(
  1470. FuriLogLevelDebug, TAG, "stat %02lX %d", ctx->dp_regs.ctrlstat, ack);
  1471. if(ack != 1 || (ctx->dp_regs.ctrlstat & STAT_ERROR_FLAGS)) {
  1472. furi_log_print_format(FuriLogLevelDebug, TAG, "send ABORT");
  1473. swd_abort(ctx);
  1474. }
  1475. furi_log_print_format(FuriLogLevelDebug, TAG, " - Fetch CTRL/STAT");
  1476. ctx->dp_regs.ctrlstat_ok = swd_read_dpbank(ctx, 1, 0, &ctx->dp_regs.ctrlstat) == 1;
  1477. if(ctx->dpidr_info.version >= 1) {
  1478. furi_log_print_format(FuriLogLevelDebug, TAG, " - DAPv1, read DLCR");
  1479. ctx->dp_regs.dlcr_ok = swd_read_dpbank(ctx, 1, 1, &ctx->dp_regs.dlcr) == 1;
  1480. }
  1481. if(ctx->dpidr_info.version >= 2) {
  1482. furi_log_print_format(FuriLogLevelDebug, TAG, " - DAPv2, read TARGETID");
  1483. ctx->dp_regs.targetid_ok =
  1484. swd_read_dpbank(ctx, 1, 2, &ctx->dp_regs.targetid) == 1;
  1485. furi_log_print_format(FuriLogLevelDebug, TAG, " - DAPv2, read EVENTSTAT");
  1486. ctx->dp_regs.eventstat_ok =
  1487. swd_read_dpbank(ctx, 1, 4, &ctx->dp_regs.eventstat) == 1;
  1488. furi_log_print_format(FuriLogLevelDebug, TAG, " - DAPv2, read DLPIDR");
  1489. ctx->dp_regs.dlpidr_ok = swd_read_dpbank(ctx, 1, 3, &ctx->dp_regs.dlpidr) == 1;
  1490. }
  1491. if(ctx->dp_regs.targetid_ok) {
  1492. ctx->targetid_info.revision = (ctx->dp_regs.targetid >> 28) & 0x0F;
  1493. ctx->targetid_info.partno = (ctx->dp_regs.targetid >> 12) & 0xFFFF;
  1494. ctx->targetid_info.designer = (ctx->dp_regs.targetid >> 1) & 0x3FF;
  1495. }
  1496. if(!ctx->script_detected_executed && strlen(ctx->script_detected) > 0) {
  1497. furi_log_print_format(
  1498. FuriLogLevelDefault, TAG, " - Run script '%s'", ctx->script_detected);
  1499. ctx->script_detected_executed = true;
  1500. ctx->mode_page = ModePageScript;
  1501. swd_execute_script(ctx, ctx->script_detected);
  1502. ctx->mode_page = ModePageScan;
  1503. }
  1504. }
  1505. } else {
  1506. if(!has_multiple_bits(ctx->io_swc)) {
  1507. furi_log_print_format(FuriLogLevelDebug, TAG, " - Lost device");
  1508. }
  1509. }
  1510. ctx->current_mask_id = (ctx->current_mask_id + 1) % COUNT(gpio_direction_mask);
  1511. break;
  1512. }
  1513. case ModePageDPRegs:
  1514. case ModePageDPID:
  1515. case ModePageAPID: {
  1516. /* set debug enable request */
  1517. if(!swd_ensure_powerup(ctx)) {
  1518. break;
  1519. }
  1520. /* only scan a few APs at once to stay responsive */
  1521. for(int pos = 0; pos < 5; pos++) {
  1522. if(ctx->ap_scanned == 0) {
  1523. swd_apscan_reset(ctx);
  1524. }
  1525. uint8_t ap = ctx->ap_scanned++;
  1526. if(ctx->apidr_info[ap].tested) {
  1527. continue;
  1528. }
  1529. if(swd_apscan_test(ctx, ap)) {
  1530. break;
  1531. }
  1532. }
  1533. break;
  1534. }
  1535. case ModePageHexDump: {
  1536. if(ctx->hex_read_delay++ < 10) {
  1537. break;
  1538. }
  1539. ctx->hex_read_delay = 0;
  1540. memset(ctx->hex_buffer, 0xEE, sizeof(ctx->hex_buffer));
  1541. uint32_t addr = ctx->hex_addr;
  1542. uint32_t data = 0;
  1543. for(int pos = 0; pos < sizeof(ctx->hex_buffer) / 4; pos++) {
  1544. ctx->hex_buffer_valid[pos] = swd_read_memory(ctx, ctx->ap_pos, addr, &data) == 1;
  1545. if(ctx->hex_buffer_valid[pos]) {
  1546. memcpy(&ctx->hex_buffer[pos * 4], &data, 4);
  1547. } else {
  1548. swd_abort_simple(ctx);
  1549. }
  1550. addr += 4;
  1551. }
  1552. }
  1553. }
  1554. }
  1555. static bool swd_message_process(AppFSM* ctx) {
  1556. bool processing = true;
  1557. AppEvent event;
  1558. FuriStatus event_status = furi_message_queue_get(ctx->event_queue, &event, 100);
  1559. if(event_status == FuriStatusOk) {
  1560. if(event.type == EventKeyPress) {
  1561. if(event.input.type == InputTypePress) {
  1562. switch(event.input.key) {
  1563. case InputKeyUp:
  1564. switch(ctx->mode_page) {
  1565. default:
  1566. break;
  1567. case ModePageScan: {
  1568. strcpy(ctx->script_detected, "");
  1569. break;
  1570. }
  1571. case ModePageAPID:
  1572. if(ctx->ap_pos > 0) {
  1573. ctx->ap_pos--;
  1574. }
  1575. break;
  1576. case ModePageHexDump: {
  1577. ctx->hex_addr +=
  1578. ((ctx->hex_select) ? 1 : 8) * (1 << (4 * ctx->hex_select));
  1579. break;
  1580. }
  1581. }
  1582. break;
  1583. case InputKeyDown:
  1584. switch(ctx->mode_page) {
  1585. default:
  1586. break;
  1587. case ModePageScan: {
  1588. FuriString* result_path = furi_string_alloc_printf(ANY_PATH("swd"));
  1589. FuriString* preselected = furi_string_alloc_printf(
  1590. (strlen(ctx->script_detected) > 0) ? ctx->script_detected :
  1591. ANY_PATH("swd"));
  1592. DialogsFileBrowserOptions options;
  1593. dialog_file_browser_set_basic_options(&options, "swd", &I_swd);
  1594. if(dialog_file_browser_show(
  1595. ctx->dialogs, result_path, preselected, &options)) {
  1596. const char* path = furi_string_get_cstr(result_path);
  1597. strcpy(ctx->script_detected, path);
  1598. }
  1599. furi_string_free(result_path);
  1600. furi_string_free(preselected);
  1601. break;
  1602. }
  1603. case ModePageAPID:
  1604. if(ctx->ap_pos + 1 < COUNT(ctx->apidr_info)) {
  1605. ctx->ap_pos++;
  1606. }
  1607. break;
  1608. case ModePageHexDump: {
  1609. ctx->hex_addr -=
  1610. ((ctx->hex_select) ? 1 : 8) * (1 << (4 * ctx->hex_select));
  1611. break;
  1612. }
  1613. }
  1614. break;
  1615. case InputKeyRight:
  1616. if(ctx->mode_page == ModePageHexDump) {
  1617. if(ctx->hex_select > 0) {
  1618. ctx->hex_select--;
  1619. }
  1620. } else {
  1621. if(ctx->mode_page + 1 < ModePageCount) {
  1622. ctx->mode_page++;
  1623. }
  1624. }
  1625. break;
  1626. case InputKeyLeft:
  1627. if(ctx->mode_page == ModePageHexDump) {
  1628. if(ctx->hex_select < 7) {
  1629. ctx->hex_select++;
  1630. }
  1631. } else if(ctx->mode_page == ModePageScan) {
  1632. FuriString* result_path = furi_string_alloc_printf(ANY_PATH("swd"));
  1633. FuriString* preselected = furi_string_alloc_printf(
  1634. (strlen(ctx->script_detected) > 0) ? ctx->script_detected :
  1635. ANY_PATH("swd"));
  1636. DialogsFileBrowserOptions options;
  1637. dialog_file_browser_set_basic_options(&options, "swd", &I_swd);
  1638. if(dialog_file_browser_show(
  1639. ctx->dialogs, result_path, preselected, &options)) {
  1640. const char* path = furi_string_get_cstr(result_path);
  1641. ctx->mode_page = ModePageScript;
  1642. swd_execute_script(ctx, path);
  1643. ctx->mode_page = ModePageScan;
  1644. }
  1645. furi_string_free(result_path);
  1646. furi_string_free(preselected);
  1647. break;
  1648. } else {
  1649. if(ctx->mode_page > 0) {
  1650. ctx->mode_page--;
  1651. }
  1652. }
  1653. break;
  1654. case InputKeyOk:
  1655. if(ctx->mode_page == ModePageAPID && ctx->apidr_info[ctx->ap_pos].ok) {
  1656. ctx->mode_page = ModePageHexDump;
  1657. }
  1658. break;
  1659. case InputKeyBack:
  1660. if(ctx->mode_page == ModePageHexDump) {
  1661. ctx->mode_page = ModePageAPID;
  1662. } else if(ctx->mode_page == ModePageScript) {
  1663. ctx->script->abort = true;
  1664. } else if(ctx->mode_page != ModePageScan) {
  1665. ctx->mode_page = ModePageScan;
  1666. } else {
  1667. processing = false;
  1668. }
  1669. break;
  1670. default:
  1671. break;
  1672. }
  1673. }
  1674. } else if(event.type == EventTimerTick) {
  1675. on_timer_tick(ctx);
  1676. }
  1677. } else {
  1678. /* timeout */
  1679. }
  1680. return processing;
  1681. }
  1682. int32_t swd_probe_app_main(void* p) {
  1683. UNUSED(p);
  1684. AppFSM* app = malloc(sizeof(AppFSM));
  1685. app_init(app);
  1686. if(!init_mutex(&app->state_mutex, app, sizeof(AppFSM))) {
  1687. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  1688. free(app);
  1689. return 255;
  1690. }
  1691. app->notification = furi_record_open(RECORD_NOTIFICATION);
  1692. app->gui = furi_record_open(RECORD_GUI);
  1693. app->dialogs = furi_record_open(RECORD_DIALOGS);
  1694. app->storage = furi_record_open(RECORD_STORAGE);
  1695. app->view_port = view_port_alloc();
  1696. app->event_queue = furi_message_queue_alloc(QUEUE_SIZE, sizeof(AppEvent));
  1697. app->timer = furi_timer_alloc(timer_tick_callback, FuriTimerTypePeriodic, app->event_queue);
  1698. view_port_draw_callback_set(app->view_port, render_callback, &app->state_mutex);
  1699. view_port_input_callback_set(app->view_port, input_callback, app->event_queue);
  1700. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  1701. notification_message_block(app->notification, &sequence_display_backlight_enforce_on);
  1702. swd_execute_script(app, ANY_PATH("swd/startup.swd"));
  1703. DOLPHIN_DEED(DolphinDeedPluginGameStart);
  1704. furi_timer_start(app->timer, furi_kernel_get_tick_frequency() / TIMER_HZ);
  1705. for(bool processing = true; processing;) {
  1706. //AppFSM* ctx = (AppFSM*)acquire_mutex_block(&app->state_mutex);
  1707. processing = swd_message_process(app);
  1708. view_port_update(app->view_port);
  1709. //release_mutex(&ctx->state_mutex, ctx);
  1710. bool beep = false;
  1711. if(app->detected_device && !app->detected_notified) {
  1712. app->detected_notified = true;
  1713. beep = true;
  1714. }
  1715. if(!app->detected_device && app->detected_notified) {
  1716. app->detected_notified = false;
  1717. }
  1718. if(beep) {
  1719. notification_message_block(app->notification, &seq_c_minor);
  1720. }
  1721. }
  1722. app_deinit(app);
  1723. // Wait for all notifications to be played and return backlight to normal state
  1724. notification_message_block(app->notification, &sequence_display_backlight_enforce_auto);
  1725. view_port_enabled_set(app->view_port, false);
  1726. gui_remove_view_port(app->gui, app->view_port);
  1727. furi_record_close(RECORD_GUI);
  1728. furi_record_close(RECORD_NOTIFICATION);
  1729. view_port_free(app->view_port);
  1730. furi_message_queue_free(app->event_queue);
  1731. delete_mutex(&app->state_mutex);
  1732. free(app);
  1733. return 0;
  1734. }