Yilin Yang | 19da693 | 2019-12-10 13:39:28 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 2 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """An utility to manipulate GPT on a disk image. |
| 7 | |
| 8 | Chromium OS factory software usually needs to access partitions from disk |
| 9 | images. However, there is no good, lightweight, and portable GPT utility. |
| 10 | Most Chromium OS systems use `cgpt`, but that's not by default installed on |
| 11 | Ubuntu. Most systems have parted (GNU) or partx (util-linux-ng) but they have |
| 12 | their own problems. |
| 13 | |
| 14 | For example, when a disk image is resized (usually enlarged for putting more |
| 15 | resources on stateful partition), GPT table must be updated. However, |
| 16 | - `parted` can't repair partition without interactive console in exception |
| 17 | handler. |
| 18 | - `partx` cannot fix headers nor make changes to partition table. |
| 19 | - `cgpt repair` does not fix `LastUsableLBA` so we cannot enlarge partition. |
| 20 | - `gdisk` is not installed on most systems. |
| 21 | |
| 22 | As a result, we need a dedicated tool to help processing GPT. |
| 23 | |
| 24 | This pygpt.py provides a simple and customized implementation for processing |
| 25 | GPT, as a replacement for `cgpt`. |
| 26 | """ |
| 27 | |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 28 | import argparse |
| 29 | import binascii |
Yilin Yang | f9fe193 | 2019-11-04 17:09:34 +0800 | [diff] [blame] | 30 | import codecs |
Hung-Te Lin | 138389f | 2018-05-15 17:55:00 +0800 | [diff] [blame] | 31 | import itertools |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 32 | import logging |
| 33 | import os |
Hung-Te Lin | 446eb51 | 2018-05-02 18:39:16 +0800 | [diff] [blame] | 34 | import stat |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 35 | import struct |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 36 | import subprocess |
| 37 | import sys |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 38 | import uuid |
| 39 | |
| 40 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 41 | class StructError(Exception): |
| 42 | """Exceptions in packing and unpacking from/to struct fields.""" |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 43 | |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 44 | |
Fei Shao | bd07c9a | 2020-06-15 19:04:50 +0800 | [diff] [blame] | 45 | class StructField: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 46 | """Definition of a field in struct. |
| 47 | |
| 48 | Attributes: |
| 49 | fmt: a format string for struct.{pack,unpack} to use. |
| 50 | name: a string for name of processed field. |
| 51 | """ |
| 52 | __slots__ = ['fmt', 'name'] |
| 53 | |
| 54 | def __init__(self, fmt, name): |
| 55 | self.fmt = fmt |
| 56 | self.name = name |
| 57 | |
| 58 | def Pack(self, value): |
| 59 | """"Packs given value from given format.""" |
| 60 | del self # Unused. |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 61 | if isinstance(value, str): |
| 62 | value = value.encode('utf-8') |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 63 | return value |
| 64 | |
| 65 | def Unpack(self, value): |
| 66 | """Unpacks given value into given format.""" |
| 67 | del self # Unused. |
| 68 | return value |
| 69 | |
| 70 | |
| 71 | class UTF16StructField(StructField): |
| 72 | """A field in UTF encoded string.""" |
Yilin Yang | e4e40e9 | 2019-10-31 09:57:57 +0800 | [diff] [blame] | 73 | __slots__ = ['max_length'] |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 74 | encoding = 'utf-16-le' |
| 75 | |
| 76 | def __init__(self, max_length, name): |
| 77 | self.max_length = max_length |
| 78 | fmt = '%ds' % max_length |
| 79 | super(UTF16StructField, self).__init__(fmt, name) |
| 80 | |
| 81 | def Pack(self, value): |
| 82 | new_value = value.encode(self.encoding) |
| 83 | if len(new_value) >= self.max_length: |
| 84 | raise StructError('Value "%s" cannot be packed into field %s (len=%s)' % |
| 85 | (value, self.name, self.max_length)) |
| 86 | return new_value |
| 87 | |
| 88 | def Unpack(self, value): |
| 89 | return value.decode(self.encoding).strip('\x00') |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 90 | |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 91 | |
| 92 | class GUID(uuid.UUID): |
| 93 | """A special UUID that defaults to upper case in str().""" |
| 94 | |
| 95 | def __str__(self): |
| 96 | """Returns GUID in upper case.""" |
| 97 | return super(GUID, self).__str__().upper() |
| 98 | |
| 99 | @staticmethod |
| 100 | def Random(): |
| 101 | return uuid.uuid4() |
| 102 | |
| 103 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 104 | class GUIDStructField(StructField): |
| 105 | """A GUID field.""" |
| 106 | |
| 107 | def __init__(self, name): |
| 108 | super(GUIDStructField, self).__init__('16s', name) |
| 109 | |
| 110 | def Pack(self, value): |
| 111 | if value is None: |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 112 | return b'\x00' * 16 |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 113 | if not isinstance(value, uuid.UUID): |
| 114 | raise StructError('Field %s needs a GUID value instead of [%r].' % |
| 115 | (self.name, value)) |
| 116 | return value.bytes_le |
| 117 | |
| 118 | def Unpack(self, value): |
| 119 | return GUID(bytes_le=value) |
| 120 | |
| 121 | |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 122 | def BitProperty(getter, setter, shift, mask): |
| 123 | """A generator for bit-field properties. |
| 124 | |
| 125 | This is used inside a class to manipulate an integer-like variable using |
| 126 | properties. The getter and setter should be member functions to change the |
| 127 | underlying member data. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 128 | |
| 129 | Args: |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 130 | getter: a function to read integer type variable (for all the bits). |
| 131 | setter: a function to set the new changed integer type variable. |
| 132 | shift: integer for how many bits should be shifted (right). |
| 133 | mask: integer for the mask to filter out bit field. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 134 | """ |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 135 | def _getter(self): |
| 136 | return (getter(self) >> shift) & mask |
| 137 | def _setter(self, value): |
| 138 | assert value & mask == value, ( |
| 139 | 'Value %s out of range (mask=%s)' % (value, mask)) |
| 140 | setter(self, getter(self) & ~(mask << shift) | value << shift) |
| 141 | return property(_getter, _setter) |
| 142 | |
| 143 | |
Fei Shao | bd07c9a | 2020-06-15 19:04:50 +0800 | [diff] [blame] | 144 | class PartitionAttributes: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 145 | """Wrapper for Partition.Attributes. |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 146 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 147 | This can be created using Partition.attrs, but the changed properties won't |
| 148 | apply to underlying Partition until an explicit call with |
| 149 | Partition.Update(Attributes=new_attrs). |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 150 | """ |
| 151 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 152 | def __init__(self, attrs): |
| 153 | self._attrs = attrs |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 154 | |
| 155 | @property |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 156 | def raw(self): |
| 157 | """Returns the raw integer type attributes.""" |
| 158 | return self._Get() |
| 159 | |
| 160 | def _Get(self): |
| 161 | return self._attrs |
| 162 | |
| 163 | def _Set(self, value): |
| 164 | self._attrs = value |
| 165 | |
| 166 | successful = BitProperty(_Get, _Set, 56, 1) |
| 167 | tries = BitProperty(_Get, _Set, 52, 0xf) |
| 168 | priority = BitProperty(_Get, _Set, 48, 0xf) |
| 169 | legacy_boot = BitProperty(_Get, _Set, 2, 1) |
| 170 | required = BitProperty(_Get, _Set, 0, 1) |
| 171 | raw_16 = BitProperty(_Get, _Set, 48, 0xffff) |
| 172 | |
| 173 | |
| 174 | class PartitionAttributeStructField(StructField): |
| 175 | |
| 176 | def Pack(self, value): |
| 177 | if not isinstance(value, PartitionAttributes): |
| 178 | raise StructError('Given value %r is not %s.' % |
| 179 | (value, PartitionAttributes.__name__)) |
| 180 | return value.raw |
| 181 | |
| 182 | def Unpack(self, value): |
| 183 | return PartitionAttributes(value) |
| 184 | |
| 185 | |
Yilin Yang | 9cf532e | 2019-12-13 12:02:59 +0800 | [diff] [blame] | 186 | # The binascii.crc32 returns unsigned integer in python3, so CRC32 in struct |
| 187 | # must be declared as 'unsigned' (L). |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 188 | # http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_table_header_.28LBA_1.29 |
| 189 | HEADER_FIELDS = [ |
| 190 | StructField('8s', 'Signature'), |
| 191 | StructField('4s', 'Revision'), |
| 192 | StructField('L', 'HeaderSize'), |
Yilin Yang | 9cf532e | 2019-12-13 12:02:59 +0800 | [diff] [blame] | 193 | StructField('L', 'CRC32'), |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 194 | StructField('4s', 'Reserved'), |
| 195 | StructField('Q', 'CurrentLBA'), |
| 196 | StructField('Q', 'BackupLBA'), |
| 197 | StructField('Q', 'FirstUsableLBA'), |
| 198 | StructField('Q', 'LastUsableLBA'), |
| 199 | GUIDStructField('DiskGUID'), |
| 200 | StructField('Q', 'PartitionEntriesStartingLBA'), |
| 201 | StructField('L', 'PartitionEntriesNumber'), |
| 202 | StructField('L', 'PartitionEntrySize'), |
Yilin Yang | 9cf532e | 2019-12-13 12:02:59 +0800 | [diff] [blame] | 203 | StructField('L', 'PartitionArrayCRC32'), |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 204 | ] |
| 205 | |
| 206 | # http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_entries |
| 207 | PARTITION_FIELDS = [ |
| 208 | GUIDStructField('TypeGUID'), |
| 209 | GUIDStructField('UniqueGUID'), |
| 210 | StructField('Q', 'FirstLBA'), |
| 211 | StructField('Q', 'LastLBA'), |
| 212 | PartitionAttributeStructField('Q', 'Attributes'), |
| 213 | UTF16StructField(72, 'Names'), |
| 214 | ] |
| 215 | |
| 216 | # The PMBR has so many variants. The basic format is defined in |
| 217 | # https://en.wikipedia.org/wiki/Master_boot_record#Sector_layout, and our |
| 218 | # implementation, as derived from `cgpt`, is following syslinux as: |
Stimim Chen | 38241b4 | 2020-12-08 13:07:34 +0800 | [diff] [blame^] | 219 | # https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/HEAD/cgpt/cgpt.h#32 |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 220 | PMBR_FIELDS = [ |
| 221 | StructField('424s', 'BootCode'), |
| 222 | GUIDStructField('BootGUID'), |
| 223 | StructField('L', 'DiskID'), |
| 224 | StructField('2s', 'Magic'), |
| 225 | StructField('16s', 'LegacyPart0'), |
| 226 | StructField('16s', 'LegacyPart1'), |
| 227 | StructField('16s', 'LegacyPart2'), |
| 228 | StructField('16s', 'LegacyPart3'), |
| 229 | StructField('2s', 'Signature'), |
| 230 | ] |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 231 | |
| 232 | |
Hung-Te Lin | 4dfd330 | 2018-04-17 14:47:52 +0800 | [diff] [blame] | 233 | class GPTError(Exception): |
| 234 | """All exceptions by GPT.""" |
Hung-Te Lin | 4dfd330 | 2018-04-17 14:47:52 +0800 | [diff] [blame] | 235 | |
| 236 | |
Fei Shao | bd07c9a | 2020-06-15 19:04:50 +0800 | [diff] [blame] | 237 | class GPTObject: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 238 | """A base object in GUID Partition Table. |
| 239 | |
| 240 | All objects (for instance, header or partition entries) must inherit this |
| 241 | class and define the FIELD attribute with a list of field definitions using |
| 242 | StructField. |
| 243 | |
| 244 | The 'name' in StructField will become the attribute name of GPT objects that |
| 245 | can be directly packed into / unpacked from. Derived (calculated from existing |
| 246 | attributes) attributes should be in lower_case. |
| 247 | |
| 248 | It is also possible to attach some additional properties to the object as meta |
| 249 | data (for example path of the underlying image file). To do that, first |
| 250 | include it in __slots__ list and specify them as dictionary-type args in |
| 251 | constructors. These properties will be preserved when you call Clone(). |
| 252 | |
| 253 | To create a new object, call the constructor. Field data can be assigned as |
| 254 | in arguments, or give nothing to initialize as zero (see Zero()). Field data |
| 255 | and meta values can be also specified in keyword arguments (**kargs) at the |
| 256 | same time. |
| 257 | |
| 258 | To read a object from file or stream, use class method ReadFrom(source). |
| 259 | To make changes, modify the field directly or use Update(dict), or create a |
| 260 | copy by Clone() first then Update. |
| 261 | |
| 262 | To wipe all fields (but not meta), call Zero(). There is currently no way |
| 263 | to clear meta except setting them to None one by one. |
| 264 | """ |
| 265 | __slots__ = [] |
| 266 | |
Peter Shih | 533566a | 2018-09-05 17:48:03 +0800 | [diff] [blame] | 267 | FIELDS = [] |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 268 | """A list of StructField definitions.""" |
| 269 | |
| 270 | def __init__(self, *args, **kargs): |
| 271 | if args: |
| 272 | if len(args) != len(self.FIELDS): |
| 273 | raise GPTError('%s need %s arguments (found %s).' % |
| 274 | (type(self).__name__, len(self.FIELDS), len(args))) |
| 275 | for f, value in zip(self.FIELDS, args): |
| 276 | setattr(self, f.name, value) |
| 277 | else: |
| 278 | self.Zero() |
| 279 | |
Yilin Yang | 4c74ca0 | 2020-07-22 16:12:01 +0800 | [diff] [blame] | 280 | all_names = list(self.__slots__) |
Yilin Yang | 879fbda | 2020-05-14 13:52:30 +0800 | [diff] [blame] | 281 | for name, value in kargs.items(): |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 282 | if name not in all_names: |
| 283 | raise GPTError('%s does not support keyword arg <%s>.' % |
| 284 | (type(self).__name__, name)) |
| 285 | setattr(self, name, value) |
| 286 | |
| 287 | def __iter__(self): |
| 288 | """An iterator to return all fields associated in the object.""" |
| 289 | return (getattr(self, f.name) for f in self.FIELDS) |
| 290 | |
| 291 | def __repr__(self): |
| 292 | return '(%s: %s)' % (type(self).__name__, ', '.join( |
Peter Shih | e6afab3 | 2018-09-11 17:16:48 +0800 | [diff] [blame] | 293 | '%s=%r' % (f, getattr(self, f)) for f in self.__slots__)) |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 294 | |
| 295 | @classmethod |
| 296 | def GetStructFormat(cls): |
| 297 | """Returns a format string for struct to use.""" |
| 298 | return '<' + ''.join(f.fmt for f in cls.FIELDS) |
| 299 | |
| 300 | @classmethod |
| 301 | def ReadFrom(cls, source, **kargs): |
| 302 | """Returns an object from given source.""" |
| 303 | obj = cls(**kargs) |
| 304 | obj.Unpack(source) |
| 305 | return obj |
| 306 | |
| 307 | @property |
| 308 | def blob(self): |
| 309 | """The (packed) blob representation of the object.""" |
| 310 | return self.Pack() |
| 311 | |
| 312 | @property |
| 313 | def meta(self): |
| 314 | """Meta values (those not in GPT object fields).""" |
Fei Shao | a161e75 | 2020-06-16 18:14:51 +0800 | [diff] [blame] | 315 | metas = set(self.__slots__) - {f.name for f in self.FIELDS} |
Fei Shao | 9507580 | 2020-06-16 16:55:25 +0800 | [diff] [blame] | 316 | return {name: getattr(self, name) for name in metas} |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 317 | |
| 318 | def Unpack(self, source): |
| 319 | """Unpacks values from a given source. |
| 320 | |
| 321 | Args: |
| 322 | source: a string of bytes or a file-like object to read from. |
| 323 | """ |
| 324 | fmt = self.GetStructFormat() |
| 325 | if source is None: |
| 326 | source = '\x00' * struct.calcsize(fmt) |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 327 | if not isinstance(source, (str, bytes)): |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 328 | return self.Unpack(source.read(struct.calcsize(fmt))) |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 329 | if isinstance(source, str): |
| 330 | source = source.encode('utf-8') |
| 331 | for f, value in zip(self.FIELDS, struct.unpack(fmt.encode('utf-8'), |
| 332 | source)): |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 333 | setattr(self, f.name, f.Unpack(value)) |
Yilin Yang | 840fdc4 | 2020-01-16 16:37:42 +0800 | [diff] [blame] | 334 | return None |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 335 | |
| 336 | def Pack(self): |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 337 | """Packs values in all fields into a bytes by struct format.""" |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 338 | return struct.pack(self.GetStructFormat(), |
| 339 | *(f.Pack(getattr(self, f.name)) for f in self.FIELDS)) |
| 340 | |
| 341 | def Clone(self): |
| 342 | """Clones a new instance.""" |
| 343 | return type(self)(*self, **self.meta) |
| 344 | |
| 345 | def Update(self, **dargs): |
| 346 | """Applies multiple values in current object.""" |
Yilin Yang | 879fbda | 2020-05-14 13:52:30 +0800 | [diff] [blame] | 347 | for name, value in dargs.items(): |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 348 | setattr(self, name, value) |
| 349 | |
| 350 | def Zero(self): |
| 351 | """Set all fields to values representing zero or empty. |
| 352 | |
| 353 | Note the meta attributes won't be cleared. |
| 354 | """ |
Fei Shao | bd07c9a | 2020-06-15 19:04:50 +0800 | [diff] [blame] | 355 | class ZeroReader: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 356 | """A /dev/zero like stream.""" |
| 357 | |
| 358 | @staticmethod |
| 359 | def read(num): |
| 360 | return '\x00' * num |
| 361 | |
| 362 | self.Unpack(ZeroReader()) |
| 363 | |
| 364 | |
Fei Shao | bd07c9a | 2020-06-15 19:04:50 +0800 | [diff] [blame] | 365 | class GPT: |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 366 | """A GPT helper class. |
| 367 | |
| 368 | To load GPT from an existing disk image file, use `LoadFromFile`. |
| 369 | After modifications were made, use `WriteToFile` to commit changes. |
| 370 | |
| 371 | Attributes: |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 372 | header: a namedtuple of GPT header. |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 373 | pmbr: a namedtuple of Protective MBR. |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 374 | partitions: a list of GPT partition entry nametuple. |
| 375 | block_size: integer for size of bytes in one block (sector). |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 376 | is_secondary: boolean to indicate if the header is from primary or backup. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 377 | """ |
Hung-Te Lin | f148d32 | 2018-04-13 10:24:42 +0800 | [diff] [blame] | 378 | DEFAULT_BLOCK_SIZE = 512 |
Hung-Te Lin | 43d54c1 | 2019-03-22 11:15:59 +0800 | [diff] [blame] | 379 | # Old devices uses 'Basic data' type for stateful partition, and newer devices |
| 380 | # should use 'Linux (fS) data' type; so we added a 'stateful' suffix for |
| 381 | # migration. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 382 | TYPE_GUID_MAP = { |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 383 | GUID('00000000-0000-0000-0000-000000000000'): 'Unused', |
Hung-Te Lin | 43d54c1 | 2019-03-22 11:15:59 +0800 | [diff] [blame] | 384 | GUID('EBD0A0A2-B9E5-4433-87C0-68B6B72699C7'): 'Basic data stateful', |
| 385 | GUID('0FC63DAF-8483-4772-8E79-3D69D8477DE4'): 'Linux data', |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 386 | GUID('FE3A2A5D-4F32-41A7-B725-ACCC3285A309'): 'ChromeOS kernel', |
| 387 | GUID('3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC'): 'ChromeOS rootfs', |
| 388 | GUID('2E0A753D-9E48-43B0-8337-B15192CB1B5E'): 'ChromeOS reserved', |
| 389 | GUID('CAB6E88E-ABF3-4102-A07A-D4BB9BE3C1D3'): 'ChromeOS firmware', |
| 390 | GUID('C12A7328-F81F-11D2-BA4B-00A0C93EC93B'): 'EFI System Partition', |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 391 | } |
Fei Shao | 9507580 | 2020-06-16 16:55:25 +0800 | [diff] [blame] | 392 | TYPE_GUID_FROM_NAME = { |
| 393 | 'efi' if v.startswith('EFI') else v.lower().split()[-1]: k |
| 394 | for k, v in TYPE_GUID_MAP.items()} |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 395 | TYPE_GUID_UNUSED = TYPE_GUID_FROM_NAME['unused'] |
| 396 | TYPE_GUID_CHROMEOS_KERNEL = TYPE_GUID_FROM_NAME['kernel'] |
| 397 | TYPE_GUID_LIST_BOOTABLE = [ |
| 398 | TYPE_GUID_CHROMEOS_KERNEL, |
| 399 | TYPE_GUID_FROM_NAME['efi'], |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 400 | ] |
| 401 | |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 402 | class ProtectiveMBR(GPTObject): |
| 403 | """Protective MBR (PMBR) in GPT.""" |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 404 | FIELDS = PMBR_FIELDS |
| 405 | __slots__ = [f.name for f in FIELDS] |
| 406 | |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 407 | SIGNATURE = b'\x55\xAA' |
| 408 | MAGIC = b'\x1d\x9a' |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 409 | |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 410 | class Header(GPTObject): |
| 411 | """Wrapper to Header in GPT.""" |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 412 | FIELDS = HEADER_FIELDS |
| 413 | __slots__ = [f.name for f in FIELDS] |
| 414 | |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 415 | SIGNATURES = [b'EFI PART', b'CHROMEOS'] |
| 416 | SIGNATURE_IGNORE = b'IGNOREME' |
| 417 | DEFAULT_REVISION = b'\x00\x00\x01\x00' |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 418 | |
| 419 | DEFAULT_PARTITION_ENTRIES = 128 |
| 420 | DEFAULT_PARTITIONS_LBA = 2 # LBA 0 = MBR, LBA 1 = GPT Header. |
| 421 | |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 422 | @classmethod |
| 423 | def Create(cls, size, block_size, pad_blocks=0, |
| 424 | part_entries=DEFAULT_PARTITION_ENTRIES): |
| 425 | """Creates a header with default values. |
| 426 | |
| 427 | Args: |
| 428 | size: integer of expected image size. |
| 429 | block_size: integer for size of each block (sector). |
| 430 | pad_blocks: number of preserved sectors between header and partitions. |
| 431 | part_entries: number of partitions to include in header. |
| 432 | """ |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 433 | PART_FORMAT = GPT.Partition.GetStructFormat() |
| 434 | FORMAT = cls.GetStructFormat() |
| 435 | part_entry_size = struct.calcsize(PART_FORMAT) |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 436 | parts_lba = cls.DEFAULT_PARTITIONS_LBA + pad_blocks |
| 437 | parts_bytes = part_entries * part_entry_size |
Yilin Yang | 14d02a2 | 2019-11-01 11:32:03 +0800 | [diff] [blame] | 438 | parts_blocks = parts_bytes // block_size |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 439 | if parts_bytes % block_size: |
| 440 | parts_blocks += 1 |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 441 | # CRC32 and PartitionsCRC32 must be updated later explicitly. |
| 442 | return cls( |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 443 | Signature=cls.SIGNATURES[0], |
| 444 | Revision=cls.DEFAULT_REVISION, |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 445 | HeaderSize=struct.calcsize(FORMAT), |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 446 | CurrentLBA=1, |
Yilin Yang | 14d02a2 | 2019-11-01 11:32:03 +0800 | [diff] [blame] | 447 | BackupLBA=size // block_size - 1, |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 448 | FirstUsableLBA=parts_lba + parts_blocks, |
Yilin Yang | 14d02a2 | 2019-11-01 11:32:03 +0800 | [diff] [blame] | 449 | LastUsableLBA=size // block_size - parts_blocks - parts_lba, |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 450 | DiskGUID=GUID.Random(), |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 451 | PartitionEntriesStartingLBA=parts_lba, |
| 452 | PartitionEntriesNumber=part_entries, |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 453 | PartitionEntrySize=part_entry_size) |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 454 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 455 | def UpdateChecksum(self): |
| 456 | """Updates the CRC32 field in GPT header. |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 457 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 458 | Note the PartitionArrayCRC32 is not touched - you have to make sure that |
| 459 | is correct before calling Header.UpdateChecksum(). |
| 460 | """ |
| 461 | self.Update(CRC32=0) |
| 462 | self.Update(CRC32=binascii.crc32(self.blob)) |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 463 | |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 464 | class Partition(GPTObject): |
| 465 | """The partition entry in GPT. |
| 466 | |
| 467 | Please include following properties when creating a Partition object: |
| 468 | - image: a string for path to the image file the partition maps to. |
| 469 | - number: the 1-based partition number. |
| 470 | - block_size: an integer for size of each block (LBA, or sector). |
| 471 | """ |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 472 | FIELDS = PARTITION_FIELDS |
| 473 | __slots__ = [f.name for f in FIELDS] + ['image', 'number', 'block_size'] |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 474 | NAMES_ENCODING = 'utf-16-le' |
| 475 | NAMES_LENGTH = 72 |
| 476 | |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 477 | def __str__(self): |
| 478 | return '%s#%s' % (self.image, self.number) |
| 479 | |
| 480 | def IsUnused(self): |
| 481 | """Returns if the partition is unused and can be allocated.""" |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 482 | return self.TypeGUID == GPT.TYPE_GUID_UNUSED |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 483 | |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 484 | def IsChromeOSKernel(self): |
| 485 | """Returns if the partition is a Chrome OS kernel partition.""" |
Hung-Te Lin | 048ac5e | 2018-05-03 23:47:45 +0800 | [diff] [blame] | 486 | return self.TypeGUID == GPT.TYPE_GUID_CHROMEOS_KERNEL |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 487 | |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 488 | @property |
| 489 | def blocks(self): |
| 490 | """Return size of partition in blocks (see block_size).""" |
| 491 | return self.LastLBA - self.FirstLBA + 1 |
| 492 | |
| 493 | @property |
| 494 | def offset(self): |
| 495 | """Returns offset to partition in bytes.""" |
| 496 | return self.FirstLBA * self.block_size |
| 497 | |
| 498 | @property |
| 499 | def size(self): |
| 500 | """Returns size of partition in bytes.""" |
| 501 | return self.blocks * self.block_size |
| 502 | |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 503 | def __init__(self): |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 504 | """GPT constructor. |
| 505 | |
| 506 | See LoadFromFile for how it's usually used. |
| 507 | """ |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 508 | self.pmbr = None |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 509 | self.header = None |
| 510 | self.partitions = None |
Hung-Te Lin | f148d32 | 2018-04-13 10:24:42 +0800 | [diff] [blame] | 511 | self.block_size = self.DEFAULT_BLOCK_SIZE |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 512 | self.is_secondary = False |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 513 | |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 514 | @classmethod |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 515 | def GetTypeGUID(cls, value): |
| 516 | """The value may be a GUID in string or a short type string.""" |
| 517 | guid = cls.TYPE_GUID_FROM_NAME.get(value.lower()) |
| 518 | return GUID(value) if guid is None else guid |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 519 | |
| 520 | @classmethod |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 521 | def Create(cls, image_name, size, block_size, pad_blocks=0): |
| 522 | """Creates a new GPT instance from given size and block_size. |
| 523 | |
| 524 | Args: |
| 525 | image_name: a string of underlying disk image file name. |
| 526 | size: expected size of disk image. |
| 527 | block_size: size of each block (sector) in bytes. |
| 528 | pad_blocks: number of blocks between header and partitions array. |
| 529 | """ |
| 530 | gpt = cls() |
| 531 | gpt.block_size = block_size |
| 532 | gpt.header = cls.Header.Create(size, block_size, pad_blocks) |
| 533 | gpt.partitions = [ |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 534 | cls.Partition(block_size=block_size, image=image_name, number=i + 1) |
Yilin Yang | bf84d2e | 2020-05-13 10:34:46 +0800 | [diff] [blame] | 535 | for i in range(gpt.header.PartitionEntriesNumber)] |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 536 | return gpt |
| 537 | |
Hung-Te Lin | 446eb51 | 2018-05-02 18:39:16 +0800 | [diff] [blame] | 538 | @staticmethod |
| 539 | def IsBlockDevice(image): |
| 540 | """Returns if the image is a block device file.""" |
| 541 | return stat.S_ISBLK(os.stat(image).st_mode) |
| 542 | |
| 543 | @classmethod |
| 544 | def GetImageSize(cls, image): |
| 545 | """Returns the size of specified image (plain or block device file).""" |
| 546 | if not cls.IsBlockDevice(image): |
| 547 | return os.path.getsize(image) |
| 548 | |
| 549 | fd = os.open(image, os.O_RDONLY) |
| 550 | try: |
| 551 | return os.lseek(fd, 0, os.SEEK_END) |
| 552 | finally: |
| 553 | os.close(fd) |
| 554 | |
| 555 | @classmethod |
| 556 | def GetLogicalBlockSize(cls, block_dev): |
| 557 | """Returns the logical block (sector) size from a block device file. |
| 558 | |
| 559 | The underlying call is BLKSSZGET. An alternative command is blockdev, |
| 560 | but that needs root permission even if we just want to get sector size. |
| 561 | """ |
| 562 | assert cls.IsBlockDevice(block_dev), '%s must be block device.' % block_dev |
| 563 | return int(subprocess.check_output( |
| 564 | ['lsblk', '-d', '-n', '-r', '-o', 'log-sec', block_dev]).strip()) |
| 565 | |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 566 | @classmethod |
Hung-Te Lin | 6977ae1 | 2018-04-17 12:20:32 +0800 | [diff] [blame] | 567 | def LoadFromFile(cls, image): |
| 568 | """Loads a GPT table from give disk image file object. |
| 569 | |
| 570 | Args: |
| 571 | image: a string as file path or a file-like object to read from. |
| 572 | """ |
Yilin Yang | 0724c9d | 2019-11-15 15:53:45 +0800 | [diff] [blame] | 573 | if isinstance(image, str): |
Hung-Te Lin | 6977ae1 | 2018-04-17 12:20:32 +0800 | [diff] [blame] | 574 | with open(image, 'rb') as f: |
| 575 | return cls.LoadFromFile(f) |
| 576 | |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 577 | gpt = cls() |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 578 | image.seek(0) |
| 579 | pmbr = gpt.ProtectiveMBR.ReadFrom(image) |
| 580 | if pmbr.Signature == cls.ProtectiveMBR.SIGNATURE: |
| 581 | logging.debug('Found MBR signature in %s', image.name) |
| 582 | if pmbr.Magic == cls.ProtectiveMBR.MAGIC: |
| 583 | logging.debug('Found PMBR in %s', image.name) |
| 584 | gpt.pmbr = pmbr |
| 585 | |
Hung-Te Lin | f148d32 | 2018-04-13 10:24:42 +0800 | [diff] [blame] | 586 | # Try DEFAULT_BLOCK_SIZE, then 4K. |
Hung-Te Lin | 446eb51 | 2018-05-02 18:39:16 +0800 | [diff] [blame] | 587 | block_sizes = [cls.DEFAULT_BLOCK_SIZE, 4096] |
| 588 | if cls.IsBlockDevice(image.name): |
| 589 | block_sizes = [cls.GetLogicalBlockSize(image.name)] |
| 590 | |
| 591 | for block_size in block_sizes: |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 592 | # Note because there are devices setting Primary as ignored and the |
| 593 | # partition table signature accepts 'CHROMEOS' which is also used by |
| 594 | # Chrome OS kernel partition, we have to look for Secondary (backup) GPT |
| 595 | # first before trying other block sizes, otherwise we may incorrectly |
| 596 | # identify a kernel partition as LBA 1 of larger block size system. |
| 597 | for i, seek in enumerate([(block_size * 1, os.SEEK_SET), |
| 598 | (-block_size, os.SEEK_END)]): |
| 599 | image.seek(*seek) |
| 600 | header = gpt.Header.ReadFrom(image) |
| 601 | if header.Signature in cls.Header.SIGNATURES: |
| 602 | gpt.block_size = block_size |
| 603 | if i != 0: |
| 604 | gpt.is_secondary = True |
| 605 | break |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 606 | # TODO(hungte) Try harder to see if this block is valid. |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 607 | else: |
| 608 | # Nothing found, try next block size. |
| 609 | continue |
| 610 | # Found a valid signature. |
| 611 | break |
Hung-Te Lin | f148d32 | 2018-04-13 10:24:42 +0800 | [diff] [blame] | 612 | else: |
Hung-Te Lin | 4dfd330 | 2018-04-17 14:47:52 +0800 | [diff] [blame] | 613 | raise GPTError('Invalid signature in GPT header.') |
Hung-Te Lin | f148d32 | 2018-04-13 10:24:42 +0800 | [diff] [blame] | 614 | |
Hung-Te Lin | 6977ae1 | 2018-04-17 12:20:32 +0800 | [diff] [blame] | 615 | image.seek(gpt.block_size * header.PartitionEntriesStartingLBA) |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 616 | def ReadPartition(image, number): |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 617 | p = gpt.Partition.ReadFrom( |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 618 | image, image=image.name, number=number, block_size=gpt.block_size) |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 619 | return p |
| 620 | |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 621 | gpt.header = header |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 622 | gpt.partitions = [ |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 623 | ReadPartition(image, i + 1) |
Yilin Yang | bf84d2e | 2020-05-13 10:34:46 +0800 | [diff] [blame] | 624 | for i in range(header.PartitionEntriesNumber)] |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 625 | return gpt |
| 626 | |
Hung-Te Lin | c519668 | 2018-04-18 22:59:59 +0800 | [diff] [blame] | 627 | def GetUsedPartitions(self): |
| 628 | """Returns a list of partitions with type GUID not set to unused. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 629 | |
Hung-Te Lin | c519668 | 2018-04-18 22:59:59 +0800 | [diff] [blame] | 630 | Use 'number' property to find the real location of partition in |
| 631 | self.partitions. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 632 | """ |
Hung-Te Lin | c519668 | 2018-04-18 22:59:59 +0800 | [diff] [blame] | 633 | return [p for p in self.partitions if not p.IsUnused()] |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 634 | |
| 635 | def GetMaxUsedLBA(self): |
Hung-Te Lin | d3a2e9a | 2018-04-19 13:07:26 +0800 | [diff] [blame] | 636 | """Returns the max LastLBA from all used partitions.""" |
Hung-Te Lin | c519668 | 2018-04-18 22:59:59 +0800 | [diff] [blame] | 637 | parts = self.GetUsedPartitions() |
Hung-Te Lin | d3a2e9a | 2018-04-19 13:07:26 +0800 | [diff] [blame] | 638 | return (max(p.LastLBA for p in parts) |
| 639 | if parts else self.header.FirstUsableLBA - 1) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 640 | |
| 641 | def GetPartitionTableBlocks(self, header=None): |
| 642 | """Returns the blocks (or LBA) of partition table from given header.""" |
| 643 | if header is None: |
| 644 | header = self.header |
| 645 | size = header.PartitionEntrySize * header.PartitionEntriesNumber |
Yilin Yang | 14d02a2 | 2019-11-01 11:32:03 +0800 | [diff] [blame] | 646 | blocks = size // self.block_size |
Hung-Te Lin | f148d32 | 2018-04-13 10:24:42 +0800 | [diff] [blame] | 647 | if size % self.block_size: |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 648 | blocks += 1 |
| 649 | return blocks |
| 650 | |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 651 | def GetPartition(self, number): |
| 652 | """Gets the Partition by given (1-based) partition number. |
| 653 | |
| 654 | Args: |
| 655 | number: an integer as 1-based partition number. |
| 656 | """ |
| 657 | if not 0 < number <= len(self.partitions): |
| 658 | raise GPTError('Invalid partition number %s.' % number) |
| 659 | return self.partitions[number - 1] |
| 660 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 661 | def UpdatePartition(self, part, number): |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 662 | """Updates the entry in partition table by given Partition object. |
| 663 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 664 | Usually you only need to call this if you want to copy one partition to |
| 665 | different location (number of image). |
| 666 | |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 667 | Args: |
| 668 | part: a Partition GPT object. |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 669 | number: an integer as 1-based partition number. |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 670 | """ |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 671 | ref = self.partitions[number - 1] |
| 672 | part = part.Clone() |
| 673 | part.number = number |
| 674 | part.image = ref.image |
| 675 | part.block_size = ref.block_size |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 676 | self.partitions[number - 1] = part |
| 677 | |
Cheng-Han Yang | dc235b3 | 2019-01-08 18:05:40 +0800 | [diff] [blame] | 678 | def GetSize(self): |
| 679 | return self.block_size * (self.header.BackupLBA + 1) |
| 680 | |
| 681 | def Resize(self, new_size, check_overlap=True): |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 682 | """Adjust GPT for a disk image in given size. |
| 683 | |
| 684 | Args: |
| 685 | new_size: Integer for new size of disk image file. |
Cheng-Han Yang | dc235b3 | 2019-01-08 18:05:40 +0800 | [diff] [blame] | 686 | check_overlap: Checks if the backup partition table overlaps used |
| 687 | partitions. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 688 | """ |
Cheng-Han Yang | dc235b3 | 2019-01-08 18:05:40 +0800 | [diff] [blame] | 689 | old_size = self.GetSize() |
Hung-Te Lin | f148d32 | 2018-04-13 10:24:42 +0800 | [diff] [blame] | 690 | if new_size % self.block_size: |
Hung-Te Lin | 4dfd330 | 2018-04-17 14:47:52 +0800 | [diff] [blame] | 691 | raise GPTError( |
| 692 | 'New file size %d is not valid for image files.' % new_size) |
Yilin Yang | 14d02a2 | 2019-11-01 11:32:03 +0800 | [diff] [blame] | 693 | new_blocks = new_size // self.block_size |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 694 | if old_size != new_size: |
Yilin Yang | 9881b1e | 2019-12-11 11:47:33 +0800 | [diff] [blame] | 695 | logging.warning('Image size (%d, LBA=%d) changed from %d (LBA=%d).', |
| 696 | new_size, new_blocks, old_size, |
| 697 | old_size // self.block_size) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 698 | else: |
| 699 | logging.info('Image size (%d, LBA=%d) not changed.', |
| 700 | new_size, new_blocks) |
Hung-Te Lin | d3a2e9a | 2018-04-19 13:07:26 +0800 | [diff] [blame] | 701 | return |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 702 | |
Hung-Te Lin | d3a2e9a | 2018-04-19 13:07:26 +0800 | [diff] [blame] | 703 | # Expected location |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 704 | backup_lba = new_blocks - 1 |
Hung-Te Lin | d3a2e9a | 2018-04-19 13:07:26 +0800 | [diff] [blame] | 705 | last_usable_lba = backup_lba - self.header.FirstUsableLBA |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 706 | |
Cheng-Han Yang | dc235b3 | 2019-01-08 18:05:40 +0800 | [diff] [blame] | 707 | if check_overlap and last_usable_lba < self.header.LastUsableLBA: |
Hung-Te Lin | d3a2e9a | 2018-04-19 13:07:26 +0800 | [diff] [blame] | 708 | max_used_lba = self.GetMaxUsedLBA() |
| 709 | if last_usable_lba < max_used_lba: |
Hung-Te Lin | 4dfd330 | 2018-04-17 14:47:52 +0800 | [diff] [blame] | 710 | raise GPTError('Backup partition tables will overlap used partitions') |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 711 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 712 | self.header.Update(BackupLBA=backup_lba, LastUsableLBA=last_usable_lba) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 713 | |
| 714 | def GetFreeSpace(self): |
| 715 | """Returns the free (available) space left according to LastUsableLBA.""" |
| 716 | max_lba = self.GetMaxUsedLBA() |
| 717 | assert max_lba <= self.header.LastUsableLBA, "Partitions too large." |
Hung-Te Lin | f148d32 | 2018-04-13 10:24:42 +0800 | [diff] [blame] | 718 | return self.block_size * (self.header.LastUsableLBA - max_lba) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 719 | |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 720 | def ExpandPartition(self, number): |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 721 | """Expands a given partition to last usable LBA. |
| 722 | |
Cheng-Han Yang | dc235b3 | 2019-01-08 18:05:40 +0800 | [diff] [blame] | 723 | The size of the partition can actually be reduced if the last usable LBA |
| 724 | decreases. |
| 725 | |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 726 | Args: |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 727 | number: an integer to specify partition in 1-based number. |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 728 | |
| 729 | Returns: |
| 730 | (old_blocks, new_blocks) for size in blocks. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 731 | """ |
| 732 | # Assume no partitions overlap, we need to make sure partition[i] has |
| 733 | # largest LBA. |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 734 | p = self.GetPartition(number) |
| 735 | if p.IsUnused(): |
| 736 | raise GPTError('Partition %s is unused.' % p) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 737 | max_used_lba = self.GetMaxUsedLBA() |
Hung-Te Lin | c519668 | 2018-04-18 22:59:59 +0800 | [diff] [blame] | 738 | # TODO(hungte) We can do more by finding free space after i. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 739 | if max_used_lba > p.LastLBA: |
Hung-Te Lin | 4dfd330 | 2018-04-17 14:47:52 +0800 | [diff] [blame] | 740 | raise GPTError( |
Hung-Te Lin | c519668 | 2018-04-18 22:59:59 +0800 | [diff] [blame] | 741 | 'Cannot expand %s because it is not allocated at last.' % p) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 742 | |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 743 | old_blocks = p.blocks |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 744 | p.Update(LastLBA=self.header.LastUsableLBA) |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 745 | new_blocks = p.blocks |
Yilin Yang | 9881b1e | 2019-12-11 11:47:33 +0800 | [diff] [blame] | 746 | logging.warning( |
Cheng-Han Yang | dc235b3 | 2019-01-08 18:05:40 +0800 | [diff] [blame] | 747 | '%s size changed in LBA: %d -> %d.', p, old_blocks, new_blocks) |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 748 | return (old_blocks, new_blocks) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 749 | |
Hung-Te Lin | 3b49167 | 2018-04-19 01:41:20 +0800 | [diff] [blame] | 750 | def CheckIntegrity(self): |
| 751 | """Checks if the GPT objects all look good.""" |
| 752 | # Check if the header allocation looks good. CurrentLBA and |
| 753 | # PartitionEntriesStartingLBA should be all outside [FirstUsableLBA, |
| 754 | # LastUsableLBA]. |
| 755 | header = self.header |
| 756 | entries_first_lba = header.PartitionEntriesStartingLBA |
| 757 | entries_last_lba = entries_first_lba + self.GetPartitionTableBlocks() - 1 |
| 758 | |
| 759 | def CheckOutsideUsable(name, lba, outside_entries=False): |
| 760 | if lba < 1: |
| 761 | raise GPTError('%s should not live in LBA %s.' % (name, lba)) |
| 762 | if lba > max(header.BackupLBA, header.CurrentLBA): |
| 763 | # Note this is "in theory" possible, but we want to report this as |
| 764 | # error as well, since it usually leads to error. |
| 765 | raise GPTError('%s (%s) should not be larger than BackupLBA (%s).' % |
| 766 | (name, lba, header.BackupLBA)) |
| 767 | if header.FirstUsableLBA <= lba <= header.LastUsableLBA: |
| 768 | raise GPTError('%s (%s) should not be included in usable LBAs [%s,%s]' % |
| 769 | (name, lba, header.FirstUsableLBA, header.LastUsableLBA)) |
| 770 | if outside_entries and entries_first_lba <= lba <= entries_last_lba: |
| 771 | raise GPTError('%s (%s) should be outside partition entries [%s,%s]' % |
| 772 | (name, lba, entries_first_lba, entries_last_lba)) |
| 773 | CheckOutsideUsable('Header', header.CurrentLBA, True) |
| 774 | CheckOutsideUsable('Backup header', header.BackupLBA, True) |
| 775 | CheckOutsideUsable('Partition entries', entries_first_lba) |
| 776 | CheckOutsideUsable('Partition entries end', entries_last_lba) |
| 777 | |
| 778 | parts = self.GetUsedPartitions() |
| 779 | # Check if partition entries overlap with each other. |
| 780 | lba_list = [(p.FirstLBA, p.LastLBA, p) for p in parts] |
| 781 | lba_list.sort(key=lambda t: t[0]) |
Yilin Yang | bf84d2e | 2020-05-13 10:34:46 +0800 | [diff] [blame] | 782 | for i in range(len(lba_list) - 1): |
Hung-Te Lin | 3b49167 | 2018-04-19 01:41:20 +0800 | [diff] [blame] | 783 | if lba_list[i][1] >= lba_list[i + 1][0]: |
| 784 | raise GPTError('Overlap in partition entries: [%s,%s]%s, [%s,%s]%s.' % |
| 785 | (lba_list[i] + lba_list[i + 1])) |
| 786 | # Now, check the first and last partition. |
| 787 | if lba_list: |
| 788 | p = lba_list[0][2] |
| 789 | if p.FirstLBA < header.FirstUsableLBA: |
| 790 | raise GPTError( |
| 791 | 'Partition %s must not go earlier (%s) than FirstUsableLBA=%s' % |
| 792 | (p, p.FirstLBA, header.FirstLBA)) |
| 793 | p = lba_list[-1][2] |
| 794 | if p.LastLBA > header.LastUsableLBA: |
| 795 | raise GPTError( |
| 796 | 'Partition %s must not go further (%s) than LastUsableLBA=%s' % |
| 797 | (p, p.LastLBA, header.LastLBA)) |
| 798 | # Check if UniqueGUIDs are not unique. |
| 799 | if len(set(p.UniqueGUID for p in parts)) != len(parts): |
| 800 | raise GPTError('Partition UniqueGUIDs are duplicated.') |
| 801 | # Check if CRCs match. |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 802 | if (binascii.crc32(b''.join(p.blob for p in self.partitions)) != |
Hung-Te Lin | 3b49167 | 2018-04-19 01:41:20 +0800 | [diff] [blame] | 803 | header.PartitionArrayCRC32): |
| 804 | raise GPTError('GPT Header PartitionArrayCRC32 does not match.') |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 805 | header_crc = header.Clone() |
| 806 | header_crc.UpdateChecksum() |
| 807 | if header_crc.CRC32 != header.CRC32: |
| 808 | raise GPTError('GPT Header CRC32 does not match.') |
Hung-Te Lin | 3b49167 | 2018-04-19 01:41:20 +0800 | [diff] [blame] | 809 | |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 810 | def UpdateChecksum(self): |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 811 | """Updates all checksum fields in GPT objects.""" |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 812 | parts = b''.join(p.blob for p in self.partitions) |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 813 | self.header.Update(PartitionArrayCRC32=binascii.crc32(parts)) |
| 814 | self.header.UpdateChecksum() |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 815 | |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 816 | def GetBackupHeader(self, header): |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 817 | """Returns the backup header according to given header. |
| 818 | |
| 819 | This should be invoked only after GPT.UpdateChecksum() has updated all CRC32 |
| 820 | fields. |
| 821 | """ |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 822 | partitions_starting_lba = ( |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 823 | header.BackupLBA - self.GetPartitionTableBlocks()) |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 824 | h = header.Clone() |
| 825 | h.Update( |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 826 | BackupLBA=header.CurrentLBA, |
| 827 | CurrentLBA=header.BackupLBA, |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 828 | PartitionEntriesStartingLBA=partitions_starting_lba) |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 829 | h.UpdateChecksum() |
| 830 | return h |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 831 | |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 832 | @classmethod |
| 833 | def WriteProtectiveMBR(cls, image, create, bootcode=None, boot_guid=None): |
| 834 | """Writes a protective MBR to given file. |
| 835 | |
| 836 | Each MBR is 512 bytes: 424 bytes for bootstrap code, 16 bytes of boot GUID, |
| 837 | 4 bytes of disk id, 2 bytes of bootcode magic, 4*16 for 4 partitions, and 2 |
| 838 | byte as signature. cgpt has hard-coded the CHS and bootstrap magic values so |
| 839 | we can follow that. |
| 840 | |
| 841 | Args: |
| 842 | create: True to re-create PMBR structure. |
| 843 | bootcode: a blob of new boot code. |
| 844 | boot_guid a blob for new boot GUID. |
| 845 | |
| 846 | Returns: |
| 847 | The written PMBR structure. |
| 848 | """ |
Yilin Yang | 0724c9d | 2019-11-15 15:53:45 +0800 | [diff] [blame] | 849 | if isinstance(image, str): |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 850 | with open(image, 'rb+') as f: |
| 851 | return cls.WriteProtectiveMBR(f, create, bootcode, boot_guid) |
| 852 | |
| 853 | image.seek(0) |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 854 | pmbr_format = cls.ProtectiveMBR.GetStructFormat() |
| 855 | assert struct.calcsize(pmbr_format) == cls.DEFAULT_BLOCK_SIZE |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 856 | pmbr = cls.ProtectiveMBR.ReadFrom(image) |
| 857 | |
| 858 | if create: |
| 859 | legacy_sectors = min( |
| 860 | 0x100000000, |
Yilin Yang | 14d02a2 | 2019-11-01 11:32:03 +0800 | [diff] [blame] | 861 | GPT.GetImageSize(image.name) // cls.DEFAULT_BLOCK_SIZE) - 1 |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 862 | # Partition 0 must have have the fixed CHS with number of sectors |
| 863 | # (calculated as legacy_sectors later). |
Yilin Yang | f9fe193 | 2019-11-04 17:09:34 +0800 | [diff] [blame] | 864 | part0 = (codecs.decode('00000200eeffffff01000000', 'hex') + |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 865 | struct.pack('<I', legacy_sectors)) |
| 866 | # Partition 1~3 should be all zero. |
| 867 | part1 = '\x00' * 16 |
| 868 | assert len(part0) == len(part1) == 16, 'MBR entry is wrong.' |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 869 | pmbr.Update( |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 870 | BootGUID=cls.TYPE_GUID_UNUSED, |
| 871 | DiskID=0, |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 872 | Magic=pmbr.MAGIC, |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 873 | LegacyPart0=part0, |
| 874 | LegacyPart1=part1, |
| 875 | LegacyPart2=part1, |
| 876 | LegacyPart3=part1, |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 877 | Signature=pmbr.SIGNATURE) |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 878 | |
| 879 | if bootcode: |
| 880 | if len(bootcode) > len(pmbr.BootCode): |
| 881 | logging.info( |
| 882 | 'Bootcode is larger (%d > %d)!', len(bootcode), len(pmbr.BootCode)) |
| 883 | bootcode = bootcode[:len(pmbr.BootCode)] |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 884 | pmbr.Update(BootCode=bootcode) |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 885 | if boot_guid: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 886 | pmbr.Update(BootGUID=boot_guid) |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 887 | |
| 888 | blob = pmbr.blob |
| 889 | assert len(blob) == cls.DEFAULT_BLOCK_SIZE |
| 890 | image.seek(0) |
| 891 | image.write(blob) |
| 892 | return pmbr |
| 893 | |
Hung-Te Lin | 6977ae1 | 2018-04-17 12:20:32 +0800 | [diff] [blame] | 894 | def WriteToFile(self, image): |
| 895 | """Updates partition table in a disk image file. |
| 896 | |
| 897 | Args: |
| 898 | image: a string as file path or a file-like object to write into. |
| 899 | """ |
Yilin Yang | 0724c9d | 2019-11-15 15:53:45 +0800 | [diff] [blame] | 900 | if isinstance(image, str): |
Hung-Te Lin | 6977ae1 | 2018-04-17 12:20:32 +0800 | [diff] [blame] | 901 | with open(image, 'rb+') as f: |
| 902 | return self.WriteToFile(f) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 903 | |
| 904 | def WriteData(name, blob, lba): |
| 905 | """Writes a blob into given location.""" |
| 906 | logging.info('Writing %s in LBA %d (offset %d)', |
Hung-Te Lin | f148d32 | 2018-04-13 10:24:42 +0800 | [diff] [blame] | 907 | name, lba, lba * self.block_size) |
Hung-Te Lin | 6977ae1 | 2018-04-17 12:20:32 +0800 | [diff] [blame] | 908 | image.seek(lba * self.block_size) |
| 909 | image.write(blob) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 910 | |
| 911 | self.UpdateChecksum() |
Hung-Te Lin | 3b49167 | 2018-04-19 01:41:20 +0800 | [diff] [blame] | 912 | self.CheckIntegrity() |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 913 | parts_blob = b''.join(p.blob for p in self.partitions) |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 914 | |
| 915 | header = self.header |
| 916 | WriteData('GPT Header', header.blob, header.CurrentLBA) |
| 917 | WriteData('GPT Partitions', parts_blob, header.PartitionEntriesStartingLBA) |
| 918 | logging.info( |
| 919 | 'Usable LBA: First=%d, Last=%d', header.FirstUsableLBA, |
| 920 | header.LastUsableLBA) |
| 921 | |
| 922 | if not self.is_secondary: |
| 923 | # When is_secondary is True, the header we have is actually backup header. |
| 924 | backup_header = self.GetBackupHeader(self.header) |
| 925 | WriteData( |
| 926 | 'Backup Partitions', parts_blob, |
| 927 | backup_header.PartitionEntriesStartingLBA) |
| 928 | WriteData( |
| 929 | 'Backup Header', backup_header.blob, backup_header.CurrentLBA) |
Yilin Yang | 840fdc4 | 2020-01-16 16:37:42 +0800 | [diff] [blame] | 930 | return None |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 931 | |
| 932 | |
Fei Shao | bd07c9a | 2020-06-15 19:04:50 +0800 | [diff] [blame] | 933 | class GPTCommands: |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 934 | """Collection of GPT sub commands for command line to use. |
| 935 | |
| 936 | The commands are derived from `cgpt`, but not necessary to be 100% compatible |
| 937 | with cgpt. |
| 938 | """ |
| 939 | |
| 940 | FORMAT_ARGS = [ |
Peter Shih | c7156ca | 2018-02-26 14:46:24 +0800 | [diff] [blame] | 941 | ('begin', 'beginning sector'), |
Hung-Te Lin | 49ac3c2 | 2018-04-17 14:37:54 +0800 | [diff] [blame] | 942 | ('size', 'partition size (in sectors)'), |
Peter Shih | c7156ca | 2018-02-26 14:46:24 +0800 | [diff] [blame] | 943 | ('type', 'type guid'), |
| 944 | ('unique', 'unique guid'), |
| 945 | ('label', 'label'), |
| 946 | ('Successful', 'Successful flag'), |
| 947 | ('Tries', 'Tries flag'), |
| 948 | ('Priority', 'Priority flag'), |
| 949 | ('Legacy', 'Legacy Boot flag'), |
| 950 | ('Attribute', 'raw 16-bit attribute value (bits 48-63)')] |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 951 | |
| 952 | def __init__(self): |
Fei Shao | 9507580 | 2020-06-16 16:55:25 +0800 | [diff] [blame] | 953 | commands = { |
| 954 | command.lower(): getattr(self, command)() |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 955 | for command in dir(self) |
| 956 | if (isinstance(getattr(self, command), type) and |
| 957 | issubclass(getattr(self, command), self.SubCommand) and |
| 958 | getattr(self, command) is not self.SubCommand) |
Fei Shao | 9507580 | 2020-06-16 16:55:25 +0800 | [diff] [blame] | 959 | } |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 960 | self.commands = commands |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 961 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 962 | def DefineArgs(self, parser): |
| 963 | """Defines all available commands to an argparser subparsers instance.""" |
| 964 | subparsers = parser.add_subparsers(help='Sub-command help.', dest='command') |
Yilin Yang | 879fbda | 2020-05-14 13:52:30 +0800 | [diff] [blame] | 965 | for name, instance in sorted(self.commands.items()): |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 966 | parser = subparsers.add_parser( |
| 967 | name, description=instance.__doc__, |
| 968 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 969 | help=instance.__doc__.splitlines()[0]) |
| 970 | instance.DefineArgs(parser) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 971 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 972 | def Execute(self, args): |
| 973 | """Execute the sub commands by given parsed arguments.""" |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 974 | return self.commands[args.command].Execute(args) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 975 | |
Fei Shao | bd07c9a | 2020-06-15 19:04:50 +0800 | [diff] [blame] | 976 | class SubCommand: |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 977 | """A base class for sub commands to derive from.""" |
| 978 | |
| 979 | def DefineArgs(self, parser): |
| 980 | """Defines command line arguments to argparse parser. |
| 981 | |
| 982 | Args: |
| 983 | parser: An argparse parser instance. |
| 984 | """ |
| 985 | del parser # Unused. |
| 986 | raise NotImplementedError |
| 987 | |
| 988 | def Execute(self, args): |
Hung-Te Lin | e0d1fa7 | 2018-05-15 00:04:48 +0800 | [diff] [blame] | 989 | """Execute the command with parsed arguments. |
| 990 | |
| 991 | To execute with raw arguments, use ExecuteCommandLine instead. |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 992 | |
| 993 | Args: |
| 994 | args: An argparse parsed namespace. |
| 995 | """ |
| 996 | del args # Unused. |
| 997 | raise NotImplementedError |
| 998 | |
Hung-Te Lin | e0d1fa7 | 2018-05-15 00:04:48 +0800 | [diff] [blame] | 999 | def ExecuteCommandLine(self, *args): |
| 1000 | """Execute as invoked from command line. |
| 1001 | |
| 1002 | This provides an easy way to execute particular sub command without |
| 1003 | creating argument parser explicitly. |
| 1004 | |
| 1005 | Args: |
| 1006 | args: a list of string type command line arguments. |
| 1007 | """ |
| 1008 | parser = argparse.ArgumentParser() |
| 1009 | self.DefineArgs(parser) |
| 1010 | return self.Execute(parser.parse_args(args)) |
| 1011 | |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 1012 | class Create(SubCommand): |
| 1013 | """Create or reset GPT headers and tables. |
| 1014 | |
| 1015 | Create or reset an empty GPT. |
| 1016 | """ |
| 1017 | |
| 1018 | def DefineArgs(self, parser): |
| 1019 | parser.add_argument( |
| 1020 | '-z', '--zero', action='store_true', |
| 1021 | help='Zero the sectors of the GPT table and entries') |
| 1022 | parser.add_argument( |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1023 | '-p', '--pad-blocks', type=int, default=0, |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 1024 | help=('Size (in blocks) of the disk to pad between the ' |
| 1025 | 'primary GPT header and its entries, default %(default)s')) |
| 1026 | parser.add_argument( |
Hung-Te Lin | 446eb51 | 2018-05-02 18:39:16 +0800 | [diff] [blame] | 1027 | '--block_size', type=int, |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 1028 | help='Size of each block (sector) in bytes.') |
| 1029 | parser.add_argument( |
| 1030 | 'image_file', type=argparse.FileType('rb+'), |
| 1031 | help='Disk image file to create.') |
| 1032 | |
| 1033 | def Execute(self, args): |
| 1034 | block_size = args.block_size |
Hung-Te Lin | 446eb51 | 2018-05-02 18:39:16 +0800 | [diff] [blame] | 1035 | if block_size is None: |
| 1036 | if GPT.IsBlockDevice(args.image_file.name): |
| 1037 | block_size = GPT.GetLogicalBlockSize(args.image_file.name) |
| 1038 | else: |
| 1039 | block_size = GPT.DEFAULT_BLOCK_SIZE |
| 1040 | |
| 1041 | if block_size != GPT.DEFAULT_BLOCK_SIZE: |
| 1042 | logging.info('Block (sector) size for %s is set to %s bytes.', |
| 1043 | args.image_file.name, block_size) |
| 1044 | |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 1045 | gpt = GPT.Create( |
Hung-Te Lin | 446eb51 | 2018-05-02 18:39:16 +0800 | [diff] [blame] | 1046 | args.image_file.name, GPT.GetImageSize(args.image_file.name), |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 1047 | block_size, args.pad_blocks) |
| 1048 | if args.zero: |
| 1049 | # In theory we only need to clear LBA 1, but to make sure images already |
| 1050 | # initialized with different block size won't have GPT signature in |
| 1051 | # different locations, we should zero until first usable LBA. |
| 1052 | args.image_file.seek(0) |
Yilin Yang | 235e598 | 2019-12-26 10:36:22 +0800 | [diff] [blame] | 1053 | args.image_file.write(b'\0' * block_size * gpt.header.FirstUsableLBA) |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 1054 | gpt.WriteToFile(args.image_file) |
Yilin Yang | f95c25a | 2019-12-23 15:38:51 +0800 | [diff] [blame] | 1055 | args.image_file.close() |
Hung-Te Lin | bad4611 | 2018-05-15 16:39:14 +0800 | [diff] [blame] | 1056 | return 'Created GPT for %s' % args.image_file.name |
Hung-Te Lin | 6c3575a | 2018-04-17 15:00:49 +0800 | [diff] [blame] | 1057 | |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 1058 | class Boot(SubCommand): |
| 1059 | """Edit the PMBR sector for legacy BIOSes. |
| 1060 | |
| 1061 | With no options, it will just print the PMBR boot guid. |
| 1062 | """ |
| 1063 | |
| 1064 | def DefineArgs(self, parser): |
| 1065 | parser.add_argument( |
| 1066 | '-i', '--number', type=int, |
| 1067 | help='Set bootable partition') |
| 1068 | parser.add_argument( |
Stimim Chen | 0e6071b | 2020-04-28 18:08:49 +0800 | [diff] [blame] | 1069 | '-b', '--bootloader', type=argparse.FileType('rb'), |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 1070 | help='Install bootloader code in the PMBR') |
| 1071 | parser.add_argument( |
| 1072 | '-p', '--pmbr', action='store_true', |
| 1073 | help='Create legacy PMBR partition table') |
| 1074 | parser.add_argument( |
| 1075 | 'image_file', type=argparse.FileType('rb+'), |
| 1076 | help='Disk image file to change PMBR.') |
| 1077 | |
| 1078 | def Execute(self, args): |
| 1079 | """Rebuilds the protective MBR.""" |
| 1080 | bootcode = args.bootloader.read() if args.bootloader else None |
| 1081 | boot_guid = None |
| 1082 | if args.number is not None: |
| 1083 | gpt = GPT.LoadFromFile(args.image_file) |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 1084 | boot_guid = gpt.GetPartition(args.number).UniqueGUID |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 1085 | pmbr = GPT.WriteProtectiveMBR( |
| 1086 | args.image_file, args.pmbr, bootcode=bootcode, boot_guid=boot_guid) |
| 1087 | |
You-Cheng Syu | fff7f42 | 2018-05-14 15:37:39 +0800 | [diff] [blame] | 1088 | print(pmbr.BootGUID) |
Yilin Yang | f95c25a | 2019-12-23 15:38:51 +0800 | [diff] [blame] | 1089 | args.image_file.close() |
Hung-Te Lin | bad4611 | 2018-05-15 16:39:14 +0800 | [diff] [blame] | 1090 | return 0 |
Hung-Te Lin | c6e009c | 2018-04-17 15:06:16 +0800 | [diff] [blame] | 1091 | |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 1092 | class Legacy(SubCommand): |
| 1093 | """Switch between GPT and Legacy GPT. |
| 1094 | |
| 1095 | Switch GPT header signature to "CHROMEOS". |
| 1096 | """ |
| 1097 | |
| 1098 | def DefineArgs(self, parser): |
| 1099 | parser.add_argument( |
| 1100 | '-e', '--efi', action='store_true', |
| 1101 | help='Switch GPT header signature back to "EFI PART"') |
| 1102 | parser.add_argument( |
| 1103 | '-p', '--primary-ignore', action='store_true', |
| 1104 | help='Switch primary GPT header signature to "IGNOREME"') |
| 1105 | parser.add_argument( |
| 1106 | 'image_file', type=argparse.FileType('rb+'), |
| 1107 | help='Disk image file to change.') |
| 1108 | |
| 1109 | def Execute(self, args): |
| 1110 | gpt = GPT.LoadFromFile(args.image_file) |
| 1111 | # cgpt behavior: if -p is specified, -e is ignored. |
| 1112 | if args.primary_ignore: |
| 1113 | if gpt.is_secondary: |
| 1114 | raise GPTError('Sorry, the disk already has primary GPT ignored.') |
| 1115 | args.image_file.seek(gpt.header.CurrentLBA * gpt.block_size) |
| 1116 | args.image_file.write(gpt.header.SIGNATURE_IGNORE) |
| 1117 | gpt.header = gpt.GetBackupHeader(self.header) |
| 1118 | gpt.is_secondary = True |
| 1119 | else: |
| 1120 | new_signature = gpt.Header.SIGNATURES[0 if args.efi else 1] |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1121 | gpt.header.Update(Signature=new_signature) |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 1122 | gpt.WriteToFile(args.image_file) |
Yilin Yang | f95c25a | 2019-12-23 15:38:51 +0800 | [diff] [blame] | 1123 | args.image_file.close() |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 1124 | if args.primary_ignore: |
Hung-Te Lin | bad4611 | 2018-05-15 16:39:14 +0800 | [diff] [blame] | 1125 | return ('Set %s primary GPT header to %s.' % |
| 1126 | (args.image_file.name, gpt.header.SIGNATURE_IGNORE)) |
Yilin Yang | 15a3f8f | 2020-01-03 17:49:00 +0800 | [diff] [blame] | 1127 | return ('Changed GPT signature for %s to %s.' % |
| 1128 | (args.image_file.name, new_signature)) |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 1129 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1130 | class Repair(SubCommand): |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1131 | """Repair damaged GPT headers and tables.""" |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1132 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1133 | def DefineArgs(self, parser): |
| 1134 | parser.add_argument( |
| 1135 | 'image_file', type=argparse.FileType('rb+'), |
| 1136 | help='Disk image file to repair.') |
| 1137 | |
| 1138 | def Execute(self, args): |
| 1139 | gpt = GPT.LoadFromFile(args.image_file) |
Hung-Te Lin | 446eb51 | 2018-05-02 18:39:16 +0800 | [diff] [blame] | 1140 | gpt.Resize(GPT.GetImageSize(args.image_file.name)) |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1141 | gpt.WriteToFile(args.image_file) |
Yilin Yang | f95c25a | 2019-12-23 15:38:51 +0800 | [diff] [blame] | 1142 | args.image_file.close() |
Hung-Te Lin | bad4611 | 2018-05-15 16:39:14 +0800 | [diff] [blame] | 1143 | return 'Disk image file %s repaired.' % args.image_file.name |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1144 | |
| 1145 | class Expand(SubCommand): |
| 1146 | """Expands a GPT partition to all available free space.""" |
| 1147 | |
| 1148 | def DefineArgs(self, parser): |
| 1149 | parser.add_argument( |
| 1150 | '-i', '--number', type=int, required=True, |
| 1151 | help='The partition to expand.') |
| 1152 | parser.add_argument( |
| 1153 | 'image_file', type=argparse.FileType('rb+'), |
| 1154 | help='Disk image file to modify.') |
| 1155 | |
| 1156 | def Execute(self, args): |
| 1157 | gpt = GPT.LoadFromFile(args.image_file) |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 1158 | old_blocks, new_blocks = gpt.ExpandPartition(args.number) |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1159 | gpt.WriteToFile(args.image_file) |
Yilin Yang | f95c25a | 2019-12-23 15:38:51 +0800 | [diff] [blame] | 1160 | args.image_file.close() |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1161 | if old_blocks < new_blocks: |
Hung-Te Lin | bad4611 | 2018-05-15 16:39:14 +0800 | [diff] [blame] | 1162 | return ( |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1163 | 'Partition %s on disk image file %s has been extended ' |
| 1164 | 'from %s to %s .' % |
| 1165 | (args.number, args.image_file.name, old_blocks * gpt.block_size, |
| 1166 | new_blocks * gpt.block_size)) |
Yilin Yang | 15a3f8f | 2020-01-03 17:49:00 +0800 | [diff] [blame] | 1167 | return ('Nothing to expand for disk image %s partition %s.' % |
| 1168 | (args.image_file.name, args.number)) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1169 | |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1170 | class Add(SubCommand): |
| 1171 | """Add, edit, or remove a partition entry. |
| 1172 | |
| 1173 | Use the -i option to modify an existing partition. |
| 1174 | The -b, -s, and -t options must be given for new partitions. |
| 1175 | |
| 1176 | The partition type may also be given as one of these aliases: |
| 1177 | |
| 1178 | firmware ChromeOS firmware |
| 1179 | kernel ChromeOS kernel |
| 1180 | rootfs ChromeOS rootfs |
| 1181 | data Linux data |
| 1182 | reserved ChromeOS reserved |
| 1183 | efi EFI System Partition |
| 1184 | unused Unused (nonexistent) partition |
| 1185 | """ |
| 1186 | def DefineArgs(self, parser): |
| 1187 | parser.add_argument( |
| 1188 | '-i', '--number', type=int, |
| 1189 | help='Specify partition (default is next available)') |
| 1190 | parser.add_argument( |
| 1191 | '-b', '--begin', type=int, |
| 1192 | help='Beginning sector') |
| 1193 | parser.add_argument( |
| 1194 | '-s', '--sectors', type=int, |
| 1195 | help='Size in sectors (logical blocks).') |
| 1196 | parser.add_argument( |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1197 | '-t', '--type-guid', type=GPT.GetTypeGUID, |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1198 | help='Partition Type GUID') |
| 1199 | parser.add_argument( |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1200 | '-u', '--unique-guid', type=GUID, |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1201 | help='Partition Unique ID') |
| 1202 | parser.add_argument( |
| 1203 | '-l', '--label', |
| 1204 | help='Label') |
| 1205 | parser.add_argument( |
Yilin Yang | bf84d2e | 2020-05-13 10:34:46 +0800 | [diff] [blame] | 1206 | '-S', '--successful', type=int, choices=list(range(2)), |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1207 | help='set Successful flag') |
| 1208 | parser.add_argument( |
| 1209 | '-T', '--tries', type=int, |
| 1210 | help='set Tries flag (0-15)') |
| 1211 | parser.add_argument( |
| 1212 | '-P', '--priority', type=int, |
| 1213 | help='set Priority flag (0-15)') |
| 1214 | parser.add_argument( |
Yilin Yang | bf84d2e | 2020-05-13 10:34:46 +0800 | [diff] [blame] | 1215 | '-R', '--required', type=int, choices=list(range(2)), |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1216 | help='set Required flag') |
| 1217 | parser.add_argument( |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1218 | '-B', '--boot-legacy', dest='legacy_boot', type=int, |
Yilin Yang | bf84d2e | 2020-05-13 10:34:46 +0800 | [diff] [blame] | 1219 | choices=list(range(2)), |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1220 | help='set Legacy Boot flag') |
| 1221 | parser.add_argument( |
| 1222 | '-A', '--attribute', dest='raw_16', type=int, |
| 1223 | help='set raw 16-bit attribute value (bits 48-63)') |
| 1224 | parser.add_argument( |
| 1225 | 'image_file', type=argparse.FileType('rb+'), |
| 1226 | help='Disk image file to modify.') |
| 1227 | |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1228 | def Execute(self, args): |
| 1229 | gpt = GPT.LoadFromFile(args.image_file) |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1230 | number = args.number |
| 1231 | if number is None: |
Hung-Te Lin | c519668 | 2018-04-18 22:59:59 +0800 | [diff] [blame] | 1232 | number = next(p for p in gpt.partitions if p.IsUnused()).number |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1233 | |
| 1234 | # First and last LBA must be calculated explicitly because the given |
| 1235 | # argument is size. |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 1236 | part = gpt.GetPartition(number) |
Hung-Te Lin | c519668 | 2018-04-18 22:59:59 +0800 | [diff] [blame] | 1237 | is_new_part = part.IsUnused() |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1238 | |
| 1239 | if is_new_part: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1240 | part.Zero() |
| 1241 | part.Update( |
Hung-Te Lin | c519668 | 2018-04-18 22:59:59 +0800 | [diff] [blame] | 1242 | FirstLBA=gpt.GetMaxUsedLBA() + 1, |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1243 | LastLBA=gpt.header.LastUsableLBA, |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1244 | UniqueGUID=GUID.Random(), |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 1245 | TypeGUID=gpt.GetTypeGUID('data')) |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1246 | |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1247 | def UpdateAttr(name): |
| 1248 | value = getattr(args, name) |
| 1249 | if value is None: |
| 1250 | return |
| 1251 | setattr(attrs, name, value) |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1252 | |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1253 | def GetArg(arg_value, default_value): |
| 1254 | return default_value if arg_value is None else arg_value |
| 1255 | |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1256 | attrs = part.Attributes |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1257 | for name in ['legacy_boot', 'required', 'priority', 'tries', |
| 1258 | 'successful', 'raw_16']: |
| 1259 | UpdateAttr(name) |
| 1260 | first_lba = GetArg(args.begin, part.FirstLBA) |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1261 | part.Update( |
| 1262 | Names=GetArg(args.label, part.Names), |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1263 | FirstLBA=first_lba, |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1264 | LastLBA=first_lba - 1 + GetArg(args.sectors, part.blocks), |
| 1265 | TypeGUID=GetArg(args.type_guid, part.TypeGUID), |
| 1266 | UniqueGUID=GetArg(args.unique_guid, part.UniqueGUID), |
| 1267 | Attributes=attrs) |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1268 | |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1269 | # Wipe partition again if it should be empty. |
| 1270 | if part.IsUnused(): |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1271 | part.Zero() |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1272 | |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1273 | gpt.WriteToFile(args.image_file) |
Yilin Yang | f95c25a | 2019-12-23 15:38:51 +0800 | [diff] [blame] | 1274 | args.image_file.close() |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1275 | if part.IsUnused(): |
| 1276 | # If we do ('%s' % part) there will be TypeError. |
Hung-Te Lin | bad4611 | 2018-05-15 16:39:14 +0800 | [diff] [blame] | 1277 | return 'Deleted (zeroed) %s.' % (part,) |
Yilin Yang | 15a3f8f | 2020-01-03 17:49:00 +0800 | [diff] [blame] | 1278 | return ('%s %s (%s+%s).' % |
| 1279 | ('Added' if is_new_part else 'Modified', |
| 1280 | part, part.FirstLBA, part.blocks)) |
Hung-Te Lin | fcd1a8d | 2018-04-17 15:15:01 +0800 | [diff] [blame] | 1281 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1282 | class Show(SubCommand): |
| 1283 | """Show partition table and entries. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1284 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1285 | Display the GPT table. |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1286 | """ |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1287 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1288 | def DefineArgs(self, parser): |
| 1289 | parser.add_argument( |
| 1290 | '--numeric', '-n', action='store_true', |
| 1291 | help='Numeric output only.') |
| 1292 | parser.add_argument( |
| 1293 | '--quick', '-q', action='store_true', |
| 1294 | help='Quick output.') |
| 1295 | parser.add_argument( |
| 1296 | '-i', '--number', type=int, |
| 1297 | help='Show specified partition only, with format args.') |
| 1298 | for name, help_str in GPTCommands.FORMAT_ARGS: |
| 1299 | # TODO(hungte) Alert if multiple args were specified. |
| 1300 | parser.add_argument( |
| 1301 | '--%s' % name, '-%c' % name[0], action='store_true', |
| 1302 | help='[format] %s.' % help_str) |
| 1303 | parser.add_argument( |
| 1304 | 'image_file', type=argparse.FileType('rb'), |
| 1305 | help='Disk image file to show.') |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1306 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1307 | def Execute(self, args): |
| 1308 | """Show partition table and entries.""" |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1309 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1310 | def FormatTypeGUID(p): |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1311 | guid = p.TypeGUID |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1312 | if not args.numeric: |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1313 | names = gpt.TYPE_GUID_MAP.get(guid) |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1314 | if names: |
| 1315 | return names |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1316 | return str(guid) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1317 | |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1318 | def IsBootableType(guid): |
| 1319 | if not guid: |
| 1320 | return False |
| 1321 | return guid in gpt.TYPE_GUID_LIST_BOOTABLE |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1322 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1323 | def FormatAttribute(attrs, chromeos_kernel=False): |
| 1324 | if args.numeric: |
| 1325 | return '[%x]' % (attrs.raw >> 48) |
| 1326 | results = [] |
| 1327 | if chromeos_kernel: |
| 1328 | results += [ |
| 1329 | 'priority=%d' % attrs.priority, |
| 1330 | 'tries=%d' % attrs.tries, |
| 1331 | 'successful=%d' % attrs.successful] |
| 1332 | if attrs.required: |
| 1333 | results += ['required=1'] |
| 1334 | if attrs.legacy_boot: |
| 1335 | results += ['legacy_boot=1'] |
| 1336 | return ' '.join(results) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1337 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1338 | def ApplyFormatArgs(p): |
| 1339 | if args.begin: |
| 1340 | return p.FirstLBA |
Fei Shao | 12ecf38 | 2020-06-23 18:32:26 +0800 | [diff] [blame] | 1341 | if args.size: |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1342 | return p.blocks |
Fei Shao | 12ecf38 | 2020-06-23 18:32:26 +0800 | [diff] [blame] | 1343 | if args.type: |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1344 | return FormatTypeGUID(p) |
Fei Shao | 12ecf38 | 2020-06-23 18:32:26 +0800 | [diff] [blame] | 1345 | if args.unique: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1346 | return p.UniqueGUID |
Fei Shao | 12ecf38 | 2020-06-23 18:32:26 +0800 | [diff] [blame] | 1347 | if args.label: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1348 | return p.Names |
Fei Shao | 12ecf38 | 2020-06-23 18:32:26 +0800 | [diff] [blame] | 1349 | if args.Successful: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1350 | return p.Attributes.successful |
Fei Shao | 12ecf38 | 2020-06-23 18:32:26 +0800 | [diff] [blame] | 1351 | if args.Priority: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1352 | return p.Attributes.priority |
Fei Shao | 12ecf38 | 2020-06-23 18:32:26 +0800 | [diff] [blame] | 1353 | if args.Tries: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1354 | return p.Attributes.tries |
Fei Shao | 12ecf38 | 2020-06-23 18:32:26 +0800 | [diff] [blame] | 1355 | if args.Legacy: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1356 | return p.Attributes.legacy_boot |
Fei Shao | 12ecf38 | 2020-06-23 18:32:26 +0800 | [diff] [blame] | 1357 | if args.Attribute: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1358 | return '[%x]' % (p.Attributes.raw >> 48) |
Yilin Yang | 15a3f8f | 2020-01-03 17:49:00 +0800 | [diff] [blame] | 1359 | return None |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1360 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1361 | def IsFormatArgsSpecified(): |
| 1362 | return any(getattr(args, arg[0]) for arg in GPTCommands.FORMAT_ARGS) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1363 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1364 | gpt = GPT.LoadFromFile(args.image_file) |
| 1365 | logging.debug('%r', gpt.header) |
| 1366 | fmt = '%12s %11s %7s %s' |
| 1367 | fmt2 = '%32s %s: %s' |
| 1368 | header = ('start', 'size', 'part', 'contents') |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1369 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1370 | if IsFormatArgsSpecified() and args.number is None: |
| 1371 | raise GPTError('Format arguments must be used with -i.') |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1372 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1373 | if not (args.number is None or |
| 1374 | 0 < args.number <= gpt.header.PartitionEntriesNumber): |
| 1375 | raise GPTError('Invalid partition number: %d' % args.number) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1376 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1377 | partitions = gpt.partitions |
| 1378 | do_print_gpt_blocks = False |
| 1379 | if not (args.quick or IsFormatArgsSpecified()): |
| 1380 | print(fmt % header) |
| 1381 | if args.number is None: |
| 1382 | do_print_gpt_blocks = True |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1383 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1384 | if do_print_gpt_blocks: |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 1385 | if gpt.pmbr: |
| 1386 | print(fmt % (0, 1, '', 'PMBR')) |
| 1387 | if gpt.is_secondary: |
| 1388 | print(fmt % (gpt.header.BackupLBA, 1, 'IGNORED', 'Pri GPT header')) |
| 1389 | else: |
| 1390 | print(fmt % (gpt.header.CurrentLBA, 1, '', 'Pri GPT header')) |
| 1391 | print(fmt % (gpt.header.PartitionEntriesStartingLBA, |
| 1392 | gpt.GetPartitionTableBlocks(), '', 'Pri GPT table')) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1393 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1394 | for p in partitions: |
| 1395 | if args.number is None: |
| 1396 | # Skip unused partitions. |
| 1397 | if p.IsUnused(): |
| 1398 | continue |
| 1399 | elif p.number != args.number: |
| 1400 | continue |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1401 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1402 | if IsFormatArgsSpecified(): |
| 1403 | print(ApplyFormatArgs(p)) |
| 1404 | continue |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1405 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1406 | print(fmt % (p.FirstLBA, p.blocks, p.number, |
| 1407 | FormatTypeGUID(p) if args.quick else |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1408 | 'Label: "%s"' % p.Names)) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1409 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1410 | if not args.quick: |
| 1411 | print(fmt2 % ('', 'Type', FormatTypeGUID(p))) |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1412 | print(fmt2 % ('', 'UUID', p.UniqueGUID)) |
| 1413 | if args.numeric or IsBootableType(p.TypeGUID): |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1414 | print(fmt2 % ('', 'Attr', FormatAttribute( |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1415 | p.Attributes, p.IsChromeOSKernel()))) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1416 | |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1417 | if do_print_gpt_blocks: |
Hung-Te Lin | c34d89c | 2018-04-17 15:11:34 +0800 | [diff] [blame] | 1418 | if gpt.is_secondary: |
| 1419 | header = gpt.header |
| 1420 | else: |
| 1421 | f = args.image_file |
| 1422 | f.seek(gpt.header.BackupLBA * gpt.block_size) |
| 1423 | header = gpt.Header.ReadFrom(f) |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1424 | print(fmt % (header.PartitionEntriesStartingLBA, |
| 1425 | gpt.GetPartitionTableBlocks(header), '', |
| 1426 | 'Sec GPT table')) |
| 1427 | print(fmt % (header.CurrentLBA, 1, '', 'Sec GPT header')) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1428 | |
Hung-Te Lin | 3b49167 | 2018-04-19 01:41:20 +0800 | [diff] [blame] | 1429 | # Check integrity after showing all fields. |
| 1430 | gpt.CheckIntegrity() |
| 1431 | |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1432 | class Prioritize(SubCommand): |
| 1433 | """Reorder the priority of all kernel partitions. |
| 1434 | |
| 1435 | Reorder the priority of all active ChromeOS Kernel partitions. |
| 1436 | |
| 1437 | With no options this will set the lowest active kernel to priority 1 while |
| 1438 | maintaining the original order. |
| 1439 | """ |
| 1440 | |
| 1441 | def DefineArgs(self, parser): |
| 1442 | parser.add_argument( |
| 1443 | '-P', '--priority', type=int, |
| 1444 | help=('Highest priority to use in the new ordering. ' |
| 1445 | 'The other partitions will be ranked in decreasing ' |
| 1446 | 'priority while preserving their original order. ' |
| 1447 | 'If necessary the lowest ranks will be coalesced. ' |
| 1448 | 'No active kernels will be lowered to priority 0.')) |
| 1449 | parser.add_argument( |
| 1450 | '-i', '--number', type=int, |
| 1451 | help='Specify the partition to make the highest in the new order.') |
| 1452 | parser.add_argument( |
| 1453 | '-f', '--friends', action='store_true', |
| 1454 | help=('Friends of the given partition (those with the same ' |
| 1455 | 'starting priority) are also updated to the new ' |
| 1456 | 'highest priority. ')) |
| 1457 | parser.add_argument( |
| 1458 | 'image_file', type=argparse.FileType('rb+'), |
| 1459 | help='Disk image file to prioritize.') |
| 1460 | |
| 1461 | def Execute(self, args): |
| 1462 | gpt = GPT.LoadFromFile(args.image_file) |
| 1463 | parts = [p for p in gpt.partitions if p.IsChromeOSKernel()] |
Hung-Te Lin | 138389f | 2018-05-15 17:55:00 +0800 | [diff] [blame] | 1464 | parts.sort(key=lambda p: p.Attributes.priority, reverse=True) |
Fei Shao | 9507580 | 2020-06-16 16:55:25 +0800 | [diff] [blame] | 1465 | groups = {k: list(g) for k, g in itertools.groupby( |
| 1466 | parts, lambda p: p.Attributes.priority)} |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1467 | if args.number: |
Hung-Te Lin | 5f0dea4 | 2018-04-18 23:20:11 +0800 | [diff] [blame] | 1468 | p = gpt.GetPartition(args.number) |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1469 | if p not in parts: |
| 1470 | raise GPTError('%s is not a ChromeOS kernel.' % p) |
Hung-Te Lin | 138389f | 2018-05-15 17:55:00 +0800 | [diff] [blame] | 1471 | pri = p.Attributes.priority |
| 1472 | friends = groups.pop(pri) |
| 1473 | new_pri = max(groups) + 1 |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1474 | if args.friends: |
Hung-Te Lin | 138389f | 2018-05-15 17:55:00 +0800 | [diff] [blame] | 1475 | groups[new_pri] = friends |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1476 | else: |
Hung-Te Lin | 138389f | 2018-05-15 17:55:00 +0800 | [diff] [blame] | 1477 | groups[new_pri] = [p] |
| 1478 | friends.remove(p) |
| 1479 | if friends: |
| 1480 | groups[pri] = friends |
| 1481 | |
| 1482 | if 0 in groups: |
| 1483 | # Do not change any partitions with priority=0 |
| 1484 | groups.pop(0) |
| 1485 | |
Yilin Yang | 78fa12e | 2019-09-25 14:21:10 +0800 | [diff] [blame] | 1486 | prios = list(groups) |
Hung-Te Lin | 138389f | 2018-05-15 17:55:00 +0800 | [diff] [blame] | 1487 | prios.sort(reverse=True) |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1488 | |
| 1489 | # Max priority is 0xf. |
| 1490 | highest = min(args.priority or len(prios), 0xf) |
| 1491 | logging.info('New highest priority: %s', highest) |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1492 | |
Hung-Te Lin | 138389f | 2018-05-15 17:55:00 +0800 | [diff] [blame] | 1493 | for i, pri in enumerate(prios): |
| 1494 | new_priority = max(1, highest - i) |
| 1495 | for p in groups[pri]: |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1496 | attrs = p.Attributes |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1497 | old_priority = attrs.priority |
Hung-Te Lin | 138389f | 2018-05-15 17:55:00 +0800 | [diff] [blame] | 1498 | if old_priority == new_priority: |
| 1499 | continue |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1500 | attrs.priority = new_priority |
Hung-Te Lin | 138389f | 2018-05-15 17:55:00 +0800 | [diff] [blame] | 1501 | if attrs.tries < 1 and not attrs.successful: |
| 1502 | attrs.tries = 15 # Max tries for new active partition. |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1503 | p.Update(Attributes=attrs) |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1504 | logging.info('%s priority changed from %s to %s.', p, old_priority, |
| 1505 | new_priority) |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1506 | |
| 1507 | gpt.WriteToFile(args.image_file) |
Yilin Yang | f95c25a | 2019-12-23 15:38:51 +0800 | [diff] [blame] | 1508 | args.image_file.close() |
Hung-Te Lin | fe724f8 | 2018-04-18 15:03:58 +0800 | [diff] [blame] | 1509 | |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 1510 | class Find(SubCommand): |
| 1511 | """Locate a partition by its GUID. |
| 1512 | |
| 1513 | Find a partition by its UUID or label. With no specified DRIVE it scans all |
| 1514 | physical drives. |
| 1515 | |
| 1516 | The partition type may also be given as one of these aliases: |
| 1517 | |
| 1518 | firmware ChromeOS firmware |
| 1519 | kernel ChromeOS kernel |
| 1520 | rootfs ChromeOS rootfs |
| 1521 | data Linux data |
| 1522 | reserved ChromeOS reserved |
| 1523 | efi EFI System Partition |
| 1524 | unused Unused (nonexistent) partition |
| 1525 | """ |
| 1526 | def DefineArgs(self, parser): |
| 1527 | parser.add_argument( |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1528 | '-t', '--type-guid', type=GPT.GetTypeGUID, |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 1529 | help='Search for Partition Type GUID') |
| 1530 | parser.add_argument( |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1531 | '-u', '--unique-guid', type=GUID, |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 1532 | help='Search for Partition Unique GUID') |
| 1533 | parser.add_argument( |
| 1534 | '-l', '--label', |
| 1535 | help='Search for Label') |
| 1536 | parser.add_argument( |
| 1537 | '-n', '--numeric', action='store_true', |
| 1538 | help='Numeric output only.') |
| 1539 | parser.add_argument( |
| 1540 | '-1', '--single-match', action='store_true', |
| 1541 | help='Fail if more than one match is found.') |
| 1542 | parser.add_argument( |
| 1543 | '-M', '--match-file', type=str, |
| 1544 | help='Matching partition data must also contain MATCH_FILE content.') |
| 1545 | parser.add_argument( |
| 1546 | '-O', '--offset', type=int, default=0, |
| 1547 | help='Byte offset into partition to match content (default 0).') |
| 1548 | parser.add_argument( |
| 1549 | 'drive', type=argparse.FileType('rb+'), nargs='?', |
| 1550 | help='Drive or disk image file to find.') |
| 1551 | |
| 1552 | def Execute(self, args): |
| 1553 | if not any((args.type_guid, args.unique_guid, args.label)): |
| 1554 | raise GPTError('You must specify at least one of -t, -u, or -l') |
| 1555 | |
| 1556 | drives = [args.drive.name] if args.drive else ( |
| 1557 | '/dev/%s' % name for name in subprocess.check_output( |
Yilin Yang | 42ba5c6 | 2020-05-05 10:32:34 +0800 | [diff] [blame] | 1558 | 'lsblk -d -n -r -o name', shell=True, encoding='utf-8').split()) |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 1559 | |
| 1560 | match_pattern = None |
| 1561 | if args.match_file: |
| 1562 | with open(args.match_file) as f: |
| 1563 | match_pattern = f.read() |
| 1564 | |
| 1565 | found = 0 |
| 1566 | for drive in drives: |
| 1567 | try: |
| 1568 | gpt = GPT.LoadFromFile(drive) |
| 1569 | except GPTError: |
| 1570 | if args.drive: |
| 1571 | raise |
| 1572 | # When scanning all block devices on system, ignore failure. |
| 1573 | |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1574 | def Unmatch(a, b): |
| 1575 | return a is not None and a != b |
| 1576 | |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 1577 | for p in gpt.partitions: |
Hung-Te Lin | bf8aa27 | 2018-04-19 03:02:29 +0800 | [diff] [blame] | 1578 | if (p.IsUnused() or |
Hung-Te Lin | 86ca4bb | 2018-04-25 10:22:10 +0800 | [diff] [blame] | 1579 | Unmatch(args.label, p.Names) or |
| 1580 | Unmatch(args.unique_guid, p.UniqueGUID) or |
| 1581 | Unmatch(args.type_guid, p.TypeGUID)): |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 1582 | continue |
| 1583 | if match_pattern: |
| 1584 | with open(drive, 'rb') as f: |
| 1585 | f.seek(p.offset + args.offset) |
| 1586 | if f.read(len(match_pattern)) != match_pattern: |
| 1587 | continue |
| 1588 | # Found the partition, now print. |
| 1589 | found += 1 |
| 1590 | if args.numeric: |
| 1591 | print(p.number) |
| 1592 | else: |
| 1593 | # This is actually more for block devices. |
| 1594 | print('%s%s%s' % (p.image, 'p' if p.image[-1].isdigit() else '', |
| 1595 | p.number)) |
| 1596 | |
| 1597 | if found < 1 or (args.single_match and found > 1): |
| 1598 | return 1 |
| 1599 | return 0 |
| 1600 | |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1601 | |
| 1602 | def main(): |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1603 | commands = GPTCommands() |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1604 | parser = argparse.ArgumentParser(description='GPT Utility.') |
| 1605 | parser.add_argument('--verbose', '-v', action='count', default=0, |
| 1606 | help='increase verbosity.') |
| 1607 | parser.add_argument('--debug', '-d', action='store_true', |
| 1608 | help='enable debug output.') |
Hung-Te Lin | 5cb0c31 | 2018-04-17 14:56:43 +0800 | [diff] [blame] | 1609 | commands.DefineArgs(parser) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1610 | |
| 1611 | args = parser.parse_args() |
| 1612 | log_level = max(logging.WARNING - args.verbose * 10, logging.DEBUG) |
| 1613 | if args.debug: |
| 1614 | log_level = logging.DEBUG |
| 1615 | logging.basicConfig(format='%(module)s:%(funcName)s %(message)s', |
| 1616 | level=log_level) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1617 | try: |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 1618 | code = commands.Execute(args) |
Peter Shih | 533566a | 2018-09-05 17:48:03 +0800 | [diff] [blame] | 1619 | if isinstance(code, int): |
Hung-Te Lin | f641d30 | 2018-04-18 15:09:35 +0800 | [diff] [blame] | 1620 | sys.exit(code) |
Yilin Yang | 0724c9d | 2019-11-15 15:53:45 +0800 | [diff] [blame] | 1621 | elif isinstance(code, str): |
Hung-Te Lin | bad4611 | 2018-05-15 16:39:14 +0800 | [diff] [blame] | 1622 | print('OK: %s' % code) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1623 | except Exception as e: |
| 1624 | if args.verbose or args.debug: |
| 1625 | logging.exception('Failure in command [%s]', args.command) |
Fei Shao | 9cd93ea | 2020-06-16 18:31:22 +0800 | [diff] [blame] | 1626 | sys.exit('ERROR: %s: %s' % (args.command, str(e) or 'Unknown error.')) |
Hung-Te Lin | c772e1a | 2017-04-14 16:50:50 +0800 | [diff] [blame] | 1627 | |
| 1628 | |
| 1629 | if __name__ == '__main__': |
| 1630 | main() |