/*
libparted - a library for manipulating disk partitions
original version by Matt Domsch <Matt_Domsch@dell.com>
Disclaimed into the Public Domain
Portions Copyright (C) 2001-2003, 2005-2010 Free Software Foundation, Inc.
EFI GUID Partition Table handling
Per Intel EFI Specification v1.02
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <inttypes.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include <errno.h>
#include "xalloc.h"
#include "pt-tools.h"
#if ENABLE_NLS
# include <libintl.h>
#else
#endif /* ENABLE_NLS */
/* NOTE: the document that describes revision 1.00 is labelled "version 1.02",
* so some implementors got confused...
*/
#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif
#ifdef __sun
#endif /* __sun */
typedef struct
{
/* commented out "__attribute__ ((packed))" to work around gcc bug (fixed
* in gcc3.1): __attribute__ ((packed)) breaks addressing on initialized
* data. It turns out we don't need it in this case, so it doesn't break
* anything :)
*/
#define UNUSED_ENTRY_GUID \
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
#define PARTITION_SYSTEM_GUID \
{ 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B }})
#define PARTITION_BIOS_GRUB_GUID \
{ 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 }})
#define LEGACY_MBR_PARTITION_GUID \
{ 0x00, 0x08, 0xC7, 0x81, 0xF3, 0x9F }})
#define PARTITION_MSFT_RESERVED_GUID \
{ 0xF9, 0x2D, 0xF0, 0x02, 0x15, 0xAE }})
#define PARTITION_MSFT_RECOVERY \
{ 0xBF, 0xD5, 0x01, 0x79, 0xD6, 0xAC }})
#define PARTITION_BASIC_DATA_GUID \
{ 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7 }})
#define PARTITION_RAID_GUID \
{ 0x74, 0x3f, 0x0f, 0x84, 0x91, 0x1e }})
#define PARTITION_SWAP_GUID \
{ 0x09, 0x33, 0xc8, 0x4b, 0x4f, 0x4f }})
#define PARTITION_LVM_GUID \
{ 0x23, 0x8f, 0x2a, 0x3d, 0xf9, 0x28 }})
#define PARTITION_RESERVED_GUID \
{ 0x08, 0x3a, 0xc8, 0x23, 0x09, 0x08 }})
#define PARTITION_HPSERVICE_GUID \
{ 0x7b, 0x03, 0xa0, 0x00, 0x00, 0x00 }})
#define PARTITION_APPLE_HFS_GUID \
{ 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC }})
#define PARTITION_APPLE_TV_RECOVERY_GUID \
{ 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC }})
#ifdef __sun
#pragma pack(1)
#endif
{
};
{
#else
# warning "Using crippled partition entry type"
#endif
};
{
};
#ifdef __sun
#pragma pack()
#endif
#define GPT_PMBR_LBA 0
/*
These values are only defaults. The actual on-disk structures
may define different sizes, so use those unless creating a new GPT disk!
*/
/* Number of actual partition entries should be calculated as: */
#define GPT_DEFAULT_PARTITION_ENTRIES \
sizeof(GuidPartitionEntry_t))
#ifdef __sun
#pragma pack(1)
#endif
{
/* Not used by EFI firmware. Set to 0x80 to indicate that this
is the bootable legacy partition. */
/* Start of partition in CHS address, not used by EFI firmware. */
/* Start of partition in CHS address, not used by EFI firmware. */
/* Start of partition in CHS address, not used by EFI firmware. */
/* OS type. A value of 0xEF defines an EFI system partition.
Other values are reserved for legacy operating systems, and
allocated independently of the EFI specification. */
/* End of partition in CHS address, not used by EFI firmware. */
/* End of partition in CHS address, not used by EFI firmware. */
/* End of partition in CHS address, not used by EFI firmware. */
/* Starting LBA address of the partition on the disk. Used by
EFI firmware to define the start of the partition. */
/* Size of partition in LBA. Used by EFI firmware to determine
the size of the partition. */
};
/* Protected Master Boot Record & Legacy MBR share same structure */
/* Needs to be packed because the u16s force misalignment. */
{
};
/* uses libparted's disk_specific field in PedDisk, to store our info */
{
int entry_count;
};
#ifdef __sun
#pragma pack()
#endif
/* uses libparted's disk_specific field in PedPartition, to store our info */
typedef struct _GPTPartitionData
{
int lvm;
int raid;
int boot;
int bios_grub;
int hp_service;
int hidden;
int msftres;
int atvrecv;
int msftrecv;
static inline uint32_t
{
}
static inline uint32_t
{
return sizeof (GuidPartitionTableHeader_t) - sizeof (uint8_t *);
}
static inline uint32_t
{
}
static GuidPartitionTableHeader_t *
{
return pth;
}
static GuidPartitionTableHeader_t *
{
return (pth);
}
static GuidPartitionTableHeader_t *
{
pth_get_size_rsv2 (dev));
return pth;
}
static void
{
return;
}
static uint8_t *
{
return NULL;
return pth_raw;
}
/**
* swap_uuid_and_efi_guid() - converts between uuid formats
* @uuid - uuid_t in either format (converts it to the other)
*
* There are two different representations for Globally Unique Identifiers
* (GUIDs or UUIDs).
*
* The RFC specifies a UUID as a string of 16 bytes, essentially
* a big-endian array of char.
* Intel, in their EFI Specification, references the same RFC, but
* then defines a GUID as a structure of little-endian fields.
* Coincidentally, both structures have the same format when unparsed.
*
* When read from disk, EFI GUIDs are in struct of little endian format,
* and need to be converted to be treated as uuid_t in memory.
*
* When writing to disk, uuid_ts need to be converted into EFI GUIDs.
*
* Blame Intel.
*/
static void
{
}
/* returns the EFI-style CRC32 value for buf
* This function uses the crc32 function by Gary S. Brown,
* but seeds the function with ~0, and xor's with ~0 at the end.
*/
static inline uint32_t
{
}
/* Compute the crc32 checksum of the partition table header
and store it in *CRC32. Return 0 upon success. Return 1
upon failure to allocate space. */
static int
{
return 1;
return 0;
}
static inline int
{
}
/* checks if 'mbr' is a protective MBR partition table */
static inline int
{
int i;
return 0;
for (i = 0; i < 4; i++)
{
return 1;
}
return 0;
}
static int
{
int gpt_sig_found = 0;
return 0;
{
gpt_sig_found = 1;
}
if (!gpt_sig_found)
return 0;
void *label;
return 0;
{
_("%s contains GPT signatures, indicating that it has "
"a GPT table. However, it does not have a valid "
"fake msdos partition table, as it should. Perhaps "
"it was corrupted -- possibly by a program that "
"doesn't understand GPT partition tables. Or "
"perhaps you deleted the GPT table, and are now "
"using an msdos partition table. Is this a GPT "
"partition table?"),
if (ex_status == PED_EXCEPTION_NO)
ok = 0;
}
return ok;
}
static PedDisk *
{
if (!disk)
goto error;
if (!disk->disk_specific)
goto error_free_disk;
return disk;
return NULL;
}
static PedDisk *
{
if (!new_disk)
return NULL;
return new_disk;
}
static void
{
}
/* Given GUID Partition table header, GPT, read its partition array
entries from DISK into malloc'd storage. Set *PTES_BYTES to the
number of bytes required. Upon success, return a pointer to the
resulting buffer. Otherwise, set errno and return NULL. */
static void *
{
{
return NULL;
}
return NULL;
{
errno = saved_errno;
return NULL;
}
return ptes;
}
static int
{
return 1;
return 0;
}
static int
{
return 0;
/*
* "While the GUID Partition Table Header's size may increase
* in the future it cannot span more than one block on the
* device." EFI Specification, version 1.10, 11.2.2.1
*/
return 0;
/* The SizeOfPartitionEntry must be a multiple of 8 and
no smaller than the size of the PartitionEntry structure.
We also require that be no larger than 1/16th of UINT32_MAX,
as an additional sanity check. */
if (sope % 8 != 0
return 0;
return 0;
/* The backup table's AlternateLBA must be 1. */
return 0;
/* The alt_lba must never be the same as my_lba. */
return 0;
bool crc_match;
return 0;
gpt->HeaderCRC32 = 0;
return 0;
}
static int
int *update_needed)
{
static int asked_already;
#ifndef DISCOVER_ONLY
{
_("The format of the GPT partition table is version "
"%x, which is newer than what Parted can "
"recognise. Please tell us! bug-parted@gnu.org"),
return 0;
}
#endif
/* Need to check whether the volume has grown, the LastUsableLBA is
normally set to disk->dev->length - 2 - ptes_size (at least for parted
created volumes), where ptes_size is the number of entries *
size of each entry / sector size or 16k / sector size, whatever the greater.
If the volume has grown, offer the user the chance to use the new
space or continue with the current usable area. Only ask once per
parted invocation. */
{
}
{
_("Not all of the space available to %s appears "
"to be used, you can fix the GPT to use all of the "
"space (an extra %llu blocks) or continue with the "
if (q == PED_EXCEPTION_FIX)
{
*update_needed = 1;
}
else if (q != PED_EXCEPTION_UNHANDLED)
{
asked_already = 1;
}
}
return 1;
}
static PedPartition *
{
unsigned int i;
if (!part)
return NULL;
for (i = 0; i < 72 / sizeof (efi_char16_t); i++)
gpt_part_data->name[i] =
gpt_part_data->name[i] = 0;
return part;
}
/* Read the primary GPT at sector 1 of DEV.
Verify its CRC and that of its partition entry array.
If they are valid, read the backup GPT specified by AlternateLBA.
If not, read the backup GPT in the last sector of the disk.
Return 1 if any read fails.
Upon successful verification of the primary GPT, set *PRIMARY_GPT, else NULL.
Upon successful verification of the backup GPT, set *BACKUP_GPT, else NULL.
If we've set *BACKUP_GPT to non-NULL, set *BACKUP_LBA to the sector
number in which it was found. */
static int
{
*primary_gpt = NULL;
*backup_gpt = NULL;
void *s1;
return 1;
if (t == NULL)
return 1;
if (valid_primary)
*primary_gpt = pri;
else
void *s_bak;
return 1;
if (t == NULL)
return 1;
{
*backup_gpt = bak;
}
else
return 0;
}
/************************************************************
* Intel is changing the EFI Spec. (after v1.02) to say that a
* disk is considered to have a GPT label only if the GPT
* structures are correct, and the MBR is actually a Protective
* MBR (has one 0xEE type partition).
* Problem occurs when a GPT-partitioned disk is then
* edited with a legacy (non-GPT-aware) application, such as
* fdisk (which doesn't generally erase the PGPT or AGPT).
* How should such a disk get handled? As a GPT disk (throwing
* away the fdisk changes), or as an MSDOS disk (throwing away
* the GPT information). Previously, I've taken the GPT-is-right,
* MBR is wrong, approach, to stay consistent with the EFI Spec.
* Intel disagrees, saying the disk should then be treated
* as having a msdos label, not a GPT label. If this is true,
* then what's the point of having an AGPT, since if the PGPT
* is screwed up, likely the PMBR is too, and the PMBR becomes
* a single point of failure.
* So, in the Linux kernel, I'm going to test for PMBR, and
* warn if it's not there, and treat the disk as MSDOS, with a note
* for users to use Parted to "fix up" their disk if they
* really want it to be considered GPT.
************************************************************/
static int
{
int i;
#ifndef DISCOVER_ONLY
int write_back = 0;
#endif
/* motivation: let the user decide about the pmbr... during
ped_disk_probe(), they probably didn't get a choice... */
goto error;
if (read_failure)
{
/* This includes the case in which there used to be a GPT partition
table here, with an alternate LBA that extended beyond the current
end-of-device. It's treated as a non-match. */
/* Another possibility:
The primary header is ok, but backup is corrupt.
In the UEFI spec, this means the primary GUID table
is officially invalid. */
return 0;
}
if (primary_gpt && backup_gpt)
{
/* Both are valid. */
#ifndef DISCOVER_ONLY
{
switch (ped_exception_throw
_("The backup GPT table is not at the end of the disk, as it "
"should be. This might mean that another operating system "
"believes the disk is smaller. Fix, by moving the backup "
"to the end (and removing the old backup)?")))
{
case PED_EXCEPTION_CANCEL:
goto error_free_gpt;
case PED_EXCEPTION_FIX:
write_back = 1;
break;
default:
break;
}
}
#endif /* !DISCOVER_ONLY */
gpt = primary_gpt;
}
else if (!primary_gpt && !backup_gpt)
{
/* Both are corrupt. */
_("Both the primary and backup GPT tables "
"are corrupt. Try making a fresh table, "
"and using Parted's rescue feature to "
"recover partitions."));
goto error;
}
else if (primary_gpt && !backup_gpt)
{
/* The primary header is ok, but backup is corrupt. */
_("The backup GPT table is corrupt, but the "
"primary appears OK, so that will be used."))
goto error_free_gpt;
gpt = primary_gpt;
}
else /* !primary_gpt && backup_gpt */
{
/* primary GPT corrupt, backup is ok. */
_("The primary GPT table is corrupt, but the "
"backup appears OK, so that will be used."))
goto error_free_gpt;
gpt = backup_gpt;
}
backup_gpt = NULL;
primary_gpt = NULL;
goto error_free_gpt;
goto error_free_gpt;
{
_("primary partition table array CRC mismatch"));
goto error_free_ptes;
}
for (i = 0; i < gpt_disk_data->entry_count; i++)
{
continue;
if (!part)
goto error_delete_all;
{
goto error_delete_all;
}
}
#ifndef DISCOVER_ONLY
if (write_back)
#endif
return 1;
return 0;
}
#ifndef DISCOVER_ONLY
/* Write the protective MBR (to keep DOS happy) */
static int
{
/* The UEFI spec is not clear about what to do with the following
elements of the Protective MBR (pmbr): BootCode (0-440B),
UniqueMBRSignature (440B-444B) and Unknown (444B-446B).
With this in mind, we try not to modify these elements. */
void *s0;
return 0;
/* Zero out the legacy partitions. */
else
return write_ok;
}
static int
{
/* per 1.00 spec */
gpt->HeaderCRC32 = 0;
if (alternate)
{
}
else
{
}
return 1;
return 0;
}
static void
{
unsigned int i;
if (gpt_part_data->hidden)
for (i = 0; i < 72 / sizeof (efi_char16_t); i++)
pte->PartitionName[i]
}
static int
{
int ptes_size;
/*
* Solaris 11 has a bug. It creates only 9 partition table entries and
* the CRC covers only them. But it allocates blocks for and writes out
* 128 PTEs, as is required by the spec.
*/
if (!ptes)
goto error;
{
continue;
}
/* Write protective MBR */
goto error_free_ptes;
/* Write PTH and PTEs */
/* FIXME: Caution: this code is nearly identical to what's just below. */
goto error_free_ptes;
goto error_free_ptes;
if (!write_ok)
goto error_free_ptes;
goto error_free_ptes;
/* Write Alternate PTH & PTEs */
/* FIXME: Caution: this code is nearly identical to what's just above. */
goto error_free_ptes;
goto error_free_ptes;
if (!write_ok)
goto error_free_ptes;
goto error_free_ptes;
return 0;
}
#endif /* !DISCOVER_ONLY */
static int
{
if (!part)
goto error;
goto error_destroy_constraint;
return 1;
return 0;
}
static PedPartition *
{
if (!part)
goto error;
if (part_type != 0)
return part;
ped_malloc (sizeof (GPTPartitionData));
if (!gpt_part_data)
goto error_free_part;
gpt_part_data->lvm = 0;
gpt_part_data->raid = 0;
gpt_part_data->boot = 0;
gpt_part_data->bios_grub = 0;
gpt_part_data->hp_service = 0;
gpt_part_data->hidden = 0;
gpt_part_data->msftres = 0;
gpt_part_data->msftrecv = 0;
gpt_part_data->atvrecv = 0;
return part;
return NULL;
}
static PedPartition *
{
if (!result)
goto error;
return result;
ped_malloc (sizeof (GPTPartitionData));
if (!result_data)
goto error_free_part;
return result;
return NULL;
}
static void
{
{
}
}
static int
const PedFileSystemType *fs_type)
{
if (gpt_part_data->lvm)
{
return 1;
}
if (gpt_part_data->raid)
{
return 1;
}
if (gpt_part_data->boot)
{
return 1;
}
if (gpt_part_data->bios_grub)
{
return 1;
}
if (gpt_part_data->hp_service)
{
return 1;
}
if (gpt_part_data->msftres)
{
return 1;
}
if (gpt_part_data->msftrecv)
{
return 1;
}
if (gpt_part_data->atvrecv)
{
return 1;
}
if (fs_type)
{
{
return 1;
}
{
return 1;
}
{
return 1;
}
}
return 1;
}
/* Allocate metadata partitions for the GPTH and PTES */
static int
{
* sizeof (GuidPartitionEntry_t),
/* metadata at the start of the disk includes the MBR */
return 0;
/* metadata at the end of the disk */
gptlength + pteslength))
return 0;
return 1;
}
static int
{
int i;
/* never change the partition numbers */
return 1;
{
{
return 1;
}
}
PED_ASSERT (0, return 0);
return 0; /* used if debug is disabled */
}
static int
{
switch (flag)
{
case PED_PARTITION_BOOT:
if (state)
= gpt_part_data->lvm
= gpt_part_data->atvrecv = 0;
case PED_PARTITION_BIOS_GRUB:
if (state)
= gpt_part_data->lvm
= gpt_part_data->atvrecv = 0;
case PED_PARTITION_RAID:
if (state)
= gpt_part_data->lvm
= gpt_part_data->atvrecv = 0;
case PED_PARTITION_LVM:
if (state)
= gpt_part_data->atvrecv = 0;
case PED_PARTITION_HPSERVICE:
if (state)
= gpt_part_data->lvm
= gpt_part_data->atvrecv = 0;
if (state)
= gpt_part_data->lvm
= gpt_part_data->atvrecv = 0;
case PED_PARTITION_DIAG:
if (state)
= gpt_part_data->lvm
= gpt_part_data->atvrecv = 0;
if (state)
= gpt_part_data->lvm
= gpt_part_data->msftrecv = 0;
case PED_PARTITION_HIDDEN:
return 1;
case PED_PARTITION_SWAP:
case PED_PARTITION_ROOT:
case PED_PARTITION_LBA:
default:
return 0;
}
return 1;
}
static int
{
switch (flag)
{
case PED_PARTITION_RAID:
return gpt_part_data->raid;
case PED_PARTITION_LVM:
return gpt_part_data->lvm;
case PED_PARTITION_BOOT:
return gpt_part_data->boot;
case PED_PARTITION_BIOS_GRUB:
return gpt_part_data->bios_grub;
case PED_PARTITION_HPSERVICE:
return gpt_part_data->hp_service;
return gpt_part_data->msftres;
case PED_PARTITION_DIAG:
return gpt_part_data->msftrecv;
return gpt_part_data->atvrecv;
case PED_PARTITION_HIDDEN:
return gpt_part_data->hidden;
case PED_PARTITION_SWAP:
case PED_PARTITION_LBA:
case PED_PARTITION_ROOT:
default:
return 0;
}
return 0;
}
static int
{
switch (flag)
{
case PED_PARTITION_RAID:
case PED_PARTITION_LVM:
case PED_PARTITION_BOOT:
case PED_PARTITION_BIOS_GRUB:
case PED_PARTITION_HPSERVICE:
case PED_PARTITION_DIAG:
case PED_PARTITION_HIDDEN:
return 1;
case PED_PARTITION_SWAP:
case PED_PARTITION_ROOT:
case PED_PARTITION_LBA:
default:
return 0;
}
return 0;
}
static void
{
}
static const char *
{
return gpt_part_data->name;
}
static int
{
return gpt_disk_data->entry_count;
}
/*
* From (http://developer.apple.com/technotes/tn2006/tn2166.html Chapter 5).
* According to the specs the first LBA (LBA0) is not relevant (it exists
* to maintain compatibility). on the second LBA(LBA1) gpt places the
* header. The header is as big as the block size. After the header we
* find the Entry array. Each element of said array, describes each
* partition. One can have as much elements as can fit between the end of
* the second LBA (where the header ends) and the FirstUsableLBA.
* FirstUsableLBA is the first logical block that is used for contents
* and is defined in header.
*
* /---------------------------------------------------\
* | BLOCK0 | HEADER | Entry Array | First Usable LBA |
* | | BLOCK1 | | |
* \---------------------------------------------------/
* / \
* /----------/ \----------\
* /-----------------------------------------\
* | E1 | E2 | E3 |...............| EN |
* \-----------------------------------------/
*
* The number of possible partitions or supported partitions is:
* SP = FirstUsableLBA*Blocksize - 2*Blocksize / SizeOfPartitionEntry
* SP = Blocksize(FirstusableLBA - 2) / SizeOfPartitoinEntry
*/
static bool
{
return false;
return true;
}
static PedConstraint *
{
}
static int
{
return 1;
#ifndef DISCOVER_ONLY
_("Unable to satisfy all constraints on the partition."));
#endif
return 0;
}
#include "pt-common.h"
{
};
{
.name = "gpt",
.ops = &gpt_disk_ops,
};
void
{
}
void
{
}