-
Notifications
You must be signed in to change notification settings - Fork 11
/
flash.py
137 lines (123 loc) · 4.14 KB
/
flash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#-----------------------------------------------------------------------------
"""
Flash Commands
This code is the generic front end to flash programming operations.
The vendor specific flash drivers are within the vendor directories.
Those drivers expose a common API used by this code.
"""
#-----------------------------------------------------------------------------
import util
import mem
import iobuf
#-----------------------------------------------------------------------------
_help_erase = (
('*', 'erase all'),
('<address/name> [len]', 'erase memory region'),
(' address', 'address of memory (hex)'),
(' name', 'name of memory region - see "map" command'),
(' len', 'length of memory region (hex) - defaults to region size'),
)
_help_write = (
('<filename> <address/name> [len]', 'write a file to flash'),
(' filename', 'name of file'),
(' address', 'address of memory (hex)'),
(' name', 'name of memory region - see "map" command'),
(' len', 'length of memory region (hex) - defaults to file size'),
)
help_program = (
('<filename>', 'write a firmware file to flash'),
(' filename', 'name of file'),
)
#-----------------------------------------------------------------------------
class flash(object):
def __init__(self, driver, device, mem):
self.driver = driver
self.device = device
self.mem = mem
self.menu = (
('erase', self.cmd_erase, _help_erase),
('info', self.cmd_info),
('write', self.cmd_write, _help_write),
)
def cmd_erase(self, ui, args):
"""erase flash"""
# check for erase all
if len(args) == 1 and args[0] == '*':
ui.put('erase all: ')
n_errors = self.driver.erase_all()
ui.put('done (%d errors)\n' % n_errors)
return
# memory region erase
x = util.mem_args(ui, args, self.device)
if x is None:
return
(adr, n) = x
if n is None:
ui.put('bad erase length\n')
return
r = mem.region(None, adr, n)
# build a list of regions to be erased
erase_list = [x for x in self.driver.sector_list() if r.overlap(x)]
if len(erase_list) == 0:
ui.put('nothing to erase\n')
return
# do the erase
ui.put('erasing : ')
progress = util.progress(ui, 1, len(erase_list))
n_erased = 0
n_errors = 0
for x in erase_list:
n_errors += self.driver.erase(x)
n_erased += 1
progress.update(n_erased)
progress.erase()
ui.put('done (%d errors)\n' % n_errors)
def cmd_write(self, ui,args):
"""write to flash"""
x = util.file_mem_args(ui, args, self.device)
if x is None:
return
(name, adr, n) = x
# check the file
filesize = util.file_arg(ui, name)
if filesize is None:
return
# round up the filesize - the io object will return 0xff for any bytes beyond EOF
filesize = util.roundup(filesize, 32)
if n is None:
# no length on the command line - program the filesize
n = filesize
if n >= filesize:
# region is bigger than the file - program the filesize
n = filesize
else:
# region is smaller than the file - truncate the file
ui.put('%s is larger than target memory: %d > %d bytes (truncating)\n' % (name, filesize, n))
# make sure the target region in flash is suitable
mr = mem.region(None, adr, n)
msg = self.driver.check_region(mr)
if msg is not None:
ui.put('%s\n' % msg)
return
# read from file, write to memory
mf = iobuf.read_file(ui, 'writing %s (%d bytes):' % (name, n), name, n)
self.driver.write(mr, mf)
mf.close(rate = True)
def cmd_info(self, ui,args):
"""display flash information"""
ui.put('%s\n' % self.driver)
def cmd_program(self, ui, args):
"""program firmware file to flash"""
if util.wrong_argc(ui, args, (1,)):
return None
x = util.file_arg(ui, args[0])
if x is None:
return
# erase all
self.cmd_erase(ui, ('*',))
# write to flash
region_name = self.driver.firmware_region()
self.cmd_write(ui, (args[0], region_name))
# verify against the file
self.mem.cmd_verify(ui, (args[0], region_name))
#-----------------------------------------------------------------------------