Skip to content

Commit

Permalink
support deleting TLV from footer
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Sep 5, 2022
1 parent e4b3056 commit 6656a4d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
5 changes: 3 additions & 2 deletions tockloader/app_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,13 @@ def align_down_to(v, a):
else:
return None

def delete_tbfh_tlv(self, tlvid):
def delete_tlv(self, tlvid):
"""
Delete a particular TLV from each TBF header.
Delete a particular TLV from each TBF header and footer.
"""
for tbf in self.tbfs:
tbf.tbfh.delete_tlv(tlvid)
tbf.tbff.delete_tlv(tlvid)

def modify_tbfh_tlv(self, tlvid, field, value):
"""
Expand Down
6 changes: 3 additions & 3 deletions tockloader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ def command_tbf_delete_tlv(args):
tabs = collect_tabs(args)

if len(tabs) == 0:
raise TockLoaderException("No TABs found, no TBF headers to process")
raise TockLoaderException("No TABs found, no TBF to process")

tlvid = args.tlvid
logging.status("Removing TLV ID {} from TBF headers...".format(tlvid))
logging.status("Removing TLV ID {} from TBF...".format(tlvid))
for tab in tabs:
# Ask the user which TBF binaries to update.
tbf_names = tab.get_tbf_names()
Expand All @@ -369,7 +369,7 @@ def command_tbf_delete_tlv(args):
for i, tbf_name in enumerate(tbf_names):
if i == index or index == len(tbf_names):
app = tab.extract_tbf(tbf_name)
app.delete_tbfh_tlv(tlvid)
app.delete_tlv(tlvid)
tab.update_tbf(app)


Expand Down
19 changes: 18 additions & 1 deletion tockloader/tbfh.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ def __init__(self, size):
self.fields["checksum"] = self._checksum(self.get_binary())


class TBFFooterTLVCredentials:
class TBFFooterTLVCredentials(TBFTLV):
"""
Represent a Credentials TLV in the footer of a TBF.
"""
Expand Down Expand Up @@ -1120,6 +1120,8 @@ def __init__(self, tbfh, buffer):
self.version = tbfh.version
# List of all TLVs in the footer.
self.tlvs = []
# Keep track if tockloader has modified the footer.
self.modified = False

# Iterate all TLVs and add to list.
position = 0
Expand All @@ -1136,6 +1138,21 @@ def __init__(self, tbfh, buffer):

buffer = buffer[tlv_length:]

def delete_tlv(self, tlvid):
"""
Delete a particular TLV by ID if it exists.
"""
indices = []
for i, tlv in enumerate(self.tlvs):
if tlv.get_tlvid() == tlvid:
# Reverse the list
indices.insert(0, i)
# Pop starting with the last match
for index in indices:
logging.debug("Removing TLV at index {}".format(index))
self.tlvs.pop(index)
self.modified = True

def get_binary(self):
"""
Get the TBF footer in a bytes array.
Expand Down

0 comments on commit 6656a4d

Please sign in to comment.