swd_probe_app.c 96 KB

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