Beispiel #1
0
def get_mbi_classes(
        family: str) -> Dict[str, Tuple[Type["MasterBootImage"], str, str]]:
    """Get all Master Boot Image supported classes for chip family.

    :param family: Chip family.
    :raises SPSDKValueError: The invalid family.
    :return: Dictionary with key like image name and values are Tuple with it's MBI Class
        and target and authentication type.
    """
    with open(DEVICE_FILE) as f:
        device_cfg = YAML(typ="safe").load(f)
    if not family in device_cfg["devices"]:
        raise SPSDKValueError("Not supported family for Master Boot Image")

    ret: Dict[str, Tuple[Type["MasterBootImage"], str, str]] = {}

    images: Dict[str, Dict[str, str]] = device_cfg["devices"][family]["images"]

    for target in images.keys():
        for authentication in images[target]:
            cls_name = images[target][authentication]

            ret[f"{family}_{target}_{authentication}"] = (
                globals()[cls_name],
                device_cfg["map_tables"]["targets"][target][0],
                device_cfg["map_tables"]["authentication"][authentication][0],
            )

    return ret
Beispiel #2
0
def get_bytes_cnt_of_int(value: int,
                         align_to_2n: bool = True,
                         byte_cnt: int = None) -> int:
    """Returns count of bytes needed to store handled integer.

    :param value: Input integer value.
    :param align_to_2n: The result will be aligned to standard sizes 1,2,4,8,12,16,20.
    :param byte_cnt: The result count of bytes.
    :raises SPSDKValueError: The integer input value doesn't fit into byte_cnt.
    :return: Number of bytes needed to store integer.
    """
    cnt = 0
    if value == 0:
        return byte_cnt or 1

    while value != 0:
        value >>= 8
        cnt += 1

    if align_to_2n and cnt > 2:
        cnt = int(ceil(cnt / 4)) * 4

    if byte_cnt and cnt > byte_cnt:
        raise SPSDKValueError(
            f"Value takes more bytes than required byte count{byte_cnt} after align."
        )

    cnt = byte_cnt or cnt

    return cnt
Beispiel #3
0
def get_key_by_val(value: str, dict: Dict[str, List[str]]) -> str:
    """Return key by its value.

    :param value: Value to find.
    :param dict: Dictionary to find in.
    :raises SPSDKValueError: Value is not present in dictionary.
    :return: Key name
    """
    for key, item in dict.items():
        if value.lower() in [x.lower() for x in item]:
            return key

    raise SPSDKValueError(f"Value {value} is not in {dict}.")
Beispiel #4
0
def get_ec_curve_object(name: str) -> ec.EllipticCurve:
    """Get the EC curve object by its name.

    :param name: Name of EC curve.
    :return: EC curve object.
    :raises SPSDKValueError: Invalid EC curve name.
    """
    # pylint: disable=protected-access
    for key_object in ec._CURVE_TYPES:  # type: ignore
        if key_object.lower() == name.lower():
            # pylint: disable=protected-access
            return ec._CURVE_TYPES[key_object]()  # type: ignore

    raise SPSDKValueError(f"The EC curve with name '{name}' is not supported.")
Beispiel #5
0
    def __init__(self, timeout: int, units: str = "s") -> None:
        """Simple timeout class constructor.

        :param timeout: Timeout value.
        :param units: Timeout units (MUST be from the UNITS list)
        :raises SPSDKValueError: Invalid input value.
        """
        if units not in self.UNITS:
            raise SPSDKValueError("Units are not in supported units.")
        self.enabled = timeout != 0
        self.timeout_us = timeout * self.UNITS[units]
        self.start_time_us = self._get_current_time_us()
        self.end_time = self.start_time_us + self.timeout_us
        self.units = units