forked from sonic-net/sonic-platform-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeprom_base.py
419 lines (354 loc) · 13.2 KB
/
eeprom_base.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Copyright 2012 Cumulus Networks LLC, all rights reserved
#############################################################################
# Base eeprom class containing the main logic for reading, writing, and
# setting the eeprom. The format definition is a list of tuples of:
# ('data name', 'data type', 'size in bytes')
# data type is one of 's', 'C', and 'x' (string, char, and ignore)
# 'burn' as a data name indicates the corresponding number of bytes are to
# be ignored
from __future__ import print_function
try:
import binascii
import os
import io
import sys
import struct
import fcntl
except ImportError as e:
raise ImportError (str(e) + "- required module not found")
class EepromDecoder(object):
def __init__(self, path, format, start, status, readonly):
self.p = path
self.f = format
self.s = start
self.u = status
self.r = readonly
# Warning: the following members are deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO.
self.cache_name = None
self.cache_update_needed = False
self.lock_file = None
def check_status(self):
if self.u != '':
F = None
try:
F = open(self.u, "r")
d = F.readline().rstrip()
except IOError as e:
raise IOError("Failed to check status : %s" % (str(e)))
finally:
if F is not None:
F.close()
return d
else:
return 'ok'
def set_cache_name(self, name):
# Warning: this method is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO.
# before accessing the eeprom we acquire an exclusive lock on the eeprom file.
# this will prevent a race condition where multiple instances of this app
# could try to update the cache at the same time
self.cache_name = name
self.lock_file = open(self.p, 'r')
fcntl.flock(self.lock_file, fcntl.LOCK_EX)
def is_read_only(self):
return self.r
def decoder(self, s, t):
return t
def encoder(self, I, v):
return v
def checksum_field_size(self):
return 4 # default
def is_checksum_field(self, I):
return I[0] == 'crc' # default
def checksum_type(self):
return 'crc32'
def encode_checksum(self, crc):
if self.checksum_field_size() == 4:
return struct.pack('>I', crc)
elif self.checksum_field_size() == 1:
return struct.pack('>B', crc)
print('checksum type not yet supported')
sys.exit(1)
def compute_2s_complement(self, e, size):
crc = 0
loc = 0
end = len(e)
while loc != end:
crc += int('0x' + binascii.b2a_hex(e[loc:loc+size]), 0)
loc += size
T = 1 << (size * 8)
return (T - crc) & (T - 1)
def compute_dell_crc(self, message):
poly = 0x8005
reg = 0x0000
message += bytearray(b'\x00\x00')
for byte in message:
mask = 0x80
while (mask > 0):
reg<<=1
if byte & mask:
reg += 1
mask>>=1
if reg > 0xffff:
reg &= 0xffff
reg ^= poly
return reg
def calculate_checksum(self, e):
if self.checksum_type() == 'crc32':
return binascii.crc32(e) & 0xffffffff
if self.checksum_type() == '2s-complement':
size = self.checksum_field_size()
return self.compute_2s_complement(e, size)
if self.checksum_type() == 'dell-crc':
return self.compute_dell_crc(e)
print('checksum type not yet supported')
sys.exit(1)
def is_checksum_valid(self, e):
offset = 0 - self.checksum_field_size()
crc = self.calculate_checksum(e[:offset])
loc = 0
for I in self.f:
end = loc + I[2]
t = e[loc:end]
loc = end
if self.is_checksum_field(I):
i = self.decoder(I[0], t)
if int(i, 0) == crc:
return (True, crc)
else:
return (False, crc)
else:
continue
return (False, crc)
def decode_eeprom(self, e):
loc = 0
for I in self.f:
end = loc + I[2]
t = e[loc:end]
loc = end
if I[0] == 'burn':
continue
elif I[1] == 's':
i = t
else:
i = self.decoder(I[0], t)
print("%-20s: %s" %(I[0], i))
def set_eeprom(self, e, cmd_args):
line = ''
loc = 0
ndict = {}
fields = list(I[0] for I in list(self.f))
if len(cmd_args):
for arg in cmd_args[0].split(','):
k, v = arg.split('=')
k = k.strip()
v = v.strip()
if k not in fields:
print("Error: invalid field '%s'" %(k))
sys.exit(1)
ndict[k] = v
for I in self.f:
# print the original value
end = loc + I[2]
sl = e[loc:end]
loc = end
if I[0] == 'burn':
#line += sl
# fill with zeros
line = line.ljust(len(line) + I[2], '\x00')
continue
elif I[1] == 's':
i = sl
else:
i = self.decoder(I[0], sl)
if len(cmd_args) == 0:
if self.is_checksum_field(I):
print("%-20s: %s " %(I[0], i))
continue
# prompt for new value
v = raw_input("%-20s: [%s] " %(I[0], i))
if v == '':
v = i
else:
if I[0] not in ndict.keys():
v = i
else:
v = ndict[I[0]]
line += self.encoder(I, v)
# compute and append crc at the end
crc = self.encode_checksum(self.calculate_checksum(line))
line += crc
return line
def open_eeprom(self):
'''
Open the EEPROM device file.
If a cache file exists, use that instead of the EEPROM.
'''
using_eeprom = True
eeprom_file = self.p
try:
# Warning: cache file is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO. This
# code need to be adjusted once cache file is completely removing from the system.
if os.path.isfile(self.cache_name):
eeprom_file = self.cache_name
using_eeprom = False
except Exception:
pass
self.cache_update_needed = using_eeprom
return io.open(eeprom_file, "rb")
def read_eeprom(self):
sizeof_info = 0
for I in self.f:
sizeof_info += I[2]
o = self.read_eeprom_bytes(sizeof_info)
return o
def read_eeprom_bytes(self, byteCount, offset=0):
F = None
try:
F = self.open_eeprom()
F.seek(self.s + offset)
o = F.read(byteCount)
# If we read from the cache file and the byte count isn't what we
# expect, the file may be corrupt. Delete it and try again, this
# time reading from the actual EEPROM.
if len(o) != byteCount and not self.cache_update_needed:
# Warning: cache file is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO. This
# code needs to be adjusted once cache file is completely removed from the system.
os.remove(self.cache_name)
self.cache_update_needed = True
F.close()
F = self.open_eeprom()
F.seek(self.s + offset)
o = F.read(byteCount)
if len(o) != byteCount:
raise RuntimeError("Expected to read %d bytes from %s, "
% (byteCount, self.p) +
"but only read %d" % (len(o)))
except IOError as e:
raise IOError("Failed to read eeprom : %s" % (str(e)))
finally:
if F is not None:
F.close()
return bytearray(o)
def read_eeprom_db(self):
return 0
def write_eeprom(self, e):
F = None
try:
F = open(self.p, "wb")
F.seek(self.s)
F.write(e)
except IOError as e:
raise IOError("Failed to write eeprom : %s" % (str(e)))
finally:
if F is not None:
F.close()
self.write_cache(e)
def write_cache(self, e):
# Warning: this method is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO.
if self.cache_name:
F = None
try:
F = open(self.cache_name, "wb")
F.seek(self.s)
F.write(e)
except IOError as e:
raise IOError("Failed to write cache : %s" % (str(e)))
finally:
if F is not None:
F.close()
def update_cache(self, e):
# Warning: this method is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO.
if self.cache_update_needed:
self.write_cache(e)
fcntl.flock(self.lock_file, fcntl.LOCK_UN)
self.lock_file.close()
def update_eeprom_db(self, e):
return 0
def diff_mac(self, mac1, mac2):
if mac1 == '' or mac2 == '':
return 0
mac1_octets = []
mac1_octets = mac1.split(':')
mac1val = int(mac1_octets[5], 16) | int(mac1_octets[4], 16) << 8 | int(mac1_octets[3], 16) << 16
mac2_octets = []
mac2_octets = mac2.split(':')
mac2val = int(mac2_octets[5], 16) | int(mac2_octets[4], 16) << 8 | int(mac2_octets[3], 16) << 16
# check oui matches
if (mac1_octets[0] != mac2_octets[0]
or mac1_octets[1] != mac2_octets[1]
or mac1_octets[2] != mac2_octets[2]) :
return 0
if mac2val < mac1val:
return 0
return (mac2val - mac1val)
def increment_mac(self, mac):
if mac != "":
mac_octets = []
mac_octets = mac.split(':')
ret_mac = int(mac_octets[5], 16) | int(mac_octets[4], 16) << 8 | int(mac_octets[3], 16) << 16
ret_mac = ret_mac + 1
if (ret_mac & 0xff000000):
print('Error: increment carries into OUI')
return ''
mac_octets[5] = hex(ret_mac & 0xff)[2:].zfill(2)
mac_octets[4] = hex((ret_mac >> 8) & 0xff)[2:].zfill(2)
mac_octets[3] = hex((ret_mac >> 16) & 0xff)[2:].zfill(2)
return ':'.join(mac_octets).upper()
return ''
@classmethod
def find_field(cls, e, name):
if not hasattr(cls, 'brd_fmt'):
raise RuntimeError("Class %s does not have brb_fmt" % cls)
if not e:
raise RuntimeError("EEPROM can not be empty")
brd_fmt = cls.brd_fmt
loc = 0
for f in brd_fmt:
end = loc + f[2]
t = e[loc:end]
loc = end
if f[0] == name:
return t
def base_mac_addr(self, e):
'''
Returns the base MAC address found in the EEPROM.
Sub-classes must override this method as reading the EEPROM
and finding the base MAC address entails platform specific
details.
See also mgmtaddrstr() and switchaddrstr().
'''
print("ERROR: Platform did not implement base_mac_addr()")
raise NotImplementedError
def mgmtaddrstr(self, e):
'''
Returns the base MAC address to use for the Ethernet
management interface(s) on the CPU complex.
By default this is the same as the base MAC address listed in
the EEPROM.
See also switchaddrstr().
'''
return self.base_mac_addr(e)
def switchaddrstr(self, e):
'''
Returns the base MAC address to use for the switch ASIC
interfaces.
By default this is *next* address after the base MAC address
listed in the EEPROM.
See also mgmtaddrstr().
'''
return self.increment_mac(self.base_mac_addr(e))
def switchaddrrange(self, e):
# this function is in the base class only to catch errors
# the platform specific import should have an override of this method
# to provide the allocated mac range from syseeprom or flash sector or
# wherever that platform stores this info
print("Platform did not indicate allocated mac address range")
raise NotImplementedError
def serial_number_str(self, e):
raise NotImplementedError("Platform did not indicate serial number")