swd_probe_app.c 97 KB

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