swd_probe_app.c 96 KB

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