blob: c49faacb8972b2d0d1fa1b43961451badfca4c31 [file] [log] [blame]
Mike Frysinger63bb3c72019-09-01 15:16:26 -04001#!/usr/bin/env python2
Hung-Te Linc772e1a2017-04-14 16:50:50 +08002# 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
8Chromium OS factory software usually needs to access partitions from disk
9images. However, there is no good, lightweight, and portable GPT utility.
10Most Chromium OS systems use `cgpt`, but that's not by default installed on
11Ubuntu. Most systems have parted (GNU) or partx (util-linux-ng) but they have
12their own problems.
13
14For example, when a disk image is resized (usually enlarged for putting more
15resources 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
22As a result, we need a dedicated tool to help processing GPT.
23
24This pygpt.py provides a simple and customized implementation for processing
25GPT, as a replacement for `cgpt`.
26"""
27
28
29from __future__ import print_function
30
31import argparse
32import binascii
Yilin Yangf9fe1932019-11-04 17:09:34 +080033import codecs
Hung-Te Lin138389f2018-05-15 17:55:00 +080034import itertools
Hung-Te Linc772e1a2017-04-14 16:50:50 +080035import logging
36import os
Hung-Te Lin446eb512018-05-02 18:39:16 +080037import stat
Hung-Te Linc772e1a2017-04-14 16:50:50 +080038import struct
Hung-Te Linf641d302018-04-18 15:09:35 +080039import subprocess
40import sys
Hung-Te Linc772e1a2017-04-14 16:50:50 +080041import uuid
42
Yilin Yangea784662019-09-26 13:51:03 +080043from six import iteritems
Yilin Yange6639682019-10-03 12:49:21 +080044from six.moves import xrange
45
Hung-Te Linc772e1a2017-04-14 16:50:50 +080046
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +080047class StructError(Exception):
48 """Exceptions in packing and unpacking from/to struct fields."""
49 pass
Hung-Te Linc772e1a2017-04-14 16:50:50 +080050
Hung-Te Linc772e1a2017-04-14 16:50:50 +080051
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +080052class StructField(object):
53 """Definition of a field in struct.
54
55 Attributes:
56 fmt: a format string for struct.{pack,unpack} to use.
57 name: a string for name of processed field.
58 """
59 __slots__ = ['fmt', 'name']
60
61 def __init__(self, fmt, name):
62 self.fmt = fmt
63 self.name = name
64
65 def Pack(self, value):
66 """"Packs given value from given format."""
67 del self # Unused.
68 return value
69
70 def Unpack(self, value):
71 """Unpacks given value into given format."""
72 del self # Unused.
73 return value
74
75
76class UTF16StructField(StructField):
77 """A field in UTF encoded string."""
Yilin Yange4e40e92019-10-31 09:57:57 +080078 __slots__ = ['max_length']
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +080079 encoding = 'utf-16-le'
80
81 def __init__(self, max_length, name):
82 self.max_length = max_length
83 fmt = '%ds' % max_length
84 super(UTF16StructField, self).__init__(fmt, name)
85
86 def Pack(self, value):
87 new_value = value.encode(self.encoding)
88 if len(new_value) >= self.max_length:
89 raise StructError('Value "%s" cannot be packed into field %s (len=%s)' %
90 (value, self.name, self.max_length))
91 return new_value
92
93 def Unpack(self, value):
94 return value.decode(self.encoding).strip('\x00')
Hung-Te Linc772e1a2017-04-14 16:50:50 +080095
Hung-Te Linbf8aa272018-04-19 03:02:29 +080096
97class GUID(uuid.UUID):
98 """A special UUID that defaults to upper case in str()."""
99
100 def __str__(self):
101 """Returns GUID in upper case."""
102 return super(GUID, self).__str__().upper()
103
104 @staticmethod
105 def Random():
106 return uuid.uuid4()
107
108
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800109class GUIDStructField(StructField):
110 """A GUID field."""
111
112 def __init__(self, name):
113 super(GUIDStructField, self).__init__('16s', name)
114
115 def Pack(self, value):
116 if value is None:
117 return '\x00' * 16
118 if not isinstance(value, uuid.UUID):
119 raise StructError('Field %s needs a GUID value instead of [%r].' %
120 (self.name, value))
121 return value.bytes_le
122
123 def Unpack(self, value):
124 return GUID(bytes_le=value)
125
126
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800127def BitProperty(getter, setter, shift, mask):
128 """A generator for bit-field properties.
129
130 This is used inside a class to manipulate an integer-like variable using
131 properties. The getter and setter should be member functions to change the
132 underlying member data.
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800133
134 Args:
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800135 getter: a function to read integer type variable (for all the bits).
136 setter: a function to set the new changed integer type variable.
137 shift: integer for how many bits should be shifted (right).
138 mask: integer for the mask to filter out bit field.
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800139 """
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800140 def _getter(self):
141 return (getter(self) >> shift) & mask
142 def _setter(self, value):
143 assert value & mask == value, (
144 'Value %s out of range (mask=%s)' % (value, mask))
145 setter(self, getter(self) & ~(mask << shift) | value << shift)
146 return property(_getter, _setter)
147
148
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800149class PartitionAttributes(object):
150 """Wrapper for Partition.Attributes.
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800151
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800152 This can be created using Partition.attrs, but the changed properties won't
153 apply to underlying Partition until an explicit call with
154 Partition.Update(Attributes=new_attrs).
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800155 """
156
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800157 def __init__(self, attrs):
158 self._attrs = attrs
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800159
160 @property
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800161 def raw(self):
162 """Returns the raw integer type attributes."""
163 return self._Get()
164
165 def _Get(self):
166 return self._attrs
167
168 def _Set(self, value):
169 self._attrs = value
170
171 successful = BitProperty(_Get, _Set, 56, 1)
172 tries = BitProperty(_Get, _Set, 52, 0xf)
173 priority = BitProperty(_Get, _Set, 48, 0xf)
174 legacy_boot = BitProperty(_Get, _Set, 2, 1)
175 required = BitProperty(_Get, _Set, 0, 1)
176 raw_16 = BitProperty(_Get, _Set, 48, 0xffff)
177
178
179class PartitionAttributeStructField(StructField):
180
181 def Pack(self, value):
182 if not isinstance(value, PartitionAttributes):
183 raise StructError('Given value %r is not %s.' %
184 (value, PartitionAttributes.__name__))
185 return value.raw
186
187 def Unpack(self, value):
188 return PartitionAttributes(value)
189
190
191# The binascii.crc32 returns signed integer, so CRC32 in in struct must be
192# declared as 'signed' (l) instead of 'unsigned' (L).
193# http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_table_header_.28LBA_1.29
194HEADER_FIELDS = [
195 StructField('8s', 'Signature'),
196 StructField('4s', 'Revision'),
197 StructField('L', 'HeaderSize'),
198 StructField('l', 'CRC32'),
199 StructField('4s', 'Reserved'),
200 StructField('Q', 'CurrentLBA'),
201 StructField('Q', 'BackupLBA'),
202 StructField('Q', 'FirstUsableLBA'),
203 StructField('Q', 'LastUsableLBA'),
204 GUIDStructField('DiskGUID'),
205 StructField('Q', 'PartitionEntriesStartingLBA'),
206 StructField('L', 'PartitionEntriesNumber'),
207 StructField('L', 'PartitionEntrySize'),
208 StructField('l', 'PartitionArrayCRC32'),
209]
210
211# http://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_entries
212PARTITION_FIELDS = [
213 GUIDStructField('TypeGUID'),
214 GUIDStructField('UniqueGUID'),
215 StructField('Q', 'FirstLBA'),
216 StructField('Q', 'LastLBA'),
217 PartitionAttributeStructField('Q', 'Attributes'),
218 UTF16StructField(72, 'Names'),
219]
220
221# The PMBR has so many variants. The basic format is defined in
222# https://en.wikipedia.org/wiki/Master_boot_record#Sector_layout, and our
223# implementation, as derived from `cgpt`, is following syslinux as:
224# https://chromium.googlesource.com/chromiumos/platform/vboot_reference/+/master/cgpt/cgpt.h#32
225PMBR_FIELDS = [
226 StructField('424s', 'BootCode'),
227 GUIDStructField('BootGUID'),
228 StructField('L', 'DiskID'),
229 StructField('2s', 'Magic'),
230 StructField('16s', 'LegacyPart0'),
231 StructField('16s', 'LegacyPart1'),
232 StructField('16s', 'LegacyPart2'),
233 StructField('16s', 'LegacyPart3'),
234 StructField('2s', 'Signature'),
235]
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800236
237
Hung-Te Lin4dfd3302018-04-17 14:47:52 +0800238class GPTError(Exception):
239 """All exceptions by GPT."""
240 pass
241
242
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800243class GPTObject(object):
244 """A base object in GUID Partition Table.
245
246 All objects (for instance, header or partition entries) must inherit this
247 class and define the FIELD attribute with a list of field definitions using
248 StructField.
249
250 The 'name' in StructField will become the attribute name of GPT objects that
251 can be directly packed into / unpacked from. Derived (calculated from existing
252 attributes) attributes should be in lower_case.
253
254 It is also possible to attach some additional properties to the object as meta
255 data (for example path of the underlying image file). To do that, first
256 include it in __slots__ list and specify them as dictionary-type args in
257 constructors. These properties will be preserved when you call Clone().
258
259 To create a new object, call the constructor. Field data can be assigned as
260 in arguments, or give nothing to initialize as zero (see Zero()). Field data
261 and meta values can be also specified in keyword arguments (**kargs) at the
262 same time.
263
264 To read a object from file or stream, use class method ReadFrom(source).
265 To make changes, modify the field directly or use Update(dict), or create a
266 copy by Clone() first then Update.
267
268 To wipe all fields (but not meta), call Zero(). There is currently no way
269 to clear meta except setting them to None one by one.
270 """
271 __slots__ = []
272
Peter Shih533566a2018-09-05 17:48:03 +0800273 FIELDS = []
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800274 """A list of StructField definitions."""
275
276 def __init__(self, *args, **kargs):
277 if args:
278 if len(args) != len(self.FIELDS):
279 raise GPTError('%s need %s arguments (found %s).' %
280 (type(self).__name__, len(self.FIELDS), len(args)))
281 for f, value in zip(self.FIELDS, args):
282 setattr(self, f.name, value)
283 else:
284 self.Zero()
285
286 all_names = [f for f in self.__slots__]
Yilin Yangea784662019-09-26 13:51:03 +0800287 for name, value in iteritems(kargs):
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800288 if name not in all_names:
289 raise GPTError('%s does not support keyword arg <%s>.' %
290 (type(self).__name__, name))
291 setattr(self, name, value)
292
293 def __iter__(self):
294 """An iterator to return all fields associated in the object."""
295 return (getattr(self, f.name) for f in self.FIELDS)
296
297 def __repr__(self):
298 return '(%s: %s)' % (type(self).__name__, ', '.join(
Peter Shihe6afab32018-09-11 17:16:48 +0800299 '%s=%r' % (f, getattr(self, f)) for f in self.__slots__))
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800300
301 @classmethod
302 def GetStructFormat(cls):
303 """Returns a format string for struct to use."""
304 return '<' + ''.join(f.fmt for f in cls.FIELDS)
305
306 @classmethod
307 def ReadFrom(cls, source, **kargs):
308 """Returns an object from given source."""
309 obj = cls(**kargs)
310 obj.Unpack(source)
311 return obj
312
313 @property
314 def blob(self):
315 """The (packed) blob representation of the object."""
316 return self.Pack()
317
318 @property
319 def meta(self):
320 """Meta values (those not in GPT object fields)."""
321 metas = set(self.__slots__) - set([f.name for f in self.FIELDS])
322 return dict((name, getattr(self, name)) for name in metas)
323
324 def Unpack(self, source):
325 """Unpacks values from a given source.
326
327 Args:
328 source: a string of bytes or a file-like object to read from.
329 """
330 fmt = self.GetStructFormat()
331 if source is None:
332 source = '\x00' * struct.calcsize(fmt)
333 if not isinstance(source, basestring):
334 return self.Unpack(source.read(struct.calcsize(fmt)))
335 for f, value in zip(self.FIELDS, struct.unpack(fmt, source)):
336 setattr(self, f.name, f.Unpack(value))
Yilin Yang840fdc42020-01-16 16:37:42 +0800337 return None
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800338
339 def Pack(self):
340 """Packs values in all fields into a string by struct format."""
341 return struct.pack(self.GetStructFormat(),
342 *(f.Pack(getattr(self, f.name)) for f in self.FIELDS))
343
344 def Clone(self):
345 """Clones a new instance."""
346 return type(self)(*self, **self.meta)
347
348 def Update(self, **dargs):
349 """Applies multiple values in current object."""
Yilin Yangea784662019-09-26 13:51:03 +0800350 for name, value in iteritems(dargs):
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800351 setattr(self, name, value)
352
353 def Zero(self):
354 """Set all fields to values representing zero or empty.
355
356 Note the meta attributes won't be cleared.
357 """
358 class ZeroReader(object):
359 """A /dev/zero like stream."""
360
361 @staticmethod
362 def read(num):
363 return '\x00' * num
364
365 self.Unpack(ZeroReader())
366
367
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800368class GPT(object):
369 """A GPT helper class.
370
371 To load GPT from an existing disk image file, use `LoadFromFile`.
372 After modifications were made, use `WriteToFile` to commit changes.
373
374 Attributes:
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800375 header: a namedtuple of GPT header.
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800376 pmbr: a namedtuple of Protective MBR.
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800377 partitions: a list of GPT partition entry nametuple.
378 block_size: integer for size of bytes in one block (sector).
Hung-Te Linc34d89c2018-04-17 15:11:34 +0800379 is_secondary: boolean to indicate if the header is from primary or backup.
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800380 """
Hung-Te Linf148d322018-04-13 10:24:42 +0800381 DEFAULT_BLOCK_SIZE = 512
Hung-Te Lin43d54c12019-03-22 11:15:59 +0800382 # Old devices uses 'Basic data' type for stateful partition, and newer devices
383 # should use 'Linux (fS) data' type; so we added a 'stateful' suffix for
384 # migration.
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800385 TYPE_GUID_MAP = {
Hung-Te Linbf8aa272018-04-19 03:02:29 +0800386 GUID('00000000-0000-0000-0000-000000000000'): 'Unused',
Hung-Te Lin43d54c12019-03-22 11:15:59 +0800387 GUID('EBD0A0A2-B9E5-4433-87C0-68B6B72699C7'): 'Basic data stateful',
388 GUID('0FC63DAF-8483-4772-8E79-3D69D8477DE4'): 'Linux data',
Hung-Te Linbf8aa272018-04-19 03:02:29 +0800389 GUID('FE3A2A5D-4F32-41A7-B725-ACCC3285A309'): 'ChromeOS kernel',
390 GUID('3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC'): 'ChromeOS rootfs',
391 GUID('2E0A753D-9E48-43B0-8337-B15192CB1B5E'): 'ChromeOS reserved',
392 GUID('CAB6E88E-ABF3-4102-A07A-D4BB9BE3C1D3'): 'ChromeOS firmware',
393 GUID('C12A7328-F81F-11D2-BA4B-00A0C93EC93B'): 'EFI System Partition',
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800394 }
Hung-Te Linbf8aa272018-04-19 03:02:29 +0800395 TYPE_GUID_FROM_NAME = dict(
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +0800396 ('efi' if v.startswith('EFI') else v.lower().split()[-1], k)
Yilin Yangea784662019-09-26 13:51:03 +0800397 for k, v in iteritems(TYPE_GUID_MAP))
Hung-Te Linbf8aa272018-04-19 03:02:29 +0800398 TYPE_GUID_UNUSED = TYPE_GUID_FROM_NAME['unused']
399 TYPE_GUID_CHROMEOS_KERNEL = TYPE_GUID_FROM_NAME['kernel']
400 TYPE_GUID_LIST_BOOTABLE = [
401 TYPE_GUID_CHROMEOS_KERNEL,
402 TYPE_GUID_FROM_NAME['efi'],
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800403 ]
404
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800405 class ProtectiveMBR(GPTObject):
406 """Protective MBR (PMBR) in GPT."""
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800407 FIELDS = PMBR_FIELDS
408 __slots__ = [f.name for f in FIELDS]
409
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800410 SIGNATURE = '\x55\xAA'
411 MAGIC = '\x1d\x9a'
412
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800413 class Header(GPTObject):
414 """Wrapper to Header in GPT."""
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800415 FIELDS = HEADER_FIELDS
416 __slots__ = [f.name for f in FIELDS]
417
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800418 SIGNATURES = ['EFI PART', 'CHROMEOS']
419 SIGNATURE_IGNORE = 'IGNOREME'
420 DEFAULT_REVISION = '\x00\x00\x01\x00'
421
422 DEFAULT_PARTITION_ENTRIES = 128
423 DEFAULT_PARTITIONS_LBA = 2 # LBA 0 = MBR, LBA 1 = GPT Header.
424
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800425 @classmethod
426 def Create(cls, size, block_size, pad_blocks=0,
427 part_entries=DEFAULT_PARTITION_ENTRIES):
428 """Creates a header with default values.
429
430 Args:
431 size: integer of expected image size.
432 block_size: integer for size of each block (sector).
433 pad_blocks: number of preserved sectors between header and partitions.
434 part_entries: number of partitions to include in header.
435 """
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800436 PART_FORMAT = GPT.Partition.GetStructFormat()
437 FORMAT = cls.GetStructFormat()
438 part_entry_size = struct.calcsize(PART_FORMAT)
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800439 parts_lba = cls.DEFAULT_PARTITIONS_LBA + pad_blocks
440 parts_bytes = part_entries * part_entry_size
Yilin Yang14d02a22019-11-01 11:32:03 +0800441 parts_blocks = parts_bytes // block_size
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800442 if parts_bytes % block_size:
443 parts_blocks += 1
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800444 # CRC32 and PartitionsCRC32 must be updated later explicitly.
445 return cls(
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800446 Signature=cls.SIGNATURES[0],
447 Revision=cls.DEFAULT_REVISION,
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800448 HeaderSize=struct.calcsize(FORMAT),
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800449 CurrentLBA=1,
Yilin Yang14d02a22019-11-01 11:32:03 +0800450 BackupLBA=size // block_size - 1,
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800451 FirstUsableLBA=parts_lba + parts_blocks,
Yilin Yang14d02a22019-11-01 11:32:03 +0800452 LastUsableLBA=size // block_size - parts_blocks - parts_lba,
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800453 DiskGUID=GUID.Random(),
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800454 PartitionEntriesStartingLBA=parts_lba,
455 PartitionEntriesNumber=part_entries,
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800456 PartitionEntrySize=part_entry_size)
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800457
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800458 def UpdateChecksum(self):
459 """Updates the CRC32 field in GPT header.
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800460
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800461 Note the PartitionArrayCRC32 is not touched - you have to make sure that
462 is correct before calling Header.UpdateChecksum().
463 """
464 self.Update(CRC32=0)
465 self.Update(CRC32=binascii.crc32(self.blob))
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800466
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800467 class Partition(GPTObject):
468 """The partition entry in GPT.
469
470 Please include following properties when creating a Partition object:
471 - image: a string for path to the image file the partition maps to.
472 - number: the 1-based partition number.
473 - block_size: an integer for size of each block (LBA, or sector).
474 """
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800475 FIELDS = PARTITION_FIELDS
476 __slots__ = [f.name for f in FIELDS] + ['image', 'number', 'block_size']
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800477 NAMES_ENCODING = 'utf-16-le'
478 NAMES_LENGTH = 72
479
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800480 def __str__(self):
481 return '%s#%s' % (self.image, self.number)
482
483 def IsUnused(self):
484 """Returns if the partition is unused and can be allocated."""
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800485 return self.TypeGUID == GPT.TYPE_GUID_UNUSED
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800486
Hung-Te Linfe724f82018-04-18 15:03:58 +0800487 def IsChromeOSKernel(self):
488 """Returns if the partition is a Chrome OS kernel partition."""
Hung-Te Lin048ac5e2018-05-03 23:47:45 +0800489 return self.TypeGUID == GPT.TYPE_GUID_CHROMEOS_KERNEL
Hung-Te Linfe724f82018-04-18 15:03:58 +0800490
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800491 @property
492 def blocks(self):
493 """Return size of partition in blocks (see block_size)."""
494 return self.LastLBA - self.FirstLBA + 1
495
496 @property
497 def offset(self):
498 """Returns offset to partition in bytes."""
499 return self.FirstLBA * self.block_size
500
501 @property
502 def size(self):
503 """Returns size of partition in bytes."""
504 return self.blocks * self.block_size
505
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800506 def __init__(self):
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800507 """GPT constructor.
508
509 See LoadFromFile for how it's usually used.
510 """
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800511 self.pmbr = None
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800512 self.header = None
513 self.partitions = None
Hung-Te Linf148d322018-04-13 10:24:42 +0800514 self.block_size = self.DEFAULT_BLOCK_SIZE
Hung-Te Linc34d89c2018-04-17 15:11:34 +0800515 self.is_secondary = False
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800516
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800517 @classmethod
Hung-Te Linbf8aa272018-04-19 03:02:29 +0800518 def GetTypeGUID(cls, value):
519 """The value may be a GUID in string or a short type string."""
520 guid = cls.TYPE_GUID_FROM_NAME.get(value.lower())
521 return GUID(value) if guid is None else guid
Hung-Te Linf641d302018-04-18 15:09:35 +0800522
523 @classmethod
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800524 def Create(cls, image_name, size, block_size, pad_blocks=0):
525 """Creates a new GPT instance from given size and block_size.
526
527 Args:
528 image_name: a string of underlying disk image file name.
529 size: expected size of disk image.
530 block_size: size of each block (sector) in bytes.
531 pad_blocks: number of blocks between header and partitions array.
532 """
533 gpt = cls()
534 gpt.block_size = block_size
535 gpt.header = cls.Header.Create(size, block_size, pad_blocks)
536 gpt.partitions = [
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800537 cls.Partition(block_size=block_size, image=image_name, number=i + 1)
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800538 for i in xrange(gpt.header.PartitionEntriesNumber)]
539 return gpt
540
Hung-Te Lin446eb512018-05-02 18:39:16 +0800541 @staticmethod
542 def IsBlockDevice(image):
543 """Returns if the image is a block device file."""
544 return stat.S_ISBLK(os.stat(image).st_mode)
545
546 @classmethod
547 def GetImageSize(cls, image):
548 """Returns the size of specified image (plain or block device file)."""
549 if not cls.IsBlockDevice(image):
550 return os.path.getsize(image)
551
552 fd = os.open(image, os.O_RDONLY)
553 try:
554 return os.lseek(fd, 0, os.SEEK_END)
555 finally:
556 os.close(fd)
557
558 @classmethod
559 def GetLogicalBlockSize(cls, block_dev):
560 """Returns the logical block (sector) size from a block device file.
561
562 The underlying call is BLKSSZGET. An alternative command is blockdev,
563 but that needs root permission even if we just want to get sector size.
564 """
565 assert cls.IsBlockDevice(block_dev), '%s must be block device.' % block_dev
566 return int(subprocess.check_output(
567 ['lsblk', '-d', '-n', '-r', '-o', 'log-sec', block_dev]).strip())
568
Hung-Te Lin6c3575a2018-04-17 15:00:49 +0800569 @classmethod
Hung-Te Lin6977ae12018-04-17 12:20:32 +0800570 def LoadFromFile(cls, image):
571 """Loads a GPT table from give disk image file object.
572
573 Args:
574 image: a string as file path or a file-like object to read from.
575 """
576 if isinstance(image, basestring):
577 with open(image, 'rb') as f:
578 return cls.LoadFromFile(f)
579
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800580 gpt = cls()
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800581 image.seek(0)
582 pmbr = gpt.ProtectiveMBR.ReadFrom(image)
583 if pmbr.Signature == cls.ProtectiveMBR.SIGNATURE:
584 logging.debug('Found MBR signature in %s', image.name)
585 if pmbr.Magic == cls.ProtectiveMBR.MAGIC:
586 logging.debug('Found PMBR in %s', image.name)
587 gpt.pmbr = pmbr
588
Hung-Te Linf148d322018-04-13 10:24:42 +0800589 # Try DEFAULT_BLOCK_SIZE, then 4K.
Hung-Te Lin446eb512018-05-02 18:39:16 +0800590 block_sizes = [cls.DEFAULT_BLOCK_SIZE, 4096]
591 if cls.IsBlockDevice(image.name):
592 block_sizes = [cls.GetLogicalBlockSize(image.name)]
593
594 for block_size in block_sizes:
Hung-Te Linc34d89c2018-04-17 15:11:34 +0800595 # Note because there are devices setting Primary as ignored and the
596 # partition table signature accepts 'CHROMEOS' which is also used by
597 # Chrome OS kernel partition, we have to look for Secondary (backup) GPT
598 # first before trying other block sizes, otherwise we may incorrectly
599 # identify a kernel partition as LBA 1 of larger block size system.
600 for i, seek in enumerate([(block_size * 1, os.SEEK_SET),
601 (-block_size, os.SEEK_END)]):
602 image.seek(*seek)
603 header = gpt.Header.ReadFrom(image)
604 if header.Signature in cls.Header.SIGNATURES:
605 gpt.block_size = block_size
606 if i != 0:
607 gpt.is_secondary = True
608 break
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800609 # TODO(hungte) Try harder to see if this block is valid.
Hung-Te Linc34d89c2018-04-17 15:11:34 +0800610 else:
611 # Nothing found, try next block size.
612 continue
613 # Found a valid signature.
614 break
Hung-Te Linf148d322018-04-13 10:24:42 +0800615 else:
Hung-Te Lin4dfd3302018-04-17 14:47:52 +0800616 raise GPTError('Invalid signature in GPT header.')
Hung-Te Linf148d322018-04-13 10:24:42 +0800617
Hung-Te Lin6977ae12018-04-17 12:20:32 +0800618 image.seek(gpt.block_size * header.PartitionEntriesStartingLBA)
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800619 def ReadPartition(image, number):
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800620 p = gpt.Partition.ReadFrom(
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800621 image, image=image.name, number=number, block_size=gpt.block_size)
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800622 return p
623
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800624 gpt.header = header
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800625 gpt.partitions = [
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800626 ReadPartition(image, i + 1)
627 for i in xrange(header.PartitionEntriesNumber)]
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800628 return gpt
629
Hung-Te Linc5196682018-04-18 22:59:59 +0800630 def GetUsedPartitions(self):
631 """Returns a list of partitions with type GUID not set to unused.
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800632
Hung-Te Linc5196682018-04-18 22:59:59 +0800633 Use 'number' property to find the real location of partition in
634 self.partitions.
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800635 """
Hung-Te Linc5196682018-04-18 22:59:59 +0800636 return [p for p in self.partitions if not p.IsUnused()]
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800637
638 def GetMaxUsedLBA(self):
Hung-Te Lind3a2e9a2018-04-19 13:07:26 +0800639 """Returns the max LastLBA from all used partitions."""
Hung-Te Linc5196682018-04-18 22:59:59 +0800640 parts = self.GetUsedPartitions()
Hung-Te Lind3a2e9a2018-04-19 13:07:26 +0800641 return (max(p.LastLBA for p in parts)
642 if parts else self.header.FirstUsableLBA - 1)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800643
644 def GetPartitionTableBlocks(self, header=None):
645 """Returns the blocks (or LBA) of partition table from given header."""
646 if header is None:
647 header = self.header
648 size = header.PartitionEntrySize * header.PartitionEntriesNumber
Yilin Yang14d02a22019-11-01 11:32:03 +0800649 blocks = size // self.block_size
Hung-Te Linf148d322018-04-13 10:24:42 +0800650 if size % self.block_size:
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800651 blocks += 1
652 return blocks
653
Hung-Te Lin5f0dea42018-04-18 23:20:11 +0800654 def GetPartition(self, number):
655 """Gets the Partition by given (1-based) partition number.
656
657 Args:
658 number: an integer as 1-based partition number.
659 """
660 if not 0 < number <= len(self.partitions):
661 raise GPTError('Invalid partition number %s.' % number)
662 return self.partitions[number - 1]
663
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800664 def UpdatePartition(self, part, number):
Hung-Te Lin5f0dea42018-04-18 23:20:11 +0800665 """Updates the entry in partition table by given Partition object.
666
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800667 Usually you only need to call this if you want to copy one partition to
668 different location (number of image).
669
Hung-Te Lin5f0dea42018-04-18 23:20:11 +0800670 Args:
671 part: a Partition GPT object.
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800672 number: an integer as 1-based partition number.
Hung-Te Lin5f0dea42018-04-18 23:20:11 +0800673 """
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800674 ref = self.partitions[number - 1]
675 part = part.Clone()
676 part.number = number
677 part.image = ref.image
678 part.block_size = ref.block_size
Hung-Te Lin5f0dea42018-04-18 23:20:11 +0800679 self.partitions[number - 1] = part
680
Cheng-Han Yangdc235b32019-01-08 18:05:40 +0800681 def GetSize(self):
682 return self.block_size * (self.header.BackupLBA + 1)
683
684 def Resize(self, new_size, check_overlap=True):
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800685 """Adjust GPT for a disk image in given size.
686
687 Args:
688 new_size: Integer for new size of disk image file.
Cheng-Han Yangdc235b32019-01-08 18:05:40 +0800689 check_overlap: Checks if the backup partition table overlaps used
690 partitions.
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800691 """
Cheng-Han Yangdc235b32019-01-08 18:05:40 +0800692 old_size = self.GetSize()
Hung-Te Linf148d322018-04-13 10:24:42 +0800693 if new_size % self.block_size:
Hung-Te Lin4dfd3302018-04-17 14:47:52 +0800694 raise GPTError(
695 'New file size %d is not valid for image files.' % new_size)
Yilin Yang14d02a22019-11-01 11:32:03 +0800696 new_blocks = new_size // self.block_size
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800697 if old_size != new_size:
698 logging.warn('Image size (%d, LBA=%d) changed from %d (LBA=%d).',
Yilin Yang14d02a22019-11-01 11:32:03 +0800699 new_size, new_blocks, old_size, old_size // self.block_size)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800700 else:
701 logging.info('Image size (%d, LBA=%d) not changed.',
702 new_size, new_blocks)
Hung-Te Lind3a2e9a2018-04-19 13:07:26 +0800703 return
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800704
Hung-Te Lind3a2e9a2018-04-19 13:07:26 +0800705 # Expected location
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800706 backup_lba = new_blocks - 1
Hung-Te Lind3a2e9a2018-04-19 13:07:26 +0800707 last_usable_lba = backup_lba - self.header.FirstUsableLBA
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800708
Cheng-Han Yangdc235b32019-01-08 18:05:40 +0800709 if check_overlap and last_usable_lba < self.header.LastUsableLBA:
Hung-Te Lind3a2e9a2018-04-19 13:07:26 +0800710 max_used_lba = self.GetMaxUsedLBA()
711 if last_usable_lba < max_used_lba:
Hung-Te Lin4dfd3302018-04-17 14:47:52 +0800712 raise GPTError('Backup partition tables will overlap used partitions')
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800713
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800714 self.header.Update(BackupLBA=backup_lba, LastUsableLBA=last_usable_lba)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800715
716 def GetFreeSpace(self):
717 """Returns the free (available) space left according to LastUsableLBA."""
718 max_lba = self.GetMaxUsedLBA()
719 assert max_lba <= self.header.LastUsableLBA, "Partitions too large."
Hung-Te Linf148d322018-04-13 10:24:42 +0800720 return self.block_size * (self.header.LastUsableLBA - max_lba)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800721
Hung-Te Lin5f0dea42018-04-18 23:20:11 +0800722 def ExpandPartition(self, number):
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800723 """Expands a given partition to last usable LBA.
724
Cheng-Han Yangdc235b32019-01-08 18:05:40 +0800725 The size of the partition can actually be reduced if the last usable LBA
726 decreases.
727
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800728 Args:
Hung-Te Lin5f0dea42018-04-18 23:20:11 +0800729 number: an integer to specify partition in 1-based number.
Hung-Te Lin5cb0c312018-04-17 14:56:43 +0800730
731 Returns:
732 (old_blocks, new_blocks) for size in blocks.
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800733 """
734 # Assume no partitions overlap, we need to make sure partition[i] has
735 # largest LBA.
Hung-Te Lin5f0dea42018-04-18 23:20:11 +0800736 p = self.GetPartition(number)
737 if p.IsUnused():
738 raise GPTError('Partition %s is unused.' % p)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800739 max_used_lba = self.GetMaxUsedLBA()
Hung-Te Linc5196682018-04-18 22:59:59 +0800740 # TODO(hungte) We can do more by finding free space after i.
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800741 if max_used_lba > p.LastLBA:
Hung-Te Lin4dfd3302018-04-17 14:47:52 +0800742 raise GPTError(
Hung-Te Linc5196682018-04-18 22:59:59 +0800743 'Cannot expand %s because it is not allocated at last.' % p)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800744
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800745 old_blocks = p.blocks
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800746 p.Update(LastLBA=self.header.LastUsableLBA)
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800747 new_blocks = p.blocks
Hung-Te Lin5cb0c312018-04-17 14:56:43 +0800748 logging.warn(
Cheng-Han Yangdc235b32019-01-08 18:05:40 +0800749 '%s size changed in LBA: %d -> %d.', p, old_blocks, new_blocks)
Hung-Te Lin5cb0c312018-04-17 14:56:43 +0800750 return (old_blocks, new_blocks)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800751
Hung-Te Lin3b491672018-04-19 01:41:20 +0800752 def CheckIntegrity(self):
753 """Checks if the GPT objects all look good."""
754 # Check if the header allocation looks good. CurrentLBA and
755 # PartitionEntriesStartingLBA should be all outside [FirstUsableLBA,
756 # LastUsableLBA].
757 header = self.header
758 entries_first_lba = header.PartitionEntriesStartingLBA
759 entries_last_lba = entries_first_lba + self.GetPartitionTableBlocks() - 1
760
761 def CheckOutsideUsable(name, lba, outside_entries=False):
762 if lba < 1:
763 raise GPTError('%s should not live in LBA %s.' % (name, lba))
764 if lba > max(header.BackupLBA, header.CurrentLBA):
765 # Note this is "in theory" possible, but we want to report this as
766 # error as well, since it usually leads to error.
767 raise GPTError('%s (%s) should not be larger than BackupLBA (%s).' %
768 (name, lba, header.BackupLBA))
769 if header.FirstUsableLBA <= lba <= header.LastUsableLBA:
770 raise GPTError('%s (%s) should not be included in usable LBAs [%s,%s]' %
771 (name, lba, header.FirstUsableLBA, header.LastUsableLBA))
772 if outside_entries and entries_first_lba <= lba <= entries_last_lba:
773 raise GPTError('%s (%s) should be outside partition entries [%s,%s]' %
774 (name, lba, entries_first_lba, entries_last_lba))
775 CheckOutsideUsable('Header', header.CurrentLBA, True)
776 CheckOutsideUsable('Backup header', header.BackupLBA, True)
777 CheckOutsideUsable('Partition entries', entries_first_lba)
778 CheckOutsideUsable('Partition entries end', entries_last_lba)
779
780 parts = self.GetUsedPartitions()
781 # Check if partition entries overlap with each other.
782 lba_list = [(p.FirstLBA, p.LastLBA, p) for p in parts]
783 lba_list.sort(key=lambda t: t[0])
784 for i in xrange(len(lba_list) - 1):
785 if lba_list[i][1] >= lba_list[i + 1][0]:
786 raise GPTError('Overlap in partition entries: [%s,%s]%s, [%s,%s]%s.' %
787 (lba_list[i] + lba_list[i + 1]))
788 # Now, check the first and last partition.
789 if lba_list:
790 p = lba_list[0][2]
791 if p.FirstLBA < header.FirstUsableLBA:
792 raise GPTError(
793 'Partition %s must not go earlier (%s) than FirstUsableLBA=%s' %
794 (p, p.FirstLBA, header.FirstLBA))
795 p = lba_list[-1][2]
796 if p.LastLBA > header.LastUsableLBA:
797 raise GPTError(
798 'Partition %s must not go further (%s) than LastUsableLBA=%s' %
799 (p, p.LastLBA, header.LastLBA))
800 # Check if UniqueGUIDs are not unique.
801 if len(set(p.UniqueGUID for p in parts)) != len(parts):
802 raise GPTError('Partition UniqueGUIDs are duplicated.')
803 # Check if CRCs match.
804 if (binascii.crc32(''.join(p.blob for p in self.partitions)) !=
805 header.PartitionArrayCRC32):
806 raise GPTError('GPT Header PartitionArrayCRC32 does not match.')
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800807 header_crc = header.Clone()
808 header_crc.UpdateChecksum()
809 if header_crc.CRC32 != header.CRC32:
810 raise GPTError('GPT Header CRC32 does not match.')
Hung-Te Lin3b491672018-04-19 01:41:20 +0800811
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800812 def UpdateChecksum(self):
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800813 """Updates all checksum fields in GPT objects."""
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800814 parts = ''.join(p.blob for p in self.partitions)
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800815 self.header.Update(PartitionArrayCRC32=binascii.crc32(parts))
816 self.header.UpdateChecksum()
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800817
Hung-Te Linc34d89c2018-04-17 15:11:34 +0800818 def GetBackupHeader(self, header):
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800819 """Returns the backup header according to given header.
820
821 This should be invoked only after GPT.UpdateChecksum() has updated all CRC32
822 fields.
823 """
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800824 partitions_starting_lba = (
Hung-Te Linc34d89c2018-04-17 15:11:34 +0800825 header.BackupLBA - self.GetPartitionTableBlocks())
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800826 h = header.Clone()
827 h.Update(
Hung-Te Linc34d89c2018-04-17 15:11:34 +0800828 BackupLBA=header.CurrentLBA,
829 CurrentLBA=header.BackupLBA,
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800830 PartitionEntriesStartingLBA=partitions_starting_lba)
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800831 h.UpdateChecksum()
832 return h
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800833
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800834 @classmethod
835 def WriteProtectiveMBR(cls, image, create, bootcode=None, boot_guid=None):
836 """Writes a protective MBR to given file.
837
838 Each MBR is 512 bytes: 424 bytes for bootstrap code, 16 bytes of boot GUID,
839 4 bytes of disk id, 2 bytes of bootcode magic, 4*16 for 4 partitions, and 2
840 byte as signature. cgpt has hard-coded the CHS and bootstrap magic values so
841 we can follow that.
842
843 Args:
844 create: True to re-create PMBR structure.
845 bootcode: a blob of new boot code.
846 boot_guid a blob for new boot GUID.
847
848 Returns:
849 The written PMBR structure.
850 """
851 if isinstance(image, basestring):
852 with open(image, 'rb+') as f:
853 return cls.WriteProtectiveMBR(f, create, bootcode, boot_guid)
854
855 image.seek(0)
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800856 pmbr_format = cls.ProtectiveMBR.GetStructFormat()
857 assert struct.calcsize(pmbr_format) == cls.DEFAULT_BLOCK_SIZE
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800858 pmbr = cls.ProtectiveMBR.ReadFrom(image)
859
860 if create:
861 legacy_sectors = min(
862 0x100000000,
Yilin Yang14d02a22019-11-01 11:32:03 +0800863 GPT.GetImageSize(image.name) // cls.DEFAULT_BLOCK_SIZE) - 1
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800864 # Partition 0 must have have the fixed CHS with number of sectors
865 # (calculated as legacy_sectors later).
Yilin Yangf9fe1932019-11-04 17:09:34 +0800866 part0 = (codecs.decode('00000200eeffffff01000000', 'hex') +
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800867 struct.pack('<I', legacy_sectors))
868 # Partition 1~3 should be all zero.
869 part1 = '\x00' * 16
870 assert len(part0) == len(part1) == 16, 'MBR entry is wrong.'
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800871 pmbr.Update(
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800872 BootGUID=cls.TYPE_GUID_UNUSED,
873 DiskID=0,
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800874 Magic=pmbr.MAGIC,
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800875 LegacyPart0=part0,
876 LegacyPart1=part1,
877 LegacyPart2=part1,
878 LegacyPart3=part1,
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800879 Signature=pmbr.SIGNATURE)
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800880
881 if bootcode:
882 if len(bootcode) > len(pmbr.BootCode):
883 logging.info(
884 'Bootcode is larger (%d > %d)!', len(bootcode), len(pmbr.BootCode))
885 bootcode = bootcode[:len(pmbr.BootCode)]
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800886 pmbr.Update(BootCode=bootcode)
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800887 if boot_guid:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +0800888 pmbr.Update(BootGUID=boot_guid)
Hung-Te Linc6e009c2018-04-17 15:06:16 +0800889
890 blob = pmbr.blob
891 assert len(blob) == cls.DEFAULT_BLOCK_SIZE
892 image.seek(0)
893 image.write(blob)
894 return pmbr
895
Hung-Te Lin6977ae12018-04-17 12:20:32 +0800896 def WriteToFile(self, image):
897 """Updates partition table in a disk image file.
898
899 Args:
900 image: a string as file path or a file-like object to write into.
901 """
902 if isinstance(image, basestring):
903 with open(image, 'rb+') as f:
904 return self.WriteToFile(f)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800905
906 def WriteData(name, blob, lba):
907 """Writes a blob into given location."""
908 logging.info('Writing %s in LBA %d (offset %d)',
Hung-Te Linf148d322018-04-13 10:24:42 +0800909 name, lba, lba * self.block_size)
Hung-Te Lin6977ae12018-04-17 12:20:32 +0800910 image.seek(lba * self.block_size)
911 image.write(blob)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800912
913 self.UpdateChecksum()
Hung-Te Lin3b491672018-04-19 01:41:20 +0800914 self.CheckIntegrity()
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800915 parts_blob = ''.join(p.blob for p in self.partitions)
Hung-Te Linc34d89c2018-04-17 15:11:34 +0800916
917 header = self.header
918 WriteData('GPT Header', header.blob, header.CurrentLBA)
919 WriteData('GPT Partitions', parts_blob, header.PartitionEntriesStartingLBA)
920 logging.info(
921 'Usable LBA: First=%d, Last=%d', header.FirstUsableLBA,
922 header.LastUsableLBA)
923
924 if not self.is_secondary:
925 # When is_secondary is True, the header we have is actually backup header.
926 backup_header = self.GetBackupHeader(self.header)
927 WriteData(
928 'Backup Partitions', parts_blob,
929 backup_header.PartitionEntriesStartingLBA)
930 WriteData(
931 'Backup Header', backup_header.blob, backup_header.CurrentLBA)
Yilin Yang840fdc42020-01-16 16:37:42 +0800932 return None
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800933
934
935class GPTCommands(object):
936 """Collection of GPT sub commands for command line to use.
937
938 The commands are derived from `cgpt`, but not necessary to be 100% compatible
939 with cgpt.
940 """
941
942 FORMAT_ARGS = [
Peter Shihc7156ca2018-02-26 14:46:24 +0800943 ('begin', 'beginning sector'),
Hung-Te Lin49ac3c22018-04-17 14:37:54 +0800944 ('size', 'partition size (in sectors)'),
Peter Shihc7156ca2018-02-26 14:46:24 +0800945 ('type', 'type guid'),
946 ('unique', 'unique guid'),
947 ('label', 'label'),
948 ('Successful', 'Successful flag'),
949 ('Tries', 'Tries flag'),
950 ('Priority', 'Priority flag'),
951 ('Legacy', 'Legacy Boot flag'),
952 ('Attribute', 'raw 16-bit attribute value (bits 48-63)')]
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800953
954 def __init__(self):
Hung-Te Lin5cb0c312018-04-17 14:56:43 +0800955 commands = dict(
956 (command.lower(), getattr(self, command)())
957 for command in dir(self)
958 if (isinstance(getattr(self, command), type) and
959 issubclass(getattr(self, command), self.SubCommand) and
960 getattr(self, command) is not self.SubCommand)
961 )
962 self.commands = commands
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800963
Hung-Te Lin5cb0c312018-04-17 14:56:43 +0800964 def DefineArgs(self, parser):
965 """Defines all available commands to an argparser subparsers instance."""
966 subparsers = parser.add_subparsers(help='Sub-command help.', dest='command')
Yilin Yangea784662019-09-26 13:51:03 +0800967 for name, instance in sorted(iteritems(self.commands)):
Hung-Te Lin5cb0c312018-04-17 14:56:43 +0800968 parser = subparsers.add_parser(
969 name, description=instance.__doc__,
970 formatter_class=argparse.RawDescriptionHelpFormatter,
971 help=instance.__doc__.splitlines()[0])
972 instance.DefineArgs(parser)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800973
Hung-Te Lin5cb0c312018-04-17 14:56:43 +0800974 def Execute(self, args):
975 """Execute the sub commands by given parsed arguments."""
Hung-Te Linf641d302018-04-18 15:09:35 +0800976 return self.commands[args.command].Execute(args)
Hung-Te Linc772e1a2017-04-14 16:50:50 +0800977
Hung-Te Lin5cb0c312018-04-17 14:56:43 +0800978 class SubCommand(object):
979 """A base class for sub commands to derive from."""
980
981 def DefineArgs(self, parser):
982 """Defines command line arguments to argparse parser.
983
984 Args:
985 parser: An argparse parser instance.
986 """
987 del parser # Unused.
988 raise NotImplementedError
989
990 def Execute(self, args):
Hung-Te Line0d1fa72018-05-15 00:04:48 +0800991 """Execute the command with parsed arguments.
992
993 To execute with raw arguments, use ExecuteCommandLine instead.
Hung-Te Lin5cb0c312018-04-17 14:56:43 +0800994
995 Args:
996 args: An argparse parsed namespace.
997 """
998 del args # Unused.
999 raise NotImplementedError
1000
Hung-Te Line0d1fa72018-05-15 00:04:48 +08001001 def ExecuteCommandLine(self, *args):
1002 """Execute as invoked from command line.
1003
1004 This provides an easy way to execute particular sub command without
1005 creating argument parser explicitly.
1006
1007 Args:
1008 args: a list of string type command line arguments.
1009 """
1010 parser = argparse.ArgumentParser()
1011 self.DefineArgs(parser)
1012 return self.Execute(parser.parse_args(args))
1013
Hung-Te Lin6c3575a2018-04-17 15:00:49 +08001014 class Create(SubCommand):
1015 """Create or reset GPT headers and tables.
1016
1017 Create or reset an empty GPT.
1018 """
1019
1020 def DefineArgs(self, parser):
1021 parser.add_argument(
1022 '-z', '--zero', action='store_true',
1023 help='Zero the sectors of the GPT table and entries')
1024 parser.add_argument(
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001025 '-p', '--pad-blocks', type=int, default=0,
Hung-Te Lin6c3575a2018-04-17 15:00:49 +08001026 help=('Size (in blocks) of the disk to pad between the '
1027 'primary GPT header and its entries, default %(default)s'))
1028 parser.add_argument(
Hung-Te Lin446eb512018-05-02 18:39:16 +08001029 '--block_size', type=int,
Hung-Te Lin6c3575a2018-04-17 15:00:49 +08001030 help='Size of each block (sector) in bytes.')
1031 parser.add_argument(
1032 'image_file', type=argparse.FileType('rb+'),
1033 help='Disk image file to create.')
1034
1035 def Execute(self, args):
1036 block_size = args.block_size
Hung-Te Lin446eb512018-05-02 18:39:16 +08001037 if block_size is None:
1038 if GPT.IsBlockDevice(args.image_file.name):
1039 block_size = GPT.GetLogicalBlockSize(args.image_file.name)
1040 else:
1041 block_size = GPT.DEFAULT_BLOCK_SIZE
1042
1043 if block_size != GPT.DEFAULT_BLOCK_SIZE:
1044 logging.info('Block (sector) size for %s is set to %s bytes.',
1045 args.image_file.name, block_size)
1046
Hung-Te Lin6c3575a2018-04-17 15:00:49 +08001047 gpt = GPT.Create(
Hung-Te Lin446eb512018-05-02 18:39:16 +08001048 args.image_file.name, GPT.GetImageSize(args.image_file.name),
Hung-Te Lin6c3575a2018-04-17 15:00:49 +08001049 block_size, args.pad_blocks)
1050 if args.zero:
1051 # In theory we only need to clear LBA 1, but to make sure images already
1052 # initialized with different block size won't have GPT signature in
1053 # different locations, we should zero until first usable LBA.
1054 args.image_file.seek(0)
1055 args.image_file.write('\0' * block_size * gpt.header.FirstUsableLBA)
1056 gpt.WriteToFile(args.image_file)
Yilin Yangf95c25a2019-12-23 15:38:51 +08001057 args.image_file.close()
Hung-Te Linbad46112018-05-15 16:39:14 +08001058 return 'Created GPT for %s' % args.image_file.name
Hung-Te Lin6c3575a2018-04-17 15:00:49 +08001059
Hung-Te Linc6e009c2018-04-17 15:06:16 +08001060 class Boot(SubCommand):
1061 """Edit the PMBR sector for legacy BIOSes.
1062
1063 With no options, it will just print the PMBR boot guid.
1064 """
1065
1066 def DefineArgs(self, parser):
1067 parser.add_argument(
1068 '-i', '--number', type=int,
1069 help='Set bootable partition')
1070 parser.add_argument(
1071 '-b', '--bootloader', type=argparse.FileType('r'),
1072 help='Install bootloader code in the PMBR')
1073 parser.add_argument(
1074 '-p', '--pmbr', action='store_true',
1075 help='Create legacy PMBR partition table')
1076 parser.add_argument(
1077 'image_file', type=argparse.FileType('rb+'),
1078 help='Disk image file to change PMBR.')
1079
1080 def Execute(self, args):
1081 """Rebuilds the protective MBR."""
1082 bootcode = args.bootloader.read() if args.bootloader else None
1083 boot_guid = None
1084 if args.number is not None:
1085 gpt = GPT.LoadFromFile(args.image_file)
Hung-Te Lin5f0dea42018-04-18 23:20:11 +08001086 boot_guid = gpt.GetPartition(args.number).UniqueGUID
Hung-Te Linc6e009c2018-04-17 15:06:16 +08001087 pmbr = GPT.WriteProtectiveMBR(
1088 args.image_file, args.pmbr, bootcode=bootcode, boot_guid=boot_guid)
1089
You-Cheng Syufff7f422018-05-14 15:37:39 +08001090 print(pmbr.BootGUID)
Yilin Yangf95c25a2019-12-23 15:38:51 +08001091 args.image_file.close()
Hung-Te Linbad46112018-05-15 16:39:14 +08001092 return 0
Hung-Te Linc6e009c2018-04-17 15:06:16 +08001093
Hung-Te Linc34d89c2018-04-17 15:11:34 +08001094 class Legacy(SubCommand):
1095 """Switch between GPT and Legacy GPT.
1096
1097 Switch GPT header signature to "CHROMEOS".
1098 """
1099
1100 def DefineArgs(self, parser):
1101 parser.add_argument(
1102 '-e', '--efi', action='store_true',
1103 help='Switch GPT header signature back to "EFI PART"')
1104 parser.add_argument(
1105 '-p', '--primary-ignore', action='store_true',
1106 help='Switch primary GPT header signature to "IGNOREME"')
1107 parser.add_argument(
1108 'image_file', type=argparse.FileType('rb+'),
1109 help='Disk image file to change.')
1110
1111 def Execute(self, args):
1112 gpt = GPT.LoadFromFile(args.image_file)
1113 # cgpt behavior: if -p is specified, -e is ignored.
1114 if args.primary_ignore:
1115 if gpt.is_secondary:
1116 raise GPTError('Sorry, the disk already has primary GPT ignored.')
1117 args.image_file.seek(gpt.header.CurrentLBA * gpt.block_size)
1118 args.image_file.write(gpt.header.SIGNATURE_IGNORE)
1119 gpt.header = gpt.GetBackupHeader(self.header)
1120 gpt.is_secondary = True
1121 else:
1122 new_signature = gpt.Header.SIGNATURES[0 if args.efi else 1]
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001123 gpt.header.Update(Signature=new_signature)
Hung-Te Linc34d89c2018-04-17 15:11:34 +08001124 gpt.WriteToFile(args.image_file)
Yilin Yangf95c25a2019-12-23 15:38:51 +08001125 args.image_file.close()
Hung-Te Linc34d89c2018-04-17 15:11:34 +08001126 if args.primary_ignore:
Hung-Te Linbad46112018-05-15 16:39:14 +08001127 return ('Set %s primary GPT header to %s.' %
1128 (args.image_file.name, gpt.header.SIGNATURE_IGNORE))
Yilin Yang15a3f8f2020-01-03 17:49:00 +08001129 return ('Changed GPT signature for %s to %s.' %
1130 (args.image_file.name, new_signature))
Hung-Te Linc34d89c2018-04-17 15:11:34 +08001131
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001132 class Repair(SubCommand):
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001133 """Repair damaged GPT headers and tables."""
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001134
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001135 def DefineArgs(self, parser):
1136 parser.add_argument(
1137 'image_file', type=argparse.FileType('rb+'),
1138 help='Disk image file to repair.')
1139
1140 def Execute(self, args):
1141 gpt = GPT.LoadFromFile(args.image_file)
Hung-Te Lin446eb512018-05-02 18:39:16 +08001142 gpt.Resize(GPT.GetImageSize(args.image_file.name))
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001143 gpt.WriteToFile(args.image_file)
Yilin Yangf95c25a2019-12-23 15:38:51 +08001144 args.image_file.close()
Hung-Te Linbad46112018-05-15 16:39:14 +08001145 return 'Disk image file %s repaired.' % args.image_file.name
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001146
1147 class Expand(SubCommand):
1148 """Expands a GPT partition to all available free space."""
1149
1150 def DefineArgs(self, parser):
1151 parser.add_argument(
1152 '-i', '--number', type=int, required=True,
1153 help='The partition to expand.')
1154 parser.add_argument(
1155 'image_file', type=argparse.FileType('rb+'),
1156 help='Disk image file to modify.')
1157
1158 def Execute(self, args):
1159 gpt = GPT.LoadFromFile(args.image_file)
Hung-Te Lin5f0dea42018-04-18 23:20:11 +08001160 old_blocks, new_blocks = gpt.ExpandPartition(args.number)
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001161 gpt.WriteToFile(args.image_file)
Yilin Yangf95c25a2019-12-23 15:38:51 +08001162 args.image_file.close()
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001163 if old_blocks < new_blocks:
Hung-Te Linbad46112018-05-15 16:39:14 +08001164 return (
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001165 'Partition %s on disk image file %s has been extended '
1166 'from %s to %s .' %
1167 (args.number, args.image_file.name, old_blocks * gpt.block_size,
1168 new_blocks * gpt.block_size))
Yilin Yang15a3f8f2020-01-03 17:49:00 +08001169 return ('Nothing to expand for disk image %s partition %s.' %
1170 (args.image_file.name, args.number))
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001171
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001172 class Add(SubCommand):
1173 """Add, edit, or remove a partition entry.
1174
1175 Use the -i option to modify an existing partition.
1176 The -b, -s, and -t options must be given for new partitions.
1177
1178 The partition type may also be given as one of these aliases:
1179
1180 firmware ChromeOS firmware
1181 kernel ChromeOS kernel
1182 rootfs ChromeOS rootfs
1183 data Linux data
1184 reserved ChromeOS reserved
1185 efi EFI System Partition
1186 unused Unused (nonexistent) partition
1187 """
1188 def DefineArgs(self, parser):
1189 parser.add_argument(
1190 '-i', '--number', type=int,
1191 help='Specify partition (default is next available)')
1192 parser.add_argument(
1193 '-b', '--begin', type=int,
1194 help='Beginning sector')
1195 parser.add_argument(
1196 '-s', '--sectors', type=int,
1197 help='Size in sectors (logical blocks).')
1198 parser.add_argument(
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001199 '-t', '--type-guid', type=GPT.GetTypeGUID,
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001200 help='Partition Type GUID')
1201 parser.add_argument(
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001202 '-u', '--unique-guid', type=GUID,
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001203 help='Partition Unique ID')
1204 parser.add_argument(
1205 '-l', '--label',
1206 help='Label')
1207 parser.add_argument(
1208 '-S', '--successful', type=int, choices=xrange(2),
1209 help='set Successful flag')
1210 parser.add_argument(
1211 '-T', '--tries', type=int,
1212 help='set Tries flag (0-15)')
1213 parser.add_argument(
1214 '-P', '--priority', type=int,
1215 help='set Priority flag (0-15)')
1216 parser.add_argument(
1217 '-R', '--required', type=int, choices=xrange(2),
1218 help='set Required flag')
1219 parser.add_argument(
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001220 '-B', '--boot-legacy', dest='legacy_boot', type=int,
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001221 choices=xrange(2),
1222 help='set Legacy Boot flag')
1223 parser.add_argument(
1224 '-A', '--attribute', dest='raw_16', type=int,
1225 help='set raw 16-bit attribute value (bits 48-63)')
1226 parser.add_argument(
1227 'image_file', type=argparse.FileType('rb+'),
1228 help='Disk image file to modify.')
1229
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001230 def Execute(self, args):
1231 gpt = GPT.LoadFromFile(args.image_file)
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001232 number = args.number
1233 if number is None:
Hung-Te Linc5196682018-04-18 22:59:59 +08001234 number = next(p for p in gpt.partitions if p.IsUnused()).number
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001235
1236 # First and last LBA must be calculated explicitly because the given
1237 # argument is size.
Hung-Te Lin5f0dea42018-04-18 23:20:11 +08001238 part = gpt.GetPartition(number)
Hung-Te Linc5196682018-04-18 22:59:59 +08001239 is_new_part = part.IsUnused()
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001240
1241 if is_new_part:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001242 part.Zero()
1243 part.Update(
Hung-Te Linc5196682018-04-18 22:59:59 +08001244 FirstLBA=gpt.GetMaxUsedLBA() + 1,
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001245 LastLBA=gpt.header.LastUsableLBA,
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001246 UniqueGUID=GUID.Random(),
Hung-Te Linf641d302018-04-18 15:09:35 +08001247 TypeGUID=gpt.GetTypeGUID('data'))
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001248
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001249 def UpdateAttr(name):
1250 value = getattr(args, name)
1251 if value is None:
1252 return
1253 setattr(attrs, name, value)
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001254
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001255 def GetArg(arg_value, default_value):
1256 return default_value if arg_value is None else arg_value
1257
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001258 attrs = part.Attributes
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001259 for name in ['legacy_boot', 'required', 'priority', 'tries',
1260 'successful', 'raw_16']:
1261 UpdateAttr(name)
1262 first_lba = GetArg(args.begin, part.FirstLBA)
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001263 part.Update(
1264 Names=GetArg(args.label, part.Names),
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001265 FirstLBA=first_lba,
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001266 LastLBA=first_lba - 1 + GetArg(args.sectors, part.blocks),
1267 TypeGUID=GetArg(args.type_guid, part.TypeGUID),
1268 UniqueGUID=GetArg(args.unique_guid, part.UniqueGUID),
1269 Attributes=attrs)
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001270
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001271 # Wipe partition again if it should be empty.
1272 if part.IsUnused():
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001273 part.Zero()
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001274
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001275 gpt.WriteToFile(args.image_file)
Yilin Yangf95c25a2019-12-23 15:38:51 +08001276 args.image_file.close()
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001277 if part.IsUnused():
1278 # If we do ('%s' % part) there will be TypeError.
Hung-Te Linbad46112018-05-15 16:39:14 +08001279 return 'Deleted (zeroed) %s.' % (part,)
Yilin Yang15a3f8f2020-01-03 17:49:00 +08001280 return ('%s %s (%s+%s).' %
1281 ('Added' if is_new_part else 'Modified',
1282 part, part.FirstLBA, part.blocks))
Hung-Te Linfcd1a8d2018-04-17 15:15:01 +08001283
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001284 class Show(SubCommand):
1285 """Show partition table and entries.
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001286
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001287 Display the GPT table.
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001288 """
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001289
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001290 def DefineArgs(self, parser):
1291 parser.add_argument(
1292 '--numeric', '-n', action='store_true',
1293 help='Numeric output only.')
1294 parser.add_argument(
1295 '--quick', '-q', action='store_true',
1296 help='Quick output.')
1297 parser.add_argument(
1298 '-i', '--number', type=int,
1299 help='Show specified partition only, with format args.')
1300 for name, help_str in GPTCommands.FORMAT_ARGS:
1301 # TODO(hungte) Alert if multiple args were specified.
1302 parser.add_argument(
1303 '--%s' % name, '-%c' % name[0], action='store_true',
1304 help='[format] %s.' % help_str)
1305 parser.add_argument(
1306 'image_file', type=argparse.FileType('rb'),
1307 help='Disk image file to show.')
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001308
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001309 def Execute(self, args):
1310 """Show partition table and entries."""
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001311
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001312 def FormatTypeGUID(p):
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001313 guid = p.TypeGUID
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001314 if not args.numeric:
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001315 names = gpt.TYPE_GUID_MAP.get(guid)
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001316 if names:
1317 return names
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001318 return str(guid)
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001319
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001320 def IsBootableType(guid):
1321 if not guid:
1322 return False
1323 return guid in gpt.TYPE_GUID_LIST_BOOTABLE
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001324
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001325 def FormatAttribute(attrs, chromeos_kernel=False):
1326 if args.numeric:
1327 return '[%x]' % (attrs.raw >> 48)
1328 results = []
1329 if chromeos_kernel:
1330 results += [
1331 'priority=%d' % attrs.priority,
1332 'tries=%d' % attrs.tries,
1333 'successful=%d' % attrs.successful]
1334 if attrs.required:
1335 results += ['required=1']
1336 if attrs.legacy_boot:
1337 results += ['legacy_boot=1']
1338 return ' '.join(results)
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001339
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001340 def ApplyFormatArgs(p):
1341 if args.begin:
1342 return p.FirstLBA
1343 elif args.size:
1344 return p.blocks
1345 elif args.type:
1346 return FormatTypeGUID(p)
1347 elif args.unique:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001348 return p.UniqueGUID
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001349 elif args.label:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001350 return p.Names
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001351 elif args.Successful:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001352 return p.Attributes.successful
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001353 elif args.Priority:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001354 return p.Attributes.priority
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001355 elif args.Tries:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001356 return p.Attributes.tries
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001357 elif args.Legacy:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001358 return p.Attributes.legacy_boot
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001359 elif args.Attribute:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001360 return '[%x]' % (p.Attributes.raw >> 48)
Yilin Yang15a3f8f2020-01-03 17:49:00 +08001361 return None
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001362
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001363 def IsFormatArgsSpecified():
1364 return any(getattr(args, arg[0]) for arg in GPTCommands.FORMAT_ARGS)
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001365
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001366 gpt = GPT.LoadFromFile(args.image_file)
1367 logging.debug('%r', gpt.header)
1368 fmt = '%12s %11s %7s %s'
1369 fmt2 = '%32s %s: %s'
1370 header = ('start', 'size', 'part', 'contents')
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001371
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001372 if IsFormatArgsSpecified() and args.number is None:
1373 raise GPTError('Format arguments must be used with -i.')
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001374
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001375 if not (args.number is None or
1376 0 < args.number <= gpt.header.PartitionEntriesNumber):
1377 raise GPTError('Invalid partition number: %d' % args.number)
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001378
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001379 partitions = gpt.partitions
1380 do_print_gpt_blocks = False
1381 if not (args.quick or IsFormatArgsSpecified()):
1382 print(fmt % header)
1383 if args.number is None:
1384 do_print_gpt_blocks = True
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001385
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001386 if do_print_gpt_blocks:
Hung-Te Linc34d89c2018-04-17 15:11:34 +08001387 if gpt.pmbr:
1388 print(fmt % (0, 1, '', 'PMBR'))
1389 if gpt.is_secondary:
1390 print(fmt % (gpt.header.BackupLBA, 1, 'IGNORED', 'Pri GPT header'))
1391 else:
1392 print(fmt % (gpt.header.CurrentLBA, 1, '', 'Pri GPT header'))
1393 print(fmt % (gpt.header.PartitionEntriesStartingLBA,
1394 gpt.GetPartitionTableBlocks(), '', 'Pri GPT table'))
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001395
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001396 for p in partitions:
1397 if args.number is None:
1398 # Skip unused partitions.
1399 if p.IsUnused():
1400 continue
1401 elif p.number != args.number:
1402 continue
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001403
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001404 if IsFormatArgsSpecified():
1405 print(ApplyFormatArgs(p))
1406 continue
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001407
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001408 print(fmt % (p.FirstLBA, p.blocks, p.number,
1409 FormatTypeGUID(p) if args.quick else
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001410 'Label: "%s"' % p.Names))
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001411
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001412 if not args.quick:
1413 print(fmt2 % ('', 'Type', FormatTypeGUID(p)))
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001414 print(fmt2 % ('', 'UUID', p.UniqueGUID))
1415 if args.numeric or IsBootableType(p.TypeGUID):
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001416 print(fmt2 % ('', 'Attr', FormatAttribute(
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001417 p.Attributes, p.IsChromeOSKernel())))
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001418
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001419 if do_print_gpt_blocks:
Hung-Te Linc34d89c2018-04-17 15:11:34 +08001420 if gpt.is_secondary:
1421 header = gpt.header
1422 else:
1423 f = args.image_file
1424 f.seek(gpt.header.BackupLBA * gpt.block_size)
1425 header = gpt.Header.ReadFrom(f)
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001426 print(fmt % (header.PartitionEntriesStartingLBA,
1427 gpt.GetPartitionTableBlocks(header), '',
1428 'Sec GPT table'))
1429 print(fmt % (header.CurrentLBA, 1, '', 'Sec GPT header'))
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001430
Hung-Te Lin3b491672018-04-19 01:41:20 +08001431 # Check integrity after showing all fields.
1432 gpt.CheckIntegrity()
1433
Hung-Te Linfe724f82018-04-18 15:03:58 +08001434 class Prioritize(SubCommand):
1435 """Reorder the priority of all kernel partitions.
1436
1437 Reorder the priority of all active ChromeOS Kernel partitions.
1438
1439 With no options this will set the lowest active kernel to priority 1 while
1440 maintaining the original order.
1441 """
1442
1443 def DefineArgs(self, parser):
1444 parser.add_argument(
1445 '-P', '--priority', type=int,
1446 help=('Highest priority to use in the new ordering. '
1447 'The other partitions will be ranked in decreasing '
1448 'priority while preserving their original order. '
1449 'If necessary the lowest ranks will be coalesced. '
1450 'No active kernels will be lowered to priority 0.'))
1451 parser.add_argument(
1452 '-i', '--number', type=int,
1453 help='Specify the partition to make the highest in the new order.')
1454 parser.add_argument(
1455 '-f', '--friends', action='store_true',
1456 help=('Friends of the given partition (those with the same '
1457 'starting priority) are also updated to the new '
1458 'highest priority. '))
1459 parser.add_argument(
1460 'image_file', type=argparse.FileType('rb+'),
1461 help='Disk image file to prioritize.')
1462
1463 def Execute(self, args):
1464 gpt = GPT.LoadFromFile(args.image_file)
1465 parts = [p for p in gpt.partitions if p.IsChromeOSKernel()]
Hung-Te Lin138389f2018-05-15 17:55:00 +08001466 parts.sort(key=lambda p: p.Attributes.priority, reverse=True)
1467 groups = dict((k, list(g)) for k, g in itertools.groupby(
1468 parts, lambda p: p.Attributes.priority))
Hung-Te Linfe724f82018-04-18 15:03:58 +08001469 if args.number:
Hung-Te Lin5f0dea42018-04-18 23:20:11 +08001470 p = gpt.GetPartition(args.number)
Hung-Te Linfe724f82018-04-18 15:03:58 +08001471 if p not in parts:
1472 raise GPTError('%s is not a ChromeOS kernel.' % p)
Hung-Te Lin138389f2018-05-15 17:55:00 +08001473 pri = p.Attributes.priority
1474 friends = groups.pop(pri)
1475 new_pri = max(groups) + 1
Hung-Te Linfe724f82018-04-18 15:03:58 +08001476 if args.friends:
Hung-Te Lin138389f2018-05-15 17:55:00 +08001477 groups[new_pri] = friends
Hung-Te Linfe724f82018-04-18 15:03:58 +08001478 else:
Hung-Te Lin138389f2018-05-15 17:55:00 +08001479 groups[new_pri] = [p]
1480 friends.remove(p)
1481 if friends:
1482 groups[pri] = friends
1483
1484 if 0 in groups:
1485 # Do not change any partitions with priority=0
1486 groups.pop(0)
1487
Yilin Yang78fa12e2019-09-25 14:21:10 +08001488 prios = list(groups)
Hung-Te Lin138389f2018-05-15 17:55:00 +08001489 prios.sort(reverse=True)
Hung-Te Linfe724f82018-04-18 15:03:58 +08001490
1491 # Max priority is 0xf.
1492 highest = min(args.priority or len(prios), 0xf)
1493 logging.info('New highest priority: %s', highest)
Hung-Te Linfe724f82018-04-18 15:03:58 +08001494
Hung-Te Lin138389f2018-05-15 17:55:00 +08001495 for i, pri in enumerate(prios):
1496 new_priority = max(1, highest - i)
1497 for p in groups[pri]:
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001498 attrs = p.Attributes
Hung-Te Linfe724f82018-04-18 15:03:58 +08001499 old_priority = attrs.priority
Hung-Te Lin138389f2018-05-15 17:55:00 +08001500 if old_priority == new_priority:
1501 continue
Hung-Te Linfe724f82018-04-18 15:03:58 +08001502 attrs.priority = new_priority
Hung-Te Lin138389f2018-05-15 17:55:00 +08001503 if attrs.tries < 1 and not attrs.successful:
1504 attrs.tries = 15 # Max tries for new active partition.
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001505 p.Update(Attributes=attrs)
Hung-Te Linfe724f82018-04-18 15:03:58 +08001506 logging.info('%s priority changed from %s to %s.', p, old_priority,
1507 new_priority)
Hung-Te Linfe724f82018-04-18 15:03:58 +08001508
1509 gpt.WriteToFile(args.image_file)
Yilin Yangf95c25a2019-12-23 15:38:51 +08001510 args.image_file.close()
Hung-Te Linfe724f82018-04-18 15:03:58 +08001511
Hung-Te Linf641d302018-04-18 15:09:35 +08001512 class Find(SubCommand):
1513 """Locate a partition by its GUID.
1514
1515 Find a partition by its UUID or label. With no specified DRIVE it scans all
1516 physical drives.
1517
1518 The partition type may also be given as one of these aliases:
1519
1520 firmware ChromeOS firmware
1521 kernel ChromeOS kernel
1522 rootfs ChromeOS rootfs
1523 data Linux data
1524 reserved ChromeOS reserved
1525 efi EFI System Partition
1526 unused Unused (nonexistent) partition
1527 """
1528 def DefineArgs(self, parser):
1529 parser.add_argument(
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001530 '-t', '--type-guid', type=GPT.GetTypeGUID,
Hung-Te Linf641d302018-04-18 15:09:35 +08001531 help='Search for Partition Type GUID')
1532 parser.add_argument(
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001533 '-u', '--unique-guid', type=GUID,
Hung-Te Linf641d302018-04-18 15:09:35 +08001534 help='Search for Partition Unique GUID')
1535 parser.add_argument(
1536 '-l', '--label',
1537 help='Search for Label')
1538 parser.add_argument(
1539 '-n', '--numeric', action='store_true',
1540 help='Numeric output only.')
1541 parser.add_argument(
1542 '-1', '--single-match', action='store_true',
1543 help='Fail if more than one match is found.')
1544 parser.add_argument(
1545 '-M', '--match-file', type=str,
1546 help='Matching partition data must also contain MATCH_FILE content.')
1547 parser.add_argument(
1548 '-O', '--offset', type=int, default=0,
1549 help='Byte offset into partition to match content (default 0).')
1550 parser.add_argument(
1551 'drive', type=argparse.FileType('rb+'), nargs='?',
1552 help='Drive or disk image file to find.')
1553
1554 def Execute(self, args):
1555 if not any((args.type_guid, args.unique_guid, args.label)):
1556 raise GPTError('You must specify at least one of -t, -u, or -l')
1557
1558 drives = [args.drive.name] if args.drive else (
1559 '/dev/%s' % name for name in subprocess.check_output(
1560 'lsblk -d -n -r -o name', shell=True).split())
1561
1562 match_pattern = None
1563 if args.match_file:
1564 with open(args.match_file) as f:
1565 match_pattern = f.read()
1566
1567 found = 0
1568 for drive in drives:
1569 try:
1570 gpt = GPT.LoadFromFile(drive)
1571 except GPTError:
1572 if args.drive:
1573 raise
1574 # When scanning all block devices on system, ignore failure.
1575
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001576 def Unmatch(a, b):
1577 return a is not None and a != b
1578
Hung-Te Linf641d302018-04-18 15:09:35 +08001579 for p in gpt.partitions:
Hung-Te Linbf8aa272018-04-19 03:02:29 +08001580 if (p.IsUnused() or
Hung-Te Lin86ca4bb2018-04-25 10:22:10 +08001581 Unmatch(args.label, p.Names) or
1582 Unmatch(args.unique_guid, p.UniqueGUID) or
1583 Unmatch(args.type_guid, p.TypeGUID)):
Hung-Te Linf641d302018-04-18 15:09:35 +08001584 continue
1585 if match_pattern:
1586 with open(drive, 'rb') as f:
1587 f.seek(p.offset + args.offset)
1588 if f.read(len(match_pattern)) != match_pattern:
1589 continue
1590 # Found the partition, now print.
1591 found += 1
1592 if args.numeric:
1593 print(p.number)
1594 else:
1595 # This is actually more for block devices.
1596 print('%s%s%s' % (p.image, 'p' if p.image[-1].isdigit() else '',
1597 p.number))
1598
1599 if found < 1 or (args.single_match and found > 1):
1600 return 1
1601 return 0
1602
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001603
1604def main():
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001605 commands = GPTCommands()
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001606 parser = argparse.ArgumentParser(description='GPT Utility.')
1607 parser.add_argument('--verbose', '-v', action='count', default=0,
1608 help='increase verbosity.')
1609 parser.add_argument('--debug', '-d', action='store_true',
1610 help='enable debug output.')
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001611 commands.DefineArgs(parser)
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001612
1613 args = parser.parse_args()
1614 log_level = max(logging.WARNING - args.verbose * 10, logging.DEBUG)
1615 if args.debug:
1616 log_level = logging.DEBUG
1617 logging.basicConfig(format='%(module)s:%(funcName)s %(message)s',
1618 level=log_level)
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001619 try:
Hung-Te Linf641d302018-04-18 15:09:35 +08001620 code = commands.Execute(args)
Peter Shih533566a2018-09-05 17:48:03 +08001621 if isinstance(code, int):
Hung-Te Linf641d302018-04-18 15:09:35 +08001622 sys.exit(code)
Hung-Te Linbad46112018-05-15 16:39:14 +08001623 elif isinstance(code, basestring):
1624 print('OK: %s' % code)
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001625 except Exception as e:
1626 if args.verbose or args.debug:
1627 logging.exception('Failure in command [%s]', args.command)
Hung-Te Lin5cb0c312018-04-17 14:56:43 +08001628 exit('ERROR: %s: %s' % (args.command, str(e) or 'Unknown error.'))
Hung-Te Linc772e1a2017-04-14 16:50:50 +08001629
1630
1631if __name__ == '__main__':
1632 main()