2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, 2005-2007, 2011 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/diskmbr.h>
35 #include <sys/endian.h>
37 #include <sys/kernel.h>
39 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/queue.h>
45 #include <sys/systm.h>
46 #include <sys/sysctl.h>
48 #include <geom/geom.h>
49 #include <geom/geom_int.h>
50 #include <geom/part/g_part.h>
52 #include "g_part_if.h"
54 FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support");
56 SYSCTL_DECL(_kern_geom_part);
57 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, gpt, CTLFLAG_RW, 0,
58 "GEOM_PART_GPT GUID Partition Table");
60 static u_int allow_nesting = 0;
61 SYSCTL_UINT(_kern_geom_part_gpt, OID_AUTO, allow_nesting,
62 CTLFLAG_RWTUN, &allow_nesting, 0, "Allow GPT to be nested inside other schemes");
64 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
65 CTASSERT(sizeof(struct gpt_ent) == 128);
67 #define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0)
80 GPT_STATE_UNKNOWN, /* Not determined. */
81 GPT_STATE_MISSING, /* No signature found. */
82 GPT_STATE_CORRUPT, /* Checksum mismatch. */
83 GPT_STATE_INVALID, /* Nonconformant/invalid. */
84 GPT_STATE_OK /* Perfectly fine. */
87 struct g_part_gpt_table {
88 struct g_part_table base;
91 quad_t lba[GPT_ELT_COUNT];
92 enum gpt_state state[GPT_ELT_COUNT];
96 struct g_part_gpt_entry {
97 struct g_part_entry base;
101 static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
102 static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
103 static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *);
105 static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
106 struct g_part_parms *);
107 static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
108 static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
109 static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
110 static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
111 struct sbuf *, const char *);
112 static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
113 static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
114 struct g_part_parms *);
115 static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
117 static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
118 static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
119 static int g_part_gpt_setunset(struct g_part_table *table,
120 struct g_part_entry *baseentry, const char *attrib, unsigned int set);
121 static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
123 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
124 static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *,
125 struct g_part_parms *);
126 static int g_part_gpt_recover(struct g_part_table *);
128 static kobj_method_t g_part_gpt_methods[] = {
129 KOBJMETHOD(g_part_add, g_part_gpt_add),
130 KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode),
131 KOBJMETHOD(g_part_create, g_part_gpt_create),
132 KOBJMETHOD(g_part_destroy, g_part_gpt_destroy),
133 KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf),
134 KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto),
135 KOBJMETHOD(g_part_modify, g_part_gpt_modify),
136 KOBJMETHOD(g_part_resize, g_part_gpt_resize),
137 KOBJMETHOD(g_part_name, g_part_gpt_name),
138 KOBJMETHOD(g_part_probe, g_part_gpt_probe),
139 KOBJMETHOD(g_part_read, g_part_gpt_read),
140 KOBJMETHOD(g_part_recover, g_part_gpt_recover),
141 KOBJMETHOD(g_part_setunset, g_part_gpt_setunset),
142 KOBJMETHOD(g_part_type, g_part_gpt_type),
143 KOBJMETHOD(g_part_write, g_part_gpt_write),
147 static struct g_part_scheme g_part_gpt_scheme = {
150 sizeof(struct g_part_gpt_table),
151 .gps_entrysz = sizeof(struct g_part_gpt_entry),
154 .gps_bootcodesz = MBRSIZE,
156 G_PART_SCHEME_DECLARE(g_part_gpt);
157 MODULE_VERSION(geom_part_gpt, 0);
159 static struct uuid gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS;
160 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT;
161 static struct uuid gpt_uuid_apple_core_storage =
162 GPT_ENT_TYPE_APPLE_CORE_STORAGE;
163 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
164 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL;
165 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID;
166 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE;
167 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY;
168 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
169 static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT;
170 static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE;
171 static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL;
172 static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED;
173 static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT;
174 static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD;
175 static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER;
176 static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2;
177 static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32;
178 static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64;
179 static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY;
180 static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP;
181 static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1;
182 static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM;
183 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
184 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
185 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
186 static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
187 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
188 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
189 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
190 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
191 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA;
192 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
193 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
194 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
195 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
196 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
197 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
198 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
199 static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY;
200 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED;
201 static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES;
202 static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
203 static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
204 static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
205 static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
206 static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID;
207 static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
208 static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA;
209 static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT;
210 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
211 static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS;
212 static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG;
213 static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED;
214 static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR;
216 static struct g_part_uuid_alias {
220 } gpt_uuid_alias_match[] = {
221 { &gpt_uuid_apple_apfs, G_PART_ALIAS_APPLE_APFS, 0 },
222 { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab },
223 { &gpt_uuid_apple_core_storage, G_PART_ALIAS_APPLE_CORE_STORAGE, 0 },
224 { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf },
225 { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 },
226 { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 },
227 { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 },
228 { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 },
229 { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 },
230 { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 },
231 { &gpt_uuid_chromeos_firmware, G_PART_ALIAS_CHROMEOS_FIRMWARE, 0 },
232 { &gpt_uuid_chromeos_kernel, G_PART_ALIAS_CHROMEOS_KERNEL, 0 },
233 { &gpt_uuid_chromeos_reserved, G_PART_ALIAS_CHROMEOS_RESERVED, 0 },
234 { &gpt_uuid_chromeos_root, G_PART_ALIAS_CHROMEOS_ROOT, 0 },
235 { &gpt_uuid_dfbsd_ccd, G_PART_ALIAS_DFBSD_CCD, 0 },
236 { &gpt_uuid_dfbsd_hammer, G_PART_ALIAS_DFBSD_HAMMER, 0 },
237 { &gpt_uuid_dfbsd_hammer2, G_PART_ALIAS_DFBSD_HAMMER2, 0 },
238 { &gpt_uuid_dfbsd_label32, G_PART_ALIAS_DFBSD, 0xa5 },
239 { &gpt_uuid_dfbsd_label64, G_PART_ALIAS_DFBSD64, 0xa5 },
240 { &gpt_uuid_dfbsd_legacy, G_PART_ALIAS_DFBSD_LEGACY, 0 },
241 { &gpt_uuid_dfbsd_swap, G_PART_ALIAS_DFBSD_SWAP, 0 },
242 { &gpt_uuid_dfbsd_ufs1, G_PART_ALIAS_DFBSD_UFS, 0 },
243 { &gpt_uuid_dfbsd_vinum, G_PART_ALIAS_DFBSD_VINUM, 0 },
244 { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee },
245 { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 },
246 { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 },
247 { &gpt_uuid_freebsd_nandfs, G_PART_ALIAS_FREEBSD_NANDFS, 0 },
248 { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 },
249 { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 },
250 { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 },
251 { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 },
252 { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b },
253 { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 },
254 { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 },
255 { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 },
256 { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 },
257 { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b },
258 { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 },
259 { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 },
260 { &gpt_uuid_ms_recovery, G_PART_ALIAS_MS_RECOVERY, 0 },
261 { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 },
262 { &gpt_uuid_ms_spaces, G_PART_ALIAS_MS_SPACES, 0 },
263 { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 },
264 { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 },
265 { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 },
266 { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 },
267 { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 },
268 { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 },
269 { &gpt_uuid_openbsd_data, G_PART_ALIAS_OPENBSD_DATA, 0 },
270 { &gpt_uuid_prep_boot, G_PART_ALIAS_PREP_BOOT, 0x41 },
271 { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 },
272 { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 },
273 { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 },
274 { &gpt_uuid_vmvsanhdr, G_PART_ALIAS_VMVSANHDR, 0 },
279 gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start,
283 if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX)
286 mbr += DOSPARTOFF + idx * DOSPARTSIZE;
290 * Treat the PMBR partition specially to maximize
291 * interoperability with BIOSes.
296 mbr[1] = mbr[2] = mbr[3] = 0xff;
298 mbr[5] = mbr[6] = mbr[7] = 0xff;
299 le32enc(mbr + 8, (uint32_t)start);
300 le32enc(mbr + 12, (uint32_t)(end - start + 1));
305 gpt_map_type(struct uuid *t)
307 struct g_part_uuid_alias *uap;
309 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
310 if (EQUUID(t, uap->uuid))
311 return (uap->mbrtype);
317 gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp)
320 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
321 gpt_write_mbr_entry(table->mbr, 0, 0xee, 1,
322 MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
323 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
327 * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the
328 * whole disk anymore. Rather, it covers the GPT table and the EFI
329 * system partition only. This way the HFS+ partition and any FAT
330 * partitions can be added to the MBR without creating an overlap.
333 gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname)
337 p = table->mbr + DOSPARTOFF;
338 if (p[4] != 0xee || le32dec(p + 8) != 1)
345 printf("GEOM: %s: enabling Boot Camp\n", provname);
350 gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp)
352 struct g_part_entry *baseentry;
353 struct g_part_gpt_entry *entry;
354 struct g_part_gpt_table *table;
355 int bootable, error, index, slices, typ;
357 table = (struct g_part_gpt_table *)basetable;
360 for (index = 0; index < NDOSPART; index++) {
361 if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index])
365 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
367 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
368 if (baseentry->gpe_deleted)
370 index = baseentry->gpe_index - 1;
371 if (index >= NDOSPART)
374 entry = (struct g_part_gpt_entry *)baseentry;
377 case 0: /* This must be the EFI system partition. */
378 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi))
380 error = gpt_write_mbr_entry(table->mbr, index, 0xee,
381 1ull, entry->ent.ent_lba_end);
383 case 1: /* This must be the HFS+ partition. */
384 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs))
386 error = gpt_write_mbr_entry(table->mbr, index, 0xaf,
387 entry->ent.ent_lba_start, entry->ent.ent_lba_end);
390 typ = gpt_map_type(&entry->ent.ent_type);
391 error = gpt_write_mbr_entry(table->mbr, index, typ,
392 entry->ent.ent_lba_start, entry->ent.ent_lba_end);
398 if (index == bootable)
399 table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80;
400 slices |= 1 << index;
402 if ((slices & 3) == 3)
407 gpt_create_pmbr(table, pp);
410 static struct gpt_hdr *
411 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
414 struct gpt_hdr *buf, *hdr;
415 struct g_provider *pp;
421 last = (pp->mediasize / pp->sectorsize) - 1;
422 table->state[elt] = GPT_STATE_MISSING;
424 * If the primary header is valid look for secondary
425 * header in AlternateLBA, otherwise in the last medium's LBA.
427 if (elt == GPT_ELT_SECHDR) {
428 if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK)
429 table->lba[elt] = last;
432 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
437 if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
440 table->state[elt] = GPT_STATE_CORRUPT;
441 sz = le32toh(buf->hdr_size);
442 if (sz < 92 || sz > pp->sectorsize)
445 hdr = g_malloc(sz, M_WAITOK | M_ZERO);
449 crc = le32toh(buf->hdr_crc_self);
450 buf->hdr_crc_self = 0;
451 if (crc32(buf, sz) != crc)
453 hdr->hdr_crc_self = crc;
455 table->state[elt] = GPT_STATE_INVALID;
456 hdr->hdr_revision = le32toh(buf->hdr_revision);
457 if (hdr->hdr_revision < GPT_HDR_REVISION)
459 hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
460 if (hdr->hdr_lba_self != table->lba[elt])
462 hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
463 if (hdr->hdr_lba_alt == hdr->hdr_lba_self ||
464 hdr->hdr_lba_alt > last)
467 /* Check the managed area. */
468 hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
469 if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
471 hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
472 if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
475 /* Check the table location and size of the table. */
476 hdr->hdr_entries = le32toh(buf->hdr_entries);
477 hdr->hdr_entsz = le32toh(buf->hdr_entsz);
478 if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
479 (hdr->hdr_entsz & 7) != 0)
481 hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
482 if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
484 if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
485 hdr->hdr_lba_table <= hdr->hdr_lba_end)
487 lba = hdr->hdr_lba_table +
488 howmany(hdr->hdr_entries * hdr->hdr_entsz, pp->sectorsize) - 1;
491 if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
494 table->state[elt] = GPT_STATE_OK;
495 le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
496 hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
498 /* save LBA for secondary header */
499 if (elt == GPT_ELT_PRIHDR)
500 table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt;
512 static struct gpt_ent *
513 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
514 enum gpt_elt elt, struct gpt_hdr *hdr)
516 struct g_provider *pp;
517 struct gpt_ent *ent, *tbl;
519 unsigned int idx, sectors, tblsz, size;
526 table->lba[elt] = hdr->hdr_lba_table;
528 table->state[elt] = GPT_STATE_MISSING;
529 tblsz = hdr->hdr_entries * hdr->hdr_entsz;
530 sectors = howmany(tblsz, pp->sectorsize);
531 buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO);
532 for (idx = 0; idx < sectors; idx += MAXPHYS / pp->sectorsize) {
533 size = (sectors - idx > MAXPHYS / pp->sectorsize) ? MAXPHYS:
534 (sectors - idx) * pp->sectorsize;
535 p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize,
541 bcopy(p, buf + idx * pp->sectorsize, size);
544 table->state[elt] = GPT_STATE_CORRUPT;
545 if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
550 table->state[elt] = GPT_STATE_OK;
551 tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
554 for (idx = 0, ent = tbl, p = buf;
555 idx < hdr->hdr_entries;
556 idx++, ent++, p += hdr->hdr_entsz) {
557 le_uuid_dec(p, &ent->ent_type);
558 le_uuid_dec(p + 16, &ent->ent_uuid);
559 ent->ent_lba_start = le64dec(p + 32);
560 ent->ent_lba_end = le64dec(p + 40);
561 ent->ent_attr = le64dec(p + 48);
562 /* Keep UTF-16 in little-endian. */
563 bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
571 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
574 if (pri == NULL || sec == NULL)
577 if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
579 return ((pri->hdr_revision == sec->hdr_revision &&
580 pri->hdr_size == sec->hdr_size &&
581 pri->hdr_lba_start == sec->hdr_lba_start &&
582 pri->hdr_lba_end == sec->hdr_lba_end &&
583 pri->hdr_entries == sec->hdr_entries &&
584 pri->hdr_entsz == sec->hdr_entsz &&
585 pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
589 gpt_parse_type(const char *type, struct uuid *uuid)
594 struct g_part_uuid_alias *uap;
596 if (type[0] == '!') {
597 error = parse_uuid(type + 1, &tmp);
600 if (EQUUID(&tmp, &gpt_uuid_unused))
605 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
606 alias = g_part_alias_name(uap->alias);
607 if (!strcasecmp(type, alias)) {
616 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
617 struct g_part_parms *gpp)
619 struct g_part_gpt_entry *entry;
622 entry = (struct g_part_gpt_entry *)baseentry;
623 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
626 kern_uuidgen(&entry->ent.ent_uuid, 1);
627 entry->ent.ent_lba_start = baseentry->gpe_start;
628 entry->ent.ent_lba_end = baseentry->gpe_end;
629 if (baseentry->gpe_deleted) {
630 entry->ent.ent_attr = 0;
631 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
633 if (gpp->gpp_parms & G_PART_PARM_LABEL)
634 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
635 sizeof(entry->ent.ent_name) /
636 sizeof(entry->ent.ent_name[0]));
641 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
643 struct g_part_gpt_table *table;
647 table = (struct g_part_gpt_table *)basetable;
648 bzero(table->mbr, codesz);
649 codesz = MIN(codesz, gpp->gpp_codesize);
651 bcopy(gpp->gpp_codeptr, table->mbr, codesz);
656 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
658 struct g_provider *pp;
659 struct g_part_gpt_table *table;
662 /* Our depth should be 0 unless nesting was explicitly enabled. */
663 if (!allow_nesting && basetable->gpt_depth != 0)
666 table = (struct g_part_gpt_table *)basetable;
667 pp = gpp->gpp_provider;
668 tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
670 if (pp->sectorsize < MBRSIZE ||
671 pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
675 gpt_create_pmbr(table, pp);
677 /* Allocate space for the header */
678 table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
680 bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
681 table->hdr->hdr_revision = GPT_HDR_REVISION;
682 table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
683 kern_uuidgen(&table->hdr->hdr_uuid, 1);
684 table->hdr->hdr_entries = basetable->gpt_entries;
685 table->hdr->hdr_entsz = sizeof(struct gpt_ent);
687 g_gpt_set_defaults(basetable, pp);
692 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
694 struct g_part_gpt_table *table;
695 struct g_provider *pp;
697 table = (struct g_part_gpt_table *)basetable;
698 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
703 * Wipe the first 2 sectors and last one to clear the partitioning.
704 * Wipe sectors only if they have valid metadata.
706 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK)
707 basetable->gpt_smhead |= 3;
708 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
709 table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1)
710 basetable->gpt_smtail |= 1;
715 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
716 struct sbuf *sb, const char *indent)
718 struct g_part_gpt_entry *entry;
720 entry = (struct g_part_gpt_entry *)baseentry;
721 if (indent == NULL) {
722 /* conftxt: libdisk compatibility */
723 sbuf_cat(sb, " xs GPT xt ");
724 sbuf_printf_uuid(sb, &entry->ent.ent_type);
725 } else if (entry != NULL) {
726 /* confxml: partition entry information */
727 sbuf_printf(sb, "%s<label>", indent);
728 g_gpt_printf_utf16(sb, entry->ent.ent_name,
729 sizeof(entry->ent.ent_name) >> 1);
730 sbuf_cat(sb, "</label>\n");
731 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)
732 sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent);
733 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) {
734 sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n",
737 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) {
738 sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n",
741 sbuf_printf(sb, "%s<rawtype>", indent);
742 sbuf_printf_uuid(sb, &entry->ent.ent_type);
743 sbuf_cat(sb, "</rawtype>\n");
744 sbuf_printf(sb, "%s<rawuuid>", indent);
745 sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
746 sbuf_cat(sb, "</rawuuid>\n");
747 sbuf_printf(sb, "%s<efimedia>", indent);
748 sbuf_printf(sb, "HD(%d,GPT,", entry->base.gpe_index);
749 sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
750 sbuf_printf(sb, ",%#jx,%#jx)", (intmax_t)entry->base.gpe_start,
751 (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1));
752 sbuf_cat(sb, "</efimedia>\n");
754 /* confxml: scheme information */
759 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
761 struct g_part_gpt_entry *entry;
763 entry = (struct g_part_gpt_entry *)baseentry;
764 return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
765 EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) ||
766 EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0);
770 g_part_gpt_modify(struct g_part_table *basetable,
771 struct g_part_entry *baseentry, struct g_part_parms *gpp)
773 struct g_part_gpt_entry *entry;
776 entry = (struct g_part_gpt_entry *)baseentry;
777 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
778 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
782 if (gpp->gpp_parms & G_PART_PARM_LABEL)
783 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
784 sizeof(entry->ent.ent_name) /
785 sizeof(entry->ent.ent_name[0]));
790 g_part_gpt_resize(struct g_part_table *basetable,
791 struct g_part_entry *baseentry, struct g_part_parms *gpp)
793 struct g_part_gpt_entry *entry;
795 if (baseentry == NULL)
796 return (g_part_gpt_recover(basetable));
798 entry = (struct g_part_gpt_entry *)baseentry;
799 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
800 entry->ent.ent_lba_end = baseentry->gpe_end;
806 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
807 char *buf, size_t bufsz)
809 struct g_part_gpt_entry *entry;
812 entry = (struct g_part_gpt_entry *)baseentry;
813 c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
814 snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
819 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
821 struct g_provider *pp;
823 int error, index, pri, res;
825 /* Our depth should be 0 unless nesting was explicitly enabled. */
826 if (!allow_nesting && table->gpt_depth != 0)
832 * Sanity-check the provider. Since the first sector on the provider
833 * must be a PMBR and a PMBR is 512 bytes large, the sector size
834 * must be at least 512 bytes. Also, since the theoretical minimum
835 * number of sectors needed by GPT is 6, any medium that has less
836 * than 6 sectors is never going to be able to hold a GPT. The
837 * number 6 comes from:
838 * 1 sector for the PMBR
839 * 2 sectors for the GPT headers (each 1 sector)
840 * 2 sectors for the GPT tables (each 1 sector)
841 * 1 sector for an actual partition
842 * It's better to catch this pathological case early than behaving
843 * pathologically later on...
845 if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
849 * Check that there's a MBR or a PMBR. If it's a PMBR, we return
850 * as the highest priority on a match, otherwise we assume some
851 * GPT-unaware tool has destroyed the GPT by recreating a MBR and
852 * we really want the MBR scheme to take precedence.
854 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
857 res = le16dec(buf + DOSMAGICOFFSET);
858 pri = G_PART_PROBE_PRI_LOW;
859 if (res == DOSMAGIC) {
860 for (index = 0; index < NDOSPART; index++) {
861 if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee)
862 pri = G_PART_PROBE_PRI_HIGH;
866 /* Check that there's a primary header. */
867 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
870 res = memcmp(buf, GPT_HDR_SIG, 8);
877 /* No primary? Check that there's a secondary. */
878 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
882 res = memcmp(buf, GPT_HDR_SIG, 8);
884 return ((res == 0) ? pri : ENXIO);
888 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
890 struct gpt_hdr *prihdr, *sechdr;
891 struct gpt_ent *tbl, *pritbl, *sectbl;
892 struct g_provider *pp;
893 struct g_part_gpt_table *table;
894 struct g_part_gpt_entry *entry;
899 table = (struct g_part_gpt_table *)basetable;
901 last = (pp->mediasize / pp->sectorsize) - 1;
904 buf = g_read_data(cp, 0, pp->sectorsize, &error);
907 bcopy(buf, table->mbr, MBRSIZE);
910 /* Read the primary header and table. */
911 prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
912 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
913 pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
915 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
919 /* Read the secondary header and table. */
920 sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
921 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
922 sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
924 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
928 /* Fail if we haven't got any good tables at all. */
929 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
930 table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
931 printf("GEOM: %s: corrupt or invalid GPT detected.\n",
933 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
947 * If both headers are good but they disagree with each other,
948 * then invalidate one. We prefer to keep the primary header,
949 * unless the primary table is corrupt.
951 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
952 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
953 !gpt_matched_hdrs(prihdr, sechdr)) {
954 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
955 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
956 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
960 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
961 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
967 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
968 printf("GEOM: %s: the primary GPT table is corrupt or "
969 "invalid.\n", pp->name);
970 printf("GEOM: %s: using the secondary instead -- recovery "
971 "strongly advised.\n", pp->name);
973 basetable->gpt_corrupt = 1;
980 if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
981 printf("GEOM: %s: the secondary GPT table is corrupt "
982 "or invalid.\n", pp->name);
983 printf("GEOM: %s: using the primary only -- recovery "
984 "suggested.\n", pp->name);
985 basetable->gpt_corrupt = 1;
986 } else if (table->lba[GPT_ELT_SECHDR] != last) {
987 printf( "GEOM: %s: the secondary GPT header is not in "
988 "the last LBA.\n", pp->name);
989 basetable->gpt_corrupt = 1;
999 basetable->gpt_first = table->hdr->hdr_lba_start;
1000 basetable->gpt_last = table->hdr->hdr_lba_end;
1001 basetable->gpt_entries = table->hdr->hdr_entries;
1003 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
1004 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
1006 entry = (struct g_part_gpt_entry *)g_part_new_entry(
1007 basetable, index + 1, tbl[index].ent_lba_start,
1008 tbl[index].ent_lba_end);
1009 entry->ent = tbl[index];
1015 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions
1016 * if (and only if) any FAT32 or FAT16 partitions have been
1017 * created. This happens irrespective of whether Boot Camp is
1018 * used/enabled, though it's generally understood to be done
1019 * to support legacy Windows under Boot Camp. We refer to this
1020 * mirroring simply as Boot Camp. We try to detect Boot Camp
1021 * so that we can update the MBR if and when GPT changes have
1022 * been made. Note that we do not enable Boot Camp if not
1023 * previously enabled because we can't assume that we're on a
1024 * Mac alongside Mac OS X.
1026 table->bootcamp = gpt_is_bootcamp(table, pp->name);
1032 g_part_gpt_recover(struct g_part_table *basetable)
1034 struct g_part_gpt_table *table;
1035 struct g_provider *pp;
1037 table = (struct g_part_gpt_table *)basetable;
1038 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1039 gpt_create_pmbr(table, pp);
1040 g_gpt_set_defaults(basetable, pp);
1041 basetable->gpt_corrupt = 0;
1046 g_part_gpt_setunset(struct g_part_table *basetable,
1047 struct g_part_entry *baseentry, const char *attrib, unsigned int set)
1049 struct g_part_gpt_entry *entry;
1050 struct g_part_gpt_table *table;
1051 struct g_provider *pp;
1056 table = (struct g_part_gpt_table *)basetable;
1057 entry = (struct g_part_gpt_entry *)baseentry;
1059 if (strcasecmp(attrib, "active") == 0) {
1060 if (table->bootcamp) {
1061 /* The active flag must be set on a valid entry. */
1064 if (baseentry->gpe_index > NDOSPART)
1066 for (i = 0; i < NDOSPART; i++) {
1067 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1068 p[0] = (i == baseentry->gpe_index - 1)
1069 ? ((set) ? 0x80 : 0) : 0;
1072 /* The PMBR is marked as active without an entry. */
1075 for (i = 0; i < NDOSPART; i++) {
1076 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1077 p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0;
1081 } else if (strcasecmp(attrib, "lenovofix") == 0) {
1083 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR.
1084 * This workaround allows Lenovo X220, T420, T520, etc to boot
1085 * from GPT Partitions in BIOS mode.
1091 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1092 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
1093 gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1,
1094 MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
1102 if (strcasecmp(attrib, "bootme") == 0) {
1103 attr |= GPT_ENT_ATTR_BOOTME;
1104 } else if (strcasecmp(attrib, "bootonce") == 0) {
1105 attr |= GPT_ENT_ATTR_BOOTONCE;
1107 attr |= GPT_ENT_ATTR_BOOTME;
1108 } else if (strcasecmp(attrib, "bootfailed") == 0) {
1110 * It should only be possible to unset BOOTFAILED, but it might
1111 * be useful for test purposes to also be able to set it.
1113 attr |= GPT_ENT_ATTR_BOOTFAILED;
1119 attr = entry->ent.ent_attr | attr;
1121 attr = entry->ent.ent_attr & ~attr;
1122 if (attr != entry->ent.ent_attr) {
1123 entry->ent.ent_attr = attr;
1124 if (!baseentry->gpe_created)
1125 baseentry->gpe_modified = 1;
1131 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
1132 char *buf, size_t bufsz)
1134 struct g_part_gpt_entry *entry;
1136 struct g_part_uuid_alias *uap;
1138 entry = (struct g_part_gpt_entry *)baseentry;
1139 type = &entry->ent.ent_type;
1140 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
1141 if (EQUUID(type, uap->uuid))
1142 return (g_part_alias_name(uap->alias));
1144 snprintf_uuid(buf + 1, bufsz - 1, type);
1150 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
1152 unsigned char *buf, *bp;
1153 struct g_provider *pp;
1154 struct g_part_entry *baseentry;
1155 struct g_part_gpt_entry *entry;
1156 struct g_part_gpt_table *table;
1162 table = (struct g_part_gpt_table *)basetable;
1163 tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz,
1166 /* Reconstruct the MBR from the GPT if under Boot Camp. */
1167 if (table->bootcamp)
1168 gpt_update_bootcamp(basetable, pp);
1170 /* Write the PMBR */
1171 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1172 bcopy(table->mbr, buf, MBRSIZE);
1173 error = g_write_data(cp, 0, buf, pp->sectorsize);
1178 /* Allocate space for the header and entries. */
1179 buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
1181 memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
1182 le32enc(buf + 8, table->hdr->hdr_revision);
1183 le32enc(buf + 12, table->hdr->hdr_size);
1184 le64enc(buf + 40, table->hdr->hdr_lba_start);
1185 le64enc(buf + 48, table->hdr->hdr_lba_end);
1186 le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
1187 le32enc(buf + 80, table->hdr->hdr_entries);
1188 le32enc(buf + 84, table->hdr->hdr_entsz);
1190 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1191 if (baseentry->gpe_deleted)
1193 entry = (struct g_part_gpt_entry *)baseentry;
1194 index = baseentry->gpe_index - 1;
1195 bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
1196 le_uuid_enc(bp, &entry->ent.ent_type);
1197 le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
1198 le64enc(bp + 32, entry->ent.ent_lba_start);
1199 le64enc(bp + 40, entry->ent.ent_lba_end);
1200 le64enc(bp + 48, entry->ent.ent_attr);
1201 memcpy(bp + 56, entry->ent.ent_name,
1202 sizeof(entry->ent.ent_name));
1205 crc = crc32(buf + pp->sectorsize,
1206 table->hdr->hdr_entries * table->hdr->hdr_entsz);
1207 le32enc(buf + 88, crc);
1209 /* Write primary meta-data. */
1210 le32enc(buf + 16, 0); /* hdr_crc_self. */
1211 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */
1212 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */
1213 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */
1214 crc = crc32(buf, table->hdr->hdr_size);
1215 le32enc(buf + 16, crc);
1217 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
1218 error = g_write_data(cp,
1219 (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize,
1220 buf + (index + 1) * pp->sectorsize,
1221 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
1222 (tblsz - index) * pp->sectorsize);
1226 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
1227 buf, pp->sectorsize);
1231 /* Write secondary meta-data. */
1232 le32enc(buf + 16, 0); /* hdr_crc_self. */
1233 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */
1234 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */
1235 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */
1236 crc = crc32(buf, table->hdr->hdr_size);
1237 le32enc(buf + 16, crc);
1239 for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
1240 error = g_write_data(cp,
1241 (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize,
1242 buf + (index + 1) * pp->sectorsize,
1243 (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
1244 (tblsz - index) * pp->sectorsize);
1248 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
1249 buf, pp->sectorsize);
1257 g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp)
1259 struct g_part_entry *baseentry;
1260 struct g_part_gpt_entry *entry;
1261 struct g_part_gpt_table *table;
1262 quad_t start, end, min, max;
1266 table = (struct g_part_gpt_table *)basetable;
1267 last = pp->mediasize / pp->sectorsize - 1;
1268 tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
1271 table->lba[GPT_ELT_PRIHDR] = 1;
1272 table->lba[GPT_ELT_PRITBL] = 2;
1273 table->lba[GPT_ELT_SECHDR] = last;
1274 table->lba[GPT_ELT_SECTBL] = last - tblsz;
1275 table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK;
1276 table->state[GPT_ELT_PRITBL] = GPT_STATE_OK;
1277 table->state[GPT_ELT_SECHDR] = GPT_STATE_OK;
1278 table->state[GPT_ELT_SECTBL] = GPT_STATE_OK;
1280 max = start = 2 + tblsz;
1281 min = end = last - tblsz - 1;
1282 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1283 if (baseentry->gpe_deleted)
1285 entry = (struct g_part_gpt_entry *)baseentry;
1286 if (entry->ent.ent_lba_start < min)
1287 min = entry->ent.ent_lba_start;
1288 if (entry->ent.ent_lba_end > max)
1289 max = entry->ent.ent_lba_end;
1291 spb = 4096 / pp->sectorsize;
1293 lba = start + ((start % spb) ? spb - start % spb : 0);
1296 lba = end - (end + 1) % spb;
1300 table->hdr->hdr_lba_start = start;
1301 table->hdr->hdr_lba_end = end;
1303 basetable->gpt_first = start;
1304 basetable->gpt_last = end;
1308 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
1314 bo = LITTLE_ENDIAN; /* GPT is little-endian */
1315 while (len > 0 && *str != 0) {
1316 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
1318 if ((ch & 0xf800) == 0xd800) {
1320 c = (bo == BIG_ENDIAN) ? be16toh(*str)
1325 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
1326 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
1330 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
1331 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
1333 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
1336 /* Write the Unicode character in UTF-8 */
1338 g_conf_printf_escaped(sb, "%c", ch);
1339 else if (ch < 0x800)
1340 g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6),
1341 0x80 | (ch & 0x3f));
1342 else if (ch < 0x10000)
1343 g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12),
1344 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1345 else if (ch < 0x200000)
1346 g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 |
1347 (ch >> 18), 0x80 | ((ch >> 12) & 0x3f),
1348 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1353 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1355 size_t s16idx, s8idx;
1357 unsigned int c, utfbytes;
1362 bzero(s16, s16len << 1);
1363 while (s8[s8idx] != 0 && s16idx < s16len) {
1365 if ((c & 0xc0) != 0x80) {
1366 /* Initial characters. */
1367 if (utfbytes != 0) {
1368 /* Incomplete encoding of previous char. */
1369 s16[s16idx++] = htole16(0xfffd);
1371 if ((c & 0xf8) == 0xf0) {
1374 } else if ((c & 0xf0) == 0xe0) {
1377 } else if ((c & 0xe0) == 0xc0) {
1385 /* Followup characters. */
1387 utfchar = (utfchar << 6) + (c & 0x3f);
1389 } else if (utfbytes == 0)
1393 * Write the complete Unicode character as UTF-16 when we
1394 * have all the UTF-8 charactars collected.
1396 if (utfbytes == 0) {
1398 * If we need to write 2 UTF-16 characters, but
1399 * we only have room for 1, then we truncate the
1400 * string by writing a 0 instead.
1402 if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1404 htole16(0xd800 | ((utfchar >> 10) - 0x40));
1406 htole16(0xdc00 | (utfchar & 0x3ff));
1408 s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1413 * If our input string was truncated, append an invalid encoding
1414 * character to the output string.
1416 if (utfbytes != 0 && s16idx < s16len)
1417 s16[s16idx++] = htole16(0xfffd);