gpt.c revision 7e7bd3dccbfe8f79e25e5c1554b5bc3a9aaca321
/*
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, 2002, 2003, 2005, 2006, 2007
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>
#if ENABLE_NLS
# include <libintl.h>
#else
#endif /* ENABLE_NLS */
#define EFI_PMBR_OSTYPE_EFI 0xEE
#define MSDOS_MBR_SIGNATURE 0xaa55
#define GPT_HEADER_SIGNATURE 0x5452415020494645LL
/* NOTE: the document that describes revision 1.00 is labelled "version 1.02",
* so some implementors got confused...
*/
#define GPT_HEADER_REVISION_V1_02 0x00010200
#define GPT_HEADER_REVISION_V1_00 0x00010000
#define GPT_HEADER_REVISION_V0_99 0x00009900
#ifdef __sun
#define __attribute__(X) /*nothing*/
#endif /* __sun */
typedef struct _GuidPartitionTableHeader_t GuidPartitionTableHeader_t;
typedef struct _GuidPartitionEntryAttributes_t GuidPartitionEntryAttributes_t;
typedef struct _GuidPartitionEntry_t GuidPartitionEntry_t;
typedef struct _PartitionRecord_t PartitionRecord_t;
typedef struct _LegacyMBR_t LegacyMBR_t;
typedef struct _GPTDiskData GPTDiskData;
typedef struct {
} /* __attribute__ ((packed)) */ efi_guid_t;
/* 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 LEGACY_MBR_PARTITION_GUID \
{ 0x00, 0x08, 0xC7, 0x81, 0xF3, 0x9F }})
#define PARTITION_MSFT_RESERVED_GUID \
{ 0xF9, 0x2D, 0xF0, 0x02, 0x15, 0xAE }})
#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 }})
#ifdef __sun
#pragma pack(1)
#endif
};
#else
#endif
};
};
#ifdef __sun
#pragma pack()
#endif
#define GPT_PMBR_LBA 0
#define GPT_PMBR_SECTORS 1
#define GPT_PRIMARY_HEADER_LBA 1
#define GPT_HEADER_SECTORS 1
#define GPT_PRIMARY_PART_TABLE_LBA 2
/*
These values are only defaults. The actual on-disk structures
may define different sizes, so use those unless creating a new GPT disk!
*/
#define GPT_DEFAULT_PARTITION_ENTRY_ARRAY_SIZE 16384
/* 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 {
char name[37];
int lvm;
int raid;
int boot;
int hp_service;
int hidden;
int msftres;
static PedDiskType gpt_disk_type;
static inline uint32_t
{
}
static inline uint32_t
{
return sizeof (GuidPartitionTableHeader_t) - sizeof (uint8_t*);
}
static inline uint32_t
{
}
static GuidPartitionTableHeader_t*
{
sizeof (GuidPartitionTableHeader_t)
+ sizeof (uint8_t));
return pth;
}
static GuidPartitionTableHeader_t*
{
return (pth);
}
static GuidPartitionTableHeader_t*
{
pth_get_size_rsv2 (dev));
return pth;
}
static void
{
}
static uint8_t*
{
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
{
}
static inline uint32_t
{
return crc32;
}
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;
gpt_sig_found = 1;
}
if (gpt)
if (!gpt_sig_found)
return 0;
if (!_pmbr_is_valid (&legacy_mbr)) {
int ex_status = ped_exception_throw (
_("%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)
return 0;
}
}
return 1;
}
#ifndef DISCOVER_ONLY
/* writes zeros to the PMBR and the primary and alternate GPTHs and PTEs */
static int
{
/*
* TO DISCUSS: check whether checksum is correct?
* If not, we might get a wrong AlternateLBA field and destroy
* one sector of random data.
*/
goto error_free;
goto error_free_with_gpt;
goto error_free_with_gpt;
goto error_free_with_gpt;
return 0;
}
return 1;
return 0;
}
#endif /* !DISCOVER_ONLY */
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
{
}
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;
gpt->HeaderCRC32 = 0;
}
static int
{
return 0;
}
return 1;
return 0;
}
static int
int *update_needed)
{
static int asked_already;
#ifndef DISCOVER_ONLY
if (ped_exception_throw (
_("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.
*/
if ( last_usable_if_grown > last_usable_min_default ) {
}
_("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] = 0;
return part;
}
/************************************************************
* 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 ptes_size;
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;
#ifndef DISCOVER_ONLY
if (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)?"))
goto error_free_gpt;
write_back = 1;
1);
#endif /* !DISCOVER_ONLY */
}
} else { /* primary GPT *not* ok */
int alternate_ok = 0;
#ifndef DISCOVER_ONLY
write_back = 1;
#endif
}
if (!alternate_ok) {
}
if (alternate_ok) {
if (ped_exception_throw (
_("The primary GPT table is corrupt, but the "
"backup appears OK, so that will be used."))
goto error_free_gpt;
} else {
_("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;
}
}
goto error_free_gpt;
/*
* ptes_size is in bytes and must be a multiple of sector_size.
*/
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
/* Writes the protective MBR (to keep DOS happy) */
static int
{
else
}
static void
{
/* per 1.00 spec */
gpt->HeaderCRC32 = 0;
if (alternate) {
/*
* ptes_size is in sectors
*/
sizeof (GuidPartitionEntry_t),
} else {
}
= PED_CPU_TO_LE32 (sizeof (GuidPartitionEntry_t));
}
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;
/*
* ptes_size is in bytes and must be a multiple of sector_size.
*/
if (!ptes)
goto error;
continue;
}
/* Write protective MBR */
goto error_free_ptes;
/* Write PTH and PTEs */
goto error_free_ptes;
goto error_free_ptes;
/* Write Alternate PTH & PTEs */
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->hp_service = 0;
gpt_part_data->hidden = 0;
gpt_part_data->msftres = 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
{
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->hp_service) {
return 1;
}
if (gpt_part_data->msftres) {
return 1;
}
if (fs_type) {
return 1;
}
return 1;
}
return 1;
}
}
return 1;
}
/* Allocate metadata partitions for the GPTH and PTES */
static int
{
/* 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
int state)
{
switch (flag) {
case PED_PARTITION_BOOT:
if (state)
= gpt_part_data->lvm
= gpt_part_data->msftres = 0;
case PED_PARTITION_RAID:
if (state)
= gpt_part_data->lvm
= gpt_part_data->msftres = 0;
case PED_PARTITION_LVM:
if (state)
= gpt_part_data->msftres = 0;
case PED_PARTITION_HPSERVICE:
if (state)
= gpt_part_data->lvm
= gpt_part_data->msftres = 0;
if (state)
= gpt_part_data->lvm
= gpt_part_data->hp_service = 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_HPSERVICE:
return gpt_part_data->hp_service;
return gpt_part_data->msftres;
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_HPSERVICE:
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;
}
static PedConstraint*
{
}
static int
{
return 1;
#ifndef DISCOVER_ONLY
_("Unable to satisfy all constraints on the partition."));
#endif
return 0;
}
static PedDiskOps gpt_disk_ops = {
#ifndef DISCOVER_ONLY
.clobber = gpt_clobber,
#else
#endif
#ifndef DISCOVER_ONLY
#else
#endif
};
static PedDiskType gpt_disk_type = {
.name = "gpt",
.ops = &gpt_disk_ops,
};
void
{
}
void
{
}