swd_probe_app.c 96 KB

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