X-Git-Url: https://git.rohieb.name/openwrt.git/blobdiff_plain/5a46662bf22265dc928510081fd4ec69559b2228..2d17b93d5618cc37b3e1a4eafbb62a95a17c339f:/scripts/flashing/jungo-image.py diff --git a/scripts/flashing/jungo-image.py b/scripts/flashing/jungo-image.py index 0630b4a77..9947e7c72 100644 --- a/scripts/flashing/jungo-image.py +++ b/scripts/flashing/jungo-image.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# Copyright 2008 (C) Jose Vasconcellos +# Copyright 2008, 2009 (C) Jose Vasconcellos # # A script that can communicate with jungo-based routers # (such as MI424-WR, USR8200 and WRV54G) to backup the installed @@ -8,20 +8,20 @@ # # Tested with Python 2.5 on Linux and Windows # -"""Usage: %s [options] [redboot.bin] +"""Usage: %s [options] [image.bin | url] Valid options: \t-h | --help: usage statement -\t-d | --no-dump: don't create a flash dump +\t-d | --dump: create a flash dump \t-f | --file: use to store dump contents \t-u | --user: provide username (default admin) \t-p | --pass: provide password (default password1) -\t-P | --proto: set transfer protocol (default http) \t --port: set port for http (default 8080) -\t-s | --server: IP address of tftp server -\t-w | --write: initiate loading of redboot (default no modification to flash) \t-q | --quiet: don't display unnecessary information -\t-v | --verbose: display progress information +\t-r | --reboot: reboot target on successful transfer \t-V | --version: display version information + +If no image (or url) is given, a flash dump is created. +A built-in http server is used when an image file is provided. """ import os @@ -36,49 +36,65 @@ import thread import SocketServer import SimpleHTTPServer -server = "" +reboot = 0 HOST = "192.168.1.1" PORT = 8080 user = "admin" #password = getpass.getpass() password = "password1" proto = "http" -imagefile = "redboot.bin" +url = "" +imagefile = "" dumpfile = "" verbose = 1 -no_dump = 0 +do_dump = 0 dumplen = 0x10000 -write_image = 0 flashsize=4*1024*1024 #device="br0" device="ixp0" #################### -def start_server(): +def start_server(server): httpd = SocketServer.TCPServer((server,PORT),SimpleHTTPServer.SimpleHTTPRequestHandler) thread.start_new_thread(httpd.serve_forever,()) #################### def get_flash_size(): - global flashsize + # make sure we don't have an A0 stepping + tn.write("cat /proc/cpuinfo\n") + buf = tn.read_until("Returned 0", 3) + if not buf: + print "Unable to obtain CPU information; make sure to not use A0 stepping!" + elif buf.find('rev 0') > 0: + print "Warning: IXP42x stepping A0 detected!" + if imagefile or url: + print "Error: No linux support for A0 stepping!" + sys.exit(2) + + # now get flash size tn.write("cat /proc/mtd\n") - # wait for prompt buf = tn.read_until("Returned 0", 3) if buf: i = buf.find('mtd0:') if i > 0: - flashsize = int(buf[i+6:].split()[0],16) - else: - print "Can't find mtd0!" + return int(buf[i+6:].split()[0],16) + # use different command + tn.write("flash_layout\n") + buf = tn.read_until("Returned 0", 3) + i = buf.rfind('Range ') + if i > 0: + return int(buf[i+17:].split()[0],16) + print "Can't determine flash size!" else: - print "Can't access /proc/mtd!" + print "Unable to obtain flash size!" + sys.exit(2) def image_dump(tn, dumpfile): if not dumpfile: tn.write("ver\n"); - buf = tn.read_until("Returned 0") + buf = tn.read_until("Returned 0",2) i = buf.find("Platform:") if i < 0: platform="jungo" @@ -87,12 +103,12 @@ def image_dump(tn, dumpfile): i=line.find('\n') platform=line[:i].split()[-1] - tn.write("ifconfig -v %s\n" % device); - buf = tn.read_until("Returned 0") + tn.write("rg_conf_print /dev/%s/mac\n" % device); + buf = tn.read_until("Returned 0",3) - i = buf.find("mac = 0") + i = buf.find("mac(") if i > 0: - i += 6 + i += 4 else: print "No MAC address found! (use -f option)" sys.exit(1) @@ -149,9 +165,9 @@ def usage(): #################### try: - opts, args = getopt.getopt(sys.argv[1:], "hdf:u:qp:P:s:vVw", \ - ["help", "dump", "file=", "user=", "pass=", "proto=", "proto=", - "quiet=", "server=", "verbose", "version", "write"]) + opts, args = getopt.getopt(sys.argv[1:], "hdf:qp:P:rvV", \ + ["help", "dump", "file=", "user=", "pass=", "port=", + "quiet=", "reboot", "verbose", "version"]) except getopt.GetoptError: # print help information and exit: usage() @@ -161,28 +177,24 @@ for o, a in opts: if o in ("-h", "--help"): usage() sys.exit(1) - if o in ("-V", "--version"): - print "%s: 0.7" % sys.argv[0] + elif o in ("-V", "--version"): + print "%s: 0.11" % sys.argv[0] sys.exit(1) - if o in ("-d", "--no-dump"): - no_dump = 1 - if o in ("-f", "--file"): + elif o in ("-d", "--no-dump"): + do_dump = 1 + elif o in ("-f", "--file"): dumpfile = a - if o in ("-s", "--server"): - server = a - if o in ("-u", "--user"): + elif o in ("-u", "--user"): user = a - if o in ("-p", "--pass"): + elif o in ("-p", "--pass"): password = a - if o in ("-P", "--proto"): - proto = a - if o in ("--port"): - PORT = a - if o in ("-w", "--write"): - write_image = 1 - if o in ("-q", "--quiet"): + elif o == "--port": + PORT = int(a) + elif o in ("-q", "--quiet"): verbose = 0 - if o in ("-v", "--verbose"): + elif o in ("-r", "--reboot"): + reboot = 1 + elif o in ("-v", "--verbose"): verbose = 1 # make sure we have enough arguments @@ -190,7 +202,12 @@ if len(args) > 0: HOST = args[0] if len(args) == 2: - imagefile = args[1] + if args[1].split(':')[0] in ("tftp", "http", "ftp"): + url = args[1] + else: + imagefile = args[1] +else: + do_dump = 1; #################### # create a telnet session to the router @@ -217,42 +234,50 @@ buf = tn.read_until("> ", 3) if not buf: telnet_timeout() -get_flash_size() +flashsize = get_flash_size() -if not no_dump: +if do_dump: image_dump(tn, dumpfile) -if write_image: - if not os.access(imagefile, os.R_OK): - print "File access error: %s" % (imagefile) - sys.exit(3) - +if imagefile or url: splitpath = os.path.split(imagefile) - # make sure we're in the directory where the image is located - if splitpath[0]: - os.chdir(splitpath[0]) - # write image file image - if not server: - server = tn.get_socket().getsockname()[0] - if proto == "http": - cmd = "load -u %s://%s:%d/%s -r 0\n" % (proto, server, PORT, splitpath[1]) + # create load command + if url: + cmd = "load -u %s -r 0\n" % (url) else: - cmd = "load -u %s://%s/%s -r 0\n" % (proto, server, splitpath[1]) + server = tn.get_socket().getsockname()[0] + cmd = "load -u http://%s:%d/%s -r 0\n" % (server, PORT, splitpath[1]) + + if not os.access(imagefile, os.R_OK): + print "File access error: %s" % (imagefile) + sys.exit(3) + + # make sure we're in the directory where the image is located + if splitpath[0]: + os.chdir(splitpath[0]) - if proto == "http": - start_server() + start_server(server) if verbose: print "Unlocking flash..." tn.write("unlock 0 0x%x\n" % flashsize) - buf = tn.read_until("Returned 0") + buf = tn.read_until("Returned 0",5) if verbose: print "Writing new image..." print cmd, tn.write(cmd) - buf = tn.read_until("Returned 0") + buf = tn.read_until("Returned 0",10) + + # wait till the transfer completed + buf = tn.read_until("Download completed successfully",20) + if buf: + print "Flash update complete!" + if reboot: + tn.write("reboot\n") + print "Rebooting..." tn.write("exit\n") tn.close() +