swd_probe_app.c 96 KB

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