PageRenderTime 38ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/src/pentest/goodfet/gplay-arm.py

https://bitbucket.org/manaphassan/raspberry-pwn
Python | 348 lines | 311 code | 11 blank | 26 comment | 0 complexity | 5184cacf5de4a27a157cabdcef948822 MD5 | raw file
  1. #!/usr/bin/env ipython
  2. import sys, struct, binascii
  3. from GoodFETARM import *
  4. from intelhex import IntelHex
  5. data = []
  6. client=GoodFETARM();
  7. def init():
  8. #Initailize FET and set baud rate
  9. client.serInit()
  10. #
  11. #Connect to target
  12. client.setup()
  13. client.start()
  14. print "STARTUP: "+repr(client.data)
  15. #
  16. def test1():
  17. global data
  18. print "\n\nTesting JTAG for ARM\n"
  19. client.writecmd(0x33,0xd0,4,[0x40,0x40,0x40,0x40]); print "loopback: \t %s"%repr(client.data) # loopback
  20. data.append(client.data)
  21. client.writecmd(0x33,0xd1,2,[1,0]); print "scanchain1:\t %s"%repr(client.data) # set scan chain
  22. data.append(client.data)
  23. client.writecmd(0x33,0xd2,0,[]); print "debug state:\t %s"%repr(client.data) # get dbg state
  24. data.append(client.data)
  25. client.writecmd(0x33,0xd3,0,[0,0,0xa0,0xe1]); print "exec_nop: \t %s"%repr(client.data) # execute instruction
  26. data.append(client.data)
  27. client.writecmd(0x33,0xd3,0,[0,0,0x8e,0xe5]); print "exec_stuff: \t %s"%repr(client.data) # execute instruction
  28. data.append(client.data)
  29. client.writecmd(0x33,0xd3,0,[0,0,0xa0,0xe1]); print "exec_nop: \t %s"%repr(client.data) # execute instruction
  30. data.append(client.data)
  31. client.writecmd(0x33,0xd3,0,[0,0,0xa0,0xe1]); print "exec_nop: \t %s"%repr(client.data) # execute instruction
  32. data.append(client.data)
  33. client.writecmd(0x33,0xd3,0,[0,0,0xa0,0xe1]); print "exec_nop: \t %s"%repr(client.data) # execute instruction
  34. data.append(client.data)
  35. client.writecmd(0x33,0xd6,0,[]); print "shift_dr_32: \t %s"%repr(client.data) # dr_shift32
  36. data.append(client.data)
  37. client.writecmd(0x33,0xd5,8,[3, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40]); print "set_register:\t %s"%repr(client.data) # set register
  38. data.append(client.data)
  39. client.writecmd(0x33,0xd4,1,[3]); print "get_register:\t %s"%repr(client.data) # get register
  40. data.append(client.data)
  41. client.writecmd(0x33,0xd7,0,[]); print "chain1: \t %s"%repr(client.data) # chain1
  42. data.append(client.data)
  43. client.writecmd(0x33,0xd8,0,[]); print "read_chain2: \t %s"%repr(client.data) # read chain2
  44. data.append(client.data)
  45. client.writecmd(0x33,0xd9,0,[]); print "idcode: \t %s"%repr(client.data) # read idcode
  46. data.append(client.data)
  47. client.writecmd(0x33,0xf0,2,[4,4,1,1]); print "f0: \t %s"%repr(client.data) # read idcode
  48. data.append(client.data)
  49. client.writecmd(0x33,0xdb,8,[0x0,4,4,4,4,4,4,4]); print "verb(0): \t %s"%repr(client.data)
  50. data.append(client.data)
  51. client.writecmd(0x33,0xdb,8,[0x2,4,4,4,4,4,4,4]); print "verb(2): \t %s"%repr(client.data)
  52. data.append(client.data)
  53. client.writecmd(0x33,0xdb,8,[0x3,4,4,4,4,4,4,4]); print "verb(3): \t %s"%repr(client.data)
  54. data.append(client.data)
  55. client.writecmd(0x33,0xdb,8,[0x4,4,4,4,4,4,4,4]); print "verb(4): \t %s"%repr(client.data)
  56. data.append(client.data)
  57. client.writecmd(0x33,0xdb,8,[0x5,4,4,4,4,4,4,4]); print "verb(5): \t %s"%repr(client.data)
  58. data.append(client.data)
  59. client.writecmd(0x33,0xdb,8,[0x7,4,4,4,4,4,4,4]); print "verb(7): \t %s"%repr(client.data)
  60. data.append(client.data)
  61. client.writecmd(0x33,0xdb,8,[0x9,4,4,4,4,4,4,4]); print "verb(9): \t %s"%repr(client.data)
  62. data.append(client.data)
  63. client.writecmd(0x33,0xdb,8,[0xc,4,4,4,4,4,4,4]); print "verb(c): \t %s"%repr(client.data)
  64. data.append(client.data)
  65. client.writecmd(0x33,0xdb,8,[0xe,0,0,0,0,0,0xa0,0xe1]); print "verb(e): \t %s"%repr(client.data)
  66. data.append(client.data)
  67. client.writecmd(0x33,0xdb,8,[0xf,4,4,4,4,4,4,4]); print "verb(f): \t %s"%repr(client.data)
  68. data.append(client.data)
  69. def test2():
  70. global data
  71. print "\n\nTesting JTAG for ARM\n"
  72. print "IDCODE: %x"%client.ARMident()
  73. print "Debug State: %x"%client.ARMget_dbgstate ()
  74. print "Debug State: %x"%client.ARMget_dbgstate ()
  75. print "Debug State: %x"%client.ARMget_dbgstate ()
  76. print "Debug CTRL: %x"%client.ARMget_dbgctrl()
  77. client.writecmd(0x33,0xda,0,[])
  78. print "TEST CHAIN0: %s"%repr(client.data)
  79. print "Debug State: %x"%client.ARMget_dbgstate ()
  80. print "IDCODE: %x"%client.ARMident()
  81. print "Debug State: %x"%client.ARMget_dbgstate ()
  82. client.writecmd(0x33,0xd0,4,[0xf7,0xf7,0xf7,0xf7])
  83. print "Loopback: \t %s"%repr(client.data) # loopback
  84. print "Debug State: %x"%client.ARMget_dbgstate ()
  85. print "IDCODE: %x"%client.ARMident()
  86. print "GetPC: %x"%client.ARMgetPC()
  87. print "IDCODE: %x"%client.ARMident()
  88. print "Debug State: %x"%client.ARMget_dbgstate ()
  89. print "IDCODE: %x"%client.ARMident()
  90. print "set_register(3,0x41414141): %x"%client.ARMset_register(3,0x41414141)
  91. print "IDCODE: %x"%client.ARMident()
  92. print "Debug State: %x"%client.ARMget_dbgstate ()
  93. print "IDCODE: %x"%client.ARMident()
  94. print "get_register(3): %x"%client.ARMget_register(3)
  95. print "IDCODE: %x"%client.ARMident()
  96. print "Debug State: %x"%client.ARMget_dbgstate ()
  97. print "IDCODE: %x"%client.ARMident()
  98. def test3():
  99. print "IDCODE: %x"%client.ARMident()
  100. print "Debug State: %x"%client.ARMget_dbgstate ()
  101. client.writecmd(0x33,0xd0,4,[0xf7,0xf7,0xf7,0xf7])
  102. print "Loopback: \t %s"%repr(client.data) # loopback
  103. client.writecmd(0x33,0xd5,8,[0,0,0,0,0xf7,0xf7,0xf7,0xf7])
  104. print "test_set_reg: \t %s"%repr(client.data)
  105. client.writecmd(0x33,0xd4,1,[0])
  106. print "test_get_reg: \t %s"%repr(client.data)
  107. print "set_register(3,0x41414141): %x"%client.ARMset_register(3,0x41414141)
  108. print "get_register(3): %x"%client.ARMget_register(3)
  109. client.writecmd(0x33,0xd4,1,[0])
  110. print "test_get_reg: \t %s"%repr(client.data)
  111. init()
  112. print "Don't forget to 'client.stop()' if you want to exit cleanly"
  113. """
  114. case 0xD0: // loopback test
  115. cmddatalong[0] = 0x12345678;
  116. case 0xD1: // Set Scan Chain
  117. cmddatalong[0] = jtagarm7tdmi_scan_n(cmddataword[0]);
  118. case 0xD2: //
  119. cmddatalong[0] = jtagarm7tdmi_get_dbgstate();
  120. case 0xD3:
  121. cmddatalong[0] = jtagarm7tdmi_exec(cmddatalong[0]);
  122. case 0xD4:
  123. cmddatalong[0] = jtagarm7tdmi_get_register(cmddata[0]);
  124. case 0xD5:
  125. cmddatalong[0] = jtagarm7tdmi_set_register(cmddata[0], cmddatalong[1]);
  126. case 0xD6:
  127. cmddatalong[0] = jtagarm7tdmi_dr_shift32(cmddatalong[0]);
  128. case 0xD7:
  129. cmddatalong[0] = jtagarm7tdmi_chain1(cmddatalong[0], 0);
  130. case 0xD8:
  131. cmddatalong[0] = jtagarm7tdmi_chain2_read(cmddata[0], 32);
  132. """
  133. """
  134. if(sys.argv[1]=="test"):
  135. client.CCtest();
  136. if(sys.argv[1]=="deadtest"):
  137. for i in range(1,10):
  138. print "IDENT as %s" % client.CCidentstr();
  139. if(sys.argv[1]=="dumpcode"):
  140. f = sys.argv[2];
  141. start=0x0000;
  142. stop=0xFFFF;
  143. if(len(sys.argv)>3):
  144. start=int(sys.argv[3],16);
  145. if(len(sys.argv)>4):
  146. stop=int(sys.argv[4],16);
  147. print "Dumping code from %04x to %04x as %s." % (start,stop,f);
  148. h = IntelHex(None);
  149. i=start;
  150. while i<=stop:
  151. h[i]=client.CCpeekcodebyte(i);
  152. if(i%0x100==0):
  153. print "Dumped %04x."%i;
  154. i+=1;
  155. h.write_hex_file(f);
  156. if(sys.argv[1]=="dumpdata"):
  157. f = sys.argv[2];
  158. start=0xE000;
  159. stop=0xFFFF;
  160. if(len(sys.argv)>3):
  161. start=int(sys.argv[3],16);
  162. if(len(sys.argv)>4):
  163. stop=int(sys.argv[4],16);
  164. print "Dumping data from %04x to %04x as %s." % (start,stop,f);
  165. h = IntelHex(None);
  166. i=start;
  167. while i<=stop:
  168. h[i]=client.CCpeekdatabyte(i);
  169. if(i%0x100==0):
  170. print "Dumped %04x."%i;
  171. i+=1;
  172. h.write_hex_file(f);
  173. if(sys.argv[1]=="status"):
  174. print "Status: %s" %client.CCstatusstr();
  175. if(sys.argv[1]=="erase"):
  176. print "Status: %s" % client.CCstatusstr();
  177. client.CCchiperase();
  178. print "Status: %s" %client.CCstatusstr();
  179. if(sys.argv[1]=="peekinfo"):
  180. print "Select info flash."
  181. client.CCwr_config(1);
  182. print "Config is %02x" % client.CCrd_config();
  183. start=0x0000;
  184. if(len(sys.argv)>2):
  185. start=int(sys.argv[2],16);
  186. stop=start;
  187. if(len(sys.argv)>3):
  188. stop=int(sys.argv[3],16);
  189. print "Peeking from %04x to %04x." % (start,stop);
  190. while start<=stop:
  191. print "%04x: %02x" % (start,client.CCpeekcodebyte(start));
  192. start=start+1;
  193. if(sys.argv[1]=="poke"):
  194. client.CCpokeirambyte(int(sys.argv[2],16),
  195. int(sys.argv[3],16));
  196. if(sys.argv[1]=="randtest"):
  197. #Seed RNG
  198. client.CCpokeirambyte(0xBD,0x01); #RNDH=0x01
  199. client.CCpokeirambyte(0xB4,0x04); #ADCCON1=0x04
  200. client.CCpokeirambyte(0xBD,0x01); #RNDH=0x01
  201. client.CCpokeirambyte(0xB4,0x04); #ADCCON1=0x04
  202. #Dump values
  203. for foo in range(1,10):
  204. print "%02x" % client.CCpeekirambyte(0xBD); #RNDH
  205. client.CCpokeirambyte(0xB4,0x04); #ADCCON1=0x04
  206. client.CCreleasecpu();
  207. client.CChaltcpu();
  208. print "%02x" % client.CCpeekdatabyte(0xDF61); #CHIP ID
  209. if(sys.argv[1]=="adctest"):
  210. # ADCTest 0xDF3A 0xDF3B
  211. print "ADC TEST %02x%02x" % (
  212. client.CCpeekdatabyte(0xDF3A),
  213. client.CCpeekdatabyte(0xDF3B));
  214. if(sys.argv[1]=="config"):
  215. print "Config is %02x" % client.CCrd_config();
  216. if(sys.argv[1]=="flash"):
  217. f=sys.argv[2];
  218. start=0;
  219. stop=0xFFFF;
  220. if(len(sys.argv)>3):
  221. start=int(sys.argv[3],16);
  222. if(len(sys.argv)>4):
  223. stop=int(sys.argv[4],16);
  224. h = IntelHex(f);
  225. page = 0x0000;
  226. pagelen = 2048; #2kB pages in 32-bit words
  227. bcount = 0;
  228. print "Wiping Flash."
  229. #Wipe all of flash.
  230. #client.CCchiperase();
  231. #Wipe the RAM buffer for the next flash page.
  232. #client.CCeraseflashbuffer();
  233. for i in h._buf.keys():
  234. while(i>page+pagelen):
  235. if bcount>0:
  236. client.CCflashpage(page);
  237. #client.CCeraseflashbuffer();
  238. bcount=0;
  239. print "Flashed page at %06x" % page
  240. page+=pagelen;
  241. #Place byte into buffer.
  242. client.CCpokedatabyte(0xF000+i-page,
  243. h[i]);
  244. bcount+=1;
  245. if(i%0x100==0):
  246. print "Buffering %04x toward %06x" % (i,page);
  247. #last page
  248. client.CCflashpage(page);
  249. print "Flashed final page at %06x" % page;
  250. if(sys.argv[1]=="lock"):
  251. print "Status: %s" %client.CCstatusstr();
  252. client.CClockchip();
  253. print "Status: %s" %client.CCstatusstr();
  254. if(sys.argv[1]=="flashpage"):
  255. target=0;
  256. if(len(sys.argv)>2):
  257. target=int(sys.argv[2],16);
  258. print "Writing a page of flash from 0xF000 in XDATA"
  259. client.CCflashpage(target);
  260. if(sys.argv[1]=="erasebuffer"):
  261. print "Erasing flash buffer.";
  262. client.CCeraseflashbuffer();
  263. if(sys.argv[1]=="writedata"):
  264. f=sys.argv[2];
  265. start=0;
  266. stop=0xFFFF;
  267. if(len(sys.argv)>3):
  268. start=int(sys.argv[3],16);
  269. if(len(sys.argv)>4):
  270. stop=int(sys.argv[4],16);
  271. h = IntelHex(f);
  272. for i in h._buf.keys():
  273. if(i>=start and i<=stop):
  274. client.CCpokedatabyte(i,h[i]);
  275. if(i%0x100==0):
  276. print "%04x" % i;
  277. #if(sys.argv[1]=="flashtest"):
  278. # client.CCflashtest();
  279. if(sys.argv[1]=="peekdata"):
  280. start=0x0000;
  281. if(len(sys.argv)>2):
  282. start=int(sys.argv[2],16);
  283. stop=start;
  284. if(len(sys.argv)>3):
  285. stop=int(sys.argv[3],16);
  286. print "Peeking from %04x to %04x." % (start,stop);
  287. while start<=stop:
  288. print "%04x: %02x" % (start,client.CCpeekdatabyte(start));
  289. start=start+1;
  290. if(sys.argv[1]=="peek"):
  291. start=0x0000;
  292. if(len(sys.argv)>2):
  293. start=int(sys.argv[2],16);
  294. stop=start;
  295. if(len(sys.argv)>3):
  296. stop=int(sys.argv[3],16);
  297. print "Peeking from %04x to %04x." % (start,stop);
  298. while start<=stop:
  299. print "%04x: %02x" % (start,client.CCpeekirambyte(start));
  300. start=start+1;
  301. if(sys.argv[1]=="peekcode"):
  302. start=0x0000;
  303. if(len(sys.argv)>2):
  304. start=int(sys.argv[2],16);
  305. stop=start;
  306. if(len(sys.argv)>3):
  307. stop=int(sys.argv[3],16);
  308. print "Peeking from %04x to %04x." % (start,stop);
  309. while start<=stop:
  310. print "%04x: %02x" % (start,client.CCpeekcodebyte(start));
  311. start=start+1;
  312. if(sys.argv[1]=="pokedata"):
  313. start=0x0000;
  314. val=0x00;
  315. if(len(sys.argv)>2):
  316. start=int(sys.argv[2],16);
  317. if(len(sys.argv)>3):
  318. val=int(sys.argv[3],16);
  319. print "Poking %04x to become %02x." % (start,val);
  320. client.CCpokedatabyte(start,val);
  321. client.stop();
  322. """