Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
contained_files = self.tab.getnames()
# A TBF name is in the format: ..tbf
for contained_file in contained_files:
name_pieces = contained_file.split('.')
if len(name_pieces) >= 2 and name_pieces[-1] == 'tbf':
if name_pieces[0] == arch:
matching_tbf_filenames.append(contained_file)
# Get all of the TBF headers and app binaries to create a TabApp.
tbfs = []
for tbf_filename in matching_tbf_filenames:
binary_tarinfo = self.tab.getmember(tbf_filename)
binary = self.tab.extractfile(binary_tarinfo).read()
# First get the TBF header from the correct binary in the TAB
tbfh = TBFHeader(binary)
if tbfh.is_valid():
# Check that total size actually matches the binary that we got.
if tbfh.get_app_size() < len(binary):
# It's fine if the binary is smaller, but the binary cannot be
# longer than the amount of reserved space (`total_size` in the
# TBF header) for the app.
raise TockLoaderException('Invalid TAB, the app binary is longer than its defined total_size')
tbfs.append((tbfh, binary[tbfh.get_size_before_app():]))
else:
raise TockLoaderException('Invalid TBF found in app in TAB')
return TabApp(tbfs)
# erase covers a large area of flash. In that case, we must read all
# apps and then re-write them, since otherwise they will be erased.
#
# So, we iterate through all apps and read them into memory if we are
# doing an erase and re-flash cycle or if the app has moved.
app_address = address
for app in apps:
# If we do not already have a binary, and any of the conditions are
# met, we need to read the app binary from the board.
if (not app.has_app_binary()) and \
(self.args.bundle_apps or
app.get_address() != app_address or
app.is_modified()):
logging.debug('Reading app {} binary from board.'.format(app))
entire_app = self.channel.read_range(app.address, app.get_size())
in_flash_tbfh = TBFHeader(entire_app)
app.set_app_binary(entire_app[in_flash_tbfh.get_header_size():])
app_address += app.get_size()
# Need to know the address we are putting each app at.
app_address = address
# Actually write apps to the board.
if self.args.bundle_apps:
# Tockloader has been configured to bundle all apps as a single
# binary. Here we concatenate all apps and then call flash once.
#
# This should be compatible with all boards, but if there several
# existing apps they have to be re-flashed, and that could add
# significant overhead. So, we prefer to flash only what has changed
# and special case this bundle operation.
# This can be the default, it can be configured in the attributes on
# the hardware, or it can be passed in to Tockloader.
address = self.channel.get_apps_start_address()
# Jump through the linked list of apps
while (True):
header_length = 200 # Version 2
flash = self.channel.read_range(address, header_length)
# if there was an error, the binary array will be empty
if len(flash) < header_length:
break
# Get all the fields from the header
tbfh = TBFHeader(flash)
if tbfh.is_valid():
app = InstalledApp(tbfh, address)
apps.append(app)
address += app.get_size()
else:
break
if self.args.debug:
logging.debug('Found {} app{} on the board.'.format(len(apps), helpers.plural(len(apps))))
for i,app in enumerate(apps):
logging.debug(' {}. {}'.format(i+1, app))
return apps
def extract_tbf (self, tbf_name):
'''
Return a `TabApp` object from this TAB. You must specify the desired TBF
name, and only that TBF will be returned.
'''
tbf_filename = '{}.tbf'.format(tbf_name)
binary_tarinfo = self.tab.getmember(tbf_filename)
binary = self.tab.extractfile(binary_tarinfo).read()
# First get the TBF header from the correct binary in the TAB
tbfh = TBFHeader(binary)
if tbfh.is_valid():
# Check that total size actually matches the binary that we got.
if tbfh.get_app_size() < len(binary):
# It's fine if the binary is smaller, but the binary cannot be
# longer than the amount of reserved space (`total_size` in the
# TBF header) for the app.
raise TockLoaderException('Invalid TAB, the app binary is longer than its defined total_size')
return TabApp([(tbfh, binary[tbfh.get_size_before_app():])])
else:
raise TockLoaderException('Invalid TBF found in app in TAB')