xref: /freebsd/usr.sbin/bsdinstall/partedit/partedit_x86.c (revision db8b56134506840832bec2d1ce07b9e00d4d6d71)
12118f387SNathan Whitehorn /*-
21de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
292118f387SNathan Whitehorn  */
302118f387SNathan Whitehorn 
316b446ed5SNathan Whitehorn #include <sys/types.h>
326b446ed5SNathan Whitehorn #include <sys/sysctl.h>
332118f387SNathan Whitehorn #include <string.h>
342118f387SNathan Whitehorn 
352118f387SNathan Whitehorn #include "partedit.h"
362118f387SNathan Whitehorn 
374ca43ae5SEd Maste /* EFI partition size in bytes */
38*db8b5613SRebecca Cran #define	EFI_BOOTPART_SIZE	(260 * 1024 * 1024)
394ca43ae5SEd Maste 
40be01d6e9SNathan Whitehorn static const char *
41be01d6e9SNathan Whitehorn x86_bootmethod(void)
42be01d6e9SNathan Whitehorn {
43be01d6e9SNathan Whitehorn 	static char fw[255] = "";
44be01d6e9SNathan Whitehorn 	size_t len = sizeof(fw);
45be01d6e9SNathan Whitehorn 	int error;
46be01d6e9SNathan Whitehorn 
47be01d6e9SNathan Whitehorn 	if (strlen(fw) == 0) {
48be01d6e9SNathan Whitehorn 		error = sysctlbyname("machdep.bootmethod", fw, &len, NULL, -1);
49be01d6e9SNathan Whitehorn 		if (error != 0)
50be01d6e9SNathan Whitehorn 			return ("BIOS");
51be01d6e9SNathan Whitehorn 	}
52be01d6e9SNathan Whitehorn 
53be01d6e9SNathan Whitehorn 	return (fw);
54be01d6e9SNathan Whitehorn }
556b446ed5SNathan Whitehorn 
562118f387SNathan Whitehorn const char *
57403a3c8cSNathan Whitehorn default_scheme(void)
58403a3c8cSNathan Whitehorn {
5904e98a37SNathan Whitehorn 	if (strcmp(x86_bootmethod(), "UEFI") == 0)
602118f387SNathan Whitehorn 		return ("GPT");
6104e98a37SNathan Whitehorn 	else
6204e98a37SNathan Whitehorn 		return ("MBR");
632118f387SNathan Whitehorn }
642118f387SNathan Whitehorn 
652118f387SNathan Whitehorn int
66be01d6e9SNathan Whitehorn is_scheme_bootable(const char *part_type)
67be01d6e9SNathan Whitehorn {
686b446ed5SNathan Whitehorn 
692118f387SNathan Whitehorn 	if (strcmp(part_type, "GPT") == 0)
702118f387SNathan Whitehorn 		return (1);
71be01d6e9SNathan Whitehorn 	if (strcmp(x86_bootmethod(), "BIOS") == 0) {
726b446ed5SNathan Whitehorn 		if (strcmp(part_type, "BSD") == 0)
736b446ed5SNathan Whitehorn 			return (1);
742118f387SNathan Whitehorn 		if (strcmp(part_type, "MBR") == 0)
752118f387SNathan Whitehorn 			return (1);
766b446ed5SNathan Whitehorn 	}
772118f387SNathan Whitehorn 
782118f387SNathan Whitehorn 	return (0);
792118f387SNathan Whitehorn }
802118f387SNathan Whitehorn 
816e15678aSNathan Whitehorn int
82be01d6e9SNathan Whitehorn is_fs_bootable(const char *part_type, const char *fs)
83be01d6e9SNathan Whitehorn {
846e15678aSNathan Whitehorn 
856e15678aSNathan Whitehorn 	if (strcmp(fs, "freebsd-ufs") == 0)
866e15678aSNathan Whitehorn 		return (1);
876e15678aSNathan Whitehorn 
88be01d6e9SNathan Whitehorn 	if (strcmp(fs, "freebsd-zfs") == 0 &&
89403a3c8cSNathan Whitehorn 	    strcmp(part_type, "GPT") == 0 &&
90be01d6e9SNathan Whitehorn 	    strcmp(x86_bootmethod(), "BIOS") == 0)
916e15678aSNathan Whitehorn 		return (1);
926e15678aSNathan Whitehorn 
936e15678aSNathan Whitehorn 	return (0);
946e15678aSNathan Whitehorn }
956e15678aSNathan Whitehorn 
962118f387SNathan Whitehorn size_t
97be01d6e9SNathan Whitehorn bootpart_size(const char *scheme)
98be01d6e9SNathan Whitehorn {
992118f387SNathan Whitehorn 
1002118f387SNathan Whitehorn 	/* No partcode except for GPT */
1016b446ed5SNathan Whitehorn 	if (strcmp(scheme, "GPT") != 0)
1026b446ed5SNathan Whitehorn 		return (0);
1036b446ed5SNathan Whitehorn 
104be01d6e9SNathan Whitehorn 	if (strcmp(x86_bootmethod(), "BIOS") == 0)
105963ae465SNathan Whitehorn 		return (512*1024);
1066b446ed5SNathan Whitehorn 	else
1074ca43ae5SEd Maste 		return (EFI_BOOTPART_SIZE);
1086b446ed5SNathan Whitehorn 
1092118f387SNathan Whitehorn 	return (0);
1102118f387SNathan Whitehorn }
1112118f387SNathan Whitehorn 
1122118f387SNathan Whitehorn const char *
11352f39da1SNathan Whitehorn bootpart_type(const char *scheme, const char **mountpoint)
114be01d6e9SNathan Whitehorn {
1156b446ed5SNathan Whitehorn 
116be01d6e9SNathan Whitehorn 	if (strcmp(x86_bootmethod(), "UEFI") == 0)
1176b446ed5SNathan Whitehorn 		return ("efi");
1186b446ed5SNathan Whitehorn 
1196b446ed5SNathan Whitehorn 	return ("freebsd-boot");
1206b446ed5SNathan Whitehorn }
1216b446ed5SNathan Whitehorn 
1226b446ed5SNathan Whitehorn const char *
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 *
140be01d6e9SNathan Whitehorn partcode_path(const char *part_type, const char *fs_type)
141be01d6e9SNathan Whitehorn {
1426b446ed5SNathan Whitehorn 
143*db8b5613SRebecca Cran 	if (strcmp(part_type, "GPT") == 0 && strcmp(x86_bootmethod(), "UEFI") != 0) {
144*db8b5613SRebecca Cran 		if (strcmp(fs_type, "zfs") == 0)
1456e15678aSNathan Whitehorn 			return ("/boot/gptzfsboot");
1466b446ed5SNathan Whitehorn 		else
1472118f387SNathan Whitehorn 			return ("/boot/gptboot");
1486b446ed5SNathan Whitehorn 	}
1492118f387SNathan Whitehorn 
150*db8b5613SRebecca Cran 	/* No partcode except for non-UEFI GPT */
1512118f387SNathan Whitehorn 	return (NULL);
1522118f387SNathan Whitehorn }
1532118f387SNathan Whitehorn 
154