-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathflashutils.py
More file actions
165 lines (140 loc) · 6.07 KB
/
flashutils.py
File metadata and controls
165 lines (140 loc) · 6.07 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Copyright (c) 2011
# Telecooperation Office (TecO), Universitaet Karlsruhe (TH), Germany.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# 3. Neither the name of the Universitaet Karlsruhe (TH) nor the names
# of its contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Author(s): Philipp Scholl <scholl@teco.edu>
import sys
class JennicProtocol:
def __init__(self):
self.mac_region = range(0x00000030, 0x00000038)
self.lic_region = range(0x00000038, 0x00000048)
self.mac, self.lic = None, None
self.isverbose = None
self.preferedblocksize = None
self.select_flash()
def select_flash(self):
self.identify_flash()
if not self.flash_jennicid in (0x00, 0x01, 0x02, 0x03):
print "unsupported flash type"
sys.exit(1)
status = self.talk(0x2C, 0x2D, data = [self.flash_jennicid])[0]
if not status == 0:
print "could not select detected flash type was: %d"%status
sys.exit(1)
def identify_flash(self):
flash = self.talk(0x25, 0x26)
self.flash_status = flash[0]
self.flash_manufacturer = flash[1]
self.flash_type = flash[2]
if not self.flash_status == 0:
print "flash status != 0 (%c)"%self.flash_status
sys.exit(0)
if self.flash_manufacturer == 0x10 and self.flash_type == 0x10:
self.flash_manufacturer = "ST"
self.flash_type = "M25P10-A"
self.flash_jennicid = 0x00
elif self.flash_manufacturer == 0xBF and self.flash_type == 0x49:
self.flash_manufacturer = "SST"
self.flash_type = "25VF010A"
self.flash_jennicid = 0x01
elif self.flash_manufacturer == 0x1f and (self.flash_type == 0x60\
or self.flash_type == 0x65):
self.flash_manufacturer = "Atmel"
self.flash_type = "25F512"
self.flash_jennicid = 0x02
elif self.flash_manufacturer == 0x12 and self.flash_type == 0x12:
self.flash_manufacturer = "ST"
self.flash_type = "M25P40"
self.flash_jennicid = 0x03
else:
self.flash_manufacturer = "unknown"
self.flash_type = "unknown"
self.flash_jennicid = 0xFF
def crc(self, arr, len):
""" calculates the crc
"""
crc = 0
for i in range(0,len):
crc ^= arr[i]
return crc
def set_mac(self, s):
self.mac = []
for i in range(0, len(s), 2):
if s[i:i+2] != "0x":
self.mac.append( int( s[i:i+2], 16 ) )
if not len(self.mac)==len(self.mac_region):
print "mac must be %i byte long"%len(self.mac_region)
sys.exit(1)
def set_license(self, s):
self.lic = []
for i in range(0, len(s), 2):
if s[i:i+2] != "0x":
self.lic.append( int( s[i:i+2], 16 ) )
if not len(self.lic)==len(self.lic_region):
print "license must be %i byte long"%len(self.lic_region)
sys.exit(1)
def erase_flash(self):
""" read mac and license key prior to erasing
"""
if not self.mac:
self.mac = self.read_mac()
if not self.lic:
self.lic = self.read_license()
#assert len(self.mac)==len(self.mac_region), "read mac addr too short"
#assert len(self.lic)==len(self.lic_region), "read license too short"
#if not self.talk( 0x0F, 0x10, data=[0x00] )[0] == 0:
# print("disabling write protection failed")
# sys.exit(1)
if not self.talk( 0x07, 0x08 )[0] == 0:
print("erasing did not work")
sys.exit(1)
def read_mac(self):
return self.read_flash(self.mac_region[0], len(self.mac_region))
def read_license(self):
return self.read_flash(self.lic_region[0], len(self.lic_region))
def write_license(self):
self.write_flash(self.lic_region[0], self.lic)
def write_mac(self):
self.write_flash(self.mac_region[0], self.mac)
def write_flash(self, addr, list):
status = self.talk( 0x09, 0x0A, addr, data=list)
if status[0] != 0:
raise Exception, "writing failed for addr %i status=%i len=%i"%(addr, status[0], len(status))
def read_flash(self, addr, len):
""" reads len bytes starting at address addr from
flash memory.
"""
return self.talk( 0x0B, 0x0C, addr, len )[1:] # strip command status
def read_ram(self, addr, len):
""" reads len bytes starting at address addr from
ram.
"""
return self.talk( 0x1F, 0x20, addr, len )[1:] # strip command status
def finish(self):
pass