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/gsb_crc32.h>
36 #include <sys/endian.h>
38 #include <sys/kernel.h>
40 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/queue.h>
46 #include <sys/systm.h>
47 #include <sys/sysctl.h>
49 #include <geom/geom.h>
50 #include <geom/geom_int.h>
51 #include <geom/part/g_part.h>
53 #include "g_part_if.h"
55 FEATURE(geom_part_gpt, "GEOM partitioning class for GPT partitions support");
57 SYSCTL_DECL(_kern_geom_part);
58 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, gpt,
59 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
60 "GEOM_PART_GPT GUID Partition Table");
62 static u_int allow_nesting = 0;
63 SYSCTL_UINT(_kern_geom_part_gpt, OID_AUTO, allow_nesting,
64 CTLFLAG_RWTUN, &allow_nesting, 0, "Allow GPT to be nested inside other schemes");
66 CTASSERT(offsetof(struct gpt_hdr, padding) == 92);
67 CTASSERT(sizeof(struct gpt_ent) == 128);
69 extern u_int geom_part_check_integrity;
71 #define EQUUID(a,b) (memcmp(a, b, sizeof(struct uuid)) == 0)
84 GPT_STATE_UNKNOWN, /* Not determined. */
85 GPT_STATE_MISSING, /* No signature found. */
86 GPT_STATE_CORRUPT, /* Checksum mismatch. */
87 GPT_STATE_INVALID, /* Nonconformant/invalid. */
88 GPT_STATE_OK /* Perfectly fine. */
91 struct g_part_gpt_table {
92 struct g_part_table base;
95 quad_t lba[GPT_ELT_COUNT];
96 enum gpt_state state[GPT_ELT_COUNT];
100 struct g_part_gpt_entry {
101 struct g_part_entry base;
105 static void g_gpt_printf_utf16(struct sbuf *, uint16_t *, size_t);
106 static void g_gpt_utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
107 static void g_gpt_set_defaults(struct g_part_table *, struct g_provider *);
109 static int g_part_gpt_add(struct g_part_table *, struct g_part_entry *,
110 struct g_part_parms *);
111 static int g_part_gpt_bootcode(struct g_part_table *, struct g_part_parms *);
112 static int g_part_gpt_create(struct g_part_table *, struct g_part_parms *);
113 static int g_part_gpt_destroy(struct g_part_table *, struct g_part_parms *);
114 static void g_part_gpt_dumpconf(struct g_part_table *, struct g_part_entry *,
115 struct sbuf *, const char *);
116 static int g_part_gpt_dumpto(struct g_part_table *, struct g_part_entry *);
117 static int g_part_gpt_modify(struct g_part_table *, struct g_part_entry *,
118 struct g_part_parms *);
119 static const char *g_part_gpt_name(struct g_part_table *, struct g_part_entry *,
121 static int g_part_gpt_probe(struct g_part_table *, struct g_consumer *);
122 static int g_part_gpt_read(struct g_part_table *, struct g_consumer *);
123 static int g_part_gpt_setunset(struct g_part_table *table,
124 struct g_part_entry *baseentry, const char *attrib, unsigned int set);
125 static const char *g_part_gpt_type(struct g_part_table *, struct g_part_entry *,
127 static int g_part_gpt_write(struct g_part_table *, struct g_consumer *);
128 static int g_part_gpt_resize(struct g_part_table *, struct g_part_entry *,
129 struct g_part_parms *);
130 static int g_part_gpt_recover(struct g_part_table *);
132 static kobj_method_t g_part_gpt_methods[] = {
133 KOBJMETHOD(g_part_add, g_part_gpt_add),
134 KOBJMETHOD(g_part_bootcode, g_part_gpt_bootcode),
135 KOBJMETHOD(g_part_create, g_part_gpt_create),
136 KOBJMETHOD(g_part_destroy, g_part_gpt_destroy),
137 KOBJMETHOD(g_part_dumpconf, g_part_gpt_dumpconf),
138 KOBJMETHOD(g_part_dumpto, g_part_gpt_dumpto),
139 KOBJMETHOD(g_part_modify, g_part_gpt_modify),
140 KOBJMETHOD(g_part_resize, g_part_gpt_resize),
141 KOBJMETHOD(g_part_name, g_part_gpt_name),
142 KOBJMETHOD(g_part_probe, g_part_gpt_probe),
143 KOBJMETHOD(g_part_read, g_part_gpt_read),
144 KOBJMETHOD(g_part_recover, g_part_gpt_recover),
145 KOBJMETHOD(g_part_setunset, g_part_gpt_setunset),
146 KOBJMETHOD(g_part_type, g_part_gpt_type),
147 KOBJMETHOD(g_part_write, g_part_gpt_write),
151 static struct g_part_scheme g_part_gpt_scheme = {
154 sizeof(struct g_part_gpt_table),
155 .gps_entrysz = sizeof(struct g_part_gpt_entry),
158 .gps_bootcodesz = MBRSIZE,
160 G_PART_SCHEME_DECLARE(g_part_gpt);
161 MODULE_VERSION(geom_part_gpt, 0);
163 static struct uuid gpt_uuid_apple_apfs = GPT_ENT_TYPE_APPLE_APFS;
164 static struct uuid gpt_uuid_apple_boot = GPT_ENT_TYPE_APPLE_BOOT;
165 static struct uuid gpt_uuid_apple_core_storage =
166 GPT_ENT_TYPE_APPLE_CORE_STORAGE;
167 static struct uuid gpt_uuid_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
168 static struct uuid gpt_uuid_apple_label = GPT_ENT_TYPE_APPLE_LABEL;
169 static struct uuid gpt_uuid_apple_raid = GPT_ENT_TYPE_APPLE_RAID;
170 static struct uuid gpt_uuid_apple_raid_offline = GPT_ENT_TYPE_APPLE_RAID_OFFLINE;
171 static struct uuid gpt_uuid_apple_tv_recovery = GPT_ENT_TYPE_APPLE_TV_RECOVERY;
172 static struct uuid gpt_uuid_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
173 static struct uuid gpt_uuid_apple_zfs = GPT_ENT_TYPE_APPLE_ZFS;
174 static struct uuid gpt_uuid_bios_boot = GPT_ENT_TYPE_BIOS_BOOT;
175 static struct uuid gpt_uuid_chromeos_firmware = GPT_ENT_TYPE_CHROMEOS_FIRMWARE;
176 static struct uuid gpt_uuid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL;
177 static struct uuid gpt_uuid_chromeos_reserved = GPT_ENT_TYPE_CHROMEOS_RESERVED;
178 static struct uuid gpt_uuid_chromeos_root = GPT_ENT_TYPE_CHROMEOS_ROOT;
179 static struct uuid gpt_uuid_dfbsd_ccd = GPT_ENT_TYPE_DRAGONFLY_CCD;
180 static struct uuid gpt_uuid_dfbsd_hammer = GPT_ENT_TYPE_DRAGONFLY_HAMMER;
181 static struct uuid gpt_uuid_dfbsd_hammer2 = GPT_ENT_TYPE_DRAGONFLY_HAMMER2;
182 static struct uuid gpt_uuid_dfbsd_label32 = GPT_ENT_TYPE_DRAGONFLY_LABEL32;
183 static struct uuid gpt_uuid_dfbsd_label64 = GPT_ENT_TYPE_DRAGONFLY_LABEL64;
184 static struct uuid gpt_uuid_dfbsd_legacy = GPT_ENT_TYPE_DRAGONFLY_LEGACY;
185 static struct uuid gpt_uuid_dfbsd_swap = GPT_ENT_TYPE_DRAGONFLY_SWAP;
186 static struct uuid gpt_uuid_dfbsd_ufs1 = GPT_ENT_TYPE_DRAGONFLY_UFS1;
187 static struct uuid gpt_uuid_dfbsd_vinum = GPT_ENT_TYPE_DRAGONFLY_VINUM;
188 static struct uuid gpt_uuid_efi = GPT_ENT_TYPE_EFI;
189 static struct uuid gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD;
190 static struct uuid gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
191 static struct uuid gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS;
192 static struct uuid gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
193 static struct uuid gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
194 static struct uuid gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
195 static struct uuid gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
196 static struct uuid gpt_uuid_hifive_fsbl = GPT_ENT_TYPE_HIFIVE_FSBL;
197 static struct uuid gpt_uuid_hifive_bbl = GPT_ENT_TYPE_HIFIVE_BBL;
198 static struct uuid gpt_uuid_linux_data = GPT_ENT_TYPE_LINUX_DATA;
199 static struct uuid gpt_uuid_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
200 static struct uuid gpt_uuid_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
201 static struct uuid gpt_uuid_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
202 static struct uuid gpt_uuid_mbr = GPT_ENT_TYPE_MBR;
203 static struct uuid gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
204 static struct uuid gpt_uuid_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
205 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
206 static struct uuid gpt_uuid_ms_recovery = GPT_ENT_TYPE_MS_RECOVERY;
207 static struct uuid gpt_uuid_ms_reserved = GPT_ENT_TYPE_MS_RESERVED;
208 static struct uuid gpt_uuid_ms_spaces = GPT_ENT_TYPE_MS_SPACES;
209 static struct uuid gpt_uuid_netbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
210 static struct uuid gpt_uuid_netbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
211 static struct uuid gpt_uuid_netbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
212 static struct uuid gpt_uuid_netbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
213 static struct uuid gpt_uuid_netbsd_raid = GPT_ENT_TYPE_NETBSD_RAID;
214 static struct uuid gpt_uuid_netbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
215 static struct uuid gpt_uuid_openbsd_data = GPT_ENT_TYPE_OPENBSD_DATA;
216 static struct uuid gpt_uuid_prep_boot = GPT_ENT_TYPE_PREP_BOOT;
217 static struct uuid gpt_uuid_solaris_boot = GPT_ENT_TYPE_SOLARIS_BOOT;
218 static struct uuid gpt_uuid_solaris_root = GPT_ENT_TYPE_SOLARIS_ROOT;
219 static struct uuid gpt_uuid_solaris_swap = GPT_ENT_TYPE_SOLARIS_SWAP;
220 static struct uuid gpt_uuid_solaris_backup = GPT_ENT_TYPE_SOLARIS_BACKUP;
221 static struct uuid gpt_uuid_solaris_var = GPT_ENT_TYPE_SOLARIS_VAR;
222 static struct uuid gpt_uuid_solaris_home = GPT_ENT_TYPE_SOLARIS_HOME;
223 static struct uuid gpt_uuid_solaris_altsec = GPT_ENT_TYPE_SOLARIS_ALTSEC;
224 static struct uuid gpt_uuid_solaris_reserved = GPT_ENT_TYPE_SOLARIS_RESERVED;
225 static struct uuid gpt_uuid_unused = GPT_ENT_TYPE_UNUSED;
226 static struct uuid gpt_uuid_vmfs = GPT_ENT_TYPE_VMFS;
227 static struct uuid gpt_uuid_vmkdiag = GPT_ENT_TYPE_VMKDIAG;
228 static struct uuid gpt_uuid_vmreserved = GPT_ENT_TYPE_VMRESERVED;
229 static struct uuid gpt_uuid_vmvsanhdr = GPT_ENT_TYPE_VMVSANHDR;
231 static struct g_part_uuid_alias {
235 } gpt_uuid_alias_match[] = {
236 { &gpt_uuid_apple_apfs, G_PART_ALIAS_APPLE_APFS, 0 },
237 { &gpt_uuid_apple_boot, G_PART_ALIAS_APPLE_BOOT, 0xab },
238 { &gpt_uuid_apple_core_storage, G_PART_ALIAS_APPLE_CORE_STORAGE, 0 },
239 { &gpt_uuid_apple_hfs, G_PART_ALIAS_APPLE_HFS, 0xaf },
240 { &gpt_uuid_apple_label, G_PART_ALIAS_APPLE_LABEL, 0 },
241 { &gpt_uuid_apple_raid, G_PART_ALIAS_APPLE_RAID, 0 },
242 { &gpt_uuid_apple_raid_offline, G_PART_ALIAS_APPLE_RAID_OFFLINE, 0 },
243 { &gpt_uuid_apple_tv_recovery, G_PART_ALIAS_APPLE_TV_RECOVERY, 0 },
244 { &gpt_uuid_apple_ufs, G_PART_ALIAS_APPLE_UFS, 0 },
245 { &gpt_uuid_apple_zfs, G_PART_ALIAS_APPLE_ZFS, 0 },
246 { &gpt_uuid_bios_boot, G_PART_ALIAS_BIOS_BOOT, 0 },
247 { &gpt_uuid_chromeos_firmware, G_PART_ALIAS_CHROMEOS_FIRMWARE, 0 },
248 { &gpt_uuid_chromeos_kernel, G_PART_ALIAS_CHROMEOS_KERNEL, 0 },
249 { &gpt_uuid_chromeos_reserved, G_PART_ALIAS_CHROMEOS_RESERVED, 0 },
250 { &gpt_uuid_chromeos_root, G_PART_ALIAS_CHROMEOS_ROOT, 0 },
251 { &gpt_uuid_dfbsd_ccd, G_PART_ALIAS_DFBSD_CCD, 0 },
252 { &gpt_uuid_dfbsd_hammer, G_PART_ALIAS_DFBSD_HAMMER, 0 },
253 { &gpt_uuid_dfbsd_hammer2, G_PART_ALIAS_DFBSD_HAMMER2, 0 },
254 { &gpt_uuid_dfbsd_label32, G_PART_ALIAS_DFBSD, 0xa5 },
255 { &gpt_uuid_dfbsd_label64, G_PART_ALIAS_DFBSD64, 0xa5 },
256 { &gpt_uuid_dfbsd_legacy, G_PART_ALIAS_DFBSD_LEGACY, 0 },
257 { &gpt_uuid_dfbsd_swap, G_PART_ALIAS_DFBSD_SWAP, 0 },
258 { &gpt_uuid_dfbsd_ufs1, G_PART_ALIAS_DFBSD_UFS, 0 },
259 { &gpt_uuid_dfbsd_vinum, G_PART_ALIAS_DFBSD_VINUM, 0 },
260 { &gpt_uuid_efi, G_PART_ALIAS_EFI, 0xee },
261 { &gpt_uuid_freebsd, G_PART_ALIAS_FREEBSD, 0xa5 },
262 { &gpt_uuid_freebsd_boot, G_PART_ALIAS_FREEBSD_BOOT, 0 },
263 { &gpt_uuid_freebsd_nandfs, G_PART_ALIAS_FREEBSD_NANDFS, 0 },
264 { &gpt_uuid_freebsd_swap, G_PART_ALIAS_FREEBSD_SWAP, 0 },
265 { &gpt_uuid_freebsd_ufs, G_PART_ALIAS_FREEBSD_UFS, 0 },
266 { &gpt_uuid_freebsd_vinum, G_PART_ALIAS_FREEBSD_VINUM, 0 },
267 { &gpt_uuid_freebsd_zfs, G_PART_ALIAS_FREEBSD_ZFS, 0 },
268 { &gpt_uuid_hifive_fsbl, G_PART_ALIAS_HIFIVE_FSBL, 0 },
269 { &gpt_uuid_hifive_bbl, G_PART_ALIAS_HIFIVE_BBL, 0 },
270 { &gpt_uuid_linux_data, G_PART_ALIAS_LINUX_DATA, 0x0b },
271 { &gpt_uuid_linux_lvm, G_PART_ALIAS_LINUX_LVM, 0 },
272 { &gpt_uuid_linux_raid, G_PART_ALIAS_LINUX_RAID, 0 },
273 { &gpt_uuid_linux_swap, G_PART_ALIAS_LINUX_SWAP, 0 },
274 { &gpt_uuid_mbr, G_PART_ALIAS_MBR, 0 },
275 { &gpt_uuid_ms_basic_data, G_PART_ALIAS_MS_BASIC_DATA, 0x0b },
276 { &gpt_uuid_ms_ldm_data, G_PART_ALIAS_MS_LDM_DATA, 0 },
277 { &gpt_uuid_ms_ldm_metadata, G_PART_ALIAS_MS_LDM_METADATA, 0 },
278 { &gpt_uuid_ms_recovery, G_PART_ALIAS_MS_RECOVERY, 0 },
279 { &gpt_uuid_ms_reserved, G_PART_ALIAS_MS_RESERVED, 0 },
280 { &gpt_uuid_ms_spaces, G_PART_ALIAS_MS_SPACES, 0 },
281 { &gpt_uuid_netbsd_ccd, G_PART_ALIAS_NETBSD_CCD, 0 },
282 { &gpt_uuid_netbsd_cgd, G_PART_ALIAS_NETBSD_CGD, 0 },
283 { &gpt_uuid_netbsd_ffs, G_PART_ALIAS_NETBSD_FFS, 0 },
284 { &gpt_uuid_netbsd_lfs, G_PART_ALIAS_NETBSD_LFS, 0 },
285 { &gpt_uuid_netbsd_raid, G_PART_ALIAS_NETBSD_RAID, 0 },
286 { &gpt_uuid_netbsd_swap, G_PART_ALIAS_NETBSD_SWAP, 0 },
287 { &gpt_uuid_openbsd_data, G_PART_ALIAS_OPENBSD_DATA, 0 },
288 { &gpt_uuid_prep_boot, G_PART_ALIAS_PREP_BOOT, 0x41 },
289 { &gpt_uuid_solaris_boot, G_PART_ALIAS_SOLARIS_BOOT, 0 },
290 { &gpt_uuid_solaris_root, G_PART_ALIAS_SOLARIS_ROOT, 0 },
291 { &gpt_uuid_solaris_swap, G_PART_ALIAS_SOLARIS_SWAP, 0 },
292 { &gpt_uuid_solaris_backup, G_PART_ALIAS_SOLARIS_BACKUP, 0 },
293 { &gpt_uuid_solaris_var, G_PART_ALIAS_SOLARIS_VAR, 0 },
294 { &gpt_uuid_solaris_home, G_PART_ALIAS_SOLARIS_HOME, 0 },
295 { &gpt_uuid_solaris_altsec, G_PART_ALIAS_SOLARIS_ALTSEC, 0 },
296 { &gpt_uuid_solaris_reserved, G_PART_ALIAS_SOLARIS_RESERVED, 0 },
297 { &gpt_uuid_vmfs, G_PART_ALIAS_VMFS, 0 },
298 { &gpt_uuid_vmkdiag, G_PART_ALIAS_VMKDIAG, 0 },
299 { &gpt_uuid_vmreserved, G_PART_ALIAS_VMRESERVED, 0 },
300 { &gpt_uuid_vmvsanhdr, G_PART_ALIAS_VMVSANHDR, 0 },
305 gpt_write_mbr_entry(u_char *mbr, int idx, int typ, quad_t start,
309 if (typ == 0 || start > UINT32_MAX || end > UINT32_MAX)
312 mbr += DOSPARTOFF + idx * DOSPARTSIZE;
316 * Treat the PMBR partition specially to maximize
317 * interoperability with BIOSes.
322 mbr[1] = mbr[2] = mbr[3] = 0xff;
324 mbr[5] = mbr[6] = mbr[7] = 0xff;
325 le32enc(mbr + 8, (uint32_t)start);
326 le32enc(mbr + 12, (uint32_t)(end - start + 1));
331 gpt_map_type(struct uuid *t)
333 struct g_part_uuid_alias *uap;
335 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
336 if (EQUUID(t, uap->uuid))
337 return (uap->mbrtype);
343 gpt_create_pmbr(struct g_part_gpt_table *table, struct g_provider *pp)
346 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
347 gpt_write_mbr_entry(table->mbr, 0, 0xee, 1,
348 MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
349 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
353 * Under Boot Camp the PMBR partition (type 0xEE) doesn't cover the
354 * whole disk anymore. Rather, it covers the GPT table and the EFI
355 * system partition only. This way the HFS+ partition and any FAT
356 * partitions can be added to the MBR without creating an overlap.
359 gpt_is_bootcamp(struct g_part_gpt_table *table, const char *provname)
363 p = table->mbr + DOSPARTOFF;
364 if (p[4] != 0xee || le32dec(p + 8) != 1)
371 printf("GEOM: %s: enabling Boot Camp\n", provname);
376 gpt_update_bootcamp(struct g_part_table *basetable, struct g_provider *pp)
378 struct g_part_entry *baseentry;
379 struct g_part_gpt_entry *entry;
380 struct g_part_gpt_table *table;
381 int bootable, error, index, slices, typ;
383 table = (struct g_part_gpt_table *)basetable;
386 for (index = 0; index < NDOSPART; index++) {
387 if (table->mbr[DOSPARTOFF + DOSPARTSIZE * index])
391 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
393 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
394 if (baseentry->gpe_deleted)
396 index = baseentry->gpe_index - 1;
397 if (index >= NDOSPART)
400 entry = (struct g_part_gpt_entry *)baseentry;
403 case 0: /* This must be the EFI system partition. */
404 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_efi))
406 error = gpt_write_mbr_entry(table->mbr, index, 0xee,
407 1ull, entry->ent.ent_lba_end);
409 case 1: /* This must be the HFS+ partition. */
410 if (!EQUUID(&entry->ent.ent_type, &gpt_uuid_apple_hfs))
412 error = gpt_write_mbr_entry(table->mbr, index, 0xaf,
413 entry->ent.ent_lba_start, entry->ent.ent_lba_end);
416 typ = gpt_map_type(&entry->ent.ent_type);
417 error = gpt_write_mbr_entry(table->mbr, index, typ,
418 entry->ent.ent_lba_start, entry->ent.ent_lba_end);
424 if (index == bootable)
425 table->mbr[DOSPARTOFF + DOSPARTSIZE * index] = 0x80;
426 slices |= 1 << index;
428 if ((slices & 3) == 3)
433 gpt_create_pmbr(table, pp);
436 static struct gpt_hdr *
437 gpt_read_hdr(struct g_part_gpt_table *table, struct g_consumer *cp,
440 struct gpt_hdr *buf, *hdr;
441 struct g_provider *pp;
447 last = (pp->mediasize / pp->sectorsize) - 1;
448 table->state[elt] = GPT_STATE_MISSING;
450 * If the primary header is valid look for secondary
451 * header in AlternateLBA, otherwise in the last medium's LBA.
453 if (elt == GPT_ELT_SECHDR) {
454 if (table->state[GPT_ELT_PRIHDR] != GPT_STATE_OK)
455 table->lba[elt] = last;
458 buf = g_read_data(cp, table->lba[elt] * pp->sectorsize, pp->sectorsize,
463 if (memcmp(buf->hdr_sig, GPT_HDR_SIG, sizeof(buf->hdr_sig)) != 0)
466 table->state[elt] = GPT_STATE_CORRUPT;
467 sz = le32toh(buf->hdr_size);
468 if (sz < 92 || sz > pp->sectorsize)
471 hdr = g_malloc(sz, M_WAITOK | M_ZERO);
475 crc = le32toh(buf->hdr_crc_self);
476 buf->hdr_crc_self = 0;
477 if (crc32(buf, sz) != crc)
479 hdr->hdr_crc_self = crc;
481 table->state[elt] = GPT_STATE_INVALID;
482 hdr->hdr_revision = le32toh(buf->hdr_revision);
483 if (hdr->hdr_revision < GPT_HDR_REVISION)
485 hdr->hdr_lba_self = le64toh(buf->hdr_lba_self);
486 if (hdr->hdr_lba_self != table->lba[elt])
488 hdr->hdr_lba_alt = le64toh(buf->hdr_lba_alt);
489 if (hdr->hdr_lba_alt == hdr->hdr_lba_self)
491 if (hdr->hdr_lba_alt > last && geom_part_check_integrity)
494 /* Check the managed area. */
495 hdr->hdr_lba_start = le64toh(buf->hdr_lba_start);
496 if (hdr->hdr_lba_start < 2 || hdr->hdr_lba_start >= last)
498 hdr->hdr_lba_end = le64toh(buf->hdr_lba_end);
499 if (hdr->hdr_lba_end < hdr->hdr_lba_start || hdr->hdr_lba_end >= last)
502 /* Check the table location and size of the table. */
503 hdr->hdr_entries = le32toh(buf->hdr_entries);
504 hdr->hdr_entsz = le32toh(buf->hdr_entsz);
505 if (hdr->hdr_entries == 0 || hdr->hdr_entsz < 128 ||
506 (hdr->hdr_entsz & 7) != 0)
508 hdr->hdr_lba_table = le64toh(buf->hdr_lba_table);
509 if (hdr->hdr_lba_table < 2 || hdr->hdr_lba_table >= last)
511 if (hdr->hdr_lba_table >= hdr->hdr_lba_start &&
512 hdr->hdr_lba_table <= hdr->hdr_lba_end)
514 lba = hdr->hdr_lba_table +
515 howmany(hdr->hdr_entries * hdr->hdr_entsz, pp->sectorsize) - 1;
518 if (lba >= hdr->hdr_lba_start && lba <= hdr->hdr_lba_end)
521 table->state[elt] = GPT_STATE_OK;
522 le_uuid_dec(&buf->hdr_uuid, &hdr->hdr_uuid);
523 hdr->hdr_crc_table = le32toh(buf->hdr_crc_table);
525 /* save LBA for secondary header */
526 if (elt == GPT_ELT_PRIHDR)
527 table->lba[GPT_ELT_SECHDR] = hdr->hdr_lba_alt;
539 static struct gpt_ent *
540 gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp,
541 enum gpt_elt elt, struct gpt_hdr *hdr)
543 struct g_provider *pp;
544 struct gpt_ent *ent, *tbl;
546 unsigned int idx, sectors, tblsz, size;
553 table->lba[elt] = hdr->hdr_lba_table;
555 table->state[elt] = GPT_STATE_MISSING;
556 tblsz = hdr->hdr_entries * hdr->hdr_entsz;
557 sectors = howmany(tblsz, pp->sectorsize);
558 buf = g_malloc(sectors * pp->sectorsize, M_WAITOK | M_ZERO);
559 for (idx = 0; idx < sectors; idx += maxphys / pp->sectorsize) {
560 size = (sectors - idx > maxphys / pp->sectorsize) ? maxphys:
561 (sectors - idx) * pp->sectorsize;
562 p = g_read_data(cp, (table->lba[elt] + idx) * pp->sectorsize,
568 bcopy(p, buf + idx * pp->sectorsize, size);
571 table->state[elt] = GPT_STATE_CORRUPT;
572 if (crc32(buf, tblsz) != hdr->hdr_crc_table) {
577 table->state[elt] = GPT_STATE_OK;
578 tbl = g_malloc(hdr->hdr_entries * sizeof(struct gpt_ent),
581 for (idx = 0, ent = tbl, p = buf;
582 idx < hdr->hdr_entries;
583 idx++, ent++, p += hdr->hdr_entsz) {
584 le_uuid_dec(p, &ent->ent_type);
585 le_uuid_dec(p + 16, &ent->ent_uuid);
586 ent->ent_lba_start = le64dec(p + 32);
587 ent->ent_lba_end = le64dec(p + 40);
588 ent->ent_attr = le64dec(p + 48);
589 /* Keep UTF-16 in little-endian. */
590 bcopy(p + 56, ent->ent_name, sizeof(ent->ent_name));
598 gpt_matched_hdrs(struct gpt_hdr *pri, struct gpt_hdr *sec)
601 if (pri == NULL || sec == NULL)
604 if (!EQUUID(&pri->hdr_uuid, &sec->hdr_uuid))
606 return ((pri->hdr_revision == sec->hdr_revision &&
607 pri->hdr_size == sec->hdr_size &&
608 pri->hdr_lba_start == sec->hdr_lba_start &&
609 pri->hdr_lba_end == sec->hdr_lba_end &&
610 pri->hdr_entries == sec->hdr_entries &&
611 pri->hdr_entsz == sec->hdr_entsz &&
612 pri->hdr_crc_table == sec->hdr_crc_table) ? 1 : 0);
616 gpt_parse_type(const char *type, struct uuid *uuid)
621 struct g_part_uuid_alias *uap;
623 if (type[0] == '!') {
624 error = parse_uuid(type + 1, &tmp);
627 if (EQUUID(&tmp, &gpt_uuid_unused))
632 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++) {
633 alias = g_part_alias_name(uap->alias);
634 if (!strcasecmp(type, alias)) {
643 g_part_gpt_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
644 struct g_part_parms *gpp)
646 struct g_part_gpt_entry *entry;
649 entry = (struct g_part_gpt_entry *)baseentry;
650 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
653 kern_uuidgen(&entry->ent.ent_uuid, 1);
654 entry->ent.ent_lba_start = baseentry->gpe_start;
655 entry->ent.ent_lba_end = baseentry->gpe_end;
656 if (baseentry->gpe_deleted) {
657 entry->ent.ent_attr = 0;
658 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
660 if (gpp->gpp_parms & G_PART_PARM_LABEL)
661 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
662 sizeof(entry->ent.ent_name) /
663 sizeof(entry->ent.ent_name[0]));
668 g_part_gpt_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
670 struct g_part_gpt_table *table;
674 table = (struct g_part_gpt_table *)basetable;
675 bzero(table->mbr, codesz);
676 codesz = MIN(codesz, gpp->gpp_codesize);
678 bcopy(gpp->gpp_codeptr, table->mbr, codesz);
683 g_part_gpt_create(struct g_part_table *basetable, struct g_part_parms *gpp)
685 struct g_provider *pp;
686 struct g_part_gpt_table *table;
689 /* Our depth should be 0 unless nesting was explicitly enabled. */
690 if (!allow_nesting && basetable->gpt_depth != 0)
693 table = (struct g_part_gpt_table *)basetable;
694 pp = gpp->gpp_provider;
695 tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
697 if (pp->sectorsize < MBRSIZE ||
698 pp->mediasize < (3 + 2 * tblsz + basetable->gpt_entries) *
702 gpt_create_pmbr(table, pp);
704 /* Allocate space for the header */
705 table->hdr = g_malloc(sizeof(struct gpt_hdr), M_WAITOK | M_ZERO);
707 bcopy(GPT_HDR_SIG, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
708 table->hdr->hdr_revision = GPT_HDR_REVISION;
709 table->hdr->hdr_size = offsetof(struct gpt_hdr, padding);
710 kern_uuidgen(&table->hdr->hdr_uuid, 1);
711 table->hdr->hdr_entries = basetable->gpt_entries;
712 table->hdr->hdr_entsz = sizeof(struct gpt_ent);
714 g_gpt_set_defaults(basetable, pp);
719 g_part_gpt_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
721 struct g_part_gpt_table *table;
722 struct g_provider *pp;
724 table = (struct g_part_gpt_table *)basetable;
725 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
730 * Wipe the first 2 sectors and last one to clear the partitioning.
731 * Wipe sectors only if they have valid metadata.
733 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK)
734 basetable->gpt_smhead |= 3;
735 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
736 table->lba[GPT_ELT_SECHDR] == pp->mediasize / pp->sectorsize - 1)
737 basetable->gpt_smtail |= 1;
742 g_part_gpt_efimedia(struct g_part_gpt_entry *entry, struct sbuf *sb)
744 sbuf_printf(sb, "HD(%d,GPT,", entry->base.gpe_index);
745 sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
746 sbuf_printf(sb, ",%#jx,%#jx)", (intmax_t)entry->base.gpe_start,
747 (intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1));
751 g_part_gpt_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
752 struct sbuf *sb, const char *indent)
754 struct g_part_gpt_entry *entry;
756 entry = (struct g_part_gpt_entry *)baseentry;
757 if (indent == NULL) {
758 /* conftxt: libdisk compatibility */
759 sbuf_cat(sb, " xs GPT xt ");
760 sbuf_printf_uuid(sb, &entry->ent.ent_type);
761 } else if (entry != NULL) {
762 /* confxml: partition entry information */
763 sbuf_printf(sb, "%s<label>", indent);
764 g_gpt_printf_utf16(sb, entry->ent.ent_name,
765 sizeof(entry->ent.ent_name) >> 1);
766 sbuf_cat(sb, "</label>\n");
767 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTME)
768 sbuf_printf(sb, "%s<attrib>bootme</attrib>\n", indent);
769 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTONCE) {
770 sbuf_printf(sb, "%s<attrib>bootonce</attrib>\n",
773 if (entry->ent.ent_attr & GPT_ENT_ATTR_BOOTFAILED) {
774 sbuf_printf(sb, "%s<attrib>bootfailed</attrib>\n",
777 sbuf_printf(sb, "%s<rawtype>", indent);
778 sbuf_printf_uuid(sb, &entry->ent.ent_type);
779 sbuf_cat(sb, "</rawtype>\n");
780 sbuf_printf(sb, "%s<rawuuid>", indent);
781 sbuf_printf_uuid(sb, &entry->ent.ent_uuid);
782 sbuf_cat(sb, "</rawuuid>\n");
783 sbuf_printf(sb, "%s<efimedia>", indent);
784 g_part_gpt_efimedia(entry, sb);
785 sbuf_cat(sb, "</efimedia>\n");
787 /* confxml: scheme information */
792 g_part_gpt_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
794 struct g_part_gpt_entry *entry;
796 entry = (struct g_part_gpt_entry *)baseentry;
797 return ((EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd_swap) ||
798 EQUUID(&entry->ent.ent_type, &gpt_uuid_linux_swap) ||
799 EQUUID(&entry->ent.ent_type, &gpt_uuid_dfbsd_swap)) ? 1 : 0);
803 g_part_gpt_modify(struct g_part_table *basetable,
804 struct g_part_entry *baseentry, struct g_part_parms *gpp)
806 struct g_part_gpt_entry *entry;
809 entry = (struct g_part_gpt_entry *)baseentry;
810 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
811 error = gpt_parse_type(gpp->gpp_type, &entry->ent.ent_type);
815 if (gpp->gpp_parms & G_PART_PARM_LABEL)
816 g_gpt_utf8_to_utf16(gpp->gpp_label, entry->ent.ent_name,
817 sizeof(entry->ent.ent_name) /
818 sizeof(entry->ent.ent_name[0]));
823 g_part_gpt_resize(struct g_part_table *basetable,
824 struct g_part_entry *baseentry, struct g_part_parms *gpp)
826 struct g_part_gpt_entry *entry;
828 if (baseentry == NULL)
829 return (g_part_gpt_recover(basetable));
831 entry = (struct g_part_gpt_entry *)baseentry;
832 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
833 entry->ent.ent_lba_end = baseentry->gpe_end;
839 g_part_gpt_name(struct g_part_table *table, struct g_part_entry *baseentry,
840 char *buf, size_t bufsz)
842 struct g_part_gpt_entry *entry;
845 entry = (struct g_part_gpt_entry *)baseentry;
846 c = (EQUUID(&entry->ent.ent_type, &gpt_uuid_freebsd)) ? 's' : 'p';
847 snprintf(buf, bufsz, "%c%d", c, baseentry->gpe_index);
852 g_part_gpt_probe(struct g_part_table *table, struct g_consumer *cp)
854 struct g_provider *pp;
856 int error, index, pri, res;
858 /* Our depth should be 0 unless nesting was explicitly enabled. */
859 if (!allow_nesting && table->gpt_depth != 0)
865 * Sanity-check the provider. Since the first sector on the provider
866 * must be a PMBR and a PMBR is 512 bytes large, the sector size
867 * must be at least 512 bytes. Also, since the theoretical minimum
868 * number of sectors needed by GPT is 6, any medium that has less
869 * than 6 sectors is never going to be able to hold a GPT. The
870 * number 6 comes from:
871 * 1 sector for the PMBR
872 * 2 sectors for the GPT headers (each 1 sector)
873 * 2 sectors for the GPT tables (each 1 sector)
874 * 1 sector for an actual partition
875 * It's better to catch this pathological case early than behaving
876 * pathologically later on...
878 if (pp->sectorsize < MBRSIZE || pp->mediasize < 6 * pp->sectorsize)
882 * Check that there's a MBR or a PMBR. If it's a PMBR, we return
883 * as the highest priority on a match, otherwise we assume some
884 * GPT-unaware tool has destroyed the GPT by recreating a MBR and
885 * we really want the MBR scheme to take precedence.
887 buf = g_read_data(cp, 0L, pp->sectorsize, &error);
890 res = le16dec(buf + DOSMAGICOFFSET);
891 pri = G_PART_PROBE_PRI_LOW;
892 if (res == DOSMAGIC) {
893 for (index = 0; index < NDOSPART; index++) {
894 if (buf[DOSPARTOFF + DOSPARTSIZE * index + 4] == 0xee)
895 pri = G_PART_PROBE_PRI_HIGH;
899 /* Check that there's a primary header. */
900 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
903 res = memcmp(buf, GPT_HDR_SIG, 8);
910 /* No primary? Check that there's a secondary. */
911 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
915 res = memcmp(buf, GPT_HDR_SIG, 8);
917 return ((res == 0) ? pri : ENXIO);
921 g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp)
923 struct gpt_hdr *prihdr, *sechdr;
924 struct gpt_ent *tbl, *pritbl, *sectbl;
925 struct g_provider *pp;
926 struct g_part_gpt_table *table;
927 struct g_part_gpt_entry *entry;
932 table = (struct g_part_gpt_table *)basetable;
934 last = (pp->mediasize / pp->sectorsize) - 1;
937 buf = g_read_data(cp, 0, pp->sectorsize, &error);
940 bcopy(buf, table->mbr, MBRSIZE);
943 /* Read the primary header and table. */
944 prihdr = gpt_read_hdr(table, cp, GPT_ELT_PRIHDR);
945 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK) {
946 pritbl = gpt_read_tbl(table, cp, GPT_ELT_PRITBL, prihdr);
948 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
952 /* Read the secondary header and table. */
953 sechdr = gpt_read_hdr(table, cp, GPT_ELT_SECHDR);
954 if (table->state[GPT_ELT_SECHDR] == GPT_STATE_OK) {
955 sectbl = gpt_read_tbl(table, cp, GPT_ELT_SECTBL, sechdr);
957 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
961 /* Fail if we haven't got any good tables at all. */
962 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK &&
963 table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
964 printf("GEOM: %s: corrupt or invalid GPT detected.\n",
966 printf("GEOM: %s: GPT rejected -- may not be recoverable.\n",
980 * If both headers are good but they disagree with each other,
981 * then invalidate one. We prefer to keep the primary header,
982 * unless the primary table is corrupt.
984 if (table->state[GPT_ELT_PRIHDR] == GPT_STATE_OK &&
985 table->state[GPT_ELT_SECHDR] == GPT_STATE_OK &&
986 !gpt_matched_hdrs(prihdr, sechdr)) {
987 if (table->state[GPT_ELT_PRITBL] == GPT_STATE_OK) {
988 table->state[GPT_ELT_SECHDR] = GPT_STATE_INVALID;
989 table->state[GPT_ELT_SECTBL] = GPT_STATE_MISSING;
993 table->state[GPT_ELT_PRIHDR] = GPT_STATE_INVALID;
994 table->state[GPT_ELT_PRITBL] = GPT_STATE_MISSING;
1000 if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK) {
1001 printf("GEOM: %s: the primary GPT table is corrupt or "
1002 "invalid.\n", pp->name);
1003 printf("GEOM: %s: using the secondary instead -- recovery "
1004 "strongly advised.\n", pp->name);
1005 table->hdr = sechdr;
1006 basetable->gpt_corrupt = 1;
1013 if (table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) {
1014 printf("GEOM: %s: the secondary GPT table is corrupt "
1015 "or invalid.\n", pp->name);
1016 printf("GEOM: %s: using the primary only -- recovery "
1017 "suggested.\n", pp->name);
1018 basetable->gpt_corrupt = 1;
1019 } else if (table->lba[GPT_ELT_SECHDR] != last) {
1020 printf( "GEOM: %s: the secondary GPT header is not in "
1021 "the last LBA.\n", pp->name);
1022 basetable->gpt_corrupt = 1;
1024 table->hdr = prihdr;
1032 basetable->gpt_first = table->hdr->hdr_lba_start;
1033 basetable->gpt_last = table->hdr->hdr_lba_end;
1034 basetable->gpt_entries = table->hdr->hdr_entries;
1036 for (index = basetable->gpt_entries - 1; index >= 0; index--) {
1037 if (EQUUID(&tbl[index].ent_type, &gpt_uuid_unused))
1039 entry = (struct g_part_gpt_entry *)g_part_new_entry(
1040 basetable, index + 1, tbl[index].ent_lba_start,
1041 tbl[index].ent_lba_end);
1042 entry->ent = tbl[index];
1048 * Under Mac OS X, the MBR mirrors the first 4 GPT partitions
1049 * if (and only if) any FAT32 or FAT16 partitions have been
1050 * created. This happens irrespective of whether Boot Camp is
1051 * used/enabled, though it's generally understood to be done
1052 * to support legacy Windows under Boot Camp. We refer to this
1053 * mirroring simply as Boot Camp. We try to detect Boot Camp
1054 * so that we can update the MBR if and when GPT changes have
1055 * been made. Note that we do not enable Boot Camp if not
1056 * previously enabled because we can't assume that we're on a
1057 * Mac alongside Mac OS X.
1059 table->bootcamp = gpt_is_bootcamp(table, pp->name);
1065 g_part_gpt_recover(struct g_part_table *basetable)
1067 struct g_part_gpt_table *table;
1068 struct g_provider *pp;
1070 table = (struct g_part_gpt_table *)basetable;
1071 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1072 gpt_create_pmbr(table, pp);
1073 g_gpt_set_defaults(basetable, pp);
1074 basetable->gpt_corrupt = 0;
1079 g_part_gpt_setunset(struct g_part_table *basetable,
1080 struct g_part_entry *baseentry, const char *attrib, unsigned int set)
1082 struct g_part_gpt_entry *entry;
1083 struct g_part_gpt_table *table;
1084 struct g_provider *pp;
1089 table = (struct g_part_gpt_table *)basetable;
1090 entry = (struct g_part_gpt_entry *)baseentry;
1092 if (strcasecmp(attrib, "active") == 0) {
1093 if (table->bootcamp) {
1094 /* The active flag must be set on a valid entry. */
1097 if (baseentry->gpe_index > NDOSPART)
1099 for (i = 0; i < NDOSPART; i++) {
1100 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1101 p[0] = (i == baseentry->gpe_index - 1)
1102 ? ((set) ? 0x80 : 0) : 0;
1105 /* The PMBR is marked as active without an entry. */
1108 for (i = 0; i < NDOSPART; i++) {
1109 p = &table->mbr[DOSPARTOFF + i * DOSPARTSIZE];
1110 p[0] = (p[4] == 0xee) ? ((set) ? 0x80 : 0) : 0;
1114 } else if (strcasecmp(attrib, "lenovofix") == 0) {
1116 * Write the 0xee GPT entry to slot #1 (2nd slot) in the pMBR.
1117 * This workaround allows Lenovo X220, T420, T520, etc to boot
1118 * from GPT Partitions in BIOS mode.
1124 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1125 bzero(table->mbr + DOSPARTOFF, DOSPARTSIZE * NDOSPART);
1126 gpt_write_mbr_entry(table->mbr, ((set) ? 1 : 0), 0xee, 1,
1127 MIN(pp->mediasize / pp->sectorsize - 1, UINT32_MAX));
1135 if (strcasecmp(attrib, "bootme") == 0) {
1136 attr |= GPT_ENT_ATTR_BOOTME;
1137 } else if (strcasecmp(attrib, "bootonce") == 0) {
1138 attr |= GPT_ENT_ATTR_BOOTONCE;
1140 attr |= GPT_ENT_ATTR_BOOTME;
1141 } else if (strcasecmp(attrib, "bootfailed") == 0) {
1143 * It should only be possible to unset BOOTFAILED, but it might
1144 * be useful for test purposes to also be able to set it.
1146 attr |= GPT_ENT_ATTR_BOOTFAILED;
1152 attr = entry->ent.ent_attr | attr;
1154 attr = entry->ent.ent_attr & ~attr;
1155 if (attr != entry->ent.ent_attr) {
1156 entry->ent.ent_attr = attr;
1157 if (!baseentry->gpe_created)
1158 baseentry->gpe_modified = 1;
1164 g_part_gpt_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
1165 char *buf, size_t bufsz)
1167 struct g_part_gpt_entry *entry;
1169 struct g_part_uuid_alias *uap;
1171 entry = (struct g_part_gpt_entry *)baseentry;
1172 type = &entry->ent.ent_type;
1173 for (uap = &gpt_uuid_alias_match[0]; uap->uuid; uap++)
1174 if (EQUUID(type, uap->uuid))
1175 return (g_part_alias_name(uap->alias));
1177 snprintf_uuid(buf + 1, bufsz - 1, type);
1183 g_part_gpt_write(struct g_part_table *basetable, struct g_consumer *cp)
1185 unsigned char *buf, *bp;
1186 struct g_provider *pp;
1187 struct g_part_entry *baseentry;
1188 struct g_part_gpt_entry *entry;
1189 struct g_part_gpt_table *table;
1195 table = (struct g_part_gpt_table *)basetable;
1196 tblsz = howmany(table->hdr->hdr_entries * table->hdr->hdr_entsz,
1199 /* Reconstruct the MBR from the GPT if under Boot Camp. */
1200 if (table->bootcamp)
1201 gpt_update_bootcamp(basetable, pp);
1203 /* Write the PMBR */
1204 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1205 bcopy(table->mbr, buf, MBRSIZE);
1206 error = g_write_data(cp, 0, buf, pp->sectorsize);
1211 /* Allocate space for the header and entries. */
1212 buf = g_malloc((tblsz + 1) * pp->sectorsize, M_WAITOK | M_ZERO);
1214 memcpy(buf, table->hdr->hdr_sig, sizeof(table->hdr->hdr_sig));
1215 le32enc(buf + 8, table->hdr->hdr_revision);
1216 le32enc(buf + 12, table->hdr->hdr_size);
1217 le64enc(buf + 40, table->hdr->hdr_lba_start);
1218 le64enc(buf + 48, table->hdr->hdr_lba_end);
1219 le_uuid_enc(buf + 56, &table->hdr->hdr_uuid);
1220 le32enc(buf + 80, table->hdr->hdr_entries);
1221 le32enc(buf + 84, table->hdr->hdr_entsz);
1223 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1224 if (baseentry->gpe_deleted)
1226 entry = (struct g_part_gpt_entry *)baseentry;
1227 index = baseentry->gpe_index - 1;
1228 bp = buf + pp->sectorsize + table->hdr->hdr_entsz * index;
1229 le_uuid_enc(bp, &entry->ent.ent_type);
1230 le_uuid_enc(bp + 16, &entry->ent.ent_uuid);
1231 le64enc(bp + 32, entry->ent.ent_lba_start);
1232 le64enc(bp + 40, entry->ent.ent_lba_end);
1233 le64enc(bp + 48, entry->ent.ent_attr);
1234 memcpy(bp + 56, entry->ent.ent_name,
1235 sizeof(entry->ent.ent_name));
1238 crc = crc32(buf + pp->sectorsize,
1239 table->hdr->hdr_entries * table->hdr->hdr_entsz);
1240 le32enc(buf + 88, crc);
1242 /* Write primary meta-data. */
1243 le32enc(buf + 16, 0); /* hdr_crc_self. */
1244 le64enc(buf + 24, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_self. */
1245 le64enc(buf + 32, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_alt. */
1246 le64enc(buf + 72, table->lba[GPT_ELT_PRITBL]); /* hdr_lba_table. */
1247 crc = crc32(buf, table->hdr->hdr_size);
1248 le32enc(buf + 16, crc);
1250 for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1251 error = g_write_data(cp,
1252 (table->lba[GPT_ELT_PRITBL] + index) * pp->sectorsize,
1253 buf + (index + 1) * pp->sectorsize,
1254 (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1255 (tblsz - index) * pp->sectorsize);
1259 error = g_write_data(cp, table->lba[GPT_ELT_PRIHDR] * pp->sectorsize,
1260 buf, pp->sectorsize);
1264 /* Write secondary meta-data. */
1265 le32enc(buf + 16, 0); /* hdr_crc_self. */
1266 le64enc(buf + 24, table->lba[GPT_ELT_SECHDR]); /* hdr_lba_self. */
1267 le64enc(buf + 32, table->lba[GPT_ELT_PRIHDR]); /* hdr_lba_alt. */
1268 le64enc(buf + 72, table->lba[GPT_ELT_SECTBL]); /* hdr_lba_table. */
1269 crc = crc32(buf, table->hdr->hdr_size);
1270 le32enc(buf + 16, crc);
1272 for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
1273 error = g_write_data(cp,
1274 (table->lba[GPT_ELT_SECTBL] + index) * pp->sectorsize,
1275 buf + (index + 1) * pp->sectorsize,
1276 (tblsz - index > maxphys / pp->sectorsize) ? maxphys :
1277 (tblsz - index) * pp->sectorsize);
1281 error = g_write_data(cp, table->lba[GPT_ELT_SECHDR] * pp->sectorsize,
1282 buf, pp->sectorsize);
1290 g_gpt_set_defaults(struct g_part_table *basetable, struct g_provider *pp)
1292 struct g_part_entry *baseentry;
1293 struct g_part_gpt_entry *entry;
1294 struct g_part_gpt_table *table;
1295 quad_t start, end, min, max;
1299 table = (struct g_part_gpt_table *)basetable;
1300 last = pp->mediasize / pp->sectorsize - 1;
1301 tblsz = howmany(basetable->gpt_entries * sizeof(struct gpt_ent),
1304 table->lba[GPT_ELT_PRIHDR] = 1;
1305 table->lba[GPT_ELT_PRITBL] = 2;
1306 table->lba[GPT_ELT_SECHDR] = last;
1307 table->lba[GPT_ELT_SECTBL] = last - tblsz;
1308 table->state[GPT_ELT_PRIHDR] = GPT_STATE_OK;
1309 table->state[GPT_ELT_PRITBL] = GPT_STATE_OK;
1310 table->state[GPT_ELT_SECHDR] = GPT_STATE_OK;
1311 table->state[GPT_ELT_SECTBL] = GPT_STATE_OK;
1313 max = start = 2 + tblsz;
1314 min = end = last - tblsz - 1;
1315 LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
1316 if (baseentry->gpe_deleted)
1318 entry = (struct g_part_gpt_entry *)baseentry;
1319 if (entry->ent.ent_lba_start < min)
1320 min = entry->ent.ent_lba_start;
1321 if (entry->ent.ent_lba_end > max)
1322 max = entry->ent.ent_lba_end;
1324 spb = 4096 / pp->sectorsize;
1326 lba = start + ((start % spb) ? spb - start % spb : 0);
1329 lba = end - (end + 1) % spb;
1333 table->hdr->hdr_lba_start = start;
1334 table->hdr->hdr_lba_end = end;
1336 basetable->gpt_first = start;
1337 basetable->gpt_last = end;
1341 g_gpt_printf_utf16(struct sbuf *sb, uint16_t *str, size_t len)
1347 bo = LITTLE_ENDIAN; /* GPT is little-endian */
1348 while (len > 0 && *str != 0) {
1349 ch = (bo == BIG_ENDIAN) ? be16toh(*str) : le16toh(*str);
1351 if ((ch & 0xf800) == 0xd800) {
1353 c = (bo == BIG_ENDIAN) ? be16toh(*str)
1358 if ((ch & 0x400) == 0 && (c & 0xfc00) == 0xdc00) {
1359 ch = ((ch & 0x3ff) << 10) + (c & 0x3ff);
1363 } else if (ch == 0xfffe) { /* BOM (U+FEFF) swapped. */
1364 bo = (bo == BIG_ENDIAN) ? LITTLE_ENDIAN : BIG_ENDIAN;
1366 } else if (ch == 0xfeff) /* BOM (U+FEFF) unswapped. */
1369 /* Write the Unicode character in UTF-8 */
1371 g_conf_printf_escaped(sb, "%c", ch);
1372 else if (ch < 0x800)
1373 g_conf_printf_escaped(sb, "%c%c", 0xc0 | (ch >> 6),
1374 0x80 | (ch & 0x3f));
1375 else if (ch < 0x10000)
1376 g_conf_printf_escaped(sb, "%c%c%c", 0xe0 | (ch >> 12),
1377 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1378 else if (ch < 0x200000)
1379 g_conf_printf_escaped(sb, "%c%c%c%c", 0xf0 |
1380 (ch >> 18), 0x80 | ((ch >> 12) & 0x3f),
1381 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
1386 g_gpt_utf8_to_utf16(const uint8_t *s8, uint16_t *s16, size_t s16len)
1388 size_t s16idx, s8idx;
1390 unsigned int c, utfbytes;
1395 bzero(s16, s16len << 1);
1396 while (s8[s8idx] != 0 && s16idx < s16len) {
1398 if ((c & 0xc0) != 0x80) {
1399 /* Initial characters. */
1400 if (utfbytes != 0) {
1401 /* Incomplete encoding of previous char. */
1402 s16[s16idx++] = htole16(0xfffd);
1404 if ((c & 0xf8) == 0xf0) {
1407 } else if ((c & 0xf0) == 0xe0) {
1410 } else if ((c & 0xe0) == 0xc0) {
1418 /* Followup characters. */
1420 utfchar = (utfchar << 6) + (c & 0x3f);
1422 } else if (utfbytes == 0)
1426 * Write the complete Unicode character as UTF-16 when we
1427 * have all the UTF-8 charactars collected.
1429 if (utfbytes == 0) {
1431 * If we need to write 2 UTF-16 characters, but
1432 * we only have room for 1, then we truncate the
1433 * string by writing a 0 instead.
1435 if (utfchar >= 0x10000 && s16idx < s16len - 1) {
1437 htole16(0xd800 | ((utfchar >> 10) - 0x40));
1439 htole16(0xdc00 | (utfchar & 0x3ff));
1441 s16[s16idx++] = (utfchar >= 0x10000) ? 0 :
1446 * If our input string was truncated, append an invalid encoding
1447 * character to the output string.
1449 if (utfbytes != 0 && s16idx < s16len)
1450 s16[s16idx++] = htole16(0xfffd);