Skip to content

Commit b4b80ea

Browse files
committed
add tockloader tbf convert command
1 parent b6dd660 commit b4b80ea

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ Interact with TLV structures within a TBF.
134134

135135
Add and remove credentials in the TBF footer.
136136

137+
#### `tockloader tbf convert [output format]`
138+
139+
Convert a TBF to a different format.
140+
137141
#### `tockloader tickv get|append|invalidate|dump|cleanup|reset [key] [value]`
138142

139143
Interact with a TicKV key-value database.

tockloader/app_tab.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,47 @@ def get_binary(self, address):
407407
else:
408408
raise ("Only valid for one TBF file.")
409409

410+
def convert(self, format):
411+
"""
412+
Convert a TAB-based app to a different format. Valid formats:
413+
- `cbinary`: Create a C struct with a binary representation of the app.
414+
415+
This is only valid if there is one TBF file.
416+
"""
417+
418+
def trim_trailing_zeroes(b):
419+
for i in range(len(b) - 1, 0, -1):
420+
if b[i] != 0x0:
421+
return b[0 : i + 1]
422+
423+
def format_data_strings(b):
424+
def chunked(source, size):
425+
for i in range(0, len(source), size):
426+
yield source[i : i + size]
427+
428+
out = "{"
429+
rows = []
430+
for chunk in chunked(b, 10):
431+
row = ["0x{:02x}".format(c) for c in chunk]
432+
rows.append(", ".join(row))
433+
return "{{{}}}".format(",\n ".join(rows))
434+
435+
if format == "cbinary":
436+
output = """
437+
struct tock_app app = {{
438+
"{name}",
439+
{length},
440+
{data},
441+
}};
442+
"""
443+
name = self.get_name()
444+
binary = self.get_binary(0)
445+
length = len(binary)
446+
trimmed = trim_trailing_zeroes(binary)
447+
data = format_data_strings(trimmed)
448+
out = output.format(name=name, length=length, data=data)
449+
return out
450+
410451
def get_names_and_binaries(self):
411452
"""
412453
Return (filename, binary) tuples for each contained TBF. This is for

tockloader/main.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,41 @@ def command_tbf_credential_delete(args):
557557
tab.update_tbf(app)
558558

559559

560+
def command_tbf_convert(args):
561+
tabs = collect_tabs(args)
562+
563+
if len(tabs) == 0:
564+
raise TockLoaderException("No TABs found to inspect")
565+
566+
if len(tabs) > 1:
567+
tab_names = [tab.get_app_name() for tab in tabs]
568+
index = helpers.menu_new(
569+
tab_names + ["None"],
570+
return_type="index",
571+
title="Which TAB to use to convert a TBF?",
572+
)
573+
if index >= len(tabs):
574+
return
575+
tab = tabs[index]
576+
else:
577+
tab = tabs[0]
578+
579+
# Ask the user if they want to see more detail about a certain TBF.
580+
tbf_names = tab.get_tbf_names()
581+
index = helpers.menu_new(
582+
tbf_names + ["None"],
583+
return_type="index",
584+
title="Which TBF to convert?",
585+
)
586+
if index >= len(tbf_names):
587+
return
588+
589+
app = tab.extract_tbf(tbf_names[index])
590+
591+
converted = app.convert(args.format)
592+
print(converted)
593+
594+
560595
def command_dump_flash_page(args):
561596
tock_loader = TockLoader(args)
562597
tock_loader.open()
@@ -1343,6 +1378,26 @@ def main():
13431378
"tab", help="The TAB or TABs to modify", nargs="*"
13441379
)
13451380

1381+
#################
1382+
## TBF CONVERT ##
1383+
#################
1384+
1385+
tbf_convert = tbf_subparser.add_parser(
1386+
"convert",
1387+
parents=[parent],
1388+
help="Commands for converting TBFs into different formats",
1389+
)
1390+
1391+
tbf_convert.set_defaults(func=command_tbf_convert)
1392+
tbf_convert.add_argument(
1393+
"format",
1394+
help="Output format",
1395+
choices=[
1396+
"cbinary",
1397+
],
1398+
)
1399+
tbf_convert.add_argument("tab", help="The TAB or TABs to modify", nargs="*")
1400+
13461401
###########
13471402
## TICKV ##
13481403
###########

0 commit comments

Comments
 (0)