]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/gps4020/v2_0/support/download/download.py
Initial revision
[karo-tx-redboot.git] / packages / hal / arm / gps4020 / v2_0 / support / download / download.py
1 #! /usr/bin/env python
2
3 #
4 # Copyright (C) 2003, MLB Associates
5 #
6
7 # This program is used to read a Motorola S-record file and
8 # download it using the GDB protocol.
9
10 import os, string, sys, time
11
12 trace = open("/tmp/download.trace", "w")
13
14 #
15 # Use up some time
16 #
17 def spin():
18     j = 0
19     for i in range(0,200):
20         j = j + 1
21
22 #
23 # Compute the checksum for a string
24 #
25 def cksum(str):
26 #    sys.stderr.write("cksum %s\n" % str)
27     sum = 0
28     cs = str[1:]
29     while cs:
30         sum = sum + ord(cs[:1])
31         cs = cs[1:]
32     return sum & 0xFF
33
34 #
35 # Send a string via the GDB protocol.  Note: this routine
36 # computes and adds the checksum before starting.
37 #
38 def send(str):
39     str = str + "#%02x" % cksum(str)
40 #    trace.write("ready to send: %s\n" % str)
41 #    trace.flush()
42     while 1:
43         s = str
44         while s:
45             os.write(1, s[:1])
46             spin()
47             # time.sleep(0.001)
48             s = s[1:]
49         c = os.read(0, 1)
50         if c <> '+':
51             trace.write("~ACK: %c\n" % c)
52             trace.write("sent: %s\n" % str)
53             trace.flush()
54             continue
55         res = ''
56         while 1:
57             c = os.read(0, 1)
58             if c == '#': break
59             res = res + c
60             # trace.write("ACK: %c, res: %s\n" % (c, res))
61             # trace.flush()
62         # trace.write("res = %s\n" % res)
63         # trace.flush()
64         csum = cksum(res)
65         cs = os.read(0, 1)
66         cs = cs + os.read(0, 1)
67         sum = string.atoi(cs, 16)
68         if csum <> sum:
69             os.write(1, '-')
70             trace.write("RES = %s, sum: %x/%x\n" % (res, csum, sum))
71             trace.write("sent: %s\n" % str)
72             trace.flush()
73             continue
74         os.write(1, '+')
75         trace.flush()
76         return
77
78 #
79 # Process a stream of S-records, supplied by 'readline()'
80 #
81 def download(readline):
82     # send("$Hc-1")
83     # send("$Hg0")
84     last_addr = 0
85     while 1:
86         line = readline()
87         if not line: break
88         if line[0] <> 'S':
89             raise ("Invalid input:" + line)
90         if line[1] in "123":
91             len = string.atoi(line[2:4],16)
92             an = ord(line[1]) - ord('1') + 2
93             ae = 4 + (an*2)
94             addr = string.atoi(line[4:ae],16)
95             #print "len = %d, addr = 0x%x " % (len, addr)
96             len = len - (an+1)
97             line = line[ae:]
98             out = "$M%x,%x:" % (addr, len)
99             for i in range(0,len):
100                 val = string.atoi(line[:2],16)
101                 #print "val = 0x%x" % val
102                 line = line[2:]
103                 out = out + "%02x" % val
104             if (addr - last_addr) >= 0x400:
105                 last_addr = addr
106                 sys.stderr.write("0x%x\n" % addr)
107             send(out)
108         elif line[1] in "789":
109             len = string.atoi(line[2:4],16)
110             eos = 10
111             if line[1] == '7':
112                 eos = 12
113             addr = string.atoi(line[4:12],16)
114             #print "len = %d, addr = 0x%x " % (len, addr)
115             len = len - 4
116             line = line[eos:]
117             out = "$P40=%08x" % addr
118             sys.stderr.write("Set PC = 0x%x\n" % addr)
119             send(out)
120     # This command starts the program
121     send("$c#63")
122         
123 if __name__ == '__main__':                     # testing
124     import sys
125     if len(sys.argv) > 1: download(open(sys.argv[1]).readline)
126     else: download(sys.stdin.readline)