From 39a18410920986e625551158e708ce9a304c4943 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Wed, 10 Apr 2013 09:51:01 +0300 Subject: gpt_parser: add backup GPT header reading support Change-Id: Ie496acd5b8020a2a48a4c905be2467dda338cf12 Signed-off-by: Artem Bityutskiy --- mic/utils/gpt_parser.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/mic/utils/gpt_parser.py b/mic/utils/gpt_parser.py index 0476bfb..52e1a6c 100644 --- a/mic/utils/gpt_parser.py +++ b/mic/utils/gpt_parser.py @@ -98,7 +98,7 @@ class GptParser: self.disk_obj.close() - def read_header(self): + def read_header(self, primary = True): """ Read and verify the GPT header and return a tuple containing the following elements: @@ -108,9 +108,12 @@ class GptParser: Size of a single partition entry, CRC32 of partition array) This tuple corresponds to the GPT header format. Please, see the UEFI - standard for the description of these fields. """ + standard for the description of these fields. - # The header sits at LBA 1 - read it + If the 'primary' parameter is 'True', the primary GPT header is read, + otherwise the backup GPT header is read instead. """ + + # Read and validate the primary GPT header self.disk_obj.seek(self.sector_size) try: header = self.disk_obj.read(struct.calcsize(_GPT_HEADER_FORMAT)) @@ -121,6 +124,18 @@ class GptParser: header = struct.unpack(_GPT_HEADER_FORMAT, header) _validate_header(header) + if not primary: + # Read and validate the backup GPT header + self.disk_obj.seek(header[6] * self.sector_size) + try: + header = self.disk_obj.read(struct.calcsize(_GPT_HEADER_FORMAT)) + except IOError as err: + raise MountError("cannot read from file '%s': %s" % \ + (self.disk_path, err)) + + header = struct.unpack(_GPT_HEADER_FORMAT, header) + _validate_header(header) + return (header[0], # 0. Signature header[1], # 1. Revision header[2], # 2. Header size in bytes -- cgit v1.2.3