#
# Copyright 2020 Google, Inc.
#
+# Copyright (c) 2020 ARM Limited
+# All rights reserved
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder. You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
import string
from subprocess import CalledProcessError, Popen, PIPE, STDOUT
from sys import exit, argv
-
+import re
# Some constants.
MaxLBACylinders = 16383
if returncode != 0:
print(out)
exit(returncode)
+
+ # Parse each line of the sfdisk output looking for the first
+ # partition description.
+ SFDISK_PARTITION_INFO_RE = re.compile(
+ r"^\s*" # Start of line
+ r"(?P<name>\S+)" # Name
+ r"\s*:\s*" # Separator
+ r"start=\s*(?P<start>\d+),\s*" # Partition start record
+ r"size=\s*(?P<size>\d+),\s*" # Partition size record
+ r"type=(?P<type>\d+)" # Partition type record
+ r"\s*$" # End of line
+ )
lines = out.splitlines()
- # Make sure the first few lines of the output look like what we expect.
- assert(lines[0][0] == '#' or lines[0].startswith('label:'))
- assert(lines[1] == 'unit: sectors' or lines[1].startswith('label-id:'))
- assert(lines[2] == '' or lines[2].startswith('device:'))
- if lines[0][0] == '#' :
- # Parsing an 'old style' dump oputput
- # Line 4 has information about the first partition.
- chunks = lines[3].split()
- else :
- # Parsing a 'new style' dump oputput
- # Line 6 has information about the first partition.
- chunks = lines[5].split()
- # The fourth chunk is the offset of the partition in sectors followed by
- # a comma. We drop the comma and convert that to an integer.
- sectors = int(chunks[3][:-1])
+ for line in lines :
+ match = SFDISK_PARTITION_INFO_RE.match(line)
+ if match:
+ sectors = int(match.group("start"))
+ break
+ else:
+ # No partition description was found
+ print("No partition description was found in sfdisk output:")
+ print("\n".join(" {}".format(line.rstrip()) for line in lines))
+ print("Could not determine size of first partition.")
+ exit(1)
+
# Free the loopback device and return an answer.
dev.destroy()
return sectors * BlockSize