jungo-image.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2008, 2009 (C) Jose Vasconcellos <[email protected]>
  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 SimpleHTTPServer
  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),SimpleHTTPServer.SimpleHTTPRequestHandler)
  55. thread.start_new_thread(httpd.serve_forever,())
  56. ####################
  57. def get_flash_size():
  58. tn.write("cat /proc/mtd\n")
  59. # wait for prompt
  60. buf = tn.read_until("Returned 0", 3)
  61. if buf:
  62. i = buf.find('mtd0:')
  63. if i > 0:
  64. return int(buf[i+6:].split()[0],16)
  65. print "Can't find mtd0!"
  66. else:
  67. print "Can't access /proc/mtd!"
  68. sys.exit(2)
  69. def image_dump(tn, dumpfile):
  70. if not dumpfile:
  71. tn.write("ver\n");
  72. buf = tn.read_until("Returned 0")
  73. i = buf.find("Platform:")
  74. if i < 0:
  75. platform="jungo"
  76. else:
  77. line=buf[i+9:]
  78. i=line.find('\n')
  79. platform=line[:i].split()[-1]
  80. tn.write("ifconfig -v %s\n" % device);
  81. buf = tn.read_until("Returned 0")
  82. i = buf.find("mac = 0")
  83. if i > 0:
  84. i += 6
  85. else:
  86. print "No MAC address found! (use -f option)"
  87. sys.exit(1)
  88. dumpfile = "%s-%s.bin" % (platform, buf[i:i+17].replace(':',''))
  89. else:
  90. tn.write("\n")
  91. print "Dumping flash contents (%dMB) to %s" % (flashsize/1048576, dumpfile)
  92. f = open(dumpfile, "wb")
  93. t=flashsize/dumplen
  94. for addr in range(t):
  95. if verbose:
  96. sys.stdout.write('\r%d%%'%(100*addr/t))
  97. sys.stdout.flush()
  98. tn.write("flash_dump -r 0x%x -l %d -4\n" % (addr*dumplen, dumplen))
  99. tn.read_until("\n")
  100. count = addr*dumplen
  101. while 1:
  102. buf = tn.read_until("\n")
  103. if buf.strip() == "Returned 0":
  104. break
  105. s = buf.split()
  106. if s and s[0][-1] == ':':
  107. a=int(s[0][:-1],16)
  108. if a != count:
  109. print "Format error: %x != %x"%(a,count)
  110. sys.exit(2)
  111. count += 16
  112. f.write(binascii.a2b_hex(string.join(s[1:],'')))
  113. tn.read_until(">",1)
  114. f.close()
  115. if verbose:
  116. print ""
  117. def telnet_option(sock,cmd,option):
  118. #print "Option: %d %d" % (ord(cmd), ord(option))
  119. if cmd == telnetlib.DO:
  120. c=telnetlib.WILL
  121. elif cmd == telnetlib.WILL:
  122. c=telnetlib.DO
  123. sock.sendall(telnetlib.IAC + c + option)
  124. def telnet_timeout():
  125. print "Fatal error: telnet timeout!"
  126. sys.exit(1)
  127. def usage():
  128. print __doc__ % os.path.basename(sys.argv[0])
  129. ####################
  130. try:
  131. opts, args = getopt.getopt(sys.argv[1:], "hdf:qp:P:rvV", \
  132. ["help", "dump", "file=", "user=", "pass=", "port=",
  133. "quiet=", "reboot", "verbose", "version"])
  134. except getopt.GetoptError:
  135. # print help information and exit:
  136. usage()
  137. sys.exit(1)
  138. for o, a in opts:
  139. if o in ("-h", "--help"):
  140. usage()
  141. sys.exit(1)
  142. elif o in ("-V", "--version"):
  143. print "%s: 0.9" % sys.argv[0]
  144. sys.exit(1)
  145. elif o in ("-d", "--no-dump"):
  146. do_dump = 1
  147. elif o in ("-f", "--file"):
  148. dumpfile = a
  149. elif o in ("-u", "--user"):
  150. user = a
  151. elif o in ("-p", "--pass"):
  152. password = a
  153. elif o == "--port":
  154. PORT = int(a)
  155. elif o in ("-q", "--quiet"):
  156. verbose = 0
  157. elif o in ("-r", "--reboot"):
  158. reboot = 1
  159. elif o in ("-v", "--verbose"):
  160. verbose = 1
  161. # make sure we have enough arguments
  162. if len(args) > 0:
  163. HOST = args[0]
  164. if len(args) == 2:
  165. if args[1].split(':')[0] in ("tftp", "http", "ftp"):
  166. url = args[1]
  167. else:
  168. imagefile = args[1]
  169. else:
  170. do_dump = 1;
  171. ####################
  172. # create a telnet session to the router
  173. try:
  174. tn = telnetlib.Telnet(HOST)
  175. except socket.error, msg:
  176. print "Unable to establish telnet session to %s: %s" % (HOST, msg)
  177. sys.exit(1)
  178. tn.set_option_negotiation_callback(telnet_option)
  179. buf = tn.read_until("Username: ", 3)
  180. if not buf:
  181. telnet_timeout()
  182. tn.write(user+"\n")
  183. if password:
  184. buf = tn.read_until("Password: ", 3)
  185. if not buf:
  186. telnet_timeout()
  187. tn.write(password+"\n")
  188. # wait for prompt
  189. buf = tn.read_until("> ", 3)
  190. if not buf:
  191. telnet_timeout()
  192. flashsize = get_flash_size()
  193. if do_dump:
  194. image_dump(tn, dumpfile)
  195. if imagefile or url:
  196. splitpath = os.path.split(imagefile)
  197. # create load command
  198. if url:
  199. cmd = "load -u %s -r 0\n" % (url)
  200. else:
  201. server = tn.get_socket().getsockname()[0]
  202. cmd = "load -u http://%s:%d/%s -r 0\n" % (server, PORT, splitpath[1])
  203. if not os.access(imagefile, os.R_OK):
  204. print "File access error: %s" % (imagefile)
  205. sys.exit(3)
  206. # make sure we're in the directory where the image is located
  207. if splitpath[0]:
  208. os.chdir(splitpath[0])
  209. start_server(server)
  210. if verbose:
  211. print "Unlocking flash..."
  212. tn.write("unlock 0 0x%x\n" % flashsize)
  213. buf = tn.read_until("Returned 0")
  214. if verbose:
  215. print "Writing new image..."
  216. print cmd,
  217. tn.write(cmd)
  218. buf = tn.read_until("Returned 0",10)
  219. # wait till the transfer completed
  220. buf = tn.read_until("Download completed successfully",20)
  221. if buf:
  222. print "Flash update complete!"
  223. if reboot:
  224. tn.write("reboot\n")
  225. print "Rebooting..."
  226. tn.write("exit\n")
  227. tn.close()