swd_probe_app.c 97 KB

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