g_part_gpt.c revision 261993
181634Sbrian/*- 281634Sbrian * Copyright (c) 2002, 2005-2007, 2011 Marcel Moolenaar 381634Sbrian * All rights reserved. 481634Sbrian * 581634Sbrian * Redistribution and use in source and binary forms, with or without 681634Sbrian * modification, are permitted provided that the following conditions 781634Sbrian * are met: 881634Sbrian * 981634Sbrian * 1. Redistributions of source code must retain the above copyright 1081634Sbrian * notice, this list of conditions and the following disclaimer. 1181634Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1281634Sbrian * notice, this list of conditions and the following disclaimer in the 1381634Sbrian * documentation and/or other materials provided with the distribution. 1481634Sbrian * 1581634Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1681634Sbrian * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1781634Sbrian * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1881634Sbrian * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1981634Sbrian * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2081634Sbrian * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2181634Sbrian * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2281634Sbrian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2381634Sbrian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2481634Sbrian * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2581634Sbrian */ 2681634Sbrian 2781634Sbrian#include <sys/cdefs.h> 2881634Sbrian__FBSDID("$FreeBSD: stable/10/sys/geom/part/g_part_gpt.c 261993 2014-02-16 19:46:20Z marcel $"); 2981634Sbrian 3081634Sbrian#include <sys/param.h> 3181634Sbrian#include <sys/bio.h> 3281634Sbrian#include <sys/diskmbr.h> 3381634Sbrian#include <sys/endian.h> 3481634Sbrian#include <sys/gpt.h> 3581634Sbrian#include <sys/kernel.h> 3681634Sbrian#include <sys/kobj.h> 3781634Sbrian#include <sys/limits.h> 3881634Sbrian#include <sys/lock.h> 39102500Sbrian#include <sys/malloc.h> 4081634Sbrian#include <sys/mutex.h> 4181634Sbrian#include <sys/queue.h> 4281634Sbrian#include <sys/sbuf.h> 4381634Sbrian#include <sys/systm.h> 4481634Sbrian#include <sys/sysctl.h> 4581634Sbrian#include <sys/uuid.h> 4681634Sbrian#include <geom/geom.h> 4781634Sbrian#include <geom/part/g_part.h> 4881634Sbrian 4981634Sbrian#include "g_part_if.h" 5081634Sbrian 5181634SbrianFEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support"); 5281634Sbrian 5381634SbrianCTASSERT(offsetof(struct gpt_hdr, padding) == 92); 5481634SbrianCTASSERT(sizeof(struct gpt_ent) == 128); 5581634Sbrian 5681634Sbrian#define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0) 5781634Sbrian 5881634Sbrian#define MBRSIZE 512 5981634Sbrian 6081634Sbrianenum gpt_elt { 6181634Sbrian GPT_ELT_PRIHDR, 6281634Sbrian GPT_ELT_PRITBL, 6381634Sbrian GPT_ELT_SECHDR, 6481634Sbrian GPT_ELT_SECTBL, 6581634Sbrian GPT_ELT_COUNT 6681634Sbrian}; 6781634Sbrian 6881634Sbrianenum gpt_state { 6981634Sbrian GPT_STATE_UNKNOWN, /* Not determined. */ 7081634Sbrian GPT_STATE_MISSING, /* No signature found. */ 7181634Sbrian GPT_STATE_CORRUPT, /* Checksum mismatch. */ 7281634Sbrian GPT_STATE_INVALID, /* Nonconformant/invalid. */ 7381634Sbrian GPT_STATE_OK /* Perfectly fine. */ 7481634Sbrian}; 7581634Sbrian 7681634Sbrianstruct g_part_gpt_table { 7781634Sbrian struct g_part_table base; 7881634Sbrian u_char mbr[MBRSIZE]; 7981634Sbrian struct gpt_hdr *hdr; 8081634Sbrian quad_t lba[GPT_ELT_COUNT]; 8181634Sbrian enum gpt_state state[GPT_ELT_COUNT]; 8281634Sbrian int bootcamp; 8381634Sbrian}; 8481634Sbrian 8581634Sbrianstruct g_part_gpt_entry { 8681634Sbrian struct g_part_entry base; 8781634Sbrian struct gpt_ent ent; 8881634Sbrian}; 8981634Sbrian 9081634Sbrianstatic void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t); 9181634Sbrianstatic void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t); 9281634Sbrianstatic void g_gpt_set_defaults(struct g_part_table *, struct g_provider *); 9381634Sbrian 9481634Sbrianstatic int g_part_gpt_add(struct g_part_table *, struct g_part_entry *, 9581634Sbrian struct g_part_parms *); 9681634Sbrianstatic int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *); 9781634Sbrianstatic int g_part_gpt_create(struct g_part_table *, struct g_part_parms *); 9881634Sbrianstatic int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *); 9981634Sbrianstatic void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *, 10081634Sbrian struct sbuf *, const char *); 10181634Sbrianstatic int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *); 102136377Sbrianstatic int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *, 103136377Sbrian struct g_part_parms *); 104136377Sbrianstatic const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *, 105136377Sbrian char *, size_t); 106136377Sbrianstatic int g_part_gpt_probe(struct g_part_table *, struct g_consumer *); 107136377Sbrianstatic int g_part_gpt_read(struct g_part_table *, struct g_consumer *); 108136377Sbrianstatic int g_part_gpt_setunset(struct g_part_table *table, 109136377Sbrian struct g_part_entry *baseentry, const char *attrib, unsigned int set); 11081634Sbrianstatic const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *, 11181634Sbrian char *, size_t); 112134789Sbrianstatic int g_part_gpt_write(struct g_part_table *, struct g_consumer *); 113134789Sbrianstatic int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *, 11481634Sbrian struct g_part_parms *); 11581634Sbrianstatic int g_part_gpt_recover(struct g_part_table *); 11681634Sbrian 11781634Sbrianstatic kobj_method_t g_part_gpt_methods[] = { 11881634Sbrian KOBJMETHOD(g_part_add, g_part_gpt_add), 11981634Sbrian KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode), 12081634Sbrian KOBJMETHOD(g_part_create, g_part_gpt_create), 12181897Sbrian KOBJMETHOD(g_part_destroy, g_part_gpt_destroy), 12281897Sbrian KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf), 12381634Sbrian KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto), 12481634Sbrian KOBJMETHOD(g_part_modify, g_part_gpt_modify), 12581634Sbrian KOBJMETHOD(g_part_resize, g_part_gpt_resize), 12681634Sbrian KOBJMETHOD(g_part_name, g_part_gpt_name), 12781634Sbrian KOBJMETHOD(g_part_probe, g_part_gpt_probe), 12881634Sbrian KOBJMETHOD(g_part_read, g_part_gpt_read), 12981634Sbrian KOBJMETHOD(g_part_recover, g_part_gpt_recover), 13081634Sbrian KOBJMETHOD(g_part_setunset, g_part_gpt_setunset), 13181897Sbrian KOBJMETHOD(g_part_type, g_part_gpt_type), 13281634Sbrian KOBJMETHOD(g_part_write, g_part_gpt_write), 13381634Sbrian { 0, 0 } 13481634Sbrian}; 13581634Sbrian 13681634Sbrianstatic struct g_part_scheme g_part_gpt_scheme = { 13781634Sbrian "GPT", 13881634Sbrian g_part_gpt_methods, 13981634Sbrian sizeof(struct g_part_gpt_table), 14081634Sbrian .gps_entrysz = sizeof(struct g_part_gpt_entry), 14181634Sbrian .gps_minent = 128, 14281634Sbrian .gps_maxent = 4096, 14381634Sbrian .gps_bootcodesz = MBRSIZE, 14481634Sbrian}; 14581634SbrianG_PART_SCHEME_DECLARE(g_part_gpt); 14681634Sbrian 147134875Sbrianstatic struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT; 148134875Sbrianstatic struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS; 149134875Sbrianstatic struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL; 150134875Sbrianstatic struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID; 151134875Sbrianstatic struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE; 152134875Sbrianstatic struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY; 153134875Sbrianstatic struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS; 15481634Sbrianstatic struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT; 15581634Sbrianstatic struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI; 15681634Sbrianstatic struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; 15781634Sbrianstatic struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; 15881634Sbrianstatic struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; 15981634Sbrianstatic struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; 16081634Sbrianstatic struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; 16181634Sbrianstatic struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 16281634Sbrianstatic struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 16381634Sbrianstatic struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA; 16481634Sbrianstatic struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM; 16581634Sbrianstatic struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID; 16681897Sbrianstatic struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP; 16781634Sbrianstatic struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS; 16881634Sbrianstatic struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG; 16981634Sbrianstatic struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED; 17081634Sbrianstatic struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR; 17181634Sbrianstatic struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; 17281634Sbrianstatic struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED; 17381634Sbrianstatic struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA; 17481634Sbrianstatic struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA; 17581634Sbrianstatic struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD; 17681634Sbrianstatic struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD; 17781634Sbrianstatic struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS; 17881634Sbrianstatic struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS; 17981634Sbrianstatic struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID; 18081634Sbrianstatic struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP; 18181634Sbrianstatic struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR; 18281634Sbrianstatic struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED; 18381634Sbrian 18481634Sbrianstatic struct g_part_uuid_alias { 18581634Sbrian struct uuid *uuid; 18681634Sbrian int alias; 18781634Sbrian int mbrtype; 18881634Sbrian} gpt_uuid_alias_match[] = { 18981634Sbrian { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab }, 19081634Sbrian { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf }, 19181634Sbrian { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 }, 19281634Sbrian { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 }, 19381634Sbrian { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 }, 19481634Sbrian { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 }, 19581634Sbrian { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 }, 19681634Sbrian { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 }, 19781634Sbrian { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee }, 19881634Sbrian { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 }, 19981634Sbrian { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 }, 20081634Sbrian { &gpt_uuid_freebsd_nandfs, G_PART_ALIAS_FREEBSD_NANDFS, 0 }, 20181634Sbrian { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 }, 20281634Sbrian { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 }, 20381897Sbrian { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 }, 20481634Sbrian { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 }, 20581634Sbrian { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b }, 20681634Sbrian { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 }, 20781634Sbrian { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 }, 20881634Sbrian { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 }, 20981634Sbrian { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 }, 21081634Sbrian { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 }, 21181634Sbrian { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 }, 212134789Sbrian { &gpt_uuid_vmvsanhdr, G_PART_ALIAS_VMVSANHDR, 0 }, 21381634Sbrian { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 }, 21481634Sbrian { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b }, 21581634Sbrian { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 }, 21681634Sbrian { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 }, 21781634Sbrian { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 }, 21881634Sbrian { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 }, 21981634Sbrian { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 }, 22081634Sbrian { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 }, 22181634Sbrian { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 }, 22281634Sbrian { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 }, 22381634Sbrian { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 }, 22481634Sbrian { NULL, 0, 0 } 22581634Sbrian}; 22681634Sbrian 22781634Sbrianstatic int 22881634Sbriangpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start, 229134789Sbrian quad_t end) 230134789Sbrian{ 23181634Sbrian 23281634Sbrian if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX) 23381634Sbrian return (EINVAL); 23481634Sbrian 23581634Sbrian mbr += DOSPARTOFF + idx * DOSPARTSIZE; 23681634Sbrian mbr[0] = 0; 23781634Sbrian if (start == 1) { 23881634Sbrian /* 23981634Sbrian * Treat the PMBR partition specially to maximize 24081634Sbrian * interoperability with BIOSes. 24181634Sbrian */ 24281634Sbrian mbr[1] = mbr[3] = 0; 243134789Sbrian mbr[2] = 2; 244134789Sbrian } else 24581634Sbrian mbr[1] = mbr[2] = mbr[3] = 0xff; 24681634Sbrian mbr[4] = typ; 24781634Sbrian mbr[5] = mbr[6] = mbr[7] = 0xff; 24881634Sbrian le32enc(mbr + 8, (uint32_t)start); 24981634Sbrian le32enc(mbr + 12, (uint32_t)(end - start + 1)); 25081634Sbrian return (0); 25181634Sbrian} 25281634Sbrian 25381634Sbrianstatic int 25481634Sbriangpt_map_type(struct uuid *t) 25581634Sbrian{ 25681634Sbrian struct g_part_uuid_alias *uap; 25781634Sbrian 25881634Sbrian for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 25981634Sbrian if (EQUUID(t, uap->uuid)) 26081634Sbrian return (uap->mbrtype); 26181634Sbrian } 26281634Sbrian return (0); 26381634Sbrian} 26481634Sbrian 26581634Sbrianstatic void 26681634Sbriangpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp) 26781634Sbrian{ 26881634Sbrian 26981634Sbrian bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 27081634Sbrian gpt_write_mbr_entry(table->mbr, 0, 0xee, 1, 27181897Sbrian MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX)); 27281634Sbrian le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 27381634Sbrian} 27481634Sbrian 27581634Sbrian/* 27681634Sbrian * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the 27781634Sbrian * whole disk anymore. Rather, it covers the GPT table and the EFI 27881634Sbrian * system partition only. This way the HFS+ partition and any FAT 27981634Sbrian * partitions can be added to the MBR without creating an overlap. 28081634Sbrian */ 28181634Sbrianstatic int 28281634Sbriangpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname) 28381634Sbrian{ 28481634Sbrian uint8_t *p; 28581634Sbrian 28681634Sbrian p = table->mbr + DOSPARTOFF; 28781634Sbrian if (p[4] != 0xee || le32dec(p + 8) != 1) 28881634Sbrian return (0); 28981634Sbrian 29081634Sbrian p += DOSPARTSIZE; 29181634Sbrian if (p[4] != 0xaf) 29281634Sbrian return (0); 29381634Sbrian 29481634Sbrian printf("GEOM: %s: enabling Boot Camp\n", provname); 29581634Sbrian return (1); 29681634Sbrian} 29781634Sbrian 29881897Sbrianstatic void 29981897Sbriangpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp) 30081897Sbrian{ 30181634Sbrian struct g_part_entry *baseentry; 30281634Sbrian struct g_part_gpt_entry *entry; 30381634Sbrian struct g_part_gpt_table *table; 30481634Sbrian int bootable, error, index, slices, typ; 30581634Sbrian 30681634Sbrian table = (struct g_part_gpt_table *)basetable; 30781634Sbrian 30881634Sbrian bootable = -1; 30981634Sbrian for (index = 0; index < NDOSPART; index++) { 31081634Sbrian if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index]) 31181634Sbrian bootable = index; 31281634Sbrian } 31381634Sbrian 31481634Sbrian bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART); 31581634Sbrian slices = 0; 31681634Sbrian LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 31781634Sbrian if (baseentry->gpe_deleted) 31881634Sbrian continue; 31981634Sbrian index = baseentry->gpe_index - 1; 32081634Sbrian if (index >= NDOSPART) 32181634Sbrian continue; 32281634Sbrian 32381634Sbrian entry = (struct g_part_gpt_entry *)baseentry; 32481634Sbrian 32581634Sbrian switch (index) { 32681634Sbrian case 0: /* This must be the EFI system partition. */ 32781634Sbrian if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi)) 32881634Sbrian goto disable; 32981634Sbrian error = gpt_write_mbr_entry(table->mbr, index, 0xee, 33081634Sbrian 1ull, entry->ent.ent_lba_end); 33181634Sbrian break; 33281634Sbrian case 1: /* This must be the HFS+ partition. */ 33381634Sbrian if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs)) 33481634Sbrian goto disable; 33581634Sbrian error = gpt_write_mbr_entry(table->mbr, index, 0xaf, 33681634Sbrian entry->ent.ent_lba_start, entry->ent.ent_lba_end); 33781634Sbrian break; 33881634Sbrian default: 33981634Sbrian typ = gpt_map_type(&entry->ent.ent_type); 340134875Sbrian error = gpt_write_mbr_entry(table->mbr, index, typ, 341134875Sbrian entry->ent.ent_lba_start, entry->ent.ent_lba_end); 342134875Sbrian break; 343134875Sbrian } 344134875Sbrian if (error) 345134875Sbrian continue; 346134875Sbrian 34781634Sbrian if (index == bootable) 34881634Sbrian table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80; 34981634Sbrian slices |= 1 << index; 35081634Sbrian } 35181634Sbrian if ((slices & 3) == 3) 35281897Sbrian return; 35381634Sbrian 35481634Sbrian disable: 35581634Sbrian table->bootcamp = 0; 35681634Sbrian gpt_create_pmbr(table, pp); 35781634Sbrian} 35881634Sbrian 35981634Sbrianstatic struct gpt_hdr * 36081634Sbriangpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp, 36181634Sbrian enum gpt_elt elt) 36281634Sbrian{ 36381634Sbrian struct gpt_hdr *buf, *hdr; 36481634Sbrian struct g_provider *pp; 36581634Sbrian quad_t lba, last; 36681634Sbrian int error; 36781634Sbrian uint32_t crc, sz; 36881634Sbrian 36981634Sbrian pp = cp->provider; 37081634Sbrian last = (pp->mediasize / pp->sectorsize) - 1; 37181634Sbrian table->state[elt] = GPT_STATE_MISSING; 37281634Sbrian /* 373134789Sbrian * If the primary header is valid look for secondary 37481634Sbrian * header in AlternateLBA, otherwise in the last medium's LBA. 37581634Sbrian */ 37681634Sbrian if (elt == GPT_ELT_SECHDR) { 37781634Sbrian if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK) 37881634Sbrian table->lba[elt] = last; 37981634Sbrian } else 38081634Sbrian table->lba[elt] = 1; 38181634Sbrian buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize, 38281634Sbrian &error); 38381634Sbrian if (buf == NULL) 38481634Sbrian return (NULL); 38581634Sbrian hdr = NULL; 386134789Sbrian if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0) 38781634Sbrian goto fail; 38881634Sbrian 38981634Sbrian table->state[elt] = GPT_STATE_CORRUPT; 39081634Sbrian sz = le32toh(buf->hdr_size); 39181634Sbrian if (sz < 92 || sz > pp->sectorsize) 39281634Sbrian goto fail; 39381634Sbrian 39481634Sbrian hdr = g_malloc(sz, M_WAITOK | M_ZERO); 39581634Sbrian bcopy(buf, hdr, sz); 39681634Sbrian hdr->hdr_size = sz; 39781634Sbrian 39881634Sbrian crc = le32toh(buf->hdr_crc_self); 39981634Sbrian buf->hdr_crc_self = 0; 40081634Sbrian if (crc32(buf, sz) != crc) 40181634Sbrian goto fail; 40281634Sbrian hdr->hdr_crc_self = crc; 40381634Sbrian 40481634Sbrian table->state[elt] = GPT_STATE_INVALID; 40581634Sbrian hdr->hdr_revision = le32toh(buf->hdr_revision); 40681634Sbrian if (hdr->hdr_revision < GPT_HDR_REVISION) 40781634Sbrian goto fail; 40881634Sbrian hdr->hdr_lba_self = le64toh(buf->hdr_lba_self); 40981634Sbrian if (hdr->hdr_lba_self != table->lba[elt]) 41081634Sbrian goto fail; 41181634Sbrian hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt); 41281634Sbrian if (hdr->hdr_lba_alt == hdr->hdr_lba_self || 41381634Sbrian hdr->hdr_lba_alt > last) 41481634Sbrian goto fail; 41581634Sbrian 41681634Sbrian /* Check the managed area. */ 41781634Sbrian hdr->hdr_lba_start = le64toh(buf->hdr_lba_start); 41881634Sbrian if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last) 41981634Sbrian goto fail; 420134789Sbrian hdr->hdr_lba_end = le64toh(buf->hdr_lba_end); 42181634Sbrian if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last) 42281634Sbrian goto fail; 42381634Sbrian 424134789Sbrian /* Check the table location and size of the table. */ 42581634Sbrian hdr->hdr_entries = le32toh(buf->hdr_entries); 42681634Sbrian hdr->hdr_entsz = le32toh(buf->hdr_entsz); 42781634Sbrian if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 || 42881634Sbrian (hdr->hdr_entsz & 7) != 0) 42981634Sbrian goto fail; 43081634Sbrian hdr->hdr_lba_table = le64toh(buf->hdr_lba_table); 43181634Sbrian if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last) 43281634Sbrian goto fail; 43381634Sbrian if (hdr->hdr_lba_table >= hdr->hdr_lba_start && 43481634Sbrian hdr->hdr_lba_table <= hdr->hdr_lba_end) 43581634Sbrian goto fail; 43681634Sbrian lba = hdr->hdr_lba_table + 43781634Sbrian (hdr->hdr_entries * hdr->hdr_entsz + pp->sectorsize - 1) / 43881634Sbrian pp->sectorsize - 1; 43981634Sbrian if (lba >= last) 44081634Sbrian goto fail; 44181634Sbrian if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end) 44281634Sbrian goto fail; 44381634Sbrian 44481634Sbrian table->state[elt] = GPT_STATE_OK; 445134789Sbrian le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid); 44681634Sbrian hdr->hdr_crc_table = le32toh(buf->hdr_crc_table); 44781634Sbrian 44881897Sbrian /* save LBA for secondary header */ 44981897Sbrian if (elt == GPT_ELT_PRIHDR) 45081634Sbrian table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt; 45181634Sbrian 45281634Sbrian g_free(buf); 45381634Sbrian return (hdr); 45481634Sbrian 45581634Sbrian fail: 45681634Sbrian if (hdr != NULL) 45781634Sbrian g_free(hdr); 45881634Sbrian g_free(buf); 45981634Sbrian return (NULL); 46081634Sbrian} 46181634Sbrian 46281634Sbrianstatic struct gpt_ent * 46381634Sbriangpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, 46481634Sbrian enum gpt_elt elt, struct gpt_hdr *hdr) 46581634Sbrian{ 46681634Sbrian struct g_provider *pp; 46781634Sbrian struct gpt_ent *ent, *tbl; 46881634Sbrian char *buf, *p; 46981634Sbrian unsigned int idx, sectors, tblsz, size; 47081634Sbrian int error; 47181634Sbrian 47281634Sbrian if (hdr == NULL) 47381634Sbrian return (NULL); 47481634Sbrian 47581634Sbrian pp = cp->provider; 47681634Sbrian table->lba[elt] = hdr->hdr_lba_table; 47781634Sbrian 47881634Sbrian table->state[elt] = GPT_STATE_MISSING; 47981634Sbrian tblsz = hdr->hdr_entries * hdr->hdr_entsz; 48081634Sbrian sectors = (tblsz + pp->sectorsize - 1) / pp->sectorsize; 48181634Sbrian buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO); 48281634Sbrian for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) { 48381634Sbrian size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS: 48481634Sbrian (sectors - idx) * pp->sectorsize; 48581634Sbrian p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize, 48681634Sbrian size, &error); 48781634Sbrian if (p == NULL) { 48881634Sbrian g_free(buf); 48981634Sbrian return (NULL); 49081634Sbrian } 49181634Sbrian bcopy(p, buf + idx * pp->sectorsize, size); 49281634Sbrian g_free(p); 49381634Sbrian } 49481634Sbrian table->state[elt] = GPT_STATE_CORRUPT; 49581634Sbrian if (crc32(buf, tblsz) != hdr->hdr_crc_table) { 49681634Sbrian g_free(buf); 49781634Sbrian return (NULL); 49881634Sbrian } 49981634Sbrian 50081634Sbrian table->state[elt] = GPT_STATE_OK; 50181634Sbrian tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent), 50281897Sbrian M_WAITOK | M_ZERO); 50381634Sbrian 50481634Sbrian for (idx = 0, ent = tbl, p = buf; 50581634Sbrian idx < hdr->hdr_entries; 50681634Sbrian idx++, ent++, p += hdr->hdr_entsz) { 50781634Sbrian le_uuid_dec(p, &ent->ent_type); 50881634Sbrian le_uuid_dec(p + 16, &ent->ent_uuid); 50981634Sbrian ent->ent_lba_start = le64dec(p + 32); 51081634Sbrian ent->ent_lba_end = le64dec(p + 40); 51181634Sbrian ent->ent_attr = le64dec(p + 48); 51281634Sbrian /* Keep UTF-16 in little-endian. */ 51381634Sbrian bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name)); 51481634Sbrian } 51581634Sbrian 51681634Sbrian g_free(buf); 51781634Sbrian return (tbl); 51881897Sbrian} 51981897Sbrian 52081897Sbrianstatic int 52181634Sbriangpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec) 52281634Sbrian{ 52381634Sbrian 52481634Sbrian if (pri == NULL || sec == NULL) 52581634Sbrian return (0); 52681634Sbrian 52781634Sbrian if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid)) 52881634Sbrian return (0); 52981634Sbrian return ((pri->hdr_revision == sec->hdr_revision && 53081634Sbrian pri->hdr_size == sec->hdr_size && 53181634Sbrian pri->hdr_lba_start == sec->hdr_lba_start && 53281634Sbrian pri->hdr_lba_end == sec->hdr_lba_end && 53381634Sbrian pri->hdr_entries == sec->hdr_entries && 53481897Sbrian pri->hdr_entsz == sec->hdr_entsz && 53581897Sbrian pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0); 53681897Sbrian} 53781634Sbrian 53881634Sbrianstatic int 53981634Sbriangpt_parse_type(const char *type, struct uuid *uuid) 54081634Sbrian{ 54181634Sbrian struct uuid tmp; 54281634Sbrian const char *alias; 54381634Sbrian int error; 54481634Sbrian struct g_part_uuid_alias *uap; 54581897Sbrian 54681634Sbrian if (type[0] == '!') { 54781634Sbrian error = parse_uuid(type + 1, &tmp); 548 if (error) 549 return (error); 550 if (EQUUID(&tmp, &gpt_uuid_unused)) 551 return (EINVAL); 552 *uuid = tmp; 553 return (0); 554 } 555 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) { 556 alias = g_part_alias_name(uap->alias); 557 if (!strcasecmp(type, alias)) { 558 *uuid = *uap->uuid; 559 return (0); 560 } 561 } 562 return (EINVAL); 563} 564 565static int 566g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 567 struct g_part_parms *gpp) 568{ 569 struct g_part_gpt_entry *entry; 570 int error; 571 572 entry = (struct g_part_gpt_entry *)baseentry; 573 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 574 if (error) 575 return (error); 576 kern_uuidgen(&entry->ent.ent_uuid, 1); 577 entry->ent.ent_lba_start = baseentry->gpe_start; 578 entry->ent.ent_lba_end = baseentry->gpe_end; 579 if (baseentry->gpe_deleted) { 580 entry->ent.ent_attr = 0; 581 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 582 } 583 if (gpp->gpp_parms & G_PART_PARM_LABEL) 584 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 585 sizeof(entry->ent.ent_name) / 586 sizeof(entry->ent.ent_name[0])); 587 return (0); 588} 589 590static int 591g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 592{ 593 struct g_part_gpt_table *table; 594 size_t codesz; 595 596 codesz = DOSPARTOFF; 597 table = (struct g_part_gpt_table *)basetable; 598 bzero(table->mbr, codesz); 599 codesz = MIN(codesz, gpp->gpp_codesize); 600 if (codesz > 0) 601 bcopy(gpp->gpp_codeptr, table->mbr, codesz); 602 return (0); 603} 604 605static int 606g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp) 607{ 608 struct g_provider *pp; 609 struct g_part_gpt_table *table; 610 size_t tblsz; 611 612 /* We don't nest, which means that our depth should be 0. */ 613 if (basetable->gpt_depth != 0) 614 return (ENXIO); 615 616 table = (struct g_part_gpt_table *)basetable; 617 pp = gpp->gpp_provider; 618 tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 619 pp->sectorsize - 1) / pp->sectorsize; 620 if (pp->sectorsize < MBRSIZE || 621 pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) * 622 pp->sectorsize) 623 return (ENOSPC); 624 625 gpt_create_pmbr(table, pp); 626 627 /* Allocate space for the header */ 628 table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO); 629 630 bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 631 table->hdr->hdr_revision = GPT_HDR_REVISION; 632 table->hdr->hdr_size = offsetof(struct gpt_hdr, padding); 633 kern_uuidgen(&table->hdr->hdr_uuid, 1); 634 table->hdr->hdr_entries = basetable->gpt_entries; 635 table->hdr->hdr_entsz = sizeof(struct gpt_ent); 636 637 g_gpt_set_defaults(basetable, pp); 638 return (0); 639} 640 641static int 642g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 643{ 644 struct g_part_gpt_table *table; 645 struct g_provider *pp; 646 647 table = (struct g_part_gpt_table *)basetable; 648 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 649 g_free(table->hdr); 650 table->hdr = NULL; 651 652 /* 653 * Wipe the first 2 sectors to clear the partitioning. Wipe the last 654 * sector only if it has valid secondary header. 655 */ 656 basetable->gpt_smhead |= 3; 657 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 658 table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1) 659 basetable->gpt_smtail |= 1; 660 return (0); 661} 662 663static void 664g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 665 struct sbuf *sb, const char *indent) 666{ 667 struct g_part_gpt_entry *entry; 668 669 entry = (struct g_part_gpt_entry *)baseentry; 670 if (indent == NULL) { 671 /* conftxt: libdisk compatibility */ 672 sbuf_printf(sb, " xs GPT xt "); 673 sbuf_printf_uuid(sb, &entry->ent.ent_type); 674 } else if (entry != NULL) { 675 /* confxml: partition entry information */ 676 sbuf_printf(sb, "%s<label>", indent); 677 g_gpt_printf_utf16(sb, entry->ent.ent_name, 678 sizeof(entry->ent.ent_name) >> 1); 679 sbuf_printf(sb, "</label>\n"); 680 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME) 681 sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent); 682 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) { 683 sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n", 684 indent); 685 } 686 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) { 687 sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n", 688 indent); 689 } 690 sbuf_printf(sb, "%s<rawtype>", indent); 691 sbuf_printf_uuid(sb, &entry->ent.ent_type); 692 sbuf_printf(sb, "</rawtype>\n"); 693 sbuf_printf(sb, "%s<rawuuid>", indent); 694 sbuf_printf_uuid(sb, &entry->ent.ent_uuid); 695 sbuf_printf(sb, "</rawuuid>\n"); 696 } else { 697 /* confxml: scheme information */ 698 } 699} 700 701static int 702g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 703{ 704 struct g_part_gpt_entry *entry; 705 706 entry = (struct g_part_gpt_entry *)baseentry; 707 return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) || 708 EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap)) ? 1 : 0); 709} 710 711static int 712g_part_gpt_modify(struct g_part_table *basetable, 713 struct g_part_entry *baseentry, struct g_part_parms *gpp) 714{ 715 struct g_part_gpt_entry *entry; 716 int error; 717 718 entry = (struct g_part_gpt_entry *)baseentry; 719 if (gpp->gpp_parms & G_PART_PARM_TYPE) { 720 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type); 721 if (error) 722 return (error); 723 } 724 if (gpp->gpp_parms & G_PART_PARM_LABEL) 725 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name, 726 sizeof(entry->ent.ent_name) / 727 sizeof(entry->ent.ent_name[0])); 728 return (0); 729} 730 731static int 732g_part_gpt_resize(struct g_part_table *basetable, 733 struct g_part_entry *baseentry, struct g_part_parms *gpp) 734{ 735 struct g_part_gpt_entry *entry; 736 entry = (struct g_part_gpt_entry *)baseentry; 737 738 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 739 entry->ent.ent_lba_end = baseentry->gpe_end; 740 741 return (0); 742} 743 744static const char * 745g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry, 746 char *buf, size_t bufsz) 747{ 748 struct g_part_gpt_entry *entry; 749 char c; 750 751 entry = (struct g_part_gpt_entry *)baseentry; 752 c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p'; 753 snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index); 754 return (buf); 755} 756 757static int 758g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp) 759{ 760 struct g_provider *pp; 761 u_char *buf; 762 int error, index, pri, res; 763 764 /* We don't nest, which means that our depth should be 0. */ 765 if (table->gpt_depth != 0) 766 return (ENXIO); 767 768 pp = cp->provider; 769 770 /* 771 * Sanity-check the provider. Since the first sector on the provider 772 * must be a PMBR and a PMBR is 512 bytes large, the sector size 773 * must be at least 512 bytes. Also, since the theoretical minimum 774 * number of sectors needed by GPT is 6, any medium that has less 775 * than 6 sectors is never going to be able to hold a GPT. The 776 * number 6 comes from: 777 * 1 sector for the PMBR 778 * 2 sectors for the GPT headers (each 1 sector) 779 * 2 sectors for the GPT tables (each 1 sector) 780 * 1 sector for an actual partition 781 * It's better to catch this pathological case early than behaving 782 * pathologically later on... 783 */ 784 if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize) 785 return (ENOSPC); 786 787 /* 788 * Check that there's a MBR or a PMBR. If it's a PMBR, we return 789 * as the highest priority on a match, otherwise we assume some 790 * GPT-unaware tool has destroyed the GPT by recreating a MBR and 791 * we really want the MBR scheme to take precedence. 792 */ 793 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 794 if (buf == NULL) 795 return (error); 796 res = le16dec(buf + DOSMAGICOFFSET); 797 pri = G_PART_PROBE_PRI_LOW; 798 for (index = 0; index < NDOSPART; index++) { 799 if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee) 800 pri = G_PART_PROBE_PRI_HIGH; 801 } 802 g_free(buf); 803 if (res != DOSMAGIC) 804 return (ENXIO); 805 806 /* Check that there's a primary header. */ 807 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 808 if (buf == NULL) 809 return (error); 810 res = memcmp(buf, GPT_HDR_SIG, 8); 811 g_free(buf); 812 if (res == 0) 813 return (pri); 814 815 /* No primary? Check that there's a secondary. */ 816 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 817 &error); 818 if (buf == NULL) 819 return (error); 820 res = memcmp(buf, GPT_HDR_SIG, 8); 821 g_free(buf); 822 return ((res == 0) ? pri : ENXIO); 823} 824 825static int 826g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) 827{ 828 struct gpt_hdr *prihdr, *sechdr; 829 struct gpt_ent *tbl, *pritbl, *sectbl; 830 struct g_provider *pp; 831 struct g_part_gpt_table *table; 832 struct g_part_gpt_entry *entry; 833 u_char *buf; 834 uint64_t last; 835 int error, index; 836 837 table = (struct g_part_gpt_table *)basetable; 838 pp = cp->provider; 839 last = (pp->mediasize / pp->sectorsize) - 1; 840 841 /* Read the PMBR */ 842 buf = g_read_data(cp, 0, pp->sectorsize, &error); 843 if (buf == NULL) 844 return (error); 845 bcopy(buf, table->mbr, MBRSIZE); 846 g_free(buf); 847 848 /* Read the primary header and table. */ 849 prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR); 850 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) { 851 pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr); 852 } else { 853 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 854 pritbl = NULL; 855 } 856 857 /* Read the secondary header and table. */ 858 sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR); 859 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) { 860 sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr); 861 } else { 862 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 863 sectbl = NULL; 864 } 865 866 /* Fail if we haven't got any good tables at all. */ 867 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && 868 table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 869 printf("GEOM: %s: corrupt or invalid GPT detected.\n", 870 pp->name); 871 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", 872 pp->name); 873 return (EINVAL); 874 } 875 876 /* 877 * If both headers are good but they disagree with each other, 878 * then invalidate one. We prefer to keep the primary header, 879 * unless the primary table is corrupt. 880 */ 881 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK && 882 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK && 883 !gpt_matched_hdrs(prihdr, sechdr)) { 884 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) { 885 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID; 886 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING; 887 g_free(sechdr); 888 sechdr = NULL; 889 } else { 890 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID; 891 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING; 892 g_free(prihdr); 893 prihdr = NULL; 894 } 895 } 896 897 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) { 898 printf("GEOM: %s: the primary GPT table is corrupt or " 899 "invalid.\n", pp->name); 900 printf("GEOM: %s: using the secondary instead -- recovery " 901 "strongly advised.\n", pp->name); 902 table->hdr = sechdr; 903 basetable->gpt_corrupt = 1; 904 if (prihdr != NULL) 905 g_free(prihdr); 906 tbl = sectbl; 907 if (pritbl != NULL) 908 g_free(pritbl); 909 } else { 910 if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { 911 printf("GEOM: %s: the secondary GPT table is corrupt " 912 "or invalid.\n", pp->name); 913 printf("GEOM: %s: using the primary only -- recovery " 914 "suggested.\n", pp->name); 915 basetable->gpt_corrupt = 1; 916 } else if (table->lba[GPT_ELT_SECHDR] != last) { 917 printf( "GEOM: %s: the secondary GPT header is not in " 918 "the last LBA.\n", pp->name); 919 basetable->gpt_corrupt = 1; 920 } 921 table->hdr = prihdr; 922 if (sechdr != NULL) 923 g_free(sechdr); 924 tbl = pritbl; 925 if (sectbl != NULL) 926 g_free(sectbl); 927 } 928 929 basetable->gpt_first = table->hdr->hdr_lba_start; 930 basetable->gpt_last = table->hdr->hdr_lba_end; 931 basetable->gpt_entries = (table->hdr->hdr_lba_start - 2) * 932 pp->sectorsize / table->hdr->hdr_entsz; 933 934 for (index = table->hdr->hdr_entries - 1; index >= 0; index--) { 935 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused)) 936 continue; 937 entry = (struct g_part_gpt_entry *)g_part_new_entry( 938 basetable, index + 1, tbl[index].ent_lba_start, 939 tbl[index].ent_lba_end); 940 entry->ent = tbl[index]; 941 } 942 943 g_free(tbl); 944 945 /* 946 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions 947 * if (and only if) any FAT32 or FAT16 partitions have been 948 * created. This happens irrespective of whether Boot Camp is 949 * used/enabled, though it's generally understood to be done 950 * to support legacy Windows under Boot Camp. We refer to this 951 * mirroring simply as Boot Camp. We try to detect Boot Camp 952 * so that we can update the MBR if and when GPT changes have 953 * been made. Note that we do not enable Boot Camp if not 954 * previously enabled because we can't assume that we're on a 955 * Mac alongside Mac OS X. 956 */ 957 table->bootcamp = gpt_is_bootcamp(table, pp->name); 958 959 return (0); 960} 961 962static int 963g_part_gpt_recover(struct g_part_table *basetable) 964{ 965 struct g_part_gpt_table *table; 966 struct g_provider *pp; 967 968 table = (struct g_part_gpt_table *)basetable; 969 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; 970 gpt_create_pmbr(table, pp); 971 g_gpt_set_defaults(basetable, pp); 972 basetable->gpt_corrupt = 0; 973 return (0); 974} 975 976static int 977g_part_gpt_setunset(struct g_part_table *basetable, 978 struct g_part_entry *baseentry, const char *attrib, unsigned int set) 979{ 980 struct g_part_gpt_entry *entry; 981 struct g_part_gpt_table *table; 982 uint8_t *p; 983 uint64_t attr; 984 int i; 985 986 table = (struct g_part_gpt_table *)basetable; 987 entry = (struct g_part_gpt_entry *)baseentry; 988 989 if (strcasecmp(attrib, "active") == 0) { 990 if (table->bootcamp) { 991 /* The active flag must be set on a valid entry. */ 992 if (entry == NULL) 993 return (ENXIO); 994 if (baseentry->gpe_index > NDOSPART) 995 return (EINVAL); 996 for (i = 0; i < NDOSPART; i++) { 997 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 998 p[0] = (i == baseentry->gpe_index - 1) 999 ? ((set) ? 0x80 : 0) : 0; 1000 } 1001 } else { 1002 /* The PMBR is marked as active without an entry. */ 1003 if (entry != NULL) 1004 return (ENXIO); 1005 for (i = 0; i < NDOSPART; i++) { 1006 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE]; 1007 p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0; 1008 } 1009 } 1010 return (0); 1011 } 1012 1013 if (entry == NULL) 1014 return (ENODEV); 1015 1016 attr = 0; 1017 if (strcasecmp(attrib, "bootme") == 0) { 1018 attr |= GPT_ENT_ATTR_BOOTME; 1019 } else if (strcasecmp(attrib, "bootonce") == 0) { 1020 attr |= GPT_ENT_ATTR_BOOTONCE; 1021 if (set) 1022 attr |= GPT_ENT_ATTR_BOOTME; 1023 } else if (strcasecmp(attrib, "bootfailed") == 0) { 1024 /* 1025 * It should only be possible to unset BOOTFAILED, but it might 1026 * be useful for test purposes to also be able to set it. 1027 */ 1028 attr |= GPT_ENT_ATTR_BOOTFAILED; 1029 } 1030 if (attr == 0) 1031 return (EINVAL); 1032 1033 if (set) 1034 attr = entry->ent.ent_attr | attr; 1035 else 1036 attr = entry->ent.ent_attr & ~attr; 1037 if (attr != entry->ent.ent_attr) { 1038 entry->ent.ent_attr = attr; 1039 if (!baseentry->gpe_created) 1040 baseentry->gpe_modified = 1; 1041 } 1042 return (0); 1043} 1044 1045static const char * 1046g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 1047 char *buf, size_t bufsz) 1048{ 1049 struct g_part_gpt_entry *entry; 1050 struct uuid *type; 1051 struct g_part_uuid_alias *uap; 1052 1053 entry = (struct g_part_gpt_entry *)baseentry; 1054 type = &entry->ent.ent_type; 1055 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) 1056 if (EQUUID(type, uap->uuid)) 1057 return (g_part_alias_name(uap->alias)); 1058 buf[0] = '!'; 1059 snprintf_uuid(buf + 1, bufsz - 1, type); 1060 1061 return (buf); 1062} 1063 1064static int 1065g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp) 1066{ 1067 unsigned char *buf, *bp; 1068 struct g_provider *pp; 1069 struct g_part_entry *baseentry; 1070 struct g_part_gpt_entry *entry; 1071 struct g_part_gpt_table *table; 1072 size_t tblsz; 1073 uint32_t crc; 1074 int error, index; 1075 1076 pp = cp->provider; 1077 table = (struct g_part_gpt_table *)basetable; 1078 tblsz = (table->hdr->hdr_entries * table->hdr->hdr_entsz + 1079 pp->sectorsize - 1) / pp->sectorsize; 1080 1081 /* Reconstruct the MBR from the GPT if under Boot Camp. */ 1082 if (table->bootcamp) 1083 gpt_update_bootcamp(basetable, pp); 1084 1085 /* Write the PMBR */ 1086 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 1087 bcopy(table->mbr, buf, MBRSIZE); 1088 error = g_write_data(cp, 0, buf, pp->sectorsize); 1089 g_free(buf); 1090 if (error) 1091 return (error); 1092 1093 /* Allocate space for the header and entries. */ 1094 buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO); 1095 1096 memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig)); 1097 le32enc(buf + 8, table->hdr->hdr_revision); 1098 le32enc(buf + 12, table->hdr->hdr_size); 1099 le64enc(buf + 40, table->hdr->hdr_lba_start); 1100 le64enc(buf + 48, table->hdr->hdr_lba_end); 1101 le_uuid_enc(buf + 56, &table->hdr->hdr_uuid); 1102 le32enc(buf + 80, table->hdr->hdr_entries); 1103 le32enc(buf + 84, table->hdr->hdr_entsz); 1104 1105 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) { 1106 if (baseentry->gpe_deleted) 1107 continue; 1108 entry = (struct g_part_gpt_entry *)baseentry; 1109 index = baseentry->gpe_index - 1; 1110 bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index; 1111 le_uuid_enc(bp, &entry->ent.ent_type); 1112 le_uuid_enc(bp + 16, &entry->ent.ent_uuid); 1113 le64enc(bp + 32, entry->ent.ent_lba_start); 1114 le64enc(bp + 40, entry->ent.ent_lba_end); 1115 le64enc(bp + 48, entry->ent.ent_attr); 1116 memcpy(bp + 56, entry->ent.ent_name, 1117 sizeof(entry->ent.ent_name)); 1118 } 1119 1120 crc = crc32(buf + pp->sectorsize, 1121 table->hdr->hdr_entries * table->hdr->hdr_entsz); 1122 le32enc(buf + 88, crc); 1123 1124 /* Write primary meta-data. */ 1125 le32enc(buf + 16, 0); /* hdr_crc_self. */ 1126 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */ 1127 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */ 1128 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */ 1129 crc = crc32(buf, table->hdr->hdr_size); 1130 le32enc(buf + 16, crc); 1131 1132 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1133 error = g_write_data(cp, 1134 (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize, 1135 buf + (index + 1) * pp->sectorsize, 1136 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1137 (tblsz - index) * pp->sectorsize); 1138 if (error) 1139 goto out; 1140 } 1141 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize, 1142 buf, pp->sectorsize); 1143 if (error) 1144 goto out; 1145 1146 /* Write secondary meta-data. */ 1147 le32enc(buf + 16, 0); /* hdr_crc_self. */ 1148 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */ 1149 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */ 1150 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */ 1151 crc = crc32(buf, table->hdr->hdr_size); 1152 le32enc(buf + 16, crc); 1153 1154 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) { 1155 error = g_write_data(cp, 1156 (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize, 1157 buf + (index + 1) * pp->sectorsize, 1158 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS: 1159 (tblsz - index) * pp->sectorsize); 1160 if (error) 1161 goto out; 1162 } 1163 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize, 1164 buf, pp->sectorsize); 1165 1166 out: 1167 g_free(buf); 1168 return (error); 1169} 1170 1171static void 1172g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp) 1173{ 1174 struct g_part_gpt_table *table; 1175 quad_t last; 1176 size_t tblsz; 1177 1178 table = (struct g_part_gpt_table *)basetable; 1179 last = pp->mediasize / pp->sectorsize - 1; 1180 tblsz = (basetable->gpt_entries * sizeof(struct gpt_ent) + 1181 pp->sectorsize - 1) / pp->sectorsize; 1182 1183 table->lba[GPT_ELT_PRIHDR] = 1; 1184 table->lba[GPT_ELT_PRITBL] = 2; 1185 table->lba[GPT_ELT_SECHDR] = last; 1186 table->lba[GPT_ELT_SECTBL] = last - tblsz; 1187 table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK; 1188 table->state[GPT_ELT_PRITBL] = GPT_STATE_OK; 1189 table->state[GPT_ELT_SECHDR] = GPT_STATE_OK; 1190 table->state[GPT_ELT_SECTBL] = GPT_STATE_OK; 1191 1192 table->hdr->hdr_lba_start = 2 + tblsz; 1193 table->hdr->hdr_lba_end = last - tblsz - 1; 1194 1195 basetable->gpt_first = table->hdr->hdr_lba_start; 1196 basetable->gpt_last = table->hdr->hdr_lba_end; 1197} 1198 1199static void 1200g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len) 1201{ 1202 u_int bo; 1203 uint32_t ch; 1204 uint16_t c; 1205 1206 bo = LITTLE_ENDIAN; /* GPT is little-endian */ 1207 while (len > 0 && *str != 0) { 1208 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str); 1209 str++, len--; 1210 if ((ch & 0xf800) == 0xd800) { 1211 if (len > 0) { 1212 c = (bo == BIG_ENDIAN) ? be16toh(*str) 1213 : le16toh(*str); 1214 str++, len--; 1215 } else 1216 c = 0xfffd; 1217 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) { 1218 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff); 1219 ch += 0x10000; 1220 } else 1221 ch = 0xfffd; 1222 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */ 1223 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN; 1224 continue; 1225 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */ 1226 continue; 1227 1228 /* Write the Unicode character in UTF-8 */ 1229 if (ch < 0x80) 1230 sbuf_printf(sb, "%c", ch); 1231 else if (ch < 0x800) 1232 sbuf_printf(sb, "%c%c", 0xc0 | (ch >> 6), 1233 0x80 | (ch & 0x3f)); 1234 else if (ch < 0x10000) 1235 sbuf_printf(sb, "%c%c%c", 0xe0 | (ch >> 12), 1236 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 1237 else if (ch < 0x200000) 1238 sbuf_printf(sb, "%c%c%c%c", 0xf0 | (ch >> 18), 1239 0x80 | ((ch >> 12) & 0x3f), 1240 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f)); 1241 } 1242} 1243 1244static void 1245g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len) 1246{ 1247 size_t s16idx, s8idx; 1248 uint32_t utfchar; 1249 unsigned int c, utfbytes; 1250 1251 s8idx = s16idx = 0; 1252 utfchar = 0; 1253 utfbytes = 0; 1254 bzero(s16, s16len << 1); 1255 while (s8[s8idx] != 0 && s16idx < s16len) { 1256 c = s8[s8idx++]; 1257 if ((c & 0xc0) != 0x80) { 1258 /* Initial characters. */ 1259 if (utfbytes != 0) { 1260 /* Incomplete encoding of previous char. */ 1261 s16[s16idx++] = htole16(0xfffd); 1262 } 1263 if ((c & 0xf8) == 0xf0) { 1264 utfchar = c & 0x07; 1265 utfbytes = 3; 1266 } else if ((c & 0xf0) == 0xe0) { 1267 utfchar = c & 0x0f; 1268 utfbytes = 2; 1269 } else if ((c & 0xe0) == 0xc0) { 1270 utfchar = c & 0x1f; 1271 utfbytes = 1; 1272 } else { 1273 utfchar = c & 0x7f; 1274 utfbytes = 0; 1275 } 1276 } else { 1277 /* Followup characters. */ 1278 if (utfbytes > 0) { 1279 utfchar = (utfchar << 6) + (c & 0x3f); 1280 utfbytes--; 1281 } else if (utfbytes == 0) 1282 utfbytes = ~0; 1283 } 1284 /* 1285 * Write the complete Unicode character as UTF-16 when we 1286 * have all the UTF-8 charactars collected. 1287 */ 1288 if (utfbytes == 0) { 1289 /* 1290 * If we need to write 2 UTF-16 characters, but 1291 * we only have room for 1, then we truncate the 1292 * string by writing a 0 instead. 1293 */ 1294 if (utfchar >= 0x10000 && s16idx < s16len - 1) { 1295 s16[s16idx++] = 1296 htole16(0xd800 | ((utfchar >> 10) - 0x40)); 1297 s16[s16idx++] = 1298 htole16(0xdc00 | (utfchar & 0x3ff)); 1299 } else 1300 s16[s16idx++] = (utfchar >= 0x10000) ? 0 : 1301 htole16(utfchar); 1302 } 1303 } 1304 /* 1305 * If our input string was truncated, append an invalid encoding 1306 * character to the output string. 1307 */ 1308 if (utfbytes != 0 && s16idx < s16len) 1309 s16[s16idx++] = htole16(0xfffd); 1310} 1311