xref: /freebsd/usr.sbin/bsdinstall/partedit/partedit_x86.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
12118f387SNathan Whitehorn /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
42118f387SNathan Whitehorn  * Copyright (c) 2011 Nathan Whitehorn
52118f387SNathan Whitehorn  * All rights reserved.
62118f387SNathan Whitehorn  *
72118f387SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
82118f387SNathan Whitehorn  * modification, are permitted provided that the following conditions
92118f387SNathan Whitehorn  * are met:
102118f387SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
112118f387SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
122118f387SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
132118f387SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
142118f387SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
152118f387SNathan Whitehorn  *
162118f387SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172118f387SNathan Whitehorn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182118f387SNathan Whitehorn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192118f387SNathan Whitehorn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202118f387SNathan Whitehorn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212118f387SNathan Whitehorn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222118f387SNathan Whitehorn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232118f387SNathan Whitehorn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242118f387SNathan Whitehorn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252118f387SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262118f387SNathan Whitehorn  * SUCH DAMAGE.
272118f387SNathan Whitehorn  */
282118f387SNathan Whitehorn 
296b446ed5SNathan Whitehorn #include <sys/types.h>
306b446ed5SNathan Whitehorn #include <sys/sysctl.h>
312118f387SNathan Whitehorn #include <string.h>
322118f387SNathan Whitehorn 
332118f387SNathan Whitehorn #include "partedit.h"
342118f387SNathan Whitehorn 
354ca43ae5SEd Maste /* EFI partition size in bytes */
36db8b5613SRebecca Cran #define	EFI_BOOTPART_SIZE	(260 * 1024 * 1024)
374ca43ae5SEd Maste 
38be01d6e9SNathan Whitehorn static const char *
x86_bootmethod(void)39be01d6e9SNathan Whitehorn x86_bootmethod(void)
40be01d6e9SNathan Whitehorn {
41be01d6e9SNathan Whitehorn 	static char fw[255] = "";
42be01d6e9SNathan Whitehorn 	size_t len = sizeof(fw);
43be01d6e9SNathan Whitehorn 	int error;
44be01d6e9SNathan Whitehorn 
45be01d6e9SNathan Whitehorn 	if (strlen(fw) == 0) {
46be01d6e9SNathan Whitehorn 		error = sysctlbyname("machdep.bootmethod", fw, &len, NULL, -1);
47be01d6e9SNathan Whitehorn 		if (error != 0)
48be01d6e9SNathan Whitehorn 			return ("BIOS");
49be01d6e9SNathan Whitehorn 	}
50be01d6e9SNathan Whitehorn 
51be01d6e9SNathan Whitehorn 	return (fw);
52be01d6e9SNathan Whitehorn }
536b446ed5SNathan Whitehorn 
542118f387SNathan Whitehorn const char *
default_scheme(void)55403a3c8cSNathan Whitehorn default_scheme(void)
56403a3c8cSNathan Whitehorn {
5704e98a37SNathan Whitehorn 	if (strcmp(x86_bootmethod(), "UEFI") == 0)
582118f387SNathan Whitehorn 		return ("GPT");
5904e98a37SNathan Whitehorn 	else
6004e98a37SNathan Whitehorn 		return ("MBR");
612118f387SNathan Whitehorn }
622118f387SNathan Whitehorn 
632118f387SNathan Whitehorn int
is_scheme_bootable(const char * part_type)64be01d6e9SNathan Whitehorn is_scheme_bootable(const char *part_type)
65be01d6e9SNathan Whitehorn {
666b446ed5SNathan Whitehorn 
672118f387SNathan Whitehorn 	if (strcmp(part_type, "GPT") == 0)
682118f387SNathan Whitehorn 		return (1);
69be01d6e9SNathan Whitehorn 	if (strcmp(x86_bootmethod(), "BIOS") == 0) {
706b446ed5SNathan Whitehorn 		if (strcmp(part_type, "BSD") == 0)
716b446ed5SNathan Whitehorn 			return (1);
722118f387SNathan Whitehorn 		if (strcmp(part_type, "MBR") == 0)
732118f387SNathan Whitehorn 			return (1);
746b446ed5SNathan Whitehorn 	}
752118f387SNathan Whitehorn 
762118f387SNathan Whitehorn 	return (0);
772118f387SNathan Whitehorn }
782118f387SNathan Whitehorn 
796e15678aSNathan Whitehorn int
is_fs_bootable(const char * part_type,const char * fs)80be01d6e9SNathan Whitehorn is_fs_bootable(const char *part_type, const char *fs)
81be01d6e9SNathan Whitehorn {
826e15678aSNathan Whitehorn 
836e15678aSNathan Whitehorn 	if (strcmp(fs, "freebsd-ufs") == 0)
846e15678aSNathan Whitehorn 		return (1);
856e15678aSNathan Whitehorn 
86be01d6e9SNathan Whitehorn 	if (strcmp(fs, "freebsd-zfs") == 0 &&
87403a3c8cSNathan Whitehorn 	    strcmp(part_type, "GPT") == 0 &&
88be01d6e9SNathan Whitehorn 	    strcmp(x86_bootmethod(), "BIOS") == 0)
896e15678aSNathan Whitehorn 		return (1);
906e15678aSNathan Whitehorn 
916e15678aSNathan Whitehorn 	return (0);
926e15678aSNathan Whitehorn }
936e15678aSNathan Whitehorn 
942118f387SNathan Whitehorn size_t
bootpart_size(const char * scheme)95be01d6e9SNathan Whitehorn bootpart_size(const char *scheme)
96be01d6e9SNathan Whitehorn {
972118f387SNathan Whitehorn 
982118f387SNathan Whitehorn 	/* No partcode except for GPT */
996b446ed5SNathan Whitehorn 	if (strcmp(scheme, "GPT") != 0)
1006b446ed5SNathan Whitehorn 		return (0);
1016b446ed5SNathan Whitehorn 
102be01d6e9SNathan Whitehorn 	if (strcmp(x86_bootmethod(), "BIOS") == 0)
103963ae465SNathan Whitehorn 		return (512*1024);
1046b446ed5SNathan Whitehorn 	else
1054ca43ae5SEd Maste 		return (EFI_BOOTPART_SIZE);
1066b446ed5SNathan Whitehorn 
1072118f387SNathan Whitehorn 	return (0);
1082118f387SNathan Whitehorn }
1092118f387SNathan Whitehorn 
1102118f387SNathan Whitehorn const char *
bootpart_type(const char * scheme,const char ** mountpoint)11152f39da1SNathan Whitehorn bootpart_type(const char *scheme, const char **mountpoint)
112be01d6e9SNathan Whitehorn {
1136b446ed5SNathan Whitehorn 
1140b7472b3SNathan Whitehorn 	if (strcmp(x86_bootmethod(), "UEFI") == 0) {
1150b7472b3SNathan Whitehorn 		*mountpoint = "/boot/efi";
1166b446ed5SNathan Whitehorn 		return ("efi");
1170b7472b3SNathan Whitehorn 	}
1186b446ed5SNathan Whitehorn 
1196b446ed5SNathan Whitehorn 	return ("freebsd-boot");
1206b446ed5SNathan Whitehorn }
1216b446ed5SNathan Whitehorn 
1226b446ed5SNathan Whitehorn const char *
bootcode_path(const char * part_type)123be01d6e9SNathan Whitehorn bootcode_path(const char *part_type)
124be01d6e9SNathan Whitehorn {
125be01d6e9SNathan Whitehorn 
126be01d6e9SNathan Whitehorn 	if (strcmp(x86_bootmethod(), "UEFI") == 0)
1276b446ed5SNathan Whitehorn 		return (NULL);
1286b446ed5SNathan Whitehorn 
1292118f387SNathan Whitehorn 	if (strcmp(part_type, "GPT") == 0)
1302118f387SNathan Whitehorn 		return ("/boot/pmbr");
1312118f387SNathan Whitehorn 	if (strcmp(part_type, "MBR") == 0)
1322118f387SNathan Whitehorn 		return ("/boot/mbr");
1332118f387SNathan Whitehorn 	if (strcmp(part_type, "BSD") == 0)
1342118f387SNathan Whitehorn 		return ("/boot/boot");
1352118f387SNathan Whitehorn 
1362118f387SNathan Whitehorn 	return (NULL);
1372118f387SNathan Whitehorn }
1382118f387SNathan Whitehorn 
1392118f387SNathan Whitehorn const char *
partcode_path(const char * part_type,const char * fs_type)140be01d6e9SNathan Whitehorn partcode_path(const char *part_type, const char *fs_type)
141be01d6e9SNathan Whitehorn {
1426b446ed5SNathan Whitehorn 
143db8b5613SRebecca Cran 	if (strcmp(part_type, "GPT") == 0 && strcmp(x86_bootmethod(), "UEFI") != 0) {
144db8b5613SRebecca Cran 		if (strcmp(fs_type, "zfs") == 0)
1456e15678aSNathan Whitehorn 			return ("/boot/gptzfsboot");
1466b446ed5SNathan Whitehorn 		else
1472118f387SNathan Whitehorn 			return ("/boot/gptboot");
1486b446ed5SNathan Whitehorn 	}
1492118f387SNathan Whitehorn 
150db8b5613SRebecca Cran 	/* No partcode except for non-UEFI GPT */
1512118f387SNathan Whitehorn 	return (NULL);
1522118f387SNathan Whitehorn }
1532118f387SNathan Whitehorn 
154