xref: /openbsd/sbin/fdisk/part.c (revision 6c9803de)
1 /*	$OpenBSD: part.c,v 1.164 2023/06/19 23:11:57 krw Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Tobias Weingartner
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/disklabel.h>
21 
22 #include <err.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <uuid.h>
28 
29 #include "part.h"
30 #include "disk.h"
31 #include "misc.h"
32 #include "gpt.h"
33 
34 struct mbr_type {
35 	int	 mt_type;
36 	char	*mt_desc;
37 };
38 
39 /*
40 * MBR type sources:
41 *	OpenBSD Historical usage
42 * 	https://en.wikipedia.org/wiki/Partition_type#List_of_partition_IDs
43 *	https://www.win.tue.nl/~aeb/partitions/partition_types-1.html
44 */
45 const struct mbr_type		mbr_types[] = {
46 	{ 0x00, NULL	},   /* unused */
47 	{ 0x01, NULL	},   /* Primary DOS with 12 bit FAT */
48 	{ 0x02, NULL	},   /* XENIX / filesystem */
49 	{ 0x03, NULL	},   /* XENIX /usr filesystem */
50 	{ 0x04, NULL	},   /* Primary DOS with 16 bit FAT */
51 	{ 0x05, NULL	},   /* Extended DOS */
52 	{ 0x06, NULL	},   /* Primary 'big' DOS (> 32MB) */
53 	{ 0x07, NULL	},   /* NTFS */
54 	{ 0x08, NULL	},   /* AIX filesystem */
55 	{ 0x09, NULL	},   /* AIX boot partition or Coherent */
56 	{ 0x0A, NULL	},   /* OS/2 Boot Manager or OPUS */
57 	{ 0x0B, NULL	},   /* Primary Win95 w/ 32-bit FAT */
58 	{ 0x0C, NULL	},   /* Primary Win95 w/ 32-bit FAT LBA-mapped */
59 	{ 0x0E, NULL	},   /* Primary DOS w/ 16-bit FAT, CHS-mapped */
60 	{ 0x0F, NULL	},   /* Extended DOS LBA-mapped */
61 	{ 0x10, NULL	},   /* OPUS */
62 	{ 0x11, NULL	},   /* OS/2 BM: hidden DOS 12-bit FAT */
63 	{ 0x12, NULL	},   /* Compaq Diagnostics */
64 	{ 0x14, NULL	},   /* OS/2 BM: hidden DOS 16-bit FAT <32M or Novell DOS 7.0 bug */
65 	{ 0x16, NULL	},   /* OS/2 BM: hidden DOS 16-bit FAT >=32M */
66 	{ 0x17, NULL	},   /* OS/2 BM: hidden IFS */
67 	{ 0x18, NULL	},   /* AST Windows swapfile */
68 	{ 0x19, NULL	},   /* Willowtech Photon coS */
69 	{ 0x1C, NULL	},   /* IBM ThinkPad recovery partition */
70 	{ 0x20, NULL	},   /* Willowsoft OFS1 */
71 	{ 0x24, NULL	},   /* NEC DOS */
72 	{ 0x27, NULL	},   /* Windows hidden Recovery Partition */
73 	{ 0x38, NULL	},   /* Theos */
74 	{ 0x39, NULL	},   /* Plan 9 */
75 	{ 0x40, NULL	},   /* VENIX 286 or LynxOS */
76 	{ 0x41, NULL 	},   /* Linux/MINIX (sharing disk with DRDOS) or Personal RISC boot */
77 	{ 0x42, NULL 	},   /* SFS or Linux swap (sharing disk with DRDOS) */
78 	{ 0x43, NULL 	},   /* Linux native (sharing disk with DRDOS) */
79 	{ 0x4D, NULL	},   /* QNX 4.2 Primary */
80 	{ 0x4E, NULL	},   /* QNX 4.2 Secondary */
81 	{ 0x4F, NULL	},   /* QNX 4.2 Tertiary */
82 	{ 0x50, NULL	},   /* DM (disk manager) */
83 	{ 0x51, NULL	},   /* DM6 Aux1 (or Novell) */
84 	{ 0x52, NULL	},   /* CP/M or Microport SysV/AT */
85 	{ 0x53, NULL	},   /* DM6 Aux3 */
86 	{ 0x54, NULL	},   /* Ontrack */
87 	{ 0x55, NULL	},   /* EZ-Drive (disk manager) */
88 	{ 0x56, NULL	},   /* Golden Bow (disk manager) */
89 	{ 0x5C, NULL	},   /* Priam Edisk (disk manager) */
90 	{ 0x61, NULL	},   /* SpeedStor */
91 	{ 0x63, NULL	},   /* ISC, System V/386, GNU HURD or Mach */
92 	{ 0x64, NULL	},   /* Novell NetWare 2.xx */
93 	{ 0x65, NULL	},   /* Novell NetWare 3.xx */
94 	{ 0x66, NULL	},   /* Novell 386 NetWare */
95 	{ 0x67, NULL	},   /* Novell */
96 	{ 0x68, NULL	},   /* Novell */
97 	{ 0x69, NULL	},   /* Novell */
98 	{ 0x70, NULL	},   /* DiskSecure Multi-Boot */
99 	{ 0x75, NULL	},   /* PCIX */
100 	{ 0x80, NULL	},   /* Minix 1.1 ... 1.4a */
101 	{ 0x81, NULL	},   /* Minix 1.4b ... 1.5.10 */
102 	{ 0x82, NULL	},   /* Linux swap */
103 	{ 0x83, NULL	},   /* Linux filesystem */
104 	{ 0x84, NULL	},   /* OS/2 hidden C: drive */
105 	{ 0x85, NULL	},   /* Linux extended */
106 	{ 0x86, NULL	},   /* NT FAT volume set */
107 	{ 0x87, NULL	},   /* NTFS volume set or HPFS mirrored */
108 	{ 0x8E, NULL	},   /* Linux LVM */
109 	{ 0x93, NULL	},   /* Amoeba filesystem */
110 	{ 0x94, NULL	},   /* Amoeba bad block table */
111 	{ 0x99, NULL	},   /* Mylex EISA SCSI */
112 	{ 0x9F, NULL	},   /* BSDI BSD/OS */
113 	{ 0xA0, NULL	},   /* Phoenix NoteBIOS save-to-disk */
114 	{ 0xA5, NULL	},   /* FreeBSD */
115 	{ 0xA6, NULL	},   /* OpenBSD */
116 	{ 0xA7, NULL	},   /* NEXTSTEP */
117 	{ 0xA8, NULL	},   /* MacOS X main partition */
118 	{ 0xA9, NULL	},   /* NetBSD */
119 	{ 0xAB, NULL	},   /* MacOS X boot partition */
120 	{ 0xAF, NULL	},   /* MacOS X HFS+ partition */
121 	{ 0xB7, NULL	},   /* BSDI BSD/386 filesystem */
122 	{ 0xB8, NULL	},   /* BSDI BSD/386 swap */
123 	{ 0xBF, NULL	},   /* Solaris */
124 	{ 0xC0, NULL	},   /* CTOS */
125 	{ 0xC1, NULL	},   /* DRDOS/sec (FAT-12) */
126 	{ 0xC4, NULL	},   /* DRDOS/sec (FAT-16, < 32M) */
127 	{ 0xC6, NULL	},   /* DRDOS/sec (FAT-16, >= 32M) */
128 	{ 0xC7, NULL	},   /* Syrinx (Cyrnix?) or HPFS disabled */
129 	{ 0xDB, NULL	},   /* Concurrent CPM or C.DOS or CTOS */
130 	{ 0xDE, NULL	},   /* Dell maintenance partition */
131 	{ 0xE1, NULL	},   /* DOS access or SpeedStor 12-bit FAT extended partition */
132 	{ 0xE3, NULL	},   /* DOS R/O or SpeedStor or Storage Dimensions */
133 	{ 0xE4, NULL	},   /* SpeedStor 16-bit FAT extended partition < 1024 cyl. */
134 	{ 0xEB, NULL	},   /* BeOS for Intel */
135 	{ 0xEE, NULL	},   /* EFI Protective Partition */
136 	{ 0xEF, NULL	},   /* EFI System Partition */
137 	{ 0xF1, NULL	},   /* SpeedStor or Storage Dimensions */
138 	{ 0xF2, NULL	},   /* DOS 3.3+ Secondary */
139 	{ 0xF4, NULL	},   /* SpeedStor >1024 cyl. or LANstep or IBM PS/2 IML */
140 	{ 0xFF, NULL	},   /* Xenix Bad Block Table */
141 };
142 
143 struct gpt_type {
144 	int	 gt_attr;
145 #define	GTATTR_PROTECT		(1 << 0)
146 #define	GTATTR_PROTECT_EFISYS	(1 << 1)
147 	char	*gt_desc;	/* NULL == try menu name (a.k.a. mi_name) */
148 	char	*gt_guid;
149 };
150 
151 /*
152  * GPT GUID sources:
153  *
154  * UEFI: UEFI Specification 2.9, March 2021, Section 5.3.3, Table 5.7
155  * Wikipedia: https://en.wikipedia.org/wiki/GUID_Partition_Table
156  * NetBSD: /usr/src/sys/sys/disklabel_gpt.h
157  * FreeBSD: /usr/src/sys/sys/disk/gpt.h.
158  * DragonFlyBSD: /usr/src/sys/sys/disk/gpt.h.
159  * Systemd:https://uapi-group.org/specifications/specs/discoverable_partitions_specification/
160  *         https://www.freedesktop.org/software/systemd/man/systemd-gpt-auto-generator.html
161  */
162 
163 #define UNUSED_GUID		"00000000-0000-0000-0000-000000000000"
164 #define LEGACY_MBR_GUID		"024dee41-33e7-11d3-9d69-0008c781f39f"
165 #define LINUX_SWAP_GUID		"0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"
166 #define LINUX_FILES_GUID	"0fc63daf-8483-4772-8e79-3d69d8477de4"
167 #define BIOS_BOOT_GUID		"21686148-6449-6e6f-744e-656564454649"
168 #define HIFIVE_BBL_GUID		"2e54b353-1271-4842-806f-e436d6af6985"
169 #define BEOS_I386_GUID		"42465331-3ba3-10f1-802a-4861696b7521"
170 #define MACOS_X_BOOT_GUID	"426f6f74-0000-11aa-aa11-00306543ecac"
171 #define MACOS_X_HFS_GUID	"48465300-0000-11aa-aa11-00306543ecac"
172 #define NETBSD_GUID		"49f48d5a-b10e-11dc-b99b-0019d1879648"
173 #define FREEBSD_GUID		"516e7cb4-6ecf-11d6-8ff8-00022d09712b"
174 #define APFS_RECOVERY_GUID	"52637672-7900-11aa-aa11-00306543ecac"
175 #define MACOS_X_GUID		"55465300-0000-11aa-aa11-00306543ecac"
176 #define HIFIVE_FSBL_GUID	"5b193300-fc78-40cd-8002-e86c45580b47"
177 #define APFS_ISC_GUID		"69646961-6700-11aa-aa11-00306543ecac"
178 #define SOLARIS_GUID		"6a85cf4d-1dd2-11b2-99a6-080020736631"
179 #define APFS_GUID		"7c3457ef-0000-11aa-aa11-00306543ecac"
180 #define OPENBSD_GUID		"824cc7a0-36a8-11e3-890a-952519ad3f61"
181 #define LINUXSWAP_DR_GUID	"af9b60a0-1431-4f62-bc68-3311714a69ad"
182 #define EFI_SYSTEM_PARTITION_GUID "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"
183 #define WIN_RECOVERY_GUID	"de94bba4-06d1-4d40-a16a-bfd50179d6ac"
184 #define LINUX_LVM_GUID		"e6d6d379-f507-44c2-a23c-238f2a3df928"
185 #define MICROSOFT_BASIC_DATA_GUID "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"
186 #define CHROME_KERNEL_GUID	"fe3a2a5d-4f32-41a7-b725-accc3285a309"
187 #if 0
188 #define	CEPH_MULTIPATH_BLOCK_LOG_GUID	"01b41e1b-002a-453c-9f17-88793989ff8f"
189 #define	MIDNIGHTBSD_UFS_GUID		"0394ef8b-237e-11e1-b4b3-e89a8f7fc3a7"
190 #define	FUCHSIA_L_DATA_GUID		"08185f0c-892d-428a-a789-dbeec8f55e6a"
191 #define	CHROMEOS_MINIOS_GUID		"09845860-705f-4bb5-b16c-8a8a099caf52"
192 #define	FUCHSIA_RO_FACTORY_BOOT_GUID	"10b8dbaa-d2bf-42a9-98c6-a7c5db3701e7"
193 #define	ANDROID_IA_BOOT2_GUID		"114eaffe-1552-4022-b26e-9b053604cf84"
194 #define	CEPH_DMCRYPT_LUKS_BLOCK_DB_GUID	"166418da-c469-4022-adf4-b30afd37f176"
195 #define	ANDROID_6_0_ARM_EXT_GUID	"193d1ea4-b3ca-11e4-b075-10604b889dcf"
196 #define	ANDROID_6_0_ARM_META_GUID	"19a710a2-b3ca-11e4-b026-10604b889dcf"
197 #define	FUCHSIA_L_MISC_GUID		"1d75395d-f2c6-476b-a8b7-45cc1c97b476"
198 #define	ANDROID_IA_METADATA_GUID	"20ac26be-20b7-11e3-84c5-6cfdb94711e9"
199 #define	FUCHSIA_L_ZIRCON_BOOTB_GUID	"23cc04df-c278-4ce7-8471-897d1a4bcdf7"
200 #define	ANDROID_IA_BOOT_GUID		"2568845d-2332-4675-bc39-8fa5a4748d15"
201 #define	FUCHSIA_L_BLOB_GUID		"2967380e-134c-4cbb-b6da-17e7ce1ca45d"
202 #define	LINUX_X86_64_ROOT_VERITY_GUID	"2c7357ed-ebd2-46d9-aec1-23d437ec2bf5"
203 #define	NETBSD_CONCATENATED_GUID	"2db519c4-b10f-11dc-b99b-0019d1879648"
204 #define	NETBSD_ENCRYPTED_GUID		"2db519ec-b10f-11dc-b99b-0019d1879648"
205 #define	CHROMEOS_FUTURE_USE_GUID	"2e0a753d-9e48-43b0-8337-b15192cb1b5e"
206 #define	SOFTRAID_SCRATCH_GUID		"2e313465-19b9-463f-8126-8a7993773801"
207 #define	CEPH_DMCRYPT_BLOCK_LOG_GUID	"306e8683-4fe2-4330-b7c0-00a917c16966"
208 #define	CEPH_BLOCK_DB_GUID		"30cd0809-c2b2-499c-8879-2d6b78529876"
209 #define	WINDOWS_IBM_GPFS_GUID		"37affc90-ef7d-4e96-91c3-2d7ae055b174"
210 #define	COREOS_RESIZABLE_ROOT_GUID	"3884dd41-8582-4404-b9a8-e9b84f2df50e"
211 #define	ANDROID_IA_SYSTEM_GUID		"38f428e6-d326-425d-9140-6e0ea133647c"
212 #define	LINUX_SRV_GUID			"3b8f8425-20e0-4f3b-907f-1a25a76f98e8"
213 #define	CHROMEOS_ROOTFS_GUID		"3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec"
214 #define	U_BOOT_ENVIRONMENT_GUID		"3de21764-95bd-54bd-a5c3-4abe786f38a8"
215 #define	CHROMEOS_HIBERNATE_GUID		"3f0f8318-f146-4e6b-8222-c28c8f02e0d5"
216 #define	ANDROID_IA_RECOVERY_GUID	"4177c722-9e92-4aab-8644-43502bfd5506"
217 #define	FUCHSIA_L_FVM_GUID		"41d0e340-57e3-954e-8c1e-17ecac44cff5"
218 #define	FUCHSIA_VERIFIED_BOOT_META_GUID	"421a8bfc-85d9-4d85-acda-b64eec0133e9"
219 #define	LINUX_IA_64_USR_GUID		"4301d2a6-4e3b-4b2a-bb94-9e0b2c4225ea"
220 #define	LINUX_X86_ROOT_GUID		"44479540-f297-41b2-9af7-d131d5f0458a"
221 #define	CEPH_MULTIPATH_JOURNAL_GUID	"45b0969e-8ae0-4982-bf9d-5a8d867af560"
222 #define	CEPH_DMCRYPT_LUKS_JOURNAL_GUID	"45b0969e-9b03-4f30-b4c6-35865ceff106"
223 #define	CEPH_DMCRYPT_JOURNAL_GUID	"45b0969e-9b03-4f30-b4c6-5ec00ceff106"
224 #define	CEPH_JOURNAL_GUID		"45b0969e-9b03-4f30-b4c6-b4b80ceff106"
225 #define	BAREBOX_BAREBOX_STATE_GUID	"4778ed65-bf42-45fa-9c5b-287a1dc4aab1"
226 #define	FUCHSIA_L_INSTALL_GUID		"48435546-4953-2041-494e-5354414c4c52"
227 #define	ANDROID_IA_BOOT3_GUID		"49a4d17f-93a3-45c1-a0de-f50b2ebe2599"
228 #define	NETBSD_SWAP_GUID		"49f48d32-b10e-11dc-b99b-0019d1879648"
229 #define	NETBSD_LFS_GUID			"49f48d82-b10e-11dc-b99b-0019d1879648"
230 #define	NETBSD_RAID_GUID		"49f48daa-b10e-11dc-b99b-0019d1879648"
231 #define	FUCHSIA_VOLUME_MANAGER_GUID	"49fd7cb8-df15-4e73-b9d9-992070127f0f"
232 #define	MACOS_LABEL_GUID		"4c616265-6c00-11aa-aa11-00306543ecac"
233 #define	LINUX_VAR_GUID			"4d21b016-b534-45c2-a9fb-5c16e091fd2d"
234 #define	FUCHSIA_L_SYS_CFG_GUID		"4e5e989e-4c86-11e8-a15b-480fcf35f8e6"
235 #define	LINUX_X86_64_ROOT_GUID		"4f68bce3-e8cd-4db1-96e7-fbcaf984b709"
236 #define	CEPH_MULTIPATH_OSD_GUID		"4fbd7e29-8ae0-4982-bf9d-5a8d867af560"
237 #define	CEPH_OSD_GUID			"4fbd7e29-9d25-41b8-afd0-062c0ceff05d"
238 #define	CEPH_DMCRYPT_LUKS_OSD_GUID	"4fbd7e29-9d25-41b8-afd0-35865ceff05d"
239 #define	CEPH_DMCRYPT_OSD_GUID		"4fbd7e29-9d25-41b8-afd0-5ec00ceff05d"
240 #define	FREEBSD_SWAP_GUID		"516e7cb5-6ecf-11d6-8ff8-00022d09712b"
241 #define	FREEBSD_UFS_GUID		"516e7cb6-6ecf-11d6-8ff8-00022d09712b"
242 #define	FREEBSD_VINUM_VOL_MGR_GUID	"516e7cb8-6ecf-11d6-8ff8-00022d09712b"
243 #define	FREEBSD_ZFS_GUID		"516e7cba-6ecf-11d6-8ff8-00022d09712b"
244 #define	MACOS_RAID_GUID			"52414944-0000-11aa-aa11-00306543ecac"
245 #define	MACOS_RAID_OFFLINE_GUID		"52414944-5f4f-11aa-aa11-00306543ecac"
246 #define	MACOS_TV_RECOVERY_GUID		"5265636f-7665-11aa-aa11-00306543ecac"
247 #define	MACOS_CORE_STORAGE_GUID		"53746f72-6167-11aa-aa11-00306543ecac"
248 #define	WINDOWS_STORAGE_REPLICA_GUID	"558d43c5-a1ac-43c0-aac8-d1472b2923d1"
249 #define	WINDOWS_LDM_METADATA_GUID	"5808c8aa-7e8f-42e0-85d2-e1e90434cfb3"
250 #define	FUCHSIA_L_FACTORY_CFG_GUID	"5a3a90be-4c86-11e8-a15b-480fcf35f8e6"
251 #define	CEPH_BLOCK_LOG_GUID		"5ce17fce-4087-4169-b7ff-056cc58473f9"
252 #define	COREOS_USR_GUID			"5dfbf5f4-2848-4bac-aa5e-0d9a20b745a6"
253 #define	FUCHSIA_L_BOOT_GUID		"5ece94fe-4c86-11e8-a15b-480fcf35f8e6"
254 #define	FUCHSIA_L_SYSTEM_GUID		"606b000b-b7c7-4653-a7d5-b737332c899d"
255 #define	LINUX_ARM32_ROOT_GUID		"69dad710-2ce4-4e3c-b16c-21a1d49abed3"
256 #define	FUCHSIA_L_VERIFIED_BOOTM_R_GUID	"6a2460c3-cd11-4e8b-80a8-12cce268ed0a"
257 #define	LINUX_IA_64_USR_VERITY_GUID	"6a491e03-3be7-4545-8e38-83320e0ea880"
258 #define	SOLARIS_BOOT_GUID		"6a82cb45-1dd2-11b2-99a6-080020736631"
259 #define	SOLARIS_SWAP_GUID		"6a87c46f-1dd2-11b2-99a6-080020736631"
260 #define	SOLARIS_USR_MACOS_ZFS_GUID	"6a898cc3-1dd2-11b2-99a6-080020736631"
261 #define	SOLARIS_BACKUP_GUID		"6a8b642b-1dd2-11b2-99a6-080020736631"
262 #define	SOLARIS_RESERVED5_GUID		"6a8d2ac7-1dd2-11b2-99a6-080020736631"
263 #define	SOLARIS_VAR_GUID		"6a8ef2e9-1dd2-11b2-99a6-080020736631"
264 #define	SOLARIS_HOME_GUID		"6a90ba39-1dd2-11b2-99a6-080020736631"
265 #define	SOLARIS_ALT_SECTOR_GUID		"6a9283a5-1dd2-11b2-99a6-080020736631"
266 #define	SOLARIS_RESERVED1_GUID		"6a945a3b-1dd2-11b2-99a6-080020736631"
267 #define	SOLARIS_RESERVED4_GUID		"6a96237f-1dd2-11b2-99a6-080020736631"
268 #define	SOLARIS_RESERVED2_GUID		"6a9630d1-1dd2-11b2-99a6-080020736631"
269 #define	SOLARIS_RESERVED3_GUID		"6a980767-1dd2-11b2-99a6-080020736631"
270 #define	LINUX_ARM64_USR_VERITY_GUID	"6e11a4e7-fbca-4ded-b9e9-e1a512bb664e"
271 #define	ATARI_TOS_BASIC_DATA_GUID	"734e5afe-f61a-11e6-bc64-92361f002671"
272 #define	LINUX_ARM32_ROOT_VERITY_GUID	"7386cdf2-203c-47a9-a498-f2ecce45a2d6"
273 #define	ONIE_BOOT_GUID			"7412f7d5-a156-4b13-81dc-867174929325"
274 #define	FREEBSD_NANDFS_GUID		"74ba7dd9-a689-11e1-bd04-00e081286acf"
275 #define	LINUX_X86_USR_GUID		"75250d76-8cc6-458e-bd66-bd47cc81a812"
276 #define	HP_UX_DATA_GUID			"75894c1e-3aeb-11d3-b7c1-7b03a0000000"
277 #define	ANDROID_IA_FASTBOOT_GUID	"767941d0-2085-11e3-ad3b-6cfdb94711e9"
278 #define	LINUX_USER_HOME_GUID		"773f91ef-66d4-49b5-bd83-d683bf40ad16"
279 #define	LINUX_X86_64_USR_VERITY_GUID	"77ff5f63-e7b6-4633-acf4-1565b864c0e6"
280 #define	SPDK_BLOCK_DEVICE_GUID		"7c5222bd-8f5d-4087-9c00-bf9843c7b58c"
281 #define	LINUX_ARM32_USR_GUID		"7d0359a3-02b3-4f0a-865c-654403e70625"
282 #define	LINUX_VAR_TMP_GUID		"7ec6f557-3bc5-4aca-b293-16ef5df639d1"
283 #define	CEPH_MULTIPATH_BLOCK_B_GUID	"7f4a666a-16f3-47a2-8445-152ef4d03f6c"
284 #define	LINUX_PLAIN_DMCRYPT_GUID	"7ffec5c9-2d00-49b7-8941-3ea10a5586b7"
285 #define	FREEBSD_BOOT_GUID		"83bd6b9d-7f41-11dc-be0b-001560b84f0f"
286 #define	LINUX_X86_64_USR_GUID		"8484680c-9521-48c6-9c11-b0720656f69e"
287 #define	MIDNIGHTBSD_DATA_GUID		"85d5e45a-237c-11e1-b4b3-e89a8f7fc3a7"
288 #define	MIDNIGHTBSD_SWAP_GUID		"85d5e45b-237c-11e1-b4b3-e89a8f7fc3a7"
289 #define	MIDNIGHTBSD_VINUM_VOL_MGR_GUID	"85d5e45c-237c-11e1-b4b3-e89a8f7fc3a7"
290 #define	MIDNIGHTBSD_ZFS_GUID		"85d5e45d-237c-11e1-b4b3-e89a8f7fc3a7"
291 #define	MIDNIGHTBSD_BOOT_GUID		"85d5e45e-237c-11e1-b4b3-e89a8f7fc3a7"
292 #define	CEPH_DMCRYPT_LUKS_LOG_GUID	"86a32090-3647-40b9-bbbd-38d8c573aa86"
293 #define	LINUX_IA_64_ROOT_VERITY_GUID	"86ed10d5-b607-45bb-8957-d350f23d0571"
294 #define	CEPH_DMCRYPT_CREATNG_DISK_GUID	"89c57f98-2fe5-4dc0-89c1-5ec00ceff2be"
295 #define	CEPH_DISK_IN_CREATION_GUID	"89c57f98-2fe5-4dc0-89c1-f3ad0ceff2be"
296 #define	FUCHSIA_L_TEST_GUID		"8b94d043-30be-4871-9dfa-d69556e8c1f3"
297 #define	VERACRYPT_ENCRYPTED_DATA_GUID	"8c8f8eff-ac95-4770-814a-21994f2dbc8f"
298 #define	LINUX_RESERVED_GUID		"8da63339-0007-60c0-c436-083ac8230908"
299 #define	LINUX_X86_USR_VERITY_GUID	"8f461b0d-14ee-4e81-9aa9-049b6fb97abd"
300 #define	ANDROID_IA_FACTORY_GUID		"8f68cc74-c5e5-48da-be91-a0c8c15e9c80"
301 #define	FUCHSIA_L_EMMC_BOOT1_GUID	"900b0fc5-90cd-4d4f-84f9-9f8ed579db88"
302 #define	ARCAOS_TYPE_1_GUID		"90b6ff38-b98f-4358-a21f-48f35b4a8ad3"
303 #define	VMWARE_RESERVED_GUID		"9198effc-31c0-11db-8f78-000c2911d1b8"
304 #define	LINUX_HOME_GUID			"933ac7e1-2eb4-4f13-b844-0e14e2aef915"
305 #define	CEPH_DMCRYPT_BLOCK_DB_GUID	"93b0052d-02d9-4d8a-a43b-33a3ee4dfbc3"
306 #define	LINUX_IA_64_ROOT_GUID		"993d8d3d-f80e-4225-855a-9daf8ed7ea97"
307 #define	FUCHSIA_ZIRCON_BOOT_ABR_GUID	"9b37fff6-2e58-466a-983a-f7926d0b04e0"
308 #define	VMWARE_VMKCORE_GUID		"9d275380-40ad-11db-bf97-000c2911d1b8"
309 #define	PPC_PREP_BOOT_GUID		"9e1a2d38-c612-4316-aa26-8b49521e5a8b"
310 #define	ANDROID_IA_FACTORY_ALT_GUID	"9fdaa6ef-4b3f-40d2-ba8d-bff16bfb887b"
311 #define	FUCHSIA_L_ZIRCON_BOOTR_GUID	"a0e5cf57-2def-46be-a80c-a2067c37cd49"
312 #define	FUCHSIA_L_VERIFIED_BOOTM_A_GUID	"a13b4d9a-ec5f-11e8-97d8-6c3be52705bf"
313 #define	LINUX_RAID_GUID			"a19d880f-05fc-4d3b-a006-743f0f84911e"
314 #define	FUCHSIA_L_VERIFIED_BOOTM_B_GUID	"a288abf2-ec5f-11e8-97d8-6c3be52705bf"
315 #define	FUCSHIA_BOOT_METADATA_GUID	"a409e16b-78aa-4acc-995c-302352621a41"
316 #define	ANDROID_IA_CACHE_GUID		"a893ef21-e428-470a-9e55-0668fd91a2d9"
317 #define	VMWARE_VMFS_FILESYSTEM_GUID	"aa31e02a-400f-11db-9590-000c2911d1b8"
318 #define	ANDROID_IA_OEM_GUID		"ac6d7924-eb71-4df8-b48d-e267b27148ff"
319 #define	LINUX_ARM64_USR_GUID		"b0e01050-ee5f-4390-949a-9101b17104e9"
320 #define	FUCHSIA_L_EMMC_BOOT2_GUID	"b2b2e8d1-7c10-4ebc-a2d0-4614568260ad"
321 #define	SOFTRAID_STATUS_GUID		"b6fa30da-92d2-4a9a-96f1-871ec6486200"
322 #define	LINUX_ARM64_ROOT_GUID		"b921b045-1df0-41c3-af44-4c6f280d3fae"
323 #define	SOFTRAID_CACHE_GUID		"bbba6df5-f46f-4a89-8f59-8765b2727503"
324 #define	LINUX_BOOT_GUID			"bc13c2ff-59e6-4262-a352-b275fd6f7172"
325 #define	ANDROID_IA_CFG_GUID		"bd59408b-4514-490d-bf12-9878d963f378"
326 #define	COREOS_ROOT_RAID_GUID		"be9067b9-ea49-4f15-b4f6-f36f8c9e1818"
327 #define	LENOVO_BOOT_GUID		"bfbfafe7-a34f-448a-9a5b-6213eb736c22"
328 #define	LINUX_ARM32_USR_VERITY_GUID	"c215d751-7bcd-4649-be90-6627490a4c05"
329 #define	ANDROID_IA_VENDOR_GUID		"c5a0aeec-13ea-11e5-a1b1-001e67ca0c3c"
330 #define	PLAN9_GUID			"c91818f9-8025-47af-89d2-f030d7000c2c"
331 #define	COREOS_OEM_GUID			"c95dc21a-df0e-4340-8d7b-26cbfa9a03e0"
332 #define	LINUX_LUKS_GUID			"ca7d7ccb-63ed-4c53-861c-1742536059cc"
333 #define	CHROMEOS_FIRMWARE_GUID		"cab6e88e-abf3-4102-a07a-d4bb9be3c1d3"
334 #define	CEPH_MULTIPATH_BLOCK_A_GUID	"cafecafe-8ae0-4982-bf9d-5a8d867af560"
335 #define	CEPH_DMCRYPT_LUKS_BLOCK_GUID	"cafecafe-9b03-4f30-b4c6-35865ceff106"
336 #define	CEPH_DMCRYPT_BLOCK_GUID		"cafecafe-9b03-4f30-b4c6-5ec00ceff106"
337 #define	CEPH_BLOCK_GUID			"cafecafe-9b03-4f30-b4c6-b4b80ceff106"
338 #define	QNX_POWER_SAFE_FS_GUID		"cef5a9ad-73bc-4601-89f3-cdeeeee321a1"
339 #define	LINUX_X86_ROOT_VERITY_GUID	"d13c5d3b-b5d1-422a-b29f-9454fdc89d76"
340 #define	INTEL_FAST_FLASH_GUID		"d3bfe2de-3daf-11df-ba40-e3a556d89593"
341 #define	ONIE_CFG_GUID			"d4e6e2cd-4469-46f3-b5cb-1bff57afc149"
342 #define	FUCHSIA_ENCRYPTED_SYS_GUID	"d9fd4535-106c-4cec-8d37-dfc020ca87cb"
343 #define	ANDROID_IA_DATA_GUID		"dc76dda9-5ac1-491c-af42-a82591580c0d"
344 #define	FUCHSIA_L_ZIRCON_BOOTA_GUID	"de30cc86-1f4a-4a31-93c4-66f147d33e05"
345 #define	LINUX_ARM64_ROOT_VERITY_GUID	"df3300ce-d69f-4c92-978c-9bfb0f38d820"
346 #define	HP_UX_SERVICE_GUID		"e2a1e728-32e3-11d6-a682-7b03a0000000"
347 #define	WINDOWS_RESERVED_GUID		"e3c9e316-0b5c-4db8-817d-f92df00215ae"
348 #define	WINDOWS_STORAGE_SPACES_GUID	"e75caf8f-f680-4cee-afa3-b001e56efc2d"
349 #define	ANDROID_IA_PERSISTENT_GUID	"ebc597d0-2053-4b15-8b64-e0aac75f4db1"
350 #define	CEPH_MULTIPATH_BLOCK_DB_GUID	"ec6d6385-e346-45dc-be91-da2a7c8b3261"
351 #define	ANDROID_IA_MISC_GUID		"ef32a33b-a409-486c-9141-9ffb711f6266"
352 #define	SONY_BOOT_GUID			"f4019732-066e-4e12-8273-346c5641494f"
353 #define	FUCHSIA_RO_FACTORY_SYS_GUID	"f95d940e-caba-4578-9b93-bb6c90f29d3e"
354 #define	SOFTRAID_VOLUME_GUID		"fa709c7e-65b1-4593-bfd5-e71d61de9b02"
355 #define	CEPH_DMCRYPT_KEYS_LOCKBOX_GUID	"fb3aabf9-d25f-47cc-bf5e-721d1816496b"
356 #define	FUCHSIA_BOOT_GUID		"fe8a2634-5e2e-46ba-99e3-3a192091a350"
357 #endif
358 
359 const struct gpt_type		gpt_types[] = {
360 	{ GTATTR_PROTECT,
361 	     NULL,	/* BIOS Boot */		BIOS_BOOT_GUID },
362 	{ GTATTR_PROTECT,
363 	     NULL,	/* HiFive BBL */	HIFIVE_BBL_GUID },
364 	{ GTATTR_PROTECT,
365 	     NULL,	/* HiFive FSBL */	HIFIVE_FSBL_GUID },
366 	{ GTATTR_PROTECT | GTATTR_PROTECT_EFISYS,
367 	     NULL,	/* APFS Recovery */	APFS_RECOVERY_GUID },
368 	{ GTATTR_PROTECT | GTATTR_PROTECT_EFISYS,
369 	     NULL,	/* APFS ISC */		APFS_ISC_GUID },
370 	{ GTATTR_PROTECT | GTATTR_PROTECT_EFISYS,
371 	     NULL,	/* APFS */		APFS_GUID },
372 
373 	{ 0, NULL,	/* Unused */		UNUSED_GUID },
374 	{ 0, NULL,	/* Legacy MBR */	LEGACY_MBR_GUID },
375 	{ 0, NULL,	/* Linux swap */	LINUX_SWAP_GUID },
376 	{ 0, NULL,	/* Linux files* */	LINUX_FILES_GUID },
377 	{ 0, NULL,	/* MacOS X */		MACOS_X_GUID },
378 	{ 0, NULL,	/* Solaris */		SOLARIS_GUID },
379 	{ 0, NULL,	/* BeOS/i386 */		BEOS_I386_GUID },
380 	{ 0, NULL,	/* MacOS X boot */	MACOS_X_BOOT_GUID },
381 	{ 0, NULL,	/* MacOS X HFS+ */	MACOS_X_HFS_GUID },
382 	{ 0, NULL,	/* NetBSD */		NETBSD_GUID },
383 	{ 0, NULL,	/* FreeBSD */		FREEBSD_GUID },
384 	{ 0, NULL,	/* OpenBSD */		OPENBSD_GUID },
385 	{ 0, NULL,	/* LinuxSwap DR */	LINUXSWAP_DR_GUID },
386 	{ 0, NULL,	/* EFI Sys */		EFI_SYSTEM_PARTITION_GUID },
387 	{ 0, NULL,	/* Win Recovery*/	WIN_RECOVERY_GUID },
388 	{ 0, NULL,	/* Linux VM */		LINUX_LVM_GUID },
389 	{ 0, "Microsoft basic data",		MICROSOFT_BASIC_DATA_GUID },
390 	{ 0, NULL,	/* ChromeKernel */	CHROME_KERNEL_GUID },
391 #if 0
392 	/* Sorted as in https://en.wikipedia.org/wiki/GUID_Partition_Table */
393 	{ 0, "Intel Fast Flash (iFFS)",		INTEL_FAST_FLASH_GUID },
394 	{ 0, "Sony boot",			SONY_BOOT_GUID },
395 	{ 0, "Lenovo boot",			LENOVO_BOOT_GUID },
396 
397 	/* Microsoft Windows */
398 	{ 0, "Windows Reserved (MSR)",		WINDOWS_RESERVED_GUID },
399 	{ 0, "Windows LDM metadata",		WINDOWS_LDM_METADATA_GUID },
400 	{ 0, "Windows IBM General Parallel FS",	WINDOWS_IBM_GPFS_GUID },
401 	{ 0, "Windows Storage Spaces",		WINDOWS_STORAGE_SPACES_GUID },
402 	{ 0, "Windows Storage Replica",		WINDOWS_STORAGE_REPLICA_GUID },
403 
404 	/* HP-UX */
405 	{ 0, "HP-UX Data",			HP_UX_DATA_GUID },
406 	{ 0, "HP-UX Service",			HP_UX_SERVICE_GUID },
407 
408 	/* Linux */
409 	{ 0, "Linux RAID",			LINUX_RAID_GUID },
410 	{ 0, "Linux x86 Root",			LINUX_X86_ROOT_GUID },
411 	{ 0, "Linux x86-64 Root",		LINUX_X86_64_ROOT_GUID },
412 	{ 0, "Linux Arm32 Root",		LINUX_ARM32_ROOT_GUID },
413 	{ 0, "Linux Arm64 root",		LINUX_ARM64_ROOT_GUID },
414 	{ 0, "Linux /boot",			LINUX_BOOT_GUID },
415 	{ 0, "Linux /home",			LINUX_HOME_GUID },
416 	{ 0, "Linux /srv (server data)",	LINUX_SRV_GUID },
417 	{ 0, "Linux Plain dm-crypt",		LINUX_PLAIN_DMCRYPT_GUID },
418 	{ 0, "Linux LUKS",			LINUX_LUKS_GUID },
419 	{ 0, "Linux Reserved",			LINUX_RESERVED_GUID },
420 
421 	/* FreeBSD */
422 	{ 0, "FreeBSD Boot",			FREEBSD_BOOT_GUID },
423 	{ 0, "FreeBSD Swap",			FREEBSD_SWAP_GUID },
424 	{ 0, "FreeBSD UFS",			FREEBSD_UFS_GUID },
425 	{ 0, "FreeBSD Vinum volume manager",	FREEBSD_VINUM_VOL_MGR_GUID },
426 	{ 0, "FreeBSD ZFS",			FREEBSD_ZFS_GUID },
427 	{ 0, "FreeBSD nandfs",			FREEBSD_NANDFS_GUID },
428 
429 	/* MacOS/Darwin */
430 	{ 0, "MacOS RAID",			MACOS_RAID_GUID },
431 	{ 0, "MacOS RAID (offline)",		MACOS_RAID_OFFLINE_GUID },
432 	{ 0, "MacOS Label",			MACOS_LABEL_GUID },
433 	{ 0, "MacOS TV Recovery",		MACOS_TV_RECOVERY_GUID },
434 	{ 0, "MacOS Core Storage container",	MACOS_CORE_STORAGE_GUID },
435 
436 	/* Solaris/illumos */
437 	{ 0, "Solaris/illumos boot",		SOLARIS_BOOT_GUID },
438 	{ 0, "Solaris/illumos Swap",		SOLARIS_SWAP_GUID },
439 	{ 0, "Solaris/illumos Backup",		SOLARIS_BACKUP_GUID },
440 	{ 0, "Solaris/illumos /usr|MacOS ZFS",	SOLARIS_USR_MACOS_ZFS_GUID },
441 	{ 0, "Solaris/illumos /var",		SOLARIS_VAR_GUID },
442 	{ 0, "Solaris/illumos /home",		SOLARIS_HOME_GUID },
443 	{ 0, "Solaris/illumos Alt sector",	SOLARIS_ALT_SECTOR_GUID },
444 	{ 0, "Solaris/illumos Reserved",	SOLARIS_RESERVED1_GUID },
445 	{ 0, "Solaris/illumos Reserved",	SOLARIS_RESERVED2_GUID },
446 	{ 0, "Solaris/illumos Reserved",	SOLARIS_RESERVED3_GUID },
447 	{ 0, "Solaris/illumos Reserved",	SOLARIS_RESERVED4_GUID },
448 	{ 0, "Solaris/illumos Reserved",	SOLARIS_RESERVED5_GUID },
449 
450 	/* NetBSD */
451 	{ 0, "NetBSD Swap",			NETBSD_SWAP_GUID },
452 	{ 0, "NetBSD LFS",			NETBSD_LFS_GUID },
453 	{ 0, "NetBSD RAID",			NETBSD_RAID_GUID },
454 	{ 0, "NetBSD Concatenated",		NETBSD_CONCATENATED_GUID },
455 	{ 0, "NetBSD Encrypted",		NETBSD_ENCRYPTED_GUID },
456 
457 	/* ChromeOS */
458 	{ 0, "ChromeOS rootfs",			CHROMEOS_ROOTFS_GUID },
459 	{ 0, "ChromeOS firmware",		CHROMEOS_FIRMWARE_GUID },
460 	{ 0, "ChromeOS future use",		CHROMEOS_FUTURE_USE_GUID },
461 	{ 0, "ChromeOS miniOS",			CHROMEOS_MINIOS_GUID },
462 	{ 0, "ChromeOS hibernate",		CHROMEOS_HIBERNATE_GUID },
463 
464 	/* Container Linux by CoreOS */
465 	{ 0, "CoreOS /usr",			COREOS_USR_GUID },
466 	{ 0, "CoreOS Resizable root",		COREOS_RESIZABLE_ROOT_GUID },
467 	{ 0, "CoreOS OEM",			COREOS_OEM_GUID },
468 	{ 0, "CoreOS root RAID",		COREOS_ROOT_RAID_GUID },
469 
470 	/* MidnightBSD */
471 	{ 0, "MidnightBSD Boot",		MIDNIGHTBSD_BOOT_GUID },
472 	{ 0, "MidnightBSD Data",		MIDNIGHTBSD_DATA_GUID },
473 	{ 0, "MidnightBSD Swap",		MIDNIGHTBSD_SWAP_GUID },
474 	{ 0, "MidnightBSD UFS",			MIDNIGHTBSD_UFS_GUID },
475 	{ 0, "MidnightBSD Vinum vol mgr",	MIDNIGHTBSD_VINUM_VOL_MGR_GUID },
476 	{ 0, "MidnightBSD ZFS",			MIDNIGHTBSD_ZFS_GUID },
477 
478 	/* Ceph */
479 	{ 0, "Ceph Journal",			CEPH_JOURNAL_GUID },
480 	{ 0, "Ceph dm-crypt journal",		CEPH_DMCRYPT_JOURNAL_GUID },
481 	{ 0, "Ceph OSD",			CEPH_OSD_GUID },
482 	{ 0, "Ceph dm-crypt OSD",		CEPH_DMCRYPT_OSD_GUID },
483 	{ 0, "Ceph Disk in creation",		CEPH_DISK_IN_CREATION_GUID },
484 	{ 0, "Ceph dm-crypt disk in creation",	CEPH_DMCRYPT_CREATNG_DISK_GUID },
485 	{ 0, "Ceph Block",			CEPH_BLOCK_GUID },
486 	{ 0, "Ceph Block DB",			CEPH_BLOCK_DB_GUID },
487 	{ 0, "Ceph Block write-ahead log",	CEPH_BLOCK_LOG_GUID },
488 	{ 0, "Ceph Lockbox for dm-crypt keys",	CEPH_DMCRYPT_KEYS_LOCKBOX_GUID },
489 	{ 0, "Ceph Multipath OSD",		CEPH_MULTIPATH_OSD_GUID },
490 	{ 0, "Ceph Multipath journal",		CEPH_MULTIPATH_JOURNAL_GUID },
491 	{ 0, "Ceph Multipath block A",		CEPH_MULTIPATH_BLOCK_A_GUID },
492 	{ 0, "Ceph Multipath block B",		CEPH_MULTIPATH_BLOCK_B_GUID },
493 	{ 0, "Ceph Multipath block DB",		CEPH_MULTIPATH_BLOCK_DB_GUID },
494 	{ 0, "Ceph Multipath block log",	CEPH_MULTIPATH_BLOCK_LOG_GUID },
495 	{ 0, "Ceph dm-crypt block",		CEPH_DMCRYPT_BLOCK_GUID },
496 	{ 0, "Ceph dm-crypt block DB",		CEPH_DMCRYPT_BLOCK_DB_GUID },
497 	{ 0, "Ceph dm-crypt block log",		CEPH_DMCRYPT_BLOCK_LOG_GUID },
498 	{ 0, "Ceph dm-crypt LUKS journal",	CEPH_DMCRYPT_LUKS_JOURNAL_GUID },
499 	{ 0, "Ceph dm-crypt LUKS block",	CEPH_DMCRYPT_LUKS_BLOCK_GUID },
500 	{ 0, "Ceph dm-crypt LUKS block DB",	CEPH_DMCRYPT_LUKS_BLOCK_DB_GUID },
501 	{ 0, "Ceph dm-crypt LUKS log",		CEPH_DMCRYPT_LUKS_LOG_GUID },
502 	{ 0, "Ceph dm-crypt LUKS OSD",		CEPH_DMCRYPT_LUKS_OSD_GUID },
503 
504 	/* QNX */
505 	{ 0, "QNX (6) Power-safe file system",	QNX_POWER_SAFE_FS_GUID },
506 
507 	/* Plan 9 */
508 	{ 0, "Plan9",				PLAN9_GUID },
509 
510 	/* VMWare ESX */
511 	{ 0, "VMWare vmkcore (coredump)",	VMWARE_VMKCORE_GUID },
512 	{ 0, "VMWare VMFS filesystem",		VMWARE_VMFS_FILESYSTEM_GUID },
513 	{ 0, "VMWare Reserved",			VMWARE_RESERVED_GUID },
514 
515 	/* Android-IA */
516 	{ 0, "Android-IA Bootloader",		ANDROID_IA_BOOT_GUID },
517 	{ 0, "Android-IA Bootloader2",		ANDROID_IA_BOOT2_GUID },
518 	{ 0, "Android-IA Boot",			ANDROID_IA_BOOT3_GUID },
519 	{ 0, "Android-IA Recovery",		ANDROID_IA_RECOVERY_GUID },
520 	{ 0, "Android-IA Misc",			ANDROID_IA_MISC_GUID },
521 	{ 0, "Android-IA Metadata",		ANDROID_IA_METADATA_GUID },
522 	{ 0, "Android-IA System",		ANDROID_IA_SYSTEM_GUID },
523 	{ 0, "Android-IA Cache",		ANDROID_IA_CACHE_GUID },
524 	{ 0, "Android-IA Data",			ANDROID_IA_DATA_GUID },
525 	{ 0, "Android-IA Persistent",		ANDROID_IA_PERSISTENT_GUID },
526 	{ 0, "Android-IA Vendor",		ANDROID_IA_VENDOR_GUID },
527 	{ 0, "Android-IA Config",		ANDROID_IA_CFG_GUID },
528 	{ 0, "Android-IA Factory",		ANDROID_IA_FACTORY_GUID },
529 	{ 0, "Android-IA Factory (alt)",	ANDROID_IA_FACTORY_ALT_GUID },
530 	{ 0, "Android-IA Fastboot/Tertiary",	ANDROID_IA_FASTBOOT_GUID },
531 	{ 0, "Android-IA OEM",			ANDROID_IA_OEM_GUID },
532 
533 	/* Android 6.0+ ARM */
534 	{ 0, "Android 6.0+ ARM EXT",		ANDROID_6_0_ARM_EXT_GUID },
535 	{ 0, "Android 6.0+ ARM Meta",		ANDROID_6_0_ARM_META_GUID },
536 
537 	/* ONIE */
538 	{ 0, "ONIE Boot",			ONIE_BOOT_GUID },
539 	{ 0, "ONIE Config",			ONIE_CFG_GUID },
540 
541 	/* PowerPC */
542 	{ 0, "PPC PReP boot",			PPC_PREP_BOOT_GUID },
543 
544 	/* Atari TOS */
545 	{ 0, "Atari TOS Basic data",		ATARI_TOS_BASIC_DATA_GUID },
546 
547 	/* VeraCrypt */
548 	{ 0, "Veracrypt Encrypted data",	VERACRYPT_ENCRYPTED_DATA_GUID },
549 
550 	/* ArcaOS (a.k.a. OS/2) */
551 	{ 0, "ArcaOS Type 1 (OS/2)",		ARCAOS_TYPE_1_GUID },
552 
553 	/* Storage Performance Development Kit (SPDK) */
554 	{ 0, "SPDK block device",		SPDK_BLOCK_DEVICE_GUID },
555 
556 	/* barebox bootloader */
557 	{ 0, "Barebox barebox-state",		BAREBOX_BAREBOX_STATE_GUID },
558 
559 	/* U-Boot bootloader */
560 	{ 0, "U-Boot environment",		U_BOOT_ENVIRONMENT_GUID },
561 
562 	/* SoftRAID (?) */
563 	{ 0, "Softraid Status",			SOFTRAID_STATUS_GUID },
564 	{ 0, "Softraid Scratch",		SOFTRAID_SCRATCH_GUID },
565 	{ 0, "Softraid Volume",			SOFTRAID_VOLUME_GUID },
566 	{ 0, "Softraid Cache",			SOFTRAID_CACHE_GUID },
567 
568 	/* Fuchsia standard partitions */
569 	{ 0, "Fuchsia bootloader (ABR)",	FUCHSIA_BOOT_GUID },
570 	{ 0, "Fuchsia encrypted system data",	FUCHSIA_ENCRYPTED_SYS_GUID },
571 	{ 0, "Fucshia boot metadata (ABR)",	FUCSHIA_BOOT_METADATA_GUID },
572 	{ 0, "Fuchsia RO Factory system data",	FUCHSIA_RO_FACTORY_SYS_GUID },
573 	{ 0, "Fuchsia RO Factory boot data",	FUCHSIA_RO_FACTORY_BOOT_GUID },
574 	{ 0, "Fuchsia Volume Manager",		FUCHSIA_VOLUME_MANAGER_GUID },
575 	{ 0, "Fuchsia Verified boot meta (ABR)",FUCHSIA_VERIFIED_BOOT_META_GUID },
576 	{ 0, "Fuchsia Zircon boot (ABR)",	FUCHSIA_ZIRCON_BOOT_ABR_GUID },
577 
578 	/* Fuchsia legacy partitions */
579 	{ 0, "Fuchsia Legacy system",		FUCHSIA_L_SYSTEM_GUID },
580 	{ 0, "Fuchsia Legacy data",		FUCHSIA_L_DATA_GUID },
581 	{ 0, "Fuchsia Legacy install",		FUCHSIA_L_INSTALL_GUID },
582 	{ 0, "Fuchsia Legacy blob",		FUCHSIA_L_BLOB_GUID },
583 	{ 0, "Fuchsia Legacy fvm",		FUCHSIA_L_FVM_GUID },
584 	{ 0, "Fuchsia Legacy Zircon boot (A)",	FUCHSIA_L_ZIRCON_BOOTA_GUID },
585 	{ 0, "Fuchsia Legacy Zircon boot (B)",	FUCHSIA_L_ZIRCON_BOOTB_GUID },
586 	{ 0, "Fuchsia Legacy Zircon boot (R)",	FUCHSIA_L_ZIRCON_BOOTR_GUID },
587 	{ 0, "Fuchsia Legacy sys-config",	FUCHSIA_L_SYS_CFG_GUID },
588 	{ 0, "Fuchsia Legacy factory-config",	FUCHSIA_L_FACTORY_CFG_GUID },
589 	{ 0, "Fuchsia Legacy bootloader",	FUCHSIA_L_BOOT_GUID },
590 	{ 0, "Fuchsia Legacy guid-test",	FUCHSIA_L_TEST_GUID },
591 	{ 0, "Fuchsia Legacy Verified boot mA", FUCHSIA_L_VERIFIED_BOOTM_A_GUID },
592 	{ 0, "Fuchsia Legacy Verified boot mB", FUCHSIA_L_VERIFIED_BOOTM_B_GUID },
593 	{ 0, "Fuchsia Legacy Verified boot mR", FUCHSIA_L_VERIFIED_BOOTM_R_GUID },
594 	{ 0, "Fuchsia Legacy misc",		FUCHSIA_L_MISC_GUID },
595 	{ 0, "Fuchsia Legacy emmc-boot1",	FUCHSIA_L_EMMC_BOOT1_GUID },
596 	{ 0, "Fuchsia Legacy emmc-boot2",	FUCHSIA_L_EMMC_BOOT2_GUID },
597 
598 	/* Systemd Discoverable Partitions */
599 	{ 0, "Linux x86-64 root verity",	LINUX_X86_64_ROOT_VERITY_GUID },
600 	{ 0, "Linux IA-64 /usr",		LINUX_IA_64_USR_GUID },
601 	{ 0, "Linux /var",			LINUX_VAR_GUID },
602 	{ 0, "Linux Arm64 /usr verity",		LINUX_ARM64_USR_VERITY_GUID },
603 	{ 0, "Linux Arm32 root verity",		LINUX_ARM32_ROOT_VERITY_GUID },
604 	{ 0, "Linux user's home",		LINUX_USER_HOME_GUID },
605 	{ 0, "Linux x86-64 /usr verity",	LINUX_X86_64_USR_VERITY_GUID },
606 	{ 0, "Linux Arm32 /usr",		LINUX_ARM32_USR_GUID },
607 	{ 0, "Linux /var/tmp",			LINUX_VAR_TMP_GUID },
608 	{ 0, "Linux IA-64 /usr verity",		LINUX_IA_64_USR_VERITY_GUID },
609 	{ 0, "Linux x86 /usr",			LINUX_X86_USR_GUID },
610 	{ 0, "Linux x86-64 /usr",		LINUX_X86_64_USR_GUID },
611 	{ 0, "Linux IA-64 root verity",		LINUX_IA_64_ROOT_VERITY_GUID },
612 	{ 0, "Linux x86 /usr verity",		LINUX_X86_USR_VERITY_GUID },
613 	{ 0, "Linux IA-64 root",		LINUX_IA_64_ROOT_GUID },
614 	{ 0, "Linux Arm64 /usr",		LINUX_ARM64_USR_GUID },
615 	{ 0, "Linux Arm32 /usr verity",		LINUX_ARM32_USR_VERITY_GUID },
616 	{ 0, "Linux x86 root verity",		LINUX_X86_ROOT_VERITY_GUID },
617 	{ 0, "Linux Arm64 root verity",		LINUX_ARM64_ROOT_VERITY_GUID },
618 #endif
619 };
620 
621 struct menu_item {
622 	int	 mi_menuid;	/* Unique hex octet */
623 	int	 mi_mbrid;	/* -1 == not on MBR menu */
624 	char 	*mi_name;	/* Truncated at 14 chars */
625 	char	*mi_guid;	/* NULL == not on GPT menu */
626 };
627 
628 const struct menu_item menu_items[] = {
629 	{ 0x00,	0x00,	"Unused",	UNUSED_GUID },
630 	{ 0x01,	0x01,	"DOS FAT-12",	MICROSOFT_BASIC_DATA_GUID },
631 	{ 0x02,	0x02,	"XENIX /",	NULL },
632 	{ 0x03,	0x03,	"XENIX /usr",	NULL },
633 	{ 0x04,	0x04,	"DOS FAT-16",	MICROSOFT_BASIC_DATA_GUID },
634 	{ 0x05,	0x05,	"Extended DOS",	NULL },
635 	{ 0x06,	0x06,	"DOS > 32MB",	MICROSOFT_BASIC_DATA_GUID },
636 	{ 0x07,	0x07,	"NTFS",		MICROSOFT_BASIC_DATA_GUID },
637 	{ 0x08,	0x08,	"AIX fs",	NULL },
638 	{ 0x09,	0x09,	"AIX/Coherent",	NULL },
639 	{ 0x0A,	0x0A,	"OS/2 Bootmgr",	NULL },
640 	{ 0x0B,	0x0B,	"Win95 FAT-32",	MICROSOFT_BASIC_DATA_GUID },
641 	{ 0x0C,	0x0C,	"Win95 FAT32L",	MICROSOFT_BASIC_DATA_GUID },
642 	{ 0x0D,   -1,	"BIOS boot",	BIOS_BOOT_GUID },
643 	{ 0x0E,	0x0E,	"DOS FAT-16",	MICROSOFT_BASIC_DATA_GUID },
644 	{ 0x0F,	0x0F,	"Extended LBA",	NULL },
645 	{ 0x10,	0x10,	"OPUS",		NULL },
646 	{ 0x11,	0x11,	"OS/2 hidden",	MICROSOFT_BASIC_DATA_GUID },
647 	{ 0x12,	0x12,	"Compaq Diag",	NULL },
648 	{ 0x14,	0x14,	"OS/2 hidden",	MICROSOFT_BASIC_DATA_GUID },
649 	{ 0x16,	0x16,	"OS/2 hidden",	MICROSOFT_BASIC_DATA_GUID },
650 	{ 0x17,	0x17,	"OS/2 hidden",	MICROSOFT_BASIC_DATA_GUID },
651 	{ 0x18,	0x18,	"AST swap",	NULL },
652 	{ 0x19,	0x19,	"Willowtech",	NULL },
653 	{ 0x1C,	0x1C,	"ThinkPad Rec",	MICROSOFT_BASIC_DATA_GUID },
654 	{ 0x20,	0x20,	"Willowsoft",	NULL },
655 	{ 0x24,	0x24,	"NEC DOS",	NULL },
656 	{ 0x27,	0x27,	"Win Recovery",	WIN_RECOVERY_GUID },
657 	{ 0x38,	0x38,	"Theos",	NULL },
658 	{ 0x39,	0x39,	"Plan 9",	NULL },
659 	{ 0x40,	0x40,	"VENIX 286",	NULL },
660 	{ 0x41,	0x41,	"Lin/Minux DR",	NULL },
661 	{ 0x42,	0x42,	"LinuxSwap DR",	LINUXSWAP_DR_GUID },
662 	{ 0x43,	0x43,	"Linux DR",	NULL },
663 	{ 0x4D,	0x4D,	"QNX 4.2 Pri",	NULL },
664 	{ 0x4E,	0x4E,	"QNX 4.2 Sec",	NULL },
665 	{ 0x4F,	0x4F,	"QNX 4.2 Ter",	NULL },
666 	{ 0x50,	0x50,	"DM",		NULL },
667 	{ 0x51,	0x51,	"DM",		NULL },
668 	{ 0x52,	0x52,	"CP/M or SysV",	NULL },
669 	{ 0x53,	0x53,	"DM",		NULL },
670 	{ 0x54,	0x54,	"Ontrack",	NULL },
671 	{ 0x55,	0x55,	"EZ-Drive",	NULL },
672 	{ 0x56,	0x56,	"Golden Bow",	NULL },
673 	{ 0x5C,	0x5C,	"Priam"	,	NULL },
674 	{ 0x61,	0x61,	"SpeedStor",	NULL },
675 	{ 0x63,	0x63,	"ISC, HURD, *",	NULL },
676 	{ 0x64,	0x64,	"NetWare 2.xx",	NULL },
677 	{ 0x65,	0x65,	"NetWare 3.xx",	NULL },
678 	{ 0x66,	0x66,	"NetWare 386",	NULL },
679 	{ 0x67,	0x67,	"Novell",	NULL },
680 	{ 0x68,	0x68,	"Novell",	NULL },
681 	{ 0x69,	0x69,	"Novell",	NULL },
682 	{ 0x70,	0x70,	"DiskSecure",	NULL },
683 	{ 0x75,	0x75,	"PCIX",		NULL },
684 	{ 0x7F,   -1,	"Chrome Kernel",CHROME_KERNEL_GUID },
685 	{ 0x80, 0x80,	"Minix (old)",	NULL },
686 	{ 0x81, 0x81,	"Minix (new)",	NULL },
687 	{ 0x82,	0x82,	"Linux swap",	LINUX_SWAP_GUID },
688 	{ 0x83,	0x83,	"Linux files*",	LINUX_FILES_GUID },
689 	{ 0x84,	0x84,	"OS/2 hidden",	NULL },
690 	{ 0x85,	0x85,	"Linux ext.",	NULL },
691 	{ 0x86, 0x86,	"NT FAT VS",	NULL },
692 	{ 0x87, 0x87,	"NTFS VS",	NULL },
693 	{ 0x8E,	0x8E,	"Linux LVM",	LINUX_LVM_GUID },
694 	{ 0x93,	0x93,	"Amoeba FS",	NULL },
695 	{ 0x94,	0x94,	"Amoeba BBT",	NULL },
696 	{ 0x99,	0x99,	"Mylex"	,	NULL },
697 	{ 0x9F,	0x9F,	"BSDI",		NULL },
698 	{ 0xA0,	0xA0,	"NotebookSave",	NULL },
699 	{ 0xA5,	0xA5,	"FreeBSD",	FREEBSD_GUID },
700 	{ 0xA6,	0xA6,	"OpenBSD",	OPENBSD_GUID },
701 	{ 0xA7,	0xA7,	"NeXTSTEP",	NULL },
702 	{ 0xA8,	0xA8,	"MacOS X",	MACOS_X_GUID },
703 	{ 0xA9,	0xA9,	"NetBSD",	NETBSD_GUID },
704 	{ 0xAB,	0xAB,	"MacOS X boot",	MACOS_X_BOOT_GUID },
705 	{ 0xAF,	0xAF,	"MacOS X HFS+",	MACOS_X_HFS_GUID },
706 	{ 0xB0,	  -1,	"APFS",		APFS_GUID },
707 	{ 0xB1,	  -1,	"APFS ISC",	APFS_ISC_GUID },
708 	{ 0xB2,	  -1,	"APFS Recovery",APFS_RECOVERY_GUID },
709 	{ 0xB3,	  -1,	"HiFive FSBL",	HIFIVE_FSBL_GUID },
710 	{ 0xB4,	  -1,	"HiFive BBL",	HIFIVE_BBL_GUID },
711 	{ 0xB7,	0xB7,	"BSDI filesy*",	NULL },
712 	{ 0xB8,	0xB8,	"BSDI swap",	NULL },
713 	{ 0xBF,	0xBF,	"Solaris",	SOLARIS_GUID },
714 	{ 0xC0,	0xC0,	"CTOS",		NULL },
715 	{ 0xC1,	0xC1,	"DRDOSs FAT12",	NULL },
716 	{ 0xC4,	0xC4,	"DRDOSs < 32M",	NULL },
717 	{ 0xC6,	0xC6,	"DRDOSs >=32M",	NULL },
718 	{ 0xC7,	0xC7,	"HPFS Disbled",	NULL },
719 	{ 0xDB,	0xDB,	"CPM/C.DOS/C*",	NULL },
720 	{ 0xDE,	0xDE,	"Dell Maint",	NULL },
721 	{ 0xE1,	0xE1,	"SpeedStor",	NULL },
722 	{ 0xE3,	0xE3,	"SpeedStor",	NULL },
723 	{ 0xE4,	0xE4,	"SpeedStor",	NULL },
724 	{ 0xEB,	0xEB,	"BeOS/i386",	BEOS_I386_GUID },
725 	{ 0xEC,	  -1,	"Legacy MBR",	LEGACY_MBR_GUID },
726 	{ 0xEE,	0xEE,	"EFI GPT",	NULL },
727 	{ 0xEF,	0xEF,	"EFI Sys",	EFI_SYSTEM_PARTITION_GUID },
728 	{ 0xF1,	0xF1,	"SpeedStor",	NULL },
729 	{ 0xF2,	0xF2,	"DOS 3.3+ Sec",	NULL },
730 	{ 0xF4,	0xF4,	"SpeedStor",	NULL },
731 	{ 0xFF,	0xFF,	"Xenix BBT",	NULL },
732 };
733 
734 void			 chs_to_dp(const unsigned char, const struct chs *,
735     uint8_t *, uint8_t *, uint8_t *);
736 const struct gpt_type	*find_gpt_type(const struct uuid *);
737 const struct menu_item	*find_gpt_menuitem(const struct gpt_type *);
738 const char		*find_gpt_desc(const struct gpt_type *);
739 int			 gpt_item(const unsigned int);
740 
741 const struct mbr_type	*find_mbr_type(const int);
742 const struct menu_item	*find_mbr_menuitem(const struct mbr_type *);
743 const char		*find_mbr_desc(const struct mbr_type *);
744 int			 mbr_item(const unsigned int);
745 
746 void			 print_menu(int (*)(const unsigned int),
747     const unsigned int);
748 int			 nth_menu_item(int (*)(const unsigned int),
749     const unsigned int, unsigned int);
750 
751 void
chs_to_dp(const unsigned char prt_id,const struct chs * chs,uint8_t * dp_cyl,uint8_t * dp_hd,uint8_t * dp_sect)752 chs_to_dp(const unsigned char prt_id, const struct chs *chs, uint8_t *dp_cyl,
753     uint8_t *dp_hd, uint8_t *dp_sect)
754 {
755 	uint64_t		cyl = chs->chs_cyl;
756 	uint32_t		head = chs->chs_head;
757 	uint32_t		sect = chs->chs_sect;
758 
759 	if (head > 254 || sect > 63 || cyl > 1023) {
760 		/* Set max values to trigger LBA. */
761 		head = (prt_id == DOSPTYP_EFI) ? 255 : 254;
762 		sect = 63;
763 		cyl = 1023;
764 	}
765 
766 	*dp_hd = head & 0xFF;
767 	*dp_sect = (sect & 0x3F) | ((cyl & 0x300) >> 2);
768 	*dp_cyl = cyl & 0xFF;
769 }
770 
771 const struct gpt_type *
find_gpt_type(const struct uuid * uuid)772 find_gpt_type(const struct uuid *uuid)
773 {
774 	const struct gpt_type	*gt = NULL;
775 	char			*guid;
776 	unsigned int		 i;
777 	uint32_t		 status;
778 
779 	if (uuid != NULL) {
780 		uuid_to_string(uuid, &guid, &status);
781 		if (status == uuid_s_ok) {
782 			for (i = 0; i < nitems(gpt_types) && gt == NULL; i++) {
783 				if (strcasecmp(gpt_types[i].gt_guid, guid) == 0)
784 					gt = &gpt_types[i];
785 			}
786 		}
787 		free(guid);
788 	}
789 
790 	return gt;
791 }
792 
793 const struct menu_item *
find_gpt_menuitem(const struct gpt_type * gt)794 find_gpt_menuitem(const struct gpt_type *gt)
795 {
796 	unsigned int		i;
797 
798 	if (gt != NULL) {
799 		for (i = 0; i < nitems(menu_items); i++) {
800 			if (gpt_item(i) == 0 &&
801 			    strcasecmp(menu_items[i].mi_guid, gt->gt_guid) == 0)
802 				return &menu_items[i];
803 		}
804 	}
805 
806 	return NULL;
807 }
808 
809 const char *
find_gpt_desc(const struct gpt_type * gt)810 find_gpt_desc(const struct gpt_type *gt)
811 {
812 	const struct menu_item	*mi;
813 
814 	if (gt != NULL) {
815 		if (gt->gt_desc != NULL)
816 			return gt->gt_desc;
817 		mi = find_gpt_menuitem(gt);
818 		if (mi)
819 			return mi->mi_name;
820 	}
821 
822 	return NULL;
823 }
824 
825 int
gpt_item(const unsigned int item)826 gpt_item(const unsigned int item)
827 {
828 	return menu_items[item].mi_guid == NULL;
829 }
830 
831 const struct mbr_type *
find_mbr_type(const int id)832 find_mbr_type(const int id)
833 {
834 	const struct mbr_type	*mt = NULL;
835 	unsigned int		 i;
836 
837 	if (id >= 0) {
838 		for (i = 0; i < nitems(mbr_types); i++) {
839 			if (mbr_types[i].mt_type == id)
840 				mt = &mbr_types[i];
841 		}
842 	}
843 
844 	return mt;
845 }
846 
847 const struct menu_item *
find_mbr_menuitem(const struct mbr_type * mt)848 find_mbr_menuitem(const struct mbr_type *mt)
849 {
850 	unsigned int		i;
851 
852 	if (mt != NULL) {
853 		for (i = 0; i < nitems(menu_items); i++) {
854 			if (mbr_item(i) == 0
855 			    && menu_items[i].mi_mbrid == mt->mt_type)
856 				return &menu_items[i];
857 		}
858 	}
859 
860 	return NULL;
861 }
862 
863 const char *
find_mbr_desc(const struct mbr_type * mt)864 find_mbr_desc(const struct mbr_type *mt)
865 {
866 	const struct menu_item	*mi;
867 
868 	if (mt != NULL) {
869 		if (mt->mt_desc != NULL)
870 			return mt->mt_desc;
871 		mi = find_mbr_menuitem(mt);
872 		if (mi)
873 			return mi->mi_name;
874 	}
875 
876 	return NULL;
877 }
878 
879 int
mbr_item(const unsigned int item)880 mbr_item(const unsigned int item)
881 {
882 	return menu_items[item].mi_mbrid == -1;
883 }
884 
885 void
print_menu(int (* test)(const unsigned int),const unsigned int columns)886 print_menu(int (*test)(const unsigned int), const unsigned int columns)
887 {
888 	int			 col, col0;
889 	unsigned int		 count, i, j, rows;
890 
891 	count = 0;
892 	for (i = 0; i < nitems(menu_items); i++)
893 		if (test(i) == 0)
894 			count++;
895 	rows = (count + columns - 1) / columns;
896 
897 	col0 = -1;
898 	for (i = 0; i < rows; i++) {
899 		col0 = nth_menu_item(test, col0, 1);
900 		printf("%02X %-15s", menu_items[col0].mi_menuid,
901 		    menu_items[col0].mi_name);
902 		for (j = 1; j < columns; j++) {
903 			col = nth_menu_item(test, col0, j * rows);
904 			if (col == -1)
905 				break;
906 			printf("%02X %-15s", menu_items[col].mi_menuid,
907 			    menu_items[col].mi_name);
908 		}
909 		printf("\n");
910 	}
911 }
912 
913 int
nth_menu_item(int (* test)(const unsigned int),const unsigned int last,unsigned int n)914 nth_menu_item(int (*test)(const unsigned int), const unsigned int last,
915     unsigned int n)
916 {
917 	unsigned int			i;
918 
919 	for (i = last + 1; i < nitems(menu_items); i++) {
920 		if (test(i) == 0) {
921 			n--;
922 			if (n == 0)
923 				return i;
924 		}
925 	}
926 
927 	return -1;
928 }
929 
930 int
PRT_protected_uuid(const struct uuid * uuid)931 PRT_protected_uuid(const struct uuid *uuid)
932 {
933 	const struct gpt_type	*gt;
934 	unsigned int		 pn;
935 
936 	gt = find_gpt_type(uuid);
937 	if (gt == NULL)
938 		return 0;	/* We don't know this type, so no protection. */
939 	if (gt->gt_attr & GTATTR_PROTECT)
940 		return 1;	/* Protected! */
941 	if (strcasecmp(gt->gt_guid, EFI_SYSTEM_PARTITION_GUID))
942 		return 0;	/* Not EFI Sys, so not protected. */
943 
944 	for (pn = 0; pn < gh.gh_part_num; pn++) {
945 		gt = find_gpt_type(&gp[pn].gp_type);
946 		if (gt && (gt->gt_attr & GTATTR_PROTECT_EFISYS))
947 			return 1;	/* EFI Sys must be protected! */
948 	}
949 
950 	return 0;
951 }
952 
953 void
PRT_print_mbrmenu(char * lbuf,size_t lbuflen)954 PRT_print_mbrmenu(char *lbuf, size_t lbuflen)
955 {
956 #define	MBR_MENU_COLUMNS	4
957 
958 	printf("Choose from the following Partition id values:\n");
959 	print_menu(mbr_item,  MBR_MENU_COLUMNS);
960 
961 	memset(lbuf, 0, lbuflen);	/* Just continue. */
962 }
963 
964 void
PRT_print_gptmenu(char * lbuf,size_t lbuflen)965 PRT_print_gptmenu(char *lbuf, size_t lbuflen)
966 {
967 #define	GPT_MENU_COLUMNS	4
968 
969 	printf("Choose from the following Partition id values:\n");
970 	print_menu(gpt_item, GPT_MENU_COLUMNS);
971 
972 	memset(lbuf, 0, lbuflen);	/* Just continue. */
973 }
974 
975 void
PRT_dp_to_prt(const struct dos_partition * dp,const uint64_t lba_self,const uint64_t lba_firstembr,struct prt * prt)976 PRT_dp_to_prt(const struct dos_partition *dp, const uint64_t lba_self,
977     const uint64_t lba_firstembr, struct prt *prt)
978 {
979 	off_t			off;
980 	uint32_t		t;
981 
982 	prt->prt_flag = dp->dp_flag;
983 	prt->prt_id = dp->dp_typ;
984 
985 	if ((prt->prt_id == DOSPTYP_EXTEND) || (prt->prt_id == DOSPTYP_EXTENDL))
986 		off = lba_firstembr;
987 	else
988 		off = lba_self;
989 
990 	memcpy(&t, &dp->dp_start, sizeof(uint32_t));
991 	prt->prt_bs = letoh32(t) + off;
992 	memcpy(&t, &dp->dp_size, sizeof(uint32_t));
993 	prt->prt_ns = letoh32(t);
994 	if (prt->prt_id == DOSPTYP_EFI && prt->prt_ns == UINT32_MAX)
995 		prt->prt_ns = DL_GETDSIZE(&dl) - prt->prt_bs;
996 }
997 
998 void
PRT_prt_to_dp(const struct prt * prt,const uint64_t lba_self,const uint64_t lba_firstembr,struct dos_partition * dp)999 PRT_prt_to_dp(const struct prt *prt, const uint64_t lba_self,
1000     const uint64_t lba_firstembr, struct dos_partition *dp)
1001 {
1002 	struct chs		start, end;
1003 	uint64_t		off, t;
1004 
1005 	if (prt->prt_ns == 0 || prt->prt_id == DOSPTYP_UNUSED) {
1006 		memset(dp, 0, sizeof(*dp));
1007 		return;
1008 	}
1009 
1010 	if ((prt->prt_id == DOSPTYP_EXTEND) || (prt->prt_id == DOSPTYP_EXTENDL))
1011 		off = lba_firstembr;
1012 	else
1013 		off = lba_self;
1014 
1015 	PRT_lba_to_chs(prt, &start, &end);
1016 	chs_to_dp(prt->prt_id, &start, &dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect);
1017 	chs_to_dp(prt->prt_id, &end, &dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect);
1018 
1019 	dp->dp_flag = prt->prt_flag & 0xFF;
1020 	dp->dp_typ = prt->prt_id & 0xFF;
1021 
1022 	t = htole64(prt->prt_bs - off);
1023 	memcpy(&dp->dp_start, &t, sizeof(uint32_t));
1024 	if (prt->prt_id == DOSPTYP_EFI && (prt->prt_bs + prt->prt_ns) >
1025 	    DL_GETDSIZE(&dl))
1026 		t = htole64(UINT32_MAX);
1027 	else
1028 		t = htole64(prt->prt_ns);
1029 	memcpy(&dp->dp_size, &t, sizeof(uint32_t));
1030 }
1031 
1032 void
PRT_print_parthdr(void)1033 PRT_print_parthdr(void)
1034 {
1035 	printf("            Starting         Ending    "
1036 	    "     LBA Info:\n");
1037 	printf(" #: id      C   H   S -      C   H   S "
1038 	    "[       start:        size ]\n");
1039 	printf("---------------------------------------"
1040 	    "----------------------------------------\n");
1041 }
1042 
1043 void
PRT_print_part(const int num,const struct prt * prt,const char * units)1044 PRT_print_part(const int num, const struct prt *prt, const char *units)
1045 {
1046 	const struct unit_type	*ut;
1047 	const char		*desc;
1048 	struct chs		 start, end;
1049 	double			 size;
1050 
1051 	size = units_size(units, prt->prt_ns, &ut);
1052 	PRT_lba_to_chs(prt, &start, &end);
1053 	desc = find_mbr_desc(find_mbr_type(prt->prt_id));
1054 
1055 	printf("%c%1d: %.2X %6llu %3u %3u - %6llu %3u %3u [%12llu:%12.0f%s] "
1056 	    "%s\n", (prt->prt_flag == DOSACTIVE) ? '*' : ' ', num,
1057 	    prt->prt_id, start.chs_cyl, start.chs_head, start.chs_sect,
1058 	    end.chs_cyl, end.chs_head, end.chs_sect,
1059 	    prt->prt_bs, size, ut->ut_abbr, desc ? desc : "<Unknown ID>");
1060 
1061 	if (prt->prt_bs >= DL_GETDSIZE(&dl))
1062 		printf("partition %d starts beyond the end of %s\n", num,
1063 		    disk.dk_name);
1064 	else if (prt->prt_bs + prt->prt_ns > DL_GETDSIZE(&dl))
1065 		printf("partition %d extends beyond the end of %s\n", num,
1066 		    disk.dk_name);
1067 }
1068 
1069 void
PRT_lba_to_chs(const struct prt * prt,struct chs * start,struct chs * end)1070 PRT_lba_to_chs(const struct prt *prt, struct chs *start, struct chs *end)
1071 {
1072 	uint64_t		lba;
1073 
1074 	if (prt->prt_ns == 0 || prt->prt_id == DOSPTYP_UNUSED) {
1075 		memset(start, 0, sizeof(*start));
1076 		memset(end, 0, sizeof(*end));
1077 		return;
1078 	}
1079 
1080 	/*
1081 	 * C = LBA ÷ (HPC × SPT)
1082 	 * H = (LBA ÷ SPT) mod HPC
1083 	 * S = (LBA mod SPT) + 1
1084 	 */
1085 
1086 	lba = prt->prt_bs;
1087 	start->chs_cyl = lba / (disk.dk_sectors * disk.dk_heads);
1088 	start->chs_head = (lba / disk.dk_sectors) % disk.dk_heads;
1089 	start->chs_sect = (lba % disk.dk_sectors) + 1;
1090 
1091 	lba = prt->prt_bs + prt->prt_ns - 1;
1092 	end->chs_cyl = lba / (disk.dk_sectors * disk.dk_heads);
1093 	end->chs_head = (lba / disk.dk_sectors) % disk.dk_heads;
1094 	end->chs_sect = (lba % disk.dk_sectors) + 1;
1095 }
1096 
1097 const char *
PRT_uuid_to_desc(const struct uuid * uuid)1098 PRT_uuid_to_desc(const struct uuid *uuid)
1099 {
1100 	static char		 guid[UUID_STR_LEN + 1];
1101 	const char		*desc;
1102 	char			*str;
1103 	uint32_t		 status;
1104 
1105 	desc = find_gpt_desc(find_gpt_type(uuid));
1106 	if (desc != NULL)
1107 		return desc;
1108 
1109 	uuid_to_string(uuid, &str, &status);
1110 	if (status == uuid_s_ok)
1111 		strlcpy(guid, str, sizeof(guid));
1112 	else
1113 		strlcpy(guid, "<Bad UUID>", sizeof(guid));
1114 	free(str);
1115 
1116 	return guid;
1117 }
1118 
1119 char *
PRT_uuid_to_menudflt(const struct uuid * uuid)1120 PRT_uuid_to_menudflt(const struct uuid *uuid)
1121 {
1122 	const struct menu_item	*mi;
1123 	char			*dflt;
1124 	uint32_t		 status;
1125 
1126 	mi = find_gpt_menuitem(find_gpt_type(uuid));
1127 	if (mi == NULL) {
1128 		uuid_to_string(uuid, &dflt, &status);
1129 		if (status != uuid_s_ok)
1130 			return NULL;
1131 	} else if (asprintf(&dflt, "%02X", mi->mi_menuid) == -1) {
1132 		return NULL;
1133 	}
1134 
1135 	return dflt;
1136 }
1137 
1138 const char *
PRT_menuid_to_guid(const int menuid)1139 PRT_menuid_to_guid(const int menuid)
1140 {
1141 	unsigned int		i;
1142 
1143 	for (i = 0; i < nitems(menu_items); i++) {
1144 		if (gpt_item(i) == 0 && menu_items[i].mi_menuid == menuid)
1145 			return menu_items[i].mi_guid;
1146 	}
1147 
1148 	return NULL;
1149 }
1150