Changeset 14098


Ignore:
Timestamp:
2009-01-18T19:38:32+01:00 (7 years ago)
Author:
matein4
Message:

Fix to wait for internal http server to complete transfer.
Rework and simplify command line options.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/scripts/flashing/jungo-image.py

    r14046 r14098  
    11#!/usr/bin/env python 
    22# 
    3 # Copyright 2008 (C) Jose Vasconcellos <jvasco@verizon.net> 
     3# Copyright 2008, 2009 (C) Jose Vasconcellos <jvasco@verizon.net> 
    44# 
    55# A script that can communicate with jungo-based routers 
     
    99# Tested with Python 2.5 on Linux and Windows 
    1010# 
    11 """Usage: %s [options] <IP_address> [redboot.bin] 
     11"""Usage: %s [options] <IP_address> [image.bin | url] 
    1212Valid options: 
    1313\t-h | --help: usage statement 
    14 \t-d | --no-dump: don't create a flash dump 
     14\t-d | --dump: create a flash dump 
    1515\t-f | --file: use <filename> to store dump contents 
    1616\t-u | --user: provide username (default admin) 
    1717\t-p | --pass: provide password (default password1) 
    18 \t-P | --proto: set transfer protocol (default http) 
    1918\t     --port: set port for http (default 8080) 
    20 \t-s | --server: IP address of tftp server 
    21 \t-w | --write: initiate loading of redboot (default no modification to flash) 
    2219\t-q | --quiet: don't display unnecessary information 
    23 \t-v | --verbose: display progress information 
     20\t-r | --reboot: reboot target on successful transfer 
    2421\t-V | --version: display version information 
     22 
     23If no image (or url) is given, a flash dump is created. 
     24A built-in http server is used when an image file is provided. 
    2525""" 
    2626 
     
    3737import SimpleHTTPServer 
    3838 
    39 server = "" 
     39reboot = 0 
    4040HOST = "192.168.1.1" 
    4141PORT = 8080 
     
    4444password = "password1" 
    4545proto = "http" 
    46 imagefile = "redboot.bin" 
     46url = "" 
     47imagefile = "" 
    4748dumpfile = "" 
    4849verbose = 1 
    49 no_dump = 0 
     50do_dump = 0 
    5051dumplen = 0x10000 
    51 write_image = 0 
    5252flashsize=4*1024*1024 
    5353#device="br0" 
     
    5656#################### 
    5757 
    58 def start_server(): 
     58def start_server(server): 
    5959    httpd = SocketServer.TCPServer((server,PORT),SimpleHTTPServer.SimpleHTTPRequestHandler) 
    6060    thread.start_new_thread(httpd.serve_forever,()) 
     
    6363 
    6464def get_flash_size(): 
    65     global flashsize 
    6665    tn.write("cat /proc/mtd\n") 
    6766    # wait for prompt 
     
    7069        i = buf.find('mtd0:') 
    7170        if i > 0: 
    72             flashsize = int(buf[i+6:].split()[0],16) 
    73         else: 
    74             print "Can't find mtd0!" 
     71            return int(buf[i+6:].split()[0],16) 
     72        print "Can't find mtd0!" 
    7573    else: 
    7674        print "Can't access /proc/mtd!" 
     75    sys.exit(2) 
    7776 
    7877def image_dump(tn, dumpfile): 
     
    150149 
    151150try: 
    152     opts, args = getopt.getopt(sys.argv[1:], "hdf:u:qp:P:s:vVw", \ 
    153         ["help", "dump", "file=", "user=", "pass=", "port=", "proto=", 
    154          "quiet=", "server=", "verbose", "version", "write"]) 
     151    opts, args = getopt.getopt(sys.argv[1:], "hdf:qp:P:rvV", \ 
     152        ["help", "dump", "file=", "user=", "pass=", "port=", 
     153         "quiet=", "reboot", "verbose", "version"]) 
    155154except getopt.GetoptError: 
    156155    # print help information and exit: 
     
    163162        sys.exit(1) 
    164163    elif o in ("-V", "--version"): 
    165         print "%s: 0.8" % sys.argv[0] 
     164        print "%s: 0.9" % sys.argv[0] 
    166165        sys.exit(1) 
    167166    elif o in ("-d", "--no-dump"): 
    168         no_dump = 1 
     167        do_dump = 1 
    169168    elif o in ("-f", "--file"): 
    170169        dumpfile = a 
    171     elif o in ("-s", "--server"): 
    172         server = a 
    173170    elif o in ("-u", "--user"): 
    174171        user = a 
    175172    elif o in ("-p", "--pass"): 
    176173        password = a 
    177     elif o in ("-P", "--proto"): 
    178         proto = a 
    179174    elif o == "--port": 
    180175        PORT = int(a) 
    181     elif o in ("-w", "--write"): 
    182         write_image = 1 
    183176    elif o in ("-q", "--quiet"): 
    184177        verbose = 0 
     178    elif o in ("-r", "--reboot"): 
     179        reboot = 1 
    185180    elif o in ("-v", "--verbose"): 
    186181        verbose = 1 
     
    191186 
    192187if len(args) == 2: 
    193     imagefile = args[1] 
     188    if args[1].split(':')[0] in ("tftp", "http", "ftp"): 
     189        url = args[1] 
     190    else: 
     191        imagefile = args[1] 
     192else: 
     193    do_dump = 1; 
    194194 
    195195#################### 
     
    218218    telnet_timeout() 
    219219 
    220 get_flash_size() 
    221  
    222 if not no_dump: 
     220flashsize = get_flash_size() 
     221 
     222if do_dump: 
    223223    image_dump(tn, dumpfile) 
    224224 
    225 if write_image: 
    226     if not os.access(imagefile, os.R_OK): 
    227         print "File access error: %s" % (imagefile) 
    228         sys.exit(3) 
    229  
     225if imagefile or url: 
    230226    splitpath = os.path.split(imagefile) 
    231     # make sure we're in the directory where the image is located 
    232     if splitpath[0]: 
    233         os.chdir(splitpath[0]) 
    234  
    235     # write image file image 
    236     if not server: 
     227 
     228    # create load command 
     229    if url: 
     230        cmd = "load -u %s -r 0\n" % (url) 
     231    else: 
    237232        server = tn.get_socket().getsockname()[0] 
    238     if proto == "http": 
    239         cmd = "load -u %s://%s:%d/%s -r 0\n" % (proto, server, PORT, splitpath[1]) 
    240     else: 
    241         cmd = "load -u %s://%s/%s -r 0\n" % (proto, server, splitpath[1]) 
    242  
    243     if proto == "http": 
    244         start_server() 
     233        cmd = "load -u http://%s:%d/%s -r 0\n" % (server, PORT, splitpath[1]) 
     234 
     235        if not os.access(imagefile, os.R_OK): 
     236            print "File access error: %s" % (imagefile) 
     237            sys.exit(3) 
     238 
     239        # make sure we're in the directory where the image is located 
     240        if splitpath[0]: 
     241            os.chdir(splitpath[0]) 
     242 
     243        start_server(server) 
    245244 
    246245    if verbose: 
     
    255254    buf = tn.read_until("Returned 0",10) 
    256255 
     256    # wait till the transfer completed 
     257    buf = tn.read_until("Download completed successfully",20) 
     258    if buf: 
     259        print "Flash update complete!" 
     260        if reboot: 
     261            tn.write("reboot\n") 
     262            print "Rebooting..." 
     263 
    257264tn.write("exit\n") 
    258265tn.close() 
     266 
Note: See TracChangeset for help on using the changeset viewer.