Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple SEC_UNLOCK sections in BD file #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions spsdk/image/hab/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,35 +507,40 @@ def __init__(self, cmd: CmdUnlockAbstract) -> None:
super().__init__(cmd)

@classmethod
def load_from_config(cls, config: HabConfig, search_paths: Optional[list[str]] = None) -> Self:
def load_from_config(cls, config: HabConfig, search_paths: Optional[list[str]] = None) -> list[Self]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to parse multiple commands of a same type and keep the order of processed commands, we could perhaps update the API of SecCommandBase(and all its child classes) to something like this:
load_from_config(cls, command_config: CommandConfig, hab_config: HabConfig, search_paths: Optional[list[str]] = None) -> Self

Once this is updated in HabContainer class, we will be able to load multiple commands of the same type keeping the order taken from configuration file.

"""Load configuration into the command.

:param config: Section config
:param search_paths: List of paths where to search for the file, defaults to None
:raises SPSDKKeyError: Unknown engine.
:return: List of command instances
"""
command_params = config.commands.get_command_params(SecCommand.UNLOCK)
cls.check_config_section_params(command_params)

unlock_engine = EnumEngine.from_label(command_params["Unlock_Engine"])
if unlock_engine not in UNLOCK_COMMANDS_MAPPING:
raise SPSDKKeyError(f"Unknown engine {unlock_engine}")
klass = UNLOCK_COMMANDS_MAPPING[unlock_engine]
kwargs = {}
unlock_features: str = command_params.get("Unlock_Features")
if unlock_features is not None:
# features may be defined as single feature of coma separated list of features
features = [
klass.FEATURES.from_label(feature.strip()).tag
for feature in unlock_features.split(",")
]
kwargs["features"] = cls.calc_features_value(features)
unlock_uid: str = command_params.get("Unlock_UID")
if unlock_uid:
uids = [int(uid.strip(), 0) for uid in unlock_uid.split(",")]
kwargs["uid"] = cls.calc_uid(uids)
cmd = klass(**kwargs)
return cls(cmd)
cmds = []
for command in config.commands:
if command.index == SecCommand.UNLOCK.tag:
command_params = command.params
cls.check_config_section_params(command_params)

unlock_engine = EnumEngine.from_label(command_params["Unlock_Engine"])
if unlock_engine not in UNLOCK_COMMANDS_MAPPING:
raise SPSDKKeyError(f"Unknown engine {unlock_engine}")
klass = UNLOCK_COMMANDS_MAPPING[unlock_engine]
kwargs = {}
unlock_features: str = command_params.get("Unlock_Features")
if unlock_features is not None:
# features may be defined as single feature or comma-separated list of features
features = [
klass.FEATURES.from_label(feature.strip()).tag
for feature in unlock_features.split(",")
]
kwargs["features"] = cls.calc_features_value(features)
unlock_uid: str = command_params.get("Unlock_UID")
if unlock_uid:
uids = [int(uid.strip(), 0) for uid in unlock_uid.split(",")]
kwargs["uid"] = cls.calc_uid(uids)
cmd = klass(**kwargs)
cmds.append(cls(cmd))
return cmds

@classmethod
def calc_features_value(cls, features: List[int]) -> int:
Expand Down
3 changes: 2 additions & 1 deletion spsdk/image/hab/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ def load_from_config(cls, config: HabConfig, search_paths: Optional[list[str]] =
command_class = COMMANDS_MAPPING.get(SecCommand.from_tag(cmd_config.index))
if not command_class:
raise SPSDKValueError(f"Command with index does not exist {cmd_config.index}")
commands.append(command_class.load_from_config(config, search_paths=search_paths))
cmd_objs = command_class.load_from_config(config, search_paths=search_paths)
commands.extend(cmd_objs if isinstance(cmd_objs, list) else [cmd_objs])
segment = SegCSF(enabled=True, version=header.version)
for command in commands:
segment.append_command(command.cmd)
Expand Down