cornucopia: bump SRCREV
[openembedded.git] / contrib / armmon_xfer / armmon_xfer.py
blobd7a9b7281dd05c5e55951fa1774f01c6659de673
1 #!/usr/bin/python
3 # Small utility to upload kernel and/or ramdisk to
4 # the compulab board through the ARMmon firmware.
5 # Requires pyserial : http://pyserial.sourceforge.net
6 # author: Tobias Pflug
8 import sys, os, serial, getopt, atexit, re
10 # regular expression to match valid IPv4 addresses
11 ipv4_regex = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
13 def wait_flash_complete():
14 while 1:
15 data = s.readline()
16 if quiet != 1:
17 print data,
18 if data.split(" ")[0] == "Finished":
19 break
21 def test_armmon():
22 # write some nonsense to the monitor and
23 # check if armmon replies accordingly
24 s.write("nonsense\n")
25 s.timeout=2
26 s.read(len("nonsense\n")+1)
27 data = s.read(len("Illegal"))
28 if data.split(" ")[0] != "Illegal":
29 sys.exit("Error: ARMmon not responding. Linux running maybe?")
30 s.timeout=None
31 s.flushInput()
32 s.flushOutput()
35 def usage():
36 sys.stderr.write("""USAGE: %s [options] <server ip>
37 armmon_xfer - data upload tool for cmx270/ARMmon
39 options:
40 -p, --port=PORT: serial port, default='/dev/ttyUSB0'
41 -k, --kernel=KERNEL: upload and flash kernel image KERNEL
42 -r, --ramdisk=RAMDISK: upload and flash ramdisk image RAMDISK
43 -b, --bootos: boot Linux
44 -R, --reboot: reboot cmx270
45 -q, --quiet be quiet, little output
46 -h, --help: print this help screen
48 """ % (sys.argv[0],))
50 if __name__ == '__main__':
52 port = "/dev/ttyUSB0"
53 baud = 38400
54 kernel = None
55 ramdisk = None
56 boot = None
57 reboot = None
58 quiet = None
59 tftp_ip = None
61 if len(sys.argv) < 2:
62 usage()
63 sys.exit(0)
65 # parse command line
67 try:
68 opts, args = getopt.getopt(sys.argv[1:],
69 "p:k:r:bRhq",
70 ["port=", "kernel=","ramdisk=","bootos","reboot","help","quiet"]
72 except getopt.GetoptError:
73 usage()
74 sys.exit(2)
76 for o, a in opts:
77 if o in ("-h","--help"):
78 usage()
79 sys.exit()
80 elif o in ("-k","--kernel"):
81 kernel = a
82 elif o in ("-r","--ramdisk"):
83 ramdisk = a
84 elif o in ("-b","--bootos"):
85 boot = 1
86 elif o in ("-R","--reboot"):
87 reboot = 1
88 elif o in ("-p","--port"):
89 port = o
90 elif o in ("-q","--quiet"):
91 quiet = 1
93 # check for arguments and validate IP address
94 # when required by commands
95 if len(args)>0 and re.match(ipv4_regex,args[0]):
96 tftp_ip = args[0]
97 else:
98 if kernel or ramdisk:
99 sys.exit("Error: invalid IP address!")
101 try:
102 s = serial.Serial('/dev/ttyUSB0',38400)
103 except:
104 sys.stderr.write("Error: Could not open port: %s\n" % (port))
105 sys.exit(1)
107 # test if armmon is responding on bogus input
108 test_armmon()
110 # carry out commands
112 if reboot:
113 print "Rebooting compulab ..."
114 s.write("reboot\n")
115 s.write("y\n")
116 s.write("y\n")
117 s.close()
118 sys.exit(0)
120 if kernel:
121 print "Downloading kernel ..."
122 cmd = "download kernel tftp %s %s\n" % (kernel,tftp_ip)
123 s.write(cmd)
124 data = s.readline()
125 data = s.readline()
127 print "Flashing kernel ..."
128 cmd = "flash kernel\n"
129 s.write(cmd)
130 wait_flash_complete()
133 if ramdisk:
134 print "Downloading ramdisk ..."
135 cmd = "download ramdisk tftp %s %s\n" % (ramdisk,tftp_ip)
136 s.write(cmd)
137 s.readline()
138 s.readline()
140 print "Flashing ramdisk ..."
141 cmd = "flash ramdisk\n"
142 s.write(cmd)
143 wait_flash_complete()
145 if boot:
146 print "Booting Linux ..."
147 s.write("bootos\n")
149 s.close()
150 sys.exit(0)