Skip to content

Commit

Permalink
liblk: Fix partition parsing for legacy LK images
Browse files Browse the repository at this point in the history
* In legacy LK images, typically only a single, primary partition
  exists, identified as 'lk'. These images don't feature additional
  partitions with external headers, making it hard to guess when we
  should stop the parsing loop.

* To address this limitation, the parsing process should include a
  check to ascertain if the most recently extracted partition is 'lk'.
  This way, if we hit an invalid magic, we'll break out of the loop if
  the current LK is a legacy image.
  • Loading branch information
R0rt1z2 committed Dec 9, 2023
1 parent a216c59 commit f036dc4
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion liblk/LkImage.py
Expand Up @@ -56,7 +56,10 @@ def parse_partitions(self, rename_duplicates=False) -> None:
# Always make sure the partition header magic is valid, this is the only way to tell
# whether the partition is valid or not.
if not partition.header.magic == Magic.MAGIC:
raise InvalidLkPartition(f"Invalid magic 0x{partition.header.magic:x} at offset 0x{offset:x}")
if len(self.partitions) != 0 and self.partitions[-1]["name"].lower() == 'lk':
break # In legacy LK images, this is completely fine, so just break the loop.
else:
raise InvalidLkPartition(f"Invalid magic 0x{partition.header.magic:x} at offset 0x{offset:x}")

# There are certain cases where one partition is repeated more than once. In order
# to avoid name collisions, append a number to the name (e.g. "cert1" -> "cert1 (1)").
Expand Down

0 comments on commit f036dc4

Please sign in to comment.