swd_probe_app.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187
  1. #include "swd_probe_app.h"
  2. #include "swd_probe_icons.h"
  3. #include "jep106.h"
  4. #include "adi.h"
  5. static void render_callback(Canvas* const canvas, void* cb_ctx);
  6. static bool swd_message_process(AppFSM* ctx);
  7. static uint8_t swd_transfer(AppFSM* const ctx, bool ap, bool write, uint8_t a23, uint32_t* data);
  8. static bool swd_execute_script(AppFSM* const ctx, const char* filename);
  9. static const GpioPin* gpios[] = {
  10. &gpio_ext_pc0,
  11. &gpio_ext_pc1,
  12. &gpio_ext_pc3,
  13. &gpio_ext_pb2,
  14. &gpio_ext_pb3,
  15. &gpio_ext_pa4,
  16. &gpio_ext_pa6,
  17. &gpio_ext_pa7};
  18. static const char* gpio_names[] = {"PC0", "PC1", "PC3", "PB2", "PB3", "PA4", "PA6", "PA7"};
  19. /* bit set: clock, else data */
  20. static const uint8_t gpio_direction_mask[6] =
  21. {0b10101010, 0b01010101, 0b11001100, 0b00110011, 0b11110000, 0b00001111};
  22. const NotificationSequence seq_c_minor = {
  23. &message_note_c4,
  24. &message_delay_100,
  25. &message_sound_off,
  26. &message_delay_10,
  27. &message_note_ds4,
  28. &message_delay_100,
  29. &message_sound_off,
  30. &message_delay_10,
  31. &message_note_g4,
  32. &message_delay_100,
  33. &message_sound_off,
  34. &message_delay_10,
  35. &message_vibro_on,
  36. &message_delay_50,
  37. &message_vibro_off,
  38. NULL,
  39. };
  40. const NotificationSequence seq_error = {
  41. &message_vibro_on,
  42. &message_delay_50,
  43. &message_vibro_off,
  44. &message_note_g4,
  45. &message_delay_100,
  46. &message_sound_off,
  47. &message_delay_10,
  48. &message_note_c4,
  49. &message_delay_500,
  50. &message_sound_off,
  51. &message_delay_10,
  52. NULL,
  53. };
  54. const NotificationSequence* seq_sounds[] = {&seq_c_minor, &seq_error};
  55. static bool has_multiple_bits(uint8_t x) {
  56. return (x & (x - 1)) != 0;
  57. }
  58. static uint8_t get_bit_num(uint8_t x) {
  59. return (uint8_t)__builtin_ctz(x);
  60. }
  61. static const char* gpio_name(uint8_t mask) {
  62. if(has_multiple_bits(mask)) {
  63. return "Pxx";
  64. }
  65. uint8_t io = get_bit_num(mask);
  66. if(io >= COUNT(gpio_names)) {
  67. return "Pxx";
  68. }
  69. return gpio_names[io];
  70. }
  71. static void swd_configure_pins(AppFSM* const ctx, bool output) {
  72. if(ctx->mode_page > ModePageFound && ctx->io_num_swc < 8 && ctx->io_num_swd < 8) {
  73. furi_hal_gpio_init(
  74. gpios[ctx->io_num_swc], GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  75. if(!output) {
  76. furi_hal_gpio_init(
  77. gpios[ctx->io_num_swd], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  78. } else {
  79. furi_hal_gpio_init(
  80. gpios[ctx->io_num_swd], GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh);
  81. }
  82. return;
  83. }
  84. for(int io = 0; io < 8; io++) {
  85. uint8_t bitmask = 1 << io;
  86. /* if neither candidate for SWC nor SWD then skip */
  87. if(!(ctx->io_swc & bitmask) && !(ctx->io_swd & bitmask)) {
  88. furi_hal_gpio_init(gpios[io], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  89. continue;
  90. }
  91. if(ctx->current_mask & bitmask) {
  92. /* set for clock */
  93. furi_hal_gpio_init(gpios[io], GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  94. } else {
  95. /* set for data */
  96. if(!output) {
  97. furi_hal_gpio_init(gpios[io], GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  98. } else {
  99. furi_hal_gpio_init(
  100. gpios[io], GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh);
  101. }
  102. }
  103. }
  104. }
  105. static void swd_set_clock(AppFSM* const ctx, const uint8_t level) {
  106. if(ctx->mode_page > ModePageFound && ctx->io_num_swc < 8) {
  107. furi_hal_gpio_write(gpios[ctx->io_num_swc], level);
  108. return;
  109. }
  110. for(int io = 0; io < 8; io++) {
  111. uint8_t bitmask = 1 << io;
  112. /* if no candidate for SWC then skip */
  113. if(!(ctx->io_swc & bitmask)) {
  114. continue;
  115. }
  116. if(ctx->current_mask & bitmask) {
  117. furi_hal_gpio_write(gpios[io], level);
  118. }
  119. }
  120. }
  121. static void swd_set_data(AppFSM* const ctx, const uint8_t level) {
  122. if(ctx->mode_page > ModePageFound && ctx->io_num_swd < 8) {
  123. furi_hal_gpio_write(gpios[ctx->io_num_swd], level);
  124. return;
  125. }
  126. for(int io = 0; io < 8; io++) {
  127. uint8_t bitmask = 1 << io;
  128. /* if no candidate for SWD then skip */
  129. if(!(ctx->io_swd & bitmask)) {
  130. continue;
  131. }
  132. if(!(ctx->current_mask & bitmask)) {
  133. furi_hal_gpio_write(gpios[io], level);
  134. }
  135. }
  136. }
  137. static uint8_t swd_get_data(AppFSM* const ctx) {
  138. if(ctx->mode_page > ModePageFound && ctx->io_num_swd < 8) {
  139. return furi_hal_gpio_read(gpios[ctx->io_num_swd]);
  140. }
  141. uint8_t bits = 0;
  142. for(int io = 0; io < 8; io++) {
  143. uint8_t bitmask = 1 << io;
  144. /* if no candidate for SWD then skip */
  145. if(!(ctx->io_swd & bitmask)) {
  146. continue;
  147. }
  148. bits |= furi_hal_gpio_read(gpios[io]) ? bitmask : 0;
  149. }
  150. return bits;
  151. }
  152. static void swd_clock_delay(AppFSM* const ctx) {
  153. if(ctx->swd_clock_delay) {
  154. furi_delay_us(ctx->swd_clock_delay);
  155. }
  156. }
  157. static void swd_write_bit(AppFSM* const ctx, bool level) {
  158. swd_set_clock(ctx, 0);
  159. swd_set_data(ctx, level);
  160. swd_clock_delay(ctx);
  161. swd_set_clock(ctx, 1);
  162. swd_clock_delay(ctx);
  163. swd_set_clock(ctx, 0);
  164. }
  165. static uint8_t swd_read_bit(AppFSM* const ctx) {
  166. swd_set_clock(ctx, 1);
  167. swd_clock_delay(ctx);
  168. swd_set_clock(ctx, 0);
  169. uint8_t bits = swd_get_data(ctx);
  170. swd_clock_delay(ctx);
  171. swd_set_clock(ctx, 1);
  172. return bits;
  173. }
  174. /* send a byte or less LSB-first */
  175. static void swd_write_byte(AppFSM* const ctx, const uint8_t data, size_t bits) {
  176. for(size_t pos = 0; pos < bits; pos++) {
  177. swd_write_bit(ctx, data & (1 << pos));
  178. }
  179. }
  180. /* send a sequence of bytes LSB-first */
  181. static void swd_write(AppFSM* const ctx, const uint8_t* data, size_t bits) {
  182. size_t byte_pos = 0;
  183. while(bits > 0) {
  184. size_t remain = (bits > 8) ? 8 : bits;
  185. swd_write_byte(ctx, data[byte_pos++], remain);
  186. bits -= remain;
  187. }
  188. }
  189. static uint8_t swd_transfer(AppFSM* const ctx, bool ap, bool write, uint8_t a23, uint32_t* data) {
  190. //notification_message(ctx->notification, &sequence_set_blue_255);
  191. //notification_message(ctx->notification, &sequence_reset_red);
  192. swd_set_data(ctx, false);
  193. swd_configure_pins(ctx, true);
  194. uint32_t idle = 0;
  195. swd_write(ctx, (uint8_t*)&idle, ctx->swd_idle_bits);
  196. uint8_t request[] = {0};
  197. request[0] |= 0x01; /* start bit*/
  198. request[0] |= ap ? 0x02 : 0; /* APnDP */
  199. request[0] |= write ? 0 : 0x04; /* operation */
  200. request[0] |= (a23 & 0x01) ? 0x08 : 0; /* A[2:3] */
  201. request[0] |= (a23 & 0x02) ? 0x10 : 0; /* A[2:3] */
  202. request[0] |= 0x80; /* park bit */
  203. request[0] |= __builtin_parity(request[0]) ? 0x20 : 0; /* parity */
  204. swd_write(ctx, request, sizeof(request) * 8);
  205. /* turnaround cycle */
  206. swd_configure_pins(ctx, false);
  207. uint8_t ack = 0;
  208. /* receive 3 ACK bits */
  209. for(int pos = 0; pos < 3; pos++) {
  210. ack >>= 1;
  211. ack |= swd_read_bit(ctx) ? 0x04 : 0;
  212. }
  213. /* force ABORT/CTRL to always work */
  214. if(!ap && a23 == 0) {
  215. ack = 1;
  216. }
  217. if(ack != 0x01) {
  218. //notification_message(ctx->notification, &sequence_reset_blue);
  219. //notification_message(ctx->notification, &sequence_set_red_255);
  220. return ack;
  221. }
  222. if(write) {
  223. swd_write_bit(ctx, 0);
  224. swd_configure_pins(ctx, true);
  225. /* send 32 WDATA bits */
  226. for(int pos = 0; pos < 32; pos++) {
  227. swd_write_bit(ctx, *data & (1 << pos));
  228. }
  229. /* send parity bit */
  230. swd_write_bit(ctx, __builtin_parity(*data));
  231. } else {
  232. *data = 0;
  233. /* receive 32 RDATA bits */
  234. for(int pos = 0; pos < 32; pos++) {
  235. *data >>= 1;
  236. *data |= swd_read_bit(ctx) ? 0x80000000 : 0;
  237. }
  238. /* receive parity bit */
  239. bool parity = swd_read_bit(ctx);
  240. if(parity != __builtin_parity(*data)) {
  241. //notification_message(ctx->notification, &sequence_reset_blue);
  242. //notification_message(ctx->notification, &sequence_set_red_255);
  243. return 8;
  244. }
  245. }
  246. swd_set_data(ctx, false);
  247. swd_configure_pins(ctx, true);
  248. //notification_message(ctx->notification, &sequence_reset_blue);
  249. return ack;
  250. }
  251. /* A line reset is achieved by holding the data signal HIGH for at least 50 clock cycles, followed by at least two idle cycles. */
  252. static void swd_line_reset(AppFSM* const ctx) {
  253. //notification_message(ctx->notification, &sequence_set_red_255);
  254. for(int bitcount = 0; bitcount < 50; bitcount += 8) {
  255. swd_write_byte(ctx, 0xFF, 8);
  256. }
  257. swd_write_byte(ctx, 0, 8);
  258. ctx->dp_regs.select_ok = false;
  259. //notification_message(ctx->notification, &sequence_reset_red);
  260. }
  261. static void swd_abort(AppFSM* const ctx) {
  262. uint32_t dpidr;
  263. /* first reset the line */
  264. swd_line_reset(ctx);
  265. swd_transfer(ctx, false, false, 0, &dpidr);
  266. uint32_t abort = 0x0E;
  267. swd_transfer(ctx, false, true, 0, &abort);
  268. }
  269. static void swd_abort_simple(AppFSM* const ctx) {
  270. uint32_t abort = 0x0E;
  271. swd_transfer(ctx, false, true, 0, &abort);
  272. uint32_t dpidr;
  273. if(swd_transfer(ctx, false, false, 0, &dpidr) != 1) {
  274. swd_abort(ctx);
  275. }
  276. }
  277. static uint8_t swd_select(AppFSM* const ctx, uint8_t ap_sel, uint8_t ap_bank, uint8_t dp_bank) {
  278. uint32_t bank_reg = (ap_sel << 24) | ((ap_bank & 0x0F) << 4) | (dp_bank & 0x0F);
  279. if(ctx->dp_regs.select_ok && bank_reg == ctx->dp_regs.select) {
  280. return 1;
  281. }
  282. uint8_t ret = swd_transfer(ctx, false, true, REG_SELECT, &bank_reg);
  283. if(ret != 1) {
  284. ctx->dp_regs.select_ok = false;
  285. DBG("failed: %d", ret);
  286. return ret;
  287. }
  288. ctx->dp_regs.select = bank_reg;
  289. ctx->dp_regs.select_ok = true;
  290. return ret;
  291. }
  292. static uint8_t
  293. swd_read_dpbank(AppFSM* const ctx, uint8_t dp_off, uint8_t dp_bank, uint32_t* data) {
  294. uint8_t ret = 0;
  295. /* select target bank */
  296. if(dp_bank < 0x10) {
  297. uint8_t ret = swd_select(ctx, 0, 0, dp_bank);
  298. if(ret != 1) {
  299. DBGS("swd_select failed");
  300. return ret;
  301. }
  302. }
  303. /* read data from it */
  304. *data = 0;
  305. ret = swd_transfer(ctx, false, false, dp_off, data);
  306. if(ret != 1) {
  307. DBG("failed: %d", ret);
  308. return ret;
  309. }
  310. return ret;
  311. }
  312. static uint8_t
  313. swd_write_dpbank(AppFSM* const ctx, uint8_t dp_off, uint8_t dp_bank, uint32_t* data) {
  314. uint8_t ret = 0;
  315. /* select target bank */
  316. if(dp_bank < 0x10) {
  317. ret = swd_select(ctx, 0, 0, dp_bank);
  318. if(ret != 1) {
  319. DBGS("swd_select failed");
  320. return ret;
  321. }
  322. }
  323. /* write it */
  324. ret = swd_transfer(ctx, false, true, dp_off, data);
  325. if(ret != 1) {
  326. DBG("failed: %d", ret);
  327. return ret;
  328. }
  329. return ret;
  330. }
  331. static uint8_t swd_read_ap(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t* data) {
  332. /* select target bank */
  333. uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
  334. if(ret != 1) {
  335. DBGS("swd_select failed");
  336. return ret;
  337. }
  338. ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
  339. *data = 0;
  340. ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
  341. if(ret != 1) {
  342. DBG("failed: %d", ret);
  343. return ret;
  344. }
  345. return ret;
  346. }
  347. static uint8_t swd_read_ap_single(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t* data) {
  348. uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
  349. if(ret != 1) {
  350. DBGS("swd_select failed");
  351. return ret;
  352. }
  353. *data = 0;
  354. ret = swd_transfer(ctx, true, false, (ap_off >> 2) & 3, data);
  355. if(ret != 1) {
  356. DBG("failed: %d", ret);
  357. return ret;
  358. }
  359. return ret;
  360. }
  361. static uint8_t swd_write_ap(AppFSM* const ctx, uint8_t ap, uint8_t ap_off, uint32_t data) {
  362. uint8_t ret = swd_select(ctx, ap, (ap_off >> 4) & 0x0F, 0);
  363. if(ret != 1) {
  364. DBGS("swd_select failed");
  365. return ret;
  366. }
  367. ret = swd_transfer(ctx, true, true, (ap_off >> 2) & 3, &data);
  368. if(ret != 1) {
  369. DBG("failed: %d", ret);
  370. return ret;
  371. }
  372. return ret;
  373. }
  374. static uint8_t swd_write_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t data) {
  375. uint8_t ret = 0;
  376. uint32_t csw = 0x23000002;
  377. ret |= swd_write_ap(ctx, ap, MEMAP_CSW, csw);
  378. ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
  379. ret |= swd_write_ap(ctx, ap, MEMAP_DRW, data);
  380. DBG("write 0x%08lX to 0x%08lX", data, address);
  381. if(ret != 1) {
  382. swd_abort(ctx);
  383. }
  384. return ret;
  385. }
  386. uint8_t swd_read_memory(AppFSM* const ctx, uint8_t ap, uint32_t address, uint32_t* data) {
  387. uint8_t ret = 0;
  388. uint32_t csw = 0x23000002;
  389. ret |= swd_write_ap(ctx, ap, MEMAP_CSW, csw);
  390. ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
  391. ret |= swd_read_ap(ctx, ap, MEMAP_DRW, data);
  392. if(ret != 1) {
  393. DBG("read from 0x%08lX failed", address);
  394. swd_abort(ctx);
  395. } else {
  396. DBG("read 0x%08lX from 0x%08lX", *data, address);
  397. }
  398. return ret;
  399. }
  400. static uint8_t swd_read_memory_block(
  401. AppFSM* const ctx,
  402. uint8_t ap,
  403. uint32_t address,
  404. uint8_t* buf,
  405. uint32_t len) {
  406. uint8_t ret = 0;
  407. uint32_t data = 0;
  408. uint32_t csw = 0x23000012;
  409. ret |= swd_write_ap(ctx, ap, MEMAP_CSW, csw);
  410. ret |= swd_write_ap(ctx, ap, MEMAP_TAR, address);
  411. ret |= swd_read_ap_single(ctx, ap, MEMAP_DRW, &data);
  412. for(size_t pos = 0; pos < len; pos += 4) {
  413. data = 0xDEADBEEF;
  414. ret |= swd_read_ap_single(ctx, ap, MEMAP_DRW, &data);
  415. DBG("read %lX", data);
  416. memcpy(&buf[pos], &data, 4);
  417. if(ret != 1) {
  418. swd_abort(ctx);
  419. return ret;
  420. }
  421. }
  422. return ret;
  423. }
  424. static uint32_t swd_detect(AppFSM* const ctx) {
  425. swd_set_data(ctx, false);
  426. swd_configure_pins(ctx, true);
  427. uint8_t data[] = {0xA5};
  428. swd_write(ctx, data, sizeof(data) * 8);
  429. /* turnaround cycle */
  430. swd_configure_pins(ctx, false);
  431. uint8_t ack_bits[3];
  432. uint8_t rdata[32];
  433. /* receive 3 ACK bits */
  434. for(int pos = 0; pos < 3; pos++) {
  435. ack_bits[pos] = swd_read_bit(ctx);
  436. }
  437. /* receive 32 RDATA bits */
  438. for(int pos = 0; pos < 32; pos++) {
  439. rdata[pos] = swd_read_bit(ctx);
  440. }
  441. /* receive parity bit */
  442. uint8_t parity = swd_read_bit(ctx);
  443. for(int io = 0; io < 8; io++) {
  444. uint8_t bitmask = 1 << io;
  445. /* skip if it's a clock */
  446. if(ctx->current_mask & bitmask) {
  447. continue;
  448. }
  449. uint8_t ack = 0;
  450. for(int pos = 0; pos < 3; pos++) {
  451. ack >>= 1;
  452. ack |= (ack_bits[pos] & bitmask) ? 4 : 0;
  453. }
  454. uint32_t dpidr = 0;
  455. for(int pos = 0; pos < 32; pos++) {
  456. dpidr >>= 1;
  457. dpidr |= (rdata[pos] & bitmask) ? 0x80000000 : 0;
  458. }
  459. if(ack == 1 && dpidr != 0 && dpidr != 0xFFFFFFFF) {
  460. bool received_parity = (parity & bitmask);
  461. if(__builtin_parity(dpidr) == received_parity) {
  462. ctx->dp_regs.dpidr = dpidr;
  463. ctx->dp_regs.dpidr_ok = true;
  464. ctx->detected = true;
  465. ctx->io_swd = bitmask;
  466. ctx->io_swc &= ctx->current_mask;
  467. LOG("swd_detect: data: %08lX, io_swd %02X, io_swc %02X",
  468. dpidr,
  469. ctx->io_swd,
  470. ctx->io_swc);
  471. if(!has_multiple_bits(ctx->io_swc)) {
  472. ctx->io_num_swd = get_bit_num(ctx->io_swd);
  473. ctx->io_num_swc = get_bit_num(ctx->io_swc);
  474. }
  475. }
  476. }
  477. }
  478. swd_set_data(ctx, false);
  479. swd_configure_pins(ctx, true);
  480. return 0;
  481. }
  482. static void swd_scan(AppFSM* const ctx) {
  483. /* To switch SWJ-DP from JTAG to SWD operation:
  484. 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.
  485. 2. Send the 16-bit JTAG-to-SWD select sequence 0x79e7 on SWDIOTMS.
  486. 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.
  487. */
  488. swd_configure_pins(ctx, true);
  489. /* reset JTAG interface */
  490. for(int bitcount = 0; bitcount < 50; bitcount += 8) {
  491. swd_write_byte(ctx, 0xFF, 8);
  492. }
  493. /* Send the 16-bit JTAG-to-SWD select sequence */
  494. swd_write_byte(ctx, 0x9E, 8);
  495. swd_write_byte(ctx, 0xE7, 8);
  496. /* resynchronize SWD */
  497. swd_line_reset(ctx);
  498. swd_detect(ctx);
  499. }
  500. static bool swd_ensure_powerup(AppFSM* const ctx) {
  501. bool ret = true;
  502. if(!(ctx->dp_regs.ctrlstat & (CSYSPWRUPREQ | CDBGPWRUPREQ))) {
  503. DBGS("no (CSYSPWRUPREQ | CDBGPWRUPREQ)");
  504. /* fetch current CTRL/STAT */
  505. DBGS(" - Fetch CTRL/STAT");
  506. ctx->dp_regs.ctrlstat_ok =
  507. swd_read_dpbank(ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat) == 1;
  508. DBG(" %08lX %s", ctx->dp_regs.ctrlstat, ctx->dp_regs.ctrlstat_ok ? "OK" : "FAIL");
  509. /* enable requests */
  510. ctx->dp_regs.ctrlstat |= (CSYSPWRUPREQ | CDBGPWRUPREQ);
  511. swd_write_dpbank(ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat);
  512. ret = false;
  513. }
  514. if(!(ctx->dp_regs.ctrlstat & CDBGPWRUPACK)) {
  515. DBGS("no CDBGPWRUPACK");
  516. /* fetch current CTRL/STAT */
  517. swd_read_dpbank(ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat);
  518. ret = false;
  519. }
  520. if(!ret) {
  521. DBGS(" - Fetch CTRL/STAT");
  522. ctx->dp_regs.ctrlstat_ok =
  523. swd_read_dpbank(ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat) == 1;
  524. DBG(" %08lX %s", ctx->dp_regs.ctrlstat, ctx->dp_regs.ctrlstat_ok ? "OK" : "FAIL");
  525. }
  526. return ret;
  527. }
  528. static void swd_apscan_reset(AppFSM* const ctx) {
  529. for(size_t reset_ap = 0; reset_ap < COUNT(ctx->apidr_info); reset_ap++) {
  530. ctx->apidr_info[reset_ap].tested = false;
  531. }
  532. }
  533. static bool swd_apscan_test(AppFSM* const ctx, uint32_t ap) {
  534. furi_assert(ctx);
  535. furi_assert(ap < sizeof(ctx->apidr_info));
  536. bool ret = true;
  537. ctx->apidr_info[ap].tested = true;
  538. uint32_t data = 0;
  539. if(swd_read_ap(ctx, ap, AP_IDR, &data) != 1) {
  540. swd_abort(ctx);
  541. return false;
  542. }
  543. if(data == 0) {
  544. return false;
  545. }
  546. DBG("AP%lu detected", ap);
  547. ctx->apidr_info[ap].ok = true;
  548. ctx->apidr_info[ap].revision = (data >> 24) & 0x0F;
  549. ctx->apidr_info[ap].designer = (data >> 17) & 0x3FF;
  550. ctx->apidr_info[ap].class = (data >> 13) & 0x0F;
  551. ctx->apidr_info[ap].variant = (data >> 4) & 0x0F;
  552. ctx->apidr_info[ap].type = (data >> 0) & 0x0F;
  553. if(swd_read_ap(ctx, ap, AP_BASE, &ctx->apidr_info[ap].base) != 1) {
  554. swd_abort(ctx);
  555. ret = false;
  556. }
  557. return ret;
  558. }
  559. /************************** script helpers **************************/
  560. static void swd_script_log(ScriptContext* ctx, FuriLogLevel level, const char* format, ...) {
  561. bool commandline = false;
  562. ScriptContext* cur = ctx;
  563. FuriString* buffer = furi_string_alloc();
  564. va_list argp;
  565. va_start(argp, format);
  566. do {
  567. if(cur == ctx->app->commandline) {
  568. commandline = true;
  569. }
  570. cur = cur->parent;
  571. } while(cur);
  572. if(commandline) {
  573. const char* prefix = "";
  574. switch(level) {
  575. case FuriLogLevelWarn:
  576. prefix = "Warning: ";
  577. break;
  578. case FuriLogLevelError:
  579. prefix = "ERROR: ";
  580. break;
  581. default:
  582. break;
  583. }
  584. furi_string_cat_str(buffer, prefix);
  585. furi_string_cat_printf(buffer, format, argp);
  586. furi_string_cat_str(buffer, "\n");
  587. if(!usb_uart_tx_data(
  588. ctx->app->uart, (uint8_t*)furi_string_get_cstr(buffer), furi_string_size(buffer))) {
  589. DBGS("Sending via USB failed");
  590. }
  591. } else {
  592. LOG(furi_string_get_cstr(buffer));
  593. }
  594. va_end(argp);
  595. furi_string_free(buffer);
  596. }
  597. /* read characters until newline was read */
  598. static bool swd_script_seek_newline(ScriptContext* ctx) {
  599. while(true) {
  600. uint8_t ch = 0;
  601. if(ctx->script_file) {
  602. if(storage_file_read(ctx->script_file, &ch, 1) != 1) {
  603. return false;
  604. }
  605. } else {
  606. ch = ctx->line_data[ctx->line_pos];
  607. if(ch == 0) {
  608. return false;
  609. }
  610. ctx->line_pos++;
  611. }
  612. if(ch == '\n') {
  613. return true;
  614. }
  615. }
  616. }
  617. /* read whitespaces until the next character is read.
  618. returns false if EOF or newline was read */
  619. static bool swd_script_skip_whitespace(ScriptContext* ctx) {
  620. while(true) {
  621. uint8_t ch = 0;
  622. uint64_t start_pos = 0;
  623. if(ctx->script_file) {
  624. start_pos = storage_file_tell(ctx->script_file);
  625. if(storage_file_read(ctx->script_file, &ch, 1) != 1) {
  626. return false;
  627. }
  628. } else {
  629. start_pos = ctx->line_pos;
  630. ch = ctx->line_data[ctx->line_pos];
  631. if(ch == 0) {
  632. return false;
  633. }
  634. ctx->line_pos++;
  635. }
  636. if(ch == '\n') {
  637. return false;
  638. }
  639. if(ch != ' ') {
  640. if(ctx->script_file) {
  641. storage_file_seek(ctx->script_file, start_pos, true);
  642. } else {
  643. ctx->line_pos = start_pos;
  644. }
  645. return true;
  646. }
  647. }
  648. }
  649. static bool swd_script_get_string(ScriptContext* ctx, char* str, size_t max_length) {
  650. bool quot = false;
  651. size_t pos = 0;
  652. str[pos] = '\000';
  653. while(true) {
  654. char ch = 0;
  655. uint64_t start_pos = 0;
  656. if(ctx->script_file) {
  657. start_pos = storage_file_tell(ctx->script_file);
  658. if(storage_file_read(ctx->script_file, &ch, 1) != 1) {
  659. DBGS("end reached");
  660. return false;
  661. }
  662. } else {
  663. start_pos = ctx->line_pos;
  664. ch = ctx->line_data[ctx->line_pos];
  665. if(ch == 0) {
  666. DBGS("end reached");
  667. return false;
  668. }
  669. ctx->line_pos++;
  670. }
  671. if(ch == '"') {
  672. quot = !quot;
  673. continue;
  674. }
  675. if(!quot) {
  676. if(ch == ' ') {
  677. break;
  678. }
  679. if(ch == '\r' || ch == '\n') {
  680. if(ctx->script_file) {
  681. storage_file_seek(ctx->script_file, start_pos, true);
  682. } else {
  683. ctx->line_pos = start_pos;
  684. }
  685. break;
  686. }
  687. }
  688. if(pos + 2 > max_length) {
  689. DBGS("too long");
  690. return false;
  691. }
  692. str[pos++] = ch;
  693. str[pos] = '\000';
  694. }
  695. DBG("got '%s'", str);
  696. return true;
  697. }
  698. static bool swd_script_get_number(ScriptContext* ctx, uint32_t* number) {
  699. char str[16];
  700. if(!swd_script_get_string(ctx, str, sizeof(str))) {
  701. DBGS("could not get string");
  702. return false;
  703. }
  704. DBG("got '%s'", str);
  705. size_t pos = 0;
  706. *number = 0;
  707. /* hex number? */
  708. if(!strncmp(str, "0x", 2)) {
  709. pos += 2;
  710. while(str[pos]) {
  711. uint8_t ch = str[pos++];
  712. uint8_t ch_num = ch - '0';
  713. uint8_t ch_hex = (ch & ~0x20) - 'A';
  714. *number <<= 4;
  715. if(ch_num <= 10) {
  716. *number += ch_num;
  717. } else if(ch_hex <= 5) {
  718. *number += 10 + ch_hex;
  719. } else {
  720. return false;
  721. }
  722. }
  723. } else {
  724. while(str[pos]) {
  725. uint8_t ch = str[pos++];
  726. uint8_t ch_num = ch - '0';
  727. *number *= 10;
  728. if(ch_num < 10) {
  729. *number += ch_num;
  730. } else {
  731. return false;
  732. }
  733. }
  734. }
  735. return true;
  736. }
  737. static void swd_script_gui_refresh(ScriptContext* ctx) {
  738. if(furi_message_queue_get_count(ctx->app->event_queue) > 0) {
  739. swd_message_process(ctx->app);
  740. }
  741. if(!ctx->status_ignore) {
  742. DBG("Status: %s", ctx->app->state_string);
  743. view_port_update(ctx->app->view_port);
  744. }
  745. }
  746. /************************** script functions **************************/
  747. static bool swd_scriptfunc_comment(ScriptContext* ctx) {
  748. DBGS("comment");
  749. swd_script_seek_newline(ctx);
  750. return true;
  751. }
  752. static bool swd_scriptfunc_label(ScriptContext* ctx) {
  753. char label[256];
  754. DBGS("label");
  755. swd_script_skip_whitespace(ctx);
  756. if(!swd_script_get_string(ctx, label, sizeof(label))) {
  757. swd_script_log(ctx, FuriLogLevelError, "failed to parse label");
  758. return false;
  759. }
  760. if(!strcmp(label, ctx->goto_label)) {
  761. ctx->goto_active = false;
  762. DBG("matches '%s'", ctx->goto_label);
  763. }
  764. swd_script_seek_newline(ctx);
  765. return true;
  766. }
  767. static bool swd_scriptfunc_goto(ScriptContext* ctx) {
  768. DBGS("goto");
  769. swd_script_skip_whitespace(ctx);
  770. if(!swd_script_get_string(ctx, ctx->goto_label, sizeof(ctx->goto_label))) {
  771. swd_script_log(ctx, FuriLogLevelError, "failed to parse target label");
  772. return false;
  773. }
  774. /* start from beginning and rerun starting from label */
  775. ctx->goto_active = true;
  776. ctx->restart = true;
  777. swd_script_seek_newline(ctx);
  778. return true;
  779. }
  780. #include <toolbox/path.h>
  781. static bool swd_scriptfunc_call(ScriptContext* ctx) {
  782. DBGS("call");
  783. swd_script_skip_whitespace(ctx);
  784. /* fetch previous file directory */
  785. FuriString* filepath = furi_string_alloc();
  786. path_extract_dirname(ctx->filename, filepath);
  787. // strncpy(filename, ctx->filename, sizeof(filename));
  788. char filename[MAX_FILE_LENGTH] = {};
  789. bool success = false;
  790. do {
  791. /* append filename */
  792. if(!swd_script_get_string(ctx, filename, sizeof(filename))) {
  793. swd_script_log(ctx, FuriLogLevelError, "failed to parse filename");
  794. break;
  795. }
  796. swd_script_seek_newline(ctx);
  797. /* append extension */
  798. furi_string_cat_str(filepath, ".swd");
  799. bool ret = swd_execute_script(ctx->app, furi_string_get_cstr(filepath));
  800. if(!ret) {
  801. swd_script_log(
  802. ctx, FuriLogLevelError, "failed to exec '%s'", furi_string_get_cstr(filepath));
  803. break;
  804. }
  805. success = true;
  806. } while(false);
  807. furi_string_free(filepath);
  808. return success;
  809. }
  810. static bool swd_scriptfunc_status(ScriptContext* ctx) {
  811. uint32_t status = 1;
  812. DBGS("status");
  813. swd_script_skip_whitespace(ctx);
  814. swd_script_get_number(ctx, &status);
  815. ctx->status_ignore = (status == 0);
  816. swd_script_seek_newline(ctx);
  817. return true;
  818. }
  819. static bool swd_scriptfunc_errors(ScriptContext* ctx) {
  820. char type[32];
  821. DBGS("errors");
  822. swd_script_skip_whitespace(ctx);
  823. if(!swd_script_get_string(ctx, type, sizeof(type))) {
  824. swd_script_log(ctx, FuriLogLevelError, "failed to parse");
  825. return false;
  826. }
  827. if(!strcmp(type, "ignore")) {
  828. ctx->errors_ignore = true;
  829. }
  830. if(!strcmp(type, "fail")) {
  831. ctx->errors_ignore = false;
  832. }
  833. swd_script_seek_newline(ctx);
  834. return true;
  835. }
  836. static bool swd_scriptfunc_beep(ScriptContext* ctx) {
  837. uint32_t sound = 0;
  838. DBGS("beep");
  839. swd_script_skip_whitespace(ctx);
  840. swd_script_get_number(ctx, &sound);
  841. notification_message_block(ctx->app->notification, seq_sounds[sound]);
  842. swd_script_seek_newline(ctx);
  843. return true;
  844. }
  845. static bool swd_scriptfunc_message(ScriptContext* ctx) {
  846. uint32_t wait_time = 0;
  847. char message[256];
  848. char type[256];
  849. bool success = true;
  850. bool show_dialog = false;
  851. if(!swd_script_skip_whitespace(ctx)) {
  852. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  853. return false;
  854. }
  855. if(!swd_script_get_number(ctx, &wait_time)) {
  856. swd_script_log(ctx, FuriLogLevelError, "failed to parse wait_time");
  857. return false;
  858. }
  859. if(!swd_script_get_string(ctx, message, sizeof(message))) {
  860. swd_script_log(ctx, FuriLogLevelError, "failed to parse message");
  861. return false;
  862. }
  863. if(swd_script_get_string(ctx, type, sizeof(type))) {
  864. if(!strcmp(type, "dialog")) {
  865. show_dialog = true;
  866. }
  867. }
  868. if(wait_time <= 60 * 1000) {
  869. strncpy(ctx->app->state_string, message, sizeof(ctx->app->state_string));
  870. swd_script_gui_refresh(ctx);
  871. furi_delay_ms(wait_time);
  872. if(show_dialog) {
  873. DialogMessage* message = dialog_message_alloc();
  874. dialog_message_set_header(message, "SWD Probe", 16, 2, AlignLeft, AlignTop);
  875. dialog_message_set_icon(message, &I_app, 3, 2);
  876. dialog_message_set_text(message, ctx->app->state_string, 3, 16, AlignLeft, AlignTop);
  877. dialog_message_set_buttons(message, "Abort", "Ok", NULL);
  878. success = dialog_message_show(ctx->app->dialogs, message) == DialogMessageButtonCenter;
  879. dialog_message_free(message);
  880. }
  881. }
  882. swd_script_seek_newline(ctx);
  883. return success;
  884. }
  885. static bool swd_scriptfunc_swd_idle_bits(ScriptContext* ctx) {
  886. uint32_t swd_idle_bits = 0;
  887. if(!swd_script_skip_whitespace(ctx)) {
  888. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  889. return false;
  890. }
  891. if(!swd_script_get_number(ctx, &swd_idle_bits)) {
  892. swd_script_log(ctx, FuriLogLevelError, "failed to parse");
  893. return false;
  894. }
  895. if(swd_idle_bits <= 32) {
  896. ctx->app->swd_idle_bits = swd_idle_bits;
  897. } else {
  898. swd_script_log(ctx, FuriLogLevelError, "value must be between 1 and 32");
  899. }
  900. swd_script_seek_newline(ctx);
  901. return true;
  902. }
  903. static bool swd_scriptfunc_swd_clock_delay(ScriptContext* ctx) {
  904. uint32_t swd_clock_delay = 0;
  905. if(!swd_script_skip_whitespace(ctx)) {
  906. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  907. return false;
  908. }
  909. if(!swd_script_get_number(ctx, &swd_clock_delay)) {
  910. swd_script_log(ctx, FuriLogLevelError, "failed to parse");
  911. return false;
  912. }
  913. if(swd_clock_delay <= 1000000) {
  914. ctx->app->swd_clock_delay = swd_clock_delay;
  915. } else {
  916. swd_script_log(ctx, FuriLogLevelError, "value must be between 1 and 1000000");
  917. }
  918. swd_script_seek_newline(ctx);
  919. return true;
  920. }
  921. static bool swd_scriptfunc_maxtries(ScriptContext* ctx) {
  922. uint32_t max_tries = 0;
  923. if(!swd_script_skip_whitespace(ctx)) {
  924. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  925. return false;
  926. }
  927. if(!swd_script_get_number(ctx, &max_tries)) {
  928. swd_script_log(ctx, FuriLogLevelError, "failed to parse");
  929. return false;
  930. }
  931. if(max_tries >= 1 && max_tries <= 1024) {
  932. ctx->max_tries = max_tries;
  933. } else {
  934. DBGS("value must be between 1 and 1024");
  935. }
  936. swd_script_seek_newline(ctx);
  937. return true;
  938. }
  939. static bool swd_scriptfunc_blocksize(ScriptContext* ctx) {
  940. uint32_t block_size = 0;
  941. if(!swd_script_skip_whitespace(ctx)) {
  942. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  943. return false;
  944. }
  945. if(!swd_script_get_number(ctx, &block_size)) {
  946. swd_script_log(ctx, FuriLogLevelError, "failed to parse");
  947. return false;
  948. }
  949. if(block_size >= 4 && block_size <= 0x1000) {
  950. ctx->block_size = block_size;
  951. } else {
  952. swd_script_log(ctx, FuriLogLevelError, "value must be between 4 and 4096");
  953. }
  954. swd_script_seek_newline(ctx);
  955. return true;
  956. }
  957. static bool swd_scriptfunc_apselect(ScriptContext* ctx) {
  958. uint32_t ap = 0;
  959. if(!swd_script_skip_whitespace(ctx)) {
  960. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  961. return false;
  962. }
  963. if(!swd_script_get_number(ctx, &ap)) {
  964. swd_script_log(ctx, FuriLogLevelError, "failed to parse AP");
  965. return false;
  966. }
  967. if(!swd_apscan_test(ctx->app, ap)) {
  968. swd_script_log(ctx, FuriLogLevelError, "no selected AP");
  969. return false;
  970. }
  971. ctx->selected_ap = ap;
  972. swd_script_seek_newline(ctx);
  973. return true;
  974. }
  975. static bool swd_scriptfunc_apscan(ScriptContext* ctx) {
  976. DBGS("Scanning APs");
  977. for(uint32_t ap = 0; ap < 255; ap++) {
  978. snprintf(ctx->app->state_string, sizeof(ctx->app->state_string), "Scan AP %lu", ap);
  979. swd_script_gui_refresh(ctx);
  980. if(swd_apscan_test(ctx->app, ap)) {
  981. DBG(" AP%lu detected", ap);
  982. }
  983. }
  984. swd_script_seek_newline(ctx);
  985. return true;
  986. }
  987. static bool swd_scriptfunc_abort(ScriptContext* ctx) {
  988. DBGS("Aborting");
  989. swd_abort(ctx->app);
  990. swd_script_seek_newline(ctx);
  991. return true;
  992. }
  993. static bool swd_scriptfunc_mem_dump(ScriptContext* ctx) {
  994. char filename[MAX_FILE_LENGTH];
  995. uint32_t address = 0;
  996. uint32_t length = 0;
  997. uint32_t flags = 0;
  998. bool success = true;
  999. /* get file */
  1000. if(!swd_script_skip_whitespace(ctx)) {
  1001. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1002. return false;
  1003. }
  1004. if(!swd_script_get_string(ctx, filename, sizeof(filename))) {
  1005. swd_script_log(ctx, FuriLogLevelError, "failed to parse filename");
  1006. return false;
  1007. }
  1008. /* get address */
  1009. if(!swd_script_get_number(ctx, &address)) {
  1010. swd_script_log(ctx, FuriLogLevelError, "failed to parse address");
  1011. return false;
  1012. }
  1013. /* get length */
  1014. if(!swd_script_get_number(ctx, &length)) {
  1015. swd_script_log(ctx, FuriLogLevelError, "failed to parse length");
  1016. return false;
  1017. }
  1018. /* get flags */
  1019. if(swd_script_get_number(ctx, &flags)) {
  1020. DBGS("found extra flags");
  1021. }
  1022. LOG("would dump %08lX, len %08lX into %s", address, length, filename);
  1023. File* dump = storage_file_alloc(ctx->app->storage);
  1024. if(!storage_file_open(dump, filename, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  1025. storage_file_free(dump);
  1026. snprintf(ctx->app->state_string, sizeof(ctx->app->state_string), "Failed to create file");
  1027. swd_script_gui_refresh(ctx);
  1028. notification_message_block(ctx->app->notification, &seq_error);
  1029. return false;
  1030. }
  1031. if(ctx->block_size == 0) {
  1032. ctx->block_size = 0x100;
  1033. }
  1034. if(ctx->block_size > 0x1000) {
  1035. ctx->block_size = 0x1000;
  1036. }
  1037. uint8_t* buffer = malloc(ctx->block_size);
  1038. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1039. for(uint32_t pos = 0; pos < length; pos += ctx->block_size) {
  1040. if((pos & 0xFF) == 0) {
  1041. int pct = pos * 100 / length;
  1042. snprintf(
  1043. ctx->app->state_string,
  1044. sizeof(ctx->app->state_string),
  1045. "Dump %08lX (%d%%)",
  1046. pos,
  1047. pct);
  1048. swd_script_gui_refresh(ctx);
  1049. }
  1050. bool read_ok = false;
  1051. for(uint32_t tries = 0; tries < ctx->max_tries; tries++) {
  1052. if(ctx->abort) {
  1053. DBGS("aborting read");
  1054. break;
  1055. }
  1056. uint32_t ret = 0;
  1057. if(ctx->block_size > 4) {
  1058. ret = swd_read_memory_block(
  1059. ctx->app, ctx->selected_ap, address + pos, buffer, ctx->block_size);
  1060. } else {
  1061. ret =
  1062. swd_read_memory(ctx->app, ctx->selected_ap, address + pos, (uint32_t*)buffer);
  1063. }
  1064. read_ok = (ret == 1);
  1065. if(!read_ok) {
  1066. snprintf(
  1067. ctx->app->state_string,
  1068. sizeof(ctx->app->state_string),
  1069. "Failed at 0x%08lX",
  1070. address + pos);
  1071. swd_script_gui_refresh(ctx);
  1072. furi_delay_ms(100);
  1073. } else {
  1074. break;
  1075. }
  1076. }
  1077. if(ctx->abort) {
  1078. DBGS("aborting");
  1079. break;
  1080. }
  1081. if(!read_ok) {
  1082. /* flags == 1: "continue reading even if it fails" */
  1083. /* flags == 2: "its okay if cannot dump fully" */
  1084. if(flags & 1) {
  1085. /* set all content to a known value as indication */
  1086. for(size_t fill_pos = 0; fill_pos < ctx->block_size; fill_pos += 4) {
  1087. *((uint32_t*)&buffer[fill_pos]) = 0xDEADFACE;
  1088. }
  1089. } else if(flags & 2) {
  1090. success = (pos > 0);
  1091. break;
  1092. } else {
  1093. notification_message_block(ctx->app->notification, &seq_error);
  1094. success = false;
  1095. break;
  1096. }
  1097. }
  1098. storage_file_write(dump, buffer, ctx->block_size);
  1099. }
  1100. furi_mutex_release(ctx->app->swd_mutex);
  1101. storage_file_close(dump);
  1102. swd_script_seek_newline(ctx);
  1103. free(buffer);
  1104. return success;
  1105. }
  1106. static bool swd_scriptfunc_mem_write(ScriptContext* ctx) {
  1107. uint32_t address = 0;
  1108. uint32_t data = 0;
  1109. bool success = true;
  1110. /* get file */
  1111. if(!swd_script_skip_whitespace(ctx)) {
  1112. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1113. return false;
  1114. }
  1115. /* get address */
  1116. if(!swd_script_get_number(ctx, &address)) {
  1117. swd_script_log(ctx, FuriLogLevelError, "failed to parse address");
  1118. return false;
  1119. }
  1120. /* get data */
  1121. if(!swd_script_get_number(ctx, &data)) {
  1122. swd_script_log(ctx, FuriLogLevelError, "failed to parse data");
  1123. return false;
  1124. }
  1125. DBG("write %08lX to %08lX", data, address);
  1126. bool access_ok = false;
  1127. for(uint32_t tries = 0; tries < ctx->max_tries; tries++) {
  1128. if(ctx->abort) {
  1129. DBGS("aborting");
  1130. break;
  1131. }
  1132. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1133. access_ok = swd_write_memory(ctx->app, ctx->selected_ap, address, data) == 1;
  1134. access_ok |= ctx->errors_ignore;
  1135. swd_read_memory(ctx->app, ctx->selected_ap, address, &data);
  1136. furi_mutex_release(ctx->app->swd_mutex);
  1137. DBG("read %08lX from %08lX", data, address);
  1138. if(!access_ok) {
  1139. snprintf(
  1140. ctx->app->state_string,
  1141. sizeof(ctx->app->state_string),
  1142. "Failed write 0x%08lX",
  1143. address);
  1144. swd_script_gui_refresh(ctx);
  1145. } else {
  1146. break;
  1147. }
  1148. }
  1149. if(!access_ok) {
  1150. notification_message_block(ctx->app->notification, &seq_error);
  1151. success = false;
  1152. }
  1153. swd_script_seek_newline(ctx);
  1154. return success;
  1155. }
  1156. static bool swd_scriptfunc_mem_read(ScriptContext* ctx) {
  1157. uint32_t address = 0;
  1158. bool success = true;
  1159. /* get file */
  1160. if(!swd_script_skip_whitespace(ctx)) {
  1161. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1162. return false;
  1163. }
  1164. /* get address */
  1165. if(!swd_script_get_number(ctx, &address)) {
  1166. swd_script_log(ctx, FuriLogLevelError, "failed to parse address");
  1167. return false;
  1168. }
  1169. DBG("read from %08lX", address);
  1170. uint32_t data = 0;
  1171. bool access_ok = false;
  1172. for(uint32_t tries = 0; tries < ctx->max_tries; tries++) {
  1173. if(ctx->abort) {
  1174. DBGS("aborting");
  1175. break;
  1176. }
  1177. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1178. access_ok = swd_read_memory(ctx->app, ctx->selected_ap, address, &data) == 1;
  1179. furi_mutex_release(ctx->app->swd_mutex);
  1180. if(!access_ok) {
  1181. swd_script_log(ctx, FuriLogLevelError, "Failed to read from %08lX", address);
  1182. snprintf(
  1183. ctx->app->state_string,
  1184. sizeof(ctx->app->state_string),
  1185. "Failed read 0x%08lX",
  1186. address);
  1187. swd_script_gui_refresh(ctx);
  1188. } else {
  1189. swd_script_log(ctx, FuriLogLevelDefault, "%08lX", data);
  1190. break;
  1191. }
  1192. }
  1193. if(!access_ok) {
  1194. notification_message_block(ctx->app->notification, &seq_error);
  1195. success = false;
  1196. }
  1197. swd_script_seek_newline(ctx);
  1198. return success;
  1199. }
  1200. static bool swd_scriptfunc_mem_ldmst(ScriptContext* ctx) {
  1201. uint32_t address = 0;
  1202. uint32_t data = 0;
  1203. uint32_t mask = 0;
  1204. bool success = true;
  1205. if(!swd_script_skip_whitespace(ctx)) {
  1206. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1207. return false;
  1208. }
  1209. /* get address */
  1210. if(!swd_script_get_number(ctx, &address)) {
  1211. swd_script_log(ctx, FuriLogLevelError, "failed to parse address");
  1212. return false;
  1213. }
  1214. /* get data */
  1215. if(!swd_script_get_number(ctx, &data)) {
  1216. swd_script_log(ctx, FuriLogLevelError, "failed to parse data");
  1217. return false;
  1218. }
  1219. /* get mask */
  1220. if(!swd_script_get_number(ctx, &mask)) {
  1221. swd_script_log(ctx, FuriLogLevelError, "failed to parse mask");
  1222. return false;
  1223. }
  1224. LOG("write %08lX to %08lX, mask %08lX", data, address, mask);
  1225. bool access_ok = false;
  1226. uint32_t modified = 0;
  1227. for(uint32_t tries = 0; tries < ctx->max_tries; tries++) {
  1228. if(ctx->abort) {
  1229. DBGS("aborting");
  1230. break;
  1231. }
  1232. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1233. access_ok = swd_read_memory(ctx->app, ctx->selected_ap, address, &modified) == 1;
  1234. modified = (modified & mask) | data;
  1235. access_ok &= swd_write_memory(ctx->app, ctx->selected_ap, address, modified) == 1;
  1236. furi_mutex_release(ctx->app->swd_mutex);
  1237. access_ok |= ctx->errors_ignore;
  1238. if(!access_ok) {
  1239. snprintf(
  1240. ctx->app->state_string,
  1241. sizeof(ctx->app->state_string),
  1242. "Failed access 0x%08lX",
  1243. address);
  1244. swd_script_gui_refresh(ctx);
  1245. } else {
  1246. break;
  1247. }
  1248. }
  1249. if(!access_ok) {
  1250. notification_message_block(ctx->app->notification, &seq_error);
  1251. success = false;
  1252. }
  1253. swd_script_seek_newline(ctx);
  1254. return success;
  1255. }
  1256. static bool swd_scriptfunc_dp_write(ScriptContext* ctx) {
  1257. uint32_t dp_bank = 0;
  1258. uint32_t dp_off = 0;
  1259. uint32_t data = 0;
  1260. bool success = true;
  1261. if(!swd_script_skip_whitespace(ctx)) {
  1262. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1263. return false;
  1264. }
  1265. /* get data */
  1266. if(!swd_script_get_number(ctx, &data)) {
  1267. swd_script_log(ctx, FuriLogLevelError, "failed to parse data");
  1268. return false;
  1269. }
  1270. /* get dp_off */
  1271. if(!swd_script_get_number(ctx, &dp_off)) {
  1272. swd_script_log(ctx, FuriLogLevelError, "failed to parse DP offset");
  1273. return false;
  1274. }
  1275. /* get dp_bank */
  1276. if(!swd_script_get_number(ctx, &dp_bank)) {
  1277. dp_bank = 0xFF;
  1278. }
  1279. swd_script_log(
  1280. ctx, FuriLogLevelDefault, "write %08lX to reg %08lX / bank %08lX", data, dp_off, dp_bank);
  1281. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1282. uint8_t ret = swd_write_dpbank(ctx->app, dp_off, dp_bank, &data);
  1283. if(ret != 1) {
  1284. swd_script_log(ctx, FuriLogLevelError, "swd_write_dpbank failed");
  1285. success = false;
  1286. }
  1287. furi_mutex_release(ctx->app->swd_mutex);
  1288. swd_script_seek_newline(ctx);
  1289. return success;
  1290. }
  1291. static bool swd_scriptfunc_dp_read(ScriptContext* ctx) {
  1292. uint32_t dp_bank = 0;
  1293. uint32_t dp_off = 0;
  1294. uint32_t data = 0;
  1295. bool success = true;
  1296. if(!swd_script_skip_whitespace(ctx)) {
  1297. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1298. return false;
  1299. }
  1300. /* get dp_off */
  1301. if(!swd_script_get_number(ctx, &dp_off)) {
  1302. swd_script_log(ctx, FuriLogLevelError, "failed to parse DP offset");
  1303. return false;
  1304. }
  1305. /* get dp_bank */
  1306. if(!swd_script_get_number(ctx, &dp_bank)) {
  1307. dp_bank = 0xFF;
  1308. }
  1309. swd_script_log(ctx, FuriLogLevelDefault, "read reg %02lX / bank %02lX", dp_off, dp_bank);
  1310. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1311. uint8_t ret = swd_read_dpbank(ctx->app, dp_off, dp_bank, &data);
  1312. if(ret != 1) {
  1313. swd_script_log(ctx, FuriLogLevelError, "swd_read_dpbank failed");
  1314. success = false;
  1315. } else {
  1316. swd_script_log(ctx, FuriLogLevelDefault, "result: 0x%08lX", data);
  1317. }
  1318. furi_mutex_release(ctx->app->swd_mutex);
  1319. swd_script_seek_newline(ctx);
  1320. return success;
  1321. }
  1322. static bool swd_scriptfunc_ap_write(ScriptContext* ctx) {
  1323. uint32_t ap_reg = 0;
  1324. uint32_t data = 0;
  1325. bool success = true;
  1326. if(!swd_script_skip_whitespace(ctx)) {
  1327. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1328. return false;
  1329. }
  1330. /* get data */
  1331. if(!swd_script_get_number(ctx, &data)) {
  1332. swd_script_log(ctx, FuriLogLevelError, "failed to parse data");
  1333. return false;
  1334. }
  1335. /* get ap_reg */
  1336. if(!swd_script_get_number(ctx, &ap_reg)) {
  1337. swd_script_log(ctx, FuriLogLevelError, "failed to parse AP register");
  1338. return false;
  1339. }
  1340. swd_script_log(
  1341. ctx, FuriLogLevelDefault, "AP%d %08lX -> %02lX", ctx->selected_ap, data, ap_reg);
  1342. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1343. uint8_t ret = swd_write_ap(ctx->app, ctx->selected_ap, ap_reg, data);
  1344. if(ret != 1) {
  1345. swd_script_log(ctx, FuriLogLevelError, "swd_write_ap failed");
  1346. success = false;
  1347. }
  1348. furi_mutex_release(ctx->app->swd_mutex);
  1349. swd_script_seek_newline(ctx);
  1350. return success;
  1351. }
  1352. static bool swd_scriptfunc_ap_read(ScriptContext* ctx) {
  1353. uint32_t ap_reg = 0;
  1354. uint32_t data = 0;
  1355. bool success = true;
  1356. if(!swd_script_skip_whitespace(ctx)) {
  1357. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1358. return false;
  1359. }
  1360. /* get ap_reg */
  1361. if(!swd_script_get_number(ctx, &ap_reg)) {
  1362. swd_script_log(ctx, FuriLogLevelError, "failed to parse AP register");
  1363. return false;
  1364. }
  1365. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1366. uint8_t ret = swd_read_ap(ctx->app, ctx->selected_ap, ap_reg, &data);
  1367. if(ret != 1) {
  1368. swd_script_log(ctx, FuriLogLevelError, "swd_read_ap failed");
  1369. success = false;
  1370. } else {
  1371. swd_script_log(
  1372. ctx, FuriLogLevelDefault, "AP%d %02lX: %08lX", ctx->selected_ap, ap_reg, data);
  1373. }
  1374. furi_mutex_release(ctx->app->swd_mutex);
  1375. swd_script_seek_newline(ctx);
  1376. return success;
  1377. }
  1378. static bool swd_scriptfunc_core_halt(ScriptContext* ctx) {
  1379. bool succ = false;
  1380. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1381. uint32_t reg_dhcsr = SCS_DHCSR_KEY | SCS_DHCSR_C_HALT | SCS_DHCSR_C_DEBUGEN;
  1382. succ = swd_write_memory(ctx->app, ctx->selected_ap, SCS_DHCSR, reg_dhcsr) == 1;
  1383. if(!succ) {
  1384. swd_script_log(ctx, FuriLogLevelError, "swd_write_memory failed");
  1385. } else {
  1386. swd_read_memory(ctx->app, ctx->selected_ap, SCS_DHCSR, &reg_dhcsr);
  1387. if(!(reg_dhcsr & SCS_DHCSR_S_HALT)) {
  1388. swd_script_log(ctx, FuriLogLevelError, "Core did not halt");
  1389. succ = false;
  1390. } else {
  1391. swd_script_log(ctx, FuriLogLevelDefault, "Core halted");
  1392. }
  1393. }
  1394. furi_mutex_release(ctx->app->swd_mutex);
  1395. swd_script_seek_newline(ctx);
  1396. return succ;
  1397. }
  1398. static bool swd_scriptfunc_core_continue(ScriptContext* ctx) {
  1399. bool succ = false;
  1400. uint32_t data = 0;
  1401. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1402. succ = swd_read_memory(ctx->app, ctx->selected_ap, SCS_DHCSR, &data) == 1;
  1403. if(!(data & SCS_DHCSR_S_HALT)) {
  1404. swd_script_log(ctx, FuriLogLevelError, "Core is not in debug state");
  1405. succ = false;
  1406. } else {
  1407. succ = swd_write_memory(ctx->app, ctx->selected_ap, SCS_DHCSR, SCS_DHCSR_KEY) == 1;
  1408. furi_mutex_release(ctx->app->swd_mutex);
  1409. }
  1410. if(!succ) {
  1411. swd_script_log(ctx, FuriLogLevelError, "swd_write_memory failed");
  1412. } else {
  1413. swd_script_log(ctx, FuriLogLevelDefault, "Core continued");
  1414. }
  1415. swd_script_seek_newline(ctx);
  1416. return succ;
  1417. }
  1418. static bool swd_scriptfunc_core_step(ScriptContext* ctx) {
  1419. bool succ = false;
  1420. uint32_t data = 0;
  1421. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1422. succ = swd_read_memory(ctx->app, ctx->selected_ap, SCS_DHCSR, &data) == 1;
  1423. if(!(data & SCS_DHCSR_S_HALT)) {
  1424. swd_script_log(ctx, FuriLogLevelError, "Core is not in debug state");
  1425. succ = false;
  1426. } else {
  1427. succ = swd_write_memory(
  1428. ctx->app,
  1429. ctx->selected_ap,
  1430. SCS_DHCSR,
  1431. SCS_DHCSR_KEY | SCS_DHCSR_C_STEP | SCS_DHCSR_C_MASKINTS |
  1432. SCS_DHCSR_C_DEBUGEN) == 1;
  1433. }
  1434. furi_mutex_release(ctx->app->swd_mutex);
  1435. if(!succ) {
  1436. swd_script_log(ctx, FuriLogLevelError, "swd_write_memory failed");
  1437. } else {
  1438. swd_script_log(ctx, FuriLogLevelDefault, "Core stepped");
  1439. }
  1440. swd_script_seek_newline(ctx);
  1441. return succ;
  1442. }
  1443. static struct cpu_regs_type {
  1444. uint8_t regsel;
  1445. const char* desc;
  1446. } cpu_regs[] = {
  1447. {0x00, "R00"}, {0x01, "R01"}, {0x02, "R02"}, {0x03, "R03"}, {0x04, "R04"},
  1448. {0x05, "R05"}, {0x06, "R06"}, {0x07, "R07"}, {0x08, "R08"}, {0x09, "R09"},
  1449. {0x0A, "R10"}, {0x0B, "R11"}, {0x0C, "R12"}, {0x0D, "SP/R13"}, {0x0E, "LR/R14"},
  1450. {0x0F, "PC/R15"}, {0x10, "xPSR"}, {0x11, "MSP"}, {0x12, "PSP"}, {0x14, "Flags"},
  1451. {0x21, "FPCSR"}, {0x40, "FP S00"}, {0x41, "FP S01"}, {0x42, "FP S02"}, {0x43, "FP S03"},
  1452. {0x44, "FP S04"}, {0x45, "FP S05"}, {0x46, "FP S06"}, {0x47, "FP S07"}, {0x48, "FP S08"},
  1453. {0x49, "FP S09"}, {0x4A, "FP S10"}, {0x4B, "FP S11"}, {0x4C, "FP S12"}, {0x4D, "FP S13"},
  1454. {0x4E, "FP S14"}, {0x4F, "FP S15"}, {0x50, "FP S16"}, {0x51, "FP S17"}, {0x52, "FP S18"},
  1455. {0x53, "FP S19"}, {0x54, "FP S20"}, {0x55, "FP S21"}, {0x56, "FP S22"}, {0x57, "FP S23"},
  1456. {0x58, "FP S24"}, {0x59, "FP S25"}, {0x5A, "FP S26"}, {0x5B, "FP S27"}, {0x5C, "FP S28"},
  1457. {0x5D, "FP S29"}, {0x5E, "FP S30"}, {0x5F, "FP S31"}};
  1458. static bool swd_scriptfunc_core_regs(ScriptContext* ctx) {
  1459. bool succ = false;
  1460. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1461. uint32_t reg_dhcsr = 0;
  1462. uint32_t reg_cpacr = 0;
  1463. swd_read_memory(ctx->app, ctx->selected_ap, SCS_DHCSR, &reg_dhcsr);
  1464. swd_read_memory(ctx->app, ctx->selected_ap, SCS_CPACR, &reg_cpacr);
  1465. /* when FPU is enabled/available, CP10 and CP11 are implemented */
  1466. bool has_fpu = ((reg_cpacr >> 20) & 0x0F) != 0;
  1467. if(!(reg_dhcsr & SCS_DHCSR_S_HALT)) {
  1468. swd_script_log(ctx, FuriLogLevelError, "Core is not in debug state");
  1469. succ = false;
  1470. } else {
  1471. for(size_t pos = 0; pos < COUNT(cpu_regs); pos++) {
  1472. if(!has_fpu && (cpu_regs[pos].regsel >= 0x20)) {
  1473. continue;
  1474. }
  1475. uint32_t core_data = 0;
  1476. succ =
  1477. swd_write_memory(
  1478. ctx->app, ctx->selected_ap, SCS_DCRSR, SCS_DCRSR_RD | cpu_regs[pos].regsel) ==
  1479. 1;
  1480. succ &= swd_read_memory(ctx->app, ctx->selected_ap, SCS_DCRDR, &core_data) == 1;
  1481. if(!succ) {
  1482. swd_script_log(ctx, FuriLogLevelDefault, "%08s ----------", cpu_regs[pos].desc);
  1483. } else {
  1484. swd_script_log(
  1485. ctx, FuriLogLevelDefault, "%06s 0x%08X", cpu_regs[pos].desc, core_data);
  1486. }
  1487. }
  1488. }
  1489. furi_mutex_release(ctx->app->swd_mutex);
  1490. swd_script_seek_newline(ctx);
  1491. return true;
  1492. }
  1493. static bool swd_scriptfunc_core_reg_get(ScriptContext* ctx) {
  1494. uint32_t core_reg = 0;
  1495. uint32_t core_data = 0;
  1496. bool succ = false;
  1497. if(!swd_script_skip_whitespace(ctx)) {
  1498. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1499. return false;
  1500. }
  1501. if(!swd_script_get_number(ctx, &core_reg)) {
  1502. swd_script_log(ctx, FuriLogLevelError, "failed to parse register");
  1503. return false;
  1504. }
  1505. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1506. uint32_t reg_dhcsr = 0;
  1507. uint32_t reg_cpacr = 0;
  1508. swd_read_memory(ctx->app, ctx->selected_ap, SCS_DHCSR, &reg_dhcsr);
  1509. swd_read_memory(ctx->app, ctx->selected_ap, SCS_CPACR, &reg_cpacr);
  1510. /* when FPU is enabled/available, CP10 and CP11 are implemented */
  1511. bool has_fpu = ((reg_cpacr >> 20) & 0x0F) != 0;
  1512. if(!(reg_dhcsr & SCS_DHCSR_S_HALT)) {
  1513. swd_script_log(ctx, FuriLogLevelError, "Core is not in debug state");
  1514. succ = false;
  1515. } else {
  1516. if(!has_fpu && (core_reg >= 0x20)) {
  1517. swd_script_log(ctx, FuriLogLevelError, "Core has no FP extensions");
  1518. succ = false;
  1519. } else {
  1520. succ = swd_write_memory(
  1521. ctx->app, ctx->selected_ap, SCS_DCRSR, SCS_DCRSR_RD | core_reg) == 1;
  1522. succ &= swd_read_memory(ctx->app, ctx->selected_ap, SCS_DCRDR, &core_data) == 1;
  1523. if(!succ) {
  1524. swd_script_log(ctx, FuriLogLevelError, "swd_write_memory failed");
  1525. }
  1526. }
  1527. }
  1528. furi_mutex_release(ctx->app->swd_mutex);
  1529. if(succ) {
  1530. swd_script_log(ctx, FuriLogLevelDefault, "0x%08X", core_data);
  1531. }
  1532. swd_script_seek_newline(ctx);
  1533. return succ;
  1534. }
  1535. static bool swd_scriptfunc_core_reg_set(ScriptContext* ctx) {
  1536. uint32_t core_reg = 0;
  1537. uint32_t core_data = 0;
  1538. bool succ = false;
  1539. if(!swd_script_skip_whitespace(ctx)) {
  1540. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1541. return false;
  1542. }
  1543. if(!swd_script_get_number(ctx, &core_reg)) {
  1544. swd_script_log(ctx, FuriLogLevelError, "failed to parse register");
  1545. return false;
  1546. }
  1547. if(!swd_script_skip_whitespace(ctx)) {
  1548. swd_script_log(ctx, FuriLogLevelError, "missing whitespace");
  1549. return false;
  1550. }
  1551. if(!swd_script_get_number(ctx, &core_data)) {
  1552. swd_script_log(ctx, FuriLogLevelError, "failed to parse data");
  1553. return false;
  1554. }
  1555. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1556. uint32_t reg_dhcsr = 0;
  1557. uint32_t reg_cpacr = 0;
  1558. swd_read_memory(ctx->app, ctx->selected_ap, SCS_DHCSR, &reg_dhcsr);
  1559. swd_read_memory(ctx->app, ctx->selected_ap, SCS_CPACR, &reg_cpacr);
  1560. /* when FPU is enabled/available, CP10 and CP11 are implemented */
  1561. bool has_fpu = ((reg_cpacr >> 20) & 0x0F) != 0;
  1562. if(!(reg_dhcsr & SCS_DHCSR_S_HALT)) {
  1563. swd_script_log(ctx, FuriLogLevelError, "Core is not in debug state");
  1564. succ = false;
  1565. } else {
  1566. if(!has_fpu && (core_reg >= 0x20)) {
  1567. swd_script_log(ctx, FuriLogLevelError, "Core has no FP extensions");
  1568. succ = false;
  1569. } else {
  1570. succ = swd_write_memory(ctx->app, ctx->selected_ap, SCS_DCRDR, core_data) == 1;
  1571. succ &= swd_write_memory(
  1572. ctx->app, ctx->selected_ap, SCS_DCRSR, SCS_DCRSR_WR | core_reg) == 1;
  1573. if(!succ) {
  1574. swd_script_log(ctx, FuriLogLevelError, "swd_write_memory failed");
  1575. }
  1576. }
  1577. }
  1578. furi_mutex_release(ctx->app->swd_mutex);
  1579. swd_script_seek_newline(ctx);
  1580. return succ;
  1581. }
  1582. static bool swd_scriptfunc_core_cpuid(ScriptContext* ctx) {
  1583. bool succ = false;
  1584. uint32_t reg_cpuid = 0;
  1585. furi_mutex_acquire(ctx->app->swd_mutex, FuriWaitForever);
  1586. succ = swd_read_memory(ctx->app, ctx->selected_ap, SCS_CPUID, &reg_cpuid) == 1;
  1587. furi_mutex_release(ctx->app->swd_mutex);
  1588. if(!succ) {
  1589. swd_script_log(ctx, FuriLogLevelError, "swd_read_memory failed");
  1590. } else {
  1591. swd_script_log(ctx, FuriLogLevelDefault, "0x%08X", reg_cpuid);
  1592. }
  1593. swd_script_seek_newline(ctx);
  1594. return succ;
  1595. }
  1596. static const ScriptFunctionInfo script_funcs[] = {
  1597. {"#", &swd_scriptfunc_comment},
  1598. {".label", &swd_scriptfunc_label},
  1599. {"goto", &swd_scriptfunc_goto},
  1600. {"call", &swd_scriptfunc_call},
  1601. {"status", &swd_scriptfunc_status},
  1602. {"errors", &swd_scriptfunc_errors},
  1603. {"message", &swd_scriptfunc_message},
  1604. {"beep", &swd_scriptfunc_beep},
  1605. {"max_tries", &swd_scriptfunc_maxtries},
  1606. {"swd_clock_delay", &swd_scriptfunc_swd_clock_delay},
  1607. {"swd_idle_bits", &swd_scriptfunc_swd_idle_bits},
  1608. {"block_size", &swd_scriptfunc_blocksize},
  1609. {"abort", &swd_scriptfunc_abort},
  1610. {"mem_dump", &swd_scriptfunc_mem_dump},
  1611. {"mem_ldmst", &swd_scriptfunc_mem_ldmst},
  1612. {"mem_write", &swd_scriptfunc_mem_write},
  1613. {"mem_read", &swd_scriptfunc_mem_read},
  1614. {"dp_write", &swd_scriptfunc_dp_write},
  1615. {"dp_read", &swd_scriptfunc_dp_read},
  1616. {"ap_scan", &swd_scriptfunc_apscan},
  1617. {"ap_select", &swd_scriptfunc_apselect},
  1618. {"ap_read", &swd_scriptfunc_ap_read},
  1619. {"ap_write", &swd_scriptfunc_ap_write},
  1620. {"core_halt", &swd_scriptfunc_core_halt},
  1621. {"core_step", &swd_scriptfunc_core_step},
  1622. {"core_continue", &swd_scriptfunc_core_continue},
  1623. {"core_regs", &swd_scriptfunc_core_regs},
  1624. {"core_reg_get", &swd_scriptfunc_core_reg_get},
  1625. {"core_reg_set", &swd_scriptfunc_core_reg_set},
  1626. {"core_cpuid", &swd_scriptfunc_core_cpuid}};
  1627. /************************** script main code **************************/
  1628. static bool swd_execute_script_line(ScriptContext* const ctx) {
  1629. char buffer[64];
  1630. uint64_t start_pos = 0;
  1631. if(ctx->script_file) {
  1632. start_pos = storage_file_tell(ctx->script_file);
  1633. uint16_t ret = storage_file_read(ctx->script_file, buffer, 2);
  1634. storage_file_seek(ctx->script_file, start_pos, true);
  1635. if(ret < 2) {
  1636. return true;
  1637. }
  1638. } else {
  1639. start_pos = ctx->line_pos;
  1640. strncpy(buffer, ctx->line_data, 2);
  1641. if(buffer[0] == 0 || buffer[1] == 0) {
  1642. return true;
  1643. }
  1644. }
  1645. if(buffer[0] == '\n' || (buffer[0] == '\r' && buffer[1] == '\n')) {
  1646. swd_script_seek_newline(ctx);
  1647. return true;
  1648. }
  1649. for(size_t entry = 0; entry < COUNT(script_funcs); entry++) {
  1650. if(ctx->abort) {
  1651. DBGS("aborting");
  1652. break;
  1653. }
  1654. size_t expected = strlen(script_funcs[entry].prefix);
  1655. if(ctx->script_file) {
  1656. storage_file_seek(ctx->script_file, start_pos, true);
  1657. if(storage_file_read(ctx->script_file, buffer, expected) != expected) {
  1658. continue;
  1659. }
  1660. } else {
  1661. ctx->line_pos = start_pos;
  1662. if(strlen(ctx->line_data) < expected) {
  1663. continue;
  1664. }
  1665. strncpy(buffer, ctx->line_data, expected);
  1666. ctx->line_pos += expected;
  1667. }
  1668. buffer[expected] = '\000';
  1669. if(strncmp(buffer, script_funcs[entry].prefix, expected)) {
  1670. continue;
  1671. }
  1672. bool success = true;
  1673. if(ctx->goto_active) {
  1674. DBG("ignore: '%s'", script_funcs[entry].prefix);
  1675. /* only execute label handlers */
  1676. if(buffer[0] == '.') {
  1677. success = script_funcs[entry].func(ctx);
  1678. } else {
  1679. swd_script_seek_newline(ctx);
  1680. }
  1681. } else {
  1682. DBG("command: '%s'", script_funcs[entry].prefix);
  1683. if(!ctx->status_ignore) {
  1684. snprintf(
  1685. ctx->app->state_string,
  1686. sizeof(ctx->app->state_string),
  1687. "CMD: %s",
  1688. script_funcs[entry].prefix);
  1689. }
  1690. swd_script_gui_refresh(ctx);
  1691. /* function, execute */
  1692. success = script_funcs[entry].func(ctx);
  1693. if(!success && !ctx->errors_ignore) {
  1694. swd_script_log(
  1695. ctx, FuriLogLevelError, "Command failed: %s", script_funcs[entry].prefix);
  1696. snprintf(
  1697. ctx->app->state_string,
  1698. sizeof(ctx->app->state_string),
  1699. "Command failed: %s",
  1700. script_funcs[entry].prefix);
  1701. return false;
  1702. }
  1703. }
  1704. return true;
  1705. }
  1706. swd_script_log(ctx, FuriLogLevelError, "unknown command '%s'", buffer);
  1707. return false;
  1708. }
  1709. static bool swd_execute_script(AppFSM* const ctx, const char* filename) {
  1710. bool success = true;
  1711. /* fetch current script and set as parent */
  1712. ScriptContext* parent = ctx->script;
  1713. ctx->script = malloc(sizeof(ScriptContext));
  1714. ctx->script->app = ctx;
  1715. ctx->script->max_tries = 1;
  1716. ctx->script->parent = parent;
  1717. strcpy(ctx->script->filename, filename);
  1718. if(!storage_file_exists(ctx->storage, filename)) {
  1719. DBG("Does not exist '%s'", filename);
  1720. parent = ctx->script->parent;
  1721. free(ctx->script);
  1722. ctx->script = parent;
  1723. return false;
  1724. }
  1725. /* first allocate a file object */
  1726. ctx->script->script_file = storage_file_alloc(ctx->storage);
  1727. /* then get our script opened */
  1728. if(!storage_file_open(ctx->script->script_file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) {
  1729. FURI_LOG_E(TAG, "open, %s", storage_file_get_error_desc(ctx->script->script_file));
  1730. DBG("Failed to open '%s'", filename);
  1731. storage_file_free(ctx->script->script_file);
  1732. parent = ctx->script->parent;
  1733. free(ctx->script);
  1734. ctx->script = parent;
  1735. return false;
  1736. }
  1737. do {
  1738. success = true;
  1739. ctx->script->restart = false;
  1740. storage_file_seek(ctx->script->script_file, 0, true);
  1741. uint32_t line = 1;
  1742. while(line < SCRIPT_MAX_LINES) {
  1743. if(ctx->script->abort) {
  1744. DBGS("Abort requested");
  1745. break;
  1746. }
  1747. if(storage_file_eof(ctx->script->script_file)) {
  1748. break;
  1749. }
  1750. DBG("line %lu", line);
  1751. if(!swd_execute_script_line(ctx->script)) {
  1752. success = false;
  1753. break;
  1754. }
  1755. if(ctx->script->restart) {
  1756. break;
  1757. }
  1758. line++;
  1759. }
  1760. if(ctx->script->restart) {
  1761. DBGS("Restarting");
  1762. } else {
  1763. DBGS("Finished");
  1764. }
  1765. if(line >= SCRIPT_MAX_LINES) {
  1766. success = true;
  1767. char text_buf[128];
  1768. snprintf(text_buf, sizeof(text_buf), "aborting after %d lines", SCRIPT_MAX_LINES);
  1769. DialogMessage* message = dialog_message_alloc();
  1770. dialog_message_set_header(message, "SWD Probe", 16, 2, AlignLeft, AlignTop);
  1771. dialog_message_set_icon(message, &I_app, 3, 2);
  1772. dialog_message_set_text(message, text_buf, 3, 16, AlignLeft, AlignTop);
  1773. dialog_message_set_buttons(message, "Back", NULL, NULL);
  1774. dialog_message_free(message);
  1775. ctx->script->restart = false;
  1776. }
  1777. if(!success) {
  1778. char text_buf[128];
  1779. snprintf(text_buf, sizeof(text_buf), "Line %lu failed:\n%s", line, ctx->state_string);
  1780. DialogMessage* message = dialog_message_alloc();
  1781. dialog_message_set_header(message, "SWD Probe", 16, 2, AlignLeft, AlignTop);
  1782. dialog_message_set_icon(message, &I_app, 3, 2);
  1783. dialog_message_set_text(message, text_buf, 3, 16, AlignLeft, AlignTop);
  1784. dialog_message_set_buttons(message, "Back", "Retry", NULL);
  1785. if(dialog_message_show(ctx->dialogs, message) == DialogMessageButtonCenter) {
  1786. ctx->script->restart = true;
  1787. }
  1788. dialog_message_free(message);
  1789. }
  1790. } while(ctx->script->restart);
  1791. storage_file_close(ctx->script->script_file);
  1792. storage_file_free(ctx->script->script_file);
  1793. parent = ctx->script->parent;
  1794. free(ctx->script);
  1795. ctx->script = parent;
  1796. return success;
  1797. }
  1798. /************************** UI functions **************************/
  1799. #define CANVAS_WIDTH 128
  1800. #define CANVAS_HEIGHT 64
  1801. #define COERCE(d, min, max) \
  1802. do { \
  1803. if(d < (min)) { \
  1804. d = (min); \
  1805. } \
  1806. if(d > (max)) { \
  1807. d = (max); \
  1808. } \
  1809. } while(0)
  1810. #define COERCE_COORDS(x1, y1, x2, y2) \
  1811. do { \
  1812. COERCE(x1, 0, CANVAS_WIDTH); \
  1813. COERCE(x2, 0, CANVAS_WIDTH); \
  1814. COERCE(y1, 0, CANVAS_HEIGHT); \
  1815. COERCE(y1, 0, CANVAS_HEIGHT); \
  1816. } while(0)
  1817. #include "model/model_chip.h"
  1818. static int rotatedVertexCoords[NUM_VERTICES][3];
  1819. static void draw_model(Canvas* const canvas) {
  1820. static float xAngle = 0;
  1821. static float yAngle = 0;
  1822. static float zAngle = 0;
  1823. static float zoom = 0;
  1824. static float speed = 0.6f;
  1825. float cosXAngle = cosf(xAngle);
  1826. float sinXAngle = sinf(xAngle);
  1827. float cosYAngle = cosf(yAngle);
  1828. float sinYAngle = sinf(yAngle);
  1829. float cosZAngle = cosf(zAngle);
  1830. float sinZAngle = sinf(zAngle);
  1831. float sinZoom = 1.2f + sinf(zoom) * 0.25f;
  1832. int centerX = CANVAS_WIDTH / 2;
  1833. int centerY = CANVAS_HEIGHT / 2 + 5;
  1834. for(int i = 0; i < NUM_VERTICES; i++) {
  1835. int x = vertexCoords[i][0] * sinZoom * 16;
  1836. int y = vertexCoords[i][1] * sinZoom * 16;
  1837. int z = vertexCoords[i][2] * sinZoom * 16;
  1838. int y1 = y * cosXAngle - z * sinXAngle;
  1839. int z1 = y * sinXAngle + z * cosXAngle;
  1840. int x2 = x * cosYAngle + z1 * sinYAngle;
  1841. int z2 = -x * sinYAngle + z1 * cosYAngle;
  1842. int x3 = x2 * cosZAngle - y1 * sinZAngle;
  1843. int y3 = x2 * sinZAngle + y1 * cosZAngle;
  1844. rotatedVertexCoords[i][0] = x3 + centerX;
  1845. rotatedVertexCoords[i][1] = y3 + centerY;
  1846. rotatedVertexCoords[i][2] = z2;
  1847. }
  1848. for(size_t i = 0; i < COUNT(edgeIndices); i++) {
  1849. int v1Index = edgeIndices[i][0];
  1850. int v2Index = edgeIndices[i][1];
  1851. int x1 = rotatedVertexCoords[v1Index][0];
  1852. int y1 = rotatedVertexCoords[v1Index][1];
  1853. int x2 = rotatedVertexCoords[v2Index][0];
  1854. int y2 = rotatedVertexCoords[v2Index][1];
  1855. COERCE_COORDS(x1, y1, x2, y2);
  1856. canvas_draw_line(canvas, x1, y1, x2, y2);
  1857. }
  1858. xAngle += speed * 0.02 / sinZoom;
  1859. yAngle += speed * 0.023 / sinZoom;
  1860. zAngle += speed * 0.029 * sinZoom;
  1861. zoom += speed * 0.005;
  1862. }
  1863. static void render_callback(Canvas* const canvas, void* ctx_in) {
  1864. furi_assert(canvas);
  1865. furi_assert(ctx_in);
  1866. AppFSM* ctx = ctx_in;
  1867. furi_mutex_acquire(ctx->gui_mutex, FuriWaitForever);
  1868. char buffer[64];
  1869. int y = 10;
  1870. canvas_draw_frame(canvas, 0, 0, 128, 64);
  1871. canvas_set_font(canvas, FontPrimary);
  1872. if(!ctx->detected_device) {
  1873. ctx->mode_page = ModePageScan;
  1874. } else if(ctx->mode_page == ModePageScan) {
  1875. ctx->mode_page = ModePageFound;
  1876. }
  1877. /* if seen less than a quarter second ago */
  1878. switch(ctx->mode_page) {
  1879. case ModePageScan: {
  1880. draw_model(canvas);
  1881. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Searching");
  1882. y += 14;
  1883. canvas_set_font(canvas, FontSecondary);
  1884. bool info_page = (ctx->loop_count % 500) >= 250;
  1885. if(info_page) {
  1886. canvas_draw_str(canvas, 2, y, "Connect GND with target GND");
  1887. y += 10;
  1888. canvas_draw_str(canvas, 2, y, "and any two GPIOs with pads");
  1889. y += 10;
  1890. canvas_draw_str(canvas, 2, y, "you want to check for SWD");
  1891. canvas_set_font(canvas, FontPrimary);
  1892. canvas_draw_str(canvas, 111, 62, "2/2");
  1893. } else {
  1894. const char* filename = "<none>";
  1895. if(strlen(ctx->script_detected) > 0) {
  1896. const char* slash = strrchr(ctx->script_detected, '/');
  1897. if(slash) {
  1898. filename = &slash[1];
  1899. } else {
  1900. filename = ctx->script_detected;
  1901. }
  1902. }
  1903. canvas_set_font(canvas, FontSecondary);
  1904. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Autoexec Script");
  1905. y += 10;
  1906. canvas_set_font(canvas, FontKeyboard);
  1907. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, filename);
  1908. y += 16;
  1909. canvas_set_font(canvas, FontSecondary);
  1910. canvas_draw_icon(canvas, 14, y - 5, &I_ButtonUp_7x4);
  1911. canvas_draw_icon(canvas, 78, y - 5, &I_ButtonDown_7x4);
  1912. canvas_draw_str(canvas, 23, y, "Clear");
  1913. canvas_draw_str(canvas, 87, y, "Choose");
  1914. canvas_set_font(canvas, FontPrimary);
  1915. canvas_draw_str(canvas, 111, 62, "1/2");
  1916. }
  1917. canvas_set_font(canvas, FontSecondary);
  1918. elements_button_left(canvas, "Script");
  1919. break;
  1920. }
  1921. case ModePageFound: {
  1922. if((ctx->detected_timeout + TIMER_HZ / 4) >= TIMER_HZ * TIMEOUT) {
  1923. snprintf(buffer, sizeof(buffer), "FOUND!");
  1924. } else {
  1925. /* if it was seen more than a quarter second ago, show countdown */
  1926. snprintf(
  1927. buffer, sizeof(buffer), "FOUND! (%lus)", (ctx->detected_timeout / TIMER_HZ) + 1);
  1928. }
  1929. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, buffer);
  1930. y += 10;
  1931. canvas_set_font(canvas, FontKeyboard);
  1932. snprintf(
  1933. buffer,
  1934. sizeof(buffer),
  1935. "SWC/SWD: %s/%s",
  1936. gpio_name(ctx->io_swc),
  1937. gpio_name(ctx->io_swd));
  1938. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1939. y += 10;
  1940. snprintf(buffer, sizeof(buffer), "DPIDR 0x%08lX", ctx->dp_regs.dpidr);
  1941. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1942. y += 10;
  1943. snprintf(
  1944. buffer,
  1945. sizeof(buffer),
  1946. "Part %02X Rev %X DAPv%d",
  1947. ctx->dpidr_info.partno,
  1948. ctx->dpidr_info.revision,
  1949. ctx->dpidr_info.version);
  1950. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1951. y += 10;
  1952. canvas_set_font(canvas, FontSecondary);
  1953. snprintf(buffer, sizeof(buffer), "%s", jep106_manufacturer(ctx->dpidr_info.designer));
  1954. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1955. y += 10;
  1956. canvas_set_font(canvas, FontSecondary);
  1957. elements_button_left(canvas, "Script");
  1958. elements_button_right(canvas, "DP Regs");
  1959. break;
  1960. }
  1961. case ModePageDPRegs: {
  1962. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "DP Registers");
  1963. y += 10;
  1964. canvas_set_font(canvas, FontKeyboard);
  1965. if(ctx->dp_regs.dpidr_ok) {
  1966. snprintf(buffer, sizeof(buffer), "DPIDR %08lX", ctx->dp_regs.dpidr);
  1967. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1968. }
  1969. y += 10;
  1970. if(ctx->dp_regs.ctrlstat_ok) {
  1971. snprintf(buffer, sizeof(buffer), "CTRL %08lX", ctx->dp_regs.ctrlstat);
  1972. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1973. }
  1974. y += 10;
  1975. if(ctx->dp_regs.targetid_ok) {
  1976. snprintf(buffer, sizeof(buffer), "TGTID %08lX", ctx->dp_regs.targetid);
  1977. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1978. }
  1979. y += 10;
  1980. if(ctx->dp_regs.eventstat_ok) {
  1981. snprintf(buffer, sizeof(buffer), "EVTST %08lX", ctx->dp_regs.eventstat);
  1982. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1983. }
  1984. y += 10;
  1985. canvas_set_font(canvas, FontSecondary);
  1986. elements_button_left(canvas, "Scan");
  1987. elements_button_right(canvas, "DPID");
  1988. break;
  1989. }
  1990. case ModePageDPID: {
  1991. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "DP ID Register");
  1992. y += 10;
  1993. canvas_set_font(canvas, FontKeyboard);
  1994. if(ctx->dpidr_info.version != 2) {
  1995. snprintf(buffer, sizeof(buffer), "TARGETID not supported");
  1996. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  1997. y += 10;
  1998. } else {
  1999. if(ctx->dp_regs.targetid_ok) {
  2000. snprintf(buffer, sizeof(buffer), "TGTID %08lX", ctx->dp_regs.targetid);
  2001. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2002. y += 10;
  2003. snprintf(buffer, sizeof(buffer), "Part No. %04X", ctx->targetid_info.partno);
  2004. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2005. y += 10;
  2006. snprintf(
  2007. buffer, sizeof(buffer), "%s", jep106_manufacturer(ctx->targetid_info.designer));
  2008. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2009. y += 10;
  2010. }
  2011. }
  2012. canvas_set_font(canvas, FontSecondary);
  2013. elements_button_left(canvas, "DP Regs");
  2014. elements_button_right(canvas, "APs");
  2015. break;
  2016. }
  2017. case ModePageAPID: {
  2018. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "AP Menu");
  2019. y += 10;
  2020. canvas_set_font(canvas, FontKeyboard);
  2021. char state = ' ';
  2022. if(ctx->ap_pos >= ctx->ap_scanned && ctx->ap_pos <= ctx->ap_scanned + 10) {
  2023. state = '*';
  2024. }
  2025. if(!ctx->apidr_info[ctx->ap_pos].ok) {
  2026. snprintf(buffer, sizeof(buffer), "[%d]%c<none>", ctx->ap_pos, state);
  2027. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2028. y += 10;
  2029. if(ctx->ap_pos == 0) {
  2030. for(size_t pos = 0; pos < COUNT(ctx->apidr_info); pos++) {
  2031. if(ctx->apidr_info[pos].ok) {
  2032. ctx->ap_pos = pos;
  2033. }
  2034. }
  2035. }
  2036. } else {
  2037. const char* class = "";
  2038. switch(ctx->apidr_info[ctx->ap_pos].class) {
  2039. case 0:
  2040. class = "und";
  2041. break;
  2042. case 1:
  2043. class = "COM";
  2044. break;
  2045. case 8:
  2046. class = "MEM";
  2047. break;
  2048. default:
  2049. class = "unk";
  2050. break;
  2051. }
  2052. const char* types[] = {
  2053. "COM-AP",
  2054. "AHB3",
  2055. "APB2 or APB3",
  2056. "Type unknown",
  2057. "AXI3 or AXI4",
  2058. "AHB5",
  2059. "APB4 and APB5",
  2060. "AXI5",
  2061. "AHB5 enh.",
  2062. };
  2063. const char* type = "Type unk";
  2064. if(ctx->apidr_info[ctx->ap_pos].type < COUNT(types)) {
  2065. type = types[ctx->apidr_info[ctx->ap_pos].type];
  2066. }
  2067. snprintf(buffer, sizeof(buffer), "[%d]%c%s, %s", ctx->ap_pos, state, class, type);
  2068. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2069. y += 10;
  2070. snprintf(buffer, sizeof(buffer), "Base 0x%08lX", ctx->apidr_info[ctx->ap_pos].base);
  2071. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2072. y += 10;
  2073. snprintf(
  2074. buffer,
  2075. sizeof(buffer),
  2076. "Rev %d Var %d",
  2077. ctx->apidr_info[ctx->ap_pos].revision,
  2078. ctx->apidr_info[ctx->ap_pos].variant);
  2079. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2080. y += 10;
  2081. snprintf(
  2082. buffer,
  2083. sizeof(buffer),
  2084. "%s",
  2085. jep106_manufacturer(ctx->apidr_info[ctx->ap_pos].designer));
  2086. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2087. y += 10;
  2088. elements_button_center(canvas, "Show");
  2089. }
  2090. canvas_set_font(canvas, FontSecondary);
  2091. elements_button_left(canvas, "DPID");
  2092. elements_button_right(canvas, "CoreS.");
  2093. elements_scrollbar_pos(canvas, 4, 10, 40, ctx->ap_pos / 32, COUNT(ctx->apidr_info) / 32);
  2094. break;
  2095. }
  2096. /* hex dump view */
  2097. case ModePageHexDump: {
  2098. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Hex dump");
  2099. y += 10;
  2100. canvas_set_font(canvas, FontKeyboard);
  2101. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, "Addr:");
  2102. snprintf(buffer, sizeof(buffer), "%08lX", ctx->hex_addr);
  2103. canvas_draw_str_aligned(canvas, 38, y, AlignLeft, AlignBottom, buffer);
  2104. uint32_t font_width = canvas_glyph_width(canvas, '0');
  2105. uint32_t x = 37 + (7 - ctx->hex_select) * font_width;
  2106. /* draw selection */
  2107. canvas_draw_line(canvas, x, y + 1, x + font_width, y + 1);
  2108. y += 10;
  2109. uint32_t byte_num = 0;
  2110. for(int line = 0; line < 4; line++) {
  2111. uint32_t x_pos = 5;
  2112. for(int byte_pos = 0; byte_pos < 8; byte_pos++) {
  2113. if(ctx->hex_buffer_valid[byte_num / 4]) {
  2114. snprintf(buffer, sizeof(buffer), "%02X", ctx->hex_buffer[byte_num]);
  2115. } else {
  2116. snprintf(buffer, sizeof(buffer), "--");
  2117. }
  2118. byte_num++;
  2119. canvas_draw_str_aligned(canvas, x_pos, y, AlignLeft, AlignBottom, buffer);
  2120. x_pos += font_width * 2 + font_width / 2;
  2121. }
  2122. y += 10;
  2123. }
  2124. break;
  2125. }
  2126. case ModePageCoresight: {
  2127. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Coresight");
  2128. y += 10;
  2129. canvas_set_font(canvas, FontSecondary);
  2130. uint32_t base = ctx->coresight_bases[ctx->coresight_level];
  2131. uint32_t base_next = adi_romtable_get(ctx, base, ctx->coresight_pos[ctx->coresight_level]);
  2132. snprintf(buffer, sizeof(buffer), "Base: %08lX", base);
  2133. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2134. y += 10;
  2135. snprintf(buffer, sizeof(buffer), "Type: %s", adi_romtable_type(ctx, base));
  2136. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2137. y += 10;
  2138. snprintf(buffer, sizeof(buffer), "Full: %s", adi_romtable_full(ctx, base));
  2139. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2140. y += 10;
  2141. if(adi_is_romtable(ctx, base)) {
  2142. snprintf(
  2143. buffer,
  2144. sizeof(buffer),
  2145. "[%lu/%lu] -> %08lX",
  2146. ctx->coresight_pos[ctx->coresight_level] + 1,
  2147. ctx->coresight_count[ctx->coresight_level],
  2148. base_next);
  2149. canvas_draw_str_aligned(canvas, 5, y, AlignLeft, AlignBottom, buffer);
  2150. canvas_set_font(canvas, FontSecondary);
  2151. elements_button_center(canvas, "Enter");
  2152. }
  2153. y += 10;
  2154. canvas_set_font(canvas, FontSecondary);
  2155. if(ctx->coresight_level) {
  2156. elements_button_left(canvas, "Prev");
  2157. } else {
  2158. elements_button_left(canvas, "APs");
  2159. }
  2160. elements_scrollbar_pos(
  2161. canvas,
  2162. 4,
  2163. 10,
  2164. 40,
  2165. ctx->coresight_pos[ctx->coresight_level],
  2166. ctx->coresight_count[ctx->coresight_level]);
  2167. break;
  2168. }
  2169. /* hex dump view */
  2170. case ModePageScript: {
  2171. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, "Script");
  2172. y += 10;
  2173. y += 10;
  2174. canvas_draw_str_aligned(canvas, 10, y, AlignLeft, AlignBottom, "Status:");
  2175. y += 10;
  2176. canvas_set_font(canvas, FontKeyboard);
  2177. canvas_draw_str_aligned(canvas, 64, y, AlignCenter, AlignBottom, ctx->state_string);
  2178. y += 10;
  2179. break;
  2180. }
  2181. }
  2182. furi_mutex_release(ctx->gui_mutex);
  2183. }
  2184. static void input_callback(InputEvent* input_event, void* ctx_in) {
  2185. furi_assert(input_event);
  2186. furi_assert(ctx_in);
  2187. AppFSM* ctx = ctx_in;
  2188. int entries = furi_message_queue_get_count(ctx->event_queue);
  2189. /* better skip than sorry */
  2190. if(entries < QUEUE_SIZE) {
  2191. AppEvent event = {.type = EventKeyPress, .input = *input_event};
  2192. furi_message_queue_put(ctx->event_queue, &event, 0);
  2193. }
  2194. }
  2195. static void app_init(AppFSM* const app) {
  2196. furi_assert(app);
  2197. app->loop_count = 0;
  2198. app->current_mask_id = 0;
  2199. app->current_mask = gpio_direction_mask[app->current_mask_id];
  2200. app->io_swd = 0xFF;
  2201. app->io_swc = 0xFF;
  2202. app->hex_addr = 0x40002800;
  2203. app->hex_addr = 0xE000EDF0;
  2204. app->swd_clock_delay = CLOCK_DELAY;
  2205. app->swd_idle_bits = IDLE_BITS;
  2206. strcpy(app->state_string, "none");
  2207. strcpy(app->script_detected, "");
  2208. }
  2209. static void app_deinit(AppFSM* const app) {
  2210. furi_assert(app);
  2211. strcpy(app->state_string, "exiting");
  2212. }
  2213. static void swd_main_loop(AppFSM* ctx) {
  2214. furi_assert(ctx);
  2215. ctx->loop_count++;
  2216. switch(ctx->mode_page) {
  2217. case ModePageScan:
  2218. case ModePageFound: {
  2219. /* reset after timeout */
  2220. if(ctx->detected_timeout > 0) {
  2221. ctx->detected_timeout--;
  2222. } else {
  2223. DBGS("Reset detected flag");
  2224. ctx->detected_device = false;
  2225. ctx->io_swd = 0xFF;
  2226. ctx->io_swc = 0xFF;
  2227. ctx->io_num_swd = 0xFF;
  2228. ctx->io_num_swc = 0xFF;
  2229. ctx->ap_scanned = 0;
  2230. memset(&ctx->dp_regs, 0x00, sizeof(ctx->dp_regs));
  2231. memset(&ctx->targetid_info, 0x00, sizeof(ctx->targetid_info));
  2232. memset(&ctx->apidr_info, 0x00, sizeof(ctx->apidr_info));
  2233. ctx->script_detected_executed = false;
  2234. }
  2235. ctx->detected = false;
  2236. ctx->current_mask = gpio_direction_mask[ctx->current_mask_id];
  2237. /* when SWD was already detected, set it to data pin regardless of the mask */
  2238. if(ctx->detected_device) {
  2239. ctx->current_mask &= ~ctx->io_swd;
  2240. }
  2241. /* do the scan */
  2242. furi_mutex_acquire(ctx->swd_mutex, FuriWaitForever);
  2243. swd_scan(ctx);
  2244. furi_mutex_release(ctx->swd_mutex);
  2245. /* now when detected a device, set the timeout */
  2246. if(ctx->detected) {
  2247. DBGS("Set detected flag");
  2248. ctx->detected_device = true;
  2249. ctx->detected_timeout = TIMER_HZ * TIMEOUT;
  2250. /* update DPIDR fields */
  2251. ctx->dpidr_info.revision = (ctx->dp_regs.dpidr >> 28) & 0x0F;
  2252. ctx->dpidr_info.partno = (ctx->dp_regs.dpidr >> 20) & 0xFF;
  2253. ctx->dpidr_info.version = (ctx->dp_regs.dpidr >> 12) & 0x0F;
  2254. ctx->dpidr_info.designer = (ctx->dp_regs.dpidr >> 1) & 0x3FF;
  2255. if(!has_multiple_bits(ctx->io_swc)) {
  2256. DBGS(" - Detected pins");
  2257. DBGS(" - Resetting error");
  2258. furi_mutex_acquire(ctx->swd_mutex, FuriWaitForever);
  2259. /* reset error */
  2260. /* first make sure we have the correct bank by invalidating the current select cache */
  2261. ctx->dp_regs.select_ok = false;
  2262. uint8_t ack =
  2263. swd_read_dpbank(ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat);
  2264. if(ack != 1 || (ctx->dp_regs.ctrlstat & STAT_ERROR_FLAGS)) {
  2265. DBGS(" - send ABORT");
  2266. swd_abort(ctx);
  2267. }
  2268. DBGS(" - Fetch CTRL/STAT");
  2269. ctx->dp_regs.ctrlstat_ok =
  2270. swd_read_dpbank(
  2271. ctx, REG_CTRLSTAT, REG_CTRLSTAT_BANK, &ctx->dp_regs.ctrlstat) == 1;
  2272. DBG(" %08lX %s",
  2273. ctx->dp_regs.ctrlstat,
  2274. ctx->dp_regs.ctrlstat_ok ? "OK" : "FAIL");
  2275. if(ctx->dpidr_info.version >= 1) {
  2276. DBGS(" - DAPv1, read DLCR");
  2277. ctx->dp_regs.dlcr_ok =
  2278. swd_read_dpbank(ctx, REG_DLCR, REG_DLCR_BANK, &ctx->dp_regs.dlcr) == 1;
  2279. DBG(" %08lX %s", ctx->dp_regs.dlcr, ctx->dp_regs.dlcr_ok ? "OK" : "FAIL");
  2280. }
  2281. if(ctx->dpidr_info.version >= 2) {
  2282. DBGS(" - DAPv2, read TARGETID");
  2283. ctx->dp_regs.targetid_ok =
  2284. swd_read_dpbank(
  2285. ctx, REG_TARGETID, REG_TARGETID_BANK, &ctx->dp_regs.targetid) == 1;
  2286. DBG(" %08lX %s",
  2287. ctx->dp_regs.targetid,
  2288. ctx->dp_regs.targetid_ok ? "OK" : "FAIL");
  2289. DBGS(" - DAPv2, read EVENTSTAT");
  2290. ctx->dp_regs.eventstat_ok =
  2291. swd_read_dpbank(
  2292. ctx, REG_EVENTSTAT, REG_EVENTSTAT_BANK, &ctx->dp_regs.eventstat) == 1;
  2293. DBG(" %08lX %s",
  2294. ctx->dp_regs.eventstat,
  2295. ctx->dp_regs.eventstat_ok ? "OK" : "FAIL");
  2296. DBGS(" - DAPv2, read DLPIDR");
  2297. ctx->dp_regs.dlpidr_ok =
  2298. swd_read_dpbank(ctx, REG_DLPIDR, REG_DLPIDR_BANK, &ctx->dp_regs.dlpidr) ==
  2299. 1;
  2300. DBG(" %08lX %s",
  2301. ctx->dp_regs.dlpidr,
  2302. ctx->dp_regs.dlpidr_ok ? "OK" : "FAIL");
  2303. }
  2304. if(ctx->dp_regs.targetid_ok) {
  2305. ctx->targetid_info.revision = (ctx->dp_regs.targetid >> 28) & 0x0F;
  2306. ctx->targetid_info.partno = (ctx->dp_regs.targetid >> 12) & 0xFFFF;
  2307. ctx->targetid_info.designer = (ctx->dp_regs.targetid >> 1) & 0x3FF;
  2308. }
  2309. if(!ctx->script_detected_executed && strlen(ctx->script_detected) > 0) {
  2310. DBG(" - Run script '%s'", ctx->script_detected);
  2311. ctx->script_detected_executed = true;
  2312. ctx->mode_page = ModePageScript;
  2313. swd_execute_script(ctx, ctx->script_detected);
  2314. ctx->mode_page = ModePageFound;
  2315. }
  2316. furi_mutex_release(ctx->swd_mutex);
  2317. }
  2318. } else {
  2319. if(!has_multiple_bits(ctx->io_swc)) {
  2320. DBGS(" - Lost device");
  2321. }
  2322. }
  2323. ctx->current_mask_id = (ctx->current_mask_id + 1) % COUNT(gpio_direction_mask);
  2324. break;
  2325. }
  2326. case ModePageDPRegs:
  2327. case ModePageAPID: {
  2328. furi_mutex_acquire(ctx->swd_mutex, FuriWaitForever);
  2329. /* set debug enable request */
  2330. if(!swd_ensure_powerup(ctx)) {
  2331. furi_mutex_release(ctx->swd_mutex);
  2332. break;
  2333. }
  2334. /* only scan a few APs at once to stay responsive */
  2335. for(int pos = 0; pos < 8; pos++) {
  2336. if(ctx->ap_scanned == 0) {
  2337. swd_apscan_reset(ctx);
  2338. }
  2339. uint8_t ap = ctx->ap_scanned++;
  2340. if(ctx->apidr_info[ap].tested) {
  2341. continue;
  2342. }
  2343. if(swd_apscan_test(ctx, ap)) {
  2344. break;
  2345. }
  2346. }
  2347. furi_mutex_release(ctx->swd_mutex);
  2348. break;
  2349. }
  2350. case ModePageHexDump: {
  2351. furi_mutex_acquire(ctx->swd_mutex, FuriWaitForever);
  2352. for(size_t byte_pos = 0; byte_pos < sizeof(ctx->hex_buffer); byte_pos += 4) {
  2353. uint32_t* data = (uint32_t*)&ctx->hex_buffer[byte_pos];
  2354. bool ret = swd_read_memory(ctx, ctx->ap_pos, ctx->hex_addr + byte_pos, data) == 1;
  2355. ctx->hex_buffer_valid[byte_pos / 4] = ret;
  2356. if(!ret) {
  2357. swd_abort_simple(ctx);
  2358. }
  2359. }
  2360. furi_mutex_release(ctx->swd_mutex);
  2361. break;
  2362. }
  2363. case ModePageDPID:
  2364. case ModePageCoresight:
  2365. furi_delay_ms(50);
  2366. break;
  2367. }
  2368. }
  2369. static bool swd_message_process(AppFSM* ctx) {
  2370. bool processing = true;
  2371. AppEvent event;
  2372. /* wait to make sure the OS can do its stuff */
  2373. FuriStatus event_status = furi_message_queue_get(ctx->event_queue, &event, 1000 / TIMER_HZ);
  2374. if(event_status != FuriStatusOk) {
  2375. return processing;
  2376. }
  2377. if(event.type == EventKeyPress) {
  2378. if(event.input.type == InputTypePress) {
  2379. switch(event.input.key) {
  2380. case InputKeyUp:
  2381. switch(ctx->mode_page) {
  2382. default:
  2383. break;
  2384. case ModePageScan:
  2385. case ModePageFound: {
  2386. strcpy(ctx->script_detected, "");
  2387. break;
  2388. }
  2389. case ModePageAPID:
  2390. if(ctx->ap_pos > 0) {
  2391. ctx->ap_pos--;
  2392. }
  2393. break;
  2394. case ModePageHexDump: {
  2395. ctx->hex_addr += ((ctx->hex_select) ? 1 : 8) * (1 << (4 * ctx->hex_select));
  2396. break;
  2397. }
  2398. case ModePageCoresight: {
  2399. if(ctx->coresight_pos[ctx->coresight_level] > 0) {
  2400. ctx->coresight_pos[ctx->coresight_level]--;
  2401. }
  2402. break;
  2403. }
  2404. }
  2405. break;
  2406. case InputKeyDown: {
  2407. switch(ctx->mode_page) {
  2408. default:
  2409. break;
  2410. case ModePageScan: {
  2411. FuriString* result_path = furi_string_alloc_printf(ANY_PATH("swd_scripts"));
  2412. FuriString* preselected = furi_string_alloc_printf(
  2413. (strlen(ctx->script_detected) > 0) ? ctx->script_detected :
  2414. ANY_PATH("swd_scripts"));
  2415. DialogsFileBrowserOptions options;
  2416. dialog_file_browser_set_basic_options(&options, "swd", &I_swd);
  2417. if(dialog_file_browser_show(ctx->dialogs, result_path, preselected, &options)) {
  2418. const char* path = furi_string_get_cstr(result_path);
  2419. strcpy(ctx->script_detected, path);
  2420. }
  2421. furi_string_free(result_path);
  2422. furi_string_free(preselected);
  2423. break;
  2424. }
  2425. case ModePageAPID:
  2426. if(ctx->ap_pos + 1U < COUNT(ctx->apidr_info)) {
  2427. ctx->ap_pos++;
  2428. }
  2429. break;
  2430. case ModePageHexDump: {
  2431. ctx->hex_addr -= ((ctx->hex_select) ? 1 : 8) * (1 << (4 * ctx->hex_select));
  2432. break;
  2433. }
  2434. case ModePageCoresight: {
  2435. if(ctx->coresight_pos[ctx->coresight_level] + 1 <
  2436. ctx->coresight_count[ctx->coresight_level]) {
  2437. ctx->coresight_pos[ctx->coresight_level]++;
  2438. }
  2439. break;
  2440. }
  2441. }
  2442. break;
  2443. }
  2444. case InputKeyRight:
  2445. if(ctx->mode_page == ModePageHexDump) {
  2446. if(ctx->hex_select > 0) {
  2447. ctx->hex_select--;
  2448. }
  2449. } else if(ctx->mode_page == ModePageAPID && ctx->apidr_info[ctx->ap_pos].ok) {
  2450. ctx->mode_page = ModePageCoresight;
  2451. uint32_t base = ctx->apidr_info[ctx->ap_pos].base & 0xFFFFF000;
  2452. ctx->coresight_level = 0;
  2453. ctx->coresight_bases[ctx->coresight_level] = base;
  2454. ctx->coresight_pos[ctx->coresight_level] = 0;
  2455. ctx->coresight_count[ctx->coresight_level] =
  2456. adi_romtable_entry_count(ctx, base);
  2457. } else if(ctx->detected) {
  2458. if(ctx->mode_page + 1 < ModePageCount) {
  2459. ctx->mode_page++;
  2460. }
  2461. }
  2462. break;
  2463. case InputKeyLeft:
  2464. if(ctx->mode_page == ModePageHexDump) {
  2465. if(ctx->hex_select < 7) {
  2466. ctx->hex_select++;
  2467. }
  2468. } else if(ctx->mode_page == ModePageCoresight) {
  2469. if(ctx->coresight_level > 0) {
  2470. ctx->coresight_level--;
  2471. } else {
  2472. ctx->mode_page = ModePageAPID;
  2473. }
  2474. } else if((ctx->mode_page == ModePageScan) || (ctx->mode_page == ModePageFound)) {
  2475. uint32_t mode_page = ctx->mode_page;
  2476. FuriString* result_path = furi_string_alloc_printf(ANY_PATH("swd_scripts"));
  2477. FuriString* preselected = furi_string_alloc_printf(
  2478. (strlen(ctx->script_detected) > 0) ? ctx->script_detected :
  2479. ANY_PATH("swd_scripts"));
  2480. DialogsFileBrowserOptions options;
  2481. dialog_file_browser_set_basic_options(&options, "swd", &I_swd);
  2482. if(dialog_file_browser_show(ctx->dialogs, result_path, preselected, &options)) {
  2483. const char* path = furi_string_get_cstr(result_path);
  2484. ctx->mode_page = ModePageScript;
  2485. swd_execute_script(ctx, path);
  2486. ctx->mode_page = mode_page;
  2487. }
  2488. furi_string_free(result_path);
  2489. furi_string_free(preselected);
  2490. break;
  2491. } else {
  2492. if(ctx->mode_page > 0) {
  2493. ctx->mode_page--;
  2494. }
  2495. }
  2496. break;
  2497. case InputKeyOk:
  2498. if(ctx->mode_page == ModePageAPID && ctx->apidr_info[ctx->ap_pos].ok) {
  2499. ctx->mode_page = ModePageHexDump;
  2500. } else if(ctx->mode_page == ModePageCoresight) {
  2501. uint32_t base = ctx->coresight_bases[ctx->coresight_level];
  2502. if(!adi_is_romtable(ctx, base)) {
  2503. break;
  2504. }
  2505. uint32_t cur_pos = ctx->coresight_pos[ctx->coresight_level];
  2506. uint32_t base_next = adi_romtable_get(ctx, base, cur_pos);
  2507. uint32_t new_count = adi_romtable_entry_count(ctx, base_next);
  2508. ctx->coresight_level++;
  2509. ctx->coresight_pos[ctx->coresight_level] = 0;
  2510. ctx->coresight_count[ctx->coresight_level] = new_count;
  2511. ctx->coresight_bases[ctx->coresight_level] = base_next;
  2512. }
  2513. break;
  2514. case InputKeyBack:
  2515. if(ctx->mode_page == ModePageHexDump) {
  2516. ctx->mode_page = ModePageAPID;
  2517. } else if(ctx->mode_page == ModePageScript) {
  2518. ctx->script->abort = true;
  2519. } else if(ctx->mode_page > ModePageFound) {
  2520. ctx->mode_page = ModePageScan;
  2521. } else if(ctx->mode_page == ModePageScan) {
  2522. processing = false;
  2523. } else if(ctx->mode_page == ModePageFound) {
  2524. processing = false;
  2525. }
  2526. break;
  2527. default:
  2528. break;
  2529. }
  2530. }
  2531. }
  2532. return processing;
  2533. }
  2534. size_t data_received(void* ctx, uint8_t* data, size_t length) {
  2535. AppFSM* app = (AppFSM*)ctx;
  2536. strncpy(app->commandline->line_data, (const char*)data, length);
  2537. app->commandline->line_pos = 0;
  2538. for(size_t pos = 0; pos < length; pos++) {
  2539. uint8_t ch = app->commandline->line_data[pos];
  2540. if((ch == '\r') || (ch == '\n')) {
  2541. app->commandline->line_data[pos++] = '\n';
  2542. app->commandline->line_data[pos] = 0;
  2543. LOG("direct command '%s'", app->commandline->line_data);
  2544. swd_execute_script_line(app->commandline);
  2545. return pos;
  2546. }
  2547. }
  2548. return 0;
  2549. }
  2550. int32_t swd_probe_app_main(void* p) {
  2551. UNUSED(p);
  2552. AppFSM* app = malloc(sizeof(AppFSM));
  2553. DBGS("App init");
  2554. app_init(app);
  2555. DBGS("furi_record_open");
  2556. app->notification = furi_record_open(RECORD_NOTIFICATION);
  2557. app->gui = furi_record_open(RECORD_GUI);
  2558. app->dialogs = furi_record_open(RECORD_DIALOGS);
  2559. app->storage = furi_record_open(RECORD_STORAGE);
  2560. DBGS("furi_mutex_alloc");
  2561. app->swd_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  2562. app->gui_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  2563. app->event_queue = furi_message_queue_alloc(QUEUE_SIZE, sizeof(AppEvent));
  2564. DBGS("usb_uart_enable");
  2565. UsbUartConfig uart_config;
  2566. uart_config.vcp_ch = 1;
  2567. uart_config.rx_data = &data_received;
  2568. uart_config.rx_data_ctx = app;
  2569. app->uart = usb_uart_enable(&uart_config);
  2570. app->commandline = malloc(sizeof(ScriptContext));
  2571. app->commandline->max_tries = 1;
  2572. app->commandline->app = app;
  2573. DBGS("view_port_alloc");
  2574. app->view_port = view_port_alloc();
  2575. view_port_draw_callback_set(app->view_port, render_callback, app);
  2576. view_port_input_callback_set(app->view_port, input_callback, app);
  2577. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  2578. DBGS("notification_message_block");
  2579. notification_message(app->notification, &sequence_display_backlight_enforce_on);
  2580. DBGS("swd_execute_script");
  2581. swd_execute_script(app, ANY_PATH("swd_scripts/startup.swd"));
  2582. dolphin_deed(DolphinDeedPluginGameStart);
  2583. DBGS("processing");
  2584. for(bool processing = true; processing;) {
  2585. swd_main_loop(app);
  2586. view_port_update(app->view_port);
  2587. processing = swd_message_process(app);
  2588. bool beep = false;
  2589. if(app->detected_device && !app->detected_notified) {
  2590. app->detected_notified = true;
  2591. beep = true;
  2592. }
  2593. if(!app->detected_device && app->detected_notified) {
  2594. app->detected_notified = false;
  2595. }
  2596. if(beep) {
  2597. notification_message_block(app->notification, &seq_c_minor);
  2598. }
  2599. }
  2600. view_port_enabled_set(app->view_port, false);
  2601. gui_remove_view_port(app->gui, app->view_port);
  2602. view_port_free(app->view_port);
  2603. app_deinit(app);
  2604. notification_message(app->notification, &sequence_display_backlight_enforce_auto);
  2605. usb_uart_disable(app->uart);
  2606. furi_message_queue_free(app->event_queue);
  2607. furi_mutex_free(app->gui_mutex);
  2608. furi_mutex_free(app->swd_mutex);
  2609. // Reset GPIO pins to default state
  2610. for(int io = 0; io < 8; io++) {
  2611. furi_hal_gpio_init(gpios[io], GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  2612. }
  2613. free(app);
  2614. furi_record_close(RECORD_GUI);
  2615. furi_record_close(RECORD_NOTIFICATION);
  2616. furi_record_close(RECORD_DIALOGS);
  2617. furi_record_close(RECORD_STORAGE);
  2618. return 0;
  2619. }