Skip to content

Commit 821fcb4

Browse files
committed
python: add -l option
1 parent b9afcb8 commit 821fcb4

File tree

1 file changed

+76
-44
lines changed

1 file changed

+76
-44
lines changed

spd-eeprom.py

Lines changed: 76 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ def i2c_smbus_write_byte_data(fd, addr, reg, val):
9696

9797
def print_usage():
9898
print("Usage:")
99+
print(" ", sys.argv[0], "-l")
99100
print(" ", sys.argv[0], "-r -d DIMM -f FILE")
100101
print(" ", sys.argv[0], "-w -d DIMM -f FILE")
101102
print()
102103
print("Options:")
104+
print(" -l list used DIMM slots")
103105
print(" -r read data from SPD EEPROM (output to file)")
104106
print(" -w write data to SPD EEPROM (input from file)")
105107
print(" -d DIMM DIMM slot index (0 - 7)")
@@ -121,7 +123,7 @@ def spd_read(fd, smbus_idx, dimm_slot, file_path):
121123

122124
ee1004 = spd_set_page(fd, 0)
123125

124-
print("Reading from", "EE1004" if ee1004 else "AT24", "SPD EEPROM: 0x5%d" % dimm_slot, "at SMBus %d" % smbus_idx)
126+
print("Reading from %s SPD EEPROM: 0x5%d on SMBus %d" % ("EE1004" if ee1004 else "AT24", dimm_slot, smbus_idx))
125127

126128
spd_file = open(file_path, "wb")
127129

@@ -169,7 +171,7 @@ def spd_write(fd, smbus_idx, dimm_slot, file_path):
169171
print("The SPD file must be exactly 512 bytes!")
170172
sys.exit(1)
171173

172-
print("Writing to", "EE1004" if ee1004 else "AT24", "SPD EEPROM: 0x5%d" % dimm_slot, "at SMBus %d" % smbus_idx)
174+
print("Writing to %s SPD EEPROM: 0x5%d on SMBus %d" % ("EE1004" if ee1004 else "AT24", dimm_slot, smbus_idx))
173175

174176
print("\nWARNING! Writing wrong data to SPD EEPROM will leave your system UNBOOTABLE!")
175177
ans = input("Continue anyway? [y/N] ").lower()
@@ -209,40 +211,7 @@ def spd_write(fd, smbus_idx, dimm_slot, file_path):
209211

210212
print("\nWrite done.")
211213

212-
def main():
213-
if os.getuid():
214-
print("Please run as root.")
215-
sys.exit(1)
216-
217-
try:
218-
opts, _args = getopt.getopt(sys.argv[1:], "rwd:f:")
219-
except getopt.error:
220-
print_usage()
221-
sys.exit(1)
222-
223-
eeprom_rw = 0
224-
smbus_idx = ""
225-
dimm_slot = ""
226-
file_path = ""
227-
228-
for opt, arg in opts:
229-
if opt in ("-r"):
230-
eeprom_rw = 1
231-
elif opt in ("-w"):
232-
eeprom_rw = 2
233-
elif opt in ("-d"):
234-
dimm_slot = arg
235-
elif opt in ("-f"):
236-
file_path = arg
237-
238-
if len(opts) != 3 \
239-
or eeprom_rw == 0 \
240-
or len(file_path) == 0 \
241-
or not dimm_slot.isdigit() \
242-
or not 0 <= int(dimm_slot) <= 7:
243-
print_usage()
244-
sys.exit(1)
245-
214+
def smbus_probe(dimm_slot = None):
246215
try:
247216
args = ["rmmod", "at24", "ee1004", "eeprom"]
248217
proc = subprocess.Popen(args=args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -254,6 +223,8 @@ def main():
254223
except Exception:
255224
pass
256225

226+
smbus_idx = ""
227+
257228
files = os.listdir("/dev")
258229
files = list(filter(lambda x: x.startswith("i2c-"), files))
259230

@@ -265,21 +236,82 @@ def main():
265236

266237
if smbus_idx.isdigit():
267238
smbus_idx = int(smbus_idx)
268-
dimm_slot = int(dimm_slot)
269239
else:
270240
print("SMBus adapter not found.")
271241
sys.exit(1)
272242

243+
if dimm_slot == None:
244+
print("Probing for SPD EEPROM on SMBus %d" % smbus_idx)
245+
print()
246+
247+
eeprom = 0
248+
ee1004 = spd_set_page(fd, 0)
249+
250+
for slot in range(0, 8):
251+
try:
252+
i2c_smbus_read_byte(fd, 0x50 + slot)
253+
254+
print("DIMM slot %d: %s SPD EEPROM" % (slot, "512 Byte EE1004" if ee1004 else "256 Byte AT24"))
255+
256+
eeprom += 1
257+
except IOError:
258+
pass
259+
260+
if eeprom == 0:
261+
print("No SPD EEPROM detected.")
262+
else:
263+
try:
264+
i2c_smbus_read_byte(fd, 0x50 + dimm_slot)
265+
except IOError:
266+
print("DIMM slot %d is empty." % dimm_slot)
267+
sys.exit(1)
268+
269+
return fd, smbus_idx
270+
271+
def main():
272+
if os.getuid():
273+
print("Please run as root.")
274+
sys.exit(1)
275+
273276
try:
274-
i2c_smbus_read_byte(fd, 0x50 + dimm_slot)
275-
except IOError:
276-
print("DIMM slot %d is empty." % dimm_slot)
277+
opts, _args = getopt.getopt(sys.argv[1:], "lrwd:f:")
278+
except getopt.error:
279+
print_usage()
277280
sys.exit(1)
278281

279-
if eeprom_rw == 1:
280-
spd_read(fd, smbus_idx, dimm_slot, file_path)
281-
elif eeprom_rw == 2:
282-
spd_write(fd, smbus_idx, dimm_slot, file_path)
282+
op_code = 0
283+
dimm_slot = ""
284+
file_path = ""
285+
286+
for opt, arg in opts:
287+
if opt in ("-l"):
288+
op_code = 1
289+
elif opt in ("-r"):
290+
op_code = 2
291+
elif opt in ("-w"):
292+
op_code = 3
293+
elif opt in ("-d"):
294+
dimm_slot = arg
295+
elif opt in ("-f"):
296+
file_path = arg
297+
298+
if op_code == 1 and len(opts) == 1:
299+
smbus_probe()
300+
elif op_code != 0 \
301+
and len(opts) == 3 \
302+
and len(file_path) != 0 \
303+
and dimm_slot.isdigit() \
304+
and 0 <= int(dimm_slot) <= 7:
305+
306+
fd, smbus_idx = smbus_probe(int(dimm_slot))
307+
308+
if op_code == 2:
309+
spd_read(fd, smbus_idx, int(dimm_slot), file_path)
310+
elif op_code == 3:
311+
spd_write(fd, smbus_idx, int(dimm_slot), file_path)
312+
else:
313+
print_usage()
314+
sys.exit(1)
283315

284316
if __name__ == "__main__":
285317
try:

0 commit comments

Comments
 (0)