jungo-image.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2008, 2009 (C) Jose Vasconcellos <jvasco@verizon.net>
  4. #
  5. # A script that can communicate with jungo-based routers
  6. # (such as MI424-WR, USR8200 and WRV54G) to backup the installed
  7. # firmware and replace the boot loader.
  8. #
  9. # Tested with Python 2.5 on Linux and Windows
  10. #
  11. """Usage: %s [options] <IP_address> [image.bin | url]
  12. Valid options:
  13. \t-h | --help: usage statement
  14. \t-d | --dump: create a flash dump
  15. \t-f | --file: use <filename> to store dump contents
  16. \t-u | --user: provide username (default admin)
  17. \t-p | --pass: provide password (default password1)
  18. \t --port: set port for http (default 8080)
  19. \t-q | --quiet: don't display unnecessary information
  20. \t-r | --reboot: reboot target on successful transfer
  21. \t-V | --version: display version information
  22. If no image (or url) is given, a flash dump is created.
  23. A built-in http server is used when an image file is provided.
  24. """
  25. import os
  26. import sys
  27. import getopt
  28. import getpass
  29. import telnetlib
  30. import string
  31. import binascii
  32. import socket
  33. import _thread
  34. import socketserver
  35. import http.server
  36. reboot = 0
  37. HOST = "192.168.1.1"
  38. PORT = 8080
  39. user = "admin"
  40. #password = getpass.getpass()
  41. password = "password1"
  42. proto = "http"
  43. url = ""
  44. imagefile = ""
  45. dumpfile = ""
  46. verbose = 1
  47. do_dump = 0
  48. dumplen = 0x10000
  49. flashsize=4*1024*1024
  50. #device="br0"
  51. device="ixp0"
  52. ####################
  53. def start_server(server):
  54. httpd = socketserver.TCPServer((server,PORT),http.server.SimpleHTTPRequestHandler)
  55. _thread.start_new_thread(httpd.serve_forever,())
  56. ####################
  57. def get_flash_size():
  58. # make sure we don't have an A0 stepping
  59. tn.write("cat /proc/cpuinfo\n")
  60. buf = tn.read_until("Returned 0", 3)
  61. if not buf:
  62. print("Unable to obtain CPU information; make sure to not use A0 stepping!")
  63. elif buf.find('rev 0') > 0:
  64. print("Warning: IXP42x stepping A0 detected!")
  65. if imagefile or url:
  66. print("Error: No linux support for A0 stepping!")
  67. sys.exit(2)
  68. # now get flash size
  69. tn.write("cat /proc/mtd\n")
  70. buf = tn.read_until("Returned 0", 3)
  71. if buf:
  72. i = buf.find('mtd0:')
  73. if i > 0:
  74. return int(buf[i+6:].split()[0],16)
  75. # use different command
  76. tn.write("flash_layout\n")
  77. buf = tn.read_until("Returned 0", 3)
  78. i = buf.rfind('Range ')
  79. if i > 0:
  80. return int(buf[i+17:].split()[0],16)
  81. print("Can't determine flash size!")
  82. else:
  83. print("Unable to obtain flash size!")
  84. sys.exit(2)
  85. def image_dump(tn, dumpfile):
  86. if not dumpfile:
  87. tn.write("ver\n");
  88. buf = tn.read_until("Returned 0",2)
  89. i = buf.find("Platform:")
  90. if i < 0:
  91. platform="jungo"
  92. else:
  93. line=buf[i+9:]
  94. i=line.find('\n')
  95. platform=line[:i].split()[-1]
  96. tn.write("rg_conf_print /dev/%s/mac\n" % device);
  97. buf = tn.read_until("Returned 0",3)
  98. i = buf.find("mac(")
  99. if i > 0:
  100. i += 4
  101. else:
  102. print("No MAC address found! (use -f option)")
  103. sys.exit(1)
  104. dumpfile = "%s-%s.bin" % (platform, buf[i:i+17].replace(':',''))
  105. else:
  106. tn.write("\n")
  107. print("Dumping flash contents (%dMB) to %s" % (flashsize/1048576, dumpfile))
  108. f = open(dumpfile, "wb")
  109. t=flashsize/dumplen
  110. for addr in range(t):
  111. if verbose:
  112. sys.stdout.write('\r%d%%'%(100*addr/t))
  113. sys.stdout.flush()
  114. tn.write("flash_dump -r 0x%x -l %d -4\n" % (addr*dumplen, dumplen))
  115. tn.read_until("\n")
  116. count = addr*dumplen
  117. while 1:
  118. buf = tn.read_until("\n")
  119. if buf.strip() == "Returned 0":
  120. break
  121. s = buf.split()
  122. if s and s[0][-1] == ':':
  123. a=int(s[0][:-1],16)
  124. if a != count:
  125. print("Format error: %x != %x"%(a,count))
  126. sys.exit(2)
  127. count += 16
  128. f.write(binascii.a2b_hex(string.join(s[1:],'')))
  129. tn.read_until(">",1)
  130. f.close()
  131. if verbose:
  132. print("")
  133. def telnet_option(sock,cmd,option):
  134. #print "Option: %d %d" % (ord(cmd), ord(option))
  135. if cmd == telnetlib.DO:
  136. c=telnetlib.WILL
  137. elif cmd == telnetlib.WILL:
  138. c=telnetlib.DO
  139. sock.sendall(telnetlib.IAC + c + option)
  140. def telnet_timeout():
  141. print("Fatal error: telnet timeout!")
  142. sys.exit(1)
  143. def usage():
  144. print(__doc__ % os.path.basename(sys.argv[0]))
  145. ####################
  146. try:
  147. opts, args = getopt.getopt(sys.argv[1:], "hdf:qp:P:rvV", \
  148. ["help", "dump", "file=", "user=", "pass=", "port=",
  149. "quiet=", "reboot", "verbose", "version"])
  150. except getopt.GetoptError:
  151. # print help information and exit:
  152. usage()
  153. sys.exit(1)
  154. for o, a in opts:
  155. if o in ("-h", "--help"):
  156. usage()
  157. sys.exit(1)
  158. elif o in ("-V", "--version"):
  159. print("%s: 0.11" % sys.argv[0])
  160. sys.exit(1)
  161. elif o in ("-d", "--no-dump"):
  162. do_dump = 1
  163. elif o in ("-f", "--file"):
  164. dumpfile = a
  165. elif o in ("-u", "--user"):
  166. user = a
  167. elif o in ("-p", "--pass"):
  168. password = a
  169. elif o == "--port":
  170. PORT = int(a)
  171. elif o in ("-q", "--quiet"):
  172. verbose = 0
  173. elif o in ("-r", "--reboot"):
  174. reboot = 1
  175. elif o in ("-v", "--verbose"):
  176. verbose = 1
  177. # make sure we have enough arguments
  178. if len(args) > 0:
  179. HOST = args[0]
  180. if len(args) == 2:
  181. if args[1].split(':')[0] in ("tftp", "http", "ftp"):
  182. url = args[1]
  183. else:
  184. imagefile = args[1]
  185. else:
  186. do_dump = 1;
  187. ####################
  188. # create a telnet session to the router
  189. try:
  190. tn = telnetlib.Telnet(HOST)
  191. except socket.error as msg:
  192. print("Unable to establish telnet session to %s: %s" % (HOST, msg))
  193. sys.exit(1)
  194. tn.set_option_negotiation_callback(telnet_option)
  195. buf = tn.read_until("Username: ", 3)
  196. if not buf:
  197. telnet_timeout()
  198. tn.write(user+"\n")
  199. if password:
  200. buf = tn.read_until("Password: ", 3)
  201. if not buf:
  202. telnet_timeout()
  203. tn.write(password+"\n")
  204. # wait for prompt
  205. buf = tn.read_until("> ", 3)
  206. if not buf:
  207. telnet_timeout()
  208. flashsize = get_flash_size()
  209. if do_dump:
  210. image_dump(tn, dumpfile)
  211. if imagefile or url:
  212. splitpath = os.path.split(imagefile)
  213. # create load command
  214. if url:
  215. cmd = "load -u %s -r 0\n" % (url)
  216. else:
  217. server = tn.get_socket().getsockname()[0]
  218. cmd = "load -u http://%s:%d/%s -r 0\n" % (server, PORT, splitpath[1])
  219. if not os.access(imagefile, os.R_OK):
  220. print("File access error: %s" % (imagefile))
  221. sys.exit(3)
  222. # make sure we're in the directory where the image is located
  223. if splitpath[0]:
  224. os.chdir(splitpath[0])
  225. start_server(server)
  226. if verbose:
  227. print("Unlocking flash...")
  228. tn.write("unlock 0 0x%x\n" % flashsize)
  229. buf = tn.read_until("Returned 0",5)
  230. if verbose:
  231. print("Writing new image...")
  232. print(cmd, end=' ')
  233. tn.write(cmd)
  234. buf = tn.read_until("Returned 0",10)
  235. # wait till the transfer completed
  236. buf = tn.read_until("Download completed successfully",20)
  237. if buf:
  238. print("Flash update complete!")
  239. if reboot:
  240. tn.write("reboot\n")
  241. print("Rebooting...")
  242. tn.write("exit\n")
  243. tn.close()