Skip to content

Commit

Permalink
tbfh: add ShortId support
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Jun 18, 2024
1 parent a0051a1 commit a41b532
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tockloader/tbfh.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TBFTLV:
HEADER_TYPE_PERSISTENT_ACL = 0x07
HEADER_TYPE_KERNEL_VERSION = 0x08
HEADER_TYPE_PROGRAM = 0x09
HEADER_TYPE_SHORT_ID = 0x0A

def get_tlvid(self):
return self.TLVID
Expand Down Expand Up @@ -672,6 +673,42 @@ def object(self):
}


class TBFTLVShortId(TBFTLV):
TLVID = TBFTLV.HEADER_TYPE_SHORT_ID
NUMBER_PARAMETERS = 1
PARAMETER_HELP = "<shortid>"

def __init__(self, buffer, parameters=[]):
self.valid = False

if len(buffer) == 4:
base = struct.unpack("<I", buffer)
self.short_id = base[0]
self.valid = True
else:
try:
if len(parameters) == 1:
self.short_id = int(parameters[0], 0)
self.valid = True
except:
logging.error("Failed parsing params for TLVID={}".format(self.TLVID))

def pack(self):
return struct.pack("<HHI", self.TLVID, 4, self.short_id)

def __str__(self):
out = "TLV: ShortID ({})\n".format(self.TLVID)
out += " {:<20}: {}\n".format("short_id", self.short_id)
return out

def object(self):
return {
"type": "short_id",
"id": self.TLVID,
"short_id": self.short_id,
}


TLV_MAPPINGS = {
"main": TBFTLVMain,
"program": TBFTLVProgram,
Expand All @@ -682,6 +719,7 @@ def object(self):
"permissions": TBFTLVPermissions,
"persistent_acl": TBFTLVPersistentACL,
"kernel_version": TBFTLVKernelVersion,
"short_id": TBFTLVShortId,
}


Expand Down Expand Up @@ -847,6 +885,10 @@ def __init__(self, buffer):
if remaining >= 4 and length == 4:
self.tlvs.append(TBFTLVKernelVersion(buffer[0:4]))

elif tipe == TBFTLV.HEADER_TYPE_SHORT_ID:
if remaining >= 4 and length == 4:
self.tlvs.append(TBFTLVShortId(buffer[0:4]))

else:
logging.warning("Unknown TLV block in TBF header.")
logging.warning("You might want to update tockloader.")
Expand Down

0 comments on commit a41b532

Please sign in to comment.