Skip to content

Commit

Permalink
Merge pull request #499 from NebraLtd/pritam/disregard_all_zero_serial
Browse files Browse the repository at this point in the history
Use proc/cpuinfo for hardware serial #496
  • Loading branch information
pritamghanghas authored Mar 10, 2023
2 parents 54a8478 + 39e300f commit 23c6698
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions hw_diag/utilities/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

logging = get_logger(__name__)

CPUINFO_SERIAL_KEY = "serial"
DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
DBUS_OBJECTMANAGER = 'org.freedesktop.DBus.ObjectManager'

Expand Down Expand Up @@ -260,8 +261,8 @@ def get_serial_number(diagnostics):
try:
cpuinfo = load_cpu_info()
serial_number = ""
if "serial" in cpuinfo:
serial_number = cpuinfo["serial"]
if has_valid_serial(cpuinfo):
serial_number = cpuinfo[CPUINFO_SERIAL_KEY]
else:
serial_number = open("/proc/device-tree/serial-number").readline() \
.rstrip('\x00')
Expand All @@ -273,6 +274,21 @@ def get_serial_number(diagnostics):
diagnostics["serial_number"] = serial_number


def has_valid_serial(cpuinfo: dict) -> bool:
if CPUINFO_SERIAL_KEY not in cpuinfo:
return False

# most systems that don't use /proc/cpuinfo
# end up serving all zeros for serial
serial_number = cpuinfo[CPUINFO_SERIAL_KEY]
if not int(serial_number):
return False

# probably more checks will go in here later
# untill we have a hal.
return True


def load_cpu_info() -> dict:
'''
returns /proc/cpuinfo as dict, keys are case-insensitive
Expand Down

0 comments on commit 23c6698

Please sign in to comment.