|
| 1 | +############################################################################# |
| 2 | +# |
| 3 | +# Component contains an implementation of SONiC Platform Base API and |
| 4 | +# provides the components firmware management function |
| 5 | +# |
| 6 | +############################################################################# |
| 7 | + |
| 8 | +try: |
| 9 | + import subprocess |
| 10 | + import logging |
| 11 | + from sonic_platform_base.component_base import ComponentBase |
| 12 | +except ImportError as e: |
| 13 | + raise ImportError(str(e) + "- required module not found") |
| 14 | + |
| 15 | +CPLD_ADDR_MAPPING = { |
| 16 | + "CPLD1": ['3', '0x60'], |
| 17 | + "CPLD2": ['3', '0x61'], |
| 18 | + "CPLD3": ['3', '0x62'] |
| 19 | +} |
| 20 | +SYSFS_PATH = "/sys/bus/i2c/devices/" |
| 21 | +BIOS_VERSION_PATH = "/sys/class/dmi/id/bios_version" |
| 22 | +COMPONENT_LIST= [ |
| 23 | + ("CPLD1", "CPLD 1"), |
| 24 | + ("CPLD2", "CPLD 2"), |
| 25 | + ("CPLD3", "CPLD 3"), |
| 26 | + ("BIOS", "Basic Input/Output System") |
| 27 | + |
| 28 | +] |
| 29 | + |
| 30 | +class Component(ComponentBase): |
| 31 | + """Platform-specific Component class""" |
| 32 | + |
| 33 | + DEVICE_TYPE = "component" |
| 34 | + |
| 35 | + def __init__(self, component_index=0): |
| 36 | + self.index = component_index |
| 37 | + self.name = self.get_name() |
| 38 | + |
| 39 | + def __run_command(self, command): |
| 40 | + # Run bash command and print output to stdout |
| 41 | + try: |
| 42 | + process = subprocess.Popen( |
| 43 | + shlex.split(command), stdout=subprocess.PIPE) |
| 44 | + while True: |
| 45 | + output = process.stdout.readline() |
| 46 | + if output == '' and process.poll() is not None: |
| 47 | + break |
| 48 | + rc = process.poll() |
| 49 | + if rc != 0: |
| 50 | + return False |
| 51 | + except Exception: |
| 52 | + return False |
| 53 | + return True |
| 54 | + |
| 55 | + def __get_bios_version(self): |
| 56 | + # Retrieves the BIOS firmware version |
| 57 | + try: |
| 58 | + with open(BIOS_VERSION_PATH, 'r') as fd: |
| 59 | + bios_version = fd.read() |
| 60 | + return bios_version.strip() |
| 61 | + except Exception as e: |
| 62 | + return None |
| 63 | + |
| 64 | + def __get_cpld_version(self): |
| 65 | + # Retrieves the CPLD firmware version |
| 66 | + cpld_version = dict() |
| 67 | + for cpld_name in CPLD_ADDR_MAPPING: |
| 68 | + cmd = "i2cget -f -y {0} {1} 0x1".format(CPLD_ADDR_MAPPING[cpld_name][0], CPLD_ADDR_MAPPING[cpld_name][1]) |
| 69 | + status, value = subprocess.getstatusoutput(cmd) |
| 70 | + if not status: |
| 71 | + cpld_version_raw = value.rstrip() |
| 72 | + cpld_version[cpld_name] = "{}".format(int(cpld_version_raw,16)) |
| 73 | + |
| 74 | + return cpld_version |
| 75 | + |
| 76 | + def get_name(self): |
| 77 | + """ |
| 78 | + Retrieves the name of the component |
| 79 | + Returns: |
| 80 | + A string containing the name of the component |
| 81 | + """ |
| 82 | + return COMPONENT_LIST[self.index][0] |
| 83 | + |
| 84 | + def get_description(self): |
| 85 | + """ |
| 86 | + Retrieves the description of the component |
| 87 | + Returns: |
| 88 | + A string containing the description of the component |
| 89 | + """ |
| 90 | + return COMPONENT_LIST[self.index][1] |
| 91 | + |
| 92 | + def get_firmware_version(self): |
| 93 | + """ |
| 94 | + Retrieves the firmware version of module |
| 95 | + Returns: |
| 96 | + string: The firmware versions of the module |
| 97 | + """ |
| 98 | + fw_version = None |
| 99 | + |
| 100 | + if self.name == "BIOS": |
| 101 | + fw_version = self.__get_bios_version() |
| 102 | + elif "CPLD" in self.name: |
| 103 | + cpld_version = self.__get_cpld_version() |
| 104 | + fw_version = cpld_version.get(self.name) |
| 105 | + |
| 106 | + return fw_version |
| 107 | + |
| 108 | + def install_firmware(self, image_path): |
| 109 | + """ |
| 110 | + Install firmware to module |
| 111 | + Args: |
| 112 | + image_path: A string, path to firmware image |
| 113 | + Returns: |
| 114 | + A boolean, True if install successfully, False if not |
| 115 | + """ |
| 116 | + raise NotImplementedError |
0 commit comments